Home | History | Annotate | Line # | Download | only in screenblank
screenblank.c revision 1.16
      1  1.16     lukem /*	$NetBSD: screenblank.c,v 1.16 2002/01/23 16:57:51 lukem Exp $	*/
      2   1.1   thorpej 
      3   1.2   thorpej /*-
      4  1.16     lukem  * Copyright (c) 1996-2002 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.6       jtc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.6       jtc  * BE 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.16     lukem  * Screensaver daemon for the Sun 3 and SPARC, and platforms using WSCONS.
     41   1.1   thorpej  */
     42   1.1   thorpej 
     43   1.5   thorpej #include <sys/cdefs.h>
     44   1.5   thorpej #ifndef lint
     45   1.5   thorpej __COPYRIGHT(
     46  1.16     lukem "@(#) Copyright (c) 1996-2002 \
     47   1.9   thorpej 	The NetBSD Foundation, Inc.  All rights reserved.");
     48  1.16     lukem __RCSID("$NetBSD: screenblank.c,v 1.16 2002/01/23 16:57:51 lukem Exp $");
     49   1.5   thorpej #endif
     50   1.5   thorpej 
     51   1.1   thorpej #include <sys/types.h>
     52   1.1   thorpej #include <sys/time.h>
     53   1.1   thorpej #include <sys/stat.h>
     54   1.1   thorpej #include <sys/ioctl.h>
     55   1.1   thorpej #include <sys/queue.h>
     56   1.1   thorpej #include <ctype.h>
     57   1.1   thorpej #include <err.h>
     58   1.1   thorpej #include <errno.h>
     59   1.1   thorpej #include <fcntl.h>
     60   1.1   thorpej #include <limits.h>
     61   1.1   thorpej #include <math.h>
     62   1.1   thorpej #include <paths.h>
     63   1.1   thorpej #include <stdlib.h>
     64   1.1   thorpej #include <stdio.h>
     65   1.1   thorpej #include <string.h>
     66   1.1   thorpej #include <signal.h>
     67  1.13  christos #include <syslog.h>
     68   1.1   thorpej #include <unistd.h>
     69  1.10   thorpej #include <util.h>
     70   1.1   thorpej 
     71   1.9   thorpej #include <dev/wscons/wsconsio.h>
     72   1.9   thorpej 
     73   1.9   thorpej #ifdef HAVE_FBIO
     74  1.12   thorpej #include <dev/sun/fbio.h>
     75   1.9   thorpej #endif
     76   1.1   thorpej 
     77   1.1   thorpej #include "pathnames.h"
     78   1.1   thorpej 
     79   1.9   thorpej u_long	setvideo = WSDISPLAYIO_SVIDEO;		/* "set video" ioctl */
     80   1.9   thorpej int	videoon  = WSDISPLAYIO_VIDEO_ON;	/* value for "on" */
     81   1.9   thorpej int	videooff = WSDISPLAYIO_VIDEO_OFF;	/* value for "off" */
     82   1.9   thorpej 
     83   1.1   thorpej struct	dev_stat {
     84   1.1   thorpej 	LIST_ENTRY(dev_stat) ds_link;	/* linked list */
     85   1.9   thorpej 	const char *ds_path;		/* path to device */
     86   1.1   thorpej 	int	ds_isfb;		/* boolean; framebuffer? */
     87   1.1   thorpej 	time_t	ds_atime;		/* time device last accessed */
     88   1.1   thorpej 	time_t	ds_mtime;		/* time device last modified */
     89   1.1   thorpej };
     90   1.1   thorpej LIST_HEAD(ds_list, dev_stat) ds_list;
     91   1.1   thorpej 
     92  1.13  christos int	main(int, char *[]);
     93  1.13  christos static	void add_dev(const char *, int);
     94  1.13  christos static	void change_state(int);
     95  1.13  christos static	void cvt_arg(char *, struct timeval *);
     96  1.13  christos static	void sighandler(int);
     97  1.13  christos static	void usage(void);
     98   1.1   thorpej 
     99   1.1   thorpej int
    100  1.13  christos main(int argc, char *argv[])
    101   1.1   thorpej {
    102   1.1   thorpej 	struct dev_stat *dsp;
    103  1.13  christos 	struct timeval timo_on, timo_off, *tvp, tv;
    104   1.1   thorpej 	struct sigaction sa;
    105   1.1   thorpej 	struct stat st;
    106   1.1   thorpej 	int ch, change, fflag = 0, kflag = 0, mflag = 0, state;
    107   1.9   thorpej 	const char *kbd, *mouse, *display;
    108   1.1   thorpej 
    109   1.1   thorpej 	LIST_INIT(&ds_list);
    110   1.1   thorpej 
    111   1.1   thorpej 	/*
    112   1.1   thorpej 	 * Set the default timeouts: 10 minutes on, .25 seconds off.
    113   1.1   thorpej 	 */
    114   1.1   thorpej 	timo_on.tv_sec = 600;
    115   1.1   thorpej 	timo_on.tv_usec = 0;
    116   1.1   thorpej 	timo_off.tv_sec = 0;
    117   1.1   thorpej 	timo_off.tv_usec = 250000;
    118   1.1   thorpej 
    119   1.1   thorpej 	while ((ch = getopt(argc, argv, "d:e:f:km")) != -1) {
    120   1.1   thorpej 		switch (ch) {
    121   1.1   thorpej 		case 'd':
    122   1.1   thorpej 			cvt_arg(optarg, &timo_on);
    123   1.1   thorpej 			break;
    124   1.1   thorpej 
    125   1.1   thorpej 		case 'e':
    126   1.1   thorpej 			cvt_arg(optarg, &timo_off);
    127   1.1   thorpej 			break;
    128   1.1   thorpej 
    129   1.1   thorpej 		case 'f':
    130   1.1   thorpej 			fflag = 1;
    131   1.1   thorpej 			add_dev(optarg, 1);
    132   1.1   thorpej 			break;
    133   1.1   thorpej 
    134   1.1   thorpej 		case 'k':
    135   1.1   thorpej 			if (mflag || kflag)
    136   1.1   thorpej 				usage();
    137   1.1   thorpej 			kflag = 1;
    138   1.1   thorpej 			break;
    139   1.1   thorpej 
    140   1.1   thorpej 		case 'm':
    141   1.1   thorpej 			if (kflag || mflag)
    142   1.1   thorpej 				usage();
    143   1.1   thorpej 			mflag = 1;
    144   1.1   thorpej 			break;
    145   1.1   thorpej 
    146   1.1   thorpej 		default:
    147   1.1   thorpej 			usage();
    148   1.1   thorpej 		}
    149   1.1   thorpej 	}
    150   1.1   thorpej 	argc -= optind;
    151   1.1   thorpej 	if (argc)
    152   1.1   thorpej 		usage();
    153   1.1   thorpej 
    154   1.1   thorpej 	/*
    155   1.9   thorpej 	 * Default to WSCONS support.
    156   1.9   thorpej 	 */
    157   1.9   thorpej 	kbd = _PATH_WSKBD;
    158   1.9   thorpej 	mouse = _PATH_WSMOUSE;
    159   1.9   thorpej 	display = _PATH_WSDISPLAY;
    160   1.9   thorpej 
    161   1.9   thorpej #ifdef HAVE_FBIO
    162   1.9   thorpej 	/*
    163   1.9   thorpej 	 * If a display device wasn't specified, check to see which we
    164   1.9   thorpej 	 * have.  If we can't open the WSCONS display, fall back to fbio.
    165   1.9   thorpej 	 */
    166   1.9   thorpej 	if (!fflag) {
    167   1.9   thorpej 		int fd;
    168   1.9   thorpej 
    169   1.9   thorpej 		if ((fd = open(display, O_RDONLY, 0666)) == -1)
    170   1.9   thorpej 			setvideo = FBIOSVIDEO;
    171   1.9   thorpej 		else
    172   1.9   thorpej 			(void) close(fd);
    173   1.9   thorpej 	}
    174   1.9   thorpej 
    175   1.9   thorpej 	/*
    176   1.9   thorpej 	 * Do this here so that -f ... args above can influence us.
    177   1.9   thorpej 	 */
    178   1.9   thorpej 	if (setvideo == FBIOSVIDEO) {
    179   1.9   thorpej 		videoon = FBVIDEO_ON;
    180   1.9   thorpej 		videooff = FBVIDEO_OFF;
    181   1.9   thorpej 		kbd = _PATH_KEYBOARD;
    182   1.9   thorpej 		mouse = _PATH_MOUSE;
    183   1.9   thorpej 		display = _PATH_FB;
    184   1.9   thorpej 	}
    185   1.9   thorpej #endif
    186   1.9   thorpej 
    187   1.9   thorpej 	/*
    188   1.1   thorpej 	 * Add the keyboard, mouse, and default framebuffer devices
    189   1.1   thorpej 	 * as necessary.  We _always_ check the console device.
    190   1.1   thorpej 	 */
    191   1.1   thorpej 	add_dev(_PATH_CONSOLE, 0);
    192   1.1   thorpej 	if (!kflag)
    193   1.9   thorpej 		add_dev(kbd, 0);
    194   1.1   thorpej 	if (!mflag)
    195   1.9   thorpej 		add_dev(mouse, 0);
    196   1.1   thorpej 	if (!fflag)
    197   1.9   thorpej 		add_dev(display, 1);
    198   1.1   thorpej 
    199   1.1   thorpej 	/* Ensure that the framebuffer is on. */
    200   1.9   thorpej 	state = videoon;
    201   1.1   thorpej 	change_state(state);
    202   1.1   thorpej 	tvp = &timo_on;
    203   1.1   thorpej 
    204   1.1   thorpej 	/*
    205   1.1   thorpej 	 * Make sure the framebuffer gets turned back on when we're
    206   1.1   thorpej 	 * killed.
    207   1.1   thorpej 	 */
    208   1.1   thorpej 	sa.sa_handler = sighandler;
    209   1.1   thorpej 	sa.sa_flags = SA_NOCLDSTOP;
    210   1.3   thorpej 	if (sigemptyset(&sa.sa_mask))
    211   1.3   thorpej 		err(1, "sigemptyset");
    212   1.1   thorpej 	if (sigaction(SIGINT, &sa, NULL) || sigaction(SIGTERM, &sa, NULL) ||
    213   1.1   thorpej 	    sigaction(SIGHUP, &sa, NULL))
    214   1.1   thorpej 		err(1, "sigaction");
    215   1.1   thorpej 
    216  1.13  christos 	openlog("screenblank", LOG_PID, LOG_DAEMON);
    217   1.1   thorpej 	/* Detach. */
    218   1.1   thorpej 	if (daemon(0, 0))
    219   1.1   thorpej 		err(1, "daemon");
    220  1.10   thorpej 	pidfile(NULL);
    221   1.1   thorpej 
    222   1.1   thorpej 	/* Start the state machine. */
    223   1.1   thorpej 	for (;;) {
    224   1.1   thorpej 		change = 0;
    225   1.1   thorpej 		for (dsp = ds_list.lh_first; dsp != NULL;
    226   1.1   thorpej 		    dsp = dsp->ds_link.le_next) {
    227  1.16     lukem #if 0		/* XXXLUKEM - doesn't make sense for wscons framebuffers */
    228   1.1   thorpej 			/* Don't check framebuffers. */
    229   1.1   thorpej 			if (dsp->ds_isfb)
    230   1.1   thorpej 				continue;
    231  1.16     lukem #endif
    232  1.13  christos 			if (stat(dsp->ds_path, &st) == -1) {
    233  1.13  christos 				syslog(LOG_CRIT,
    234  1.13  christos 				    "Can't stat `%s' (%m)", dsp->ds_path);
    235  1.13  christos 				exit(1);
    236  1.13  christos 			}
    237   1.1   thorpej 			if (st.st_atime > dsp->ds_atime) {
    238   1.1   thorpej 				change = 1;
    239   1.1   thorpej 				dsp->ds_atime = st.st_atime;
    240   1.1   thorpej 			}
    241   1.1   thorpej 			if (st.st_mtime > dsp->ds_mtime) {
    242   1.1   thorpej 				change = 1;
    243   1.1   thorpej 				dsp->ds_mtime = st.st_mtime;
    244   1.1   thorpej 			}
    245   1.1   thorpej 		}
    246   1.1   thorpej 
    247   1.9   thorpej 		if (state == videoon) {
    248   1.1   thorpej 			if (!change) {
    249   1.9   thorpej 				state = videooff;
    250   1.1   thorpej 				change_state(state);
    251   1.1   thorpej 				tvp = &timo_off;
    252   1.1   thorpej 			}
    253   1.9   thorpej 		} else {
    254   1.1   thorpej 			if (change) {
    255   1.9   thorpej 				state = videoon;
    256   1.1   thorpej 				change_state(state);
    257   1.1   thorpej 				tvp = &timo_on;
    258   1.1   thorpej 			}
    259   1.1   thorpej 		}
    260   1.1   thorpej 
    261  1.13  christos 		tv = *tvp;
    262  1.13  christos 		if (select(0, NULL, NULL, NULL, &tv) == -1)
    263   1.1   thorpej 			err(1, "select");
    264   1.1   thorpej 	}
    265   1.1   thorpej 	/* NOTREACHED */
    266   1.1   thorpej }
    267   1.1   thorpej 
    268   1.1   thorpej static void
    269  1.13  christos add_dev(const char *path, int isfb)
    270   1.1   thorpej {
    271   1.9   thorpej 	struct dev_stat *dsp;
    272  1.14  augustss 	struct stat sb;
    273   1.9   thorpej 
    274  1.15  augustss 	/* Make sure we can stat the device. */
    275  1.14  augustss 	if (stat(path, &sb) == -1) {
    276  1.14  augustss 		warn("Can't stat `%s'", path);
    277  1.13  christos 		return;
    278  1.13  christos 	}
    279   1.9   thorpej 
    280   1.9   thorpej #ifdef HAVE_FBIO
    281   1.9   thorpej 	/*
    282   1.9   thorpej 	 * We default to WSCONS.  If this is a frame buffer
    283   1.9   thorpej 	 * device, check to see if it responds to the old
    284   1.9   thorpej 	 * Sun-style fbio ioctls.  If so, switch to fbio mode.
    285   1.9   thorpej 	 */
    286   1.9   thorpej 	if (isfb && setvideo != FBIOSVIDEO) {
    287  1.14  augustss 		int onoff, fd;
    288   1.9   thorpej 
    289  1.14  augustss 		if ((fd = open(path, O_RDWR, 0666)) == -1) {
    290  1.14  augustss 			warn("Can't open `%s'", path);
    291  1.14  augustss 			return;
    292  1.14  augustss 		}
    293   1.9   thorpej 		if ((ioctl(fd, FBIOGVIDEO, &onoff)) == 0)
    294   1.9   thorpej 			setvideo = FBIOSVIDEO;
    295  1.14  augustss 		(void)close(fd);
    296   1.9   thorpej 	}
    297   1.9   thorpej #endif
    298   1.1   thorpej 
    299   1.1   thorpej 	/* Create the entry... */
    300   1.9   thorpej 	dsp = malloc(sizeof(struct dev_stat));
    301   1.9   thorpej 	if (dsp == NULL)
    302  1.13  christos 		err(1, "Can't allocate memory for `%s'", path);
    303  1.13  christos 	(void)memset(dsp, 0, sizeof(struct dev_stat));
    304   1.9   thorpej 	dsp->ds_path = path;
    305   1.9   thorpej 	dsp->ds_isfb = isfb;
    306   1.1   thorpej 
    307   1.1   thorpej 	/* ...and put it in the list. */
    308   1.9   thorpej 	LIST_INSERT_HEAD(&ds_list, dsp, ds_link);
    309   1.1   thorpej }
    310   1.1   thorpej 
    311   1.1   thorpej /* ARGSUSED */
    312   1.1   thorpej static void
    313  1.13  christos sighandler(int sig)
    314   1.1   thorpej {
    315   1.1   thorpej 
    316   1.1   thorpej 	/* Kill the pid file and re-enable the framebuffer before exit. */
    317   1.9   thorpej 	change_state(videoon);
    318   1.1   thorpej 	exit(0);
    319   1.1   thorpej }
    320   1.1   thorpej 
    321   1.1   thorpej static void
    322  1.13  christos change_state(int state)
    323   1.1   thorpej {
    324   1.1   thorpej 	struct dev_stat *dsp;
    325   1.1   thorpej 	int fd;
    326  1.13  christos 	int fail = 1;
    327   1.1   thorpej 
    328   1.1   thorpej 	for (dsp = ds_list.lh_first; dsp != NULL; dsp = dsp->ds_link.le_next) {
    329   1.1   thorpej 		/* Don't change the state of non-framebuffers! */
    330   1.1   thorpej 		if (dsp->ds_isfb == 0)
    331   1.1   thorpej 			continue;
    332  1.13  christos 		if ((fd = open(dsp->ds_path, O_RDWR, 0)) == -1) {
    333  1.13  christos 			syslog(LOG_WARNING, "Can't open `%s' (%m)",
    334  1.13  christos 			    dsp->ds_path);
    335   1.1   thorpej 			continue;
    336   1.1   thorpej 		}
    337  1.13  christos 		if (ioctl(fd, setvideo, &state) == -1)
    338  1.13  christos 			syslog(LOG_WARNING, "Can't set video on `%s' (%m)",
    339  1.13  christos 			    dsp->ds_path);
    340  1.13  christos 		else
    341  1.13  christos 			fail = 0;
    342   1.1   thorpej 		(void)close(fd);
    343   1.1   thorpej 	}
    344  1.13  christos 	if (fail) {
    345  1.13  christos 		syslog(LOG_CRIT, "No frame buffer devices, exiting\n");
    346  1.13  christos 		exit(1);
    347  1.13  christos 	}
    348   1.1   thorpej }
    349   1.1   thorpej 
    350   1.1   thorpej static void
    351  1.13  christos cvt_arg(char *arg, struct timeval *tvp)
    352   1.1   thorpej {
    353   1.1   thorpej 	char *cp;
    354   1.8        is 	int seconds, microseconds, factor;
    355   1.1   thorpej 	int period = 0;
    356   1.8        is 	factor = 1000000;
    357   1.8        is 	microseconds = 0;
    358   1.8        is 	seconds = 0;
    359   1.1   thorpej 
    360   1.1   thorpej 	for (cp = arg; *cp != '\0'; ++cp) {
    361   1.1   thorpej 		if (*cp == '.') {
    362   1.1   thorpej 			if (period)
    363  1.13  christos 				errx(1, "Invalid argument: %s", arg);
    364   1.1   thorpej 			period = 1;
    365   1.1   thorpej 			continue;
    366   1.1   thorpej 		}
    367   1.1   thorpej 
    368   1.1   thorpej 		if (!isdigit(*cp))
    369  1.13  christos 			errx(1, "Invalid argument: %s", arg);
    370   1.1   thorpej 
    371   1.1   thorpej 		if (period) {
    372   1.8        is 			if (factor > 1) {
    373   1.8        is 				microseconds = microseconds * 10 + (*cp - '0');
    374   1.8        is 				factor /= 10;
    375   1.8        is 			}
    376   1.1   thorpej 		} else
    377   1.8        is 			seconds = (seconds * 10) + (*cp - '0');
    378   1.1   thorpej 	}
    379   1.1   thorpej 
    380   1.8        is 	tvp->tv_sec = seconds;
    381   1.8        is 	if (factor > 1)
    382   1.8        is 		microseconds *= factor;
    383   1.8        is 
    384   1.8        is 	tvp->tv_usec = microseconds;
    385   1.1   thorpej }
    386   1.1   thorpej 
    387   1.1   thorpej static void
    388  1.13  christos usage(void)
    389   1.1   thorpej {
    390   1.1   thorpej 
    391  1.13  christos 	(void)fprintf(stderr,
    392  1.13  christos 	    "Usage: %s [-k | -m] [-d timeout] [-e timeout] [-f framebuffer]\n",
    393  1.13  christos 	    getprogname());
    394   1.1   thorpej 	exit(1);
    395   1.1   thorpej }
    396