This repository has been archived by the owner on Oct 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uwm.c
executable file
·1112 lines (981 loc) · 30.5 KB
/
uwm.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
/* $XConsortium: uwm.c,v 1.23 88/11/16 09:41:32 jim Exp $ */
#include <X11/copyright.h>
/*
* Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
*
* All Rights Reserved
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation, and that the name of Digital Equipment
* Corporation not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior permission.
*
*
* DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
* DIGITAL 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.
*/
/*
* MODIFICATION HISTORY
*
* 000 -- M. Gancarz, DEC Ultrix Engineering Group
* 001 -- Loretta Guarino Reid, DEC Ultrix Engineering Group,
* Western Software Lab. Convert to X11.
*/
#ifndef lint
static char *sccsid = "%W% %G%";
#endif
char *ProgramName;
#include "uwm.h"
#include <ctype.h>
#include <X11/Xproto.h>
#ifdef PROFIL
#include <signal.h>
/*
* Dummy handler for profiling.
*/
ptrap()
{
exit(0);
}
#endif
#define gray_width 16
#define gray_height 16
static char gray_bits[] = {
0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa,
0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa,
0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa,
0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa
};
Bool NeedRootInput=FALSE;
Bool ChkMline();
char *sfilename;
extern FILE *yyin;
/*
* Main program.
*/
main(argc, argv, environ)
int argc;
char **argv;
char **environ;
{
int hi; /* Button event high detail. */
int lo; /* Button event low detail. */
int x, y; /* Mouse X and Y coordinates. */
int root_x, root_y; /* Mouse root X and Y coordinates. */
int cur_x, cur_y; /* Current mouse X and Y coordinates. */
int down_x, down_y; /* mouse X and Y at ButtonPress. */
int str_width; /* Width in pixels of output string. */
int pop_width, pop_height; /* Pop up window width and height. */
int context; /* Root, window, or icon context. */
int ptrmask; /* for QueryPointer */
Bool func_stat; /* If true, function swallowed a ButtonUp. */
Bool delta_done; /* If true, then delta functions are done. */
Bool local = FALSE; /* If true, then do not use system defaults. */
register Binding *bptr; /* Pointer to Bindings list. */
char *root_name; /* Root window name. */
char *display = NULL; /* Display name pointer. */
char message[128]; /* Error message buffer. */
char *rc_file; /* Pointer to $HOME/.uwmrc. */
Window event_win; /* Event window. */
Window sub_win; /* Subwindow for XUpdateMouse calls. */
Window root; /* Root window for QueryPointer. */
XWindowAttributes event_info; /* Event window info. */
XEvent button_event; /* Button input event. */
GC gc; /* graphics context for gray background */
XImage grayimage; /* for gray background */
XGCValues xgc; /* to create font GCs */
char *malloc();
Bool fallbackMFont = False, /* using default GC font for menus, */
fallbackPFont = False, /* popups, */
fallbackIFont = False; /* icons */
XSetWindowAttributes attr; /* for setting save under flag */
unsigned long whitepix, blackpix, borderpix, popborderpix;
ProgramName = argv[0];
#ifdef PROFIL
signal(SIGTERM, ptrap);
#endif
/*
* Set up internal defaults.
*/
SetVarDefaults();
/*
* Parse the command line arguments.
*/
Argv = argv;
Environ = environ;
argc--, argv++;
while (argc) {
if (**argv == '-') {
if (strcmp(*argv, "-display") == 0 || strcmp(*argv, "-d") == 0) {
argc--, argv++;
if (argc <= 0)
Usage ();
display = *argv;
} else if (strcmp(*argv, "-f") == 0) {
argc--, argv++;
if ((argc == 0) || (Startup_File[0] != '\0'))
Usage();
strncpy(Startup_File, *argv, NAME_LEN);
} else if (strcmp(*argv, "-b") == 0)
local = TRUE;
else
Usage();
} else
Usage ();
argc--, argv++;
}
/*
* Initialize the default bindings.
*/
if (!local)
InitBindings();
/*
* Open the display.
*/
if ((dpy = XOpenDisplay(display)) == NULL)
Error("Unable to open display");
scr = DefaultScreen(dpy);
SetEnvironment ();
LoadXDefaults ();
/*
* Set XErrorFunction to be non-terminating.
*/
XSetErrorHandler(XError);
/*
* Force child processes to disinherit the TCP file descriptor.
* This helps shell commands forked and exec'ed from menus
* to work properly.
*/
if ((status = fcntl(ConnectionNumber(dpy), F_SETFD, 1)) == -1) {
perror("uwm: child cannot disinherit TCP fd");
Error("TCP file descriptor problems");
}
/*
* Read in and parse $HOME/.uwmrc, if it exists.
*/
sfilename = rc_file = malloc(NAME_LEN);
sprintf(rc_file, "%s/.uwmrc", getenv("HOME"));
if ((yyin = fopen(rc_file, "r")) != NULL) {
Lineno = 1;
yyparse();
fclose(yyin);
if (Startup_File_Error)
Error("Bad .uwmrc file...aborting");
}
/*
* Read in and parse the startup file from the command line, if
* specified.
*/
if (Startup_File[0] != '\0') {
sfilename = Startup_File;
if ((yyin = fopen(Startup_File, "r")) == NULL) {
sprintf(message, "Cannot open startup file '%s'", Startup_File);
Error(message);
}
Lineno = 1;
yyparse();
fclose(yyin);
if (Startup_File_Error)
Error("Bad startup file...aborting");
}
/*
* Verify the menu bindings.
*/
VerifyMenuBindings();
if (Startup_File_Error)
Error("Bad startup file...aborting");
/*
* Verify the menu variables.
*/
VerifyMenuVariables();
/*
* If the root window has not been named, name it.
*/
status = XFetchName(dpy, RootWindow(dpy, scr), &root_name);
if (root_name == NULL)
XStoreName(dpy, RootWindow(dpy, scr), " X Root Window ");
else free(root_name);
ScreenHeight = DisplayHeight(dpy, scr);
ScreenWidth = DisplayWidth(dpy, scr);
/*
* set the default colors
*/
{
Bool whiteset = False, blackset = False, borderset = False;
Colormap cmap = DefaultColormap (dpy, scr);
XColor cdef;
/*
* the model is foreground = black, background = white
*/
if (DisplayCells (dpy, scr) > 2) {
if (PBackgroundName[0]) {
if (XParseColor (dpy, cmap, PBackgroundName, &cdef) &&
XAllocColor (dpy, cmap, &cdef)) {
whitepix = cdef.pixel;
whiteset = True;
}
}
if (PForegroundName[0]) {
if (XParseColor (dpy, cmap, PForegroundName, &cdef) &&
XAllocColor (dpy, cmap, &cdef)) {
blackpix = cdef.pixel;
blackset = True;
}
}
if (PBorderColor[0]) {
if (XParseColor (dpy, cmap, PBorderColor, &cdef) &&
XAllocColor (dpy, cmap, &cdef)) {
borderpix = cdef.pixel;
borderset = True;
}
}
}
if (!whiteset) whitepix = WhitePixel (dpy, scr);
if (!blackset) blackpix = BlackPixel (dpy, scr);
if (!borderset) borderpix = blackpix;
}
/*
* Create and store the icon background pixmap.
*/
GrayPixmap = (Pixmap)XCreatePixmap(dpy, RootWindow(dpy, scr),
gray_width, gray_height, DefaultDepth(dpy,scr));
xgc.foreground = blackpix;
xgc.background = whitepix;
gc = XCreateGC(dpy, GrayPixmap, GCForeground+GCBackground, &xgc);
grayimage.height = gray_width;
grayimage.width = gray_height;
grayimage.xoffset = 0;
grayimage.format = XYBitmap;
grayimage.data = (char *)gray_bits;
grayimage.byte_order = LSBFirst;
grayimage.bitmap_unit = 8;
grayimage.bitmap_bit_order = LSBFirst;
grayimage.bitmap_pad = 16;
grayimage.bytes_per_line = 2;
grayimage.depth = 1;
XPutImage(dpy, GrayPixmap, gc, &grayimage, 0, 0,
0, 0, gray_width, gray_height);
XFreeGC(dpy, gc);
/*
* Set up icon window, icon cursor and pop-up window color parameters.
* The world needs to be consistent, so we choose a default of black on
* white. Note that this means that sizing windows will look "backwards"
* from what they used to be.
*/
if (Reverse) {
PBackground = MBackground = blackpix;
ITextBackground = MTextBackground = PTextBackground = blackpix;
ITextForeground = MTextForeground = PTextForeground = whitepix;
} else {
PBackground = MBackground = whitepix;
ITextBackground = MTextBackground = PTextBackground = whitepix;
ITextForeground = MTextForeground = PTextForeground = blackpix;
}
IBackground = GrayPixmap;
IBorder = MBorder = PBorder = borderpix;
/*
* Store all the cursors.
*/
StoreCursors();
/*
* grab the mouse buttons according to the map structure
*/
Grab_Buttons();
/*
* Set initial focus to PointerRoot.
*/
XSetInputFocus(dpy, PointerRoot, None, CurrentTime);
/*
* register an interest for colormap tracking
*/
{
Window root_return, parent_return, *children_return;
int nchildren_return;
initColormap ();
/*
* watch for initial window mapping and window destruction
*/
x_error_occurred = FALSE;
(void) uwmExpressInterest (RootWindow (dpy, scr),
SubstructureNotifyMask|SubstructureRedirectMask|FocusChangeMask|
(NeedRootInput ? EVENTMASK|OwnerGrabButtonMask : 0));
XSync (dpy, 0);
if (x_error_occurred &&
last_error_event.request_code == X_ChangeWindowAttributes &&
last_error_event.error_code == BadAccess) {
Error("can't select for root window events; is another window manager running?");
}
/*
* discover existing windows
*/
if (XQueryTree (dpy, RootWindow(dpy, scr), &root_return,
&parent_return, &children_return, &nchildren_return)) {
int i;
for (i = 0; i < nchildren_return; i++)
(void) getWindowInfo (children_return[i]);
}
}
/*
* Retrieve the information structure for the specifed fonts and
* set the global font information pointers.
*/
IFontInfo = XLoadQueryFont(dpy, IFontName);
if (IFontInfo == NULL) {
fprintf(stderr, "uwm: Unable to open icon font '%s', using server default.\n",
IFontName);
IFontInfo = XQueryFont(dpy, DefaultGC(dpy, scr)->gid);
fallbackIFont = True;
}
PFontInfo = XLoadQueryFont(dpy, PFontName);
if (PFontInfo == NULL) {
fprintf(stderr, "uwm: Unable to open resize font '%s', using server default.\n",
PFontName);
if (fallbackIFont)
PFontInfo = IFontInfo;
else
PFontInfo = XQueryFont(dpy, DefaultGC(dpy, scr)->gid);
fallbackPFont = True;
}
MFontInfo = XLoadQueryFont(dpy, MFontName);
if (MFontInfo == NULL) {
fprintf(stderr, "uwm: Unable to open menu font '%s', using server default.\n",
MFontName);
if (fallbackIFont || fallbackPFont)
MFontInfo = fallbackPFont ? PFontInfo : IFontInfo;
else
MFontInfo = XQueryFont(dpy, DefaultGC(dpy, scr)->gid);
fallbackMFont = True;
}
/*
* Calculate size of the resize pop-up window.
*/
str_width = XTextWidth(PFontInfo, PText, strlen(PText));
pop_width = str_width + (PPadding << 1);
PWidth = pop_width + (PBorderWidth << 1);
pop_height = PFontInfo->ascent + PFontInfo->descent + (PPadding << 1);
PHeight = pop_height + (PBorderWidth << 1);
/*
* Create the pop-up window. Create it at (0, 0) for now. We will
* move it where we want later.
*/
Pop = XCreateSimpleWindow(dpy, RootWindow(dpy, scr),
0, 0,
pop_width, pop_height,
PBorderWidth,
PBorder, PBackground);
if (Pop == FAILURE) Error("Can't create pop-up dimension display window.");
attr.save_under = True;
XChangeWindowAttributes (dpy, Pop, CWSaveUnder, &attr);
/*
* Create the menus for later use.
*/
CreateMenus();
/*
* Create graphics context.
*/
xgc.foreground = ITextForeground;
xgc.background = ITextBackground;
xgc.font = IFontInfo->fid;
xgc.graphics_exposures = FALSE;
IconGC = XCreateGC(dpy,
RootWindow(dpy, scr),
GCForeground+GCBackground+GCGraphicsExposures
+(fallbackIFont ? 0 : GCFont), &xgc);
xgc.foreground = MTextForeground;
xgc.background = MTextBackground;
xgc.font = MFontInfo->fid;
MenuGC = XCreateGC(dpy,
RootWindow(dpy, scr),
GCForeground+GCBackground+(fallbackMFont ? 0 : GCFont), &xgc);
xgc.function = GXinvert;
xgc.plane_mask = MTextForeground ^ MTextBackground;
MenuInvGC = XCreateGC(dpy,
RootWindow(dpy, scr), GCForeground+GCFunction+GCPlaneMask, &xgc);
xgc.foreground = PTextForeground;
xgc.background = PTextBackground;
xgc.font = PFontInfo->fid;
PopGC = XCreateGC(dpy,
RootWindow(dpy, scr),
GCForeground+GCBackground+(fallbackPFont ? 0 : GCFont), &xgc);
xgc.line_width = DRAW_WIDTH;
xgc.foreground = DRAW_VALUE;
xgc.function = DRAW_FUNC;
xgc.subwindow_mode = IncludeInferiors;
DrawGC = XCreateGC(dpy, RootWindow(dpy, scr),
GCLineWidth+GCForeground+GCFunction+GCSubwindowMode, &xgc);
/*
* Tell the user we're alive and well.
*/
XBell(dpy, VOLUME_PERCENTAGE(Volume));
/*
* Main command loop.
*/
while (TRUE) {
delta_done = func_stat = FALSE;
/*
* Get the next mouse button event. Spin our wheels until
* a ButtonPressed event is returned.
* Note that mouse events within an icon window are handled
* in the "GetButton" function or by the icon's owner if
* it is not uwm.
*/
while (TRUE) {
if (!GetButton(&button_event)) continue;
if (button_event.type == ButtonPress) break;
}
/* save mouse coords in case we want them later for a delta action */
down_x = ((XButtonPressedEvent *)&button_event)->x;
down_y = ((XButtonPressedEvent *)&button_event)->y;
/*
* Okay, determine the event window and mouse coordinates.
*/
status = XTranslateCoordinates(dpy,
RootWindow(dpy, scr), RootWindow(dpy, scr),
((XButtonPressedEvent *)&button_event)->x,
((XButtonPressedEvent *)&button_event)->y,
&x, &y,
&event_win);
if (status == FAILURE) continue;
/*
* Determine the event window and context.
*/
if (event_win == 0) {
event_win = RootWindow(dpy, scr);
context = ROOT;
} else {
if (IsIcon(event_win, 0, 0, FALSE, NULL))
context = ICON;
else context = WINDOW;
}
/*
* Get the button event detail.
*/
lo = ((XButtonPressedEvent *)&button_event)->button;
hi = ((XButtonPressedEvent *)&button_event)->state;
/*
* Determine which function was selected and invoke it.
*/
for(bptr = Blist; bptr; bptr = bptr->next) {
if ((bptr->button != lo) ||
(((int)bptr->mask & ModMask) != hi))
continue;
if (bptr->context != context)
continue;
if (!(bptr->mask & ButtonDown))
continue;
/*
* Found a match! Invoke the function.
*/
if ((*bptr->func)(event_win,
(int)bptr->mask & ModMask,
bptr->button,
x, y,
bptr->menu)) {
func_stat = TRUE;
break;
}
}
/*
* If the function ate the ButtonUp event, then restart the loop.
*/
if (func_stat) continue;
while(TRUE) {
/*
* Wait for the next button event.
*/
if (XPending(dpy) && GetButton(&button_event)) {
/*
* If it's not a release of the same button that was pressed,
* don't do the function bound to 'ButtonUp'.
*/
if (button_event.type != ButtonRelease)
break;
if (lo != ((XButtonReleasedEvent *)&button_event)->button)
break;
if ((hi|ButtonMask(lo)) !=
((XButtonReleasedEvent *)&button_event)->state)
break;
/*
* Okay, determine the event window and mouse coordinates.
*/
status = XTranslateCoordinates(dpy,
RootWindow(dpy, scr), RootWindow(dpy, scr),
((XButtonReleasedEvent *)&button_event)->x,
((XButtonReleasedEvent *)&button_event)->y,
&x, &y,
&event_win);
if (status == FAILURE) break;
if (event_win == 0) {
event_win = RootWindow(dpy, scr);
context = ROOT;
} else {
if (IsIcon(event_win, 0, 0, FALSE, NULL))
context = ICON;
else context = WINDOW;
}
/*
* Determine which function was selected and invoke it.
*/
for(bptr = Blist; bptr; bptr = bptr->next) {
if ((bptr->button != lo) ||
(((int)bptr->mask & ModMask) != hi))
continue;
if (bptr->context != context)
continue;
if (!(bptr->mask & ButtonUp))
continue;
/*
* Found a match! Invoke the function.
*/
(*bptr->func)(event_win,
(int)bptr->mask & ModMask,
bptr->button,
x, y,
bptr->menu);
}
break;
}
XQueryPointer(dpy, RootWindow(dpy, scr),
&root, &event_win, &root_x, &root_y, &cur_x, &cur_y, &ptrmask);
if (!delta_done &&
((abs(cur_x - x) > Delta) || (abs(cur_y - y) > Delta))) {
/*
* Delta functions are done once (and only once.)
*/
delta_done = TRUE;
/*
* Determine the new event window's coordinates.
* from the original ButtonPress event
*/
status = XTranslateCoordinates(dpy,
RootWindow(dpy, scr), RootWindow(dpy, scr),
down_x, down_y, &x, &y, &event_win);
if (status == FAILURE) break;
/*
* Determine the event window and context.
*/
if (event_win == 0) {
event_win = RootWindow(dpy, scr);
context = ROOT;
} else {
if (IsIcon(event_win, 0, 0, FALSE, NULL))
context = ICON;
else context = WINDOW;
}
/*
* Determine which function was selected and invoke it.
*/
for(bptr = Blist; bptr; bptr = bptr->next) {
if ((bptr->button != lo) ||
(((int)bptr->mask & ModMask) != hi))
continue;
if (bptr->context != context)
continue;
if (!(bptr->mask & DeltaMotion))
continue;
/*
* Found a match! Invoke the function.
*/
if ((*bptr->func)(event_win,
(int)bptr->mask & ModMask,
bptr->button,
x, y,
bptr->menu)) {
func_stat = TRUE;
break;
}
}
/*
* If the function ate the ButtonUp event,
* then restart the loop.
*/
if (func_stat) break;
}
}
}
}
/*
* Initialize the default bindings. First, write the character array
* out to a temp file, then point the parser to it and read it in.
* Afterwards, we unlink the temp file.
*/
InitBindings()
{
char *mktemp();
char *tempfile; /* Temporary filename. */
register FILE *fp; /* Temporary file pointer. */
register char **ptr; /* Default bindings string array pointer. */
/*
* Create and write the temp file.
*/
tempfile = malloc (strlen (TEMPFILE) + 1);
if (!tempfile) {
perror("uwm: cannot allocate name for temp file");
exit (1);
}
strcpy (tempfile, TEMPFILE);
sfilename = mktemp(tempfile);
if ((fp = fopen(tempfile, "w")) == NULL) {
perror("uwm: cannot create temp file");
exit(1);
}
for (ptr = DefaultBindings; *ptr; ptr++) {
fputs(*ptr, fp);
fputc('\n', fp);
}
fclose(fp);
/*
* Read in the bindings from the temp file and parse them.
*/
if ((yyin = fopen(tempfile, "r")) == NULL) {
perror("uwm: cannot open temp file");
exit(1);
}
Lineno = 1;
yyparse();
fclose(yyin);
unlink(tempfile);
if (Startup_File_Error)
Error("Bad default bindings...aborting");
/*
* Parse the system startup file, if one exists.
*/
if ((yyin = fopen(SYSFILE, "r")) != NULL) {
sfilename = SYSFILE;
Lineno = 1;
yyparse();
fclose(yyin);
if (Startup_File_Error)
Error("Bad system startup file...aborting");
}
}
/*
* Verify menu bindings by checking that a menu that is mapped actually
* exists. Stash a pointer in the binding to the relevant menu info data
* structure.
* Check nested menu consistency.
*/
VerifyMenuBindings()
{
Binding *bptr;
MenuLink *mptr;
for(bptr = Blist; bptr; bptr = bptr->next) {
if (bptr->func == Menu) {
for(mptr = Menus; mptr; mptr = mptr->next) {
if(!(strcmp(bptr->menuname, mptr->menu->name))) {
bptr->menu = mptr->menu;
break;
}
}
if (mptr == NULL) {
fprintf(stderr,
"uwm: non-existent menu reference: \"%s\"\n",
bptr->menuname);
Startup_File_Error = TRUE;
}
}
}
CheckMenus();
}
/*
* Verify that the menu variables have reasonable values
*/
VerifyMenuVariables()
{
/*
* If we pushrelative, we divide the window size by
* the push variable. If it's zero, we die a sad death.
* So lets use the default push value in this case.
*/
if (!Pushval && Push) Pushval = DEF_PUSH;
}
/*
* Check nested menu consistency by verifying that every menu line that
* calls another menu references a menu that actually exists.
*/
CheckMenus()
{
MenuLink *ptr;
Bool errflag = FALSE;
for(ptr = Menus; ptr; ptr = ptr->next) {
if (ChkMline(ptr->menu))
errflag = TRUE;
}
if (errflag)
Error("Nested menu inconsistency");
}
Bool ChkMline(menu)
MenuInfo *menu;
{
MenuLine *ptr;
MenuLink *lptr;
Bool errflag = FALSE;
for(ptr = menu->line; ptr; ptr = ptr->next) {
if (ptr->type == IsMenuFunction) {
for(lptr = Menus; lptr; lptr = lptr->next) {
if(!(strcmp(ptr->text, lptr->menu->name))) {
ptr->menu = lptr->menu;
break;
}
}
if (lptr == NULL) {
fprintf(stderr,
"uwm: non-existent menu reference: \"%s\"\n",
ptr->text);
errflag = TRUE;
}
}
}
return(errflag);
}
/*
* Grab the mouse buttons according to the bindings list.
*/
Grab_Buttons()
{
Binding *bptr;
for(bptr = Blist; bptr; bptr = bptr->next)
if ((bptr->context & (WINDOW | ICON | ROOT)) == ROOT) {
/* don't grab buttons if you don't have to - allow application
access to buttons unless context includes window or icon */
NeedRootInput = TRUE;
}
else {
/* context includes a window, so must grab */
Grab(bptr->mask);
}
}
/*
* Grab a mouse button according to the given mask.
*/
Grab(mask)
unsigned int mask;
{
unsigned int m = LeftMask | MiddleMask | RightMask;
switch (mask & m) {
case LeftMask:
XGrabButton(dpy, LeftButton, mask & ModMask,
RootWindow(dpy, scr), TRUE, EVENTMASK,
GrabModeAsync, GrabModeAsync, None, LeftButtonCursor);
break;
case MiddleMask:
XGrabButton(dpy, MiddleButton, mask & ModMask,
RootWindow(dpy, scr), TRUE, EVENTMASK,
GrabModeAsync, GrabModeAsync, None, MiddleButtonCursor);
break;
case RightMask:
XGrabButton(dpy, RightButton, mask & ModMask,
RootWindow(dpy, scr), TRUE, EVENTMASK,
GrabModeAsync, GrabModeAsync, None, RightButtonCursor);
break;
}
}
/*
* Restore cursor to normal state.
*/
ResetCursor(button)
int button;
{
switch (button) {
case LeftButton:
XChangeActivePointerGrab(
dpy, EVENTMASK, LeftButtonCursor, CurrentTime);
break;
case MiddleButton:
XChangeActivePointerGrab(
dpy, EVENTMASK, MiddleButtonCursor, CurrentTime);
break;
case RightButton:
XChangeActivePointerGrab(
dpy, EVENTMASK, RightButtonCursor, CurrentTime);
break;
}
}
/*
* error routine for .uwmrc parser
*/
yyerror(s)
char*s;
{
fprintf(stderr, "uwm: %s: %d: %s\n", sfilename, Lineno, s);
Startup_File_Error = TRUE;
}
/*
* Print usage message and quit.
*/
Usage()
{
fprintf (stderr,
"usage: %s [-display host:dpy] [-b] [-f filename]\n\n",
Argv[0]);
fputs("The -b option bypasses system and default bindings\n", stderr);
fputs("The -f option specifies an additional startup file\n", stderr);
exit(1);
}
/*
* error handler for X I/O errors
*/
XIOError(dsp)
Display *dsp;
{
perror("uwm");
exit(3);
}
SetVarDefaults()
{
strcpy(IFontName, DEF_FONT);
strcpy(PFontName, DEF_FONT);
strcpy(MFontName, DEF_FONT);
Delta = DEF_DELTA;
IBorderWidth = DEF_ICON_BORDER_WIDTH;
HIconPad = DEF_ICON_PADDING;
VIconPad = DEF_ICON_PADDING;
PBorderWidth = DEF_POP_BORDER_WIDTH;
PPadding = DEF_POP_PADDING;
MBorderWidth = DEF_MENU_BORDER_WIDTH;
HMenuPad = DEF_MENU_PADDING;
VMenuPad = DEF_MENU_PADDING;
Volume = DEF_VOLUME;
Pushval = DEF_PUSH;
FocusSetByUser = FALSE;
PBackgroundName[0] = '\0'; /* so that default is computed */
PForegroundName[0] = '\0'; /* so that default is computed */
PBorderColor[0] = '\0'; /* so that default is computed */
}
static char *get_default (d, prog, name)
Display *d;
char *prog;
char *name;
{
char *option;
char buf[256];
option = XGetDefault (d, prog, name);
if (!option) {
strcpy (buf, name);
if (isascii(buf[0]) && islower(buf[0])) buf[0] = toupper(buf[0]);
option = XGetDefault (d, prog, buf);
}
return option;
}
static int parse_boolean (s)
char *s;
{
char *cp;
static struct _boolpair {
char *name;
int value;
} table[] = {
{ "on", 1 }, { "yes", 1 }, { "t", 1 }, { "true", 1 },
{ "off", 0 }, { "no", 0 }, { "f", 0 }, { "false", 0 },
{ NULL, -1 }};
struct _boolpair *bpp;
if (!s) return -1;
for (cp = s; *cp; cp++) {
if (isascii(*cp) && isupper(*cp)) *cp = tolower(*cp);
}