mouse.c revision 1.7 1 /* $NetBSD: mouse.c,v 1.7 2006/02/05 18:11:46 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
81 if (field_by_value(&mstype)->flags & FLG_GET)
82 if (ioctl(fd, WSMOUSEIO_GTYPE, &mstype) < 0)
83 err(EXIT_FAILURE, "WSMOUSEIO_GTYPE");
84
85 if (field_by_value(&repeat.wr_buttons)->flags & FLG_GET ||
86 field_by_value(&repeat.wr_delay_first)->flags & FLG_GET ||
87 field_by_value(&repeat.wr_delay_decrement)->flags & FLG_GET ||
88 field_by_value(&repeat.wr_delay_minimum)->flags & FLG_GET)
89 mouse_get_repeat(fd);
90 }
91
92 static void
93 mouse_get_repeat(int fd)
94 {
95 struct wsmouse_repeat tmp;
96
97 if (ioctl(fd, WSMOUSEIO_GETREPEAT, &tmp) == -1)
98 err(EXIT_FAILURE, "WSMOUSEIO_GETREPEAT");
99
100 if (field_by_value(&repeat.wr_buttons)->flags & FLG_GET)
101 repeat.wr_buttons = tmp.wr_buttons;
102 if (field_by_value(&repeat.wr_delay_first)->flags & FLG_GET)
103 repeat.wr_delay_first = tmp.wr_delay_first;
104 if (field_by_value(&repeat.wr_delay_decrement)->flags & FLG_GET)
105 repeat.wr_delay_decrement = tmp.wr_delay_decrement;
106 if (field_by_value(&repeat.wr_delay_minimum)->flags & FLG_GET)
107 repeat.wr_delay_minimum = tmp.wr_delay_minimum;
108 }
109
110 void
111 mouse_put_values(int fd)
112 {
113 int tmp;
114
115 if (field_by_value(&resolution)->flags & FLG_SET) {
116 tmp = resolution;
117 if (ioctl(fd, WSMOUSEIO_SRES, &tmp) < 0)
118 err(EXIT_FAILURE, "WSMOUSEIO_SRES");
119 pr_field(field_by_value(&resolution), " -> ");
120 }
121
122 if (field_by_value(&samplerate)->flags & FLG_SET) {
123 tmp = samplerate;
124 if (ioctl(fd, WSMOUSEIO_SRATE, &tmp) < 0)
125 err(EXIT_FAILURE, "WSMOUSEIO_SRATE");
126 pr_field(field_by_value(&samplerate), " -> ");
127 }
128
129 if (field_by_value(&repeat.wr_buttons)->flags & FLG_SET ||
130 field_by_value(&repeat.wr_delay_first)->flags & FLG_SET ||
131 field_by_value(&repeat.wr_delay_decrement)->flags & FLG_SET ||
132 field_by_value(&repeat.wr_delay_minimum)->flags & FLG_SET)
133 mouse_put_repeat(fd);
134 }
135
136 static void
137 mouse_put_repeat(int fd)
138 {
139 struct wsmouse_repeat tmp;
140
141 /* Fetch current values into the temporary structure. */
142 if (ioctl(fd, WSMOUSEIO_GETREPEAT, &tmp) == -1)
143 err(EXIT_FAILURE, "WSMOUSEIO_GETREPEAT");
144
145 /* Overwrite the desired values in the temporary structure. */
146 if (field_by_value(&repeat.wr_buttons)->flags & FLG_SET)
147 tmp.wr_buttons = repeat.wr_buttons;
148 if (field_by_value(&repeat.wr_delay_first)->flags & FLG_SET)
149 tmp.wr_delay_first = repeat.wr_delay_first;
150 if (field_by_value(&repeat.wr_delay_decrement)->flags & FLG_SET)
151 tmp.wr_delay_decrement = repeat.wr_delay_decrement;
152 if (field_by_value(&repeat.wr_delay_minimum)->flags & FLG_SET)
153 tmp.wr_delay_minimum = repeat.wr_delay_minimum;
154
155 /* Set new values for repeating events. */
156 if (ioctl(fd, WSMOUSEIO_SETREPEAT, &tmp) == -1)
157 err(EXIT_FAILURE, "WSMOUSEIO_SETREPEAT");
158
159 /* Now print what changed. */
160 if (field_by_value(&repeat.wr_buttons)->flags & FLG_SET)
161 pr_field(field_by_value(&repeat.wr_buttons), " -> ");
162 if (field_by_value(&repeat.wr_delay_first)->flags & FLG_SET)
163 pr_field(field_by_value(&repeat.wr_delay_first), " -> ");
164 if (field_by_value(&repeat.wr_delay_decrement)->flags & FLG_SET)
165 pr_field(field_by_value(&repeat.wr_delay_decrement), " -> ");
166 if (field_by_value(&repeat.wr_delay_minimum)->flags & FLG_SET)
167 pr_field(field_by_value(&repeat.wr_delay_minimum), " -> ");
168 }
169