2 * lockward.c: First attempt at an Xscreensaver.
4 * Leo L. Schwab 2007.08.17
6 * Copyright (c) 2007 Leo L. Schwab
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to permit
13 * persons to whom the Software is furnished to do so, subject to the
14 * following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
22 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #include "xlockmore.h"
34 /***************************************************************************
37 #ifdef USE_GL /* whole file */
39 #define DEFAULTS "*delay: 20000 \n"\
42 #define refresh_lockward 0
45 #define NUMOF(x) (sizeof ((x)) / sizeof ((*x)))
50 #define COLORIDX_SHF 4
53 /***************************************************************************
54 * Structure definitions.
56 struct lockward_context; /* Forward declaration. */
58 typedef struct bladestate {
59 uint8_t outer, inner; /* Radii */
62 typedef struct spinnerstate {
63 GLfloat rot; /* Terminal rotation after count expires */
64 GLfloat rotinc; /* Per-frame increment to rot. */
67 int ncolors; /* n.4 fixed-point */
68 int ccolor; /* n.4 fixed-point */
69 int colorinc; /* n.4 fixed-point */
74 typedef struct blinkstate {
75 int (*drawfunc) (struct lockward_context *ctx,
76 struct blinkstate *bs);
77 uint32_t *noise; /* For draw_blink_segment_scatter() */
80 int16_t dwell; /* <0: sharp >0: decay */
89 BTYPE_RADIAL_SINGLE = 0,
92 BTYPE_RADIAL_DOUBLESEQ,
95 BTYPE_CONCENTRIC_SINGLE,
96 BTYPE_CONCENTRIC_RANDOM,
98 BTYPE_SEGMENT_SCATTER,
102 typedef struct lockward_context {
103 GLXContext *glx_context;
105 spinnerstate spinners[NSPINNERS];
108 GLuint blades_outer, blades_inner;
116 /***************************************************************************
119 static void free_lockward (lockward_context *ctx);
122 /***************************************************************************
125 static lockward_context *g_ctx = NULL;
126 static Bool g_blink_p = True;
127 static int g_blades = NBLADES;
128 static int g_rotateidle_min,
130 static int g_blinkidle_min,
132 static int g_blinkdwell_min,
135 static XrmOptionDescRec opts[] = {
136 { "-blink", ".blink", XrmoptionNoArg, "on" },
137 { "+blink", ".blink", XrmoptionNoArg, "off" },
138 { "-rotateidle-min", ".rotateidle-min", XrmoptionSepArg, 0 },
139 { "-rotateidle-max", ".rotateidle-max", XrmoptionSepArg, 0 },
140 { "-blinkidle-min", ".blinkidle-min", XrmoptionSepArg, 0 },
141 { "-blinkidle-max", ".blinkidle-max", XrmoptionSepArg, 0 },
142 { "-blinkdwell-min", ".blinkdwell-min", XrmoptionSepArg, 0 },
143 { "-blinkdwell-max", ".blinkdwell-max", XrmoptionSepArg, 0 },
146 static argtype vars[] = {
147 { &g_blink_p, "blink", "Blink", "True", t_Bool },
148 { &g_rotateidle_min, "rotateidle-min", "Rotateidle-min", "1000", t_Int },
149 { &g_rotateidle_max, "rotateidle-max", "Rotateidle-max", "6000", t_Int },
150 { &g_blinkidle_min, "blinkidle-min", "Blinkidle-min", "1000", t_Int },
151 { &g_blinkidle_max, "blinkidle-max", "Blinkidle-max", "9000", t_Int },
152 { &g_blinkdwell_min, "blinkdwell-min", "Blinkdwell-min", "100", t_Int },
153 { &g_blinkdwell_max, "blinkdwell-max", "Blinkdwell-max", "600", t_Int },
156 static OptionStruct desc[] = {
157 { "-/+blink", "Turn on/off blinking effects." },
158 { "-rotateidle-min", "Minimum idle time for rotators, in milliseconds." },
159 { "-rotateidle-max", "Maximum idle time for rotators, in milliseconds." },
160 { "-blinkidle-min", "Minimum idle time between blink effects, in milliseconds." },
161 { "-blinkidle-max", "Maximum idle time between blink effects, in milliseconds." },
162 { "-blinkdwell-min", "Minimum dwell time for blink effects, in milliseconds." },
163 { "-blinkdwell-max", "Maximum dwell time for blink effects, in milliseconds." },
166 ENTRYPOINT ModeSpecOpt lockward_opts = {
167 NUMOF(opts), opts, NUMOF(vars), vars, desc
171 /***************************************************************************
175 reshape_lockward (ModeInfo *mi, int width, int height)
177 GLfloat h = (GLfloat) height / (GLfloat) width;
179 glViewport (0, 0, (GLint) width, (GLint) height);
181 glMatrixMode (GL_PROJECTION);
184 gluOrtho2D (-8.0, 8.0, -8.0 * h, 8.0 * h);
186 gluOrtho2D (-8.0 / h, 8.0 / h, -8.0, 8.0);
188 glMatrixMode (GL_MODELVIEW);
192 lockward_handle_event (ModeInfo *mi, XEvent *event)
194 lockward_context *ctx = &g_ctx[MI_SCREEN (mi)];
196 if (event->xany.type == KeyPress) {
200 XLookupString (&event->xkey, &c, 1, &keysym, 0);
211 /***************************************************************************
215 random_blade_rot (lockward_context *ctx, struct spinnerstate *ss)
218 * The circle is divided up in to g_blades divisions. The idea here
219 * is to rotate to an exact division point.
221 * The target rotation is computed via random numbers.
223 * The time it takes to get there is a maximum of six seconds per
224 * division, and a minimum of one second (no matter how far away it
225 * is), and is selected by random numbers.
227 * The time value is converted into frames, and a per-frame rotation
230 * During rendering, we approach the target rotation by subtracting
231 * from it the per-frame rotation times the number of outstanding
232 * ticks. Doing it this way means we'll hit the target rotation
233 * exactly, without low-order errors creeping in to the values (at
234 * least not nearly as quickly).
239 dist = random() % g_blades + 1;
241 ss->rotcount = random() % (6 * dist * ctx->fps - ctx->fps)
246 d = dist * 360.0 / (GLfloat) g_blades;
248 ss->rotinc = d / (GLfloat) ss->rotcount;
253 * A "blade" is pie-wedge shaped flat thing that is rotated around where the
254 * apex is/would be. Initially envisioned as 1/12th of a circle, but that
255 * could be configurable. The inner and outer edges are rounded off using
256 * six subdivisions so that, when multiple blades are assembled, it looks
257 * more like a circle and less like a polygon.
259 * The blade is assembled as a tri-fan. It is oriented centered at 3
260 * o'clock. The blade is composed of two display lists -- arcs, essentially
261 * -- the outer and the inner one. The outer one *must* be called before
262 * the inner one, or the blade clockwise-ness will be wrong, and become
263 * invisible. Arcs of various radii are compiled.
268 gen_blade_arcs (lockward_context *ctx)
270 GLfloat here, there, step;
274 there = M_PI * 2.0 / g_blades;
275 step = there / SUBDIV;
276 here -= SUBDIV * step / 2.0;
277 there -= SUBDIV * step / 2.0;
280 * Build outer blade arcs.
281 * Start at left side of outer radius. Strike all its vertices.
283 for (n = 0; n < NRADII; ++n) {
284 glNewList (ctx->blades_outer + n, GL_COMPILE);
285 for (i = SUBDIV; i >= 0; --i)
286 glVertex3f (cos (here + step * i) * (n + 1.0),
287 sin (here + step * i) * (n + 1.0), 0);
292 * Build inner blade arcs.
293 * Move to inner radius, strike all vertices in opposite order.
295 for (n = 0; n < NRADII; ++n) {
296 glNewList (ctx->blades_inner + n, GL_COMPILE);
297 for (i = 0; i <= SUBDIV; ++i)
298 glVertex3f (cos (here + step * i) * (n + 1.0),
299 sin (here + step * i) * (n + 1.0), 0);
305 gen_rings (lockward_context *ctx)
310 step = M_PI * 2.0 / (g_blades * SUBDIV);
312 for (n = 0; n < NRADII - 1; ++n) {
313 glNewList (ctx->rings + n, GL_COMPILE);
314 glBegin (GL_TRIANGLE_STRIP);
315 for (i = g_blades * SUBDIV; i >= 0; --i) {
316 glVertex3f (cos (step * i) * (n + 1.0),
317 sin (step * i) * (n + 1.0), 0);
318 glVertex3f (cos (step * i) * (n + 2.0),
319 sin (step * i) * (n + 2.0), 0);
327 /***************************************************************************
331 calc_interval_frames (lockward_context *ctx, int min, int max)
334 * Compute random interval between min and max milliseconds.
335 * Returned value is in frames.
341 i += random() % (max - min);
343 return i * ctx->fps / 1000;
347 set_alpha_by_dwell (struct blinkstate *bs)
350 bs->color[3] = (GLfloat) bs->dwellcnt / (GLfloat) bs->dwell;
352 bs->color[3] = bs->dwellcnt > (-bs->dwell >> 2) ? 1.0 : 0.0;
356 draw_blink_blade (lockward_context *ctx, int inner, int outer)
358 glBegin (GL_TRIANGLE_FAN);
359 glCallList (ctx->blades_outer + outer);
360 glCallList (ctx->blades_inner + inner);
365 draw_blink_radial_random (lockward_context *ctx, struct blinkstate *bs)
370 * There is no sense of direction in a random sweep, so re-use the
371 * 'direction' field to hold the current blade we're messing with.
373 if (bs->dwellcnt < 0) {
374 if (bs->counter <= 0) {
380 * Find available blade. Potentially very slow, depending on
381 * how unlucky we are.
384 i = random() % g_blades;
385 } while (bs->val & (1 << i));
386 bs->val |= (1 << i); /* Mark as used. */
388 if ((bs->dwellcnt = bs->dwell) < 0)
389 bs->dwellcnt = -bs->dwellcnt;
391 if ( bs->type == BTYPE_SEGMENT_SINGLE
392 || bs->type == BTYPE_SEGMENT_RANDOM)
393 bs->radius = random() % (NRADII - 1);
398 set_alpha_by_dwell (bs);
399 glBlendFunc (GL_DST_COLOR, GL_SRC_ALPHA);
400 glColor4fv (bs->color);
401 glRotatef (bs->direction * 360.0 / (GLfloat) g_blades, 0, 0, 1);
403 draw_blink_blade (ctx, bs->radius, bs->radius + 1);
405 draw_blink_blade (ctx, 0, NRADII - 1);
409 return SUBDIV + SUBDIV;
413 draw_blink_radial_sequential (lockward_context *ctx, struct blinkstate *bs)
415 if (bs->dwellcnt < 0) {
416 if (bs->counter <= 0) {
420 if ((bs->dwellcnt = bs->dwell) < 0)
421 bs->dwellcnt = -bs->dwellcnt;
425 set_alpha_by_dwell (bs);
426 glBlendFunc (GL_DST_COLOR, GL_SRC_ALPHA);
427 glColor4fv (bs->color);
428 glRotatef ((bs->counter * bs->direction + (int) bs->val)
429 * 360.0 / (GLfloat) g_blades,
431 draw_blink_blade (ctx, 0, NRADII - 1);
435 return SUBDIV + SUBDIV;
439 draw_blink_radial_doubleseq (lockward_context *ctx, struct blinkstate *bs)
443 if (bs->dwellcnt < 0) {
444 if (bs->counter <= 0) {
448 if ((bs->dwellcnt = bs->dwell) < 0)
449 bs->dwellcnt = -bs->dwellcnt;
453 set_alpha_by_dwell (bs);
454 glBlendFunc (GL_DST_COLOR, GL_SRC_ALPHA);
455 glColor4fv (bs->color);
458 glRotatef (((int) bs->val + bs->counter) * 360.0 / (GLfloat) g_blades,
460 draw_blink_blade (ctx, 0, NRADII - 1);
462 polys = SUBDIV + SUBDIV;
464 if (bs->counter && bs->counter < g_blades / 2) {
465 glRotatef (((int) bs->val - bs->counter)
466 * 360.0 / (GLfloat) g_blades,
468 draw_blink_blade (ctx, 0, NRADII - 1);
469 polys += SUBDIV + SUBDIV;
478 draw_blink_concentric_random (lockward_context *ctx, struct blinkstate *bs)
482 if (bs->dwellcnt < 0) {
483 if (bs->counter <= 0) {
489 i = random() % (NRADII - 1);
490 } while (bs->val & (1 << i));
493 if ((bs->dwellcnt = bs->dwell) < 0)
494 bs->dwellcnt = -bs->dwellcnt;
499 set_alpha_by_dwell (bs);
500 glBlendFunc (GL_DST_COLOR, GL_SRC_ALPHA);
501 glColor4fv (bs->color);
502 glCallList (ctx->rings + bs->direction);
506 return g_blades * SUBDIV * 2;
510 draw_blink_concentric_sequential (lockward_context *ctx, struct blinkstate *bs)
512 if (bs->dwellcnt < 0) {
513 if (bs->counter <= 0) {
517 if ((bs->dwellcnt = bs->dwell) < 0)
518 bs->dwellcnt = -bs->dwellcnt;
522 set_alpha_by_dwell (bs);
523 glBlendFunc (GL_DST_COLOR, GL_SRC_ALPHA);
524 glColor4fv (bs->color);
525 if (bs->direction > 0)
526 glCallList (ctx->rings + (NRADII - 2) - bs->counter);
528 glCallList (ctx->rings + bs->counter);
532 return g_blades * SUBDIV * 2;
536 draw_blink_segment_scatter (lockward_context *ctx, struct blinkstate *bs)
540 if (bs->dwellcnt < 0) {
541 if (bs->counter <= 0) {
547 * Init random noise array. On average, 1/4 of the bits will
548 * be set, which should look nice. (1/2 looks too busy.)
550 for (i = g_blades; --i >= 0; )
551 bs->noise[i] = random() & random()
552 & ((1 << (NRADII - 1)) - 1);
554 if ((bs->dwellcnt = bs->dwell) < 0)
555 bs->dwellcnt = -bs->dwellcnt;
559 set_alpha_by_dwell (bs);
560 glBlendFunc (GL_DST_COLOR, GL_SRC_ALPHA);
561 glColor4fv (bs->color);
563 for (i = g_blades; --i >= 0; ) {
564 register uint32_t bits;
568 * Find consecutive runs of 1 bits. Keep going until we run
571 for (bits = bs->noise[i]; bits; ) {
572 inner = ffs (bits) - 1;
573 bits = ~bits & ~((1 << inner) - 1);
574 outer = ffs (bits) - 1;
575 bits = ~bits & ~((1 << outer) - 1);
578 glRotatef (i * 360.0 / (GLfloat) g_blades, 0, 0, 1);
579 draw_blink_blade (ctx, inner, outer);
582 polys += SUBDIV + SUBDIV;
592 random_blink (lockward_context *ctx, struct blinkstate *bs)
600 bs->dwell = calc_interval_frames
601 (ctx, g_blinkdwell_min, g_blinkdwell_max);
603 bs->dwell = -bs->dwell;
605 bs->type = random() % MAX_BTYPE;
608 case BTYPE_RADIAL_SINGLE:
609 case BTYPE_SEGMENT_SINGLE:
610 bs->drawfunc = draw_blink_radial_random;
614 case BTYPE_RADIAL_RANDOM:
615 case BTYPE_SEGMENT_RANDOM:
616 bs->drawfunc = draw_blink_radial_random;
618 bs->counter = g_blades;
620 case BTYPE_RADIAL_SEQ:
621 bs->drawfunc = draw_blink_radial_sequential;
622 bs->val = random() % g_blades; /* Initial offset */
623 bs->direction = random() & 8 ? 1 : -1;
624 bs->counter = g_blades;
626 case BTYPE_RADIAL_DOUBLESEQ:
627 bs->drawfunc = draw_blink_radial_doubleseq;
628 bs->val = random() % g_blades; /* Initial offset */
629 bs->counter = g_blades / 2 + 1;
631 case BTYPE_CONCENTRIC_SINGLE:
632 bs->drawfunc = draw_blink_concentric_random;
636 case BTYPE_CONCENTRIC_RANDOM:
637 bs->drawfunc = draw_blink_concentric_random;
639 bs->counter = NRADII - 1;
641 case BTYPE_CONCENTRIC_SEQ:
642 bs->drawfunc = draw_blink_concentric_sequential;
643 bs->direction = random() & 8 ? 1 : -1;
644 bs->counter = NRADII - 1;
646 case BTYPE_SEGMENT_SCATTER:
647 bs->drawfunc = draw_blink_segment_scatter;
648 bs->counter = random() % (g_blades / 2) + (g_blades / 2) + 1;
654 /***************************************************************************
655 * Main rendering routine.
658 draw_lockward (ModeInfo *mi)
660 lockward_context *ctx = &g_ctx[MI_SCREEN (mi)];
662 Display *dpy = MI_DISPLAY(mi);
663 Window window = MI_WINDOW(mi);
666 GLfloat scolor[4] = {0.0, 0.0, 0.0, 0.5};
668 if (!ctx->glx_context)
671 glXMakeCurrent (MI_DISPLAY (mi), MI_WINDOW (mi), *(ctx->glx_context));
674 glClear (GL_COLOR_BUFFER_BIT);
677 glBlendFunc (GL_ONE, GL_ONE);
679 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
684 mi->polygon_count = 0;
686 for (n = NSPINNERS; --n >= 0; ) {
687 ss = &ctx->spinners[n];
690 i = ss->ccolor >> COLORIDX_SHF;
691 scolor[0] = ss->colors[i].red / 65535.0;
692 scolor[1] = ss->colors[i].green / 65535.0;
693 scolor[2] = ss->colors[i].blue / 65535.0;
697 glRotatef (ss->rot - ss->rotcount * ss->rotinc, 0, 0, 1);
698 for (i = ss->nblades; --i >= 0; ) {
700 glRotatef (360.0 * i / ss->nblades, 0, 0, 1);
702 glBegin (GL_TRIANGLE_FAN);
703 glCallList (ctx->blades_outer + ss->bladeidx[i].outer);
704 glCallList (ctx->blades_inner + ss->bladeidx[i].inner);
708 mi->polygon_count += SUBDIV + SUBDIV;
712 /* Advance rotation. */
714 if (ss->rotcount > 0)
717 if (ss->rotinc == 0.0)
718 random_blade_rot (ctx, ss);
720 /* Compute # of ticks to sit idle. */
723 calc_interval_frames (ctx,
729 /* Advance colors. */
730 if ((ss->ccolor += ss->colorinc) >= ss->ncolors)
731 ss->ccolor -= ss->ncolors;
732 else if (ss->ccolor < 0)
733 ss->ccolor += ss->ncolors;
737 if (ctx->blink.drawfunc) {
739 ctx->blink.drawfunc (ctx, &ctx->blink);
741 if (ctx->nextblink > 0)
744 /* Compute # of frames for blink idle time. */
746 calc_interval_frames (ctx,
749 random_blink (ctx, &ctx->blink);
755 if (MI_IS_FPS (mi)) do_fps (mi);
758 glXSwapBuffers (dpy, window);
762 /***************************************************************************
763 * Initialization/teardown.
766 init_lockward (ModeInfo *mi)
768 lockward_context *ctx;
772 g_ctx = (lockward_context *) calloc (MI_NUM_SCREENS (mi),
773 sizeof (lockward_context));
775 fprintf (stderr, "%s: can't allocate context.\n",
780 ctx = &g_ctx[MI_SCREEN (mi)];
782 ctx->glx_context = init_GL (mi);
784 reshape_lockward (mi, MI_WIDTH (mi), MI_HEIGHT (mi));
786 glEnable (GL_CULL_FACE);
788 glDisable (GL_DEPTH_TEST);
790 glShadeModel (GL_FLAT);
794 glClearColor (0.0, 0.0, 0.0, 1.0);
796 ctx->blades_outer = glGenLists (NRADII);
797 ctx->blades_inner = glGenLists (NRADII);
798 ctx->rings = glGenLists (NRADII - 1);
800 ctx->fps = 1000000 / MI_DELAY (mi);
801 ctx->nextblink = calc_interval_frames
802 (ctx, g_blinkidle_min, g_blinkidle_max);
803 ctx->blink.drawfunc = NULL;
804 ctx->blink.noise = malloc (sizeof (uint32_t) * g_blades);
805 if (!ctx->blink.noise) {
806 fprintf (stderr, "Can't allocate noise array.\n");
810 gen_blade_arcs (ctx);
813 for (i = NSPINNERS; --i >= 0; ) {
814 spinnerstate *ss = &ctx->spinners[i];
819 /* Establish rotation */
820 random_blade_rot (ctx, ss);
823 * Establish color cycling path and rate. Rate avoids zero.
826 ss->colorinc = (random() & ((2 << COLORIDX_SHF) - 1))
827 - (1 << COLORIDX_SHF);
828 if (ss->colorinc >= 0)
831 ss->colors = (XColor *) calloc (ss->ncolors, sizeof (XColor));
834 "Can't allocate XColors for spinner %d.\n",
838 make_smooth_colormap (0, 0, 0,
839 ss->colors, &ss->ncolors,
841 ss->ncolors <<= COLORIDX_SHF;
846 ss->nblades = g_blades;
847 ss->bladeidx = malloc (sizeof (bladestate) * g_blades);
849 fprintf (stderr, "Can't allocate blades.\n");
852 for (n = g_blades; --n >= 0; ) {
854 * Establish blade radii. Can't be equal. Ensure
858 ss->bladeidx[n].outer = random() & 7;
859 ss->bladeidx[n].inner = random() & 7;
860 } while (ss->bladeidx[n].outer ==
861 ss->bladeidx[n].inner);
863 if (ss->bladeidx[n].outer < ss->bladeidx[n].inner) {
866 tmp = ss->bladeidx[n].outer;
867 ss->bladeidx[n].outer = ss->bladeidx[n].inner;
868 ss->bladeidx[n].inner = tmp;
875 free_lockward (lockward_context *ctx)
879 if (ctx->blink.noise)
880 free (ctx->blink.noise);
881 if (glIsList (ctx->rings))
882 glDeleteLists (ctx->rings, NRADII - 1);
883 if (glIsList (ctx->blades_outer))
884 glDeleteLists (ctx->blades_outer, NRADII);
885 if (glIsList (ctx->blades_inner))
886 glDeleteLists (ctx->blades_inner, NRADII);
888 for (i = NSPINNERS; --i >= 0; ) {
889 spinnerstate *ss = &ctx->spinners[i];
899 release_lockward (ModeInfo *mi)
906 for (i = MI_NUM_SCREENS (mi); --i >= 0; ) {
907 if (g_ctx[i].glx_context)
908 glXMakeCurrent (MI_DISPLAY (mi), MI_WINDOW (mi),
909 *(g_ctx[i].glx_context));
910 free_lockward (&g_ctx[i]);
914 free (g_ctx); g_ctx = NULL;
918 XSCREENSAVER_MODULE ("Lockward", lockward)
922 /* vim:se ts=8 sts=8 sw=8: */