wsmoused.c revision 1.12 1 1.12 jmmv /* $NetBSD: wsmoused.c,v 1.12 2003/08/06 22:11:50 jmmv Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.7 jmmv * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.11 jmmv * by Julio M. Merino Vidal.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. The name authors may not be used to endorse or promote products
16 1.1 christos * derived from this software without specific prior written
17 1.1 christos * permission.
18 1.1 christos *
19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
20 1.1 christos * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 1.1 christos * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
23 1.1 christos * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 1.1 christos * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 1.1 christos * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 1.1 christos * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 1.1 christos * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos
32 1.1 christos #include <sys/cdefs.h>
33 1.1 christos
34 1.1 christos #ifndef lint
35 1.11 jmmv __COPYRIGHT("@(#) Copyright (c) 2002, 2003\n"
36 1.11 jmmv "The NetBSD Foundation, Inc. All rights reserved.\n");
37 1.12 jmmv __RCSID("$NetBSD: wsmoused.c,v 1.12 2003/08/06 22:11:50 jmmv Exp $");
38 1.1 christos #endif /* not lint */
39 1.1 christos
40 1.1 christos #include <sys/ioctl.h>
41 1.1 christos #include <sys/time.h>
42 1.1 christos #include <sys/types.h>
43 1.1 christos #include <sys/tty.h>
44 1.1 christos #include <dev/wscons/wsconsio.h>
45 1.1 christos
46 1.1 christos #include <err.h>
47 1.1 christos #include <fcntl.h>
48 1.1 christos #include <poll.h>
49 1.1 christos #include <signal.h>
50 1.1 christos #include <stdio.h>
51 1.1 christos #include <string.h>
52 1.1 christos #include <stdlib.h>
53 1.1 christos #include <unistd.h>
54 1.9 jmmv #include <util.h>
55 1.1 christos
56 1.1 christos #include "pathnames.h"
57 1.1 christos #include "wsmoused.h"
58 1.1 christos
59 1.11 jmmv /* --------------------------------------------------------------------- */
60 1.1 christos
61 1.1 christos /*
62 1.11 jmmv * Global variables.
63 1.1 christos */
64 1.11 jmmv
65 1.11 jmmv static struct mouse Mouse;
66 1.11 jmmv static char *Pid_File = NULL;
67 1.11 jmmv static int X_Console = -1;
68 1.11 jmmv
69 1.11 jmmv #ifdef WSMOUSED_SELECTION_MODE
70 1.12 jmmv extern struct mode_bootstrap Action_Mode;
71 1.12 jmmv #endif
72 1.12 jmmv #ifdef WSMOUSED_SELECTION_MODE
73 1.11 jmmv extern struct mode_bootstrap Selection_Mode;
74 1.11 jmmv #endif
75 1.11 jmmv
76 1.12 jmmv #define MAX_MODES 2
77 1.11 jmmv static struct mode_bootstrap *Modes[MAX_MODES];
78 1.11 jmmv static struct mode_bootstrap *Avail_Modes[] = {
79 1.12 jmmv #ifdef WSMOUSED_ACTION_MODE
80 1.12 jmmv &Action_Mode,
81 1.12 jmmv #endif
82 1.11 jmmv #ifdef WSMOUSED_SELECTION_MODE
83 1.11 jmmv &Selection_Mode,
84 1.11 jmmv #endif
85 1.11 jmmv };
86 1.11 jmmv
87 1.11 jmmv /* --------------------------------------------------------------------- */
88 1.11 jmmv
89 1.11 jmmv /*
90 1.11 jmmv * Prototypes for functions private to this module.
91 1.11 jmmv */
92 1.11 jmmv
93 1.11 jmmv static void usage(void);
94 1.11 jmmv static void open_device(unsigned int);
95 1.11 jmmv static void init_mouse(void);
96 1.11 jmmv static void event_loop(void);
97 1.11 jmmv static void generic_wscons_event(struct wscons_event);
98 1.11 jmmv static int attach_mode(const char *);
99 1.11 jmmv static void attach_modes(char *);
100 1.11 jmmv static void detach_mode(const char *);
101 1.11 jmmv static void detach_modes(void);
102 1.11 jmmv static void signal_terminate(int);
103 1.11 jmmv int main(int, char **);
104 1.11 jmmv
105 1.11 jmmv /* --------------------------------------------------------------------- */
106 1.11 jmmv
107 1.11 jmmv /* Shows program usage information and exits. */
108 1.1 christos static void
109 1.1 christos usage(void)
110 1.1 christos {
111 1.11 jmmv
112 1.1 christos (void)fprintf(stderr,
113 1.11 jmmv "Usage: %s [-d device] [-f config_file] [-m modes] [-n]\n",
114 1.1 christos getprogname());
115 1.1 christos exit(EXIT_FAILURE);
116 1.1 christos }
117 1.1 christos
118 1.11 jmmv /* --------------------------------------------------------------------- */
119 1.1 christos
120 1.11 jmmv /* Initializes mouse information. Basically, it opens required files
121 1.11 jmmv * for the daemon to work. */
122 1.1 christos static void
123 1.11 jmmv init_mouse(void)
124 1.1 christos {
125 1.1 christos
126 1.11 jmmv Mouse.m_devfd = -1;
127 1.11 jmmv open_device(0);
128 1.1 christos
129 1.11 jmmv /* Open FIFO, if wanted */
130 1.11 jmmv Mouse.m_fifofd = -1;
131 1.11 jmmv if (Mouse.m_fifoname != NULL) {
132 1.11 jmmv Mouse.m_fifofd = open(Mouse.m_fifoname,
133 1.11 jmmv O_RDWR | O_NONBLOCK, 0);
134 1.11 jmmv if (Mouse.m_fifofd == -1)
135 1.11 jmmv err(EXIT_FAILURE, "cannot open %s", Mouse.m_fifoname);
136 1.1 christos }
137 1.1 christos }
138 1.1 christos
139 1.11 jmmv /* --------------------------------------------------------------------- */
140 1.11 jmmv
141 1.11 jmmv /* Opens the mouse device (if not already opened). The argument `secs'
142 1.11 jmmv * specifies how much seconds the function will wait before trying to
143 1.11 jmmv * open the device; this is used when returning from the X console. */
144 1.1 christos static void
145 1.11 jmmv open_device(unsigned int secs)
146 1.1 christos {
147 1.1 christos
148 1.11 jmmv if (Mouse.m_devfd != -1)
149 1.11 jmmv return;
150 1.11 jmmv
151 1.11 jmmv sleep(secs);
152 1.1 christos
153 1.11 jmmv /* Open mouse file descriptor */
154 1.11 jmmv Mouse.m_devfd = open(Mouse.m_devname, O_RDONLY | O_NONBLOCK, 0);
155 1.11 jmmv if (Mouse.m_devfd == -1)
156 1.11 jmmv err(EXIT_FAILURE, "cannot open %s", Mouse.m_devname);
157 1.1 christos }
158 1.1 christos
159 1.11 jmmv /* --------------------------------------------------------------------- */
160 1.11 jmmv
161 1.11 jmmv /* Main program event loop. This function polls the wscons status
162 1.11 jmmv * device and the mouse device; whenever an event is received, the
163 1.11 jmmv * appropiate callback is fired for all attached modes. If the polls
164 1.11 jmmv * times out (which only appens when the mouse is disabled), another
165 1.11 jmmv * callback is launched. */
166 1.1 christos static void
167 1.1 christos event_loop(void)
168 1.1 christos {
169 1.11 jmmv int i, res;
170 1.1 christos struct pollfd fds[2];
171 1.1 christos struct wscons_event event;
172 1.1 christos
173 1.11 jmmv fds[0].fd = Mouse.m_statfd;
174 1.1 christos fds[0].events = POLLIN;
175 1.1 christos
176 1.1 christos for (;;) {
177 1.11 jmmv fds[1].fd = Mouse.m_devfd;
178 1.1 christos fds[1].events = POLLIN;
179 1.11 jmmv if (Mouse.m_disabled)
180 1.1 christos res = poll(fds, 1, INFTIM);
181 1.1 christos else
182 1.1 christos res = poll(fds, 2, 300);
183 1.4 christos
184 1.1 christos if (res < 0)
185 1.1 christos warn("failed to read from devices");
186 1.1 christos
187 1.1 christos if (fds[0].revents & POLLIN) {
188 1.11 jmmv res = read(Mouse.m_statfd, &event, sizeof(event));
189 1.1 christos if (res != sizeof(event))
190 1.1 christos warn("failed to read from mouse stat");
191 1.11 jmmv
192 1.11 jmmv generic_wscons_event(event);
193 1.11 jmmv
194 1.11 jmmv for (i = 0; i < MAX_MODES && Modes[i] != NULL; i++)
195 1.11 jmmv if (Modes[i]->mb_wscons_event != NULL)
196 1.11 jmmv Modes[i]->mb_wscons_event(event);
197 1.11 jmmv
198 1.1 christos } else if (fds[1].revents & POLLIN) {
199 1.11 jmmv res = read(Mouse.m_devfd, &event, sizeof(event));
200 1.1 christos if (res != sizeof(event))
201 1.1 christos warn("failed to read from mouse");
202 1.1 christos
203 1.11 jmmv if (Mouse.m_fifofd >= 0) {
204 1.11 jmmv res = write(Mouse.m_fifofd, &event,
205 1.1 christos sizeof(event));
206 1.1 christos if (res != sizeof(event))
207 1.1 christos warn("failed to write to fifo");
208 1.1 christos }
209 1.1 christos
210 1.11 jmmv for (i = 0; i < MAX_MODES && Modes[i] != NULL; i++)
211 1.11 jmmv if (Modes[i]->mb_wsmouse_event != NULL)
212 1.11 jmmv Modes[i]->mb_wsmouse_event(event);
213 1.11 jmmv } else {
214 1.11 jmmv for (i = 0; i < MAX_MODES && Modes[i] != NULL; i++)
215 1.11 jmmv if (Modes[i]->mb_poll_timeout != NULL)
216 1.11 jmmv Modes[i]->mb_poll_timeout();
217 1.11 jmmv }
218 1.1 christos }
219 1.1 christos }
220 1.1 christos
221 1.11 jmmv /* --------------------------------------------------------------------- */
222 1.11 jmmv
223 1.11 jmmv /* This function parses generic wscons status events. Actually, it
224 1.11 jmmv * handles the screen switch event to enable or disable the mouse,
225 1.11 jmmv * depending if we are entering or leaving the X console. */
226 1.1 christos static void
227 1.11 jmmv generic_wscons_event(struct wscons_event evt)
228 1.1 christos {
229 1.1 christos
230 1.11 jmmv switch (evt.type) {
231 1.11 jmmv case WSCONS_EVENT_SCREEN_SWITCH:
232 1.11 jmmv if (evt.value == X_Console) {
233 1.11 jmmv Mouse.m_disabled = 1;
234 1.11 jmmv (void)close(Mouse.m_devfd);
235 1.11 jmmv Mouse.m_devfd = -1;
236 1.11 jmmv } else {
237 1.11 jmmv if (Mouse.m_disabled) {
238 1.11 jmmv open_device(5);
239 1.11 jmmv Mouse.m_disabled = 0;
240 1.11 jmmv } else {
241 1.11 jmmv (void)close(Mouse.m_devfd);
242 1.11 jmmv Mouse.m_devfd = -1;
243 1.11 jmmv open_device(0);
244 1.11 jmmv }
245 1.11 jmmv }
246 1.11 jmmv break;
247 1.11 jmmv }
248 1.1 christos }
249 1.1 christos
250 1.11 jmmv /* --------------------------------------------------------------------- */
251 1.11 jmmv
252 1.11 jmmv /* Attaches a mode to the list of active modes, based on its name.
253 1.11 jmmv * Returns 1 on success or 0 if the mode fails to initialize or there is
254 1.11 jmmv * any other problem. */
255 1.11 jmmv static int
256 1.11 jmmv attach_mode(const char *name)
257 1.11 jmmv {
258 1.11 jmmv int i, pos;
259 1.11 jmmv struct mode_bootstrap *mb;
260 1.11 jmmv
261 1.11 jmmv for (i = 0, pos = -1; i < MAX_MODES; i++)
262 1.11 jmmv if (Modes[i] == NULL) {
263 1.11 jmmv pos = i;
264 1.11 jmmv break;
265 1.11 jmmv }
266 1.11 jmmv if (pos == -1) {
267 1.11 jmmv warnx("modes table full; cannot register `%s'", name);
268 1.11 jmmv return 0;
269 1.11 jmmv }
270 1.11 jmmv
271 1.11 jmmv for (i = 0; i < MAX_MODES; i++) {
272 1.11 jmmv mb = Avail_Modes[i];
273 1.11 jmmv if (mb != NULL && strcmp(name, mb->mb_name) == 0) {
274 1.11 jmmv int res;
275 1.11 jmmv
276 1.11 jmmv res = mb->mb_startup(&Mouse);
277 1.11 jmmv if (res == 0) {
278 1.11 jmmv warnx("startup failed for `%s' mode",
279 1.11 jmmv mb->mb_name);
280 1.11 jmmv return 0;
281 1.11 jmmv } else {
282 1.11 jmmv Modes[pos] = mb;
283 1.11 jmmv return 1;
284 1.11 jmmv }
285 1.11 jmmv }
286 1.11 jmmv }
287 1.11 jmmv
288 1.11 jmmv warnx("unknown mode `%s' (see the `modes' directive)", name);
289 1.11 jmmv return 0;
290 1.1 christos }
291 1.1 christos
292 1.11 jmmv /* --------------------------------------------------------------------- */
293 1.11 jmmv
294 1.11 jmmv /* Attaches all modes given in the whitespace separated string `list'.
295 1.11 jmmv * A fatal error is produced if no active modes can be attached. */
296 1.11 jmmv static void
297 1.11 jmmv attach_modes(char *list)
298 1.1 christos {
299 1.11 jmmv char *last, *p;
300 1.11 jmmv int count;
301 1.11 jmmv
302 1.11 jmmv /* Attach all requested modes */
303 1.11 jmmv (void)memset(&Modes, 0, sizeof(struct mode_bootstrap) * MAX_MODES);
304 1.11 jmmv for (count = 0, (p = strtok_r(list, " ", &last)); p;
305 1.11 jmmv (p = strtok_r(NULL, " ", &last))) {
306 1.11 jmmv if (attach_mode(p))
307 1.11 jmmv count++;
308 1.11 jmmv }
309 1.11 jmmv
310 1.11 jmmv if (count == 0)
311 1.11 jmmv errx(EXIT_FAILURE, "no active modes found; exiting...");
312 1.1 christos }
313 1.1 christos
314 1.11 jmmv /* --------------------------------------------------------------------- */
315 1.11 jmmv
316 1.11 jmmv /* Detaches a mode from the active modes list based on its name. */
317 1.11 jmmv static void
318 1.11 jmmv detach_mode(const char *name)
319 1.1 christos {
320 1.11 jmmv int i;
321 1.11 jmmv struct mode_bootstrap *mb;
322 1.1 christos
323 1.11 jmmv for (i = 0; i < MAX_MODES; i++) {
324 1.11 jmmv mb = Modes[i];
325 1.11 jmmv if (mb != NULL && strcmp(name, mb->mb_name) == 0) {
326 1.11 jmmv int res;
327 1.11 jmmv
328 1.11 jmmv res = mb->mb_cleanup();
329 1.11 jmmv if (res == 0) {
330 1.11 jmmv warnx("cleanup failed for `%s' mode",
331 1.11 jmmv mb->mb_name);
332 1.11 jmmv return;
333 1.11 jmmv } else {
334 1.11 jmmv Modes[i] = NULL;
335 1.11 jmmv return;
336 1.11 jmmv }
337 1.11 jmmv }
338 1.1 christos }
339 1.11 jmmv
340 1.11 jmmv warnx("unknown mode `%s' (see the `modes' directive)", name);
341 1.1 christos }
342 1.1 christos
343 1.11 jmmv /* --------------------------------------------------------------------- */
344 1.11 jmmv
345 1.11 jmmv /* Detaches all active modes. */
346 1.11 jmmv static void
347 1.11 jmmv detach_modes(void)
348 1.1 christos {
349 1.11 jmmv int i;
350 1.1 christos
351 1.11 jmmv for (i = 0; i < MAX_MODES && Modes[i] != NULL; i++)
352 1.11 jmmv detach_mode(Modes[i]->mb_name);
353 1.11 jmmv }
354 1.1 christos
355 1.11 jmmv /* --------------------------------------------------------------------- */
356 1.1 christos
357 1.11 jmmv /* Signal handler for close signals. The program can only be exited
358 1.11 jmmv * through this function. */
359 1.11 jmmv /* ARGSUSED */
360 1.11 jmmv static void
361 1.11 jmmv signal_terminate(int sig)
362 1.11 jmmv {
363 1.1 christos
364 1.11 jmmv detach_modes();
365 1.11 jmmv config_free();
366 1.11 jmmv exit(EXIT_SUCCESS);
367 1.1 christos }
368 1.1 christos
369 1.11 jmmv /* --------------------------------------------------------------------- */
370 1.11 jmmv
371 1.11 jmmv /* Main program. Parses command line options, reads the configuration
372 1.11 jmmv * file, initializes the mouse and associated files and launches the main
373 1.11 jmmv * event loop. */
374 1.1 christos int
375 1.1 christos main(int argc, char **argv)
376 1.1 christos {
377 1.11 jmmv char *conffile, *modelist, *tstat;
378 1.11 jmmv int needconf, nodaemon, opt;
379 1.11 jmmv struct block *conf;
380 1.1 christos
381 1.1 christos setprogname(argv[0]);
382 1.1 christos
383 1.11 jmmv (void)memset(&Mouse, 0, sizeof(struct mouse));
384 1.7 jmmv conffile = _PATH_CONF;
385 1.11 jmmv modelist = NULL;
386 1.11 jmmv needconf = 0;
387 1.11 jmmv nodaemon = -1;
388 1.6 jmmv
389 1.1 christos /* Parse command line options */
390 1.11 jmmv while ((opt = getopt(argc, argv, "d:f:m:n")) != -1) {
391 1.1 christos switch (opt) {
392 1.1 christos case 'd': /* Mouse device name */
393 1.11 jmmv Mouse.m_devname = optarg;
394 1.1 christos break;
395 1.7 jmmv case 'f': /* Configuration file name */
396 1.7 jmmv needconf = 1;
397 1.7 jmmv conffile = optarg;
398 1.1 christos break;
399 1.11 jmmv case 'm': /* List of modes to activate */
400 1.11 jmmv modelist = optarg;
401 1.11 jmmv break;
402 1.1 christos case 'n': /* No daemon */
403 1.7 jmmv nodaemon = 1;
404 1.1 christos break;
405 1.1 christos default:
406 1.1 christos usage();
407 1.1 christos /* NOTREACHED */
408 1.1 christos }
409 1.1 christos }
410 1.1 christos
411 1.11 jmmv /* Read the configuration file and get some basic properties */
412 1.7 jmmv config_read(conffile, needconf);
413 1.11 jmmv conf = config_get_mode("Global");
414 1.11 jmmv if (nodaemon == -1)
415 1.11 jmmv nodaemon = block_get_propval_int(conf, "nodaemon", 0);
416 1.11 jmmv X_Console = block_get_propval_int(conf, "xconsole", -1);
417 1.7 jmmv
418 1.11 jmmv /* Open wsdisplay status device */
419 1.11 jmmv tstat = block_get_propval(conf, "ttystat", _PATH_TTYSTAT);
420 1.11 jmmv Mouse.m_statfd = open(tstat, O_RDONLY | O_NONBLOCK, 0);
421 1.11 jmmv if (Mouse.m_statfd == -1)
422 1.11 jmmv err(EXIT_FAILURE, "cannot open %s", tstat);
423 1.11 jmmv
424 1.11 jmmv /* Initialize mouse information and attach modes */
425 1.11 jmmv if (Mouse.m_devname == NULL)
426 1.11 jmmv Mouse.m_devname = block_get_propval(conf, "device",
427 1.7 jmmv _PATH_DEFAULT_MOUSE);
428 1.11 jmmv init_mouse();
429 1.11 jmmv if (modelist != NULL)
430 1.11 jmmv attach_modes(modelist);
431 1.11 jmmv else
432 1.11 jmmv attach_modes(block_get_propval(conf, "modes", "selection"));
433 1.1 christos
434 1.1 christos /* Setup signal handlers */
435 1.1 christos (void)signal(SIGINT, signal_terminate);
436 1.1 christos (void)signal(SIGKILL, signal_terminate);
437 1.1 christos (void)signal(SIGQUIT, signal_terminate);
438 1.1 christos (void)signal(SIGTERM, signal_terminate);
439 1.1 christos
440 1.9 jmmv if (!nodaemon) {
441 1.9 jmmv /* Become a daemon */
442 1.9 jmmv if (daemon(0, 0) == -1)
443 1.9 jmmv err(EXIT_FAILURE, "failed to become a daemon");
444 1.9 jmmv
445 1.9 jmmv /* Create the pidfile, if wanted */
446 1.11 jmmv Pid_File = block_get_propval(conf, "pidfile", NULL);
447 1.11 jmmv if (pidfile(Pid_File) == -1)
448 1.11 jmmv warn("pidfile %s", Pid_File);
449 1.9 jmmv }
450 1.11 jmmv
451 1.1 christos event_loop();
452 1.1 christos
453 1.7 jmmv /* NOTREACHED */
454 1.1 christos return EXIT_SUCCESS;
455 1.1 christos }
456