xref: /openbsd/sbin/wsconsctl/mouse.c (revision d89ec533)
1 /*	$OpenBSD: mouse.c,v 1.20 2019/08/19 21:42:33 bru Exp $	*/
2 /*	$NetBSD: mouse.c,v 1.3 1999/11/15 13:47:30 ad Exp $ */
3 
4 /*-
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Juergen Hannken-Illjes.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/ioctl.h>
34 #include <sys/time.h>
35 #include <dev/wscons/wsconsio.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include "wsconsctl.h"
41 #include "mousecfg.h"
42 
43 static u_int mstype;
44 static u_int resolution;
45 static u_int samplerate;
46 static int rawmode;
47 
48 struct wsmouse_calibcoords wmcoords, wmcoords_save;
49 
50 struct field mouse_field_tab[] = {
51     { "resolution",		&resolution,	FMT_UINT,	FLG_WRONLY },
52     { "samplerate",		&samplerate,	FMT_UINT,	FLG_WRONLY },
53     { "type",			&mstype,	FMT_MSTYPE,	FLG_RDONLY },
54     { "rawmode",		&rawmode,	FMT_UINT,	FLG_MODIFY|FLG_INIT},
55     { "scale",			&wmcoords,	FMT_SCALE,	FLG_MODIFY|FLG_INIT},
56     /* mouse and touchpad configuration (mousecfg): */
57     { "reverse_scrolling",	&cfg_revscroll, FMT_CFG,	FLG_NORDBACK },
58     /* touchpad-specific options: */
59     { "tp.tapping",		&cfg_tapping,	FMT_CFG,	FLG_NORDBACK },
60     { "tp.scaling",		&cfg_scaling,	FMT_CFG,	FLG_NORDBACK },
61     { "tp.swapsides",		&cfg_swapsides,	FMT_CFG,	FLG_NORDBACK },
62     { "tp.disable",		&cfg_disable,	FMT_CFG,	FLG_NORDBACK },
63     { "tp.edges",		&cfg_edges,	FMT_CFG,	FLG_NORDBACK },
64     { "tp.param",		&cfg_param,	FMT_CFG,	FLG_WRONLY },
65     /* Add an alias.  This field is valid for all wsmouse devices. */
66     { "param",			&cfg_param,	FMT_CFG,	FLG_WRONLY },
67     { NULL }
68 };
69 
70 static int dev_index = -1;
71 
72 
73 void
74 mouse_init(int devfd, int devidx) {
75 	struct field *f;
76 	const char *errstr;
77 	int err;
78 
79 	if (dev_index == devidx)
80 		return;
81 
82 	if ((err = mousecfg_init(devfd, &errstr))) {
83 		devidx = -1;
84 		for (f = mouse_field_tab; f->name != NULL; f++) {
85 			if (f->format == FMT_CFG)
86 				f->flags |= FLG_DEAD;
87 		}
88 		warnx("mousecfg error: %s (%d)", errstr, err);
89 	} else if (cfg_touchpad) {
90 		for (f = mouse_field_tab; f->name != NULL; f++)
91 			if (f->format == FMT_CFG) {
92 				f->flags &= ~FLG_DEAD;
93 			}
94 	} else {
95 		for (f = mouse_field_tab; f->name != NULL; f++)
96 			if (f->format == FMT_CFG) {
97 				if (f->valp != &cfg_param
98 				    && f->valp != &cfg_revscroll)
99 					f->flags |= FLG_DEAD;
100 				else
101 					f->flags &= ~FLG_DEAD;
102 			}
103 	}
104 
105 	dev_index = devidx;
106 }
107 
108 void
109 mouse_get_values(int fd)
110 {
111 	struct field *f;
112 
113 	if (field_by_value(mouse_field_tab, &mstype)->flags & FLG_GET)
114 		if (ioctl(fd, WSMOUSEIO_GTYPE, &mstype) == -1)
115 			warn("WSMOUSEIO_GTYPE");
116 
117 	if (field_by_value(mouse_field_tab, &rawmode)->flags & FLG_GET) {
118 		if (ioctl(fd, WSMOUSEIO_GCALIBCOORDS, &wmcoords) == -1) {
119 			if (errno == ENOTTY)
120 				field_by_value(mouse_field_tab,
121 				    &rawmode)->flags |= FLG_DEAD;
122 			else
123 				warn("WSMOUSEIO_GCALIBCOORDS");
124 		}
125 		rawmode = wmcoords.samplelen;
126 	}
127 
128 	if (field_by_value(mouse_field_tab, &wmcoords)->flags & FLG_GET)
129 		if (ioctl(fd, WSMOUSEIO_GCALIBCOORDS, &wmcoords) == -1) {
130 			if (errno == ENOTTY)
131 				field_by_value(mouse_field_tab,
132 				    &wmcoords)->flags |= FLG_DEAD;
133 			else
134 				warn("WSMOUSEIO_GCALIBCOORDS");
135 	}
136 
137 	for (f = mouse_field_tab; f->name != NULL; f++) {
138 		if (f->format != FMT_CFG || !(f->flags & FLG_GET)
139 		    || f->valp == &cfg_param || (f->flags & FLG_DEAD))
140 			continue;
141 		if (mousecfg_get_field((struct wsmouse_parameters *) f->valp)) {
142 			f->flags |= FLG_DEAD;
143 			warnx("mousecfg: invalid key in '%s'", f->name);
144 		}
145 	}
146 }
147 
148 int
149 mouse_put_values(int fd)
150 {
151 	struct field *f;
152 
153 	if (field_by_value(mouse_field_tab, &resolution)->flags & FLG_SET) {
154 		if (ioctl(fd, WSMOUSEIO_SRES, &resolution) == -1) {
155 			warn("WSMOUSEIO_SRES");
156 			return 1;
157 		}
158 	}
159 	if (field_by_value(mouse_field_tab, &rawmode)->flags & FLG_SET) {
160 		wmcoords.samplelen = rawmode;
161 		if (ioctl(fd, WSMOUSEIO_SCALIBCOORDS, &wmcoords) == -1) {
162 			if (errno == ENOTTY) {
163 				field_by_value(mouse_field_tab,
164 				    &rawmode)->flags |= FLG_DEAD;
165 			} else {
166 				warn("WSMOUSEIO_SCALIBCOORDS");
167 				return 1;
168 			}
169 		}
170 	}
171 	if (field_by_value(mouse_field_tab, &wmcoords)->flags & FLG_SET) {
172 		if (ioctl(fd, WSMOUSEIO_GCALIBCOORDS, &wmcoords_save) == -1) {
173 			if (errno == ENOTTY)
174 				field_by_value(mouse_field_tab,
175 				    &wmcoords)->flags |= FLG_DEAD;
176 			else
177 				warn("WSMOUSEIO_GCALIBCOORDS");
178 		}
179 		wmcoords.samplelen = wmcoords_save.samplelen;
180 		if (ioctl(fd, WSMOUSEIO_SCALIBCOORDS, &wmcoords) == -1) {
181 			if (errno == ENOTTY) {
182 				field_by_value(mouse_field_tab,
183 				    &wmcoords)->flags |= FLG_DEAD;
184 			} else {
185 				warn("WSMOUSEIO_SCALIBCOORDS");
186 				return 1;
187 			}
188 		}
189 	}
190 
191 	for (f = mouse_field_tab; f->name != NULL; f++) {
192 		if (f->format != FMT_CFG || !(f->flags & FLG_SET))
193 			continue;
194 		if (mousecfg_put_field(fd,
195 		    (struct wsmouse_parameters *) f->valp)) {
196 			warn("mousecfg error (%s)", f->name);
197 			return 1;
198 		}
199 	}
200 
201 	return 0;
202 }
203 
204 char *
205 mouse_next_device(int index)
206 {
207 	static char devname[20];
208 
209 	snprintf(devname, sizeof(devname), "/dev/wsmouse%d", index);
210 	return (devname);
211 }
212