wsmuxctl.c revision 1.1 1 1.1 augustss /* $NetBSD: wsmuxctl.c,v 1.1 2001/10/13 20:05:43 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.1 augustss #include <sys/types.h>
44 1.1 augustss #include <sys/ioctl.h>
45 1.1 augustss #include <err.h>
46 1.1 augustss #include <errno.h>
47 1.1 augustss
48 1.1 augustss #include <dev/wscons/wsconsio.h>
49 1.1 augustss
50 1.1 augustss static void usage(void);
51 1.1 augustss int main(int, char**);
52 1.1 augustss
53 1.1 augustss const char *devnames[] = { "?", "wsmouse", "wskbd", "wsmux" };
54 1.1 augustss
55 1.1 augustss static void
56 1.1 augustss usage(void)
57 1.1 augustss {
58 1.1 augustss
59 1.1 augustss fprintf(stderr, "Usage: %s [-a dev] -f wsmux [-l] [-r dev]\n",
60 1.1 augustss getprogname());
61 1.1 augustss exit(1);
62 1.1 augustss }
63 1.1 augustss
64 1.1 augustss static void
65 1.1 augustss parsedev(const char *dev, struct wsmux_device *mdev)
66 1.1 augustss {
67 1.1 augustss if (sscanf(dev, "wsmouse%d", &mdev->idx) == 1) {
68 1.1 augustss mdev->type = WSMUX_MOUSE;
69 1.1 augustss return;
70 1.1 augustss }
71 1.1 augustss if (sscanf(dev, "wskbd%d", &mdev->idx) == 1) {
72 1.1 augustss mdev->type = WSMUX_KBD;
73 1.1 augustss return;
74 1.1 augustss }
75 1.1 augustss if (sscanf(dev, "wsmux%d", &mdev->idx) == 1) {
76 1.1 augustss mdev->type = WSMUX_MUX;
77 1.1 augustss return;
78 1.1 augustss }
79 1.1 augustss errx(1, "bad device: `%s', use wsmouse, wskdb, or wsmux\n", dev);
80 1.1 augustss }
81 1.1 augustss
82 1.1 augustss int
83 1.1 augustss main(int argc, char **argv)
84 1.1 augustss {
85 1.1 augustss char *wsdev, *dev;
86 1.1 augustss int wsfd, list, c, i, add, rem;
87 1.1 augustss struct wsmux_device_list devs;
88 1.1 augustss struct wsmux_device mdev;
89 1.1 augustss
90 1.1 augustss wsdev = NULL;
91 1.1 augustss dev = NULL;
92 1.1 augustss add = 0;
93 1.1 augustss rem = 0;
94 1.1 augustss list = 0;
95 1.1 augustss
96 1.1 augustss while ((c = getopt(argc, argv, "a:f:lr:")) != -1) {
97 1.1 augustss switch (c) {
98 1.1 augustss case 'a':
99 1.1 augustss if (dev)
100 1.1 augustss usage();
101 1.1 augustss dev = optarg;
102 1.1 augustss add++;
103 1.1 augustss break;
104 1.1 augustss case 'r':
105 1.1 augustss if (dev)
106 1.1 augustss usage();
107 1.1 augustss dev = optarg;
108 1.1 augustss rem++;
109 1.1 augustss break;
110 1.1 augustss case 'f':
111 1.1 augustss wsdev = optarg;
112 1.1 augustss break;
113 1.1 augustss case 'l':
114 1.1 augustss list++;
115 1.1 augustss break;
116 1.1 augustss case '?':
117 1.1 augustss default:
118 1.1 augustss usage();
119 1.1 augustss break;
120 1.1 augustss }
121 1.1 augustss }
122 1.1 augustss argc -= optind;
123 1.1 augustss argv += optind;
124 1.1 augustss
125 1.1 augustss if (wsdev == NULL)
126 1.1 augustss usage();
127 1.1 augustss
128 1.1 augustss wsfd = open(wsdev, O_WRONLY, 0);
129 1.1 augustss if (wsfd < 0)
130 1.1 augustss err(2, "%s", wsdev);
131 1.1 augustss
132 1.1 augustss if (list) {
133 1.1 augustss if (add || rem)
134 1.1 augustss usage();
135 1.1 augustss if (ioctl(wsfd, WSMUX_LIST_DEVICES, &devs) < 0)
136 1.1 augustss err(1, "WSMUX_LIST_DEVICES");
137 1.1 augustss for (i = 0; i < devs.ndevices; i++)
138 1.1 augustss printf("%s%d\n", devnames[devs.devices[i].type],
139 1.1 augustss devs.devices[i].idx);
140 1.1 augustss exit(0);
141 1.1 augustss }
142 1.1 augustss
143 1.1 augustss if (add) {
144 1.1 augustss parsedev(dev, &mdev);
145 1.1 augustss if (ioctl(wsfd, WSMUX_ADD_DEVICE, &mdev) < 0)
146 1.1 augustss err(1, "WSMUX_ADD_DEVICE");
147 1.1 augustss }
148 1.1 augustss
149 1.1 augustss if (rem) {
150 1.1 augustss parsedev(dev, &mdev);
151 1.1 augustss if (ioctl(wsfd, WSMUX_REMOVE_DEVICE, &mdev) < 0)
152 1.1 augustss err(1, "WSMUX_REMOVE_DEVICE");
153 1.1 augustss }
154 1.1 augustss
155 1.1 augustss close(wsfd);
156 1.1 augustss return (0);
157 1.1 augustss }
158