xref: /openbsd-src/sbin/wsconsctl/mouse.c (revision c0dd97bfcad3dab6c31ec12b9de1274fd2d2f993)
1 /*	$OpenBSD: mouse.c,v 1.16 2017/10/24 09:36:13 jsg 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     /* touchpad configuration (mousecfg): */
57     { "tp.tapping",		&cfg_tapping,	FMT_CFG,	FLG_NORDBACK },
58     { "tp.scaling",		&cfg_scaling,	FMT_CFG,	FLG_NORDBACK },
59     { "tp.swapsides",		&cfg_swapsides,	FMT_CFG,	FLG_NORDBACK },
60     { "tp.disable",		&cfg_disable,	FMT_CFG,	FLG_NORDBACK },
61     { "tp.param",		&cfg_param,	FMT_CFG,	FLG_WRONLY },
62     { NULL }
63 };
64 
65 static int dev_index = -1;
66 
67 
68 void
69 mouse_init(int devfd, int devidx) {
70 	struct field *f;
71 	const char *errstr;
72 	int err;
73 
74 	if (dev_index == devidx)
75 		return;
76 
77 	if ((err = mousecfg_init(devfd, &errstr))) {
78 		devidx = -1;
79 		for (f = mouse_field_tab; f->name != NULL; f++) {
80 			if (f->format == FMT_CFG)
81 				f->flags |= FLG_DEAD;
82 		}
83 		if (errstr != NULL)
84 			warnx("mousecfg error: %s (%d)", errstr, err);
85 	} else {
86 		for (f = mouse_field_tab; f->name != NULL; f++) {
87 			if (f->format == FMT_CFG)
88 				f->flags &= ~FLG_DEAD;
89 		}
90 	}
91 
92 	dev_index = devidx;
93 }
94 
95 void
96 mouse_get_values(int fd)
97 {
98 	struct field *f;
99 
100 	if (field_by_value(mouse_field_tab, &mstype)->flags & FLG_GET)
101 		if (ioctl(fd, WSMOUSEIO_GTYPE, &mstype) < 0)
102 			warn("WSMOUSEIO_GTYPE");
103 
104 	if (field_by_value(mouse_field_tab, &rawmode)->flags & FLG_GET) {
105 		if (ioctl(fd, WSMOUSEIO_GCALIBCOORDS, &wmcoords) < 0) {
106 			if (errno == ENOTTY)
107 				field_by_value(mouse_field_tab,
108 				    &rawmode)->flags |= FLG_DEAD;
109 			else
110 				warn("WSMOUSEIO_GCALIBCOORDS");
111 		}
112 		rawmode = wmcoords.samplelen;
113 	}
114 
115 	if (field_by_value(mouse_field_tab, &wmcoords)->flags & FLG_GET)
116 		if (ioctl(fd, WSMOUSEIO_GCALIBCOORDS, &wmcoords) < 0) {
117 			if (errno == ENOTTY)
118 				field_by_value(mouse_field_tab,
119 				    &wmcoords)->flags |= FLG_DEAD;
120 			else
121 				warn("WSMOUSEIO_GCALIBCOORDS");
122 	}
123 
124 	for (f = mouse_field_tab; f->name != NULL; f++) {
125 		if (f->format != FMT_CFG || !(f->flags & FLG_GET))
126 			continue;
127 		if (f->valp == &cfg_param)
128 			continue;
129 		if (mousecfg_get_field((struct wsmouse_parameters *) f->valp)) {
130 			f->flags |= FLG_DEAD;
131 			warnx("mousecfg: invalid key in '%s'", f->name);
132 		}
133 	}
134 }
135 
136 int
137 mouse_put_values(int fd)
138 {
139 	struct field *f;
140 
141 	if (field_by_value(mouse_field_tab, &resolution)->flags & FLG_SET) {
142 		if (ioctl(fd, WSMOUSEIO_SRES, &resolution) < 0) {
143 			warn("WSMOUSEIO_SRES");
144 			return 1;
145 		}
146 	}
147 	if (field_by_value(mouse_field_tab, &rawmode)->flags & FLG_SET) {
148 		wmcoords.samplelen = rawmode;
149 		if (ioctl(fd, WSMOUSEIO_SCALIBCOORDS, &wmcoords) < 0) {
150 			if (errno == ENOTTY) {
151 				field_by_value(mouse_field_tab,
152 				    &rawmode)->flags |= FLG_DEAD;
153 			} else {
154 				warn("WSMOUSEIO_SCALIBCOORDS");
155 				return 1;
156 			}
157 		}
158 	}
159 	if (field_by_value(mouse_field_tab, &wmcoords)->flags & FLG_SET) {
160 		if (ioctl(fd, WSMOUSEIO_GCALIBCOORDS, &wmcoords_save) < 0) {
161 			if (errno == ENOTTY)
162 				field_by_value(mouse_field_tab,
163 				    &wmcoords)->flags |= FLG_DEAD;
164 			else
165 				warn("WSMOUSEIO_GCALIBCOORDS");
166 		}
167 		wmcoords.samplelen = wmcoords_save.samplelen;
168 		if (ioctl(fd, WSMOUSEIO_SCALIBCOORDS, &wmcoords) < 0) {
169 			if (errno == ENOTTY) {
170 				field_by_value(mouse_field_tab,
171 				    &wmcoords)->flags |= FLG_DEAD;
172 			} else {
173 				warn("WSMOUSEIO_SCALIBCOORDS");
174 				return 1;
175 			}
176 		}
177 	}
178 
179 	for (f = mouse_field_tab; f->name != NULL; f++) {
180 		if (f->format != FMT_CFG || !(f->flags & FLG_SET))
181 			continue;
182 		if (mousecfg_put_field(fd,
183 		    (struct wsmouse_parameters *) f->valp)) {
184 			warn("mousecfg error (%s)", f->name);
185 			return 1;
186 		}
187 	}
188 
189 	return 0;
190 }
191 
192 char *
193 mouse_next_device(int index)
194 {
195 	static char devname[20];
196 
197 	snprintf(devname, sizeof(devname), "/dev/wsmouse%d", index);
198 	return (devname);
199 }
200