1 /* xscreensaver, Copyright (c) 1992, 1997 Jamie Zawinski <jwz@netscape.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
13 #include "resources.h"
14 #include <X11/Xresource.h>
17 /* Resource functions. Assumes: */
19 extern char *progname;
20 extern char *progclass;
21 extern XrmDatabase db;
23 static unsigned int get_time_resource (char *res_name, char *res_class,
27 # define isupper(c) ((c) >= 'A' && (c) <= 'Z')
30 # define _tolower(c) ((c) - 'A' + 'a')
34 get_string_resource (char *res_name, char *res_class)
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))
47 char *str = (char *) malloc (value.size + 1);
48 strncpy (str, (char *) value.addr, value.size);
56 get_boolean_resource (char *res_name, char *res_class)
59 char *s = get_string_resource (res_name, res_class);
62 for (tmp = buf; *s; s++)
63 *tmp++ = isupper (*s) ? _tolower (*s) : *s;
68 (buf[strlen(buf)-1] == ' ' ||
69 buf[strlen(buf)-1] == '\t'))
70 buf[strlen(buf)-1] = 0;
72 if (!strcmp (buf, "on") || !strcmp (buf, "true") || !strcmp (buf, "yes"))
74 if (!strcmp (buf,"off") || !strcmp (buf, "false") || !strcmp (buf,"no"))
76 fprintf (stderr, "%s: %s must be boolean, not %s.\n",
77 progname, res_class, buf);
82 get_integer_resource (char *res_name, char *res_class)
85 char c, *s = get_string_resource (res_name, res_class);
87 if (1 == sscanf (s, " %d %c", &val, &c))
92 fprintf (stderr, "%s: %s must be an integer, not %s.\n",
93 progname, res_name, s);
99 get_float_resource (char *res_name, char *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 (char *res_name, char *res_class,
118 Display *dpy, Colormap cmap)
121 char *s = get_string_resource (res_name, res_class);
123 if (!s) goto DEFAULT;
125 for (s2 = s + strlen(s) - 1; s2 > s; s2--)
126 if (*s2 == ' ' || *s2 == '\t')
129 if (! XParseColor (dpy, cmap, s, &color))
131 fprintf (stderr, "%s: can't parse color %s\n", progname, s);
134 if (! XAllocColor (dpy, cmap, &color))
136 fprintf (stderr, "%s: couldn't allocate color %s\n", progname, s);
143 return (strcmp (res_class, "Background")
144 ? WhitePixel (dpy, DefaultScreen (dpy))
145 : BlackPixel (dpy, DefaultScreen (dpy)));
150 parse_time (char *string, Bool seconds_default_p, Bool silent_p)
152 unsigned int h, m, s;
154 if (3 == sscanf (string, " %u : %2u : %2u %c", &h, &m, &s, &c))
156 else if (2 == sscanf (string, " : %2u : %2u %c", &m, &s, &c) ||
157 2 == sscanf (string, " %u : %2u %c", &m, &s, &c))
159 else if (1 == sscanf (string, " : %2u %c", &s, &c))
161 else if (1 == sscanf (string, " %u %c",
162 (seconds_default_p ? &s : &m), &c))
165 if (seconds_default_p) m = 0;
171 fprintf (stderr, "%s: invalid time interval specification \"%s\".\n",
175 if (s >= 60 && (h != 0 || m != 0))
178 fprintf (stderr, "%s: seconds > 59 in \"%s\".\n", progname, string);
181 if (m >= 60 && h > 0)
184 fprintf (stderr, "%s: minutes > 59 in \"%s\".\n", progname, string);
187 return ((h * 60 * 60) + (m * 60) + s);
191 get_time_resource (char *res_name, char *res_class, Bool sec_p)
194 char *s = get_string_resource (res_name, res_class);
196 val = parse_time (s, sec_p, False);
198 return (val < 0 ? 0 : val);
202 get_seconds_resource (char *res_name, char *res_class)
204 return get_time_resource (res_name, res_class, True);
208 get_minutes_resource (char *res_name, char *res_class)
210 return get_time_resource (res_name, res_class, False);