screenblank.c revision 1.1 1 /* $NetBSD: screenblank.c,v 1.1 1995/07/12 04:57:51 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1995 Jason R. Thorpe.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project
18 * by Jason R. Thorpe.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 /*
36 * Screensaver daemon for the Sun 3 and SPARC.
37 */
38
39 #include <sys/types.h>
40 #include <sys/time.h>
41 #include <sys/stat.h>
42 #include <sys/ioctl.h>
43 #include <sys/queue.h>
44 #include <ctype.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <limits.h>
49 #include <math.h>
50 #include <paths.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <signal.h>
55 #include <unistd.h>
56
57 #include <machine/fbio.h>
58
59 #include "pathnames.h"
60
61 struct dev_stat {
62 LIST_ENTRY(dev_stat) ds_link; /* linked list */
63 char *ds_path; /* path to device */
64 int ds_isfb; /* boolean; framebuffer? */
65 time_t ds_atime; /* time device last accessed */
66 time_t ds_mtime; /* time device last modified */
67 };
68 LIST_HEAD(ds_list, dev_stat) ds_list;
69
70 extern char *__progname;
71
72 static void add_dev __P((char *, int));
73 static void change_state __P((int));
74 static void cvt_arg __P((char *, struct timeval *));
75 static void logpid __P((void));
76 static void sighandler __P((int, int, struct sigcontext *));
77 static void usage __P((void));
78
79 int
80 main(argc, argv)
81 int argc;
82 char **argv;
83 {
84 struct dev_stat *dsp;
85 struct timeval timo_on, timo_off, *tvp;
86 struct sigaction sa;
87 struct stat st;
88 int ch, change, fflag = 0, kflag = 0, mflag = 0, state;
89
90 LIST_INIT(&ds_list);
91
92 /*
93 * Set the default timeouts: 10 minutes on, .25 seconds off.
94 */
95 timo_on.tv_sec = 600;
96 timo_on.tv_usec = 0;
97 timo_off.tv_sec = 0;
98 timo_off.tv_usec = 250000;
99
100 while ((ch = getopt(argc, argv, "d:e:f:km")) != -1) {
101 switch (ch) {
102 case 'd':
103 cvt_arg(optarg, &timo_on);
104 break;
105
106 case 'e':
107 cvt_arg(optarg, &timo_off);
108 break;
109
110 case 'f':
111 fflag = 1;
112 add_dev(optarg, 1);
113 break;
114
115 case 'k':
116 if (mflag || kflag)
117 usage();
118 kflag = 1;
119 break;
120
121 case 'm':
122 if (kflag || mflag)
123 usage();
124 mflag = 1;
125 break;
126
127 default:
128 usage();
129 }
130 }
131 argc -= optind;
132 if (argc)
133 usage();
134
135 /*
136 * Add the keyboard, mouse, and default framebuffer devices
137 * as necessary. We _always_ check the console device.
138 */
139 add_dev(_PATH_CONSOLE, 0);
140 if (!kflag)
141 add_dev(_PATH_KEYBOARD, 0);
142 if (!mflag)
143 add_dev(_PATH_MOUSE, 0);
144 if (!fflag)
145 add_dev(_PATH_FB, 1);
146
147 /* Ensure that the framebuffer is on. */
148 state = FBVIDEO_ON;
149 change_state(state);
150 tvp = &timo_on;
151
152 /*
153 * Make sure the framebuffer gets turned back on when we're
154 * killed.
155 */
156 sa.sa_handler = sighandler;
157 sa.sa_mask = 0;
158 sa.sa_flags = SA_NOCLDSTOP;
159 if (sigaction(SIGINT, &sa, NULL) || sigaction(SIGTERM, &sa, NULL) ||
160 sigaction(SIGHUP, &sa, NULL))
161 err(1, "sigaction");
162
163 /* Detach. */
164 if (daemon(0, 0))
165 err(1, "daemon");
166 logpid();
167
168 /* Start the state machine. */
169 for (;;) {
170 change = 0;
171 for (dsp = ds_list.lh_first; dsp != NULL;
172 dsp = dsp->ds_link.le_next) {
173 /* Don't check framebuffers. */
174 if (dsp->ds_isfb)
175 continue;
176 if (stat(dsp->ds_path, &st) < 0)
177 err(1, "stat: %s", dsp->ds_path);
178 if (st.st_atime > dsp->ds_atime) {
179 change = 1;
180 dsp->ds_atime = st.st_atime;
181 }
182 if (st.st_mtime > dsp->ds_mtime) {
183 change = 1;
184 dsp->ds_mtime = st.st_mtime;
185 }
186 }
187
188 switch (state) {
189 case FBVIDEO_ON:
190 if (!change) {
191 state = FBVIDEO_OFF;
192 change_state(state);
193 tvp = &timo_off;
194 }
195 break;
196
197 case FBVIDEO_OFF:
198 if (change) {
199 state = FBVIDEO_ON;
200 change_state(state);
201 tvp = &timo_on;
202 }
203 break;
204 }
205
206 if (select(0, NULL, NULL, NULL, tvp) < 0)
207 err(1, "select");
208 }
209 /* NOTREACHED */
210 }
211
212 static void
213 add_dev(path, isfb)
214 char *path;
215 int isfb;
216 {
217 struct dev_stat *dsp1, *dsp2;
218
219 /* Create the entry... */
220 dsp1 = malloc(sizeof(struct dev_stat));
221 if (dsp1 == NULL)
222 errx(1, "can't allocate memory for %s", path);
223 bzero(dsp1, sizeof(struct dev_stat));
224 dsp1->ds_path = path;
225 dsp1->ds_isfb = isfb;
226
227 /* ...and put it in the list. */
228 if (ds_list.lh_first == NULL) {
229 LIST_INSERT_HEAD(&ds_list, dsp1, ds_link);
230 } else {
231 for (dsp2 = ds_list.lh_first; dsp2->ds_link.le_next != NULL;
232 dsp2 = dsp2->ds_link.le_next)
233 /* Nothing. */ ;
234 LIST_INSERT_AFTER(dsp2, dsp1, ds_link);
235 }
236 }
237
238 /* ARGSUSED */
239 static void
240 sighandler(sig, code, context)
241 int sig, code;
242 struct sigcontext *context;
243 {
244
245 /* Kill the pid file and re-enable the framebuffer before exit. */
246 (void)unlink(_PATH_SCREENBLANKPID);
247 change_state(FBVIDEO_ON);
248 exit(0);
249 }
250
251 static void
252 change_state(state)
253 int state;
254 {
255 struct dev_stat *dsp;
256 int fd;
257
258 for (dsp = ds_list.lh_first; dsp != NULL; dsp = dsp->ds_link.le_next) {
259 /* Don't change the state of non-framebuffers! */
260 if (dsp->ds_isfb == 0)
261 continue;
262 if ((fd = open(dsp->ds_path, O_RDWR, 0)) < 0) {
263 warn("open: %s", dsp->ds_path);
264 continue;
265 }
266 if (ioctl(fd, FBIOSVIDEO, &state) < 0)
267 warn("ioctl: %s", dsp->ds_path);
268 (void)close(fd);
269 }
270 }
271
272 static void
273 cvt_arg(arg, tvp)
274 char *arg;
275 struct timeval *tvp;
276 {
277 char *cp;
278 double seconds = 0.0, exponent = -1.0;
279 int period = 0;
280
281 for (cp = arg; *cp != '\0'; ++cp) {
282 if (*cp == '.') {
283 if (period)
284 errx(1, "invalid argument: %s", arg);
285 period = 1;
286 continue;
287 }
288
289 if (!isdigit(*cp))
290 errx(1, "invalid argument: %s", arg);
291
292 if (period) {
293 seconds = seconds + ((*cp - '0') * pow(10.0, exponent));
294 exponent -= 1.0;
295 } else
296 seconds = (seconds * 10.0) + (*cp - '0');
297 }
298
299 tvp->tv_sec = (long)seconds;
300 tvp->tv_usec = (long)((seconds - tvp->tv_sec) * 1000000);
301 }
302
303 static void
304 logpid()
305 {
306 FILE *fp;
307
308 if ((fp = fopen(_PATH_SCREENBLANKPID, "w")) != NULL) {
309 fprintf(fp, "%u\n", getpid());
310 (void)fclose(fp);
311 }
312 }
313
314 static void
315 usage()
316 {
317
318 fprintf(stderr, "usage: %s [-k | -m] [-d timeout] [-e timeout] %s\n",
319 __progname, "[-f framebuffer]");
320 exit(1);
321 }
322