File: xsnow.c

package info (click to toggle)
xsnow 1.41-1
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 308 kB
  • ctags: 178
  • sloc: ansic: 1,214; makefile: 56; sh: 10
file content (1824 lines) | stat: -rwxr-xr-x 46,442 bytes parent folder | download | duplicates (2)
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
/*

Copyright 1984, 1988, 1990, 1993-1995, 2000 by Rick Jansen, all rights reserved.

	X S N O W 
				*
	*   *
		*       *
				*
	 *

 In 1984 the first Macintosh program I wrote was a computer christmas
 card, which showed a picture of a snowman and falling snow. Later
 a Father Xmas in his sleigh was added. I converted this program to
 an undying desk accessory in 1988 with several additions.
 But, little boys grow up, and when they are forced to a workstation
 they want their thingies there too. So here is Xsnow. For *you*.

 Happy winter and a merry X-mas!

 Rick Jansen
 (rja@euronet.nl)
 http://www.euronet.nl/~rja/
 Xsnow WWW page:  http://www.euronet.nl/~rja/Xsnow/ 


 Latest news: (December 16th 2000)
 ------------
 After 5 years here's a new Xsnow! The program has been around bit,
 and is now even making it snow on many windows machines, and we're
 not talking X or Macintosh here (sorry!). Anyway, it's high time for
 an improved version for X. Xsnow 1.41 works with modern X servers
 like XFree86, where older versions made snow build up somewhere in
 the sky at times, defying gravity.
	
 Xsnow 1.41 now features the (big) Santa that is long featured in Snow 
 for Macintosh and in Snow for Windows, many thanks to Thomas Linder!
 (Thomas.Linder@gmx.net).
	 
 It is now possible to slow Santa down. See the option -santaupdatefactor   

 Availability
 ------------
 Xsnow is available directly from me via my WWW page:

	 http://www.euronet.nl/~rja/

 Click on the appropriate item and the tar file will be downloaded
 upon your request.

 Xsnow is also archived at ftp.x.org in the contrib/games directory.
 Ftp to ftp.x.org and enter 'anonymous' as the login name, your
 e-mail address as the password. Then 'cd contrib/games' and a 
 'get xsnow.README' for further information.

 Copyright
 ---------
 Xsnow is available freely and you may give it to other people as is, 
 but I retain all rights. Therefore it does not classify as 'Public 
 Domain' software. It *is* Alwd to package Xsnow for Unix/Linux
 distributions, CD-Roms etc, and to make the necessary changes to
 makefiles etc. to facilitate this. 

 Credits
 -------
 Xsnow borrows code for drawing on the desktop (the 'root' window as
 they insist on calling it in X) from the infamous xroach program, 
 which is copyright 1991 by J.T. Anderson (jta@locus.com).
 Xroach is available in the 'contrib' directory of 'ftp.x.org' via 
 anonymous ftp.

 For compatibility with virtual window managers Xsnow uses vroot.h
 vroot.h is copyright 1991 by Andreas Stolcke, copyright 1990 by 
 Solbourne Computer Inc. (stolcke@ICSI.Berkeley.EDU)
 The complete distribution of vroot is available in the 'contrib'
 directory of 'ftp.x.org' via anonymous ftp as 'vroot.shar.Z' 

 The code for wind was done by Eiichi TAZOE (tazoe@yamato.ibm.co.jp,
 tazoe@vnet.ibm.com)
		
 Installation
 ------------
	To build:
			xmkmf
			make depend
			make

	To run:
			./xsnow
		or:
			./xsnow&  (in the background)

	To install (be root):
			make install
			make install.man
	or:
			cc -o xsnow snow.c -lX11 [-lsocketorwhatever] [-lm] [-l...] -lXpm
 
	SUN Solaris users without a C-compiler, but with gcc installed,
	try the following two commands. The first compiles Xsnow, the second
	links Xsnow. 

		 gcc -c -O2 -I/usr/openwin/include -DSVR4 -DSYSV xsnow.c
		 gcc -o xsnow xsnow.o -O2 -L/usr/openwin/lib -lXext -lX11 -lXpm -lm -lsocket -lnsl

	Note: Xsnow-1.41 needs the Xpm library. It is available from:
	ftp://ftp.x.org/contrib/libraries/
	The current Xpm version is xpm3.4k  

 Silicon Graphics
 ----------------
 Silicon Graphics and Irix 5.x users may not see any snow or
 Santa at all, as long the desktop icons are visible. To
 circumvent this problem issue this command:

		/usr/lib/desktop/telldesktop quit

 The icons will disappear and xsnow will work perfectly. To
 restart the desktop just start /usr/lib/desktop/startdesktop
 or select Desktop->Home Directory from the toolchest.

 It's even possible to have both - desktop icons and xsnow
 (and even multiple desks). You need to modify the window
 manager's resource file 4DWm, the file ~/.desktop-
 `hostname`/4DWm. Example:

 *Global.backgroundDescription:   -execute /etc/killall -TERM
 xsnow ; /usr/local/bin/xsnow
 *Desk 1.backgroundDescription:   -execute /etc/killall -TERM
 xsnow ; /usr/local/bin/xsnow
 *Desk 2.backgroundDescription:   -execute /etc/killall -TERM
 xsnow ; /usr/local/bin/xsnow

 Restart the he toolchest and Xsnow should appear. What this
 does is stop the currently running Xsnow and start a new one
 when you switch to another desktop.

 HP
 --
 HP also uses a Workspace Manager which may interfere with
 Xsnow.  If Xsnow does not appear:  In the "Style Manager",
 choose "Backdrop" and select "NoBackdrop".  You should now
 be able to run Xsnow.


 Modification history
 --------------------
 1984/1985 Macintosh animated Christmas card
 1988/1990 Macintosh undying desk accessory, and MultiFinder version
 03DEC93 version 0 for X-windows
 04DEC93 background setting
 06DEC93 delay in main loop using select
 10DEC93 keep snow on top of windows and at bottom of display
 11DEC93 snow kept sticking to the right of display (1.11)
 11DEC93 updated calcwindowtops
 12DEC93 call calcwindowtops after last exposure event only
 13DEC93 vroot added for virtual window managers
 14DEC93 separate options for snowdepth on windows and on screen
 15DEC93 patch for fvwm from Mike Hollick (hollick@graphics.cis.upenn.edu)
 16DEC93 Really building up snow at bottom of screen
 16DEC93 A choice of Santa's. Thanks to Holger Veit (Holger.Veit@gmd.de)
				 for Santa no. 2. Santa 1 was derived from 2 by scaling down.
 20DEC93 Aaaargh! gnu C doesn't like the initialization of the new
				 Santa pixmaps. Extra braces do the trick, which is so often the case
 20DEC93 Deleted PaintSnowAtBottom. It's more of a surprise this way.
 21DEC93 In case of negative x coordinates adapt width (CalcWindowTops)
 22DEC93 Rudolf's red nose. Silly. Grmbll.
 22DEC93 Bug with -nokeepsnowonwindows removed
 10JAN94 Improved handling of erasing snow at screen bottom in case of 
				 window in or under the snow.
 18FEB94 X-speed really 0 inside snow layers - prevents snowflakes from 
				 erasing already fallen snow a bit better
 30OCT94 More efficiency due to GC's in server and not drawing where
				 the windows are.
 30OCT94 1.30 Eiichi TAZOE's code for wind included in general distribution.
 11NOV94 Bug in XRectInRegion call resolved (santa not appearing gradually
				 from behind windows)
 15NOV94 Unsigned longs changed to longs, as strtoul is troublesome on many
				 systems (snowDelay)
 08DEC94 Snow is really building up on windows now
 10DEC94 1.36 Handle Popups (no snow on them)
 16DEC94 1.37 -offset and (-nokeepsnowonwindows and -nokeepsnowonscreen back)
 20DEC94 1.38 Let Santa be a real snowplough now
 21DEC94 1.39 VMS fix for uSsleep, thanks to Ulrich Kabatek 
							<Kaba@isr.uni-stuttgart.d400.de>
 19NOV95 1.40 Memory leak fixed in CalcWindowTops, thanks to Satoshi Asami
							<asami@cs.berkeley.edu> 
 19NOV95 1.40 Greatly increased performance when using solid backgrounds
							by using XFillRectangle instead of XClearArea. Thanks to
							Benedikt Kessler <bjk@benedikt.munich.sgi.com>
 20NOV95 1.40 Bug fixed with erasing snow at bot of screen
 1996    New macintosh version, in color http://www.euronet.nl/~rja/MacSnow/
 1998    Snow for Windows http://www.euronet.nl/~rja/WinSnow/
 18DEC2000	1.41 Fix for X-servers that do not send expose events after a
						while, due to backing store.
*/

#define debug 0


/*
	And in a vocoded voice it sounds:
		Xsnow zwei-tausend
		Xsnow two-thousand 
		Xsnow deux mille
		Xsnow dos mil
	etc.
*/
#define VERSION "Xsnow-1.41, December 16th 2000 by Rick Jansen (rja@euronet.nl)\n\
WWW: http://www.euronet.nl/~rja/Xsnow/\n"


#ifdef VMS
#include <socket.h>
# if defined(__SOCKET_TYPEDEFS) && !defined(CADDR_T)
#  define CADDR_T
# endif
#endif


#include <X11/Intrinsic.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/xpm.h>

/**** V R O O T ****/
/* For vroot.h see the credits at the beginning of Xsnow */
/***#include <X11/vroot.h>   /* if vroot.h installed in /usr/include/X11 ***/
#include "vroot.h"  /* Use this if vroot.h installed in current directory */

#include <stdio.h>
#include <math.h>
#include <signal.h>
#include <limits.h>
#include <string.h>


#if defined(__STDC__) || defined(VMS)
#include <stdlib.h>
#else
long strtol();
/*double strtod();*/
char *getenv();
#endif

char Copyright[] = "\nXsnow\nCopyright 1984,1988,1990,1993-1995,2000 by Rick Jansen, all rights reserved.\n";

#include "xsnow.h"

typedef int ErrorHandler();

#if !defined(GRAB_SERVER)
#define GRAB_SERVER	0
#endif

Display *display;
int screen;
Window rootWin;
int display_width, display_height;
int center_x, center_y;
GC gc;
char *display_name = NULL;
Pixel black, white;

int done = 0;
int errorVal = 0;

int flake;
int SmoothWhirl = 1;
int NoTrees = 0;
int NoSanta = 0;
int Rudolf = 1;
int NoKeepSnow = 0;
int NoKeepSBot = 0;
int NoKeepSWin = 0;
int NoPopuphandling = 1;
int NoWind = 0;
unsigned int borderWidth = 0;
int SnowOffset = 0;
int UseFillForClear = 0;

Snow *snowflakes;
int MaxSnowFlakeHeight = 0;  /* Biggest flake */
int maxSnowflakes = INITIALSNOWFLAKES;
int curSnowflakes = 0;
long snowDelay = 50000; /* useconds */
int MaxXStep = MAXXSTEP;
int MaxYStep = MAXYSTEP;
int WhirlFactor = WHIRLFACTOR;
int WhirlAdapt;
int MaxWinSnowDepth = INITWINSNOWDEPTH;
int MaxScrSnowDepth = INITSCRSNOWDEPTH;


/* The default Santa 0,1,2 */
int SantaSize = 2;
int SantaSpeed = -1;  /* Not initialized yet */
#define	MAXSANTA_SPEED 30

int SantaX;
int SantaY;
int SantaXStep;
int SantaYStep;
int SantaVisible;
int CurrentSanta;
Pixmap SantaPixmap[PIXINANIMATION];
Pixmap SantaMaskPixmap[PIXINANIMATION];
int SantaWidth;
int SantaHeight;   


/* For building up snow at bottom of screen and windows */
Region WindowsR = NULL;
Region PWindowsR = NULL;
Region WindowsDifferenceR = NULL;
Region CtchDifferenceR = NULL;   
Region RecalcSnowCtchR = NULL;

Region SnowCtchR = NULL;
Region SnowAlwdR = NULL;
Region SubR = NULL;
int StillDoDiffToDoFlag = 0;

/* Wind stuff */
int diff=0;
int wind = 0;
int direction=0;
int WindTimer=30;
int current_snow_height;
int geta=0;
XRectangle AddRect;

unsigned int RunCounter = 0;
int SantaUpdateFactor = 3;

/* Forward decls */
void Usage();
void SigHandler();
void SigHupHandler();
void InitSnowflake();
void UpdateSnowflake();
void DrawSnowflake();
void EraseSnowflake();
void PaintSnowAtBottom();
void DrawTannenbaum();
void InitSantaPixymaps();
void ResetSanta();
void DrawSanta();
void EraseSanta();
void UpdateSanta();
void uSsleep();
int MakeWR(int);
void MkASR();
void ReclcCSR();
int DoDffWLT();     
void HandleEvA(int X,int Y,int W, int H);
Pixel AllocNamedColor();
void sig_alarm();



/* Colo(u)rs */
char *snowColor = "snow";
char *slcColor = "black";
char *blackColor = "black";
char *redColor = "red";
char *whiteColor = "white";
char *bgColor = "none";
char *trColor = "chartreuse";
/* The author thoroughly recommends a cup of tea */
/* with a dash of green Chartreuse. Jum!         */
char *greenColor = "chartreuse"; 
Pixel redPix;
Pixel whitePix;
Pixel greenPix;
Pixel blackPix;
Pixel snowcPix;
Pixel bgPix;
Pixel trPix;
Pixel slcPix;

/* GC's */
GC SnowGC[SNOWFLAKEMAXTYPE+1];
GC ClearSnowGC;
GC SleighGCs[3];
GC SantaGC,RudolfGC,FurGC;
GC TreesGC;

int rc;


/* ------------------------------------------------------------------ */ 
int main(ac, av)
int ac;
char *av[];
{
		XGCValues xgcv;
		int ax;
		char *arg;
		SnowMap *rp;
		TannenbaumMap *tp;
		XEvent ev;
		int Exposed;
		int ConfigureNotified;
		int i; 
		int x,y;

		Window root; /* used in getgeometry call */
		int winX, winY;
		unsigned int winHeight, winWidth;
		unsigned int depth;
		
		/*
			 Process command line options.
		*/
		for (ax=1; ax<ac; ax++) {
			arg = av[ax];
			if (strcmp(arg, "-display") == 0) {
				display_name = av[++ax];
			}
			else if (strcmp(arg, "-sc") == 0) {
				snowColor = av[++ax];
			}
			else if (strcmp(arg, "-bg") == 0) {
				bgColor = av[++ax];
 
				/* If solid colour used as bg we can use XFillArea for clear */
				/* This results in a big performance improvement */
				/*** Uncomment this if sure all Xservers are faster with
					 XFillRectangle than with XClearArea ***/
				/** December 2000: well, this is NOT true for all X-servers!
						It does work this way on SGI, but on SUN Solaris 2.5.1 
						UseFillForClear costs 20% *more* resources! **/
				/*** UseFillForClear = 1; ***/
			}
			else if (strcmp(arg, "-solidbg") == 0) {
				/*	
						If solid colour used as bg we can use XFillArea for clear
						This results in a big performance improvement, but not on all
						X-servers! (Yes on SGI, NO on SUN (Solaris 2.5.1) (Dec 2000)
				*/
				UseFillForClear = 1;
			}
			else if (strcmp(arg, "-tc") == 0) {
				trColor = av[++ax];
			}
			else if (strcmp(arg, "-slc") == 0) {
				/* Obsolete option, Santa is a colour pic now */
				/*slcColor = av[++ax];*/
				fprintf(stderr,"** The option -slc (sleigh color) is obsolete\n");
			}
			else if (strcmp(arg, "-delay") == 0) {
				snowDelay = strtol(av[++ax], (char **)NULL,0);
				snowDelay = snowDelay * 1000;  /* ms to us */
			}
			else if (strcmp(arg, "-santaupdatefactor") == 0) {
				SantaUpdateFactor = strtol(av[++ax], (char **)NULL,0);
			}
			else if (strcmp(arg, "-snowflakes") == 0) {
				maxSnowflakes = strtol(av[++ax], (char **)NULL, 0);
			}
			else if (strcmp(arg, "-unsmooth") == 0) {
				SmoothWhirl = 0;
			}
			else if (strcmp(arg, "-whirl") == 0) {
				WhirlFactor = strtol(av[++ax], (char **)NULL, 0);
			}
			else if (strcmp(arg, "-nowind") == 0) {
				NoWind = 1;
			}
			else if (strcmp(arg, "-windtimer") == 0) {
				WindTimer = strtol(av[++ax], (char **)NULL, 0);
			}
			else if (strcmp(arg, "-xspeed") == 0) {
				MaxXStep = strtol(av[++ax], (char **)NULL, 0);
			}
			else if (strcmp(arg, "-yspeed") == 0) {
				MaxYStep = strtol(av[++ax], (char **)NULL, 0);
			}
			else if (strcmp(arg, "-wsnowdepth") == 0) {
				MaxWinSnowDepth = strtol(av[++ax], (char **)NULL, 0);
			}
			else if (strcmp(arg, "-ssnowdepth") == 0) {
				MaxScrSnowDepth = strtol(av[++ax], (char **)NULL, 0);
			}
			else if (strcmp(arg, "-offset") == 0) {
				SnowOffset = strtol(av[++ax], (char **)NULL, 0);
			}
			else if (strcmp(arg, "-notrees") == 0) {
				NoTrees = 1;     
			}
			else if (strcmp(arg, "-nosanta") == 0) {
				NoSanta = 1;     
			}
			else if (strcmp(arg, "-santa") == 0) {
				SantaSize = strtol(av[++ax], (char **)NULL, 0);
			}
			else if (strcmp(arg, "-norudolf") == 0) {
				Rudolf = 0;
			}
			else if (strcmp(arg, "-santaspeed") == 0) {
				SantaSpeed = strtol(av[++ax], (char **)NULL, 0);
			}
			else if (strcmp(arg, "-nokeepsnow") == 0) {
				NoKeepSnow = 1;
				NoKeepSWin = 1;
				NoKeepSBot = 1;
			}
			else if (strcmp(arg, "-nokeepsnowonwindows") == 0) {
				NoKeepSWin = 1;
			}
			else if (strcmp(arg, "-nokeepsnowonscreen") == 0) {
				NoKeepSBot = 1;
			}

			/* And now for some confusion */
			else if (strcmp(arg, "-popup") == 0) {
				NoPopuphandling = 1;
			}
			else if (strcmp(arg, "-nopopup") == 0) {
				NoPopuphandling = 0;
			}
			else if (strcmp(arg, "-nonopopup") == 0) {
				NoPopuphandling = 1;
			}
			else if (strcmp(arg, "-nononopopup") == 0) {
				NoPopuphandling = 0;
			}

			else if (strcmp(arg, "-version") == 0) {
				printf("\nThis is %s\n\n", VERSION);
				exit(0);
			}
			else {
				Usage();
			}
		}

		printf("%s\n",VERSION);

		/* Some people... */
		if (maxSnowflakes > MAXSNOWFLAKES) {
			fprintf(stderr,"** Maximum number of snowflakes is %d\n", MAXSNOWFLAKES);
			exit(1);
		}
		if ((SantaSize < 0) || (SantaSize > MAXSANTA)) {
			fprintf(stderr,"** Maximum Santa is %d\n",MAXSANTA);
			exit(1);
		}
		if (SantaUpdateFactor == 0) {
			fprintf(stderr,"** It is naughty (sic!) to specify zero for -santaupdatefactor\n");
			exit(1);
		}

		/* Eskimo warning */
		if (strstr(snowColor,"yellow") != NULL)
			printf("\nWarning: don't eat yellow snow!\n\n");


		/* Santa speed is different for different size Santa */
		if (SantaSpeed < 0) SantaSpeed = Speed[SantaSize];

		/* Seed random */
#ifdef VMS
		srand((int)time((unsigned long *)NULL));
#else
		srand((int)time((long *)NULL));
#endif


		/*
			 Ctch some signals so we can erase any visible snowflakes.
		*/
		signal(SIGKILL, SigHandler);
		signal(SIGINT, SigHandler);
		signal(SIGTERM, SigHandler);
#if debug
		signal(SIGHUP, SigHupHandler);
#else
		signal(SIGHUP, SigHandler);
#endif

		/* Wind stuff */
		signal(SIGALRM, sig_alarm);
		alarm(WindTimer);


		/* Open X */
		display = XOpenDisplay(display_name);
		if (display == NULL) {
			if (display_name == NULL) display_name = getenv("DISPLAY");
			(void) fprintf(stderr, "%s: cannot connect to X server %s\n", av[0],
				display_name ? display_name : "(default)");
				exit(1);
		}

		screen = DefaultScreen(display);
		rootWin = RootWindow(display, screen);
		black = BlackPixel(display, screen);
		white = WhitePixel(display, screen);

		display_width = DisplayWidth(display, screen);
		display_height = DisplayHeight(display, screen);
		center_x = display_width / 2;
		center_y = display_height / 2;

		/* No snow at all yet */
		current_snow_height = display_height;

		/* Do not let all of the display snow under */
		if (MaxScrSnowDepth> (display_height-SNOWFREE)) {
			printf("** Maximum snow depth set to %d\n", display_height-SNOWFREE);
			MaxScrSnowDepth = display_height-SNOWFREE;
		}
		

		/*
				P I X M A P S 
		*/

		/* Create the snowflake pixmaps */
		for (flake=0; flake<=SNOWFLAKEMAXTYPE; flake++) {
			rp = &snowPix[flake];
			rp->pixmap = XCreateBitmapFromData(display, rootWin,
						rp->snowBits, rp->width, rp->height);
			/* Whats the biggest flake? (used for erasing kept snow later) */
			if (rp->height > MaxSnowFlakeHeight) MaxSnowFlakeHeight = rp->height;
		}

		/* Allocate structures containing the coordinates and things */
		snowflakes = (Snow *)malloc(sizeof(Snow) * MAXSNOWFLAKES);

		/* Create the tree pixmap */
		tp = &tannenbaumPix[0];
		tp->pixmap = XCreateBitmapFromData(display, rootWin,
						tp->tannenbaumBits, tp->width, tp->height);


		/* Create the Santa pixmaps, dependent on the Santa and Rudolf wanted */
		InitSantaPixymaps();

		/* Allocate colors just once */
		redPix =   AllocNamedColor(redColor, black);
		whitePix = AllocNamedColor(whiteColor, white);
		greenPix = AllocNamedColor(greenColor, black);
		blackPix = AllocNamedColor(blackColor, black);
		snowcPix = AllocNamedColor(snowColor, white);   
		trPix = AllocNamedColor(trColor, black);
		/*slcPix = AllocNamedColor(slcColor, black);*/


		gc = XCreateGC(display, rootWin, 0, NULL);
		XGetGCValues(display, gc, 0, &xgcv);
		XSetForeground(display, gc, blackPix);
		XSetFillStyle(display, gc, FillStippled);

		/* Set the background color, if specified */
		if(strcmp(bgColor,"none") != 0) {
			bgPix = AllocNamedColor(bgColor, white);

			XSetWindowBackground(display, rootWin, bgPix);
			XClearWindow(display, rootWin);
			XFlush(display);

			/* If -solidbg not specified notify that this may improve things! */
			if (!UseFillForClear) {
				printf("\nNote: when using backgrounds of one color also specifying\n");
				printf("      -solidbg MAY greatly improve performance!\n");
			}
			else {
				ClearSnowGC = XCreateGC(display, rootWin, 0L, &xgcv);
				XCopyGC(display,gc,0,ClearSnowGC);
				XSetForeground(display,ClearSnowGC, bgPix);
			}
		}
		else if (UseFillForClear) {
			printf("\n");
			printf(
			 "Sorry, -solidbg can only be specified with a background color (-bg)");
			printf("\n");
			exit(1);
		}
		
		/* Fill in GCs for snowflakes */
		for (flake=0; flake<=SNOWFLAKEMAXTYPE; flake++) {
			SnowGC[flake] = XCreateGC(display, rootWin, 0L, &xgcv);
			XCopyGC(display,gc,0,SnowGC[flake]);
			XSetStipple(display, SnowGC[flake], snowPix[flake].pixmap);
			XSetForeground(display,SnowGC[flake],snowcPix);
			XSetFillStyle(display, SnowGC[flake], FillStippled);
		}
		
		/* GC for trees */
		TreesGC = XCreateGC(display, rootWin, 0L, &xgcv);
		XCopyGC(display,gc,0,TreesGC);
		XSetStipple(display, TreesGC, tannenbaumPix[0].pixmap);
		XSetForeground(display,TreesGC,trPix);
		XSetFillStyle(display, TreesGC, FillStippled);

		/*
		 *
		 * Here snowing starts
		 *
		 */

		WindowsR = XCreateRegion();
		PWindowsR = XCreateRegion();
		SnowCtchR = XCreateRegion();
		SnowAlwdR = XCreateRegion();
		WindowsDifferenceR = XCreateRegion();
		CtchDifferenceR = XCreateRegion();                                                                                 
		RecalcSnowCtchR = XCreateRegion();

		/* The initial Ctch region: a little bit below the bottom of the screen */
		AddRect.x = 0;
		AddRect.y = display_height;
		AddRect.width = display_width - 1;
		AddRect.height = MaxYStep+MaxSnowFlakeHeight;
		if (!NoKeepSBot)
			XUnionRectWithRegion(&AddRect, SnowCtchR, SnowCtchR);

		/* Initialize all snowflakes */
		for (i=0; i<maxSnowflakes; i++) InitSnowflake(i);

    rc = MakeWR(1);
		XUnionRegion(WindowsR,PWindowsR, PWindowsR);                                                                 
		ReclcCSR();
		MkASR();

		ResetSanta();   

		/* Notify if part of the root window changed */
		XSelectInput(display, rootWin, ExposureMask | SubstructureNotifyMask);


		/*
		 *  M A I N   L O O P
		 */
		while (!done) {

			/* X event ? */
			Exposed = 0;
			ConfigureNotified = 0;
			while (XPending(display) || StillDoDiffToDoFlag) {

				XNextEvent(display, &ev);

				/* If we still had a DoDffWLT to do, do it now. */
				if (StillDoDiffToDoFlag) {
					StillDoDiffToDoFlag = !DoDffWLT();
					/*continue;*/
				}


				/* No need to do all this computing if we're not keeping the snow */
				if (!NoKeepSnow) {

					switch (ev.type) {

							case Expose:    
								
								  Exposed = 1;
									if (!DoDffWLT()) {
										StillDoDiffToDoFlag = 1;
										continue; 
									}
									/*HandleEvA(ev.xexpose.x,ev.xexpose.y,ev.xexpose.height,ev.xexpose.width);*/

								  break;


							case ConfigureNotify:
									if (!DoDffWLT()) {
										StillDoDiffToDoFlag = 1;
										continue; 
									}
									/*HandleEvA(ev.xconfigure.x,ev.xconfigure.y,ev.xconfigure.height,ev.xconfigure.width);*/
								  break;
							case ConfigureRequest:
								  break;

							case CreateNotify:
									DoDffWLT(); 
									if (!DoDffWLT()) {
										StillDoDiffToDoFlag = 1;
										continue; 
									}
								  break;
							case DestroyNotify:

									if (!DoDffWLT()) {
										StillDoDiffToDoFlag = 1;
										continue; 
									}
								  break;

							case MapNotify:
									if (!DoDffWLT()) {
										StillDoDiffToDoFlag = 1;
										continue; 
									}

									XGetGeometry(	ev.xmap.display, ev.xmap.window, 
																&root, &winX, &winY, &winWidth, &winHeight, &borderWidth, &depth);
								  break;
							case UnmapNotify:
									if (!DoDffWLT()) {
										StillDoDiffToDoFlag = 1;
										continue; 
									}

									/* Cannot fetch geometry of a window that has already been destroyed...
									XGetGeometry(	ev.xunmap.display, ev.xunmap.window, 
																&root, &winX, &winY, &winWidth, &winHeight, &borderWidth, &depth);
									printf("UnmapNotify r=%d w=%d geo=(%d,%d,%d,%d) bw=%d d=%d\n\n", 
														root, ev.xunmap.window, winX,winY,winWidth,winHeight, borderWidth,depth);
									*/
								  break;

					} /* switch */

				}  /* if (!NoKeepSnow) */
			 
			}  /* while Xpending */

			/* If things have changed while we were away... */


#if debug
/***
XClearWindow(display, rootWin);
XFlush(display);
XSetForeground(display,FurGC,trPix);
XSetFillStyle(display,FurGC, FillSolid);
XSetRegion(display,FurGC,SnowCtchR);
XFillRectangle(display,rootWin,FurGC,0,0,display_width,display_height);
XFlush(display);
***/
#endif


			/* Sleep a bit */
			uSsleep(snowDelay);

			/* 
			 *  Update     
			 */

			RunCounter++;

			/* Snowflakes */
			for (i=0; i<maxSnowflakes; i++) UpdateSnowflake(i);

			/* Draw dear Andrews */
			if (!NoTrees ) {
				DrawTannenbaum(display_width-150, display_height-500);
				DrawTannenbaum(display_width-100, display_height-200);
				DrawTannenbaum(100, display_height-200);
				DrawTannenbaum(50, display_height-150);
				DrawTannenbaum(center_x, display_height-100);
				DrawTannenbaum(200,400);
			}

			/* Dear Santa */
			if (!NoSanta) {
				if ((RunCounter % SantaUpdateFactor) == 0) UpdateSanta();
			}

		}

/***
sleep(10);
XClearWindow(display, rootWin);
XFlush(display);
XSetFillStyle(display,FurGC, FillSolid);
XSetRegion(display,FurGC,SnowAlwdR);
XFillRectangle(display,rootWin,FurGC,0,0,display_width,display_height);
XFlush(display);
sleep(4);
***/
		
		XDestroyRegion(WindowsR);
		XDestroyRegion(SnowCtchR);
		XDestroyRegion(SnowAlwdR);
		XDestroyRegion(PWindowsR);
		XDestroyRegion(WindowsDifferenceR);
		XDestroyRegion(RecalcSnowCtchR);

		XClearWindow(display, rootWin);

		XCloseDisplay(display);

		exit(0);

}		/* End of the snow */


/* ------------------------------------------------------------------ */ 







/* ------------------------------------------------------------------ */ 

#define USEPRT(msg) fprintf(stderr, msg)

void
Usage()
{
		USEPRT("Usage: xsnow [options]\n\n");
		USEPRT("Options:\n");
		USEPRT("       -display     <displayname>\n");
		USEPRT("       -sc          <snowcolor>\n");
		USEPRT("       -tc          <tree color>\n");
		USEPRT("       -bg          <background color>\n");
		USEPRT("       -solidbg     (Performance improvement!)\n");
		USEPRT("       -snowflakes  <numsnowflakes>\n");
		USEPRT("       -delay       <delay in milliseconds>\n");
		USEPRT("       -unsmooth\n");
		USEPRT("       -whirl       <whirlfactor>\n");
		USEPRT("       -nowind\n");
		USEPRT("       -windtimer   <Time between windy periods in seconds>\n");
		USEPRT("       -xspeed      <max xspeed snowflakes>\n");
		USEPRT("       -yspeed      <max yspeed snowflakes>\n");
		USEPRT("       -wsnowdepth  <max snow depth on windows>\n");
		USEPRT("       -offset      <shift snow down>\n");
		USEPRT("       -ssnowdepth  <max snow depth at bottom of display>\n");
		USEPRT("       -notrees\n");
		USEPRT("       -nosanta\n");
		USEPRT("       -norudolf\n");
		USEPRT("       -santa       <santa>\n");
		USEPRT("       -santaspeed  <santa_speed>\n");
		USEPRT("       -santaupdatefactor  <factor>\n");
		USEPRT("       -nokeepsnow\n");
		USEPRT("       -nokeepsnowonwindows\n");
		USEPRT("       -nokeepsnowonscreen\n");
		USEPRT("       -nonopopup\n");
		USEPRT("       -version\n\n");
		USEPRT("Recommended: xsnow -bg SkyBlue3\n");
		
		exit(1);
}
/* ------------------------------------------------------------------ */ 

void
SigHandler()
{
	done = 1;
}


void
SigHupHandler()
{
	void (*Sig_Hup_ptr)() = SigHupHandler;

	/*XClearWindow(display, rootWin);*/
	XFlush(display);
	XSetForeground(display,FurGC,trPix);
	XSetFillStyle(display,FurGC, FillSolid);
	XSetRegion(display,FurGC,SnowCtchR);
	XFillRectangle(display,rootWin,FurGC,0,0,display_width,display_height);
	XFlush(display);

	signal(SIGHUP, Sig_Hup_ptr);
}

/* ------------------------------------------------------------------ */ 

/*
	 Generate random integer between 0 and maxVal-1.
*/
int
RandInt(maxVal)
int maxVal;
{
	return rand() % maxVal;
}



/* ------------------------------------------------------------------ */ 

/*
 * sleep for a number of micro-seconds
 */
void uSsleep(usec) 
unsigned long usec;
{
#ifdef SVR3
		poll((struct poll *)0, (size_t)0, usec/1000);   /* ms resolution */
#else
#ifdef VMS
		void lib$wait();
		float t;
		t= usec/1000000.0;
		lib$wait(&t);
#else
		struct timeval t;
		t.tv_usec = usec%(unsigned long)1000000;
		t.tv_sec = usec/(unsigned long)1000000;
		select(0, (void *)0, (void *)0, (void *)0, &t);
#endif
#endif
}


/* ------------------------------------------------------------------ */ 

/*
	 Check for topleft point of snowflake in specified rectangle.
*/
int
SnowPtInRect(snx, sny, recx, recy, width, height)
int snx;
int sny;
int recx;
int recy;
int width;
int height;
{
		if (snx < recx) return 0;
		if (snx > (recx + width)) return 0;
		if (sny < recy) return 0;
		if (sny > (recy + height)) return 0;
		
		return 1;
}



/*
	 Give birth to a snowflake.
*/
void
InitSnowflake(rx)
int rx;
{
		Snow *r;

		r = &snowflakes[rx];

	if (curSnowflakes < maxSnowflakes) {
		r->whatFlake = RandInt(SNOWFLAKEMAXTYPE+1);

		/* Wind strikes! */
		if (wind) {
			if (direction == 1) 
				/* Create snow on the left of the display */
				r->intX = RandInt(display_width/3);
			else
				/* Create snow on the right of the display */
				r->intX = display_width - RandInt(display_width/3);
			r->intY =  RandInt(display_height);
		}
		else  {
			r->intX = RandInt(display_width - snowPix[r->whatFlake].width);
			r->intY =  RandInt(display_height/10);
		}

		r->yStep = RandInt(MaxYStep+1)+1;
		r->xStep = RandInt(r->yStep/4+1);
		if (RandInt(1000) > 500) r->xStep = -r->xStep;
		r->active = 1;
		r->visible = 1;
		r->insnow = 0;
	}
}



/*
	 Move a snowflake by erasing and redraw
*/
void
UpdateSnowflake(rx)
int rx;
{
Snow *snow;

int NewX;
int NewY;
int tmp_x;
int TouchDown;
int InVisible;
int NoErase;
		
	/* move an active snowflake */

	snow = &snowflakes[rx];
	NoErase = 0;  /* Always erase the flakes unless special case */


	/* Activate a new snowflake */
	if (!snow->active) {
		InitSnowflake(rx);
		/* Make sure newly created flakes aren't erased before actually drawn */
		/* Especially with wind, where flakes are created all over the screen */
		snow->insnow = 1; 
	}

	/* New x,y */
	if (wind) {
		tmp_x = abs(snow->xStep);
		if( wind == 1 ){
				tmp_x = tmp_x + (RandInt(WhirlFactor+1) - WhirlFactor/2);
		}else{         
				tmp_x = tmp_x + (RandInt(20));
		}
		snow->xStep = tmp_x * direction;

		if (snow->xStep > 50) snow->xStep = 50;
		if (snow->xStep < -50) snow->xStep = -50;

		if( wind == 1 ){
				NewY = NewY+3;
		}else{
				NewY = NewY+10;
		}
	}
	NewX = snow->intX + snow->xStep;
	NewY = snow->intY + snow->yStep;


	/* If flake disappears offscreen (below bottom) it becomes inactive */
	/* In case of wind also deactivate flakes blown offscreen sideways, */
	/* so they'll reappear immediately. Make sure snow at bottom isn't  */
	/* disrupted.                          */

	/* Snowflake at the end of it's life? */
	snow->active = (NewY < display_height);
	/* If it is windy we need the flakes that blow offscreen! */
	if (wind) snow->active = 
			(snow->active && (NewX > 0) && (NewX < display_width));
		

	/* If flake covered by windows don't bother drawing it */
	InVisible = XRectInRegion(WindowsR,
								            NewX,NewY,
								            snowPix[snow->whatFlake].width,
								            snowPix[snow->whatFlake].height);
	snow->visible = (InVisible != RectangleIn);


	/*
	 *  Snow touches snow
	 */
	if (snow->visible) {

		/* Do not use XRectInRegion here, as that makes snow add to the bottom */
		/* of layers on windows too */
		/* Use middle bottom of flake */
		TouchDown = XPointInRegion(SnowCtchR,
								               NewX+snowPix[snow->whatFlake].width/2,
								               NewY+snowPix[snow->whatFlake].height);

		/* If the flake already is completely inside the region */
		if (TouchDown && snow->visible && !NoKeepSnow) {
			/* Only add snow that's visible, else snow will grow */
			/* on the *back* of windows */

			NoErase = 1;

			/* Add a bit to the snow bottom region to make snow build up */
			/* Snow isn't built up more than about a certain amount: inside */
			/* SnowAlwdR */

			/* If flake already inside snow layer don't add it again */
			TouchDown = XRectInRegion(SnowCtchR,
								                NewX,NewY,
								                snowPix[snow->whatFlake].width,
								                snowPix[snow->whatFlake].height);
			if (TouchDown == RectanglePart) {
				AddRect.x = NewX;
				AddRect.y = NewY + snowPix[snow->whatFlake].height - 2;
				AddRect.height = 2;
				AddRect.width = snowPix[snow->whatFlake].width;               
				if (XRectInRegion(SnowAlwdR,
								          AddRect.x, AddRect.y,
								          AddRect.width, AddRect.height) == RectangleIn) {
					XUnionRectWithRegion(&AddRect, SnowCtchR,SnowCtchR);
				}
			}
		}


		/* Adjust horizontal speed to mimic whirling */
		if (SmoothWhirl) {
			WhirlAdapt = RandInt(WhirlFactor); 
			if (RandInt(1000) > 500) WhirlAdapt = -WhirlAdapt;

			snow->xStep = snow->xStep + WhirlAdapt;
		}
		else {
			/* Jerkier movement */
			snow->xStep = snow->xStep + RandInt(WhirlFactor);
			if (RandInt(1000) > 500) snow->xStep = -snow->xStep;
		}

	}  /* if snow->visible */



	if (!wind) {
		if (snow->xStep > MaxXStep) snow->xStep = MaxXStep;
		if (snow->xStep < -MaxXStep) snow->xStep = -MaxXStep;
	}

	/* Limit X speed inside snowlayers. 's only natural... */
	if (NoErase && !wind) snow->xStep = snow->xStep/2;
	/* xsnow 1.29 method: */
	/***if (NoErase) {
		snow->xStep = 0;
		NewX = snow->intX;
	}***/


	/* Erase, unless something special has happened */
	if (!snow->insnow) EraseSnowflake(rx);

	/* New coords, unless it's windy */
	snow->intX = NewX;
	snow->intY = NewY;

	/* If snowflake inside a snow layer now don't erase it next time */
	snow->insnow = NoErase;

	/* Draw if still active  and visible*/
	if (snow->active && snow->visible) DrawSnowflake(rx);
 
}


		
/*
	 Draw a snowflake.
*/
void
DrawSnowflake(rx)
int rx;
{
		Snow *snow;

		snow = &snowflakes[rx];

		XSetTSOrigin(display, SnowGC[snow->whatFlake], snow->intX, snow->intY);
		XFillRectangle(display, rootWin, SnowGC[snow->whatFlake],
				 snow->intX, snow->intY,
				 snowPix[snow->whatFlake].width, snowPix[snow->whatFlake].height);
}


/*
	 Erase a snowflake.
*/
void
EraseSnowflake(rx)
int rx;
{
		Snow *snow;

		snow = &snowflakes[rx];
	 
		if (UseFillForClear) {
			XFillRectangle(display, rootWin, ClearSnowGC,
								 snow->intX, snow->intY,
								 snowPix[snow->whatFlake].width, 
								 snowPix[snow->whatFlake].height);
			}
		else {
			XClearArea(display, rootWin, 
								 snow->intX, snow->intY,
								 snowPix[snow->whatFlake].width, 
								 snowPix[snow->whatFlake].height,
								 False);
		}
}


/*
	Paint thick snow at bottom of screen for impaintient people
*/
void
PaintSnowAtBottom(depth)
int depth;
{
int x,y;
Snow *snow;

	for (y=0; y < depth; y++) {
		for (x=0; x<maxSnowflakes; x++) {
			InitSnowflake(x);
			snow = &snowflakes[x];
			snow->intY = display_height - y;
			DrawSnowflake(x);
		}
	}

}


/* ------------------------------------------------------------------ */ 

/*
	 Draw a tree
*/
void
DrawTannenbaum(x,y)
int x,y;
{
		XSetTSOrigin(display, TreesGC, x,y);
		XFillRectangle(display, rootWin, TreesGC,
				 x,y,
				 tannenbaumPix[0].width, tannenbaumPix[0].height);
}




/* ------------------------------------------------------------------ */ 



/*
	Initialize the pixmaps for Santa, depending on SantaSize and Rudolf
*/
void InitSantaPixymaps()
{
	int rc;
	char t[255];

	switch (SantaSize) {
		case 0:
			if (!Rudolf) {
				rc = XpmCreatePixmapFromData(	display, rootWin, RegularSanta1, &SantaPixmap[0], &SantaMaskPixmap[0], NULL);
				rc = XpmCreatePixmapFromData(	display, rootWin, RegularSanta2, &SantaPixmap[1], &SantaMaskPixmap[1], NULL);
				rc = XpmCreatePixmapFromData(	display, rootWin, RegularSanta3, &SantaPixmap[2], &SantaMaskPixmap[2], NULL);
				rc = XpmCreatePixmapFromData(	display, rootWin, RegularSanta4, &SantaPixmap[3], &SantaMaskPixmap[3], NULL);
				sscanf(*RegularSanta1,"%d %d", &SantaWidth,&SantaHeight);
			}
			else {
				rc = XpmCreatePixmapFromData(	display, rootWin, RegularSantaRudolf1, &SantaPixmap[0], &SantaMaskPixmap[0], NULL);
				rc = XpmCreatePixmapFromData(	display, rootWin, RegularSantaRudolf2, &SantaPixmap[1], &SantaMaskPixmap[1], NULL);
				rc = XpmCreatePixmapFromData(	display, rootWin, RegularSantaRudolf3, &SantaPixmap[2], &SantaMaskPixmap[2], NULL);
				rc = XpmCreatePixmapFromData(	display, rootWin, RegularSantaRudolf4, &SantaPixmap[3], &SantaMaskPixmap[3], NULL);
				sscanf(*RegularSantaRudolf1,"%d %d", &SantaWidth,&SantaHeight);
			}
		break;

		case 1:
			if (!Rudolf) {
				rc = XpmCreatePixmapFromData(	display, rootWin, MediumSanta1, &SantaPixmap[0], &SantaMaskPixmap[0], NULL);
				rc = XpmCreatePixmapFromData(	display, rootWin, MediumSanta2, &SantaPixmap[1], &SantaMaskPixmap[1], NULL);
				rc = XpmCreatePixmapFromData(	display, rootWin, MediumSanta3, &SantaPixmap[2], &SantaMaskPixmap[2], NULL);
				rc = XpmCreatePixmapFromData(	display, rootWin, MediumSanta4, &SantaPixmap[3], &SantaMaskPixmap[3], NULL);
				sscanf(*MediumSanta1,"%d %d", &SantaWidth,&SantaHeight);
			}
			else {
				rc = XpmCreatePixmapFromData(	display, rootWin, MediumSantaRudolf1, &SantaPixmap[0], &SantaMaskPixmap[0], NULL);
				rc = XpmCreatePixmapFromData(	display, rootWin, MediumSantaRudolf2, &SantaPixmap[1], &SantaMaskPixmap[1], NULL);
				rc = XpmCreatePixmapFromData(	display, rootWin, MediumSantaRudolf3, &SantaPixmap[2], &SantaMaskPixmap[2], NULL);
				rc = XpmCreatePixmapFromData(	display, rootWin, MediumSantaRudolf4, &SantaPixmap[3], &SantaMaskPixmap[3], NULL);
				sscanf(*MediumSantaRudolf1,"%d %d", &SantaWidth,&SantaHeight);
			}
		break;

		case 2:
			if (!Rudolf) {
				rc = XpmCreatePixmapFromData(	display, rootWin, BigSanta1, &SantaPixmap[0], &SantaMaskPixmap[0], NULL);
				rc = XpmCreatePixmapFromData(	display, rootWin, BigSanta2, &SantaPixmap[1], &SantaMaskPixmap[1], NULL);
				rc = XpmCreatePixmapFromData(	display, rootWin, BigSanta3, &SantaPixmap[2], &SantaMaskPixmap[2], NULL);
				rc = XpmCreatePixmapFromData(	display, rootWin, BigSanta4, &SantaPixmap[3], &SantaMaskPixmap[3], NULL);
				sscanf(*BigSanta1,"%d %d", &SantaWidth,&SantaHeight);
			}
			else {
				rc = XpmCreatePixmapFromData(	display, rootWin, BigSantaRudolf1, &SantaPixmap[0], &SantaMaskPixmap[0], NULL);
				rc = XpmCreatePixmapFromData(	display, rootWin, BigSantaRudolf2, &SantaPixmap[1], &SantaMaskPixmap[1], NULL);
				rc = XpmCreatePixmapFromData(	display, rootWin, BigSantaRudolf3, &SantaPixmap[2], &SantaMaskPixmap[2], NULL);
				rc = XpmCreatePixmapFromData(	display, rootWin, BigSantaRudolf4, &SantaPixmap[3], &SantaMaskPixmap[3], NULL);
				sscanf(*BigSantaRudolf1,"%d %d", &SantaWidth,&SantaHeight);
			}
		break;

		default:
			fprintf(stderr,"** Maximum Santa is %d\n",MAXSANTA);
			exit(1);
		break;

	}	/* switch */


}		/* InitSantaPixymaps */




/*
	 Give birth to a Santa. (What a conception)
*/
void
ResetSanta()      
{
	SantaX = -SantaWidth;
	SantaY = RandInt(display_height / 3)+40;
	SantaXStep = SantaSpeed;
	SantaYStep = 1;
	CurrentSanta = 0;
	SantaVisible = 1;
}


/*
	Update Santa (How can you update the oldest icon in the world? Oh well. )
*/
void
UpdateSanta()
{
int Visible;
int tmp_x;
static int SantaXWindXcel = 0;

	if (SantaVisible) EraseSanta();

	if (wind) {
		/* Acceleration of Santa */
		if (wind == 2) {
			if (direction > 0) SantaXWindXcel += 2;
			SantaXWindXcel++;
			if (SantaXWindXcel > 2*MAXSANTA_SPEED) SantaXWindXcel = 0;
			SantaX = SantaX + SantaXStep + direction*SantaXWindXcel; 
		}

		if( wind == 1 ) { 
			SantaXWindXcel -= 3;
			if (SantaXWindXcel < 0) SantaXWindXcel = 0;
			SantaX = SantaX + SantaXStep + direction*SantaXWindXcel/4;  
		}
	}
	else {
		SantaX = SantaX + SantaXStep;
	}
 
	if (RandInt(10) > 3) SantaY = SantaY + SantaYStep; 
	if (SantaY < 0) SantaY = 0;
	if (RandInt(100) > 80) SantaYStep = -SantaYStep;

	if (SantaX >= display_width) ResetSanta(); 

	Visible = XRectInRegion(WindowsR,
								          SantaX, SantaY,
								          SantaWidth,SantaHeight);
	SantaVisible = (Visible != RectangleIn);
	SantaVisible = (SantaVisible || (SantaX < 0));

	Visible = XRectInRegion(SnowCtchR,
								          SantaX, SantaY,
								          SantaWidth,SantaHeight);

	if (Visible != RectangleOut) {
		SubR = XCreateRegion();

		AddRect.x = SantaX;
		AddRect.y = SantaY;
		AddRect.width = SantaWidth;
		AddRect.height = SantaHeight;
		XUnionRectWithRegion(&AddRect, SubR,SubR);
		XSubtractRegion(SubR,WindowsR, SubR);
 
		XSubtractRegion(SnowCtchR,SubR, SnowCtchR);

		XDestroyRegion(SubR);
	}

	CurrentSanta++;
	if (CurrentSanta >= PIXINANIMATION) CurrentSanta = 0;

	if (SantaVisible) DrawSanta();
}




/*
	Draw Santa
*/
void
DrawSanta() {
	
	int rc;


	rc = XSetClipMask(	display,
											gc,
											SantaMaskPixmap[CurrentSanta]);

	rc = XSetClipOrigin(display,
											gc,
											SantaX,SantaY);

	rc = XCopyArea(			display,
											SantaPixmap[CurrentSanta],
											rootWin,
											gc,
											0,0,SantaWidth,SantaHeight,
											SantaX,SantaY);

	/* Note: the fur in his hat is *imitation* white-seal fur, of course. */
	/* Santa is a big supporter of Greenpeace.                            */

}	/* DrawSanta */



/*
	Erase Santa
*/
void
EraseSanta()
{
	XClearArea(display, rootWin,
						 SantaX , SantaY,     
						 SantaWidth,SantaHeight,
						 False);
}


/* ------------------------------------------------------------------ */ 


#if !GRAB_SERVER

int
XsnowErrors(dpy, err)
Display *dpy;
XErrorEvent *err;
{
		errorVal = err->error_code;

		return 0;
}

#endif /* GRAB_SERVER */


/*
	 Allocate a color by name.
*/
Pixel
AllocNamedColor(colorName, dfltPix)
char *colorName;
Pixel dfltPix;
{
	Pixel pix;
	XColor scrncolor;
	XColor exactcolor;

	if (XAllocNamedColor(display, DefaultColormap(display, screen),
		colorName, &scrncolor, &exactcolor)) {
		pix = scrncolor.pixel;
	}
	else {
		pix = dfltPix;
	}

	return pix;
}



void sig_alarm()
{
		int rand=RandInt(100);
		void (*sig_alarm_ptr)() = sig_alarm;

		/* No wind at all? */
		if (NoWind) return;

		if( rand > 80 ){
				geta -= 1;
		}else if( rand > 55 ){
				geta += 1;
		}

		if( rand > 65 ){
				if( RandInt(10) > 4 ){
						direction = 1;
				}else{
						direction = -1;
				}

				wind = 2;
				signal(SIGALRM, sig_alarm_ptr);

				alarm(abs(RandInt(5))+1);
		}else{
				if( wind == 2 ){
						wind = 1;
						signal(SIGALRM, sig_alarm_ptr);
						alarm(RandInt(3)+1);
				}else{
						wind = 0;
						signal(SIGALRM, sig_alarm_ptr);
						alarm(WindTimer);
				}
		}
}




int MakeWR(int Force) {

		Window *children;
		unsigned int nChildren;
		Window dummy;
		XWindowAttributes wa;
		int wx;
		XRectangle CtchRect;
		XRectangle AllowRect;
		XRectangle WinRect;
		int winX, winY;
		int NouMoe;
		unsigned int winHeight, winWidth;
		unsigned int depth;

#if GRAB_SERVER
		XGrabServer(display);
#else
		XSetErrorHandler(XsnowErrors);
#endif

		XDestroyRegion(WindowsR);
		WindowsR = XCreateRegion();

		XQueryTree(display, rootWin, &dummy, &dummy, &children, &nChildren);

		for (wx=0; wx<nChildren; wx++) {
				if (XEventsQueued(display, QueuedAlready) && (! Force)) {
					XFree((char *)children); 
					return 0;
				}
				errorVal = 0;
				XGetWindowAttributes(display, children[wx], &wa);
				if (errorVal) {
					printf("XGetWindowAttributes err\n");
					continue;
				}

				if (!NoPopuphandling && wa.save_under) continue;

				if (wa.map_state == IsViewable) {
						XGetGeometry(display, children[wx], &dummy, &winX, &winY,
								&winWidth, &winHeight, &borderWidth, &depth);
						if (errorVal) continue;
 
						WinRect.x = winX;
						WinRect.y = winY;
						WinRect.height = winHeight + 2*borderWidth;
						WinRect.width = winWidth + 2*borderWidth;

						XUnionRectWithRegion(&WinRect,WindowsR, WindowsR);
				}
		}

		XFree((char *)children);

#if GRAB_SERVER
		XUngrabServer(display);
#else
		XSetErrorHandler((ErrorHandler *)NULL);
#endif

		return 1;
}		/* MakeWR */




void ReclcCSR() {

	XDestroyRegion(RecalcSnowCtchR);
	RecalcSnowCtchR = XCreateRegion();


	XUnionRegion(WindowsR,RecalcSnowCtchR, RecalcSnowCtchR);
	XOffsetRegion(RecalcSnowCtchR, 0,MaxYStep+MaxSnowFlakeHeight);
	XSubtractRegion(WindowsR,RecalcSnowCtchR, RecalcSnowCtchR);

	if (NoKeepSWin) {
		XDestroyRegion(SnowCtchR);
		SnowCtchR = XCreateRegion();
		return;
	}

	XUnionRegion(RecalcSnowCtchR,SnowCtchR, SnowCtchR);

	if (SnowOffset != 0) XOffsetRegion(SnowCtchR,0,SnowOffset);

}		/* ReclcCSR */




void MkASR() {

	XRectangle ARect;


	XDestroyRegion(SnowAlwdR);
	SnowAlwdR = XCreateRegion();
	
	if (!NoKeepSWin) {
		XUnionRegion(WindowsR,SnowAlwdR, SnowAlwdR);
		XOffsetRegion(SnowAlwdR, 0,-MaxWinSnowDepth);
	
		XSubtractRegion(SnowAlwdR,WindowsR, SnowAlwdR);
	}

	if (!NoKeepSWin) {
		ARect.x = 0;
		ARect.y = display_height - MaxScrSnowDepth;
		ARect.width = display_width - 1;
		ARect.height = MaxYStep+MaxSnowFlakeHeight+MaxScrSnowDepth;
		XUnionRectWithRegion(&ARect, SnowAlwdR, SnowAlwdR);
	}
	XUnionRegion(SnowCtchR,SnowAlwdR, SnowAlwdR);

}		/* MkASR */





int DoDffWLT() {

	XRectangle DRect;
	
	if (!MakeWR(0)) return 0;

	XClipBox( WindowsR, &DRect );

	if (! XEqualRegion(WindowsR,PWindowsR) ) {

		XXorRegion(WindowsR,PWindowsR, WindowsDifferenceR);
		XClipBox( WindowsDifferenceR, &DRect );
	  XClearArea(display, rootWin,
	             DRect.x,
	             DRect.y,
	             DRect.width,              
	             DRect.height,
	             False);

		HandleEvA(DRect.x,DRect.y,DRect.width,DRect.height);

		XDestroyRegion(PWindowsR);
		PWindowsR = XCreateRegion();
		XUnionRegion(WindowsR,PWindowsR, PWindowsR);
	}
	else {
		;;
	}

	ReclcCSR();
	MkASR();

	return 1;

}		/* DoDffWLT */




void HandleEvA(int X,int Y,int W, int H) {

int AddX,AddY;
int ClearX, ClearY;

/*return;		/* DEBUG */

	AddX = X - MaxSnowFlakeHeight;
	AddY = Y - MaxWinSnowDepth;
	AddRect.height = H +  MaxWinSnowDepth;
	AddRect.width = W + 2*MaxSnowFlakeHeight;
	if (AddY < 0) {
	  AddRect.height = AddRect.height + AddY;
	  AddY = 0;
	}
	if ((AddY+AddRect.height) > display_height) {
	  AddRect.height = display_height - AddY;
	}

	AddRect.x = AddX;
	AddRect.y = AddY;

	SubR= XCreateRegion();
	  XUnionRectWithRegion(&AddRect, SubR,SubR);
	
	  AddY = AddY - MaxYStep-MaxSnowFlakeHeight;
	  AddRect.height = 
	    MaxWinSnowDepth+MaxYStep*2+MaxSnowFlakeHeight*2;
	  if (AddY < 0) {
	    AddRect.height = AddRect.height + AddY;
	    AddY = 0;
	  }
	  AddRect.y = AddY;
	  if ((AddY+AddRect.height) > display_height) {
	    AddRect.height = display_height - AddY;
	  }
	  XUnionRectWithRegion(&AddRect, SubR,SubR);
	  XClearArea(display, rootWin,
	             AddX,
	             AddY,
	             AddRect.width,              
	             AddRect.height,
	             False);

	  XSubtractRegion(SnowCtchR,SubR, SnowCtchR);
	XDestroyRegion(SubR);

}	/* HandleEvA */