1 /* xscreensaver, Copyright (c) 1992, 1997, 1998
2 * Jamie Zawinski <jwz@netscape.com>
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
14 #include "resources.h"
15 #include <X11/Xresource.h>
18 /* Resource functions. Assumes: */
20 extern char *progname;
21 extern char *progclass;
22 extern XrmDatabase db;
24 static unsigned int get_time_resource (char *res_name, char *res_class,
28 # define isupper(c) ((c) >= 'A' && (c) <= 'Z')
31 # define _tolower(c) ((c) - 'A' + 'a')
35 get_string_resource (char *res_name, char *res_class)
39 char full_name [1024], full_class [1024];
40 strcpy (full_name, progname);
41 strcat (full_name, ".");
42 strcat (full_name, res_name);
43 strcpy (full_class, progclass);
44 strcat (full_class, ".");
45 strcat (full_class, res_class);
46 if (XrmGetResource (db, full_name, full_class, &type, &value))
48 char *str = (char *) malloc (value.size + 1);
49 strncpy (str, (char *) value.addr, value.size);
57 get_boolean_resource (char *res_name, char *res_class)
60 char *s = get_string_resource (res_name, res_class);
63 for (tmp = buf; *s; s++)
64 *tmp++ = isupper (*s) ? _tolower (*s) : *s;
69 (buf[strlen(buf)-1] == ' ' ||
70 buf[strlen(buf)-1] == '\t'))
71 buf[strlen(buf)-1] = 0;
73 if (!strcmp (buf, "on") || !strcmp (buf, "true") || !strcmp (buf, "yes"))
75 if (!strcmp (buf,"off") || !strcmp (buf, "false") || !strcmp (buf,"no"))
77 fprintf (stderr, "%s: %s must be boolean, not %s.\n",
78 progname, res_class, buf);
83 get_integer_resource (char *res_name, char *res_class)
86 char c, *s = get_string_resource (res_name, res_class);
88 if (1 == sscanf (s, " %d %c", &val, &c))
93 fprintf (stderr, "%s: %s must be an integer, not %s.\n",
94 progname, res_name, s);
100 get_float_resource (char *res_name, char *res_class)
103 char c, *s = get_string_resource (res_name, res_class);
105 if (1 == sscanf (s, " %lf %c", &val, &c))
110 fprintf (stderr, "%s: %s must be a float, not %s.\n",
111 progname, res_name, s);
118 get_pixel_resource (char *res_name, char *res_class,
119 Display *dpy, Colormap cmap)
122 char *s = get_string_resource (res_name, res_class);
124 if (!s) goto DEFAULT;
126 for (s2 = s + strlen(s) - 1; s2 > s; s2--)
127 if (*s2 == ' ' || *s2 == '\t')
132 if (! XParseColor (dpy, cmap, s, &color))
134 fprintf (stderr, "%s: can't parse color %s\n", progname, s);
137 if (! XAllocColor (dpy, cmap, &color))
139 fprintf (stderr, "%s: couldn't allocate color %s\n", progname, s);
146 return (strcmp (res_class, "Background")
147 ? WhitePixel (dpy, DefaultScreen (dpy))
148 : BlackPixel (dpy, DefaultScreen (dpy)));
153 parse_time (char *string, Bool seconds_default_p, Bool silent_p)
155 unsigned int h, m, s;
157 if (3 == sscanf (string, " %u : %2u : %2u %c", &h, &m, &s, &c))
159 else if (2 == sscanf (string, " : %2u : %2u %c", &m, &s, &c) ||
160 2 == sscanf (string, " %u : %2u %c", &m, &s, &c))
162 else if (1 == sscanf (string, " : %2u %c", &s, &c))
164 else if (1 == sscanf (string, " %u %c",
165 (seconds_default_p ? &s : &m), &c))
168 if (seconds_default_p) m = 0;
174 fprintf (stderr, "%s: invalid time interval specification \"%s\".\n",
178 if (s >= 60 && (h != 0 || m != 0))
181 fprintf (stderr, "%s: seconds > 59 in \"%s\".\n", progname, string);
184 if (m >= 60 && h > 0)
187 fprintf (stderr, "%s: minutes > 59 in \"%s\".\n", progname, string);
190 return ((h * 60 * 60) + (m * 60) + s);
194 get_time_resource (char *res_name, char *res_class, Bool sec_p)
197 char *s = get_string_resource (res_name, res_class);
199 val = parse_time (s, sec_p, False);
201 return (val < 0 ? 0 : val);
205 get_seconds_resource (char *res_name, char *res_class)
207 return get_time_resource (res_name, res_class, True);
211 get_minutes_resource (char *res_name, char *res_class)
213 return get_time_resource (res_name, res_class, False);