From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / polytopes.c
index f21246ed2131255c65003fe58d3feb8fd5b799bb..0b4d68481d7c7718d2f496075540fa26a5cb3168 100644 (file)
@@ -90,9 +90,11 @@ static const char sccsid[] = "@(#)polytopes.c  1.2 05/09/28 xlockmore";
 
 #ifdef STANDALONE
 # define DEFAULTS           "*delay:      25000 \n" \
-                            "*showFPS:    False \n"
+                            "*showFPS:    False \n" \
+                           "*suppressRotationAnimation: True\n" \
 
 # define refresh_polytopes 0
+# define release_polytopes 0
 # include "xlockmore.h"         /* from the xscreensaver distribution */
 #else  /* !STANDALONE */
 # include "xlock.h"             /* from the xlockmore distribution */
@@ -100,13 +102,15 @@ static const char sccsid[] = "@(#)polytopes.c  1.2 05/09/28 xlockmore";
 
 #ifdef USE_GL
 
-#include <X11/keysym.h>
+#ifndef HAVE_JWXYZ
+# include <X11/keysym.h>
+#endif
 #include "gltrackball.h"
 
 
 #ifdef USE_MODULES
 ModStruct   polytopes_description =
-{"polytopes", "init_polytopes", "draw_polytopes", "release_polytopes",
+{"polytopes", "init_polytopes", "draw_polytopes", NULL,
  "draw_polytopes", "change_polytopes", NULL, &polytopes_opts,
  25000, 1, 1, 1, 1.0, 4, "",
  "Shows one of the six regular 4d polytopes rotating in 4d", 0, NULL};
@@ -2769,11 +2773,6 @@ static void set_colors(ModeInfo *mi)
 
 static void init(ModeInfo *mi)
 {
-  static const GLfloat light_ambient[]  = { 0.0, 0.0, 0.0, 1.0 };
-  static const GLfloat light_diffuse[]  = { 1.0, 1.0, 1.0, 1.0 };
-  static const GLfloat light_specular[] = { 0.0, 0.0, 0.0, 1.0 };
-  static const GLfloat light_position[] = { 0.0, 0.0, 1.0, 0.0 };
-  static const GLfloat mat_specular[]   = { 1.0, 1.0, 1.0, 1.0 };
   polytopesstruct *pp = &poly[MI_SCREEN(mi)];
 
   set_colors(mi);
@@ -2787,62 +2786,6 @@ static void init(ModeInfo *mi)
 
   pp->tick = 0;
   pp->poly = 0;
-
-  glMatrixMode(GL_PROJECTION);
-  glLoadIdentity();
-  if (projection_3d == DISP_3D_PERSPECTIVE)
-    gluPerspective(60.0,1.0,0.1,100.0);
-  else
-    glOrtho(-1.0,1.0,-1.0,1.0,0.1,100.0);;
-  glMatrixMode(GL_MODELVIEW);
-  glLoadIdentity();
-
-  if (display_mode == DISP_WIREFRAME)
-  {
-    glDisable(GL_DEPTH_TEST);
-    glDisable(GL_LIGHTING);
-    glDisable(GL_LIGHT0);
-    glDisable(GL_BLEND);
-  }
-  else if (display_mode == DISP_SURFACE)
-  {
-    glEnable(GL_DEPTH_TEST);
-    glDepthFunc(GL_LESS);
-    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
-    glEnable(GL_LIGHTING);
-    glEnable(GL_LIGHT0);
-    glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
-    glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
-    glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
-    glLightfv(GL_LIGHT0,GL_POSITION,light_position);
-    glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
-    glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,0.0);
-    glDepthMask(GL_TRUE);
-    glDisable(GL_BLEND);
-  }
-  else if (display_mode == DISP_TRANSPARENT)
-  {
-    glDisable(GL_DEPTH_TEST);
-    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
-    glEnable(GL_LIGHTING);
-    glEnable(GL_LIGHT0);
-    glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
-    glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
-    glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
-    glLightfv(GL_LIGHT0,GL_POSITION,light_position);
-    glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
-    glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,0.0);
-    glDepthMask(GL_FALSE);
-    glEnable(GL_BLEND);
-    glBlendFunc(GL_SRC_ALPHA,GL_ONE);
-  }
-  else
-  {
-    glDisable(GL_DEPTH_TEST);
-    glDisable(GL_LIGHTING);
-    glDisable(GL_LIGHT0);
-    glDisable(GL_BLEND);
-  }
 }
 
 
@@ -2886,6 +2829,7 @@ static void display_polytopes(ModeInfo *mi)
   {
     gluPerspective(60.0,pp->aspect,0.1,100.0);
   }
+
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
 
@@ -2929,9 +2873,12 @@ ENTRYPOINT void reshape_polytopes(ModeInfo *mi, int width, int height)
 
 ENTRYPOINT Bool polytopes_handle_event(ModeInfo *mi, XEvent *event)
 {
-  Display *display = MI_DISPLAY(mi);
   polytopesstruct *pp = &poly[MI_SCREEN(mi)];
-  KeySym  sym;
+  KeySym  sym = 0;
+  char c = 0;
+
+  if (event->xany.type == KeyPress || event->xany.type == KeyRelease)
+    XLookupString (&event->xkey, &c, 1, &sym, 0);
 
   if (event->xany.type == ButtonPress &&
       event->xbutton.button == Button1)
@@ -2950,7 +2897,6 @@ ENTRYPOINT Bool polytopes_handle_event(ModeInfo *mi, XEvent *event)
   }
   else if (event->xany.type == KeyPress)
   {
-    sym = XKeycodeToKeysym(display,event->xkey.keycode,0);
     if (sym == XK_Shift_L || sym == XK_Shift_R)
     {
       pp->current_trackball = 1;
@@ -2963,7 +2909,6 @@ ENTRYPOINT Bool polytopes_handle_event(ModeInfo *mi, XEvent *event)
   }
   else if (event->xany.type == KeyRelease)
   {
-    sym = XKeycodeToKeysym(display,event->xkey.keycode,0);
     if (sym == XK_Shift_L || sym == XK_Shift_R)
     {
       pp->current_trackball = 0;
@@ -3004,17 +2949,11 @@ ENTRYPOINT void init_polytopes(ModeInfo *mi)
 {
   polytopesstruct *pp;
 
-  if (poly == NULL)
-  {
-    poly = (polytopesstruct *)calloc(MI_NUM_SCREENS(mi),
-                                     sizeof(polytopesstruct));
-    if (poly == NULL)
-      return;
-  }
+  MI_INIT(mi, poly, NULL);
   pp = &poly[MI_SCREEN(mi)];
 
-  pp->trackballs[0] = gltrackball_init();
-  pp->trackballs[1] = gltrackball_init();
+  pp->trackballs[0] = gltrackball_init(True);
+  pp->trackballs[1] = gltrackball_init(True);
   pp->current_trackball = 0;
   pp->button_pressed = False;
 
@@ -3134,6 +3073,12 @@ ENTRYPOINT void init_polytopes(ModeInfo *mi)
  */
 ENTRYPOINT void draw_polytopes(ModeInfo *mi)
 {
+  static const GLfloat light_ambient[]  = { 0.0, 0.0, 0.0, 1.0 };
+  static const GLfloat light_diffuse[]  = { 1.0, 1.0, 1.0, 1.0 };
+  static const GLfloat light_specular[] = { 0.0, 0.0, 0.0, 1.0 };
+  static const GLfloat light_position[] = { 0.0, 0.0, 1.0, 0.0 };
+  static const GLfloat mat_specular[]   = { 1.0, 1.0, 1.0, 1.0 };
+
   Display         *display = MI_DISPLAY(mi);
   Window          window = MI_WINDOW(mi);
   polytopesstruct *hp;
@@ -3148,6 +3093,64 @@ ENTRYPOINT void draw_polytopes(ModeInfo *mi)
 
   glXMakeCurrent(display,window,*(hp->glx_context));
 
+
+  glMatrixMode(GL_PROJECTION);
+  glLoadIdentity();
+  if (projection_3d == DISP_3D_PERSPECTIVE)
+    gluPerspective(60.0,1.0,0.1,100.0);
+  else
+    glOrtho(-1.0,1.0,-1.0,1.0,0.1,100.0);;
+  glMatrixMode(GL_MODELVIEW);
+  glLoadIdentity();
+
+  if (display_mode == DISP_WIREFRAME)
+  {
+    glDisable(GL_DEPTH_TEST);
+    glDisable(GL_LIGHTING);
+    glDisable(GL_LIGHT0);
+    glDisable(GL_BLEND);
+  }
+  else if (display_mode == DISP_SURFACE)
+  {
+    glEnable(GL_DEPTH_TEST);
+    glDepthFunc(GL_LESS);
+    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
+    glEnable(GL_LIGHTING);
+    glEnable(GL_LIGHT0);
+    glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
+    glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
+    glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
+    glLightfv(GL_LIGHT0,GL_POSITION,light_position);
+    glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
+    glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,0.0);
+    glDepthMask(GL_TRUE);
+    glDisable(GL_BLEND);
+  }
+  else if (display_mode == DISP_TRANSPARENT)
+  {
+    glDisable(GL_DEPTH_TEST);
+    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
+    glEnable(GL_LIGHTING);
+    glEnable(GL_LIGHT0);
+    glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
+    glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
+    glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
+    glLightfv(GL_LIGHT0,GL_POSITION,light_position);
+    glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
+    glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,0.0);
+    glDepthMask(GL_FALSE);
+    glEnable(GL_BLEND);
+    glBlendFunc(GL_SRC_ALPHA,GL_ONE);
+  }
+  else
+  {
+    glDisable(GL_DEPTH_TEST);
+    glDisable(GL_LIGHTING);
+    glDisable(GL_LIGHT0);
+    glDisable(GL_BLEND);
+  }
+
+
   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   glLoadIdentity();
 
@@ -3162,33 +3165,6 @@ ENTRYPOINT void draw_polytopes(ModeInfo *mi)
 }
 
 
-/*
- *-----------------------------------------------------------------------------
- *    The display is being taken away from us.  Free up malloc'ed 
- *      memory and X resources that we've alloc'ed.  Only called
- *      once, we must zap everything for every screen.
- *-----------------------------------------------------------------------------
- */
-
-ENTRYPOINT void release_polytopes(ModeInfo *mi)
-{
-  if (poly != NULL)
-  {
-    int screen;
-
-    for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
-    {
-      polytopesstruct *hp = &poly[screen];
-
-      if (hp->glx_context)
-        hp->glx_context = (GLXContext *)NULL;
-    }
-    (void) free((void *)poly);
-    poly = (polytopesstruct *)NULL;
-  }
-  FreeAllGL(mi);
-}
-
 #ifndef STANDALONE
 ENTRYPOINT void change_polytopes(ModeInfo *mi)
 {