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
31 #include <X11/Xresource.h>
32 #include "new_memory.h"
33 #include "formatter.h"
35 #include "variables.h"
39 #include "unsigned_long_dictionary.h"
44 * dpy - the display we are outputting to
49 /****************************************************************************/
51 /* Code to deal with getting X resources: */
53 /****************************************************************************/
60 #define APPNAME "zwgc"
68 #define APPCLASS "Zwgc"
72 * x_resources - our X resources from application resources, command line,
73 * and user's X resources.
76 static XrmDatabase x_resources = NULL;
81 * int convert_string_to_bool(string text)
82 * Effects: If text represents yes/true/on, return 1. If text
83 * representes no/false/off, return 0. Otherwise,
87 static int convert_string_to_bool(text)
90 if (!strcasecmp("yes", text) || !strcasecmp("y", text) ||
91 !strcasecmp("true", text) || !strcasecmp("t", text) ||
92 !strcasecmp("on", text))
94 else if (!strcasecmp("no", text) || !strcasecmp("n", text) ||
95 !strcasecmp("false", text) || !strcasecmp("f", text) ||
96 !strcasecmp("off", text))
106 char *get_string_resource(name, class)
110 string full_name, full_class;
115 full_name = string_Concat(APPNAME, ".");
116 full_name = string_Concat2(full_name, name);
117 full_class = string_Concat(APPCLASS, ".");
118 full_class = string_Concat2(full_class, class);
120 status = XrmGetResource(x_resources, full_name, full_class, &type, &value);
127 if (string_Neq(type, "String"))
137 int get_bool_resource(name, class, default_value)
145 if (!(temp = get_string_resource(name, class)))
146 return(default_value);
148 result = convert_string_to_bool(temp);
150 result = default_value;
155 static unsigned_long_dictionary color_dict = NULL;
157 /* Requires: name points to color name or hex string. name must be free'd
158 * eventually by the caller.
159 * Effects: returns unsigned long pixel value, or default if the
160 * color is not known by the server. If name is NULL, returns
163 * comment: caches return values from X server round trips. If name does
164 * not resolve, this fact is NOT cached, and will result in a round
168 unsigned long x_string_to_color(name,def)
172 unsigned_long_dictionary_binding *binding;
179 binding = unsigned_long_dictionary_Define(color_dict,name,&exists);
182 return((unsigned long) binding->value);
184 if (XParseColor(dpy,DefaultColormapOfScreen(DefaultScreenOfDisplay(dpy)),
187 DefaultColormapOfScreen(DefaultScreenOfDisplay(dpy)),
189 binding->value = (unsigned long) xc.pixel;
192 ERROR2("Error in XAllocColor on \"%s\": using default color\n",
196 ERROR2("Error in XParseColor on \"%s\": using default color\n",
199 unsigned_long_dictionary_Delete(color_dict,binding);
206 * Standard X Toolkit command line options:
209 static XrmOptionDescRec cmd_options[] = {
210 {"+rv", "*reverseVideo", XrmoptionNoArg, (caddr_t) "off"},
211 {"+synchronous", "*synchronous", XrmoptionNoArg, (caddr_t) "off"},
212 {"-background", "*background", XrmoptionSepArg, (caddr_t) NULL},
213 {"-bd", "*borderColor", XrmoptionSepArg, (caddr_t) NULL},
214 {"-bg", "*background", XrmoptionSepArg, (caddr_t) NULL},
215 {"-bordercolor", "*borderColor", XrmoptionSepArg, (caddr_t) NULL},
216 {"-borderwidth", ".borderWidth", XrmoptionSepArg, (caddr_t) NULL},
217 {"-bw", ".borderWidth", XrmoptionSepArg, (caddr_t) NULL},
218 {"-display", ".display", XrmoptionSepArg, (caddr_t) NULL},
219 {"-fg", "*foreground", XrmoptionSepArg, (caddr_t) NULL},
220 {"-fn", "*font", XrmoptionSepArg, (caddr_t) NULL},
221 {"-font", "*font", XrmoptionSepArg, (caddr_t) NULL},
222 {"-foreground", "*foreground", XrmoptionSepArg, (caddr_t) NULL},
223 {"-geometry", ".geometry", XrmoptionSepArg, (caddr_t) NULL},
224 {"-iconname", ".iconName", XrmoptionSepArg, (caddr_t) NULL},
225 {"-name", ".name", XrmoptionSepArg, (caddr_t) NULL},
226 {"-reverse", "*reverseVideo", XrmoptionNoArg, (caddr_t) "on"},
227 {"-rv", "*reverseVideo", XrmoptionNoArg, (caddr_t) "on"},
228 {"-transient", "*transient", XrmoptionNoArg, (caddr_t) "on"},
229 {"-synchronous", "*synchronous", XrmoptionNoArg, (caddr_t) "on"},
230 {"-title", ".title", XrmoptionSepArg, (caddr_t) NULL},
231 {"-xrm", NULL, XrmoptionResArg, (caddr_t) NULL} };
233 #define NUMBER_OF_OPTIONS ((sizeof (cmd_options))/ sizeof(cmd_options[0]))
239 int open_display_and_load_resources(pargc, argv)
243 XrmDatabase temp_db1, temp_db2, temp_db3;
244 char *filename, *res, *xdef;
246 extern char *getenv();
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).
319 int X_driver_ioerror(display)
322 extern void finalize_zephyr();
324 ERROR2("X IO error on display '%s'--exiting\n", DisplayString(display));
328 /****************************************************************************/
330 /* Code to deal with initializing the driver: */
332 /****************************************************************************/
334 extern void x_get_input();
337 int X_driver_init(drivername, notfirst, pargc, argv)
347 * Attempt to open display and read resources, including from the
348 * command line. If fail, exit with error code, disabling this
351 if (open_display_and_load_resources(pargc, argv)) {
352 ERROR("Unable to open X display -- disabling X driver.\n");
356 XSetIOErrorHandler(X_driver_ioerror);
359 * For now, set some useful variables using resources:
361 if (sync=get_bool_resource("synchronous", "Synchronous", 0))
362 XSynchronize(dpy,sync);
363 if (temp = get_string_resource("geometry", "Geometry"))
364 var_set_variable("default_X_geometry", temp);
366 temp=strrchr(argv[0],'/');
368 app_instance=string_Copy(temp?temp+1:argv[0]);
370 color_dict = unsigned_long_dictionary_Create(37);
374 xicccmInitAtoms(dpy);
376 mux_add_input_source(ConnectionNumber(dpy), x_get_input, dpy);
381 void X_driver_reset()
385 /****************************************************************************/
387 /* The display routine itself: */
389 /****************************************************************************/
398 text_copy = string_Copy(text);
399 desc = disp_get_cmds(text_copy, &numstr, &numnl);
401 xshow(dpy, desc, numstr, numnl);
408 #endif /* X_DISPLAY_MISSING */