Home | History | Annotate | Line # | Download | only in tpctl
tp.c revision 1.3
      1  1.3       jrf /*	$NetBSD: tp.c,v 1.3 2004/09/07 13:20:40 jrf 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.1  takemura  * 3. Neither the name of The NetBSD Foundation nor the names of its
     16  1.1  takemura  *    contributors may be used to endorse or promote products derived
     17  1.1  takemura  *    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.3       jrf __RCSID("$NetBSD: tp.c,v 1.3 2004/09/07 13:20:40 jrf 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.3       jrf 		(void)strlcpy(tp->id, id.data, MIN(sizeof(tp->id), id.length));
     79  1.2  takemura 	} else {
     80  1.2  takemura 		tp->id[0] = '*';
     81  1.2  takemura 		tp->id[1] = '\0';
     82  1.2  takemura 	}
     83  1.1  takemura 
     84  1.1  takemura 	return (0);
     85  1.1  takemura }
     86  1.1  takemura 
     87  1.1  takemura int
     88  1.1  takemura tp_setrawmode(struct tp *tp)
     89  1.1  takemura {
     90  1.1  takemura 	struct wsmouse_calibcoords raw;
     91  1.1  takemura 
     92  1.1  takemura 	memset(&raw, 0, sizeof(raw));
     93  1.1  takemura 	raw.samplelen = WSMOUSE_CALIBCOORDS_RESET;
     94  1.1  takemura 
     95  1.1  takemura 	return ioctl(tp->fd, WSMOUSEIO_SCALIBCOORDS, &raw);
     96  1.1  takemura }
     97  1.1  takemura 
     98  1.1  takemura int
     99  1.1  takemura tp_setcalibcoords(struct tp *tp, struct wsmouse_calibcoords *calibcoords)
    100  1.1  takemura {
    101  1.1  takemura 	return ioctl(tp->fd, WSMOUSEIO_SCALIBCOORDS, calibcoords);
    102  1.1  takemura }
    103  1.1  takemura 
    104  1.1  takemura int
    105  1.1  takemura tp_flush(struct tp *tp)
    106  1.1  takemura {
    107  1.1  takemura 	struct wscons_event ev;
    108  1.1  takemura 	int count;
    109  1.1  takemura 
    110  1.1  takemura 	count = 0;
    111  1.1  takemura 	while (read(tp->fd, &ev, sizeof(ev)) == sizeof(ev)) {
    112  1.1  takemura 		switch (ev.type) {
    113  1.1  takemura 		case WSCONS_EVENT_MOUSE_UP:
    114  1.1  takemura 		case WSCONS_EVENT_MOUSE_DOWN:
    115  1.1  takemura 		case WSCONS_EVENT_MOUSE_DELTA_X:
    116  1.1  takemura 		case WSCONS_EVENT_MOUSE_DELTA_Y:
    117  1.1  takemura 		case WSCONS_EVENT_MOUSE_ABSOLUTE_X:
    118  1.1  takemura 		case WSCONS_EVENT_MOUSE_ABSOLUTE_Y:
    119  1.1  takemura 		case WSCONS_EVENT_MOUSE_DELTA_Z:
    120  1.1  takemura 		case WSCONS_EVENT_MOUSE_ABSOLUTE_Z:
    121  1.1  takemura 			count++;
    122  1.1  takemura 			break;
    123  1.1  takemura 
    124  1.1  takemura 		default:
    125  1.1  takemura 			break;
    126  1.1  takemura 		}
    127  1.1  takemura 	}
    128  1.1  takemura 
    129  1.1  takemura 	return (count);
    130  1.1  takemura }
    131  1.1  takemura 
    132  1.1  takemura int
    133  1.1  takemura tp_get(struct tp *tp, int *x, int *y, int (*cancel)(void *), void *data)
    134  1.1  takemura {
    135  1.1  takemura 	struct wscons_event ev;
    136  1.1  takemura 	int x_done, y_done, res;
    137  1.1  takemura 
    138  1.1  takemura 	x_done = y_done = 0;
    139  1.1  takemura 	while (1) {
    140  1.1  takemura 		if (cancel != NULL && (res = (*cancel)(data)) != 0)
    141  1.1  takemura 			return (res);
    142  1.1  takemura 		if ((res = read(tp->fd, &ev, sizeof(ev))) < 0) {
    143  1.1  takemura 			if (errno != EWOULDBLOCK)
    144  1.1  takemura 				return (-1);
    145  1.1  takemura 			continue;
    146  1.1  takemura 		}
    147  1.1  takemura 		if (res != sizeof(ev)) {
    148  1.1  takemura 			errno = EINVAL;
    149  1.1  takemura 			return (-1);
    150  1.1  takemura 		}
    151  1.1  takemura 		switch (ev.type) {
    152  1.1  takemura 		case WSCONS_EVENT_MOUSE_ABSOLUTE_X:
    153  1.1  takemura 			*x = ev.value;
    154  1.1  takemura 			if (y_done)
    155  1.1  takemura 				return (0);
    156  1.1  takemura 			x_done = 1;
    157  1.1  takemura 			break;
    158  1.1  takemura 
    159  1.1  takemura 		case WSCONS_EVENT_MOUSE_ABSOLUTE_Y:
    160  1.1  takemura 			*y = ev.value;
    161  1.1  takemura 			if (x_done)
    162  1.1  takemura 				return (0);
    163  1.1  takemura 			y_done = 1;
    164  1.1  takemura 			break;
    165  1.1  takemura 
    166  1.1  takemura 		default:
    167  1.1  takemura 			break;
    168  1.1  takemura 		}
    169  1.1  takemura 	}
    170  1.1  takemura }
    171  1.1  takemura 
    172  1.1  takemura int
    173  1.1  takemura tp_waitup(struct tp *tp, int msec, int (*cancel)(void*), void *data)
    174  1.1  takemura {
    175  1.1  takemura 	int res;
    176  1.1  takemura 
    177  1.1  takemura 	while (1) {
    178  1.1  takemura 		if (cancel != NULL && (res = (*cancel)(data)) != 0)
    179  1.1  takemura 			return (res);
    180  1.1  takemura 		usleep(msec * 1000);
    181  1.1  takemura 		if (tp_flush(tp) == 0)
    182  1.1  takemura 			break;
    183  1.1  takemura 	}
    184  1.1  takemura 
    185  1.1  takemura 	return (0);
    186  1.1  takemura }
    187