Home | History | Annotate | Line # | Download | only in wsconsctl
mouse.c revision 1.5
      1 /*	$NetBSD: mouse.c,v 1.5 2006/02/05 17:38:33 jmmv Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998, 2006 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Juergen Hannken-Illjes and Julio M. Merino Vidal.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/ioctl.h>
     40 #include <sys/time.h>
     41 #include <dev/wscons/wsconsio.h>
     42 
     43 #include <err.h>
     44 #include <errno.h>
     45 #include <limits.h>
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 #include <string.h>
     49 
     50 #include "wsconsctl.h"
     51 
     52 static int mstype;
     53 static int resolution;
     54 static int samplerate;
     55 static struct wsmouse_repeat repeat;
     56 
     57 static void mouse_get_repeat(int);
     58 static void mouse_put_repeat(int);
     59 
     60 struct field mouse_field_tab[] = {
     61     { "resolution",		&resolution,	FMT_UINT,	FLG_WRONLY },
     62     { "samplerate",		&samplerate,	FMT_UINT,	FLG_WRONLY },
     63     { "type",			&mstype,	FMT_MSTYPE,	FLG_RDONLY },
     64     { "repeat.buttons",		&repeat.wr_buttons,
     65     				FMT_BITFIELD, FLG_MODIFY },
     66     { "repeat.delay.first",	&repeat.wr_delay_first,
     67     				FMT_UINT, FLG_MODIFY },
     68     { "repeat.delay.decrement",	&repeat.wr_delay_decrement,
     69     				FMT_UINT, FLG_MODIFY },
     70     { "repeat.delay.minimum",	&repeat.wr_delay_minimum,
     71     				FMT_UINT, FLG_MODIFY },
     72 };
     73 
     74 int mouse_field_tab_len = sizeof(mouse_field_tab)/
     75 			   sizeof(mouse_field_tab[0]);
     76 
     77 void
     78 mouse_get_values(int fd)
     79 {
     80 	if (field_by_value(&mstype)->flags & FLG_GET)
     81 		if (ioctl(fd, WSMOUSEIO_GTYPE, &mstype) < 0)
     82 			err(1, "WSMOUSEIO_GTYPE");
     83 
     84 	if (field_by_value(&repeat.wr_buttons)->flags & FLG_GET ||
     85 	    field_by_value(&repeat.wr_delay_first)->flags & FLG_GET ||
     86 	    field_by_value(&repeat.wr_delay_decrement)->flags & FLG_GET ||
     87 	    field_by_value(&repeat.wr_delay_minimum)->flags & FLG_GET)
     88 		mouse_get_repeat(fd);
     89 }
     90 
     91 static void
     92 mouse_get_repeat(int fd)
     93 {
     94 	struct wsmouse_repeat tmp;
     95 
     96 	if (ioctl(fd, WSMOUSEIO_GETREPEAT, &tmp) == -1)
     97 		err(1, "WSMOUSEIO_GETREPEAT");
     98 
     99 	if (field_by_value(&repeat.wr_buttons)->flags & FLG_GET)
    100 		repeat.wr_buttons = tmp.wr_buttons;
    101 	if (field_by_value(&repeat.wr_delay_first)->flags & FLG_GET)
    102 		repeat.wr_delay_first = tmp.wr_delay_first;
    103 	if (field_by_value(&repeat.wr_delay_decrement)->flags & FLG_GET)
    104 		repeat.wr_delay_decrement = tmp.wr_delay_decrement;
    105 	if (field_by_value(&repeat.wr_delay_minimum)->flags & FLG_GET)
    106 		repeat.wr_delay_minimum = tmp.wr_delay_minimum;
    107 }
    108 
    109 void
    110 mouse_put_values(int fd)
    111 {
    112 	int tmp;
    113 
    114 	if (field_by_value(&resolution)->flags & FLG_SET) {
    115 		tmp = resolution;
    116 		if (ioctl(fd, WSMOUSEIO_SRES, &tmp) < 0)
    117 			err(1, "WSMOUSEIO_SRES");
    118 		pr_field(field_by_value(&resolution), " -> ");
    119 	}
    120 
    121 	if (field_by_value(&samplerate)->flags & FLG_SET) {
    122 		tmp = samplerate;
    123 		if (ioctl(fd, WSMOUSEIO_SRATE, &tmp) < 0)
    124 			err(1, "WSMOUSEIO_SRES");
    125 		pr_field(field_by_value(&tmp), " -> ");
    126 	}
    127 
    128 	if (field_by_value(&repeat.wr_buttons)->flags & FLG_SET ||
    129 	    field_by_value(&repeat.wr_delay_first)->flags & FLG_SET ||
    130 	    field_by_value(&repeat.wr_delay_decrement)->flags & FLG_SET ||
    131 	    field_by_value(&repeat.wr_delay_minimum)->flags & FLG_SET)
    132 		mouse_put_repeat(fd);
    133 }
    134 
    135 static void
    136 mouse_put_repeat(int fd)
    137 {
    138 	struct wsmouse_repeat tmp;
    139 
    140 	/* Fetch current values into the temporary structure. */
    141 	if (ioctl(fd, WSMOUSEIO_GETREPEAT, &tmp) == -1)
    142 		err(1, "WSMOUSEIO_GETREPEAT");
    143 
    144 	/* Overwrite the desired values in the temporary structure. */
    145 	if (field_by_value(&repeat.wr_buttons)->flags & FLG_SET)
    146 		tmp.wr_buttons = repeat.wr_buttons;
    147 	if (field_by_value(&repeat.wr_delay_first)->flags & FLG_SET)
    148 		tmp.wr_delay_first = repeat.wr_delay_first;
    149 	if (field_by_value(&repeat.wr_delay_decrement)->flags & FLG_SET)
    150 		tmp.wr_delay_decrement = repeat.wr_delay_decrement;
    151 	if (field_by_value(&repeat.wr_delay_minimum)->flags & FLG_SET)
    152 		tmp.wr_delay_minimum = repeat.wr_delay_minimum;
    153 
    154 	/* Set new values for repeating events. */
    155 	if (ioctl(fd, WSMOUSEIO_SETREPEAT, &tmp) == -1)
    156 		err(1, "WSMOUSEIO_SETREPEAT");
    157 
    158 	/* Now print what changed. */
    159 	if (field_by_value(&repeat.wr_buttons)->flags & FLG_SET)
    160 		pr_field(field_by_value(&repeat.wr_buttons), " -> ");
    161 	if (field_by_value(&repeat.wr_delay_first)->flags & FLG_SET)
    162 		pr_field(field_by_value(&repeat.wr_delay_first), " -> ");
    163 	if (field_by_value(&repeat.wr_delay_decrement)->flags & FLG_SET)
    164 		pr_field(field_by_value(&repeat.wr_delay_decrement), " -> ");
    165 	if (field_by_value(&repeat.wr_delay_minimum)->flags & FLG_SET)
    166 		pr_field(field_by_value(&repeat.wr_delay_minimum), " -> ");
    167 }
    168