From http://www.jwz.org/xscreensaver/xscreensaver-5.38.tar.gz
[xscreensaver] / hacks / glx / sonar.c
index 74e7b6b252c5d5fd8b1aae72b336ea21aa55a2cc..218a62269e1506e05a49697f789345869c0d8b52 100644 (file)
@@ -1,4 +1,4 @@
-/* sonar, Copyright (c) 1998-2014 Jamie Zawinski and Stephen Martin
+/* sonar, Copyright (c) 1998-2015 Jamie Zawinski and Stephen Martin
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
@@ -76,7 +76,7 @@
                        THREAD_DEFAULTS_XLOCK
 
 
-# define refresh_sonar 0
+# define release_sonar 0
 #undef countof
 #define countof(x) (sizeof((x))/sizeof((*x)))
 
@@ -370,7 +370,11 @@ draw_text (ModeInfo *mi, const char *string, GLfloat r, GLfloat th,
 
   while ((line = strtok (token, "\r\n")))
     {
-      int w = texture_string_width (sp->texfont, line, &lh);
+      XCharStruct e;
+      int w, ascent, descent;
+      texture_string_metrics (sp->texfont, line, &e, &ascent, &descent);
+      w = e.width;
+      lh = ascent + descent;
       if (w > max_w) max_w = w;
       lines++;
       token = 0;
@@ -403,7 +407,10 @@ draw_text (ModeInfo *mi, const char *string, GLfloat r, GLfloat th,
   token = string2;
   while ((line = strtok (token, "\r\n")))
     {
-      int w = texture_string_width (sp->texfont, line, 0);
+      XCharStruct e;
+      int w;
+      texture_string_metrics (sp->texfont, line, &e, 0, 0);
+      w = e.width;
       glPushMatrix();
       glTranslatef ((max_w-w)/2, 0, polys * 4); /* 'polys' stops Z-fighting. */
 
@@ -750,22 +757,22 @@ static void
 draw_startup_blurb (ModeInfo *mi)
 {
   sonar_configuration *sp = &sps[MI_SCREEN(mi)];
-  const char *msg = (sp->error ? sp->error : "Resolving hosts...");
-  static const GLfloat color[4] = {0, 1, 0, 1};
 
-  if (!sp->error && ping_arg && !strcmp (ping_arg, "simulation"))
-    return;  /* don't bother */
+  if (sp->error)
+    {
+      const char *msg = sp->error;
+      static const GLfloat color[4] = {0, 1, 0, 1};
 
-  glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
-  glTranslatef (0, 0, 0.3);
-  draw_text (mi, msg, 0, 0, 0, 30.0);
+      glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
+      glTranslatef (0, 0, 0.3);
+      draw_text (mi, msg, 0, 0, 0, 30.0);
 
-  /* only leave error message up for N seconds */
-  if (sp->error &&
-      sp->start_time + 6 < double_time())
-    {
-      free (sp->error);
-      sp->error = 0;
+      /* only leave error message up for N seconds */
+      if (sp->start_time + 6 < double_time())
+        {
+          free (sp->error);
+          sp->error = 0;
+        }
     }
 }
 
@@ -777,8 +784,15 @@ reshape_sonar (ModeInfo *mi, int width, int height)
 {
   sonar_configuration *sp = &sps[MI_SCREEN(mi)];
   GLfloat h = (GLfloat) height / (GLfloat) width;
+  int y = 0;
 
-  glViewport (0, 0, (GLint) width, (GLint) height);
+  if (width > height * 5) {   /* tiny window: show middle */
+    height = width * 9/16;
+    y = -height/2;
+    h = height / (GLfloat) width;
+  }
+
+  glViewport (0, y, (GLint) width, (GLint) height);
 
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
@@ -809,20 +823,12 @@ sonar_handle_event (ModeInfo *mi, XEvent *event)
   return False;
 }
 
-
 ENTRYPOINT void 
 init_sonar (ModeInfo *mi)
 {
   sonar_configuration *sp;
 
-  if (!sps) {
-    sps = (sonar_configuration *)
-      calloc (MI_NUM_SCREENS(mi), sizeof (sonar_configuration));
-    if (!sps) {
-      fprintf(stderr, "%s: out of memory\n", progname);
-      exit(1);
-    }
-  }
+  MI_INIT (mi, sps);
   sp = &sps[MI_SCREEN(mi)];
   sp->glx_context = init_GL(mi);
 
@@ -919,16 +925,15 @@ init_sensor (ModeInfo *mi)
   if (!ping_arg || !*ping_arg ||
       !strcmp(ping_arg, "default") ||
       !!strcmp (ping_arg, "simulation"))
+    /* sonar_init_ping() always disavows privs, even on failure. */
     sp->ssd = sonar_init_ping (MI_DISPLAY (mi), &sp->error, &sp->desc,
                                ping_arg, ping_timeout, resolve_p, times_p,
                                debug_p);
+  else
+    setuid(getuid()); /* Disavow privs if not pinging. */
 
   sp->start_time = double_time ();  /* for error message timing */
 
-  /* Disavow privs.  This was already done in init_ping(), but
-     we might not have called that at all, so do it again. */
-  setuid(getuid());
-
   if (!sp->ssd)
     sp->ssd = sonar_init_simulation (MI_DISPLAY (mi), &sp->error, &sp->desc,
                                      team_a_name, team_b_name,
@@ -1218,7 +1223,7 @@ draw_sonar (ModeInfo *mi)
 }
 
 ENTRYPOINT void
-release_sonar (ModeInfo *mi)
+free_sonar (ModeInfo *mi)
 {
 #if 0
   sonar_configuration *sp = &sps[MI_SCREEN(mi)];