From http://www.jwz.org/xscreensaver/xscreensaver-5.27.tar.gz
[xscreensaver] / hacks / glx / fliptext.c
index 1a0eb499e054ee609cb93e1087a3e3ac723e0a11..798c1c685ae0f5f6d6f2fc5ad818729d56f6f07e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * fliptext, Copyright (c) 2005 Jamie Zawinski <jwz@jwz.org>
+ * fliptext, Copyright (c) 2005-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
  * implied warranty.
  */
 
-#include <X11/Intrinsic.h>
-
-extern XtAppContext app;
-
-#define PROGCLASS      "FlipText"
-#define HACK_INIT      init_fliptext
-#define HACK_DRAW      draw_fliptext
-#define HACK_RESHAPE   reshape_fliptext
-#define fliptext_opts  xlockmore_opts
-
-#define DEF_PROGRAM    "xscreensaver-text --cols 0"  /* don't wrap */
-#define DEF_LINES      "8"
-#define DEF_FONT_SIZE  "20"
-#define DEF_COLUMNS    "80"
-#define DEF_ALIGN      "random"
-#define DEF_COLOR      "#00CCFF"
-#define DEF_SPEED       "1.0"
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif /* HAVE_CONFIG_H */
 
 /* Utopia 800 needs 64 512x512 textures (4096x4096 bitmap).
    Utopia 720 needs 16 512x512 textures (2048x2048 bitmap).
@@ -36,18 +22,17 @@ extern XtAppContext app;
    Times  240 needs  1 512x512 texture.
  */
 #define DEF_FONT       "-*-utopia-bold-r-normal-*-*-720-*-*-*-*-iso8859-1"
-
-#define TAB_WIDTH        8
-
-#define FONT_WEIGHT       14
-#define KEEP_ASPECT
+#define DEF_COLOR      "#00CCFF"
 
 #define DEFAULTS "*delay:        10000      \n" \
                 "*showFPS:      False      \n" \
                 "*wireframe:    False      \n" \
+                "*usePty:       False      \n" \
                 "*font:       " DEF_FONT  "\n" \
                 ".foreground: " DEF_COLOR "\n" \
 
+# define refresh_fliptext 0
+# define fliptext_handle_event 0
 #undef countof
 #define countof(x) (sizeof((x))/sizeof((*x)))
 
@@ -55,17 +40,30 @@ extern XtAppContext app;
 #define BELLRAND(n) ((frand((n)) + frand((n)) + frand((n))) / 3)
 
 #include "xlockmore.h"
+#include "texfont.h"
+#include "textclient.h"
 
 #ifdef USE_GL /* whole file */
 
-#include <ctype.h>
-#include <GL/glu.h>
-#include <sys/stat.h>
-#include "texfont.h"
+/* Should be in <GL/glext.h> */
+# ifndef  GL_TEXTURE_MAX_ANISOTROPY_EXT
+#  define GL_TEXTURE_MAX_ANISOTROPY_EXT     0x84FE
+# endif
+# ifndef  GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT
+#  define GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF
+# endif
+
+
+#define DEF_PROGRAM    "xscreensaver-text --cols 0"  /* don't wrap */
+#define DEF_LINES      "8"
+#define DEF_FONT_SIZE  "20"
+#define DEF_COLUMNS    "80"
+#define DEF_ALIGNMENT  "random"
+#define DEF_SPEED       "1.0"
+#define TAB_WIDTH        8
 
-#ifdef HAVE_UNAME
-# include <sys/utsname.h>
-#endif /* HAVE_UNAME */
+#define FONT_WEIGHT       14
+#define KEEP_ASPECT
 
 typedef enum { NEW, HESITATE, IN, LINGER, OUT, DEAD } line_state;
 typedef enum { SCROLL_BOTTOM, SCROLL_TOP, SPIN } line_anim_type;
@@ -89,13 +87,11 @@ typedef struct {
 
 
 typedef struct {
+  Display *dpy;
   GLXContext *glx_context;
 
   texture_font_data *texfont;
-
-  FILE *pipe;
-  XtInputId pipe_id;
-  Time subproc_relaunch_delay;
+  text_data *tc;
 
   char *buf;
   int buf_size;
@@ -139,6 +135,7 @@ static XrmOptionDescRec opts[] = {
   {"-columns",    ".columns",   XrmoptionSepArg, 0 },
   {"-speed",       ".speed",     XrmoptionSepArg, 0 },
 /*{"-font",        ".font",      XrmoptionSepArg, 0 },*/
+  {"-alignment",   ".alignment", XrmoptionSepArg, 0 },
   {"-left",        ".alignment", XrmoptionNoArg,  "Left"   },
   {"-right",       ".alignment", XrmoptionNoArg,  "Right"  },
   {"-center",      ".alignment", XrmoptionNoArg,  "Center" },
@@ -149,11 +146,11 @@ static argtype vars[] = {
   {&max_lines,      "lines",     "Integer",    DEF_LINES,     t_Int},
   {&font_size,      "fontSize",  "Float",      DEF_FONT_SIZE, t_Float},
   {&target_columns, "columns",   "Integer",    DEF_COLUMNS,   t_Int},
-  {&alignment_str,  "alignment", "Alignment",  DEF_ALIGN,     t_String},
+  {&alignment_str,  "alignment", "Alignment",  DEF_ALIGNMENT, t_String},
   {&speed,         "speed",     "Speed",      DEF_SPEED,     t_Float},
 };
 
-ModeSpecOpt fliptext_opts = {countof(opts), opts, countof(vars), vars, NULL};
+ENTRYPOINT ModeSpecOpt fliptext_opts = {countof(opts), opts, countof(vars), vars, NULL};
 
 
 
@@ -218,102 +215,6 @@ strip (char *s, Bool leading, Bool trailing)
 }
 
 
-\f
-/* Subprocess.
-   (This bit mostly cribbed from phosphor.c)
- */
-
-static void drain_input (fliptext_configuration *sc);
-
-static void
-subproc_cb (XtPointer closure, int *source, XtInputId *id)
-{
-  fliptext_configuration *sc = (fliptext_configuration *) closure;
-  drain_input (sc);
-}
-
-
-static void
-launch_text_generator (fliptext_configuration *sc)
-{
-  char *oprogram = get_string_resource ("program", "Program");
-  char *program = (char *) malloc (strlen (oprogram) + 10);
-  strcpy (program, "( ");
-  strcat (program, oprogram);
-  strcat (program, " ) 2>&1");
-
-  if ((sc->pipe = popen (program, "r")))
-    {
-      sc->pipe_id =
-        XtAppAddInput (app, fileno (sc->pipe),
-                       (XtPointer) (XtInputReadMask | XtInputExceptMask),
-                       subproc_cb, (XtPointer) sc);
-    }
-  else
-    {
-      perror (program);
-    }
-}
-
-
-static void
-relaunch_generator_timer (XtPointer closure, XtIntervalId *id)
-{
-  fliptext_configuration *sc = (fliptext_configuration *) closure;
-  launch_text_generator (sc);
-}
-
-
-/* When the subprocess has generated some output, this reads as much as it
-   can into sc->buf at sc->buf_tail.
- */
-static void
-drain_input (fliptext_configuration *sc)
-{
-  if (sc->buf_tail < sc->buf_size - 2)
-    {
-      int target = sc->buf_size - sc->buf_tail - 2;
-      int n = (sc->pipe
-               ? read (fileno (sc->pipe),
-                       (void *) (sc->buf + sc->buf_tail),
-                       target)
-               : 0);
-      if (n > 0)
-        {
-          sc->buf_tail += n;
-          sc->buf[sc->buf_tail] = 0;
-        }
-      else
-        {
-          if (sc->pipe)
-            {
-              XtRemoveInput (sc->pipe_id);
-              sc->pipe_id = 0;
-              pclose (sc->pipe);
-              sc->pipe = 0;
-            }
-
-          /* If the process didn't print a terminating newline, add one. */
-          if (sc->buf_tail > 1 &&
-              sc->buf[sc->buf_tail-1] != '\n')
-            {
-              sc->buf[sc->buf_tail++] = '\n';
-              sc->buf[sc->buf_tail] = 0;
-            }
-
-          /* Then add one more, just for giggles. */
-          sc->buf[sc->buf_tail++] = '\n';
-          sc->buf[sc->buf_tail] = 0;
-
-          /* Set up a timer to re-launch the subproc in a bit. */
-          XtAppAddTimeOut (app, sc->subproc_relaunch_delay,
-                           relaunch_generator_timer,
-                           (XtPointer) sc);
-        }
-    }
-}
-
-
 static int
 char_width (fliptext_configuration *sc, char c)
 {
@@ -335,8 +236,23 @@ get_one_line (fliptext_configuration *sc)
   int wrap_pix = sc->font_wrap_pixels;
   int col = 0;
   int col_pix = 0;
-
   char *s = sc->buf;
+  int target = sc->buf_size - sc->buf_tail - 2;
+
+  /* Fill as much as we can into sc->buf, but stop at newline.
+   */
+  while (target > 0)
+    {
+      int c = textclient_getc (sc->tc);
+      if (c <= 0)
+        break;
+      sc->buf[sc->buf_tail++] = (char) c;
+      sc->buf[sc->buf_tail] = 0;
+      target--;
+      if (c == '\r' || c == '\n')
+        break;
+    }
+
   while (!result)
     {
       int cw;
@@ -846,7 +762,7 @@ parse_color (ModeInfo *mi, const char *name, const char *s, GLfloat *a)
 
 /* Window management, etc
  */
-void
+ENTRYPOINT void
 reshape_fliptext (ModeInfo *mi, int width, int height)
 {
   fliptext_configuration *sc = &scs[MI_SCREEN(mi)];
@@ -871,7 +787,7 @@ reshape_fliptext (ModeInfo *mi, int width, int height)
 }
 
 
-void 
+ENTRYPOINT void 
 init_fliptext (ModeInfo *mi)
 {
   int wire = MI_IS_WIREFRAME(mi);
@@ -891,12 +807,14 @@ init_fliptext (ModeInfo *mi)
   }
 
   sc = &scs[MI_SCREEN(mi)];
+  sc->dpy = MI_DISPLAY(mi);
 
   if ((sc->glx_context = init_GL(mi)) != NULL) {
     reshape_fliptext (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
+    clear_gl_error(); /* WTF? sometimes "invalid op" from glViewport! */
   }
 
-  program = get_string_resource ("program", "Program");
+  program = get_string_resource (mi->dpy, "program", "Program");
 
   {
     int cw, lh;
@@ -914,15 +832,20 @@ init_fliptext (ModeInfo *mi)
       glEnable (GL_ALPHA_TEST);
       glEnable (GL_TEXTURE_2D);
 
-# ifdef GL_TEXTURE_MAX_ANISOTROPY_EXT
       /* "Anistropic filtering helps for quadrilateral-angled textures.
          A sharper image is accomplished by interpolating and filtering
          multiple samples from one or more mipmaps to better approximate
          very distorted textures.  This is the next level of filtering
          after trilinear filtering." */
-      glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16);
-      clear_gl_error();
-# endif
+      if (strstr ((char *) glGetString(GL_EXTENSIONS),
+                  "GL_EXT_texture_filter_anisotropic"))
+      {
+        GLfloat anisotropic = 0.0;
+        glGetFloatv (GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &anisotropic);
+        if (anisotropic >= 1.0)
+          glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 
+                           anisotropic);
+      }
     }
   
   /* The default font is (by fiat) "18 points".
@@ -947,8 +870,6 @@ init_fliptext (ModeInfo *mi)
   sc->buf_size = target_columns * max_lines;
   sc->buf = (char *) calloc (1, sc->buf_size);
 
-  sc->subproc_relaunch_delay = 2 * 1000;   /* 2 seconds */
-
   alignment_random_p = False;
   if (!alignment_str || !*alignment_str ||
       !strcasecmp(alignment_str, "left"))
@@ -969,7 +890,7 @@ init_fliptext (ModeInfo *mi)
       exit (1);
     }
 
-  launch_text_generator (sc);
+  sc->tc = textclient_open (sc->dpy);
 
   if (max_lines < 1) max_lines = 1;
   min_lines = max_lines * 0.66;
@@ -977,7 +898,7 @@ init_fliptext (ModeInfo *mi)
   if (min_lines < 1) min_lines = 1;
 
   parse_color (mi, "foreground",
-               get_string_resource("foreground", "Foreground"),
+               get_string_resource(mi->dpy, "foreground", "Foreground"),
                sc->color);
 
   sc->top_margin = (sc->char_width * 100);
@@ -986,10 +907,11 @@ init_fliptext (ModeInfo *mi)
 }
 
 
-void
+ENTRYPOINT void
 draw_fliptext (ModeInfo *mi)
 {
   fliptext_configuration *sc = &scs[MI_SCREEN(mi)];
+/*  XtAppContext app = XtDisplayToApplicationContext (sc->dpy);*/
   Display *dpy = MI_DISPLAY(mi);
   Window window = MI_WINDOW(mi);
   int i;
@@ -997,14 +919,19 @@ draw_fliptext (ModeInfo *mi)
   if (!sc->glx_context)
     return;
 
+  glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sc->glx_context));
+
+#if 0
   if (XtAppPending (app) & (XtIMTimer|XtIMAlternateInput))
     XtAppProcessEvent (app, XtIMTimer|XtIMAlternateInput);
+#endif
 
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
   mi->polygon_count = 0;
 
   glPushMatrix();
+  glRotatef(current_device_rotation(), 0, 0, 1);
   {
     GLfloat s = 3.0 / (sc->top_margin - sc->bottom_margin);
     glScalef(s, s, s);
@@ -1058,4 +985,24 @@ draw_fliptext (ModeInfo *mi)
   glXSwapBuffers(dpy, window);
 }
 
+ENTRYPOINT void
+release_fliptext (ModeInfo *mi)
+{
+  if (scs) {
+    int screen;
+    for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
+      fliptext_configuration *sc = &scs[screen];
+      if (sc->tc)
+        textclient_close (sc->tc);
+
+      /* #### there's more to free here */
+    }
+    free (scs);
+    scs = 0;
+  }
+  FreeAllGL(mi);
+}
+
+XSCREENSAVER_MODULE ("FlipText", fliptext)
+
 #endif /* USE_GL */