Home | History | Annotate | Line # | Download | only in wsconsctl
mouse.c revision 1.9
      1  1.9  khorben /*	$NetBSD: mouse.c,v 1.9 2012/12/24 01:27:23 khorben Exp $ */
      2  1.1  hannken 
      3  1.1  hannken /*-
      4  1.9  khorben  * Copyright (c) 1998, 2006, 2012 The NetBSD Foundation, Inc.
      5  1.1  hannken  * All rights reserved.
      6  1.1  hannken  *
      7  1.1  hannken  * This code is derived from software contributed to The NetBSD Foundation
      8  1.5     jmmv  * by Juergen Hannken-Illjes and Julio M. Merino Vidal.
      9  1.1  hannken  *
     10  1.1  hannken  * Redistribution and use in source and binary forms, with or without
     11  1.1  hannken  * modification, are permitted provided that the following conditions
     12  1.1  hannken  * are met:
     13  1.1  hannken  * 1. Redistributions of source code must retain the above copyright
     14  1.1  hannken  *    notice, this list of conditions and the following disclaimer.
     15  1.1  hannken  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  hannken  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  hannken  *    documentation and/or other materials provided with the distribution.
     18  1.1  hannken  *
     19  1.1  hannken  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  hannken  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  hannken  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  hannken  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  hannken  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  hannken  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  hannken  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  hannken  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  hannken  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  hannken  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  hannken  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  hannken  */
     31  1.1  hannken 
     32  1.1  hannken #include <sys/ioctl.h>
     33  1.1  hannken #include <sys/time.h>
     34  1.1  hannken #include <dev/wscons/wsconsio.h>
     35  1.5     jmmv 
     36  1.1  hannken #include <err.h>
     37  1.5     jmmv #include <errno.h>
     38  1.5     jmmv #include <limits.h>
     39  1.5     jmmv #include <stdio.h>
     40  1.5     jmmv #include <stdlib.h>
     41  1.5     jmmv #include <string.h>
     42  1.5     jmmv 
     43  1.1  hannken #include "wsconsctl.h"
     44  1.1  hannken 
     45  1.1  hannken static int mstype;
     46  1.2       ad static int resolution;
     47  1.2       ad static int samplerate;
     48  1.9  khorben static struct wsmouse_calibcoords calibration;
     49  1.9  khorben static char *calibration_samples;
     50  1.5     jmmv static struct wsmouse_repeat repeat;
     51  1.5     jmmv 
     52  1.9  khorben static void mouse_get_calibration(int);
     53  1.9  khorben 
     54  1.5     jmmv static void mouse_get_repeat(int);
     55  1.5     jmmv static void mouse_put_repeat(int);
     56  1.1  hannken 
     57  1.1  hannken struct field mouse_field_tab[] = {
     58  1.2       ad     { "resolution",		&resolution,	FMT_UINT,	FLG_WRONLY },
     59  1.2       ad     { "samplerate",		&samplerate,	FMT_UINT,	FLG_WRONLY },
     60  1.1  hannken     { "type",			&mstype,	FMT_MSTYPE,	FLG_RDONLY },
     61  1.9  khorben     { "calibration.minx",	&calibration.minx,
     62  1.9  khorben     						FMT_INT,	FLG_RDONLY },
     63  1.9  khorben     { "calibration.miny",	&calibration.miny,
     64  1.9  khorben     						FMT_INT,	FLG_RDONLY },
     65  1.9  khorben     { "calibration.maxx",	&calibration.maxx,
     66  1.9  khorben     						FMT_INT,	FLG_RDONLY },
     67  1.9  khorben     { "calibration.maxy",	&calibration.maxy,
     68  1.9  khorben     						FMT_INT,	FLG_RDONLY },
     69  1.9  khorben     { "calibration.samples",	&calibration_samples,
     70  1.9  khorben 	    					FMT_STRING,	FLG_RDONLY },
     71  1.5     jmmv     { "repeat.buttons",		&repeat.wr_buttons,
     72  1.7     jmmv     						FMT_BITFIELD, FLG_MODIFY },
     73  1.5     jmmv     { "repeat.delay.first",	&repeat.wr_delay_first,
     74  1.7     jmmv     						FMT_UINT, FLG_MODIFY },
     75  1.5     jmmv     { "repeat.delay.decrement",	&repeat.wr_delay_decrement,
     76  1.7     jmmv     						FMT_UINT, FLG_MODIFY },
     77  1.5     jmmv     { "repeat.delay.minimum",	&repeat.wr_delay_minimum,
     78  1.7     jmmv  		   				FMT_UINT, FLG_MODIFY },
     79  1.1  hannken };
     80  1.1  hannken 
     81  1.1  hannken int mouse_field_tab_len = sizeof(mouse_field_tab)/
     82  1.1  hannken 			   sizeof(mouse_field_tab[0]);
     83  1.1  hannken 
     84  1.1  hannken void
     85  1.4  xtraeme mouse_get_values(int fd)
     86  1.1  hannken {
     87  1.7     jmmv 
     88  1.1  hannken 	if (field_by_value(&mstype)->flags & FLG_GET)
     89  1.1  hannken 		if (ioctl(fd, WSMOUSEIO_GTYPE, &mstype) < 0)
     90  1.7     jmmv 			err(EXIT_FAILURE, "WSMOUSEIO_GTYPE");
     91  1.5     jmmv 
     92  1.9  khorben 	if (field_by_value(&calibration.minx)->flags & FLG_GET ||
     93  1.9  khorben 	    field_by_value(&calibration.miny)->flags & FLG_GET ||
     94  1.9  khorben 	    field_by_value(&calibration.maxx)->flags & FLG_GET ||
     95  1.9  khorben 	    field_by_value(&calibration.maxy)->flags & FLG_GET ||
     96  1.9  khorben 	    field_by_value(&calibration_samples)->flags & FLG_GET)
     97  1.9  khorben 		mouse_get_calibration(fd);
     98  1.9  khorben 
     99  1.5     jmmv 	if (field_by_value(&repeat.wr_buttons)->flags & FLG_GET ||
    100  1.5     jmmv 	    field_by_value(&repeat.wr_delay_first)->flags & FLG_GET ||
    101  1.5     jmmv 	    field_by_value(&repeat.wr_delay_decrement)->flags & FLG_GET ||
    102  1.5     jmmv 	    field_by_value(&repeat.wr_delay_minimum)->flags & FLG_GET)
    103  1.5     jmmv 		mouse_get_repeat(fd);
    104  1.5     jmmv }
    105  1.5     jmmv 
    106  1.5     jmmv static void
    107  1.9  khorben mouse_get_calibration(int fd)
    108  1.9  khorben {
    109  1.9  khorben 	struct wsmouse_calibcoords tmp;
    110  1.9  khorben 	char *samples;
    111  1.9  khorben 	char buf[48];
    112  1.9  khorben 	int i;
    113  1.9  khorben 
    114  1.9  khorben 	if (ioctl(fd, WSMOUSEIO_GCALIBCOORDS, &tmp) < 0) {
    115  1.9  khorben 		field_disable_by_value(&calibration.minx);
    116  1.9  khorben 		field_disable_by_value(&calibration.miny);
    117  1.9  khorben 		field_disable_by_value(&calibration.maxx);
    118  1.9  khorben 		field_disable_by_value(&calibration.maxy);
    119  1.9  khorben 		field_disable_by_value(&calibration_samples);
    120  1.9  khorben 		return;
    121  1.9  khorben 	}
    122  1.9  khorben 
    123  1.9  khorben 	if (field_by_value(&calibration.minx)->flags & FLG_GET)
    124  1.9  khorben 		calibration.minx = tmp.minx;
    125  1.9  khorben 	if (field_by_value(&calibration.miny)->flags & FLG_GET)
    126  1.9  khorben 		calibration.miny = tmp.miny;
    127  1.9  khorben 	if (field_by_value(&calibration.maxx)->flags & FLG_GET)
    128  1.9  khorben 		calibration.maxx = tmp.maxx;
    129  1.9  khorben 	if (field_by_value(&calibration.maxy)->flags & FLG_GET)
    130  1.9  khorben 		calibration.maxy = tmp.maxy;
    131  1.9  khorben 	if (field_by_value(&calibration_samples)->flags & FLG_GET) {
    132  1.9  khorben 		free(calibration_samples);
    133  1.9  khorben 		if (tmp.samplelen <= 0) {
    134  1.9  khorben 			calibration_samples = strdup("");
    135  1.9  khorben 			if (calibration_samples == NULL)
    136  1.9  khorben 				err(EXIT_FAILURE, "could not list calibration"
    137  1.9  khorben 						" samples");
    138  1.9  khorben 		} else {
    139  1.9  khorben 			samples = malloc(tmp.samplelen * sizeof(buf));
    140  1.9  khorben 			if (samples == NULL)
    141  1.9  khorben 				err(EXIT_FAILURE, "could not list calibration"
    142  1.9  khorben 						" samples");
    143  1.9  khorben 			samples[0] = '\0';
    144  1.9  khorben 			for (i = 0; i < tmp.samplelen; i++) {
    145  1.9  khorben 				snprintf(buf, sizeof(buf), "%s%d,%d,%d,%d",
    146  1.9  khorben 						(i == 0) ? "" : ":",
    147  1.9  khorben 						tmp.samples[i].rawx,
    148  1.9  khorben 						tmp.samples[i].rawy,
    149  1.9  khorben 						tmp.samples[i].x,
    150  1.9  khorben 						tmp.samples[i].y);
    151  1.9  khorben 				strcat(samples, buf);
    152  1.9  khorben 			}
    153  1.9  khorben 			calibration_samples = samples;
    154  1.9  khorben 		}
    155  1.9  khorben 	}
    156  1.9  khorben }
    157  1.9  khorben 
    158  1.9  khorben static void
    159  1.5     jmmv mouse_get_repeat(int fd)
    160  1.5     jmmv {
    161  1.5     jmmv 	struct wsmouse_repeat tmp;
    162  1.5     jmmv 
    163  1.9  khorben 	if (ioctl(fd, WSMOUSEIO_GETREPEAT, &tmp) < 0)
    164  1.7     jmmv 		err(EXIT_FAILURE, "WSMOUSEIO_GETREPEAT");
    165  1.5     jmmv 
    166  1.5     jmmv 	if (field_by_value(&repeat.wr_buttons)->flags & FLG_GET)
    167  1.5     jmmv 		repeat.wr_buttons = tmp.wr_buttons;
    168  1.5     jmmv 	if (field_by_value(&repeat.wr_delay_first)->flags & FLG_GET)
    169  1.5     jmmv 		repeat.wr_delay_first = tmp.wr_delay_first;
    170  1.5     jmmv 	if (field_by_value(&repeat.wr_delay_decrement)->flags & FLG_GET)
    171  1.5     jmmv 		repeat.wr_delay_decrement = tmp.wr_delay_decrement;
    172  1.5     jmmv 	if (field_by_value(&repeat.wr_delay_minimum)->flags & FLG_GET)
    173  1.5     jmmv 		repeat.wr_delay_minimum = tmp.wr_delay_minimum;
    174  1.1  hannken }
    175  1.1  hannken 
    176  1.1  hannken void
    177  1.4  xtraeme mouse_put_values(int fd)
    178  1.1  hannken {
    179  1.3       ad 	int tmp;
    180  1.2       ad 
    181  1.3       ad 	if (field_by_value(&resolution)->flags & FLG_SET) {
    182  1.3       ad 		tmp = resolution;
    183  1.3       ad 		if (ioctl(fd, WSMOUSEIO_SRES, &tmp) < 0)
    184  1.7     jmmv 			err(EXIT_FAILURE, "WSMOUSEIO_SRES");
    185  1.3       ad 		pr_field(field_by_value(&resolution), " -> ");
    186  1.3       ad 	}
    187  1.5     jmmv 
    188  1.3       ad 	if (field_by_value(&samplerate)->flags & FLG_SET) {
    189  1.3       ad 		tmp = samplerate;
    190  1.3       ad 		if (ioctl(fd, WSMOUSEIO_SRATE, &tmp) < 0)
    191  1.7     jmmv 			err(EXIT_FAILURE, "WSMOUSEIO_SRATE");
    192  1.6     jmmv 		pr_field(field_by_value(&samplerate), " -> ");
    193  1.3       ad 	}
    194  1.5     jmmv 
    195  1.5     jmmv 	if (field_by_value(&repeat.wr_buttons)->flags & FLG_SET ||
    196  1.5     jmmv 	    field_by_value(&repeat.wr_delay_first)->flags & FLG_SET ||
    197  1.5     jmmv 	    field_by_value(&repeat.wr_delay_decrement)->flags & FLG_SET ||
    198  1.5     jmmv 	    field_by_value(&repeat.wr_delay_minimum)->flags & FLG_SET)
    199  1.5     jmmv 		mouse_put_repeat(fd);
    200  1.5     jmmv }
    201  1.5     jmmv 
    202  1.5     jmmv static void
    203  1.5     jmmv mouse_put_repeat(int fd)
    204  1.5     jmmv {
    205  1.5     jmmv 	struct wsmouse_repeat tmp;
    206  1.5     jmmv 
    207  1.5     jmmv 	/* Fetch current values into the temporary structure. */
    208  1.9  khorben 	if (ioctl(fd, WSMOUSEIO_GETREPEAT, &tmp) < 0)
    209  1.7     jmmv 		err(EXIT_FAILURE, "WSMOUSEIO_GETREPEAT");
    210  1.5     jmmv 
    211  1.5     jmmv 	/* Overwrite the desired values in the temporary structure. */
    212  1.5     jmmv 	if (field_by_value(&repeat.wr_buttons)->flags & FLG_SET)
    213  1.5     jmmv 		tmp.wr_buttons = repeat.wr_buttons;
    214  1.5     jmmv 	if (field_by_value(&repeat.wr_delay_first)->flags & FLG_SET)
    215  1.5     jmmv 		tmp.wr_delay_first = repeat.wr_delay_first;
    216  1.5     jmmv 	if (field_by_value(&repeat.wr_delay_decrement)->flags & FLG_SET)
    217  1.5     jmmv 		tmp.wr_delay_decrement = repeat.wr_delay_decrement;
    218  1.5     jmmv 	if (field_by_value(&repeat.wr_delay_minimum)->flags & FLG_SET)
    219  1.5     jmmv 		tmp.wr_delay_minimum = repeat.wr_delay_minimum;
    220  1.5     jmmv 
    221  1.5     jmmv 	/* Set new values for repeating events. */
    222  1.9  khorben 	if (ioctl(fd, WSMOUSEIO_SETREPEAT, &tmp) < 0)
    223  1.7     jmmv 		err(EXIT_FAILURE, "WSMOUSEIO_SETREPEAT");
    224  1.5     jmmv 
    225  1.5     jmmv 	/* Now print what changed. */
    226  1.5     jmmv 	if (field_by_value(&repeat.wr_buttons)->flags & FLG_SET)
    227  1.5     jmmv 		pr_field(field_by_value(&repeat.wr_buttons), " -> ");
    228  1.5     jmmv 	if (field_by_value(&repeat.wr_delay_first)->flags & FLG_SET)
    229  1.5     jmmv 		pr_field(field_by_value(&repeat.wr_delay_first), " -> ");
    230  1.5     jmmv 	if (field_by_value(&repeat.wr_delay_decrement)->flags & FLG_SET)
    231  1.5     jmmv 		pr_field(field_by_value(&repeat.wr_delay_decrement), " -> ");
    232  1.5     jmmv 	if (field_by_value(&repeat.wr_delay_minimum)->flags & FLG_SET)
    233  1.5     jmmv 		pr_field(field_by_value(&repeat.wr_delay_minimum), " -> ");
    234  1.1  hannken }
    235