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