GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/wsconsctl/mouse.c Lines: 0 73 0.0 %
Date: 2017-11-07 Branches: 0 72 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: mouse.c,v 1.15 2017/08/01 23:23:44 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
    /* 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, &samplerate)->flags & FLG_SET) {
148
		if (ioctl(fd, WSMOUSEIO_SRATE, &samplerate) < 0) {
149
			warn("WSMOUSEIO_SRATE");
150
			return 1;
151
		}
152
	}
153
	if (field_by_value(mouse_field_tab, &rawmode)->flags & FLG_SET) {
154
		wmcoords.samplelen = rawmode;
155
		if (ioctl(fd, WSMOUSEIO_SCALIBCOORDS, &wmcoords) < 0) {
156
			if (errno == ENOTTY) {
157
				field_by_value(mouse_field_tab,
158
				    &rawmode)->flags |= FLG_DEAD;
159
			} else {
160
				warn("WSMOUSEIO_SCALIBCOORDS");
161
				return 1;
162
			}
163
		}
164
	}
165
	if (field_by_value(mouse_field_tab, &wmcoords)->flags & FLG_SET) {
166
		if (ioctl(fd, WSMOUSEIO_GCALIBCOORDS, &wmcoords_save) < 0) {
167
			if (errno == ENOTTY)
168
				field_by_value(mouse_field_tab,
169
				    &wmcoords)->flags |= FLG_DEAD;
170
			else
171
				warn("WSMOUSEIO_GCALIBCOORDS");
172
		}
173
		wmcoords.samplelen = wmcoords_save.samplelen;
174
		if (ioctl(fd, WSMOUSEIO_SCALIBCOORDS, &wmcoords) < 0) {
175
			if (errno == ENOTTY) {
176
				field_by_value(mouse_field_tab,
177
				    &wmcoords)->flags |= FLG_DEAD;
178
			} else {
179
				warn("WSMOUSEIO_SCALIBCOORDS");
180
				return 1;
181
			}
182
		}
183
	}
184
185
	for (f = mouse_field_tab; f->name != NULL; f++) {
186
		if (f->format != FMT_CFG || !(f->flags & FLG_SET))
187
			continue;
188
		if (mousecfg_put_field(fd,
189
		    (struct wsmouse_parameters *) f->valp)) {
190
			warn("mousecfg error (%s)", f->name);
191
			return 1;
192
		}
193
	}
194
195
	return 0;
196
}
197
198
char *
199
mouse_next_device(int index)
200
{
201
	static char devname[20];
202
203
	snprintf(devname, sizeof(devname), "/dev/wsmouse%d", index);
204
	return (devname);
205
}