ftp.nice.ch/pub/next/unix/admin/idled.1.0.NIHS.bs.tar.gz#/idled/idled.c

This is idled.c in view mode; [Download] [Up]

/*
 * idled.c -- Show correct idle time for console
 *
 * $Header: /Users/marcel/src/idled/RCS/idled.c,v 1.1 1996/07/21 18:22:46 mwa Exp $
 *
 * (c) 1996 Marcel Waldvogel, mwa@nice.ch
 *
 * History
 * =======
 * 960721 mwa	Created
 */

#import <libc.h>
#import <drivers/event_status_driver.h>

/*
 * The interval in which we check for user actions.
 * One could choose anything shorter than 1 minute
 * (1 minute is the minimum idle time being shown).
 * The smaller the time, the smaller is also the update latency
 * (the time between your typing or mousing and the clearing of the
 * idle time), but the CPU load increases.
 */
#define INTERVAL	15
#define CONSOLE		"/dev/console"

void
usage(const char *prog)
{
    fprintf(stderr, "Usage: %s\n", prog);
    fputs("Keeps the idle time for the console accurately", stderr);
    exit(1);
}

void
daemonize(void)
{
    /*
     * This code was inspired by the xntpd source
     */
    int fid;
    u_long s;
    int max_fd;

    if (fork())
	exit(0);

    max_fd = getdtablesize();
    for (s = 3; s < max_fd; s++)
	close(s);
    /*
     * You could also start closing at 0 and add the following:
     * But then you would have to care for perror()s etc.
    open("/", 0);
    dup2(0, 1);
    dup2(0, 2);
     */
    chdir("/");

    fid = open("/dev/tty", 2);
    if (fid >= 0) {
	ioctl(fid, (u_long) TIOCNOTTY, (char *) 0);
	close(fid);
    }
    setpgrp(0, getpid());
}

int
idle(NXEventHandle evs)
{
#if NX_CURRENT_COMPILER_RELEASE >= 330
    /*
     * It is bogous to check for the compiler release, we should check
     * for the current system release. NXIdleTime() is only supported
     * since NS 3.3
     */
    return (int)NXIdleTime(evs);
#else
    /*
     * Not supported directly, so we calculate it ourselves
     */
    return (int)(NXAutoDimThreshold(evs) - NXAutoDimTime(evs));
#endif
}

void
update(NXEventHandle evs)
{
    if (idle(evs) < INTERVAL) {
	/*
	 * Reset the idle timer if the user has been doing something lately
	 */
	struct stat console;
	if (stat(CONSOLE, &console) == 0) {
	    struct timeval tv[2];

	    /*
	     * Build a structure suitable for utimes(), preserving the file's
	     * modification time but changing the access time to now
	     */
	    gettimeofday(&tv[0], NULL);
	    tv[1].tv_usec = 0;
	    tv[1].tv_sec = console.st_mtime;
	    utimes(CONSOLE, tv);
	}
    }
}

/*
 * Daemon: update idle time in regular intervals
 */
void
main(int argc, char *argv[])
{
    NXEventHandle evs;

    /* Did we get any argument? */
    if (argc > 1)
	usage(argv[0]);

    daemonize();
    
    evs = NXOpenEventStatus();
    if (evs == NULL) {
	perror("NXOpenEventStatus");
	exit(1);
    }

    /* Otherwise, go into "daemon" mode */
    while (1) {
	update(evs);
	sleep(INTERVAL);
    }
    /*
     * Do it if the while() condition should ever change...
    NXCloseEventStatus(evs);
     */
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.