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