-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxplot.c
3892 lines (3497 loc) · 103 KB
/
xplot.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
/*
This software is being provided to you, the LICENSEE, by the
Massachusetts Institute of Technology (M.I.T.) under the following
license. By obtaining, using and/or copying this software, you agree
that you have read, understood, and will comply with these terms and
conditions:
Permission to use, copy, modify and distribute, including the right to
grant others the right to distribute at any tier, this software and
its documentation for any purpose and without fee or royalty is hereby
granted, provided that you agree to comply with the following
copyright notice and statements, including the disclaimer, and that
the same appear on ALL copies of the software and documentation,
including modifications that you make for internal use or for
distribution:
Copyright 1992,1993 by the Massachusetts Institute of Technology.
All rights reserved.
THIS SOFTWARE IS PROVIDED "AS IS", AND M.I.T. MAKES NO REPRESENTATIONS
OR WARRANTIES, EXPRESS OR IMPLIED. By way of example, but not
limitation, M.I.T. MAKES NO REPRESENTATIONS OR WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE
OF THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD
PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
The name of the Massachusetts Institute of Technology or M.I.T. may
NOT be used in advertising or publicity pertaining to distribution of
the software. Title to copyright in this software and any associated
documentation shall at all times remain with M.I.T., and USER agrees
to preserve same.
*/
/*
xplot -- Written by Timothy J Shepard.
Some features added by Andrew Heybey.
Some features added by Greg Troxel.
Some improved color support and a few other tweaks were contributed
by Shawn D. Ostermann (@cs.ohiou.edu).
*/
#include "xplot.h"
#include <stdio.h>
#ifdef HAVE_LIBX11
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#else
#error xplot requires x11
#endif
#include <ctype.h>
void panic(char *s)
{
fprintf(stderr,"fatal error: %s\n",s);
(void) fflush(stderr);
abort();
exit(1);
}
void fatalerror(char *s)
{
fprintf(stderr,"fatal error: %s\n",s);
(void) fflush(stderr);
exit(1);
}
int get_input();
void emit_PS();
#define min(x,y) (((x)<(y))?(x):(y))
#define max(x,y) (((x)>(y))?(x):(y))
#define abs(x) (((x)>0)?(x):(-(x)))
typedef enum {CENTERED, ABOVE, BELOW, TO_THE_LEFT, TO_THE_RIGHT} position;
/* dXPoint is just like Xpoint but uses doubles instead instead of shorts.
*/
typedef struct { double x,y; } dXPoint;
/* lXPoint is just like Xpoint but uses longs instead instead of shorts.
We need the extra bits to do Postscript stuff in emit_PS() below.
*/
typedef struct { int x,y; } lXPoint;
static dXPoint dXPoint_from_lXPoint(lXPoint lxp)
{
dXPoint r;
r.x = (double) lxp.x;
r.y = (double) lxp.y;
return r;
}
static lXPoint lXPoint_from_dXPoint(dXPoint dxp)
{
lXPoint r;
r.x = (int) rint(dxp.x);
r.y = (int) rint(dxp.y);
return r;
}
/* xpcolor_t is a short so it fits in the command struct (60 bytes =
64 - malloc overhead (on a mips)
*/
#define NCOLORS 10
char *ColorNames[NCOLORS] =
{
"white", "green", "red", "blue", "yellow", "purple", "orange", "magenta", "pink", "gray20"
};
char *GrayPSrep[NCOLORS] =
{
"0 setgray", /* white */
".3 setgray", /* green */
".5 setgray", /* red */
".7 setgray", /* blue */
".9 setgray", /* yellow infreq */
".6 setgray", /* purple infreq */
".8 setgray",
".4 setgray",
".95 setgray",
".2 setgray" /* 20% gray */
};
char *ColorPSrep[NCOLORS] =
{
"0 setgray", "0 1 0 setrgbcolor", "1 0 0 setrgbcolor", "0 0 1 setrgbcolor", "1 1 0 setrgbcolor", "0 1 1 setrgbcolor", "1 .5 0 setrgbcolor", "0 .5 1 setrgbcolor", "1 .5 .5 setrgbcolor", "0.2 0.2 0.2 setrgbcolor"
};
int NColors = NCOLORS;
typedef short xpcolor_t;
typedef struct command_struct {
struct command_struct *next;
enum plot_command_type { X, DOT, PLUS, BOX, DIAMOND,
UTICK, DTICK, LTICK, RTICK, HTICK, VTICK,
UARROW, DARROW, LARROW, RARROW,
INVISIBLE, LINE, DLINE,
TEXT, TITLE, XLABEL, YLABEL } type:5;
position position:3;
bool needs_redraw:1;
bool mapped:1;
bool decoration:1;
xpcolor_t color;
#ifdef WINDOW_COORDS_IN_COMMAND_STRUCT
dXPoint a,b;
#endif
coord xa, ya;
coord xb, yb;
char *text;
} command;
#define NUMVIEWS 30
#define pl_x_left pl->x_left[pl->viewno]
#define pl_x_right pl->x_right[pl->viewno]
#define pl_y_top pl->y_top[pl->viewno]
#define pl_y_bottom pl->y_bottom[pl->viewno]
#ifndef WINDOW_COORDS_IN_COMMAND_STRUCT
#define pspl_x_left pspl.x_left[pspl.viewno]
#define pspl_x_right pspl.x_right[pspl.viewno]
#define pspl_y_top pspl.y_top[pspl.viewno]
#define pspl_y_bottom pspl.y_bottom[pspl.viewno]
#endif
typedef struct plotter {
struct plotter *next;
command *commands;
coord_type x_type;
coord_type y_type;
char *x_units;
char *y_units;
double aspect_ratio; /* 0.0 unless specified */
int viewno;
coord x_left[NUMVIEWS];
coord y_bottom[NUMVIEWS];
coord x_right[NUMVIEWS];
coord y_top[NUMVIEWS];
dXPoint origin;
dXPoint size;
dXPoint mainsize;
Display *dpy;
Screen *screen;
int numtiles;
int tileno;
Window win;
XSizeHints xsh;
int visibility;
int size_changed;
int new_expose;
int clean;
int is_hidden[NCOLORS];
GC gcs[NCOLORS];
GC decgc;
GC xorgc;
GC bacgc;
XFontStruct *font_struct;
XGCValues gcv;
enum plstate {NORMAL, SLAVE,
ZOOM, HZOOM, VZOOM,
DRAG, HDRAG, VDRAG,
EXITING, PRINTING, FIGING, THINFIGING,
ADVANCING, BACKINGUP,
WEDGED} state;
struct plotter *master; /* pointer to master when in SLAVE state */
enum plstate master_state; /* state of master when in SLAVE state */
int buttonsdown;
lXPoint raw_dragstart;
lXPoint dragstart;
lXPoint dragend;
lXPoint pointer;
lXPoint pointer_marks;
bool pointer_marks_on_screen;
Atom xplot_nagle_atom;
bool slave_draw_in_progress;
bool slave_motion_pending;
lXPoint master_pointer;
xpcolor_t default_color;
xpcolor_t current_color;
XColor foreground_color;
XColor background_color;
bool thick;
} *PLOTTER;
PLOTTER the_plotter_list;
int option_thick;
int option_mono;
int global_argc;
char **global_argv;
unsigned int keycode_up, keycode_down, keycode_left, keycode_right;
unsigned int color_keycode[NCOLORS];
command *new_command(struct plotter *pl)
{
command *c;
c = (command *) malloc(sizeof(command));
if (c == 0) fatalerror("malloc returned null");
c->decoration = FALSE;
#ifdef WINDOW_COORDS_IN_COMMAND_STRUCT
c->a.x = 0;
c->a.y = 0;
c->b.x = 0;
c->b.y = 0;
#endif
c->color = pl->current_color;
c->xa.d = 0.0;
c->ya.d = 0.0;
c->xb.d = 0.0;
c->yb.d = 0.0;
c->next = pl->commands;
pl->commands = c;
return c;
}
void free_command(command *c)
{
switch(c->type) {
case TEXT:
free(c->text);
break;
default:
break;
}
free((char *)c);
}
dXPoint tomain(struct plotter *pl, dXPoint xp)
{
dXPoint r;
r.x = xp.x + pl->origin.x;
r.y = xp.y + pl->origin.y;
return r;
}
dXPoint tosub(struct plotter *pl, dXPoint xp)
{
dXPoint r;
r.x = xp.x - pl->origin.x;
r.y = xp.y - pl->origin.y;
return r;
}
/*
0 1 2
3 4 5
6 7 8
*/
enum in_p { NO, YES, MAYBE } in_rect_table[9][9] = {
{ NO, NO, NO, NO, YES, MAYBE, NO, MAYBE, MAYBE },
{ NO, NO, NO, MAYBE, YES, MAYBE, MAYBE, YES, MAYBE },
{ NO, NO, NO, MAYBE, YES, NO, MAYBE, MAYBE, NO },
{ NO, MAYBE, MAYBE, NO, YES, YES, NO, MAYBE, MAYBE },
{ YES, YES, YES, YES, YES, YES, YES, YES, YES },
{ MAYBE, MAYBE, NO, YES, YES, NO, MAYBE, MAYBE, NO },
{ NO, MAYBE, MAYBE, NO, YES, MAYBE, NO, NO, NO },
{ MAYBE, YES, MAYBE, MAYBE, YES, MAYBE, NO, NO, NO },
{ MAYBE, MAYBE, NO, MAYBE, YES, NO, NO, NO, NO }
};
static void compute_window_coords(struct plotter *pl, command *com)
{
int loc1;
int loc2;
if (com->type == TITLE
|| com->type == XLABEL
|| com->type == YLABEL
) {
/* complete special case */
com->mapped = TRUE;
com->needs_redraw = TRUE;
return;
}
if (com->type == INVISIBLE) {
com->mapped = FALSE;
com->needs_redraw = FALSE;
return;
}
#define ccmp(ctype, c1, c2, op) (cmp_coord(ctype, c1, c2) op 0)
#define xcmp(xa, xb, op) ccmp(pl->x_type, xa, xb, op)
#define ycmp(ya, yb, op) ccmp(pl->y_type, ya, yb, op)
if (xcmp(com->xa, pl_x_left, <))
if (ycmp(com->ya, pl_y_top, >)) loc1 = 0;
else
if (ycmp(com->ya, pl_y_bottom, <)) loc1 = 6;
else loc1 = 3;
else
if (xcmp(com->xa, pl_x_right, >))
if (ycmp(com->ya, pl_y_top, >)) loc1 = 2;
else
if (ycmp(com->ya, pl_y_bottom, <)) loc1 = 8;
else loc1 = 5;
else
if (ycmp(com->ya, pl_y_top, >)) loc1 = 1;
else
if (ycmp(com->ya, pl_y_bottom, <)) loc1 = 7;
else loc1 = 4;
switch(com->type) {
default:
panic("compute_window_coords: unknown command type");
case X:
case DOT:
case PLUS:
case BOX:
case DIAMOND:
case UTICK:
case DTICK:
case LTICK:
case RTICK:
case HTICK:
case VTICK:
case UARROW:
case DARROW:
case LARROW:
case RARROW:
case TEXT:
loc2 = loc1;
break;
case DLINE:
case LINE:
if (xcmp(com->xb, pl_x_left, <))
if (ycmp(com->yb, pl_y_top, >)) loc2 = 0;
else
if (ycmp(com->yb, pl_y_bottom, <)) loc2 = 6;
else loc2 = 3;
else
if (xcmp(com->xb, pl_x_right, >))
if (ycmp(com->yb, pl_y_top, >)) loc2 = 2;
else
if (ycmp(com->yb, pl_y_bottom, <)) loc2 = 8;
else loc2 = 5;
else
if (ycmp(com->yb, pl_y_top, >)) loc2 = 1;
else
if (ycmp(com->yb, pl_y_bottom, <)) loc2 = 7;
else loc2 = 4;
break;
}
com->mapped = FALSE;
switch(in_rect_table[loc1][loc2]) {
case NO:
return;
case MAYBE:
case YES:
break;
default:
panic("compute_window_coords: unknown value from table");
}
#ifdef WINDOW_COORDS_IN_COMMAND_STRUCT
com->a.x = map_coord(pl->x_type, pl_x_left, pl_x_right, pl->size.x,
com->xa);
com->a.y = (pl->size.y - 1) -
map_coord(pl->y_type, pl_y_bottom, pl_y_top, pl->size.y,
com->ya);
switch(com->type) {
case X:
case DOT:
case PLUS:
case BOX:
case DIAMOND:
case UTICK:
case DTICK:
case LTICK:
case RTICK:
case HTICK:
case VTICK:
case RARROW:
case UARROW:
case DARROW:
case LARROW:
case TEXT:
break;
case DLINE:
case LINE:
com->b.x = map_coord(pl->x_type, pl_x_left, pl_x_right, pl->size.x,
com->xb);
com->b.y = (pl->size.y - 1) -
map_coord(pl->y_type, pl_y_bottom, pl_y_top, pl->size.y,
com->yb);
break;
default:
panic("compute_window_coords: unknown command type");
}
com->a = tomain(pl,com->a);
com->b = tomain(pl,com->b);
#endif
com->mapped = TRUE;
com->needs_redraw = TRUE;
}
static struct plotter *the_plotter_we_are_working_on; /* C really looses */
char *append_strings_with_space_freeing_first(char *s1, char *s2)
{
int len2,len;
char *r;
len2 = strlen(s2);
if (len2 == 0)
return s1;
len = strlen(s1) + 1 + len2 + 1;
r = (char *) malloc(len);
if (r == 0) fatalerror("malloc returned null");
strcpy(r, s1);
strcat(r, " ");
strcat(r, s2);
free(s1);
return r;
}
void doxtick(coord c,int labelflag)
{
struct plotter *pl = the_plotter_we_are_working_on;
command *com = new_command(pl);
com->decoration = TRUE;
com->type = DTICK;
com->xa = c;
com->ya = pl_y_bottom;
if (labelflag) {
com = new_command(pl);
com->decoration = TRUE;
com->type = TEXT;
com->position = BELOW;
com->xa = c;
com->ya = pl_y_bottom;
com->text = unparse_coord(pl->x_type, c);
com->text = append_strings_with_space_freeing_first(com->text,
pl->x_units);
}
}
void doytick(coord c,int labelflag)
{
struct plotter *pl = the_plotter_we_are_working_on;
command *com = new_command(pl);
com->decoration = TRUE;
com->type = LTICK;
com->xa = pl_x_left;
com->ya = c;
if (labelflag) {
com = new_command(pl);
com->decoration = TRUE;
com->type = TEXT;
com->position = TO_THE_LEFT;
com->xa = pl_x_left;
com->ya = c;
com->text = unparse_coord(pl->y_type, c);
com->text = append_strings_with_space_freeing_first(com->text,
pl->y_units);
}
}
void axis(struct plotter *pl)
{
command *com;
com = new_command(pl);
com->decoration = TRUE;
com->type = LINE;
com->xa = pl_x_left;
com->ya = pl_y_top;
com->xb = pl_x_left;
com->yb = pl_y_bottom;
com = new_command(pl);
com->decoration = TRUE;
com->type = LINE;
com->xa = pl_x_left;
com->ya = pl_y_bottom;
com->xb = pl_x_right;
com->yb = pl_y_bottom;
the_plotter_we_are_working_on = pl;
cticks(pl->x_type, pl_x_left, pl_x_right, 1, doxtick);
cticks(pl->y_type, pl_y_bottom, pl_y_top, 0, doytick);
}
void size_window(struct plotter *pl)
{
command *c;
pl->origin.x = 70;
pl->origin.y = 30;
pl->size.x = pl->mainsize.x - pl->origin.x - 10;
pl->size.y = pl->mainsize.y - pl->origin.y - 30;
/*************** CAVEAT abstraction violation in emit_PS() code below,
caused mostly by hardwired constants above. */
while (pl->commands && pl->commands->decoration) {
c = pl->commands;
pl->commands = pl->commands->next;
free_command(c);
}
axis(pl);
for (c = pl->commands; c != NULL; c = c->next)
compute_window_coords(pl, c);
}
lXPoint detent(struct plotter *pl, lXPoint xp)
{
dXPoint dr;
dXPoint dxp;
dxp = dXPoint_from_lXPoint(xp);
dxp = tosub(pl, dxp);
dr.x = map_coord(pl->x_type, pl_x_left, pl_x_right, pl->size.x,
unmap_coord(pl->x_type, pl_x_left, pl_x_right,
pl->size.x, dxp.x));
dr.y = map_coord(pl->y_type, pl_y_bottom, pl_y_top, pl->size.y,
unmap_coord(pl->y_type, pl_y_bottom, pl_y_top,
pl->size.y, dxp.y));
dr = tomain(pl,dr);
return lXPoint_from_dXPoint(dr);
}
lXPoint map_pl_pl(struct plotter *pl_from, struct plotter *pl_to, lXPoint xp)
{
dXPoint dr;
dXPoint dxp;
coord x;
coord y;
#define pl pl_from
dxp = dXPoint_from_lXPoint(xp);
dxp = tosub(pl, dxp);
x = unmap_coord(pl->x_type, pl_x_left, pl_x_right, pl->size.x, dxp.x);
y = unmap_coord(pl->y_type, pl_y_bottom, pl_y_top, pl->size.y, dxp.y);
#undef pl
#define pl pl_to
dr.x = map_coord(pl->x_type, pl_x_left, pl_x_right, pl->size.x, x);
dr.y = map_coord(pl->y_type, pl_y_bottom, pl_y_top, pl->size.y, y);
dr = tomain(pl,dr);
#undef pl
return lXPoint_from_dXPoint(dr);
}
#ifndef ScreenNumberOfScreen
/* X.V11R3 is/was missing this functionality.
X Consortium people promissed that this will be
done right and defined as a macro eventually.
For now, kludge in a hopefully forward compatible way.
*/
#define ScreenNumberOfScreen X_ScreenNumberOfScreen
int X_ScreenNumberOfScreen(Screen *scr)
{
Display *dpy;
int i;
Screen *iscr;
dpy = DisplayOfScreen(scr);
for (i=0;i<XScreenCount(dpy);i++) {
iscr = ScreenOfDisplay(dpy,i);
if (iscr == scr)
return i;
}
/* oops */
panic("XScreenNumberOfScreen: could not find screen number");
return 0;
}
#endif
/*
* numwins, nth: specifies how many windows and which one (zero-based)
* are desired. This is a hint to new_plotter, which could check resources.
* If tiling, we stack multiple windows vertically and make them smaller.
*/
void new_plotter(FILE *fp, Display *dpy, int numtiles, int tileno, int lineno)
{
int r = 0;
PLOTTER pl;
do {
pl = (PLOTTER) malloc(sizeof(*pl));
if (pl == 0) fatalerror("malloc returned null");
pl->next = the_plotter_list;
the_plotter_list = pl;
pl->dpy = dpy;
pl->screen = XDefaultScreenOfDisplay(pl->dpy);
pl->numtiles = numtiles;
pl->tileno = tileno;
pl->win = 0;
pl->aspect_ratio = 0.0;
pl->viewno = 0;
pl->commands = NULL;
pl->x_type = INT;
pl->y_type = INT;
pl->x_units = "";
pl->y_units = "";
pl->mainsize.x = 0;
pl->mainsize.y = 0;
pl->size_changed = 0;
pl->size.x = 0;
pl->size.y = 0;
pl->origin.x = 0;
pl->origin.y = 0;
pl->state = NORMAL;
pl->raw_dragstart.x = 0;
pl->raw_dragstart.y = 0;
pl->dragstart.x = 0;
pl->dragstart.y = 0;
pl->dragend.x = 0;
pl->dragend.y = 0;
pl->pointer.x = 0;
pl->pointer.y = 0;
pl->pointer_marks.x = 0;
pl->pointer_marks.y = 0;
pl->pointer_marks_on_screen = FALSE;
pl->xplot_nagle_atom = None;
pl->slave_draw_in_progress = FALSE;
pl->slave_motion_pending = FALSE;
pl->master_pointer.x = 0;
pl->master_pointer.y = 0;
pl->buttonsdown = 0;
pl->new_expose = 0;
pl->clean = 0;
pl->default_color = -1;
pl->current_color = -1;
pl->thick = option_thick? TRUE: FALSE;
r = get_input(fp, dpy, lineno, pl);
lineno = r;
} while (r > 0);
}
void display_plotter(PLOTTER pl)
{
XSetWindowAttributes attr;
Window rootwindow;
if (pl->win != 0) {
fprintf(stderr,
"display_plotter called for already-displayed plotter\n");
return;
}
rootwindow = XRootWindowOfScreen(pl->screen);
/* set up foreground/background colors */
{
Colormap default_cmap = DefaultColormap(pl->dpy, DefaultScreen(pl->dpy));
char *foreground_color_name;
char *background_color_name;
XColor exact_return;
int i;
foreground_color_name = XGetDefault(pl->dpy, global_argv[0], "foreground");
if (!foreground_color_name) foreground_color_name = "white";
i = XAllocNamedColor(pl->dpy, default_cmap, foreground_color_name,
&exact_return, &pl->foreground_color);
if (i < 0)
{
fprintf(stderr, "XAllocNamedColor failed for %s: %d\n",
foreground_color_name, i);
return;
}
#if 0
ColorNames[0] = strdup(foreground_color_name);
#else
ColorNames[0] = (char *) malloc(strlen(foreground_color_name) + 1);
if (ColorNames[0] == NULL) fatalerror("malloc returned null");
strcpy(ColorNames[0], foreground_color_name);
#endif
background_color_name = XGetDefault(pl->dpy, global_argv[0], "background");
if (!background_color_name) background_color_name = "black";
i = XAllocNamedColor(pl->dpy, default_cmap, background_color_name,
&exact_return, &pl->background_color);
if (i < 0)
{
fprintf(stderr, "XAllocNamedColor failed for %s: %d\n",
background_color_name, i);
return;
}
}
attr.background_pixel = pl->background_color.pixel;
attr.border_pixel = pl->foreground_color.pixel;
attr.event_mask = ButtonReleaseMask|ButtonPressMask|ExposureMask|
EnterWindowMask|LeaveWindowMask|PointerMotionMask|PointerMotionHintMask|
StructureNotifyMask|VisibilityChangeMask|KeyPressMask;
#if 1
attr.cursor = XCreateFontCursor(pl->dpy, XC_crosshair);
#else
attr.cursor = XCreateFontCursor(pl->dpy, XC_tcross);
#endif
{
XColor blk,wht;
blk.red = blk.green = blk.blue = 0;
wht.red = wht.green = wht.blue = -1;
wht.flags = blk.flags = DoRed|DoGreen|DoBlue;
XRecolorCursor(pl->dpy, attr.cursor, &wht, &blk);
}
{
char *geom = XGetDefault (pl->dpy, global_argv[0], "geometry");
int x = 10, y = 70, borderw = 1;
unsigned int width = 400, height = 400;
int flags = 0;
int i;
for (i = 1 ; i < global_argc ; i++) {
if (strcmp("-geometry", global_argv[i]) == 0) {
if (i+1 < global_argc) {
geom = global_argv[i+1];
break;
} else {
static int virgin = 1;
if (virgin) {
fprintf(stderr, "-geometry on command line without any following argument\n");
virgin = 0;
}
}
}
}
if (geom)
{
flags = XParseGeometry (geom, &x, &y, &width, &height);
if (flags & XValue)
{
if (flags & XNegative)
x += DisplayWidth (pl->dpy, ScreenNumberOfScreen(pl->screen))
- width - borderw*2;
else
x += borderw;
}
if (flags & YValue)
{
if (flags & YNegative)
y += DisplayHeight (pl->dpy, ScreenNumberOfScreen(pl->screen))
- height - borderw*2;
else
y += borderw;
}
}
if (pl->numtiles > 0) {
/* Now we have geometry in x, y, height, width for total xplot area */
y += (height * pl->tileno) / pl->numtiles;
height = height / pl->numtiles;
/* we have now selected the nth 1/numtiles chunk */
}
pl->win = XCreateWindow(pl->dpy, rootwindow, x, y, width, height, borderw,
DefaultDepth(pl->dpy, ScreenNumberOfScreen(pl->screen)),
CopyFromParent, CopyFromParent,
CWBackPixel|CWBorderPixel|CWEventMask|CWCursor,
&attr);
pl->xsh.flags = 0;
if (flags & (WidthValue | HeightValue))
pl->xsh.flags |= USSize;
else
pl->xsh.flags |= PSize;
if (flags & (XValue | YValue))
pl->xsh.flags |= USPosition;
else
pl->xsh.flags |= PPosition;
pl->xsh.width = width;
pl->xsh.height = height;
pl->xsh.x = x;
pl->xsh.y = y;
XSetStandardProperties(pl->dpy, pl->win, "xplot", "xplot", None,
global_argv, global_argc, &pl->xsh);
}
XMapRaised(pl->dpy, pl->win);
pl->visibility = VisibilityFullyObscured; /* initially true */
{
char *use_font = XGetDefault (pl->dpy, global_argv[0], "font");
pl->font_struct = XLoadQueryFont(pl->dpy, use_font ? use_font : "fixed");
}
pl->gcv.foreground = pl->foreground_color.pixel;
pl->gcv.background = pl->background_color.pixel;
pl->gcv.font = pl->font_struct->fid;
pl->gcv.line_width = 0;
pl->gcv.cap_style = CapProjecting;
if (pl->thick) {
pl->gcv.line_width = 3;
pl->gcv.cap_style = CapRound;
}
{
#define N_DPY_S 2
static struct dpy_info {
unsigned long line_plane_mask;
Colormap clr_map;
int depth;
XColor clr;
unsigned long pixel[NCOLORS];
int Colors[NCOLORS];
int virgin;
int warned_color_alloc_failed;
Atom xplot_nagle_atom;
Display *saved_dpy;
} d_i[N_DPY_S];
int i;
int d;
for (d = 0; d < N_DPY_S; d++) {
if (pl->dpy == d_i[d].saved_dpy)
break;
if (d_i[d].saved_dpy == 0) {
d_i[d].saved_dpy = pl->dpy;
d_i[d].virgin = 1;
break;
}
}
if (d >= N_DPY_S) {
fprintf(stderr, "%s:%d:%s: bug -- pl->dpy != saved_dpy\n",
__FILE__, __LINE__, __FUNCTION__);
panic("bug");
}
/* Allocate some color cells */
if (d_i[d].virgin ) {
int ci;
d_i[d].virgin = 0;
d_i[d].xplot_nagle_atom = XInternAtom(pl->dpy, "XPLOT_NAGLE", False);
d_i[d].clr_map = DefaultColormap(pl->dpy, DefaultScreen(pl->dpy));
d_i[d].depth = DisplayPlanes(pl->dpy, DefaultScreen(pl->dpy));
ci = 0;
/* if display/screen has only 1 bit of depth, don't try to
* allocate any colors - just use BlackPixel and WhitePixel
*/
if ( ! option_mono && d_i[d].depth > 1
&& XAllocColorCells(pl->dpy,
d_i[d].clr_map, 0,
&d_i[d].line_plane_mask, 1,
d_i[d].pixel, NColors)
)
{
for ( ; ci < NColors; ci++) {
XParseColor(pl->dpy, d_i[d].clr_map, ColorNames[ci], &d_i[d].clr);
d_i[d].clr.pixel = d_i[d].pixel[ci];
XStoreColor (pl->dpy, d_i[d].clr_map, &d_i[d].clr);
d_i[d].clr.pixel |= d_i[d].line_plane_mask;
XStoreColor (pl->dpy, d_i[d].clr_map, &d_i[d].clr);
d_i[d].Colors[ci] = d_i[d].clr.pixel;
}
} else if (! option_mono && d_i[d].depth > 1 ) {
/* some visual types (e.g. TrueColor) do not support XAllocColorCells */
for ( ; ci < NColors; ci++) {
XColor exact_return;
i = XAllocNamedColor(pl->dpy, d_i[d].clr_map, ColorNames[ci],
&exact_return, &d_i[d].clr);
if ( i < 0 )
{
fprintf(stderr, "XAllocNamedColor failed for %s: %d\n",
ColorNames[ci], i);
break;
}
/* Here, we should check if the color is close enough. */
d_i[d].Colors[ci] = d_i[d].clr.pixel;
#if 0
/***** and on any failure, you should break out of this
loop leaving ci pointing at the first color (perhaps
zero) of the first color you've failed to allocate. */
/***** need to take care that this does the correct thing
on one-bit-plane displays. There are two ways that this might occur:
1. All pixel values obtained in this loop on a
one-bit-plane display are the same as WhitePixelOfScreen()
or
2. On a one-bitplane display we break out of this loop
and leave the loop below to fill in WhitePixelOfScreen()
for each plot color.
*/
#endif
}
}
for ( ; ci < NColors; ci++) {
/* probably only one bit plane, or all the color cells are taken
(or option_mono)*/
if (!d_i[d].warned_color_alloc_failed && !option_mono) {
fputs("unable to get all desired colors, will substitute white for some or all colors\n",
stderr);
d_i[d].warned_color_alloc_failed = 1;
}
d_i[d].Colors[ci] = WhitePixelOfScreen(pl->screen);
}
}