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