Home | History | Annotate | Line # | Download | only in screenblank
screenblank.c revision 1.12
      1 /*	$NetBSD: screenblank.c,v 1.12 2001/09/19 16:16:03 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.12 2001/09/19 16:16:03 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 <dev/sun/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 int	main __P((int, char *[]));
     92 static	void add_dev __P((const char *, int));
     93 static	void change_state __P((int));
     94 static	void cvt_arg __P((char *, struct timeval *));
     95 static	void sighandler __P((int));
     96 static	void usage __P((void));
     97 
     98 int
     99 main(argc, argv)
    100 	int argc;
    101 	char *argv[];
    102 {
    103 	struct dev_stat *dsp;
    104 	struct timeval timo_on, timo_off, *tvp;
    105 	struct sigaction sa;
    106 	struct stat st;
    107 	int ch, change, fflag = 0, kflag = 0, mflag = 0, state;
    108 	const char *kbd, *mouse, *display;
    109 
    110 	LIST_INIT(&ds_list);
    111 
    112 	/*
    113 	 * Set the default timeouts: 10 minutes on, .25 seconds off.
    114 	 */
    115 	timo_on.tv_sec = 600;
    116 	timo_on.tv_usec = 0;
    117 	timo_off.tv_sec = 0;
    118 	timo_off.tv_usec = 250000;
    119 
    120 	while ((ch = getopt(argc, argv, "d:e:f:km")) != -1) {
    121 		switch (ch) {
    122 		case 'd':
    123 			cvt_arg(optarg, &timo_on);
    124 			break;
    125 
    126 		case 'e':
    127 			cvt_arg(optarg, &timo_off);
    128 			break;
    129 
    130 		case 'f':
    131 			fflag = 1;
    132 			add_dev(optarg, 1);
    133 			break;
    134 
    135 		case 'k':
    136 			if (mflag || kflag)
    137 				usage();
    138 			kflag = 1;
    139 			break;
    140 
    141 		case 'm':
    142 			if (kflag || mflag)
    143 				usage();
    144 			mflag = 1;
    145 			break;
    146 
    147 		default:
    148 			usage();
    149 		}
    150 	}
    151 	argc -= optind;
    152 	if (argc)
    153 		usage();
    154 
    155 	/*
    156 	 * Default to WSCONS support.
    157 	 */
    158 	kbd = _PATH_WSKBD;
    159 	mouse = _PATH_WSMOUSE;
    160 	display = _PATH_WSDISPLAY;
    161 
    162 #ifdef HAVE_FBIO
    163 	/*
    164 	 * If a display device wasn't specified, check to see which we
    165 	 * have.  If we can't open the WSCONS display, fall back to fbio.
    166 	 */
    167 	if (!fflag) {
    168 		int fd;
    169 
    170 		if ((fd = open(display, O_RDONLY, 0666)) == -1)
    171 			setvideo = FBIOSVIDEO;
    172 		else
    173 			(void) close(fd);
    174 	}
    175 
    176 	/*
    177 	 * Do this here so that -f ... args above can influence us.
    178 	 */
    179 	if (setvideo == FBIOSVIDEO) {
    180 		videoon = FBVIDEO_ON;
    181 		videooff = FBVIDEO_OFF;
    182 		kbd = _PATH_KEYBOARD;
    183 		mouse = _PATH_MOUSE;
    184 		display = _PATH_FB;
    185 	}
    186 #endif
    187 
    188 	/*
    189 	 * Add the keyboard, mouse, and default framebuffer devices
    190 	 * as necessary.  We _always_ check the console device.
    191 	 */
    192 	add_dev(_PATH_CONSOLE, 0);
    193 	if (!kflag)
    194 		add_dev(kbd, 0);
    195 	if (!mflag)
    196 		add_dev(mouse, 0);
    197 	if (!fflag)
    198 		add_dev(display, 1);
    199 
    200 	/* Ensure that the framebuffer is on. */
    201 	state = videoon;
    202 	change_state(state);
    203 	tvp = &timo_on;
    204 
    205 	/*
    206 	 * Make sure the framebuffer gets turned back on when we're
    207 	 * killed.
    208 	 */
    209 	sa.sa_handler = sighandler;
    210 	sa.sa_flags = SA_NOCLDSTOP;
    211 	if (sigemptyset(&sa.sa_mask))
    212 		err(1, "sigemptyset");
    213 	if (sigaction(SIGINT, &sa, NULL) || sigaction(SIGTERM, &sa, NULL) ||
    214 	    sigaction(SIGHUP, &sa, NULL))
    215 		err(1, "sigaction");
    216 
    217 	/* Detach. */
    218 	if (daemon(0, 0))
    219 		err(1, "daemon");
    220 	pidfile(NULL);
    221 
    222 	/* Start the state machine. */
    223 	for (;;) {
    224 		change = 0;
    225 		for (dsp = ds_list.lh_first; dsp != NULL;
    226 		    dsp = dsp->ds_link.le_next) {
    227 			/* Don't check framebuffers. */
    228 			if (dsp->ds_isfb)
    229 				continue;
    230 			if (stat(dsp->ds_path, &st) < 0)
    231 				err(1, "stat: %s", dsp->ds_path);
    232 			if (st.st_atime > dsp->ds_atime) {
    233 				change = 1;
    234 				dsp->ds_atime = st.st_atime;
    235 			}
    236 			if (st.st_mtime > dsp->ds_mtime) {
    237 				change = 1;
    238 				dsp->ds_mtime = st.st_mtime;
    239 			}
    240 		}
    241 
    242 		if (state == videoon) {
    243 			if (!change) {
    244 				state = videooff;
    245 				change_state(state);
    246 				tvp = &timo_off;
    247 			}
    248 		} else {
    249 			if (change) {
    250 				state = videoon;
    251 				change_state(state);
    252 				tvp = &timo_on;
    253 			}
    254 		}
    255 
    256 		if (select(0, NULL, NULL, NULL, tvp) < 0)
    257 			err(1, "select");
    258 	}
    259 	/* NOTREACHED */
    260 }
    261 
    262 static void
    263 add_dev(path, isfb)
    264 	const char *path;
    265 	int isfb;
    266 {
    267 	struct dev_stat *dsp;
    268 	int fd;
    269 
    270 	/* Make sure we can open the device. */
    271 	if ((fd = open(path, O_RDWR, 0666)) == -1)
    272 		err(1, "can't open %s", path);
    273 
    274 #ifdef HAVE_FBIO
    275 	/*
    276 	 * We default to WSCONS.  If this is a frame buffer
    277 	 * device, check to see if it responds to the old
    278 	 * Sun-style fbio ioctls.  If so, switch to fbio mode.
    279 	 */
    280 	if (isfb && setvideo != FBIOSVIDEO) {
    281 		int onoff;
    282 
    283 		if ((ioctl(fd, FBIOGVIDEO, &onoff)) == 0)
    284 			setvideo = FBIOSVIDEO;
    285 	}
    286 #endif
    287 
    288 	(void) close(fd);
    289 
    290 	/* Create the entry... */
    291 	dsp = malloc(sizeof(struct dev_stat));
    292 	if (dsp == NULL)
    293 		errx(1, "can't allocate memory for %s", path);
    294 	memset(dsp, 0, sizeof(struct dev_stat));
    295 	dsp->ds_path = path;
    296 	dsp->ds_isfb = isfb;
    297 
    298 	/* ...and put it in the list. */
    299 	LIST_INSERT_HEAD(&ds_list, dsp, ds_link);
    300 }
    301 
    302 /* ARGSUSED */
    303 static void
    304 sighandler(sig)
    305 	int sig;
    306 {
    307 
    308 	/* Kill the pid file and re-enable the framebuffer before exit. */
    309 	change_state(videoon);
    310 	exit(0);
    311 }
    312 
    313 static void
    314 change_state(state)
    315 	int state;
    316 {
    317 	struct dev_stat *dsp;
    318 	int fd;
    319 
    320 	for (dsp = ds_list.lh_first; dsp != NULL; dsp = dsp->ds_link.le_next) {
    321 		/* Don't change the state of non-framebuffers! */
    322 		if (dsp->ds_isfb == 0)
    323 			continue;
    324 		if ((fd = open(dsp->ds_path, O_RDWR, 0)) < 0) {
    325 			warn("open: %s", dsp->ds_path);
    326 			continue;
    327 		}
    328 		if (ioctl(fd, setvideo, &state) < 0)
    329 			warn("ioctl: %s", dsp->ds_path);
    330 		(void)close(fd);
    331 	}
    332 }
    333 
    334 static void
    335 cvt_arg(arg, tvp)
    336 	char *arg;
    337 	struct timeval *tvp;
    338 {
    339 	char *cp;
    340 	int seconds, microseconds, factor;
    341 	int period = 0;
    342 	factor = 1000000;
    343 	microseconds = 0;
    344 	seconds = 0;
    345 
    346 	for (cp = arg; *cp != '\0'; ++cp) {
    347 		if (*cp == '.') {
    348 			if (period)
    349 				errx(1, "invalid argument: %s", arg);
    350 			period = 1;
    351 			continue;
    352 		}
    353 
    354 		if (!isdigit(*cp))
    355 			errx(1, "invalid argument: %s", arg);
    356 
    357 		if (period) {
    358 			if (factor > 1) {
    359 				microseconds = microseconds * 10 + (*cp - '0');
    360 				factor /= 10;
    361 			}
    362 		} else
    363 			seconds = (seconds * 10) + (*cp - '0');
    364 	}
    365 
    366 	tvp->tv_sec = seconds;
    367 	if (factor > 1)
    368 		microseconds *= factor;
    369 
    370 	tvp->tv_usec = microseconds;
    371 }
    372 
    373 static void
    374 usage()
    375 {
    376 
    377 	fprintf(stderr, "usage: %s [-k | -m] [-d timeout] [-e timeout] %s\n",
    378 	    getprogname(), "[-f framebuffer]");
    379 	exit(1);
    380 }
    381