Home | History | Annotate | Line # | Download | only in tpctl
      1  1.9    nonaka /*	$NetBSD: tp.c,v 1.9 2009/04/28 10:57:24 nonaka 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.9    nonaka __RCSID("$NetBSD: tp.c,v 1.9 2009/04/28 10:57:24 nonaka 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.8    nonaka #ifdef WSMOUSEIO_SETVERSION
     56  1.8    nonaka 	int version = WSMOUSE_EVENT_VERSION;
     57  1.8    nonaka 
     58  1.8    nonaka 	if (ioctl(fd, WSMOUSEIO_SETVERSION, &version) == -1) {
     59  1.8    nonaka 	    return (-1);
     60  1.8    nonaka 	}
     61  1.8    nonaka #endif
     62  1.1  takemura 
     63  1.1  takemura 	tp->fd = fd;
     64  1.1  takemura 
     65  1.1  takemura #if 0
     66  1.1  takemura 	if (ioctl(tp->fd, WSMOUSEIO_GTYPE, &type) < 0)
     67  1.1  takemura 		return (-1);
     68  1.1  takemura 	if (type != WSMOUSE_TYPE_TPANEL) {
     69  1.1  takemura 		errno = EINVAL;
     70  1.1  takemura 		return (-1);
     71  1.1  takemura 	}
     72  1.1  takemura #else
     73  1.1  takemura 	if (ioctl(tp->fd, WSMOUSEIO_GCALIBCOORDS, &calibcoords) < 0)
     74  1.1  takemura 		return (-1);
     75  1.1  takemura #endif
     76  1.1  takemura 	flags = fcntl(tp->fd, F_GETFL);
     77  1.9    nonaka 	if (flags == (u_int)-1)
     78  1.1  takemura 		return (-1);
     79  1.1  takemura 	flags |= O_NONBLOCK;
     80  1.1  takemura 	if (fcntl(tp->fd, F_SETFL, flags) < 0)
     81  1.1  takemura 		return (-1);
     82  1.1  takemura 
     83  1.2  takemura 	id.type = WSMOUSE_ID_TYPE_UIDSTR;
     84  1.2  takemura 	if (ioctl(tp->fd, WSMOUSEIO_GETID, &id) == 0) {
     85  1.5       uwe 		(void)strlcpy(tp->id, (char *)id.data,
     86  1.4       uwe 			      MIN(sizeof(tp->id), id.length));
     87  1.2  takemura 	} else {
     88  1.2  takemura 		tp->id[0] = '*';
     89  1.2  takemura 		tp->id[1] = '\0';
     90  1.2  takemura 	}
     91  1.1  takemura 
     92  1.1  takemura 	return (0);
     93  1.1  takemura }
     94  1.1  takemura 
     95  1.1  takemura int
     96  1.1  takemura tp_setrawmode(struct tp *tp)
     97  1.1  takemura {
     98  1.1  takemura 	struct wsmouse_calibcoords raw;
     99  1.1  takemura 
    100  1.1  takemura 	memset(&raw, 0, sizeof(raw));
    101  1.1  takemura 	raw.samplelen = WSMOUSE_CALIBCOORDS_RESET;
    102  1.1  takemura 
    103  1.1  takemura 	return ioctl(tp->fd, WSMOUSEIO_SCALIBCOORDS, &raw);
    104  1.1  takemura }
    105  1.1  takemura 
    106  1.1  takemura int
    107  1.1  takemura tp_setcalibcoords(struct tp *tp, struct wsmouse_calibcoords *calibcoords)
    108  1.1  takemura {
    109  1.1  takemura 	return ioctl(tp->fd, WSMOUSEIO_SCALIBCOORDS, calibcoords);
    110  1.1  takemura }
    111  1.1  takemura 
    112  1.1  takemura int
    113  1.1  takemura tp_flush(struct tp *tp)
    114  1.1  takemura {
    115  1.1  takemura 	struct wscons_event ev;
    116  1.1  takemura 	int count;
    117  1.1  takemura 
    118  1.1  takemura 	count = 0;
    119  1.1  takemura 	while (read(tp->fd, &ev, sizeof(ev)) == sizeof(ev)) {
    120  1.1  takemura 		switch (ev.type) {
    121  1.1  takemura 		case WSCONS_EVENT_MOUSE_UP:
    122  1.1  takemura 		case WSCONS_EVENT_MOUSE_DOWN:
    123  1.1  takemura 		case WSCONS_EVENT_MOUSE_DELTA_X:
    124  1.1  takemura 		case WSCONS_EVENT_MOUSE_DELTA_Y:
    125  1.1  takemura 		case WSCONS_EVENT_MOUSE_ABSOLUTE_X:
    126  1.1  takemura 		case WSCONS_EVENT_MOUSE_ABSOLUTE_Y:
    127  1.1  takemura 		case WSCONS_EVENT_MOUSE_DELTA_Z:
    128  1.1  takemura 		case WSCONS_EVENT_MOUSE_ABSOLUTE_Z:
    129  1.1  takemura 			count++;
    130  1.1  takemura 			break;
    131  1.1  takemura 
    132  1.1  takemura 		default:
    133  1.1  takemura 			break;
    134  1.1  takemura 		}
    135  1.1  takemura 	}
    136  1.1  takemura 
    137  1.1  takemura 	return (count);
    138  1.1  takemura }
    139  1.1  takemura 
    140  1.1  takemura int
    141  1.1  takemura tp_get(struct tp *tp, int *x, int *y, int (*cancel)(void *), void *data)
    142  1.1  takemura {
    143  1.1  takemura 	struct wscons_event ev;
    144  1.1  takemura 	int x_done, y_done, res;
    145  1.1  takemura 
    146  1.1  takemura 	x_done = y_done = 0;
    147  1.1  takemura 	while (1) {
    148  1.1  takemura 		if (cancel != NULL && (res = (*cancel)(data)) != 0)
    149  1.1  takemura 			return (res);
    150  1.1  takemura 		if ((res = read(tp->fd, &ev, sizeof(ev))) < 0) {
    151  1.1  takemura 			if (errno != EWOULDBLOCK)
    152  1.1  takemura 				return (-1);
    153  1.1  takemura 			continue;
    154  1.1  takemura 		}
    155  1.1  takemura 		if (res != sizeof(ev)) {
    156  1.1  takemura 			errno = EINVAL;
    157  1.1  takemura 			return (-1);
    158  1.1  takemura 		}
    159  1.1  takemura 		switch (ev.type) {
    160  1.1  takemura 		case WSCONS_EVENT_MOUSE_ABSOLUTE_X:
    161  1.1  takemura 			*x = ev.value;
    162  1.1  takemura 			if (y_done)
    163  1.1  takemura 				return (0);
    164  1.1  takemura 			x_done = 1;
    165  1.1  takemura 			break;
    166  1.1  takemura 
    167  1.1  takemura 		case WSCONS_EVENT_MOUSE_ABSOLUTE_Y:
    168  1.1  takemura 			*y = ev.value;
    169  1.1  takemura 			if (x_done)
    170  1.1  takemura 				return (0);
    171  1.1  takemura 			y_done = 1;
    172  1.1  takemura 			break;
    173  1.1  takemura 
    174  1.1  takemura 		default:
    175  1.1  takemura 			break;
    176  1.1  takemura 		}
    177  1.1  takemura 	}
    178  1.1  takemura }
    179  1.1  takemura 
    180  1.1  takemura int
    181  1.1  takemura tp_waitup(struct tp *tp, int msec, int (*cancel)(void*), void *data)
    182  1.1  takemura {
    183  1.1  takemura 	int res;
    184  1.1  takemura 
    185  1.1  takemura 	while (1) {
    186  1.1  takemura 		if (cancel != NULL && (res = (*cancel)(data)) != 0)
    187  1.1  takemura 			return (res);
    188  1.1  takemura 		usleep(msec * 1000);
    189  1.1  takemura 		if (tp_flush(tp) == 0)
    190  1.1  takemura 			break;
    191  1.1  takemura 	}
    192  1.1  takemura 
    193  1.1  takemura 	return (0);
    194  1.1  takemura }
    195