xref: /openbsd/sbin/wsconsctl/mouse.c (revision a6445c1d)
1 /*	$OpenBSD: mouse.c,v 1.13 2012/08/08 16:44:07 shadchin 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 
42 static u_int mstype;
43 static u_int resolution;
44 static u_int samplerate;
45 static int rawmode;
46 
47 struct wsmouse_calibcoords wmcoords, wmcoords_save;
48 
49 struct field mouse_field_tab[] = {
50     { "resolution",		&resolution,	FMT_UINT,	FLG_WRONLY },
51     { "samplerate",		&samplerate,	FMT_UINT,	FLG_WRONLY },
52     { "type",			&mstype,	FMT_MSTYPE,	FLG_RDONLY },
53     { "rawmode",		&rawmode,	FMT_UINT,	FLG_MODIFY|FLG_INIT},
54     { "scale",			&wmcoords,	FMT_SCALE,	FLG_MODIFY|FLG_INIT},
55     { NULL }
56 };
57 
58 void
59 mouse_get_values(int fd)
60 {
61 	if (field_by_value(mouse_field_tab, &mstype)->flags & FLG_GET)
62 		if (ioctl(fd, WSMOUSEIO_GTYPE, &mstype) < 0)
63 			warn("WSMOUSEIO_GTYPE");
64 
65 	if (field_by_value(mouse_field_tab, &rawmode)->flags & FLG_GET) {
66 		if (ioctl(fd, WSMOUSEIO_GCALIBCOORDS, &wmcoords) < 0) {
67 			if (errno == ENOTTY)
68 				field_by_value(mouse_field_tab,
69 				    &rawmode)->flags |= FLG_DEAD;
70 			else
71 				warn("WSMOUSEIO_GCALIBCOORDS");
72 		}
73 		rawmode = wmcoords.samplelen;
74 	}
75 
76 	if (field_by_value(mouse_field_tab, &wmcoords)->flags & FLG_GET)
77 		if (ioctl(fd, WSMOUSEIO_GCALIBCOORDS, &wmcoords) < 0) {
78 			if (errno == ENOTTY)
79 				field_by_value(mouse_field_tab,
80 				    &wmcoords)->flags |= FLG_DEAD;
81 			else
82 				warn("WSMOUSEIO_GCALIBCOORDS");
83 	}
84 }
85 
86 int
87 mouse_put_values(int fd)
88 {
89 	if (field_by_value(mouse_field_tab, &resolution)->flags & FLG_SET) {
90 		if (ioctl(fd, WSMOUSEIO_SRES, &resolution) < 0) {
91 			warn("WSMOUSEIO_SRES");
92 			return 1;
93 		}
94 	}
95 	if (field_by_value(mouse_field_tab, &samplerate)->flags & FLG_SET) {
96 		if (ioctl(fd, WSMOUSEIO_SRATE, &samplerate) < 0) {
97 			warn("WSMOUSEIO_SRATE");
98 			return 1;
99 		}
100 	}
101 	if (field_by_value(mouse_field_tab, &rawmode)->flags & FLG_SET) {
102 		wmcoords.samplelen = rawmode;
103 		if (ioctl(fd, WSMOUSEIO_SCALIBCOORDS, &wmcoords) < 0) {
104 			if (errno == ENOTTY) {
105 				field_by_value(mouse_field_tab,
106 				    &rawmode)->flags |= FLG_DEAD;
107 			} else {
108 				warn("WSMOUSEIO_SCALIBCOORDS");
109 				return 1;
110 			}
111 		}
112 	}
113 	if (field_by_value(mouse_field_tab, &wmcoords)->flags & FLG_SET) {
114 		if (ioctl(fd, WSMOUSEIO_GCALIBCOORDS, &wmcoords_save) < 0) {
115 			if (errno == ENOTTY)
116 				field_by_value(mouse_field_tab,
117 				    &wmcoords)->flags |= FLG_DEAD;
118 			else
119 				warn("WSMOUSEIO_GCALIBCOORDS");
120 		}
121 		wmcoords.samplelen = wmcoords_save.samplelen;
122 		if (ioctl(fd, WSMOUSEIO_SCALIBCOORDS, &wmcoords) < 0) {
123 			if (errno == ENOTTY) {
124 				field_by_value(mouse_field_tab,
125 				    &wmcoords)->flags |= FLG_DEAD;
126 			} else {
127 				warn("WSMOUSEIO_SCALIBCOORDS");
128 				return 1;
129 			}
130 		}
131 	}
132 
133 	return 0;
134 }
135 
136 char *
137 mouse_next_device(int index)
138 {
139 	static char devname[20];
140 
141 	snprintf(devname, sizeof(devname), "/dev/wsmouse%d", index);
142 	return (devname);
143 }
144