1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It is one of the source files comprising zwgc, the Zephyr WindowGram
5 * Created by: Marc Horowitz <marc@athena.mit.edu>
9 * Copyright (c) 1989 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, see the file
16 #if (!defined(lint) && !defined(SABER))
17 static const char rcsid_X_driver_c[] = "$Id$";
20 #include <zephyr/mit-copyright.h>
22 /****************************************************************************/
26 /****************************************************************************/
28 #ifndef X_DISPLAY_MISSING
30 #include "new_string.h"
32 #include <X11/Xresource.h>
33 #include "new_memory.h"
34 #include "formatter.h"
36 #include "variables.h"
40 #include "unsigned_long_dictionary.h"
45 * dpy - the display we are outputting to
50 /****************************************************************************/
52 /* Code to deal with getting X resources: */
54 /****************************************************************************/
61 #define APPNAME "zwgc"
69 #define APPCLASS "Zwgc"
73 * x_resources - our X resources from application resources, command line,
74 * and user's X resources.
77 static XrmDatabase x_resources = NULL;
82 * int convert_string_to_bool(string text)
83 * Effects: If text represents yes/true/on, return 1. If text
84 * representes no/false/off, return 0. Otherwise,
89 convert_string_to_bool(string text)
91 if (!strcasecmp("yes", text) || !strcasecmp("y", text) ||
92 !strcasecmp("true", text) || !strcasecmp("t", text) ||
93 !strcasecmp("on", text))
95 else if (!strcasecmp("no", text) || !strcasecmp("n", text) ||
96 !strcasecmp("false", text) || !strcasecmp("f", text) ||
97 !strcasecmp("off", text))
108 get_string_resource(string name,
111 string full_name, full_class;
116 full_name = string_Concat(APPNAME, ".");
117 full_name = string_Concat2(full_name, name);
118 full_class = string_Concat(APPCLASS, ".");
119 full_class = string_Concat2(full_class, class);
121 status = XrmGetResource(x_resources, full_name, full_class, &type, &value);
128 if (string_Neq(type, "String"))
139 get_bool_resource(string name,
146 if (!(temp = get_string_resource(name, class)))
147 return(default_value);
149 result = convert_string_to_bool(temp);
151 result = default_value;
156 static unsigned_long_dictionary color_dict = NULL;
158 /* Requires: name points to color name or hex string. name must be free'd
159 * eventually by the caller.
160 * Effects: returns unsigned long pixel value, or default if the
161 * color is not known by the server. If name is NULL, returns
164 * comment: caches return values from X server round trips. If name does
165 * not resolve, this fact is NOT cached, and will result in a round
170 x_string_to_color(char *name,
173 unsigned_long_dictionary_binding *binding;
180 binding = unsigned_long_dictionary_Define(color_dict,name,&exists);
183 return((unsigned long) binding->value);
185 if (XParseColor(dpy,DefaultColormapOfScreen(DefaultScreenOfDisplay(dpy)),
188 DefaultColormapOfScreen(DefaultScreenOfDisplay(dpy)),
190 binding->value = (unsigned long) xc.pixel;
193 ERROR2("Error in XAllocColor on \"%s\": using default color\n",
197 ERROR2("Error in XParseColor on \"%s\": using default color\n",
200 unsigned_long_dictionary_Delete(color_dict,binding);
207 * Standard X Toolkit command line options:
210 static XrmOptionDescRec cmd_options[] = {
211 {"+rv", "*reverseVideo", XrmoptionNoArg, (caddr_t) "off"},
212 {"+synchronous", "*synchronous", XrmoptionNoArg, (caddr_t) "off"},
213 {"-background", "*background", XrmoptionSepArg, (caddr_t) NULL},
214 {"-bd", "*borderColor", XrmoptionSepArg, (caddr_t) NULL},
215 {"-bg", "*background", XrmoptionSepArg, (caddr_t) NULL},
216 {"-bordercolor", "*borderColor", XrmoptionSepArg, (caddr_t) NULL},
217 {"-borderwidth", ".borderWidth", XrmoptionSepArg, (caddr_t) NULL},
218 {"-bw", ".borderWidth", XrmoptionSepArg, (caddr_t) NULL},
219 {"-display", ".display", XrmoptionSepArg, (caddr_t) NULL},
220 {"-fg", "*foreground", XrmoptionSepArg, (caddr_t) NULL},
221 {"-fn", "*font", XrmoptionSepArg, (caddr_t) NULL},
222 {"-font", "*font", XrmoptionSepArg, (caddr_t) NULL},
223 {"-foreground", "*foreground", XrmoptionSepArg, (caddr_t) NULL},
224 {"-geometry", ".geometry", XrmoptionSepArg, (caddr_t) NULL},
225 {"-iconname", ".iconName", XrmoptionSepArg, (caddr_t) NULL},
226 {"-name", ".name", XrmoptionSepArg, (caddr_t) NULL},
227 {"-reverse", "*reverseVideo", XrmoptionNoArg, (caddr_t) "on"},
228 {"-rv", "*reverseVideo", XrmoptionNoArg, (caddr_t) "on"},
229 {"-transient", "*transient", XrmoptionNoArg, (caddr_t) "on"},
230 {"-synchronous", "*synchronous", XrmoptionNoArg, (caddr_t) "on"},
231 {"-title", ".title", XrmoptionSepArg, (caddr_t) NULL},
232 {"-xrm", NULL, XrmoptionResArg, (caddr_t) NULL} };
234 #define NUMBER_OF_OPTIONS ((sizeof (cmd_options))/ sizeof(cmd_options[0]))
241 open_display_and_load_resources(int *pargc,
244 XrmDatabase temp_db1, temp_db2, temp_db3;
245 char *filename, *res, *xdef;
248 /* Initialize X resource manager: */
252 * Parse X toolkit command line arguments (including -display)
255 XrmParseCommand(&x_resources, cmd_options, NUMBER_OF_OPTIONS, APPNAME,
259 * Try and open the display using the display specified if given.
260 * If can't open the display, return an error code.
262 dpy = XOpenDisplay(get_string_resource("display", "display"));
266 /* Read in our application-specific resources: */
267 sprintf(dbasename, "%s/zephyr/zwgc_resources", DATADIR);
268 temp_db1 = XrmGetFileDatabase(dbasename);
271 * Get resources from the just opened display:
273 xdef = XResourceManagerString(dpy);
275 temp_db2 = XrmGetStringDatabase(xdef);
280 * Merge the 4 sets of resources together such that when searching
281 * for resources, they are checking in the following order:
282 * command arguments, XENVIRONMENT resources, server resources,
283 * application resources
285 XrmMergeDatabases(temp_db2, &temp_db1);
287 #if XlibSpecificationRelease > 4
288 /* X11 R5 per-screen resources */
289 res = XScreenResourceString (DefaultScreenOfDisplay (dpy));
291 XrmMergeDatabases(XrmGetStringDatabase(res), &temp_db1);
295 * Get XENVIRONMENT resources, if they exist, and merge
297 if (filename = getenv("XENVIRONMENT"))
299 temp_db3 = XrmGetFileDatabase(filename);
300 XrmMergeDatabases(temp_db3, &temp_db1);
302 XrmMergeDatabases(x_resources, &temp_db1);
303 x_resources = temp_db1;
309 * X_driver_ioerror: called by Xlib in case of an X IO error.
310 * Shouldn't return (according to man page).
312 * on IO error, we clean up and exit.
314 * XXX it would be better to set mux_end_loop_p, but we can't return to
315 * get there (Xlib will exit if this routine returns).
320 X_driver_ioerror(Display *display)
322 ERROR2("X IO error on display '%s'--exiting\n", DisplayString(display));
326 /****************************************************************************/
328 /* Code to deal with initializing the driver: */
330 /****************************************************************************/
334 X_driver_init(char *drivername,
343 * Attempt to open display and read resources, including from the
344 * command line. If fail, exit with error code, disabling this
347 if (open_display_and_load_resources(pargc, argv)) {
348 ERROR("Unable to open X display -- disabling X driver.\n");
352 XSetIOErrorHandler(X_driver_ioerror);
355 * For now, set some useful variables using resources:
357 if (sync=get_bool_resource("synchronous", "Synchronous", 0))
358 XSynchronize(dpy,sync);
359 if (temp = get_string_resource("geometry", "Geometry"))
360 var_set_variable("default_X_geometry", temp);
362 temp=strrchr(argv[0],'/');
364 app_instance=string_Copy(temp?temp+1:argv[0]);
366 color_dict = unsigned_long_dictionary_Create(37);
370 xicccmInitAtoms(dpy);
372 mux_add_input_source(ConnectionNumber(dpy), (void(*)(void *))x_get_input, dpy);
382 /****************************************************************************/
384 /* The display routine itself: */
386 /****************************************************************************/
389 X_driver(string text)
395 text_copy = string_Copy(text);
396 desc = disp_get_cmds(text_copy, &numstr, &numnl);
398 xshow(dpy, desc, numstr, numnl);
405 #endif /* X_DISPLAY_MISSING */