File: xscreen.c

package info (click to toggle)
astrolog 5.40-2
  • links: PTS
  • area: non-free
  • in suites: sarge
  • size: 1,524 kB
  • ctags: 2,155
  • sloc: ansic: 20,628; perl: 313; makefile: 59
file content (1848 lines) | stat: -rw-r--r-- 51,783 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
/*
** Astrolog (Version 5.40) File: xscreen.c
**
** IMPORTANT NOTICE: The graphics database and chart display routines
** used in this program are Copyright (C) 1991-1998 by Walter D. Pullen
** (Astara@msn.com, http://www.magitech.com/~cruiser1/astrolog.htm).
** Permission is granted to freely use and distribute these routines
** provided one doesn't sell, restrict, or profit from them in any way.
** Modification is allowed provided these notices remain with any
** altered or edited versions of the program.
**
** The main planetary calculation routines used in this program have
** been Copyrighted and the core of this program is basically a
** conversion to C of the routines created by James Neely as listed in
** Michael Erlewine's 'Manual of Computer Programming for Astrologers',
** available from Matrix Software. The copyright gives us permission to
** use the routines for personal use but not to sell them or profit from
** them in any way.
**
** The PostScript code within the core graphics routines are programmed
** and Copyright (C) 1992-1993 by Brian D. Willoughby
** (brianw@sounds.wa.com). Conditions are identical to those above.
**
** The extended accurate ephemeris databases and formulas are from the
** calculation routines in the program "Placalc" and are programmed and
** Copyright (C) 1989,1991,1993 by Astrodienst AG and Alois Treindl
** (alois@azur.ch). The use of that source code is subject to
** regulations made by Astrodienst Zurich, and the code is not in the
** public domain. This copyright notice must not be changed or removed
** by any user of this program.
**
** Initial programming 8/28,30, 9/10,13,16,20,23, 10/3,6,7, 11/7,10,21/1991.
** X Window graphics initially programmed 10/23-29/1991.
** PostScript graphics initially programmed 11/29-30/1992.
** Last code change made 12/20/1998.
*/

#include "astrolog.h"


#ifdef GRAPH
/*
******************************************************************************
** Astrolog Icon.
******************************************************************************
*/

#ifdef X11
/* This information used to define Astrolog's X icon (Rainbow over Third */
/* Eye) is identical to the output format used by the bitmap program.    */
/* You could extract this section and run xsetroot -bitmap on it.        */

#define icon_width 63
#define icon_height 32
static char icon_bits[] = {
 0x00,0x00,0x00,0xa8,0x0a,0x00,0x00,0x00,0x00,0x00,0x40,0x55,0x55,0x01,0x00,
 0x00,0x00,0x00,0xa8,0xaa,0xaa,0x0a,0x00,0x00,0x00,0x00,0x54,0xf5,0x57,0x15,
 0x00,0x00,0x00,0x80,0xaa,0xaa,0xaa,0xaa,0x00,0x00,0x00,0x40,0xd5,0xff,0xff,
 0x55,0x01,0x00,0x00,0xa0,0xaa,0xaa,0xaa,0xaa,0x02,0x00,0x00,0x50,0xfd,0xff,
 0xff,0x5f,0x05,0x00,0x00,0xa8,0xaa,0x2a,0xaa,0xaa,0x0a,0x00,0x00,0xd4,0xff,
 0xaf,0xfa,0xff,0x15,0x00,0x00,0xaa,0x2a,0x00,0x00,0xaa,0x2a,0x00,0x00,0xf5,
 0xbf,0xaa,0xaa,0xfe,0x57,0x00,0x80,0xaa,0x02,0x00,0x00,0xa0,0xaa,0x00,0x40,
 0xfd,0xab,0xfa,0xaf,0xea,0x5f,0x01,0xa0,0xaa,0x80,0xff,0xff,0x80,0xaa,0x02,
 0x50,0xff,0xea,0xff,0xff,0xab,0x7f,0x05,0xa0,0x2a,0xf0,0xff,0xff,0x07,0xaa,
 0x02,0xd0,0xbf,0xfa,0x0f,0xf8,0xaf,0x7e,0x05,0xa8,0x0a,0xfc,0x01,0xc0,0x1f,
 0xa8,0x0a,0xd4,0xaf,0x7e,0x00,0x00,0xbf,0xfa,0x15,0xa8,0x0a,0x3f,0x00,0x00,
 0x7e,0xa8,0x0a,0xf4,0xaf,0x1f,0xe0,0x03,0xfc,0xfa,0x15,0xaa,0x82,0x0f,0xdc,
 0x1d,0xf8,0xa0,0x2a,0xf4,0xab,0x07,0x23,0x62,0xf0,0xea,0x17,0xaa,0xc2,0x87,
 0x91,0xc4,0xf0,0xa1,0x2a,0xf4,0xeb,0xc3,0xd0,0x85,0xe1,0xeb,0x17,0xaa,0xe0,
 0x83,0x91,0xc4,0xe0,0x83,0x2a,0xf5,0xeb,0x03,0x23,0x62,0xe0,0xeb,0x57,0xaa,
 0xe0,0x01,0xdc,0x1d,0xc0,0x83,0x2a,0xf5,0xeb,0x01,0xe0,0x03,0xc0,0xeb,0x57,
 0xaa,0xe0,0x01,0x00,0x00,0xc0,0x83,0x2a,0xfd,0xeb,0x01,0x00,0x00,0xc0,0xeb,
 0x5f};
#endif


/*
******************************************************************************
** Interactive Screen Graphics Routines.
******************************************************************************
*/

/* Set up all the colors used by the program, i.e. the foreground and   */
/* background colors, and all the colors in the object arrays, based on */
/* whether or not we are in monochrome and/or reverse video mode.       */

void InitColorsX()
{
  int i;
#ifdef X11
  Colormap cmap;
  XColor xcol;

  if (!gi.fFile) {
    cmap = XDefaultColormap(gi.disp, gi.screen);

    /* Allocate a color from the present X11 colormap. Given a string like */
    /* "violet", allocate this color and return a value specifying it.     */

    for (i = 0; i < 16; i++) {
      XParseColor(gi.disp, cmap, szColorX[i], &xcol);
      XAllocColor(gi.disp, cmap, &xcol);
      rgbind[i] = xcol.pixel;
    }
  }
#endif
  gi.kiOn   = kMainA[!gs.fInverse];
  gi.kiOff  = kMainA[gs.fInverse];
  gi.kiLite = gs.fColor ? kMainA[2+gs.fInverse] : gi.kiOn;
  gi.kiGray = gs.fColor ? kMainA[3-gs.fInverse] : gi.kiOn;
  for (i = 0; i <= 8; i++)
    kMainB[i]    = gs.fColor ? kMainA[i]    : gi.kiOn;
  for (i = 0; i <= 7; i++)
    kRainbowB[i] = gs.fColor ? kRainbowA[i] : gi.kiOn;
  for (i = 0; i < 4; i++)
    kElemB[i]    = gs.fColor ? kElemA[i]    : gi.kiOn;
  for (i = 0; i <= cAspect; i++)
    kAspB[i]     = gs.fColor ? kAspA[i]     : gi.kiOn;
  for (i = 0; i <= cObj; i++)
    kObjB[i]     = gs.fColor ? kObjA[i]     : gi.kiOn;
#ifdef X11
  if (!gi.fFile) {
    XSetBackground(gi.disp, gi.gc,   rgbind[gi.kiOff]);
    XSetForeground(gi.disp, gi.pmgc, rgbind[gi.kiOff]);
  }
#endif
}


#ifdef ISG
/* This routine opens up and initializes a window and prepares it to be */
/* drawn upon, and gets various information about the display, too.     */

void BeginX()
{
#ifdef X11
  gi.disp = XOpenDisplay(gs.szDisplay);
  if (gi.disp == NULL) {
    PrintError("Can't open display.");
    Terminate(tcFatal);
  }
  gi.screen = DefaultScreen(gi.disp);
  bg = BlackPixel(gi.disp, gi.screen);
  fg = WhitePixel(gi.disp, gi.screen);
  hint.x = gi.xOffset; hint.y = gi.yOffset;
  hint.width = gs.xWin; hint.height = gs.yWin;
  hint.min_width = BITMAPX1; hint.min_height = BITMAPY1;
  hint.max_width = BITMAPX;  hint.max_height = BITMAPY;
  hint.flags = PPosition | PSize | PMaxSize | PMinSize;
#if FALSE
  wmhint = XGetWMHints(gi.disp, gi.wind);
  wmhint->input = True;
  XSetWMHints(gi.disp, gi.wind, wmhint);
#endif
  gi.depth = DefaultDepth(gi.disp, gi.screen);
  if (gi.depth < 5) {
    gi.fMono = fTrue;      /* Is this a monochrome monitor? */
    gs.fColor = fFalse;
  }
  gi.root = RootWindow(gi.disp, gi.screen);
  if (gs.fRoot)
    gi.wind = gi.root;     /* If -XB in effect, we'll use the root window. */
  else
    gi.wind = XCreateSimpleWindow(gi.disp, DefaultRootWindow(gi.disp),
      hint.x, hint.y, hint.width, hint.height, 5, fg, bg);
  gi.pmap = XCreatePixmap(gi.disp, gi.wind, gs.xWin, gs.yWin, gi.depth);
  gi.icon = XCreateBitmapFromData(gi.disp, DefaultRootWindow(gi.disp),
    icon_bits, icon_width, icon_height);
  if (!gs.fRoot)
    XSetStandardProperties(gi.disp, gi.wind, szAppName, szAppName, gi.icon,
      (char **)xkey, 0, &hint);

  /* We have two graphics workareas. One is what the user currently sees in */
  /* the window, and the other is what we are currently drawing on. When    */
  /* done, we can quickly copy this to the viewport for a smooth look.      */

  gi.gc = XCreateGC(gi.disp, gi.wind, 0, 0);
  XSetGraphicsExposures(gi.disp, gi.gc, 0);
  gi.pmgc = XCreateGC(gi.disp, gi.wind, 0, 0);
  InitColorsX();                                  /* Go set up colors. */
  if (!gs.fRoot)
    XSelectInput(gi.disp, gi.wind, KeyPressMask | StructureNotifyMask |
      ExposureMask | ButtonPressMask | ButtonReleaseMask | ButtonMotionMask);
  XMapRaised(gi.disp, gi.wind);
  XSync(gi.disp, 0);
  XFillRectangle(gi.disp, gi.pmap, gi.pmgc, 0, 0, gs.xWin, gs.yWin);
#endif /* X11 */

#ifdef WIN
  if (wi.fChartWindow && (wi.xClient != gs.xWin ||
    wi.yClient != gs.yWin) && wi.hdcPrint == hdcNil)
    ResizeWindowToChart();
  gi.xOffset = NMultDiv(wi.xClient - gs.xWin, wi.xScroll, nScrollDiv);
  gi.yOffset = NMultDiv(wi.yClient - gs.yWin, wi.yScroll, nScrollDiv);
  SetWindowOrg(wi.hdc, -gi.xOffset, -gi.yOffset);
  SetWindowExt(wi.hdc, wi.xClient, wi.yClient);
  SetMapMode(wi.hdc, MM_ANISOTROPIC);
  SelectObject(wi.hdc, GetStockObject(NULL_PEN));
  SelectObject(wi.hdc, GetStockObject(NULL_BRUSH));
  if (!gs.fJetTrail || wi.hdcPrint != hdcNil)
    PatBlt(wi.hdc, -gi.xOffset, -gi.yOffset, wi.xClient, wi.yClient,
      gs.fInverse ? WHITENESS : BLACKNESS);
  InitColorsX();
#endif /* WIN */

#ifdef MSG
  if (!FValidResmode(gi.nRes))    /* Initialize graphics mode to hi-res. */
    gi.nRes = gs.nResHi;
  _setvideomode(gi.nRes == -1 ? _VRES16COLOR : gi.nRes);
  if (_grstatus()) {
    PrintError("Can't enter graphics mode.");
    Terminate(tcFatal);
  }
  _getvideoconfig((struct videoconfig far *) &gi.cfg);
  if (gi.cfg.numcolors < 16) {
    gi.fMono  = fTrue;
    gs.fColor = fFalse;
  }
  _remapallpalette((long FPTR *) rgb);
  _setactivepage(0);
  _setvisualpage(0);
  InitColorsX();
#ifdef MOUSE
  MouseInit(xPcScreen, yPcScreen);
#endif
  /* Make sure we reset textrows upon restart. */
  gs.nTextRows = abs(gs.nTextRows);
#endif /* MSG */

#ifdef BGI
  int i;
  static struct palettetype pal;

  if (!FValidResmode(gi.nRes))    /* Initialize graphics mode to hi-res. */
    gi.nRes = gs.nResHi;
  if (!gi.fLoaded) {
    registerfarbgidriver(ATT_driver_far);      /* attf.obj     */
    registerfarbgidriver(CGA_driver_far);      /* cgaf.obj     */
    registerfarbgidriver(EGAVGA_driver_far);   /* egavgaf.obj  */
    registerfarbgidriver(Herc_driver_far);     /* hercf.obj    */
    registerfarbgidriver(IBM8514_driver_far);  /* ibm8514f.obj */
    registerfarbgidriver(PC3270_driver_far);   /* pc3270f.obj  */
    gi.nDriver = DETECT;
    initgraph(&gi.nDriver, &gi.nGraph, "");
    gi.fLoaded = fTrue;
  }
  if (gi.nRes <= 0) {
    switch (gi.nDriver) {
    case CGA:      gi.nGraph = CGAHI;      break;
    case MCGA:     gi.nGraph = MCGAHI;     break;
    case EGA:      gi.nGraph = EGAHI;      break;
    case EGA64:    gi.nGraph = EGA64HI;    break;
    case EGAMONO:  gi.nGraph = EGAMONOHI;  break;
    case HERCMONO: gi.nGraph = HERCMONOHI; break;
    case ATT400:   gi.nGraph = ATT400HI;   break;
    case VGA:      gi.nGraph = VGAHI;      break;
    case PC3270:   gi.nGraph = PC3270HI;   break;
    case IBM8514:  gi.nGraph = IBM8514HI;  break;
    default:       gi.nGraph = 0;
    }
  } else {
    switch (gi.nDriver) {
    case CGA:      gi.nGraph = CGAHI;      break;
    case MCGA:     gi.nGraph = MCGAHI;     break;
    case EGA:      gi.nGraph = EGAHI;      break;
    case EGA64:    gi.nGraph = EGA64HI;    break;
    case EGAMONO:  gi.nGraph = EGAMONOHI;  break;
    case HERCMONO: gi.nGraph = HERCMONOHI; break;
    case ATT400:   gi.nGraph = ATT400HI;   break;
    case VGA:      gi.nGraph = VGAMED;     break;
    case PC3270:   gi.nGraph = PC3270HI;   break;
    case IBM8514:  gi.nGraph = IBM8514LO;  break;
    default:       gi.nGraph = 0;
    }
  }
  setgraphmode(gi.nGraph);
  if (graphresult()) {
    PrintError("Can't enter graphics mode.");
    Terminate(tcFatal);
  }
  gi.nPages = 1 + (gi.nDriver == HERCMONO ||
    (gi.nDriver == VGA && gi.nGraph != VGAHI) || gi.nDriver == EGA);
  if (getmaxcolor()+1 < 16) {
    gi.fMono  = fTrue;
    gs.fColor = fFalse;
  }
  getpalette(&pal);
  for (i = 0; i < pal.size; i++)
    pal.colors[i] = (char)rgb[i];
  setallpalette(&pal);
  setactivepage(0);
  setvisualpage(0);
  gi.nPageCur = 0;
  InitColorsX();
#ifdef MOUSE
  MouseInit(xPcScreen, yPcScreen);
#endif
  /* Make sure we reset textrows upon restart. */
  gs.nTextRows = abs(gs.nTextRows);
#endif /* BGI */

#ifdef MACG
  MaxApplZone();
  InitGraf(&thePort);
  InitFonts();
  FlushEvents(everyEvent, 0);
  InitWindows();
  InitMenus();
  TEInit();
  InitDialogs(0L);
  InitCursor();

  gi.rcDrag = screenBits.bounds;
  gi.rcBounds.left = 20;
  gi.rcBounds.top = 20 + GetMBarHeight();
  gi.rcBounds.right = gi.rcBounds.left + gs.xWin;
  gi.rcBounds.bottom = gi.rcBounds.top + gs.yWin;
  gi.wpAst = NewCWindow(0L, &gi.rcBounds, "\pAstrolog 5.40", true,
    noGrowDocProc, (WindowPtr)-1L, true, 0);
  SetPort(gi.wpAst);
  InitColorsX();
#endif /* MACG */
}


/* Add a certain amount of time to the current hour/day/month/year quantity */
/* defining the present chart. This is used by the chart animation feature. */
/* We can add or subtract anywhere from 1 to 9 seconds, minutes, hours,     */
/* days, months, years, decades, centuries, or millenia in any one call.    */
/* This is mainly just addition to the appropriate quantity, but we have    */
/* to check for overflows, e.g. Dec 30 + 3 days = Jan 2 of Current year + 1 */

void AddTime(mode, toadd)
int mode, toadd;
{
  int d;
  real h, m;

  if (!FBetween(mode, 1, 9))
    mode = 4;

  h = RFloor(TT);
  m = RFract(TT)*100.0;
  if (mode == 1)
    m += 1.0/60.0*(real)toadd;    /* Add seconds. */
  else if (mode == 2)
    m += (real)toadd;             /* add minutes. */

  /* Add hours, either naturally or if minute value overflowed. */

  if (m < 0.0 || m >= 60.0 || mode == 3) {
    if (m >= 60.0) {
      m -= 60.0; toadd = NSgn(toadd);
    } else if (m < 0.0) {
      m += 60.0; toadd = NSgn(toadd);
    }
    h += (real)toadd;
  }

  /* Add days, either naturally or if hour value overflowed. */

  if (h >= 24.0 || h < 0.0 || mode == 4) {
    if (h >= 24.0) {
      h -= 24.0; toadd = NSgn(toadd);
    } else if (h < 0.0) {
      h += 24.0; toadd = NSgn(toadd);
    }
    DD = AddDay(MM, DD, YY, toadd);
  }

  /* Add months, either naturally or if day value overflowed. */

  if (DD > (d = DayInMonth(MM, YY)) || DD < 1 || mode == 5) {
    if (DD > d) {
      DD -= d; toadd = NSgn(toadd);
    } else if (DD < 1) {
      DD += DayInMonth(Mod12(MM - 1), YY);
      toadd = NSgn(toadd);
    }
    MM += toadd;
  }

  /* Add years, either naturally or if month value overflowed. */

  if (MM > 12 || MM < 1 || mode == 6) {
    if (MM > 12) {
      MM -= 12; toadd = NSgn(toadd);
    } else if (MM < 1) {
      MM += 12; toadd = NSgn(toadd);
    }
    YY += toadd;
  }
  if (mode == 7)
    YY += 10 * toadd;      /* Add decades.   */
  else if (mode == 8)
    YY += 100 * toadd;     /* Add centuries. */
  else if (mode == 9)
    YY += 1000 * toadd;    /* Add millenia.  */
  TT = h+m/100.0;          /* Recalibrate hour time. */
}


/* Animate the current chart based on the given values indicating how much  */
/* to update by. We update and recast the current chart info appropriately. */
/* Note animation mode for comparison charts will update the second chart.  */

void Animate(mode, toadd)
int mode, toadd;
{
  if (gi.nMode == gWorldMap || gi.nMode == gGlobe || gi.nMode == gPolar) {
    gs.nRot += toadd;
    if (gs.nRot >= nDegMax)     /* For animating globe display, add */
      gs.nRot -= nDegMax;       /* in appropriate degree value.     */
    else if (gs.nRot < 0)
      gs.nRot += nDegMax;
  } else {
    if (mode == 10) {
#ifdef TIME
      /* For the continuous chart update to present moment */
      /* animation mode, go get whatever time it is now.   */
      FInputData(szNowCore);
#else
      if (us.nRel <= rcDual)
        ciCore = ciTwin;
      else
        ciCore = ciMain;
      AddTime(1, toadd);
#endif
    } else {  /* Otherwise add on appropriate time vector to chart info. */
      if (us.nRel <= rcDual)
        ciCore = ciTwin;
      else
        ciCore = ciMain;
      AddTime(mode, toadd);
    }
    if (us.nRel <= rcDual) {
      ciTwin = ciCore;
      ciCore = ciMain;
    } else
      ciMain = ciCore;
    if (us.nRel)
      CastRelation();
    else
      CastChart(fTrue);
  }
}


/* This routine exits graphics mode, prompts the user for a set of command */
/* switches, processes them, and returns to the previous graphics with the */
/* new settings in effect, allowing one to change most any setting without */
/* having to lose their graphics state or fall way back to a -Q loop.      */

void CommandLineX()
{
  char szCommandLine[cchSzMax], *rgsz[MAXSWITCHES];
  int argc, fT, fPause = fFalse;

  ciCore = ciMain;
#ifdef MSG
  _setvideomode(_DEFAULTMODE);
  _settextrows(gs.nTextRows);
#endif
#ifdef BGI
  restorecrtmode();
  if (gs.nTextRows > 25)
    textmode(C4350);
#endif
  fT = us.fLoop; us.fLoop = fTrue;
  argc = NPromptSwitches(szCommandLine, rgsz);
  is.cchRow = 0;
  is.fSzInteract = fTrue;
  if (!FProcessSwitches(argc, rgsz))
    fPause = fTrue;
  else {
    is.fMult = fFalse;
    FPrintTables();
    if (is.fMult) {
      ClearB((lpbyte)&us.fCredit,
        (int)((lpbyte)&us.fLoop - (lpbyte)&us.fCredit));
      fPause = fTrue;
    }
  }

#ifdef PCG
  /* Pause for the user if there was either an error processing the    */
  /* switches, or one of the informational text tables was brought up. */

  if (fPause) {
    AnsiColor(kDefault);
    is.cchRow = 0;
    PrintSz("Press any key to return to graphics.\n");
    while (!kbhit())
      ;
    getch();
  }
#endif
  is.fSzInteract = fFalse;
  us.fLoop = fT;
  ciMain = ciCore;
  BeginX();
}


/* Given two chart size values, adjust them such that the chart will look */
/* "square". We round the higher value down and check certain conditions. */

void SquareX(x, y, force)
int *x, *y, force;
{
  if (!force && !fSquare)    /* Unless we want to force a square, realize */
    return;                  /* that some charts look better rectangular. */
  if (*x > *y)
    *x = *y;
  else
    *y = *x;
#ifdef PCG
  if (FEgaRes(gi.nRes))         /* Scale horizontal size if we're in a PC */
    *x = VgaFromEga(*x);        /* graphics mode without "square" pixels. */
  else if (FCgaRes(gi.nRes))
    *x = VgaFromCga(*x);
#endif
  if (fSidebar)      /* Take into account chart's sidebar, if any. */
    *x += xSideT;
}


#ifndef WIN
/* This routine gets called after graphics are brought up and displayed     */
/* on the screen. It loops, processing key presses, mouse clicks, etc, that */
/* the window receives, until the user specifies they want to exit program. */

void InteractX()
{
#ifdef X11
  char sz[cchSzDef];
  XEvent xevent;
  KeySym keysym;
  int fResize = fFalse, fRedraw = fTrue;
#endif
#ifdef PCG
#ifdef MOUSE
  int eventx, eventy, eventbtn;
#endif
  int fResize = fTrue, fRedraw = fFalse;
#endif /* PCG */
#ifdef MACG
  EventRecord erCur;
  WindowPtr wpCur;
  int wc, fEvent, fResize = fFalse, fRedraw = fTrue;
#endif
  int fBreak = fFalse, fPause = fFalse, fCast = fFalse, xcorner = 7,
    mousex = -1, mousey = -1, buttonx = -1, buttony = -1, dir = 1,
    length, key, i;
  bool fT;
  KI coldrw = gi.kiLite;

  neg(gs.nAnim);
  while (!fBreak) {
    gi.nScale = gs.nScale/100;

    /* Some chart windows, like the world maps and aspect grids, should */
    /* always be a certian size, so correct if a resize was attempted.  */

    if (fMap) {
      length = nDegMax*gi.nScale;
      if (gs.xWin != length) {
        gs.xWin = length;
        fResize = fTrue;
      }
      length = nDegHalf*gi.nScale;
      if (gs.yWin != length) {
        gs.yWin = length;
        fResize = fTrue;
      }
    } else if (gi.nMode == gGrid) {
      if (gs.xWin != (length =
        (gs.nGridCell + (us.nRel <= rcDual))*CELLSIZE*gi.nScale+1)) {
        gs.xWin = length;
        fResize = fTrue;
      } if (gs.yWin != length) {
        gs.yWin = length;
        fResize = fTrue;
      }

    /* Make sure the window isn't too large or too small. */

    } else {
      if (gs.xWin < BITMAPX1) {
        gs.xWin = BITMAPX1;
        fResize = fTrue;
      } else if (gs.xWin > BITMAPX) {
        gs.xWin = BITMAPX;
        fResize = fTrue;
      }
      if (gs.yWin < BITMAPY1) {
        gs.yWin = BITMAPY1;
        fResize = fTrue;
      } else if (gs.yWin > BITMAPY) {
        gs.yWin = BITMAPY;
        fResize = fTrue;
      }
    }

    /* If in animation mode, ensure we are in the flicker free resolution. */

    if (gs.nAnim < 0) {
      neg(gs.nAnim);
#ifdef PCG
      if (gi.nRes == gs.nResHi && !gs.fJetTrail) {
        gi.nRes = gs.nResLo;
        BeginX();
        gs.xWin = xPcScreen;
        gs.yWin = yPcScreen;
        SquareX(&gs.xWin, &gs.yWin, fFalse);
        fResize = fTrue;
      }
#endif
    }

    /* Physically resize window if we've changed the size parameters. */

    if (fResize) {
      fResize = fFalse;
#ifdef X11
      XResizeWindow(gi.disp, gi.wind, gs.xWin, gs.yWin);
      XFreePixmap(gi.disp, gi.pmap);
      gi.pmap = XCreatePixmap(gi.disp, gi.wind, gs.xWin, gs.yWin, gi.depth);
#endif
#ifdef PCG
      if (xPcScreen > gs.xWin)
        gi.xOffset = (xPcScreen - gs.xWin) / 2;
      else {
        if (xcorner % 3 == 1)
          gi.xOffset = 0;
        else if (xcorner % 3 == 0)
          gi.xOffset = -gs.xWin + xPcScreen;
        else
          gi.xOffset = -(gs.xWin - xPcScreen) / 2;
      }
      if (yPcScreen > gs.yWin)
        gi.yOffset = (yPcScreen - gs.yWin) / 2;
      else {
        if (xcorner > 6)
          gi.yOffset = 0;
        else if (xcorner < 4)
          gi.yOffset = -gs.yWin + yPcScreen;
        else
          gi.yOffset = -(gs.yWin - yPcScreen) / 2;
      }
#endif
#ifdef MACG
      SizeWindow(gi.wpAst, gs.xWin, gs.yWin, fTrue);
#endif
      fRedraw = fTrue;
    }

    /* Recast chart if the chart information has changed any. */

    if (fCast) {
      fCast = fFalse;
      ciCore = ciMain;
      if (us.nRel)
        CastRelation();
      else
        CastChart(fTrue);
      fRedraw = fTrue;
    }
    if (gs.nAnim && !fPause)
      fRedraw = fTrue;

    /* Update the screen if anything has changed since last time around. */

    if (fRedraw && (!fPause || gs.nAnim)) {
      fRedraw = fFalse;

      /* If we're in animation mode, change the chart info appropriately. */

      if (gs.nAnim && !fPause)
        Animate(gs.nAnim, dir);

      /* Clear the screen and set up a buffer to draw in. */

#ifdef X11
      XFillRectangle(gi.disp, gi.pmap, gi.pmgc, 0, 0, gs.xWin, gs.yWin);
#endif
#ifdef PCG
#ifdef MOUSE
      MouseShow(fFalse);
#endif
#ifdef MSG
      if (gi.cfg.numvideopages > 1)
        _setactivepage(_getactivepage() == gs.fJetTrail);
#else
      if (gi.nPages > 1) {
        gi.nPageCur = (gi.nPageCur == gs.fJetTrail);
        setactivepage(gi.nPageCur);
      }
#endif
#endif /* PCG */
#ifdef MACG
      SetPort(gi.wpAst);
      InvalRect(&gi.wpAst->portRect);
      BeginUpdate(gi.wpAst);
      EraseRect(&gi.wpAst->portRect);
#endif

      DrawChartX();

      /* Make the drawn chart visible in the current screen buffer. */

#ifdef X11
      XSync(gi.disp, 0);
      XCopyArea(gi.disp, gi.pmap, gi.wind, gi.gc,
        0, 0, gs.xWin, gs.yWin, 0, 0);
#endif
#ifdef PCG
#ifdef MSG
      if (gi.cfg.numvideopages > 1)
        _setvisualpage(_getactivepage());
#else
      if (gi.nPages > 1)
        setvisualpage(gi.nPageCur);
#endif
#ifdef MOUSE
      if (!gs.nAnim || fPause)
        MouseShow(fTrue);
#endif
#endif /* PCG */
#ifdef MACG
      EndUpdate(gi.wpAst);
#endif
    }  /* if */

    /* Now process what's on the event queue, i.e. any keys pressed, etc. */

#ifdef X11
    if (XEventsQueued(gi.disp, QueuedAfterFlush /*QueuedAfterReading*/) ||
      !gs.nAnim || fPause) {
      XNextEvent(gi.disp, &xevent);

      /* Restore what's on window if a part of it gets uncovered. */

      if (xevent.type == Expose && xevent.xexpose.count == 0) {
        XSync(gi.disp, 0);
        XCopyArea(gi.disp, gi.pmap, gi.wind, gi.gc,
          0, 0, gs.xWin, gs.yWin, 0, 0);
      }
      switch (xevent.type) {

      /* Check for a manual resize of window by user. */

      case ConfigureNotify:
        gs.xWin = xevent.xconfigure.width;
        gs.yWin = xevent.xconfigure.height;
        XFreePixmap(gi.disp, gi.pmap);
        gi.pmap = XCreatePixmap(gi.disp, gi.wind, gs.xWin, gs.yWin, gi.depth);
        fRedraw = fTrue;
        break;
      case MappingNotify:
        XRefreshKeyboardMapping((XMappingEvent *)&xevent);
        break;

#ifdef MOUSE
      /* Process any mouse buttons the user pressed. */

      case ButtonPress:
        mousex = xevent.xbutton.x; mousey = xevent.xbutton.y;
        if (xevent.xbutton.button == Button1) {
          DrawColor(gi.kiLite);
          DrawPoint(mousex, mousey);
          XSync(gi.disp, 0);
          XCopyArea(gi.disp, gi.pmap, gi.wind, gi.gc,
            0, 0, gs.xWin, gs.yWin, 0, 0);
        } else if (xevent.xbutton.button == Button2 && (gi.nMode ==
          gAstroGraph || gi.nMode == gWorldMap) && gs.nRot == 0) {
          Lon = DegToDec(rDegHalf -
            (real)(xevent.xbutton.x-1)/(real)(gs.xWin-2)*rDegMax);
          Lat = DegToDec(rDegQuad -
            (real)(xevent.xbutton.y-1)/(real)(gs.yWin-2)*181.0);
          sprintf(sz, "Mouse is at %s.", SzLocation(Lon, Lat));
          PrintNotice(sz);
        } else if (xevent.xbutton.button == Button3)
          fBreak = fTrue;
        break;

      /* Check for user dragging any of the mouse buttons across window. */

      case MotionNotify:
        DrawColor(coldrw);
        DrawLine(mousex, mousey, xevent.xbutton.x, xevent.xbutton.y);
        XSync(gi.disp, 0);
        XCopyArea(gi.disp, gi.pmap, gi.wind, gi.gc,
          0, 0, gs.xWin, gs.yWin, 0, 0);
        mousex = xevent.xbutton.x; mousey = xevent.xbutton.y;
        break;
#endif

      /* Process any keys user pressed in window. */

      case KeyPress:
        length = XLookupString((XKeyEvent *)&xevent, xkey, 10, &keysym, 0);
        if (length == 1) {
          key = xkey[0];
#endif /* X11 */

#ifdef PCG
#ifdef MOUSE
      if ((!gs.nAnim || fPause) && MouseStatus(&eventx, &eventy, &eventbtn)) {

        /* If the left button is down, draw on the screen. */
        if (eventbtn == mfLeft && mousex >= 0) {
          MouseShow(fFalse);
          DrawColor(coldrw);
          PcMoveTo(mousex, mousey);
          buttonx = eventx; buttony = eventy;
          PcLineTo(buttonx, buttony);

        /* If the right button is down, change the default location. */
        } else if (eventbtn == mfRight) {
          if (fMap && gs.nRot == 0 && !gs.fConstel && !gs.fMollewide) {
            Lon = DegToDec(rDegHalf-(real)(eventx-gi.xOffset)/
              (real)gs.xWin*rDegMax);
            if (Lon < -rDegHalf)
              Lon = -rDegHalf;
            else if (Lon > rDegHalf)
              Lon = rDegHalf;
            Lat = DegToDec(rDegQuad-(real)(eventy-gi.yOffset)/
              (real)gs.yWin*181.0);
            if (Lat < -rDegQuad)
              Lat = -rDegQuad;
            else if (Lat > rDegQuad)
              Lat = rDegQuad;
            fCast = fTrue;

          /* Right button means draw lines if not in a world map mode. */
          } else if (buttonx >= 0) {
            MouseShow(fFalse);
            DrawColor(coldrw);
            PcMoveTo(buttonx, buttony);
            PcLineTo(eventx, eventy);
          }

        /* Middle button (which most PC's don't have) means exit program. */
        } else if (eventbtn == mfMiddle)
          fBreak = fTrue;

        mousex = eventx; mousey = eventy;
        MouseShow(fTrue);
      } else
#endif /* MOUSE */
        if (kbhit()) {
          key = getch();
#endif /* PCG */

#ifdef MACG
      HiliteMenu(0);
      SystemTask();
      fEvent = GetNextEvent(everyEvent, &erCur);
      if (fEvent) {
        switch (erCur.what) {
        case mouseDown:
          wc = FindWindow(erCur.where, &wpCur);
          switch (wc) {
          case inSysWindow:
            SystemClick(&erCur, wpCur);
            break;
          case inMenuBar:
            MenuSelect(erCur.where);
            break;
          case inDrag:
            if (wpCur == gi.wpAst)
              DragWindow(gi.wpAst, erCur.where, &gi.rcDrag);
            break;
          case inContent:
            if (wpCur == gi.wpAst && wpCur != FrontWindow())
              SelectWindow(gi.wpAst);
            break;
          case inGoAway:
            if (wpCur == gi.wpAst && TrackGoAway(gi.wpAst, erCur.where))
              HideWindow(gi.wpAst);
            break;
          }
          break;
        case updateEvt:
          /*fRedraw = fTrue;*/
          break;
        case keyDown:
        case autoKey:
          key = (char)(erCur.message & charCodeMask);
#endif /* MACG */

LSwitch:
          switch (key) {
#ifdef PCG
          case chNull:
            key = NFromAltN(getch());
            goto LSwitch;
#endif
          case ' ':
            fRedraw = fTrue;
            break;
          case 'p':
            not(fPause);
            break;
          case 'r':
            neg(dir);
            break;
          case 'x':
            not(gs.fInverse);
            InitColorsX();
            fRedraw = fTrue;
            break;
          case 'm':
            if (!gi.fMono) {
              not(gs.fColor);
#ifdef MSG
              _getvideoconfig((struct videoconfig far *) &gi.cfg);
#endif
              InitColorsX();
              fRedraw = fTrue;
            }
            break;
          case 'B':
#ifdef X11
            XSetWindowBackgroundPixmap(gi.disp, gi.root, gi.pmap);
            XClearWindow(gi.disp, gi.root);
#endif
#ifdef PCG
            gs.xWin = xPcScreen;
            gs.yWin = yPcScreen;
            SquareX(&gs.xWin, &gs.yWin, fFalse);
            fResize = fTrue;
#endif
            break;
          case 't':
            not(gs.fText);
            fRedraw = fTrue;
            break;
          case 'i':
            not(gs.fAlt);
            fRedraw = fTrue;
            break;
          case 'b':
            not(gs.fBorder);
            fRedraw = fTrue;
            break;
          case 'l':
            not(gs.fLabel);
            fRedraw = fTrue;
            break;
          case 'j':
            not(gs.fJetTrail);
            break;
          case '<':
            if (gs.nScale > 100) {
              gs.nScale -= 100;
              fResize = fTrue;
            }
            break;
          case '>':
            if (gs.nScale < MAXSCALE) {
              gs.nScale += 100;
              fResize = fTrue;
            }
            break;
          case '[':
            if (gi.nMode == gGlobe && gs.rTilt > -rDegQuad) {
              gs.rTilt = gs.rTilt > -rDegQuad ? gs.rTilt-TILTSTEP : -rDegQuad;
              fRedraw = fTrue;
            }
            break;
          case ']':
            if (gi.nMode == gGlobe && gs.rTilt < rDegQuad) {
              gs.rTilt = gs.rTilt < rDegQuad ? gs.rTilt+TILTSTEP : rDegQuad;
              fRedraw = fTrue;
            }
            break;
          case 'Q':
            SquareX(&gs.xWin, &gs.yWin, fTrue);
            fResize = fTrue;
            break;
          case 'R':
            for (i = oChi; i <= oVes; i++)
              not(ignore[i]);
            for (i = oLil; i <= oEP; i++)
              not(ignore[i]);
            fCast = fTrue;
            break;
          case 'C':
            not(us.fCusp);
            for (i = cuspLo; i <= cuspHi; i++)
              ignore[i] = !us.fCusp || !ignore[i];
            fCast = fTrue;
            break;
          case 'u':
            not(us.fUranian);
            for (i = uranLo; i <= uranHi; i++)
              ignore[i] = !us.fUranian || !ignore[i];
            fCast = fTrue;
            break;
          case 'U':
            us.nStar = !us.nStar;
            for (i = starLo; i <= starHi; i++)
              ignore[i] = !us.nStar || !ignore[i];
            fCast = fTrue;
            break;
          case 'c':
            us.nRel = us.nRel ? 0 : rcDual;
            fCast = fTrue;
            break;
          case 's':
            not(us.fSidereal);
            fCast = fTrue;
            break;
          case 'h':
            not(us.objCenter);
            fCast = fTrue;
            break;
          case 'f':
            not(us.fFlip);
            fCast = fTrue;
            break;
          case 'g':
            not(us.fDecan);
            fCast = fTrue;
            break;
          case 'z':
            not(us.fVedic);
            fRedraw = fTrue;
            break;
          case 'y':
            not(us.fNavamsa);
            fCast = fTrue;
            break;
          case '+':
            Animate(gs.nAnim, abs(dir));
            fCast = fTrue;
            break;
          case '-':
            Animate(gs.nAnim, -abs(dir));
            fCast = fTrue;
            break;
          case 'o':
            ciSave = ciMain;
            break;
          case 'O':
            ciMain = ciSave;
            fCast = fTrue;
            break;
#ifdef TIME
          case 'n':
            FInputData(szNowCore);
            ciMain = ciCore;
            fCast = fTrue;
            break;
#endif
          case 'N':                     /* The continuous update animation. */
            gs.nAnim = gs.nAnim ? 0 : -10;
            break;

          /* These are the nine different "add time to chart" animations. */
          case '!': gs.nAnim = -1; break;
          case '@': gs.nAnim = -2; break;
          case '#': gs.nAnim = -3; break;
          case '$': gs.nAnim = -4; break;
          case '%': gs.nAnim = -5; break;
          case '^': gs.nAnim = -6; break;
          case '&': gs.nAnim = -7; break;
          case '*': gs.nAnim = -8; break;
          case '(': gs.nAnim = -9; break;

          /* Should we go switch to a new chart type? */
          case 'V': gi.nMode = gWheel;      fRedraw = fTrue; break;
          case 'A': gi.nMode = gGrid;       fRedraw = fTrue; break;
          case 'Z': gi.nMode = gHorizon;    fRedraw = fTrue; break;
          case 'S': gi.nMode = gOrbit;      fRedraw = fTrue; break;
          case 'M': gi.nMode = gSector;     fRedraw = fTrue; break;
          case 'K': gi.nMode = gCalendar;   fRedraw = fTrue; break;
          case 'J': gi.nMode = gDisposit;   fRedraw = fTrue; break;
          case 'L': gi.nMode = gAstroGraph; fRedraw = fTrue; break;
          case 'E': gi.nMode = gEphemeris;  fRedraw = fTrue; break;
          case 'W': gi.nMode = gWorldMap;   fRedraw = fTrue; break;
          case 'G': gi.nMode = gGlobe;      fRedraw = fTrue; break;
          case 'P': gi.nMode = gPolar;      fRedraw = fTrue; break;
#ifdef BIORHYTHM
          case 'Y':            /* Should we switch to biorhythm chart? */
            us.nRel = rcBiorhythm;
            gi.nMode = gBiorhythm;
            fCast = fTrue;
            break;
#endif
#ifdef CONSTEL
          case 'F':
            if (!fMap && gi.nMode != gGlobe && gi.nMode != gPolar)
              gi.nMode = gWorldMap;
            not(gs.fConstel);
            fRedraw = fTrue;
            break;
#endif
          case '0':
            not(us.fPrimeVert);
            not(us.fCalendarYear);
            not(us.nEphemYears);
            not(gs.fMollewide);
            gi.nMode = (gi.nMode == gWheel ? gHouse :
              (gi.nMode == gHouse ? gWheel : gi.nMode));
            fRedraw = fTrue;
            break;
          case 'v': case 'H': case '?':
#ifdef MSG
            _setvideomode(_DEFAULTMODE);
            if (key != 'v')
              _settextrows(50);
#endif
#ifdef BGI
            restorecrtmode();
            if (key != 'v')
              textmode(C4350);
#endif
            length = us.nScrollRow;
            us.nScrollRow = 0;
            if (key == 'v')
              ChartListing();
            else
              DisplayKeysX();
            us.nScrollRow = length;
#ifdef PCG
            while (!kbhit())
              ;
            key = getch();
            if (key == 'q' || key == chEscape || key == chBreak) {
              fBreak = fTrue;
              break;
            }
            BeginX();
            fResize = fTrue;
#endif
            break;
          case chReturn:
            CommandLineX();
            fResize = fCast = fTrue;
            break;
#ifdef PCG
          case chTab:
            if (gi.nRes == gs.nResHi)
              gi.nRes = gs.nResLo;
            else
              gi.nRes = gs.nResHi;
            BeginX();
            gs.xWin = xPcScreen;
            gs.yWin = yPcScreen;
            SquareX(&gs.xWin, &gs.yWin, fFalse);
            fResize = fTrue;
            break;
#endif
          case chDelete:
#ifdef PCG
#ifdef MOUSE
            MouseShow(fFalse);
#endif
#endif /* PCG */
            fT = gs.fJetTrail;
            gs.fJetTrail = fFalse;
            DrawClearScreen();
            gs.fJetTrail = fT;
            break;
#ifdef MOUSE
          case 'z'-'`': coldrw = kBlack;   break;
          case 'e'-'`': coldrw = kMaroon;  break;
          case 'f'-'`': coldrw = kDkGreen; break;
          case 'o'-'`': coldrw = kOrange;  break;
          case 'n'-'`': coldrw = kDkBlue;  break;
          case 'u'-'`': coldrw = kPurple;  break;
          case 'k'-'`': coldrw = kDkCyan;  break;
          case 'l'-'`': coldrw = kLtGray;  break;
          case 'd'-'`': coldrw = kDkGray;  break;
          case 'r'-'`': coldrw = kRed;     break;
          case 'g'-'`': coldrw = kGreen;   break;
          case 'y'-'`': coldrw = kYellow;  break;
          case 'b'-'`': coldrw = kBlue;    break;
          case 'v'-'`': coldrw = kMagenta; break;
          case 'j'-'`': coldrw = kCyan;    break;
          case 'a'-'`': coldrw = kWhite;   break;
#ifdef PCG
          case 't'-'`':
            MouseShow(fFalse);
            if (buttonx >= 0)
#ifdef MSG
              _rectangle(_GBORDER, buttonx, buttony, mousex, mousey);
#else
              DrawEdge(Min(buttonx, mousex) - gi.xOffset,
                Min(buttony, mousey) - gi.yOffset,
                Max(mousex, buttonx) - gi.xOffset,
                Max(mousey, buttony) - gi.yOffset);
#endif
            MouseShow(fTrue);
            break;
          case 'x'-'`':
            MouseShow(fFalse);
            if (buttonx >= 0)
#ifdef MSG
              _ellipse(_GBORDER, buttonx, buttony, mousex, mousey);
#else
              DrawEllipse(Min(buttonx, mousex) - gi.xOffset,
                Min(buttony, mousey) - gi.yOffset,
                Max(mousex, buttonx) - gi.xOffset,
                Max(mousey, buttony) - gi.yOffset);
#endif
            MouseShow(fTrue);
            break;
#endif /* PCG */
#endif /* MOUSE */
          case 'q': case chEscape: case chBreak:
            fBreak = fTrue;
            break;
          default:
            if (key > '0' && key <= '9') {
#ifdef PCG
              if (gs.nAnim && !fPause)
#endif
                /* Process numbers 1..9 signifying animation rate. */
                dir = (dir > 0 ? 1 : -1)*(key-'0');
#ifdef PCG
              else {
                /* If we aren't in animation mode, then 1..9 refers to the */
                /* clipping "quadrant" to use if chart size > screen size. */
                xcorner = key-'0';
                fResize = fTrue;
              }
#endif
              break;
            } else if (FBetween(key, 201, 248)) {
              is.fSzInteract = fTrue;
              if (szMacro[key-201]) {
                FProcessCommandLine(szMacro[key-201]);
                fResize = fCast = fTrue;
              }
              is.fSzInteract = fFalse;
              break;
            }
            putchar(chBell);    /* Any key not bound will sound a beep. */
          }  /* switch */
        }  /* if */
#ifdef X11
      default:
        ;
      }  /* switch */
    }  /* if */
#endif
#ifdef MACG
    }  /* if */
#endif
  }  /* while */
}


/* This is called right before program termination to get rid of the window. */

void EndX()
{
#ifdef X11
  XFreeGC(gi.disp, gi.gc);
  XFreeGC(gi.disp, gi.pmgc);
  XFreePixmap(gi.disp, gi.pmap);
  XDestroyWindow(gi.disp, gi.wind);
  XCloseDisplay(gi.disp);
#endif
#ifdef MSG
  _setvideomode(_DEFAULTMODE);
#endif
#ifdef BGI
  restorecrtmode();
#endif
#ifdef MACG
  DisposeWindow(gi.wpAst);
#endif
}
#endif /* ISG */
#endif /* WIN */


/*
******************************************************************************
** Main Graphics Processing.
******************************************************************************
*/

/* Process one command line switch passed to the program dealing with the    */
/* graphics features. This is just like the processing of each switch in the */
/* main program, however here each switch has been prefixed with an 'X'.     */

int NProcessSwitchesX(argc, argv, pos, fOr, fAnd, fNot)
int argc, pos;
bool fOr, fAnd, fNot;
char **argv;
{
  int darg = 0, i, j;
  real rT;
  char ch1;

  ch1 = argv[0][pos+1];
  switch (argv[0][pos]) {
  case chNull:
    break;

  case 'b':
    if (us.fNoWrite || is.fSzInteract) {
      ErrorArgv("Xb");
      return tcError;
    }
    ch1 = ChCap(ch1);
    if (FValidBmpmode(ch1))
      gs.chBmpMode = ch1;
    SwitchF2(gs.fBitmap);
    gs.fPS = gs.fMeta = fFalse;
    break;

#ifdef PS
  case 'p':
    if (us.fNoWrite || is.fSzInteract) {
      ErrorArgv("Xp");
      return tcError;
    }
    gs.fPS = fTrue + (ch1 != '0');
    gs.fBitmap = gs.fMeta = fFalse;
    break;
#endif

#ifdef META
  case 'M':
    if (us.fNoWrite || is.fSzInteract) {
      ErrorArgv("XM");
      return tcError;
    }
    if (ch1 == '0')
      SwitchF(gs.fFont);
    SwitchF2(gs.fMeta);
    gs.fBitmap = gs.fPS = fFalse;
    break;
#endif

  case 'o':
    if (us.fNoWrite || is.fSzInteract) {
      ErrorArgv("Xo");
      return tcError;
    }
    if (argc <= 1) {
      ErrorArgc("Xo");
      return tcError;
    }
    if (!gs.fBitmap && !gs.fPS && !gs.fMeta)
      gs.fBitmap = fTrue;
    gi.szFileOut = SzPersist(argv[1]);
    darg++;
    break;

#ifdef X11
  case 'B':
    if (is.fSzInteract) {
      ErrorArgv("XB");
      return tcError;
    }
    SwitchF(gs.fRoot);
    break;
#endif

  case 'm':
    SwitchF(gs.fColor);
    break;

  case 'r':
    SwitchF(gs.fInverse);
    break;

  case 'w':
    if (argc <= 1) {
      ErrorArgc("Xw");
      return tcError;
    }
    i = atoi(argv[1]);
    if (argc > 2 && ((j = atoi(argv[2])) || argv[2][0] == '0')) {
      argc--; argv++;
      darg++;
    } else
      j = i;
    if (!FValidGraphx(i)) {
      ErrorValN("Xw", i);
      return tcError;
    }
    if (!FValidGraphy(j)) {
      ErrorValN("Xw", j);
      return tcError;
    }
    gs.xWin = i; gs.yWin = j;
    darg++;
    break;

  case 's':
    if (argc <= 1) {
      ErrorArgc("Xs");
      return tcError;
    }
    i = atoi(argv[1]);
    if (i < 100)
      i *= 100;
    if (!FValidScale(i)) {
      ErrorValN("Xs", i);
      return tcError;
    }
    gs.nScale = i;
    darg++;
    break;

  case 'i':
    SwitchF(gs.fAlt);
    break;

  case 't':
    SwitchF(gs.fText);
    break;

  case 'u':
    SwitchF(gs.fBorder);
    break;

  case 'l':
    SwitchF(gs.fLabel);
    break;

  case 'j':
    SwitchF(gs.fJetTrail);
    break;

  case '1':
    if (argc <= 1) {
      ErrorArgc("X1");
      return tcError;
    }
    i = NParseSz(argv[1], pmObject);
    if (!FItem(i)) {
      ErrorValN("X1", i);
      return tcError;
    }
    gs.objLeft = i;
    darg++;
    break;

  case '2':
    if (argc <= 1) {
      ErrorArgc("X2");
      return tcError;
    }
    i = NParseSz(argv[1], pmObject);
    if (!FItem(i)) {
      ErrorValN("X2", i);
      return tcError;
    }
    gs.objLeft = -i;
    darg++;
    break;

#ifdef X11
  case 'd':
    if (is.fSzInteract) {
      ErrorArgv("Xd");
      return tcError;
    }
    if (argc <= 1) {
      ErrorArgc("Xd");
      return tcError;
    }
    gs.szDisplay = SzPersist(argv[1]);
    darg++;
    break;
#endif

  case 'W':
    if (argc > 1 && ((i = atoi(argv[1])) || argv[1][0] == '0')) {
      darg++;
      if (!FValidRotation(i)) {
        ErrorValN("XW", i);
        return tcError;
      }
      gs.nRot = i;
    }
    gi.nMode = gWorldMap;
    if (ch1 == '0')
      gs.fMollewide = fTrue;
    is.fHaveInfo = fTrue;
    break;

  case 'G':
    if (argc > 1 && ((i = atoi(argv[1])) || argv[1][0] == '0')) {
      darg++;
      if (!FValidRotation(i)) {
        ErrorValN("XG", i);
        return tcError;
      }
      gs.nRot = i;
      if (argc > 2 && ((rT = atof(argv[2])) || argv[2][0] == '0')) {
        darg++;
        if (!FValidTilt(rT)) {
          ErrorValR("XG", rT);
          return tcError;
        }
        gs.rTilt = rT;
      }
    }
    gi.nMode = gGlobe;
    is.fHaveInfo = fTrue;
    break;

  case 'P':
    if (argc > 1 && ((i = atoi(argv[1])) || argv[1][0] == '0')) {
      darg++;
      if (!FValidRotation(i)) {
        ErrorValN("XP", i);
        return tcError;
      }
    } else
      i = 0;
    gs.nRot = i;
    gi.nMode = gPolar;
    if (ch1 == '0')
      gs.fPrintMap = fTrue;
    is.fHaveInfo = fTrue;
    break;

#ifdef CONSTEL
  case 'F':
    if (!fMap && gi.nMode != gGlobe && gi.nMode != gPolar)
      gi.nMode = gWorldMap;
    not(gs.fConstel);
    is.fHaveInfo = fTrue;
    break;
#endif

#ifdef ISG
  case 'n':
    if (argc > 1 && (i = atoi(argv[1])))
      darg++;
    else
      i = 10;
    if (i < 1 || i > 10) {
      ErrorValN("Xn", i);
      return tcError;
    }
    gs.nAnim = i;
    break;
#endif

  default:
    ErrorSwitch(argv[0]);
    return tcError;
  }
  /* 'darg' contains the value to be added to argc when we return. */
  return darg;
}


/* Process one command line switch passed to the program dealing with more  */
/* obscure graphics options. This is structured very much like the function */
/* NProcessSwitchesX(), except here we know each switch begins with 'YX'.   */

int NProcessSwitchesRareX(argc, argv, pos)
int argc, pos;
char **argv;
{
  int darg = 0, i;
#ifdef PS
  char ch1;

  ch1 = argv[0][pos+1];
#endif
  switch (argv[0][pos]) {
  case chNull:
    if (argc <= 2) {
      ErrorArgc("YX");
      return tcError;
    }
#ifdef PCG
    i = atoi(argv[1]);
    if (!FValidResmode(i)) {
      ErrorValN("YX", i);
      return tcError;
    }
    gs.nResHi = i;
    i = atoi(argv[2]);
    if (!FValidResmode(i)) {
      ErrorValN("YX", i);
      return tcError;
    }
    gs.nResLo = i;
    gs.fBitmap = gs.fPS = gs.fMeta = fFalse;
#endif
    darg += 2;
    break;

  case 'G':
    if (argc <= 1) {
      ErrorArgc("YXG");
      return tcError;
    }
    i = atoi(argv[1]);
    if (!FValidGlyphs(i)) {
      ErrorValN("YXg", i);
      return tcError;
    }
    gs.nGlyphs = i;
    darg++;
    break;

  case 'g':
    if (argc <= 1) {
      ErrorArgc("YXg");
      return tcError;
    }
    i = atoi(argv[1]);
    if (!FValidGrid(i)) {
      ErrorValN("YXg", i);
      return tcError;
    }
    gs.nGridCell = i;
    darg++;
    break;

  case 'f':
    if (argc <= 1) {
      ErrorArgc("YXf");
      return tcError;
    }
    gs.fFont = atoi(argv[1]);
    darg++;
    break;

#ifdef PS
  case 'p':
    if (ch1 == '0') {
      if (argc <= 2) {
        ErrorArgc("YXp0");
        return tcError;
      }
      gs.xInch = atof(argv[1]);
      gs.yInch = atof(argv[2]);
      darg += 2;
      break;
    }
    if (argc <= 1) {
      ErrorArgc("YXp");
      return tcError;
    }
    gs.nOrient = atoi(argv[1]);
    darg++;
    break;
#endif

  default:
    ErrorSwitch(argv[0]);
    return tcError;
  }
  /* 'darg' contains the value to be added to argc when we return. */
  return darg;
}


/* This is the main interface to all the graphics features. This routine     */
/* is called from the main program if any of the -X switches were specified, */
/* and it sets up for and goes and generates the appropriate graphics chart. */
/* We return fTrue if successfull, fFalse if some non-fatal error occurred.  */

bool FActionX()
{
  gi.fFile = (gs.fBitmap || gs.fPS || gs.fMeta);
#ifdef PS
  gi.fEps = gs.fPS > fTrue;
#endif

  /* First figure out what graphic mode to generate the chart in, based on */
  /* various non-X command switches, e.g. -L combined with -X, -g combined */
  /* with -X, and so on, and determine the size the window is to be, too.  */

  if (gi.nMode == 0) {
    if (us.fWheel)
      gi.nMode = gHouse;
    else if (us.fGrid || us.fMidpoint)
      gi.nMode = gGrid;
    else if (us.fHorizon)
      gi.nMode = gHorizon;
    else if (us.fOrbit)
      gi.nMode = gOrbit;
    else if (us.fSector)
      gi.nMode = gSector;
    else if (us.fInfluence)
      gi.nMode = gDisposit;
    else if (us.fAstroGraph)
      gi.nMode = gAstroGraph;
    else if (us.fCalendar)
      gi.nMode = gCalendar;
    else if (us.fEphemeris)
      gi.nMode = gEphemeris;
    else if (us.nRel == rcBiorhythm)
      gi.nMode = gBiorhythm;
    else
      gi.nMode = gWheel;
  }
  if (gi.nMode == gGrid) {
    if (us.nRel <= rcDual && us.fMidpoint && !us.fAspList)
      us.fGridConfig = fTrue;
    gs.xWin = gs.yWin =
      (gs.nGridCell + (us.nRel <= rcDual))*CELLSIZE*gi.nScale + 1;
  } else if (fMap) {
    gs.xWin = nDegMax*gi.nScale;
    gs.yWin = nDegHalf*gi.nScale;
  }
#ifdef WIN
  if (fSidebar)
    gs.xWin -= SIDESIZE;
#endif
  gi.nScaleT = gs.fPS ? PSMUL : (gs.fMeta ? METAMUL : 1);
#ifdef WIN
  if (wi.hdcPrint != hdcNil)
    gi.nScaleT = METAMUL;
#endif

  if (gi.fFile) {
    if (gs.xWin == 0)
      gs.xWin = DEFAULTX;
    if (gs.yWin == 0)
      gs.yWin = DEFAULTY;
    if (fSidebar)
      gs.xWin += SIDESIZE;
    if (gs.xWin > BITMAPX)
      gs.xWin = BITMAPX;
    if (gs.yWin > BITMAPY)
      gs.yWin = BITMAPY;
    BeginFileX();
    if (gs.fBitmap) {
      gi.cbBmpRow = (gs.xWin + 1) >> 1;
      gi.yBand = gs.yWin;
      if (!FEnsureGrid())
        return fFalse;
      while ((gi.bm = PAllocate((long)gi.cbBmpRow * gi.yBand, fTrue, NULL)) ==
        NULL) {
        PrintWarning("The bitmap must be generated in multiple stages.");
        gi.yBand = (gi.yBand + 1) / 2;
        if (gi.yBand < 1 || gs.chBmpMode != 'B')
          return fFalse;
      }
      if (gi.yBand == gs.yWin)
        gi.yBand = 0;
      else {
        gi.yOffset = gs.yWin - gs.yWin % gi.yBand;
        if (gi.yOffset == gs.yWin)
          gi.yOffset -= gi.yBand;
      }
    }
#ifdef PS
    else if (gs.fPS)
      PsBegin();
#endif
#ifdef META
    else {
      if (!FEnsureGrid())
        return fFalse;
      for (gi.cbMeta = MAXMETA; gi.cbMeta > 0 &&
        (gi.bm = PAllocate(gi.cbMeta, fTrue, NULL)) == NULL;
        gi.cbMeta -= MAXMETA/8)
        PrintWarning("Attempting to get maximum memory for metafile.");
      if (gi.cbMeta == 0)
        return fFalse;
      gs.xWin   *= METAMUL;  /* Increase chart sizes and scales behind the */
      gs.yWin   *= METAMUL;  /* scenes to make graphics look smoother.     */
      gs.nScale *= METAMUL;
    }
#endif
    InitColorsX();
  }
#ifdef ISG
  else {
#ifdef PCG
    BeginX();
    if (gs.xWin == 0 || gs.yWin == 0) {
      if (gs.xWin == 0)
        gs.xWin = xPcScreen;
      if (gs.yWin == 0)
        gs.yWin = yPcScreen;
      SquareX(&gs.xWin, &gs.yWin, fFalse);
    } else if (fSidebar)
      gs.xWin += SIDESIZE;
#else
    if (gs.xWin == 0 || gs.yWin == 0) {
      if (gs.xWin == 0)
        gs.xWin = DEFAULTX;
      if (gs.yWin == 0)
        gs.yWin = DEFAULTY;
      SquareX(&gs.xWin, &gs.yWin, fFalse);
    } else if (fSidebar)
      gs.xWin += SIDESIZE;
    BeginX();
#endif
  }
#endif /* ISG */

  if (gi.fFile || gs.fRoot)    /* Go draw the graphic chart. */
    DrawChartX();
  if (gi.fFile) {    /* Write bitmap to file if in that mode. */
    EndFileX();
    while (gi.yBand) {
      gi.yOffset -= gi.yBand;
      DrawChartX();
      EndFileX();
    }
    if (!gs.fPS)
      DeallocateHuge(gi.bm);
  }
#ifdef ISG
  else {
#ifdef X11
    if (gs.fRoot) {                                         /* Process -XB. */
      XSetWindowBackgroundPixmap(gi.disp, gi.root, gi.pmap);
      XClearWindow(gi.disp, gi.root);

      /* If -Xn in effect with -XB, then enter infinite loop where we */
      /* calculate and animate chart, displaying on the root window.  */
      while (gs.nAnim) {
        Animate(gs.nAnim, 1);
        if (!gs.fJetTrail)
          XFillRectangle(gi.disp, gi.pmap, gi.pmgc, 0, 0, gs.xWin, gs.yWin);
        DrawChartX();
        XSetWindowBackgroundPixmap(gi.disp, gi.root, gi.pmap);
        XClearWindow(gi.disp, gi.root);
      }
    } else
#endif
#ifndef WIN
      InteractX();    /* Window's up; process commands given to window now. */
    EndX();
#else
    DrawChartX();
#endif
  }
#endif /* ISG */
  return fTrue;
}
#endif /* GRAPH */

/* xscreen.c */