1 /*
2 * This file is part of x48, an emulator of the HP-48sx Calculator.
3 * Copyright (C) 1994 Eddie C. Dost (ecd@dressler.de)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 /* $Log: resources.c,v $
21 * Revision 1.3 1995/01/11 18:20:01 ecd
22 * major update to support HP48 G/GX
23 *
24 * Revision 1.2 1994/12/07 20:20:50 ecd
25 * more resource get functions
26 *
27 * Revision 1.2 1994/12/07 20:20:50 ecd
28 * more resource get functions
29 *
30 * Revision 1.1 1994/12/07 10:15:47 ecd
31 * Initial revision
32 *
33 *
34 * $Id: resources.c,v 1.3 1995/01/11 18:20:01 ecd Exp ecd $
35 */
36
37 /* xscreensaver, Copyright (c) 1992 Jamie Zawinski <jwz@lucid.com>
38 *
39 * Permission to use, copy, modify, distribute, and sell this software and its
40 * documentation for any purpose is hereby granted without fee, provided that
41 * the above copyright notice appear in all copies and that both that
42 * copyright notice and this permission notice appear in supporting
43 * documentation. No representations are made about the suitability of this
44 * software for any purpose. It is provided "as is" without express or
45 * implied warranty.
46 */
47
48 #include "global.h"
49
50 #include <stdlib.h>
51 #include <string.h>
52
53 #include <stdio.h>
54 #include <X11/Xlib.h>
55 #include <X11/Xutil.h>
56 #include <X11/Xresource.h>
57
58 #include "resources.h"
59 #include "disasm.h"
60 #include "errors.h"
61
62 XrmDatabase rdb = (XrmDatabase)0;
63
64 int verbose;
65 int quiet;
66 int useTerminal;
67 int useSerial;
68 char *serialLine;
69 int useXShm;
70 int useDebugger;
71 int netbook;
72 int throttle;
73 int initialize;
74 int resetOnStartup;
75 char *romFileName;
76 char *homeDirectory;
77
78 void
79 #ifdef __FunctionProto__
get_resources(void)80 get_resources(void)
81 #else
82 get_resources()
83 #endif
84 {
85 if (get_boolean_resource("printVersion", "PrintVersion"))
86 show_version();
87 if (get_boolean_resource("printCopyright", "PrintCopyright"))
88 show_copyright();
89 if (get_boolean_resource("printWarranty", "PrintWarranty"))
90 show_warranty();
91
92 verbose = get_boolean_resource("verbose", "Verbose");
93 quiet = get_boolean_resource("quiet", "Quiet");
94
95 useXShm = get_boolean_resource("useXShm", "UseXShm");
96
97 useTerminal = get_boolean_resource("useTerminal", "UseTerminal");
98 useSerial = get_boolean_resource("useSerial", "UseSerial");
99 serialLine = get_string_resource("serialLine", "SerialLine");
100
101 initialize = get_boolean_resource("completeInitialize",
102 "CompleteInitialize");
103 resetOnStartup = get_boolean_resource("resetOnStartup",
104 "ResetOnStartup");
105 romFileName = get_string_resource("romFileName", "RomFileName");
106 homeDirectory = get_string_resource("homeDirectory", "HomeDirectory");
107
108 useDebugger = get_boolean_resource("useDebugger", "UseDebugger");
109 disassembler_mode = get_mnemonic_resource("disassemblerMnemonics",
110 "DisassemblerMnemonics");
111
112 netbook = get_boolean_resource("netbook", "Netbook");
113
114 throttle = get_boolean_resource("throttle", "Throttle");
115 }
116
117 char *
118 #ifdef __FunctionProto__
get_string_resource_from_db(XrmDatabase db,char * name,char * class)119 get_string_resource_from_db (XrmDatabase db, char *name, char *class)
120 #else
121 get_string_resource_from_db (db, name, class)
122 XrmDatabase db;
123 char *name;
124 char *class;
125 #endif
126 {
127 XrmValue value;
128 char *type;
129 char full_name [1024], full_class [1024];
130
131 strcpy (full_name, res_name);
132 strcat (full_name, ".");
133 strcat (full_name, name);
134 strcpy (full_class, res_class);
135 strcat (full_class, ".");
136 strcat (full_class, class);
137 if (XrmGetResource (db, full_name, full_class, &type, &value))
138 {
139 char *str = (char *) malloc (value.size + 1);
140 strncpy (str, (char *) value.addr, value.size);
141 str [value.size] = 0;
142 return str;
143 }
144 return (char *)0;
145 }
146
147 char *
148 #ifdef __FunctionProto__
get_string_resource(char * name,char * class)149 get_string_resource (char *name, char *class)
150 #else
151 get_string_resource (name, class)
152 char *name;
153 char *class;
154 #endif
155 {
156 return get_string_resource_from_db(rdb, name, class);
157 }
158
159 int
160 #ifdef __FunctionProto__
get_mnemonic_resource(char * name,char * class)161 get_mnemonic_resource (char *name, char *class)
162 #else
163 get_mnemonic_resource (name, class)
164 char *name;
165 char *class;
166 #endif
167 {
168 char *tmp, buf [100];
169 char *s = get_string_resource (name, class);
170 char *os = s;
171
172 if (! s) return CLASS_MNEMONICS;
173 for (tmp = buf; *s; s++)
174 *tmp++ = isupper (*s) ? _tolower (*s) : *s;
175 *tmp = 0;
176 free (os);
177
178 if (!strcmp (buf, "hp"))
179 return HP_MNEMONICS;
180 if (!strcmp (buf, "class"))
181 return CLASS_MNEMONICS;
182 fprintf (stderr, "%s: %s must be one of \'HP\' or \'class\', not %s.\n",
183 progname, name, buf);
184 return CLASS_MNEMONICS;
185 }
186
187 int
188 #ifdef __FunctionProto__
get_boolean_resource(char * name,char * class)189 get_boolean_resource (char *name, char *class)
190 #else
191 get_boolean_resource (name, class)
192 char *name;
193 char *class;
194 #endif
195 {
196 char *tmp, buf [100];
197 char *s = get_string_resource (name, class);
198 char *os = s;
199 if (! s) return 0;
200 for (tmp = buf; *s; s++)
201 *tmp++ = isupper (*s) ? _tolower (*s) : *s;
202 *tmp = 0;
203 free (os);
204
205 if (!strcmp (buf, "on") || !strcmp (buf, "true") || !strcmp (buf, "yes"))
206 return 1;
207 if (!strcmp (buf, "off") || !strcmp (buf, "false") || !strcmp (buf, "no"))
208 return 0;
209 fprintf (stderr, "%s: %s must be boolean, not %s.\n",
210 progname, name, buf);
211 return 0;
212 }
213
214 int
215 #ifdef __FunctionProto__
get_integer_resource(char * name,char * class)216 get_integer_resource (char *name, char *class)
217 #else
218 get_integer_resource (name, class)
219 char *name;
220 char *class;
221 #endif
222 {
223 int val;
224 char c, *s = get_string_resource (name, class);
225 if (!s) return 0;
226 if (1 == sscanf (s, " %d %c", &val, &c))
227 {
228 free (s);
229 return val;
230 }
231 fprintf (stderr, "%s: %s must be an integer, not %s.\n",
232 progname, name, s);
233 free (s);
234 return 0;
235 }
236
237 unsigned int
238 #ifdef __FunctionProto__
get_pixel_resource(char * name,char * class,Display * dpy,Colormap cmap)239 get_pixel_resource (char *name, char *class, Display *dpy,
240 Colormap cmap)
241 #else
242 get_pixel_resource (name, class, dpy, cmap)
243 char *name;
244 char *class;
245 Display *dpy;
246 Colormap cmap;
247 #endif
248 {
249 XColor color;
250 char *s = get_string_resource (name, class);
251 if (!s) goto DEFAULT;
252
253 if (! XParseColor (dpy, cmap, s, &color))
254 {
255 fprintf (stderr, "%s: can't parse color %s\n", progname, s);
256 goto DEFAULT;
257 }
258 if (! XAllocColor (dpy, cmap, &color))
259 {
260 fprintf (stderr, "%s: couldn't allocate color %s\n", progname, s);
261 goto DEFAULT;
262 }
263 free (s);
264 return color.pixel;
265 DEFAULT:
266 if (s) free (s);
267 return (strcmp (class, "Background")
268 ? WhitePixel (dpy, DefaultScreen (dpy))
269 : BlackPixel (dpy, DefaultScreen (dpy)));
270 }
271
272 static Visual *
273 #ifdef __FunctionProto__
pick_visual_of_class(Display * dpy,int visual_class,unsigned int * depth)274 pick_visual_of_class (Display *dpy, int visual_class, unsigned int *depth)
275 #else
276 pick_visual_of_class (dpy, visual_class, depth)
277 Display *dpy;
278 int visual_class;
279 unsigned int *depth;
280 #endif
281 {
282 XVisualInfo vi_in, *vi_out;
283 int out_count;
284
285 vi_in.class = visual_class;
286 vi_in.screen = DefaultScreen(dpy);
287 vi_out = XGetVisualInfo(dpy, VisualClassMask|VisualScreenMask,
288 &vi_in, &out_count);
289 if (vi_out)
290 { /* choose the 'best' one, if multiple */
291 int i, best;
292 Visual *visual;
293 for (i = 0, best = 0; i < out_count; i++)
294 if (vi_out[i].depth > vi_out[best].depth)
295 best = i;
296 visual = vi_out[best].visual;
297 *depth = vi_out[best].depth;
298 XFree ((char *)vi_out);
299 return visual;
300 }
301 else
302 {
303 *depth = DefaultDepth(dpy, DefaultScreen(dpy));
304 return DefaultVisual(dpy, DefaultScreen(dpy));
305 }
306 }
307
308 static Visual *
309 #ifdef __FunctionProto__
id_to_visual(Display * dpy,int id,unsigned int * depth)310 id_to_visual (Display *dpy, int id, unsigned int *depth)
311 #else
312 id_to_visual (dpy, id, depth)
313 Display *dpy;
314 int id;
315 unsigned int *depth;
316 #endif
317 {
318 XVisualInfo vi_in, *vi_out;
319 int out_count;
320
321 vi_in.screen = DefaultScreen(dpy);
322 vi_in.visualid = id;
323 vi_out = XGetVisualInfo(dpy, VisualScreenMask|VisualIDMask,
324 &vi_in, &out_count);
325 if (vi_out)
326 {
327 Visual *v = vi_out[0].visual;
328 *depth = vi_out[0].depth;
329 XFree((char *)vi_out);
330 return v;
331 }
332 return 0;
333 }
334
335 Visual *
336 #ifdef __FunctionProto__
get_visual_resource(Display * dpy,char * name,char * class,unsigned int * depth)337 get_visual_resource(Display *dpy, char *name, char *class, unsigned int *depth)
338 #else
339 get_visual_resource(dpy, name, class, depth)
340 Display *dpy;
341 char *name;
342 char *class;
343 unsigned int *depth;
344 #endif
345 {
346 char c;
347 char *tmp, *s;
348 int vclass;
349 int id;
350
351 s = get_string_resource(name, class);
352 if (s)
353 for (tmp = s; *tmp; tmp++)
354 if (isupper(*tmp)) *tmp = _tolower(*tmp);
355
356 if (!s || !strcmp(s, "default")) vclass = -1;
357 else if (!strcmp (s, "staticgray")) vclass = StaticGray;
358 else if (!strcmp (s, "staticcolor")) vclass = StaticColor;
359 else if (!strcmp (s, "truecolor")) vclass = TrueColor;
360 else if (!strcmp (s, "grayscale")) vclass = GrayScale;
361 else if (!strcmp (s, "pseudocolor")) vclass = PseudoColor;
362 else if (!strcmp (s, "directcolor")) vclass = DirectColor;
363 else if (1 == sscanf (s, " %d %c", &id, &c)) vclass = -2;
364 else if (1 == sscanf (s, " 0x%x %c", &id, &c)) vclass = -2;
365 else
366 {
367 fprintf (stderr, "%s: unrecognized visual \"%s\".\n", progname, s);
368 vclass = -1;
369 }
370 if (s) free (s);
371
372 if (vclass == -1)
373 {
374 *depth = DefaultDepth(dpy, DefaultScreen(dpy));
375 return DefaultVisual(dpy, DefaultScreen(dpy));
376 }
377 else if (vclass == -2)
378 {
379 Visual *v = id_to_visual (dpy, id, depth);
380 if (v) return v;
381 fprintf (stderr, "%s: no visual with id 0x%x.\n", progname, id);
382 *depth = DefaultDepth(dpy, DefaultScreen(dpy));
383 return DefaultVisual(dpy, DefaultScreen(dpy));
384 }
385 else
386 return pick_visual_of_class(dpy, vclass, depth);
387 }
388
389 XFontStruct *
390 #ifdef __FunctionProto__
get_font_resource(Display * dpy,char * name,char * class)391 get_font_resource(Display *dpy, char *name, char *class)
392 #else
393 get_font_resource(dpy, name, class)
394 Display *dpy;
395 char *name;
396 char *class;
397 #endif
398 {
399 char *s;
400 XFontStruct *f = (XFontStruct *)0;
401
402 s = get_string_resource(name, class);
403
404 if (s)
405 f = XLoadQueryFont(dpy, s);
406 else
407 {
408 sprintf(errbuf, "can\'t get resource \'%s\'", name);
409 fatal_exit();
410 }
411 if (f == (XFontStruct *)0)
412 {
413 sprintf(errbuf, "can\'t load font \'%s\'", s);
414 sprintf(fixbuf, "Please change resource \'%s\'", name);
415 fatal_exit();
416 }
417 return f;
418 }
419
420