home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume3 / xsaver / part02 / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-10  |  4.6 KB  |  236 lines

  1. /*
  2.  * $Source: /mit/sipbsrc/uus/src/xscreensaver/RCS/util.c,v $
  3.  * $Author: jik $
  4.  *
  5.  * This file is part of xscreensaver.  It contains a whole bunch of
  6.  * utility procedures.
  7.  *
  8.  * Author: Jonathan Kamens, MIT Project Athena and
  9.  *                          MIT Student Information Processing Board
  10.  *
  11.  * Copyright (c) 1989 by Jonathan Kamens.  This code may be
  12.  * distributed freely as long as this notice is kept intact in its
  13.  * entirety and every effort is made to send all corrections and
  14.  * improvements to the code back to the author.  Also, don't try to
  15.  * make any money off of it or pretend that you wrote it.
  16.  */
  17.  
  18. #ifndef lint
  19. static char rcsid_util_c[] = "$Header: util.c,v 1.4 89/02/28 06:55:28 jik Exp $";
  20. #endif
  21.  
  22. #include <X11/Intrinsic.h>
  23. #include <X11/StringDefs.h>
  24. #include <X11/Form.h>
  25. #include <pwd.h>
  26. #include "xsaver.h"
  27. #include "globals.h"
  28.  
  29.  
  30. extern char *sprintf(), *getenv();
  31.  
  32. char *get_user();
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40. /* Returns the width of the widget passed in as an argument */
  41. Dimension widget_width(w)
  42. Widget w;
  43. {
  44.      Arg arglist[1];
  45.      Dimension width;
  46.      
  47.      XtSetArg(arglist[0], XtNwidth, &width);
  48.      XtGetValues(w, arglist, 1);
  49.  
  50.      return(width);
  51. }
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  /* Useed with children of a form widget -- given the widget, its width */
  58.  /* and the horizontal width in which to center it, this function will  */
  59.  /* set the XtNhorizDistance value of the widget properly.              */
  60. center (w, width, in_width)
  61. Widget w;
  62. Dimension width, in_width;
  63. {
  64.      Arg arglist[1];
  65.  
  66.      width = (in_width - width) / 2;
  67.      XtSetArg(arglist[0], XtNhorizDistance, width);
  68.      XtSetValues(w, arglist, 1);
  69. }
  70.  
  71.  
  72.  
  73.  
  74. /* Returns a blank cursor */
  75. Cursor blank_cursor()
  76. {
  77.      Pixmap foo;
  78.      Cursor bar;
  79.      XColor baz;
  80.  
  81.      foo = XCreatePixmap(dpy, RootWindow(dpy, screen), 1, 1, 1);
  82.      baz.red = baz.blue = baz.green = 0;
  83.      bar = XCreatePixmapCursor(dpy, foo, foo, &baz, &baz, 1, 1);
  84.      return(bar);
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
  91. char *user_string(str)
  92. char *str;
  93. {
  94.      static char buf[100];
  95.      static int initialized = 0;
  96.  
  97.      if (! initialized) {
  98.       sprintf(buf, str, get_user());
  99.      }
  100.      return(buf);
  101. }
  102.  
  103.  
  104. char *time_string(str, force)
  105. ForceType force;
  106. char *str;
  107. {
  108.      struct tm *local;
  109.      static char buf[100];
  110.      char new[100];
  111.      int newf = 0;
  112.      
  113.      local = localtime((long) ×.current);
  114.      sprintf(new, str, local->tm_hour, local->tm_min);
  115.      if (strcmp(new, buf)) {
  116.       strcpy(buf, new);
  117.       newf++;
  118.      }
  119.      if (newf || (force == Force))
  120.       return(buf);
  121.      else
  122.       return ((char *) NULL);
  123. }     
  124.  
  125.  
  126.  
  127. char *elapsed_string(str, force)
  128. ForceType force;
  129. char *str;
  130. {
  131.      static char buf[100];
  132.      int hours, minutes;
  133.      char new[100];
  134.      int newf = 0;
  135.  
  136.      int elapsed_sec = times.current - times.start;
  137.      hours = elapsed_sec / 3600;
  138.      minutes = (elapsed_sec - 3600 * hours) / 60;
  139.      sprintf(new, str, hours, minutes);
  140.      if (strcmp(new, buf)) {
  141.       strcpy(buf, new);
  142.       newf++;
  143.      }
  144.      if (newf || (force == Force))
  145.       return(buf);
  146.      else
  147.       return((char *) NULL);
  148. }
  149.  
  150.  
  151. char *timeout_string(str, force)
  152. ForceType force;
  153. char *str;
  154. {
  155.      static char buf[100];
  156.      int hours, minutes;
  157.      char new[100];
  158.      int newf = 0;
  159.      
  160.      hours = defs.timeout / 60;
  161.      minutes = defs.timeout - 60 * hours;
  162.      sprintf(new, str, hours, (hours == 1 ? "hour" : "hours"), 
  163.          minutes, (minutes == 1 ? "minute" : "minutes"));
  164.      if (strcmp(new, buf)) {
  165.       strcpy(buf, new);
  166.       newf++;
  167.      }
  168.  
  169.      if (newf || (force == Force))
  170.       return(buf);
  171.      else
  172.       return((char *) NULL);
  173. }
  174.  
  175.  
  176. char *timeleft_string(str, force)
  177. ForceType force;
  178. char *str;
  179. {
  180.      static char buf[100];
  181.      int hours, minutes;
  182.      int elapsed_sec = times.current - times.start;
  183.      char new[100];
  184.      int newf = 0;
  185.  
  186.      /* Note that the + 60 in both equations is so that the counter will
  187.       * always display the minute it is counting down, instead of the
  188.       * minute before it.
  189.       */
  190.      hours = (defs.timeout * 60 - elapsed_sec + 60) / 3600;
  191.      minutes = (defs.timeout * 60 - elapsed_sec + 60 - 3600 * hours) / 60;
  192.      sprintf(new, str, hours, (hours == 1 ? "hour" : "hours"),
  193.          minutes, (minutes == 1 ? "minute" : "minutes"));
  194.      if (strcmp(new, buf)) {
  195.       strcpy(buf, new);
  196.       newf++;
  197.      }
  198.  
  199.      if (newf || (force == Force))
  200.       return(buf);
  201.      else
  202.       return((char *) NULL);
  203. }
  204.  
  205.  
  206.  
  207.  
  208.  
  209. char *get_user()
  210. {
  211.      static char user[MAXUSERNAME];
  212.      struct passwd *pwent;
  213.      static char *ptr = (char *) NULL;
  214.      
  215.      if (ptr)
  216.       return(ptr);
  217.      
  218.      ptr = getenv("USER");
  219.  
  220.      if (ptr) {
  221.       ptr = strcpy(user, ptr);
  222.       return(user);
  223.      }
  224.  
  225.      pwent = getpwuid(getuid());
  226.  
  227.      if (pwent) {
  228.       ptr = strcpy(user, pwent->pw_name);
  229.       return(ptr);
  230.      }
  231.  
  232.      *user = '\0';
  233.      
  234.      return(user);
  235. }
  236.