1 /* xscreensaver, Copyright (c) 1992 Jamie Zawinski <jwz@lucid.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 # define isupper(c) ((c) >= 'A' && (c) <= 'Z')
31 # define _tolower(c) ((c) - 'A' + 'a')
35 get_string_resource (res_name, res_class)
36 char *res_name, *res_class;
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))
49 char *str = (char *) malloc (value.size + 1);
50 strncpy (str, (char *) value.addr, value.size);
58 get_boolean_resource (res_name, res_class)
59 char *res_name, *res_class;
62 char *s = get_string_resource (res_name, res_class);
65 for (tmp = buf; *s; s++)
66 *tmp++ = isupper (*s) ? _tolower (*s) : *s;
70 if (!strcmp (buf, "on") || !strcmp (buf, "true") || !strcmp (buf, "yes"))
72 if (!strcmp (buf,"off") || !strcmp (buf, "false") || !strcmp (buf,"no"))
74 fprintf (stderr, "%s: %s must be boolean, not %s.\n",
75 progname, res_class, buf);
80 get_integer_resource (res_name, res_class)
81 char *res_name, *res_class;
84 char c, *s = get_string_resource (res_name, res_class);
86 if (1 == sscanf (s, " %d %c", &val, &c))
91 fprintf (stderr, "%s: %s must be an integer, not %s.\n",
92 progname, res_name, s);
98 get_float_resource (res_name, res_class)
99 char *res_name, *res_class;
102 char c, *s = get_string_resource (res_name, res_class);
104 if (1 == sscanf (s, " %lf %c", &val, &c))
109 fprintf (stderr, "%s: %s must be a float, not %s.\n",
110 progname, res_name, s);
117 get_pixel_resource (res_name, res_class, dpy, cmap)
118 char *res_name, *res_class;
123 char *s = get_string_resource (res_name, res_class);
124 if (!s) goto DEFAULT;
126 if (! XParseColor (dpy, cmap, s, &color))
128 fprintf (stderr, "%s: can't parse color %s\n", progname, s);
131 if (! XAllocColor (dpy, cmap, &color))
133 fprintf (stderr, "%s: couldn't allocate color %s\n", progname, s);
140 return (strcmp (res_class, "Background")
141 ? WhitePixel (dpy, DefaultScreen (dpy))
142 : BlackPixel (dpy, DefaultScreen (dpy)));
147 parse_time (string, seconds_default_p, silent_p)
149 Bool seconds_default_p, silent_p;
151 unsigned int h, m, s;
153 if (3 == sscanf (string, " %u : %2u : %2u %c", &h, &m, &s, &c))
155 else if (2 == sscanf (string, " : %2u : %2u %c", &m, &s, &c) ||
156 2 == sscanf (string, " %u : %2u %c", &m, &s, &c))
158 else if (1 == sscanf (string, " : %2u %c", &s, &c))
160 else if (1 == sscanf (string, " %u %c",
161 (seconds_default_p ? &s : &m), &c))
164 if (seconds_default_p) m = 0;
170 fprintf (stderr, "%s: invalid time interval specification \"%s\".\n",
174 if (s >= 60 && (h != 0 || m != 0))
177 fprintf (stderr, "%s: seconds > 59 in \"%s\".\n", progname, string);
180 if (m >= 60 && h > 0)
183 fprintf (stderr, "%s: minutes > 59 in \"%s\".\n", progname, string);
186 return ((h * 60 * 60) + (m * 60) + s);
190 get_time_resource (res_name, res_class, sec_p)
191 char *res_name, *res_class;
195 char *s = get_string_resource (res_name, res_class);
197 val = parse_time (s, sec_p, False);
199 return (val < 0 ? 0 : val);
203 get_seconds_resource (res_name, res_class)
204 char *res_name, *res_class;
206 return get_time_resource (res_name, res_class, True);
210 get_minutes_resource (res_name, res_class)
211 char *res_name, *res_class;
213 return get_time_resource (res_name, res_class, False);