diff options
Diffstat (limited to 'apps/plugins/zxbox/ax.c')
-rw-r--r-- | apps/plugins/zxbox/ax.c | 815 |
1 files changed, 815 insertions, 0 deletions
diff --git a/apps/plugins/zxbox/ax.c b/apps/plugins/zxbox/ax.c new file mode 100644 index 0000000000..c7db7f5c6b --- /dev/null +++ b/apps/plugins/zxbox/ax.c | |||
@@ -0,0 +1,815 @@ | |||
1 | /* | ||
2 | * Copyright (C) 1996-1998 Szeredi Miklos | ||
3 | * Email: mszeredi@inf.bme.hu | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation; either version 2 of the License, or | ||
8 | * (at your option) any later version. See the file COPYING. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program; if not, write to the Free Software | ||
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
18 | * | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * | ||
23 | * ax.c | ||
24 | * | ||
25 | * | ||
26 | * | ||
27 | * Created: 94/11/11 Szeredi Miklos | ||
28 | * | ||
29 | * Version: 0.1 94/11/11 | ||
30 | * 0.2 95/06/12 | ||
31 | * | ||
32 | */ | ||
33 | |||
34 | /* #define DEBUG_EVENTS */ | ||
35 | |||
36 | #include "ax.h" | ||
37 | #include <X11/Xutil.h> | ||
38 | #include <X11/Xresource.h> | ||
39 | |||
40 | #include <stdio.h> | ||
41 | #include <stdlib.h> | ||
42 | #include <string.h> | ||
43 | #include <unistd.h> | ||
44 | |||
45 | #include <sys/time.h> | ||
46 | #include <sys/types.h> | ||
47 | #include <signal.h> | ||
48 | |||
49 | |||
50 | #ifndef TRUE | ||
51 | #define TRUE 1 | ||
52 | #endif | ||
53 | #ifndef FALSE | ||
54 | #define FALSE 0 | ||
55 | #endif | ||
56 | typedef int boolean; | ||
57 | #define max(x, y) ((x) < (y) ? (y) : (x)) | ||
58 | #define min(x, y) ((x) < (y) ? (x) : (y)) | ||
59 | #define minmax(a, l, u) ((a) < (l) ? (l) : ((a) > (u) ? (u) : (a))) | ||
60 | |||
61 | #define MAX_PROG_NAME_LEN 128 | ||
62 | #define MAX_CLASS_NAME_LEN MAX_PROG_NAME_LEN | ||
63 | #define MAX_PRES_LEN 256 | ||
64 | #define MAX_FILENAME_LEN 1024 | ||
65 | |||
66 | static const char *empty_str = ""; | ||
67 | |||
68 | typedef void (*eventproc_t)(XEvent *, void *); | ||
69 | |||
70 | typedef struct _event_proc_struct { | ||
71 | eventproc_t event_proc; | ||
72 | unsigned long event_mask; | ||
73 | Display *disp; | ||
74 | void *ptr; | ||
75 | Window event_win; | ||
76 | boolean done_proc; | ||
77 | struct _event_proc_struct *next_proc; | ||
78 | } event_proc_struct; | ||
79 | |||
80 | typedef struct { | ||
81 | const char *event_name; | ||
82 | boolean win_given; | ||
83 | event_proc_struct *next_proc; | ||
84 | } event_struct; | ||
85 | |||
86 | typedef struct _disp_struct{ | ||
87 | Display *disp; | ||
88 | XFontStruct *font; | ||
89 | struct _disp_struct *next_disp; | ||
90 | } disp_struct; | ||
91 | |||
92 | |||
93 | #define EVENT_NUM LASTEvent | ||
94 | |||
95 | static event_struct event_info[EVENT_NUM]; | ||
96 | |||
97 | static disp_struct disp_start = {NULL, NULL, NULL}; | ||
98 | |||
99 | static boolean disp_chain_modified; | ||
100 | |||
101 | static char prog_name[MAX_PROG_NAME_LEN + 1]; | ||
102 | static char class_name[MAX_CLASS_NAME_LEN + 1]; | ||
103 | |||
104 | static XrmDatabase rDB = NULL; /* Has to be global otherwise Xlib hangs */ | ||
105 | |||
106 | static int opTableEntries = 19; | ||
107 | static aX_options opTable[] = { | ||
108 | {"-display", ".display", XrmoptionSepArg, NULL}, | ||
109 | {"-geometry", ".geometry", XrmoptionSepArg, NULL}, | ||
110 | {"-bg", ".background", XrmoptionSepArg, NULL}, | ||
111 | {"-background", ".background", XrmoptionSepArg, NULL}, | ||
112 | {"-bd", ".borderColor", XrmoptionSepArg, NULL}, | ||
113 | {"-bordercolor", ".borderColor", XrmoptionSepArg, NULL}, | ||
114 | {"-bw", ".borderWidth", XrmoptionSepArg, NULL}, | ||
115 | {"-borderwidth", ".borderWidth", XrmoptionSepArg, NULL}, | ||
116 | {"-fg", ".foreground", XrmoptionSepArg, NULL}, | ||
117 | {"-foreground", ".foreground", XrmoptionSepArg, NULL}, | ||
118 | {"-fn", ".font", XrmoptionSepArg, NULL}, | ||
119 | {"-font", ".font", XrmoptionSepArg, NULL}, | ||
120 | {"-name", ".name", XrmoptionSepArg, NULL}, | ||
121 | {"-rv", ".reverseVideo", XrmoptionNoArg, "on"}, | ||
122 | {"-reverse", ".reverseVideo", XrmoptionNoArg, "on"}, | ||
123 | {"+rv", ".reverseVideo", XrmoptionNoArg, "off"}, | ||
124 | {"-bg", ".background", XrmoptionSepArg, NULL}, | ||
125 | {"-title", ".title", XrmoptionSepArg, NULL}, | ||
126 | {"-xrm", NULL, XrmoptionResArg, NULL} | ||
127 | }; | ||
128 | |||
129 | static char *addstr(const char str1[], const char str2[], char str12[], | ||
130 | unsigned int str12len) | ||
131 | { | ||
132 | unsigned int i, j, k; | ||
133 | |||
134 | str12[str12len-1] = '\0'; | ||
135 | |||
136 | for(i=0, j=0, k=0; i + 1 < str12len; ) { | ||
137 | if(str1[j]) str12[i] = str1[j++]; | ||
138 | else str12[i] = str2[k++]; | ||
139 | if(! str12[i++]) break; | ||
140 | } | ||
141 | return str12; | ||
142 | } | ||
143 | |||
144 | |||
145 | static char *pname(const char *resource) { | ||
146 | static char pnameres[MAX_PRES_LEN]; | ||
147 | |||
148 | return addstr(prog_name, resource, pnameres, MAX_PRES_LEN); | ||
149 | } | ||
150 | |||
151 | static char *pclass(const char *resource) { | ||
152 | static char pclassres[MAX_PRES_LEN]; | ||
153 | |||
154 | return addstr(class_name, resource, pclassres, MAX_PRES_LEN); | ||
155 | } | ||
156 | |||
157 | |||
158 | static void fill_event_info(void) | ||
159 | { | ||
160 | int i; | ||
161 | |||
162 | for(i = 0; i < EVENT_NUM; i++) { | ||
163 | event_info[i].event_name = empty_str; | ||
164 | event_info[i].win_given = TRUE; | ||
165 | event_info[i].next_proc = NULL; | ||
166 | } | ||
167 | |||
168 | event_info[MappingNotify].win_given = FALSE; | ||
169 | |||
170 | event_info[ButtonPress].event_name = "ButtonPress"; | ||
171 | event_info[ButtonRelease].event_name = "ButtonRelease"; | ||
172 | event_info[CirculateNotify].event_name = "CirculateNotify"; | ||
173 | event_info[CirculateRequest].event_name = "CirculateRequest"; | ||
174 | event_info[ClientMessage].event_name = "ClientMessage"; | ||
175 | event_info[ColormapNotify].event_name = "ColormapNotify"; | ||
176 | event_info[ConfigureNotify].event_name = "ConfigureNotify"; | ||
177 | event_info[ConfigureRequest].event_name = "ConfigureRequest"; | ||
178 | event_info[CreateNotify].event_name = "CreateNotify"; | ||
179 | event_info[DestroyNotify].event_name = "DestroyNotify"; | ||
180 | event_info[EnterNotify].event_name = "EnterNotify"; | ||
181 | event_info[LeaveNotify].event_name = "LeaveNotify"; | ||
182 | event_info[Expose].event_name = "Expose"; | ||
183 | event_info[FocusIn].event_name = "FocusIn"; | ||
184 | event_info[FocusOut].event_name = "FocusOut"; | ||
185 | event_info[GraphicsExpose].event_name = "GraphicsExpose"; | ||
186 | event_info[NoExpose].event_name = "NoExpose"; | ||
187 | event_info[GravityNotify].event_name = "GravityNotify"; | ||
188 | event_info[KeymapNotify].event_name = "KeymapNotify"; | ||
189 | event_info[KeyPress].event_name = "KeyPress"; | ||
190 | event_info[KeyRelease].event_name = "KeyRelease"; | ||
191 | event_info[MapNotify].event_name = "MapNotify"; | ||
192 | event_info[UnmapNotify].event_name = "UnmapNotify"; | ||
193 | event_info[MappingNotify].event_name = "MappingNotify"; | ||
194 | event_info[MapRequest].event_name = "MapRequest"; | ||
195 | event_info[MotionNotify].event_name = "MotionNotify"; | ||
196 | event_info[PropertyNotify].event_name = "PropertyNotify"; | ||
197 | event_info[ReparentNotify].event_name = "ReparentNotify"; | ||
198 | event_info[ResizeRequest].event_name = "ResizeRequest"; | ||
199 | event_info[SelectionClear].event_name = "SelectionClear"; | ||
200 | event_info[SelectionNotify].event_name = "SelectionNotify"; | ||
201 | event_info[SelectionRequest].event_name = "SelectionRequest"; | ||
202 | event_info[VisibilityNotify].event_name = "VisibilityNotify"; | ||
203 | |||
204 | } | ||
205 | |||
206 | static void get_def_res(aX_default_resources *defres) | ||
207 | { | ||
208 | XrmValue value; | ||
209 | char *str_type; | ||
210 | int flags; | ||
211 | XColor color_def; | ||
212 | unsigned long tmp_pixel; | ||
213 | Colormap def_map; | ||
214 | int font_spec; | ||
215 | |||
216 | |||
217 | defres->window_name = prog_name; | ||
218 | defres->icon_name = prog_name; | ||
219 | |||
220 | |||
221 | defres->scr = DefaultScreen(defres->disp); | ||
222 | defres->scr_ptr = ScreenOfDisplay(defres->disp, defres->scr); | ||
223 | def_map = DefaultColormapOfScreen(defres->scr_ptr); | ||
224 | |||
225 | |||
226 | if(XrmGetResource(rDB, pname(".title"), pclass(".Title"), | ||
227 | &str_type, &value)) | ||
228 | defres->window_name = (char *) value.addr; | ||
229 | |||
230 | defres->sflags = PSize; | ||
231 | if(XrmGetResource(rDB, pname(".geometry"), pclass(".Geometry"), | ||
232 | &str_type, &value)) { | ||
233 | flags = XParseGeometry((char *) value.addr, &(defres->x), &(defres->y), | ||
234 | &(defres->width), &(defres->height)); | ||
235 | if((XValue | YValue) & flags) defres->sflags |= USPosition; | ||
236 | if((WidthValue | HeightValue) & flags) | ||
237 | defres->sflags = (defres->sflags & ~PSize) | USSize; | ||
238 | } | ||
239 | |||
240 | defres->background = defres->background ? | ||
241 | WhitePixel(defres->disp, defres->scr) : | ||
242 | BlackPixel(defres->disp, defres->scr); | ||
243 | |||
244 | if(XrmGetResource(rDB, pname(".background"), pclass(".Background"), | ||
245 | &str_type, &value)) { | ||
246 | if(XParseColor(defres->disp, def_map, value.addr, &color_def)) { | ||
247 | if(XAllocColor(defres->disp, def_map, &color_def)) | ||
248 | defres->background = color_def.pixel; | ||
249 | } | ||
250 | else fprintf(stderr, "%s: aX: warning: Invalid color specification %s\n", | ||
251 | prog_name, value.addr); | ||
252 | } | ||
253 | |||
254 | defres->foreground = defres->foreground ? | ||
255 | WhitePixel(defres->disp, defres->scr) : | ||
256 | BlackPixel(defres->disp, defres->scr); | ||
257 | |||
258 | if(XrmGetResource(rDB, pname(".foreground"), pclass(".Foreground"), | ||
259 | &str_type, &value)) { | ||
260 | if(XParseColor(defres->disp, def_map, value.addr, &color_def)) { | ||
261 | if(XAllocColor(defres->disp, def_map, &color_def)) | ||
262 | defres->foreground = color_def.pixel; | ||
263 | } | ||
264 | else fprintf(stderr, "%s: aX: warning: Invalid color specification %s\n", | ||
265 | prog_name, value.addr); | ||
266 | } | ||
267 | |||
268 | if(XrmGetResource(rDB, pname(".borderWidth"), pclass(".BorderWidth"), | ||
269 | &str_type, &value)) { | ||
270 | defres->border_width = atoi(value.addr); | ||
271 | } | ||
272 | |||
273 | defres->border_color = defres->foreground; | ||
274 | if(XrmGetResource(rDB, pname(".borderColor"), pclass(".BorderColor"), | ||
275 | &str_type, &value)) { | ||
276 | if(XParseColor(defres->disp, def_map, value.addr, &color_def)) { | ||
277 | if(XAllocColor(defres->disp, def_map, &color_def)) | ||
278 | defres->border_color = color_def.pixel; | ||
279 | } | ||
280 | else fprintf(stderr, "%s: aX: warning: Invalid color specification %s\n", | ||
281 | prog_name, value.addr); | ||
282 | } | ||
283 | |||
284 | font_spec = 0; | ||
285 | if(XrmGetResource(rDB, pname(".font"), pclass(".Font"), | ||
286 | &str_type, &value)) { | ||
287 | defres->font_name = value.addr; | ||
288 | if(defres->font_name != NULL) font_spec = 1; | ||
289 | |||
290 | } | ||
291 | |||
292 | if(XrmGetResource(rDB, pname(".fallbackFont"), pclass(".Font"), | ||
293 | &str_type, &value)) | ||
294 | defres->fallback_font_name = value.addr; | ||
295 | |||
296 | if(defres->font_name == NULL || | ||
297 | (defres->font = XLoadQueryFont(defres->disp, defres->font_name)) | ||
298 | == NULL) { | ||
299 | |||
300 | if(font_spec) | ||
301 | fprintf(stderr, "%s: aX: warning: cannot open %s font, ", | ||
302 | prog_name, defres->font_name); | ||
303 | |||
304 | defres->font_name = defres->fallback_font_name; | ||
305 | |||
306 | if(font_spec && defres->font_name != NULL) | ||
307 | fprintf(stderr, "trying %s...\n",defres->font_name); | ||
308 | |||
309 | if(defres->font_name == NULL || | ||
310 | (defres->font = | ||
311 | XLoadQueryFont(defres->disp, defres->fallback_font_name)) == NULL) { | ||
312 | |||
313 | if(defres->font_name != NULL) { | ||
314 | |||
315 | fprintf(stderr, "%s: aX: warning: cannot open %s font, ", | ||
316 | prog_name, defres->font_name); | ||
317 | } | ||
318 | |||
319 | defres->font_name = "fixed"; | ||
320 | |||
321 | fprintf(stderr, "trying %s...\n",defres->font_name); | ||
322 | |||
323 | if((defres->font = XLoadQueryFont(defres->disp, defres->font_name)) | ||
324 | == NULL) { | ||
325 | |||
326 | fprintf(stderr, "%s: aX: warning: cannot open %s font\n", | ||
327 | prog_name, defres->font_name); | ||
328 | |||
329 | exit(-1); | ||
330 | } | ||
331 | } | ||
332 | else defres->font_name = defres->fallback_font_name; | ||
333 | } | ||
334 | |||
335 | if(XrmGetResource(rDB, pname(".reverseVideo"), pclass(".ReverseVideo"), | ||
336 | &str_type, &value)) | ||
337 | if(strcmp(value.addr, "on") == 0) { | ||
338 | tmp_pixel = defres->foreground; | ||
339 | defres->foreground = defres->background; | ||
340 | defres->background = tmp_pixel; | ||
341 | } | ||
342 | |||
343 | } | ||
344 | |||
345 | |||
346 | static void add_disp(aX_default_resources *defres) | ||
347 | { | ||
348 | disp_struct *last; | ||
349 | |||
350 | for(last = &disp_start; last->next_disp != NULL; last = last->next_disp); | ||
351 | |||
352 | if((last->next_disp = malloc(sizeof(disp_struct))) == NULL) { | ||
353 | fprintf(stderr, "%s: aX: Not enough memory.\n", prog_name); | ||
354 | exit(-1); | ||
355 | }; | ||
356 | |||
357 | last = last->next_disp; | ||
358 | |||
359 | last->disp = defres->disp; | ||
360 | last->font = defres->font; | ||
361 | last->next_disp = NULL; | ||
362 | |||
363 | |||
364 | disp_chain_modified = TRUE; | ||
365 | } | ||
366 | |||
367 | void aX_open_disp(aX_options *useropt, int useroptlen, | ||
368 | int *argcp, char *argv[], | ||
369 | aX_default_resources *defres) | ||
370 | { | ||
371 | |||
372 | XrmValue value; | ||
373 | char *str_type; | ||
374 | char *disp_res; | ||
375 | char *environment; | ||
376 | char *display_name = NULL; | ||
377 | char filename[MAX_FILENAME_LEN]; | ||
378 | int i; | ||
379 | XrmDatabase commandlineDB = NULL, usercommandlineDB = NULL; | ||
380 | XrmDatabase homeDB, serverDB, applicationDB; | ||
381 | |||
382 | /* | ||
383 | if(disp_start.next_disp != NULL) { | ||
384 | fprintf(stderr, "aX_open_disp: Cannot open first display twice.\n"); | ||
385 | exit(-1); | ||
386 | } | ||
387 | */ | ||
388 | |||
389 | XrmInitialize(); | ||
390 | |||
391 | class_name[0] = '\0'; | ||
392 | class_name[MAX_CLASS_NAME_LEN] = '\0'; | ||
393 | if(defres->class_name != NULL) | ||
394 | strncpy(class_name, defres->class_name, MAX_CLASS_NAME_LEN); | ||
395 | |||
396 | |||
397 | fill_event_info(); | ||
398 | |||
399 | for(i = 1; i < *argcp; i++) | ||
400 | if(strcmp(argv[i], "-name") == 0 && ++i < *argcp){ | ||
401 | defres->prog_name = argv[i]; | ||
402 | break; | ||
403 | } | ||
404 | |||
405 | |||
406 | prog_name[0] = '\0'; | ||
407 | prog_name[MAX_PROG_NAME_LEN] = '\0'; | ||
408 | if(defres->prog_name != NULL) | ||
409 | strncpy(prog_name, defres->prog_name, MAX_PROG_NAME_LEN); | ||
410 | else | ||
411 | strncpy(prog_name, argv[0], MAX_PROG_NAME_LEN); | ||
412 | |||
413 | defres->prog_name = prog_name; | ||
414 | |||
415 | XrmParseCommand(&commandlineDB, (XrmOptionDescRec *) opTable, | ||
416 | opTableEntries, prog_name, argcp, argv); | ||
417 | |||
418 | if(useropt != NULL) | ||
419 | XrmParseCommand(&usercommandlineDB, (XrmOptionDescRec *) useropt, | ||
420 | useroptlen, prog_name, argcp, argv); | ||
421 | else usercommandlineDB = NULL; | ||
422 | |||
423 | /* | ||
424 | if(*argcp != 1) { | ||
425 | fprintf(stderr, | ||
426 | "%s: aX_open_disp: Unrecognised options in command line!\n", | ||
427 | prog_name); | ||
428 | exit(-1); | ||
429 | } | ||
430 | */ | ||
431 | |||
432 | if(XrmGetResource(commandlineDB, pname(".display"), pclass(".Display"), | ||
433 | &str_type, &value)) display_name = (char *) value.addr; | ||
434 | |||
435 | if((defres->disp = XOpenDisplay(display_name)) == NULL) { | ||
436 | fprintf(stderr, "%s: aX_open_disp: cannot connect to X server %s\n", | ||
437 | prog_name, XDisplayName(display_name)); | ||
438 | exit(-1); | ||
439 | } | ||
440 | |||
441 | applicationDB = XrmGetFileDatabase( | ||
442 | addstr("/usr/lib/X11/app-defaults/", | ||
443 | class_name, | ||
444 | filename, | ||
445 | MAX_FILENAME_LEN)); | ||
446 | /* | ||
447 | if(defres->disp->xdefaults) | ||
448 | serverDB = XrmGetStringDatabase(defres->disp->xdefaults); | ||
449 | else serverDB = NULL; | ||
450 | */ | ||
451 | |||
452 | |||
453 | disp_res = XResourceManagerString(defres->disp); | ||
454 | |||
455 | if(disp_res) serverDB = XrmGetStringDatabase(disp_res); | ||
456 | else serverDB = NULL; | ||
457 | |||
458 | |||
459 | if((environment = getenv("XENVIRONMENT")) != NULL) | ||
460 | homeDB = XrmGetFileDatabase(environment); | ||
461 | else homeDB = NULL; | ||
462 | |||
463 | |||
464 | XrmMergeDatabases(applicationDB, &rDB); | ||
465 | XrmMergeDatabases(serverDB, &rDB); | ||
466 | XrmMergeDatabases(homeDB, &rDB); | ||
467 | XrmMergeDatabases(commandlineDB, &rDB); | ||
468 | XrmMergeDatabases(usercommandlineDB, &rDB); | ||
469 | |||
470 | get_def_res(defres); | ||
471 | |||
472 | add_disp(defres); | ||
473 | |||
474 | } | ||
475 | |||
476 | |||
477 | void aX_open_second_disp(char *display_name, | ||
478 | aX_default_resources *defres) | ||
479 | { | ||
480 | char *disp_res; | ||
481 | |||
482 | XrmDatabase serverDB; | ||
483 | |||
484 | |||
485 | if((defres->disp = XOpenDisplay(display_name)) == NULL) { | ||
486 | fprintf(stderr, | ||
487 | "%s: aX_open_second_disp: cannot connect to X server %s\n", | ||
488 | prog_name, XDisplayName(display_name)); | ||
489 | exit(-1); | ||
490 | } | ||
491 | |||
492 | |||
493 | disp_res = XResourceManagerString(defres->disp); | ||
494 | |||
495 | if(disp_res) serverDB = XrmGetStringDatabase(disp_res); | ||
496 | else serverDB = NULL; | ||
497 | |||
498 | |||
499 | XrmMergeDatabases(serverDB, &rDB); | ||
500 | |||
501 | get_def_res(defres); | ||
502 | |||
503 | add_disp(defres); | ||
504 | } | ||
505 | |||
506 | /* | ||
507 | |||
508 | void smallwait(boolean event_prev) | ||
509 | { | ||
510 | event_prev = event_prev; | ||
511 | sleep(1); | ||
512 | } | ||
513 | |||
514 | */ | ||
515 | |||
516 | |||
517 | static void sigalrm_handler(int i) | ||
518 | { | ||
519 | i = i; | ||
520 | signal(SIGALRM, SIG_IGN); | ||
521 | } | ||
522 | |||
523 | |||
524 | static void smallwait(boolean event_prev) | ||
525 | { | ||
526 | struct itimerval value; | ||
527 | |||
528 | signal(SIGALRM, sigalrm_handler); | ||
529 | |||
530 | event_prev = event_prev; | ||
531 | |||
532 | value.it_interval.tv_sec = 0L; | ||
533 | value.it_interval.tv_usec = 0L; | ||
534 | value.it_value.tv_sec = 0L; | ||
535 | value.it_value.tv_usec = 100000L; | ||
536 | setitimer(ITIMER_REAL, &value, NULL); | ||
537 | |||
538 | pause(); | ||
539 | } | ||
540 | |||
541 | /* This aX_wait_all_muck needs to be cleared out! */ | ||
542 | |||
543 | void aX_wait_event(int eventtype) | ||
544 | { | ||
545 | XEvent ev; | ||
546 | event_proc_struct **curr; | ||
547 | int i; | ||
548 | disp_struct *dsp, *dsp1; | ||
549 | |||
550 | boolean event_prev; | ||
551 | |||
552 | |||
553 | start:; | ||
554 | |||
555 | if(disp_start.next_disp == NULL) { | ||
556 | fprintf(stderr, "%s: aX_wait_event: No connection to any display\n", | ||
557 | prog_name); | ||
558 | exit(-1); | ||
559 | } | ||
560 | |||
561 | dsp = disp_start.next_disp; | ||
562 | |||
563 | |||
564 | do { | ||
565 | event_prev = TRUE; | ||
566 | |||
567 | dsp1 = dsp; | ||
568 | if((disp_start.next_disp)->next_disp != NULL || | ||
569 | eventtype == AX_LOOK_EVENTS) | ||
570 | while(XPending(dsp->disp) == 0) { | ||
571 | dsp = dsp->next_disp; | ||
572 | if(dsp == NULL) dsp = disp_start.next_disp; | ||
573 | |||
574 | if(dsp == dsp1) { | ||
575 | if(eventtype == AX_LOOK_EVENTS) return; | ||
576 | |||
577 | smallwait(event_prev); | ||
578 | event_prev = FALSE; | ||
579 | dsp = dsp1 = disp_start.next_disp; | ||
580 | } | ||
581 | } | ||
582 | |||
583 | XNextEvent(dsp->disp, &ev); | ||
584 | #ifdef DEBUG_EVENTS | ||
585 | fprintf(stderr,"Event: %s (%i) in win: %li)\n", | ||
586 | event_info[ev.type].event_name, | ||
587 | ev.type, | ||
588 | ev.xany.window); | ||
589 | #endif | ||
590 | |||
591 | |||
592 | if(dsp->disp != ev.xany.display) | ||
593 | fprintf(stderr, "Ha! Event read from wrong display! Stupid XLib!!!\n"); | ||
594 | |||
595 | curr = &(event_info[ev.type].next_proc); | ||
596 | i = 0; | ||
597 | while(*curr != NULL) { | ||
598 | if((*curr)->disp == dsp->disp && | ||
599 | (!event_info[ev.type].win_given || | ||
600 | ev.xany.window == (*curr)->event_win) && | ||
601 | !(*curr)->done_proc) { | ||
602 | i++; | ||
603 | (*curr)->done_proc = TRUE; | ||
604 | disp_chain_modified = FALSE; | ||
605 | |||
606 | if((*curr)->event_proc != NULL) { | ||
607 | (*((*curr)->event_proc))(&ev, (*curr)->ptr); | ||
608 | } | ||
609 | |||
610 | if(disp_chain_modified) goto start; | ||
611 | curr = &(event_info[ev.type].next_proc); | ||
612 | } | ||
613 | else curr = &((*curr)->next_proc); | ||
614 | } | ||
615 | curr = &(event_info[ev.type].next_proc); | ||
616 | while(*curr != NULL) { | ||
617 | (*curr)->done_proc = FALSE; | ||
618 | curr = &((*curr)->next_proc); | ||
619 | } | ||
620 | |||
621 | if(i == 0) | ||
622 | fprintf(stderr, "%s: aX_wait_event: warning: " | ||
623 | "Unexpected event: %s (%i) in win: %li)\n", | ||
624 | prog_name, | ||
625 | event_info[ev.type].event_name, | ||
626 | ev.type, | ||
627 | ev.xany.window); | ||
628 | |||
629 | } while(eventtype != ev.type && eventtype != AX_ANY_EVENT); | ||
630 | } | ||
631 | |||
632 | |||
633 | void aX_look_events(void) | ||
634 | { | ||
635 | aX_wait_event(AX_LOOK_EVENTS); | ||
636 | } | ||
637 | |||
638 | char *aX_get_prog_res(const char *resname, const char* resclass) | ||
639 | { | ||
640 | XrmValue value; | ||
641 | char *str_type; | ||
642 | |||
643 | if(XrmGetResource(rDB, pname(resname), pclass(resclass), | ||
644 | &str_type, &value)) | ||
645 | return (char *)value.addr; | ||
646 | else return NULL; | ||
647 | } | ||
648 | |||
649 | char *aX_get_resource(const char *resname, const char* resclass) | ||
650 | { | ||
651 | XrmValue value; | ||
652 | char *str_type; | ||
653 | |||
654 | if(XrmGetResource(rDB, resname, resclass, | ||
655 | &str_type, &value)) | ||
656 | return (char *)value.addr; | ||
657 | else return NULL; | ||
658 | } | ||
659 | |||
660 | static long get_win_mask(Display *disp, Window win) | ||
661 | { | ||
662 | int evt; | ||
663 | event_proc_struct *ep; | ||
664 | long winmask; | ||
665 | |||
666 | for(evt = 0, winmask = 0; evt < EVENT_NUM; evt++) { | ||
667 | ep = event_info[evt].next_proc; | ||
668 | while(ep != NULL) { | ||
669 | if(ep->event_win == win && ep->disp == disp) winmask |= ep->event_mask; | ||
670 | ep = ep->next_proc; | ||
671 | } | ||
672 | } | ||
673 | return winmask; | ||
674 | } | ||
675 | |||
676 | |||
677 | |||
678 | void aX_add_event_proc(Display *disp, | ||
679 | Window win, | ||
680 | int eventtype, | ||
681 | void (*eventproc)(XEvent *, void *), | ||
682 | unsigned long eventmask, | ||
683 | void *ptr) | ||
684 | { | ||
685 | event_proc_struct **epp; | ||
686 | long winmask; | ||
687 | |||
688 | epp = &(event_info[eventtype].next_proc); | ||
689 | while(*epp != NULL) epp = &((*epp)->next_proc); | ||
690 | |||
691 | if((*epp = (event_proc_struct *) | ||
692 | malloc((size_t) sizeof(event_proc_struct))) == NULL) { | ||
693 | fprintf(stderr, | ||
694 | "%s: aX_add_event_proc_disp: Not enough memory.\n", prog_name); | ||
695 | exit(-1); | ||
696 | } | ||
697 | |||
698 | (*epp)->event_proc = eventproc; | ||
699 | (*epp)->ptr = ptr; | ||
700 | (*epp)->event_mask = eventmask; | ||
701 | (*epp)->disp = disp; | ||
702 | (*epp)->event_win = win; | ||
703 | (*epp)->done_proc = FALSE; | ||
704 | (*epp)->next_proc = NULL; | ||
705 | |||
706 | if(win) { | ||
707 | winmask = get_win_mask(disp, win); | ||
708 | XSelectInput(disp, win, winmask); | ||
709 | } | ||
710 | } | ||
711 | |||
712 | void aX_remove_event_proc(Display *disp, | ||
713 | Window win, | ||
714 | int eventtype, | ||
715 | void (*eventproc)(XEvent *, void *)) | ||
716 | { | ||
717 | event_proc_struct **epp; | ||
718 | event_proc_struct *tmp; | ||
719 | long winmask; | ||
720 | |||
721 | epp = &(event_info[eventtype].next_proc); | ||
722 | while(*epp != NULL) { | ||
723 | if((*epp)->disp == disp && | ||
724 | (*epp)->event_win == win && | ||
725 | (*epp)->event_proc == eventproc) { | ||
726 | tmp = (*epp)->next_proc; | ||
727 | free(*epp); | ||
728 | *epp = tmp; | ||
729 | if(win) { | ||
730 | winmask = get_win_mask(disp, win); | ||
731 | XSelectInput(disp, win, winmask); | ||
732 | } | ||
733 | return; | ||
734 | } | ||
735 | else epp = &((*epp)->next_proc); | ||
736 | } | ||
737 | fprintf(stderr, "%s: aX_remove_event_proc_disp: warning: " | ||
738 | "Could not remove event proc (event: %s (%i), window: %lX)\n", | ||
739 | prog_name, event_info[eventtype].event_name, eventtype, win); | ||
740 | |||
741 | } | ||
742 | |||
743 | void aX_close_one_disp(Display *disp) | ||
744 | { | ||
745 | /* int evt; | ||
746 | event_proc_struct **curr; */ | ||
747 | disp_struct *dsp, *dsp_tmp; | ||
748 | |||
749 | /* | ||
750 | for(evt = 0; evt < EVENT_NUM; evt++) { | ||
751 | curr = &(event_info[evt].next_proc); | ||
752 | while(*curr != NULL) { | ||
753 | if(disp == (*curr)->disp) { | ||
754 | aX_remove_event_proc_disp((*curr)->disp, (*curr)->event_win, | ||
755 | evt, (*curr)->event_proc); | ||
756 | curr = &(event_info[evt].next_proc); | ||
757 | } | ||
758 | else curr = &((*curr)->next_proc); | ||
759 | } | ||
760 | } | ||
761 | |||
762 | */ | ||
763 | |||
764 | for(dsp = &disp_start; dsp->next_disp->disp != disp; dsp = dsp->next_disp) | ||
765 | if(dsp->next_disp == NULL) { | ||
766 | fprintf(stderr, | ||
767 | "%s: aX_close_one_disp: warning: Trying to close unopened display.\n", | ||
768 | prog_name); | ||
769 | return; | ||
770 | } | ||
771 | |||
772 | XUnloadFont(dsp->next_disp->disp, dsp->next_disp->font->fid); | ||
773 | XCloseDisplay(dsp->next_disp->disp); | ||
774 | |||
775 | dsp_tmp = dsp->next_disp; | ||
776 | dsp->next_disp = dsp->next_disp->next_disp; | ||
777 | free(dsp_tmp); | ||
778 | |||
779 | disp_chain_modified = TRUE; | ||
780 | |||
781 | } | ||
782 | |||
783 | void aX_close_disp(void) | ||
784 | { | ||
785 | |||
786 | while(disp_start.next_disp != NULL) | ||
787 | aX_close_one_disp(disp_start.next_disp->disp); | ||
788 | |||
789 | } | ||
790 | |||
791 | |||
792 | unsigned long aX_get_color(Display *disp, int scr, unsigned long def_col, | ||
793 | const char *color_name) | ||
794 | { | ||
795 | XColor color_def; | ||
796 | Colormap def_map; | ||
797 | Screen *scr_ptr; | ||
798 | |||
799 | |||
800 | if(color_name == NULL) return def_col; | ||
801 | |||
802 | scr_ptr = ScreenOfDisplay(disp, scr); | ||
803 | def_map = DefaultColormapOfScreen(scr_ptr); | ||
804 | |||
805 | if(XParseColor(disp, def_map, color_name, &color_def)) { | ||
806 | if(XAllocColor(disp, def_map, &color_def)) | ||
807 | return color_def.pixel; | ||
808 | } | ||
809 | else fprintf(stderr, | ||
810 | "%s: aX_get_color: warning: Invalid color specification %s\n", | ||
811 | prog_name, color_name); | ||
812 | |||
813 | return def_col; | ||
814 | |||
815 | } | ||