Daniel Stenberg | 9f6733f | 2002-03-25 14:21:30 +0000 | [diff] [blame] | 1 | /* xscreensaver, Copyright (c) 1992, 1997, 1998 |
| 2 | * Jamie Zawinski <jwz@jwz.org> |
| 3 | * |
| 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 |
| 10 | * implied warranty. |
| 11 | */ |
| 12 | |
| 13 | #include "utils.h" |
| 14 | #include "resources.h" |
| 15 | #include <X11/Xresource.h> |
| 16 | |
| 17 | |
| 18 | /* Resource functions. Assumes: */ |
| 19 | |
| 20 | extern char *progname; |
| 21 | extern char *progclass; |
| 22 | extern XrmDatabase db; |
| 23 | |
Daniel Stenberg | 9f6733f | 2002-03-25 14:21:30 +0000 | [diff] [blame] | 24 | #ifndef isupper |
| 25 | # define isupper(c) ((c) >= 'A' && (c) <= 'Z') |
| 26 | #endif |
| 27 | #ifndef _tolower |
| 28 | # define _tolower(c) ((c) - 'A' + 'a') |
| 29 | #endif |
| 30 | |
| 31 | char * |
| 32 | get_string_resource (char *res_name, char *res_class) |
| 33 | { |
| 34 | XrmValue value; |
| 35 | char *type; |
| 36 | char full_name [1024], full_class [1024]; |
| 37 | strcpy (full_name, progname); |
| 38 | strcat (full_name, "."); |
| 39 | strcat (full_name, res_name); |
| 40 | strcpy (full_class, progclass); |
| 41 | strcat (full_class, "."); |
| 42 | strcat (full_class, res_class); |
| 43 | if (XrmGetResource (db, full_name, full_class, &type, &value)) |
| 44 | { |
| 45 | char *str = (char *) malloc (value.size + 1); |
| 46 | strncpy (str, (char *) value.addr, value.size); |
| 47 | str [value.size] = 0; |
| 48 | return str; |
| 49 | } |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | Bool |
| 54 | get_boolean_resource (char *res_name, char *res_class) |
| 55 | { |
| 56 | char *tmp, buf [100]; |
| 57 | char *s = get_string_resource (res_name, res_class); |
| 58 | char *os = s; |
| 59 | if (! s) return 0; |
| 60 | for (tmp = buf; *s; s++) |
| 61 | *tmp++ = isupper (*s) ? _tolower (*s) : *s; |
| 62 | *tmp = 0; |
| 63 | free (os); |
| 64 | |
| 65 | while (*buf && |
| 66 | (buf[strlen(buf)-1] == ' ' || |
| 67 | buf[strlen(buf)-1] == '\t')) |
| 68 | buf[strlen(buf)-1] = 0; |
| 69 | |
| 70 | if (!strcmp (buf, "on") || !strcmp (buf, "true") || !strcmp (buf, "yes")) |
| 71 | return 1; |
| 72 | if (!strcmp (buf,"off") || !strcmp (buf, "false") || !strcmp (buf,"no")) |
| 73 | return 0; |
| 74 | fprintf (stderr, "%s: %s must be boolean, not %s.\n", |
| 75 | progname, res_name, buf); |
| 76 | return 0; |
| 77 | } |
| 78 | |
Daniel Stenberg | 9f6733f | 2002-03-25 14:21:30 +0000 | [diff] [blame] | 79 | unsigned int |
| 80 | get_pixel_resource (char *res_name, char *res_class, |
| 81 | Display *dpy, Colormap cmap) |
| 82 | { |
| 83 | XColor color; |
| 84 | char *s = get_string_resource (res_name, res_class); |
| 85 | char *s2; |
| 86 | if (!s) goto DEFAULT; |
| 87 | |
| 88 | for (s2 = s + strlen(s) - 1; s2 > s; s2--) |
| 89 | if (*s2 == ' ' || *s2 == '\t') |
| 90 | *s2 = 0; |
| 91 | else |
| 92 | break; |
| 93 | |
| 94 | if (! XParseColor (dpy, cmap, s, &color)) |
| 95 | { |
| 96 | fprintf (stderr, "%s: can't parse color %s\n", progname, s); |
| 97 | goto DEFAULT; |
| 98 | } |
| 99 | if (! XAllocColor (dpy, cmap, &color)) |
| 100 | { |
| 101 | fprintf (stderr, "%s: couldn't allocate color %s\n", progname, s); |
| 102 | goto DEFAULT; |
| 103 | } |
| 104 | free (s); |
| 105 | return color.pixel; |
| 106 | DEFAULT: |
| 107 | if (s) free (s); |
| 108 | return ((strlen(res_class) >= 10 && |
| 109 | !strcmp ("Background", res_class + strlen(res_class) - 10)) |
| 110 | ? BlackPixel (dpy, DefaultScreen (dpy)) |
| 111 | : WhitePixel (dpy, DefaultScreen (dpy))); |
| 112 | } |
| 113 | |