home *** CD-ROM | disk | FTP | other *** search
- /*
- * $Source: /mit/sipbsrc/uus/src/xscreensaver/RCS/util.c,v $
- * $Author: jik $
- *
- * This file is part of xscreensaver. It contains a whole bunch of
- * utility procedures.
- *
- * Author: Jonathan Kamens, MIT Project Athena and
- * MIT Student Information Processing Board
- *
- * Copyright (c) 1989 by Jonathan Kamens. This code may be
- * distributed freely as long as this notice is kept intact in its
- * entirety and every effort is made to send all corrections and
- * improvements to the code back to the author. Also, don't try to
- * make any money off of it or pretend that you wrote it.
- */
-
- #ifndef lint
- static char rcsid_util_c[] = "$Header: util.c,v 1.4 89/02/28 06:55:28 jik Exp $";
- #endif
-
- #include <X11/Intrinsic.h>
- #include <X11/StringDefs.h>
- #include <X11/Form.h>
- #include <pwd.h>
- #include "xsaver.h"
- #include "globals.h"
-
-
- extern char *sprintf(), *getenv();
-
- char *get_user();
-
-
-
-
-
-
-
- /* Returns the width of the widget passed in as an argument */
- Dimension widget_width(w)
- Widget w;
- {
- Arg arglist[1];
- Dimension width;
-
- XtSetArg(arglist[0], XtNwidth, &width);
- XtGetValues(w, arglist, 1);
-
- return(width);
- }
-
-
-
-
-
- /* Useed with children of a form widget -- given the widget, its width */
- /* and the horizontal width in which to center it, this function will */
- /* set the XtNhorizDistance value of the widget properly. */
- center (w, width, in_width)
- Widget w;
- Dimension width, in_width;
- {
- Arg arglist[1];
-
- width = (in_width - width) / 2;
- XtSetArg(arglist[0], XtNhorizDistance, width);
- XtSetValues(w, arglist, 1);
- }
-
-
-
-
- /* Returns a blank cursor */
- Cursor blank_cursor()
- {
- Pixmap foo;
- Cursor bar;
- XColor baz;
-
- foo = XCreatePixmap(dpy, RootWindow(dpy, screen), 1, 1, 1);
- baz.red = baz.blue = baz.green = 0;
- bar = XCreatePixmapCursor(dpy, foo, foo, &baz, &baz, 1, 1);
- return(bar);
- }
-
-
-
-
-
- char *user_string(str)
- char *str;
- {
- static char buf[100];
- static int initialized = 0;
-
- if (! initialized) {
- sprintf(buf, str, get_user());
- }
- return(buf);
- }
-
-
- char *time_string(str, force)
- ForceType force;
- char *str;
- {
- struct tm *local;
- static char buf[100];
- char new[100];
- int newf = 0;
-
- local = localtime((long) ×.current);
- sprintf(new, str, local->tm_hour, local->tm_min);
- if (strcmp(new, buf)) {
- strcpy(buf, new);
- newf++;
- }
- if (newf || (force == Force))
- return(buf);
- else
- return ((char *) NULL);
- }
-
-
-
- char *elapsed_string(str, force)
- ForceType force;
- char *str;
- {
- static char buf[100];
- int hours, minutes;
- char new[100];
- int newf = 0;
-
- int elapsed_sec = times.current - times.start;
- hours = elapsed_sec / 3600;
- minutes = (elapsed_sec - 3600 * hours) / 60;
- sprintf(new, str, hours, minutes);
- if (strcmp(new, buf)) {
- strcpy(buf, new);
- newf++;
- }
- if (newf || (force == Force))
- return(buf);
- else
- return((char *) NULL);
- }
-
-
- char *timeout_string(str, force)
- ForceType force;
- char *str;
- {
- static char buf[100];
- int hours, minutes;
- char new[100];
- int newf = 0;
-
- hours = defs.timeout / 60;
- minutes = defs.timeout - 60 * hours;
- sprintf(new, str, hours, (hours == 1 ? "hour" : "hours"),
- minutes, (minutes == 1 ? "minute" : "minutes"));
- if (strcmp(new, buf)) {
- strcpy(buf, new);
- newf++;
- }
-
- if (newf || (force == Force))
- return(buf);
- else
- return((char *) NULL);
- }
-
-
- char *timeleft_string(str, force)
- ForceType force;
- char *str;
- {
- static char buf[100];
- int hours, minutes;
- int elapsed_sec = times.current - times.start;
- char new[100];
- int newf = 0;
-
- /* Note that the + 60 in both equations is so that the counter will
- * always display the minute it is counting down, instead of the
- * minute before it.
- */
- hours = (defs.timeout * 60 - elapsed_sec + 60) / 3600;
- minutes = (defs.timeout * 60 - elapsed_sec + 60 - 3600 * hours) / 60;
- sprintf(new, str, hours, (hours == 1 ? "hour" : "hours"),
- minutes, (minutes == 1 ? "minute" : "minutes"));
- if (strcmp(new, buf)) {
- strcpy(buf, new);
- newf++;
- }
-
- if (newf || (force == Force))
- return(buf);
- else
- return((char *) NULL);
- }
-
-
-
-
-
- char *get_user()
- {
- static char user[MAXUSERNAME];
- struct passwd *pwent;
- static char *ptr = (char *) NULL;
-
- if (ptr)
- return(ptr);
-
- ptr = getenv("USER");
-
- if (ptr) {
- ptr = strcpy(user, ptr);
- return(user);
- }
-
- pwent = getpwuid(getuid());
-
- if (pwent) {
- ptr = strcpy(user, pwent->pw_name);
- return(ptr);
- }
-
- *user = '\0';
-
- return(user);
- }
-