1 /* xscreensaver, Copyright (c) 1992, 1997, 1998, 2001, 2003, 2006
2 * Jamie Zawinski <jwz@jwz.org>
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"
16 extern char *progname;
21 #include <X11/Xresource.h>
23 /* These are the Xlib/Xrm versions of these functions.
24 The Cocoa versions are on OSX/XScreenSaverView.m.
27 extern char *progclass;
28 extern XrmDatabase XtDatabase (Display *);
30 static unsigned int get_time_resource (Display *dpy,
31 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 (Display *dpy, char *res_name, char *res_class)
46 char full_name [1024], full_class [1024];
47 strcpy (full_name, progname);
48 strcat (full_name, ".");
49 strcat (full_name, res_name);
50 strcpy (full_class, progclass);
51 strcat (full_class, ".");
52 strcat (full_class, res_class);
53 if (XrmGetResource (XtDatabase (dpy), full_name, full_class, &type, &value))
55 char *str = (char *) malloc (value.size + 1);
56 strncpy (str, (char *) value.addr, value.size);
64 get_boolean_resource (Display *dpy, char *res_name, char *res_class)
67 char *s = get_string_resource (dpy, res_name, res_class);
70 for (tmp = buf; *s; s++)
71 *tmp++ = isupper (*s) ? _tolower (*s) : *s;
76 (buf[strlen(buf)-1] == ' ' ||
77 buf[strlen(buf)-1] == '\t'))
78 buf[strlen(buf)-1] = 0;
80 if (!strcmp (buf, "on") || !strcmp (buf, "true") || !strcmp (buf, "yes"))
82 if (!strcmp (buf,"off") || !strcmp (buf, "false") || !strcmp (buf,"no"))
84 fprintf (stderr, "%s: %s must be boolean, not %s.\n",
85 progname, res_name, buf);
90 get_integer_resource (Display *dpy, char *res_name, char *res_class)
93 char c, *s = get_string_resource (dpy, res_name, res_class);
97 while (*ss && *ss <= ' ') ss++; /* skip whitespace */
99 if (ss[0] == '0' && (ss[1] == 'x' || ss[1] == 'X')) /* 0x: parse as hex */
101 if (1 == sscanf (ss+2, "%x %c", (unsigned int *) &val, &c))
107 else /* else parse as dec */
109 if (1 == sscanf (ss, "%d %c", &val, &c))
116 fprintf (stderr, "%s: %s must be an integer, not %s.\n",
117 progname, res_name, s);
123 get_float_resource (Display *dpy, char *res_name, char *res_class)
126 char c, *s = get_string_resource (dpy, res_name, res_class);
128 if (1 == sscanf (s, " %lf %c", &val, &c))
133 fprintf (stderr, "%s: %s must be a float, not %s.\n",
134 progname, res_name, s);
139 #endif /* !HAVE_COCOA */
142 /* These functions are the same with Xlib and Cocoa:
147 get_pixel_resource (Display *dpy, Colormap cmap,
148 char *res_name, char *res_class)
151 char *s = get_string_resource (dpy, res_name, res_class);
154 if (!s) goto DEFAULT;
156 for (s2 = s + strlen(s) - 1; s2 > s; s2--)
157 if (*s2 == ' ' || *s2 == '\t')
162 if (! XParseColor (dpy, cmap, s, &color))
164 fprintf (stderr, "%s: can't parse color %s", progname, s);
168 if (! XAllocColor (dpy, cmap, &color))
170 fprintf (stderr, "%s: couldn't allocate color %s", progname, s);
175 return (unsigned int) color.pixel;
180 Bool black_p = (strlen(res_class) >= 10 &&
181 !strcasecmp ("Background",
182 res_class + strlen(res_class) - 10));
184 fprintf (stderr, ": using %s.\n", (black_p ? "black" : "white"));
185 color.flags = DoRed|DoGreen|DoBlue;
186 color.red = color.green = color.blue = (black_p ? 0 : 0xFFFF);
187 if (XAllocColor (dpy, cmap, &color))
188 return (unsigned int) color.pixel;
191 fprintf (stderr, "%s: couldn't allocate %s either!\n", progname,
192 (black_p ? "black" : "white"));
193 /* We can't use BlackPixel/WhitePixel here, because we don't know
194 what screen we're allocating on (only an issue when running inside
195 the xscreensaver daemon: for hacks, DefaultScreen is fine.)
204 parse_time (const char *string, Bool seconds_default_p, Bool silent_p)
206 unsigned int h, m, s;
208 if (3 == sscanf (string, " %u : %2u : %2u %c", &h, &m, &s, &c))
210 else if (2 == sscanf (string, " : %2u : %2u %c", &m, &s, &c) ||
211 2 == sscanf (string, " %u : %2u %c", &m, &s, &c))
213 else if (1 == sscanf (string, " : %2u %c", &s, &c))
215 else if (1 == sscanf (string, " %u %c",
216 (seconds_default_p ? &s : &m), &c))
219 if (seconds_default_p) m = 0;
225 fprintf (stderr, "%s: invalid time interval specification \"%s\".\n",
229 if (s >= 60 && (h != 0 || m != 0))
232 fprintf (stderr, "%s: seconds > 59 in \"%s\".\n", progname, string);
235 if (m >= 60 && h > 0)
238 fprintf (stderr, "%s: minutes > 59 in \"%s\".\n", progname, string);
241 return ((h * 60 * 60) + (m * 60) + s);
245 get_time_resource (Display *dpy, char *res_name, char *res_class, Bool sec_p)
248 char *s = get_string_resource (dpy, res_name, res_class);
250 val = parse_time (s, sec_p, False);
252 return (val < 0 ? 0 : val);
256 get_seconds_resource (Display *dpy, char *res_name, char *res_class)
258 return get_time_resource (dpy, res_name, res_class, True);
262 get_minutes_resource (Display *dpy, char *res_name, char *res_class)
264 return get_time_resource (dpy, res_name, res_class, False);