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"
46 * dpy - the display we are outputting to
51 /****************************************************************************/
53 /* Code to deal with getting X resources: */
55 /****************************************************************************/
62 #define APPNAME "zwgc"
70 #define APPCLASS "Zwgc"
74 * x_resources - our X resources from application resources, command line,
75 * and user's X resources.
78 static XrmDatabase x_resources = NULL;
83 * int convert_string_to_bool(string text)
84 * Effects: If text represents yes/true/on, return 1. If text
85 * representes no/false/off, return 0. Otherwise,
90 convert_string_to_bool(string text)
92 if (!strcasecmp("yes", text) || !strcasecmp("y", text) ||
93 !strcasecmp("true", text) || !strcasecmp("t", text) ||
94 !strcasecmp("on", text))
96 else if (!strcasecmp("no", text) || !strcasecmp("n", text) ||
97 !strcasecmp("false", text) || !strcasecmp("f", text) ||
98 !strcasecmp("off", text))
109 get_string_resource(string name,
112 string full_name, full_class;
117 full_name = string_Concat(APPNAME, ".");
118 full_name = string_Concat2(full_name, name);
119 full_class = string_Concat(APPCLASS, ".");
120 full_class = string_Concat2(full_class, class);
122 status = XrmGetResource(x_resources, full_name, full_class, &type, &value);
129 if (string_Neq(type, "String"))
140 get_bool_resource(string name,
147 if (!(temp = get_string_resource(name, class)))
148 return(default_value);
150 result = convert_string_to_bool(temp);
152 result = default_value;
157 static unsigned_long_dictionary color_dict = NULL;
159 /* Requires: name points to color name or hex string. name must be free'd
160 * eventually by the caller.
161 * Effects: returns unsigned long pixel value, or default if the
162 * color is not known by the server. If name is NULL, returns
165 * comment: caches return values from X server round trips. If name does
166 * not resolve, this fact is NOT cached, and will result in a round
171 x_string_to_color(char *name,
174 unsigned_long_dictionary_binding *binding;
181 binding = unsigned_long_dictionary_Define(color_dict,name,&exists);
184 return((unsigned long) binding->value);
186 if (XParseColor(dpy,DefaultColormapOfScreen(DefaultScreenOfDisplay(dpy)),
189 DefaultColormapOfScreen(DefaultScreenOfDisplay(dpy)),
191 binding->value = (unsigned long) xc.pixel;
194 ERROR2("Error in XAllocColor on \"%s\": using default color\n",
198 ERROR2("Error in XParseColor on \"%s\": using default color\n",
201 unsigned_long_dictionary_Delete(color_dict,binding);
208 * Standard X Toolkit command line options:
211 static XrmOptionDescRec cmd_options[] = {
212 {"+rv", "*reverseVideo", XrmoptionNoArg, (caddr_t) "off"},
213 {"+synchronous", "*synchronous", XrmoptionNoArg, (caddr_t) "off"},
214 {"-background", "*background", XrmoptionSepArg, (caddr_t) NULL},
215 {"-bd", "*borderColor", XrmoptionSepArg, (caddr_t) NULL},
216 {"-bg", "*background", XrmoptionSepArg, (caddr_t) NULL},
217 {"-bordercolor", "*borderColor", XrmoptionSepArg, (caddr_t) NULL},
218 {"-borderwidth", ".borderWidth", XrmoptionSepArg, (caddr_t) NULL},
219 {"-bw", ".borderWidth", XrmoptionSepArg, (caddr_t) NULL},
220 {"-display", ".display", XrmoptionSepArg, (caddr_t) NULL},
221 {"-fg", "*foreground", XrmoptionSepArg, (caddr_t) NULL},
222 {"-fn", "*font", XrmoptionSepArg, (caddr_t) NULL},
223 {"-font", "*font", XrmoptionSepArg, (caddr_t) NULL},
224 {"-foreground", "*foreground", XrmoptionSepArg, (caddr_t) NULL},
225 {"-geometry", ".geometry", XrmoptionSepArg, (caddr_t) NULL},
226 {"-iconname", ".iconName", XrmoptionSepArg, (caddr_t) NULL},
227 {"-name", ".name", XrmoptionSepArg, (caddr_t) NULL},
228 {"-reverse", "*reverseVideo", XrmoptionNoArg, (caddr_t) "on"},
229 {"-rv", "*reverseVideo", XrmoptionNoArg, (caddr_t) "on"},
230 {"-transient", "*transient", XrmoptionNoArg, (caddr_t) "on"},
231 {"-synchronous", "*synchronous", XrmoptionNoArg, (caddr_t) "on"},
232 {"-title", ".title", XrmoptionSepArg, (caddr_t) NULL},
233 {"-xrm", NULL, XrmoptionResArg, (caddr_t) NULL} };
235 #define NUMBER_OF_OPTIONS ((sizeof (cmd_options))/ sizeof(cmd_options[0]))
242 open_display_and_load_resources(int *pargc,
245 XrmDatabase temp_db1, temp_db2, temp_db3;
246 char *filename, *res, *xdef;
249 /* Initialize X resource manager: */
253 * Parse X toolkit command line arguments (including -display)
256 XrmParseCommand(&x_resources, cmd_options, NUMBER_OF_OPTIONS, APPNAME,
260 * Try and open the display using the display specified if given.
261 * If can't open the display, return an error code.
263 dpy = XOpenDisplay(get_string_resource("display", "display"));
267 /* Read in our application-specific resources: */
268 sprintf(dbasename, "%s/zephyr/zwgc_resources", DATADIR);
269 temp_db1 = XrmGetFileDatabase(dbasename);
272 * Get resources from the just opened display:
274 xdef = XResourceManagerString(dpy);
276 temp_db2 = XrmGetStringDatabase(xdef);
281 * Merge the 4 sets of resources together such that when searching
282 * for resources, they are checking in the following order:
283 * command arguments, XENVIRONMENT resources, server resources,
284 * application resources
286 XrmMergeDatabases(temp_db2, &temp_db1);
288 #if XlibSpecificationRelease > 4
289 /* X11 R5 per-screen resources */
290 res = XScreenResourceString (DefaultScreenOfDisplay (dpy));
292 XrmMergeDatabases(XrmGetStringDatabase(res), &temp_db1);
296 * Get XENVIRONMENT resources, if they exist, and merge
298 filename = getenv("XENVIRONMENT");
301 temp_db3 = XrmGetFileDatabase(filename);
302 XrmMergeDatabases(temp_db3, &temp_db1);
304 XrmMergeDatabases(x_resources, &temp_db1);
305 x_resources = temp_db1;
311 * X_driver_ioerror: called by Xlib in case of an X IO error.
312 * Shouldn't return (according to man page).
314 * on IO error, we clean up and exit.
316 * XXX it would be better to set mux_end_loop_p, but we can't return to
317 * get there (Xlib will exit if this routine returns).
322 X_driver_ioerror(Display *display)
324 ERROR2("X IO error on display '%s'--exiting\n", DisplayString(display));
328 /****************************************************************************/
330 /* Code to deal with initializing the driver: */
332 /****************************************************************************/
336 X_driver_init(char *drivername,
345 * Attempt to open display and read resources, including from the
346 * command line. If fail, exit with error code, disabling this
349 if (open_display_and_load_resources(pargc, argv)) {
350 ERROR("Unable to open X display -- disabling X driver.\n");
354 XSetIOErrorHandler(X_driver_ioerror);
357 * For now, set some useful variables using resources:
359 sync = get_bool_resource("synchronous", "Synchronous", 0);
361 XSynchronize(dpy, sync);
362 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), (void(*)(void *))x_get_input, dpy);
386 /****************************************************************************/
388 /* The display routine itself: */
390 /****************************************************************************/
393 X_driver(string text)
399 text_copy = string_Copy(text);
400 desc = disp_get_cmds(text_copy, &numstr, &numnl);
402 xshow(dpy, desc, numstr, numnl);
409 #endif /* X_DISPLAY_MISSING */