xref: /netbsd-src/usr.sbin/tpctl/tp.c (revision 50010e742d7715992428e391d175087da72c3fcf)
1*50010e74Snonaka /*	$NetBSD: tp.c,v 1.9 2009/04/28 10:57:24 nonaka Exp $	*/
2b8ce6e82Stakemura 
3b8ce6e82Stakemura /*-
4bb756166Stakemura  * Copyright (c) 2002, 2003 TAKEMRUA Shin
5b8ce6e82Stakemura  * All rights reserved.
6b8ce6e82Stakemura  *
7b8ce6e82Stakemura  * Redistribution and use in source and binary forms, with or without
8b8ce6e82Stakemura  * modification, are permitted provided that the following conditions
9b8ce6e82Stakemura  * are met:
10b8ce6e82Stakemura  * 1. Redistributions of source code must retain the above copyright
11b8ce6e82Stakemura  *    notice, this list of conditions and the following disclaimer.
12b8ce6e82Stakemura  * 2. Redistributions in binary form must reproduce the above copyright
13b8ce6e82Stakemura  *    notice, this list of conditions and the following disclaimer in the
14b8ce6e82Stakemura  *    documentation and/or other materials provided with the distribution.
155d1469bdSmartin  * 3. Neither the name of The NetBSD Foundation nor the names of its
165d1469bdSmartin  *    contributors may be used to endorse or promote products derived
175d1469bdSmartin  *    from this software without specific prior written permission.
18b8ce6e82Stakemura  *
19b8ce6e82Stakemura  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20b8ce6e82Stakemura  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21b8ce6e82Stakemura  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22b8ce6e82Stakemura  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23b8ce6e82Stakemura  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24b8ce6e82Stakemura  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25b8ce6e82Stakemura  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26b8ce6e82Stakemura  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27b8ce6e82Stakemura  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28b8ce6e82Stakemura  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29b8ce6e82Stakemura  * POSSIBILITY OF SUCH DAMAGE.
30b8ce6e82Stakemura  */
31b8ce6e82Stakemura 
32b8ce6e82Stakemura #include <stdio.h>
33bb756166Stakemura #include <string.h>
34b8ce6e82Stakemura #include <sys/ioctl.h>
35b8ce6e82Stakemura #include <sys/fcntl.h>
36b8ce6e82Stakemura #include <sys/mman.h>
37b8ce6e82Stakemura #include <errno.h>
38b8ce6e82Stakemura #include <unistd.h>
39b8ce6e82Stakemura 
40b8ce6e82Stakemura #include "tpctl.h"
41b8ce6e82Stakemura 
42bb756166Stakemura #define MIN(a, b)	((a) < (b) ? (a) : (b))
43bb756166Stakemura 
44b8ce6e82Stakemura #ifndef lint
45b8ce6e82Stakemura #include <sys/cdefs.h>
46*50010e74Snonaka __RCSID("$NetBSD: tp.c,v 1.9 2009/04/28 10:57:24 nonaka Exp $");
47b8ce6e82Stakemura #endif /* not lint */
48b8ce6e82Stakemura 
49b8ce6e82Stakemura int
tp_init(struct tp * tp,int fd)50b8ce6e82Stakemura tp_init(struct tp *tp, int fd)
51b8ce6e82Stakemura {
52b8ce6e82Stakemura 	u_int flags;
53b8ce6e82Stakemura 	struct wsmouse_calibcoords calibcoords;
54bb756166Stakemura 	struct wsmouse_id id;
55674f8b31Snonaka #ifdef WSMOUSEIO_SETVERSION
56674f8b31Snonaka 	int version = WSMOUSE_EVENT_VERSION;
57674f8b31Snonaka 
58674f8b31Snonaka 	if (ioctl(fd, WSMOUSEIO_SETVERSION, &version) == -1) {
59674f8b31Snonaka 	    return (-1);
60674f8b31Snonaka 	}
61674f8b31Snonaka #endif
62b8ce6e82Stakemura 
63b8ce6e82Stakemura 	tp->fd = fd;
64b8ce6e82Stakemura 
65b8ce6e82Stakemura #if 0
66b8ce6e82Stakemura 	if (ioctl(tp->fd, WSMOUSEIO_GTYPE, &type) < 0)
67b8ce6e82Stakemura 		return (-1);
68b8ce6e82Stakemura 	if (type != WSMOUSE_TYPE_TPANEL) {
69b8ce6e82Stakemura 		errno = EINVAL;
70b8ce6e82Stakemura 		return (-1);
71b8ce6e82Stakemura 	}
72b8ce6e82Stakemura #else
73b8ce6e82Stakemura 	if (ioctl(tp->fd, WSMOUSEIO_GCALIBCOORDS, &calibcoords) < 0)
74b8ce6e82Stakemura 		return (-1);
75b8ce6e82Stakemura #endif
76b8ce6e82Stakemura 	flags = fcntl(tp->fd, F_GETFL);
77*50010e74Snonaka 	if (flags == (u_int)-1)
78b8ce6e82Stakemura 		return (-1);
79b8ce6e82Stakemura 	flags |= O_NONBLOCK;
80b8ce6e82Stakemura 	if (fcntl(tp->fd, F_SETFL, flags) < 0)
81b8ce6e82Stakemura 		return (-1);
82b8ce6e82Stakemura 
83bb756166Stakemura 	id.type = WSMOUSE_ID_TYPE_UIDSTR;
84bb756166Stakemura 	if (ioctl(tp->fd, WSMOUSEIO_GETID, &id) == 0) {
855ea47b2fSuwe 		(void)strlcpy(tp->id, (char *)id.data,
865efcb4b5Suwe 			      MIN(sizeof(tp->id), id.length));
87bb756166Stakemura 	} else {
88b8ce6e82Stakemura 		tp->id[0] = '*';
89b8ce6e82Stakemura 		tp->id[1] = '\0';
90bb756166Stakemura 	}
91b8ce6e82Stakemura 
92b8ce6e82Stakemura 	return (0);
93b8ce6e82Stakemura }
94b8ce6e82Stakemura 
95b8ce6e82Stakemura int
tp_setrawmode(struct tp * tp)96b8ce6e82Stakemura tp_setrawmode(struct tp *tp)
97b8ce6e82Stakemura {
98b8ce6e82Stakemura 	struct wsmouse_calibcoords raw;
99b8ce6e82Stakemura 
100b8ce6e82Stakemura 	memset(&raw, 0, sizeof(raw));
101b8ce6e82Stakemura 	raw.samplelen = WSMOUSE_CALIBCOORDS_RESET;
102b8ce6e82Stakemura 
103b8ce6e82Stakemura 	return ioctl(tp->fd, WSMOUSEIO_SCALIBCOORDS, &raw);
104b8ce6e82Stakemura }
105b8ce6e82Stakemura 
106b8ce6e82Stakemura int
tp_setcalibcoords(struct tp * tp,struct wsmouse_calibcoords * calibcoords)107b8ce6e82Stakemura tp_setcalibcoords(struct tp *tp, struct wsmouse_calibcoords *calibcoords)
108b8ce6e82Stakemura {
109b8ce6e82Stakemura 	return ioctl(tp->fd, WSMOUSEIO_SCALIBCOORDS, calibcoords);
110b8ce6e82Stakemura }
111b8ce6e82Stakemura 
112b8ce6e82Stakemura int
tp_flush(struct tp * tp)113b8ce6e82Stakemura tp_flush(struct tp *tp)
114b8ce6e82Stakemura {
115b8ce6e82Stakemura 	struct wscons_event ev;
116b8ce6e82Stakemura 	int count;
117b8ce6e82Stakemura 
118b8ce6e82Stakemura 	count = 0;
119b8ce6e82Stakemura 	while (read(tp->fd, &ev, sizeof(ev)) == sizeof(ev)) {
120b8ce6e82Stakemura 		switch (ev.type) {
121b8ce6e82Stakemura 		case WSCONS_EVENT_MOUSE_UP:
122b8ce6e82Stakemura 		case WSCONS_EVENT_MOUSE_DOWN:
123b8ce6e82Stakemura 		case WSCONS_EVENT_MOUSE_DELTA_X:
124b8ce6e82Stakemura 		case WSCONS_EVENT_MOUSE_DELTA_Y:
125b8ce6e82Stakemura 		case WSCONS_EVENT_MOUSE_ABSOLUTE_X:
126b8ce6e82Stakemura 		case WSCONS_EVENT_MOUSE_ABSOLUTE_Y:
127b8ce6e82Stakemura 		case WSCONS_EVENT_MOUSE_DELTA_Z:
128b8ce6e82Stakemura 		case WSCONS_EVENT_MOUSE_ABSOLUTE_Z:
129b8ce6e82Stakemura 			count++;
130b8ce6e82Stakemura 			break;
131b8ce6e82Stakemura 
132b8ce6e82Stakemura 		default:
133b8ce6e82Stakemura 			break;
134b8ce6e82Stakemura 		}
135b8ce6e82Stakemura 	}
136b8ce6e82Stakemura 
137b8ce6e82Stakemura 	return (count);
138b8ce6e82Stakemura }
139b8ce6e82Stakemura 
140b8ce6e82Stakemura int
tp_get(struct tp * tp,int * x,int * y,int (* cancel)(void *),void * data)141b8ce6e82Stakemura tp_get(struct tp *tp, int *x, int *y, int (*cancel)(void *), void *data)
142b8ce6e82Stakemura {
143b8ce6e82Stakemura 	struct wscons_event ev;
144b8ce6e82Stakemura 	int x_done, y_done, res;
145b8ce6e82Stakemura 
146b8ce6e82Stakemura 	x_done = y_done = 0;
147b8ce6e82Stakemura 	while (1) {
148b8ce6e82Stakemura 		if (cancel != NULL && (res = (*cancel)(data)) != 0)
149b8ce6e82Stakemura 			return (res);
150b8ce6e82Stakemura 		if ((res = read(tp->fd, &ev, sizeof(ev))) < 0) {
151b8ce6e82Stakemura 			if (errno != EWOULDBLOCK)
152b8ce6e82Stakemura 				return (-1);
153b8ce6e82Stakemura 			continue;
154b8ce6e82Stakemura 		}
155b8ce6e82Stakemura 		if (res != sizeof(ev)) {
156b8ce6e82Stakemura 			errno = EINVAL;
157b8ce6e82Stakemura 			return (-1);
158b8ce6e82Stakemura 		}
159b8ce6e82Stakemura 		switch (ev.type) {
160b8ce6e82Stakemura 		case WSCONS_EVENT_MOUSE_ABSOLUTE_X:
161b8ce6e82Stakemura 			*x = ev.value;
162b8ce6e82Stakemura 			if (y_done)
163b8ce6e82Stakemura 				return (0);
164b8ce6e82Stakemura 			x_done = 1;
165b8ce6e82Stakemura 			break;
166b8ce6e82Stakemura 
167b8ce6e82Stakemura 		case WSCONS_EVENT_MOUSE_ABSOLUTE_Y:
168b8ce6e82Stakemura 			*y = ev.value;
169b8ce6e82Stakemura 			if (x_done)
170b8ce6e82Stakemura 				return (0);
171b8ce6e82Stakemura 			y_done = 1;
172b8ce6e82Stakemura 			break;
173b8ce6e82Stakemura 
174b8ce6e82Stakemura 		default:
175b8ce6e82Stakemura 			break;
176b8ce6e82Stakemura 		}
177b8ce6e82Stakemura 	}
178b8ce6e82Stakemura }
179b8ce6e82Stakemura 
180b8ce6e82Stakemura int
tp_waitup(struct tp * tp,int msec,int (* cancel)(void *),void * data)181b8ce6e82Stakemura tp_waitup(struct tp *tp, int msec, int (*cancel)(void*), void *data)
182b8ce6e82Stakemura {
183b8ce6e82Stakemura 	int res;
184b8ce6e82Stakemura 
185b8ce6e82Stakemura 	while (1) {
186b8ce6e82Stakemura 		if (cancel != NULL && (res = (*cancel)(data)) != 0)
187b8ce6e82Stakemura 			return (res);
188b8ce6e82Stakemura 		usleep(msec * 1000);
189b8ce6e82Stakemura 		if (tp_flush(tp) == 0)
190b8ce6e82Stakemura 			break;
191b8ce6e82Stakemura 	}
192b8ce6e82Stakemura 
193b8ce6e82Stakemura 	return (0);
194b8ce6e82Stakemura }
195