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
|
/*
* oneko - X11 猫
*/
#ifndef lint
static char rcsid[] = "$Header: /home/sun/unix/kato/xsam/oneko/oneko.c,v 1.5 90/10/19 21:25:16 kato Exp $";
#endif
#include "oneko.h"
#include "patchlevel.h"
/*
* グローバル変数
*/
char *ClassName = "Oneko"; /* コマンド名称 */
char *ProgramName; /* コマンド名称 */
Display *theDisplay; /* ディスプレイ構造体 */
int theScreen; /* スクリーン番号 */
unsigned int theDepth; /* デプス */
Window theRoot; /* ルートウィンドウのID */
Window theWindow; /* 猫ウィンドウのID */
char *WindowName = NULL; /* 猫ウィンドウの名前 */
Window theTarget = None; /* 目標ウィンドウのID */
char *TargetName = NULL; /* 目標ウィンドウの名前 */
Cursor theCursor; /* ねずみカーソル */
unsigned int WindowWidth; /* ルートウィンドウの幅 */
unsigned int WindowHeight; /* ルートウィンドウの高さ */
XColor theForegroundColor; /* 色 (フォアグラウンド) */
XColor theBackgroundColor; /* 色 (バックグラウンド) */
int Synchronous = False;
/* Types of animals */
#define BITMAPTYPES 6
typedef struct _AnimalDefaults {
char *name;
int speed, idle, bitmap_width, bitmap_height;
long time;
int off_x, off_y;
char *cursor,*mask;
int cursor_width,cursor_height,cursor_x_hot,cursor_y_hot;
} AnimalDefaultsData;
AnimalDefaultsData AnimalDefaultsDataTable[] =
{
{ "neko", 13, 6, 32, 32, 125000L, 0, 0, mouse_cursor_bits,mouse_cursor_mask_bits,
mouse_cursor_width,mouse_cursor_height, mouse_cursor_x_hot,mouse_cursor_y_hot },
{ "tora", 16, 6, 32, 32, 125000L, 0, 0, mouse_cursor_bits,mouse_cursor_mask_bits,
mouse_cursor_width,mouse_cursor_height, mouse_cursor_x_hot,mouse_cursor_y_hot },
{ "dog" , 10, 6, 32, 32, 125000L, 0, 0, bone_cursor_bits,bone_cursor_mask_bits,
bone_cursor_width,bone_cursor_height, bone_cursor_x_hot,bone_cursor_y_hot },
{ "bsd_daemon" , 16, 6, 32, 32, 300000L, 22, 20, bsd_cursor_bits,bsd_cursor_mask_bits,
bsd_cursor_width,bsd_cursor_height, bsd_cursor_x_hot,bsd_cursor_y_hot },
{ "sakura" , 13, 6, 32, 32, 125000L, 0, 0, card_cursor_bits,card_cursor_mask_bits,
card_cursor_width,card_cursor_height, card_cursor_x_hot,card_cursor_y_hot },
{ "tomoyo" , 10, 6, 32, 32, 125000L, 32, 32, petal_cursor_bits,petal_cursor_mask_bits,
petal_cursor_width,petal_cursor_height, petal_cursor_x_hot,petal_cursor_y_hot },
};
/*
* いろいろな初期設定 (オプション、リソースで変えられるよ)
*/
/* Resource: */
char *Foreground = NULL; /* foreground */
char *Background = NULL; /* background */
long IntervalTime = 0L; /* time */
double NekoSpeed = (double)0; /* speed */
int IdleSpace = 0; /* idle */
int NekoMoyou = NOTDEFINED; /* tora */
int NoShape = NOTDEFINED; /* noshape */
int ReverseVideo = NOTDEFINED; /* reverse */
int ToWindow = NOTDEFINED; /* towindow */
int ToFocus = NOTDEFINED; /* tofocus */
int XOffset=0,YOffset=0; /* X and Y offsets for cat from mouse
pointer. */
/*
* いろいろな状態変数
*/
Bool DontMapped = True;
int NekoTickCount; /* 猫動作カウンタ */
int NekoStateCount; /* 猫同一状態カウンタ */
int NekoState; /* 猫の状態 */
int MouseX; /* マウスX座標 */
int MouseY; /* マウスY座標 */
int PrevMouseX = 0; /* 直前のマウスX座標 */
int PrevMouseY = 0; /* 直前のマウスY座標 */
Window PrevTarget = None; /* 直前の目標ウィンドウのID */
int NekoX; /* 猫X座標 */
int NekoY; /* 猫Y座標 */
int NekoMoveDx; /* 猫移動距離X */
int NekoMoveDy; /* 猫移動距離Y */
int NekoLastX; /* 猫最終描画X座標 */
int NekoLastY; /* 猫最終描画Y座標 */
GC NekoLastGC; /* 猫最終描画 GC */
/* Variables used to set how quickly the program will chose to raise itself. */
/* Look at Interval(), Handle Visiblility Notify Event */
#define DEFAULT_RAISE_WAIT 16 /* About 2 seconds with default interval */
int RaiseWindowDelay=0;
/*
* その他
*/
double SinPiPer8Times3; /* sin(3π/8) */
double SinPiPer8; /* sin(π/8) */
Pixmap Mati2Xbm, Jare2Xbm, Kaki1Xbm, Kaki2Xbm, Mati3Xbm, Sleep1Xbm, Sleep2Xbm;
Pixmap Mati2Msk, Jare2Msk, Kaki1Msk, Kaki2Msk, Mati3Msk, Sleep1Msk, Sleep2Msk;
Pixmap AwakeXbm, AwakeMsk;
Pixmap Up1Xbm, Up2Xbm, Down1Xbm, Down2Xbm, Left1Xbm, Left2Xbm;
Pixmap Up1Msk, Up2Msk, Down1Msk, Down2Msk, Left1Msk, Left2Msk;
Pixmap Right1Xbm, Right2Xbm, UpLeft1Xbm, UpLeft2Xbm, UpRight1Xbm;
Pixmap Right1Msk, Right2Msk, UpLeft1Msk, UpLeft2Msk, UpRight1Msk;
Pixmap UpRight2Xbm, DownLeft1Xbm, DownLeft2Xbm, DownRight1Xbm, DownRight2Xbm;
Pixmap UpRight2Msk, DownLeft1Msk, DownLeft2Msk, DownRight1Msk, DownRight2Msk;
Pixmap UpTogi1Xbm, UpTogi2Xbm, DownTogi1Xbm, DownTogi2Xbm, LeftTogi1Xbm;
Pixmap UpTogi1Msk, UpTogi2Msk, DownTogi1Msk, DownTogi2Msk, LeftTogi1Msk;
Pixmap LeftTogi2Xbm, RightTogi1Xbm, RightTogi2Xbm;
Pixmap LeftTogi2Msk, RightTogi1Msk, RightTogi2Msk;
GC Mati2GC;
GC Jare2GC, Kaki1GC, Kaki2GC, Mati3GC, Sleep1GC, Sleep2GC;
GC AwakeGC;
GC Up1GC, Up2GC, Down1GC, Down2GC, Left1GC, Left2GC, Right1GC, Right2GC;
GC UpLeft1GC, UpLeft2GC, UpRight1GC, UpRight2GC, DownLeft1GC, DownLeft2GC;
GC DownRight1GC, DownRight2GC;
GC UpTogi1GC, UpTogi2GC, DownTogi1GC, DownTogi2GC, LeftTogi1GC;
GC LeftTogi2GC, RightTogi1GC, RightTogi2GC;
typedef struct {
GC *GCCreatePtr;
Pixmap *BitmapCreatePtr;
char *PixelPattern[BITMAPTYPES];
Pixmap *BitmapMasksPtr;
char *MaskPattern[BITMAPTYPES];
} BitmapGCData;
BitmapGCData BitmapGCDataTable[] =
{
{ &Mati2GC, &Mati2Xbm, mati2_bits, mati2_tora_bits, mati2_dog_bits, mati2_bsd_bits, mati2_sakura_bits, mati2_tomoyo_bits,
&Mati2Msk, mati2_mask_bits, mati2_mask_bits, mati2_dog_mask_bits, mati2_bsd_mask_bits, mati2_sakura_mask_bits, mati2_tomoyo_mask_bits },
{ &Jare2GC, &Jare2Xbm, jare2_bits, jare2_tora_bits, jare2_dog_bits, jare2_bsd_bits, jare2_sakura_bits, jare2_tomoyo_bits,
&Jare2Msk, jare2_mask_bits, jare2_mask_bits, jare2_dog_mask_bits, jare2_bsd_mask_bits, jare2_sakura_mask_bits, jare2_tomoyo_mask_bits },
{ &Kaki1GC, &Kaki1Xbm, kaki1_bits, kaki1_tora_bits, kaki1_dog_bits, kaki1_bsd_bits, kaki1_sakura_bits, kaki1_tomoyo_bits,
&Kaki1Msk, kaki1_mask_bits, kaki1_mask_bits, kaki1_dog_mask_bits, kaki1_bsd_mask_bits, kaki1_sakura_mask_bits, kaki1_tomoyo_mask_bits },
{ &Kaki2GC, &Kaki2Xbm, kaki2_bits, kaki2_tora_bits, kaki2_dog_bits, kaki2_bsd_bits, kaki2_sakura_bits, kaki2_tomoyo_bits,
&Kaki2Msk, kaki2_mask_bits, kaki2_mask_bits, kaki2_dog_mask_bits, kaki2_bsd_mask_bits, kaki2_sakura_mask_bits, kaki2_tomoyo_mask_bits },
{ &Mati3GC, &Mati3Xbm, mati3_bits, mati3_tora_bits, mati3_dog_bits, mati3_bsd_bits, mati3_sakura_bits, mati3_tomoyo_bits,
&Mati3Msk, mati3_mask_bits, mati3_mask_bits, mati3_dog_mask_bits, mati3_bsd_mask_bits, mati3_sakura_mask_bits, mati3_tomoyo_mask_bits },
{ &Sleep1GC, &Sleep1Xbm, sleep1_bits, sleep1_tora_bits, sleep1_dog_bits, sleep1_bsd_bits, sleep1_sakura_bits, sleep1_tomoyo_bits,
&Sleep1Msk, sleep1_mask_bits, sleep1_mask_bits, sleep1_dog_mask_bits, sleep1_bsd_mask_bits, sleep1_sakura_mask_bits, sleep1_tomoyo_mask_bits },
{ &Sleep2GC, &Sleep2Xbm, sleep2_bits, sleep2_tora_bits, sleep2_dog_bits, sleep2_bsd_bits, sleep2_sakura_bits, sleep2_tomoyo_bits,
&Sleep2Msk, sleep2_mask_bits, sleep2_mask_bits, sleep2_dog_mask_bits, sleep2_bsd_mask_bits, sleep2_sakura_mask_bits, sleep2_tomoyo_mask_bits },
{ &AwakeGC, &AwakeXbm, awake_bits, awake_tora_bits, awake_dog_bits, awake_bsd_bits, awake_sakura_bits, awake_tomoyo_bits,
&AwakeMsk, awake_mask_bits, awake_mask_bits, awake_dog_mask_bits, awake_bsd_mask_bits, awake_sakura_mask_bits, awake_tomoyo_mask_bits },
{ &Up1GC, &Up1Xbm, up1_bits, up1_tora_bits, up1_dog_bits, up1_bsd_bits, up1_sakura_bits, up1_tomoyo_bits,
&Up1Msk, up1_mask_bits, up1_mask_bits, up1_dog_mask_bits, up1_bsd_mask_bits, up1_sakura_mask_bits, up1_tomoyo_mask_bits },
{ &Up2GC, &Up2Xbm, up2_bits, up2_tora_bits, up2_dog_bits, up2_bsd_bits, up2_sakura_bits, up2_tomoyo_bits,
&Up2Msk, up2_mask_bits, up2_mask_bits, up2_dog_mask_bits, up2_bsd_mask_bits, up2_sakura_mask_bits, up2_tomoyo_mask_bits },
{ &Down1GC, &Down1Xbm, down1_bits, down1_tora_bits, down1_dog_bits, down1_bsd_bits, down1_sakura_bits, down1_tomoyo_bits,
&Down1Msk, down1_mask_bits, down1_mask_bits, down1_dog_mask_bits, down1_bsd_mask_bits, down1_sakura_mask_bits, down1_tomoyo_mask_bits },
{ &Down2GC, &Down2Xbm, down2_bits, down2_tora_bits, down2_dog_bits, down2_bsd_bits, down2_sakura_bits, down2_tomoyo_bits,
&Down2Msk, down2_mask_bits, down2_mask_bits, down2_dog_mask_bits, down2_bsd_mask_bits, down2_sakura_mask_bits, down2_tomoyo_mask_bits },
{ &Left1GC, &Left1Xbm, left1_bits, left1_tora_bits, left1_dog_bits, left1_bsd_bits, left1_sakura_bits, left1_tomoyo_bits,
&Left1Msk, left1_mask_bits, left1_mask_bits, left1_dog_mask_bits, left1_bsd_mask_bits, left1_sakura_mask_bits, left1_tomoyo_mask_bits },
{ &Left2GC, &Left2Xbm, left2_bits, left2_tora_bits, left2_dog_bits, left2_bsd_bits, left2_sakura_bits, left2_tomoyo_bits,
&Left2Msk, left2_mask_bits, left2_mask_bits, left2_dog_mask_bits, left2_bsd_mask_bits, left2_sakura_mask_bits, left2_tomoyo_mask_bits },
{ &Right1GC, &Right1Xbm, right1_bits, right1_tora_bits, right1_dog_bits, right1_bsd_bits, right1_sakura_bits, right1_tomoyo_bits,
&Right1Msk, right1_mask_bits, right1_mask_bits,right1_dog_mask_bits, right1_bsd_mask_bits, right1_sakura_mask_bits, right1_tomoyo_mask_bits },
{ &Right2GC, &Right2Xbm, right2_bits, right2_tora_bits, right2_dog_bits, right2_bsd_bits, right2_sakura_bits, right2_tomoyo_bits,
&Right2Msk, right2_mask_bits, right2_mask_bits, right2_dog_mask_bits, right2_bsd_mask_bits, right2_sakura_mask_bits, right2_tomoyo_mask_bits },
{ &UpLeft1GC, &UpLeft1Xbm, upleft1_bits, upleft1_tora_bits, upleft1_dog_bits, upleft1_bsd_bits, upleft1_sakura_bits, upleft1_tomoyo_bits,
&UpLeft1Msk, upleft1_mask_bits, upleft1_mask_bits, upleft1_dog_mask_bits, upleft1_bsd_mask_bits, upleft1_sakura_mask_bits, upleft1_tomoyo_mask_bits },
{ &UpLeft2GC, &UpLeft2Xbm, upleft2_bits, upleft2_tora_bits, upleft2_dog_bits, upleft2_bsd_bits, upleft2_sakura_bits, upleft2_tomoyo_bits,
&UpLeft2Msk, upleft2_mask_bits, upleft2_mask_bits,upleft2_dog_mask_bits, upleft2_bsd_mask_bits, upleft2_sakura_mask_bits, upleft2_tomoyo_mask_bits },
{ &UpRight1GC, &UpRight1Xbm, upright1_bits, upright1_tora_bits, upright1_dog_bits, upright1_bsd_bits, upright1_sakura_bits, upright1_tomoyo_bits,
&UpRight1Msk, upright1_mask_bits, upright1_mask_bits,upright1_dog_mask_bits, upright1_bsd_mask_bits, upright1_sakura_mask_bits, upright1_tomoyo_mask_bits },
{ &UpRight2GC, &UpRight2Xbm, upright2_bits, upright2_tora_bits, upright2_dog_bits, upright2_bsd_bits, upright2_sakura_bits, upright2_tomoyo_bits,
&UpRight2Msk, upright2_mask_bits, upright2_mask_bits,upright2_dog_mask_bits, upright2_bsd_mask_bits, upright2_sakura_mask_bits, upright2_tomoyo_mask_bits },
{ &DownLeft1GC, &DownLeft1Xbm, dwleft1_bits, dwleft1_tora_bits, dwleft1_dog_bits, dwleft1_bsd_bits, dwleft1_sakura_bits, dwleft1_tomoyo_bits,
&DownLeft1Msk, dwleft1_mask_bits, dwleft1_mask_bits, dwleft1_dog_mask_bits, dwleft1_bsd_mask_bits, dwleft1_sakura_mask_bits, dwleft1_tomoyo_mask_bits },
{ &DownLeft2GC, &DownLeft2Xbm, dwleft2_bits, dwleft2_tora_bits, dwleft2_dog_bits, dwleft2_bsd_bits, dwleft2_sakura_bits, dwleft2_tomoyo_bits,
&DownLeft2Msk, dwleft2_mask_bits, dwleft2_mask_bits, dwleft2_dog_mask_bits, dwleft2_bsd_mask_bits, dwleft2_sakura_mask_bits, dwleft2_tomoyo_mask_bits },
{ &DownRight1GC, &DownRight1Xbm, dwright1_bits, dwright1_tora_bits, dwright1_dog_bits, dwright1_bsd_bits, dwright1_sakura_bits, dwright1_tomoyo_bits,
&DownRight1Msk, dwright1_mask_bits, dwright1_mask_bits, dwright1_dog_mask_bits, dwright1_bsd_mask_bits, dwright1_sakura_mask_bits, dwright1_tomoyo_mask_bits },
{ &DownRight2GC, &DownRight2Xbm, dwright2_bits, dwright2_tora_bits, dwright2_dog_bits, dwright2_bsd_bits, dwright2_sakura_bits, dwright2_tomoyo_bits,
&DownRight2Msk, dwright2_mask_bits, dwright2_mask_bits, dwright2_dog_mask_bits, dwright2_bsd_mask_bits, dwright2_sakura_mask_bits, dwright2_tomoyo_mask_bits },
{ &UpTogi1GC, &UpTogi1Xbm, utogi1_bits, utogi1_tora_bits, utogi1_dog_bits, utogi1_bsd_bits, utogi1_sakura_bits, utogi1_tomoyo_bits,
&UpTogi1Msk, utogi1_mask_bits, utogi1_mask_bits, utogi1_dog_mask_bits, utogi1_bsd_mask_bits, utogi1_sakura_mask_bits, utogi1_tomoyo_mask_bits },
{ &UpTogi2GC, &UpTogi2Xbm, utogi2_bits, utogi2_tora_bits, utogi2_dog_bits, utogi2_bsd_bits, utogi2_sakura_bits, utogi2_tomoyo_bits,
&UpTogi2Msk, utogi2_mask_bits, utogi2_mask_bits, utogi2_dog_mask_bits, utogi2_bsd_mask_bits, utogi2_sakura_mask_bits, utogi2_tomoyo_mask_bits },
{ &DownTogi1GC, &DownTogi1Xbm, dtogi1_bits, dtogi1_tora_bits, dtogi1_dog_bits, dtogi1_bsd_bits, dtogi1_sakura_bits, dtogi1_tomoyo_bits,
&DownTogi1Msk, dtogi1_mask_bits, dtogi1_mask_bits, dtogi1_dog_mask_bits, dtogi1_bsd_mask_bits, dtogi1_sakura_mask_bits, dtogi1_tomoyo_mask_bits },
{ &DownTogi2GC, &DownTogi2Xbm, dtogi2_bits, dtogi2_tora_bits, dtogi2_dog_bits, dtogi2_bsd_bits, dtogi2_sakura_bits, dtogi2_tomoyo_bits,
&DownTogi2Msk, dtogi2_mask_bits, dtogi2_mask_bits, dtogi2_dog_mask_bits, dtogi2_bsd_mask_bits, dtogi2_sakura_mask_bits, dtogi2_tomoyo_mask_bits },
{ &LeftTogi1GC, &LeftTogi1Xbm, ltogi1_bits, ltogi1_tora_bits, ltogi1_dog_bits, ltogi1_bsd_bits, ltogi1_sakura_bits, ltogi1_tomoyo_bits,
&LeftTogi1Msk, ltogi1_mask_bits, ltogi1_mask_bits,ltogi1_dog_mask_bits, ltogi1_bsd_mask_bits, ltogi1_sakura_mask_bits, ltogi1_tomoyo_mask_bits },
{ &LeftTogi2GC, &LeftTogi2Xbm, ltogi2_bits, ltogi2_tora_bits, ltogi2_dog_bits, ltogi2_bsd_bits, ltogi2_sakura_bits, ltogi2_tomoyo_bits,
&LeftTogi2Msk, ltogi2_mask_bits, ltogi2_mask_bits,ltogi2_dog_mask_bits, ltogi2_bsd_mask_bits, ltogi2_sakura_mask_bits, ltogi2_tomoyo_mask_bits },
{ &RightTogi1GC, &RightTogi1Xbm, rtogi1_bits, rtogi1_tora_bits, rtogi1_dog_bits, rtogi1_bsd_bits, rtogi1_sakura_bits, rtogi1_tomoyo_bits,
&RightTogi1Msk, rtogi1_mask_bits, rtogi1_mask_bits,rtogi1_dog_mask_bits, rtogi1_bsd_mask_bits, rtogi1_sakura_mask_bits, rtogi1_tomoyo_mask_bits },
{ &RightTogi2GC, &RightTogi2Xbm, rtogi2_bits, rtogi2_tora_bits, rtogi2_dog_bits, rtogi2_bsd_bits, rtogi2_sakura_bits, rtogi2_tomoyo_bits,
&RightTogi2Msk, rtogi2_mask_bits, rtogi2_mask_bits,rtogi2_dog_mask_bits, rtogi2_bsd_mask_bits, rtogi2_sakura_mask_bits, rtogi2_tomoyo_mask_bits },
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
};
typedef struct {
GC *TickGCPtr;
Pixmap *TickMaskPtr;
} Animation;
Animation AnimationPattern[][2] =
{
{ { &Mati2GC, &Mati2Msk },
{ &Mati2GC, &Mati2Msk } }, /* NekoState == NEKO_STOP */
{ { &Jare2GC, &Jare2Msk },
{ &Mati2GC, &Mati2Msk } }, /* NekoState == NEKO_JARE */
{ { &Kaki1GC, &Kaki1Msk },
{ &Kaki2GC, &Kaki2Msk } }, /* NekoState == NEKO_KAKI */
{ { &Mati3GC, &Mati3Msk },
{ &Mati3GC, &Mati3Msk } }, /* NekoState == NEKO_AKUBI */
{ { &Sleep1GC, &Sleep1Msk },
{ &Sleep2GC, &Sleep2Msk } }, /* NekoState == NEKO_SLEEP */
{ { &AwakeGC, &AwakeMsk },
{ &AwakeGC, &AwakeMsk } }, /* NekoState == NEKO_AWAKE */
{ { &Up1GC, &Up1Msk },
{ &Up2GC, &Up2Msk } }, /* NekoState == NEKO_U_MOVE */
{ { &Down1GC, &Down1Msk },
{ &Down2GC, &Down2Msk } }, /* NekoState == NEKO_D_MOVE */
{ { &Left1GC, &Left1Msk },
{ &Left2GC, &Left2Msk } }, /* NekoState == NEKO_L_MOVE */
{ { &Right1GC, &Right1Msk },
{ &Right2GC, &Right2Msk } }, /* NekoState == NEKO_R_MOVE */
{ { &UpLeft1GC, &UpLeft1Msk },
{ &UpLeft2GC, &UpLeft2Msk } }, /* NekoState == NEKO_UL_MOVE */
{ { &UpRight1GC, &UpRight1Msk },
{ &UpRight2GC, &UpRight2Msk } }, /* NekoState == NEKO_UR_MOVE */
{ { &DownLeft1GC, &DownLeft1Msk },
{ &DownLeft2GC, &DownLeft2Msk } }, /* NekoState == NEKO_DL_MOVE */
{ { &DownRight1GC, &DownRight1Msk },
{ &DownRight2GC, &DownRight2Msk } }, /* NekoState == NEKO_DR_MOVE */
{ { &UpTogi1GC, &UpTogi1Msk },
{ &UpTogi2GC, &UpTogi2Msk } }, /* NekoState == NEKO_U_TOGI */
{ { &DownTogi1GC, &DownTogi1Msk },
{ &DownTogi2GC, &DownTogi2Msk } }, /* NekoState == NEKO_D_TOGI */
{ { &LeftTogi1GC, &LeftTogi1Msk },
{ &LeftTogi2GC, &LeftTogi2Msk } }, /* NekoState == NEKO_L_TOGI */
{ { &RightTogi1GC, &RightTogi1Msk },
{ &RightTogi2GC, &RightTogi2Msk } }, /* NekoState == NEKO_R_TOGI */
};
static void NullFunction();
/*
* ビットマップデータ・GC 初期化
*/
void
InitBitmapAndGCs()
{
BitmapGCData *BitmapGCDataTablePtr;
XGCValues theGCValues;
theGCValues.function = GXcopy;
theGCValues.foreground = theForegroundColor.pixel;
theGCValues.background = theBackgroundColor.pixel;
theGCValues.fill_style = FillTiled;
theGCValues.ts_x_origin = 0;
theGCValues.ts_y_origin = 0;
for (BitmapGCDataTablePtr = BitmapGCDataTable;
BitmapGCDataTablePtr->GCCreatePtr != NULL;
BitmapGCDataTablePtr++) {
*(BitmapGCDataTablePtr->BitmapCreatePtr)
= XCreatePixmapFromBitmapData(theDisplay, theRoot,
BitmapGCDataTablePtr->PixelPattern[NekoMoyou],
BITMAP_WIDTH, BITMAP_HEIGHT,
theForegroundColor.pixel,
theBackgroundColor.pixel,
DefaultDepth(theDisplay, theScreen));
theGCValues.tile = *(BitmapGCDataTablePtr->BitmapCreatePtr);
*(BitmapGCDataTablePtr->BitmapMasksPtr)
= XCreateBitmapFromData(theDisplay, theRoot,
BitmapGCDataTablePtr->MaskPattern[NekoMoyou],
BITMAP_WIDTH, BITMAP_HEIGHT);
*(BitmapGCDataTablePtr->GCCreatePtr)
= XCreateGC(theDisplay, theWindow,
GCFunction | GCForeground | GCBackground | GCTile |
GCTileStipXOrigin | GCTileStipYOrigin | GCFillStyle,
&theGCValues);
}
}
/*
* リソース・データベースから必要なリソースを取り出す
*/
char *
NekoGetDefault(resource)
char *resource;
{
char *value;
if (value = XGetDefault(theDisplay, ProgramName, resource)) {
return value;
}
if (value = XGetDefault(theDisplay, ClassName, resource)) {
return value;
}
return NULL;
}
/*
* リソース・データベースからオプションを設定
*/
GetResources()
{
char *resource;
int num;
int loop;
if (Foreground == NULL) {
if ((resource = NekoGetDefault("foreground")) != NULL) {
Foreground = resource;
}
}
if (Background == NULL) {
if ((resource = NekoGetDefault("background")) != NULL) {
Background = resource;
}
}
if (IntervalTime == 0) {
if ((resource = NekoGetDefault("time")) != NULL) {
if (num = atoi(resource)) {
IntervalTime = num;
}
}
}
if (NekoSpeed == (double)0) {
if ((resource = NekoGetDefault("speed")) != NULL) {
if (num = atoi(resource)) {
NekoSpeed = (double)num;
}
}
}
if (IdleSpace == 0) {
if ((resource = NekoGetDefault("idle")) != NULL) {
if (num = atoi(resource)) {
IdleSpace = num;
}
}
}
if (NekoMoyou == NOTDEFINED) {
for (loop=0;loop<BITMAPTYPES;loop++)
if ((resource = NekoGetDefault(AnimalDefaultsDataTable[loop].name)) != NULL) {
if (IsTrue(resource))
NekoMoyou = loop;
}
}
if (NoShape == NOTDEFINED) {
if ((resource = NekoGetDefault("noshape")) != NULL) {
NoShape = IsTrue(resource);
}
}
if (ReverseVideo == NOTDEFINED) {
if ((resource = NekoGetDefault("reverse")) != NULL) {
ReverseVideo = IsTrue(resource);
}
}
if (Foreground == NULL) {
Foreground = DEFAULT_FOREGROUND;
}
if (Background == NULL) {
Background = DEFAULT_BACKGROUND;
}
if (NekoMoyou == NOTDEFINED) {
NekoMoyou = 0;
}
if (IntervalTime == 0) {
IntervalTime = AnimalDefaultsDataTable[NekoMoyou].time;
}
if (NekoSpeed == (double)0) {
NekoSpeed = (double)(AnimalDefaultsDataTable[NekoMoyou].speed);
}
if (IdleSpace == 0) {
IdleSpace = AnimalDefaultsDataTable[NekoMoyou].idle;
}
XOffset = XOffset + AnimalDefaultsDataTable[NekoMoyou].off_x;
YOffset = YOffset + AnimalDefaultsDataTable[NekoMoyou].off_y;
if (NoShape == NOTDEFINED) {
NoShape = False;
}
if (ReverseVideo == NOTDEFINED) {
ReverseVideo = False;
}
if (ToWindow == NOTDEFINED) {
ToWindow = False;
}
if (ToFocus == NOTDEFINED) {
ToFocus = False;
}
}
/*
* ねずみ型カーソルを作る
*/
MakeMouseCursor()
{
Pixmap theCursorSource;
Pixmap theCursorMask;
theCursorSource
= XCreateBitmapFromData(theDisplay, theRoot,
AnimalDefaultsDataTable[NekoMoyou].cursor,
AnimalDefaultsDataTable[NekoMoyou].cursor_width,
AnimalDefaultsDataTable[NekoMoyou].cursor_height);
theCursorMask
= XCreateBitmapFromData(theDisplay, theRoot,
AnimalDefaultsDataTable[NekoMoyou].mask,
AnimalDefaultsDataTable[NekoMoyou].cursor_width,
AnimalDefaultsDataTable[NekoMoyou].cursor_height);
theCursor = XCreatePixmapCursor(theDisplay, theCursorSource, theCursorMask,
&theBackgroundColor, &theForegroundColor,
AnimalDefaultsDataTable[NekoMoyou].cursor_x_hot,
AnimalDefaultsDataTable[NekoMoyou].cursor_y_hot);
XFreePixmap(theDisplay,theCursorSource);
XFreePixmap(theDisplay,theCursorMask);
}
/*
* 色を初期設定する
*/
SetupColors()
{
XColor theExactColor;
Colormap theColormap;
theColormap = DefaultColormap(theDisplay, theScreen);
if (theDepth == 1) {
Foreground = "black";
Background = "white";
}
if (ReverseVideo == True) {
char *tmp;
tmp = Foreground;
Foreground = Background;
Background = tmp;
}
if (!XAllocNamedColor(theDisplay, theColormap,
Foreground, &theForegroundColor, &theExactColor)) {
fprintf(stderr, "%s: Can't XAllocNamedColor(\"%s\").\n",
ProgramName, Foreground);
exit(1);
}
if (!XAllocNamedColor(theDisplay, theColormap,
Background, &theBackgroundColor, &theExactColor)) {
fprintf(stderr, "%s: Can't XAllocNamedColor(\"%s\").\n",
ProgramName, Background);
exit(1);
}
}
/*
* Routine to let user select a window using the mouse
*
* This routine originate in dsimple.c
*/
Window Select_Window(dpy)
Display *dpy;
{
int status;
Cursor cursor;
XEvent event;
Window target_win = None, root = theRoot;
int buttons = 0;
/* Make the target cursor */
cursor = theCursor;
/* Grab the pointer using target cursor, letting it room all over */
status = XGrabPointer(dpy, root, False,
ButtonPressMask|ButtonReleaseMask, GrabModeSync,
GrabModeAsync, root, cursor, CurrentTime);
if (status != GrabSuccess) {
fprintf(stderr, "%s: Can't grab the mouse.\n", ProgramName);
exit(1);
}
/* Let the user select a window... */
while ((target_win == None) || (buttons != 0)) {
/* allow one more event */
XAllowEvents(dpy, SyncPointer, CurrentTime);
XWindowEvent(dpy, root, ButtonPressMask|ButtonReleaseMask, &event);
switch (event.type) {
case ButtonPress:
if (target_win == None) {
target_win = event.xbutton.subwindow; /* window selected */
if (target_win == None) target_win = root;
}
buttons++;
break;
case ButtonRelease:
if (buttons > 0) /* there may have been some down before we started */
buttons--;
break;
}
}
XUngrabPointer(dpy, CurrentTime); /* Done with pointer */
return(target_win);
}
/*
* Window_With_Name: routine to locate a window with a given name on a display.
* If no window with the given name is found, 0 is returned.
* If more than one window has the given name, the first
* one found will be returned. Only top and its subwindows
* are looked at. Normally, top should be the RootWindow.
*
* This routine originate in dsimple.c
*/
Window Window_With_Name(dpy, top, name)
Display *dpy;
Window top;
char *name;
{
Window *children, dummy;
unsigned int nchildren;
int i;
Window w=0;
char *window_name;
if (XFetchName(dpy, top, &window_name) && !strcmp(window_name, name))
return(top);
if (!XQueryTree(dpy, top, &dummy, &dummy, &children, &nchildren))
return(0);
for (i=0; i<nchildren; i++) {
w = Window_With_Name(dpy, children[i], name);
if (w)
break;
}
if (children) XFree ((char *)children);
return(w);
}
/*
* スクリーン環境初期化
*/
void
InitScreen(DisplayName)
char *DisplayName;
{
XSetWindowAttributes theWindowAttributes;
unsigned long theWindowMask;
Window theTempRoot;
int WindowPointX;
int WindowPointY;
unsigned int BorderWidth;
int event_base, error_base;
if ((theDisplay = XOpenDisplay(DisplayName)) == NULL) {
fprintf(stderr, "%s: Can't open display", ProgramName);
if (DisplayName != NULL) {
fprintf(stderr, " %s.\n", DisplayName);
} else {
fprintf(stderr, ".\n");
}
exit(1);
}
GetResources();
if (Synchronous == True) {
fprintf(stderr,"Synchronizing.\n");
XSynchronize(theDisplay,True);
}
#ifdef SHAPE
if (!NoShape && XShapeQueryExtension(theDisplay,
&event_base, &error_base) == False) {
fprintf(stderr, "Display not suported shape extension.\n");
NoShape = True;
}
#endif SHAPE
theScreen = DefaultScreen(theDisplay);
theDepth = DefaultDepth(theDisplay, theScreen);
theRoot = RootWindow(theDisplay, theScreen);
XGetGeometry(theDisplay, theRoot, &theTempRoot,
&WindowPointX, &WindowPointY,
&WindowWidth, &WindowHeight,
&BorderWidth, &theDepth);
SetupColors();
MakeMouseCursor();
if (ToWindow && theTarget == None) {
if (TargetName != NULL) {
int i;
for (i=0; i<5; i++) {
theTarget = Window_With_Name(theDisplay, theRoot, TargetName);
if (theTarget != None) break;
}
if (theTarget == None) {
fprintf(stderr, "%s: No window with name '%s' exists.\n",
ProgramName, TargetName);
exit(1);
}
} else {
theTarget = Select_Window(theDisplay);
if (theTarget == theRoot) {
theTarget = None;
ToWindow = False;
}
}
if (theTarget != None) {
Window QueryRoot, QueryParent, *QueryChildren;
unsigned int nchild;
for (;;) {
if (XQueryTree(theDisplay, theTarget, &QueryRoot,
&QueryParent, &QueryChildren, &nchild)) {
XFree(QueryChildren);
if (QueryParent == QueryRoot) break;
theTarget = QueryParent;
}
else {
fprintf(stderr, "%s: Target Lost.\n",ProgramName);
exit(1);
}
}
}
}
theWindowAttributes.background_pixel = theBackgroundColor.pixel;
theWindowAttributes.cursor = theCursor;
theWindowAttributes.override_redirect = True;
if (!ToWindow) XChangeWindowAttributes(theDisplay, theRoot, CWCursor,
&theWindowAttributes);
theWindowMask = CWBackPixel |
CWCursor |
CWOverrideRedirect;
theWindow = XCreateWindow(theDisplay, theRoot, 0, 0,
BITMAP_WIDTH, BITMAP_HEIGHT,
0, theDepth, InputOutput, CopyFromParent,
theWindowMask, &theWindowAttributes);
if (WindowName == NULL) WindowName = ProgramName;
XStoreName(theDisplay, theWindow, WindowName);
InitBitmapAndGCs();
XSelectInput(theDisplay, theWindow,
ExposureMask|VisibilityChangeMask|KeyPressMask);
XFlush(theDisplay);
}
/*
* SIGINT シグナル処理
*/
void
RestoreCursor()
{
XSetWindowAttributes theWindowAttributes;
BitmapGCData *BitmapGCDataTablePtr;
theWindowAttributes.cursor = None;
XChangeWindowAttributes(theDisplay, theRoot, CWCursor,
&theWindowAttributes);
for (BitmapGCDataTablePtr = BitmapGCDataTable;
BitmapGCDataTablePtr->GCCreatePtr != NULL;
BitmapGCDataTablePtr++) {
XFreePixmap(theDisplay,*(BitmapGCDataTablePtr->BitmapCreatePtr));
XFreePixmap(theDisplay,*(BitmapGCDataTablePtr->BitmapMasksPtr));
XFreeGC(theDisplay,*(BitmapGCDataTablePtr->GCCreatePtr));
}
XFreeCursor(theDisplay,theCursor);
XCloseDisplay(theDisplay);
exit(0);
}
/*
* インターバル
*
* この関数を呼ぶと、ある一定の時間返ってこなくなる。猫
* の動作タイミング調整に利用すること。
*/
void
Interval()
{
pause();
if (RaiseWindowDelay>0)
RaiseWindowDelay--;
}
/*
* ティックカウント処理
*/
void
TickCount()
{
if (++NekoTickCount >= MAX_TICK) {
NekoTickCount = 0;
}
if (NekoTickCount % 2 == 0) {
if (NekoStateCount < MAX_TICK) {
NekoStateCount++;
}
}
}
/*
* 猫状態設定
*/
void
SetNekoState(SetValue)
int SetValue;
{
NekoTickCount = 0;
NekoStateCount = 0;
NekoState = SetValue;
}
/*
* 猫描画処理
*/
void
DrawNeko(x, y, DrawAnime)
int x;
int y;
Animation DrawAnime;
{
/*@@@@@@*/
register GC DrawGC = *(DrawAnime.TickGCPtr);
register Pixmap DrawMask = *(DrawAnime.TickMaskPtr);
if ((x != NekoLastX) || (y != NekoLastY)
|| (DrawGC != NekoLastGC)) {
XWindowChanges theChanges;
theChanges.x = x;
theChanges.y = y;
XConfigureWindow(theDisplay, theWindow, CWX | CWY, &theChanges);
#ifdef SHAPE
if (NoShape == False) {
XShapeCombineMask(theDisplay, theWindow, ShapeBounding,
0, 0, DrawMask, ShapeSet);
}
#endif SHAPE
if (DontMapped) {
XMapWindow(theDisplay, theWindow);
DontMapped = 0;
}
XFillRectangle(theDisplay, theWindow, DrawGC,
0, 0, BITMAP_WIDTH, BITMAP_HEIGHT);
}
XFlush(theDisplay);
NekoLastX = x;
NekoLastY = y;
NekoLastGC = DrawGC;
}
/*
* 猫再描画処理
*/
void
RedrawNeko()
{
XFillRectangle(theDisplay, theWindow, NekoLastGC,
0, 0, BITMAP_WIDTH, BITMAP_HEIGHT);
XFlush(theDisplay);
}
/*
* 猫移動方法決定
*
* This sets the direction that the neko is moving.
*
*/
void
NekoDirection()
{
int NewState;
double LargeX, LargeY;
double Length;
double SinTheta;
if (NekoMoveDx == 0 && NekoMoveDy == 0) {
NewState = NEKO_STOP;
} else {
LargeX = (double)NekoMoveDx;
LargeY = (double)(-NekoMoveDy);
Length = sqrt(LargeX * LargeX + LargeY * LargeY);
SinTheta = LargeY / Length;
if (NekoMoveDx > 0) {
if (SinTheta > SinPiPer8Times3) {
NewState = NEKO_U_MOVE;
} else if ((SinTheta <= SinPiPer8Times3)
&& (SinTheta > SinPiPer8)) {
NewState = NEKO_UR_MOVE;
} else if ((SinTheta <= SinPiPer8)
&& (SinTheta > -(SinPiPer8))) {
NewState = NEKO_R_MOVE;
} else if ((SinTheta <= -(SinPiPer8))
&& (SinTheta > -(SinPiPer8Times3))) {
NewState = NEKO_DR_MOVE;
} else {
NewState = NEKO_D_MOVE;
}
} else {
if (SinTheta > SinPiPer8Times3) {
NewState = NEKO_U_MOVE;
} else if ((SinTheta <= SinPiPer8Times3)
&& (SinTheta > SinPiPer8)) {
NewState = NEKO_UL_MOVE;
} else if ((SinTheta <= SinPiPer8)
&& (SinTheta > -(SinPiPer8))) {
NewState = NEKO_L_MOVE;
} else if ((SinTheta <= -(SinPiPer8))
&& (SinTheta > -(SinPiPer8Times3))) {
NewState = NEKO_DL_MOVE;
} else {
NewState = NEKO_D_MOVE;
}
}
}
if (NekoState != NewState) {
SetNekoState(NewState);
}
}
/*
* 猫壁ぶつかり判定
*/
Bool
IsWindowOver()
{
Bool ReturnValue = False;
if (NekoY <= 0) {
NekoY = 0;
ReturnValue = True;
} else if (NekoY >= WindowHeight - BITMAP_HEIGHT) {
NekoY = WindowHeight - BITMAP_HEIGHT;
ReturnValue = True;
}
if (NekoX <= 0) {
NekoX = 0;
ReturnValue = True;
} else if (NekoX >= WindowWidth - BITMAP_WIDTH) {
NekoX = WindowWidth - BITMAP_WIDTH;
ReturnValue = True;
}
return(ReturnValue);
}
/*
* 猫移動状況判定
*/
Bool
IsNekoDontMove()
{
if (NekoX == NekoLastX && NekoY == NekoLastY) {
return(True);
} else {
return(False);
}
}
/*
* 猫移動開始判定
*/
Bool
IsNekoMoveStart()
{
if ((PrevMouseX >= MouseX - IdleSpace
&& PrevMouseX <= MouseX + IdleSpace) &&
(PrevMouseY >= MouseY - IdleSpace
&& PrevMouseY <= MouseY + IdleSpace) &&
(PrevTarget == theTarget)) {
return(False);
} else {
return(True);
}
}
/*
* 猫移動 dx, dy 計算
*/
void
CalcDxDy()
{
Window QueryRoot, QueryChild;
int AbsoluteX, AbsoluteY;
int RelativeX, RelativeY;
unsigned int ModKeyMask;
double LargeX, LargeY;
double DoubleLength, Length;
XQueryPointer(theDisplay, theWindow,
&QueryRoot, &QueryChild,
&AbsoluteX, &AbsoluteY,
&RelativeX, &RelativeY,
&ModKeyMask);
PrevMouseX = MouseX;
PrevMouseY = MouseY;
PrevTarget = theTarget;
MouseX = AbsoluteX+XOffset;
MouseY = AbsoluteY+YOffset;
if (ToFocus) {
int revert;
XGetInputFocus(theDisplay, &theTarget, &revert);
if (theTarget != theRoot
&& theTarget != PointerRoot && theTarget != None) {
Window QueryParent, *QueryChildren;
unsigned int nchild;
for (;;) {
if (XQueryTree(theDisplay, theTarget, &QueryRoot,
&QueryParent, &QueryChildren, &nchild)) {
XFree(QueryChildren);
if (QueryParent == QueryRoot) break;
theTarget = QueryParent;
}
else {
theTarget = None;
break;
}
}
}
else {
theTarget = None;
}
}
if ((ToWindow || ToFocus) && theTarget != None) {
int status;
XWindowAttributes theTargetAttributes;
status =
XGetWindowAttributes(theDisplay, theTarget, &theTargetAttributes);
if (ToWindow && status == 0) {
fprintf(stderr, "%s: '%s', Target Lost.\n",ProgramName, WindowName);
RestoreCursor();
}
if (theTargetAttributes.x+theTargetAttributes.width > 0
&& theTargetAttributes.x < (int)WindowWidth
&& theTargetAttributes.y+theTargetAttributes.height > 0
&& theTargetAttributes.y < (int)WindowHeight
&& theTargetAttributes.map_state == IsViewable) {
if (ToFocus) {
if (MouseX < theTargetAttributes.x+BITMAP_WIDTH/2)
LargeX = (double)(theTargetAttributes.x + XOffset - NekoX);
else if (MouseX > theTargetAttributes.x+theTargetAttributes.width
-BITMAP_WIDTH/2)
LargeX = (double)(theTargetAttributes.x + theTargetAttributes.width
+ XOffset - NekoX - BITMAP_WIDTH);
else
LargeX = (double)(MouseX - NekoX - BITMAP_WIDTH / 2);
LargeY = (double)(theTargetAttributes.y
+ YOffset - NekoY - BITMAP_HEIGHT);
}
else {
MouseX = theTargetAttributes.x
+ theTargetAttributes.width / 2 + XOffset;
MouseY = theTargetAttributes.y + YOffset;
LargeX = (double)(MouseX - NekoX - BITMAP_WIDTH / 2);
LargeY = (double)(MouseY - NekoY - BITMAP_HEIGHT);
}
}
else {
LargeX = (double)(MouseX - NekoX - BITMAP_WIDTH / 2);
LargeY = (double)(MouseY - NekoY - BITMAP_HEIGHT);
}
}
else {
LargeX = (double)(MouseX - NekoX - BITMAP_WIDTH / 2);
LargeY = (double)(MouseY - NekoY - BITMAP_HEIGHT);
}
DoubleLength = LargeX * LargeX + LargeY * LargeY;
if (DoubleLength != (double)0) {
Length = sqrt(DoubleLength);
if (Length <= NekoSpeed) {
NekoMoveDx = (int)LargeX;
NekoMoveDy = (int)LargeY;
} else {
NekoMoveDx = (int)((NekoSpeed * LargeX) / Length);
NekoMoveDy = (int)((NekoSpeed * LargeY) / Length);
}
} else {
NekoMoveDx = NekoMoveDy = 0;
}
}
/*
* 動作解析猫描画処理
*/
void
NekoThinkDraw()
{
CalcDxDy();
if (NekoState != NEKO_SLEEP) {
DrawNeko(NekoX, NekoY,
AnimationPattern[NekoState][NekoTickCount & 0x1]);
} else {
DrawNeko(NekoX, NekoY,
AnimationPattern[NekoState][(NekoTickCount >> 2) & 0x1]);
}
TickCount();
switch (NekoState) {
case NEKO_STOP:
if (IsNekoMoveStart()) {
SetNekoState(NEKO_AWAKE);
break;
}
if (NekoStateCount < NEKO_STOP_TIME) {
break;
}
if (NekoMoveDx < 0 && NekoX <= 0) {
SetNekoState(NEKO_L_TOGI);
} else if (NekoMoveDx > 0 && NekoX >= WindowWidth - BITMAP_WIDTH) {
SetNekoState(NEKO_R_TOGI);
} else if ((NekoMoveDy < 0 && NekoY <= 0)
|| (ToFocus && theTarget != None && NekoY > MouseY)){
SetNekoState(NEKO_U_TOGI);
} else if ((NekoMoveDy > 0 && NekoY >= WindowHeight - BITMAP_HEIGHT)
|| (ToFocus && theTarget != None
&& NekoY < MouseY - BITMAP_HEIGHT)){
SetNekoState(NEKO_D_TOGI);
} else {
SetNekoState(NEKO_JARE);
}
break;
case NEKO_JARE:
if (IsNekoMoveStart()) {
SetNekoState(NEKO_AWAKE);
break;
}
if (NekoStateCount < NEKO_JARE_TIME) {
break;
}
SetNekoState(NEKO_KAKI);
break;
case NEKO_KAKI:
if (IsNekoMoveStart()) {
SetNekoState(NEKO_AWAKE);
break;
}
if (NekoStateCount < NEKO_KAKI_TIME) {
break;
}
SetNekoState(NEKO_AKUBI);
break;
case NEKO_AKUBI:
if (IsNekoMoveStart()) {
SetNekoState(NEKO_AWAKE);
break;
}
if (NekoStateCount < NEKO_AKUBI_TIME) {
break;
}
SetNekoState(NEKO_SLEEP);
break;
case NEKO_SLEEP:
if (IsNekoMoveStart()) {
SetNekoState(NEKO_AWAKE);
break;
}
break;
case NEKO_AWAKE:
if (NekoStateCount < NEKO_AWAKE_TIME) {
break;
}
NekoDirection(); /* 猫が動く向きを求める */
break;
case NEKO_U_MOVE:
case NEKO_D_MOVE:
case NEKO_L_MOVE:
case NEKO_R_MOVE:
case NEKO_UL_MOVE:
case NEKO_UR_MOVE:
case NEKO_DL_MOVE:
case NEKO_DR_MOVE:
NekoX += NekoMoveDx;
NekoY += NekoMoveDy;
NekoDirection();
if (IsWindowOver()) {
if (IsNekoDontMove()) {
SetNekoState(NEKO_STOP);
}
}
break;
case NEKO_U_TOGI:
case NEKO_D_TOGI:
case NEKO_L_TOGI:
case NEKO_R_TOGI:
if (IsNekoMoveStart()) {
SetNekoState(NEKO_AWAKE);
break;
}
if (NekoStateCount < NEKO_TOGI_TIME) {
break;
}
SetNekoState(NEKO_KAKI);
break;
default:
/* Internal Error */
SetNekoState(NEKO_STOP);
break;
}
Interval();
}
/*
* キーイベント処理
*/
Bool
ProcessKeyPress(theKeyEvent)
XKeyEvent *theKeyEvent;
{
int Length;
int theKeyBufferMaxLen = AVAIL_KEYBUF;
char theKeyBuffer[AVAIL_KEYBUF + 1];
KeySym theKeySym;
XComposeStatus theComposeStatus;
Bool ReturnState;
ReturnState = True;
Length = XLookupString(theKeyEvent,
theKeyBuffer, theKeyBufferMaxLen,
&theKeySym, &theComposeStatus);
if (Length > 0) {
switch (theKeyBuffer[0]) {
case 'q':
case 'Q':
if (theKeyEvent->state & Mod1Mask) { /* META (Alt) キー */
ReturnState = False;
}
break;
default:
break;
}
}
return(ReturnState);
}
/*
* イベント処理
*/
Bool
ProcessEvent()
{
XEvent theEvent;
Bool ContinueState = True;
while (XPending(theDisplay)) {
XNextEvent(theDisplay,&theEvent);
switch (theEvent.type) {
case Expose:
if (theEvent.xexpose.count == 0) {
RedrawNeko();
}
break;
case KeyPress:
ContinueState = ProcessKeyPress(&theEvent);
if (!ContinueState) {
return(ContinueState);
}
break;
case VisibilityNotify:
if (RaiseWindowDelay==0) {
XRaiseWindow(theDisplay,theWindow);
RaiseWindowDelay=DEFAULT_RAISE_WAIT;
}
default:
/* Unknown Event */
break;
}
}
return(ContinueState);
}
/*
* 猫処理
*/
void
ProcessNeko()
{
struct itimerval Value;
/* 猫の初期化 */
NekoX = (WindowWidth - BITMAP_WIDTH / 2) / 2;
NekoY = (WindowHeight - BITMAP_HEIGHT / 2) / 2;
NekoLastX = NekoX;
NekoLastY = NekoY;
SetNekoState(NEKO_STOP);
/* タイマー設定 */
timerclear(&Value.it_interval);
timerclear(&Value.it_value);
Value.it_interval.tv_usec = IntervalTime;
Value.it_value.tv_usec = IntervalTime;
setitimer(ITIMER_REAL, &Value, 0);
/* メイン処理 */
do {
NekoThinkDraw();
} while (ProcessEvent());
}
/*
* SIGALRM シグナル処理
*/
static void
NullFunction()
{
/* No Operation */
#if defined(SYSV) || defined(SVR4)
signal(SIGALRM, NullFunction);
#endif /* SYSV || SVR4 */
}
/*
* エラー処理
*/
int
NekoErrorHandler(dpy, err)
Display *dpy;
XErrorEvent *err;
{
if (err->error_code==BadWindow && (ToWindow || ToFocus)) {
}
else {
char msg[80];
XGetErrorText(dpy, err->error_code, msg, 80);
fprintf(stderr, "%s: Error and exit.\n%s\n", ProgramName, msg);
exit(1);
}
}
/*
* Usage
*/
char *message[] = {
"",
"Options are:",
"-display <display> : Neko appears on specified display.",
"-fg <color> : Foreground color",
"-bg <color> : Background color",
"-speed <dots>",
"-time <microseconds>",
"-idle <dots>",
"-name <name> : set window name of neko.",
"-towindow : Neko chases selected window.",
"-toname <name> : Neko chases specified window.",
"-tofocus : Neko runs on top of focus window",
"-rv : Reverse video. (effects monochrome display only)",
"-position <geometry> : adjust position relative to mouse pointer.",
"-debug : puts you in synchronous mode.",
"-patchlevel : print out your current patchlevel.",
NULL };
void
Usage()
{
char **mptr;
int loop;
mptr = message;
fprintf(stderr, "Usage: %s [<options>]\n", ProgramName);
while (*mptr) {
fprintf(stderr,"%s\n", *mptr);
mptr++;
}
for (loop=0;loop<BITMAPTYPES;loop++)
fprintf(stderr,"-%s Use %s bitmaps\n",AnimalDefaultsDataTable[loop].name,AnimalDefaultsDataTable[loop].name);
}
/*
* オプションの理解
*/
Bool
GetArguments(argc, argv, theDisplayName)
int argc;
char *argv[];
char *theDisplayName;
{
int ArgCounter;
int result,foo,bar;
extern int XOffset,YOffset;
int loop,found=0;
theDisplayName[0] = '\0';
for (ArgCounter = 0; ArgCounter < argc; ArgCounter++) {
if (strncmp(argv[ArgCounter], "-h", 2) == 0) {
Usage();
exit(0);
}
if (strcmp(argv[ArgCounter], "-display") == 0) {
ArgCounter++;
if (ArgCounter < argc) {
strcpy(theDisplayName, argv[ArgCounter]);
} else {
fprintf(stderr, "%s: -display option error.\n", ProgramName);
exit(1);
}
}
else if (strcmp(argv[ArgCounter], "-speed") == 0) {
ArgCounter++;
if (ArgCounter < argc) {
NekoSpeed = atof(argv[ArgCounter]);
} else {
fprintf(stderr, "%s: -speed option error.\n", ProgramName);
exit(1);
}
}
else if (strcmp(argv[ArgCounter], "-time") == 0) {
ArgCounter++;
if (ArgCounter < argc) {
IntervalTime = atol(argv[ArgCounter]);
} else {
fprintf(stderr, "%s: -time option error.\n", ProgramName);
exit(1);
}
}
else if (strcmp(argv[ArgCounter], "-idle") == 0) {
ArgCounter++;
if (ArgCounter < argc) {
IdleSpace = atol(argv[ArgCounter]);
} else {
fprintf(stderr, "%s: -idle option error.\n", ProgramName);
exit(1);
}
}
else if (strcmp(argv[ArgCounter], "-name") == 0) {
ArgCounter++;
if (ArgCounter < argc) {
WindowName = argv[ArgCounter];
} else {
fprintf(stderr, "%s: -name option error.\n", ProgramName);
exit(1);
}
}
else if (strcmp(argv[ArgCounter], "-towindow") == 0) {
ToWindow = True;
ToFocus = False;
}
else if (strcmp(argv[ArgCounter], "-toname") == 0) {
ArgCounter++;
if (ArgCounter < argc) {
TargetName = argv[ArgCounter];
ToWindow = True;
ToFocus = False;
} else {
fprintf(stderr, "%s: -toname option error.\n", ProgramName);
exit(1);
}
}
else if (strcmp(argv[ArgCounter], "-tofocus") == 0) {
ToFocus = True;
ToWindow = False;
}
else if ((strcmp(argv[ArgCounter], "-fg") == 0) ||
(strcmp(argv[ArgCounter], "-foreground") == 0)) {
ArgCounter++;
Foreground = argv[ArgCounter];
}
else if ((strcmp(argv[ArgCounter], "-bg") == 0) ||
(strcmp(argv[ArgCounter], "-background") == 0)) {
ArgCounter++;
Background = argv[ArgCounter];
}
else if (strcmp(argv[ArgCounter], "-rv") == 0) {
ReverseVideo = True;
}
else if (strcmp(argv[ArgCounter], "-noshape") == 0) {
NoShape = True;
}
else if (strcmp(argv[ArgCounter], "-position") == 0) {
ArgCounter++;
result=XParseGeometry(argv[ArgCounter],&XOffset,&YOffset,&foo,&bar);
}
else if (strcmp(argv[ArgCounter], "-debug") ==0) {
Synchronous = True;
}
else if (strcmp(argv[ArgCounter], "-patchlevel") == 0) {
fprintf(stderr,"Patchlevel :%s\n",PATCHLEVEL);
}
else {
char *av = argv[ArgCounter] + 1;
if (strcmp(av, "bsd") == 0)
av = "bsd_daemon";
for (loop=0;loop<BITMAPTYPES;loop++) {
if (strcmp(av,AnimalDefaultsDataTable[loop].name)==0)
{NekoMoyou = loop;found=1;}
}
if (!found) {
fprintf(stderr,
"%s: Unknown option \"%s\".\n", ProgramName,
argv[ArgCounter]);
Usage();
exit(1);
}
}
}
if (strlen(theDisplayName) < 1) {
theDisplayName = NULL;
}
}
/*
* メイン関数
*/
int
main(argc, argv)
int argc;
char *argv[];
{
char theDisplayName[MAXDISPLAYNAME];
ProgramName = argv[0];
argc--;
argv++;
GetArguments(argc, argv, theDisplayName);
XSetErrorHandler(NekoErrorHandler);
InitScreen(theDisplayName);
signal(SIGALRM, NullFunction);
signal(SIGINT, RestoreCursor);
signal(SIGTERM, RestoreCursor);
signal(SIGQUIT, RestoreCursor);
SinPiPer8Times3 = sin(PI_PER8 * (double)3);
SinPiPer8 = sin(PI_PER8);
ProcessNeko();
RestoreCursor();
}
|