X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=utils%2Fresources.c;h=19b2cb6ccf8149d1750f2ad41272e3fa488a15c3;hb=49f5b54f312fe4ac2e9bc47581a72451bd0e8439;hp=771e20f18663b8e7fa66addb8ba32150f4ba4693;hpb=0a1527cc01e9894017614b7c36b838b2b6914ba9;p=xscreensaver diff --git a/utils/resources.c b/utils/resources.c index 771e20f1..19b2cb6c 100644 --- a/utils/resources.c +++ b/utils/resources.c @@ -1,4 +1,5 @@ -/* xscreensaver, Copyright (c) 1992 Jamie Zawinski +/* xscreensaver, Copyright (c) 1992, 1997, 1998, 2001, 2003, 2006 + * Jamie Zawinski * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that @@ -9,20 +10,26 @@ * implied warranty. */ -#if __STDC__ -#include -#include -#endif +#include "utils.h" +#include "resources.h" + +extern char *progname; + + +#ifndef HAVE_COCOA -#include -#include #include -/* Resource functions. Assumes: */ +/* These are the Xlib/Xrm versions of these functions. + The Cocoa versions are on OSX/XScreenSaverView.m. + */ -extern char *progname; extern char *progclass; -extern XrmDatabase db; +extern XrmDatabase XtDatabase (Display *); + +static unsigned int get_time_resource (Display *dpy, + char *res_name, char *res_class, + Bool sec_p); #ifndef isupper # define isupper(c) ((c) >= 'A' && (c) <= 'Z') @@ -32,8 +39,7 @@ extern XrmDatabase db; #endif char * -get_string_resource (res_name, res_class) - char *res_name, *res_class; +get_string_resource (Display *dpy, char *res_name, char *res_class) { XrmValue value; char *type; @@ -44,7 +50,7 @@ get_string_resource (res_name, res_class) strcpy (full_class, progclass); strcat (full_class, "."); strcat (full_class, res_class); - if (XrmGetResource (db, full_name, full_class, &type, &value)) + if (XrmGetResource (XtDatabase (dpy), full_name, full_class, &type, &value)) { char *str = (char *) malloc (value.size + 1); strncpy (str, (char *) value.addr, value.size); @@ -55,11 +61,10 @@ get_string_resource (res_name, res_class) } Bool -get_boolean_resource (res_name, res_class) - char *res_name, *res_class; +get_boolean_resource (Display *dpy, char *res_name, char *res_class) { char *tmp, buf [100]; - char *s = get_string_resource (res_name, res_class); + char *s = get_string_resource (dpy, res_name, res_class); char *os = s; if (! s) return 0; for (tmp = buf; *s; s++) @@ -67,27 +72,47 @@ get_boolean_resource (res_name, res_class) *tmp = 0; free (os); + while (*buf && + (buf[strlen(buf)-1] == ' ' || + buf[strlen(buf)-1] == '\t')) + buf[strlen(buf)-1] = 0; + if (!strcmp (buf, "on") || !strcmp (buf, "true") || !strcmp (buf, "yes")) return 1; if (!strcmp (buf,"off") || !strcmp (buf, "false") || !strcmp (buf,"no")) return 0; fprintf (stderr, "%s: %s must be boolean, not %s.\n", - progname, res_class, buf); + progname, res_name, buf); return 0; } int -get_integer_resource (res_name, res_class) - char *res_name, *res_class; +get_integer_resource (Display *dpy, char *res_name, char *res_class) { int val; - char c, *s = get_string_resource (res_name, res_class); + char c, *s = get_string_resource (dpy, res_name, res_class); + char *ss = s; if (!s) return 0; - if (1 == sscanf (s, " %d %c", &val, &c)) + + while (*ss && *ss <= ' ') ss++; /* skip whitespace */ + + if (ss[0] == '0' && (ss[1] == 'x' || ss[1] == 'X')) /* 0x: parse as hex */ { - free (s); - return val; + if (1 == sscanf (ss+2, "%x %c", (unsigned int *) &val, &c)) + { + free (s); + return val; + } + } + else /* else parse as dec */ + { + if (1 == sscanf (ss, "%d %c", &val, &c)) + { + free (s); + return val; + } } + fprintf (stderr, "%s: %s must be an integer, not %s.\n", progname, res_name, s); free (s); @@ -95,11 +120,10 @@ get_integer_resource (res_name, res_class) } double -get_float_resource (res_name, res_class) - char *res_name, *res_class; +get_float_resource (Display *dpy, char *res_name, char *res_class) { double val; - char c, *s = get_string_resource (res_name, res_class); + char c, *s = get_string_resource (dpy, res_name, res_class); if (! s) return 0.0; if (1 == sscanf (s, " %lf %c", &val, &c)) { @@ -112,41 +136,72 @@ get_float_resource (res_name, res_class) return 0.0; } +#endif /* !HAVE_COCOA */ + + +/* These functions are the same with Xlib and Cocoa: + */ + unsigned int -get_pixel_resource (res_name, res_class, dpy, cmap) - char *res_name, *res_class; - Display *dpy; - Colormap cmap; +get_pixel_resource (Display *dpy, Colormap cmap, + char *res_name, char *res_class) { XColor color; - char *s = get_string_resource (res_name, res_class); + char *s = get_string_resource (dpy, res_name, res_class); + char *s2; + Bool ok = True; if (!s) goto DEFAULT; + for (s2 = s + strlen(s) - 1; s2 > s; s2--) + if (*s2 == ' ' || *s2 == '\t') + *s2 = 0; + else + break; + if (! XParseColor (dpy, cmap, s, &color)) { - fprintf (stderr, "%s: can't parse color %s\n", progname, s); + fprintf (stderr, "%s: can't parse color %s", progname, s); + ok = False; goto DEFAULT; } if (! XAllocColor (dpy, cmap, &color)) { - fprintf (stderr, "%s: couldn't allocate color %s\n", progname, s); + fprintf (stderr, "%s: couldn't allocate color %s", progname, s); + ok = False; goto DEFAULT; } free (s); return color.pixel; DEFAULT: if (s) free (s); - return (strcmp (res_class, "Background") - ? WhitePixel (dpy, DefaultScreen (dpy)) - : BlackPixel (dpy, DefaultScreen (dpy))); + + { + Bool black_p = (strlen(res_class) >= 10 && + !strcasecmp ("Background", + res_class + strlen(res_class) - 10)); + if (!ok) + fprintf (stderr, ": using %s.\n", (black_p ? "black" : "white")); + color.flags = DoRed|DoGreen|DoBlue; + color.red = color.green = color.blue = (black_p ? 0 : 0xFFFF); + if (XAllocColor (dpy, cmap, &color)) + return color.pixel; + else + { + fprintf (stderr, "%s: couldn't allocate %s either!\n", progname, + (black_p ? "black" : "white")); + /* We can't use BlackPixel/WhitePixel here, because we don't know + what screen we're allocating on (only an issue when running inside + the xscreensaver daemon: for hacks, DefaultScreen is fine.) + */ + return 0; + } + } } int -parse_time (string, seconds_default_p, silent_p) - char *string; - Bool seconds_default_p, silent_p; +parse_time (const char *string, Bool seconds_default_p, Bool silent_p) { unsigned int h, m, s; char c; @@ -187,12 +242,10 @@ parse_time (string, seconds_default_p, silent_p) } static unsigned int -get_time_resource (res_name, res_class, sec_p) - char *res_name, *res_class; - Bool sec_p; +get_time_resource (Display *dpy, char *res_name, char *res_class, Bool sec_p) { int val; - char *s = get_string_resource (res_name, res_class); + char *s = get_string_resource (dpy, res_name, res_class); if (!s) return 0; val = parse_time (s, sec_p, False); free (s); @@ -200,15 +253,13 @@ get_time_resource (res_name, res_class, sec_p) } unsigned int -get_seconds_resource (res_name, res_class) - char *res_name, *res_class; +get_seconds_resource (Display *dpy, char *res_name, char *res_class) { - return get_time_resource (res_name, res_class, True); + return get_time_resource (dpy, res_name, res_class, True); } unsigned int -get_minutes_resource (res_name, res_class) - char *res_name, *res_class; +get_minutes_resource (Display *dpy, char *res_name, char *res_class) { - return get_time_resource (res_name, res_class, False); + return get_time_resource (dpy, res_name, res_class, False); }