forked from ghewgill/xearth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x11.c
1586 lines (1373 loc) · 41.5 KB
/
x11.c
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
/*
* x11.c
* kirk johnson
* july 1993
*
* Copyright (C) 1989, 1990, 1993-1995, 1999 Kirk Lauritz Johnson
*
* Parts of the source code (as marked) are:
* Copyright (C) 1989, 1990, 1991 by Jim Frost
* Copyright (C) 1992 by Jamie Zawinski <[email protected]>
*
* Permission to use, copy, modify and freely distribute xearth for
* non-commercial and not-for-profit purposes is hereby granted
* without fee, provided that both the above copyright notice and this
* permission notice appear in all copies and in supporting
* documentation.
*
* Unisys Corporation holds worldwide patent rights on the Lempel Zev
* Welch (LZW) compression technique employed in the CompuServe GIF
* image file format as well as in other formats. Unisys has made it
* clear, however, that it does not require licensing or fees to be
* paid for freely distributed, non-commercial applications (such as
* xearth) that employ LZW/GIF technology. Those wishing further
* information about licensing the LZW patent should contact Unisys
* directly at ([email protected]) or by writing to
*
* Unisys Corporation
* Welch Licensing Department
* M/S-C1SW19
* P.O. Box 500
* Blue Bell, PA 19424
*
* The author makes no representations about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*
* THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT
* OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "xearth.h"
#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
#include <X11/Xatom.h>
#include <X11/Xproto.h>
#include <signal.h>
#include "kljcpyrt.h"
#define RETAIN_PROP_NAME "_XSETROOT_ID"
#define MONO_1 (0)
#define MONO_8 (1)
#define COLOR_8 (2)
#define MONO_16 (3)
#define COLOR_16 (4)
#define MONO_32 (5)
#define COLOR_32 (6)
#define LABEL_LEFT_FLUSH (1<<0)
#define LABEL_TOP_FLUSH (1<<1)
static void init_x_general _P((int, char *[]));
static void process_opts _P((void));
static void init_x_colors _P((void));
static void init_x_pixmaps _P((void));
static void init_x_root_window _P((void));
static void init_x_separate_window _P((void));
static void wakeup _P((int));
static int get_bits_per_pixel _P((int));
static XFontStruct *load_x_font _P((Display *, char *));
static void get_proj_type _P((void));
static void get_viewing_position _P((void));
static void get_sun_position _P((void));
static void get_rotation _P((void));
static void get_size _P((void));
static void get_shift _P((void));
static void get_labelpos _P((void));
static void get_geometry _P((void));
static void get_ncolors _P((void));
static void x11_setup _P((void));
static void pack_mono_1 _P((u16or32 *, u_char *));
static void pack_8 _P((u16or32 *, Pixel *, u_char *));
static void pack_16 _P((u16or32 *, Pixel *, u_char *));
static void pack_24 _P((u_char *, u_char *));
static void pack_32 _P((u_char *, u_char *));
static int x11_row _P((u_char *));
static void x11_cleanup _P((void));
static void draw_label _P((Display *));
static void mark_location _P((Display *, MarkerInfo *));
static void draw_outlined_string _P((Display *, Pixmap, Pixel, Pixel,
int, int, char *, int));
static Window GetVRoot _P((Display *));
static void updateProperty _P((Display *, Window, const char *, Atom,
int, int, int));
static void preserveResource _P((Display *, Window));
static void freePrevious _P((Display *, Window));
static int xkill_handler _P((Display *, XErrorEvent *));
static int bpp;
static u16or32 *dith;
static u_char *xbuf;
static int idx;
static XImage *xim;
static Pixmap work_pix;
static Pixmap disp_pix;
static int (*orig_error_handler) _P((Display *, XErrorEvent *));
#ifdef DEBUG
static int frame = 0;
#endif /* DEBUG */
char *progclass;
Widget app_shell;
XtAppContext app_context;
XrmDatabase db;
Display *dsply; /* display connection */
int scrn; /* screen number */
Window root; /* root window */
Window xearth_window; /* xearth window */
Colormap cmap; /* default colormap */
Visual *visl; /* default visual */
int dpth; /* default depth */
Pixel white; /* white pixel */
Pixel black; /* black pixel */
Pixel hlight; /* highlight pixel */
GC gc; /* graphics context */
Pixel *pels; /* allocated colors */
char *font_name; /* text font name */
XFontStruct *font; /* basic text font */
static int do_once; /* only render once? */
static int mono; /* render in mono? */
static int use_root; /* render into root? */
static int window_pos_flag; /* spec'ed window pos? */
static int window_xvalue; /* window x position */
static int window_yvalue; /* window y position */
static int window_gravity; /* window gravity */
static int label_xvalue; /* label x position */
static int label_yvalue; /* label y position */
static int label_orient; /* label orientation */
static char *defaults[] =
{
"*proj: orthographic",
"*pos: sunrel 0 0",
"*rot: 0",
"*shift: 0 0",
"*mag: 1.0",
"*shade: on",
"*label: off",
"*labelpos: -5-5",
"*markers: on",
"*markerfile: built-in",
"*wait: 300",
"*timewarp: 1",
"*day: 100",
"*night: 5",
"*term: 1",
"*twopix: on",
"*ncolors: 64",
"*fork: off",
"*once: off",
"*nice: 0",
"*stars: on",
"*starfreq: 0.002",
"*bigstars: 0",
"*grid: off",
"*grid1: 6",
"*grid2: 15",
"*overlayfile: none",
"*gamma: 1.0",
"*font: variable",
"*title: xearth",
"*iconname: xearth",
NULL
};
static XrmOptionDescRec options[] =
{
{ "-proj", ".proj", XrmoptionSepArg, 0 },
{ "-pos", ".pos", XrmoptionSepArg, 0 },
{ "-rot", ".rot", XrmoptionSepArg, 0 },
{ "-mag", ".mag", XrmoptionSepArg, 0 },
{ "-shade", ".shade", XrmoptionNoArg, "on" },
{ "-noshade", ".shade", XrmoptionNoArg, "off" },
{ "-sunpos", ".sunpos", XrmoptionSepArg, 0 },
{ "-size", ".size", XrmoptionSepArg, 0 },
{ "-shift", ".shift", XrmoptionSepArg, 0 },
{ "-label", ".label", XrmoptionNoArg, "on" },
{ "-nolabel", ".label", XrmoptionNoArg, "off" },
{ "-labelpos", ".labelpos", XrmoptionSepArg, 0 },
{ "-markers", ".markers", XrmoptionNoArg, "on" },
{ "-nomarkers", ".markers", XrmoptionNoArg, "off" },
{ "-markerfile", ".markerfile", XrmoptionSepArg, 0 },
{ "-showmarkers", ".showmarkers", XrmoptionNoArg, "on" },
{ "-overlayfile", ".overlayfile", XrmoptionSepArg, 0 },
{ "-wait", ".wait", XrmoptionSepArg, 0 },
{ "-timewarp", ".timewarp", XrmoptionSepArg, 0 },
{ "-day", ".day", XrmoptionSepArg, 0 },
{ "-night", ".night", XrmoptionSepArg, 0 },
{ "-term", ".term", XrmoptionSepArg, 0 },
{ "-onepix", ".twopix", XrmoptionNoArg, "off" },
{ "-twopix", ".twopix", XrmoptionNoArg, "on" },
{ "-ncolors", ".ncolors", XrmoptionSepArg, 0 },
{ "-fork", ".fork", XrmoptionNoArg, "on" },
{ "-nofork", ".fork", XrmoptionNoArg, "off" },
{ "-once", ".once", XrmoptionNoArg, "on" },
{ "-noonce", ".once", XrmoptionNoArg, "off" },
{ "-nice", ".nice", XrmoptionSepArg, 0 },
{ "-version", ".version", XrmoptionNoArg, "on" },
{ "-stars", ".stars", XrmoptionNoArg, "on" },
{ "-nostars", ".stars", XrmoptionNoArg, "off" },
{ "-starfreq", ".starfreq", XrmoptionSepArg, 0 },
{ "-bigstars", ".bigstars", XrmoptionSepArg, 0 },
{ "-grid", ".grid", XrmoptionNoArg, "on" },
{ "-nogrid", ".grid", XrmoptionNoArg, "off" },
{ "-grid1", ".grid1", XrmoptionSepArg, 0 },
{ "-grid2", ".grid2", XrmoptionSepArg, 0 },
{ "-time", ".time", XrmoptionSepArg, 0 },
{ "-gamma", ".gamma", XrmoptionSepArg, 0 },
{ "-font", ".font", XrmoptionSepArg, 0 },
{ "-mono", ".mono", XrmoptionNoArg, "on" },
{ "-nomono", ".mono", XrmoptionNoArg, "off" },
{ "-root", ".root", XrmoptionNoArg, "on" },
{ "-noroot", ".root", XrmoptionNoArg, "off" },
{ "-geometry", ".geometry", XrmoptionSepArg, 0 },
{ "-title", ".title", XrmoptionSepArg, 0 },
{ "-iconname", ".iconname", XrmoptionSepArg, 0 },
};
void command_line_x(argc, argv)
int argc;
char *argv[];
{
init_x_general(argc, argv);
process_opts();
init_x_colors();
init_x_pixmaps();
font = load_x_font(dsply, font_name);
if (use_root)
init_x_root_window();
else
init_x_separate_window();
}
static void init_x_general(argc, argv)
int argc;
char *argv[];
{
progname = argv[0];
progclass = "XEarth";
app_shell = XtAppInitialize(&app_context, progclass,
options, XtNumber(options),
&argc, argv, defaults, 0, 0);
if (argc > 1) usage(NULL);
dsply = XtDisplay(app_shell);
scrn = DefaultScreen(dsply);
db = XtDatabase(dsply);
XtGetApplicationNameAndClass(dsply, &progname, &progclass);
root = RootWindow(dsply, scrn);
cmap = DefaultColormap(dsply, scrn);
visl = DefaultVisual(dsply, scrn);
dpth = DefaultDepth(dsply, scrn);
wdth = DisplayWidth(dsply, scrn);
hght = DisplayHeight(dsply, scrn);
white = WhitePixel(dsply, scrn);
black = BlackPixel(dsply, scrn);
gc = XCreateGC(dsply, root, 0, NULL);
hlight = white;
XSetState(dsply, gc, white, black, GXcopy, AllPlanes);
bpp = get_bits_per_pixel(dpth);
}
static void process_opts()
{
if (get_boolean_resource("version", "Version"))
version_info(1);
if (get_boolean_resource("showmarkers", "Showmarkers"))
{
markerfile = get_string_resource("markerfile", "Markerfile");
show_marker_info(markerfile);
}
/* process complex resources
*/
get_proj_type();
get_viewing_position();
get_sun_position();
get_size();
get_shift();
get_labelpos();
get_rotation();
get_geometry();
get_ncolors();
/* process simple resources
*/
view_mag = get_float_resource("mag", "Mag");
do_shade = get_boolean_resource("shade", "Shade");
do_label = get_boolean_resource("label", "Label");
do_markers = get_boolean_resource("markers", "Markers");
markerfile = get_string_resource("markerfile", "Markerfile");
wait_time = get_integer_resource("wait", "Wait");
time_warp = get_float_resource("timewarp", "Timewarp");
day = get_integer_resource("day", "Day");
night = get_integer_resource("night", "Night");
terminator = get_integer_resource("term", "Term");
use_two_pixmaps = get_boolean_resource("twopix", "Twopix");
do_fork = get_boolean_resource("fork", "Fork");
do_once = get_boolean_resource("once", "Once");
priority = get_integer_resource("nice", "Nice");
do_stars = get_boolean_resource("stars", "Stars");
star_freq = get_float_resource("starfreq", "Starfreq");
big_stars = get_integer_resource("bigstars", "Bigstars");
do_grid = get_boolean_resource("grid", "Grid");
grid_big = get_integer_resource("grid1", "Grid1");
grid_small = get_integer_resource("grid2", "Grid2");
fixed_time = get_integer_resource("time", "Time");
xgamma = get_float_resource("gamma", "Gamma");
font_name = get_string_resource("font", "Font");
mono = get_boolean_resource("mono", "Mono");
overlayfile[0] = get_string_resource("overlayfile", "Overlayfile");
/* various sanity checks on simple resources
*/
if ((view_rot < -180) || (view_rot > 360))
fatal("viewing rotation must be between -180 and 360");
if (view_mag <= 0)
fatal("viewing magnification must be positive");
if (wait_time < 0)
fatal("arg to -wait must be non-negative");
if (time_warp <= 0)
fatal("arg to -timewarp must be positive");
if ((star_freq < 0) || (star_freq > 1))
fatal("arg to -starfreq must be between 0 and 1");
if ((big_stars < 0) || (big_stars > 100))
fatal("arg to -bigstars must be between 0 and 100");
if (grid_big <= 0)
fatal("arg to -grid1 must be positive");
if (grid_small <= 0)
fatal("arg to -grid2 must be positive");
if ((day > 100) || (day < 0))
fatal("arg to -day must be between 0 and 100");
if ((night > 100) || (night < 0))
fatal("arg to -night must be between 0 and 100");
if ((terminator > 100) || (terminator < 0))
fatal("arg to -term must be between 0 and 100");
if (xgamma <= 0)
fatal("arg to -gamma must be positive");
if (strcmp(overlayfile[0], "none") == 0)
overlayfile[0] = NULL;
/* if we're only rendering once, make sure we don't
* waste memory by allocating two pixmaps
*/
if (do_once)
use_two_pixmaps = 0;
/* if we're working with a one-bit display, force -mono mode
*/
if (dpth == 1)
mono = 1;
}
static void init_x_colors()
{
int i;
XColor xc, junk;
u_char *tmp;
double inv_xgamma;
if (mono)
{
mono_dither_setup();
pels = (Pixel *) malloc((unsigned) sizeof(Pixel) * 2);
assert(pels != NULL);
pels[0] = black;
pels[1] = white;
}
else
{
if (XAllocNamedColor(dsply, cmap, "red", &xc, &junk) != 0)
hlight = xc.pixel;
dither_setup(num_colors);
pels = (Pixel *) malloc((unsigned) sizeof(Pixel) * dither_ncolors);
assert(pels != NULL);
tmp = dither_colormap;
inv_xgamma = 1.0 / xgamma;
for (i=0; i<dither_ncolors; i++)
{
xc.red = ((1<<16)-1) * pow(((double) tmp[0] / 255), inv_xgamma);
xc.green = ((1<<16)-1) * pow(((double) tmp[1] / 255), inv_xgamma);
xc.blue = ((1<<16)-1) * pow(((double) tmp[2] / 255), inv_xgamma);
if (XAllocColor(dsply, cmap, &xc) == 0)
fatal("unable to allocate enough colors");
pels[i] = xc.pixel;
tmp += 3;
}
}
}
static void init_x_pixmaps()
{
work_pix = XCreatePixmap(dsply, root, (unsigned) wdth,
(unsigned) hght, (unsigned) dpth);
if (use_two_pixmaps)
disp_pix = XCreatePixmap(dsply, root, (unsigned) wdth,
(unsigned) hght, (unsigned) dpth);
}
static void init_x_root_window()
{
xearth_window = GetVRoot(dsply);
/* try to free any resources retained by any previous clients that
* scribbled in the root window (also deletes the _XSETROOT_ID
* property from the root window, if it was there)
*/
freePrevious(dsply, xearth_window);
/* 18 may 1994
*
* setting the _XSETROOT_ID property is dangerous if xearth might be
* killed by other means (e.g., from the shell), because some other
* client might allocate a resource with the same resource ID that
* xearth had stored in the _XSETROOT_ID property, so subsequent
* attempts to free any resources retained by a client that had
* scribbled on the root window via XKillClient() may end up killing
* the wrong client.
*
* this possibility could be eliminated by setting the closedown
* mode for the display connection to RetainPermanent, but this
* seemed to be causing core dumps in an R5pl26 server -- i submitted
* a bug report to the X consortium about this. i _think_ the server
* core dumps were related to the fact that xearth can sleep for a
* _long_ time between protocol requests, perhaps longer than it
* takes for one server to die (e.g., when somebody logs out) and a
* new server to be restarted, and somehow exercising the display
* connection from the old server was causing the new one to crash?
*
* possible fixes:
*
* - replace the big sleep() with a loop that interleaves sleep(1)
* and, say, calls to XNoOp() to test the display connection;
* presumably one second is short enough to avoid the possibility
* of one server dying and another restarting before a call to
* XNoOp() catches the fact that the connection to the old server
* died.
*
* - use RetainTemporary mode instead of RetainPermanent? need to
* check the X documentation and figure out exactly what this
* would mean.
*
* it would be nice to install the _XSETROOT_ID property so xearth
* interoperates gracefully with other things that try to scribble
* on the root window (e.g., xsetroot, xloadimage, xv), but until i
* figure out a fix to the problems described above, probably best
* not to bother.
*/
/* preserveResource(dsply, xearth_window); */
}
static void init_x_separate_window()
{
XSizeHints *xsh;
char *title;
char *iname;
xearth_window = XCreateSimpleWindow(dsply,
root,
window_xvalue,
window_yvalue,
wdth,
hght,
DefaultBorderWidth,
white,
black);
xsh = XAllocSizeHints();
xsh->width = wdth;
xsh->height = hght;
xsh->min_width = wdth;
xsh->min_height = hght;
xsh->max_width = wdth;
xsh->max_height = hght;
xsh->base_width = wdth;
xsh->base_height = hght;
xsh->flags = (PSize|PMinSize|PMaxSize|PBaseSize);
if (window_pos_flag)
{
xsh->x = window_xvalue;
xsh->y = window_yvalue;
xsh->win_gravity = window_gravity;
xsh->flags |= (USPosition|PWinGravity);
}
title = get_string_resource("title", "Title");
iname = get_string_resource("iconname", "Iconname");
if ((title == NULL) || (iname == NULL))
fatal("title or iconname is NULL (this shouldn't happen)");
XSetWMNormalHints(dsply, xearth_window, xsh);
XStoreName(dsply, xearth_window, title);
XSetIconName(dsply, xearth_window, iname);
XMapRaised(dsply, xearth_window);
XSync(dsply, False);
XFree((char *) xsh);
free(title);
free(iname);
}
void x11_output()
{
while (1)
{
compute_positions();
/* if we were really clever, we'd only
* do this if the position has changed
*/
scan_map();
do_dots();
/* for now, go ahead and reload the marker info every time
* we redraw, but maybe change this in the future?
*/
load_marker_info(markerfile);
x11_setup();
render(x11_row);
x11_cleanup();
if (do_once)
{
if (use_root)
preserveResource(dsply, xearth_window);
XSync(dsply, True);
return;
}
/* schedule an alarm for wait_time seconds and pause. alarm() and
* pause() are used instead of sleep so that if xearth is sent a
* SIGSTOP and SIGCONT separated by more than wait_time, it will
* refresh the screen as soon as the SIGCONT is received. this
* facilitates graceful interaction with things like FvwmBacker.
* (thanks to Richard Everson for passing this along.)
*/
signal(SIGALRM, wakeup);
signal(SIGCONT, wakeup);
if (wait_time > 0)
{
/* only do the alarm()/pause() stuff if wait_time is non-zero,
* else alarm() will not behave as desired.
*/
alarm(wait_time);
pause();
}
}
}
/* no-op signal handler for catching SIGALRM and SIGCONT
* (used to wake up from pause() system call)
*/
static void wakeup(int junk)
{
/* nothing */
}
/* determine bits_per_pixel value for pixmaps of specified depth
*/
static int get_bits_per_pixel(depth)
int depth;
{
int i;
int cnt;
XPixmapFormatValues *pmf;
int rslt;
pmf = XListPixmapFormats(dsply, &cnt);
if (pmf == NULL)
fatal("unable to get pixmap format list");
rslt = 0;
for (i=0; i<cnt; i++)
if (pmf[i].depth == depth)
{
rslt = pmf[i].bits_per_pixel;
break;
}
if (rslt == 0)
fatal("unable to determine pixmap format");
XFree(pmf);
return rslt;
}
static XFontStruct *load_x_font(dpy, fontname)
Display *dpy;
char *fontname;
{
XFontStruct *rslt;
rslt = XLoadQueryFont(dpy, fontname);
if (rslt == NULL)
{
rslt = XQueryFont(dpy, XGContextFromGC(gc));
if (rslt == NULL)
fatal("completely unable to load fonts");
else
warning("unable to load font, reverting to default");
}
else
{
XSetFont(dpy, gc, rslt->fid);
}
return rslt;
}
/* fetch and decode 'proj' resource (projection type)
*/
static void get_proj_type()
{
char *res;
res = get_string_resource("proj", "Proj");
if (res != NULL)
{
decode_proj_type(res);
free(res);
}
}
/* fetch and decode 'pos' resource (viewing position specifier)
*/
static void get_viewing_position()
{
char *res;
res = get_string_resource("pos", "Pos");
if (res != NULL)
{
decode_viewing_pos(res);
free(res);
}
}
/* fetch and decode 'sunpos' resource (sun position specifier)
*/
static void get_sun_position()
{
char *res;
res = get_string_resource("sunpos", "Sunpos");
if (res != NULL)
{
decode_sun_pos(res);
free(res);
}
}
/* fetch and decode 'rot' resource (rotation specifier)
*/
static void get_rotation()
{
char *res;
res = get_string_resource("rot", "Rotation");
if (res != NULL)
{
decode_rotation(res);
free(res);
}
}
/* fetch and decode 'size' resource (size specifier)
*/
static void get_size()
{
char *res;
res = get_string_resource("size", "Size");
if (res != NULL)
{
decode_size(res);
free(res);
}
}
/* fetch and decode 'shift' resource (shift specifier)
*/
static void get_shift()
{
char *res;
res = get_string_resource("shift", "Shift");
if (res != NULL)
{
decode_shift(res);
free(res);
}
}
/* fetch and decode 'ncolors' resource (color depth)
*/
static void get_ncolors()
{
char *res;
res = get_string_resource("ncolors", "Ncolors");
if (res != NULL)
{
decode_colors(res);
free(res);
}
}
/* fetch and decode 'labelpos' resource (label position)
*/
static void get_labelpos()
{
char *res;
int mask;
int x, y;
unsigned w, h;
/* it's somewhat brute-force ugly to hard-code these here,
* duplicating information contained in defaults[], but such it is.
*/
label_orient = 0;
label_xvalue = wdth - 5;
label_yvalue = hght - 5;
res = get_string_resource("labelpos", "Labelpos");
if (res != NULL)
{
mask = XParseGeometry(res, &x, &y, &w, &h);
if (mask & (WidthValue | HeightValue))
warning("width and height ignored in label position");
if ((mask & XValue) && (mask & YValue))
{
label_xvalue = x;
label_yvalue = y;
if ((mask & XNegative) == 0)
label_orient |= LABEL_LEFT_FLUSH;
if ((mask & YNegative) == 0)
label_orient |= LABEL_TOP_FLUSH;
}
else
{
warning("label position must specify x and y offsets");
}
free(res);
}
}
/* fetch and decode 'root' and 'geometry' resource (whether to render
* into root or separate window; if separate window, position of that
* window) [this is pretty ugly code, but it gets the job done ...]
*/
static void get_geometry()
{
int check_geom;
char *res;
int mask;
int x, y;
unsigned int w, h;
res = get_string_resource("root", "Root");
if (res != NULL)
{
free(res);
if (get_boolean_resource("root", "Root"))
{
/* user specified -root; render into the root window
* (ignore any -geometry, if provided)
*/
use_root = 1;
check_geom = 0;
}
else
{
/* user specified -noroot; render into separate window
*/
use_root = 0;
check_geom = 1;
}
}
else
{
/* user specified neither -root nor -noroot; if -geometry is
* provided, render into separate window, else render into root
*/
use_root = 1;
check_geom = 1;
}
/* if check_geom isn't set, nothing more to do
*/
if (check_geom == 0) return;
/* look for -geometry and try to make sense of it
*/
res = get_string_resource("geometry", "Geometry");
if (res != NULL)
{
/* if -geometry is specified, assume -noroot and set default width
* and height (which get overridden by -geometry width and height,
* if provided)
*/
use_root = 0;
wdth = DefaultWdthHght;
hght = DefaultWdthHght;
mask = XParseGeometry(res, &x, &y, &w, &h);
/* extract width and height information
*/
if ((mask & WidthValue) && (mask & HeightValue))
{
wdth = w;
hght = h;
}
else
{
if ((mask & WidthValue) || (mask & HeightValue))
warning("geometry must specify both width and height");
}
/* extract position information
*/
if ((mask & XValue) && (mask & YValue))
{
window_pos_flag = 1;
window_xvalue = x;
window_yvalue = y;
if (mask & XNegative)
{
window_xvalue += (DisplayWidth(dsply, scrn) - wdth);
window_xvalue -= (2 * DefaultBorderWidth);
}
if (mask & YNegative)
{
window_yvalue += (DisplayHeight(dsply, scrn) - hght);
window_yvalue -= (2 * DefaultBorderWidth);
}
if (mask & XNegative)
if (mask & YNegative)
window_gravity = SouthEastGravity;
else
window_gravity = NorthEastGravity;
else
if (mask & YNegative)
window_gravity = SouthWestGravity;
else
window_gravity = NorthWestGravity;
}
else
{
if ((mask & XValue) || (mask & YValue))
warning("geometry must specify both x and y offsets");
window_pos_flag = 0;
window_xvalue = 0;
window_yvalue = 0;
window_gravity = 0;
}
free(res);
}
else if (use_root == 0)
{
/* if -noroot was specified but no -geometry was provided, assume
* defaults
*/
wdth = DefaultWdthHght;
hght = DefaultWdthHght;
window_pos_flag = 0;
window_xvalue = 0;
window_yvalue = 0;
}
}
static void x11_setup()
{
unsigned dith_size;
unsigned xbuf_size;
switch (bpp)
{
case 1:
dith_size = wdth + 7;
break;
case 8:
case 16:
case 24:
case 32:
dith_size = wdth;
break;
default:
dith_size = 0; /* keep lint happy */
fprintf(stderr,
"xearth %s: fatal - unsupported pixmap format (%d bits/pixel)\n",
VersionString, bpp);
exit(1);
}
xbuf_size = (dith_size * bpp) >> 3;
dith = (u16or32 *) malloc((unsigned) sizeof(u16or32) * dith_size);
assert(dith != NULL);
xbuf = (u_char *) malloc((unsigned) xbuf_size);
assert(xbuf != NULL);
xim = XCreateImage(dsply, visl, (unsigned) dpth, ZPixmap, 0,
(char *) xbuf, (unsigned) wdth, 1, 8,
xbuf_size);
if (xim->bits_per_pixel != bpp)
{
fprintf(stderr,
"xearth %s: fatal - unexpected bits/pixel for depth %d\n",
VersionString, dpth);
fprintf(stderr,
" (expected %d bits/pixel, actual value is %d)\n",
bpp, xim->bits_per_pixel);
exit(1);
}
if (bpp == 1)
{
/* force MSBFirst bitmap_bit_order and byte_order
*/
xim->bitmap_bit_order = MSBFirst;
xim->byte_order = MSBFirst;
}
idx = 0;
}
/* pack pixels into ximage format (assuming bits_per_pixel == 1,
* bitmap_bit_order == MSBFirst, and byte_order == MSBFirst)
*/