1 /* photopile, Copyright (c) 2008-2009 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, IN, NORMAL, LOADING, SHUFFLE } 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. */static Bool shadows_p; /* Draw drop shadows. */
113 static Bool debug_p; /* Be loud and do weird things. */
116 static XrmOptionDescRec opts[] = {
117 {"-scale", ".scale", XrmoptionSepArg, 0 },
118 {"-maxTilt", ".maxTilt", XrmoptionSepArg, 0 },
119 {"-speed", ".speed", XrmoptionSepArg, 0 },
120 {"-duration", ".duration", XrmoptionSepArg, 0 },
121 {"-mipmaps", ".mipmap", XrmoptionNoArg, "True" },
122 {"-no-mipmaps", ".mipmap", XrmoptionNoArg, "False" },
123 {"-titles", ".titles", XrmoptionNoArg, "True" },
124 {"-no-titles", ".titles", XrmoptionNoArg, "False" },
125 {"-polaroid", ".polaroid", XrmoptionNoArg, "True" },
126 {"-no-polaroid", ".polaroid", XrmoptionNoArg, "False" },
127 {"-clip", ".clip", XrmoptionNoArg, "True" },
128 {"-no-clip", ".clip", XrmoptionNoArg, "False" },
129 {"-shadows", ".shadows", XrmoptionNoArg, "True" },
130 {"-no-shadows", ".shadows", XrmoptionNoArg, "False" },
131 {"-debug", ".debug", XrmoptionNoArg, "True" },
132 {"-font", ".font", XrmoptionSepArg, 0 },
135 static argtype vars[] = {
136 { &scale, "scale", "Scale", DEF_SCALE, t_Float},
137 { &max_tilt, "maxTilt", "MaxTilt", DEF_MAX_TILT, t_Float},
138 { &speed, "speed", "Speed", DEF_SPEED, t_Float},
139 { &duration, "duration", "Duration", DEF_DURATION, t_Int},
140 { &mipmap_p, "mipmap", "Mipmap", DEF_MIPMAP, t_Bool},
141 { &titles_p, "titles", "Titles", DEF_TITLES, t_Bool},
142 { &polaroid_p, "polaroid", "Polaroid", DEF_POLAROID, t_Bool},
143 { &clip_p, "clip", "Clip", DEF_CLIP, t_Bool},
144 { &shadows_p, "shadows", "Shadows", DEF_SHADOWS, t_Bool},
145 { &debug_p, "debug", "Debug", DEF_DEBUG, t_Bool},
148 ENTRYPOINT ModeSpecOpt photopile_opts = {countof(opts), opts, countof(vars), vars, NULL};
151 /* Functions to interpolate between image positions.
154 add_pos(position p, position q)
163 scale_pos(GLfloat t, position p)
172 linear_combination(GLfloat t, position p, position q)
174 return add_pos(scale_pos(1.0 - t, p), scale_pos(t, q));
178 interpolate(GLfloat t, position p[4])
180 /* de Casteljau's algorithm, 4 control points */
181 position p10 = linear_combination(t, p[0], p[1]);
182 position p11 = linear_combination(t, p[1], p[2]);
183 position p12 = linear_combination(t, p[2], p[3]);
185 position p20 = linear_combination(t, p10, p11);
186 position p21 = linear_combination(t, p11, p12);
188 return linear_combination(t, p20, p21);
192 offset_pos(position p, GLfloat th, GLfloat r)
196 p.angle = (frand(2.0) - 1.0) * max_tilt;
200 /* Calculate new positions for all images.
203 set_new_positions(photopile_state *ss)
205 ModeInfo *mi = ss->mi;
208 for (i = 0; i < MI_COUNT(mi)+1; ++i)
210 image *frame = ss->frames + i;
211 GLfloat d = sqrt(frame->w*frame->w + frame->h*frame->h);
212 GLfloat leave = frand(M_PI * 2.0);
213 GLfloat enter = frand(M_PI * 2.0);
216 frame->pos[0] = frame->pos[3];
219 frame->pos[3].x = BELLRAND(MI_WIDTH(mi));
220 frame->pos[3].y = BELLRAND(MI_HEIGHT(mi));
221 frame->pos[3].angle = (frand(2.0) - 1.0) * max_tilt;
223 /* intermediate points */
224 frame->pos[1] = offset_pos(frame->pos[0], leave, d * (0.5 + frand(1.0)));
225 frame->pos[2] = offset_pos(frame->pos[3], enter, d * (0.5 + frand(1.0)));
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 photopile_state *ss = (photopile_state *) closure;
238 ModeInfo *mi = ss->mi;
239 int wire = MI_IS_WIREFRAME(mi);
240 image *frame = ss->frames + ss->nframe;
246 frame->w = (int)(MI_WIDTH(mi) * scale) - 1;
247 frame->h = (int)(MI_HEIGHT(mi) * scale) - 1;
251 frame->w = (int)(MI_HEIGHT(mi) * scale) - 1;
252 frame->h = (int)(MI_WIDTH(mi) * scale) - 1;
254 if (frame->w <= 10) frame->w = 10;
255 if (frame->h <= 10) frame->h = 10;
256 frame->geom.width = frame->w;
257 frame->geom.height = frame->h;
261 if (image_width == 0 || image_height == 0)
264 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
265 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
266 mipmap_p ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
268 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
269 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
271 frame->w = image_width;
272 frame->h = image_height;
273 frame->tw = texture_width;
274 frame->th = texture_height;
279 frame->title = (filename ? strdup (filename) : 0);
281 if (frame->title) /* strip filename to part after last /. */
283 char *s = strrchr (frame->title, '/');
284 if (s) strcpy (frame->title, s+1);
288 fprintf (stderr, "%s: loaded %4d x %-4d %4d x %-4d \"%s\"\n",
292 frame->tw, frame->th,
293 (frame->title ? frame->title : "(null)"));
296 frame->loaded_p = True;
303 load_image (ModeInfo *mi)
305 photopile_state *ss = &sss[MI_SCREEN(mi)];
306 int wire = MI_IS_WIREFRAME(mi);
307 image *frame = ss->frames + ss->nframe;
309 if (debug_p && !wire && frame->w != 0)
310 fprintf (stderr, "%s: dropped %4d x %-4d %4d x %-4d \"%s\"\n",
314 frame->tw, frame->th,
315 (frame->title ? frame->title : "(null)"));
317 frame->loaded_p = False;
320 image_loaded_cb (0, 0, 0, 0, 0, 0, ss);
323 int w = MI_WIDTH(mi);
324 int h = MI_HEIGHT(mi);
325 int size = (int)((w > h ? w : h) * scale);
326 if (size <= 10) size = 10;
327 load_texture_async (mi->xgwa.screen, mi->window, *ss->glx_context,
329 mipmap_p, frame->texid,
330 image_loaded_cb, ss);
336 loading_msg (ModeInfo *mi)
338 photopile_state *ss = &sss[MI_SCREEN(mi)];
339 int wire = MI_IS_WIREFRAME(mi);
340 const char text[] = "Loading...";
344 if (ss->loading_sw == 0) /* only do this once */
345 ss->loading_sw = texture_string_width (ss->texfont, text, &ss->loading_sh);
347 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
349 glMatrixMode(GL_PROJECTION);
353 glMatrixMode(GL_MODELVIEW);
356 gluOrtho2D(0, MI_WIDTH(mi), 0, MI_HEIGHT(mi));
358 glTranslatef ((MI_WIDTH(mi) - ss->loading_sw) / 2,
359 (MI_HEIGHT(mi) - ss->loading_sh) / 2,
362 glEnable (GL_TEXTURE_2D);
363 glDisable (GL_DEPTH_TEST);
364 print_texture_string (ss->texfont, text);
365 glEnable (GL_DEPTH_TEST);
368 glMatrixMode(GL_PROJECTION);
371 glMatrixMode(GL_MODELVIEW);
374 glXSwapBuffers (MI_DISPLAY (mi), MI_WINDOW(mi));
379 loading_initial_image (ModeInfo *mi)
381 photopile_state *ss = &sss[MI_SCREEN(mi)];
383 if (ss->frames[ss->nframe].loaded_p)
385 /* The initial image has been fully loaded, start fading it in. */
388 for (i = 0; i < ss->nframe; ++i)
390 ss->frames[i].pos[3].x = MI_WIDTH(mi) * 0.5;
391 ss->frames[i].pos[3].y = MI_HEIGHT(mi) * 0.5;
392 ss->frames[i].pos[3].angle = 0.0;
394 set_new_positions(ss);
397 ss->mode_tick = fade_ticks / speed;
404 return (ss->mode == EARLY);
409 reshape_photopile (ModeInfo *mi, int width, int height)
411 glViewport (0, 0, (GLint) width, (GLint) height);
413 glMatrixMode(GL_PROJECTION);
416 glMatrixMode(GL_MODELVIEW);
418 gluOrtho2D(0, MI_WIDTH(mi), 0, MI_HEIGHT(mi));
420 glClear(GL_COLOR_BUFFER_BIT);
424 /* Kludge to add "-v" to invocation of "xscreensaver-getimage" in -debug mode
427 hack_resources (Display *dpy)
430 char *res = "desktopGrabber";
431 char *val = get_string_resource (dpy, res, "DesktopGrabber");
435 XrmDatabase db = XtDatabase (dpy);
436 sprintf (buf1, "%.100s.%.100s", progname, res);
437 sprintf (buf2, "%.200s -v", val);
439 value.size = strlen(buf2);
440 XrmPutResource (&db, buf1, "String", &value);
441 # endif /* !HAVE_COCOA */
446 init_photopile (ModeInfo *mi)
448 int screen = MI_SCREEN(mi);
450 int wire = MI_IS_WIREFRAME(mi);
453 if ((sss = (photopile_state *)
454 calloc (MI_NUM_SCREENS(mi), sizeof(photopile_state))) == NULL)
460 if ((ss->glx_context = init_GL(mi)) != NULL) {
461 reshape_photopile (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
466 glDisable (GL_LIGHTING);
467 glEnable (GL_DEPTH_TEST);
468 glDisable (GL_CULL_FACE);
472 glShadeModel (GL_SMOOTH);
473 glEnable (GL_LINE_SMOOTH);
474 glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
476 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
479 ss->shadow = init_drop_shadow();
480 ss->texfont = load_texture_font (MI_DISPLAY(mi), "font");
483 hack_resources (MI_DISPLAY (mi));
485 ss->frames = (image *)calloc (MI_COUNT(mi) + 1, sizeof(image));
490 for (i = 0; i < MI_COUNT(mi) + 1; ++i)
492 glGenTextures (1, &(ss->frames[i].texid));
493 if (ss->frames[i].texid <= 0) abort();
498 load_image(mi); /* start loading the first image */
503 draw_image (ModeInfo *mi, int i, GLfloat t, GLfloat s, GLfloat z)
505 int wire = MI_IS_WIREFRAME(mi);
506 photopile_state *ss = &sss[MI_SCREEN(mi)];
507 image *frame = ss->frames + i;
509 position pos = interpolate(t, frame->pos);
510 GLfloat w = frame->geom.width * 0.5;
511 GLfloat h = frame->geom.height * 0.5;
512 GLfloat z1 = z - 0.25 / (MI_COUNT(mi) + 1);
513 GLfloat z2 = z - 0.5 / (MI_COUNT(mi) + 1);
520 GLfloat minSize = MIN(w, h);
521 GLfloat maxSize = MAX(w, h);
523 /* Clip or scale image to fit in the frame.
531 GLfloat scale = minSize / maxSize;
536 w1 = minSize * 1.16; /* enlarge frame border */
539 s /= 1.5; /* compensate for border size */
544 /* Position and scale this image.
546 glTranslatef (pos.x, pos.y, 0);
547 glRotatef (pos.angle, 0, 0, 1);
550 /* Draw the drop shadow. */
551 if (shadows_p && !wire)
554 draw_drop_shadow(ss->shadow, -w1, -h1, z2, 2.0 * w1, h1 + h2, 20.0);
557 /* Draw the retro instant-film frame.
565 glVertex3f (-w1, -h1, z2);
566 glVertex3f ( w1, -h1, z2);
567 glVertex3f ( w1, h2, z2);
568 glVertex3f (-w1, h2, z2);
573 glColor3f (0.5, 0.5, 0.5);
574 glBegin (GL_LINE_LOOP);
575 glVertex3f (-w1, -h1, z);
576 glVertex3f ( w1, -h1, z);
577 glVertex3f ( w1, h2, z);
578 glVertex3f (-w1, h2, z);
582 /* Draw the image quad.
586 GLfloat texw = w / frame->tw;
587 GLfloat texh = h / frame->th;
588 GLfloat texx = (frame->geom.x + 0.5 * frame->geom.width) / frame->tw;
589 GLfloat texy = (frame->geom.y + 0.5 * frame->geom.height) / frame->th;
591 glBindTexture (GL_TEXTURE_2D, frame->texid);
592 glEnable (GL_TEXTURE_2D);
595 glTexCoord2f (texx - texw, texy + texh); glVertex3f (-w, -h, z1);
596 glTexCoord2f (texx + texw, texy + texh); glVertex3f ( w, -h, z1);
597 glTexCoord2f (texx + texw, texy - texh); glVertex3f ( w, h, z1);
598 glTexCoord2f (texx - texw, texy - texh); glVertex3f (-w, h, z1);
600 glDisable (GL_TEXTURE_2D);
603 /* Draw a box around it.
606 glColor3f (0.5, 0.5, 0.5);
607 glBegin (GL_LINE_LOOP);
608 glVertex3f (-w, -h, z);
609 glVertex3f ( w, -h, z);
610 glVertex3f ( w, h, z);
611 glVertex3f (-w, h, z);
614 /* Draw a title under the image.
620 char *title = frame->title ? frame->title : "(untitled)";
621 sw = texture_string_width (ss->texfont, title, &sh);
623 glTranslatef (-sw*scale*0.5, -h - sh*scale, z);
624 glScalef (scale, scale, 1);
626 if (wire || !polaroid_p)
637 glEnable (GL_TEXTURE_2D);
638 print_texture_string (ss->texfont, title);
639 glDisable (GL_TEXTURE_2D);
643 glBegin (GL_LINE_LOOP);
644 glVertex3f (0, 0, 0);
645 glVertex3f (sw, 0, 0);
646 glVertex3f (sw, sh, 0);
647 glVertex3f (0, sh, 0);
657 draw_photopile (ModeInfo *mi)
659 photopile_state *ss = &sss[MI_SCREEN(mi)];
662 if (!ss->glx_context)
665 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(ss->glx_context));
667 if (ss->mode == EARLY)
668 if (loading_initial_image (mi))
671 /* Only check the wall clock every 10 frames */
672 if (ss->now == 0 || ss->draw_tick++ > 10)
674 ss->now = time((time_t *) 0);
675 if (ss->last_time == 0) ss->last_time = ss->now;
679 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
683 /* Handle state transitions. */
687 if (--ss->mode_tick <= 0)
690 ss->last_time = time((time_t *) 0);
694 if (ss->now - ss->last_time > duration)
701 if (ss->frames[ss->nframe].loaded_p)
703 set_new_positions(ss);
705 ss->mode_tick = fade_ticks / speed;
709 if (--ss->mode_tick <= 0)
711 ss->nframe = (ss->nframe+1) % (MI_COUNT(mi)+1);
714 ss->last_time = time((time_t *) 0);
721 t = 1.0 - ss->mode_tick / (fade_ticks / speed);
722 t = 0.5 * (1.0 - cos(M_PI * t));
724 /* Draw the images. */
725 for (i = 0; i < MI_COUNT(mi) + (ss->mode == SHUFFLE); ++i)
727 int j = (ss->nframe + i + 1) % (MI_COUNT(mi) + 1);
729 if (ss->frames[j].loaded_p)
732 GLfloat z = (GLfloat)i / (MI_COUNT(mi) + 1);
744 if (i == MI_COUNT(mi))
757 draw_image(mi, j, t, s, z);
762 if (mi->fps_p) do_fps (mi);
764 glXSwapBuffers (MI_DISPLAY (mi), MI_WINDOW(mi));
767 XSCREENSAVER_MODULE ("Photopile", photopile)