1 /* $NetBSD: tp.c,v 1.6 2008/04/29 06:53:04 martin 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <stdio.h> 30 #include <string.h> 31 #include <sys/ioctl.h> 32 #include <sys/fcntl.h> 33 #include <sys/mman.h> 34 #include <errno.h> 35 #include <unistd.h> 36 37 #include "tpctl.h" 38 39 #define MIN(a, b) ((a) < (b) ? (a) : (b)) 40 41 #ifndef lint 42 #include <sys/cdefs.h> 43 __RCSID("$NetBSD: tp.c,v 1.6 2008/04/29 06:53:04 martin Exp $"); 44 #endif /* not lint */ 45 46 int 47 tp_init(struct tp *tp, int fd) 48 { 49 u_int flags; 50 struct wsmouse_calibcoords calibcoords; 51 struct wsmouse_id id; 52 53 tp->fd = fd; 54 55 #if 0 56 if (ioctl(tp->fd, WSMOUSEIO_GTYPE, &type) < 0) 57 return (-1); 58 if (type != WSMOUSE_TYPE_TPANEL) { 59 errno = EINVAL; 60 return (-1); 61 } 62 #else 63 if (ioctl(tp->fd, WSMOUSEIO_GCALIBCOORDS, &calibcoords) < 0) 64 return (-1); 65 #endif 66 flags = fcntl(tp->fd, F_GETFL); 67 if (flags == -1) 68 return (-1); 69 flags |= O_NONBLOCK; 70 if (fcntl(tp->fd, F_SETFL, flags) < 0) 71 return (-1); 72 73 id.type = WSMOUSE_ID_TYPE_UIDSTR; 74 if (ioctl(tp->fd, WSMOUSEIO_GETID, &id) == 0) { 75 (void)strlcpy(tp->id, (char *)id.data, 76 MIN(sizeof(tp->id), id.length)); 77 } else { 78 tp->id[0] = '*'; 79 tp->id[1] = '\0'; 80 } 81 82 return (0); 83 } 84 85 int 86 tp_setrawmode(struct tp *tp) 87 { 88 struct wsmouse_calibcoords raw; 89 90 memset(&raw, 0, sizeof(raw)); 91 raw.samplelen = WSMOUSE_CALIBCOORDS_RESET; 92 93 return ioctl(tp->fd, WSMOUSEIO_SCALIBCOORDS, &raw); 94 } 95 96 int 97 tp_setcalibcoords(struct tp *tp, struct wsmouse_calibcoords *calibcoords) 98 { 99 return ioctl(tp->fd, WSMOUSEIO_SCALIBCOORDS, calibcoords); 100 } 101 102 int 103 tp_flush(struct tp *tp) 104 { 105 struct wscons_event ev; 106 int count; 107 108 count = 0; 109 while (read(tp->fd, &ev, sizeof(ev)) == sizeof(ev)) { 110 switch (ev.type) { 111 case WSCONS_EVENT_MOUSE_UP: 112 case WSCONS_EVENT_MOUSE_DOWN: 113 case WSCONS_EVENT_MOUSE_DELTA_X: 114 case WSCONS_EVENT_MOUSE_DELTA_Y: 115 case WSCONS_EVENT_MOUSE_ABSOLUTE_X: 116 case WSCONS_EVENT_MOUSE_ABSOLUTE_Y: 117 case WSCONS_EVENT_MOUSE_DELTA_Z: 118 case WSCONS_EVENT_MOUSE_ABSOLUTE_Z: 119 count++; 120 break; 121 122 default: 123 break; 124 } 125 } 126 127 return (count); 128 } 129 130 int 131 tp_get(struct tp *tp, int *x, int *y, int (*cancel)(void *), void *data) 132 { 133 struct wscons_event ev; 134 int x_done, y_done, res; 135 136 x_done = y_done = 0; 137 while (1) { 138 if (cancel != NULL && (res = (*cancel)(data)) != 0) 139 return (res); 140 if ((res = read(tp->fd, &ev, sizeof(ev))) < 0) { 141 if (errno != EWOULDBLOCK) 142 return (-1); 143 continue; 144 } 145 if (res != sizeof(ev)) { 146 errno = EINVAL; 147 return (-1); 148 } 149 switch (ev.type) { 150 case WSCONS_EVENT_MOUSE_ABSOLUTE_X: 151 *x = ev.value; 152 if (y_done) 153 return (0); 154 x_done = 1; 155 break; 156 157 case WSCONS_EVENT_MOUSE_ABSOLUTE_Y: 158 *y = ev.value; 159 if (x_done) 160 return (0); 161 y_done = 1; 162 break; 163 164 default: 165 break; 166 } 167 } 168 } 169 170 int 171 tp_waitup(struct tp *tp, int msec, int (*cancel)(void*), void *data) 172 { 173 int res; 174 175 while (1) { 176 if (cancel != NULL && (res = (*cancel)(data)) != 0) 177 return (res); 178 usleep(msec * 1000); 179 if (tp_flush(tp) == 0) 180 break; 181 } 182 183 return (0); 184 } 185