1 /* xscreensaver, Copyright (c) 1992 Jamie Zawinski <jwz@mcom.com>
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
19 #include <X11/Xresource.h>
21 /* Resource functions. Assumes: */
23 extern char *progname;
24 extern char *progclass;
25 extern XrmDatabase db;
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,
35 # define isupper(c) ((c) >= 'A' && (c) <= 'Z')
38 # define _tolower(c) ((c) - 'A' + 'a')
42 get_string_resource (res_name, res_class)
43 char *res_name, *res_class;
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))
56 char *str = (char *) malloc (value.size + 1);
57 strncpy (str, (char *) value.addr, value.size);
65 get_boolean_resource (res_name, res_class)
66 char *res_name, *res_class;
69 char *s = get_string_resource (res_name, res_class);
72 for (tmp = buf; *s; s++)
73 *tmp++ = isupper (*s) ? _tolower (*s) : *s;
77 if (!strcmp (buf, "on") || !strcmp (buf, "true") || !strcmp (buf, "yes"))
79 if (!strcmp (buf,"off") || !strcmp (buf, "false") || !strcmp (buf,"no"))
81 fprintf (stderr, "%s: %s must be boolean, not %s.\n",
82 progname, res_class, buf);
87 get_integer_resource (res_name, res_class)
88 char *res_name, *res_class;
91 char c, *s = get_string_resource (res_name, res_class);
93 if (1 == sscanf (s, " %d %c", &val, &c))
98 fprintf (stderr, "%s: %s must be an integer, not %s.\n",
99 progname, res_name, s);
105 get_float_resource (res_name, res_class)
106 char *res_name, *res_class;
109 char c, *s = get_string_resource (res_name, res_class);
111 if (1 == sscanf (s, " %lf %c", &val, &c))
116 fprintf (stderr, "%s: %s must be a float, not %s.\n",
117 progname, res_name, s);
124 get_pixel_resource (res_name, res_class, dpy, cmap)
125 char *res_name, *res_class;
130 char *s = get_string_resource (res_name, res_class);
131 if (!s) goto DEFAULT;
133 if (! XParseColor (dpy, cmap, s, &color))
135 fprintf (stderr, "%s: can't parse color %s\n", progname, s);
138 if (! XAllocColor (dpy, cmap, &color))
140 fprintf (stderr, "%s: couldn't allocate color %s\n", progname, s);
147 return (strcmp (res_class, "Background")
148 ? WhitePixel (dpy, DefaultScreen (dpy))
149 : BlackPixel (dpy, DefaultScreen (dpy)));
154 parse_time (string, seconds_default_p, silent_p)
156 Bool seconds_default_p, silent_p;
158 unsigned int h, m, s;
161 if (3 == sscanf (string, " %u : %2u : %2u %c", &h, &m, &s, &c))
163 else if (2 == sscanf (string, " : %2u : %2u %c", &m, &s, &c) ||
164 2 == sscanf (string, " %u : %2u %c", &m, &s, &c))
166 else if (1 == sscanf (string, " : %2u %c", &s, &c))
168 else if (1 == sscanf (string, " %u %c",
169 (seconds_default_p ? &s : &m), &c))
171 if (3 == sscanf (string, " %d : %2d : %2d %c", &h, &m, &s, &c))
173 else if (2 == sscanf (string, " : %2d : %2d %c", &m, &s, &c) ||
174 2 == sscanf (string, " %d : %2d %c", &m, &s, &c))
176 else if (1 == sscanf (string, " : %2d %c", &s, &c))
178 else if (1 == sscanf (string, " %d %c",
179 (seconds_default_p ? &s : &m), &c))
183 if (seconds_default_p) m = 0;
189 fprintf (stderr, "%s: invalid time interval specification \"%s\".\n",
193 if (s >= 60 && (h != 0 || m != 0))
196 fprintf (stderr, "%s: seconds > 59 in \"%s\".\n", progname, string);
199 if (m >= 60 && h > 0)
202 fprintf (stderr, "%s: minutes > 59 in \"%s\".\n", progname, string);
205 return ((h * 60 * 60) + (m * 60) + s);
209 get_time_resource (res_name, res_class, sec_p)
210 char *res_name, *res_class;
214 char *s = get_string_resource (res_name, res_class);
216 val = parse_time (s, sec_p, False);
218 return (val < 0 ? 0 : val);
222 get_seconds_resource (res_name, res_class)
223 char *res_name, *res_class;
225 return get_time_resource (res_name, res_class, True);
229 get_minutes_resource (res_name, res_class)
230 char *res_name, *res_class;
232 return get_time_resource (res_name, res_class, False);