Home | History | Annotate | Line # | Download | only in wsmoused
wsmoused.c revision 1.5
      1  1.5  christos /* $NetBSD: wsmoused.c,v 1.5 2002/08/20 16:55:28 christos Exp $ */
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 2002 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.1  christos  * by Julio Merino.
      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.5  christos __RCSID("$NetBSD: wsmoused.c,v 1.5 2002/08/20 16:55:28 christos Exp $");
     36  1.1  christos #endif /* not lint */
     37  1.1  christos 
     38  1.1  christos #include <sys/ioctl.h>
     39  1.1  christos #include <sys/time.h>
     40  1.1  christos #include <sys/types.h>
     41  1.1  christos #include <sys/tty.h>
     42  1.1  christos #include <dev/wscons/wsconsio.h>
     43  1.1  christos 
     44  1.1  christos #include <err.h>
     45  1.1  christos #include <errno.h>
     46  1.1  christos #include <fcntl.h>
     47  1.1  christos #include <poll.h>
     48  1.1  christos #include <signal.h>
     49  1.1  christos #include <stdio.h>
     50  1.1  christos #include <string.h>
     51  1.1  christos #include <stdlib.h>
     52  1.1  christos #include <syslog.h>
     53  1.1  christos #include <unistd.h>
     54  1.1  christos 
     55  1.1  christos #include "pathnames.h"
     56  1.1  christos #include "wsmoused.h"
     57  1.1  christos 
     58  1.1  christos #define IS_MOTION_EVENT(type) (((type) == WSCONS_EVENT_MOUSE_DELTA_X) || \
     59  1.1  christos 			       ((type) == WSCONS_EVENT_MOUSE_DELTA_Y) || \
     60  1.1  christos 			       ((type) == WSCONS_EVENT_MOUSE_DELTA_Z))
     61  1.1  christos #define IS_BUTTON_EVENT(type) (((type) == WSCONS_EVENT_MOUSE_UP) || \
     62  1.1  christos 			       ((type) == WSCONS_EVENT_MOUSE_DOWN))
     63  1.1  christos 
     64  1.1  christos static struct mouse mouse;
     65  1.1  christos int XConsole = -1;
     66  1.1  christos int NoDaemon = 0;
     67  1.1  christos 
     68  1.1  christos /*
     69  1.1  christos  * Show program usage information
     70  1.1  christos  */
     71  1.1  christos static void
     72  1.1  christos usage(void)
     73  1.1  christos {
     74  1.1  christos 	(void)fprintf(stderr,
     75  1.5  christos 	    "Usage: %s [-d device] [-f fifo] [-n] [-X number] [-x number] "
     76  1.5  christos 	    "[-y number]\n",
     77  1.1  christos 	    getprogname());
     78  1.1  christos 	exit(EXIT_FAILURE);
     79  1.1  christos }
     80  1.1  christos 
     81  1.1  christos /*
     82  1.1  christos  * Handler for close signals
     83  1.1  christos  */
     84  1.1  christos static void
     85  1.1  christos signal_terminate(int sig)
     86  1.1  christos {
     87  1.1  christos 	mouse_cursor_hide(&mouse);
     88  1.1  christos 	exit(EXIT_SUCCESS);
     89  1.1  christos }
     90  1.1  christos 
     91  1.1  christos /*
     92  1.1  christos  * Open the mouse device (i.e. /dev/wsmouse). The argument secs
     93  1.1  christos  * specifies the number of seconds we must wait before opening the
     94  1.1  christos  * device. This is used when returning from X. See mouse_open_tty().
     95  1.1  christos  */
     96  1.1  christos static void
     97  1.1  christos mouse_open_device(struct mouse *m, int secs)
     98  1.1  christos {
     99  1.1  christos 	if (m->fd != -1) return;
    100  1.1  christos 
    101  1.1  christos 	sleep(secs);
    102  1.1  christos 
    103  1.1  christos 	/* Open mouse file descriptor */
    104  1.1  christos 	m->fd = open(m->device_name,
    105  1.1  christos 	             O_RDONLY | O_NONBLOCK, 0);
    106  1.1  christos 	if (m->fd == -1) {
    107  1.1  christos 		err(EXIT_FAILURE, "cannot open '%s'",
    108  1.1  christos 		    m->device_name);
    109  1.1  christos 	}
    110  1.1  christos }
    111  1.1  christos 
    112  1.1  christos /*
    113  1.1  christos  * Prepare mouse file descriptors
    114  1.1  christos  */
    115  1.1  christos static void
    116  1.1  christos open_files(void)
    117  1.1  christos {
    118  1.1  christos 	/* Open wsdisplay status device */
    119  1.2  christos 	mouse.stat_fd = open(_PATH_TTYSTAT, O_RDONLY | O_NONBLOCK, 0);
    120  1.1  christos 	if (mouse.stat_fd == -1)
    121  1.2  christos 		err(EXIT_FAILURE, "Cannot open `%s'", _PATH_TTYSTAT);
    122  1.1  christos 
    123  1.1  christos 	mouse.fd = -1;
    124  1.1  christos 	mouse_open_device(&mouse, 0);
    125  1.1  christos 
    126  1.1  christos 	/* Open FIFO, if wanted */
    127  1.1  christos 	if (mouse.fifo_name != NULL) {
    128  1.1  christos 		mouse.fifo_fd = open(mouse.fifo_name, O_RDWR | O_NONBLOCK, 0);
    129  1.1  christos 		if (mouse.fifo_fd == -1)
    130  1.1  christos 			err(EXIT_FAILURE, "Cannot open `%s'", mouse.fifo_name);
    131  1.1  christos 	}
    132  1.1  christos }
    133  1.1  christos 
    134  1.1  christos /*
    135  1.1  christos  * Mouse event loop
    136  1.1  christos  */
    137  1.1  christos static void
    138  1.1  christos event_loop(void)
    139  1.1  christos {
    140  1.1  christos 	int res;
    141  1.1  christos 	struct pollfd fds[2];
    142  1.1  christos 	struct wscons_event event;
    143  1.1  christos 
    144  1.1  christos 	fds[0].fd = mouse.stat_fd;
    145  1.1  christos 	fds[0].events = POLLIN;
    146  1.1  christos 
    147  1.1  christos 	for (;;) {
    148  1.1  christos 		fds[1].fd = mouse.fd;
    149  1.1  christos 		fds[1].events = POLLIN;
    150  1.1  christos 		if (mouse.disabled)
    151  1.1  christos 			res = poll(fds, 1, INFTIM);
    152  1.1  christos 		else
    153  1.1  christos 			res = poll(fds, 2, 300);
    154  1.4  christos 
    155  1.1  christos 		if (res < 0)
    156  1.1  christos 			warn("failed to read from devices");
    157  1.1  christos 
    158  1.1  christos 		if (fds[0].revents & POLLIN) {
    159  1.1  christos 			res = read(mouse.stat_fd, &event, sizeof(event));
    160  1.1  christos 			if (res != sizeof(event))
    161  1.1  christos 				warn("failed to read from mouse stat");
    162  1.1  christos 			screen_event(&mouse, &event);
    163  1.1  christos 		} else if (fds[1].revents & POLLIN) {
    164  1.1  christos 			res = read(mouse.fd, &event, sizeof(event));
    165  1.1  christos 			if (res != sizeof(event))
    166  1.1  christos 				warn("failed to read from mouse");
    167  1.1  christos 
    168  1.1  christos 			if (mouse.fifo_fd >= 0) {
    169  1.1  christos 				res = write(mouse.fifo_fd, &event,
    170  1.1  christos 				            sizeof(event));
    171  1.1  christos 				if (res != sizeof(event))
    172  1.1  christos 					warn("failed to write to fifo");
    173  1.1  christos 			}
    174  1.1  christos 
    175  1.1  christos 			if (IS_MOTION_EVENT(event.type)) {
    176  1.1  christos 				mouse_motion_event(&mouse, &event);
    177  1.1  christos 			} else if (IS_BUTTON_EVENT(event.type)) {
    178  1.1  christos 				mouse_button_event(&mouse, &event);
    179  1.1  christos 			} else {
    180  1.1  christos 				warn("unknown wsmouse event");
    181  1.1  christos 			}
    182  1.1  christos 		} else
    183  1.1  christos 			if (!mouse.selecting) mouse_cursor_hide(&mouse);
    184  1.1  christos 	}
    185  1.1  christos }
    186  1.1  christos 
    187  1.1  christos /*
    188  1.1  christos  * Initializes mouse structure coordinate information. The mouse will
    189  1.1  christos  * be displayed at the center of the screen at start.
    190  1.1  christos  */
    191  1.1  christos static void
    192  1.1  christos mouse_init(void)
    193  1.1  christos {
    194  1.1  christos 	struct winsize ws;
    195  1.1  christos 	struct wsdisplay_char ch;
    196  1.1  christos 	int i;
    197  1.1  christos 
    198  1.1  christos 	/* Get terminal size */
    199  1.1  christos 	if (ioctl(0, TIOCGWINSZ, &ws) < 0)
    200  1.1  christos 		err(EXIT_FAILURE, "Cannot get terminal size");
    201  1.1  christos 
    202  1.1  christos 	/* Open current tty */
    203  1.1  christos 	ioctl(mouse.stat_fd, WSDISPLAYIO_GETACTIVESCREEN, &i);
    204  1.1  christos 	mouse.tty_fd = -1;
    205  1.1  christos 	mouse_open_tty(&mouse, i);
    206  1.4  christos 
    207  1.1  christos 	/* Check if the kernel has character functions */
    208  1.1  christos 	ch.row = ch.col = 0;
    209  1.1  christos 	if (ioctl(mouse.tty_fd, WSDISPLAYIO_GETWSCHAR, &ch) < 0)
    210  1.1  christos 		err(EXIT_FAILURE, "ioctl(WSDISPLAYIO_GETWSCHAR) failed");
    211  1.1  christos 
    212  1.1  christos 	mouse.max_row = ws.ws_row - 1;
    213  1.1  christos 	mouse.max_col = ws.ws_col - 1;
    214  1.1  christos 	mouse.row = mouse.max_row / 2;
    215  1.1  christos 	mouse.col = mouse.max_col / 2;
    216  1.1  christos 	mouse.count_row = 0;
    217  1.1  christos 	mouse.count_col = 0;
    218  1.1  christos 	mouse.cursor = 0;
    219  1.1  christos 	mouse.selecting = 0;
    220  1.1  christos }
    221  1.1  christos 
    222  1.1  christos /*
    223  1.1  christos  * Hides the mouse cursor
    224  1.1  christos  */
    225  1.1  christos void
    226  1.1  christos mouse_cursor_hide(struct mouse *m)
    227  1.1  christos {
    228  1.1  christos 	if (!m->cursor) return;
    229  1.1  christos 	char_invert(m, m->row, m->col);
    230  1.1  christos 	m->cursor = 0;
    231  1.1  christos }
    232  1.1  christos 
    233  1.1  christos /*
    234  1.1  christos  * Shows the mouse cursor
    235  1.1  christos  */
    236  1.1  christos void
    237  1.1  christos mouse_cursor_show(struct mouse *m)
    238  1.1  christos {
    239  1.1  christos 	if (m->cursor) return;
    240  1.1  christos 	char_invert(m, m->row, m->col);
    241  1.1  christos 	m->cursor = 1;
    242  1.1  christos }
    243  1.1  christos 
    244  1.1  christos /*
    245  1.1  christos  * Opens the specified tty (we want it for ioctl's). If tty_fd in
    246  1.1  christos  * mouse structure is -1, no close is performed. Otherwise, the old
    247  1.1  christos  * file descriptor is closed and the new one opened.
    248  1.1  christos  */
    249  1.1  christos void
    250  1.1  christos mouse_open_tty(struct mouse *m, int ttyno)
    251  1.1  christos {
    252  1.1  christos 	char buf[20];
    253  1.1  christos 
    254  1.1  christos 	if (m->tty_fd >= 0) close(m->tty_fd);
    255  1.1  christos 	if (ttyno == XConsole) {
    256  1.1  christos 		m->disabled = 1;
    257  1.1  christos 		(void)close(m->fd);
    258  1.1  christos 		m->fd = -1;
    259  1.1  christos 		return;
    260  1.1  christos 	}
    261  1.1  christos 	/* Open with delay. When returning from X, wsmoused keeps busy
    262  1.1  christos 	   some seconds so we have to wait. */
    263  1.1  christos 	mouse_open_device(m, 5);
    264  1.1  christos 	(void)snprintf(buf, sizeof(buf), _PATH_TTYPREFIX "%d", ttyno);
    265  1.1  christos 	m->tty_fd = open(buf, O_RDONLY | O_NONBLOCK);
    266  1.1  christos 	if (m->tty_fd < 0)
    267  1.1  christos 		errx(EXIT_FAILURE, "Cannot open `%s'", buf);
    268  1.1  christos 	m->disabled = 0;
    269  1.1  christos }
    270  1.1  christos 
    271  1.1  christos /*
    272  1.1  christos  * Flip the foreground and background colors on char at coordinates
    273  1.1  christos  */
    274  1.1  christos void
    275  1.1  christos char_invert(struct mouse *m, size_t row, size_t col)
    276  1.1  christos {
    277  1.1  christos 	struct wsdisplay_char ch;
    278  1.1  christos 	int t;
    279  1.1  christos 
    280  1.1  christos 	ch.row = row;
    281  1.1  christos 	ch.col = col;
    282  1.1  christos 
    283  1.1  christos 	if (ioctl(m->tty_fd, WSDISPLAYIO_GETWSCHAR, &ch) == -1) {
    284  1.1  christos 		warn("ioctl(WSDISPLAYIO_GETWSCHAR) failed");
    285  1.1  christos 		return;
    286  1.1  christos 	}
    287  1.1  christos 
    288  1.1  christos 	t = ch.foreground;
    289  1.1  christos 	ch.foreground = ch.background;
    290  1.1  christos 	ch.background = t;
    291  1.1  christos 
    292  1.1  christos 	if (ioctl(m->tty_fd, WSDISPLAYIO_PUTWSCHAR, &ch) == -1)
    293  1.1  christos 		warn("ioctl(WSDISPLAYIO_PUTWSCHAR) failed");
    294  1.1  christos }
    295  1.1  christos 
    296  1.1  christos /*
    297  1.1  christos  * Main function
    298  1.1  christos  */
    299  1.1  christos int
    300  1.1  christos main(int argc, char **argv)
    301  1.1  christos {
    302  1.1  christos 	int opt;
    303  1.1  christos 
    304  1.1  christos 	setprogname(argv[0]);
    305  1.1  christos 
    306  1.1  christos 	/* Setup mouse default data */
    307  1.1  christos 	memset(&mouse, 0, sizeof(struct mouse));
    308  1.1  christos 	mouse.fifo_fd = -1;
    309  1.1  christos 	mouse.device_name = _PATH_DEFAULT_MOUSE;
    310  1.1  christos 	mouse.fifo_name = NULL;
    311  1.1  christos 
    312  1.5  christos 	/* Defaults that can be overriden by options. If you change
    313  1.5  christos 	 * these, update wsmoused.8 accordingly. */
    314  1.5  christos 	mouse.slowdown_x = 0;
    315  1.5  christos 	mouse.slowdown_y = 3;
    316  1.5  christos 
    317  1.1  christos 	/* Parse command line options */
    318  1.5  christos 	while ((opt = getopt(argc, argv, "nf:d:X:x:y:")) != -1) {
    319  1.1  christos 		switch (opt) {
    320  1.1  christos 		case 'd': /* Mouse device name */
    321  1.1  christos 			mouse.device_name = strdup(optarg);
    322  1.1  christos 			break;
    323  1.1  christos 		case 'f': /* FIFO file name */
    324  1.1  christos 			mouse.fifo_name = strdup(optarg);
    325  1.1  christos 			break;
    326  1.1  christos 		case 'n': /* No daemon */
    327  1.1  christos 			NoDaemon = 1;
    328  1.1  christos 			break;
    329  1.1  christos 		case 'X': /* X console number */
    330  1.1  christos 			XConsole = atoi(optarg);
    331  1.5  christos 			break;
    332  1.5  christos 		case 'x': /* x slowdown */
    333  1.5  christos 			mouse.slowdown_x = atoi(optarg);
    334  1.5  christos 			break;
    335  1.5  christos 		case 'y': /* y slowdown */
    336  1.5  christos 			mouse.slowdown_y = atoi(optarg);
    337  1.1  christos 			break;
    338  1.1  christos 		default:
    339  1.1  christos 			usage();
    340  1.1  christos 			/* NOTREACHED */
    341  1.1  christos 		}
    342  1.1  christos 	}
    343  1.1  christos 
    344  1.1  christos 	open_files();
    345  1.1  christos 	mouse_init();
    346  1.1  christos 	mouse_sel_init();
    347  1.1  christos 
    348  1.1  christos 	/* Setup signal handlers */
    349  1.1  christos 	(void)signal(SIGINT,  signal_terminate);
    350  1.1  christos 	(void)signal(SIGKILL, signal_terminate);
    351  1.1  christos 	(void)signal(SIGQUIT, signal_terminate);
    352  1.1  christos 	(void)signal(SIGTERM, signal_terminate);
    353  1.1  christos 
    354  1.1  christos 	if (!NoDaemon) daemon(0, 0);
    355  1.1  christos 	event_loop();
    356  1.1  christos 
    357  1.1  christos 	return EXIT_SUCCESS;
    358  1.1  christos }
    359