Home | History | Annotate | Line # | Download | only in tpctl
tp.c revision 1.1
      1 /*	$NetBSD: tp.c,v 1.1 2002/08/27 14:12:18 takemura Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002 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 <sys/ioctl.h>
     34 #include <sys/fcntl.h>
     35 #include <sys/mman.h>
     36 #include <errno.h>
     37 #include <unistd.h>
     38 
     39 #include "tpctl.h"
     40 
     41 #ifndef lint
     42 #include <sys/cdefs.h>
     43 __RCSID("$NetBSD: tp.c,v 1.1 2002/08/27 14:12:18 takemura 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 
     52 	tp->fd = fd;
     53 
     54 #if 0
     55 	if (ioctl(tp->fd, WSMOUSEIO_GTYPE, &type) < 0)
     56 		return (-1);
     57 	if (type != WSMOUSE_TYPE_TPANEL) {
     58 		errno = EINVAL;
     59 		return (-1);
     60 	}
     61 #else
     62 	if (ioctl(tp->fd, WSMOUSEIO_GCALIBCOORDS, &calibcoords) < 0)
     63 		return (-1);
     64 #endif
     65 	flags = fcntl(tp->fd, F_GETFL);
     66 	if (flags == -1)
     67 		return (-1);
     68 	flags |= O_NONBLOCK;
     69 	if (fcntl(tp->fd, F_SETFL, flags) < 0)
     70 		return (-1);
     71 
     72 	/*
     73 	 * XXX, currently, you can't retrieve any identifier
     74 	 */
     75 	tp->id[0] = '*';
     76 	tp->id[1] = '\0';
     77 
     78 	return (0);
     79 }
     80 
     81 int
     82 tp_setrawmode(struct tp *tp)
     83 {
     84 	struct wsmouse_calibcoords raw;
     85 
     86 	memset(&raw, 0, sizeof(raw));
     87 	raw.samplelen = WSMOUSE_CALIBCOORDS_RESET;
     88 
     89 	return ioctl(tp->fd, WSMOUSEIO_SCALIBCOORDS, &raw);
     90 }
     91 
     92 int
     93 tp_setcalibcoords(struct tp *tp, struct wsmouse_calibcoords *calibcoords)
     94 {
     95 	return ioctl(tp->fd, WSMOUSEIO_SCALIBCOORDS, calibcoords);
     96 }
     97 
     98 int
     99 tp_flush(struct tp *tp)
    100 {
    101 	struct wscons_event ev;
    102 	int count;
    103 
    104 	count = 0;
    105 	while (read(tp->fd, &ev, sizeof(ev)) == sizeof(ev)) {
    106 		switch (ev.type) {
    107 		case WSCONS_EVENT_MOUSE_UP:
    108 		case WSCONS_EVENT_MOUSE_DOWN:
    109 		case WSCONS_EVENT_MOUSE_DELTA_X:
    110 		case WSCONS_EVENT_MOUSE_DELTA_Y:
    111 		case WSCONS_EVENT_MOUSE_ABSOLUTE_X:
    112 		case WSCONS_EVENT_MOUSE_ABSOLUTE_Y:
    113 		case WSCONS_EVENT_MOUSE_DELTA_Z:
    114 		case WSCONS_EVENT_MOUSE_ABSOLUTE_Z:
    115 			count++;
    116 			break;
    117 
    118 		default:
    119 			break;
    120 		}
    121 	}
    122 
    123 	return (count);
    124 }
    125 
    126 int
    127 tp_get(struct tp *tp, int *x, int *y, int (*cancel)(void *), void *data)
    128 {
    129 	struct wscons_event ev;
    130 	int x_done, y_done, res;
    131 
    132 	x_done = y_done = 0;
    133 	while (1) {
    134 		if (cancel != NULL && (res = (*cancel)(data)) != 0)
    135 			return (res);
    136 		if ((res = read(tp->fd, &ev, sizeof(ev))) < 0) {
    137 			if (errno != EWOULDBLOCK)
    138 				return (-1);
    139 			continue;
    140 		}
    141 		if (res != sizeof(ev)) {
    142 			errno = EINVAL;
    143 			return (-1);
    144 		}
    145 		switch (ev.type) {
    146 		case WSCONS_EVENT_MOUSE_ABSOLUTE_X:
    147 			*x = ev.value;
    148 			if (y_done)
    149 				return (0);
    150 			x_done = 1;
    151 			break;
    152 
    153 		case WSCONS_EVENT_MOUSE_ABSOLUTE_Y:
    154 			*y = ev.value;
    155 			if (x_done)
    156 				return (0);
    157 			y_done = 1;
    158 			break;
    159 
    160 		default:
    161 			break;
    162 		}
    163 	}
    164 }
    165 
    166 int
    167 tp_waitup(struct tp *tp, int msec, int (*cancel)(void*), void *data)
    168 {
    169 	int res;
    170 
    171 	while (1) {
    172 		if (cancel != NULL && (res = (*cancel)(data)) != 0)
    173 			return (res);
    174 		usleep(msec * 1000);
    175 		if (tp_flush(tp) == 0)
    176 			break;
    177 	}
    178 
    179 	return (0);
    180 }
    181