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