http://se.aminet.net/pub/X11/ftp.x.org/contrib/vms/xscreensaver-124.zip
[xscreensaver] / utils / resources.c
1 /* xscreensaver, Copyright (c) 1992 Jamie Zawinski <jwz@mcom.com>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  */
11
12 #if __STDC__
13 #include <stdlib.h>
14 #include <string.h>
15 #endif
16
17 #include <stdio.h>
18 #include <X11/Xlib.h>
19 #include <X11/Xresource.h>
20
21 /* Resource functions.  Assumes: */
22
23 extern char *progname;
24 extern char *progclass;
25 extern XrmDatabase db;
26
27 #if __STDC__
28 char *get_string_resource (char *res_name, char *res_class);
29 int parse_time (char *string, Bool seconds_default_p, Bool silent_p);
30 static unsigned int get_time_resource (char *res_name, char *res_class,
31                                        Bool sec_p);
32 #endif
33
34 #ifndef isupper
35 # define isupper(c)  ((c) >= 'A' && (c) <= 'Z')
36 #endif
37 #ifndef _tolower
38 # define _tolower(c)  ((c) - 'A' + 'a')
39 #endif
40
41 char *
42 get_string_resource (res_name, res_class)
43      char *res_name, *res_class;
44 {
45   XrmValue value;
46   char  *type;
47   char full_name [1024], full_class [1024];
48   strcpy (full_name, progname);
49   strcat (full_name, ".");
50   strcat (full_name, res_name);
51   strcpy (full_class, progclass);
52   strcat (full_class, ".");
53   strcat (full_class, res_class);
54   if (XrmGetResource (db, full_name, full_class, &type, &value))
55     {
56       char *str = (char *) malloc (value.size + 1);
57       strncpy (str, (char *) value.addr, value.size);
58       str [value.size] = 0;
59       return str;
60     }
61   return 0;
62 }
63
64 Bool 
65 get_boolean_resource (res_name, res_class)
66      char *res_name, *res_class;
67 {
68   char *tmp, buf [100];
69   char *s = get_string_resource (res_name, res_class);
70   char *os = s;
71   if (! s) return 0;
72   for (tmp = buf; *s; s++)
73     *tmp++ = isupper (*s) ? _tolower (*s) : *s;
74   *tmp = 0;
75   free (os);
76
77   if (!strcmp (buf, "on") || !strcmp (buf, "true") || !strcmp (buf, "yes"))
78     return 1;
79   if (!strcmp (buf,"off") || !strcmp (buf, "false") || !strcmp (buf,"no"))
80     return 0;
81   fprintf (stderr, "%s: %s must be boolean, not %s.\n",
82            progname, res_class, buf);
83   return 0;
84 }
85
86 int 
87 get_integer_resource (res_name, res_class)
88      char *res_name, *res_class;
89 {
90   int val;
91   char c, *s = get_string_resource (res_name, res_class);
92   if (!s) return 0;
93   if (1 == sscanf (s, " %d %c", &val, &c))
94     {
95       free (s);
96       return val;
97     }
98   fprintf (stderr, "%s: %s must be an integer, not %s.\n",
99            progname, res_name, s);
100   free (s);
101   return 0;
102 }
103
104 double
105 get_float_resource (res_name, res_class)
106      char *res_name, *res_class;
107 {
108   double val;
109   char c, *s = get_string_resource (res_name, res_class);
110   if (! s) return 0.0;
111   if (1 == sscanf (s, " %lf %c", &val, &c))
112     {
113       free (s);
114       return val;
115     }
116   fprintf (stderr, "%s: %s must be a float, not %s.\n",
117            progname, res_name, s);
118   free (s);
119   return 0.0;
120 }
121
122
123 unsigned int
124 get_pixel_resource (res_name, res_class, dpy, cmap)
125      char *res_name, *res_class;
126      Display *dpy;
127      Colormap cmap;
128 {
129   XColor color;
130   char *s = get_string_resource (res_name, res_class);
131   if (!s) goto DEFAULT;
132
133   if (! XParseColor (dpy, cmap, s, &color))
134     {
135       fprintf (stderr, "%s: can't parse color %s\n", progname, s);
136       goto DEFAULT;
137     }
138   if (! XAllocColor (dpy, cmap, &color))
139     {
140       fprintf (stderr, "%s: couldn't allocate color %s\n", progname, s);
141       goto DEFAULT;
142     }
143   free (s);
144   return color.pixel;
145  DEFAULT:
146   if (s) free (s);
147   return (strcmp (res_class, "Background")
148           ? WhitePixel (dpy, DefaultScreen (dpy))
149           : BlackPixel (dpy, DefaultScreen (dpy)));
150 }
151
152
153 int
154 parse_time (string, seconds_default_p, silent_p)
155      char *string;
156      Bool seconds_default_p, silent_p;
157 {
158   unsigned int h, m, s;
159   char c;
160 #ifdef __DECC
161   if (3 == sscanf (string,   " %u : %2u : %2u %c", &h, &m, &s, &c))
162     ;
163   else if (2 == sscanf (string, " : %2u : %2u %c", &m, &s, &c) ||
164            2 == sscanf (string,    " %u : %2u %c", &m, &s, &c))
165     h = 0;
166   else if (1 == sscanf (string,       " : %2u %c", &s, &c))
167     h = m = 0;
168   else if (1 == sscanf (string,          " %u %c",
169                         (seconds_default_p ? &s : &m), &c))
170 #else
171   if (3 == sscanf (string,   " %d : %2d : %2d %c", &h, &m, &s, &c))
172     ;
173   else if (2 == sscanf (string, " : %2d : %2d %c", &m, &s, &c) ||
174            2 == sscanf (string,    " %d : %2d %c", &m, &s, &c))
175     h = 0;
176   else if (1 == sscanf (string,       " : %2d %c", &s, &c))
177     h = m = 0;
178   else if (1 == sscanf (string,          " %d %c",
179                         (seconds_default_p ? &s : &m), &c))
180 #endif
181     {
182       h = 0;
183       if (seconds_default_p) m = 0;
184       else s = 0;
185     }
186   else
187     {
188       if (! silent_p)
189         fprintf (stderr, "%s: invalid time interval specification \"%s\".\n",
190                  progname, string);
191       return -1;
192     }
193   if (s >= 60 && (h != 0 || m != 0))
194     {
195       if (! silent_p)
196         fprintf (stderr, "%s: seconds > 59 in \"%s\".\n", progname, string);
197       return -1;
198     }
199   if (m >= 60 && h > 0)
200     {
201       if (! silent_p)
202         fprintf (stderr, "%s: minutes > 59 in \"%s\".\n", progname, string);
203       return -1;
204     }
205   return ((h * 60 * 60) + (m * 60) + s);
206 }
207
208 static unsigned int 
209 get_time_resource (res_name, res_class, sec_p)
210      char *res_name, *res_class;
211      Bool sec_p;
212 {
213   int val;
214   char *s = get_string_resource (res_name, res_class);
215   if (!s) return 0;
216   val = parse_time (s, sec_p, False);
217   free (s);
218   return (val < 0 ? 0 : val);
219 }
220
221 unsigned int 
222 get_seconds_resource (res_name, res_class)
223      char *res_name, *res_class;
224 {
225   return get_time_resource (res_name, res_class, True);
226 }
227
228 unsigned int 
229 get_minutes_resource (res_name, res_class)
230      char *res_name, *res_class;
231 {
232   return get_time_resource (res_name, res_class, False);
233 }