1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
/* xscreensaver, Copyright (c) 2008-2015 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.
*
* Draws repetitive patterns that should undo burned in LCD screens.
* Concept shamelessly cloned from
* http://toastycode.com/blog/2008/02/05/lcd-scrub/
*/
#include "screenhack.h"
struct state {
Display *dpy;
Window window;
XWindowAttributes xgwa;
enum { HORIZ_W, HORIZ_B,
VERT_W, VERT_B,
DIAG_W, DIAG_B,
WHITE, BLACK,
RGB,
RANDOM,
END } mode;
unsigned int enabled_mask;
int count;
GC fg, bg, bg2;
int color_tick;
int delay;
int spread;
int cycles;
XImage *collisions;
long ncollisions;
};
static void
pick_mode (struct state *st)
{
st->count = 0;
while (1)
{
if (++st->mode == END)
st->mode = 0;
if (st->enabled_mask & (1 << st->mode))
break;
}
}
static void *
lcdscrub_init (Display *dpy, Window window)
{
struct state *st = (struct state *) calloc (1, sizeof(*st));
XGCValues gcv;
unsigned long fgp, bgp;
st->dpy = dpy;
st->window = window;
st->delay = get_integer_resource (st->dpy, "delay", "Integer");
st->spread = get_integer_resource (st->dpy, "spread", "Integer");
st->cycles = get_integer_resource (st->dpy, "cycles", "Integer");
XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
fgp = get_pixel_resource (st->dpy, st->xgwa.colormap,
"foreground", "Foreground");
bgp = get_pixel_resource (st->dpy, st->xgwa.colormap,
"background", "Background");
gcv.foreground = bgp;
gcv.background = fgp;
st->bg = XCreateGC (st->dpy, st->window, GCForeground, &gcv);
st->bg2 = XCreateGC (st->dpy, st->window, GCForeground, &gcv);
gcv.foreground = fgp;
gcv.background = bgp;
st->fg = XCreateGC (st->dpy, st->window, GCForeground, &gcv);
#ifdef HAVE_JWXYZ
jwxyz_XSetAntiAliasing (st->dpy, st->fg, False);
jwxyz_XSetAntiAliasing (st->dpy, st->bg, False);
jwxyz_XSetAntiAliasing (st->dpy, st->bg2, False);
#endif
st->enabled_mask = 0;
# define PREF(R,F) \
if (get_boolean_resource (st->dpy, R, "Mode")) st->enabled_mask |= (1 << F)
PREF("modeHW", HORIZ_W);
PREF("modeHB", HORIZ_B);
PREF("modeVW", VERT_W);
PREF("modeVB", VERT_B);
PREF("modeDW", DIAG_W);
PREF("modeDB", DIAG_B);
PREF("modeW", WHITE);
PREF("modeB", BLACK);
PREF("modeRGB", RGB);
PREF("modeRandom", RANDOM);
# undef PREF
if (! st->enabled_mask)
{
fprintf (stderr, "%s: no modes enabled\n", progname);
exit (1);
}
pick_mode (st);
return st;
}
/* A test harness for visualizing different random number generators.
This doesn't really belong in lcdscrub, but it was a convenient
place to put it.
*/
#if 0 /* mwc1616 */
static unsigned long mwc1616_x = 1;
static unsigned long mwc1616_y = 2;
static void
mwc1616_srand (unsigned long seed)
{
mwc1616_x = seed | 1;
mwc1616_y = seed | 2;
}
static unsigned long
mwc1616 (void)
{
mwc1616_x = 18000 * (mwc1616_x & 0xFFFF) + (mwc1616_x >> 16);
mwc1616_y = 30903 * (mwc1616_y & 0xFFFF) + (mwc1616_y >> 16);
return (mwc1616_x << 16) + (mwc1616_y & 0xFFFF);
}
# undef random
# undef srand
# define srand mwc1616_srand
# define random() ((unsigned int) (mwc1616() & (unsigned int) (~0)))
#elif 0 /* xorshift128plus */
static uint64_t xo_state0 = 1;
static uint64_t xo_state1 = 2;
static void
xorshift128plus_srand (unsigned long seed)
{
xo_state0 = seed | 1;
xo_state1 = seed | 2;
}
static uint64_t
xorshift128plus (void)
{
register uint64_t s1 = xo_state0;
register uint64_t s0 = xo_state1;
xo_state0 = s0;
s1 ^= s1 << 23;
s1 ^= s1 >> 17;
s1 ^= s0;
s1 ^= s0 >> 26;
xo_state1 = s1;
return s1;
}
# undef random
# undef srand
# define srand xorshift128plus_srand
# define random() ((unsigned int) (xorshift128plus() & (unsigned int) (~0)))
#else /* ya_random */
# undef srand
# define srand(n)
#endif /* ya_random */
/* If you see patterns in this image, the PRNG sucks.
*/
static void
lcdscrub_random (struct state *st)
{
unsigned long steps_per_frame = 3000000;
unsigned long segments = 0x8000; /* 2^15 */
if (! st->collisions)
{
struct timeval tp;
# if GETTIMEOFDAY_TWO_ARGS
gettimeofday (&tp, 0);
# else
gettimeofday (&tp);
# endif
srand ((unsigned int) (tp.tv_sec ^ tp.tv_usec));
st->collisions =
XCreateImage (st->dpy, st->xgwa.visual, 1, XYPixmap,
0, NULL, segments, segments, 8, 0);
if (! st->collisions) abort();
st->collisions->data = (char *)
calloc (segments, st->collisions->bytes_per_line); /* 128 MB */
if (! st->collisions->data) abort();
}
while (--steps_per_frame)
{
unsigned long x = random() & (segments-1);
unsigned long y = random() & (segments-1);
unsigned long p = XGetPixel (st->collisions, x, y) ? 0 : 1;
XPutPixel (st->collisions, x, y, p);
st->ncollisions += (p ? 1 : -1);
}
{
int w, h;
Pixmap p;
GC gc;
XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
w = st->xgwa.width;
h = st->xgwa.height;
p = XCreatePixmap (st->dpy, st->window, w, h, 1);
gc = XCreateGC (st->dpy, p, 0, 0);
XSetBackground (st->dpy, gc, 0);
XSetForeground (st->dpy, gc, 1);
XPutImage (st->dpy, p, gc, st->collisions, 0, 0, 0, 0, w, h);
XFreeGC (st->dpy, gc);
gc = st->fg;
XClearWindow (st->dpy, st->window);
XSetClipMask (st->dpy, gc, p);
XFillRectangle (st->dpy, st->window, gc, 0, 0, w, h);
XFreePixmap (st->dpy, p);
}
/*
fprintf(stderr, "%.2f\n", st->ncollisions / (float) (segments*segments));
*/
}
static unsigned long
lcdscrub_draw (Display *dpy, Window window, void *closure)
{
struct state *st = (struct state *) closure;
int count = st->count % st->spread;
int i;
GC fg = (st->mode & 1 ? st->fg : st->bg);
GC bg = (st->mode & 1 ? st->bg : st->fg);
switch (st->mode) {
case HORIZ_W:
case HORIZ_B:
XFillRectangle (st->dpy, st->window, bg, 0, 0,
st->xgwa.width, st->xgwa.height);
for (i = count; i < st->xgwa.height; i += st->spread)
XDrawLine (st->dpy, st->window, fg, 0, i, st->xgwa.width, i);
break;
case VERT_W:
case VERT_B:
XFillRectangle (st->dpy, st->window, bg, 0, 0,
st->xgwa.width, st->xgwa.height);
for (i = count; i < st->xgwa.width; i += st->spread)
XDrawLine (st->dpy, st->window, fg, i, 0, i, st->xgwa.height);
break;
case DIAG_W:
case DIAG_B:
XFillRectangle (st->dpy, st->window, bg, 0, 0,
st->xgwa.width, st->xgwa.height);
for (i = count; i < st->xgwa.width; i += st->spread)
XDrawLine (st->dpy, st->window, fg, i, 0,
i + st->xgwa.width, st->xgwa.width);
for (i = -count; i < st->xgwa.height; i += st->spread)
XDrawLine (st->dpy, st->window, fg, 0, i,
st->xgwa.height, i + st->xgwa.height);
break;
case RGB:
{
int scale = 10 * 8; /* 8 sec */
static const unsigned short colors[][3] = {
{ 0xFFFF, 0x0000, 0x0000 },
{ 0x0000, 0xFFFF, 0x0000 },
{ 0x0000, 0x0000, 0xFFFF },
{ 0xFFFF, 0xFFFF, 0x0000 },
{ 0xFFFF, 0x0000, 0xFFFF },
{ 0x0000, 0xFFFF, 0xFFFF },
{ 0xFFFF, 0xFFFF, 0xFFFF },
{ 0x0000, 0x0000, 0x0000 },
};
static unsigned long last = 0;
XColor xc;
bg = st->bg2;
xc.red = colors[st->color_tick / scale][0];
xc.green = colors[st->color_tick / scale][1];
xc.blue = colors[st->color_tick / scale][2];
if (last) XFreeColors (st->dpy, st->xgwa.colormap, &last, 1, 0);
XAllocColor (st->dpy, st->xgwa.colormap, &xc);
last = xc.pixel;
XSetForeground (st->dpy, bg, xc.pixel);
st->color_tick = (st->color_tick + 1) % (countof(colors) * scale);
/* fall through */
}
case WHITE:
case BLACK:
XFillRectangle (st->dpy, st->window, bg, 0, 0,
st->xgwa.width, st->xgwa.height);
break;
case RANDOM:
lcdscrub_random (st);
break;
default:
abort();
break;
}
st->count++;
if (st->count > st->spread * st->cycles)
pick_mode (st);
return st->delay;
}
static void
lcdscrub_reshape (Display *dpy, Window window, void *closure,
unsigned int w, unsigned int h)
{
}
static Bool
lcdscrub_event (Display *dpy, Window window, void *closure, XEvent *event)
{
return False;
}
static void
lcdscrub_free (Display *dpy, Window window, void *closure)
{
struct state *st = (struct state *) closure;
XFreeGC (dpy, st->fg);
XFreeGC (dpy, st->bg);
XFreeGC (dpy, st->bg2);
if (st->collisions)
{
free (st->collisions->data);
st->collisions->data = 0;
XDestroyImage (st->collisions);
}
free (st);
}
static const char *lcdscrub_defaults [] = {
".background: black",
".foreground: white",
"*fpsSolid: True",
"*delay: 100000",
"*spread: 8",
"*cycles: 60",
"*modeHW: True",
"*modeHB: True",
"*modeVW: True",
"*modeVB: True",
"*modeDW: True",
"*modeDB: True",
"*modeW: True",
"*modeB: True",
"*modeRGB: True",
"*modeRandom: False",
0
};
static XrmOptionDescRec lcdscrub_options [] = {
{ "-delay", ".delay", XrmoptionSepArg, 0 },
{ "-spread", ".spread", XrmoptionSepArg, 0 },
{ "-cycles", ".cycles", XrmoptionSepArg, 0 },
{ "-no-hw", ".modeHW", XrmoptionNoArg, "False" },
{ "-no-hb", ".modeHB", XrmoptionNoArg, "False" },
{ "-no-vw", ".modeVW", XrmoptionNoArg, "False" },
{ "-no-vb", ".modeVB", XrmoptionNoArg, "False" },
{ "-no-dw", ".modeDW", XrmoptionNoArg, "False" },
{ "-no-db", ".modeDB", XrmoptionNoArg, "False" },
{ "-no-w", ".modeW", XrmoptionNoArg, "False" },
{ "-no-b", ".modeB", XrmoptionNoArg, "False" },
{ "-no-rgb", ".modeRGB", XrmoptionNoArg, "False" },
{ "-random", ".modeRandom", XrmoptionNoArg, "True" },
{ 0, 0, 0, 0 }
};
XSCREENSAVER_MODULE ("LCDscrub", lcdscrub)
|