File: 3ddeskd.cpp

package info (click to toggle)
3ddesktop 0.2.8-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,096 kB
  • ctags: 686
  • sloc: cpp: 5,996; sh: 3,222; makefile: 78
file content (2288 lines) | stat: -rw-r--r-- 61,751 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
//  ----------------------------------------------------------
//  3d Desktop
//
//  Allows you to switch virtual desktops (viewports) in several
//  3-dimensional ways.
// 
//  I started with a program called "OpenGL cube demo" by Chris
//  Halsall.  It has changed quite dramatically since then but it was
//  a *huge* help to me.  This is my first OpenGL program.  It was
//  coded up starting around Feb/March 2002.  Please provide feedback,
//  fixes, improvements etc. to bard@systemtoolbox.com
//
//  ----------------------------------------------------------
//
//  Copyright (C) 2002 Brad Wasson <bard@systemtoolbox.com>
//
//  This file is part of 3ddesktop.
//
//  3ddesktop is free software; you can redistribute it and/or modify it
//  under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2, or (at your option)
//  any later version.
//
//  3ddesktop is distributed in the hope that it will be useful, but
//  WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with 3ddesktop; see the file COPYING.   If not, write to
//  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
//

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include <signal.h>

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <getopt.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
#include <sys/time.h>
#include <sys/resource.h>

#include "3ddesk.h"

#include "move.hpp"
#include "face.hpp"
#include "arrange.hpp"
#include "faceset.hpp"
#include "xutil.hpp"
#include "event.hpp"

#include "util.h"

#include "win.hpp"

#include "message.hpp"

// STL
using namespace std;
#include <iostream>
#include <list>

#include <GL/glx.h>
#include <GL/gl.h>   // OpenGL itself.
#include <GL/glu.h>  // GLU support library.
//#include <GL/glut.h> // GLUT support library.

#include <X11/extensions/xf86vmode.h>
#include <X11/keysym.h>


Config *cfg;

typedef unsigned char uchar;

// Some global variables.... 
int do_screenshot = 1;
int do_vpswitch = 1;



// Our display mode settings.
int Light_On = 1;
int Blend_On = 0;
int Texture_On = 1;
int Alpha_Add = 1;

int Curr_TexMode = 1;
char *TexModesStr[] = {"GL_DECAL","GL_MODULATE","GL_BLEND","GL_REPLACE"};
GLint TexModes[] = {GL_DECAL,GL_MODULATE,GL_BLEND,GL_REPLACE};



// delay between updates of the movements (in milliseconds)
long last_time;  // for movement timing


// time of last cbrenderscene
long last_renderscene_time = 0;
// time of last keypress 
float last_keypress = 0;
// time of next "ghost keypress" when having fun
float next_action = 0;

int goto_on_flag = 0;

int initiate_destroy = 0;   // starts the z offset zoom in

Bool disable_autoacquire = false;

Bool disable_random_fun = false;

desktop_coords_t  last_coords;

time_t time_of_last_acquire = 0;

VDesktops vdesks;

MessageManager msgmgr;


// lights

// diffuse light
//float light_ambient[]=  { 1.0f, 1.0f, 1.0f, 1.0f };
float light_diffuse[]=  { 1.0f, 1.0f, 1.0f, 1.0f }; 
float light_position[]= { 0.0f, 0.0f, 17.0f, 1.0f };


Movement digit_vis;
float digit_visibility = 0.0;

Movement breathing;  // changes the ambient light to look like breathing
float ambient_value = 2.0;


// ambient light
float light_ambient2[]=  { 1.0f, 1.0f, 1.0f, 1.0f };
//float light_diffuse2[]=  { 1.0f, 1.0f, 1.0f, 1.0f }; 
float light_position2[]= { 0.0f, 0.0f, 17.0f, 1.0f };




// this contains all the "Face" types and each holds location
// information and texture_id.  This in turn is manipulated by each
// faceset (the coords of each face not the texture_id).  This things
// lifetime is for the duration of the daemon.
FaceSet *face_set = NULL;

// this determines the coords of each face in a faceset.  it "has-a"
// faceset (passed in at creation time).  This things lifetime is for
// the duration of an arrangement or "mode".  Can be deleted and
// recreated as another arrangement many times.
Arrangement *faces = NULL;

// The class that creates the window etc.
GLWindow GLWin;

// The class that handles events.  Manages "Event"'s.  
EventManager em;

Bool end_X_event_loop;


#define STATE_ENTRY      0x00000001
#define STATE_EXIT       0x00000002
#define STATE_NORMAL     0x00000004
#define STATE_GOTO       0x00000008
#define STATE_SUSPENDED  0x00000010

#define SET_STATE(s)     (state |=  (s))
#define ISSET_STATE(s)   (state &   (s))
#define UNSET_STATE(s)   (state &= ~(s))
#define CLEAR_STATE()    (state =    0)

unsigned int state = STATE_ENTRY;


// prototypes 

static void goto_face_complete (Event *e);
static void switch_desktop_on_exit (Event *e);
static void entry_move_start_hook (Event *e);
static void exit_move_start_hook (Event *e);
static void setup_goto_face(desktop_coords_t  goto_coords, 
                            desktop_coords_t  current_coord, 
                            desktop_coords_t  current_count);
static void calc_fps(void);
static void render_scene(void);
static void mouse_pressed(unsigned int button);
static void key_pressed(KeySym key);
static void initialize_arrangements (void);
static void set_arrangement (float z_offset_distance);
static void begin_exit (void);
static desktop_coords_t wait_for_activation(void);
static void idle_func(void);
static int main_loop(void);
static void gl_init (void);
static void initialize_3ddesktop (void);
static void load_digits(void);


// Some Event hooks

// runs when a goto face reaches its destination
static void 
goto_face_complete (Event *e)
{
    goto_on_flag = 0;

    //msgout (DEBUG, "-=-=-=-=- goto_face_complete\n");

    if (!cfg->options->disable_exit_after_goto) {
        begin_exit();
    } else {
        cfg->options->disable_exit_after_goto = false;  // dont want this option "sticky"
    }

}

// runs when a goto face reaches its destination (when setup by
// setup_goto_face) and does not start an exit.
static void 
goto_face_complete2 (Event *e)
{
    goto_on_flag = 0;
}


// start the exit movement as soon as the entry movement is over when
// where doing a "goto".
static void 
goto_face_start_exit (Event *e)
{
    //msgout (DEBUG, "-=-=-=-=- goto_face_start_exit\n");
    if (!cfg->options->disable_exit_after_goto) {
        begin_exit();
    } else {
        cfg->options->disable_exit_after_goto = false;  // dont want this option "sticky"
    }

}


// runs when exit movement stops if do_vpswitch_early is false
static void 
switch_desktop_on_exit (Event *e)
{
    msgout (DEBUG, "Doing exit VD switch  c=%d   r=%d\n",
            faces->get_current_column(),
            faces->get_current_row() );
    vdesks.set_vdesktop (faces->get_current_column(),
                         faces->get_current_row() );
}

// runs when entry movement starts
static void 
entry_move_start_hook (Event *e)
{
    msgout (DEBUG, "################ ENTRY START!\n");

    digit_vis.init(&digit_visibility, 0.0, 1.0, 45, Movement::MOVE_TYPE_SMOOTH);

    faces->entry_movement_start();
}

// runs when exit movement starts
static void 
exit_move_start_hook (Event *e)
{
    msgout (DEBUG, "################ EXIT START!\n");

    digit_vis.init(&digit_visibility, 1.0, 0.0, 45, Movement::MOVE_TYPE_SMOOTH);

    faces->exit_movement_start();
}



static void 
setup_goto_face(desktop_coords_t  goto_coords, 
                desktop_coords_t  current_coord, 
                desktop_coords_t  current_count)
{
    msgout (DEBUG, "**** setting up goto = %d x %d\n", goto_coords.column, goto_coords.row);

    switch (goto_coords.column) 
    {
    case GOTO_FACE_RIGHT:
        goto_coords.column = current_coord.column + 1;
        if (goto_coords.column >= current_count.column) 
            goto_coords.column = 0;
        break;

    case GOTO_FACE_LEFT:
        goto_coords.column = current_coord.column - 1;
        if (goto_coords.column < 0) 
            goto_coords.column = current_count.column - 1;
        break;

    case NO_GOTO:
        goto_coords.column = current_coord.column;
        break;

    default:
        goto_coords.column--;  // change 1 thru max to 0 thru max-1
    }

    switch (goto_coords.row) 
    {
    case GOTO_FACE_DOWN:
        goto_coords.row = current_coord.row + 1;
        if (goto_coords.row >= current_count.row) 
            goto_coords.row = 0;
        break;

    case GOTO_FACE_UP:
        goto_coords.row = current_coord.row - 1;
        if (goto_coords.row < 0) 
            goto_coords.row = current_count.row - 1;
        break;

    case NO_GOTO:
        goto_coords.row = current_coord.row;
        break;

    default:
        goto_coords.row--;
    }

    goto_on_flag = 1;

    msgout (DEBUG, "**** complete - going with = %d x %d\n", goto_coords.column, goto_coords.row);

    desktop_coords_t dc(goto_coords);

    SET_STATE(STATE_GOTO);
    faces->goto_face (dc);

    em.add_event (GOTO_FACE_COMPLETE, goto_face_complete2);

    em.add_event (ENTRY_MOVEMENT_STOP, goto_face_start_exit);

} // END setup_goto_face


typedef struct portion_control_s {
    int inited;
    int x_segment_size;
    int y_segment_size;
    int n_x_seg;
    int n_y_seg;
    int current_x_seg;
    int current_y_seg;
} portion_control_t;

//#define SCAN_MODE

#ifdef SCAN_MODE

static void
init_portion_control (portion_control_t *pc)
{
    pc->x_segment_size = 128;
    pc->y_segment_size = 128;
    pc->n_x_seg = (int)floor(GLWin.get_screen_width() / pc->x_segment_size);
    pc->n_y_seg = (int)floor(GLWin.get_screen_height() / pc->y_segment_size);

    //msgout (DEBUG, "*************** %d x %d\n", pc->n_x_seg, pc->n_y_seg);

    pc->current_x_seg = 0;
    pc->current_y_seg = 0;

    pc->inited = 1;
}

static void
grab_portion_of_screen (portion_control_t *pc)
{
    if (!pc->inited)
        init_portion_control(pc);

    int x1 = pc->current_x_seg * pc->x_segment_size;
    int x2 = pc->current_x_seg * pc->x_segment_size + pc->x_segment_size;
    int y1 = pc->current_y_seg * pc->y_segment_size;
    int y2 = pc->current_y_seg * pc->y_segment_size + pc->y_segment_size;

    msgout (DEBUG, "GRABBING segment %d x %d\n", 
            pc->current_x_seg, pc->current_y_seg);

    GLWin.grab_screenshot_data_portion (x1, y1, x2, y2);

    pc->current_x_seg++;
    if (pc->current_x_seg == pc->n_x_seg) {

        msgout (DEBUG, "reached max X %d\n", pc->n_x_seg);

        pc->current_x_seg = 0;
        pc->current_y_seg++;
        if (pc->current_y_seg == pc->n_y_seg) 
            pc->current_y_seg = 0;
    }

    
    //= get_randomi(0, pc->n_x_seg - 1);
    //= get_randomi(0, pc->n_y_seg - 1);

} // END grab_portion_of_screen 

#endif // SCAN_MODE 





// ------
// Frames per second (FPS) statistic variables and routine.
#define FRAME_RATE_SAMPLES 100
int   FrameCount = 0;
float FrameRate = 0;

static void 
calc_fps(void)
{
    static clock_t last=0;
    clock_t now;
    float delta;

    if (++FrameCount >= FRAME_RATE_SAMPLES) {
        now  = clock();
        delta= (now - last) / (float) CLOCKS_PER_SEC;
        last = now;

        FrameRate = FRAME_RATE_SAMPLES / delta;
        FrameCount = 0;
    }
} // END calc_fps


uint digit_texture;


static void draw_digits (int r, int c);
static void draw_desktop_number(int num);
static void draw_digit (int d, float x_trans, float y_trans);
static void draw_x (float x_trans, float y_trans);
static void set_color (int color, float level);


static void
set_color (int color, float level) 
{
    switch (color) {
    case COLOR_RED:
        glColor4f(level, 0, 0, 0); 
        break;
    case COLOR_GREEN:
        glColor4f(0, level, 0, 0);
        break;
    case COLOR_BLUE:
        glColor4f(0, 0, level, 0);
        break;
    case COLOR_LIGHTBLUE:
        glColor4f(0, level, level, 0);
        break;
    case COLOR_WHITE:
        glColor4f(level, level, level, 0);
        break;
    case COLOR_GRAY:
        glColor4f(level/2, level/2, level/2, 0);
        break;
    case COLOR_PURPLE:
        glColor4f(level, 0, level, 0);
        break;
    case COLOR_YELLOW:
        glColor4f(level, level, 0, 0);
        break;
    default: // white
        glColor4f(level, level, level, 0);
        break;
    }
}


static void 
draw_desktop_number(int num)
{
    float digit_size = cfg->options->digit_size;
  
    // where to draw to 
    float centre_x = GLWin.get_width() / 2 ;
    float centre_y = GLWin.get_height() - (digit_size / 2) ;
  
    // decode to digits
 
    int numdigits = (int) log10((float)num);
 
    float current_x = centre_x - (digit_size * numdigits / 2);
  
    while(num >= 10) {
	draw_digit(num / 10, 
		   current_x, 
		   centre_y);
	current_x += digit_size;
  	num = num % 10; 
    }

    draw_digit(num, 
               current_x, 
               centre_y);
}



static void
draw_digits (int r, int c)
{
    float digit_size = cfg->options->digit_size;

    draw_x (GLWin.get_width() / 2,
            GLWin.get_height() - (digit_size / 2) );

    draw_digit (r, 
                GLWin.get_width() / 2 - (digit_size),
                GLWin.get_height() - (digit_size / 2) );

    draw_digit (c, 
                GLWin.get_width() / 2 + (digit_size),
                GLWin.get_height() - (digit_size / 2) );

}


static void
draw_x (float x_trans, float y_trans)
{
    float x1 = .5, y1 = .5, x2 = .75, y2 = .75;

    float w = cfg->options->digit_size/2.0;

    // But we like our current view too; so we save it here.
    glPushMatrix();
    
    // Now we set up a new projection for the text.
    glLoadIdentity();
    glOrtho(0,GLWin.get_width(),0,GLWin.get_height(),-1.0,1.0);


    glTranslatef(x_trans, y_trans, 0.0f);

    set_color (cfg->options->digit_color, digit_visibility);

    glEnable(GL_BLEND);
    glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

    glBindTexture(GL_TEXTURE_2D, digit_texture );

    glBegin(GL_QUADS);
    
    glTexCoord2f (x1, y2);
    glVertex3f(-w,-w,0);
    
    glTexCoord2f (x2, y2);
    glVertex3f(w,-w,0);
    
    glTexCoord2f (x2, y1);
    glVertex3f(w,w,0);
    
    glTexCoord2f (x1, y1);
    glVertex3f(-w,w,0);
    
    glEnd();

    // Done with this special projection matrix.  Throw it away.
    glPopMatrix();
}


// this is where the digit is located in the texture
float digit_texture_coords[] = 
{ .25	, .5, 
  0	, 0, 
 .25	, 0, 
 .5	, 0, 
 .75	, 0, 
 0	, .25, 
 .25	, .25, 
 .5	, .25, 
 .75	, .25, 
 0	, .5, 
 .25	, .5 };

static void
draw_digit (int d, float x_trans, float y_trans)
{
    float x1, y1, x2, y2;

    if (d >= 10)
        return ;

    x1 = digit_texture_coords[d * 2];
    y1 = digit_texture_coords[d * 2 + 1];
   
    x2 = x1 + 0.25;
    y2 = y1 + 0.25;

    //msgout (DEBUG, "oooooooo d=%d  x1=%f  y1=%f  x2=%f  y2=%f\n",
    //        d, x1, y1, x2, y2);


    float w = cfg->options->digit_size/2.0;

    // But we like our current view too; so we save it here.
    glPushMatrix();
    
    // Now we set up a new projection for the text.
    glLoadIdentity();
    glOrtho(0,GLWin.get_width(),0,GLWin.get_height(),-1.0,1.0);


    glTranslatef(x_trans, y_trans, 0.0f);


    set_color (cfg->options->digit_color, digit_visibility);

    glEnable(GL_BLEND);
    glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

    glBindTexture(GL_TEXTURE_2D, digit_texture );

    glBegin(GL_QUADS);
    
    glTexCoord2f (x1, y2);
    glVertex3f(-w,-w,0);
    
    glTexCoord2f (x2, y2);
    glVertex3f(w,-w,0);
    
    glTexCoord2f (x2, y1);
    glVertex3f(w,w,0);
    
    glTexCoord2f (x1, y1);
    glVertex3f(-w,w,0);
    
    glEnd();

    // Done with this special projection matrix.  Throw it away.
    glPopMatrix();
}


// ------
// Routine which actually does the drawing
static void
render_scene(void)
{
    //char buf[80]; // For our strings.
    long current_time;


    // Enables, disables or otherwise adjusts as 
    // appropriate for our current settings.

    if (Texture_On)
        glEnable(GL_TEXTURE_2D);
    else
        glDisable(GL_TEXTURE_2D);

    if (Light_On) 
        glEnable(GL_LIGHTING);
    else 
        glDisable(GL_LIGHTING);

    if (Alpha_Add)
        //glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

        //glBlendFunc(GL_ONE, GL_SRC_ALPHA); 

        //glBlendFunc(GL_ONE, GL_CONSTANT_ALPHA); 
        
        // this one is good with NO color material, disable depth test, blend on
        glBlendFunc(GL_SRC_ALPHA, GL_ONE); // GOOD!

    else
        glBlendFunc(GL_ONE, GL_ONE_MINUS_DST_ALPHA);
    
    //glBlendFunc(GL_ONE_MINUS_DST_ALPHA,GL_SRC_ALPHA); 


    // If we're blending, we don't want z-buffering.
    if (Blend_On) {
        //glEnable(GL_DEPTH_TEST); 
        glDisable(GL_DEPTH_TEST); 
        glEnable(GL_BLEND);
    } else { 
        glEnable(GL_DEPTH_TEST); 
        glDisable(GL_BLEND); 
        //glEnable(GL_DEPTH_TEST); 
    }

    glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,TexModes[Curr_TexMode]);

    // only effects gl_blend
    //float text_env_color[] = { .1, .1, .1, .1 };
    //glTexEnvfv(GL_TEXTURE_ENV,GL_TEXTURE_ENV_COLOR, text_env_color);


    // Need to manipulate the ModelView matrix to move our model around.
    glMatrixMode(GL_MODELVIEW);

    // Reset to 0,0,0; no rotation, no scaling.
    glLoadIdentity(); 


    current_time = get_milli_time();

    // this gives us semi-precise/consistent timing of the movements
    // 
    if (current_time - last_time > cfg->options->animation_speed) {

        if (ISSET_STATE(STATE_EXIT)) {

            //if (!cfg->options->entry_exit_movement || faces->is_exit_movement_done()) {
            


            if (faces->is_exit_movement_done()) {
                // quit (the z offset was changed to match the screen)

                if (!ISSET_STATE(STATE_GOTO)) {  // dont exit if we're still goto'ing
                    UNSET_STATE(STATE_EXIT);
                    end_X_event_loop = True;
                    em.trigger_event (EXIT_MOVEMENT_STOP);
                    return;
                }
                
            } else {
                faces->entry_exit_move();
            }

        } else if (ISSET_STATE(STATE_ENTRY)) {

            if ( faces->is_entry_movement_done() ) {
                UNSET_STATE(STATE_ENTRY);
                SET_STATE(STATE_NORMAL);
                em.trigger_event (ENTRY_MOVEMENT_STOP);

            } else {
                faces->entry_exit_move();

            }

        } 

        // change state to normal from goto if done goto
        if (ISSET_STATE(STATE_GOTO)) {
            if ( !faces->in_goto() ) {
                UNSET_STATE(STATE_GOTO);
                SET_STATE(STATE_NORMAL);
                em.trigger_event (GOTO_FACE_COMPLETE);
            }
        }

        
        if (cfg->options->use_breathing) {
            if (breathing.at_destination()) {
                breathing.init(&ambient_value, 1.7, 3.0, 200, Movement::MOVE_TYPE_SINE);
            } else {
                breathing.change_a_bit();
                light_ambient2[0] = light_ambient2[1] = light_ambient2[2] = ambient_value;
            }

        }

        if (!digit_vis.at_destination())
            digit_vis.change_a_bit();


        faces->do_movement();

        last_time = get_milli_time();

    } // end animation step
 

    // FIXME: use event to accomplish this?

    // after some seconds start some fun ;)
    if (!disable_random_fun && 
        cfg->options->random_fun_delay &&
        current_time - last_keypress > cfg->options->random_fun_delay * 1000) 
    {
        if ((unsigned long)current_time >= (unsigned long)next_action) {

            //msgout(DEBUG, "having some fun...\n");
            faces->goto_random_face();
            
            next_action = current_time + get_randomi(350, 600);
        }
    }

    glLoadIdentity();

    // adjust the faces the necessary amount
    faces->do_position_GL();

    // Clear the color and depth buffers.
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    faces->render();

    //glPushMatrix();

    glLoadIdentity();

    glColor3f(0.0, 0.0, 1.0);
    glPointSize(5.0);
    glBegin(GL_POINTS);
    glVertex4fv(light_position2);
    glVertex4fv(light_position);
    glEnd();


    // lighting (light2 is for ambient only)
    glLightfv(GL_LIGHT2, GL_POSITION, light_position2);
    glLightfv(GL_LIGHT2, GL_AMBIENT,  light_ambient2);
    //glLightfv(GL_LIGHT2, GL_DIFFUSE,  Light_Diffuse2); 
    glEnable (GL_LIGHT2);

    // lighting (light1 is diffuse only)
    glLightfv(GL_LIGHT1, GL_POSITION, light_position);

    //glLightfv(GL_LIGHT1, GL_AMBIENT,  Light_Ambient);
    //glLightfv(GL_LIGHT1, GL_DIFFUSE,  light_diffuse);

    //float halfs[] = { .50, .50, .50, .50 };
    float ones[] = { 1.0, 1.0, 1.0, 1.0 };


    glLightfv(GL_LIGHT1, GL_DIFFUSE,  ones);
    glLightfv(GL_LIGHT1, GL_SPECULAR,  ones);

    glEnable (GL_LIGHT1);


    //glPopMatrix();

    // We need to change the projection matrix for the text rendering.  
    glMatrixMode(GL_PROJECTION);

    // But we like our current view too; so we save it here.
    glPushMatrix();

    // Now we set up a new projection for the text.
    glLoadIdentity();
    glOrtho(0,GLWin.get_width(),0,GLWin.get_height(),-1.0,1.0);

    // Lit or textured text looks awful.
    glDisable(GL_TEXTURE_2D);
    glDisable(GL_LIGHTING);

    // We don't want depth-testing either.
    glDisable(GL_DEPTH_TEST); 


    // Now we want to render the calulated FPS at the top.
   
    // To ease, simply translate up.  Note we're working in screen
    // pixels in this projection.
   
#if 0
    if (cfg->options->show_fps) {
        glTranslatef(6.0f, GLWin.get_height() - 14,0.0f);
        
        // Make sure we can read the FPS section by first placing a 
        // dark, mostly opaque backdrop rectangle.
        glColor4f(0.2,0.2,0.2,0.75);
        
        glBegin(GL_QUADS);
        glVertex3f(  0.0f, -2.0f, 0.0f);
        glVertex3f(  0.0f, 12.0f, 0.0f);
        glVertex3f(140.0f, 12.0f, 0.0f);
        glVertex3f(140.0f, -2.0f, 0.0f);
        glEnd();
        
        glColor4f(0.9,0.2,0.2,.75);
        char buf[50];
        sprintf(buf,"FPS: %f F: %2d", FrameRate, FrameCount);
        glRasterPos2i(6,0);
        ourPrintString(GLUT_BITMAP_HELVETICA_12,buf);
    }
#endif // if 0

    // Done with this special projection matrix.  Throw it away.
    glPopMatrix();



    if (cfg->options->show_digits) {

        // We need to change the projection matrix for the text rendering.  
        glMatrixMode(GL_PROJECTION);

        // Lit or textured text looks awful.
        glEnable(GL_TEXTURE_2D);
        glDisable(GL_LIGHTING);

        // We don't want depth-testing either.
        glDisable(GL_DEPTH_TEST); 



        // Now we want to render the calulated FPS at the top.
   
        // To ease, simply translate up.  Note we're working in screen
        // pixels in this projection.
   
        if (faces->get_number_of_columns() > 1 &&
            faces->get_number_of_rows() > 1) 
        {
            draw_digits (faces->get_current_row() + 1,
                         faces->get_current_column() + 1);
        } else {
            int n = 1;
            if (faces->get_number_of_columns() > 1)
                n = faces->get_current_column() + 1;
            else
                n = faces->get_current_row() + 1;

            draw_desktop_number (n);
        }

#if 0  // use a string rather then the digit

        // FIXME: do this if the digit texture didn't load

        char buf[50];

        // use N x N if both rows and columns otherwise use juse N
        // (representing columns or rows depending on which is being
        // used)
        if (faces->get_number_of_columns() > 1 &&
            faces->get_number_of_rows() > 1) 
        {
            sprintf(buf,"%d x %d", 
                    faces->get_current_row() + 1,
                    faces->get_current_column() + 1);
        } else {
            int n = 1;
            if (faces->get_number_of_columns() > 1)
                n = faces->get_current_column() + 1;
            else
                n = faces->get_current_row() + 1;

            sprintf(buf,"%d", n);
        }
        
        glRasterPos2i(6,0);
        ourPrintString(GLUT_BITMAP_TIMES_ROMAN_24,buf);
#endif
        

    }



    // All done drawing.  Let's show it.
    glXSwapBuffers(GLWin.get_display(), GLWin.get_window());

    // And collect our statistics.
    if (cfg->options->show_fps)
        calc_fps();

    last_renderscene_time = get_milli_time();

} // END render_scene



static void
mouse_pressed(unsigned int button)
{
    last_keypress = get_milli_time();

    //msgout (DEBUG, "button pressed %d - 0x%x\n", button, button);

    switch (button) {
    case 1: // left
    	if (cfg->options->alt_mousebuttons)
            begin_exit();
        else
            if (cfg->options->swap_mousebuttons)
                faces->goto_right();
            else
                faces->goto_left();
        break;

    case 3: // right
        if (cfg->options->alt_mousebuttons) {
            SET_STATE(STATE_GOTO);
            faces->goto_face(last_coords);
            begin_exit();
        } else 
            if (cfg->options->swap_mousebuttons)
                faces->goto_left();
            else
                faces->goto_right();
        break;

    case 2: // middle
        if (cfg->options->alt_mousebuttons) {
            SET_STATE(STATE_GOTO);
            faces->goto_face(last_coords);
        } else {
            begin_exit();
        }
        break;


    case 4: // mouse wheel up
        if (cfg->options->reverse_mousewheel)
            faces->goto_left();
        else
            faces->goto_right();
        break;

    case 5: // mouse wheel down
        if (cfg->options->reverse_mousewheel)
            faces->goto_right();
        else
            faces->goto_left();
        break;


    case 6: // extra button on some mice, do we use this?
        if (cfg->options->swap_mousebuttons)
            faces->goto_left();
        else
            faces->goto_right();
        break;

    case 7: // extra button on some mice, do we use this?
        if (cfg->options->swap_mousebuttons)
            faces->goto_right();
        else
            faces->goto_left();
        break;

    }
} // END mouse_pressed


// ------
// Callback function called when a normal key is pressed.

static void
key_pressed(KeySym key)
{
    static KeySym last_key_pressed = 0;

    if (cfg->disable_keys_in_goto &&
        goto_on_flag && 
        !cfg->options->disable_exit_after_goto)
        return;

    last_keypress = get_milli_time();

    next_action = 0;

    switch (key) {
    case 130: case 98: // B - Blending.
        Blend_On = Blend_On ? 0 : 1; 
        if (!Blend_On)
            glDisable(GL_BLEND); 
        else
            glEnable(GL_BLEND);
        break;

    case 108: case 76:  // L - last desktop
        SET_STATE(STATE_GOTO);
        faces->goto_face(last_coords);
        
        // this used to be for turning lights on an off...
        //Light_On = Light_On ? 0 : 1; 
        break;

    case 109: case 77:  // M - Mode of Blending
        if ( ++ Curr_TexMode > 3 )
            Curr_TexMode=0;
        glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,TexModes[Curr_TexMode]);
        msgout (DEBUG, "glTexEnv mode=%s (%d)\n", TexModesStr[Curr_TexMode],
                TexModes[Curr_TexMode]);
        break;

    case 116: case 84: // T - Texturing.
        Texture_On = Texture_On ? 0 : 1; 
        break;

    case 97: case 65:  // A - Alpha-blending hack.
        Alpha_Add = Alpha_Add ? 0 : 1; 
        break;

    case 114: case 82: // R - random
        faces->goto_random_face();
        break;

    case 102: case 70: // F - Fun Mode
        disable_random_fun = disable_random_fun ? 0 : 1;
        break;

#if 1 // for debugging best light position...
    case 119: // w
        light_position[0] -= 0.5;
        msgout (DEBUG, "light x at %f\n", light_position[0]);
        break;
    case 101: // e
        light_position[0] += 0.5;
        msgout (DEBUG, "light x at %f\n", light_position[0]);
        break;
    case 115: // s 
        light_position[1] -= 0.5;
        msgout (DEBUG, "light y at %f\n", light_position[1]);
        break;
    case 100: // d 
        light_position[1] += 0.5;
        msgout (DEBUG, "light y at %f\n", light_position[1]);
        break;
    case 120: // x
        light_position[2] -= 0.5;
        msgout (DEBUG, "light z at %f\n", light_position[2]);
        break;
    case 99: // c
        light_position[2] += 0.5;
        msgout (DEBUG, "light z at %f\n", light_position[2]);
        break;
#endif

    case 48:
        if (last_key_pressed == key) {
            // second time this key was hit so exit.
            begin_exit();
            break;
        }
        last_key_pressed = key;

        SET_STATE(STATE_GOTO);
        faces->goto_face(9);
        break;
        
    case 49:
    case 50:
    case 51:
    case 52:
    case 53:
    case 54:
    case 55:
    case 56:
    case 57:
        if (last_key_pressed == key) {
            // second time this key was hit so exit.
            begin_exit();
            break;
        }

        last_key_pressed = key;

        if (initiate_destroy) {

            initiate_destroy = 0;
            faces->exit_movement_cancel();

            // FIXME: replace with event something or other
            SET_STATE(STATE_GOTO);
            faces->goto_face(key - 49);

        } else {
            SET_STATE(STATE_GOTO);
            faces->goto_face(key - 49);
        }

        break;

#define  KEY_ENTER  0xff0d
#define  KEY_UP     0xff52
#define  KEY_DOWN   0xff54
#define  KEY_RIGHT  0xff53
#define  KEY_LEFT   0xff51
#define  KEY_ESCAPE 0xff1b

    case KEY_ENTER: 
    case KEY_ESCAPE:
    case 113:  // 'q'
    case 32:   // space
        // We're outta here.

        begin_exit();

        // the z_offset translation starts and is carried out by the
        // render function where we exit when it completes
        break;

    case KEY_LEFT:

        faces->goto_left();

        break;

    case KEY_RIGHT:

        faces->goto_right();

        break;

    case KEY_DOWN:

        faces->goto_down();

        break;

    case KEY_UP:

        faces->goto_up();

        break;

    default:
        msgout (DEBUG, "KP: No action for %ld / 0x%lx.\n", key, key);
        break;
    }
} // END key_pressed



Arrangement *arrangement_array[FACE_TYPE_LAST];

// assumes 'a' is Arrangement *a;
#define  ALLOC_ARRANGEMENT(enum_name, type)                     \
    if (arrangement_array[enum_name])                           \
        delete (arrangement_array[enum_name]);                  \
    a = new type (vdesks,                                       \
                  face_width, face_height,                      \
                  face_set, cfg);                               \
    if (!a) {                                                   \
        msgout(ERROR, "out of memory\n");                       \
        end_program (1);                                        \
    }                                                           \
    arrangement_array[enum_name] = a;


static void
initialize_arrangements (void) 
{

    memset(arrangement_array, 0, sizeof(arrangement_array));
    

#if 0
    float aspect;
    float face_height, face_width;

    Arrangement *a = NULL;

    aspect = (float)GLWin.get_screen_width() / (float)GLWin.get_screen_height();
    face_width = 4.0;
    face_height = face_width/aspect;

    static bool first_time = true;
    if (first_time) {
        memset(arrangement_array, 0, sizeof(arrangement_array));
        first_time = false;
    }
    
    // tediously allocate each arrangement type and stuff into
    // "arrangement_array"

    ALLOC_ARRANGEMENT(FACE_TYPE_CAROUSEL, CarouselArrangement);
    ALLOC_ARRANGEMENT(FACE_TYPE_CYLINDER, CylinderArrangement);
    ALLOC_ARRANGEMENT(FACE_TYPE_VIEWMASTER, ViewMasterArrangement);
    ALLOC_ARRANGEMENT(FACE_TYPE_PRICEISRIGHT, PriceIsRightArrangement);
    ALLOC_ARRANGEMENT(FACE_TYPE_LINEAR, LinearArrangement);
    ALLOC_ARRANGEMENT(FACE_TYPE_FLIP, FlipArrangement);
#endif

} // END initialize_arrangements


static Arrangement *
demand_load_arrangement (FaceType_t type)
{
    
    Arrangement *f = arrangement_array [type];
    if (!f) {
        msgout (DEBUG, "DDDDDDDDDDDDDD demand loading %d\n", type);

        float aspect;
        float face_height, face_width;
        
        aspect = (float)GLWin.get_screen_width() / (float)GLWin.get_screen_height();
        face_width = 4.0;
        face_height = face_width/aspect;
        
        switch (type) {
        case FACE_TYPE_CAROUSEL:
            f = new CarouselArrangement (vdesks, face_width, face_height, face_set, cfg);
            break;
#if !defined (ONE_ARRANGEMENT_ONLY)
        case FACE_TYPE_CYLINDER:
            f = new CylinderArrangement (vdesks, face_width, face_height, face_set, cfg);
            break;
        case FACE_TYPE_VIEWMASTER:
            f = new ViewMasterArrangement (vdesks, face_width, face_height, face_set, cfg);
            break;
        case FACE_TYPE_PRICEISRIGHT:
            f = new PriceIsRightArrangement (vdesks, face_width, face_height, face_set, cfg);
            break;
        case FACE_TYPE_LINEAR:
            f = new LinearArrangement (vdesks, face_width, face_height, face_set, cfg);
            break;
        case FACE_TYPE_FLIP:
            f = new FlipArrangement (vdesks, face_width, face_height, face_set, cfg);
            break;
#endif
        default:
            f = NULL;
        }

        if (!f) {
            msgout(ERROR, "out of memory\n");
            end_program(-1);
        }
        arrangement_array[type] = f;
    }
    return f;
}


static void
set_arrangement (float z_offset_distance) 
{
    FaceType_t type = cfg->options->face_type;

    if (type == FACE_TYPE_RANDOM)
        type = (FaceType_t)get_randomi (1, FACE_TYPE_LAST - 1);


    if (type == FACE_TYPE_NONE)
        if (z_offset_distance)
            faces->set_z_offset_distance(z_offset_distance);


    if (type < 0 || type >= FACE_TYPE_LAST) {
        msgout (ERROR, "BUG: Unknown face type (?)\n");
        end_program(1);
    }
        

    // type is valid - demand load the arrangement
    if ((faces = demand_load_arrangement(type)) == NULL) {
        msgout (ERROR, "BUG: could not demand load arrangement %d!\n", type);
        end_program(-1);
    }
    
    faces->calculate_faces(vdesks);
    
    //msgout (DEBUG, "xxxxxxxx Setting %d (zod %f) at 0x%x\n", 
    //        type, z_offset_distance, faces);
    
    
    if (z_offset_distance)
        faces->set_z_offset_distance(z_offset_distance);

} // END set_arrangement



static void 
begin_exit (void)
{
    if (    faces->cant_exit_in_goto()
         && faces->in_goto())
    {
        // if no "zooming" then we don't want to exit until the goto
        // face is complete
        msgout (DEBUG, "cant exit in goto....\n");
        SET_STATE(STATE_GOTO);
        em.remove_event (GOTO_FACE_COMPLETE, goto_face_complete2);
        em.add_event (GOTO_FACE_COMPLETE, goto_face_complete);
        return;
    }

    SET_STATE(STATE_EXIT);
    em.trigger_event (EXIT_MOVEMENT_START);
    initiate_destroy = 1;

    if (do_vpswitch) {

        if (cfg->early_desktop_switch) {
            msgout (DEBUG, "Doing VD switch  c=%d   r=%d\n",
                    faces->get_current_column(),
                    faces->get_current_row() );
            vdesks.set_vdesktop (faces->get_current_column(),
                                 faces->get_current_row() );
        } else {
            em.add_event (EXIT_MOVEMENT_STOP, switch_desktop_on_exit);
        }
    }

    // the z_offset translation starts and is carried out by the
    // render function where we exit when it completes
}






portion_control_t portion_control;


static desktop_coords_t
wait_for_activation(void)
{
    //threedeedesk_command_t  msg;

    int done = 0;
    int col_count = 1;
    int row_count = 1;
    int current_col = 0;
    int current_row = 0;

    desktop_coords_t goto_coords;

    goto_coords.column = NO_GOTO;
    goto_coords.column = NO_GOTO;

    CLEAR_STATE();
    SET_STATE(STATE_SUSPENDED);

    disable_autoacquire = false;
    if (cfg->autoacquire)
        alarm(cfg->autoacquire);

    do {

#ifdef SCAN_MODE
        do {

            // must setup current_etc for screen portion grab. this
            // should work by registering notifiers so we can change
            // the values only when we need to rather then "polling"
            // for changes
            vdesks.get_vdesktop_info (&current_col, &current_row, &col_count, &row_count);

            // do some work here....
            grab_portion_of_screen (&portion_control);

            face_set->load_texture_data(current_col + (current_row * col_count), 
                                        cfg->texture_size,
                                        GLWin.get_screenshot_data_ptr() );

            //msgout (DEBUG, ".....grabbed\n");
            sleep_ms (20);


            // non-blocking - returns -1 error and 0 for success or NOMSG
            if (msgmgr.recieve(MessageManager::NOWAIT) < 0) {
                msgout (ERROR, "msgrcv failed: %s\n", strerror(errno));
                continue;
            }

        } while (errno == ENOMSG);
#else
        if (msgmgr.flush() < 0) {
            msgout (ERROR, "flush failed: %s\n", strerror(errno));
            end_program(-1);
        }

        // should block until message received
        if (msgmgr.recieve() < 0) {
            msgout (ERROR, "msgrcv failed: %s\n", strerror(errno));
            end_program(-1);
        }

#endif


        msgout (DEBUG, "MSG: %ld\n", msgmgr.msg.mtype);

        disable_autoacquire = true;

        // must get this now in case the current desktop was changed by
        // other means
        vdesks.get_vdesktop_info (&current_col, &current_row, &col_count, &row_count);

        msgout(DEBUG, "####### current %d x %d\n", current_col, current_row);
        faces->set_current_face (current_col, current_row);

        switch (msgmgr.msg.mtype) {
        case MTYPE_STOP:
            msgout (DEBUG, "-- quiting\n");

            end_program(0);

            break;
        case MTYPE_ACQUIRE_CURRENT:

            if (msgmgr.msg.reload)
                cfg->reload();

            if (GLWin.grab_screenshot_data() < 0) {
                msgout (ERROR, "couldn't grab screen image\n");
                end_program(-1);
            }
            
            face_set->load_texture_data(current_col + (current_row * col_count), 
                                        cfg->texture_size,
                                        GLWin.get_screenshot_data_ptr() );
            
            continue;  // don't activate
            
            break;
        case MTYPE_ACQUIRE:

            if (msgmgr.msg.reload)
                cfg->reload();

            // loop through all desktops and grab them.
            int i, j;
            for (i = 0; i < row_count; i++) {
                for (j = 0; j < col_count; j++) {
                
                    vdesks.set_vdesktop (j, i);
                    
                    if (msgmgr.msg.acquire_sleep > 1) {
                        msgout (DEBUG, "acquire sleep is %d\n", msgmgr.msg.acquire_sleep);
                        sleep_ms (msgmgr.msg.acquire_sleep);
                    }
                    
                    if (GLWin.grab_screenshot_data() < 0) {
                        msgout (ERROR, "couldn't grab screen image\n");
                        end_program(-1);
                    }
                    
                    face_set->load_texture_data(j + (i * col_count), 
                                                cfg->texture_size,
                                                GLWin.get_screenshot_data_ptr() );
                }
            }
            // go back to current
            vdesks.set_vdesktop (current_col, current_row);
            
            continue;  // don't activate

            break;
        case MTYPE_JUSTSWITCH:
            
            goto_coords.column = msgmgr.msg.goto_column;
            goto_coords.row = msgmgr.msg.goto_row;
            
            if (goto_coords.column == NO_GOTO &&
                goto_coords.row    == NO_GOTO)
                continue;  // do nothing

            // assign as current col/row if no_goto
            if (goto_coords.column == NO_GOTO)
                goto_coords.column = current_col + 1;

            if (goto_coords.row == NO_GOTO)
                goto_coords.row = current_row + 1;

            // if we are asked to goto the current row and column then
            // do nothing
            if (goto_coords.column == (current_col + 1)
                && goto_coords.row == (current_row + 1))
                continue;

            // check bounds on goto column
            if ( goto_coords.column < 1
                 || goto_coords.column > col_count )
            {
                msgout (DEBUG, "Ignoring out of bounds goto column of %d\n", goto_coords.column);
                continue;
            }


            // check bounds on goto row
            if ( goto_coords.row < 1
                 || goto_coords.row > row_count )
            {
                msgout (DEBUG, "Ignoring out of bounds goto row of %d\n", goto_coords.row);
                continue;
            }

            // capture the current desktop
            if (GLWin.grab_screenshot_data() < 0) {
                msgout (ERROR, "couldn't grab screen image\n");
                end_program(-1);
            }
                    
            face_set->load_texture_data(current_col + (current_row * col_count), 
                                        cfg->texture_size,
                                        GLWin.get_screenshot_data_ptr() );
            
            // now go to the specified desktop
            vdesks.set_vdesktop (goto_coords.column-1, goto_coords.row-1);

            // now capture this desktop so we have an updated image
            if (GLWin.grab_screenshot_data() < 0) {
                msgout (ERROR, "couldn't grab screen image\n");
                end_program(-1);
            }
                    
            face_set->load_texture_data((goto_coords.column-1) + ((goto_coords.row-1) * col_count), 
                                        cfg->texture_size,
                                        GLWin.get_screenshot_data_ptr() );
            
            // we are done and do not activate.
            continue;

            break;
        case MTYPE_ACTIVATE:

            if (strlen(msgmgr.msg.view)) {
                // client specified a view - ignore cmd line params except goto

                cfg->reload(); // always reload cfg if view specified

                cfg->set_config_set (msgmgr.msg.view);

                goto_coords.column = cfg->options->goto_column;
                goto_coords.row = cfg->options->goto_row;

            } else {

                cfg->reload(); // reload in case "default" changed
                
                cfg->set_config_set ("default");

                // set is cmd line (default) and let other client
                // specified options override anything

                if (msgmgr.msg.zoom != -1)
                    cfg->options->entry_exit_movement = msgmgr.msg.zoom;

                if (msgmgr.msg.face_change_steps)
                    cfg->options->face_change_steps = msgmgr.msg.face_change_steps;

                if (msgmgr.msg.zoom_steps)
                    cfg->options->zoom_steps = msgmgr.msg.zoom_steps;

                msgout (DEBUG, "ZOOM is %d\n", cfg->options->entry_exit_movement);

                if (msgmgr.msg.face_type != FACE_TYPE_NONE)
                    cfg->options->face_type = msgmgr.msg.face_type;


                goto_coords.column = msgmgr.msg.goto_column;
                goto_coords.row = msgmgr.msg.goto_row;

            }

            if (msgmgr.msg.disable_exit_after_goto)
                cfg->options->disable_exit_after_goto = true;

            if (msgmgr.msg.disable_random_fun)
                disable_random_fun = 0;

            if (msgmgr.msg.reverse_mousewheel)
                cfg->options->reverse_mousewheel = true;
            if (msgmgr.msg.swap_mousebuttons)
                cfg->options->swap_mousebuttons = true;
            if (msgmgr.msg.alt_mousebuttons)
                cfg->options->alt_mousebuttons = true;

            // if we are asked to goto the current row and column then
            // do nothing
            if (goto_coords.column == (current_col + 1)
                && goto_coords.row == (current_row + 1))
                continue;

            // check bounds on goto column
            if (goto_coords.column > NO_GOTO) {

                if ( goto_coords.column < 1
                     || goto_coords.column > col_count )
                {
                    msgout (DEBUG, "Ignoring out of bounds goto column of %d\n", goto_coords.column);
                    continue;
                }
            }


            // check bounds on goto row
            if (goto_coords.row > NO_GOTO) {

                if ( goto_coords.row < 1
                     || goto_coords.row > row_count )
                {
                    msgout (DEBUG, "Ignoring out of bounds goto row of %d\n", goto_coords.row);
                    continue;
                }
            }

            // we must destroy our current face and create another one
            // if the indicated face_type is different or the number
            // of faces has changed
            if (cfg->options->face_type != faces->get_type()
                || cfg->options->recalc)
            {
                //msgout(DEBUG, "switch faceset %d for %d\n", 
                //       faces->get_type(), cfg->options->face_type);

                face_set->reconfigure(row_count, col_count);
                set_arrangement (cfg->options->default_z_offset_distance);

                cfg->options->recalc = 0;
            }


            // reload face set if different number of columns or rows
            if (col_count != faces->get_number_of_columns() ||
                row_count != faces->get_number_of_rows() ) 
            {
                msgout (DEBUG, "++++ column_count changed from %d to %d\n", 
                        faces->get_number_of_columns(), col_count);

                face_set->reconfigure(row_count, col_count);
                set_arrangement (cfg->options->default_z_offset_distance);

            }

            done = 1;
            break;
        default:
            msgout (ERROR, "Unknown message! mytpe = %d\n", msgmgr.msg.mtype);
            break;
        }
    } while (!done);

    SET_STATE(STATE_NORMAL);

    msgout (DEBUG, "Activated\n");
    
    return goto_coords;
}


#if 0
static void
idle_func (void)
{
    static int count = 0;

    // context every 30 calls
    if (++count >= 30) {
        msgout (DEBUG, "a;lskdjf;asldfjasldkjf\n");
        context_switch();
        count = 0;
    } 

    render_scene();
        
}
#endif


#if 1
static void 
idle_func(void)
{
    // This prevents the scene from being rendered more then 1000
    // times a second ie 1000fps -- by limiting it like this we sneak
    // in some context switches so the CPU usage isn't through the
    // roof.

    long start = get_milli_time();

    if ( (start - last_renderscene_time) >= 1 ) {

        //msgout (DEBUG, "diff = %lu\n", start - last_renderscene_time);

        render_scene();

    } else {
        //sleep_ms(1);  // allow context switch
        context_switch();
    }


} // END idle_func
#endif

void 
acquire_desktop (int rv)
{
    if (disable_autoacquire || !cfg->autoacquire)
        return;

    int current_row = 0, current_col = 0;
    int row_count = 0, col_count = 0;

    vdesks.get_vdesktop_info (&current_col, &current_row, &col_count, &row_count);

    msgout (DEBUG, ")))) GRABBING %d x %d\n", current_col, current_row);

    if (GLWin.grab_screenshot_data() < 0) {
        msgout (ERROR, "couldn't grab screen image\n");
        end_program(-1);
    }

    time_of_last_acquire = time(NULL);
    
    face_set->load_texture_data(current_col + (current_row * col_count), 
                                cfg->texture_size,
                                GLWin.get_screenshot_data_ptr() );

    faces->make_display_list();

    if (cfg->autoacquire)
        alarm(cfg->autoacquire);

} // END acquire_desktop


static int 
main_loop(void)
{
    XEvent event;
    KeySym key;
    Bool quit = False;

    desktop_coords_t  goto_coords;
    desktop_coords_t  current_count;
    desktop_coords_t  current_coords;

    while(!quit) {

        goto_coords.column = NO_GOTO;
        goto_coords.column = NO_GOTO;
        goto_on_flag = 0;

        goto_coords = wait_for_activation();

        current_coords.column = faces->get_current_column();
        current_coords.row = faces->get_current_row();
        current_count.column = faces->get_number_of_columns();
        current_count.row = faces->get_number_of_rows();
        
        // last coords are used when you press L or in alt_mousebuttons
        last_coords = current_coords;

#ifndef SCAN_MODE
        if (do_screenshot &&
            (time(NULL) - time_of_last_acquire) > 1) {
            
            if (GLWin.grab_screenshot_data() < 0) {
                msgout (ERROR, "couldn't grab screen image\n");
                end_program(-1);
            }

            face_set->load_texture_data(current_coords.column + (current_coords.row * current_count.column), 
                                        cfg->texture_size,
                                        GLWin.get_screenshot_data_ptr() );

            faces->make_display_list();
        } 
        //else {
        //    msgout(DEBUG, "~~~~~~~ NOT GRABBING\n");
        //}
#endif

        if (cfg->options->do_fullscreen)
            GLWin.unhide_GL_window();

        initiate_destroy = 0;

        if (goto_coords.column != NO_GOTO
            || goto_coords.row != NO_GOTO) 
        {
            setup_goto_face(goto_coords, 
                            current_coords,
                            current_count);
        }

        SET_STATE(STATE_ENTRY);
        em.trigger_event (ENTRY_MOVEMENT_START);


        end_X_event_loop = False;

        // finally this is so the "fun" behavior doesn't start right away
        last_keypress = get_milli_time();
    
        // wait for events
        while (!end_X_event_loop)
        {
            // handle the events in the queue 
            while (XPending(GLWin.get_display()) > 0)
            {
                XNextEvent(GLWin.get_display(), &event);
                switch (event.type)
                {
                case Expose:
                    if (event.xexpose.count != 0)
                        break;
                    render_scene();
                    break;
                case ConfigureNotify:
                    // call resizeGLScene only if our window-size changed 
                    if (((unsigned int)event.xconfigure.width != GLWin.get_width()) || 
                        ((unsigned int)event.xconfigure.height != GLWin.get_height()))
                    {
                        GLWin.resize_scene(event.xconfigure.width,
                                           event.xconfigure.height);
                    }
                    break;
                    // exit in case of a mouse button press 
                case ButtonPress:
                    mouse_pressed (event.xbutton.button);
                    break;
                case KeyPress:
                    key = XLookupKeysym(&event.xkey, 0);
                    key_pressed (key);
                    break;
                case ClientMessage:
                    if (*XGetAtomName(GLWin.get_display(), event.xclient.message_type) == 
                        *"WM_PROTOCOLS")
                    {
                        msgout(INFO, "Exiting sanely...\n");
                        end_X_event_loop = True;
                        quit = True;
                    }

                    break;
                default:
                    break;
                }
            }

            if (cfg->options->use_context_switch)
                idle_func();
            else
                render_scene();

        }

        if (cfg->options->do_fullscreen)
            GLWin.hide_GL_window();

    }


    return 0;

}  // END main_loop




static void
gl_init (void)
{
    // Color to clear color buffer to.
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    // Depth to clear depth buffer to; type of test.
    glClearDepth(1.0);
    glDepthFunc(GL_LESS); 

    // Enables Smooth Color Shading; try GL_FLAT for (lack of) fun.
    glShadeModel(GL_SMOOTH);

    glLoadIdentity();

    // A handy trick -- have surface material mirror the color.
    //glColorMaterial(GL_FRONT_AND_BACK,GL_DIFFUSE);
    //glEnable(GL_COLOR_MATERIAL);

    //glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
    glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);

    //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);

    float ones[] = { 1.0, 1.0, 1.0, 1.0 };
    //float halfs[] = { .50, .50, .50, .50 };

    glMaterialfv(GL_FRONT, GL_DIFFUSE, ones);
    glMaterialfv(GL_FRONT, GL_SPECULAR, ones);
    glMaterialf(GL_FRONT, GL_SHININESS, 100.0);


#if 0
    float spot_direction[] = { 0.0, 0.0, -1.0 };
    glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction);
    glLighti(GL_LIGHT1, GL_SPOT_EXPONENT, 30);
    glLighti(GL_LIGHT1, GL_SPOT_CUTOFF, 180);
#endif


    if (cfg->options->use_breathing)
        breathing.init(&ambient_value, 1.7, 3.0, 200, Movement::MOVE_TYPE_SINE);
    else
        light_ambient2[0] = light_ambient2[1] = light_ambient2[2] = 2.0;

} // END gl_init



static void 
load_digits(void) 
{
        
    char filename[100];
    unsigned char *data;
    unsigned int sizex, sizey;
    
    extern Config *cfg;

    // load digits for screens we don't have yet


    // allocate space for texture
    data = new unsigned char [256 * 256 * 4];
    if (data == NULL) {
        msgout(ERROR, "Error allocating space for image");
        end_program(0);
    }
        
    // try loading /usr/share/3ddesktop/digits.bmp
    //             /usr/local/share/3ddesktop/digits.bmp
    //             ~/.3ddesktop/digits.bmp
    //             ./digits.bmp

    sprintf (filename, "%s/digits.bmp", cfg->options->base_dir);

    if (!load_image(filename, data, &sizex, &sizey, 1)) {

        sprintf (filename, SHAREDIR "/digits.bmp");

        if (!load_image(filename, data, &sizex, &sizey, 1)) {

            sprintf (filename, "%s/digits.bmp", cfg->working_dir);
            
            if (!load_image(filename, data, &sizex, &sizey, 1)) {
                
                sprintf (filename, "./digits.bmp");
                
                if (!load_image(filename, data, &sizex, &sizey, 1)) {
                    msgout (DEBUG, "no digits.bmp found\n");
                    cfg->options->show_digits = 0;
                    return ;
                }
            }
        }
    }
        
    // Create Texture
    glGenTextures(1, &digit_texture );
    glBindTexture(GL_TEXTURE_2D, digit_texture );   // 2d texture (x and y size)
        
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, MIN_FILTER);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, MAX_FILTER);
        
    // 2d texture, 3 components (red, green, blue), x size from image, y size from image, 
    // rgb color data, unsigned byte data, and finally the data itself.
    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, sizex, sizey, GL_RGBA, GL_UNSIGNED_BYTE, data);

    //glTexImage2D(GL_TEXTURE_2D, 0, 3, sizex, sizey, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
        
    delete data;

} // END load_digits



// accesses global cfg
static void
initialize_3ddesktop (void)
{

    // creates ~/.3ddesktop or /tmp/.3ddesktop-user if not alread there
    // used for pid file but no longer the screenshot
    //cfg->create_working_dir_if_necessary ();

    // there is a race between here and marking that I am running
    // but it should be small.  We must separate these because the
    // semaphore ownership is not inherited if we daemonize.
    if (check_if_i_am_running()) {
        msgout (ERROR, "3ddesktop is already running.  bye\n");
        exit (1);
    }
        
    if (vdesks.init (cfg) < 0) {
        msgout (ERROR, "Failed to initialize display.\n");
        exit(1);
    }

    int current_row = 0, current_col = 0;
    int row_count = 0, col_count = 0;

    vdesks.get_vdesktop_info (&current_col, &current_row, &col_count, &row_count);


#if 0
    if ((row_count * col_count) > MAX_FACES) {
        msgout (ERROR, "Can't support this many virtual desktops (%d), MAX is %d.\n", 
                row_count * col_count, MAX_FACES);
        end_program (1);
    }
#endif

#if 0
    if (row_count <= 1 && col_count <= 1) {
        msgout (ERROR, "You only have %d x %d virtual desktop(s)!  Need at least 2 to\n"
                "switch between them...\n", col_count, row_count);
        end_program (1);
    }
#endif

    // the actual acquire is done below but the message must be here,
    // before we daemonize
    if (cfg->options->acquire)
        msgout(INFO, 
               "\n"
               "================================================================\n"
               "3ddesktop will be acquiring images in one moment. Please wait...\n"
               "================================================================\n\n");


    // become a daemon 
    if (!cfg->verbose) {
        openlog ("3ddeskd", LOG_CONS, LOG_DAEMON);
        
        if (daemon_init() < 0) {
            msgout(ERROR, "Failed to daemonize.\n");
            end_program(-1);
        }

        cfg->options->daemonized = 1;
    }

    struct sigaction sa;
    sigset_t mask;

    sigemptyset (&mask);
    memset (&sa, 0, sizeof (struct sigaction));

    sa.sa_mask = mask;
    sa.sa_handler = end_program;

    sigaction (SIGTERM, &sa, NULL);
    sigaction (SIGINT, &sa, NULL);
    sigaction (SIGQUIT, &sa, NULL);

    sa.sa_mask = mask;
    sa.sa_handler = acquire_desktop;
    sigaction (SIGALRM, &sa, NULL);

    if (cfg->autoacquire)
        alarm(cfg->autoacquire);

    // open the messaging mechanism (message queue) we will listen on
    
    if (msgmgr.open(MessageManager::CREATE) < 0) {
        msgout(ERROR, "Could not create message queue! %s\n", strerror(errno));
        end_program(-1);
    }

    if (cfg->make_pidfile() < 0)
        end_program(-1);


    // initialize opengl

    // our own init function.
    gl_init();

    GLWin.open_display();


    GLWin.create_GL_window("Enhanced 3-dimensional workspace visualization magic machine", 
                           cfg->options->do_fullscreen);

    cfg->texture_size = GLWin.get_best_texture_size(cfg->texture_size);

    // Loads up the correct perspective matrix
    GLWin.resize_scene();

    if (cfg->options->do_fullscreen)
        GLWin.hide_GL_window();

    // this must be here after opengl is initialized since the FaceSet
    // constructor loads the digit textures.
    face_set = new FaceSet(row_count, col_count);  // never deleted
    if (face_set == NULL) {
        msgout (ERROR, "Out of memory: face_set\n");
        end_program(-1);
    }

    last_time = get_milli_time();

    initialize_arrangements();

    set_arrangement(cfg->options->default_z_offset_distance);

    // loop through desktops and capture their images...
    if (cfg->options->acquire) {

        sleep(1);

        int i, j;
        for (i = 0; i < row_count; i++) {

            for (j = 0; j < col_count; j++) {

                vdesks.set_vdesktop (j, i);

                if (cfg->options->acquire > 1) {
                    msgout(DEBUG, "sleeping %d...\n", cfg->options->acquire);
                    sleep_ms (cfg->options->acquire);
                }
                
                if (GLWin.grab_screenshot_data() < 0) {
                    msgout (ERROR, "couldn't grab screen image\n");
                    end_program(-1);
                }
                
                face_set->load_texture_data(j + (i * col_count), 
                                            cfg->texture_size,
                                            GLWin.get_screenshot_data_ptr() );
            }
        }

        // go back to current
        vdesks.set_vdesktop (current_col, current_row);

    }

    load_digits();

    if (flag_that_i_am_running()) {
        // (if we've daemonized this goes to syslog which is not easily seen)
        msgout(ERROR, "Oops, someone beat us to start up!!  Amazing!\n");
        end_program(-1);
    }

    em.add_event (ENTRY_MOVEMENT_START, entry_move_start_hook, NULL, 1);
    em.add_event (EXIT_MOVEMENT_START, exit_move_start_hook, NULL, 1);
                      
} //  END initialize_3ddesktop



int main(int argc,char **argv)
{
    cfg = new Config();   // deleted in end_program()
    if (!cfg) {
        msgout (ERROR, "new Config: out of memory\n");
        end_program(-1);
    }

    cfg->init (argc, argv);

    cfg->load_conf();

    initialize_3ddesktop();

    int rc = setpriority(PRIO_PROCESS, 0, cfg->priority);
    if (rc < 0) {
        msgout(DEBUG, "~~~~~ failed to set priority %d\n", cfg->priority);
    }

    return main_loop();

}  // END main