http://packetstormsecurity.org/UNIX/admin/xscreensaver-4.04.2.tar.gz
[xscreensaver] / hacks / glx / extrusion.c
index 2e6958ccf0effdcb631e39b2b6a1eea4f6a66400..fd9f0f4891b5bcf084c3da5308ff07b6398a0d31 100644 (file)
@@ -38,6 +38,8 @@
 # define HACK_INIT                                             init_screensaver
 # define HACK_DRAW                                             draw_screensaver
 # define HACK_RESHAPE                                  reshape_screensaver
+# define HACK_HANDLE_EVENT                             screensaver_handle_event
+# define EVENT_MASK                                            PointerMotionMask
 # define screensaver_opts                              xlockmore_opts
 #define        DEFAULTS                        "*delay:                        10000   \n" \
                                                                                "*showFPS:              False   \n" \
@@ -80,6 +82,7 @@
 #define countof(x) (sizeof((x))/sizeof((*x)))
 
 #include "xpm-ximage.h"
+#include "rotator.h"
 
 #define checkImageWidth 64
 #define checkImageHeight 64
@@ -175,6 +178,9 @@ ModStruct   screensaver_description =
 typedef struct {
   int screen_width, screen_height;
   GLXContext *glx_context;
+  rotator *rot;
+  Bool button_down_p;
+  int mouse_x, mouse_y;
   Window window;
   XColor fg, bg;
 } screensaverstruct;
@@ -194,17 +200,12 @@ static GLfloat lightTwoPosition[] = {-40.0, 40, 100.0, 0.0};
 static GLfloat lightTwoColor[] = {0.99, 0.99, 0.99, 1.0}; 
 
 float rot_x=0, rot_y=0, rot_z=0;
-static float dx=0, dy=0, dz=0;
-static float ddx=0, ddy=0, ddz=0;
-static float d_max = 0;
-static int screensaver_number;
+float lastx=0, lasty=0;
 
 static float max_lastx=300, max_lasty=400;
 static float min_lastx=-400, min_lasty=-400;
-static float d_lastx=0, d_lasty=0;
-static float dd_lastx=0, dd_lasty=0;
-static float max_dlastx=0, max_dlasty=0;
-float lastx=0, lasty=0;
+
+static int screensaver_number;
 
 struct functions {
   void (*InitStuff)(void);
@@ -326,139 +327,15 @@ void Create_Texture(ModeInfo *mi, const char *filename)
 }
 
 
-/* mostly lifted from lament.c */
-static void
-rotate (float *pos, float *v, float *dv, float max_v)
-{
-  double ppos = *pos;
-
-  /* tick position */
-  if (ppos < 0)
-    ppos = -(ppos + *v);
-  else
-    ppos += *v;
-
-  if (ppos > 360)
-    ppos -= 360;
-  else if (ppos < 0)
-    ppos += 360;
-
-  if (ppos < 0) abort();
-  if (ppos > 360) abort();
-  *pos = (*pos > 0 ? ppos : -ppos);
-
-  /* accelerate */
-  *v += *dv;
-
-  /* clamp velocity */
-  if (*v > max_v || *v < -max_v)
-    {
-      *dv = -*dv;
-    }
-  /* If it stops, start it going in the other direction. */
-  else if (*v < 0)
-    {
-      if (random() % 4)
-        {
-          *v = 0;
-
-          /* keep going in the same direction */
-          if (random() % 2)
-            *dv = 0;
-          else if (*dv < 0)
-            *dv = -*dv;
-        }
-      else
-        {
-          /* reverse gears */
-          *v = -*v;
-          *dv = -*dv;
-          *pos = -*pos;
-        }
-    }
-
-  /* Alter direction of rotational acceleration randomly. */
-  if (! (random() % 120))
-    *dv = -*dv;
-
-  /* Change acceleration very occasionally. */
-  if (! (random() % 200))
-    {
-      if (*dv == 0)
-        *dv = 0.00001;
-      else if (random() & 1)
-        *dv *= 1.2;
-      else
-        *dv *= 0.8;
-    }
-}
-
-
-static void
-bounce (float *pos, float *v, float *dv, float max_v)
-{
-  *pos += *v;
-
-  if (*pos > 1.0)
-    *pos = 1.0, *v = -*v, *dv = -*dv;
-  else if (*pos < 0)
-    *pos = 0, *v = -*v, *dv = -*dv;
-
-  if (*pos < 0.0) abort();
-  if (*pos > 1.0) abort();
-
-  /* accelerate */
-  *v += *dv;
-
-  /* clamp velocity */
-  if (*v > max_v || *v < -max_v)
-    {
-      *dv = -*dv;
-    }
-
-  /* Alter direction of rotational acceleration randomly. */
-  if (! (random() % 120))
-    *dv = -*dv;
-
-  /* Change acceleration very occasionally. */
-  if (! (random() % 200))
-    {
-      if (*dv == 0)
-        *dv = 0.00001;
-      else if (random() & 1)
-        *dv *= 1.2;
-      else
-        *dv *= 0.8;
-    }
-}
-
-
 static void
-init_rotation (void)
+init_rotation (ModeInfo *mi)
 {
-  rot_x = (float) (random() % (360 * 2)) - 360;  /* -360 - 360 */
-  rot_y = (float) (random() % (360 * 2)) - 360;
-  rot_z = (float) (random() % (360 * 2)) - 360;
-
-  /* bell curve from 0-1.5 degrees, avg 0.75 */
-  dx = (frand(1) + frand(1) + frand(1)) / 2.0;
-  dy = (frand(1) + frand(1) + frand(1)) / 2.0;
-  dz = (frand(1) + frand(1) + frand(1)) / 2.0;
-
-  d_max = dx * 2;
-
-  ddx = 0.004;
-  ddy = 0.004;
-  ddz = 0.004;
+  screensaverstruct *gp = &Screensaver[MI_SCREEN(mi)];
+  double spin_speed = 1.0;
+  gp->rot = make_rotator (spin_speed, spin_speed, spin_speed, 1.0, 0.0, True);
 
   lastx = (random() % (int) (max_lastx - min_lastx)) + min_lastx;
   lasty = (random() % (int) (max_lasty - min_lasty)) + min_lasty;
-  d_lastx = (frand(1) + frand(1) + frand(1));
-  d_lasty = (frand(1) + frand(1) + frand(1));
-  max_dlastx = d_lastx * 2;
-  max_dlasty = d_lasty * 2;
-  dd_lastx = 0.004;
-  dd_lasty = 0.004;
 }
 
 
@@ -469,11 +346,6 @@ void draw_screensaver(ModeInfo * mi)
   Display    *display = MI_DISPLAY(mi);
   Window      window = MI_WINDOW(mi);
 
-  Window root, child;
-  int rootx, rooty, winx, winy;
-  unsigned int mask;
-  XEvent event;
-
   if (!gp->glx_context)
        return;
 
@@ -481,44 +353,22 @@ void draw_screensaver(ModeInfo * mi)
 
   funcs_ptr[screensaver_number].DrawStuff();
          
-  rotate(&rot_x, &dx, &ddx, d_max);
-  rotate(&rot_y, &dy, &ddy, d_max);
-  rotate(&rot_z, &dz, &ddz, d_max);
-
-  /* swallow any ButtonPress events */
-  while (XCheckMaskEvent (MI_DISPLAY(mi), ButtonPressMask, &event))
-    ;
-  /* check the pointer position and button state. */
-  XQueryPointer (MI_DISPLAY(mi), MI_WINDOW(mi),
-                 &root, &child, &rootx, &rooty, &winx, &winy, &mask);
-
   /* track the mouse only if a button is down. */
-  if (mask & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask))
+  if (gp->button_down_p)
     {
-      lastx = winx;
-      lasty = winy;
+      lastx = gp->mouse_x;
+      lasty = gp->mouse_y;
     }
   else
     {
       float scale = (max_lastx - min_lastx);
-      lastx -= min_lastx;
-      lasty -= min_lasty;
-      lastx /= scale;
-      lasty /= scale;
-      d_lastx /= scale;
-      d_lasty /= scale;
-      dd_lastx /= scale;
-      dd_lasty /= scale;
-      bounce(&lastx, &d_lastx, &dd_lastx, max_dlastx);
-      bounce(&lasty, &d_lasty, &dd_lasty, max_dlasty);
-      lastx *= scale;
-      lasty *= scale;
-      lastx += min_lastx;
-      lasty += min_lasty;
-      d_lastx *= scale;
-      d_lasty *= scale;
-      dd_lastx *= scale;
-      dd_lasty *= scale;
+      double x, y, z;
+      get_rotation (gp->rot, &x, &y, &z, True);
+      rot_x = x * 360;
+      rot_y = y * 360;
+      rot_z = z * 360;
+      lastx = x * scale + min_lastx;
+      lasty = y * scale + min_lasty;
     }
 
   if (mi->fps_p) do_fps (mi);
@@ -564,7 +414,8 @@ reshape_screensaver(ModeInfo *mi, int width, int height)
 
 
 /* decide which screensaver example to run */
-static void chooseScreensaverExample(void) {
+static void chooseScreensaverExample(ModeInfo *mi)
+{
   int i;
   /* call the extrusion init routine */
 
@@ -587,7 +438,7 @@ static void chooseScreensaverExample(void) {
          fprintf(stderr,"\t%s\n", funcs_ptr[i].name);
        exit(1);
   }
-  init_rotation();
+  init_rotation(mi);
   funcs_ptr[screensaver_number].InitStuff();
 }
 
@@ -605,6 +456,7 @@ initializeGL(ModeInfo *mi, GLsizei width, GLsizei height)
   glClearColor(0,0,0,0);
 /*    glCullFace(GL_BACK); */
 /*    glEnable(GL_CULL_FACE); */
+  glLightModeli (GL_LIGHT_MODEL_TWO_SIDE, TRUE);
   glShadeModel(GL_SMOOTH);
 
   if (do_light)
@@ -634,6 +486,37 @@ initializeGL(ModeInfo *mi, GLsizei width, GLsizei height)
 
 }
 
+Bool
+screensaver_handle_event (ModeInfo *mi, XEvent *event)
+{
+  screensaverstruct *gp = &Screensaver[MI_SCREEN(mi)];
+
+  if (event->xany.type == ButtonPress &&
+      event->xbutton.button & Button1)
+    {
+      gp->button_down_p = True;
+      gp->mouse_x = event->xbutton.x;
+      gp->mouse_y = event->xbutton.y;
+      return True;
+    }
+  else if (event->xany.type == ButtonRelease &&
+           event->xbutton.button & Button1)
+    {
+      gp->button_down_p = False;
+      return True;
+    }
+  else if (event->xany.type == MotionNotify)
+    {
+      gp->mouse_x = event->xmotion.x;
+      gp->mouse_y = event->xmotion.y;
+      return True;
+    }
+
+  return False;
+}
+
+
+
 /* xscreensaver initialization routine */
 void init_screensaver(ModeInfo * mi)
 {
@@ -650,7 +533,7 @@ void init_screensaver(ModeInfo * mi)
   if ((gp->glx_context = init_GL(mi)) != NULL) {
        reshape_screensaver(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
        initializeGL(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
-       chooseScreensaverExample();
+       chooseScreensaverExample(mi);
   } else {
        MI_CLEARWINDOW(mi);
   }