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