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