1 /* carousel, Copyright (c) 2005-2006 Jamie Zawinski <jwz@jwz.org>
2 * Loads a sequence of images and rotates them around.
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
12 * Created: 21-Feb-2005
15 #define DEF_FONT "-*-times-bold-r-normal-*-240-*"
16 #define DEFAULTS "*count: 7 \n" \
18 "*wireframe: False \n" \
19 "*showFPS: False \n" \
20 "*fpsSolid: True \n" \
22 "*font: " DEF_FONT "\n" \
23 "*desktopGrabber: xscreensaver-getimage -no-desktop %s\n" \
24 "*grabDesktopImages: False \n" \
25 "*chooseRandomImages: True \n"
27 # define refresh_carousel 0
28 # define release_carousel 0
29 # include "xlockmore.h"
32 #define countof(x) (sizeof((x))/sizeof((*x)))
36 # define DEF_SPEED "1.0"
37 # define DEF_DURATION "20"
38 # define DEF_TITLES "True"
39 # define DEF_ZOOM "True"
40 # define DEF_TILT "XY"
41 # define DEF_MIPMAP "True"
42 # define DEF_DEBUG "False"
45 #include "gltrackball.h"
46 #include "grab-ximage.h"
50 # include <X11/Intrinsic.h> /* for XrmDatabase in -debug mode */
57 typedef enum { EARLY, NORMAL, LOADING, OUT, IN, DEAD } fade_mode;
58 static int fade_ticks = 60;
61 char *title; /* the filename of this image */
62 int w, h; /* size in pixels of the image */
63 int tw, th; /* size in pixels of the texture */
64 XRectangle geom; /* where in the image the bits are */
70 image current, loading;
71 GLfloat r, theta; /* radius and rotation on the tube */
72 rotator *rot; /* for zoomery */
73 Bool from_top_p; /* whether this image drops in or rises up */
74 time_t expires; /* when this image should be replaced */
75 fade_mode mode; /* in/out animation state */
77 Bool loaded_p; /* whether background load is done */
82 GLXContext *glx_context;
84 trackball_state *trackball;
86 time_t button_down_time;
88 int nframes; /* how many frames are loaded */
90 image_frame **frames; /* pointers to the frames */
92 Bool awaiting_first_images_p;
93 int loads_in_progress;
95 texture_font_data *texfont;
100 int loading_sw, loading_sh;
102 time_t last_time, now;
107 static carousel_state *sss = NULL;
110 /* Command-line arguments
112 static GLfloat speed; /* animation speed scale factor */
113 static int duration; /* reload images after this long */
114 static Bool mipmap_p; /* Use mipmaps instead of single textures. */
115 static Bool titles_p; /* Display image titles. */
116 static Bool zoom_p; /* Throb the images in and out as they spin. */
117 static char *tilt_str;
118 static Bool tilt_x_p; /* Tilt axis towards the viewer */
119 static Bool tilt_y_p; /* Tilt axis side to side */
120 static Bool debug_p; /* Be loud and do weird things. */
123 static XrmOptionDescRec opts[] = {
124 {"-zoom", ".zoom", XrmoptionNoArg, "True" },
125 {"-no-zoom", ".zoom", XrmoptionNoArg, "False" },
126 {"-tilt", ".tilt", XrmoptionSepArg, 0 },
127 {"-no-tilt", ".tilt", XrmoptionNoArg, "" },
128 {"-titles", ".titles", XrmoptionNoArg, "True" },
129 {"-no-titles", ".titles", XrmoptionNoArg, "False" },
130 {"-mipmaps", ".mipmap", XrmoptionNoArg, "True" },
131 {"-no-mipmaps", ".mipmap", XrmoptionNoArg, "False" },
132 {"-duration", ".duration", XrmoptionSepArg, 0 },
133 {"-debug", ".debug", XrmoptionNoArg, "True" },
134 {"-font", ".font", XrmoptionSepArg, 0 },
135 {"-speed", ".speed", XrmoptionSepArg, 0 },
138 static argtype vars[] = {
139 { &mipmap_p, "mipmap", "Mipmap", DEF_MIPMAP, t_Bool},
140 { &debug_p, "debug", "Debug", DEF_DEBUG, t_Bool},
141 { &titles_p, "titles", "Titles", DEF_TITLES, t_Bool},
142 { &zoom_p, "zoom", "Zoom", DEF_ZOOM, t_Bool},
143 { &tilt_str, "tilt", "Tilt", DEF_TILT, t_String},
144 { &speed, "speed", "Speed", DEF_SPEED, t_Float},
145 { &duration, "duration", "Duration", DEF_DURATION, t_Int},
148 ENTRYPOINT ModeSpecOpt carousel_opts = {countof(opts), opts, countof(vars), vars, NULL};
151 /* Allocates a frame structure and stores it in the list.
154 alloc_frame (ModeInfo *mi)
156 carousel_state *ss = &sss[MI_SCREEN(mi)];
157 image_frame *frame = (image_frame *) calloc (1, sizeof (*frame));
161 frame->rot = make_rotator (0, 0, 0, 0, 0.04 * frand(1.0) * speed, False);
163 glGenTextures (1, &frame->current.texid);
164 glGenTextures (1, &frame->loading.texid);
165 if (frame->current.texid <= 0) abort();
166 if (frame->loading.texid <= 0) abort();
168 if (ss->frames_size <= ss->nframes)
170 ss->frames_size = (ss->frames_size * 1.2) + ss->nframes;
171 ss->frames = (image_frame **)
172 realloc (ss->frames, ss->frames_size * sizeof(*ss->frames));
175 fprintf (stderr, "%s: out of memory (%d images)\n",
176 progname, ss->frames_size);
181 ss->frames[ss->nframes++] = frame;
187 static void image_loaded_cb (const char *filename, XRectangle *geom,
188 int image_width, int image_height,
189 int texture_width, int texture_height,
193 /* Load a new file into the given image struct.
196 load_image (ModeInfo *mi, image_frame *frame)
198 carousel_state *ss = &sss[MI_SCREEN(mi)];
199 int wire = MI_IS_WIREFRAME(mi);
201 if (debug_p && !wire && frame->current.w != 0)
202 fprintf (stderr, "%s: dropped %4d x %-4d %4d x %-4d \"%s\"\n",
204 frame->current.geom.width,
205 frame->current.geom.height,
206 frame->current.tw, frame->current.th,
207 (frame->current.title ? frame->current.title : "(null)"));
212 case NORMAL: frame->mode = LOADING; break;
216 ss->loads_in_progress++;
219 image_loaded_cb (0, 0, 0, 0, 0, 0, frame);
221 load_texture_async (mi->xgwa.screen, mi->window, *ss->glx_context,
222 (MI_WIDTH(mi) / 2) - 1,
223 (MI_HEIGHT(mi) / 2) - 1,
224 mipmap_p, frame->loading.texid,
225 image_loaded_cb, frame);
229 /* Callback that tells us that the texture has been loaded.
232 image_loaded_cb (const char *filename, XRectangle *geom,
233 int image_width, int image_height,
234 int texture_width, int texture_height,
237 image_frame *frame = (image_frame *) closure;
238 ModeInfo *mi = frame->mi;
239 carousel_state *ss = &sss[MI_SCREEN(mi)];
240 int wire = MI_IS_WIREFRAME(mi);
244 frame->loading.w = MI_WIDTH (mi) * (0.5 + frand (1.0));
245 frame->loading.h = MI_HEIGHT (mi);
246 frame->loading.geom.width = frame->loading.w;
247 frame->loading.geom.height = frame->loading.h;
251 if (image_width == 0 || image_height == 0)
254 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
255 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
256 mipmap_p ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
258 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
259 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
261 frame->loading.w = image_width;
262 frame->loading.h = image_height;
263 frame->loading.tw = texture_width;
264 frame->loading.th = texture_height;
265 frame->loading.geom = *geom;
267 if (frame->loading.title)
268 free (frame->loading.title);
269 frame->loading.title = (filename ? strdup (filename) : 0);
271 if (frame->loading.title) /* strip filename to part after last /. */
273 char *s = strrchr (frame->loading.title, '/');
274 if (s) strcpy (frame->loading.title, s+1);
278 fprintf (stderr, "%s: loaded %4d x %-4d %4d x %-4d \"%s\"\n",
280 frame->loading.geom.width,
281 frame->loading.geom.height,
282 frame->loading.tw, frame->loading.th,
283 (frame->loading.title ? frame->loading.title : "(null)"));
287 frame->loaded_p = True;
289 if (ss->loads_in_progress <= 0) abort();
290 ss->loads_in_progress--;
292 /* This image expires N seconds after it finished loading. */
293 frame->expires = time((time_t *) 0) + (duration * MI_COUNT(mi));
297 case EARLY: /* part of the initial batch of images */
299 image swap = frame->current;
300 frame->current = frame->loading;
301 frame->loading = swap;
304 case LOADING: /* start dropping the old image out */
307 frame->mode_tick = fade_ticks / speed;
308 frame->from_top_p = random() & 1;
317 static void loading_msg (ModeInfo *mi, int n);
320 load_initial_images (ModeInfo *mi)
322 carousel_state *ss = &sss[MI_SCREEN(mi)];
324 Bool all_loaded_p = True;
325 for (i = 0; i < ss->nframes; i++)
326 if (! ss->frames[i]->loaded_p)
327 all_loaded_p = False;
331 if (ss->nframes < MI_COUNT (mi))
333 /* The frames currently on the list are fully loaded.
334 Start the next one loading. (We run the image loader
335 asynchronously, but we load them one at a time.)
337 load_image (mi, alloc_frame (mi));
341 /* The first batch of images are now all loaded!
342 Stagger the expire times so that they don't all drop out at once.
344 time_t now = time((time_t *) 0);
347 for (i = 0; i < ss->nframes; i++)
349 image_frame *frame = ss->frames[i];
351 frame->theta = i * 360.0 / ss->nframes;
352 frame->expires = now + (duration * (i + 1));
353 frame->mode = NORMAL;
356 /* Instead of always going clockwise, shuffle the expire times
357 of the frames so that they drop out in a random order.
359 for (i = 0; i < ss->nframes; i++)
361 image_frame *frame1 = ss->frames[i];
362 image_frame *frame2 = ss->frames[random() % ss->nframes];
363 time_t swap = frame1->expires;
364 frame1->expires = frame2->expires;
365 frame2->expires = swap;
368 ss->awaiting_first_images_p = False;
372 loading_msg (mi, ss->nframes-1);
374 return !ss->awaiting_first_images_p;
379 reshape_carousel (ModeInfo *mi, int width, int height)
381 GLfloat h = (GLfloat) height / (GLfloat) width;
383 glViewport (0, 0, (GLint) width, (GLint) height);
385 glMatrixMode(GL_PROJECTION);
387 gluPerspective (60.0, 1/h, 1.0, 8.0);
389 glMatrixMode(GL_MODELVIEW);
391 gluLookAt( 0.0, 0.0, 2.6,
395 glClear(GL_COLOR_BUFFER_BIT);
400 carousel_handle_event (ModeInfo *mi, XEvent *event)
402 carousel_state *ss = &sss[MI_SCREEN(mi)];
404 if (event->xany.type == ButtonPress &&
405 event->xbutton.button == Button1)
407 if (! ss->button_down_p)
408 ss->button_down_time = time((time_t *) 0);
409 ss->button_down_p = True;
410 gltrackball_start (ss->trackball,
411 event->xbutton.x, event->xbutton.y,
412 MI_WIDTH (mi), MI_HEIGHT (mi));
415 else if (event->xany.type == ButtonRelease &&
416 event->xbutton.button == Button1)
418 if (ss->button_down_p)
420 /* Add the time the mouse was held to the expire times of all
421 frames, so that mouse-dragging doesn't count against
424 int secs = time((time_t *) 0) - ss->button_down_time;
426 for (i = 0; i < ss->nframes; i++)
427 ss->frames[i]->expires += secs;
429 ss->button_down_p = False;
432 else if (event->xany.type == ButtonPress &&
433 (event->xbutton.button == Button4 ||
434 event->xbutton.button == Button5))
436 gltrackball_mousewheel (ss->trackball, event->xbutton.button, 5,
437 !event->xbutton.state);
440 else if (event->xany.type == MotionNotify &&
443 gltrackball_track (ss->trackball,
444 event->xmotion.x, event->xmotion.y,
445 MI_WIDTH (mi), MI_HEIGHT (mi));
453 /* Kludge to add "-v" to invocation of "xscreensaver-getimage" in -debug mode
456 hack_resources (Display *dpy)
459 char *res = "desktopGrabber";
460 char *val = get_string_resource (dpy, res, "DesktopGrabber");
464 XrmDatabase db = XtDatabase (dpy);
465 sprintf (buf1, "%.100s.%.100s", progname, res);
466 sprintf (buf2, "%.200s -v", val);
468 value.size = strlen(buf2);
469 XrmPutResource (&db, buf1, "String", &value);
470 # endif /* !HAVE_COCOA */
475 loading_msg (ModeInfo *mi, int n)
477 carousel_state *ss = &sss[MI_SCREEN(mi)];
478 int wire = MI_IS_WIREFRAME(mi);
485 sprintf (text, "Loading images...");
487 sprintf (text, "Loading images... (%d%%)",
488 (int) (n * 100 / MI_COUNT(mi)));
490 if (ss->loading_sw == 0) /* only do this once, so that the string doesn't move. */
491 ss->loading_sw = texture_string_width (ss->texfont, text, &ss->loading_sh);
493 scale = ss->loading_sh / (GLfloat) MI_HEIGHT(mi);
495 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
497 glMatrixMode(GL_PROJECTION);
501 glMatrixMode(GL_MODELVIEW);
504 gluOrtho2D(0, MI_WIDTH(mi), 0, MI_HEIGHT(mi));
506 glTranslatef ((MI_WIDTH(mi) - ss->loading_sw) / 2,
507 (MI_HEIGHT(mi) - ss->loading_sh) / 2,
510 glEnable (GL_TEXTURE_2D);
511 glDisable (GL_DEPTH_TEST);
512 print_texture_string (ss->texfont, text);
513 glEnable (GL_DEPTH_TEST);
516 glMatrixMode(GL_PROJECTION);
519 glMatrixMode(GL_MODELVIEW);
522 glXSwapBuffers (MI_DISPLAY (mi), MI_WINDOW(mi));
527 init_carousel (ModeInfo *mi)
529 int screen = MI_SCREEN(mi);
531 int wire = MI_IS_WIREFRAME(mi);
534 if ((sss = (carousel_state *)
535 calloc (MI_NUM_SCREENS(mi), sizeof(carousel_state))) == NULL)
540 if ((ss->glx_context = init_GL(mi)) != NULL) {
541 reshape_carousel (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
546 if (!tilt_str || !*tilt_str)
548 else if (!strcasecmp (tilt_str, "0"))
550 else if (!strcasecmp (tilt_str, "X"))
552 else if (!strcasecmp (tilt_str, "Y"))
554 else if (!strcasecmp (tilt_str, "XY"))
555 tilt_x_p = tilt_y_p = 1;
558 fprintf (stderr, "%s: tilt must be 'X', 'Y', 'XY' or '', not '%s'\n",
564 double spin_speed = speed * 0.2; /* rotation of tube around axis */
565 double spin_accel = speed * 0.1;
566 double wander_speed = speed * 0.001; /* tilting of axis */
568 spin_speed *= 0.9 + frand(0.2);
569 wander_speed *= 0.9 + frand(0.2);
571 ss->rot = make_rotator (spin_speed, spin_speed, spin_speed,
572 spin_accel, wander_speed, True);
574 ss->trackball = gltrackball_init ();
577 glDisable (GL_LIGHTING);
578 glEnable (GL_DEPTH_TEST);
579 glDisable (GL_CULL_FACE);
583 glShadeModel (GL_SMOOTH);
584 glEnable (GL_LINE_SMOOTH);
585 /* This gives us a transparent diagonal slice through each image! */
586 /* glEnable (GL_POLYGON_SMOOTH); */
587 glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
589 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
590 glEnable (GL_ALPHA_TEST);
592 glEnable (GL_POLYGON_OFFSET_FILL);
593 glPolygonOffset (1.0, 1.0);
597 ss->texfont = load_texture_font (MI_DISPLAY(mi), "font");
600 hack_resources (MI_DISPLAY (mi));
603 ss->frames_size = 10;
604 ss->frames = (image_frame **)
605 calloc (1, ss->frames_size * sizeof(*ss->frames));
608 ss->mode_tick = fade_ticks / speed;
610 ss->awaiting_first_images_p = True;
615 draw_frame (ModeInfo *mi, image_frame *frame, time_t now, Bool body_p)
617 carousel_state *ss = &sss[MI_SCREEN(mi)];
618 int wire = MI_IS_WIREFRAME(mi);
620 GLfloat texw = frame->current.geom.width / (GLfloat) frame->current.tw;
621 GLfloat texh = frame->current.geom.height / (GLfloat) frame->current.th;
622 GLfloat texx1 = frame->current.geom.x / (GLfloat) frame->current.tw;
623 GLfloat texy1 = frame->current.geom.y / (GLfloat) frame->current.th;
624 GLfloat texx2 = texx1 + texw;
625 GLfloat texy2 = texy1 + texh;
626 GLfloat aspect = ((GLfloat) frame->current.geom.height /
627 (GLfloat) frame->current.geom.width);
629 glBindTexture (GL_TEXTURE_2D, frame->current.texid);
633 /* Position this image on the wheel.
635 glRotatef (frame->theta, 0, 1, 0);
636 glTranslatef (0, 0, frame->r);
638 /* Scale down the image so that all N frames fit on the wheel
639 without bumping in to each other.
645 case 1: t = -1.0; s = 1.7; break;
646 case 2: t = -0.8; s = 1.6; break;
647 case 3: t = -0.4; s = 1.5; break;
648 case 4: t = -0.2; s = 1.3; break;
649 default: t = 0.0; s = 6.0 / ss->nframes; break;
651 glTranslatef (0, 0, t);
655 /* Center this image on the wheel plane.
657 glTranslatef (-0.5, -(aspect/2), 0);
659 /* Move as per the "zoom in and out" setting.
664 /* Only use the Z component of the rotator for in/out position. */
665 get_position (frame->rot, &x, &y, &z, !ss->button_down_p);
666 glTranslatef (0, 0, z/2);
669 /* Compute the "drop in and out" state.
677 if (!ss->button_down_p &&
678 now >= frame->expires &&
679 ss->loads_in_progress == 0) /* only load one at a time */
680 load_image (mi, frame);
685 if (--frame->mode_tick <= 0) {
686 image swap = frame->current;
687 frame->current = frame->loading;
688 frame->loading = swap;
691 frame->mode_tick = fade_ticks / speed;
695 if (--frame->mode_tick <= 0)
696 frame->mode = NORMAL;
702 /* Now translate for current in/out state.
704 if (frame->mode == OUT || frame->mode == IN)
706 GLfloat t = (frame->mode == OUT
707 ? frame->mode_tick / (fade_ticks / speed)
708 : (((fade_ticks / speed) - frame->mode_tick + 1) /
709 (fade_ticks / speed)));
711 if (frame->from_top_p) t = -t;
712 glTranslatef (0, t, 0);
715 if (body_p) /* Draw the image quad. */
720 glNormal3f (0, 0, 1);
721 glEnable (GL_TEXTURE_2D);
723 glNormal3f (0, 0, 1);
724 glTexCoord2f (texx1, texy2); glVertex3f (0, 0, 0);
725 glTexCoord2f (texx2, texy2); glVertex3f (1, 0, 0);
726 glTexCoord2f (texx2, texy1); glVertex3f (1, aspect, 0);
727 glTexCoord2f (texx1, texy1); glVertex3f (0, aspect, 0);
731 /* Draw a box around it.
734 glColor3f (0.5, 0.5, 0.5);
735 glDisable (GL_TEXTURE_2D);
736 glBegin (GL_LINE_LOOP);
737 glVertex3f (0, 0, 0);
738 glVertex3f (1, 0, 0);
739 glVertex3f (1, aspect, 0);
740 glVertex3f (0, aspect, 0);
744 else /* Draw a title under the image. */
747 GLfloat scale = 0.05;
748 char *title = frame->current.title ? frame->current.title : "(untitled)";
749 sw = texture_string_width (ss->texfont, title, &sh);
751 glTranslatef (0, -scale, 0);
754 glScalef (scale, scale, scale);
756 glTranslatef (((1/scale) - sw) / 2, 0, 0);
761 glEnable (GL_TEXTURE_2D);
762 print_texture_string (ss->texfont, title);
766 glBegin (GL_LINE_LOOP);
767 glVertex3f (0, 0, 0);
768 glVertex3f (sw, 0, 0);
769 glVertex3f (sw, sh, 0);
770 glVertex3f (0, sh, 0);
780 draw_carousel (ModeInfo *mi)
782 carousel_state *ss = &sss[MI_SCREEN(mi)];
785 if (!ss->glx_context)
788 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(ss->glx_context));
790 if (ss->awaiting_first_images_p)
791 if (!load_initial_images (mi))
794 /* Only check the wall clock every 10 frames */
796 if (ss->now == 0 || ss->draw_tick++ > 10)
798 ss->now = time((time_t *) 0);
799 if (ss->last_time == 0) ss->last_time = ss->now;
805 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
810 /* Run the startup "un-shrink" animation.
815 if (--ss->mode_tick <= 0)
818 ss->last_time = time((time_t *) 0);
828 /* Scale as per the startup "un-shrink" animation.
830 if (ss->mode != NORMAL)
832 GLfloat s = (ss->mode == OUT
833 ? ss->mode_tick / (fade_ticks / speed)
834 : (((fade_ticks / speed) - ss->mode_tick + 1) /
835 (fade_ticks / speed)));
839 /* Rotate and tilt as per the user, and the motion modeller.
843 gltrackball_rotate (ss->trackball);
845 /* Tilt the tube up or down by up to 30 degrees */
846 get_position (ss->rot, &x, &y, &z, !ss->button_down_p);
848 glRotatef (15 - (x * 30), 1, 0, 0);
850 glRotatef (7 - (y * 14), 0, 0, 1);
852 /* Only use the Y component of the rotator. */
853 get_rotation (ss->rot, &x, &y, &z, !ss->button_down_p);
854 glRotatef (y * 360, 0, 1, 0);
857 /* First draw each image, then draw the titles. GL insists that you
858 draw back-to-front in order to make alpha blending work properly,
859 so we need to draw all of the 100% opaque images before drawing
860 any of the not-100%-opaque titles.
862 for (i = 0; i < ss->nframes; i++)
863 draw_frame (mi, ss->frames[i], ss->now, True);
865 for (i = 0; i < ss->nframes; i++)
866 draw_frame (mi, ss->frames[i], ss->now, False);
870 if (mi->fps_p) do_fps (mi);
872 glXSwapBuffers (MI_DISPLAY (mi), MI_WINDOW(mi));
875 XSCREENSAVER_MODULE ("Carousel", carousel)