Home | History | Annotate | Line # | Download | only in wsconsctl
mouse.c revision 1.10
      1  1.10  khorben /*	$NetBSD: mouse.c,v 1.10 2012/12/24 01:29:20 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.10  khorben static void mouse_put_calibration(int);
     54   1.9  khorben 
     55   1.5     jmmv static void mouse_get_repeat(int);
     56   1.5     jmmv static void mouse_put_repeat(int);
     57   1.1  hannken 
     58   1.1  hannken struct field mouse_field_tab[] = {
     59   1.2       ad     { "resolution",		&resolution,	FMT_UINT,	FLG_WRONLY },
     60   1.2       ad     { "samplerate",		&samplerate,	FMT_UINT,	FLG_WRONLY },
     61   1.1  hannken     { "type",			&mstype,	FMT_MSTYPE,	FLG_RDONLY },
     62   1.9  khorben     { "calibration.minx",	&calibration.minx,
     63  1.10  khorben     						FMT_INT,	FLG_MODIFY },
     64   1.9  khorben     { "calibration.miny",	&calibration.miny,
     65  1.10  khorben     						FMT_INT,	FLG_MODIFY },
     66   1.9  khorben     { "calibration.maxx",	&calibration.maxx,
     67  1.10  khorben     						FMT_INT,	FLG_MODIFY },
     68   1.9  khorben     { "calibration.maxy",	&calibration.maxy,
     69  1.10  khorben     						FMT_INT,	FLG_MODIFY },
     70   1.9  khorben     { "calibration.samples",	&calibration_samples,
     71  1.10  khorben 	    					FMT_STRING,	FLG_MODIFY },
     72   1.5     jmmv     { "repeat.buttons",		&repeat.wr_buttons,
     73   1.7     jmmv     						FMT_BITFIELD, FLG_MODIFY },
     74   1.5     jmmv     { "repeat.delay.first",	&repeat.wr_delay_first,
     75   1.7     jmmv     						FMT_UINT, FLG_MODIFY },
     76   1.5     jmmv     { "repeat.delay.decrement",	&repeat.wr_delay_decrement,
     77   1.7     jmmv     						FMT_UINT, FLG_MODIFY },
     78   1.5     jmmv     { "repeat.delay.minimum",	&repeat.wr_delay_minimum,
     79   1.7     jmmv  		   				FMT_UINT, FLG_MODIFY },
     80   1.1  hannken };
     81   1.1  hannken 
     82   1.1  hannken int mouse_field_tab_len = sizeof(mouse_field_tab)/
     83   1.1  hannken 			   sizeof(mouse_field_tab[0]);
     84   1.1  hannken 
     85   1.1  hannken void
     86   1.4  xtraeme mouse_get_values(int fd)
     87   1.1  hannken {
     88   1.7     jmmv 
     89   1.1  hannken 	if (field_by_value(&mstype)->flags & FLG_GET)
     90   1.1  hannken 		if (ioctl(fd, WSMOUSEIO_GTYPE, &mstype) < 0)
     91   1.7     jmmv 			err(EXIT_FAILURE, "WSMOUSEIO_GTYPE");
     92   1.5     jmmv 
     93   1.9  khorben 	if (field_by_value(&calibration.minx)->flags & FLG_GET ||
     94   1.9  khorben 	    field_by_value(&calibration.miny)->flags & FLG_GET ||
     95   1.9  khorben 	    field_by_value(&calibration.maxx)->flags & FLG_GET ||
     96   1.9  khorben 	    field_by_value(&calibration.maxy)->flags & FLG_GET ||
     97   1.9  khorben 	    field_by_value(&calibration_samples)->flags & FLG_GET)
     98   1.9  khorben 		mouse_get_calibration(fd);
     99   1.9  khorben 
    100   1.5     jmmv 	if (field_by_value(&repeat.wr_buttons)->flags & FLG_GET ||
    101   1.5     jmmv 	    field_by_value(&repeat.wr_delay_first)->flags & FLG_GET ||
    102   1.5     jmmv 	    field_by_value(&repeat.wr_delay_decrement)->flags & FLG_GET ||
    103   1.5     jmmv 	    field_by_value(&repeat.wr_delay_minimum)->flags & FLG_GET)
    104   1.5     jmmv 		mouse_get_repeat(fd);
    105   1.5     jmmv }
    106   1.5     jmmv 
    107   1.5     jmmv static void
    108   1.9  khorben mouse_get_calibration(int fd)
    109   1.9  khorben {
    110   1.9  khorben 	struct wsmouse_calibcoords tmp;
    111   1.9  khorben 	char *samples;
    112   1.9  khorben 	char buf[48];
    113   1.9  khorben 	int i;
    114   1.9  khorben 
    115   1.9  khorben 	if (ioctl(fd, WSMOUSEIO_GCALIBCOORDS, &tmp) < 0) {
    116   1.9  khorben 		field_disable_by_value(&calibration.minx);
    117   1.9  khorben 		field_disable_by_value(&calibration.miny);
    118   1.9  khorben 		field_disable_by_value(&calibration.maxx);
    119   1.9  khorben 		field_disable_by_value(&calibration.maxy);
    120   1.9  khorben 		field_disable_by_value(&calibration_samples);
    121   1.9  khorben 		return;
    122   1.9  khorben 	}
    123   1.9  khorben 
    124   1.9  khorben 	if (field_by_value(&calibration.minx)->flags & FLG_GET)
    125   1.9  khorben 		calibration.minx = tmp.minx;
    126   1.9  khorben 	if (field_by_value(&calibration.miny)->flags & FLG_GET)
    127   1.9  khorben 		calibration.miny = tmp.miny;
    128   1.9  khorben 	if (field_by_value(&calibration.maxx)->flags & FLG_GET)
    129   1.9  khorben 		calibration.maxx = tmp.maxx;
    130   1.9  khorben 	if (field_by_value(&calibration.maxy)->flags & FLG_GET)
    131   1.9  khorben 		calibration.maxy = tmp.maxy;
    132   1.9  khorben 	if (field_by_value(&calibration_samples)->flags & FLG_GET) {
    133   1.9  khorben 		free(calibration_samples);
    134   1.9  khorben 		if (tmp.samplelen <= 0) {
    135   1.9  khorben 			calibration_samples = strdup("");
    136   1.9  khorben 			if (calibration_samples == NULL)
    137   1.9  khorben 				err(EXIT_FAILURE, "could not list calibration"
    138   1.9  khorben 						" samples");
    139   1.9  khorben 		} else {
    140   1.9  khorben 			samples = malloc(tmp.samplelen * sizeof(buf));
    141   1.9  khorben 			if (samples == NULL)
    142   1.9  khorben 				err(EXIT_FAILURE, "could not list calibration"
    143   1.9  khorben 						" samples");
    144   1.9  khorben 			samples[0] = '\0';
    145   1.9  khorben 			for (i = 0; i < tmp.samplelen; i++) {
    146   1.9  khorben 				snprintf(buf, sizeof(buf), "%s%d,%d,%d,%d",
    147   1.9  khorben 						(i == 0) ? "" : ":",
    148   1.9  khorben 						tmp.samples[i].rawx,
    149   1.9  khorben 						tmp.samples[i].rawy,
    150   1.9  khorben 						tmp.samples[i].x,
    151   1.9  khorben 						tmp.samples[i].y);
    152   1.9  khorben 				strcat(samples, buf);
    153   1.9  khorben 			}
    154   1.9  khorben 			calibration_samples = samples;
    155   1.9  khorben 		}
    156   1.9  khorben 	}
    157   1.9  khorben }
    158   1.9  khorben 
    159   1.9  khorben static void
    160   1.5     jmmv mouse_get_repeat(int fd)
    161   1.5     jmmv {
    162   1.5     jmmv 	struct wsmouse_repeat tmp;
    163   1.5     jmmv 
    164   1.9  khorben 	if (ioctl(fd, WSMOUSEIO_GETREPEAT, &tmp) < 0)
    165   1.7     jmmv 		err(EXIT_FAILURE, "WSMOUSEIO_GETREPEAT");
    166   1.5     jmmv 
    167   1.5     jmmv 	if (field_by_value(&repeat.wr_buttons)->flags & FLG_GET)
    168   1.5     jmmv 		repeat.wr_buttons = tmp.wr_buttons;
    169   1.5     jmmv 	if (field_by_value(&repeat.wr_delay_first)->flags & FLG_GET)
    170   1.5     jmmv 		repeat.wr_delay_first = tmp.wr_delay_first;
    171   1.5     jmmv 	if (field_by_value(&repeat.wr_delay_decrement)->flags & FLG_GET)
    172   1.5     jmmv 		repeat.wr_delay_decrement = tmp.wr_delay_decrement;
    173   1.5     jmmv 	if (field_by_value(&repeat.wr_delay_minimum)->flags & FLG_GET)
    174   1.5     jmmv 		repeat.wr_delay_minimum = tmp.wr_delay_minimum;
    175   1.1  hannken }
    176   1.1  hannken 
    177   1.1  hannken void
    178   1.4  xtraeme mouse_put_values(int fd)
    179   1.1  hannken {
    180   1.3       ad 	int tmp;
    181   1.2       ad 
    182   1.3       ad 	if (field_by_value(&resolution)->flags & FLG_SET) {
    183   1.3       ad 		tmp = resolution;
    184   1.3       ad 		if (ioctl(fd, WSMOUSEIO_SRES, &tmp) < 0)
    185   1.7     jmmv 			err(EXIT_FAILURE, "WSMOUSEIO_SRES");
    186   1.3       ad 		pr_field(field_by_value(&resolution), " -> ");
    187   1.3       ad 	}
    188   1.5     jmmv 
    189   1.3       ad 	if (field_by_value(&samplerate)->flags & FLG_SET) {
    190   1.3       ad 		tmp = samplerate;
    191   1.3       ad 		if (ioctl(fd, WSMOUSEIO_SRATE, &tmp) < 0)
    192   1.7     jmmv 			err(EXIT_FAILURE, "WSMOUSEIO_SRATE");
    193   1.6     jmmv 		pr_field(field_by_value(&samplerate), " -> ");
    194   1.3       ad 	}
    195   1.5     jmmv 
    196  1.10  khorben 	if (field_by_value(&calibration.minx)->flags & FLG_SET ||
    197  1.10  khorben 	    field_by_value(&calibration.miny)->flags & FLG_SET ||
    198  1.10  khorben 	    field_by_value(&calibration.maxx)->flags & FLG_SET ||
    199  1.10  khorben 	    field_by_value(&calibration.maxy)->flags & FLG_SET ||
    200  1.10  khorben 	    field_by_value(&calibration_samples)->flags & FLG_SET)
    201  1.10  khorben 		mouse_put_calibration(fd);
    202  1.10  khorben 
    203   1.5     jmmv 	if (field_by_value(&repeat.wr_buttons)->flags & FLG_SET ||
    204   1.5     jmmv 	    field_by_value(&repeat.wr_delay_first)->flags & FLG_SET ||
    205   1.5     jmmv 	    field_by_value(&repeat.wr_delay_decrement)->flags & FLG_SET ||
    206   1.5     jmmv 	    field_by_value(&repeat.wr_delay_minimum)->flags & FLG_SET)
    207   1.5     jmmv 		mouse_put_repeat(fd);
    208   1.5     jmmv }
    209   1.5     jmmv 
    210   1.5     jmmv static void
    211  1.10  khorben mouse_put_calibration(int fd)
    212  1.10  khorben {
    213  1.10  khorben 	struct wsmouse_calibcoords tmp;
    214  1.10  khorben 	int i;
    215  1.10  khorben 	const char *p;
    216  1.10  khorben 	char *q;
    217  1.10  khorben 
    218  1.10  khorben 	/* Fetch current values into the temporary structure. */
    219  1.10  khorben 	if (ioctl(fd, WSMOUSEIO_GCALIBCOORDS, &tmp) < 0)
    220  1.10  khorben 		err(EXIT_FAILURE, "WSMOUSEIO_GCALIBCOORDS");
    221  1.10  khorben 
    222  1.10  khorben 	/* Overwrite the desired values in the temporary structure. */
    223  1.10  khorben 	if (field_by_value(&calibration.minx)->flags & FLG_SET)
    224  1.10  khorben 		tmp.minx = calibration.minx;
    225  1.10  khorben 	if (field_by_value(&calibration.miny)->flags & FLG_SET)
    226  1.10  khorben 		tmp.miny = calibration.miny;
    227  1.10  khorben 	if (field_by_value(&calibration.maxx)->flags & FLG_SET)
    228  1.10  khorben 		tmp.maxx = calibration.maxx;
    229  1.10  khorben 	if (field_by_value(&calibration.maxy)->flags & FLG_SET)
    230  1.10  khorben 		tmp.maxy = calibration.maxy;
    231  1.10  khorben 	if (field_by_value(&calibration_samples)->flags & FLG_SET) {
    232  1.10  khorben 		p = calibration_samples;
    233  1.10  khorben 		for (i = 0; p[0] != '\0' && i < WSMOUSE_CALIBCOORDS_MAX; i++) {
    234  1.10  khorben 			tmp.samples[i].rawx = strtol(p, &q, 0);
    235  1.10  khorben 			if (*q != ',')
    236  1.10  khorben 				break;
    237  1.10  khorben 			p = q + 1;
    238  1.10  khorben 			tmp.samples[i].rawy = strtol(p, &q, 0);
    239  1.10  khorben 			if (*q != ',')
    240  1.10  khorben 				break;
    241  1.10  khorben 			p = q + 1;
    242  1.10  khorben 			tmp.samples[i].x = strtol(p, &q, 0);
    243  1.10  khorben 			if (*q != ',')
    244  1.10  khorben 				break;
    245  1.10  khorben 			p = q + 1;
    246  1.10  khorben 			tmp.samples[i].y = strtol(p, &q, 0);
    247  1.10  khorben 			p = q + 1;
    248  1.10  khorben 			if (*q != '\0' && *q != ':')
    249  1.10  khorben 				break;
    250  1.10  khorben 		}
    251  1.10  khorben 		if (p[0] != '\0')
    252  1.10  khorben 			errx(EXIT_FAILURE, "%s: invalid calibration data",
    253  1.10  khorben 					calibration_samples);
    254  1.10  khorben 		tmp.samplelen = i;
    255  1.10  khorben 	}
    256  1.10  khorben 
    257  1.10  khorben 	/* Set new values for calibrating events. */
    258  1.10  khorben 	if (ioctl(fd, WSMOUSEIO_SCALIBCOORDS, &tmp) < 0)
    259  1.10  khorben 		err(EXIT_FAILURE, "WSMOUSEIO_SCALIBCOORDS");
    260  1.10  khorben 
    261  1.10  khorben 	/* Now print what changed. */
    262  1.10  khorben 	if (field_by_value(&calibration.minx)->flags & FLG_SET)
    263  1.10  khorben 		pr_field(field_by_value(&calibration.minx), " -> ");
    264  1.10  khorben 	if (field_by_value(&calibration.miny)->flags & FLG_SET)
    265  1.10  khorben 		pr_field(field_by_value(&calibration.miny), " -> ");
    266  1.10  khorben 	if (field_by_value(&calibration.maxx)->flags & FLG_SET)
    267  1.10  khorben 		pr_field(field_by_value(&calibration.maxx), " -> ");
    268  1.10  khorben 	if (field_by_value(&calibration.maxy)->flags & FLG_SET)
    269  1.10  khorben 		pr_field(field_by_value(&calibration.maxy), " -> ");
    270  1.10  khorben 	if (field_by_value(&calibration_samples)->flags & FLG_SET)
    271  1.10  khorben 		pr_field(field_by_value(&calibration_samples), " -> ");
    272  1.10  khorben }
    273  1.10  khorben 
    274  1.10  khorben static void
    275   1.5     jmmv mouse_put_repeat(int fd)
    276   1.5     jmmv {
    277   1.5     jmmv 	struct wsmouse_repeat tmp;
    278   1.5     jmmv 
    279   1.5     jmmv 	/* Fetch current values into the temporary structure. */
    280   1.9  khorben 	if (ioctl(fd, WSMOUSEIO_GETREPEAT, &tmp) < 0)
    281   1.7     jmmv 		err(EXIT_FAILURE, "WSMOUSEIO_GETREPEAT");
    282   1.5     jmmv 
    283   1.5     jmmv 	/* Overwrite the desired values in the temporary structure. */
    284   1.5     jmmv 	if (field_by_value(&repeat.wr_buttons)->flags & FLG_SET)
    285   1.5     jmmv 		tmp.wr_buttons = repeat.wr_buttons;
    286   1.5     jmmv 	if (field_by_value(&repeat.wr_delay_first)->flags & FLG_SET)
    287   1.5     jmmv 		tmp.wr_delay_first = repeat.wr_delay_first;
    288   1.5     jmmv 	if (field_by_value(&repeat.wr_delay_decrement)->flags & FLG_SET)
    289   1.5     jmmv 		tmp.wr_delay_decrement = repeat.wr_delay_decrement;
    290   1.5     jmmv 	if (field_by_value(&repeat.wr_delay_minimum)->flags & FLG_SET)
    291   1.5     jmmv 		tmp.wr_delay_minimum = repeat.wr_delay_minimum;
    292   1.5     jmmv 
    293   1.5     jmmv 	/* Set new values for repeating events. */
    294   1.9  khorben 	if (ioctl(fd, WSMOUSEIO_SETREPEAT, &tmp) < 0)
    295   1.7     jmmv 		err(EXIT_FAILURE, "WSMOUSEIO_SETREPEAT");
    296   1.5     jmmv 
    297   1.5     jmmv 	/* Now print what changed. */
    298   1.5     jmmv 	if (field_by_value(&repeat.wr_buttons)->flags & FLG_SET)
    299   1.5     jmmv 		pr_field(field_by_value(&repeat.wr_buttons), " -> ");
    300   1.5     jmmv 	if (field_by_value(&repeat.wr_delay_first)->flags & FLG_SET)
    301   1.5     jmmv 		pr_field(field_by_value(&repeat.wr_delay_first), " -> ");
    302   1.5     jmmv 	if (field_by_value(&repeat.wr_delay_decrement)->flags & FLG_SET)
    303   1.5     jmmv 		pr_field(field_by_value(&repeat.wr_delay_decrement), " -> ");
    304   1.5     jmmv 	if (field_by_value(&repeat.wr_delay_minimum)->flags & FLG_SET)
    305   1.5     jmmv 		pr_field(field_by_value(&repeat.wr_delay_minimum), " -> ");
    306   1.1  hannken }
    307