Sync usage with man page.
[netbsd-mini2440.git] / usr.sbin / tpctl / tp.c
blobe329ab8480ae97b85ed948ef08a859965d6171a1
1 /* $NetBSD: tp.c,v 1.8 2009/03/03 18:43:15 nonaka Exp $ */
3 /*-
4 * Copyright (c) 2002, 2003 TAKEMRUA Shin
5 * All rights reserved.
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.
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.
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>
40 #include "tpctl.h"
42 #define MIN(a, b) ((a) < (b) ? (a) : (b))
44 #ifndef lint
45 #include <sys/cdefs.h>
46 __RCSID("$NetBSD: tp.c,v 1.8 2009/03/03 18:43:15 nonaka Exp $");
47 #endif /* not lint */
49 int
50 tp_init(struct tp *tp, int fd)
52 u_int flags;
53 struct wsmouse_calibcoords calibcoords;
54 struct wsmouse_id id;
55 #ifdef WSMOUSEIO_SETVERSION
56 int version = WSMOUSE_EVENT_VERSION;
58 if (ioctl(fd, WSMOUSEIO_SETVERSION, &version) == -1) {
59 return (-1);
61 #endif
63 tp->fd = fd;
65 #if 0
66 if (ioctl(tp->fd, WSMOUSEIO_GTYPE, &type) < 0)
67 return (-1);
68 if (type != WSMOUSE_TYPE_TPANEL) {
69 errno = EINVAL;
70 return (-1);
72 #else
73 if (ioctl(tp->fd, WSMOUSEIO_GCALIBCOORDS, &calibcoords) < 0)
74 return (-1);
75 #endif
76 flags = fcntl(tp->fd, F_GETFL);
77 if (flags == (u_int)-1)
78 return (-1);
79 flags |= O_NONBLOCK;
80 if (fcntl(tp->fd, F_SETFL, flags) < 0)
81 return (-1);
83 id.type = WSMOUSE_ID_TYPE_UIDSTR;
84 if (ioctl(tp->fd, WSMOUSEIO_GETID, &id) == 0) {
85 (void)strlcpy(tp->id, (char *)id.data,
86 MIN(sizeof(tp->id), id.length));
87 } else {
88 tp->id[0] = '*';
89 tp->id[1] = '\0';
92 return (0);
95 int
96 tp_setrawmode(struct tp *tp)
98 struct wsmouse_calibcoords raw;
100 memset(&raw, 0, sizeof(raw));
101 raw.samplelen = WSMOUSE_CALIBCOORDS_RESET;
103 return ioctl(tp->fd, WSMOUSEIO_SCALIBCOORDS, &raw);
107 tp_setcalibcoords(struct tp *tp, struct wsmouse_calibcoords *calibcoords)
109 return ioctl(tp->fd, WSMOUSEIO_SCALIBCOORDS, calibcoords);
113 tp_flush(struct tp *tp)
115 struct wscons_event ev;
116 int count;
118 count = 0;
119 while (read(tp->fd, &ev, sizeof(ev)) == sizeof(ev)) {
120 switch (ev.type) {
121 case WSCONS_EVENT_MOUSE_UP:
122 case WSCONS_EVENT_MOUSE_DOWN:
123 case WSCONS_EVENT_MOUSE_DELTA_X:
124 case WSCONS_EVENT_MOUSE_DELTA_Y:
125 case WSCONS_EVENT_MOUSE_ABSOLUTE_X:
126 case WSCONS_EVENT_MOUSE_ABSOLUTE_Y:
127 case WSCONS_EVENT_MOUSE_DELTA_Z:
128 case WSCONS_EVENT_MOUSE_ABSOLUTE_Z:
129 count++;
130 break;
132 default:
133 break;
137 return (count);
141 tp_get(struct tp *tp, int *x, int *y, int (*cancel)(void *), void *data)
143 struct wscons_event ev;
144 int x_done, y_done, res;
146 x_done = y_done = 0;
147 while (1) {
148 if (cancel != NULL && (res = (*cancel)(data)) != 0)
149 return (res);
150 if ((res = read(tp->fd, &ev, sizeof(ev))) < 0) {
151 if (errno != EWOULDBLOCK)
152 return (-1);
153 continue;
155 if (res != sizeof(ev)) {
156 errno = EINVAL;
157 return (-1);
159 switch (ev.type) {
160 case WSCONS_EVENT_MOUSE_ABSOLUTE_X:
161 *x = ev.value;
162 if (y_done)
163 return (0);
164 x_done = 1;
165 break;
167 case WSCONS_EVENT_MOUSE_ABSOLUTE_Y:
168 *y = ev.value;
169 if (x_done)
170 return (0);
171 y_done = 1;
172 break;
174 default:
175 break;
181 tp_waitup(struct tp *tp, int msec, int (*cancel)(void*), void *data)
183 int res;
185 while (1) {
186 if (cancel != NULL && (res = (*cancel)(data)) != 0)
187 return (res);
188 usleep(msec * 1000);
189 if (tp_flush(tp) == 0)
190 break;
193 return (0);