Home | History | Annotate | Line # | Download | only in wsmuxctl
wsmuxctl.c revision 1.1
      1 /* $NetBSD: wsmuxctl.c,v 1.1 2001/10/13 20:05:43 augustss Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Author: Lennart Augustsson <augustss (at) 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <stdio.h>
     40 #include <stdlib.h>
     41 #include <fcntl.h>
     42 #include <unistd.h>
     43 #include <sys/types.h>
     44 #include <sys/ioctl.h>
     45 #include <err.h>
     46 #include <errno.h>
     47 
     48 #include <dev/wscons/wsconsio.h>
     49 
     50 static void usage(void);
     51 int main(int, char**);
     52 
     53 const char *devnames[] = { "?", "wsmouse", "wskbd", "wsmux" };
     54 
     55 static void
     56 usage(void)
     57 {
     58 
     59 	fprintf(stderr, "Usage: %s [-a dev] -f wsmux [-l] [-r dev]\n",
     60 		    getprogname());
     61 	exit(1);
     62 }
     63 
     64 static void
     65 parsedev(const char *dev, struct wsmux_device *mdev)
     66 {
     67 	if (sscanf(dev, "wsmouse%d", &mdev->idx) == 1) {
     68 		mdev->type = WSMUX_MOUSE;
     69 		return;
     70 	}
     71 	if (sscanf(dev, "wskbd%d", &mdev->idx) == 1) {
     72 		mdev->type = WSMUX_KBD;
     73 		return;
     74 	}
     75 	if (sscanf(dev, "wsmux%d", &mdev->idx) == 1) {
     76 		mdev->type = WSMUX_MUX;
     77 		return;
     78 	}
     79 	errx(1, "bad device: `%s', use wsmouse, wskdb, or wsmux\n", dev);
     80 }
     81 
     82 int
     83 main(int argc, char **argv)
     84 {
     85 	char *wsdev, *dev;
     86 	int wsfd, list, c, i, add, rem;
     87 	struct wsmux_device_list devs;
     88 	struct wsmux_device mdev;
     89 
     90 	wsdev = NULL;
     91 	dev = NULL;
     92 	add = 0;
     93 	rem = 0;
     94 	list = 0;
     95 
     96 	while ((c = getopt(argc, argv, "a:f:lr:")) != -1) {
     97 		switch (c) {
     98 		case 'a':
     99 			if (dev)
    100 				usage();
    101 			dev = optarg;
    102 			add++;
    103 			break;
    104 		case 'r':
    105 			if (dev)
    106 				usage();
    107 			dev = optarg;
    108 			rem++;
    109 			break;
    110 		case 'f':
    111 			wsdev = optarg;
    112 			break;
    113 		case 'l':
    114 			list++;
    115 			break;
    116 		case '?':
    117 		default:
    118 			usage();
    119 			break;
    120 		}
    121 	}
    122 	argc -= optind;
    123 	argv += optind;
    124 
    125 	if (wsdev == NULL)
    126 		usage();
    127 
    128 	wsfd = open(wsdev, O_WRONLY, 0);
    129 	if (wsfd < 0)
    130 		err(2, "%s", wsdev);
    131 
    132 	if (list) {
    133 		if (add || rem)
    134 			usage();
    135 		if (ioctl(wsfd, WSMUX_LIST_DEVICES, &devs) < 0)
    136 			err(1, "WSMUX_LIST_DEVICES");
    137 		for (i = 0; i < devs.ndevices; i++)
    138 			printf("%s%d\n", devnames[devs.devices[i].type],
    139 			       devs.devices[i].idx);
    140 		exit(0);
    141 	}
    142 
    143 	if (add) {
    144 		parsedev(dev, &mdev);
    145 		if (ioctl(wsfd, WSMUX_ADD_DEVICE, &mdev) < 0)
    146 			err(1, "WSMUX_ADD_DEVICE");
    147 	}
    148 
    149 	if (rem) {
    150 		parsedev(dev, &mdev);
    151 		if (ioctl(wsfd, WSMUX_REMOVE_DEVICE, &mdev) < 0)
    152 			err(1, "WSMUX_REMOVE_DEVICE");
    153 	}
    154 
    155 	close(wsfd);
    156 	return (0);
    157 }
    158