Home | History | Annotate | Line # | Download | only in wsmuxctl
wsmuxctl.c revision 1.8
      1  1.8      jmmv /* $NetBSD: wsmuxctl.c,v 1.8 2004/01/05 23:23:39 jmmv 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.8      jmmv 	fprintf(stderr, "usage: %s [-a dev] -f wsmux [-l] [-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.6     grant 	errx(1, "bad device: `%s', use wsmouse, wskdb, or wsmux", 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.7    itojun 			snprintf(buf, sizeof(buf), "%s%d", ctlpath,
     99  1.7    itojun 			    devs.devices[i].idx);
    100  1.2  augustss 			rfd = open(buf, O_WRONLY, 0);
    101  1.2  augustss 			if (rfd < 0)
    102  1.2  augustss 				warn("%s", buf);
    103  1.2  augustss 			listdevs(rfd, rec, ind+2);
    104  1.2  augustss 			close(rfd);
    105  1.2  augustss 		}
    106  1.2  augustss 	}
    107  1.2  augustss }
    108  1.2  augustss 
    109  1.1  augustss int
    110  1.1  augustss main(int argc, char **argv)
    111  1.1  augustss {
    112  1.1  augustss 	char *wsdev, *dev;
    113  1.2  augustss 	int wsfd, list, c, add, rem, recursive;
    114  1.1  augustss 	struct wsmux_device mdev;
    115  1.2  augustss 	char buf[100];
    116  1.1  augustss 
    117  1.1  augustss 	wsdev = NULL;
    118  1.1  augustss 	dev = NULL;
    119  1.1  augustss 	add = 0;
    120  1.1  augustss 	rem = 0;
    121  1.1  augustss 	list = 0;
    122  1.2  augustss 	recursive = 0;
    123  1.1  augustss 
    124  1.2  augustss 	while ((c = getopt(argc, argv, "a:f:lLr:")) != -1) {
    125  1.1  augustss 		switch (c) {
    126  1.1  augustss 		case 'a':
    127  1.1  augustss 			if (dev)
    128  1.1  augustss 				usage();
    129  1.1  augustss 			dev = optarg;
    130  1.1  augustss 			add++;
    131  1.1  augustss 			break;
    132  1.1  augustss 		case 'r':
    133  1.1  augustss 			if (dev)
    134  1.1  augustss 				usage();
    135  1.1  augustss 			dev = optarg;
    136  1.1  augustss 			rem++;
    137  1.1  augustss 			break;
    138  1.1  augustss 		case 'f':
    139  1.1  augustss 			wsdev = optarg;
    140  1.1  augustss 			break;
    141  1.2  augustss 		case 'L':
    142  1.2  augustss 			recursive++;
    143  1.1  augustss 		case 'l':
    144  1.1  augustss 			list++;
    145  1.1  augustss 			break;
    146  1.1  augustss 		case '?':
    147  1.1  augustss 		default:
    148  1.1  augustss 			usage();
    149  1.1  augustss 			break;
    150  1.1  augustss 		}
    151  1.1  augustss 	}
    152  1.1  augustss 	argc -= optind;
    153  1.1  augustss 	argv += optind;
    154  1.1  augustss 
    155  1.1  augustss 	if (wsdev == NULL)
    156  1.1  augustss 		usage();
    157  1.1  augustss 
    158  1.1  augustss 	wsfd = open(wsdev, O_WRONLY, 0);
    159  1.2  augustss 	if (wsfd < 0) {
    160  1.2  augustss 		if (isdigit(wsdev[0])) {
    161  1.7    itojun 			snprintf(buf, sizeof(buf), "%s%s", ctlpath, wsdev);
    162  1.2  augustss 			wsdev = buf;
    163  1.2  augustss 			wsfd = open(wsdev, O_WRONLY, 0);
    164  1.2  augustss 		}
    165  1.5  drochner 		if (wsfd < 0)
    166  1.5  drochner 			err(2, "%s", wsdev);
    167  1.2  augustss 	}
    168  1.1  augustss 
    169  1.1  augustss 	if (list) {
    170  1.1  augustss 		if (add || rem)
    171  1.1  augustss 			usage();
    172  1.2  augustss 		listdevs(wsfd, recursive, 0);
    173  1.1  augustss 		exit(0);
    174  1.1  augustss 	}
    175  1.1  augustss 
    176  1.1  augustss 	if (add) {
    177  1.1  augustss 		parsedev(dev, &mdev);
    178  1.3  augustss 		if (ioctl(wsfd, WSMUXIO_ADD_DEVICE, &mdev) < 0)
    179  1.3  augustss 			err(1, "WSMUXIO_ADD_DEVICE");
    180  1.1  augustss 	}
    181  1.1  augustss 
    182  1.1  augustss 	if (rem) {
    183  1.1  augustss 		parsedev(dev, &mdev);
    184  1.3  augustss 		if (ioctl(wsfd, WSMUXIO_REMOVE_DEVICE, &mdev) < 0)
    185  1.3  augustss 			err(1, "WSMUXIO_REMOVE_DEVICE");
    186  1.1  augustss 	}
    187  1.1  augustss 
    188  1.1  augustss 	close(wsfd);
    189  1.1  augustss 	return (0);
    190  1.1  augustss }
    191