screenblank.c revision 1.9 1 /* $NetBSD: screenblank.c,v 1.9 1998/12/18 01:15:45 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.9 1998/12/18 01:15:45 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
69 #include <dev/wscons/wsconsio.h>
70
71 #ifdef HAVE_FBIO
72 #include <machine/fbio.h>
73 #endif
74
75 #include "pathnames.h"
76
77 u_long setvideo = WSDISPLAYIO_SVIDEO; /* "set video" ioctl */
78 int videoon = WSDISPLAYIO_VIDEO_ON; /* value for "on" */
79 int videooff = WSDISPLAYIO_VIDEO_OFF; /* value for "off" */
80
81 struct dev_stat {
82 LIST_ENTRY(dev_stat) ds_link; /* linked list */
83 const char *ds_path; /* path to device */
84 int ds_isfb; /* boolean; framebuffer? */
85 time_t ds_atime; /* time device last accessed */
86 time_t ds_mtime; /* time device last modified */
87 };
88 LIST_HEAD(ds_list, dev_stat) ds_list;
89
90 extern char *__progname;
91
92 int main __P((int, char *[]));
93 static void add_dev __P((const char *, int));
94 static void change_state __P((int));
95 static void cvt_arg __P((char *, struct timeval *));
96 static void logpid __P((void));
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 logpid();
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 (void)unlink(_PATH_SCREENBLANKPID);
312 change_state(videoon);
313 exit(0);
314 }
315
316 static void
317 change_state(state)
318 int state;
319 {
320 struct dev_stat *dsp;
321 int fd;
322
323 for (dsp = ds_list.lh_first; dsp != NULL; dsp = dsp->ds_link.le_next) {
324 /* Don't change the state of non-framebuffers! */
325 if (dsp->ds_isfb == 0)
326 continue;
327 if ((fd = open(dsp->ds_path, O_RDWR, 0)) < 0) {
328 warn("open: %s", dsp->ds_path);
329 continue;
330 }
331 if (ioctl(fd, setvideo, &state) < 0)
332 warn("ioctl: %s", dsp->ds_path);
333 (void)close(fd);
334 }
335 }
336
337 static void
338 cvt_arg(arg, tvp)
339 char *arg;
340 struct timeval *tvp;
341 {
342 char *cp;
343 int seconds, microseconds, factor;
344 int period = 0;
345 factor = 1000000;
346 microseconds = 0;
347 seconds = 0;
348
349 for (cp = arg; *cp != '\0'; ++cp) {
350 if (*cp == '.') {
351 if (period)
352 errx(1, "invalid argument: %s", arg);
353 period = 1;
354 continue;
355 }
356
357 if (!isdigit(*cp))
358 errx(1, "invalid argument: %s", arg);
359
360 if (period) {
361 if (factor > 1) {
362 microseconds = microseconds * 10 + (*cp - '0');
363 factor /= 10;
364 }
365 } else
366 seconds = (seconds * 10) + (*cp - '0');
367 }
368
369 tvp->tv_sec = seconds;
370 if (factor > 1)
371 microseconds *= factor;
372
373 tvp->tv_usec = microseconds;
374 }
375
376 static void
377 logpid()
378 {
379 FILE *fp;
380
381 if ((fp = fopen(_PATH_SCREENBLANKPID, "w")) != NULL) {
382 fprintf(fp, "%u\n", getpid());
383 (void)fclose(fp);
384 }
385 }
386
387 static void
388 usage()
389 {
390
391 fprintf(stderr, "usage: %s [-k | -m] [-d timeout] [-e timeout] %s\n",
392 __progname, "[-f framebuffer]");
393 exit(1);
394 }
395