00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "mit-copyright.h"
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef X_DISPLAY_MISSING
00018
00019 #include "X_driver.h"
00020 #include <X11/Xresource.h>
00021 #include "formatter.h"
00022 #include "mux.h"
00023 #include "variables.h"
00024 #include "error.h"
00025 #include "X_gram.h"
00026 #include "xselect.h"
00027 #include "ulong_dictionary.h"
00028
00029 char *app_instance;
00030 void xshowinit();
00031
00032
00033
00034
00035
00036 Display *dpy = NULL;
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #ifndef APPNAME
00049 #define APPNAME "jwgc"
00050 #endif
00051
00052
00053
00054
00055
00056 #ifndef APPCLASS
00057 #define APPCLASS "Jwgc"
00058 #endif
00059
00060
00061
00062
00063
00064
00065 static XrmDatabase x_resources = NULL;
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 static int
00077 convert_string_to_bool(text)
00078 string text;
00079 {
00080 if (!strcasecmp("yes", text) || !strcasecmp("y", text) ||
00081 !strcasecmp("true", text) || !strcasecmp("t", text) ||
00082 !strcasecmp("on", text))
00083 return (1);
00084 else if (!strcasecmp("no", text) || !strcasecmp("n", text) ||
00085 !strcasecmp("false", text) || !strcasecmp("f", text) ||
00086 !strcasecmp("off", text))
00087 return (0);
00088 else
00089 return (-1);
00090 }
00091
00092
00093
00094
00095
00096 char *
00097 get_string_resource(name, class)
00098 string name;
00099 string class;
00100 {
00101 string full_name, full_class;
00102 int status;
00103 char *type;
00104 XrmValue value;
00105
00106 full_name = string_Concat(APPNAME, ".");
00107 full_name = string_Concat2(full_name, name);
00108 full_class = string_Concat(APPCLASS, ".");
00109 full_class = string_Concat2(full_class, class);
00110
00111 status = XrmGetResource(x_resources, full_name, full_class, &type, &value);
00112 free(full_name);
00113 free(full_class);
00114
00115 if (status != True)
00116 return (NULL);
00117
00118 if (string_Neq(type, "String"))
00119 return (NULL);
00120
00121 return (value.addr);
00122 }
00123
00124
00125
00126
00127
00128 int
00129 get_bool_resource(name, class, default_value)
00130 string name;
00131 string class;
00132 int default_value;
00133 {
00134 int result;
00135 char *temp;
00136
00137 if (!(temp = get_string_resource(name, class)))
00138 return (default_value);
00139
00140 result = convert_string_to_bool(temp);
00141 if (result == -1)
00142 result = default_value;
00143
00144 return (result);
00145 }
00146
00147 static ulong_dictionary color_dict = NULL;
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 unsigned long
00161 x_string_to_color(name, def)
00162 char *name;
00163 unsigned long def;
00164 {
00165 ulong_dictionary_binding *binding;
00166 int exists;
00167 XColor xc;
00168
00169 if (name == NULL)
00170 return (def);
00171
00172 binding = ulong_dictionary_Define(color_dict, name, &exists);
00173
00174 if (exists) {
00175 return ((unsigned long) binding->value);
00176 }
00177 else {
00178 if (XParseColor(dpy, DefaultColormapOfScreen(DefaultScreenOfDisplay(dpy)),
00179 name, &xc)) {
00180 if (XAllocColor(dpy,
00181 DefaultColormapOfScreen(DefaultScreenOfDisplay(dpy)),
00182 &xc)) {
00183 binding->value = (ulong) xc.pixel;
00184 return (xc.pixel);
00185 }
00186 else {
00187 ERROR2("Error in XAllocColor on \"%s\": using default color\n",
00188 name);
00189 }
00190 }
00191 else {
00192 ERROR2("Error in XParseColor on \"%s\": using default color\n",
00193 name);
00194 }
00195 ulong_dictionary_Delete(color_dict, binding);
00196 return (def);
00197 }
00198
00199 }
00200
00201
00202
00203
00204
00205 static XrmOptionDescRec cmd_options[] = {
00206 {"+rv", "*reverseVideo", XrmoptionNoArg, (caddr_t) "off"},
00207 {"+synchronous", "*synchronous", XrmoptionNoArg, (caddr_t) "off"},
00208 {"-background", "*background", XrmoptionSepArg, (caddr_t) NULL},
00209 {"-bd", "*borderColor", XrmoptionSepArg, (caddr_t) NULL},
00210 {"-bg", "*background", XrmoptionSepArg, (caddr_t) NULL},
00211 {"-bordercolor", "*borderColor", XrmoptionSepArg, (caddr_t) NULL},
00212 {"-borderwidth", ".borderWidth", XrmoptionSepArg, (caddr_t) NULL},
00213 {"-bw", ".borderWidth", XrmoptionSepArg, (caddr_t) NULL},
00214 {"-display", ".display", XrmoptionSepArg, (caddr_t) NULL},
00215 {"-fg", "*foreground", XrmoptionSepArg, (caddr_t) NULL},
00216 {"-fn", "*font", XrmoptionSepArg, (caddr_t) NULL},
00217 {"-font", "*font", XrmoptionSepArg, (caddr_t) NULL},
00218 {"-foreground", "*foreground", XrmoptionSepArg, (caddr_t) NULL},
00219 {"-geometry", ".geometry", XrmoptionSepArg, (caddr_t) NULL},
00220 {"-iconname", ".iconName", XrmoptionSepArg, (caddr_t) NULL},
00221 {"-name", ".name", XrmoptionSepArg, (caddr_t) NULL},
00222 {"-reverse", "*reverseVideo", XrmoptionNoArg, (caddr_t) "on"},
00223 {"-rv", "*reverseVideo", XrmoptionNoArg, (caddr_t) "on"},
00224 {"-transient", "*transient", XrmoptionNoArg, (caddr_t) "on"},
00225 {"-synchronous", "*synchronous", XrmoptionNoArg, (caddr_t) "on"},
00226 {"-title", ".title", XrmoptionSepArg, (caddr_t) NULL},
00227 {"-xrm", NULL, XrmoptionResArg, (caddr_t) NULL}};
00228
00229 #define NUMBER_OF_OPTIONS ((sizeof (cmd_options))/ sizeof(cmd_options[0]))
00230
00231
00232
00233
00234
00235 int
00236 open_display_and_load_resources(pargc, argv)
00237 int *pargc;
00238 char **argv;
00239 {
00240 XrmDatabase temp_db1, temp_db2, temp_db3;
00241 char *filename, *res, *xdef;
00242 char dbasename[128];
00243 extern char *getenv();
00244
00245
00246 XrmInitialize();
00247
00248
00249
00250
00251
00252 XrmParseCommand(&x_resources, cmd_options, NUMBER_OF_OPTIONS, APPNAME,
00253 pargc, argv);
00254
00255
00256
00257
00258
00259 dpy = XOpenDisplay(get_string_resource("display", "display"));
00260 if (!dpy)
00261 return (1);
00262
00263
00264 sprintf(dbasename, "%s/jwgc_resources", DATADIR);
00265 temp_db1 = XrmGetFileDatabase(dbasename);
00266
00267
00268
00269
00270 xdef = XResourceManagerString(dpy);
00271 if (xdef)
00272 temp_db2 = XrmGetStringDatabase(xdef);
00273 else
00274 temp_db2 = NULL;
00275
00276
00277
00278
00279
00280
00281
00282 XrmMergeDatabases(temp_db2, &temp_db1);
00283
00284 #if XlibSpecificationRelease > 4
00285
00286 res = XScreenResourceString(DefaultScreenOfDisplay(dpy));
00287 if (res != NULL)
00288 XrmMergeDatabases(XrmGetStringDatabase(res), &temp_db1);
00289 #endif
00290
00291
00292
00293
00294 if ((filename = getenv("XENVIRONMENT"))) {
00295 temp_db3 = XrmGetFileDatabase(filename);
00296 XrmMergeDatabases(temp_db3, &temp_db1);
00297 }
00298 XrmMergeDatabases(x_resources, &temp_db1);
00299 x_resources = temp_db1;
00300
00301 return (0);
00302 }
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315 int
00316 X_driver_ioerror(display)
00317 Display *display;
00318 {
00319 ERROR2("X IO error on display '%s'--exiting\n", DisplayString(display));
00320 exit(1);
00321 }
00322
00323
00324
00325
00326
00327
00328 extern void x_get_input();
00329
00330
00331 int
00332 X_driver_init(drivername, notfirst, pargc, argv)
00333 char *drivername;
00334 char notfirst;
00335 int *pargc;
00336 char **argv;
00337 {
00338 string temp;
00339 int sync;
00340
00341
00342
00343
00344
00345
00346 if (open_display_and_load_resources(pargc, argv)) {
00347 ERROR("Unable to open X display -- disabling X driver.\n");
00348 return (1);
00349 }
00350
00351 XSetIOErrorHandler(X_driver_ioerror);
00352
00353
00354
00355
00356 if ((sync = get_bool_resource("synchronous", "Synchronous", 0)))
00357 XSynchronize(dpy, sync);
00358 if ((temp = get_string_resource("geometry", "Geometry")))
00359 var_set_variable("default_X_geometry", temp);
00360
00361 temp = strrchr(argv[0], '/');
00362
00363 app_instance = string_Copy(temp ? temp + 1 : argv[0]);
00364
00365 color_dict = ulong_dictionary_Create(37);
00366
00367 xshowinit();
00368 x_gram_init(dpy);
00369 xicccmInitAtoms(dpy);
00370
00371 mux_add_input_source(ConnectionNumber(dpy), x_get_input, dpy);
00372
00373 return (0);
00374 }
00375
00376 void
00377 X_driver_reset()
00378 {
00379 }
00380
00381
00382
00383
00384
00385
00386
00387 char *
00388 X_driver(text)
00389 string text;
00390 {
00391 string text_copy;
00392 desctype *desc;
00393 int numstr, numnl;
00394
00395 text_copy = string_Copy(text);
00396 desc = disp_get_cmds(text_copy, &numstr, &numnl);
00397
00398 xshow(dpy, desc, numstr, numnl);
00399
00400 free(text_copy);
00401 free_desc(desc);
00402 return (NULL);
00403 }
00404
00405 #endif