http://packetstormsecurity.org/UNIX/admin/xscreensaver-3.34.tar.gz
[xscreensaver] / driver / prefs.c
index 71ed252aa5741793d25d6a46126366f74429b6b2..c074d1f4c2cb509bc043f80a0a1ee41ba1c4bba2 100644 (file)
@@ -23,6 +23,7 @@
 #include <stdio.h>
 #include <ctype.h>
 #include <string.h>
+#include <time.h>
 #include <sys/stat.h>
 #include <sys/time.h>
 
@@ -192,6 +193,39 @@ init_file_tmp_name (void)
     return 0;
 }
 
+static int
+get_byte_resource (char *name, char *class)
+{
+  char *s = get_string_resource (name, class);
+  char *s2 = s;
+  int n = 0;
+  if (!s) return 0;
+
+  while (isspace(*s2)) s2++;
+  while (*s2 >= '0' && *s2 <= '9')
+    {
+      n = (n * 10) + (*s2 - '0');
+      s2++;
+    }
+  while (isspace(*s2)) s2++;
+  if      (*s2 == 'k' || *s2 == 'K') n <<= 10;
+  else if (*s2 == 'm' || *s2 == 'M') n <<= 20;
+  else if (*s2 == 'g' || *s2 == 'G') n <<= 30;
+  else if (*s2)
+    {
+    LOSE:
+      fprintf (stderr, "%s: %s must be a number of bytes, not \"%s\".\n",
+               progname, name, s);
+      return 0;
+    }
+  s2++;
+  if (*s2 == 'b' || *s2 == 'B') s2++;
+  while (isspace(*s2)) s2++;
+  if (*s2) goto LOSE;
+
+  return n;
+}
+
 
 static const char * const prefs[] = {
   "timeout",
@@ -211,6 +245,7 @@ static const char * const prefs[] = {
   "helpURL",
   "loadURL",
   "nice",
+  "memoryLimit",
   "fade",
   "unfade",
   "fadeSeconds",
@@ -222,6 +257,10 @@ static const char * const prefs[] = {
   "dpmsStandby",
   "dpmsSuspend",
   "dpmsOff",
+  "grabDesktopImages",
+  "grabVideoFrames",
+  "chooseRandomImages",
+  "imageDirectory",
   "",
   "programs",
   "",
@@ -495,6 +534,9 @@ write_entry (FILE *out, const char *key, const char *value)
   fprintf(out, "%s:", key);
   col = strlen(key) + 1;
 
+  if (strlen(key) > 14)
+    col = tab_to (out, col, 20);
+
   while (1)
     {
       if (!programs_p)
@@ -680,7 +722,7 @@ write_init_file (saver_preferences *p, const char *version_string,
     {
       char buf[255];
       const char *pr = prefs[j];
-      enum pref_type { pref_str, pref_int, pref_bool, pref_time
+      enum pref_type { pref_str, pref_int, pref_bool, pref_byte, pref_time
       } type = pref_str;
       const char *s = 0;
       int i = 0;
@@ -717,6 +759,7 @@ write_init_file (saver_preferences *p, const char *version_string,
       CHECK("helpURL")         type = pref_str,  s = p->help_url;
       CHECK("loadURL")         type = pref_str,  s = p->load_url_command;
       CHECK("nice")            type = pref_int,  i = p->nice_inferior;
+      CHECK("memoryLimit")     type = pref_byte, i = p->inferior_memory_limit;
       CHECK("fade")            type = pref_bool, b = p->fade_p;
       CHECK("unfade")          type = pref_bool, b = p->unfade_p;
       CHECK("fadeSeconds")     type = pref_time, t = p->fade_seconds;
@@ -724,10 +767,17 @@ write_init_file (saver_preferences *p, const char *version_string,
       CHECK("captureStderr")   type = pref_bool, b = p->capture_stderr_p;
       CHECK("captureStdout")   continue;  /* don't save */
       CHECK("font")            type = pref_str,  s =    stderr_font;
+
       CHECK("dpmsEnabled")     type = pref_bool, b = p->dpms_enabled_p;
       CHECK("dpmsStandby")     type = pref_time, t = p->dpms_standby;
       CHECK("dpmsSuspend")     type = pref_time, t = p->dpms_suspend;
       CHECK("dpmsOff")         type = pref_time, t = p->dpms_off;
+
+      CHECK("grabDesktopImages") type =pref_bool, b = p->grab_desktop_p;
+      CHECK("grabVideoFrames")   type =pref_bool, b = p->grab_video_p;
+      CHECK("chooseRandomImages")type =pref_bool, b = p->random_image_p;
+      CHECK("imageDirectory")    type =pref_str,  s = p->image_directory;
+
       CHECK("programs")                type = pref_str,  s =    programs;
       CHECK("pointerPollTime") type = pref_time, t = p->pointer_timeout;
       CHECK("windowCreationTimeout")type=pref_time,t= p->notice_events_timeout;
@@ -771,6 +821,19 @@ write_init_file (saver_preferences *p, const char *version_string,
            s = buf;
          }
          break;
+       case pref_byte:
+         {
+            if      (i >= (1<<30) && i == ((i >> 30) << 30))
+              sprintf(buf, "%dG", i >> 30);
+            else if (i >= (1<<20) && i == ((i >> 20) << 20))
+              sprintf(buf, "%dM", i >> 20);
+            else if (i >= (1<<10) && i == ((i >> 10) << 10))
+              sprintf(buf, "%dK", i >> 10);
+            else
+              sprintf(buf, "%d", i);
+            s = buf;
+          }
+         break;
        default:
          abort();
          break;
@@ -910,6 +973,7 @@ load_init_file (saver_preferences *p)
   p->fade_ticks            = get_integer_resource ("fadeTicks", "Integer");
   p->install_cmap_p = get_boolean_resource ("installColormap", "Boolean");
   p->nice_inferior  = get_integer_resource ("nice", "Nice");
+  p->inferior_memory_limit = get_byte_resource ("memoryLimit", "MemoryLimit");
   p->splash_p       = get_boolean_resource ("splash", "Boolean");
   p->capture_stderr_p = get_boolean_resource ("captureStderr", "Boolean");
 
@@ -924,9 +988,15 @@ load_init_file (saver_preferences *p)
                                                       "Time");
 
   p->dpms_enabled_p  = get_boolean_resource ("dpmsEnabled", "Boolean");
-  p->dpms_standby    = 1000 * get_seconds_resource ("dpmsStandby", "Time");
-  p->dpms_suspend    = 1000 * get_seconds_resource ("dpmsSuspend", "Time");
-  p->dpms_off        = 1000 * get_seconds_resource ("dpmsOff",     "Time");
+  p->dpms_standby    = 1000 * get_minutes_resource ("dpmsStandby", "Time");
+  p->dpms_suspend    = 1000 * get_minutes_resource ("dpmsSuspend", "Time");
+  p->dpms_off        = 1000 * get_minutes_resource ("dpmsOff",     "Time");
+
+  p->grab_desktop_p  = get_boolean_resource ("grabDesktopImages",  "Boolean");
+  p->grab_video_p    = get_boolean_resource ("grabVideoFrames",    "Boolean");
+  p->random_image_p  = get_boolean_resource ("chooseRandomImages", "Boolean");
+  p->image_directory = get_string_resource  ("imageDirectory",
+                                             "ImageDirectory");
 
   p->shell = get_string_resource ("bourneShell", "BourneShell");
 
@@ -945,6 +1015,15 @@ load_init_file (saver_preferences *p)
       p->splash_p = True;
   }
 
+  /* If "*grabDesktopImages" is unset, default to true. */
+  {
+    char *s = get_string_resource ("grabDesktopImages", "Boolean");
+    if (s)
+      free (s);
+    else
+      p->grab_desktop_p = True;
+  }
+
   p->use_xidle_extension = get_boolean_resource ("xidleExtension","Boolean");
   p->use_mit_saver_extension = get_boolean_resource ("mitSaverExtension",
                                                     "Boolean");
@@ -964,12 +1043,25 @@ load_init_file (saver_preferences *p)
     p->fade_p = False;
   if (! p->fade_p) p->unfade_p = False;
 
-  if (p->dpms_standby <= 0 || p->dpms_suspend <= 0 || p->dpms_off <= 0)
+  /* The DPMS settings may have the value 0.
+     But if they are negative, or are a range less than 10 seconds,
+     reset them to sensible defaults.  (Since that must be a mistake.)
+   */
+  if (p->dpms_standby != 0 &&
+      p->dpms_standby < 10 * 1000)
+    p->dpms_standby =  2 * 60 * 60 * 1000;                      /* 2 hours */
+  if (p->dpms_suspend != 0 &&
+      p->dpms_suspend < 10 * 1000)
+    p->dpms_suspend =  2 * 60 * 60 * 1000;                      /* 2 hours */
+  if (p->dpms_off != 0 &&
+      p->dpms_off < 10 * 1000)
+    p->dpms_off      = 4 * 60 * 60 * 1000;                      /* 4 hours */
+
+  if (p->dpms_standby == 0 &&     /* if *all* are 0, then DPMS is disabled */
+      p->dpms_suspend == 0 &&
+      p->dpms_off     == 0)
     p->dpms_enabled_p = False;
 
-  if (p->dpms_standby <= 10000) p->dpms_standby = 10000;        /* 10 secs */
-  if (p->dpms_suspend <= 10000) p->dpms_suspend = 10000;        /* 10 secs */
-  if (p->dpms_off     <= 10000) p->dpms_off     = 10000;        /* 10 secs */
 
   p->watchdog_timeout = p->cycle * 0.6;
   if (p->watchdog_timeout < 30000) p->watchdog_timeout = 30000;          /* 30 secs */
@@ -1049,7 +1141,7 @@ merge_system_screenhacks (saver_preferences *p,
               made_space = 10;
               p->screenhacks = (screenhack **)
                 realloc (p->screenhacks,
-                         (p->screenhacks_count + made_space
+                         (p->screenhacks_count + made_space + 1)
                          * sizeof(screenhack));
               if (!p->screenhacks) abort();
             }
@@ -1060,6 +1152,7 @@ merge_system_screenhacks (saver_preferences *p,
           nh->command   = oh->command ? strdup(oh->command) : 0;
 
           p->screenhacks[p->screenhacks_count++] = nh;
+          p->screenhacks[p->screenhacks_count] = 0;
           made_space--;
 
 #if 0
@@ -1259,10 +1352,7 @@ format_hack (screenhack *hack, Bool wrap_p)
 
       col = string_columns (h2, strlen (h2), 0);
       if (wrap_p && col >= tab)
-        {
-          out = stab_to (out, col, 77);
-          *out += strlen(out);
-        }
+        out = stab_to (out, col, 77);
       else
         *out++ = ' ';
 
@@ -1287,7 +1377,7 @@ format_hack (screenhack *hack, Bool wrap_p)
 static void
 get_screenhacks (saver_preferences *p)
 {
-  int i = 0;
+  int i, j;
   int start = 0;
   int end = 0;
   int size;
@@ -1320,15 +1410,14 @@ get_screenhacks (saver_preferences *p)
 
 
   /* Count up the number of newlines (which will be equal to or larger than
-     the number of hacks.)
+     one less than the number of hacks.)
    */
-  i = 0;
-  for (i = 0; d[i]; i++)
+  for (i = j = 0; d[i]; i++)
     if (d[i] == '\n')
-      i++;
-  i++;
+      j++;
+  j++;
 
-  p->screenhacks = (screenhack **) calloc (sizeof (screenhack *), i+1);
+  p->screenhacks = (screenhack **) calloc (j + 1, sizeof (screenhack *));
 
   /* Iterate over the lines in `d' (the string with newlines)
      and make new strings to stuff into the `screenhacks' array.