wsmoused.c revision 1.9 1 1.9 jmmv /* $NetBSD: wsmoused.c,v 1.9 2003/03/04 22:31:15 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.9 jmmv __RCSID("$NetBSD: wsmoused.c,v 1.9 2003/03/04 22:31:15 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.9 jmmv #include <util.h>
55 1.1 christos
56 1.1 christos #include "pathnames.h"
57 1.1 christos #include "wsmoused.h"
58 1.1 christos
59 1.1 christos #define IS_MOTION_EVENT(type) (((type) == WSCONS_EVENT_MOUSE_DELTA_X) || \
60 1.1 christos ((type) == WSCONS_EVENT_MOUSE_DELTA_Y) || \
61 1.1 christos ((type) == WSCONS_EVENT_MOUSE_DELTA_Z))
62 1.1 christos #define IS_BUTTON_EVENT(type) (((type) == WSCONS_EVENT_MOUSE_UP) || \
63 1.1 christos ((type) == WSCONS_EVENT_MOUSE_DOWN))
64 1.1 christos
65 1.1 christos static struct mouse mouse;
66 1.9 jmmv static char *PidFile = NULL;
67 1.1 christos int XConsole = -1;
68 1.1 christos
69 1.1 christos /*
70 1.1 christos * Show program usage information
71 1.1 christos */
72 1.1 christos static void
73 1.1 christos usage(void)
74 1.1 christos {
75 1.1 christos (void)fprintf(stderr,
76 1.7 jmmv "Usage: %s [-d device] [-f config_file] [-n]\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.7 jmmv config_free();
89 1.1 christos exit(EXIT_SUCCESS);
90 1.1 christos }
91 1.1 christos
92 1.1 christos /*
93 1.1 christos * Open the mouse device (i.e. /dev/wsmouse). The argument secs
94 1.1 christos * specifies the number of seconds we must wait before opening the
95 1.1 christos * device. This is used when returning from X. See mouse_open_tty().
96 1.1 christos */
97 1.1 christos static void
98 1.1 christos mouse_open_device(struct mouse *m, int secs)
99 1.1 christos {
100 1.1 christos if (m->fd != -1) return;
101 1.1 christos
102 1.1 christos sleep(secs);
103 1.1 christos
104 1.1 christos /* Open mouse file descriptor */
105 1.1 christos m->fd = open(m->device_name,
106 1.1 christos O_RDONLY | O_NONBLOCK, 0);
107 1.1 christos if (m->fd == -1) {
108 1.7 jmmv err(EXIT_FAILURE, "cannot open %s", 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.7 jmmv err(EXIT_FAILURE, "cannot open %s", mouse.tstat_name);
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.8 jmmv mouse.fifo_fd = -1;
128 1.1 christos if (mouse.fifo_name != NULL) {
129 1.1 christos mouse.fifo_fd = open(mouse.fifo_name, O_RDWR | O_NONBLOCK, 0);
130 1.1 christos if (mouse.fifo_fd == -1)
131 1.7 jmmv err(EXIT_FAILURE, "cannot open %s", mouse.fifo_name);
132 1.1 christos }
133 1.1 christos }
134 1.1 christos
135 1.1 christos /*
136 1.1 christos * Mouse event loop
137 1.1 christos */
138 1.1 christos static void
139 1.1 christos event_loop(void)
140 1.1 christos {
141 1.1 christos int res;
142 1.1 christos struct pollfd fds[2];
143 1.1 christos struct wscons_event event;
144 1.1 christos
145 1.1 christos fds[0].fd = mouse.stat_fd;
146 1.1 christos fds[0].events = POLLIN;
147 1.1 christos
148 1.1 christos for (;;) {
149 1.1 christos fds[1].fd = mouse.fd;
150 1.1 christos fds[1].events = POLLIN;
151 1.1 christos if (mouse.disabled)
152 1.1 christos res = poll(fds, 1, INFTIM);
153 1.1 christos else
154 1.1 christos res = poll(fds, 2, 300);
155 1.4 christos
156 1.1 christos if (res < 0)
157 1.1 christos warn("failed to read from devices");
158 1.1 christos
159 1.1 christos if (fds[0].revents & POLLIN) {
160 1.1 christos res = read(mouse.stat_fd, &event, sizeof(event));
161 1.1 christos if (res != sizeof(event))
162 1.1 christos warn("failed to read from mouse stat");
163 1.1 christos screen_event(&mouse, &event);
164 1.1 christos } else if (fds[1].revents & POLLIN) {
165 1.1 christos res = read(mouse.fd, &event, sizeof(event));
166 1.1 christos if (res != sizeof(event))
167 1.1 christos warn("failed to read from mouse");
168 1.1 christos
169 1.1 christos if (mouse.fifo_fd >= 0) {
170 1.1 christos res = write(mouse.fifo_fd, &event,
171 1.1 christos sizeof(event));
172 1.1 christos if (res != sizeof(event))
173 1.1 christos warn("failed to write to fifo");
174 1.1 christos }
175 1.1 christos
176 1.1 christos if (IS_MOTION_EVENT(event.type)) {
177 1.1 christos mouse_motion_event(&mouse, &event);
178 1.1 christos } else if (IS_BUTTON_EVENT(event.type)) {
179 1.1 christos mouse_button_event(&mouse, &event);
180 1.1 christos } else {
181 1.1 christos warn("unknown wsmouse event");
182 1.1 christos }
183 1.1 christos } else
184 1.1 christos if (!mouse.selecting) mouse_cursor_hide(&mouse);
185 1.1 christos }
186 1.1 christos }
187 1.1 christos
188 1.1 christos /*
189 1.1 christos * Initializes mouse structure coordinate information. The mouse will
190 1.1 christos * be displayed at the center of the screen at start.
191 1.1 christos */
192 1.1 christos static void
193 1.1 christos mouse_init(void)
194 1.1 christos {
195 1.1 christos struct winsize ws;
196 1.1 christos struct wsdisplay_char ch;
197 1.1 christos int i;
198 1.1 christos
199 1.1 christos /* Get terminal size */
200 1.1 christos if (ioctl(0, TIOCGWINSZ, &ws) < 0)
201 1.7 jmmv err(EXIT_FAILURE, "cannot get terminal size");
202 1.1 christos
203 1.1 christos /* Open current tty */
204 1.1 christos ioctl(mouse.stat_fd, WSDISPLAYIO_GETACTIVESCREEN, &i);
205 1.1 christos mouse.tty_fd = -1;
206 1.1 christos mouse_open_tty(&mouse, i);
207 1.4 christos
208 1.1 christos /* Check if the kernel has character functions */
209 1.1 christos ch.row = ch.col = 0;
210 1.1 christos if (ioctl(mouse.tty_fd, WSDISPLAYIO_GETWSCHAR, &ch) < 0)
211 1.1 christos err(EXIT_FAILURE, "ioctl(WSDISPLAYIO_GETWSCHAR) failed");
212 1.1 christos
213 1.1 christos mouse.max_row = ws.ws_row - 1;
214 1.1 christos mouse.max_col = ws.ws_col - 1;
215 1.1 christos mouse.row = mouse.max_row / 2;
216 1.1 christos mouse.col = mouse.max_col / 2;
217 1.1 christos mouse.count_row = 0;
218 1.1 christos mouse.count_col = 0;
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.7 jmmv 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.7 jmmv int opt, nodaemon = -1;
304 1.7 jmmv int needconf = 0;
305 1.7 jmmv char *conffile;
306 1.7 jmmv struct block *mode;
307 1.1 christos
308 1.1 christos setprogname(argv[0]);
309 1.1 christos
310 1.1 christos memset(&mouse, 0, sizeof(struct mouse));
311 1.7 jmmv conffile = _PATH_CONF;
312 1.6 jmmv
313 1.1 christos /* Parse command line options */
314 1.7 jmmv while ((opt = getopt(argc, argv, "d:f:n")) != -1) {
315 1.1 christos switch (opt) {
316 1.1 christos case 'd': /* Mouse device name */
317 1.7 jmmv mouse.device_name = optarg;
318 1.1 christos break;
319 1.7 jmmv case 'f': /* Configuration file name */
320 1.7 jmmv needconf = 1;
321 1.7 jmmv conffile = optarg;
322 1.1 christos break;
323 1.1 christos case 'n': /* No daemon */
324 1.7 jmmv nodaemon = 1;
325 1.1 christos break;
326 1.1 christos default:
327 1.1 christos usage();
328 1.1 christos /* NOTREACHED */
329 1.1 christos }
330 1.1 christos }
331 1.1 christos
332 1.7 jmmv /* Read the configuration file and get our mode configuration */
333 1.7 jmmv config_read(conffile, needconf);
334 1.7 jmmv mode = config_get_mode("sel");
335 1.7 jmmv
336 1.7 jmmv /* Set values according to the configuration file */
337 1.7 jmmv if (mouse.device_name == NULL)
338 1.7 jmmv mouse.device_name = block_get_propval(mode, "device",
339 1.7 jmmv _PATH_DEFAULT_MOUSE);
340 1.7 jmmv
341 1.7 jmmv if (nodaemon == -1)
342 1.7 jmmv nodaemon = block_get_propval_int(mode, "nodaemon", 0);
343 1.7 jmmv
344 1.7 jmmv mouse.slowdown_x = block_get_propval_int(mode, "slowdown_x", 0);
345 1.7 jmmv mouse.slowdown_y = block_get_propval_int(mode, "slowdown_y", 3);
346 1.7 jmmv mouse.tstat_name = block_get_propval(mode, "ttystat", _PATH_TTYSTAT);
347 1.7 jmmv XConsole = block_get_propval_int(mode, "xconsole", -1);
348 1.7 jmmv
349 1.7 jmmv if (block_get_propval_int(mode, "lefthanded", 0)) {
350 1.7 jmmv mouse.but_select = 2;
351 1.7 jmmv mouse.but_paste = 0;
352 1.7 jmmv } else {
353 1.7 jmmv mouse.but_select = 0;
354 1.7 jmmv mouse.but_paste = 2;
355 1.7 jmmv }
356 1.7 jmmv
357 1.1 christos open_files();
358 1.1 christos mouse_init();
359 1.1 christos mouse_sel_init();
360 1.1 christos
361 1.1 christos /* Setup signal handlers */
362 1.1 christos (void)signal(SIGINT, signal_terminate);
363 1.1 christos (void)signal(SIGKILL, signal_terminate);
364 1.1 christos (void)signal(SIGQUIT, signal_terminate);
365 1.1 christos (void)signal(SIGTERM, signal_terminate);
366 1.1 christos
367 1.9 jmmv if (!nodaemon) {
368 1.9 jmmv /* Become a daemon */
369 1.9 jmmv if (daemon(0, 0) == -1)
370 1.9 jmmv err(EXIT_FAILURE, "failed to become a daemon");
371 1.9 jmmv
372 1.9 jmmv /* Create the pidfile, if wanted */
373 1.9 jmmv PidFile = block_get_propval(mode, "pidfile", NULL);
374 1.9 jmmv if (pidfile(PidFile) == -1)
375 1.9 jmmv warn("pidfile %s", PidFile);
376 1.9 jmmv }
377 1.1 christos event_loop();
378 1.1 christos
379 1.7 jmmv /* NOTREACHED */
380 1.1 christos return EXIT_SUCCESS;
381 1.1 christos }
382