4fd1b2fcb2266856b203223004a9c97871ed239d
[xscreensaver] / hacks / glx / carousel.c
1 /* carousel, Copyright (c) 2005-2006 Jamie Zawinski <jwz@jwz.org>
2  * Loads a sequence of images and rotates them around.
3  *
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
10  * implied warranty.
11  *
12  * Created: 21-Feb-2005
13  */
14
15 #define DEF_FONT "-*-times-bold-r-normal-*-240-*"
16 #define DEFAULTS  "*count:           7         \n" \
17                   "*delay:           10000     \n" \
18                   "*wireframe:       False     \n" \
19                   "*showFPS:         False     \n" \
20                   "*fpsSolid:        True      \n" \
21                   "*useSHM:          True      \n" \
22                   "*font:          " DEF_FONT "\n" \
23                   "*desktopGrabber:  xscreensaver-getimage -no-desktop %s\n" \
24                   "*grabDesktopImages:   False \n" \
25                   "*chooseRandomImages:  True  \n"
26
27 # define refresh_carousel 0
28 # define release_carousel 0
29 # include "xlockmore.h"
30
31 #undef countof
32 #define countof(x) (sizeof((x))/sizeof((*x)))
33
34 #ifdef USE_GL
35
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"
43
44 #include "rotator.h"
45 #include "gltrackball.h"
46 #include "grab-ximage.h"
47 #include "texfont.h"
48
49 # ifndef HAVE_COCOA
50 #  include <X11/Intrinsic.h>     /* for XrmDatabase in -debug mode */
51 # endif
52
53 typedef struct {
54   double x, y, w, h;
55 } rect;
56
57 typedef enum { EARLY, NORMAL, LOADING, OUT, IN, DEAD } fade_mode;
58 static int fade_ticks = 60;
59
60 typedef struct {
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 */
65   GLuint texid;
66 } image;
67
68 typedef struct {
69   ModeInfo *mi;
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 */
76   int mode_tick;
77   Bool loaded_p;                /* whether background load is done */
78 } image_frame;
79
80
81 typedef struct {
82   GLXContext *glx_context;
83   rotator *rot;
84   trackball_state *trackball;
85   Bool button_down_p;
86   time_t button_down_time;
87
88   int nframes;                  /* how many frames are loaded */
89   int frames_size;
90   image_frame **frames;         /* pointers to the frames */
91
92   Bool awaiting_first_images_p;
93   int loads_in_progress;
94
95   texture_font_data *texfont;
96
97   fade_mode mode;
98   int mode_tick;
99
100   int loading_sw, loading_sh;
101
102   time_t last_time, now;
103   int draw_tick;
104
105 } carousel_state;
106
107 static carousel_state *sss = NULL;
108
109
110 /* Command-line arguments
111  */
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. */
121
122
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 },
136 };
137
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},
146 };
147
148 ENTRYPOINT ModeSpecOpt carousel_opts = {countof(opts), opts, countof(vars), vars, NULL};
149
150
151 /* Allocates a frame structure and stores it in the list.
152  */
153 static image_frame *
154 alloc_frame (ModeInfo *mi)
155 {
156   carousel_state *ss = &sss[MI_SCREEN(mi)];
157   image_frame *frame = (image_frame *) calloc (1, sizeof (*frame));
158
159   frame->mi = mi;
160   frame->mode = EARLY;
161   frame->rot = make_rotator (0, 0, 0, 0, 0.04 * frand(1.0) * speed, False);
162
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();
167
168   if (ss->frames_size <= ss->nframes)
169     {
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));
173       if (! ss->frames)
174         {
175           fprintf (stderr, "%s: out of memory (%d images)\n",
176                    progname, ss->frames_size);
177           exit (1);
178         }
179     }
180
181   ss->frames[ss->nframes++] = frame;
182
183   return frame;
184 }
185
186
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,
190                              void *closure);
191
192
193 /* Load a new file into the given image struct.
194  */
195 static void
196 load_image (ModeInfo *mi, image_frame *frame)
197 {
198   carousel_state *ss = &sss[MI_SCREEN(mi)];
199   int wire = MI_IS_WIREFRAME(mi);
200
201   if (debug_p && !wire && frame->current.w != 0)
202     fprintf (stderr, "%s:  dropped %4d x %-4d  %4d x %-4d  \"%s\"\n",
203              progname, 
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)"));
208
209   switch (frame->mode) 
210     {
211     case EARLY:  break;
212     case NORMAL: frame->mode = LOADING; break;
213     default: abort();
214     }
215
216   ss->loads_in_progress++;
217
218   if (wire)
219     image_loaded_cb (0, 0, 0, 0, 0, 0, frame);
220   else
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);
226 }
227
228
229 /* Callback that tells us that the texture has been loaded.
230  */
231 static void
232 image_loaded_cb (const char *filename, XRectangle *geom,
233                  int image_width, int image_height,
234                  int texture_width, int texture_height,
235                  void *closure)
236 {
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);
241
242   if (wire)
243     {
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;
248       goto DONE;
249     }
250
251   if (image_width == 0 || image_height == 0)
252     exit (1);
253
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);
257
258   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
259   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
260
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;
266
267   if (frame->loading.title)
268     free (frame->loading.title);
269   frame->loading.title = (filename ? strdup (filename) : 0);
270
271   if (frame->loading.title)   /* strip filename to part after last /. */
272     {
273       char *s = strrchr (frame->loading.title, '/');
274       if (s) strcpy (frame->loading.title, s+1);
275     }
276
277   if (debug_p)
278     fprintf (stderr, "%s:   loaded %4d x %-4d  %4d x %-4d  \"%s\"\n",
279              progname,
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)"));
284
285  DONE:
286
287   frame->loaded_p = True;
288
289   if (ss->loads_in_progress <= 0) abort();
290   ss->loads_in_progress--;
291
292   /* This image expires N seconds after it finished loading. */
293   frame->expires = time((time_t *) 0) + (duration * MI_COUNT(mi));
294
295   switch (frame->mode) 
296     {
297     case EARLY:         /* part of the initial batch of images */
298       {
299         image swap = frame->current;
300         frame->current = frame->loading;
301         frame->loading = swap;
302       }
303       break;
304     case LOADING:       /* start dropping the old image out */
305       {
306         frame->mode = OUT;
307         frame->mode_tick = fade_ticks / speed;
308         frame->from_top_p = random() & 1;
309       }
310       break;
311     default:
312       abort();
313     }
314 }
315
316
317 static void loading_msg (ModeInfo *mi, int n);
318
319 static Bool
320 load_initial_images (ModeInfo *mi)
321 {
322   carousel_state *ss = &sss[MI_SCREEN(mi)];
323   int i;
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;
328
329   if (all_loaded_p)
330     {
331       if (ss->nframes < MI_COUNT (mi))
332         {
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.)
336            */
337           load_image (mi, alloc_frame (mi));
338         }
339       else
340         {
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.
343            */
344           time_t now = time((time_t *) 0);
345           int i;
346
347           for (i = 0; i < ss->nframes; i++)
348             {
349               image_frame *frame = ss->frames[i];
350               frame->r = 1.0;
351               frame->theta = i * 360.0 / ss->nframes;
352               frame->expires = now + (duration * (i + 1));
353               frame->mode = NORMAL;
354             }
355
356           /* Instead of always going clockwise, shuffle the expire times
357              of the frames so that they drop out in a random order.
358           */
359           for (i = 0; i < ss->nframes; i++)
360             {
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;
366             }
367
368           ss->awaiting_first_images_p = False;
369         }
370     }
371       
372   loading_msg (mi, ss->nframes-1);
373
374   return !ss->awaiting_first_images_p;
375 }
376
377
378 ENTRYPOINT void
379 reshape_carousel (ModeInfo *mi, int width, int height)
380 {
381   GLfloat h = (GLfloat) height / (GLfloat) width;
382
383   glViewport (0, 0, (GLint) width, (GLint) height);
384
385   glMatrixMode(GL_PROJECTION);
386   glLoadIdentity();
387   gluPerspective (60.0, 1/h, 1.0, 8.0);
388
389   glMatrixMode(GL_MODELVIEW);
390   glLoadIdentity();
391   gluLookAt( 0.0, 0.0, 2.6,
392              0.0, 0.0, 0.0,
393              0.0, 1.0, 0.0);
394
395   glClear(GL_COLOR_BUFFER_BIT);
396 }
397
398
399 ENTRYPOINT Bool
400 carousel_handle_event (ModeInfo *mi, XEvent *event)
401 {
402   carousel_state *ss = &sss[MI_SCREEN(mi)];
403
404   if (event->xany.type == ButtonPress &&
405       event->xbutton.button == Button1)
406     {
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));
413       return True;
414     }
415   else if (event->xany.type == ButtonRelease &&
416            event->xbutton.button == Button1)
417     {
418       if (ss->button_down_p)
419         {
420           /* Add the time the mouse was held to the expire times of all
421              frames, so that mouse-dragging doesn't count against
422              image expiration.
423            */
424           int secs = time((time_t *) 0) - ss->button_down_time;
425           int i;
426           for (i = 0; i < ss->nframes; i++)
427             ss->frames[i]->expires += secs;
428         }
429       ss->button_down_p = False;
430       return True;
431     }
432   else if (event->xany.type == ButtonPress &&
433            (event->xbutton.button == Button4 ||
434             event->xbutton.button == Button5))
435     {
436       gltrackball_mousewheel (ss->trackball, event->xbutton.button, 5,
437                               !event->xbutton.state);
438       return True;
439     }
440   else if (event->xany.type == MotionNotify &&
441            ss->button_down_p)
442     {
443       gltrackball_track (ss->trackball,
444                          event->xmotion.x, event->xmotion.y,
445                          MI_WIDTH (mi), MI_HEIGHT (mi));
446       return True;
447     }
448
449   return False;
450 }
451
452
453 /* Kludge to add "-v" to invocation of "xscreensaver-getimage" in -debug mode
454  */
455 static void
456 hack_resources (Display *dpy)
457 {
458 # ifndef HAVE_COCOA
459   char *res = "desktopGrabber";
460   char *val = get_string_resource (dpy, res, "DesktopGrabber");
461   char buf1[255];
462   char buf2[255];
463   XrmValue value;
464   XrmDatabase db = XtDatabase (dpy);
465   sprintf (buf1, "%.100s.%.100s", progname, res);
466   sprintf (buf2, "%.200s -v", val);
467   value.addr = buf2;
468   value.size = strlen(buf2);
469   XrmPutResource (&db, buf1, "String", &value);
470 # endif /* !HAVE_COCOA */
471 }
472
473
474 static void
475 loading_msg (ModeInfo *mi, int n)
476 {
477   carousel_state *ss = &sss[MI_SCREEN(mi)];
478   int wire = MI_IS_WIREFRAME(mi);
479   char text[100];
480   GLfloat scale;
481
482   if (wire) return;
483
484   if (n == 0)
485     sprintf (text, "Loading images...");
486   else
487     sprintf (text, "Loading images...  (%d%%)",
488              (int) (n * 100 / MI_COUNT(mi)));
489
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);
492
493   scale = ss->loading_sh / (GLfloat) MI_HEIGHT(mi);
494
495   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
496
497   glMatrixMode(GL_PROJECTION);
498   glPushMatrix();
499   glLoadIdentity();
500
501   glMatrixMode(GL_MODELVIEW);
502   glPushMatrix();
503   glLoadIdentity();
504   gluOrtho2D(0, MI_WIDTH(mi), 0, MI_HEIGHT(mi));
505
506   glTranslatef ((MI_WIDTH(mi)  - ss->loading_sw) / 2,
507                 (MI_HEIGHT(mi) - ss->loading_sh) / 2,
508                 0);
509   glColor3f (1, 1, 0);
510   glEnable (GL_TEXTURE_2D);
511   glDisable (GL_DEPTH_TEST);
512   print_texture_string (ss->texfont, text);
513   glEnable (GL_DEPTH_TEST);
514   glPopMatrix();
515
516   glMatrixMode(GL_PROJECTION);
517   glPopMatrix();
518
519   glMatrixMode(GL_MODELVIEW);
520
521   glFinish();
522   glXSwapBuffers (MI_DISPLAY (mi), MI_WINDOW(mi));
523 }
524
525
526 ENTRYPOINT void
527 init_carousel (ModeInfo *mi)
528 {
529   int screen = MI_SCREEN(mi);
530   carousel_state *ss;
531   int wire = MI_IS_WIREFRAME(mi);
532   
533   if (sss == NULL) {
534     if ((sss = (carousel_state *)
535          calloc (MI_NUM_SCREENS(mi), sizeof(carousel_state))) == NULL)
536       return;
537   }
538   ss = &sss[screen];
539
540   if ((ss->glx_context = init_GL(mi)) != NULL) {
541     reshape_carousel (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
542   } else {
543     MI_CLEARWINDOW(mi);
544   }
545
546   if (!tilt_str || !*tilt_str)
547     ;
548   else if (!strcasecmp (tilt_str, "0"))
549     ;
550   else if (!strcasecmp (tilt_str, "X"))
551     tilt_x_p = 1;
552   else if (!strcasecmp (tilt_str, "Y"))
553     tilt_y_p = 1;
554   else if (!strcasecmp (tilt_str, "XY"))
555     tilt_x_p = tilt_y_p = 1;
556   else
557     {
558       fprintf (stderr, "%s: tilt must be 'X', 'Y', 'XY' or '', not '%s'\n",
559                progname, tilt_str);
560       exit (1);
561     }
562
563   {
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 */
567
568     spin_speed   *= 0.9 + frand(0.2);
569     wander_speed *= 0.9 + frand(0.2);
570
571     ss->rot = make_rotator (spin_speed, spin_speed, spin_speed,
572                             spin_accel, wander_speed, True);
573
574     ss->trackball = gltrackball_init ();
575   }
576
577   glDisable (GL_LIGHTING);
578   glEnable (GL_DEPTH_TEST);
579   glDisable (GL_CULL_FACE);
580
581   if (! wire)
582     {
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);
588       glEnable (GL_BLEND);
589       glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
590       glEnable (GL_ALPHA_TEST);
591
592       glEnable (GL_POLYGON_OFFSET_FILL);
593       glPolygonOffset (1.0, 1.0);
594
595     }
596
597   ss->texfont = load_texture_font (MI_DISPLAY(mi), "font");
598
599   if (debug_p)
600     hack_resources (MI_DISPLAY (mi));
601
602   ss->nframes = 0;
603   ss->frames_size = 10;
604   ss->frames = (image_frame **)
605     calloc (1, ss->frames_size * sizeof(*ss->frames));
606
607   ss->mode = IN;
608   ss->mode_tick = fade_ticks / speed;
609
610   ss->awaiting_first_images_p = True;
611 }
612
613
614 static void
615 draw_frame (ModeInfo *mi, image_frame *frame, time_t now, Bool body_p)
616 {
617   carousel_state *ss = &sss[MI_SCREEN(mi)];
618   int wire = MI_IS_WIREFRAME(mi);
619
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);
628
629   glBindTexture (GL_TEXTURE_2D, frame->current.texid);
630
631   glPushMatrix();
632
633   /* Position this image on the wheel.
634    */
635   glRotatef (frame->theta, 0, 1, 0);
636   glTranslatef (0, 0, frame->r);
637
638   /* Scale down the image so that all N frames fit on the wheel
639      without bumping in to each other.
640   */
641   {
642     GLfloat t, s;
643     switch (ss->nframes)
644       {
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;
650       }
651     glTranslatef (0, 0, t);
652     glScalef (s, s, s);
653   }
654
655   /* Center this image on the wheel plane.
656    */
657   glTranslatef (-0.5, -(aspect/2), 0);
658
659   /* Move as per the "zoom in and out" setting.
660    */
661   if (zoom_p)
662     {
663       double x, y, z;
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);
667     }
668
669   /* Compute the "drop in and out" state.
670    */
671   switch (frame->mode)
672     {
673     case EARLY:
674       abort();
675       break;
676     case NORMAL:
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);
681       break;
682     case LOADING:
683       break;
684     case OUT:
685       if (--frame->mode_tick <= 0) {
686         image swap = frame->current;
687         frame->current = frame->loading;
688         frame->loading = swap;
689
690         frame->mode = IN;
691         frame->mode_tick = fade_ticks / speed;
692       }
693       break;
694     case IN:
695       if (--frame->mode_tick <= 0)
696         frame->mode = NORMAL;
697       break;
698     default:
699       abort();
700     }
701
702   /* Now translate for current in/out state.
703    */
704   if (frame->mode == OUT || frame->mode == IN)
705     {
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)));
710       t = 5 * (1 - t);
711       if (frame->from_top_p) t = -t;
712       glTranslatef (0, t, 0);
713     }
714
715   if (body_p)                                   /* Draw the image quad. */
716     {
717       if (! wire)
718         {
719           glColor3f (1, 1, 1);
720           glNormal3f (0, 0, 1);
721           glEnable (GL_TEXTURE_2D);
722           glBegin (GL_QUADS);
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);
728           glEnd();
729         }
730
731       /* Draw a box around it.
732        */
733       glLineWidth (2.0);
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);
741       glEnd();
742
743     }
744   else                                  /* Draw a title under the image. */
745     {
746       int sw, sh;
747       GLfloat scale = 0.05;
748       char *title = frame->current.title ? frame->current.title : "(untitled)";
749       sw = texture_string_width (ss->texfont, title, &sh);
750
751       glTranslatef (0, -scale, 0);
752
753       scale /= sh;
754       glScalef (scale, scale, scale);
755
756       glTranslatef (((1/scale) - sw) / 2, 0, 0);
757       glColor3f (1, 1, 1);
758
759       if (!wire)
760         {
761           glEnable (GL_TEXTURE_2D);
762           print_texture_string (ss->texfont, title);
763         }
764       else
765         {
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);
771           glEnd();
772         }
773     }
774
775   glPopMatrix();
776 }
777
778
779 ENTRYPOINT void
780 draw_carousel (ModeInfo *mi)
781 {
782   carousel_state *ss = &sss[MI_SCREEN(mi)];
783   int i;
784
785   if (!ss->glx_context)
786     return;
787
788   glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(ss->glx_context));
789
790   if (ss->awaiting_first_images_p)
791     if (!load_initial_images (mi))
792       return;
793
794   /* Only check the wall clock every 10 frames */
795   {
796     if (ss->now == 0 || ss->draw_tick++ > 10)
797       {
798         ss->now = time((time_t *) 0);
799         if (ss->last_time == 0) ss->last_time = ss->now;
800         ss->draw_tick = 0;
801       }
802   }
803
804
805   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
806
807   glPushMatrix();
808
809
810   /* Run the startup "un-shrink" animation.
811    */
812   switch (ss->mode)
813     {
814     case IN:
815       if (--ss->mode_tick <= 0)
816         {
817           ss->mode = NORMAL;
818           ss->last_time = time((time_t *) 0);
819         }
820       break;
821     case NORMAL:
822       break;
823     default:
824       abort();
825     }
826
827
828   /* Scale as per the startup "un-shrink" animation.
829    */
830   if (ss->mode != NORMAL)
831     {
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)));
836       glScalef (s, s, s);
837     }
838
839   /* Rotate and tilt as per the user, and the motion modeller.
840    */
841   {
842     double x, y, z;
843     gltrackball_rotate (ss->trackball);
844
845     /* Tilt the tube up or down by up to 30 degrees */
846     get_position (ss->rot, &x, &y, &z, !ss->button_down_p);
847     if (tilt_x_p)
848       glRotatef (15 - (x * 30), 1, 0, 0);
849     if (tilt_y_p)
850       glRotatef (7  - (y * 14), 0, 0, 1);
851
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);
855   }
856
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.
861    */
862   for (i = 0; i < ss->nframes; i++)
863     draw_frame (mi, ss->frames[i], ss->now, True);
864   if (titles_p)
865     for (i = 0; i < ss->nframes; i++)
866       draw_frame (mi, ss->frames[i], ss->now, False);
867
868   glPopMatrix();
869
870   if (mi->fps_p) do_fps (mi);
871   glFinish();
872   glXSwapBuffers (MI_DISPLAY (mi), MI_WINDOW(mi));
873 }
874
875 XSCREENSAVER_MODULE ("Carousel", carousel)
876
877 #endif /* USE_GL */