1 /* photopile, Copyright (c) 2008-2015 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 #if defined(HAVE_COCOA) || defined(HAVE_ANDROID)
15 # define DEF_FONT "OCR A Std 48, Lucida Console 48, Monaco 48"
17 # define DEF_FONT "-*-helvetica-bold-r-normal-*-*-480-*-*-*-*-*-*"
20 #define DEFAULTS "*count: 7 \n" \
22 "*wireframe: False \n" \
23 "*showFPS: False \n" \
24 "*fpsSolid: True \n" \
26 "*font: " DEF_FONT "\n" \
27 "*desktopGrabber: xscreensaver-getimage -no-desktop %s\n" \
28 "*grabDesktopImages: False \n" \
29 "*chooseRandomImages: True \n" \
30 "*suppressRotationAnimation: True\n" \
32 # define refresh_photopile 0
33 # define release_photopile 0
34 # define photopile_handle_event 0
37 #define countof(x) (sizeof((x))/sizeof((*x)))
40 # include <X11/Intrinsic.h> /* for XrmDatabase in -debug mode */
44 #include "xlockmore.h"
45 #include "grab-ximage.h"
47 #include "dropshadow.h"
51 # define DEF_SCALE "0.4"
52 # define DEF_MAX_TILT "50"
53 # define DEF_SPEED "1.0"
54 # define DEF_DURATION "5"
55 # define DEF_MIPMAP "True"
56 # define DEF_TITLES "True"
57 # define DEF_POLAROID "True"
58 # define DEF_CLIP "True"
59 # define DEF_SHADOWS "True"
60 # define DEF_DEBUG "False"
62 #define BELLRAND(n) ((frand((n)) + frand((n)) + frand((n))) / 3)
65 GLfloat x, y; /* position on screen */
66 GLfloat angle; /* rotation angle */
71 Bool loaded_p; /* true if image can be drawn */
73 char *title; /* the filename of this image */
74 int w, h; /* size in pixels of the image */
75 int tw, th; /* size in pixels of the texture */
76 XRectangle geom; /* where in the image the bits are */
78 position pos[4]; /* control points for calculating position */
80 GLuint texid; /* GL texture ID */
85 typedef enum { EARLY, SHUFFLE, NORMAL, LOADING } fade_mode;
86 static int fade_ticks = 60;
90 GLXContext *glx_context;
92 image *frames; /* pointer to array of images */
93 int nframe; /* image being (resp. next to be) loaded */
96 texture_font_data *texfont;
97 int loading_sw, loading_sh;
99 time_t last_time, now;
106 static photopile_state *sss = NULL;
109 /* Command-line arguments
111 static GLfloat scale; /* Scale factor for loading images. */
112 static GLfloat max_tilt; /* Maximum angle from vertical. */
113 static GLfloat speed; /* Animation speed scale factor. */
114 static int duration; /* Reload images after this long. */
115 static Bool mipmap_p; /* Use mipmaps instead of single textures. */
116 static Bool titles_p; /* Display image titles. */
117 static Bool polaroid_p; /* Use instant-film look for images. */
118 static Bool clip_p; /* Clip images instead of scaling for -polaroid. */
119 static Bool shadows_p; /* Draw drop shadows. */
120 static Bool debug_p; /* Be loud and do weird things. */
123 static XrmOptionDescRec opts[] = {
124 {"-scale", ".scale", XrmoptionSepArg, 0 },
125 {"-maxTilt", ".maxTilt", XrmoptionSepArg, 0 },
126 {"-speed", ".speed", XrmoptionSepArg, 0 },
127 {"-duration", ".duration", XrmoptionSepArg, 0 },
128 {"-mipmaps", ".mipmap", XrmoptionNoArg, "True" },
129 {"-no-mipmaps", ".mipmap", XrmoptionNoArg, "False" },
130 {"-titles", ".titles", XrmoptionNoArg, "True" },
131 {"-no-titles", ".titles", XrmoptionNoArg, "False" },
132 {"-polaroid", ".polaroid", XrmoptionNoArg, "True" },
133 {"-no-polaroid", ".polaroid", XrmoptionNoArg, "False" },
134 {"-clip", ".clip", XrmoptionNoArg, "True" },
135 {"-no-clip", ".clip", XrmoptionNoArg, "False" },
136 {"-shadows", ".shadows", XrmoptionNoArg, "True" },
137 {"-no-shadows", ".shadows", XrmoptionNoArg, "False" },
138 {"-debug", ".debug", XrmoptionNoArg, "True" },
139 {"-font", ".font", XrmoptionSepArg, 0 },
142 static argtype vars[] = {
143 { &scale, "scale", "Scale", DEF_SCALE, t_Float},
144 { &max_tilt, "maxTilt", "MaxTilt", DEF_MAX_TILT, t_Float},
145 { &speed, "speed", "Speed", DEF_SPEED, t_Float},
146 { &duration, "duration", "Duration", DEF_DURATION, t_Int},
147 { &mipmap_p, "mipmap", "Mipmap", DEF_MIPMAP, t_Bool},
148 { &titles_p, "titles", "Titles", DEF_TITLES, t_Bool},
149 { &polaroid_p, "polaroid", "Polaroid", DEF_POLAROID, t_Bool},
150 { &clip_p, "clip", "Clip", DEF_CLIP, t_Bool},
151 { &shadows_p, "shadows", "Shadows", DEF_SHADOWS, t_Bool},
152 { &debug_p, "debug", "Debug", DEF_DEBUG, t_Bool},
155 ENTRYPOINT ModeSpecOpt photopile_opts = {countof(opts), opts, countof(vars), vars, NULL};
158 /* Functions to interpolate between image positions.
161 add_pos(position p, position q)
170 scale_pos(GLfloat t, position p)
179 linear_combination(GLfloat t, position p, position q)
181 return add_pos(scale_pos(1.0 - t, p), scale_pos(t, q));
185 interpolate(GLfloat t, position p[4])
187 /* de Casteljau's algorithm, 4 control points */
188 position p10 = linear_combination(t, p[0], p[1]);
189 position p11 = linear_combination(t, p[1], p[2]);
190 position p12 = linear_combination(t, p[2], p[3]);
192 position p20 = linear_combination(t, p10, p11);
193 position p21 = linear_combination(t, p11, p12);
195 return linear_combination(t, p20, p21);
199 offset_pos(position p, GLfloat th, GLfloat r)
203 p.angle = (frand(2.0) - 1.0) * max_tilt;
207 /* Calculate new positions for all images.
210 set_new_positions(photopile_state *ss)
212 ModeInfo *mi = ss->mi;
215 for (i = 0; i < MI_COUNT(mi)+1; ++i)
217 image *frame = ss->frames + i;
218 GLfloat w = frame->w;
219 GLfloat h = frame->h;
220 GLfloat d = sqrt(w*w + h*h);
221 GLfloat leave = frand(M_PI * 2.0);
222 GLfloat enter = frand(M_PI * 2.0);
225 frame->pos[0] = frame->pos[3];
228 frame->pos[3].x = BELLRAND(MI_WIDTH(mi));
229 frame->pos[3].y = BELLRAND(MI_HEIGHT(mi));
230 frame->pos[3].angle = (frand(2.0) - 1.0) * max_tilt;
232 /* Try to keep the images mostly inside the screen bounds */
233 frame->pos[3].x = MAX(0.5*w, MIN(MI_WIDTH(mi)-0.5*w, frame->pos[3].x));
234 frame->pos[3].y = MAX(0.5*h, MIN(MI_HEIGHT(mi)-0.5*h, frame->pos[3].y));
236 /* intermediate points */
237 frame->pos[1] = offset_pos(frame->pos[0], leave, d * (0.5 + frand(1.0)));
238 frame->pos[2] = offset_pos(frame->pos[3], enter, d * (0.5 + frand(1.0)));
242 /* Callback that tells us that the texture has been loaded.
245 image_loaded_cb (const char *filename, XRectangle *geom,
246 int image_width, int image_height,
247 int texture_width, int texture_height,
250 photopile_state *ss = (photopile_state *) closure;
251 ModeInfo *mi = ss->mi;
252 int wire = MI_IS_WIREFRAME(mi);
253 image *frame = ss->frames + ss->nframe;
259 frame->w = (int)(MI_WIDTH(mi) * scale) - 1;
260 frame->h = (int)(MI_HEIGHT(mi) * scale) - 1;
264 frame->w = (int)(MI_HEIGHT(mi) * scale) - 1;
265 frame->h = (int)(MI_WIDTH(mi) * scale) - 1;
267 if (frame->w <= 10) frame->w = 10;
268 if (frame->h <= 10) frame->h = 10;
269 frame->geom.width = frame->w;
270 frame->geom.height = frame->h;
274 if (image_width == 0 || image_height == 0)
277 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
278 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
279 mipmap_p ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
281 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
282 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
284 frame->w = image_width;
285 frame->h = image_height;
286 frame->tw = texture_width;
287 frame->th = texture_height;
292 frame->title = (filename ? strdup (filename) : 0);
294 /* xscreensaver-getimage returns paths relative to the image directory
295 now, so leave the sub-directory part in. Unless it's an absolute path.
297 if (frame->title && frame->title[0] == '/')
299 /* strip filename to part after last /. */
300 char *s = strrchr (frame->title, '/');
301 if (s) strcpy (frame->title, s+1);
305 fprintf (stderr, "%s: loaded %4d x %-4d %4d x %-4d \"%s\"\n",
309 frame->tw, frame->th,
310 (frame->title ? frame->title : "(null)"));
313 frame->loaded_p = True;
320 load_image (ModeInfo *mi)
322 photopile_state *ss = &sss[MI_SCREEN(mi)];
323 int wire = MI_IS_WIREFRAME(mi);
324 image *frame = ss->frames + ss->nframe;
326 if (debug_p && !wire && frame->w != 0)
327 fprintf (stderr, "%s: dropped %4d x %-4d %4d x %-4d \"%s\"\n",
331 frame->tw, frame->th,
332 (frame->title ? frame->title : "(null)"));
334 frame->loaded_p = False;
337 image_loaded_cb (0, 0, 0, 0, 0, 0, ss);
340 int w = MI_WIDTH(mi);
341 int h = MI_HEIGHT(mi);
342 int size = (int)((w > h ? w : h) * scale);
343 if (size <= 10) size = 10;
344 load_texture_async (mi->xgwa.screen, mi->window, *ss->glx_context,
346 mipmap_p, frame->texid,
347 image_loaded_cb, ss);
353 loading_msg (ModeInfo *mi)
355 photopile_state *ss = &sss[MI_SCREEN(mi)];
356 int wire = MI_IS_WIREFRAME(mi);
357 const char text[] = "Loading...";
361 if (ss->loading_sw == 0) /* only do this once */
364 texture_string_metrics (ss->texfont, text, &e, 0, 0);
365 ss->loading_sw = e.width;
366 ss->loading_sh = e.ascent + e.descent;
369 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
371 glMatrixMode(GL_PROJECTION);
375 glMatrixMode(GL_MODELVIEW);
378 glOrtho(0, MI_WIDTH(mi), 0, MI_HEIGHT(mi), -1, 1);
380 glTranslatef ((MI_WIDTH(mi) - ss->loading_sw) / 2,
381 (MI_HEIGHT(mi) - ss->loading_sh) / 2,
384 glEnable (GL_TEXTURE_2D);
385 glDisable (GL_DEPTH_TEST);
386 print_texture_string (ss->texfont, text);
387 glEnable (GL_DEPTH_TEST);
390 glMatrixMode(GL_PROJECTION);
393 glMatrixMode(GL_MODELVIEW);
396 glXSwapBuffers (MI_DISPLAY (mi), MI_WINDOW(mi));
401 loading_initial_image (ModeInfo *mi)
403 photopile_state *ss = &sss[MI_SCREEN(mi)];
405 if (ss->frames[ss->nframe].loaded_p)
407 /* The initial image has been fully loaded, start fading it in. */
410 for (i = 0; i < ss->nframe; ++i)
412 ss->frames[i].pos[3].x = MI_WIDTH(mi) * 0.5;
413 ss->frames[i].pos[3].y = MI_HEIGHT(mi) * 0.5;
414 ss->frames[i].pos[3].angle = 0.0;
416 set_new_positions(ss);
419 ss->mode_tick = fade_ticks / speed;
426 return (ss->mode == EARLY);
431 reshape_photopile (ModeInfo *mi, int width, int height)
433 glViewport (0, 0, (GLint) width, (GLint) height);
435 glMatrixMode(GL_PROJECTION);
438 glMatrixMode(GL_MODELVIEW);
440 glOrtho(0, MI_WIDTH(mi), 0, MI_HEIGHT(mi), -1, 1);
442 # ifdef HAVE_MOBILE /* Keep it the same relative size when rotated. */
444 GLfloat h = MI_HEIGHT(mi) / (GLfloat) MI_WIDTH(mi);
445 int o = (int) current_device_rotation();
446 if (o != 0 && o != 180 && o != -180)
447 glScalef (1/h, h, 1);
451 glClear(GL_COLOR_BUFFER_BIT);
455 /* Kludge to add "-v" to invocation of "xscreensaver-getimage" in -debug mode
458 hack_resources (Display *dpy)
461 char *res = "desktopGrabber";
462 char *val = get_string_resource (dpy, res, "DesktopGrabber");
466 XrmDatabase db = XtDatabase (dpy);
467 sprintf (buf1, "%.100s.%.100s", progname, res);
468 sprintf (buf2, "%.200s -v", val);
470 value.size = strlen(buf2);
471 XrmPutResource (&db, buf1, "String", &value);
472 # endif /* !HAVE_JWXYZ */
477 init_photopile (ModeInfo *mi)
479 int screen = MI_SCREEN(mi);
481 int wire = MI_IS_WIREFRAME(mi);
484 if ((sss = (photopile_state *)
485 calloc (MI_NUM_SCREENS(mi), sizeof(photopile_state))) == NULL)
491 if ((ss->glx_context = init_GL(mi)) != NULL) {
492 reshape_photopile (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
493 clear_gl_error(); /* WTF? sometimes "invalid op" from glViewport! */
498 ss->shadow = init_drop_shadow();
499 ss->texfont = load_texture_font (MI_DISPLAY(mi), "font");
502 hack_resources (MI_DISPLAY (mi));
504 ss->frames = (image *)calloc (MI_COUNT(mi) + 1, sizeof(image));
509 for (i = 0; i < MI_COUNT(mi) + 1; ++i)
511 glGenTextures (1, &(ss->frames[i].texid));
512 if (ss->frames[i].texid <= 0) abort();
517 load_image(mi); /* start loading the first image */
522 draw_image (ModeInfo *mi, int i, GLfloat t, GLfloat s, GLfloat z)
524 int wire = MI_IS_WIREFRAME(mi);
525 photopile_state *ss = &sss[MI_SCREEN(mi)];
526 image *frame = ss->frames + i;
528 position pos = interpolate(t, frame->pos);
529 GLfloat w = frame->geom.width * 0.5;
530 GLfloat h = frame->geom.height * 0.5;
531 GLfloat z1 = z - 0.25 / (MI_COUNT(mi) + 1);
532 GLfloat z2 = z - 0.5 / (MI_COUNT(mi) + 1);
539 GLfloat minSize = MIN(w, h);
540 GLfloat maxSize = MAX(w, h);
542 /* Clip or scale image to fit in the frame.
550 GLfloat scale = minSize / maxSize;
555 w1 = minSize * 1.16; /* enlarge frame border */
558 s /= 1.5; /* compensate for border size */
563 /* Position and scale this image.
565 glTranslatef (pos.x, pos.y, 0);
566 glRotatef (pos.angle, 0, 0, 1);
569 /* Draw the drop shadow. */
570 if (shadows_p && !wire)
573 draw_drop_shadow(ss->shadow, -w1, -h1, z2, 2.0 * w1, h1 + h2, 20.0);
574 glDisable (GL_BLEND);
577 glDisable (GL_LIGHTING);
578 glEnable (GL_DEPTH_TEST);
579 glDisable (GL_CULL_FACE);
581 /* Draw the retro instant-film frame.
587 glShadeModel (GL_SMOOTH);
588 glEnable (GL_LINE_SMOOTH);
589 glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
593 glVertex3f (-w1, -h1, z2);
594 glVertex3f ( w1, -h1, z2);
595 glVertex3f ( w1, h2, z2);
596 glVertex3f (-w1, h2, z2);
601 glColor3f (0.5, 0.5, 0.5);
602 glBegin (GL_LINE_LOOP);
603 glVertex3f (-w1, -h1, z);
604 glVertex3f ( w1, -h1, z);
605 glVertex3f ( w1, h2, z);
606 glVertex3f (-w1, h2, z);
610 /* Draw the image quad.
614 GLfloat texw = w / frame->tw;
615 GLfloat texh = h / frame->th;
616 GLfloat texx = (frame->geom.x + 0.5 * frame->geom.width) / frame->tw;
617 GLfloat texy = (frame->geom.y + 0.5 * frame->geom.height) / frame->th;
619 glBindTexture (GL_TEXTURE_2D, frame->texid);
620 glEnable (GL_TEXTURE_2D);
623 glTexCoord2f (texx - texw, texy + texh); glVertex3f (-w, -h, z1);
624 glTexCoord2f (texx + texw, texy + texh); glVertex3f ( w, -h, z1);
625 glTexCoord2f (texx + texw, texy - texh); glVertex3f ( w, h, z1);
626 glTexCoord2f (texx - texw, texy - texh); glVertex3f (-w, h, z1);
628 glDisable (GL_TEXTURE_2D);
631 /* Draw a box around it.
635 glShadeModel (GL_SMOOTH);
636 glEnable (GL_LINE_SMOOTH);
637 glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
640 glColor3f (0.5, 0.5, 0.5);
641 glBegin (GL_LINE_LOOP);
642 glVertex3f (-w, -h, z);
643 glVertex3f ( w, -h, z);
644 glVertex3f ( w, h, z);
645 glVertex3f (-w, h, z);
648 /* Draw a title under the image.
657 const char *title = frame->title ? frame->title : "(untitled)";
660 texture_string_metrics (ss->texfont, title, &e, &ascent, &descent);
662 sh = ascent; /* + descent; */
664 /* Scale the text to match the pixel size of the photo */
667 # if defined(HAVE_COCOA)
671 # if defined(HAVE_MOBILE)
675 /* Clip characters off the left end of the string until it fits. */
676 if (clip_p || polaroid_p)
677 while (sw * scale > tw && strlen (title) > 10)
680 texture_string_metrics (ss->texfont, title, &e, &ascent, &descent);
684 if (th <= 0) /* Non-polaroid */
687 glTranslatef (-w, -h1, 0);
688 glTranslatef ((tw - sw*scale) / 2, (th - sh*scale) / 2, 0);
690 glScalef (scale, scale, 1);
692 if (wire || !polaroid_p)
698 glColor3f (0.5, 0.5, 0.5);
703 glEnable (GL_TEXTURE_2D);
705 glDisable (GL_DEPTH_TEST);
706 print_texture_string (ss->texfont, title);
707 glEnable (GL_DEPTH_TEST);
711 glBegin (GL_LINE_LOOP);
712 glVertex3f (0, 0, 0);
713 glVertex3f (sw, 0, 0);
714 glVertex3f (sw, sh, 0);
715 glVertex3f (0, sh, 0);
726 draw_photopile (ModeInfo *mi)
728 photopile_state *ss = &sss[MI_SCREEN(mi)];
731 if (!ss->glx_context)
734 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(ss->glx_context));
736 if (ss->mode == EARLY)
737 if (loading_initial_image (mi))
740 /* Only check the wall clock every 10 frames */
741 if (ss->now == 0 || ss->draw_tick++ > 10)
743 ss->now = time((time_t *) 0);
744 if (ss->last_time == 0) ss->last_time = ss->now;
748 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
753 glTranslatef (MI_WIDTH(mi)/2, MI_HEIGHT(mi)/2, 0);
754 glRotatef(current_device_rotation(), 0, 0, 1);
755 glTranslatef (-MI_WIDTH(mi)/2, -MI_HEIGHT(mi)/2, 0);
757 /* Handle state transitions. */
761 if (--ss->mode_tick <= 0)
763 ss->nframe = (ss->nframe+1) % (MI_COUNT(mi)+1);
766 ss->last_time = time((time_t *) 0);
770 if (ss->now - ss->last_time > duration)
777 if (ss->frames[ss->nframe].loaded_p)
779 set_new_positions(ss);
781 ss->mode_tick = fade_ticks / speed;
788 t = 1.0 - ss->mode_tick / (fade_ticks / speed);
789 t = 0.5 * (1.0 - cos(M_PI * t));
791 /* Draw the images. */
792 for (i = 0; i < MI_COUNT(mi) + (ss->mode == SHUFFLE); ++i)
794 int j = (ss->nframe + i + 1) % (MI_COUNT(mi) + 1);
796 if (ss->frames[j].loaded_p)
799 GLfloat z = (GLfloat)i / (MI_COUNT(mi) + 1);
804 if (i == MI_COUNT(mi))
821 draw_image(mi, j, t, s, z);
827 if (mi->fps_p) do_fps (mi);
829 glXSwapBuffers (MI_DISPLAY (mi), MI_WINDOW(mi));
832 XSCREENSAVER_MODULE ("Photopile", photopile)