xref: /netbsd-src/usr.sbin/screenblank/screenblank.c (revision 2d6640c450b81cf156eff24f060f723333916837)
1*2d6640c4Sjoerg /*	$NetBSD: screenblank.c,v 1.29 2011/08/30 20:33:30 joerg Exp $	*/
2dcbb2c50Sthorpej 
3c479d69aSthorpej /*-
4cd95ffb6Slukem  * Copyright (c) 1996-2002 The NetBSD Foundation, Inc.
5dcbb2c50Sthorpej  * All rights reserved.
6dcbb2c50Sthorpej  *
7c479d69aSthorpej  * This code is derived from software contributed to The NetBSD Foundation
8c479d69aSthorpej  * by Jason R. Thorpe.
9c479d69aSthorpej  *
10dcbb2c50Sthorpej  * Redistribution and use in source and binary forms, with or without
11dcbb2c50Sthorpej  * modification, are permitted provided that the following conditions
12dcbb2c50Sthorpej  * are met:
13dcbb2c50Sthorpej  * 1. Redistributions of source code must retain the above copyright
14dcbb2c50Sthorpej  *    notice, this list of conditions and the following disclaimer.
15dcbb2c50Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
16dcbb2c50Sthorpej  *    notice, this list of conditions and the following disclaimer in the
17dcbb2c50Sthorpej  *    documentation and/or other materials provided with the distribution.
18dcbb2c50Sthorpej  *
19c479d69aSthorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20c479d69aSthorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21c479d69aSthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2287f4ccd4Sjtc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2387f4ccd4Sjtc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24c479d69aSthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25c479d69aSthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26c479d69aSthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27c479d69aSthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28c479d69aSthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29c479d69aSthorpej  * POSSIBILITY OF SUCH DAMAGE.
30dcbb2c50Sthorpej  */
31dcbb2c50Sthorpej 
32dcbb2c50Sthorpej /*
33cd95ffb6Slukem  * Screensaver daemon for the Sun 3 and SPARC, and platforms using WSCONS.
34dcbb2c50Sthorpej  */
35dcbb2c50Sthorpej 
3635a6561eSthorpej #include <sys/cdefs.h>
3735a6561eSthorpej #ifndef lint
389c194566Slukem __COPYRIGHT("@(#) Copyright (c) 1996-2002\
39cdee73fdSthorpej  The NetBSD Foundation, Inc.  All rights reserved.");
40*2d6640c4Sjoerg __RCSID("$NetBSD: screenblank.c,v 1.29 2011/08/30 20:33:30 joerg Exp $");
4135a6561eSthorpej #endif
4235a6561eSthorpej 
43dcbb2c50Sthorpej #include <sys/types.h>
44dcbb2c50Sthorpej #include <sys/time.h>
45dcbb2c50Sthorpej #include <sys/stat.h>
46dcbb2c50Sthorpej #include <sys/ioctl.h>
47dcbb2c50Sthorpej #include <sys/queue.h>
48dcbb2c50Sthorpej #include <ctype.h>
49dcbb2c50Sthorpej #include <err.h>
50dcbb2c50Sthorpej #include <errno.h>
51dcbb2c50Sthorpej #include <fcntl.h>
52dcbb2c50Sthorpej #include <limits.h>
53dcbb2c50Sthorpej #include <math.h>
54dcbb2c50Sthorpej #include <paths.h>
55dcbb2c50Sthorpej #include <stdlib.h>
56dcbb2c50Sthorpej #include <stdio.h>
57dcbb2c50Sthorpej #include <string.h>
58dcbb2c50Sthorpej #include <signal.h>
59ffea4be4Schristos #include <syslog.h>
60dcbb2c50Sthorpej #include <unistd.h>
61bcd46591Sthorpej #include <util.h>
62dcbb2c50Sthorpej 
63cdee73fdSthorpej #include <dev/wscons/wsconsio.h>
64cdee73fdSthorpej 
65cdee73fdSthorpej #ifdef HAVE_FBIO
66f189453cSthorpej #include <dev/sun/fbio.h>
67cdee73fdSthorpej #endif
68dcbb2c50Sthorpej 
69dcbb2c50Sthorpej #include "pathnames.h"
70dcbb2c50Sthorpej 
71*2d6640c4Sjoerg static u_long	setvideo = WSDISPLAYIO_SVIDEO;		/* "set video" ioctl */
72*2d6640c4Sjoerg static int	videoon  = WSDISPLAYIO_VIDEO_ON;	/* value for "on" */
73*2d6640c4Sjoerg static int	videooff = WSDISPLAYIO_VIDEO_OFF;	/* value for "off" */
74cdee73fdSthorpej 
75dcbb2c50Sthorpej struct	dev_stat {
76dcbb2c50Sthorpej 	LIST_ENTRY(dev_stat) ds_link;	/* linked list */
77cdee73fdSthorpej 	const char *ds_path;		/* path to device */
78dcbb2c50Sthorpej 	int	ds_isfb;		/* boolean; framebuffer? */
79dcbb2c50Sthorpej 	time_t	ds_atime;		/* time device last accessed */
80dcbb2c50Sthorpej 	time_t	ds_mtime;		/* time device last modified */
81dcbb2c50Sthorpej };
82*2d6640c4Sjoerg static LIST_HEAD(ds_list, dev_stat) ds_list;
83dcbb2c50Sthorpej 
84ffea4be4Schristos static	void add_dev(const char *, int);
85ffea4be4Schristos static	void change_state(int);
867fa9b492Smycroft static	void cvt_arg(char *, struct timespec *);
87*2d6640c4Sjoerg __dead static	void sighandler(int);
88845189a2Schristos static	int is_graphics_fb(struct dev_stat *);
89*2d6640c4Sjoerg __dead static	void usage(void);
90dcbb2c50Sthorpej 
91dcbb2c50Sthorpej int
main(int argc,char * argv[])92ffea4be4Schristos main(int argc, char *argv[])
93dcbb2c50Sthorpej {
94dcbb2c50Sthorpej 	struct dev_stat *dsp;
957fa9b492Smycroft 	struct timespec timo_on, timo_off, *tvp, tv;
96dcbb2c50Sthorpej 	struct sigaction sa;
97dcbb2c50Sthorpej 	struct stat st;
98dcbb2c50Sthorpej 	int ch, change, fflag = 0, kflag = 0, mflag = 0, state;
99741d9f3bSelad 	int bflag = 0, uflag = 0;
100cdee73fdSthorpej 	const char *kbd, *mouse, *display;
101dcbb2c50Sthorpej 
102dcbb2c50Sthorpej 	LIST_INIT(&ds_list);
103dcbb2c50Sthorpej 
104dcbb2c50Sthorpej 	/*
105dcbb2c50Sthorpej 	 * Set the default timeouts: 10 minutes on, .25 seconds off.
106dcbb2c50Sthorpej 	 */
107dcbb2c50Sthorpej 	timo_on.tv_sec = 600;
1087fa9b492Smycroft 	timo_on.tv_nsec = 0;
109dcbb2c50Sthorpej 	timo_off.tv_sec = 0;
1107fa9b492Smycroft 	timo_off.tv_nsec = 250000000;
111dcbb2c50Sthorpej 
112741d9f3bSelad 	while ((ch = getopt(argc, argv, "bd:e:f:i:kmu")) != -1) {
113dcbb2c50Sthorpej 		switch (ch) {
114741d9f3bSelad 		case 'b':
115741d9f3bSelad 			bflag = 1;
116741d9f3bSelad 			uflag = 0;
117741d9f3bSelad 			break;
118741d9f3bSelad 
119dcbb2c50Sthorpej 		case 'd':
120dcbb2c50Sthorpej 			cvt_arg(optarg, &timo_on);
121dcbb2c50Sthorpej 			break;
122dcbb2c50Sthorpej 
123dcbb2c50Sthorpej 		case 'e':
124dcbb2c50Sthorpej 			cvt_arg(optarg, &timo_off);
125dcbb2c50Sthorpej 			break;
126dcbb2c50Sthorpej 
127dcbb2c50Sthorpej 		case 'f':
128dcbb2c50Sthorpej 			fflag = 1;
129dcbb2c50Sthorpej 			add_dev(optarg, 1);
130dcbb2c50Sthorpej 			break;
131dcbb2c50Sthorpej 
13225cdf6cfSlukem 		case 'i':
13325cdf6cfSlukem 			add_dev(optarg, 0);
13425cdf6cfSlukem 			break;
13525cdf6cfSlukem 
136dcbb2c50Sthorpej 		case 'k':
137dcbb2c50Sthorpej 			if (mflag || kflag)
138dcbb2c50Sthorpej 				usage();
139dcbb2c50Sthorpej 			kflag = 1;
140dcbb2c50Sthorpej 			break;
141dcbb2c50Sthorpej 
142dcbb2c50Sthorpej 		case 'm':
143dcbb2c50Sthorpej 			if (kflag || mflag)
144dcbb2c50Sthorpej 				usage();
145dcbb2c50Sthorpej 			mflag = 1;
146dcbb2c50Sthorpej 			break;
147dcbb2c50Sthorpej 
148741d9f3bSelad 		case 'u':
149741d9f3bSelad 			uflag = 1;
150741d9f3bSelad 			bflag = 0;
151741d9f3bSelad 			break;
152741d9f3bSelad 
153dcbb2c50Sthorpej 		default:
154dcbb2c50Sthorpej 			usage();
155dcbb2c50Sthorpej 		}
156dcbb2c50Sthorpej 	}
157dcbb2c50Sthorpej 	argc -= optind;
158dcbb2c50Sthorpej 	if (argc)
159dcbb2c50Sthorpej 		usage();
160dcbb2c50Sthorpej 
161dcbb2c50Sthorpej 	/*
162cdee73fdSthorpej 	 * Default to WSCONS support.
163cdee73fdSthorpej 	 */
164cdee73fdSthorpej 	kbd = _PATH_WSKBD;
165cdee73fdSthorpej 	mouse = _PATH_WSMOUSE;
166cdee73fdSthorpej 	display = _PATH_WSDISPLAY;
167cdee73fdSthorpej 
168cdee73fdSthorpej #ifdef HAVE_FBIO
169cdee73fdSthorpej 	/*
170cdee73fdSthorpej 	 * If a display device wasn't specified, check to see which we
171cdee73fdSthorpej 	 * have.  If we can't open the WSCONS display, fall back to fbio.
172cdee73fdSthorpej 	 */
173cdee73fdSthorpej 	if (!fflag) {
174cdee73fdSthorpej 		int fd;
175cdee73fdSthorpej 
176cdee73fdSthorpej 		if ((fd = open(display, O_RDONLY, 0666)) == -1)
177cdee73fdSthorpej 			setvideo = FBIOSVIDEO;
178cdee73fdSthorpej 		else
179cdee73fdSthorpej 			(void) close(fd);
180cdee73fdSthorpej 	}
181cdee73fdSthorpej 
182cdee73fdSthorpej 	/*
183cdee73fdSthorpej 	 * Do this here so that -f ... args above can influence us.
184cdee73fdSthorpej 	 */
185cdee73fdSthorpej 	if (setvideo == FBIOSVIDEO) {
186cdee73fdSthorpej 		videoon = FBVIDEO_ON;
187cdee73fdSthorpej 		videooff = FBVIDEO_OFF;
188cdee73fdSthorpej 		kbd = _PATH_KEYBOARD;
189cdee73fdSthorpej 		mouse = _PATH_MOUSE;
190cdee73fdSthorpej 		display = _PATH_FB;
191cdee73fdSthorpej 	}
192cdee73fdSthorpej #endif
193cdee73fdSthorpej 
194cdee73fdSthorpej 	/*
195d2af0aadSuwe 	 * Add the default framebuffer device if necessary.
196d2af0aadSuwe 	 * We _always_ check the console device.
197d2af0aadSuwe 	 */
198d2af0aadSuwe 	add_dev(_PATH_CONSOLE, 0);
199d2af0aadSuwe 	if (!fflag)
200d2af0aadSuwe 		add_dev(display, 1);
201d2af0aadSuwe 
202d2af0aadSuwe 	/*
203d2af0aadSuwe 	 * If this is an one-off blank/unblank request, handle it now.
204d2af0aadSuwe 	 * We don't need to open keyboard/mouse device for that.
205741d9f3bSelad 	 */
206741d9f3bSelad 	if (bflag || uflag) {
207741d9f3bSelad 		change_state(bflag ? videooff : videoon);
208741d9f3bSelad 		exit(0);
209741d9f3bSelad 	}
210741d9f3bSelad 
211d2af0aadSuwe 
212d2af0aadSuwe 	/* Add the keyboard and mouse devices as necessary. */
213dcbb2c50Sthorpej 	if (!kflag)
214cdee73fdSthorpej 		add_dev(kbd, 0);
215dcbb2c50Sthorpej 	if (!mflag)
216cdee73fdSthorpej 		add_dev(mouse, 0);
217dcbb2c50Sthorpej 
218dcbb2c50Sthorpej 	/* Ensure that the framebuffer is on. */
219cdee73fdSthorpej 	state = videoon;
220dcbb2c50Sthorpej 	change_state(state);
221dcbb2c50Sthorpej 	tvp = &timo_on;
222dcbb2c50Sthorpej 
223dcbb2c50Sthorpej 	/*
224dcbb2c50Sthorpej 	 * Make sure the framebuffer gets turned back on when we're
225dcbb2c50Sthorpej 	 * killed.
226dcbb2c50Sthorpej 	 */
227dcbb2c50Sthorpej 	sa.sa_handler = sighandler;
228dcbb2c50Sthorpej 	sa.sa_flags = SA_NOCLDSTOP;
2295ab89821Sthorpej 	if (sigemptyset(&sa.sa_mask))
2305ab89821Sthorpej 		err(1, "sigemptyset");
231dcbb2c50Sthorpej 	if (sigaction(SIGINT, &sa, NULL) || sigaction(SIGTERM, &sa, NULL) ||
232dcbb2c50Sthorpej 	    sigaction(SIGHUP, &sa, NULL))
233dcbb2c50Sthorpej 		err(1, "sigaction");
234dcbb2c50Sthorpej 
235ffea4be4Schristos 	openlog("screenblank", LOG_PID, LOG_DAEMON);
236dcbb2c50Sthorpej 	/* Detach. */
237dcbb2c50Sthorpej 	if (daemon(0, 0))
238dcbb2c50Sthorpej 		err(1, "daemon");
239bcd46591Sthorpej 	pidfile(NULL);
240dcbb2c50Sthorpej 
241dcbb2c50Sthorpej 	/* Start the state machine. */
242dcbb2c50Sthorpej 	for (;;) {
243dcbb2c50Sthorpej 		change = 0;
244a5478619Speter 		LIST_FOREACH(dsp, &ds_list, ds_link) {
245845189a2Schristos 			/* Don't check framebuffers in graphics mode. */
246845189a2Schristos 			if (is_graphics_fb(dsp))
247dcbb2c50Sthorpej 				continue;
248ffea4be4Schristos 			if (stat(dsp->ds_path, &st) == -1) {
249ffea4be4Schristos 				syslog(LOG_CRIT,
250ffea4be4Schristos 				    "Can't stat `%s' (%m)", dsp->ds_path);
251ffea4be4Schristos 				exit(1);
252ffea4be4Schristos 			}
253dcbb2c50Sthorpej 			if (st.st_atime > dsp->ds_atime) {
254dcbb2c50Sthorpej 				change = 1;
255dcbb2c50Sthorpej 				dsp->ds_atime = st.st_atime;
256dcbb2c50Sthorpej 			}
257dcbb2c50Sthorpej 			if (st.st_mtime > dsp->ds_mtime) {
258dcbb2c50Sthorpej 				change = 1;
259dcbb2c50Sthorpej 				dsp->ds_mtime = st.st_mtime;
260dcbb2c50Sthorpej 			}
261dcbb2c50Sthorpej 		}
262dcbb2c50Sthorpej 
263cdee73fdSthorpej 		if (state == videoon) {
264dcbb2c50Sthorpej 			if (!change) {
265cdee73fdSthorpej 				state = videooff;
266dcbb2c50Sthorpej 				change_state(state);
267dcbb2c50Sthorpej 				tvp = &timo_off;
268dcbb2c50Sthorpej 			}
269cdee73fdSthorpej 		} else {
270dcbb2c50Sthorpej 			if (change) {
271cdee73fdSthorpej 				state = videoon;
272dcbb2c50Sthorpej 				change_state(state);
273dcbb2c50Sthorpej 				tvp = &timo_on;
274dcbb2c50Sthorpej 			}
275dcbb2c50Sthorpej 		}
276dcbb2c50Sthorpej 
277ffea4be4Schristos 		tv = *tvp;
2787fa9b492Smycroft 		if (nanosleep(&tv, NULL) == -1)
2797fa9b492Smycroft 			err(1, "nanosleep");
280dcbb2c50Sthorpej 	}
281dcbb2c50Sthorpej 	/* NOTREACHED */
282dcbb2c50Sthorpej }
283dcbb2c50Sthorpej 
284dcbb2c50Sthorpej static void
add_dev(const char * path,int isfb)285ffea4be4Schristos add_dev(const char *path, int isfb)
286dcbb2c50Sthorpej {
287cdee73fdSthorpej 	struct dev_stat *dsp;
28887faac94Saugustss 	struct stat sb;
289cdee73fdSthorpej 
2905d206b8dSaugustss 	/* Make sure we can stat the device. */
29187faac94Saugustss 	if (stat(path, &sb) == -1) {
29287faac94Saugustss 		warn("Can't stat `%s'", path);
293ffea4be4Schristos 		return;
294ffea4be4Schristos 	}
295cdee73fdSthorpej 
296cdee73fdSthorpej #ifdef HAVE_FBIO
297cdee73fdSthorpej 	/*
298cdee73fdSthorpej 	 * We default to WSCONS.  If this is a frame buffer
299cdee73fdSthorpej 	 * device, check to see if it responds to the old
300cdee73fdSthorpej 	 * Sun-style fbio ioctls.  If so, switch to fbio mode.
301cdee73fdSthorpej 	 */
302cdee73fdSthorpej 	if (isfb && setvideo != FBIOSVIDEO) {
30387faac94Saugustss 		int onoff, fd;
304cdee73fdSthorpej 
30587faac94Saugustss 		if ((fd = open(path, O_RDWR, 0666)) == -1) {
30687faac94Saugustss 			warn("Can't open `%s'", path);
30787faac94Saugustss 			return;
30887faac94Saugustss 		}
309cdee73fdSthorpej 		if ((ioctl(fd, FBIOGVIDEO, &onoff)) == 0)
310cdee73fdSthorpej 			setvideo = FBIOSVIDEO;
31187faac94Saugustss 		(void)close(fd);
312cdee73fdSthorpej 	}
313cdee73fdSthorpej #endif
314cdee73fdSthorpej 
315dcbb2c50Sthorpej 	/* Create the entry... */
316cdee73fdSthorpej 	dsp = malloc(sizeof(struct dev_stat));
317cdee73fdSthorpej 	if (dsp == NULL)
318ffea4be4Schristos 		err(1, "Can't allocate memory for `%s'", path);
319ffea4be4Schristos 	(void)memset(dsp, 0, sizeof(struct dev_stat));
320cdee73fdSthorpej 	dsp->ds_path = path;
321cdee73fdSthorpej 	dsp->ds_isfb = isfb;
322dcbb2c50Sthorpej 
323dcbb2c50Sthorpej 	/* ...and put it in the list. */
324cdee73fdSthorpej 	LIST_INSERT_HEAD(&ds_list, dsp, ds_link);
325dcbb2c50Sthorpej }
326dcbb2c50Sthorpej 
327dcbb2c50Sthorpej /* ARGSUSED */
328dcbb2c50Sthorpej static void
sighandler(int sig)329ffea4be4Schristos sighandler(int sig)
330dcbb2c50Sthorpej {
331dcbb2c50Sthorpej 
332dcbb2c50Sthorpej 	/* Kill the pid file and re-enable the framebuffer before exit. */
333cdee73fdSthorpej 	change_state(videoon);
334dcbb2c50Sthorpej 	exit(0);
335dcbb2c50Sthorpej }
336dcbb2c50Sthorpej 
337845189a2Schristos /*
338845189a2Schristos  * Return 1 if we are a framebuffer in graphics mode or a framebuffer
339845189a2Schristos  * where we cannot tell the mode. Return 0 if we are not a framebuffer
340845189a2Schristos  * device, or a wscons framebuffer in text mode.
341845189a2Schristos  */
342845189a2Schristos static int
is_graphics_fb(struct dev_stat * dsp)343845189a2Schristos is_graphics_fb(struct dev_stat *dsp)
344845189a2Schristos {
345845189a2Schristos 	int fd;
346845189a2Schristos 	int state;
347845189a2Schristos 
348845189a2Schristos 	if (dsp->ds_isfb == 0)
349845189a2Schristos 		return 0;
350845189a2Schristos 
351845189a2Schristos 	/* We can't tell if we are not a wscons device */
352845189a2Schristos 	if (setvideo != WSDISPLAYIO_SVIDEO)
353845189a2Schristos 		return 1;
354845189a2Schristos 
355845189a2Schristos 	if ((fd = open(dsp->ds_path, O_RDWR, 0)) == -1) {
356845189a2Schristos 		syslog(LOG_WARNING, "Cannot open `%s' (%m)", dsp->ds_path);
357845189a2Schristos 		return 1;
358845189a2Schristos 	}
359845189a2Schristos 
360845189a2Schristos 	if (ioctl(fd, WSDISPLAYIO_GMODE, &state) == -1) {
361845189a2Schristos 		syslog(LOG_WARNING, "Cannot get mode on `%s' (%m)",
362845189a2Schristos 		    dsp->ds_path);
363845189a2Schristos 		/* We can't tell, so we say we are mapped */
364845189a2Schristos 		state = WSDISPLAYIO_MODE_MAPPED;
365845189a2Schristos 	}
366845189a2Schristos 
367845189a2Schristos 	(void)close(fd);
368845189a2Schristos 
369845189a2Schristos 	return state != WSDISPLAYIO_MODE_EMUL;
370845189a2Schristos }
371845189a2Schristos 
372dcbb2c50Sthorpej static void
change_state(int state)373ffea4be4Schristos change_state(int state)
374dcbb2c50Sthorpej {
375dcbb2c50Sthorpej 	struct dev_stat *dsp;
376dcbb2c50Sthorpej 	int fd;
377ffea4be4Schristos 	int fail = 1;
378dcbb2c50Sthorpej 
379a5478619Speter 	LIST_FOREACH(dsp, &ds_list, ds_link) {
380dcbb2c50Sthorpej 		/* Don't change the state of non-framebuffers! */
381dcbb2c50Sthorpej 		if (dsp->ds_isfb == 0)
382dcbb2c50Sthorpej 			continue;
383ffea4be4Schristos 		if ((fd = open(dsp->ds_path, O_RDWR, 0)) == -1) {
384ffea4be4Schristos 			syslog(LOG_WARNING, "Can't open `%s' (%m)",
385ffea4be4Schristos 			    dsp->ds_path);
386dcbb2c50Sthorpej 			continue;
387dcbb2c50Sthorpej 		}
388ffea4be4Schristos 		if (ioctl(fd, setvideo, &state) == -1)
389ffea4be4Schristos 			syslog(LOG_WARNING, "Can't set video on `%s' (%m)",
390ffea4be4Schristos 			    dsp->ds_path);
391ffea4be4Schristos 		else
392ffea4be4Schristos 			fail = 0;
393dcbb2c50Sthorpej 		(void)close(fd);
394dcbb2c50Sthorpej 	}
395ffea4be4Schristos 	if (fail) {
396ffea4be4Schristos 		syslog(LOG_CRIT, "No frame buffer devices, exiting\n");
397ffea4be4Schristos 		exit(1);
398ffea4be4Schristos 	}
399dcbb2c50Sthorpej }
400dcbb2c50Sthorpej 
401dcbb2c50Sthorpej static void
cvt_arg(char * arg,struct timespec * tvp)4027fa9b492Smycroft cvt_arg(char *arg, struct timespec *tvp)
403dcbb2c50Sthorpej {
404dcbb2c50Sthorpej 	char *cp;
4057fa9b492Smycroft 	int seconds, nanoseconds, factor;
406dcbb2c50Sthorpej 	int period = 0;
4077fa9b492Smycroft 	factor = 1000000000;
4087fa9b492Smycroft 	nanoseconds = 0;
4096ee30151Sis 	seconds = 0;
410dcbb2c50Sthorpej 
411dcbb2c50Sthorpej 	for (cp = arg; *cp != '\0'; ++cp) {
412dcbb2c50Sthorpej 		if (*cp == '.') {
413dcbb2c50Sthorpej 			if (period)
414ffea4be4Schristos 				errx(1, "Invalid argument: %s", arg);
415dcbb2c50Sthorpej 			period = 1;
416dcbb2c50Sthorpej 			continue;
417dcbb2c50Sthorpej 		}
418dcbb2c50Sthorpej 
4199122339bSdsl 		if (!isdigit((unsigned char)*cp))
420ffea4be4Schristos 			errx(1, "Invalid argument: %s", arg);
421dcbb2c50Sthorpej 
422dcbb2c50Sthorpej 		if (period) {
4236ee30151Sis 			if (factor > 1) {
4247fa9b492Smycroft 				nanoseconds = nanoseconds * 10 + (*cp - '0');
4256ee30151Sis 				factor /= 10;
4266ee30151Sis 			}
427dcbb2c50Sthorpej 		} else
4286ee30151Sis 			seconds = (seconds * 10) + (*cp - '0');
429dcbb2c50Sthorpej 	}
430dcbb2c50Sthorpej 
4316ee30151Sis 	tvp->tv_sec = seconds;
4326ee30151Sis 	if (factor > 1)
4337fa9b492Smycroft 		nanoseconds *= factor;
4346ee30151Sis 
4357fa9b492Smycroft 	tvp->tv_nsec = nanoseconds;
436dcbb2c50Sthorpej }
437dcbb2c50Sthorpej 
438dcbb2c50Sthorpej static void
usage(void)439ffea4be4Schristos usage(void)
440dcbb2c50Sthorpej {
441dcbb2c50Sthorpej 
442ffea4be4Schristos 	(void)fprintf(stderr,
443b635f565Sjmmv 	    "usage: %s [-k | -m] [-d inactivity-timeout] [-e wakeup-delay]\n"
4448651a9dfSwiz 	    "\t\t[-f framebuffer] [-i input-device]\n"
4458651a9dfSwiz 	    "       %s {-b | -u}\n",
4468651a9dfSwiz 	    getprogname(),
447ffea4be4Schristos 	    getprogname());
448dcbb2c50Sthorpej 	exit(1);
449dcbb2c50Sthorpej }
450