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