xref: /netbsd-src/usr.sbin/screenblank/screenblank.c (revision fd5cb0acea84d278e04e640d37ca2398f894991f)
1 /*	$NetBSD: screenblank.c,v 1.22 2004/11/25 20:23:36 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996-2002 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, and platforms using WSCONS.
41  */
42 
43 #include <sys/cdefs.h>
44 #ifndef lint
45 __COPYRIGHT(
46 "@(#) Copyright (c) 1996-2002 \
47 	The NetBSD Foundation, Inc.  All rights reserved.");
48 __RCSID("$NetBSD: screenblank.c,v 1.22 2004/11/25 20:23:36 christos 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 <syslog.h>
68 #include <unistd.h>
69 #include <util.h>
70 
71 #include <dev/wscons/wsconsio.h>
72 
73 #ifdef HAVE_FBIO
74 #include <dev/sun/fbio.h>
75 #endif
76 
77 #include "pathnames.h"
78 
79 u_long	setvideo = WSDISPLAYIO_SVIDEO;		/* "set video" ioctl */
80 int	videoon  = WSDISPLAYIO_VIDEO_ON;	/* value for "on" */
81 int	videooff = WSDISPLAYIO_VIDEO_OFF;	/* value for "off" */
82 
83 struct	dev_stat {
84 	LIST_ENTRY(dev_stat) ds_link;	/* linked list */
85 	const char *ds_path;		/* path to device */
86 	int	ds_isfb;		/* boolean; framebuffer? */
87 	time_t	ds_atime;		/* time device last accessed */
88 	time_t	ds_mtime;		/* time device last modified */
89 };
90 LIST_HEAD(ds_list, dev_stat) ds_list;
91 
92 int	main(int, char *[]);
93 static	void add_dev(const char *, int);
94 static	void change_state(int);
95 static	void cvt_arg(char *, struct timespec *);
96 static	void sighandler(int);
97 static	int is_graphics_fb(struct dev_stat *);
98 static	void usage(void);
99 
100 int
101 main(int argc, char *argv[])
102 {
103 	struct dev_stat *dsp;
104 	struct timespec timo_on, timo_off, *tvp, tv;
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_nsec = 0;
117 	timo_off.tv_sec = 0;
118 	timo_off.tv_nsec = 250000000;
119 
120 	while ((ch = getopt(argc, argv, "d:e:f:i: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 'i':
136 			add_dev(optarg, 0);
137 			break;
138 
139 		case 'k':
140 			if (mflag || kflag)
141 				usage();
142 			kflag = 1;
143 			break;
144 
145 		case 'm':
146 			if (kflag || mflag)
147 				usage();
148 			mflag = 1;
149 			break;
150 
151 		default:
152 			usage();
153 		}
154 	}
155 	argc -= optind;
156 	if (argc)
157 		usage();
158 
159 	/*
160 	 * Default to WSCONS support.
161 	 */
162 	kbd = _PATH_WSKBD;
163 	mouse = _PATH_WSMOUSE;
164 	display = _PATH_WSDISPLAY;
165 
166 #ifdef HAVE_FBIO
167 	/*
168 	 * If a display device wasn't specified, check to see which we
169 	 * have.  If we can't open the WSCONS display, fall back to fbio.
170 	 */
171 	if (!fflag) {
172 		int fd;
173 
174 		if ((fd = open(display, O_RDONLY, 0666)) == -1)
175 			setvideo = FBIOSVIDEO;
176 		else
177 			(void) close(fd);
178 	}
179 
180 	/*
181 	 * Do this here so that -f ... args above can influence us.
182 	 */
183 	if (setvideo == FBIOSVIDEO) {
184 		videoon = FBVIDEO_ON;
185 		videooff = FBVIDEO_OFF;
186 		kbd = _PATH_KEYBOARD;
187 		mouse = _PATH_MOUSE;
188 		display = _PATH_FB;
189 	}
190 #endif
191 
192 	/*
193 	 * Add the keyboard, mouse, and default framebuffer devices
194 	 * as necessary.  We _always_ check the console device.
195 	 */
196 	add_dev(_PATH_CONSOLE, 0);
197 	if (!kflag)
198 		add_dev(kbd, 0);
199 	if (!mflag)
200 		add_dev(mouse, 0);
201 	if (!fflag)
202 		add_dev(display, 1);
203 
204 	/* Ensure that the framebuffer is on. */
205 	state = videoon;
206 	change_state(state);
207 	tvp = &timo_on;
208 
209 	/*
210 	 * Make sure the framebuffer gets turned back on when we're
211 	 * killed.
212 	 */
213 	sa.sa_handler = sighandler;
214 	sa.sa_flags = SA_NOCLDSTOP;
215 	if (sigemptyset(&sa.sa_mask))
216 		err(1, "sigemptyset");
217 	if (sigaction(SIGINT, &sa, NULL) || sigaction(SIGTERM, &sa, NULL) ||
218 	    sigaction(SIGHUP, &sa, NULL))
219 		err(1, "sigaction");
220 
221 	openlog("screenblank", LOG_PID, LOG_DAEMON);
222 	/* Detach. */
223 	if (daemon(0, 0))
224 		err(1, "daemon");
225 	pidfile(NULL);
226 
227 	/* Start the state machine. */
228 	for (;;) {
229 		change = 0;
230 		for (dsp = ds_list.lh_first; dsp != NULL;
231 		    dsp = dsp->ds_link.le_next) {
232 			/* Don't check framebuffers in graphics mode. */
233 			if (is_graphics_fb(dsp))
234 				continue;
235 			if (stat(dsp->ds_path, &st) == -1) {
236 				syslog(LOG_CRIT,
237 				    "Can't stat `%s' (%m)", dsp->ds_path);
238 				exit(1);
239 			}
240 			if (st.st_atime > dsp->ds_atime) {
241 				change = 1;
242 				dsp->ds_atime = st.st_atime;
243 			}
244 			if (st.st_mtime > dsp->ds_mtime) {
245 				change = 1;
246 				dsp->ds_mtime = st.st_mtime;
247 			}
248 		}
249 
250 		if (state == videoon) {
251 			if (!change) {
252 				state = videooff;
253 				change_state(state);
254 				tvp = &timo_off;
255 			}
256 		} else {
257 			if (change) {
258 				state = videoon;
259 				change_state(state);
260 				tvp = &timo_on;
261 			}
262 		}
263 
264 		tv = *tvp;
265 		if (nanosleep(&tv, NULL) == -1)
266 			err(1, "nanosleep");
267 	}
268 	/* NOTREACHED */
269 }
270 
271 static void
272 add_dev(const char *path, int isfb)
273 {
274 	struct dev_stat *dsp;
275 	struct stat sb;
276 
277 	/* Make sure we can stat the device. */
278 	if (stat(path, &sb) == -1) {
279 		warn("Can't stat `%s'", path);
280 		return;
281 	}
282 
283 #ifdef HAVE_FBIO
284 	/*
285 	 * We default to WSCONS.  If this is a frame buffer
286 	 * device, check to see if it responds to the old
287 	 * Sun-style fbio ioctls.  If so, switch to fbio mode.
288 	 */
289 	if (isfb && setvideo != FBIOSVIDEO) {
290 		int onoff, fd;
291 
292 		if ((fd = open(path, O_RDWR, 0666)) == -1) {
293 			warn("Can't open `%s'", path);
294 			return;
295 		}
296 		if ((ioctl(fd, FBIOGVIDEO, &onoff)) == 0)
297 			setvideo = FBIOSVIDEO;
298 		(void)close(fd);
299 	}
300 #endif
301 
302 	/* Create the entry... */
303 	dsp = malloc(sizeof(struct dev_stat));
304 	if (dsp == NULL)
305 		err(1, "Can't allocate memory for `%s'", path);
306 	(void)memset(dsp, 0, sizeof(struct dev_stat));
307 	dsp->ds_path = path;
308 	dsp->ds_isfb = isfb;
309 
310 	/* ...and put it in the list. */
311 	LIST_INSERT_HEAD(&ds_list, dsp, ds_link);
312 }
313 
314 /* ARGSUSED */
315 static void
316 sighandler(int sig)
317 {
318 
319 	/* Kill the pid file and re-enable the framebuffer before exit. */
320 	change_state(videoon);
321 	exit(0);
322 }
323 
324 /*
325  * Return 1 if we are a framebuffer in graphics mode or a framebuffer
326  * where we cannot tell the mode. Return 0 if we are not a framebuffer
327  * device, or a wscons framebuffer in text mode.
328  */
329 static int
330 is_graphics_fb(struct dev_stat *dsp)
331 {
332 	int fd;
333 	int state;
334 
335 	if (dsp->ds_isfb == 0)
336 		return 0;
337 
338 	/* We can't tell if we are not a wscons device */
339 	if (setvideo != WSDISPLAYIO_SVIDEO)
340 		return 1;
341 
342 	if ((fd = open(dsp->ds_path, O_RDWR, 0)) == -1) {
343 		syslog(LOG_WARNING, "Cannot open `%s' (%m)", dsp->ds_path);
344 		return 1;
345 	}
346 
347 	if (ioctl(fd, WSDISPLAYIO_GMODE, &state) == -1) {
348 		syslog(LOG_WARNING, "Cannot get mode on `%s' (%m)",
349 		    dsp->ds_path);
350 		/* We can't tell, so we say we are mapped */
351 		state = WSDISPLAYIO_MODE_MAPPED;
352 	}
353 
354 	(void)close(fd);
355 
356 	return state != WSDISPLAYIO_MODE_EMUL;
357 }
358 
359 static void
360 change_state(int state)
361 {
362 	struct dev_stat *dsp;
363 	int fd;
364 	int fail = 1;
365 
366 	for (dsp = ds_list.lh_first; dsp != NULL; dsp = dsp->ds_link.le_next) {
367 		/* Don't change the state of non-framebuffers! */
368 		if (dsp->ds_isfb == 0)
369 			continue;
370 		if ((fd = open(dsp->ds_path, O_RDWR, 0)) == -1) {
371 			syslog(LOG_WARNING, "Can't open `%s' (%m)",
372 			    dsp->ds_path);
373 			continue;
374 		}
375 		if (ioctl(fd, setvideo, &state) == -1)
376 			syslog(LOG_WARNING, "Can't set video on `%s' (%m)",
377 			    dsp->ds_path);
378 		else
379 			fail = 0;
380 		(void)close(fd);
381 	}
382 	if (fail) {
383 		syslog(LOG_CRIT, "No frame buffer devices, exiting\n");
384 		exit(1);
385 	}
386 }
387 
388 static void
389 cvt_arg(char *arg, struct timespec *tvp)
390 {
391 	char *cp;
392 	int seconds, nanoseconds, factor;
393 	int period = 0;
394 	factor = 1000000000;
395 	nanoseconds = 0;
396 	seconds = 0;
397 
398 	for (cp = arg; *cp != '\0'; ++cp) {
399 		if (*cp == '.') {
400 			if (period)
401 				errx(1, "Invalid argument: %s", arg);
402 			period = 1;
403 			continue;
404 		}
405 
406 		if (!isdigit((unsigned char)*cp))
407 			errx(1, "Invalid argument: %s", arg);
408 
409 		if (period) {
410 			if (factor > 1) {
411 				nanoseconds = nanoseconds * 10 + (*cp - '0');
412 				factor /= 10;
413 			}
414 		} else
415 			seconds = (seconds * 10) + (*cp - '0');
416 	}
417 
418 	tvp->tv_sec = seconds;
419 	if (factor > 1)
420 		nanoseconds *= factor;
421 
422 	tvp->tv_nsec = nanoseconds;
423 }
424 
425 static void
426 usage(void)
427 {
428 
429 	(void)fprintf(stderr,
430 	    "usage: %s [-k | -m] [-d inactivity-timeout] [-e wakeup-delay]\n"
431 	    "\t\t[-f framebuffer] [-i input-device]\n",
432 	    getprogname());
433 	exit(1);
434 }
435