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