1 /* photopile, Copyright (c) 2008-2012 Jens Kilian <jjk@acm.org>
2 * Based on carousel, Copyright (c) 2005-2008 Jamie Zawinski <jwz@jwz.org>
3 * Loads a sequence of images and shuffles them into a pile.
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation. No representations are made about the suitability of this
10 * software for any purpose. It is provided "as is" without express or
14 #define DEF_FONT "-*-helvetica-bold-r-normal-*-240-*"
15 #define DEFAULTS "*count: 7 \n" \
17 "*wireframe: False \n" \
18 "*showFPS: False \n" \
19 "*fpsSolid: True \n" \
21 "*font: " DEF_FONT "\n" \
22 "*desktopGrabber: xscreensaver-getimage -no-desktop %s\n" \
23 "*grabDesktopImages: False \n" \
24 "*chooseRandomImages: True \n"
26 # define refresh_photopile 0
27 # define release_photopile 0
28 # define photopile_handle_event 0
31 #define countof(x) (sizeof((x))/sizeof((*x)))
34 # include <X11/Intrinsic.h> /* for XrmDatabase in -debug mode */
38 #include "xlockmore.h"
39 #include "grab-ximage.h"
41 #include "dropshadow.h"
45 # define DEF_SCALE "0.4"
46 # define DEF_MAX_TILT "50"
47 # define DEF_SPEED "1.0"
48 # define DEF_DURATION "5"
49 # define DEF_MIPMAP "True"
50 # define DEF_TITLES "False"
51 # define DEF_POLAROID "True"
52 # define DEF_CLIP "True"
53 # define DEF_SHADOWS "True"
54 # define DEF_DEBUG "False"
56 #define BELLRAND(n) ((frand((n)) + frand((n)) + frand((n))) / 3)
59 GLfloat x, y; /* position on screen */
60 GLfloat angle; /* rotation angle */
65 Bool loaded_p; /* true if image can be drawn */
67 char *title; /* the filename of this image */
68 int w, h; /* size in pixels of the image */
69 int tw, th; /* size in pixels of the texture */
70 XRectangle geom; /* where in the image the bits are */
72 position pos[4]; /* control points for calculating position */
74 GLuint texid; /* GL texture ID */
79 typedef enum { EARLY, SHUFFLE, NORMAL, LOADING } fade_mode;
80 static int fade_ticks = 60;
84 GLXContext *glx_context;
86 image *frames; /* pointer to array of images */
87 int nframe; /* image being (resp. next to be) loaded */
90 texture_font_data *texfont;
91 int loading_sw, loading_sh;
93 time_t last_time, now;
100 static photopile_state *sss = NULL;
103 /* Command-line arguments
105 static GLfloat scale; /* Scale factor for loading images. */
106 static GLfloat max_tilt; /* Maximum angle from vertical. */
107 static GLfloat speed; /* Animation speed scale factor. */
108 static int duration; /* Reload images after this long. */
109 static Bool mipmap_p; /* Use mipmaps instead of single textures. */
110 static Bool titles_p; /* Display image titles. */
111 static Bool polaroid_p; /* Use instant-film look for images. */
112 static Bool clip_p; /* Clip images instead of scaling for -polaroid. */
113 static Bool shadows_p; /* Draw drop shadows. */
114 static Bool debug_p; /* Be loud and do weird things. */
117 static XrmOptionDescRec opts[] = {
118 {"-scale", ".scale", XrmoptionSepArg, 0 },
119 {"-maxTilt", ".maxTilt", XrmoptionSepArg, 0 },
120 {"-speed", ".speed", XrmoptionSepArg, 0 },
121 {"-duration", ".duration", XrmoptionSepArg, 0 },
122 {"-mipmaps", ".mipmap", XrmoptionNoArg, "True" },
123 {"-no-mipmaps", ".mipmap", XrmoptionNoArg, "False" },
124 {"-titles", ".titles", XrmoptionNoArg, "True" },
125 {"-no-titles", ".titles", XrmoptionNoArg, "False" },
126 {"-polaroid", ".polaroid", XrmoptionNoArg, "True" },
127 {"-no-polaroid", ".polaroid", XrmoptionNoArg, "False" },
128 {"-clip", ".clip", XrmoptionNoArg, "True" },
129 {"-no-clip", ".clip", XrmoptionNoArg, "False" },
130 {"-shadows", ".shadows", XrmoptionNoArg, "True" },
131 {"-no-shadows", ".shadows", XrmoptionNoArg, "False" },
132 {"-debug", ".debug", XrmoptionNoArg, "True" },
133 {"-font", ".font", XrmoptionSepArg, 0 },
136 static argtype vars[] = {
137 { &scale, "scale", "Scale", DEF_SCALE, t_Float},
138 { &max_tilt, "maxTilt", "MaxTilt", DEF_MAX_TILT, t_Float},
139 { &speed, "speed", "Speed", DEF_SPEED, t_Float},
140 { &duration, "duration", "Duration", DEF_DURATION, t_Int},
141 { &mipmap_p, "mipmap", "Mipmap", DEF_MIPMAP, t_Bool},
142 { &titles_p, "titles", "Titles", DEF_TITLES, t_Bool},
143 { &polaroid_p, "polaroid", "Polaroid", DEF_POLAROID, t_Bool},
144 { &clip_p, "clip", "Clip", DEF_CLIP, t_Bool},
145 { &shadows_p, "shadows", "Shadows", DEF_SHADOWS, t_Bool},
146 { &debug_p, "debug", "Debug", DEF_DEBUG, t_Bool},
149 ENTRYPOINT ModeSpecOpt photopile_opts = {countof(opts), opts, countof(vars), vars, NULL};
152 /* Functions to interpolate between image positions.
155 add_pos(position p, position q)
164 scale_pos(GLfloat t, position p)
173 linear_combination(GLfloat t, position p, position q)
175 return add_pos(scale_pos(1.0 - t, p), scale_pos(t, q));
179 interpolate(GLfloat t, position p[4])
181 /* de Casteljau's algorithm, 4 control points */
182 position p10 = linear_combination(t, p[0], p[1]);
183 position p11 = linear_combination(t, p[1], p[2]);
184 position p12 = linear_combination(t, p[2], p[3]);
186 position p20 = linear_combination(t, p10, p11);
187 position p21 = linear_combination(t, p11, p12);
189 return linear_combination(t, p20, p21);
193 offset_pos(position p, GLfloat th, GLfloat r)
197 p.angle = (frand(2.0) - 1.0) * max_tilt;
201 /* Calculate new positions for all images.
204 set_new_positions(photopile_state *ss)
206 ModeInfo *mi = ss->mi;
209 for (i = 0; i < MI_COUNT(mi)+1; ++i)
211 image *frame = ss->frames + i;
212 GLfloat w = frame->w;
213 GLfloat h = frame->h;
214 GLfloat d = sqrt(w*w + h*h);
215 GLfloat leave = frand(M_PI * 2.0);
216 GLfloat enter = frand(M_PI * 2.0);
219 frame->pos[0] = frame->pos[3];
222 frame->pos[3].x = BELLRAND(MI_WIDTH(mi));
223 frame->pos[3].y = BELLRAND(MI_HEIGHT(mi));
224 frame->pos[3].angle = (frand(2.0) - 1.0) * max_tilt;
226 /* Try to keep the images mostly inside the screen bounds */
227 frame->pos[3].x = MAX(0.5*w, MIN(MI_WIDTH(mi)-0.5*w, frame->pos[3].x));
228 frame->pos[3].y = MAX(0.5*h, MIN(MI_HEIGHT(mi)-0.5*h, frame->pos[3].y));
230 /* intermediate points */
231 frame->pos[1] = offset_pos(frame->pos[0], leave, d * (0.5 + frand(1.0)));
232 frame->pos[2] = offset_pos(frame->pos[3], enter, d * (0.5 + frand(1.0)));
236 /* Callback that tells us that the texture has been loaded.
239 image_loaded_cb (const char *filename, XRectangle *geom,
240 int image_width, int image_height,
241 int texture_width, int texture_height,
244 photopile_state *ss = (photopile_state *) closure;
245 ModeInfo *mi = ss->mi;
246 int wire = MI_IS_WIREFRAME(mi);
247 image *frame = ss->frames + ss->nframe;
253 frame->w = (int)(MI_WIDTH(mi) * scale) - 1;
254 frame->h = (int)(MI_HEIGHT(mi) * scale) - 1;
258 frame->w = (int)(MI_HEIGHT(mi) * scale) - 1;
259 frame->h = (int)(MI_WIDTH(mi) * scale) - 1;
261 if (frame->w <= 10) frame->w = 10;
262 if (frame->h <= 10) frame->h = 10;
263 frame->geom.width = frame->w;
264 frame->geom.height = frame->h;
268 if (image_width == 0 || image_height == 0)
271 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
272 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
273 mipmap_p ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
275 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
276 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
278 frame->w = image_width;
279 frame->h = image_height;
280 frame->tw = texture_width;
281 frame->th = texture_height;
286 frame->title = (filename ? strdup (filename) : 0);
288 /* xscreensaver-getimage returns paths relative to the image directory
289 now, so leave the sub-directory part in. Unless it's an absolute path.
291 if (frame->title && frame->title[0] == '/')
293 /* strip filename to part after last /. */
294 char *s = strrchr (frame->title, '/');
295 if (s) strcpy (frame->title, s+1);
299 fprintf (stderr, "%s: loaded %4d x %-4d %4d x %-4d \"%s\"\n",
303 frame->tw, frame->th,
304 (frame->title ? frame->title : "(null)"));
307 frame->loaded_p = True;
314 load_image (ModeInfo *mi)
316 photopile_state *ss = &sss[MI_SCREEN(mi)];
317 int wire = MI_IS_WIREFRAME(mi);
318 image *frame = ss->frames + ss->nframe;
320 if (debug_p && !wire && frame->w != 0)
321 fprintf (stderr, "%s: dropped %4d x %-4d %4d x %-4d \"%s\"\n",
325 frame->tw, frame->th,
326 (frame->title ? frame->title : "(null)"));
328 frame->loaded_p = False;
331 image_loaded_cb (0, 0, 0, 0, 0, 0, ss);
334 int w = MI_WIDTH(mi);
335 int h = MI_HEIGHT(mi);
336 int size = (int)((w > h ? w : h) * scale);
337 if (size <= 10) size = 10;
338 load_texture_async (mi->xgwa.screen, mi->window, *ss->glx_context,
340 mipmap_p, frame->texid,
341 image_loaded_cb, ss);
347 loading_msg (ModeInfo *mi)
349 photopile_state *ss = &sss[MI_SCREEN(mi)];
350 int wire = MI_IS_WIREFRAME(mi);
351 const char text[] = "Loading...";
355 if (ss->loading_sw == 0) /* only do this once */
356 ss->loading_sw = texture_string_width (ss->texfont, text, &ss->loading_sh);
358 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
360 glMatrixMode(GL_PROJECTION);
364 glMatrixMode(GL_MODELVIEW);
367 glOrtho(0, MI_WIDTH(mi), 0, MI_HEIGHT(mi), -1, 1);
369 glTranslatef ((MI_WIDTH(mi) - ss->loading_sw) / 2,
370 (MI_HEIGHT(mi) - ss->loading_sh) / 2,
373 glEnable (GL_TEXTURE_2D);
374 glDisable (GL_DEPTH_TEST);
375 print_texture_string (ss->texfont, text);
376 glEnable (GL_DEPTH_TEST);
379 glMatrixMode(GL_PROJECTION);
382 glMatrixMode(GL_MODELVIEW);
385 glXSwapBuffers (MI_DISPLAY (mi), MI_WINDOW(mi));
390 loading_initial_image (ModeInfo *mi)
392 photopile_state *ss = &sss[MI_SCREEN(mi)];
394 if (ss->frames[ss->nframe].loaded_p)
396 /* The initial image has been fully loaded, start fading it in. */
399 for (i = 0; i < ss->nframe; ++i)
401 ss->frames[i].pos[3].x = MI_WIDTH(mi) * 0.5;
402 ss->frames[i].pos[3].y = MI_HEIGHT(mi) * 0.5;
403 ss->frames[i].pos[3].angle = 0.0;
405 set_new_positions(ss);
408 ss->mode_tick = fade_ticks / speed;
415 return (ss->mode == EARLY);
420 reshape_photopile (ModeInfo *mi, int width, int height)
422 glViewport (0, 0, (GLint) width, (GLint) height);
424 glMatrixMode(GL_PROJECTION);
427 glMatrixMode(GL_MODELVIEW);
429 glOrtho(0, MI_WIDTH(mi), 0, MI_HEIGHT(mi), -1, 1);
431 glClear(GL_COLOR_BUFFER_BIT);
435 /* Kludge to add "-v" to invocation of "xscreensaver-getimage" in -debug mode
438 hack_resources (Display *dpy)
441 char *res = "desktopGrabber";
442 char *val = get_string_resource (dpy, res, "DesktopGrabber");
446 XrmDatabase db = XtDatabase (dpy);
447 sprintf (buf1, "%.100s.%.100s", progname, res);
448 sprintf (buf2, "%.200s -v", val);
450 value.size = strlen(buf2);
451 XrmPutResource (&db, buf1, "String", &value);
452 # endif /* !HAVE_COCOA */
457 init_photopile (ModeInfo *mi)
459 int screen = MI_SCREEN(mi);
461 int wire = MI_IS_WIREFRAME(mi);
464 if ((sss = (photopile_state *)
465 calloc (MI_NUM_SCREENS(mi), sizeof(photopile_state))) == NULL)
471 if ((ss->glx_context = init_GL(mi)) != NULL) {
472 reshape_photopile (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
473 clear_gl_error(); /* WTF? sometimes "invalid op" from glViewport! */
478 ss->shadow = init_drop_shadow();
479 ss->texfont = load_texture_font (MI_DISPLAY(mi), "font");
482 hack_resources (MI_DISPLAY (mi));
484 ss->frames = (image *)calloc (MI_COUNT(mi) + 1, sizeof(image));
489 for (i = 0; i < MI_COUNT(mi) + 1; ++i)
491 glGenTextures (1, &(ss->frames[i].texid));
492 if (ss->frames[i].texid <= 0) abort();
497 load_image(mi); /* start loading the first image */
502 draw_image (ModeInfo *mi, int i, GLfloat t, GLfloat s, GLfloat z)
504 int wire = MI_IS_WIREFRAME(mi);
505 photopile_state *ss = &sss[MI_SCREEN(mi)];
506 image *frame = ss->frames + i;
508 position pos = interpolate(t, frame->pos);
509 GLfloat w = frame->geom.width * 0.5;
510 GLfloat h = frame->geom.height * 0.5;
511 GLfloat z1 = z - 0.25 / (MI_COUNT(mi) + 1);
512 GLfloat z2 = z - 0.5 / (MI_COUNT(mi) + 1);
519 GLfloat minSize = MIN(w, h);
520 GLfloat maxSize = MAX(w, h);
522 /* Clip or scale image to fit in the frame.
530 GLfloat scale = minSize / maxSize;
535 w1 = minSize * 1.16; /* enlarge frame border */
538 s /= 1.5; /* compensate for border size */
543 /* Position and scale this image.
545 glTranslatef (pos.x, pos.y, 0);
546 glRotatef (pos.angle, 0, 0, 1);
549 /* Draw the drop shadow. */
550 if (shadows_p && !wire)
553 draw_drop_shadow(ss->shadow, -w1, -h1, z2, 2.0 * w1, h1 + h2, 20.0);
554 glDisable (GL_BLEND);
557 glDisable (GL_LIGHTING);
558 glEnable (GL_DEPTH_TEST);
559 glDisable (GL_CULL_FACE);
561 /* Draw the retro instant-film frame.
567 glShadeModel (GL_SMOOTH);
568 glEnable (GL_LINE_SMOOTH);
569 glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
573 glVertex3f (-w1, -h1, z2);
574 glVertex3f ( w1, -h1, z2);
575 glVertex3f ( w1, h2, z2);
576 glVertex3f (-w1, h2, z2);
581 glColor3f (0.5, 0.5, 0.5);
582 glBegin (GL_LINE_LOOP);
583 glVertex3f (-w1, -h1, z);
584 glVertex3f ( w1, -h1, z);
585 glVertex3f ( w1, h2, z);
586 glVertex3f (-w1, h2, z);
590 /* Draw the image quad.
594 GLfloat texw = w / frame->tw;
595 GLfloat texh = h / frame->th;
596 GLfloat texx = (frame->geom.x + 0.5 * frame->geom.width) / frame->tw;
597 GLfloat texy = (frame->geom.y + 0.5 * frame->geom.height) / frame->th;
599 glBindTexture (GL_TEXTURE_2D, frame->texid);
600 glEnable (GL_TEXTURE_2D);
603 glTexCoord2f (texx - texw, texy + texh); glVertex3f (-w, -h, z1);
604 glTexCoord2f (texx + texw, texy + texh); glVertex3f ( w, -h, z1);
605 glTexCoord2f (texx + texw, texy - texh); glVertex3f ( w, h, z1);
606 glTexCoord2f (texx - texw, texy - texh); glVertex3f (-w, h, z1);
608 glDisable (GL_TEXTURE_2D);
611 /* Draw a box around it.
615 glShadeModel (GL_SMOOTH);
616 glEnable (GL_LINE_SMOOTH);
617 glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
620 glColor3f (0.5, 0.5, 0.5);
621 glBegin (GL_LINE_LOOP);
622 glVertex3f (-w, -h, z);
623 glVertex3f ( w, -h, z);
624 glVertex3f ( w, h, z);
625 glVertex3f (-w, h, z);
628 /* Draw a title under the image.
634 const char *title = frame->title ? frame->title : "(untitled)";
636 /* #### Highly approximate, but doing real clipping is harder... */
638 if (strlen(title) > max)
639 title += strlen(title) - max;
641 sw = texture_string_width (ss->texfont, title, &sh);
643 glTranslatef (-sw*scale*0.5, -h - sh*scale, z);
644 glScalef (scale, scale, 1);
646 if (wire || !polaroid_p)
657 glEnable (GL_TEXTURE_2D);
659 print_texture_string (ss->texfont, title);
663 glBegin (GL_LINE_LOOP);
664 glVertex3f (0, 0, 0);
665 glVertex3f (sw, 0, 0);
666 glVertex3f (sw, sh, 0);
667 glVertex3f (0, sh, 0);
677 draw_photopile (ModeInfo *mi)
679 photopile_state *ss = &sss[MI_SCREEN(mi)];
682 if (!ss->glx_context)
685 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(ss->glx_context));
687 if (ss->mode == EARLY)
688 if (loading_initial_image (mi))
691 /* Only check the wall clock every 10 frames */
692 if (ss->now == 0 || ss->draw_tick++ > 10)
694 ss->now = time((time_t *) 0);
695 if (ss->last_time == 0) ss->last_time = ss->now;
699 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
704 glTranslatef (MI_WIDTH(mi)/2, MI_HEIGHT(mi)/2, 0);
705 glRotatef(current_device_rotation(), 0, 0, 1);
706 glTranslatef (-MI_WIDTH(mi)/2, -MI_HEIGHT(mi)/2, 0);
708 /* Handle state transitions. */
712 if (--ss->mode_tick <= 0)
714 ss->nframe = (ss->nframe+1) % (MI_COUNT(mi)+1);
717 ss->last_time = time((time_t *) 0);
721 if (ss->now - ss->last_time > duration)
728 if (ss->frames[ss->nframe].loaded_p)
730 set_new_positions(ss);
732 ss->mode_tick = fade_ticks / speed;
739 t = 1.0 - ss->mode_tick / (fade_ticks / speed);
740 t = 0.5 * (1.0 - cos(M_PI * t));
742 /* Draw the images. */
743 for (i = 0; i < MI_COUNT(mi) + (ss->mode == SHUFFLE); ++i)
745 int j = (ss->nframe + i + 1) % (MI_COUNT(mi) + 1);
747 if (ss->frames[j].loaded_p)
750 GLfloat z = (GLfloat)i / (MI_COUNT(mi) + 1);
755 if (i == MI_COUNT(mi))
772 draw_image(mi, j, t, s, z);
778 if (mi->fps_p) do_fps (mi);
780 glXSwapBuffers (MI_DISPLAY (mi), MI_WINDOW(mi));
783 XSCREENSAVER_MODULE ("Photopile", photopile)