Skip to content

Instantly share code, notes, and snippets.

@Mononofu
Created March 9, 2012 20:34
Show Gist options
  • Save Mononofu/2008542 to your computer and use it in GitHub Desktop.
Save Mononofu/2008542 to your computer and use it in GitHub Desktop.
Minimal xscreensaver
/* xscreensaver, Copyright (c) 1992-2008 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
* implied warranty.
*/
#include "screenhack.h"
struct state {
Display *dpy;
Window window;
GC gc;
int delay;
unsigned long fg, bg, pixels [512];
int npixels;
int xlim, ylim;
Colormap cmap;
};
static void *
minimal_init (Display *dpy, Window window)
{
struct state *st = (struct state *) calloc (1, sizeof(*st));
int i = 0;
XGCValues gcv;
XWindowAttributes xgwa;
st->dpy = dpy;
st->window = window;
XGetWindowAttributes (st->dpy, st->window, &xgwa);
st->xlim = xgwa.width;
st->ylim = xgwa.height;
st->cmap = xgwa.colormap;
gcv.foreground= st->fg= get_pixel_resource(st->dpy, st->cmap, "foreground","Foreground");
gcv.background= st->bg= get_pixel_resource(st->dpy, st->cmap, "background","Background");
st->delay = get_integer_resource (st->dpy, "delay", "Integer");
if (st->delay < 0) st->delay = 0;
st->gc = XCreateGC (st->dpy, st->window, GCForeground, &gcv);
for(st->npixels = 0; st->npixels < 512; st->npixels++)
{
XColor fgc;
fgc.flags = DoRed|DoGreen|DoBlue;
fgc.red = random ();
fgc.green = random ();
fgc.blue = random ();
XAllocColor (st->dpy, st->cmap, &fgc);
st->pixels[st->npixels] = fgc.pixel;
}
return st;
}
static unsigned long
minimal_draw (Display *dpy, Window window, void *closure)
{
struct state *st = (struct state *) closure;
int x, y, w=50, h=50, i;
XGCValues gcv;
x = random () % (st->xlim - w);
y = random () % (st->ylim - h);
gcv.foreground = st->pixels [random () % st->npixels];
XChangeGC (st->dpy, st->gc, GCForeground, &gcv);
XFillRectangle (st->dpy, st->window, st->gc, x, y, w, h);
return st->delay;
}
static const char *minimal_defaults [] = {
".background: black",
".foreground: white",
"*fpsSolid: true",
"*delay: 10000",
0
};
static XrmOptionDescRec minimal_options [] = {
{ "-delay", ".delay", XrmoptionSepArg, 0 },
{ 0, 0, 0, 0 }
};
static void
minimal_reshape (Display *dpy, Window window, void *closure,
unsigned int w, unsigned int h)
{
struct state *st = (struct state *) closure;
st->xlim = w;
st->ylim = h;
}
static Bool
minimal_event (Display *dpy, Window window, void *closure, XEvent *event)
{
return False;
}
static void
minimal_free (Display *dpy, Window window, void *closure)
{
}
XSCREENSAVER_MODULE ("Minimal", minimal)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment