summaryrefslogtreecommitdiff
path: root/beryl-plugins/src/water.c
blob: faa5f6b665667297494ac7beceee105b2a2dd3f4 (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
/*
 * Copyright © 2006 Novell, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * Novell, Inc. not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior permission.
 * Novell, Inc. makes no representations about the suitability of this
 * software for any purpose. It is provided "as is" without express or
 * implied warranty.
 *
 * NOVELL, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 * NO EVENT SHALL NOVELL, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: David Reveman <davidr@novell.com>
 */

#ifdef HAVE_CONFIG_H
#  include "../config.h"
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include <beryl.h>

#define TEXTURE_SIZE 256

#define TEXTURE_NUM  3

typedef struct _WaterFunction
{
	struct _WaterFunction *next;

	int handle;
	int target;
	int param;
	int unit;
} WaterFunction;

#define TINDEX(ws, i) (((ws)->tIndex + (i)) % TEXTURE_NUM)

#define CLAMP(v, min, max) \
    if ((v) > (max))	   \
(v) = (max);	   \
else if ((v) < (min))  \
(v) = (min)

#define WATER_INITIATE_MODIFIERS_DEFAULT (ControlMask | CompSuperMask)

#define WATER_TOGGLE_RAIN_KEY_DEFAULT       "F9"
#define WATER_TOGGLE_RAIN_MODIFIERS_DEFAULT ShiftMask

#define WATER_TOGGLE_WIPER_KEY_DEFAULT       "F8"
#define WATER_TOGGLE_WIPER_MODIFIERS_DEFAULT ShiftMask

#define WATER_OFFSET_SCALE_DEFAULT    1.0f
#define WATER_OFFSET_SCALE_MIN        0.0f
#define WATER_OFFSET_SCALE_MAX       10.0f
#define WATER_OFFSET_SCALE_PRECISION  0.1f

#define WATER_TITLE_AMPL_DEFAULT      0.1f
#define WATER_TITLE_AMPL_MIN          0.01f
#define WATER_TITLE_AMPL_MAX          1.0f
#define WATER_TITLE_AMPL_PRECISION    0.01f

#define WATER_RAIN_AMPL_DEFAULT       0.8f
#define WATER_RAIN_AMPL_MIN           0.01f
#define WATER_RAIN_AMPL_MAX           1.0f
#define WATER_RAIN_AMPL_PRECISION     0.01f

#define WATER_VISCOS_DEFAULT          0.2f
#define WATER_VISCOS_MIN              0.01f
#define WATER_VISCOS_MAX              0.6f
#define WATER_VISCOS_PRECISION        0.01f

#define WATER_INIT_VERTICES_DEFAULT   3000
#define WATER_INIT_VERTICES_MIN       100
#define WATER_INIT_VERTICES_MAX       10000

#define WATER_RAIN_DELAY_DEFAULT 250
#define WATER_RAIN_DELAY_MIN	 10
#define WATER_RAIN_DELAY_MAX	 60000	// max of 1 minute...hope this is good

#define WATER_TITLE_WAVE_BELL_DEFAULT FALSE
#define WATER_UNGRAB_WAVE_DEFAULT     FALSE
#define WATER_SHIVER_WIN_DEFAULT      TRUE

static int displayPrivateIndex;

static int waterLastPointerX = 0;
static int waterLastPointerY = 0;

#define WATER_DISPLAY_OPTION_INITIATE      0
#define WATER_DISPLAY_OPTION_TOGGLE_RAIN   1
#define WATER_DISPLAY_OPTION_TOGGLE_WIPER  2
#define WATER_DISPLAY_OPTION_OFFSET_SCALE  3
#define WATER_DISPLAY_OPTION_RAIN_DELAY	   4
#define WATER_DISPLAY_OPTION_TITLE_WAVE    5
#define WATER_DISPLAY_OPTION_POINT         6
#define WATER_DISPLAY_OPTION_LINE          7
#define WATER_DISPLAY_OPTION_QUAD          8
#define WATER_DISPLAY_OPTION_WIN           9
#define WATER_DISPLAY_OPTION_UNGRAB_WAVE   10
#define WATER_DISPLAY_OPTION_SHIVER_WIN    11
#define WATER_DISPLAY_OPTION_TITLE_AMPL    12
#define WATER_DISPLAY_OPTION_RAIN_AMPL     13
#define WATER_DISPLAY_OPTION_VISCOSITY     14
#define WATER_DISPLAY_OPTION_INIT_VERTICES 15
#define WATER_DISPLAY_OPTION_NUM           16

typedef struct _WaterDisplay
{
	int screenPrivateIndex;

	CompOption opt[WATER_DISPLAY_OPTION_NUM];

	HandleEventProc handleEvent;
} WaterDisplay;

typedef struct _WaterScreen
{
	PreparePaintScreenProc preparePaintScreen;
	DonePaintScreenProc donePaintScreen;
	DrawWindowTextureProc drawWindowTexture;
	WindowUngrabNotifyProc windowUngrabNotify;

	int grabIndex;
	int width, height;

	GLuint program;
	GLuint texture[TEXTURE_NUM];

	int tIndex;
	GLenum target;
	GLfloat tx, ty;

	int count;

	GLuint fbo;
	GLint fboStatus;

	void *data;
	float *d0;
	float *d1;
	unsigned char *t0;

	CompTimeoutHandle rainHandle;
	CompTimeoutHandle wiperHandle;

	float wiperAngle;
	float wiperSpeed;

	struct {
		CompWindow *w;
		int vpX, vpY;
	} current;

	WaterFunction *bumpMapFunctions;
} WaterScreen;

#define GET_WATER_DISPLAY(d)				      \
    ((WaterDisplay *) (d)->privates[displayPrivateIndex].ptr)

#define WATER_DISPLAY(d)		     \
    WaterDisplay *wd = GET_WATER_DISPLAY (d)

#define GET_WATER_SCREEN(s, wd)					  \
    ((WaterScreen *) (s)->privates[(wd)->screenPrivateIndex].ptr)

#define WATER_SCREEN(s)							   \
    WaterScreen *ws = GET_WATER_SCREEN (s, GET_WATER_DISPLAY (s->display))

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

static Bool waterRainTimeout(void *closure);

static Bool waterWiperTimeout(void *closure);

static const char *waterFpString =
		"!!ARBfp1.0"
		"PARAM param = program.local[0];"
		"ATTRIB t11  = fragment.texcoord[0];"
		"TEMP t01, t21, t10, t12;"
		"TEMP c11, c01, c21, c10, c12;"
		"TEMP prev, v, temp, accel;"
		"TEX prev, t11, texture[0], %s;" "TEX c11,  t11, texture[1], %s;"
/* sample offsets */
		"ADD t01, t11, { - %f, 0.0, 0.0, 0.0 };"
		"ADD t21, t11, {   %f, 0.0, 0.0, 0.0 };"
		"ADD t10, t11, { 0.0, - %f, 0.0, 0.0 };"
		"ADD t12, t11, { 0.0,   %f, 0.0, 0.0 };"
/* fetch nesseccary samples */
		"TEX c01, t01, texture[1], %s;"
		"TEX c21, t21, texture[1], %s;"
		"TEX c10, t10, texture[1], %s;" "TEX c12, t12, texture[1], %s;"
/* x/y normals from height */
		"MOV v, { 0.0, 0.0, 0.75, 0.0 };"
		"SUB v.x, c12.w, c10.w;" "SUB v.y, c01.w, c21.w;"
/* bumpiness */
		"MUL v, v, 1.5;"
/* normalize */
		"MAD temp, v.x, v.x, 1.0;"
		"MAD temp, v.y, v.y, temp;" "RSQ temp, temp.x;" "MUL v, v, temp;"
/* add scale and bias to normal */
		"MAD v, v, 0.5, 0.5;"
/* done with computing the normal, continue with computing the next
   height value */
		"ADD accel, c10, c12;"
		"ADD accel, c01, accel;"
		"ADD accel, c21, accel;" "MAD accel, -4.0, c11, accel;"
/* store new height in alpha component */
		"MAD v.w, 2.0, c11, -prev.w;" "MAD v.w, accel, param.x, v.w;"
/* fade out height */
		"MUL v.w, v.w, param.y;" "MOV result.color, v;" "END";

static int
loadFragmentProgram(CompScreen * s, GLuint * program, const char *string)
{
	GLint errorPos;

	/* clear errors */
	glGetError();

	if (!*program)
		(*s->genPrograms) (1, program);

	(*s->bindProgram) (GL_FRAGMENT_PROGRAM_ARB, *program);
	(*s->programString) (GL_FRAGMENT_PROGRAM_ARB,
						 GL_PROGRAM_FORMAT_ASCII_ARB, strlen(string), string);

	glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorPos);
	if (glGetError() != GL_NO_ERROR || errorPos != -1)
	{
		fprintf(stderr,
				"%s: water: failed to load bump map program\n",
				getProgramName());

		(*s->deletePrograms) (1, program);
		*program = 0;

		return 0;
	}

	return 1;
}

static int loadWaterProgram(CompScreen * s)
{
	char buffer[1024];

	WATER_SCREEN(s);

	if (ws->target == GL_TEXTURE_2D)
		sprintf(buffer, waterFpString,
				"2D", "2D",
				1.0f / ws->width, 1.0f / ws->width,
				1.0f / ws->height, 1.0f / ws->height, "2D", "2D", "2D", "2D");
	else
		sprintf(buffer, waterFpString,
				"RECT", "RECT",
				1.0f, 1.0f, 1.0f, 1.0f, "RECT", "RECT", "RECT", "RECT");

	return loadFragmentProgram(s, &ws->program, buffer);
}

static int getBumpMapFragmentFunction (CompScreen *s, CompTexture *texture,
		int unit, int param)
{
	WaterFunction *function;
	CompFunctionData *data;
	int target;

	WATER_SCREEN(s);

	if (texture->target == GL_TEXTURE_2D)
		target = COMP_FETCH_TARGET_2D;
	else
		target = COMP_FETCH_TARGET_RECT;

	for (function = ws->bumpMapFunctions; function; function = function->next)
	{
		if ((function->param == param) && (function->unit == unit) &&
			(function->target == target))
			return function->handle;
	}

	data = createFunctionData();
	if (data)
	{
		static char *temp[] = { "normal", "temp", "total", "bump", "offset" };
		int         i, handle = 0;
		char        str[1024];

		for (i = 0; i < sizeof (temp) / sizeof (temp[0]); i++)
		{
			if (!addTempHeaderOpToFunctionData (data, temp[i]))
			{
				destroyFunctionData (data);
				return 0;
			}
		}

		snprintf(str, 1024,
                /* get normal from normal map */
                "TEX normal, fragment.texcoord[%d], texture[%d], %s;"
                /* save height */
                "MOV offset, normal;"
                /* remove scale and bias from normal */
                "SUB normal, normal, 0.5;"
                "MUL normal, normal, 2.0;"
                /* normalize the normal map */
                "DP3 temp, normal, normal;"
                "RSQ temp, temp.x;"
                "MUL normal, normal, temp;"
                /* scale down normal by height and constant and use as
                   offset in texture */
                "MUL offset, normal, offset.w;"
                "MUL offset, offset, program.env[%d];",
                unit, unit,
                (ws->target == GL_TEXTURE_2D) ? "2D" : "RECT",
                param);

		if (!addDataOpToFunctionData (data, str))
		{
			destroyFunctionData (data);
			return 0;
		}

		if (!addFetchOpToFunctionData (data, "output", "offset.yxzz", target))
		{
			destroyFunctionData (data);
			return 0;
		}

		snprintf (str, 1024,
                  /* normal dot lightdir, this should eventually be
                     changed to a real light vector */
                  "DP3 bump, normal, { 0.707, 0.707, 0.0, 0.0 };"
                  "MUL bump, bump, state.light[0].diffuse;");

		if (!addDataOpToFunctionData (data, str))
		{
			destroyFunctionData (data);
			return 0;
		}

		if (!addColorOpToFunctionData (data, "output", "output"))
		{
			destroyFunctionData (data);
			return 0;
		}

		snprintf (str, 1024,
                  /* diffuse per-vertex lighting, opacity and brightness
                     and add lightsource bump color */
                  "ADD output, output, bump;");

		if (!addDataOpToFunctionData (data, str))
		{
			destroyFunctionData (data);
			return 0;
		}

		function = malloc (sizeof (WaterFunction));
		if (function)
		{
			handle = createFragmentFunction (s, "water", data);

			function->handle = handle;
			function->target = target;
			function->param  = param;
			function->unit   = unit;

			function->next = ws->bumpMapFunctions;
			ws->bumpMapFunctions = function;
		}

		destroyFunctionData (data);

		return handle;
	}

	return 0;
}

static void allocTexture(CompScreen * s, int index)
{
	WATER_SCREEN(s);

	glGenTextures(1, &ws->texture[index]);
	glBindTexture(ws->target, ws->texture[index]);

	glTexParameteri(ws->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(ws->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(ws->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(ws->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

	glTexImage2D(ws->target, 0, GL_RGBA, ws->width, ws->height, 0, GL_BGRA,
#if IMAGE_BYTE_ORDER == MSBFirst
				 GL_UNSIGNED_INT_8_8_8_8_REV,
#else
				 GL_UNSIGNED_BYTE,
#endif
				 ws->t0);
}

static int fboPrologue(CompScreen * s, int tIndex)
{
	WATER_SCREEN(s);

	if (!ws->fbo)
		return 0;

	if (!ws->texture[tIndex])
		allocTexture(s, tIndex);

	(*s->bindFramebuffer) (GL_FRAMEBUFFER_EXT, ws->fbo);

	(*s->framebufferTexture2D) (GL_FRAMEBUFFER_EXT,
								GL_COLOR_ATTACHMENT0_EXT,
								ws->target, ws->texture[tIndex], 0);

	glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
	glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);

	/* check status the first time */
	if (!ws->fboStatus)
	{
		ws->fboStatus = (*s->checkFramebufferStatus) (GL_FRAMEBUFFER_EXT);
		if (ws->fboStatus != GL_FRAMEBUFFER_COMPLETE_EXT)
		{
			fprintf(stderr,
					"%s: water: framebuffer incomplete\n", getProgramName());

			(*s->bindFramebuffer) (GL_FRAMEBUFFER_EXT, 0);
			(*s->deleteFramebuffers) (1, &ws->fbo);

			glDrawBuffer(GL_BACK);
			glReadBuffer(GL_BACK);

			ws->fbo = 0;

			return 0;
		}
	}

	glViewport(0, 0, ws->width, ws->height);
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();

	return 1;
}

static void fboEpilogue(CompScreen * s)
{
	(*s->bindFramebuffer) (GL_FRAMEBUFFER_EXT, 0);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glDepthRange(0, 1);
	glViewport(-1, -1, 2, 2);
	glRasterPos2f(0, 0);

	s->rasterX = s->rasterY = 0;

	setDefaultViewport(s);
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
	glPopMatrix();

	glDrawBuffer(GL_BACK);
	glReadBuffer(GL_BACK);
}

static int fboUpdate(CompScreen * s, float dt, float fade)
{
	WATER_SCREEN(s);
	WATER_DISPLAY(s->display);

	if (!fboPrologue(s, TINDEX(ws, 1)))
		return 0;

	if (!ws->texture[TINDEX(ws, 2)])
		allocTexture(s, TINDEX(ws, 2));

	if (!ws->texture[TINDEX(ws, 0)])
		allocTexture(s, TINDEX(ws, 0));

	glEnable(ws->target);

	(*s->activeTexture) (GL_TEXTURE0_ARB);
	glBindTexture(ws->target, ws->texture[TINDEX(ws, 2)]);

	glTexParameteri(ws->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(ws->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	(*s->activeTexture) (GL_TEXTURE1_ARB);
	glBindTexture(ws->target, ws->texture[TINDEX(ws, 0)]);
	glTexParameteri(ws->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(ws->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	glEnable(GL_FRAGMENT_PROGRAM_ARB);
	(*s->bindProgram) (GL_FRAGMENT_PROGRAM_ARB, ws->program);

	(*s->programLocalParameter4f) (GL_FRAGMENT_PROGRAM_ARB, 0,
								   dt *
								   wd->
								   opt[WATER_DISPLAY_OPTION_VISCOSITY].
								   value.f, fade, 1.0f, 1.0f);

	glBegin(GL_QUADS);

	glTexCoord2f(0.0f, 0.0f);
	glVertex2f(0.0f, 0.0f);
	glTexCoord2f(ws->tx, 0.0f);
	glVertex2f(1.0f, 0.0f);
	glTexCoord2f(ws->tx, ws->ty);
	glVertex2f(1.0f, 1.0f);
	glTexCoord2f(0.0f, ws->ty);
	glVertex2f(0.0f, 1.0f);

	glEnd();

	glDisable(GL_FRAGMENT_PROGRAM_ARB);

	glBindTexture(ws->target, 0);
	glTexParameteri(ws->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(ws->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	(*s->activeTexture) (GL_TEXTURE0_ARB);
	glTexParameteri(ws->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(ws->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glBindTexture(ws->target, 0);

	fboEpilogue(s);

	/* increment texture index */
	ws->tIndex = TINDEX(ws, 1);

	return 1;
}

static int
fboVertices(CompScreen * s, GLenum type, XPoint * p, int n, float v)
{
	WATER_SCREEN(s);

	if (!fboPrologue(s, TINDEX(ws, 0)))
		return 0;

	glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
	glColor4f(0.0f, 0.0f, 0.0f, v);

	glPointSize(3.0f);
	glLineWidth(1.0f);

	glScalef(1.0f / ws->width, 1.0f / ws->height, 1.0);
	glTranslatef(0.5f, 0.5f, 0.0f);

	glBegin(type);

	while (n--)
	{
		glVertex2i(p->x, p->y);
		p++;
	}

	glEnd();

	glColor4usv(defaultColor);
	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

	fboEpilogue(s);

	return 1;
}

static void softwareUpdate(CompScreen * s, float dt, float fade)
{
	float *dTmp;
	int i, j;
	float v0, v1, inv;
	float accel, value;
	unsigned char *t0, *t;
	int dWidth, dHeight;
	float *d01, *d10, *d11, *d12;

	WATER_SCREEN(s);
	WATER_DISPLAY(s->display);

	if (!ws->texture[TINDEX(ws, 0)])
		allocTexture(s, TINDEX(ws, 0));

	dt *= wd->opt[WATER_DISPLAY_OPTION_VISCOSITY].value.f * 2.0f;
	fade *= 0.99f;

	dWidth = ws->width + 2;
	dHeight = ws->height + 2;

#define D(d, j) (*((d) + (j)))

	d01 = ws->d0 + dWidth;
	d10 = ws->d1;
	d11 = d10 + dWidth;
	d12 = d11 + dWidth;

	for (i = 1; i < dHeight - 1; i++)
	{
		for (j = 1; j < dWidth - 1; j++)
		{
			accel = dt * (D(d10, j) +
						  D(d12, j) +
						  D(d11, j - 1) + D(d11, j + 1) - 4.0f * D(d11, j));

			value = (2.0f * D(d11, j) - D(d01, j) + accel) * fade;

			CLAMP(value, 0.0f, 1.0f);

			D(d01, j) = value;
		}

		d01 += dWidth;
		d10 += dWidth;
		d11 += dWidth;
		d12 += dWidth;
	}

	/* update border */
	memcpy(ws->d0, ws->d0 + dWidth, dWidth * sizeof(GLfloat));
	memcpy(ws->d0 + dWidth * (dHeight - 1),
		   ws->d0 + dWidth * (dHeight - 2), dWidth * sizeof(GLfloat));

	d01 = ws->d0 + dWidth;

	for (i = 1; i < dHeight - 1; i++)
	{
		D(d01, 0) = D(d01, 1);
		D(d01, dWidth - 1) = D(d01, dWidth - 2);

		d01 += dWidth;
	}

	d10 = ws->d1;
	d11 = d10 + dWidth;
	d12 = d11 + dWidth;

	t0 = ws->t0;

	/* update texture */
	for (i = 0; i < ws->height; i++)
	{
		for (j = 0; j < ws->width; j++)
		{
			v0 = (D(d12, j) - D(d10, j)) * 1.5f;
			v1 = (D(d11, j - 1) - D(d11, j + 1)) * 1.5f;

			/* 0.5 for scale */
			inv = 0.5f / sqrtf(v0 * v0 + v1 * v1 + 1.0f);

			/* add scale and bias to normal */
			v0 = v0 * inv + 0.5f;
			v1 = v1 * inv + 0.5f;

			/* store normal map in RGB components */
			t = t0 + (j * 4);
			t[0] = (unsigned char)((inv + 0.5f) * 255.0f);
			t[1] = (unsigned char)(v1 * 255.0f);
			t[2] = (unsigned char)(v0 * 255.0f);

			/* store height in A component */
			t[3] = (unsigned char)(D(d11, j) * 255.0f);
		}

		d10 += dWidth;
		d11 += dWidth;
		d12 += dWidth;

		t0 += ws->width * 4;
	}

#undef D

	/* swap height maps */
	dTmp = ws->d0;
	ws->d0 = ws->d1;
	ws->d1 = dTmp;

	if (ws->texture[TINDEX(ws, 0)])
	{
		glBindTexture(ws->target, ws->texture[TINDEX(ws, 0)]);
		glTexImage2D(ws->target,
					 0, GL_RGBA, ws->width, ws->height, 0, GL_BGRA,
#if IMAGE_BYTE_ORDER == MSBFirst
					 GL_UNSIGNED_INT_8_8_8_8_REV,
#else
					 GL_UNSIGNED_BYTE,
#endif
					 ws->t0);
	}
}


#define SET(x, y, v) *((ws->d1) + (ws->width + 2) * (y + 1) + (x + 1)) = (v)

static void softwarePoints(CompScreen * s, XPoint * p, int n, float add)
{
	WATER_SCREEN(s);

	while (n--)
	{
		SET(p->x - 1, p->y - 1, add);
		SET(p->x, p->y - 1, add);
		SET(p->x + 1, p->y - 1, add);

		SET(p->x - 1, p->y, add);
		SET(p->x, p->y, add);
		SET(p->x + 1, p->y, add);

		SET(p->x - 1, p->y + 1, add);
		SET(p->x, p->y + 1, add);
		SET(p->x + 1, p->y + 1, add);

		p++;
	}
}

/* bresenham */
static void softwareLines(CompScreen * s, XPoint * p, int n, float v)
{
	int x1, y1, x2, y2;
	Bool steep;
	int tmp;
	int deltaX, deltaY;
	int error = 0;
	int yStep;
	int x, y;

	WATER_SCREEN(s);

#define SWAP(v0, v1) \
    tmp = v0;	     \
    v0 = v1;	     \
    v1 = tmp

	while (n > 1)
	{
		x1 = p->x;
		y1 = p->y;

		p++;
		n--;

		x2 = p->x;
		y2 = p->y;

		p++;
		n--;

		steep = abs(y2 - y1) > abs(x2 - x1);
		if (steep)
		{
			SWAP(x1, y1);
			SWAP(x2, y2);
		}

		if (x1 > x2)
		{
			SWAP(x1, x2);
			SWAP(y1, y2);
		}
#undef SWAP

		deltaX = x2 - x1;
		deltaY = abs(y2 - y1);

		y = y1;
		if (y1 < y2)
			yStep = 1;
		else
			yStep = -1;

		for (x = x1; x <= x2; x++)
		{
			if (steep)
			{
				SET(y, x, v);
			}
			else
			{
				SET(x, y, v);
			}

			error += deltaY;
			if (2 * error >= deltaX)
			{
				y += yStep;
				error -= deltaX;
			}
		}
	}
}

#undef SET
static void softwareQuads(CompScreen * s, XPoint * p, int n, float v)
{
	/* this seemed the most logical way to handle this */

	/* the quad is defined by its four corners
	 * rather than redesign a new algorithm to
	 * handlle this, I just munge the data to work
	 * with softwareLines
	 */
	XPoint pq[8];

	pq[0] = p[0];
	pq[1] = p[1];

	pq[2] = p[1];
	pq[3] = p[2];

	pq[4] = p[2];
	pq[5] = p[3];

	pq[6] = p[3];
	pq[7] = p[0];

	softwareLines(s, pq, 8, v);
}

static void
softwareVertices(CompScreen * s, GLenum type, XPoint * p, int n, float v)
{
	switch (type)
	{
	case GL_POINTS:
		softwarePoints(s, p, n, v);
		break;
	case GL_LINES:
		softwareLines(s, p, n, v);
		break;
	case GL_QUADS:
		softwareQuads(s, p, n, v);
		break;
	}
}

static void waterUpdate(CompScreen * s, float dt)
{
	GLfloat fade = 1.0f;

	WATER_SCREEN(s);

	if (ws->count < 1000)
	{
		if (ws->count > 1)
			fade = 0.90f + ws->count / 10000.0f;
		else
			fade = 0.0f;
	}

	if (!fboUpdate(s, dt, fade))
		softwareUpdate(s, dt, fade);
}

static void scaleVertices(CompScreen * s, XPoint * p, int n)
{
	WATER_SCREEN(s);

	while (n--)
	{
		p[n].x = (ws->width * p[n].x) / s->width;
		p[n].y = (ws->height * p[n].y) / s->height;
	}
}

static void
waterVertices(CompScreen * s, GLenum type, XPoint * p, int n, float v)
{
	WATER_SCREEN(s);
	WATER_DISPLAY(s->display);

	if (!s->fragmentProgram)
		return;

	scaleVertices(s, p, n);

	if (!fboVertices(s, type, p, n, v))
		softwareVertices(s, type, p, n, v);

	if (ws->count < wd->opt[WATER_DISPLAY_OPTION_INIT_VERTICES].value.i)
		ws->count = wd->opt[WATER_DISPLAY_OPTION_INIT_VERTICES].value.i;
}

static Bool waterRainTimeout(void *closure)
{
	CompScreen *s = closure;
	XPoint p;

	WATER_SCREEN(s);
	WATER_DISPLAY(s->display);

	if (ws->current.w)
		ws->current.w = NULL;

	p.x = (int)(s->width * (rand() / (float)RAND_MAX));
	p.y = (int)(s->height * (rand() / (float)RAND_MAX));

	waterVertices(s, GL_POINTS, &p, 1,
				  wd->opt[WATER_DISPLAY_OPTION_RAIN_AMPL].value.f *
				  (rand() / (float)RAND_MAX));

	damageScreen(s);

	return TRUE;
}

static Bool waterWiperTimeout(void *closure)
{
	CompScreen *s = closure;

	WATER_SCREEN(s);

	if (ws->count)
	{
		if (ws->wiperAngle == 0.0f)
			ws->wiperSpeed = 2.5f;
		else if (ws->wiperAngle == 180.0f)
			ws->wiperSpeed = -2.5f;
		if (ws->current.w)
			ws->current.w = NULL;
	}

	return TRUE;
}

static void waterReset(CompScreen * s)
{
	int size, i, j;

	WATER_SCREEN(s);

	ws->height = TEXTURE_SIZE;
	ws->width = (ws->height * s->width) / s->height;

	if (s->textureNonPowerOfTwo ||
		(POWER_OF_TWO(ws->width) && POWER_OF_TWO(ws->height)))
	{
		ws->target = GL_TEXTURE_2D;
		ws->tx = ws->ty = 1.0f;
	}
	else
	{
		ws->target = GL_TEXTURE_RECTANGLE_NV;
		ws->tx = ws->width;
		ws->ty = ws->height;
	}

	if (!s->fragmentProgram)
		return;

	if (s->fbo)
	{
		loadWaterProgram(s);
		if (!ws->fbo)
			(*s->genFramebuffers) (1, &ws->fbo);
	}

	ws->fboStatus = 0;

	for (i = 0; i < TEXTURE_NUM; i++)
	{
		if (ws->texture[i])
		{
			glDeleteTextures(1, &ws->texture[i]);
			ws->texture[i] = 0;
		}
	}

	if (ws->data)
		free(ws->data);

	size = (ws->width + 2) * (ws->height + 2);

	ws->data = calloc(1, (sizeof(float) * size * 2) +
					  (sizeof(GLubyte) * ws->width * ws->height * 4));
	if (!ws->data)
		return;

	ws->d0 = ws->data;
	ws->d1 = (ws->d0 + (size));
	ws->t0 = (unsigned char *)(ws->d1 + (size));

	for (i = 0; i < ws->height; i++)
	{
		for (j = 0; j < ws->width; j++)
		{
			(ws->t0 + (ws->width * 4 * i + j * 4))[0] = 0xff;
		}
	}
}

static void
waterDrawWindowTexture(CompWindow * w,
					   CompTexture * texture,
					   const FragmentAttrib *fAttrib,
					   unsigned int mask)
{
	WATER_SCREEN(w->screen);
	Bool drawIt = TRUE;

	if (ws->current.w)
	{
		if (w->id == ws->current.w->id)
			drawIt = FALSE;
		else if ((w->screen->x != ws->current.vpX) || (w->screen->y != ws->current.vpY))
			drawIt = FALSE;
	}

	if (drawIt && ws->count)
	{
		FragmentAttrib fa = *fAttrib;
		Bool lighting = w->screen->lighting;
		GLfloat plane[4];
		int param, function, unit;

		WATER_DISPLAY(w->screen->display);

		param = allocFragmentParameters (&fa, 1);
		unit = allocFragmentTextureUnits (&fa, 1);

		function = getBumpMapFragmentFunction (w->screen, texture, unit, param);
		if (function)
		{
			addFragmentFunction(&fa, function);

			screenLighting(w->screen, TRUE);

			(*w->screen->activeTexture) (GL_TEXTURE0_ARB + unit);

			glBindTexture(ws->target, ws->texture[TINDEX(ws, 0)]);

			plane[0] = ws->tx / (GLfloat) w->screen->width;
			plane[1] = plane[2] = plane[3] = 0.0f;

			glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
			glTexGenfv (GL_S, GL_EYE_PLANE, plane);
			glEnable (GL_TEXTURE_GEN_S);

			plane[0] = 0.0f;
			plane[1] = ws->ty / (GLfloat) w->screen->height;
			plane[2] = plane[3] = 0.0f;

			glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
			glTexGenfv (GL_T, GL_EYE_PLANE, plane);
			glEnable (GL_TEXTURE_GEN_T);

			(*w->screen->activeTexture) (GL_TEXTURE0_ARB);

			(*w->screen->programEnvParameter4f) (
					GL_FRAGMENT_PROGRAM_ARB, param,
					texture->matrix.yy *
					wd->opt[WATER_DISPLAY_OPTION_OFFSET_SCALE].value.f * 50.0f,
					-texture->matrix.xx *
					wd->opt[WATER_DISPLAY_OPTION_OFFSET_SCALE].value.f * 50.0f,
					0.0f, 0.0f);
		}

		/* to get appropriate filtering of texture */
		mask |= PAINT_WINDOW_ON_TRANSFORMED_SCREEN_MASK;

		UNWRAP (ws, w->screen, drawWindowTexture);
		(*w->screen->drawWindowTexture) (w, texture, &fa, mask);
		WRAP (ws, w->screen, drawWindowTexture, waterDrawWindowTexture);

		if (function)
		{
			(*w->screen->activeTexture) (GL_TEXTURE0_ARB + unit);
			glDisable (GL_TEXTURE_GEN_T);
			glDisable (GL_TEXTURE_GEN_S);
			glBindTexture (ws->target, 0);
			(*w->screen->activeTexture) (GL_TEXTURE0_ARB);

			screenLighting (w->screen, lighting);
		}
	}
	else
	{
		UNWRAP(ws, w->screen, drawWindowTexture);
		(*w->screen->drawWindowTexture) (w, texture, fAttrib, mask);
		WRAP(ws, w->screen, drawWindowTexture, waterDrawWindowTexture);
	}
}

/* TODO: a way to control the speed */
static void waterPreparePaintScreen(CompScreen * s, int msSinceLastPaint)
{
	WATER_SCREEN(s);

	if (ws->count)
	{
		ws->count -= 10;
		if (ws->count < 0)
			ws->count = 0;

		if (ws->wiperHandle)
		{
			float step, angle0, angle1;
			Bool wipe = FALSE;
			XPoint p[3];

			p[1].x = s->width / 2;
			p[1].y = s->height;

			step = ws->wiperSpeed * msSinceLastPaint / 20.0f;

			if (ws->wiperSpeed > 0.0f)
			{
				if (ws->wiperAngle < 180.0f)
				{
					angle0 = ws->wiperAngle;

					ws->wiperAngle += step;
					ws->wiperAngle = MIN(ws->wiperAngle, 180.0f);

					angle1 = ws->wiperAngle;

					wipe = TRUE;
				}
			}
			else
			{
				if (ws->wiperAngle > 0.0f)
				{
					angle1 = ws->wiperAngle;

					ws->wiperAngle += step;
					ws->wiperAngle = MAX(ws->wiperAngle, 0.0f);

					angle0 = ws->wiperAngle;

					wipe = TRUE;
				}
			}

#define TAN(a) (tanf ((a) * (M_PI / 180.0f)))

			if (wipe)
			{
				if (angle0 > 0.0f)
				{
					p[2].x = s->width / 2 - s->height / TAN(angle0);
					p[2].y = 0;
				}
				else
				{
					p[2].x = 0;
					p[2].y = s->height;
				}

				if (angle1 < 180.0f)
				{
					p[0].x = s->width / 2 - s->height / TAN(angle1);
					p[0].y = 0;
				}
				else
				{
					p[0].x = s->width;
					p[0].y = s->height;
				}

				/* software rasterizer doesn't support triangles yet so wiper
				   effect will only work with FBOs right now */
				waterVertices(s, GL_TRIANGLES, p, 3, 0.0f);
			}
#undef TAN

		}

		waterUpdate(s, 0.8f);
	}

	UNWRAP(ws, s, preparePaintScreen);
	(*s->preparePaintScreen) (s, msSinceLastPaint);
	WRAP(ws, s, preparePaintScreen, waterPreparePaintScreen);
}

static void waterDonePaintScreen(CompScreen * s)
{
	WATER_SCREEN(s);

	if (ws->count)
		damageScreen(s);
	else
		ws->current.w = NULL;

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

static void waterHandleMotionEvent(CompDisplay * d, Window root)
{
	CompScreen *s;

	s = findScreenAtDisplay(d, root);
	if (s)
	{
		WATER_SCREEN(s);

		if (ws->grabIndex)
		{
			XPoint p[2];

			p[0].x = waterLastPointerX;
			p[0].y = waterLastPointerY;

			p[1].x = waterLastPointerX = d->pointerX;
			p[1].y = waterLastPointerY = d->pointerY;

			waterVertices(s, GL_LINES, p, 2, 0.2f);

			damageScreen(s);
		}
	}
}

static Bool
waterInitiate(CompDisplay * d,
			  CompAction * action,
			  CompActionState state, CompOption * option, int nOption)
{
	CompScreen *s;
	unsigned int ui;
	Window root, child;
	int xRoot, yRoot, i;

	for (s = d->screens; s; s = s->next)
	{
		WATER_SCREEN(s);

		if (otherScreenGrabExist(s, "water", 0))
			continue;

		if (!ws->grabIndex)
			ws->grabIndex = pushScreenGrab(s, None, "water");

		if (ws->current.w)
			ws->current.w = NULL;

		if (XQueryPointer
			(d->display, s->root, &root, &child, &xRoot, &yRoot, &i, &i, &ui))
		{
			XPoint p;

			p.x = waterLastPointerX = xRoot;
			p.y = waterLastPointerY = yRoot;

			waterVertices(s, GL_POINTS, &p, 1, 0.8f);

			damageScreen(s);
		}
	}

	if (state & CompActionStateInitButton)
		action->state |= CompActionStateTermButton;

	if (state & CompActionStateInitKey)
		action->state |= CompActionStateTermKey;

	return FALSE;
}

static Bool
waterTerminate(CompDisplay * d,
			   CompAction * action,
			   CompActionState state, CompOption * option, int nOption)
{
	CompScreen *s;

	for (s = d->screens; s; s = s->next)
	{
		WATER_SCREEN(s);

		if (ws->grabIndex)
		{
			removeScreenGrab(s, ws->grabIndex, 0);
			ws->grabIndex = 0;
		}
		ws->current.w = NULL;
	}

	return FALSE;
}

static Bool
waterToggleRain(CompDisplay * d,
				CompAction * action,
				CompActionState state, CompOption * option, int nOption)
{
	CompScreen *s;

	WATER_DISPLAY(d);

	s = findScreenAtDisplay(d, getIntOptionNamed(option, nOption, "root", 0));
	if (s)
	{
		WATER_SCREEN(s);

		if (!ws->rainHandle)
		{
			int delay;

			delay = wd->opt[WATER_DISPLAY_OPTION_RAIN_DELAY].value.i;
			ws->rainHandle = compAddTimeout(delay, waterRainTimeout, s);
		}
		else
		{
			compRemoveTimeout(ws->rainHandle);
			ws->rainHandle = 0;
		}
	}

	return FALSE;
}

static Bool
waterToggleWiper(CompDisplay * d,
				 CompAction * action,
				 CompActionState state, CompOption * option, int nOption)
{
	CompScreen *s;

	s = findScreenAtDisplay(d, getIntOptionNamed(option, nOption, "root", 0));
	if (s)
	{
		WATER_SCREEN(s);

		if (!ws->wiperHandle)
		{
			int delay;

			delay = 2000;
			ws->wiperHandle = compAddTimeout(delay, waterWiperTimeout, s);
		}
		else
		{
			compRemoveTimeout(ws->wiperHandle);
			ws->wiperHandle = 0;
		}
	}

	return FALSE;
}

static Bool
waterTitleWave(CompDisplay * d,
			   CompAction * action,
			   CompActionState state, CompOption * option, int nOption)
{
	CompWindow *w;
	int xid;

	xid = getIntOptionNamed(option, nOption, "window", d->activeWindow);

	w = findWindowAtDisplay(d, xid);
	if (w)
	{
		WATER_DISPLAY(w->screen->display);
		XPoint p[2];

		p[0].x = w->attrib.x - w->input.left;
		p[0].y = w->attrib.y - (w->input.top >> 2);

		p[1].x = w->attrib.x + w->width + w->input.right;
		p[1].y = p[0].y;

		waterVertices(w->screen, GL_LINES, p, 2,
					  wd->opt[WATER_DISPLAY_OPTION_TITLE_AMPL].value.f);

		damageScreen(w->screen);
	}

	return FALSE;
}

static Bool
waterPoint(CompDisplay * d,
		   CompAction * action,
		   CompActionState state, CompOption * option, int nOption)
{
	CompScreen *s;
	int xid;

	xid = getIntOptionNamed(option, nOption, "root", 0);

	s = findScreenAtDisplay(d, xid);
	if (s)
	{
		XPoint p;
		float amp;

		p.x = getIntOptionNamed(option, nOption, "x", s->width / 2);
		p.y = getIntOptionNamed(option, nOption, "y", s->height / 2);

		amp = getFloatOptionNamed(option, nOption, "amplitude", 0.5f);

		waterVertices(s, GL_POINTS, &p, 1, amp);

		damageScreen(s);
	}

	return FALSE;
}

static Bool
waterLine(CompDisplay * d,
		  CompAction * action,
		  CompActionState state, CompOption * option, int nOption)
{
	CompScreen *s;
	int xid;

	xid = getIntOptionNamed(option, nOption, "root", 0);

	s = findScreenAtDisplay(d, xid);
	if (s)
	{
		XPoint p[2];
		float amp;

		p[0].x = getIntOptionNamed(option, nOption, "x0", s->width / 4);
		p[0].y = getIntOptionNamed(option, nOption, "y0", s->height / 2);

		p[1].x = getIntOptionNamed(option, nOption, "x1",
								   s->width - s->width / 4);
		p[1].y = getIntOptionNamed(option, nOption, "y1", s->height / 2);


		amp = getFloatOptionNamed(option, nOption, "amplitude", 0.25f);

		waterVertices(s, GL_LINES, p, 2, amp);

		damageScreen(s);
	}

	return FALSE;
}



static Bool
waterQuad(CompDisplay * d,
		  CompAction * action,
		  CompActionState state, CompOption * option, int nOption)
{
	CompScreen *s;
	int xid;

	xid = getIntOptionNamed(option, nOption, "root", 0);

	s = findScreenAtDisplay(d, xid);
	if (s)
	{
		XPoint p[4];
		float amp;

		/* top left corner */
		p[0].x = getIntOptionNamed(option, nOption, "x0", s->width / 4);
		p[0].y = getIntOptionNamed(option, nOption, "y0", s->height / 2);

		/* top right corner */
		p[1].x = getIntOptionNamed(option, nOption, "x1",
								   s->width - s->width / 4);
		p[1].y = getIntOptionNamed(option, nOption, "y1", s->height / 2);

		/* bottom right corner */
		p[2].x = getIntOptionNamed(option, nOption, "x2", s->width / 4);
		p[2].y = getIntOptionNamed(option, nOption, "y2", s->height / 2);

		/* bottom left corner */
		p[3].x = getIntOptionNamed(option, nOption, "x3",
								   s->width - s->width / 4);
		p[3].y = getIntOptionNamed(option, nOption, "y3", s->height / 2);

		amp = getFloatOptionNamed(option, nOption, "amplitude", 0.25f);

		waterVertices(s, GL_QUADS, p, 4, amp);

		damageScreen(s);
	}

	return FALSE;
}


static Bool
waterWin(CompDisplay * d,
		 CompAction * action,
		 CompActionState state, CompOption * option, int nOption)
{
	CompWindow *w;
	int xid;

	xid = getIntOptionNamed(option, nOption, "window", d->activeWindow);

	if (!xid)
		xid = d->activeWindow;

	w = findWindowAtDisplay(d, xid);
	if (w)
	{
		CompOption o[10];

		WATER_DISPLAY(d);
		WATER_SCREEN(w->screen);

		ws->current.w = w;
		defaultViewportForWindow(w, &ws->current.vpX, &ws->current.vpY);

		o[0].type = CompOptionTypeInt;
		o[0].name = "root";
		o[0].value.i = w->screen->root;

		/* top left */
		o[1].type = CompOptionTypeInt;
		o[1].name = "x0";
		o[1].value.i = w->attrib.x - w->input.left;

		o[2].type = CompOptionTypeInt;
		o[2].name = "y0";
		o[2].value.i = w->attrib.y - w->input.top;

		/* top right */
		o[3].type = CompOptionTypeInt;
		o[3].name = "x1";
		o[3].value.i = w->attrib.x + w->width + w->input.right;

		o[4].type = CompOptionTypeInt;
		o[4].name = "y1";
		o[4].value.i = w->attrib.y - w->input.top;

		/* bottom right */
		o[5].type = CompOptionTypeInt;
		o[5].name = "x2";
		o[5].value.i = w->attrib.x + w->width + w->input.right;

		o[6].type = CompOptionTypeInt;
		o[6].name = "y2";
		o[6].value.i = w->attrib.y + w->height + w->input.bottom;

		/* bottom left */
		o[7].type = CompOptionTypeInt;
		o[7].name = "x3";
		o[7].value.i = w->attrib.x - w->input.left;

		o[8].type = CompOptionTypeInt;
		o[8].name = "y3";
		o[8].value.i = w->attrib.y + w->height + w->input.bottom;

		o[9].type = CompOptionTypeFloat;
		o[9].name = "amplitude";
		o[9].value.f =
				getFloatOptionNamed(option, nOption, "amplitude", 0.25f);

		if (wd->opt[WATER_DISPLAY_OPTION_SHIVER_WIN].value.b)
		{
			/* Send a client message to wobbly */
			/* I'm not sure if this is the best way to do this, but it is simple and it works */
			XClientMessageEvent xev;

			xev.type = ClientMessage;
			xev.window = w->id;
			xev.message_type =
					XInternAtom(w->screen->display->display,
								"_BERYL_WOBBLY_SHIVER", 0);
			xev.format = 32;

			XSendEvent(w->screen->display->display,
					   w->screen->root, 0, StructureNotifyMask,
					   (XEvent *) & xev);
		}

		waterQuad(d, NULL, 0, o, 10);
	}

	return FALSE;
}

static void waterWindowUngrabNotify(CompWindow * w)
{

	WATER_DISPLAY(w->screen->display);
	WATER_SCREEN(w->screen);

	if (wd->opt[WATER_DISPLAY_OPTION_UNGRAB_WAVE].value.b &&
		!(w->state & CompWindowStateOffscreenMask))
	{
		/* generate a wave from the windows edges */
		CompOption o[2];

		o[0].type = CompOptionTypeInt;
		o[0].name = "window";
		o[0].value.i = w->id;

		o[1].type = CompOptionTypeFloat;
		o[1].name = "amplitude";
		o[1].value.f = wd->opt[WATER_DISPLAY_OPTION_TITLE_AMPL].value.f;

		waterWin(w->screen->display, NULL, 0, o, 2);
	}

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

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

	WATER_DISPLAY(d);

	switch (event->type)
	{
	case ButtonPress:
		s = findScreenAtDisplay(d, event->xbutton.root);
		if (s)
		{
			WATER_SCREEN(s);

			if (ws->grabIndex)
			{
				XPoint p;

				p.x = d->pointerX;
				p.y = d->pointerY;

				waterVertices(s, GL_POINTS, &p, 1, 0.8f);
				damageScreen(s);
			}
		}
		break;
	case EnterNotify:
	case LeaveNotify:
		waterHandleMotionEvent(d, event->xcrossing.root);
		break;
	case MotionNotify:
		waterHandleMotionEvent(d, event->xmotion.root);
	default:
		break;
	}

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

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

	WATER_DISPLAY(display);

	o = compFindOption(wd->opt, NUM_OPTIONS(wd), name, &index);
	if (!o)
		return FALSE;

	switch (index)
	{
	case WATER_DISPLAY_OPTION_INITIATE:
	case WATER_DISPLAY_OPTION_TOGGLE_RAIN:
	case WATER_DISPLAY_OPTION_TOGGLE_WIPER:
	case WATER_DISPLAY_OPTION_TITLE_WAVE:
	case WATER_DISPLAY_OPTION_POINT:
	case WATER_DISPLAY_OPTION_LINE:
	case WATER_DISPLAY_OPTION_QUAD:
	case WATER_DISPLAY_OPTION_WIN:
		if (setDisplayAction(display, o, value))
			return TRUE;
		break;
	case WATER_DISPLAY_OPTION_OFFSET_SCALE:
	case WATER_DISPLAY_OPTION_TITLE_AMPL:
	case WATER_DISPLAY_OPTION_RAIN_AMPL:
	case WATER_DISPLAY_OPTION_VISCOSITY:
		if (compSetFloatOption(o, value))
			return TRUE;
		break;
	case WATER_DISPLAY_OPTION_INIT_VERTICES:
		if (compSetIntOption(o, value))
			return TRUE;
		break;
	case WATER_DISPLAY_OPTION_RAIN_DELAY:
		if (compSetIntOption(o, value))
		{
			CompScreen *s;

			for (s = display->screens; s; s = s->next)
			{
				WATER_SCREEN(s);

				if (!ws->rainHandle)
					continue;

				compRemoveTimeout(ws->rainHandle);
				ws->rainHandle =
						compAddTimeout(o->value.i, waterRainTimeout, s);
			}
			return TRUE;
		}
		break;
	case WATER_DISPLAY_OPTION_UNGRAB_WAVE:
	case WATER_DISPLAY_OPTION_SHIVER_WIN:
		if (compSetBoolOption(o, value))
			return TRUE;
		break;
	default:
		break;
	}

	return FALSE;
}

static void waterDisplayInitOptions(WaterDisplay * wd)
{
	CompOption *o;

	o = &wd->opt[WATER_DISPLAY_OPTION_INITIATE];
	o->advanced = False;
	o->name = "initiate";
	o->group = N_("Pointer wave");
	o->subGroup = N_("Initiate");
	o->displayHints = "";
	o->shortDesc = N_("Initiate");
	o->longDesc = N_("Enable pointer Water Effects.");
	o->type = CompOptionTypeAction;
	o->value.action.initiate = waterInitiate;
	o->value.action.terminate = waterTerminate;
	o->value.action.bell = FALSE;
	o->value.action.edgeMask = 0;
	o->value.action.type = CompBindingTypeKey;
	o->value.action.state = CompActionStateInitKey;
	o->value.action.key.modifiers = WATER_INITIATE_MODIFIERS_DEFAULT;
	o->value.action.key.keysym = 0;

	o = &wd->opt[WATER_DISPLAY_OPTION_TOGGLE_RAIN];
	o->advanced = False;
	o->name = "toggle_rain";
	o->group = N_("Rain");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Toggle Rain");
	o->longDesc = N_("Toggle Rain Effect (random drops of water).");
	o->type = CompOptionTypeAction;
	o->value.action.initiate = waterToggleRain;
	o->value.action.terminate = 0;
	o->value.action.bell = FALSE;
	o->value.action.edgeMask = 0;
	o->value.action.type = CompBindingTypeKey;
	o->value.action.state = CompActionStateInitKey;
	o->value.action.key.modifiers = WATER_TOGGLE_RAIN_MODIFIERS_DEFAULT;
	o->value.action.key.keysym =
			XStringToKeysym(WATER_TOGGLE_RAIN_KEY_DEFAULT);

	o = &wd->opt[WATER_DISPLAY_OPTION_TOGGLE_WIPER];
	o->advanced = False;
	o->name = "toggle_wiper";
	o->group = N_("Rain");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Toggle Wiper");
	o->longDesc = N_("Toggle Wiper Effect.");
	o->type = CompOptionTypeAction;
	o->value.action.initiate = waterToggleWiper;
	o->value.action.terminate = 0;
	o->value.action.bell = FALSE;
	o->value.action.edgeMask = 0;
	o->value.action.type = CompBindingTypeKey;
	o->value.action.state = CompActionStateInitKey;
	o->value.action.key.modifiers = WATER_TOGGLE_WIPER_MODIFIERS_DEFAULT;
	o->value.action.key.keysym =
			XStringToKeysym(WATER_TOGGLE_WIPER_KEY_DEFAULT);

	o = &wd->opt[WATER_DISPLAY_OPTION_OFFSET_SCALE];
	o->advanced = False;
	o->name = "offset_scale";
	o->group = N_("Misc. Options");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Offset Scale");
	o->longDesc = N_("Adjusts the blur effect under the wave.");
	o->type = CompOptionTypeFloat;
	o->value.f = WATER_OFFSET_SCALE_DEFAULT;
	o->rest.f.min = WATER_OFFSET_SCALE_MIN;
	o->rest.f.max = WATER_OFFSET_SCALE_MAX;
	o->rest.f.precision = WATER_OFFSET_SCALE_PRECISION;

	o = &wd->opt[WATER_DISPLAY_OPTION_TITLE_AMPL];
	o->advanced = False;
	o->name = "title_wave_amplitude";
	o->group = N_("Title wave");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Title Wave Amplitude");
	o->longDesc = N_("Amplitude of title and window Wave.");
	o->type = CompOptionTypeFloat;
	o->value.f = WATER_TITLE_AMPL_DEFAULT;
	o->rest.f.min = WATER_TITLE_AMPL_MIN;
	o->rest.f.max = WATER_TITLE_AMPL_MAX;
	o->rest.f.precision = WATER_TITLE_AMPL_PRECISION;

	o = &wd->opt[WATER_DISPLAY_OPTION_RAIN_AMPL];
	o->advanced = False;
	o->name = "rain_wave_amplitude";
	o->group = N_("Rain");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Rain Wave Maximum Amplitude");
	o->longDesc = N_("Amplitude of Rain Wave.");
	o->type = CompOptionTypeFloat;
	o->value.f = WATER_RAIN_AMPL_DEFAULT;
	o->rest.f.min = WATER_RAIN_AMPL_MIN;
	o->rest.f.max = WATER_RAIN_AMPL_MAX;
	o->rest.f.precision = WATER_RAIN_AMPL_PRECISION;

	o = &wd->opt[WATER_DISPLAY_OPTION_VISCOSITY];
	o->advanced = False;
	o->name = "water_viscosity";
	o->group = N_("Misc. Options");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Water Viscosity");
	o->longDesc =
			N_("Water Viscosity (higher number results in larger wave).");
	o->type = CompOptionTypeFloat;
	o->value.f = WATER_VISCOS_DEFAULT;
	o->rest.f.min = WATER_VISCOS_MIN;
	o->rest.f.max = WATER_VISCOS_MAX;
	o->rest.f.precision = WATER_VISCOS_PRECISION;

	o = &wd->opt[WATER_DISPLAY_OPTION_INIT_VERTICES];
	o->advanced = False;
	o->name = "initial_vertices";
	o->group = N_("Misc. Options");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Initial Vertices");
	o->longDesc =
			N_
			("Initial number of Water Vertices (larger number results in larger wave duration).");
	o->type = CompOptionTypeInt;
	o->value.i = WATER_INIT_VERTICES_DEFAULT;
	o->rest.i.min = WATER_INIT_VERTICES_MIN;
	o->rest.i.max = WATER_INIT_VERTICES_MAX;

	o = &wd->opt[WATER_DISPLAY_OPTION_RAIN_DELAY];
	o->advanced = False;
	o->name = "rain_delay";
	o->group = N_("Rain");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Rain Delay");
	o->longDesc = N_("Delay (in ms) between each Raindrop.");
	o->type = CompOptionTypeInt;
	o->value.i = WATER_RAIN_DELAY_DEFAULT;
	o->rest.i.min = WATER_RAIN_DELAY_MIN;
	o->rest.i.max = WATER_RAIN_DELAY_MAX;

	o = &wd->opt[WATER_DISPLAY_OPTION_TITLE_WAVE];
	o->advanced = False;
	o->name = "title_wave";
	o->group = N_("Title wave");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Title Wave");
	o->longDesc = N_("Wave effect from window Title.");
	o->type = CompOptionTypeAction;
	o->value.action.initiate = waterTitleWave;
	o->value.action.terminate = 0;
	o->value.action.bell = WATER_TITLE_WAVE_BELL_DEFAULT;
	o->value.action.edgeMask = 0;
	o->value.action.type = CompBindingTypeNone;
	o->value.action.state = CompActionStateInitBell;

	o = &wd->opt[WATER_DISPLAY_OPTION_POINT];
	o->advanced = False;
	o->name = "point";
	o->group = N_("Misc. Options");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Point");
	o->longDesc = N_("Add Point.");
	o->type = CompOptionTypeAction;
	o->value.action.initiate = waterPoint;
	o->value.action.terminate = 0;
	o->value.action.bell = FALSE;
	o->value.action.edgeMask = 0;
	o->value.action.type = CompBindingTypeNone;
	o->value.action.state = 0;

	o = &wd->opt[WATER_DISPLAY_OPTION_LINE];
	o->advanced = False;
	o->name = "line";
	o->group = N_("");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Line");
	o->longDesc = N_("Add Line.");
	o->type = CompOptionTypeAction;
	o->value.action.initiate = waterLine;
	o->value.action.terminate = 0;
	o->value.action.bell = FALSE;
	o->value.action.edgeMask = 0;
	o->value.action.type = CompBindingTypeNone;
	o->value.action.state = 0;

	o = &wd->opt[WATER_DISPLAY_OPTION_QUAD];
	o->advanced = False;
	o->name = "quad";
	o->group = N_("");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Quad");
	o->longDesc = N_("Add Quad.");
	o->type = CompOptionTypeAction;
	o->value.action.initiate = waterQuad;
	o->value.action.terminate = 0;
	o->value.action.bell = FALSE;
	o->value.action.edgeMask = 0;
	o->value.action.type = CompBindingTypeNone;
	o->value.action.state = 0;

	o = &wd->opt[WATER_DISPLAY_OPTION_WIN];
	o->advanced = False;
	o->name = "window_wave";
	o->group = N_("Window wave");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Window Wave");
	o->longDesc = N_("Wave effect from Window.");
	o->type = CompOptionTypeAction;
	o->value.action.initiate = waterWin;
	o->value.action.terminate = 0;
	o->value.action.bell = FALSE;
	o->value.action.edgeMask = 0;
	o->value.action.state = CompActionStateInitKey;
	o->value.action.state |= CompActionStateInitButton;
	o->value.action.state |= CompActionStateInitBell;
	o->value.action.type = CompBindingTypeNone;

	o = &wd->opt[WATER_DISPLAY_OPTION_UNGRAB_WAVE];
	o->advanced = False;
	o->name = "ungrab_wave";
	o->group = N_("Window wave");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Ungrab Wave Effect");
	o->longDesc = N_("Wave when Ungrabing windows.");
	o->type = CompOptionTypeBool;
	o->value.b = WATER_UNGRAB_WAVE_DEFAULT;

	o = &wd->opt[WATER_DISPLAY_OPTION_SHIVER_WIN];
	o->advanced = False;
	o->name = "shiver_window";
	o->group = N_("Window wave");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Shiver Window");
	o->longDesc = N_("Wobbly Shiver when doing Window or ungrab wave.");
	o->type = CompOptionTypeBool;
	o->value.b = WATER_SHIVER_WIN_DEFAULT;
}

static CompOption *waterGetDisplayOptions(CompDisplay * display, int *count)
{
	if (display)
	{
		WATER_DISPLAY(display);

		*count = NUM_OPTIONS(wd);
		return wd->opt;
	}
	else
	{
		WaterDisplay *wd = malloc(sizeof(WaterDisplay));

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


static Bool waterInitDisplay(CompPlugin * p, CompDisplay * d)
{
	WaterDisplay *wd;

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

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

	waterDisplayInitOptions(wd);

	WRAP(wd, d, handleEvent, waterHandleEvent);

	d->privates[displayPrivateIndex].ptr = wd;

	return TRUE;
}

static void waterFiniDisplay(CompPlugin * p, CompDisplay * d)
{
	WATER_DISPLAY(d);

	freeScreenPrivateIndex(d, wd->screenPrivateIndex);

	UNWRAP(wd, d, handleEvent);

	free(wd);
}

static Bool waterInitScreen(CompPlugin * p, CompScreen * s)
{
	WaterScreen *ws;

	WATER_DISPLAY(s->display);

	ws = calloc(1, sizeof(WaterScreen));
	if (!ws)
		return FALSE;

	ws->grabIndex = 0;
	ws->current.w = NULL;
	ws->current.vpX = 0;
	ws->current.vpY = 0;

	addScreenAction(s, &wd->opt[WATER_DISPLAY_OPTION_INITIATE].value.action);
	addScreenAction(s,
					&wd->opt[WATER_DISPLAY_OPTION_TOGGLE_RAIN].value.action);
	addScreenAction(s,
					&wd->opt[WATER_DISPLAY_OPTION_TOGGLE_WIPER].value.action);
	addScreenAction(s,
					&wd->opt[WATER_DISPLAY_OPTION_TITLE_WAVE].value.action);
	addScreenAction(s, &wd->opt[WATER_DISPLAY_OPTION_POINT].value.action);
	addScreenAction(s, &wd->opt[WATER_DISPLAY_OPTION_LINE].value.action);
	addScreenAction(s, &wd->opt[WATER_DISPLAY_OPTION_QUAD].value.action);
	addScreenAction(s, &wd->opt[WATER_DISPLAY_OPTION_WIN].value.action);

	WRAP(ws, s, preparePaintScreen, waterPreparePaintScreen);
	WRAP(ws, s, donePaintScreen, waterDonePaintScreen);
	WRAP(ws, s, drawWindowTexture, waterDrawWindowTexture);
	WRAP(ws, s, windowUngrabNotify, waterWindowUngrabNotify);

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

	waterReset(s);

	return TRUE;
}

static void waterFiniScreen(CompPlugin * p, CompScreen * s)
{
	WaterFunction *function, *next;
	int i;

	WATER_SCREEN(s);
	WATER_DISPLAY(s->display);

	if (ws->rainHandle)
		compRemoveTimeout(ws->rainHandle);

	if (ws->wiperHandle)
		compRemoveTimeout(ws->wiperHandle);

	if (ws->fbo)
		(*s->deleteFramebuffers) (1, &ws->fbo);

	for (i = 0; i < TEXTURE_NUM; i++)
	{
		if (ws->texture[i])
			glDeleteTextures(1, &ws->texture[i]);
	}

	if (ws->program)
		(*s->deletePrograms) (1, &ws->program);

	if (ws->data)
		free(ws->data);

	function = ws->bumpMapFunctions;
	while (function)
	{
		destroyFragmentFunction (s, function->handle);

		next = function->next;
		free (function);
		function = next;
	}

	UNWRAP(ws, s, preparePaintScreen);
	UNWRAP(ws, s, donePaintScreen);
	UNWRAP(ws, s, drawWindowTexture);
	UNWRAP(ws, s, windowUngrabNotify);

	removeScreenAction(s,
					   &wd->opt[WATER_DISPLAY_OPTION_INITIATE].value.action);
	removeScreenAction(s,
					   &wd->opt[WATER_DISPLAY_OPTION_TOGGLE_RAIN].
					   value.action);
	removeScreenAction(s,
					   &wd->opt[WATER_DISPLAY_OPTION_TOGGLE_WIPER].
					   value.action);
	removeScreenAction(s,
					   &wd->opt[WATER_DISPLAY_OPTION_TITLE_WAVE].value.
					   action);
	removeScreenAction(s, &wd->opt[WATER_DISPLAY_OPTION_POINT].value.action);
	removeScreenAction(s, &wd->opt[WATER_DISPLAY_OPTION_LINE].value.action);
	removeScreenAction(s, &wd->opt[WATER_DISPLAY_OPTION_QUAD].value.action);
	removeScreenAction(s, &wd->opt[WATER_DISPLAY_OPTION_WIN].value.action);

	free(ws);
}

static Bool waterInit(CompPlugin * p)
{
	displayPrivateIndex = allocateDisplayPrivateIndex();
	if (displayPrivateIndex < 0)
		return FALSE;

	return TRUE;
}

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

static CompPluginVTable waterVTable = {
	"water",
	N_("Water Effect"),
	N_("Adds water effects to different desktop actions"),
	waterInit,
	waterFini,
	waterInitDisplay,
	waterFiniDisplay,
	waterInitScreen,
	waterFiniScreen,
	0,							/* InitWindow */
	0,							/* FiniWindow */
	waterGetDisplayOptions,
	waterSetDisplayOption,
	0,							/* GetScreenOptions */
	0,							/* SetScreenOption */
	NULL,
	0,
	0,
	0,
	BERYL_ABI_INFO,
	"beryl-plugins",
	"misc",
	0,
	0,
	False,
};

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