comparison lcdemu/xrm.c @ 903:312778104f54

lcdemu started, compiles and runs w/o actual functionality
author Space Falcon <falcon@ivan.Harhan.ORG>
date Mon, 07 Sep 2015 08:34:37 +0000
parents
children
comparison
equal deleted inserted replaced
902:4423039aeb4b 903:312778104f54
1 /*
2 * LCDemu based on HECterm by the same author
3 * Xrm functions
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <strings.h>
10 #include <X11/Xlib.h>
11 #include <X11/Xresource.h>
12 #include <X11/Xutil.h>
13 #include "globals.h"
14
15 static char appdefaults_pathname[] =
16 "/usr/local/share/freecalypso/lcdemu-defaults";
17
18 load_resources()
19 {
20 xrmquark_topclass = XrmStringToQuark("LCDemu");
21 xrmquark_topinstance = XrmStringToQuark(proginstancename);
22 xrmdb_defaults = XrmGetFileDatabase(appdefaults_pathname);
23 xrmdb_displayres =
24 XrmGetStringDatabase(XResourceManagerString(mydisplay));
25 }
26
27 /*
28 * The following function looks up a resource in all of our databases
29 * and returns a pointer (char *) to the value in a malloced buffer that
30 * can be freed when it is no longer needed. My reading of X11R4
31 * documentation indicates that resource values returned from Xrm functions
32 * are not necessarily NUL-terminated (no claim is made that they are
33 * and XrmValue structure has a size field), which is why I copy to
34 * my own buffer and NUL-terminate it there.
35 *
36 * Returns NULL pointer if not found in any of the databases.
37 */
38 char *
39 xrm_lookup(instquarks, classquarks)
40 XrmQuark *instquarks, *classquarks;
41 {
42 XrmRepresentation reptype;
43 XrmValue value;
44 register char *buf;
45
46 if (XrmQGetResource(xrmdb_cmdline, instquarks, classquarks, &reptype,
47 &value))
48 goto found;
49 if (XrmQGetResource(xrmdb_displayres, instquarks, classquarks, &reptype,
50 &value))
51 goto found;
52 if (XrmQGetResource(xrmdb_defaults, instquarks, classquarks, &reptype,
53 &value))
54 goto found;
55 return(NULL);
56 found: buf = malloc(value.size + 1);
57 if (!buf) {
58 perror("malloc");
59 exit(1);
60 }
61 bcopy(value.addr, buf, value.size);
62 buf[value.size] = '\0';
63 return(buf);
64 }
65
66 parse_boolean_resource(str)
67 register char *str;
68 {
69 if (!strcasecmp(str, "on") || !strcasecmp(str, "true") ||
70 !strcasecmp(str, "yes"))
71 return(1);
72 if (!strcasecmp(str, "off") || !strcasecmp(str, "false") ||
73 !strcasecmp(str, "no"))
74 return(0);
75 return(atoi(str));
76 }
77
78 get_boolean_resource(resource, def)
79 char *resource;
80 int def;
81 {
82 XrmQuark instquarks[3], classquarks[3];
83 register char *cp;
84 register int i;
85
86 instquarks[0] = xrmquark_topinstance;
87 classquarks[0] = xrmquark_topclass;
88 classquarks[1] = instquarks[1] = XrmStringToQuark(resource);
89 instquarks[2] = classquarks[2] = NULLQUARK;
90 cp = xrm_lookup(instquarks, classquarks);
91 if (cp) {
92 i = parse_boolean_resource(cp);
93 free(cp);
94 } else
95 i = def;
96 return(i);
97 }