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