/* xscreensaver, Copyright (c) 1992-2014 Jamie Zawinski <jwz@jwz.org>
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation. No representations are made about the suitability of this
* software for any purpose. It is provided "as is" without express or
#if !defined(HAVE_COCOA) && !defined(HAVE_ANDROID)
#include <X11/Xresource.h>
/* These are the Xlib/Xrm versions of these functions.
The Cocoa versions are on OSX/XScreenSaverView.m.
extern XrmDatabase
XtDatabase (Display
*);
static unsigned int get_time_resource (Display
*dpy
,
char *res_name
, char *res_class
,
# define isupper(c) ((c) >= 'A' && (c) <= 'Z')
# define _tolower(c) ((c) - 'A' + 'a')
get_string_resource (Display
*dpy
, char *res_name
, char *res_class
)
char full_name
[1024], full_class
[1024];
strcpy (full_name
, progname
);
strcat (full_name
, res_name
);
strcpy (full_class
, progclass
);
strcat (full_class
, ".");
strcat (full_class
, res_class
);
if (XrmGetResource (XtDatabase (dpy
), full_name
, full_class
, &type
, &value
))
char *str
= (char *) malloc (value
.size
+ 1);
strncpy (str
, (char *) value
.addr
, value
.size
);
get_boolean_resource (Display
*dpy
, char *res_name
, char *res_class
)
char *s
= get_string_resource (dpy
, res_name
, res_class
);
*tmp
++ = isupper (*s
) ? _tolower (*s
) : *s
;
(buf
[strlen(buf
)-1] == ' ' ||
buf
[strlen(buf
)-1] == '\t'))
if (!strcmp (buf
, "on") || !strcmp (buf
, "true") || !strcmp (buf
, "yes"))
if (!strcmp (buf
,"off") || !strcmp (buf
, "false") || !strcmp (buf
,"no"))
fprintf (stderr
, "%s: %s must be boolean, not %s.\n",
progname
, res_name
, buf
);
get_integer_resource (Display
*dpy
, char *res_name
, char *res_class
)
char c
, *s
= get_string_resource (dpy
, res_name
, res_class
);
while (*ss
&& *ss
<= ' ') ss
++; /* skip whitespace */
if (ss
[0] == '0' && (ss
[1] == 'x' || ss
[1] == 'X')) /* 0x: parse as hex */
if (1 == sscanf (ss
+2, "%x %c", (unsigned int *) &val
, &c
))
else /* else parse as dec */
if (1 == sscanf (ss
, "%d %c", &val
, &c
))
fprintf (stderr
, "%s: %s must be an integer, not %s.\n",
get_float_resource (Display
*dpy
, char *res_name
, char *res_class
)
char c
, *s
= get_string_resource (dpy
, res_name
, res_class
);
if (1 == sscanf (s
, " %lf %c", &val
, &c
))
fprintf (stderr
, "%s: %s must be a float, not %s.\n",
#endif /* !HAVE_COCOA && !HAVE_ANDROID */
/* These functions are the same with Xlib, Cocoa and Android:
get_pixel_resource (Display
*dpy
, Colormap cmap
,
char *res_name
, char *res_class
)
char *s
= get_string_resource (dpy
, res_name
, res_class
);
for (s2
= s
+ strlen(s
) - 1; s2
> s
; s2
--)
if (*s2
== ' ' || *s2
== '\t')
if (! XParseColor (dpy
, cmap
, s
, &color
))
fprintf (stderr
, "%s: can't parse color %s", progname
, s
);
if (! XAllocColor (dpy
, cmap
, &color
))
fprintf (stderr
, "%s: couldn't allocate color %s", progname
, s
);
return (unsigned int) color
.pixel
;
Bool black_p
= (strlen(res_class
) >= 10 &&
!strcasecmp ("Background",
res_class
+ strlen(res_class
) - 10));
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 (unsigned int) color
.pixel
;
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.)
parse_time (const char *string
, Bool seconds_default_p
, Bool silent_p
)
if (3 == sscanf (string
, " %u : %2u : %2u %c", &h
, &m
, &s
, &c
))
else if (2 == sscanf (string
, " : %2u : %2u %c", &m
, &s
, &c
) ||
2 == sscanf (string
, " %u : %2u %c", &m
, &s
, &c
))
else if (1 == sscanf (string
, " : %2u %c", &s
, &c
))
else if (1 == sscanf (string
, " %u %c",
(seconds_default_p
? &s
: &m
), &c
))
if (seconds_default_p
) m
= 0;
fprintf (stderr
, "%s: invalid time interval specification \"%s\".\n",
if (s
>= 60 && (h
!= 0 || m
!= 0))
fprintf (stderr
, "%s: seconds > 59 in \"%s\".\n", progname
, string
);
fprintf (stderr
, "%s: minutes > 59 in \"%s\".\n", progname
, string
);
return ((h
* 60 * 60) + (m
* 60) + s
);
get_time_resource (Display
*dpy
, char *res_name
, char *res_class
, Bool sec_p
)
char *s
= get_string_resource (dpy
, res_name
, res_class
);
val
= parse_time (s
, sec_p
, False
);
return (val
< 0 ? 0 : val
);
get_seconds_resource (Display
*dpy
, char *res_name
, char *res_class
)
return get_time_resource (dpy
, res_name
, res_class
, True
);
get_minutes_resource (Display
*dpy
, char *res_name
, char *res_class
)
return get_time_resource (dpy
, res_name
, res_class
, False
);
/* A utility function for event-handler functions:
Returns True if the event is a simple click, Space, Tab, etc.
The idea here is that most hacks interpret to clicks or basic
keypresses as "change it up".
This isn't really the right file for this, but whatever.
screenhack_event_helper (Display
*dpy
, Window window
, XEvent
*event
)
if (event
->xany
.type
== KeyPress
)
XLookupString (&event
->xkey
, &c
, 1, &keysym
, 0);
if (c
== ' ' || c
== '\t' || c
== '\r' || c
== '\n' ||
keysym
== XK_Left
|| keysym
== XK_Right
||
keysym
== XK_Up
|| keysym
== XK_Down
||
keysym
== XK_Prior
|| keysym
== XK_Next
)
else if (event
->xany
.type
== ButtonPress
)
if (event
->xbutton
.button
== 1)