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