summaryrefslogtreecommitdiff
path: root/beryl-plugins/src/wall.c
blob: 6640b178c0bbbfc6527b74056bbd011ab73df66f (plain)
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
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
/**
 *
 * Beryl wall plugin
 *
 * wall.c
 *
 * Copyright (c) 2006 Robert Carr <racarr@beryl-project.org>
 *
 * Authors:
 * Robert Carr <racarr@beryl-project.org>
 * Dennis Kasprzyk <onestone@beryl-project.org>
 *
 * This program 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
 * of the License, or (at your option) any later version.
 *
 * This program 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.
 *
 **/
/*
Wall Features:
	Drag and drop of viewports (low priority) No spring physics as in group DnD, simple slide back and fade and the viewports are swapped, or physically swap the viewports, no more than two viewports should ever be moving.

	Move with window keyboard shortcuts (high priority)

	Snapping to wall edges (medium priority)

	Animation that zooms from current viewport out and back in to new viewport (medium priority) - Cornelius is already working on this

    ****************************************************************************************************
    *	If you plan on adding a feature not on the above list please bring it up for discussion first. *
    ****************************************************************************************************

Known Bugs:
	Current behavior with input zoom is screwy... (disable input zoom?)

*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/time.h>

#include <beryl.h>
#include <beryl_helpers.h>

#include <GL/glu.h>

#define PI 3.14159265359f

#define WALL_EXPO_KEY_DEFAULT "e"
#define WALL_EXPO_MODIFIERS_DEFAULT (CompSuperMask)

#define WALL_STICKY_KEY_DEFAULT "s"
#define WALL_STICKY_MODIFIERS_DEFAULT (CompSuperMask | CompAltMask)

#define WALL_MOVE_MODIFIERS_DEFAULT (CompAltMask | ControlMask)
#define WALL_MOVE_WITH_WINDOW_MODIFIERS_DEFAULT \
									(CompAltMask | ControlMask | ShiftMask)

#define WALL_LEFT_KEY_DEFAULT "Left"
#define WALL_RIGHT_KEY_DEFAULT "Right"
#define WALL_UP_KEY_DEFAULT "Up"
#define WALL_DOWN_KEY_DEFAULT "Down"
#define WALL_NEXT_KEY_DEFAULT "Page_Down"
#define WALL_PREV_KEY_DEFAULT "Page_Up"

#define WALL_ROTATE_DEFAULT FALSE

#define WALL_SLIDE_DURATION_DEFAULT		0.3f
#define WALL_SLIDE_DURATION_MIN			0.1f
#define WALL_SLIDE_DURATION_MAX			5.0f
#define WALL_SLIDE_DURATION_PRECISION	0.05f

#define WALL_PREVIEW_TIMEOUT_DEFAULT		0.4f
#define WALL_PREVIEW_TIMEOUT_MIN			0.0f
#define WALL_PREVIEW_TIMEOUT_MAX			2.0f
#define WALL_PREVIEW_TIMEOUT_PRECISION		0.05f

#define WALL_UP_WHEEL_DEFAULT Button4
#define WALL_DOWN_WHEEL_DEFAULT Button5
#define WALL_WHEEL_MODIFIERS_DEFAULT 0

#define WALL_MIPMAPS_DEFAULT FALSE
#define WALL_ASPECT_DEFAULT 1.0f
#define WALL_MINISCREEN_DEFAULT FALSE
#define WALL_ALLOW_WRAPAROUND_DEFAULT FALSE

#define WIN_X(w) ((w)->attrib.x - (w)->input.left)
#define WIN_Y(w) ((w)->attrib.y - (w)->input.top)
#define WIN_W(w) ((w)->width + (w)->input.left + (w)->input.right)
#define WIN_H(w) ((w)->height + (w)->input.top + (w)->input.bottom)

/* Unimplemented increase/decrease */

static int displayPrivateIndex;

#define WALL_DISPLAY_OPTION_EXPO				0
#define WALL_DISPLAY_OPTION_ROTATE				1
#define WALL_DISPLAY_OPTION_STICKY				2
#define WALL_DISPLAY_OPTION_EXPO_ANIMATION		3
#define WALL_DISPLAY_OPTION_LEFT				4
#define WALL_DISPLAY_OPTION_RIGHT				5
#define WALL_DISPLAY_OPTION_UP					6
#define WALL_DISPLAY_OPTION_DOWN				7
#define WALL_DISPLAY_OPTION_WINDOW_LEFT			8
#define WALL_DISPLAY_OPTION_WINDOW_RIGHT		9
#define WALL_DISPLAY_OPTION_WINDOW_UP			10
#define WALL_DISPLAY_OPTION_WINDOW_DOWN			11
#define WALL_DISPLAY_OPTION_NEXT				12
#define WALL_DISPLAY_OPTION_PREV				13
#define WALL_DISPLAY_OPTION_WHEEL_UP			14
#define WALL_DISPLAY_OPTION_WHEEL_DOWN			15
#define WALL_DISPLAY_OPTION_SLIDE_DURATION		16
#define WALL_DISPLAY_OPTION_MIPMAPS				17
#define WALL_DISPLAY_OPTION_ASPECT				18
#define WALL_DISPLAY_OPTION_MINISCREEN			19
#define WALL_DISPLAY_OPTION_PREVIEW_TIMEOUT		20
#define WALL_DISPLAY_OPTION_ALLOW_WRAPAROUND	21
#define WALL_DISPLAY_OPTION_NUM					22

/* Enums */
enum Direction
{
	UpLeft = 0,
	UpRight,
	DownLeft,
	DownRight
};

enum DnDState
{
	DnDNone = 0,
	DnDDuring,
	DnDStart
};

typedef enum
{
	ExpoAnimationZoom = 0,
	ExpoAnimationFadeZoom,
	ExpoAnimationVortex,
	ExpoAnimationNum
} ExpoAnimation;

#define WALL_EXPO_ANIMATION_DEFAULT   ExpoAnimationZoom

static char *expoAnimationName[] = {
	N_("Zoom"),
	N_("Fade + Zoom"),
	N_("Vortex")
};

typedef struct _WallDisplay
{
	int screenPrivateIndex;
	HandleEventProc handleEvent;

	CompOption opt[WALL_DISPLAY_OPTION_NUM];

	ExpoAnimation expoAnimationType; 

} WallDisplay;

typedef struct _WallScreen
{
	int windowPrivateIndex;

	DonePaintScreenProc donePaintScreen;
	PaintScreenProc paintScreen;
	PreparePaintScreenProc preparePaintScreen;
	PaintTransformedScreenProc paintTransformedScreen;
	DrawWindowProc drawWindow;
	WindowGrabNotifyProc windowGrabNotify;
	WindowUngrabNotifyProc windowUngrabNotify;

	int moveStep;
	float expoCam;				// Used for expo zoom animation
	Bool expoActive;

	float size;					// Used to store zoom out amount of miniview, if someone added bindings to change this miniview would be resizable, greater size = smaller miniview

	Bool moving;				// Used to track miniview movement
	Bool expoMode;				// In expo mode?

	int pointerX;				// Updated in donePaintScreen with XQueryPointer if pointer location is neccesary i.e. moving miniview
	int pointerY;

	int grabIndex;				// For expo grab

	GLint viewport[4];

	int dndState;
	CompWindow *dndWindow;		// Window being dragged in expo mode
	int prevCursorX, prevCursorY;
	int newCursorX, newCursorY;
	CompWindow *stickyWindow;
	int origVX;
	int origVY;
	int rorigx;
	int rorigy;
	int mouseOverViewX;
	int mouseOverViewY;

	int destx;
	int desty;
	int curx;
	int cury;

	int timer;

	Bool anyClick;
	int boxTimeout;

	CompWindow *grabbedWindow;

	Window moveWindow;

	Bool miniScreen;
	WindowPaintAttrib mSAttribs;
	
	int moveWindowX;
	int moveWindowY;

} WallScreen;

typedef struct _WallWindow
{
	Bool hovered;				// Is a window hovered over in expo mode?
	Bool skipNotify;
	int origx;
	int origy;
} WallWindow;

typedef struct _xyz_tuple
{
	float x, y, z;
} Point3d;


/* Helpers */
#define GET_WALL_DISPLAY(d) \
		((WallDisplay *) (d)->privates[displayPrivateIndex].ptr)
#define WALL_DISPLAY(d) \
		WallDisplay *wd = GET_WALL_DISPLAY(d);

#define GET_WALL_SCREEN(s, wd) \
		((WallScreen *) (s)->privates[(wd)->screenPrivateIndex].ptr)
#define WALL_SCREEN(s) \
		WallScreen *ws = GET_WALL_SCREEN(s, GET_WALL_DISPLAY(s->display))

#define GET_WALL_WINDOW(w, ws)                                     \
        ((WallWindow *) (w)->privates[(ws)->windowPrivateIndex].ptr)
#define WALL_WINDOW(w)    \
        WallWindow *ww = GET_WALL_WINDOW  (w,                     \
                GET_WALL_SCREEN  (w->screen,             \
                        GET_WALL_DISPLAY (w->screen->display)))

#define NUM_OPTIONS(d) (sizeof ((d->opt)) / sizeof (CompOption))

#define GET_SCREEN \
 	CompScreen *s;\
	Window xid; \
	xid = getIntOptionNamed(option, nOption, "root", 0); \
	s = findScreenAtDisplay(d, xid)

#define sigmoid(x) (1.0f/(1.0f+exp(-5.5f*2*((x)-0.5))))
#define sigmoidProgress(x) ((sigmoid(x) - sigmoid(0)) / \
							(sigmoid(1) - sigmoid(0)))

static Bool wallCheckDestination(CompScreen * s, int destX, int destY)
{
	if (s->x - destX < 0)
		return FALSE;

	if (s->x - destX >= s->hsize)
		return FALSE;

	if (s->y - destY >= s->vsize)
		return FALSE;

	if (s->y - destY < 0)
		return FALSE;

	return TRUE;
}

static void wallReleaseMoveWindow (CompScreen * s)
{
	CompWindow *w;
	WALL_SCREEN(s);
	w = findWindowAtScreen (s, ws->moveWindow);
	if (w)
		syncWindowPosition (w);
	ws->moveWindow = 0;
}

static void wallMoveViewport(CompScreen * s, int x, int y, Window moveWindow)
{
	int newDestX = MAXSHORT;
	int newDestY = MAXSHORT;

	if (!x && !y)
		return;

	WALL_SCREEN(s);
	WALL_DISPLAY(s->display);

	if (!ws->expoMode)
	{
		if (!ws->timer)
		{
			ws->curx = 0;
			ws->cury = 0;

			newDestX = x;
			newDestY = y;
		}
      else
      {
		return;
      }

		if (wallCheckDestination(s, newDestX, newDestY))
		{
	
			float newTimeout = wd->opt[WALL_DISPLAY_OPTION_SLIDE_DURATION].value.f * 1000.0f * (float)(MAX(abs(newDestY), abs(newDestX)));

			ws->destx = newDestX;
			ws->desty = newDestY;
			ws->moving = TRUE;
			ws->timer = (int)newTimeout - ws->timer;
			if (ws->moveWindow != moveWindow)
			{
				CompWindow *w;
				wallReleaseMoveWindow (s);
				w = findWindowAtScreen(s, moveWindow);
				if (w)
				{
					if (!(w->type & (CompWindowTypeDesktopMask |
									 CompWindowTypeDockMask)))
					{
						if (!(w->state & CompWindowStateStickyMask))
						{
							ws->moveWindow = w->id;
							ws->moveWindowX = w->attrib.x;
							ws->moveWindowY = w->attrib.y;
							raiseWindow (w);
						}
					}
				}
			}
		}
	}

	if (ws->moving)
		ws->boxTimeout = wd->opt[WALL_DISPLAY_OPTION_PREVIEW_TIMEOUT].value.f * 1000;

	damageScreen(s);
}

static void wallHandleEvent(CompDisplay * d, XEvent * event)
{
	WALL_DISPLAY(d);
	CompScreen *s;

	WallScreen *ws;

	switch (event->type)
	{
	case ButtonPress:
		s = findScreenAtDisplay(d, event->xbutton.root);
		ws = GET_WALL_SCREEN(s, wd);
		ws->anyClick = TRUE;
		if (ws->expoMode)
		{
			if (event->xbutton.button == Button1)
				ws->dndState = DnDStart;
			else if (event->xbutton.button != Button5)
			{
				CompWindow *w;

				for (w = s->windows; w; w = w->next)
					syncWindowPosition(w);
				removeScreenGrabKeyboardOptional(s, ws->grabIndex, 0, FALSE);
				ws->grabIndex = 0;
				if (ws->stickyWindow)
				{
					WALL_WINDOW(ws->stickyWindow);
					moveWindow(ws->stickyWindow,
							   s->workArea.x + ww->origx -
							   ws->stickyWindow->input.left - 20,
							   s->workArea.y + ww->origy -
							   ws->stickyWindow->input.top - 20, TRUE, TRUE);
					syncWindowPosition(ws->stickyWindow);
				}
				ws->origVX = ws->mouseOverViewX;
				ws->origVY = ws->mouseOverViewY;
				ws->expoMode = FALSE;
				damageScreen(s);
				while (s->x != ws->mouseOverViewX)
					moveScreenViewport(s, 1, 0, TRUE);
				while (s->y != ws->mouseOverViewY)
					moveScreenViewport(s, 0, 1, TRUE);

				focusDefaultWindow(s);
			}
		}
		break;
	case ButtonRelease:
		s = findScreenAtDisplay(d, event->xbutton.root);
		ws = GET_WALL_SCREEN(s, wd);

		if (ws->dndState == DnDDuring || ws->dndState == DnDStart)
		{
			if (ws->dndWindow)
			{
				syncWindowPosition(ws->dndWindow);
				(*s->windowUngrabNotify)(ws->dndWindow);
			}

			ws->dndState = DnDNone;
			ws->dndWindow = NULL;
		}
		break; // FIXME: I'm not sure this break is needed, but I think so
	case ClientMessage:
		if (event->xclient.message_type == d->winActiveAtom)
		{
			CompWindow *w;

			w = findWindowAtDisplay(d, event->xclient.window);
			if (w)
			{
				int dx, dy;

				s = w->screen;

				/* window must be placed */
				if (!w->placed)
					break;

				if (otherScreenGrabExist
					(s, "plane", "switcher", "cube", "scale", 0))
					break;

				defaultViewportForWindow(w, &dx, &dy);
				dx -= s->x;
				dy -= s->y;
				if (dx || dy)
					wallMoveViewport(s, -dx, -dy, None);
			}
		}
		else if (event->xclient.message_type == d->desktopViewportAtom)
		{
			int dx, dy;

			s = findScreenAtDisplay(d, event->xclient.window);
			if (!s)
				break;

			if (otherScreenGrabExist
				(s, "plane", "switcher", "cube", "scale", 0))
				break;

			dx = event->xclient.data.l[0] / s->width - s->x;
			dy = event->xclient.data.l[1] / s->height - s->y;

			if (!dx && !dy)
				break;
			wallMoveViewport(s, -dx, -dy, None);
		}
		break;
	}

	UNWRAP(wd, d, handleEvent);
	(*d->handleEvent) (d, event);
	WRAP(wd, d, handleEvent, wallHandleEvent);
}

static Bool wallNext(CompDisplay * d, CompAction * action,
					 CompActionState state, CompOption * option, int nOption)
{
	GET_SCREEN;
	WALL_SCREEN(s);

	if (ws->expoCam == 0.0f)
	{
		if ((s->x == s->hsize - 1) && (s->y == s->vsize - 1))
			wallMoveViewport(s, s->hsize - 1, s->vsize - 1, None);
		else if (s->x == s->hsize - 1)
			wallMoveViewport(s, s->hsize - 1, -1, None);
		else
			wallMoveViewport(s, -1, 0, None);
	}

	return FALSE;
}

static Bool wallPrev(CompDisplay * d, CompAction * action,
					 CompActionState state, CompOption * option, int nOption)
{
	GET_SCREEN;
	WALL_SCREEN(s);

	if (ws->expoCam == 0.0f)
	{
		if ((s->x == 0) && (s->y == 0))
			wallMoveViewport(s, -(s->hsize - 1), -(s->vsize - 1), None);
		else if (s->x == 0)
			wallMoveViewport(s, -(s->hsize - 1), 1, None);
		else
			wallMoveViewport(s, 1, 0, None);
	}

	return FALSE;
}

static Bool wallUpWheel(CompDisplay * d, CompAction * action,
						CompActionState state, CompOption * option,
						int nOption)
{
	GET_SCREEN;
	int winX, winY;
	int rootX, rootY;
	unsigned int mask_return;
	Window root_return;
	Window child_return;

	XQueryPointer(s->display->display, s->root,
				  &root_return, &child_return,
				  &rootX, &rootY, &winX, &winY, &mask_return);

	if (pointerOnlyOnDesktop(s, rootX, rootY))
		wallPrev(d, action, state, option, nOption);

	return FALSE;
}

static Bool wallDownWheel(CompDisplay * d, CompAction * action,
						  CompActionState state, CompOption * option,
						  int nOption)
{
	GET_SCREEN;
	int winX, winY;
	int rootX, rootY;
	unsigned int mask_return;
	Window root_return;
	Window child_return;

	XQueryPointer(s->display->display, s->root,
				  &root_return, &child_return,
				  &rootX, &rootY, &winX, &winY, &mask_return);

	if (pointerOnlyOnDesktop(s, rootX, rootY))
		wallNext(d, action, state, option, nOption);

	return FALSE;
}

static Bool wallLeft(CompDisplay * d, CompAction * action,
					 CompActionState state, CompOption * option, int nOption)
{
	GET_SCREEN;
	WALL_DISPLAY(d);

	if (wd->opt[WALL_DISPLAY_OPTION_ALLOW_WRAPAROUND].value.b && (s->x == 0))
		wallMoveViewport(s, -(s->hsize - 1), 0, None);
	else
		wallMoveViewport(s, 1, 0, None);

	return FALSE;
}

static Bool wallRight(CompDisplay * d, CompAction * action,
					  CompActionState state, CompOption * option, int nOption)
{
	GET_SCREEN;
	WALL_DISPLAY(d);

	if (wd->opt[WALL_DISPLAY_OPTION_ALLOW_WRAPAROUND].value.b && (s->x == s->hsize - 1))
		wallMoveViewport(s, s->hsize - 1, 0, None);
	else
		wallMoveViewport(s, -1, 0, None);

	return FALSE;
}

static Bool wallUp(CompDisplay * d, CompAction * action,
				   CompActionState state, CompOption * option, int nOption)
{
	GET_SCREEN;
	WALL_DISPLAY(d);

	if (wd->opt[WALL_DISPLAY_OPTION_ALLOW_WRAPAROUND].value.b && (s->y == 0))
		wallMoveViewport(s, 0, -(s->vsize - 1), None);
	else
		wallMoveViewport(s, 0, 1, None);

	return FALSE;
}

static Bool wallDown(CompDisplay * d, CompAction * action,
					 CompActionState state, CompOption * option, int nOption)
{
	GET_SCREEN;
	WALL_DISPLAY(d);

	if (wd->opt[WALL_DISPLAY_OPTION_ALLOW_WRAPAROUND].value.b && (s->y == s->vsize - 1))
		wallMoveViewport(s, 0, s->vsize - 1, None);
	else
		wallMoveViewport(s, 0, -1, None);

	return FALSE;
}

static Bool wallLeftWithWindow (CompDisplay *d, CompAction * action,
								CompActionState state, CompOption * option,
								int nOption)
{
	GET_SCREEN;
	Window win = getIntOptionNamed (option, nOption, "window", 0);
	WALL_DISPLAY(d);

	if (wd->opt[WALL_DISPLAY_OPTION_ALLOW_WRAPAROUND].value.b && (s->x == 0))
		wallMoveViewport(s, -(s->hsize - 1), 0, win);
	else
		wallMoveViewport(s, 1, 0, win);

	return FALSE;
}

static Bool wallRightWithWindow (CompDisplay *d, CompAction * action,
								 CompActionState state, CompOption * option,
								 int nOption)
{
	GET_SCREEN;
	Window win = getIntOptionNamed (option, nOption, "window", 0);
	WALL_DISPLAY(d);

	if (wd->opt[WALL_DISPLAY_OPTION_ALLOW_WRAPAROUND].value.b && (s->x == s->hsize - 1))
		wallMoveViewport(s, s->hsize - 1, 0, win);
	else
		wallMoveViewport(s, -1, 0, win);

	return FALSE;
}

static Bool wallUpWithWindow (CompDisplay *d, CompAction * action,
							  CompActionState state, CompOption * option,
							  int nOption)
{
	GET_SCREEN;
	Window win = getIntOptionNamed (option, nOption, "window", 0);
	WALL_DISPLAY(d);

	if (wd->opt[WALL_DISPLAY_OPTION_ALLOW_WRAPAROUND].value.b && (s->y == 0))
		wallMoveViewport(s, 0, -(s->vsize - 1), win);
	else
		wallMoveViewport(s, 0, 1, win);

	return FALSE;
}

static Bool wallDownWithWindow (CompDisplay *d, CompAction * action,
								CompActionState state, CompOption * option,
								int nOption)
{
	GET_SCREEN;
	Window win = getIntOptionNamed (option, nOption, "window", 0);
	WALL_DISPLAY(d);

	if (wd->opt[WALL_DISPLAY_OPTION_ALLOW_WRAPAROUND].value.b && (s->y == s->vsize - 1))
		wallMoveViewport(s, 0, s->vsize - 1, win);
	else
		wallMoveViewport(s, 0, -1, win);

	return FALSE;
}

static Bool wallExpo(CompDisplay * d, CompAction * action,
					 CompActionState state, CompOption * option, int nOption)
{
	GET_SCREEN;
	WALL_SCREEN(s);

	ws->expoMode = !ws->expoMode;
	ws->anyClick = FALSE;
	if (ws->expoMode)
		ws->grabIndex =
				pushScreenGrabKeyboardOptional(s, None, "expo", FALSE);
	else if (ws->grabIndex)
		removeScreenGrabKeyboardOptional(s, ws->grabIndex, 0, FALSE);

	if (ws->stickyWindow)
	{
		WALL_WINDOW(ws->stickyWindow);
		moveWindow(ws->stickyWindow,
				   s->workArea.x + ww->origx - ws->stickyWindow->input.left -
				   20,
				   s->workArea.y + ww->origy - ws->stickyWindow->input.top -
				   20, TRUE, TRUE);
		syncWindowPosition(ws->stickyWindow);
	}

	ws->stickyWindow = 0;

	if (ws->dndWindow)
		syncWindowPosition(ws->dndWindow);
	ws->dndState = DnDNone;
	ws->dndWindow = 0;

	if (!ws->expoMode && ws->origVX >= 0 && ws->origVY >= 0)
	{
		while (s->x != ws->origVX)
			moveScreenViewport(s, 1, 0, TRUE);
		while (s->y != ws->origVY)
			moveScreenViewport(s, 0, 1, TRUE);
	}
	if (ws->expoMode)
	{
		ws->origVX = -1;
		ws->origVY = -1;
		ws->rorigx = s->x;
		ws->rorigy = s->y;
	}
	damageScreen(s);

	focusDefaultWindow(s);

	return FALSE;
}

static Bool wallSticky(CompDisplay * d, CompAction * action,
					   CompActionState state, CompOption * option,
					   int nOption)
{
	GET_SCREEN;
	WALL_SCREEN(s);
	if (ws->stickyWindow)
	{
		WALL_WINDOW(ws->stickyWindow);
		moveWindow(ws->stickyWindow,
				   s->workArea.x + ww->origx - ws->stickyWindow->input.left -
				   20,
				   s->workArea.y + ww->origy - ws->stickyWindow->input.top -
				   20, TRUE, TRUE);
		syncWindowPosition(ws->stickyWindow);
		ws->dndWindow = NULL;
		ws->dndState = DnDNone;
	}
	if (ws->expoMode)
	{
		CompWindow *w = findWindowAtDisplay(d, d->activeWindow);

		if (w)
			syncWindowPosition(w);
		if (w && !ws->stickyWindow)
			ws->stickyWindow = w;
		else if (ws->stickyWindow)
			ws->stickyWindow = 0;
	}

	if (ws->stickyWindow)
	{
		WALL_WINDOW(ws->stickyWindow);
		ww->origx = ws->stickyWindow->serverX;
		ww->origy = ws->stickyWindow->serverY;
		moveWindow(ws->stickyWindow,
				   -ws->stickyWindow->serverX + ws->stickyWindow->input.left +
				   20 + s->workArea.x,
				   s->workArea.y + -ws->stickyWindow->serverY +
				   ws->stickyWindow->input.top + 20, TRUE, TRUE);
		syncWindowPosition(ws->stickyWindow);
		ws->dndState = DnDNone;
		ws->dndWindow = NULL;

		raiseWindow(ws->stickyWindow);
		moveInputFocusToWindow(ws->stickyWindow);
	}

	return FALSE;
}

//Other way around
static void invertTransformedVertex(CompScreen * s, const ScreenPaintAttrib * sAttrib,
						   const CompTransform *transform,
						   int output, int vertex[2])
{
	//Projection Matrix.
	WALL_SCREEN(s);

	CompTransform     sTransform = *transform;

	(s->applyScreenTransform) (s, sAttrib, output, &sTransform);
	transformToScreenSpace (s, output, -sAttrib->zTranslate, &sTransform);

	glGetIntegerv(GL_VIEWPORT, ws->viewport);

	GLdouble p1[3];
	GLdouble p2[3];
	GLdouble v[3];
	GLdouble alpha;
	int i;

	GLdouble mvm[16];
	GLdouble pm[16];

	for (i = 0; i < 16; i++)
	{
		mvm[i] = sTransform.m[i];
		pm[i] = s->projection[i];
	}

 	gluUnProject(vertex[0], s->height - vertex[1], 0, mvm, pm,
				 ws->viewport, &p1[0], &p1[1], &p1[2]);
	gluUnProject(vertex[0], s->height - vertex[1], -1.0, mvm, pm,
				 ws->viewport, &p2[0], &p2[1], &p2[2]);

	for (i = 0; i < 3; i++)
		v[i] = p1[i] - p2[i];

	alpha = -p1[2] / v[2];

	vertex[0] = ceil(p1[0] + (alpha * v[0]));
	vertex[1] = ceil(p1[1] + (alpha * v[1]));
}


static void wallWindowUngrabNotify(CompWindow * w)
{
	WALL_SCREEN(w->screen);

	if (w == ws->grabbedWindow)
		ws->grabbedWindow = NULL;

	UNWRAP(ws, w->screen, windowUngrabNotify);
	(*w->screen->windowUngrabNotify) (w);
	WRAP(ws, w->screen, windowUngrabNotify, wallWindowUngrabNotify);
}

static void wallWindowGrabNotify(CompWindow * w,
								 int x, int y, unsigned int state,
								 unsigned int mask)
{
	WALL_SCREEN(w->screen);

	if (!ws->grabbedWindow && (mask & CompWindowGrabMoveMask))
		ws->grabbedWindow = w;

	UNWRAP(ws, w->screen, windowGrabNotify);
	(*w->screen->windowGrabNotify) (w, x, y, state, mask);
	WRAP(ws, w->screen, windowGrabNotify, wallWindowGrabNotify);
}

static Bool wallPaintScreen(CompScreen * s,
							const ScreenPaintAttrib * sAttrib,
							const CompTransform    *transform,
							Region region, int output, unsigned int mask)
{
	Bool status;

	WALL_SCREEN(s);

	ws->miniScreen = FALSE;

	if (ws->expoCam > 0.0 || ws->moving)	// Miniview or expo?
	{
		mask |= PAINT_SCREEN_TRANSFORMED_MASK;
	}

	UNWRAP(ws, s, paintScreen);
	status = (*s->paintScreen) (s, sAttrib, transform, region, output, mask);
	WRAP(ws, s, paintScreen, wallPaintScreen);

	if (ws->expoMode)
	{
		CompTransform     sTransform = *transform;

		transformToScreenSpace (s, output, -DEFAULT_Z_CAMERA, &sTransform);

		if (ws->stickyWindow)
			s->paintWindow(ws->stickyWindow, &ws->stickyWindow->paint,
						   &sTransform, getInfiniteRegion(), 0);

	}



	if ((ws->moving || ws->boxTimeout) &&
		(s->nOutputDev == 1 || output == screenGetCurrentOutputDev(s)))
	{
		WALL_DISPLAY(s->display);
		glDisableClientState(GL_TEXTURE_COORD_ARRAY);

		CompTransform     sTransform = *transform;

		transformToScreenSpace (s, output, -DEFAULT_Z_CAMERA, &sTransform);
		glPushMatrix ();
    	glLoadMatrixf (sTransform.m);


		int hsize = s->hsize;
		int vsize = s->vsize;
		float centerx =
				s->outputDev[output].region.extents.x1 +
				(s->outputDev[output].width / 2);
		float centery =
				s->outputDev[output].region.extents.y1 +
				(s->outputDev[output].height / 2);

		float topleftx = centerx - (hsize * 50);
		float width = 2 * (hsize * 50);
		float toplefty = centery - (vsize * 50);
		float height = 2 * (vsize * 50);




		if (s->width > s->height)
			width += width * (1 - ((float)s->height / (float)s->width));

		if (s->height > s->width)
			height += height * (1 - ((float)s->width / (float)s->height));

		float border = 10.0f;

		topleftx -= (s->hsize + 2) * border;	// Yes, both of these are right
		toplefty += (s->vsize + 1) * (border / 2.0f);



		float viewportWidth = (width - border * (hsize + 2)) / (float)hsize;
		float viewportHeight = (height - border * (vsize + 2)) / (float)vsize;

		width -= border * (hsize + 1);
		height -= border * (vsize + 1);

		float firstviewX = topleftx;
		float firstviewY = toplefty;






		glEnable(GL_BLEND);
		glColor4f(0.8, 0.8, 0.8, 0.85);	// Back rect color
		glLineWidth(2.0f);

		//glRectf(topleftx+border,toplefty+height-border,topleftx+width-border,toplefty+border);
		glRectf(topleftx + border, toplefty + border,
				topleftx + width - border, toplefty);
		glRectf(topleftx, toplefty + height - border, topleftx + border,
				toplefty + border);
		glRectf(topleftx + width - border, toplefty + height - border,
				topleftx + width, toplefty + border);
		glRectf(topleftx + border, toplefty + height,
				topleftx + width - border, toplefty + height - border);
		int i;

		for (i = 1; i < vsize; i++)
		{
			int m;

			for (m = 0; m < hsize; m++)
				glRectf(topleftx + border + viewportWidth * m,
						toplefty + viewportHeight * i + border,
						topleftx + viewportWidth + viewportWidth * m,
						toplefty + viewportHeight * i);

		}

		for (i = 1; i < hsize; i++)
			glRectf(topleftx + viewportWidth * i, toplefty + height - border,
					topleftx + border + viewportWidth * i, toplefty + border);

		glTranslatef(topleftx + border, toplefty + border, 0.0f);
#define CORNER(a,b) \
		for (k = a; k < b; k+=1) \
		{\
				float rad = k*(3.14159/180.0f);\
				glVertex2f(0.0f,0.0f);\
				glVertex2f(cos(rad)*border,sin(rad)*border);\
				glVertex2f(cos((k-1)*(3.14159/180.0f))*border,sin((k-1)*(3.14159/180.0f))*border);\
		}
		glBegin(GL_TRIANGLES);


		int k;

		CORNER(180, 270) glEnd();
		int j;

		glTranslatef(-(topleftx + border), -(toplefty + border), 0.0f);

		glTranslatef(topleftx + width - border, toplefty + border, 0.0f);
		glBegin(GL_TRIANGLES);
		CORNER(270, 360) glEnd();
		glTranslatef(-(topleftx + width - border), -(toplefty + border),
					 0.0f);

		glTranslatef(topleftx + border, toplefty + height - border, 0.0f);
		glBegin(GL_TRIANGLES);
		CORNER(90, 180) glEnd();
		glTranslatef(-(topleftx + border), -(toplefty + height - border),
					 0.0f);

		glTranslatef((topleftx + width - border),
					 (toplefty + height - border), 0.0f);
		glBegin(GL_TRIANGLES);
		CORNER(0, 90) glEnd();
		glTranslatef(-(topleftx + width - border),
					 -(toplefty + height - border), 0.0f);

		glColor4f(0.1, 0.1, 0.1, 0.7);	// Highlighted color
		if (ws->moving)
		{

			glPushMatrix();
			if (ws->destx < 0 && ws->desty == 0)
			{
				glTranslatef(firstviewX + width + border * 3,
							 firstviewY + height / 2.0f, 0.0f);
			}
			if (ws->destx > 0 && ws->desty == 0)
			{
				glTranslatef(firstviewX - border * 3,
							 firstviewY + height / 2.0f, 0.0f);
				glRotatef(180, 0.0f, 0.0f, 1.0f);
			}
			if (ws->desty > 0 && ws->destx == 0)
			{
				glTranslatef(firstviewX + width / 2.0f,
							 firstviewY - border * 3, 0.0f);
				glRotatef(-90, 0.0f, 0.0f, 1.0f);
			}
			if (ws->desty < 0 && ws->destx == 0)
			{
				glTranslatef(firstviewX + width / 2.0f,
							 firstviewY + height + border * 3, 0.0f);
				glRotatef(90, 0.0f, 0.0f, 1.0f);
			}
			if (ws->desty > 0 && ws->destx > 0)
			{
				glTranslatef(firstviewX - width / 8.0f,
							 firstviewY - height / 8.0f - border, 0.0f);
				glRotatef(-135, 0.0f, 0.0f, 1.0f);
			}
			if (ws->desty > 0 && ws->destx < 0)
			{
				glTranslatef(firstviewX + width + width / 8.0f,
							 firstviewY - height / 8.0f - border, 0.0f);
				glRotatef(-45, 0.0f, 0.0f, 1.0f);
			}
			if (ws->desty < 0 && ws->destx > 0)
			{
				glTranslatef(firstviewX - width / 8.0f,
							 firstviewY + height + height / 8.0f + border,
							 0.0f);
				glRotatef(135, 0.0f, 0.0f, 1.0f);
			}
			if (ws->desty < 0 && ws->destx < 0)
			{
				glTranslatef(firstviewX + width + width / 8.0f,
							 firstviewY + height + height / 8.0f + border,
							 0.0f);
				glRotatef(45, 0.0f, 0.0f, 1.0f);
			}
			glBegin(GL_TRIANGLES);
			glVertex2f(-s->width / 60.0f, -s->width / 60.0f);
			glVertex2f(-s->width / 60.0f, s->width / 60.0f);
			glVertex2f(s->width / 60.0f, 0);
			glEnd();
			glPopMatrix();
		}
		glColor4f(0.1, 0.1, 0.2, 0.08);	// Viewport colors

		if (!wd->opt[WALL_DISPLAY_OPTION_MINISCREEN].value.b)
		{
			for (j = 0; j < vsize; j++)
			{
				for (i = 0; i < hsize; i++)
				{
					glColor4f(0.2, 0.2, 0.2, 0.5);	// Viewport colors


					if (i == s->x - ws->destx && j == s->y - ws->desty &&
						ws->moving)
					{
						glColor4f(0.1, 0.1, 0.1, 0.7);	// Highlighted color
					}
					if (ws->boxTimeout && !ws->moving && i == s->x &&
						j == s->y)
						glColor4f(0.1, 0.1, 0.1, 0.7);	// Highlighted color


					glRectf(firstviewX + (i * viewportWidth) + border,
							firstviewY + (j * viewportHeight) +
							viewportHeight,
							firstviewX + (i * viewportWidth) + viewportWidth,
							border + firstviewY + (j * viewportHeight));
					glColor4f(0.2, 0.2, 0.2, 0.45);
					glBegin(GL_LINE_LOOP);
					glVertex2f(firstviewX + (i * viewportWidth) + border,
							   firstviewY + (j * viewportHeight) +
							   viewportHeight);
					glVertex2f(firstviewX + (i * viewportWidth) + border,
							   firstviewY + (j * viewportHeight) + border);
					glVertex2f(firstviewX + (i * viewportWidth) +
							   viewportWidth,
							   firstviewY + (j * viewportHeight) + border);
					glVertex2f(firstviewX + (i * viewportWidth) +
							   viewportWidth,
							   firstviewY + (j * viewportHeight) +
							   viewportHeight);
					glEnd();


				}
			}
		}

		glDisable(GL_BLEND);
		glEnableClientState(GL_TEXTURE_COORD_ARRAY);
		glPopMatrix();
		glColor4usv(defaultColor);

		if (wd->opt[WALL_DISPLAY_OPTION_MINISCREEN].value.b)
		{
			int origVX = s->x;
			int origVY = s->y;

			moveScreenViewport(s, s->x, s->y, FALSE);

			for (j = 0; j < vsize; j++)
			{
				for (i = 0; i < hsize; i++)
				{
					ws->miniScreen = TRUE;
					float mx = firstviewX + (i * viewportWidth) + border;
					float my = firstviewY + (j * viewportHeight) + border;
					float mw = viewportWidth - border;
					float mh = viewportHeight - border;

					ws->mSAttribs.xTranslate =
							mx / s->outputDev[output].width;
					ws->mSAttribs.yTranslate =
							-my / s->outputDev[output].height;
					ws->mSAttribs.xScale = mw / s->width;
					ws->mSAttribs.yScale = mh / s->height;
					ws->mSAttribs.opacity = OPAQUE;
					ws->mSAttribs.brightness = 0.4f * BRIGHT;
					ws->mSAttribs.saturation = COLOR;

					if (i == origVX - ws->destx && j == origVY - ws->desty &&
						ws->moving)
					{
						ws->mSAttribs.brightness = BRIGHT;
					}
					if (ws->boxTimeout && !ws->moving && i == origVX &&
						j == origVY)
						ws->mSAttribs.brightness = BRIGHT;


					(*s->paintTransformedScreen) (s, sAttrib, transform, region,
												  output, mask |
												  PAINT_SCREEN_TRANSFORMED_MASK);
					ws->miniScreen = FALSE;

					moveScreenViewport(s, -1, 0, FALSE);
				}
				moveScreenViewport(s, 0, -1, FALSE);
			}
			moveScreenViewport(s, -origVX, -origVY, FALSE);
		}

	}
	return status;
}

static void moveWindowToViewportYPosition(CompWindow * w, int y, Bool sync)
{
	int ty, vHeight = w->screen->height * w->screen->hsize;

	y += w->screen->y * w->screen->height;
	y = MOD(y, vHeight);
	y -= w->screen->y * w->screen->height;

	ty = y - w->attrib.y;
	if (ty)
	{
		int m, wy;

		if (!w->managed)
			return;

		if (w->type & (CompWindowTypeDesktopMask | CompWindowTypeDockMask))
			return;

		if (w->state & CompWindowStateStickyMask)
			return;

		m = w->attrib.y + ty;
		if (m - w->output.top < w->screen->height - vHeight)
			wy = ty + vHeight;
		else if (m + w->height + w->output.bottom > vHeight)
			wy = ty - vHeight;
		else
			wy = ty;

		if (w->saveMask & CWY)
			w->saveWc.y += wy;

		moveWindow(w, 0, wy, sync, TRUE);

		if (sync)
			syncWindowPosition(w);
	}
}


static void wallPreparePaintScreen(CompScreen * s, int ms)
{
	WALL_SCREEN(s);

	if (ws->expoMode || ws->grabbedWindow)	// Do we need the mouse location?
	{
		int winX, winY;
		int rootX, rootY;
		unsigned int mask_return;
		Window root_return;
		Window child_return;

		XQueryPointer(s->display->display, s->root,
					  &root_return, &child_return,
					  &rootX, &rootY, &winX, &winY, &mask_return);

		ws->pointerX = rootX;
		ws->pointerY = rootY;

	}
	if (!ws->moving && ws->boxTimeout)
	{
		ws->boxTimeout -= ms;
	}

	if (ws->expoMode)
	{
		ws->expoCam = MIN(1.0, ws->expoCam + ((float)ms / 500.0));
	}
	else
	{
		ws->expoCam = MAX(0.0, ws->expoCam - ((float)ms / 500.0));
	}

	if (ws->moving)
	{
		ws->timer -= ms;
		if (ws->moveWindow)
		{
			CompWindow *w;
		
			WALL_DISPLAY(s->display);

			double left =
				(float)ws->timer /
				(wd->opt[WALL_DISPLAY_OPTION_SLIDE_DURATION].value.f * 1000.0f *
				  MAX(abs(ws->destx), abs(ws->desty)));
			
			w = findWindowAtScreen(s, ws->moveWindow);
			if (w)
			{
				moveWindow (w, ws->moveWindowX-w->attrib.x ,ws->moveWindowY-w->attrib.y, TRUE, TRUE);
				moveWindowToViewportPosition(w,ws->moveWindowX-(1-left)*s->width*ws->destx,FALSE);
				moveWindowToViewportYPosition(w,ws->moveWindowY-(1-left)*s->height*ws->desty,FALSE);
				syncWindowPosition(w);
			}
		}
	}

	if (ws->moving && ws->timer <= 0)
	{
		ws->moving = FALSE;
		moveScreenViewport(s, ws->destx, ws->desty, TRUE);
		ws->timer = 0;
		if (ws->moveWindow)
			wallReleaseMoveWindow(s);
		else
			focusDefaultWindow(s);
	}

	UNWRAP(ws, s, preparePaintScreen);
	(*s->preparePaintScreen) (s, ms);
	WRAP(ws, s, preparePaintScreen, wallPreparePaintScreen);
}

static void wallPaintTransformedScreen(CompScreen * s,
									   const ScreenPaintAttrib * sAttrib,
									   const CompTransform    *transform,
									   Region region, int output,
									   unsigned int mask)
{
	WALL_SCREEN(s);
	WALL_DISPLAY(s->display);

	CompTransform     sTransform = *transform;

	if (ws->miniScreen)
	{
		// move each screen to the correct output position
		matrixTranslate(&sTransform, -s->outputDev[output].region.extents.x1 /
					 s->outputDev[output].width,
					 s->outputDev[output].region.extents.y1 /
					 s->outputDev[output].height, 0.0f);
		matrixTranslate(&sTransform, 0.0f, 0.0f, -DEFAULT_Z_CAMERA);

		matrixTranslate(&sTransform, ws->mSAttribs.xTranslate, ws->mSAttribs.yTranslate, 0);

		// move orgin to top left
		matrixTranslate(&sTransform, -0.5f, 0.5f, 0.0f);

		matrixScale(&sTransform, ws->mSAttribs.xScale, ws->mSAttribs.yScale, 1.0);

		// revert prepareXCoords region shift. Now all screens display the same
		matrixTranslate(&sTransform, 0.5f, 0.5f, DEFAULT_Z_CAMERA);
		matrixTranslate(&sTransform, s->outputDev[output].region.extents.x1 /
					 s->outputDev[output].width,
					 -s->outputDev[output].region.extents.y2 /
					 s->outputDev[output].height, 0.0f);

		UNWRAP(ws, s, paintTransformedScreen);
		(*s->paintTransformedScreen) (s, sAttrib, &sTransform, &s->region, output, mask);
		WRAP(ws, s, paintTransformedScreen, wallPaintTransformedScreen);
		return;
	}

	UNWRAP(ws, s, paintTransformedScreen);

	ws->expoActive = FALSE;

	(*s->paintTransformedScreen) (s, sAttrib, &sTransform, region, output, mask);

	if (ws->expoCam > 0.0)
	{
		int oldFilter = s->display->textureFilter;

		if (ws->expoCam == 1 && wd->opt[WALL_DISPLAY_OPTION_MIPMAPS].value.b)
			s->display->textureFilter = GL_LINEAR_MIPMAP_LINEAR;
		XRectangle oRect = { 0, 0, 0, 0 };
		screenGetOutputDevRect(s, output, &oRect);

		int origVX = s->x;
		int origVY = s->y;

		const float gapy = 0.01f; // amount of gap between viewports
		const float gapx = 0.01f * s->height / s->width;

		// Zoom animation stuff

		Point3d vpCamPos = {0, 0, 0};   // camera position for the selected viewport
		Point3d expoCamPos = {0, 0, 0}; // camera position during expo mode

		vpCamPos.x = s->hsize * ((s->x + 0.5) / s->hsize - 0.5) + gapx * (s->x);
		vpCamPos.y = -s->vsize * ((s->y + 0.5) / s->vsize - 0.5) - gapy * (s->y);
		vpCamPos.z = 0;

		float biasz = 0;
		if (wd->opt[WALL_DISPLAY_OPTION_ROTATE].value.b)
			biasz = MAX(s->hsize, s->vsize) * 0.15;

		expoCamPos.x = gapx * (s->hsize - 1) * 0.5;
		expoCamPos.y = -gapy * (s->vsize - 1) * 0.5;
		expoCamPos.z = -DEFAULT_Z_CAMERA +
			DEFAULT_Z_CAMERA * (MAX(s->hsize +
									(s->hsize - 1) * gapx /
									(s->width / s->outputDev[output].width),
									s->vsize +
									(s->vsize - 1) * gapy /
									(s->height / s->outputDev[output].height))
								+ biasz);

		float progress = sigmoidProgress(ws->expoCam);

		// interpolate between vpCamPos and expoCamPos
		float camx = vpCamPos.x * (1 - progress) + expoCamPos.x * progress;
		float camy = vpCamPos.y * (1 - progress) + expoCamPos.y * progress;
		float camz = vpCamPos.z * (1 - progress) + expoCamPos.z * progress;

		// End of Zoom animation stuff


		moveScreenViewport(s, s->x, s->y, FALSE);

		WALL_DISPLAY(s->display);


		float rotation = 0.0;

		if (wd->opt[WALL_DISPLAY_OPTION_ROTATE].value.b)
		{
			if (wd->expoAnimationType == ExpoAnimationZoom)
				rotation = 10.0 * sigmoidProgress(ws->expoCam);
			else
				rotation = 10.0 * ws->expoCam;
		}
		// ALL TRANSFORMATION ARE EXECUTED FROM BOTTOM TO TOP


		// move each screen to the correct output position
		matrixTranslate(&sTransform, -s->outputDev[output].region.extents.x1 /
					 s->outputDev[output].width,
					 s->outputDev[output].region.extents.y1 /
					 s->outputDev[output].height, 0.0f);
		matrixTranslate(&sTransform, 0.0f, 0.0f, -DEFAULT_Z_CAMERA);

		// zoom out
		if (wd->expoAnimationType == ExpoAnimationFadeZoom)
			matrixTranslate(&sTransform, 0.0f, 0.0f, 1.0f - 1.2 * ws->expoCam);
		else if (wd->expoAnimationType == ExpoAnimationZoom)
			matrixTranslate(&sTransform, -camx, -camy, -camz);
		else
			matrixTranslate(&sTransform, 0.0f, 0.0f, -1.4f + 1.2 * ws->expoCam);

		// move orgin to top left
		matrixTranslate(&sTransform, -0.5f, 0.5f, 0.0f);

		// translate wall to orgin
		matrixTranslate(&sTransform, (s->width / s->outputDev[output].width) * 0.5,
					 -(s->height / s->outputDev[output].height) * 0.5, 0.0f);

		// rotate
		matrixRotate(&sTransform, rotation, 0.0f, 1.0f, 0.0f);

		if (wd->expoAnimationType != ExpoAnimationZoom)
		{
			// scale that all viewports fit the screen
			float aspect = wd->opt[WALL_DISPLAY_OPTION_ASPECT].value.f;
			float scale = (1.0 / MAX(s->hsize, s->vsize)) * ws->expoCam;
			float scaleX =
					(aspect * scale) +
					((1.0 - aspect) * ((1.0 / s->hsize) * ws->expoCam));
			float scaleY =
					(aspect * scale) +
					((1.0 - aspect) * ((1.0 / s->vsize) * ws->expoCam));
			matrixScale(&sTransform, scaleX, scaleY, 1.0);
		}

		// translate wall to center
		matrixTranslate(&sTransform, -(s->width / s->outputDev[output].width) * s->hsize *
					 0.5,
					 (s->height / s->outputDev[output].height) * s->vsize *
					 0.5, 0.0f);

		// revert prepareXCoords region shift. Now all screens display the same
		matrixTranslate(&sTransform, 0.5f, 0.5f, DEFAULT_Z_CAMERA);
		matrixTranslate(&sTransform, s->outputDev[output].region.extents.x1 /
					 s->outputDev[output].width,
					 -s->outputDev[output].region.extents.y2 /
					 s->outputDev[output].height, 0.0f);

		int i, j;

		ws->expoActive = TRUE;

		for (j = 0; j < s->vsize; j++)
		{

			CompTransform  sTransform2 = sTransform;
			for (i = 0; i < s->hsize; i++)
			{
				if (wd->expoAnimationType == ExpoAnimationVortex)
 					matrixRotate(&sTransform2, 360 * ws->expoCam, 0.0f, 1.0f,
							  2.0f * ws->expoCam);

				(*s->paintTransformedScreen) (s, sAttrib, &sTransform2, &s->region, output,
											  mask);


				if ((ws->pointerX >= s->outputDev[output].region.extents.x1)
					&& (ws->pointerX < s->outputDev[output].region.extents.x2)
					&& (ws->pointerY >=
						s->outputDev[output].region.extents.y1) &&
					(ws->pointerY < s->outputDev[output].region.extents.y2))
				{
					int cursor[2] = { ws->pointerX, ws->pointerY };

					invertTransformedVertex(s, sAttrib, &sTransform2, output, cursor);

					if ((cursor[0] > 0) && (cursor[0] < s->width) &&
						(cursor[1] > 0) && (cursor[1] < s->height))
					{
						ws->mouseOverViewX = i;
						ws->mouseOverViewY = j;
						ws->newCursorX = i * s->width + cursor[0];
						ws->newCursorY = j * s->height + cursor[1];
						if (ws->dndState != DnDNone || ws->anyClick)
						{

							ws->origVX = i;	// Used to save last viewport interaction was in
							ws->origVY = j;
							ws->anyClick = FALSE;
						}
					}
				}


				// not sure this will work with different resolutions
				matrixTranslate(&sTransform2,(s->width / s->outputDev[output].width + gapx),
							 0.0f, 0.0);

				moveScreenViewport(s, -1, 0, FALSE);
			}

			// not sure this will work with different resolutions
 			matrixTranslate(&sTransform, 0,
						 (-s->height / s->outputDev[output].height - gapy),
						 0.0f);

			moveScreenViewport(s, 0, -1, FALSE);
		}

		ws->expoActive = FALSE;
		moveScreenViewport(s, -origVX, -origVY, FALSE);

		s->filter[SCREEN_TRANS_FILTER] = oldFilter;
		s->display->textureFilter = oldFilter;
	}
	else if (ws->moving)
	{
		WALL_DISPLAY(s->display);

		double left =
				(float)ws->timer /
				(wd->opt[WALL_DISPLAY_OPTION_SLIDE_DURATION].value.f * 1000.0f *
				  MAX(abs(ws->destx), abs(ws->desty)));
		int origx = s->x;
		int origy = s->y;

		clearTargetOutput(s->display, GL_COLOR_BUFFER_BIT);

		int i, j;

		matrixTranslate(&sTransform, ((1 -
					   left) * ws->destx) * (s->width /
											 (float)s->outputDev[output].
											 width),
					 (-(1 - left) * ws->desty) * (s->height /
												  (float)s->outputDev[output].
												  height), 0);



		for (i = -abs(ws->destx); i < s->hsize; i++)	// TODO: Add a slight bit of intelligence to not be so ineffecient
		{
			for (j = -abs(ws->desty); j < s->vsize; j++)
			{

				matrixTranslate(&sTransform, i *
							 (s->width / (float)s->outputDev[output].width),
							 j * (s->height /
								  (float)s->outputDev[output].height), 0);
				moveScreenViewport(s, -i, j, FALSE);
				(*s->paintTransformedScreen) (s,
											  sAttrib, &sTransform,
											  &s->region, output, mask);

				matrixTranslate(&sTransform, -i *
							 (s->width / (float)s->outputDev[output].width),
							 -j * (s->height /
								   (float)s->outputDev[output].height), 0);
				moveScreenViewport(s, i, -j, FALSE);


			}


		}


		while (s->x != origx)
			moveScreenViewport(s, -1, 0, FALSE);
		while (s->y != origy)
			moveScreenViewport(s, 0, 1, FALSE);

	}

	WRAP(ws, s, paintTransformedScreen, wallPaintTransformedScreen);
}

static Bool
wallDrawWindow(CompWindow * w,
			   const CompTransform    *transform,
			   const FragmentAttrib * attrib,
			   Region region, unsigned int mask)
{
	WALL_SCREEN(w->screen);
	WALL_DISPLAY(w->screen->display);
	Bool status;

	FragmentAttrib pA = *attrib;

	if (ws->expoCam > 0.0)
	{

		if (ws->expoActive)
		{
			if (wd->expoAnimationType == ExpoAnimationZoom)
				pA.opacity = attrib->opacity;
			else
				pA.opacity = attrib->opacity * ws->expoCam;

			if (w == ws->stickyWindow)
				pA.opacity = 0;
			if (w->wmType & CompWindowTypeDockMask)
			{
				if (wd->expoAnimationType == ExpoAnimationZoom &&
					((w->screen->x == ws->origVX &&
					  w->screen->y == ws->origVY) ||
					 (w->screen->x == ws->rorigx &&
					  w->screen->y == ws->rorigy &&
					  ws->origVY < 0 &&
					  ws->origVX < 0)))
					pA.opacity = attrib->opacity * (1 - sigmoidProgress(ws->expoCam));
				else
					pA.opacity = 0;
			}
			pA.brightness = attrib->brightness * .75;
			if (w->screen->x == ws->origVX && w->screen->y == ws->origVY)
			{
				pA.brightness = attrib->brightness;
			}
			if (w->screen->x == ws->rorigx && w->screen->y == ws->rorigy &&
				ws->origVY < 0 && ws->origVX < 0)
			{
				pA.brightness = attrib->brightness;
			}
/*
 * FIXME: Bugged with windows intersecting more than one border
			int winx = w->attrib.x - w->input.left;
			int winy = w->attrib.y - w->input.top;
			if (winx > -1 && winx < w->screen->width)
				if (winy > -1 && winy < w->screen->height)
					mask |= PAINT_WINDOW_TRANSFORMED_MASK;
*/
		}
		else
		{
			if (!(w == ws->stickyWindow))
			{
				if (wd->expoAnimationType == ExpoAnimationZoom)
					pA.brightness = attrib->brightness * 0.01;
				else
					pA.brightness =
							attrib->brightness * (1.0 - (0.99 * ws->expoCam));
			}
		}
	}

	if (ws->miniScreen)
	{
		pA.opacity =
				attrib->opacity * ((float)ws->mSAttribs.opacity / OPAQUE);
		pA.brightness =
				attrib->brightness * ((float)ws->mSAttribs.brightness /
									  BRIGHT);
		pA.saturation =
				attrib->saturation * ((float)ws->mSAttribs.saturation /
									  COLOR);
	}

	UNWRAP(ws, w->screen, drawWindow);
	status = (*w->screen->drawWindow) (w, transform, &pA, region, mask);
	WRAP(ws, w->screen, drawWindow, wallDrawWindow);

	return status;
}

static void wallDonePaintScreen(CompScreen * s)
{
	WALL_SCREEN(s);

	if (ws->expoCam > 0.0 || ws->moving || ws->boxTimeout)
		damageScreen(s);

	if (ws->boxTimeout < 0)
	{
		ws->boxTimeout = 0;
		damageScreen(s);
	}
	if (ws->grabbedWindow && !ws->moving)
	{
		if (ws->pointerX >= (s->width - 1))
		{
			wallMoveViewport(s, -1, 0, None);
			if (ws->moving)
				warpPointer(s, 10 - s->width, 0);
		}
		else if (ws->pointerX <= 1)
		{
			wallMoveViewport(s, 1, 0, None);
			if (ws->moving)
				warpPointer(s, s->width - 10, 0);
		}
		else if (ws->pointerY >= (s->height - 1))
		{
			wallMoveViewport(s, 0, -1, None);
			if (ws->moving)
				warpPointer(s, 0, -(s->height - 10));
		}
		else if (ws->pointerY <= (1))
		{
			wallMoveViewport(s, 0, 1, None);
			if (ws->moving)
				warpPointer(s, 0, (s->height - 10));
		}
	}

	UNWRAP(ws, s, donePaintScreen);
	(*s->donePaintScreen) (s);
	WRAP(ws, s, donePaintScreen, wallDonePaintScreen);

	if (ws->dndState == DnDDuring)
	{
		int dx = ws->newCursorX - ws->prevCursorX;
		int dy = ws->newCursorY - ws->prevCursorY;

		if (ws->dndWindow && !ws->stickyWindow)
			moveWindow(ws->dndWindow, dx, dy, TRUE, FALSE);

		ws->prevCursorX = ws->newCursorX;
		ws->prevCursorY = ws->newCursorY;


		damageScreen(s);
	}

	if (ws->dndState != DnDStart)
		return;

	int origView = s->x;
	int origViewY = s->y;

	// needs to be moved into handle event
	moveScreenViewport(s, s->x, s->y, FALSE);

	CompWindow *w;

	for (w = s->reverseWindows; w; w = w->prev)
	{
		if (w->destroyed)
			continue;

		if (!w->shaded)
		{
			if (w->attrib.map_state != IsViewable || !w->damaged)
				continue;
		}

		if (w->state & CompWindowStateOffscreenMask)
			continue;

		if (w->type & CompWindowTypeNormalMask)
		{
			if (ws->newCursorX < WIN_X(w) ||
				ws->newCursorX > WIN_X(w) + WIN_W(w))
				continue;
			if (ws->newCursorY < WIN_Y(w) ||
				ws->newCursorY > WIN_Y(w) + WIN_H(w))
				continue;
			ws->dndState = DnDDuring;
			ws->dndWindow = w;
			(*w->screen->windowGrabNotify) (w, ws->newCursorX, ws->newCursorY, 0,
					CompWindowGrabMoveMask | CompWindowGrabButtonMask);
			break;
		}
	}
	if (ws->dndWindow)
	{
		raiseWindow(ws->dndWindow);
		moveInputFocusToWindow(ws->dndWindow);
	}

	moveScreenViewport(s, -origView, -origViewY, FALSE);

	for (w = s->windows; w; w = w->next)
	{
		WALL_WINDOW(w);
		ww->hovered = FALSE;
	}

	if (ws->dndState == DnDStart)	// No window is hovered
		ws->dndState = DnDNone;

	ws->prevCursorX = ws->newCursorX;
	ws->prevCursorY = ws->newCursorY;

	//damageScreen(s);
}

static void wallDisplayInitOptions(WallDisplay * wd)
{
	CompOption *o;

	o = &wd->opt[WALL_DISPLAY_OPTION_EXPO];
	o->advanced = False;
	o->name = "expo";
	o->group = N_("Misc. Settings");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Expo");
	o->longDesc = N_("Engage wall expo mode.");
	o->type = CompOptionTypeAction;
	o->value.action.initiate = wallExpo;
	o->value.action.terminate = 0;
	o->value.action.bell = 0;
	o->value.action.edgeMask = 0;
	o->value.action.state = CompActionStateInitKey;
	o->value.action.state |= CompActionStateInitEdge;
	o->value.action.edgeMask = (1 << SCREEN_EDGE_TOPLEFT);
	o->value.action.type = CompBindingTypeKey;
	o->value.action.key.modifiers = WALL_EXPO_MODIFIERS_DEFAULT;
	o->value.action.key.keysym = XStringToKeysym(WALL_EXPO_KEY_DEFAULT);

	o = &wd->opt[WALL_DISPLAY_OPTION_ROTATE];
	o->advanced = False;
	o->name = "rotate";
	o->group = N_("Misc. Settings");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Rotate Wall");
	o->longDesc = N_("Rotate wall in expo mode");
	o->type = CompOptionTypeBool;
	o->value.b = WALL_ROTATE_DEFAULT;

	o = &wd->opt[WALL_DISPLAY_OPTION_SLIDE_DURATION];
	o->advanced = True;
	o->name = "slide_duration";
	o->group = N_("Misc. Settings");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Wall Sliding Duration");
	o->longDesc = N_("Duration (in seconds) for wall sliding viewport "
			"switching animation");
	o->type = CompOptionTypeFloat;
	o->value.f = WALL_SLIDE_DURATION_DEFAULT;
	o->rest.f.max = WALL_SLIDE_DURATION_MAX;
	o->rest.f.min = WALL_SLIDE_DURATION_MIN;
	o->rest.f.precision = WALL_SLIDE_DURATION_PRECISION;

	o = &wd->opt[WALL_DISPLAY_OPTION_EXPO_ANIMATION];
	o->name = "expo_animation";
	o->group = N_("Misc. Settings");
	o->subGroup = N_("");
	o->advanced = False;
	o->shortDesc = N_("Expo Animation");
	o->longDesc = N_("Animation used when switching to expo mode.");
	o->displayHints = "";
	o->type = CompOptionTypeString;
	o->value.s = strdup(expoAnimationName[WALL_EXPO_ANIMATION_DEFAULT]);
	o->rest.s.string = expoAnimationName;
	o->rest.s.nString = ExpoAnimationNum;

	o = &wd->opt[WALL_DISPLAY_OPTION_STICKY];
	o->advanced = False;
	o->name = "sticky";
	o->group = N_("Misc. Settings");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Sticky");
	o->longDesc = N_("Engage wall sticky mode.");
	o->type = CompOptionTypeAction;
	o->value.action.initiate = wallSticky;
	o->value.action.terminate = 0;
	o->value.action.bell = 0;
	o->value.action.edgeMask = 0;
	o->value.action.state = CompActionStateInitKey;
	o->value.action.type = CompBindingTypeKey;
	o->value.action.key.modifiers = WALL_STICKY_MODIFIERS_DEFAULT;
	o->value.action.key.keysym = XStringToKeysym(WALL_STICKY_KEY_DEFAULT);

#define WALL_MOVE_OPTION(UPPER,LOWERCASE) \
	o = &wd->opt [WALL_DISPLAY_OPTION_##UPPER]; \
	o->advanced = False; \
	o->name = #LOWERCASE; \
	o->group = N_("Viewport Switching"); \
	o->subGroup = N_(""); \
	o->displayHints = ""; \
	o->shortDesc = N_("Move "#LOWERCASE); \
	o->longDesc = N_("Move along the wall "#LOWERCASE); \
	o->type = CompOptionTypeAction; \
	o->value.action.initiate = wall##LOWERCASE; \
	o->value.action.terminate = 0; \
	o->value.action.bell = 0; \
	o->value.action.edgeMask = 0; \
	o->value.action.state = CompActionStateInitKey;\
	o->value.action.state |= CompActionStateInitButton; \
	o->value.action.type = CompBindingTypeKey;\
	o->value.action.type |= CompBindingTypeButton;\
	o->value.action.key.modifiers = WALL_MOVE_MODIFIERS_DEFAULT;\
	o->value.action.key.keysym = XStringToKeysym(WALL_##UPPER##_KEY_DEFAULT);\

	WALL_MOVE_OPTION(LEFT, Left)
	WALL_MOVE_OPTION(RIGHT, Right)
	WALL_MOVE_OPTION(UP, Up)
	WALL_MOVE_OPTION(DOWN, Down)
	WALL_MOVE_OPTION(NEXT, Next)
	o->value.action.edgeButton = Button4;
	o->value.action.state |= CompActionStateInitEdge;
	o->value.action.type |= CompBindingTypeEdgeButton;
	o->value.action.edgeMask  = 1 << SCREEN_EDGE_LEFT;
	o->value.action.edgeMask |= 1 << SCREEN_EDGE_RIGHT;
	o->value.action.edgeMask |= 1 << SCREEN_EDGE_TOP;
	o->value.action.edgeMask |= 1 << SCREEN_EDGE_BOTTOM;
	WALL_MOVE_OPTION(PREV, Prev)
	o->value.action.edgeButton = Button5;
	o->value.action.state |= CompActionStateInitEdge;
	o->value.action.type |= CompBindingTypeEdgeButton;
	o->value.action.edgeMask  = 1 << SCREEN_EDGE_LEFT;
	o->value.action.edgeMask |= 1 << SCREEN_EDGE_RIGHT;
	o->value.action.edgeMask |= 1 << SCREEN_EDGE_TOP;
	o->value.action.edgeMask |= 1 << SCREEN_EDGE_BOTTOM;

	#define WALL_MOVE_WITH_WINDOW_OPTION(UPPER,LOWERCASE) \
	o = &wd->opt [WALL_DISPLAY_OPTION_WINDOW_##UPPER]; \
	o->advanced = False; \
	o->name = "wall_window_"#LOWERCASE; \
	o->group = N_("Viewport Switching"); \
	o->subGroup = N_(""); \
	o->displayHints = ""; \
	o->shortDesc = N_("Move With Window "#LOWERCASE); \
	o->longDesc = N_("Move with window along the wall "#LOWERCASE); \
	o->type = CompOptionTypeAction; \
	o->value.action.initiate = wall##LOWERCASE##WithWindow; \
	o->value.action.terminate = 0; \
	o->value.action.bell = 0; \
	o->value.action.edgeMask = 0; \
	o->value.action.state = CompActionStateInitKey;\
	o->value.action.state |= CompActionStateInitButton; \
	o->value.action.type = CompBindingTypeKey;\
	o->value.action.key.modifiers = WALL_MOVE_WITH_WINDOW_MODIFIERS_DEFAULT;\
	o->value.action.key.keysym = XStringToKeysym(WALL_##UPPER##_KEY_DEFAULT);\

	WALL_MOVE_WITH_WINDOW_OPTION(LEFT,Left)
	WALL_MOVE_WITH_WINDOW_OPTION(RIGHT,Right)
	WALL_MOVE_WITH_WINDOW_OPTION(UP,Up)
	WALL_MOVE_WITH_WINDOW_OPTION(DOWN,Down)

	o = &wd->opt[WALL_DISPLAY_OPTION_WHEEL_UP];
	o->advanced = False;
	o->name = "wall_up_wheel";
	o->group = N_("Viewport Switching");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Mousewheel Up");
	o->longDesc = N_("Mousewheel up");
	o->type = CompOptionTypeAction;
	o->value.action.initiate = wallUpWheel;
	o->value.action.terminate = 0;
	o->value.action.bell = FALSE;
	o->value.action.edgeMask = 0;
	o->value.action.state = CompActionStateInitButton;
	o->value.action.type = CompBindingTypeButton;
	o->value.action.button.modifiers = WALL_WHEEL_MODIFIERS_DEFAULT;
	o->value.action.button.button = WALL_UP_WHEEL_DEFAULT;

	o = &wd->opt[WALL_DISPLAY_OPTION_WHEEL_DOWN];
	o->advanced = False;
	o->name = "wall_down_wheel";
	o->group = N_("Viewport Switching");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Mousewheel Down");
	o->longDesc = N_("Mousewheel down");
	o->type = CompOptionTypeAction;
	o->value.action.initiate = wallDownWheel;
	o->value.action.terminate = 0;
	o->value.action.bell = FALSE;
	o->value.action.edgeMask = 0;
	o->value.action.state = CompActionStateInitButton;
	o->value.action.type = CompBindingTypeButton;
	o->value.action.button.modifiers = WALL_WHEEL_MODIFIERS_DEFAULT;
	o->value.action.button.button = WALL_DOWN_WHEEL_DEFAULT;

	o = &wd->opt[WALL_DISPLAY_OPTION_MIPMAPS];
	o->advanced = True;
	o->name = "mipmaps";
	o->group = N_("Misc. Settings");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Mipmaps");
	o->longDesc = N_("Generate mipmaps in expo mode");
	o->type = CompOptionTypeBool;
	o->value.b = WALL_MIPMAPS_DEFAULT;

	o = &wd->opt[WALL_DISPLAY_OPTION_ASPECT];
	o->advanced = False;
	o->name = "aspect";
	o->group = N_("Misc. Settings");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Expo Mode Aspect Ratio");
	o->longDesc = N_("Expo mode wall aspect ratio");
	o->type = CompOptionTypeFloat;
	o->value.f = WALL_ASPECT_DEFAULT;
	o->rest.f.min = 0.0;
	o->rest.f.max = 1.0;
	o->rest.f.precision = 0.1;

	o = &wd->opt[WALL_DISPLAY_OPTION_MINISCREEN];
	o->advanced = False;
	o->name = "miniscreen";
	o->group = N_("Viewport Switching");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Show Viewport Previews");
	o->longDesc = N_("Show viewport previews in switch window");
	o->type = CompOptionTypeBool;
	o->value.b = WALL_MINISCREEN_DEFAULT;

	o = &wd->opt[WALL_DISPLAY_OPTION_PREVIEW_TIMEOUT];
	o->advanced = False;
	o->name = "preview_timeout";
	o->group = N_("Viewport Switching");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Switch Target Preview Visibility Time");
	o->longDesc = N_("Duration (in seconds) the switch target preview should remain"
			"visible after switching ends.");
	o->type = CompOptionTypeFloat;
	o->value.f = WALL_PREVIEW_TIMEOUT_DEFAULT;
	o->rest.f.max = WALL_PREVIEW_TIMEOUT_MAX;
	o->rest.f.min = WALL_PREVIEW_TIMEOUT_MIN;
	o->rest.f.precision = WALL_PREVIEW_TIMEOUT_PRECISION;

	o = &wd->opt[WALL_DISPLAY_OPTION_ALLOW_WRAPAROUND];
	o->advanced = False;
	o->name = "allow_wraparound";
	o->group = N_("Viewport Switching");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Allow wrap-around");
	o->longDesc = N_("Allow wrap-around when moving the wall up/down/left/right");
	o->type = CompOptionTypeBool;
	o->value.b = WALL_ALLOW_WRAPAROUND_DEFAULT;
}

static CompOption *wallGetDisplayOptions(CompDisplay * d, int *count)
{
	if (d)
	{
		WALL_DISPLAY(d);
		*count = NUM_OPTIONS(wd);
		return wd->opt;
	}
	else
	{
		WallDisplay *wd = malloc(sizeof(WallDisplay));

		wallDisplayInitOptions(wd);
		*count = NUM_OPTIONS(wd);
		return wd->opt;
	}
}

static Bool
wallSetDisplayOption(CompDisplay * d, char *name, CompOptionValue * value)
{
	CompOption *o;
	int index;

	WALL_DISPLAY(d);

	o = compFindOption(wd->opt, NUM_OPTIONS(wd), name, &index);

	if (!o)
		return FALSE;

	switch (index)
	{
		case WALL_DISPLAY_OPTION_EXPO:
		case WALL_DISPLAY_OPTION_STICKY:
		case WALL_DISPLAY_OPTION_LEFT:
		case WALL_DISPLAY_OPTION_RIGHT:
		case WALL_DISPLAY_OPTION_UP:
		case WALL_DISPLAY_OPTION_DOWN:
		case WALL_DISPLAY_OPTION_WINDOW_LEFT:
		case WALL_DISPLAY_OPTION_WINDOW_RIGHT:
		case WALL_DISPLAY_OPTION_WINDOW_UP:
		case WALL_DISPLAY_OPTION_WINDOW_DOWN:
		case WALL_DISPLAY_OPTION_WHEEL_UP:
		case WALL_DISPLAY_OPTION_WHEEL_DOWN:
		case WALL_DISPLAY_OPTION_NEXT:
		case WALL_DISPLAY_OPTION_PREV:
			if (setDisplayAction(d, o, value))
				return TRUE;
			break;
		case WALL_DISPLAY_OPTION_ROTATE:
		case WALL_DISPLAY_OPTION_MIPMAPS:
		case WALL_DISPLAY_OPTION_MINISCREEN:
		case WALL_DISPLAY_OPTION_ALLOW_WRAPAROUND:
			if (compSetBoolOption(o, value))
				return TRUE;
			break;
		case WALL_DISPLAY_OPTION_SLIDE_DURATION:
		case WALL_DISPLAY_OPTION_PREVIEW_TIMEOUT:
		case WALL_DISPLAY_OPTION_ASPECT:
			if (compSetFloatOption(o, value))
				return TRUE;
			break;
		case WALL_DISPLAY_OPTION_EXPO_ANIMATION:
			if (compSetStringOption(o, value))
			{
				int i;
				for (i = 0; i < ExpoAnimationNum; i++)
				{
					if (strcmp(o->value.s, expoAnimationName[i]) == 0)
					{
						wd->expoAnimationType = i;
						return TRUE;
					}
				}
			}
			break;
		default:
			break;
	}
	return FALSE;
}

static Bool wallInitDisplay(CompPlugin * p, CompDisplay * d)
{
	WallDisplay *wd;

	wd = malloc(sizeof(WallDisplay));
	if (!wd)
		return FALSE;

	wd->screenPrivateIndex = allocateScreenPrivateIndex(d);

	if (wd->screenPrivateIndex < 0)
	{
		free(wd);
		return FALSE;
	}

	wd->expoAnimationType = WALL_EXPO_ANIMATION_DEFAULT;

	wallDisplayInitOptions(wd);

	WRAP(wd, d, handleEvent, wallHandleEvent);
	d->privates[displayPrivateIndex].ptr = wd;

	return TRUE;
}

static void wallFiniDisplay(CompPlugin * p, CompDisplay * d)
{
	WALL_DISPLAY(d);

	UNWRAP(wd, d, handleEvent);

	freeScreenPrivateIndex(d, wd->screenPrivateIndex);
	free(wd);
}

static Bool wallInitScreen(CompPlugin * p, CompScreen * s)
{
	WallScreen *ws;

	WALL_DISPLAY(s->display);

	ws = malloc(sizeof(WallScreen));

	if (!ws)
		return FALSE;

	s->projectionStyle = COMP_PERSPECTIVE_GLOBAL;

	ws->windowPrivateIndex = allocateWindowPrivateIndex(s);

	ws->boxTimeout = 0;
	ws->timer = 0;
	ws->anyClick = FALSE;

	ws->destx = 0;
	ws->desty = 0;

	ws->moving = FALSE;

	ws->mouseOverViewX = 0;
	ws->mouseOverViewY = 0;
	ws->origVX = 0;
	ws->origVY = 0;

	ws->grabIndex = 0;

	ws->pointerX = 0;
	ws->pointerY = 0;

	ws->expoCam = 0.0f;
	ws->expoMode = 0;

	ws->grabbedWindow = NULL;
	ws->stickyWindow = NULL;
	ws->moveWindow = None;
	ws->size = 3.0;				// Size is kind of misleading it's more like amount zoomed out for miniviews...

	ws->dndState = DnDNone;
	ws->dndWindow = NULL;

	addScreenAction(s, &wd->opt[WALL_DISPLAY_OPTION_EXPO].value.action);

	WRAP(ws, s, paintScreen, wallPaintScreen);
	WRAP(ws, s, donePaintScreen, wallDonePaintScreen);
	WRAP(ws, s, paintTransformedScreen, wallPaintTransformedScreen);
	WRAP(ws, s, preparePaintScreen, wallPreparePaintScreen);
	WRAP(ws, s, drawWindow, wallDrawWindow);
	WRAP(ws, s, windowGrabNotify, wallWindowGrabNotify);
	WRAP(ws, s, windowUngrabNotify, wallWindowUngrabNotify);

	s->privates[wd->screenPrivateIndex].ptr = ws;

	return TRUE;
}

static void wallFiniScreen(CompPlugin * p, CompScreen * s)
{
	WALL_SCREEN(s);
	WALL_DISPLAY(s->display);

	free(wd->opt[WALL_DISPLAY_OPTION_EXPO_ANIMATION].value.s);

	removeScreenAction(s, &wd->opt[WALL_DISPLAY_OPTION_EXPO].value.action);

	UNWRAP(ws, s, paintScreen);
	UNWRAP(ws, s, donePaintScreen);
	UNWRAP(ws, s, paintTransformedScreen);
	UNWRAP(ws, s, preparePaintScreen);
	UNWRAP(ws, s, drawWindow);
	UNWRAP(ws, s, windowGrabNotify);
	UNWRAP(ws, s, windowUngrabNotify);

	free(ws);
}

static Bool wallInitWindow(CompPlugin * p, CompWindow * w)
{
	WallWindow *ww;

	WALL_SCREEN(w->screen);

	ww = malloc(sizeof(WallWindow));
	if (!ww)
		return FALSE;

	ww->skipNotify = ww->hovered = FALSE;
	ww->origx = ww->origy = 0;

	w->privates[ws->windowPrivateIndex].ptr = ww;

	return TRUE;
}

static void wallFiniWindow(CompPlugin * p, CompWindow * w)
{
	WALL_WINDOW(w);

	free(ww);
}

static Bool wallInit(CompPlugin * p)
{
	displayPrivateIndex = allocateDisplayPrivateIndex();

	if (displayPrivateIndex < 0)
		return FALSE;

	return TRUE;
}

static void wallFini(CompPlugin * p)
{
	if (displayPrivateIndex >= 0)
		freeDisplayPrivateIndex(displayPrivateIndex);
}

CompPluginFeature wallFeatures[] = {
	{"largedesktop"}
};

CompPluginDep wallDeps[] = {
	{CompPluginRuleAfter, "decoration"},
	{CompPluginRuleAfter, "inputzoom"},
	{CompPluginRuleAfter, "group"}
};

CompPluginVTable wallVTable = {
	"wall",
	N_("Desktop Wall"),
	N_("Desktop Wall Plugin"),
	wallInit,
	wallFini,
	wallInitDisplay,
	wallFiniDisplay,
	wallInitScreen,
	wallFiniScreen,
	wallInitWindow,
	wallFiniWindow,
	wallGetDisplayOptions,
	wallSetDisplayOption,
	0,
	0,
	wallDeps,
	sizeof(wallDeps) / sizeof(wallDeps[0]),
	wallFeatures,
	sizeof(wallFeatures) / sizeof(wallFeatures[0]),
	BERYL_ABI_INFO,
	"beryl-plugins",
	"desktop",
	0,
	0,
	FALSE,
};

CompPluginVTable *getCompPluginInfo(void)
{
	return &wallVTable;
}