ftp://ftp.ntnu.no/old/pub/X11/R5contrib/xscreensaver-1.17.tar.gz
[xscreensaver] / utils / resources.c
1 /* xscreensaver, Copyright (c) 1992 Jamie Zawinski <jwz@lucid.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 #ifndef isupper
28 # define isupper(c)  ((c) >= 'A' && (c) <= 'Z')
29 #endif
30 #ifndef _tolower
31 # define _tolower(c)  ((c) - 'A' + 'a')
32 #endif
33
34 char *
35 get_string_resource (res_name, res_class)
36      char *res_name, *res_class;
37 {
38   XrmValue value;
39   char  *type;
40   char full_name [1024], full_class [1024];
41   strcpy (full_name, progname);
42   strcat (full_name, ".");
43   strcat (full_name, res_name);
44   strcpy (full_class, progclass);
45   strcat (full_class, ".");
46   strcat (full_class, res_class);
47   if (XrmGetResource (db, full_name, full_class, &type, &value))
48     {
49       char *str = (char *) malloc (value.size + 1);
50       strncpy (str, (char *) value.addr, value.size);
51       str [value.size] = 0;
52       return str;
53     }
54   return 0;
55 }
56
57 Bool 
58 get_boolean_resource (res_name, res_class)
59      char *res_name, *res_class;
60 {
61   char *tmp, buf [100];
62   char *s = get_string_resource (res_name, res_class);
63   char *os = s;
64   if (! s) return 0;
65   for (tmp = buf; *s; s++)
66     *tmp++ = isupper (*s) ? _tolower (*s) : *s;
67   *tmp = 0;
68   free (os);
69
70   if (!strcmp (buf, "on") || !strcmp (buf, "true") || !strcmp (buf, "yes"))
71     return 1;
72   if (!strcmp (buf,"off") || !strcmp (buf, "false") || !strcmp (buf,"no"))
73     return 0;
74   fprintf (stderr, "%s: %s must be boolean, not %s.\n",
75            progname, res_class, buf);
76   return 0;
77 }
78
79 int 
80 get_integer_resource (res_name, res_class)
81      char *res_name, *res_class;
82 {
83   int val;
84   char c, *s = get_string_resource (res_name, res_class);
85   if (!s) return 0;
86   if (1 == sscanf (s, " %d %c", &val, &c))
87     {
88       free (s);
89       return val;
90     }
91   fprintf (stderr, "%s: %s must be an integer, not %s.\n",
92            progname, res_name, s);
93   free (s);
94   return 0;
95 }
96
97 double
98 get_float_resource (res_name, res_class)
99      char *res_name, *res_class;
100 {
101   double val;
102   char c, *s = get_string_resource (res_name, res_class);
103   if (! s) return 0.0;
104   if (1 == sscanf (s, " %lf %c", &val, &c))
105     {
106       free (s);
107       return val;
108     }
109   fprintf (stderr, "%s: %s must be a float, not %s.\n",
110            progname, res_name, s);
111   free (s);
112   return 0.0;
113 }
114
115
116 unsigned int
117 get_pixel_resource (res_name, res_class, dpy, cmap)
118      char *res_name, *res_class;
119      Display *dpy;
120      Colormap cmap;
121 {
122   XColor color;
123   char *s = get_string_resource (res_name, res_class);
124   if (!s) goto DEFAULT;
125
126   if (! XParseColor (dpy, cmap, s, &color))
127     {
128       fprintf (stderr, "%s: can't parse color %s\n", progname, s);
129       goto DEFAULT;
130     }
131   if (! XAllocColor (dpy, cmap, &color))
132     {
133       fprintf (stderr, "%s: couldn't allocate color %s\n", progname, s);
134       goto DEFAULT;
135     }
136   free (s);
137   return color.pixel;
138  DEFAULT:
139   if (s) free (s);
140   return (strcmp (res_class, "Background")
141           ? WhitePixel (dpy, DefaultScreen (dpy))
142           : BlackPixel (dpy, DefaultScreen (dpy)));
143 }
144
145
146 int
147 parse_time (string, seconds_default_p, silent_p)
148      char *string;
149      Bool seconds_default_p, silent_p;
150 {
151   unsigned int h, m, s;
152   char c;
153   if (3 == sscanf (string,   " %u : %2u : %2u %c", &h, &m, &s, &c))
154     ;
155   else if (2 == sscanf (string, " : %2u : %2u %c", &m, &s, &c) ||
156            2 == sscanf (string,    " %u : %2u %c", &m, &s, &c))
157     h = 0;
158   else if (1 == sscanf (string,       " : %2u %c", &s, &c))
159     h = m = 0;
160   else if (1 == sscanf (string,          " %u %c",
161                         (seconds_default_p ? &s : &m), &c))
162     {
163       h = 0;
164       if (seconds_default_p) m = 0;
165       else s = 0;
166     }
167   else
168     {
169       if (! silent_p)
170         fprintf (stderr, "%s: invalid time interval specification \"%s\".\n",
171                  progname, string);
172       return -1;
173     }
174   if (s >= 60 && (h != 0 || m != 0))
175     {
176       if (! silent_p)
177         fprintf (stderr, "%s: seconds > 59 in \"%s\".\n", progname, string);
178       return -1;
179     }
180   if (m >= 60 && h > 0)
181     {
182       if (! silent_p)
183         fprintf (stderr, "%s: minutes > 59 in \"%s\".\n", progname, string);
184       return -1;
185     }
186   return ((h * 60 * 60) + (m * 60) + s);
187 }
188
189 static unsigned int 
190 get_time_resource (res_name, res_class, sec_p)
191      char *res_name, *res_class;
192      Bool sec_p;
193 {
194   int val;
195   char *s = get_string_resource (res_name, res_class);
196   if (!s) return 0;
197   val = parse_time (s, sec_p, False);
198   free (s);
199   return (val < 0 ? 0 : val);
200 }
201
202 unsigned int 
203 get_seconds_resource (res_name, res_class)
204      char *res_name, *res_class;
205 {
206   return get_time_resource (res_name, res_class, True);
207 }
208
209 unsigned int 
210 get_minutes_resource (res_name, res_class)
211      char *res_name, *res_class;
212 {
213   return get_time_resource (res_name, res_class, False);
214 }