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