From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / kaleidocycle.c
index 025e45b40f284c9cca869b59e8c535adc1dda79f..679151ffa5957e9888aea07161ca384d7262e4c2 100644 (file)
@@ -1,4 +1,4 @@
-/* kaleidocycle, Copyright (c) 2013 Jamie Zawinski <jwz@jwz.org>
+/* kaleidocycle, Copyright (c) 2013-2014 Jamie Zawinski <jwz@jwz.org>
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
@@ -19,6 +19,7 @@
                        "*count:        16          \n" \
                        "*showFPS:      False       \n" \
                        "*wireframe:    False       \n" \
+                       "*suppressRotationAnimation: True\n" \
 
 # define refresh_kaleidocycle 0
 # define release_kaleidocycle 0
@@ -31,6 +32,7 @@
 #include "rotator.h"
 #include "gltrackball.h"
 #include <ctype.h>
+#include <math.h>
 
 #ifdef USE_GL /* whole file */
 
@@ -101,6 +103,14 @@ reshape_kaleidocycle (ModeInfo *mi, int width, int height)
              0.0, 0.0, 0.0,
              0.0, 1.0, 0.0);
 
+# ifdef HAVE_MOBILE    /* Keep it the same relative size when rotated. */
+  {
+    int o = (int) current_device_rotation();
+    if (o != 0 && o != 180 && o != -180)
+      glScalef (1/h, 1/h, 1/h);
+  }
+# endif
+
   glClear(GL_COLOR_BUFFER_BIT);
 }
 
@@ -110,37 +120,38 @@ kaleidocycle_handle_event (ModeInfo *mi, XEvent *event)
 {
   kaleidocycle_configuration *bp = &bps[MI_SCREEN(mi)];
 
-  if (event->xany.type == ButtonPress &&
-      event->xbutton.button == Button1)
-    {
-      bp->button_down_p = True;
-      gltrackball_start (bp->trackball,
-                         event->xbutton.x, event->xbutton.y,
-                         MI_WIDTH (mi), MI_HEIGHT (mi));
-      return True;
-    }
-  else if (event->xany.type == ButtonRelease &&
-           event->xbutton.button == Button1)
-    {
-      bp->button_down_p = False;
-      return True;
-    }
-  else if (event->xany.type == ButtonPress &&
-           (event->xbutton.button == Button4 ||
-            event->xbutton.button == Button5 ||
-            event->xbutton.button == Button6 ||
-            event->xbutton.button == Button7))
+  if (gltrackball_event_handler (event, bp->trackball,
+                                 MI_WIDTH (mi), MI_HEIGHT (mi),
+                                 &bp->button_down_p))
+    return True;
+  else if (event->xany.type == KeyPress)
     {
-      gltrackball_mousewheel (bp->trackball, event->xbutton.button, 5,
-                              !!event->xbutton.state);
-      return True;
+      KeySym keysym;
+      char c = 0;
+      XLookupString (&event->xkey, &c, 1, &keysym, 0);
+      if (c == '+' || c == '.' || c == '>' || c == '=' || c == '+' ||
+          keysym == XK_Right || keysym == XK_Up || keysym == XK_Next)
+        {
+          bp->mode = IN;
+          return True;
+        }
+      else if ((c == '-' || c == ',' || c == '<' || c == '-' || c == '_' ||
+               keysym == XK_Left || keysym == XK_Down || keysym == XK_Prior) &&
+               bp->count > bp->min_count)
+        {
+          bp->mode = OUT;
+          return True;
+        }
+      else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
+        goto DEF;
     }
-  else if (event->xany.type == MotionNotify &&
-           bp->button_down_p)
+  else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
     {
-      gltrackball_track (bp->trackball,
-                         event->xmotion.x, event->xmotion.y,
-                         MI_WIDTH (mi), MI_HEIGHT (mi));
+    DEF:
+      if (bp->count <= bp->min_count)
+        bp->mode = IN;
+      else
+        bp->mode = (random() & 1) ? IN : OUT;
       return True;
     }
 
@@ -156,14 +167,7 @@ init_kaleidocycle (ModeInfo *mi)
   int wire = MI_IS_WIREFRAME(mi);
   int i;
 
-  if (!bps) {
-    bps = (kaleidocycle_configuration *)
-      calloc (MI_NUM_SCREENS(mi), sizeof (kaleidocycle_configuration));
-    if (!bps) {
-      fprintf(stderr, "%s: out of memory\n", progname);
-      exit(1);
-    }
-  }
+  MI_INIT (mi, bps, NULL);
 
   bp = &bps[MI_SCREEN(mi)];
 
@@ -227,7 +231,7 @@ init_kaleidocycle (ModeInfo *mi)
                             False);
     bp->rot2 = make_rotator (twist_speed, 0, 0, twist_accel, 0, True);
 
-    bp->trackball = gltrackball_init ();
+    bp->trackball = gltrackball_init (True);
   }
 
   if (MI_COUNT(mi) < 8) MI_COUNT(mi) = 8;
@@ -423,10 +427,7 @@ draw_kaleidocycle (ModeInfo *mi)
                  (y - 0.5) * 5,
                  (z - 0.5) * 10);
 
-    /* Do it twice because we don't track the device's orientation. */
-    glRotatef( current_device_rotation(), 0, 0, 1);
     gltrackball_rotate (bp->trackball);
-    glRotatef(-current_device_rotation(), 0, 0, 1);
 
     get_rotation (bp->rot, &x, &y, &z, !bp->button_down_p);
     glRotatef (x * 360, 1, 0, 0);