wsmuxctl.c revision 1.3 1 1.3 augustss /* $NetBSD: wsmuxctl.c,v 1.3 2001/10/24 14:14:03 augustss Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.1 augustss * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.1 augustss * Author: Lennart Augustsson <augustss (at) carlstedt.se>
8 1.1 augustss * Carlstedt Research & Technology
9 1.1 augustss *
10 1.1 augustss * Redistribution and use in source and binary forms, with or without
11 1.1 augustss * modification, are permitted provided that the following conditions
12 1.1 augustss * are met:
13 1.1 augustss * 1. Redistributions of source code must retain the above copyright
14 1.1 augustss * notice, this list of conditions and the following disclaimer.
15 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 augustss * notice, this list of conditions and the following disclaimer in the
17 1.1 augustss * documentation and/or other materials provided with the distribution.
18 1.1 augustss * 3. All advertising materials mentioning features or use of this software
19 1.1 augustss * must display the following acknowledgement:
20 1.1 augustss * This product includes software developed by the NetBSD
21 1.1 augustss * Foundation, Inc. and its contributors.
22 1.1 augustss * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 augustss * contributors may be used to endorse or promote products derived
24 1.1 augustss * from this software without specific prior written permission.
25 1.1 augustss *
26 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
37 1.1 augustss */
38 1.1 augustss
39 1.1 augustss #include <stdio.h>
40 1.1 augustss #include <stdlib.h>
41 1.1 augustss #include <fcntl.h>
42 1.1 augustss #include <unistd.h>
43 1.2 augustss #include <ctype.h>
44 1.1 augustss #include <sys/types.h>
45 1.1 augustss #include <sys/ioctl.h>
46 1.1 augustss #include <err.h>
47 1.1 augustss #include <errno.h>
48 1.1 augustss
49 1.1 augustss #include <dev/wscons/wsconsio.h>
50 1.1 augustss
51 1.1 augustss static void usage(void);
52 1.1 augustss int main(int, char**);
53 1.1 augustss
54 1.2 augustss const char *ctlpath = "/dev/wsmuxctl";
55 1.2 augustss
56 1.1 augustss const char *devnames[] = { "?", "wsmouse", "wskbd", "wsmux" };
57 1.1 augustss
58 1.1 augustss static void
59 1.1 augustss usage(void)
60 1.1 augustss {
61 1.1 augustss
62 1.1 augustss fprintf(stderr, "Usage: %s [-a dev] -f wsmux [-l] [-r dev]\n",
63 1.1 augustss getprogname());
64 1.1 augustss exit(1);
65 1.1 augustss }
66 1.1 augustss
67 1.1 augustss static void
68 1.1 augustss parsedev(const char *dev, struct wsmux_device *mdev)
69 1.1 augustss {
70 1.1 augustss if (sscanf(dev, "wsmouse%d", &mdev->idx) == 1) {
71 1.1 augustss mdev->type = WSMUX_MOUSE;
72 1.1 augustss return;
73 1.1 augustss }
74 1.1 augustss if (sscanf(dev, "wskbd%d", &mdev->idx) == 1) {
75 1.1 augustss mdev->type = WSMUX_KBD;
76 1.1 augustss return;
77 1.1 augustss }
78 1.1 augustss if (sscanf(dev, "wsmux%d", &mdev->idx) == 1) {
79 1.1 augustss mdev->type = WSMUX_MUX;
80 1.1 augustss return;
81 1.1 augustss }
82 1.1 augustss errx(1, "bad device: `%s', use wsmouse, wskdb, or wsmux\n", dev);
83 1.1 augustss }
84 1.1 augustss
85 1.2 augustss static void
86 1.2 augustss listdevs(int fd, int rec, int ind)
87 1.2 augustss {
88 1.2 augustss int i, rfd;
89 1.2 augustss char buf[100];
90 1.2 augustss struct wsmux_device_list devs;
91 1.2 augustss
92 1.3 augustss if (ioctl(fd, WSMUXIO_LIST_DEVICES, &devs) < 0)
93 1.3 augustss err(1, "WSMUXIO_LIST_DEVICES");
94 1.2 augustss for (i = 0; i < devs.ndevices; i++) {
95 1.2 augustss printf("%*s%s%d\n", ind, "", devnames[devs.devices[i].type],
96 1.2 augustss devs.devices[i].idx);
97 1.2 augustss if (rec && devs.devices[i].type == WSMUX_MUX) {
98 1.2 augustss sprintf(buf, "%s%d", ctlpath, devs.devices[i].idx);
99 1.2 augustss rfd = open(buf, O_WRONLY, 0);
100 1.2 augustss if (rfd < 0)
101 1.2 augustss warn("%s", buf);
102 1.2 augustss listdevs(rfd, rec, ind+2);
103 1.2 augustss close(rfd);
104 1.2 augustss }
105 1.2 augustss }
106 1.2 augustss }
107 1.2 augustss
108 1.1 augustss int
109 1.1 augustss main(int argc, char **argv)
110 1.1 augustss {
111 1.1 augustss char *wsdev, *dev;
112 1.2 augustss int wsfd, list, c, add, rem, recursive;
113 1.1 augustss struct wsmux_device mdev;
114 1.2 augustss char buf[100];
115 1.1 augustss
116 1.1 augustss wsdev = NULL;
117 1.1 augustss dev = NULL;
118 1.1 augustss add = 0;
119 1.1 augustss rem = 0;
120 1.1 augustss list = 0;
121 1.2 augustss recursive = 0;
122 1.1 augustss
123 1.2 augustss while ((c = getopt(argc, argv, "a:f:lLr:")) != -1) {
124 1.1 augustss switch (c) {
125 1.1 augustss case 'a':
126 1.1 augustss if (dev)
127 1.1 augustss usage();
128 1.1 augustss dev = optarg;
129 1.1 augustss add++;
130 1.1 augustss break;
131 1.1 augustss case 'r':
132 1.1 augustss if (dev)
133 1.1 augustss usage();
134 1.1 augustss dev = optarg;
135 1.1 augustss rem++;
136 1.1 augustss break;
137 1.1 augustss case 'f':
138 1.1 augustss wsdev = optarg;
139 1.1 augustss break;
140 1.2 augustss case 'L':
141 1.2 augustss recursive++;
142 1.1 augustss case 'l':
143 1.1 augustss list++;
144 1.1 augustss break;
145 1.1 augustss case '?':
146 1.1 augustss default:
147 1.1 augustss usage();
148 1.1 augustss break;
149 1.1 augustss }
150 1.1 augustss }
151 1.1 augustss argc -= optind;
152 1.1 augustss argv += optind;
153 1.1 augustss
154 1.1 augustss if (wsdev == NULL)
155 1.1 augustss usage();
156 1.1 augustss
157 1.1 augustss wsfd = open(wsdev, O_WRONLY, 0);
158 1.2 augustss if (wsfd < 0) {
159 1.2 augustss if (isdigit(wsdev[0])) {
160 1.2 augustss sprintf(buf, "%s%s", ctlpath, wsdev);
161 1.2 augustss wsdev = buf;
162 1.2 augustss wsfd = open(wsdev, O_WRONLY, 0);
163 1.2 augustss if (wsfd < 0)
164 1.2 augustss err(2, "%s", wsdev);
165 1.2 augustss }
166 1.2 augustss }
167 1.1 augustss
168 1.1 augustss if (list) {
169 1.1 augustss if (add || rem)
170 1.1 augustss usage();
171 1.2 augustss listdevs(wsfd, recursive, 0);
172 1.1 augustss exit(0);
173 1.1 augustss }
174 1.1 augustss
175 1.1 augustss if (add) {
176 1.1 augustss parsedev(dev, &mdev);
177 1.3 augustss if (ioctl(wsfd, WSMUXIO_ADD_DEVICE, &mdev) < 0)
178 1.3 augustss err(1, "WSMUXIO_ADD_DEVICE");
179 1.1 augustss }
180 1.1 augustss
181 1.1 augustss if (rem) {
182 1.1 augustss parsedev(dev, &mdev);
183 1.3 augustss if (ioctl(wsfd, WSMUXIO_REMOVE_DEVICE, &mdev) < 0)
184 1.3 augustss err(1, "WSMUXIO_REMOVE_DEVICE");
185 1.1 augustss }
186 1.1 augustss
187 1.1 augustss close(wsfd);
188 1.1 augustss return (0);
189 1.1 augustss }
190