1 | /* xscreensaver, Copyright (c) 1992, 1997, 1998 |
---|
2 | * Jamie Zawinski <jwz@jwz.org> |
---|
3 | * |
---|
4 | * Permission to use, copy, modify, distribute, and sell this software and its |
---|
5 | * documentation for any purpose is hereby granted without fee, provided that |
---|
6 | * the above copyright notice appear in all copies and that both that |
---|
7 | * copyright notice and this permission notice appear in supporting |
---|
8 | * documentation. No representations are made about the suitability of this |
---|
9 | * software for any purpose. It is provided "as is" without express or |
---|
10 | * implied warranty. |
---|
11 | */ |
---|
12 | |
---|
13 | /* 1999-Nov-21 Modified by Jim Knoble <jmknoble@pobox.com>. |
---|
14 | * Modifications: |
---|
15 | * |
---|
16 | * - Made get_boolean_resource() accept a third parameter, default_value, |
---|
17 | * which determines the result of get_boolean_resource if either (a) |
---|
18 | * no such resource exists, or (b) the resource value does not conform |
---|
19 | * to the syntax of a boolean resource. |
---|
20 | * |
---|
21 | * - Same for get_integer_resource(), get_pixel_resource(). |
---|
22 | * |
---|
23 | * - 1999-Dec-24 Moved header includes from utils.h to here. |
---|
24 | * Trimmed unused functions. |
---|
25 | */ |
---|
26 | |
---|
27 | #include <stdlib.h> |
---|
28 | #include <stdio.h> |
---|
29 | #include <string.h> |
---|
30 | #include <math.h> |
---|
31 | #include <X11/Xlib.h> |
---|
32 | #include <X11/Xos.h> |
---|
33 | #include <X11/Xresource.h> |
---|
34 | #include "resources.h" |
---|
35 | |
---|
36 | |
---|
37 | /* Resource functions. Assumes: */ |
---|
38 | |
---|
39 | extern char *progname; |
---|
40 | extern char *progclass; |
---|
41 | extern XrmDatabase db; |
---|
42 | |
---|
43 | #ifndef isupper |
---|
44 | # define isupper(c) ((c) >= 'A' && (c) <= 'Z') |
---|
45 | #endif |
---|
46 | #ifndef _tolower |
---|
47 | # define _tolower(c) ((c) - 'A' + 'a') |
---|
48 | #endif |
---|
49 | |
---|
50 | char * |
---|
51 | get_string_resource (char *res_name, char *res_class) |
---|
52 | { |
---|
53 | XrmValue value; |
---|
54 | char *type; |
---|
55 | char full_name [1024], full_class [1024]; |
---|
56 | strcpy (full_name, progname); |
---|
57 | strcat (full_name, "."); |
---|
58 | strcat (full_name, res_name); |
---|
59 | strcpy (full_class, progclass); |
---|
60 | strcat (full_class, "."); |
---|
61 | strcat (full_class, res_class); |
---|
62 | if (XrmGetResource (db, full_name, full_class, &type, &value)) |
---|
63 | { |
---|
64 | char *str = (char *) malloc (value.size + 1); |
---|
65 | strncpy (str, (char *) value.addr, value.size); |
---|
66 | str [value.size] = 0; |
---|
67 | return str; |
---|
68 | } |
---|
69 | return 0; |
---|
70 | } |
---|
71 | |
---|
72 | Bool |
---|
73 | get_boolean_resource (char *res_name, char *res_class, Bool default_value) |
---|
74 | { |
---|
75 | char *tmp, buf [100]; |
---|
76 | char *s = get_string_resource (res_name, res_class); |
---|
77 | char *os = s; |
---|
78 | if (! s) return default_value; |
---|
79 | for (tmp = buf; *s; s++) |
---|
80 | *tmp++ = isupper (*s) ? _tolower (*s) : *s; |
---|
81 | *tmp = 0; |
---|
82 | free (os); |
---|
83 | |
---|
84 | while (*buf && |
---|
85 | (buf[strlen(buf)-1] == ' ' || |
---|
86 | buf[strlen(buf)-1] == '\t')) |
---|
87 | buf[strlen(buf)-1] = 0; |
---|
88 | |
---|
89 | if (!strcmp (buf, "on") || !strcmp (buf, "true") || !strcmp (buf, "yes")) |
---|
90 | return 1; |
---|
91 | if (!strcmp (buf,"off") || !strcmp (buf, "false") || !strcmp (buf,"no")) |
---|
92 | return 0; |
---|
93 | fprintf (stderr, "%s: %s must be boolean, not %s.\n", |
---|
94 | progname, res_name, buf); |
---|
95 | return default_value; |
---|
96 | } |
---|
97 | |
---|
98 | int |
---|
99 | get_integer_resource (char *res_name, char *res_class, int default_value) |
---|
100 | { |
---|
101 | int val; |
---|
102 | char c, *s = get_string_resource (res_name, res_class); |
---|
103 | char *ss = s; |
---|
104 | if (!s) return default_value; |
---|
105 | |
---|
106 | while (*ss && *ss <= ' ') ss++; /* skip whitespace */ |
---|
107 | |
---|
108 | if (ss[0] == '0' && (ss[1] == 'x' || ss[1] == 'X')) /* 0x: parse as hex */ |
---|
109 | { |
---|
110 | if (1 == sscanf (ss+2, "%x %c", &val, &c)) |
---|
111 | { |
---|
112 | free (s); |
---|
113 | return val; |
---|
114 | } |
---|
115 | } |
---|
116 | else /* else parse as dec */ |
---|
117 | { |
---|
118 | if (1 == sscanf (ss, "%d %c", &val, &c)) |
---|
119 | { |
---|
120 | free (s); |
---|
121 | return val; |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | fprintf (stderr, "%s: %s must be an integer, not %s.\n", |
---|
126 | progname, res_name, s); |
---|
127 | free (s); |
---|
128 | return default_value; |
---|
129 | } |
---|
130 | |
---|
131 | double |
---|
132 | get_float_resource (char *res_name, char *res_class) |
---|
133 | { |
---|
134 | double val; |
---|
135 | char c, *s = get_string_resource (res_name, res_class); |
---|
136 | if (! s) return 0.0; |
---|
137 | if (1 == sscanf (s, " %lf %c", &val, &c)) |
---|
138 | { |
---|
139 | free (s); |
---|
140 | return val; |
---|
141 | } |
---|
142 | fprintf (stderr, "%s: %s must be a float, not %s.\n", |
---|
143 | progname, res_name, s); |
---|
144 | free (s); |
---|
145 | return 0.0; |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | unsigned int |
---|
150 | get_pixel_resource (char *res_name, char *res_class, |
---|
151 | Display *dpy, Colormap cmap, unsigned int default_value) |
---|
152 | { |
---|
153 | XColor color; |
---|
154 | char *s = get_string_resource (res_name, res_class); |
---|
155 | char *s2; |
---|
156 | if (!s) goto DEFAULT; |
---|
157 | |
---|
158 | for (s2 = s + strlen(s) - 1; s2 > s; s2--) |
---|
159 | if (*s2 == ' ' || *s2 == '\t') |
---|
160 | *s2 = 0; |
---|
161 | else |
---|
162 | break; |
---|
163 | |
---|
164 | if (! XParseColor (dpy, cmap, s, &color)) |
---|
165 | { |
---|
166 | fprintf (stderr, "%s: can't parse color %s\n", progname, s); |
---|
167 | goto DEFAULT; |
---|
168 | } |
---|
169 | if (! XAllocColor (dpy, cmap, &color)) |
---|
170 | { |
---|
171 | fprintf (stderr, "%s: couldn't allocate color %s\n", progname, s); |
---|
172 | goto DEFAULT; |
---|
173 | } |
---|
174 | free (s); |
---|
175 | return color.pixel; |
---|
176 | DEFAULT: |
---|
177 | if (s) free (s); |
---|
178 | return default_value; |
---|
179 | } |
---|
180 | |
---|