http://packetstormsecurity.org/UNIX/admin/xscreensaver-4.14.tar.gz
[xscreensaver] / hacks / glx / starwars.c
index a6d5745caccce1c48704d5acb95cd1bef773cb81..5e41af2a7c8ab386ad8dfba8919fcf026b7258c0 100644 (file)
@@ -43,7 +43,7 @@ extern XtAppContext app;
 #define HACK_RESHAPE   reshape_sws
 #define sws_opts       xlockmore_opts
 
-#define DEF_PROGRAM    ZIPPY_PROGRAM
+#define DEF_PROGRAM    "(default)"
 #define DEF_LINES      "125"
 #define DEF_STEPS      "35"
 #define DEF_SPIN       "0.03"
@@ -87,11 +87,17 @@ extern XtAppContext app;
 
 #ifdef USE_GL /* whole file */
 
+#include <ctype.h>
 #include <GL/glu.h>
+#include <sys/stat.h>
 #include "glutstroke.h"
 #include "glut_roman.h"
 #define GLUT_FONT (&glutStrokeRoman)
 
+#ifdef HAVE_UNAME
+# include <sys/utsname.h>
+#endif /* HAVE_UNAME */
+
 
 typedef struct {
   GLXContext *glx_context;
@@ -253,8 +259,59 @@ static void
 launch_text_generator (sws_configuration *sc)
 {
   char *oprogram = get_string_resource ("program", "Program");
-  char *program = (char *) malloc (strlen (oprogram) + 10);
+  char *program;
+
+  if (!strcasecmp(oprogram, "(default)"))
+    {
+      oprogram = FORTUNE_PROGRAM;
+
+#ifdef __linux__
+      {
+        static int done_once = 0;
+        if (!done_once)
+          {
+            struct utsname uts;
+            struct stat st;
+            done_once = 1;
+            if (uname (&uts) == 0)
+              {
+                static char cmd[200];
+                char *s;
+                /* strip version at the first non-digit-dash-dot, to
+                   lose any "SMP" crap at the end. */
+                for (s = uts.release; *s; s++)
+                  if (!isdigit(*s) && *s != '.' && *s != '-')
+                    *s = 0;
+                sprintf (cmd, "cat /usr/src/linux-%s/README", uts.release);
+                if (!stat (cmd+4, &st))
+                  oprogram = cmd;
+                else
+                  {
+                    /* kernel source not installed?  try X... */
+                    strcpy (cmd, "cat /usr/X11R6/lib/X11/doc/README");
+                    if (!stat (cmd+4, &st))
+                      oprogram = cmd;
+                  }
+              }
+          }
+      }
+#endif /* __linux__ */
+
+#ifdef __APPLE__   /* MacOS X + XDarwin */
+      {
+        static int done_once = 0;
+        if (!done_once)
+          {
+            struct stat st;
+            static char *cmd = "cat /usr/X11R6/README";
+            if (!stat (cmd+4, &st))
+              oprogram = cmd;
+          }
+      }
+#endif /* __APPLE__ */
+    }
 
+  program = (char *) malloc (strlen (oprogram) + 10);
   strcpy (program, "( ");
   strcat (program, oprogram);
   strcat (program, " ) 2>&1");
@@ -342,12 +399,17 @@ get_more_lines (sws_configuration *sc)
           return;
         }
 
-      if (*s == '\n' || col > sc->columns)
+      if (*s == '\r' || *s == '\n' || col > sc->columns)
         {
           int L = s - sc->buf;
 
-          if (*s == '\n')
-            *s++ = 0;
+          if (*s == '\r' || *s == '\n')
+            {
+              if (*s == '\r' && s[1] == '\n')  /* swallow CRLF too */
+                *s++ = 0;
+
+              *s++ = 0;
+            }
           else
             {
               /* We wrapped -- try to back up to the previous word boundary. */
@@ -381,7 +443,7 @@ get_more_lines (sws_configuration *sc)
           if (sc->buf_tail > (s - sc->buf))
             {
               int i = sc->buf_tail - (s - sc->buf);
-              memcpy (sc->buf, s, i);
+              memmove (sc->buf, s, i);
               sc->buf_tail = i;
               sc->buf[sc->buf_tail] = 0;
             }
@@ -496,6 +558,41 @@ box (double width, double height, double depth)
 #endif /* DEBUG */
 
 
+/* Construct stars (number of stars is dependent on size of screen) */
+static void
+init_stars (ModeInfo *mi, int width, int height)
+{
+  sws_configuration *sc = &scs[MI_SCREEN(mi)];
+  int i, j;
+  int nstars = width * height / 320;
+  int max_size = 3;
+  GLfloat inc = 0.5;
+  int steps = max_size / inc;
+
+  glDeleteLists (sc->star_list, 1);
+  sc->star_list = glGenLists (1);
+  glNewList (sc->star_list, GL_COMPILE);
+
+  glEnable(GL_POINT_SMOOTH);
+
+  for (j = 1; j <= steps; j++)
+    {
+      glPointSize(inc * j);
+      glBegin (GL_POINTS);
+      for (i = 0; i < nstars / steps; i++)
+        {
+          glColor3f (0.6 + frand(0.3),
+                     0.6 + frand(0.3),
+                     0.6 + frand(0.3));
+          glVertex2f (2 * width  * (0.5 - frand(1.0)),
+                      2 * height * (0.5 - frand(1.0)));
+        }
+      glEnd ();
+    }
+  glEndList ();
+}
+
+
 /* Window management, etc
  */
 void
@@ -536,27 +633,6 @@ reshape_sws (ModeInfo *mi, int width, int height)
   }
 
 
-  /* Construct stars (number of stars is dependent on size of screen) */
-  {
-    int i;
-    int nstars = width * height / 320;
-    glDeleteLists (sc->star_list, 1);
-    sc->star_list = glGenLists (1);
-    glNewList (sc->star_list, GL_COMPILE);
-    glBegin (GL_POINTS);
-    for (i = 0; i < nstars; i++)
-      {
-        GLfloat c = 0.6 + 0.3 * random() / RAND_MAX;
-        glColor3f (c, c, c);
-        glVertex3f (2 * width  * (0.5 - 1.0 * random() / RAND_MAX),
-                    2 * height * (0.5 - 1.0 * random() / RAND_MAX),
-                    0.0);
-      }
-    glEnd ();
-    glEndList ();
-  }
-
-
   /* Compute the height in pixels of the line at the bottom of the screen. */
   {
     GLdouble mm[17], pm[17];
@@ -640,10 +716,10 @@ init_sws (ModeInfo *mi)
   if ((sc->glx_context = init_GL(mi)) != NULL) {
     gl_init(mi);
     reshape_sws (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
+    init_stars (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
   }
 
 
-
   font_height = GLUT_FONT->top - GLUT_FONT->bottom;
   sc->font_scale = 1.0 / glutStrokeWidth (GLUT_FONT, 'z');   /* 'n' seems
                                                                 too wide */
@@ -666,7 +742,6 @@ init_sws (ModeInfo *mi)
 
   sc->subproc_relaunch_delay = 2 * 1000;
   sc->total_lines = max_lines-1;
-  launch_text_generator (sc);
 
   if (random() & 1)
     star_spin = -star_spin;
@@ -686,6 +761,11 @@ init_sws (ModeInfo *mi)
                progname, alignment_str);
       exit (1);
     }
+
+  launch_text_generator (sc);
+
+  /* one more reshape, after line_height has been computed */
+  reshape_sws (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
 }
 
 
@@ -825,7 +905,9 @@ draw_sws (ModeInfo *mi)
             }
 
           if (alignment >= 0)
-            xoff = 1.0 - (glutStrokeLength(GLUT_FONT, line) * sc->font_scale);
+            xoff = 1.0 - (glutStrokeLength(GLUT_FONT,
+                                           (unsigned char *) line)
+                          * sc->font_scale);
           if (alignment == 0)
             xoff /= 2;