Home | History | Annotate | Line # | Download | only in screenblank
screenblank.c revision 1.2
      1  1.2  thorpej /*	$NetBSD: screenblank.c,v 1.2 1996/02/28 01:18:34 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.2  thorpej /*-
      4  1.2  thorpej  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.2  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2  thorpej  * by Jason R. Thorpe.
      9  1.2  thorpej  *
     10  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.1  thorpej  * modification, are permitted provided that the following conditions
     12  1.1  thorpej  * are met:
     13  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     19  1.1  thorpej  *    must display the following acknowledgement:
     20  1.2  thorpej  *        This product includes software developed by the NetBSD
     21  1.2  thorpej  *        Foundation, Inc. and its contributors.
     22  1.2  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.2  thorpej  *    contributors may be used to endorse or promote products derived
     24  1.2  thorpej  *    from this software without specific prior written permission.
     25  1.1  thorpej  *
     26  1.2  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.2  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.2  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.2  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
     30  1.2  thorpej  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.2  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.2  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.2  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.2  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.2  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.2  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  thorpej  */
     38  1.1  thorpej 
     39  1.1  thorpej /*
     40  1.1  thorpej  * Screensaver daemon for the Sun 3 and SPARC.
     41  1.1  thorpej  */
     42  1.1  thorpej 
     43  1.1  thorpej #include <sys/types.h>
     44  1.1  thorpej #include <sys/time.h>
     45  1.1  thorpej #include <sys/stat.h>
     46  1.1  thorpej #include <sys/ioctl.h>
     47  1.1  thorpej #include <sys/queue.h>
     48  1.1  thorpej #include <ctype.h>
     49  1.1  thorpej #include <err.h>
     50  1.1  thorpej #include <errno.h>
     51  1.1  thorpej #include <fcntl.h>
     52  1.1  thorpej #include <limits.h>
     53  1.1  thorpej #include <math.h>
     54  1.1  thorpej #include <paths.h>
     55  1.1  thorpej #include <stdlib.h>
     56  1.1  thorpej #include <stdio.h>
     57  1.1  thorpej #include <string.h>
     58  1.1  thorpej #include <signal.h>
     59  1.1  thorpej #include <unistd.h>
     60  1.1  thorpej 
     61  1.1  thorpej #include <machine/fbio.h>
     62  1.1  thorpej 
     63  1.1  thorpej #include "pathnames.h"
     64  1.1  thorpej 
     65  1.1  thorpej struct	dev_stat {
     66  1.1  thorpej 	LIST_ENTRY(dev_stat) ds_link;	/* linked list */
     67  1.1  thorpej 	char	*ds_path;		/* path to device */
     68  1.1  thorpej 	int	ds_isfb;		/* boolean; framebuffer? */
     69  1.1  thorpej 	time_t	ds_atime;		/* time device last accessed */
     70  1.1  thorpej 	time_t	ds_mtime;		/* time device last modified */
     71  1.1  thorpej };
     72  1.1  thorpej LIST_HEAD(ds_list, dev_stat) ds_list;
     73  1.1  thorpej 
     74  1.1  thorpej extern	char *__progname;
     75  1.1  thorpej 
     76  1.1  thorpej static	void add_dev __P((char *, int));
     77  1.1  thorpej static	void change_state __P((int));
     78  1.1  thorpej static	void cvt_arg __P((char *, struct timeval *));
     79  1.1  thorpej static	void logpid __P((void));
     80  1.1  thorpej static	void sighandler __P((int, int, struct sigcontext *));
     81  1.1  thorpej static	void usage __P((void));
     82  1.1  thorpej 
     83  1.1  thorpej int
     84  1.1  thorpej main(argc, argv)
     85  1.1  thorpej 	int argc;
     86  1.1  thorpej 	char **argv;
     87  1.1  thorpej {
     88  1.1  thorpej 	struct dev_stat *dsp;
     89  1.1  thorpej 	struct timeval timo_on, timo_off, *tvp;
     90  1.1  thorpej 	struct sigaction sa;
     91  1.1  thorpej 	struct stat st;
     92  1.1  thorpej 	int ch, change, fflag = 0, kflag = 0, mflag = 0, state;
     93  1.1  thorpej 
     94  1.1  thorpej 	LIST_INIT(&ds_list);
     95  1.1  thorpej 
     96  1.1  thorpej 	/*
     97  1.1  thorpej 	 * Set the default timeouts: 10 minutes on, .25 seconds off.
     98  1.1  thorpej 	 */
     99  1.1  thorpej 	timo_on.tv_sec = 600;
    100  1.1  thorpej 	timo_on.tv_usec = 0;
    101  1.1  thorpej 	timo_off.tv_sec = 0;
    102  1.1  thorpej 	timo_off.tv_usec = 250000;
    103  1.1  thorpej 
    104  1.1  thorpej 	while ((ch = getopt(argc, argv, "d:e:f:km")) != -1) {
    105  1.1  thorpej 		switch (ch) {
    106  1.1  thorpej 		case 'd':
    107  1.1  thorpej 			cvt_arg(optarg, &timo_on);
    108  1.1  thorpej 			break;
    109  1.1  thorpej 
    110  1.1  thorpej 		case 'e':
    111  1.1  thorpej 			cvt_arg(optarg, &timo_off);
    112  1.1  thorpej 			break;
    113  1.1  thorpej 
    114  1.1  thorpej 		case 'f':
    115  1.1  thorpej 			fflag = 1;
    116  1.1  thorpej 			add_dev(optarg, 1);
    117  1.1  thorpej 			break;
    118  1.1  thorpej 
    119  1.1  thorpej 		case 'k':
    120  1.1  thorpej 			if (mflag || kflag)
    121  1.1  thorpej 				usage();
    122  1.1  thorpej 			kflag = 1;
    123  1.1  thorpej 			break;
    124  1.1  thorpej 
    125  1.1  thorpej 		case 'm':
    126  1.1  thorpej 			if (kflag || mflag)
    127  1.1  thorpej 				usage();
    128  1.1  thorpej 			mflag = 1;
    129  1.1  thorpej 			break;
    130  1.1  thorpej 
    131  1.1  thorpej 		default:
    132  1.1  thorpej 			usage();
    133  1.1  thorpej 		}
    134  1.1  thorpej 	}
    135  1.1  thorpej 	argc -= optind;
    136  1.1  thorpej 	if (argc)
    137  1.1  thorpej 		usage();
    138  1.1  thorpej 
    139  1.1  thorpej 	/*
    140  1.1  thorpej 	 * Add the keyboard, mouse, and default framebuffer devices
    141  1.1  thorpej 	 * as necessary.  We _always_ check the console device.
    142  1.1  thorpej 	 */
    143  1.1  thorpej 	add_dev(_PATH_CONSOLE, 0);
    144  1.1  thorpej 	if (!kflag)
    145  1.1  thorpej 		add_dev(_PATH_KEYBOARD, 0);
    146  1.1  thorpej 	if (!mflag)
    147  1.1  thorpej 		add_dev(_PATH_MOUSE, 0);
    148  1.1  thorpej 	if (!fflag)
    149  1.1  thorpej 		add_dev(_PATH_FB, 1);
    150  1.1  thorpej 
    151  1.1  thorpej 	/* Ensure that the framebuffer is on. */
    152  1.1  thorpej 	state = FBVIDEO_ON;
    153  1.1  thorpej 	change_state(state);
    154  1.1  thorpej 	tvp = &timo_on;
    155  1.1  thorpej 
    156  1.1  thorpej 	/*
    157  1.1  thorpej 	 * Make sure the framebuffer gets turned back on when we're
    158  1.1  thorpej 	 * killed.
    159  1.1  thorpej 	 */
    160  1.1  thorpej 	sa.sa_handler = sighandler;
    161  1.1  thorpej 	sa.sa_mask = 0;
    162  1.1  thorpej 	sa.sa_flags = SA_NOCLDSTOP;
    163  1.1  thorpej 	if (sigaction(SIGINT, &sa, NULL) || sigaction(SIGTERM, &sa, NULL) ||
    164  1.1  thorpej 	    sigaction(SIGHUP, &sa, NULL))
    165  1.1  thorpej 		err(1, "sigaction");
    166  1.1  thorpej 
    167  1.1  thorpej 	/* Detach. */
    168  1.1  thorpej 	if (daemon(0, 0))
    169  1.1  thorpej 		err(1, "daemon");
    170  1.1  thorpej 	logpid();
    171  1.1  thorpej 
    172  1.1  thorpej 	/* Start the state machine. */
    173  1.1  thorpej 	for (;;) {
    174  1.1  thorpej 		change = 0;
    175  1.1  thorpej 		for (dsp = ds_list.lh_first; dsp != NULL;
    176  1.1  thorpej 		    dsp = dsp->ds_link.le_next) {
    177  1.1  thorpej 			/* Don't check framebuffers. */
    178  1.1  thorpej 			if (dsp->ds_isfb)
    179  1.1  thorpej 				continue;
    180  1.1  thorpej 			if (stat(dsp->ds_path, &st) < 0)
    181  1.1  thorpej 				err(1, "stat: %s", dsp->ds_path);
    182  1.1  thorpej 			if (st.st_atime > dsp->ds_atime) {
    183  1.1  thorpej 				change = 1;
    184  1.1  thorpej 				dsp->ds_atime = st.st_atime;
    185  1.1  thorpej 			}
    186  1.1  thorpej 			if (st.st_mtime > dsp->ds_mtime) {
    187  1.1  thorpej 				change = 1;
    188  1.1  thorpej 				dsp->ds_mtime = st.st_mtime;
    189  1.1  thorpej 			}
    190  1.1  thorpej 		}
    191  1.1  thorpej 
    192  1.1  thorpej 		switch (state) {
    193  1.1  thorpej 		case FBVIDEO_ON:
    194  1.1  thorpej 			if (!change) {
    195  1.1  thorpej 				state = FBVIDEO_OFF;
    196  1.1  thorpej 				change_state(state);
    197  1.1  thorpej 				tvp = &timo_off;
    198  1.1  thorpej 			}
    199  1.1  thorpej 			break;
    200  1.1  thorpej 
    201  1.1  thorpej 		case FBVIDEO_OFF:
    202  1.1  thorpej 			if (change) {
    203  1.1  thorpej 				state = FBVIDEO_ON;
    204  1.1  thorpej 				change_state(state);
    205  1.1  thorpej 				tvp = &timo_on;
    206  1.1  thorpej 			}
    207  1.1  thorpej 			break;
    208  1.1  thorpej 		}
    209  1.1  thorpej 
    210  1.1  thorpej 		if (select(0, NULL, NULL, NULL, tvp) < 0)
    211  1.1  thorpej 			err(1, "select");
    212  1.1  thorpej 	}
    213  1.1  thorpej 	/* NOTREACHED */
    214  1.1  thorpej }
    215  1.1  thorpej 
    216  1.1  thorpej static void
    217  1.1  thorpej add_dev(path, isfb)
    218  1.1  thorpej 	char *path;
    219  1.1  thorpej 	int isfb;
    220  1.1  thorpej {
    221  1.1  thorpej 	struct dev_stat *dsp1, *dsp2;
    222  1.1  thorpej 
    223  1.1  thorpej 	/* Create the entry... */
    224  1.1  thorpej 	dsp1 = malloc(sizeof(struct dev_stat));
    225  1.1  thorpej 	if (dsp1 == NULL)
    226  1.1  thorpej 		errx(1, "can't allocate memory for %s", path);
    227  1.1  thorpej 	bzero(dsp1, sizeof(struct dev_stat));
    228  1.1  thorpej 	dsp1->ds_path = path;
    229  1.1  thorpej 	dsp1->ds_isfb = isfb;
    230  1.1  thorpej 
    231  1.1  thorpej 	/* ...and put it in the list. */
    232  1.1  thorpej 	if (ds_list.lh_first == NULL) {
    233  1.1  thorpej 		LIST_INSERT_HEAD(&ds_list, dsp1, ds_link);
    234  1.1  thorpej 	} else {
    235  1.1  thorpej 		for (dsp2 = ds_list.lh_first; dsp2->ds_link.le_next != NULL;
    236  1.1  thorpej 		    dsp2 = dsp2->ds_link.le_next)
    237  1.1  thorpej 			/* Nothing. */ ;
    238  1.1  thorpej 		LIST_INSERT_AFTER(dsp2, dsp1, ds_link);
    239  1.1  thorpej 	}
    240  1.1  thorpej }
    241  1.1  thorpej 
    242  1.1  thorpej /* ARGSUSED */
    243  1.1  thorpej static void
    244  1.1  thorpej sighandler(sig, code, context)
    245  1.1  thorpej 	int sig, code;
    246  1.1  thorpej 	struct sigcontext *context;
    247  1.1  thorpej {
    248  1.1  thorpej 
    249  1.1  thorpej 	/* Kill the pid file and re-enable the framebuffer before exit. */
    250  1.1  thorpej 	(void)unlink(_PATH_SCREENBLANKPID);
    251  1.1  thorpej 	change_state(FBVIDEO_ON);
    252  1.1  thorpej 	exit(0);
    253  1.1  thorpej }
    254  1.1  thorpej 
    255  1.1  thorpej static void
    256  1.1  thorpej change_state(state)
    257  1.1  thorpej 	int state;
    258  1.1  thorpej {
    259  1.1  thorpej 	struct dev_stat *dsp;
    260  1.1  thorpej 	int fd;
    261  1.1  thorpej 
    262  1.1  thorpej 	for (dsp = ds_list.lh_first; dsp != NULL; dsp = dsp->ds_link.le_next) {
    263  1.1  thorpej 		/* Don't change the state of non-framebuffers! */
    264  1.1  thorpej 		if (dsp->ds_isfb == 0)
    265  1.1  thorpej 			continue;
    266  1.1  thorpej 		if ((fd = open(dsp->ds_path, O_RDWR, 0)) < 0) {
    267  1.1  thorpej 			warn("open: %s", dsp->ds_path);
    268  1.1  thorpej 			continue;
    269  1.1  thorpej 		}
    270  1.1  thorpej 		if (ioctl(fd, FBIOSVIDEO, &state) < 0)
    271  1.1  thorpej 			warn("ioctl: %s", dsp->ds_path);
    272  1.1  thorpej 		(void)close(fd);
    273  1.1  thorpej 	}
    274  1.1  thorpej }
    275  1.1  thorpej 
    276  1.1  thorpej static void
    277  1.1  thorpej cvt_arg(arg, tvp)
    278  1.1  thorpej 	char *arg;
    279  1.1  thorpej 	struct timeval *tvp;
    280  1.1  thorpej {
    281  1.1  thorpej 	char *cp;
    282  1.1  thorpej 	double seconds = 0.0, exponent = -1.0;
    283  1.1  thorpej 	int period = 0;
    284  1.1  thorpej 
    285  1.1  thorpej 	for (cp = arg; *cp != '\0'; ++cp) {
    286  1.1  thorpej 		if (*cp == '.') {
    287  1.1  thorpej 			if (period)
    288  1.1  thorpej 				errx(1, "invalid argument: %s", arg);
    289  1.1  thorpej 			period = 1;
    290  1.1  thorpej 			continue;
    291  1.1  thorpej 		}
    292  1.1  thorpej 
    293  1.1  thorpej 		if (!isdigit(*cp))
    294  1.1  thorpej 			errx(1, "invalid argument: %s", arg);
    295  1.1  thorpej 
    296  1.1  thorpej 		if (period) {
    297  1.1  thorpej 			seconds = seconds + ((*cp - '0') * pow(10.0, exponent));
    298  1.1  thorpej 			exponent -= 1.0;
    299  1.1  thorpej 		} else
    300  1.1  thorpej 			seconds = (seconds * 10.0) + (*cp - '0');
    301  1.1  thorpej 	}
    302  1.1  thorpej 
    303  1.1  thorpej 	tvp->tv_sec = (long)seconds;
    304  1.1  thorpej 	tvp->tv_usec = (long)((seconds - tvp->tv_sec) * 1000000);
    305  1.1  thorpej }
    306  1.1  thorpej 
    307  1.1  thorpej static void
    308  1.1  thorpej logpid()
    309  1.1  thorpej {
    310  1.1  thorpej 	FILE *fp;
    311  1.1  thorpej 
    312  1.1  thorpej 	if ((fp = fopen(_PATH_SCREENBLANKPID, "w")) != NULL) {
    313  1.1  thorpej 		fprintf(fp, "%u\n", getpid());
    314  1.1  thorpej 		(void)fclose(fp);
    315  1.1  thorpej 	}
    316  1.1  thorpej }
    317  1.1  thorpej 
    318  1.1  thorpej static void
    319  1.1  thorpej usage()
    320  1.1  thorpej {
    321  1.1  thorpej 
    322  1.1  thorpej 	fprintf(stderr, "usage: %s [-k | -m] [-d timeout] [-e timeout] %s\n",
    323  1.1  thorpej 	    __progname, "[-f framebuffer]");
    324  1.1  thorpej 	exit(1);
    325  1.1  thorpej }
    326