wsconscfg.c revision 1.11       1 /* $NetBSD: wsconscfg.c,v 1.11 2005/07/10 17:57:28 christos Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1999
      5  *	Matthias Drochner.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed for the NetBSD Project
     18  *	by Matthias Drochner.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  */
     34 
     35 #include <stdio.h>
     36 #include <stdlib.h>
     37 #include <fcntl.h>
     38 #include <unistd.h>
     39 #include <sys/types.h>
     40 #include <sys/ioctl.h>
     41 #include <err.h>
     42 #include <errno.h>
     43 
     44 #include <dev/wscons/wsconsio.h>
     45 #include <dev/wscons/wsdisplay_usl_io.h>
     46 
     47 #define DEFDEV "/dev/ttyEcfg"
     48 
     49 static void usage(void) __attribute__((__unused__));
     50 int main(int, char **);
     51 
     52 static void
     53 usage(void)
     54 {
     55 
     56 	(void)fprintf(stderr,
     57 	     "Usage: %s [-f wsdev] [-d [-F]] [-k] [-s] [-m] [-t type]"
     58 	     " [-e emul] {vt | [kbd] | [mux]}\n", getprogname());
     59 	exit(1);
     60 }
     61 
     62 int
     63 main(int argc, char **argv)
     64 {
     65 	const char *wsdev;
     66 	int c, delete, kbd, idx, wsfd, swtch, mux;
     67 	struct wsdisplay_addscreendata asd;
     68 	struct wsdisplay_delscreendata dsd;
     69 	struct wsmux_device wmd;
     70 
     71 	setprogname(argv[0]);
     72 	wsdev = DEFDEV;
     73 	delete = 0;
     74 	kbd = 0;
     75 	mux = 0;
     76 	swtch = 0;
     77 	idx = -1;
     78 	asd.screentype = 0;
     79 	asd.emul = 0;
     80 	dsd.flags = 0;
     81 
     82 	while ((c = getopt(argc, argv, "f:dkmst:e:F")) != -1) {
     83 		switch (c) {
     84 		case 'f':
     85 			wsdev = optarg;
     86 			break;
     87 		case 'd':
     88 			delete++;
     89 			break;
     90 		case 'k':
     91 			kbd++;
     92 			break;
     93 		case 'm':
     94 			mux++;
     95 			kbd++;
     96 			break;
     97 		case 's':
     98 			swtch++;
     99 			break;
    100 		case 't':
    101 			asd.screentype = optarg;
    102 			break;
    103 		case 'e':
    104 			asd.emul = optarg;
    105 			break;
    106 		case 'F':
    107 			dsd.flags |= WSDISPLAY_DELSCR_FORCE;
    108 			break;
    109 		case '?':
    110 		default:
    111 			usage();
    112 			break;
    113 		}
    114 	}
    115 	argc -= optind;
    116 	argv += optind;
    117 
    118 	if ((kbd || swtch) ? (argc > 1) : (argc != 1))
    119 		usage();
    120 
    121 	if (argc > 0 && sscanf(argv[0], "%d", &idx) != 1)
    122 		errx(1, "invalid index");
    123 
    124 	if ((wsfd = open(wsdev, O_RDWR)) == -1)
    125 		err(EXIT_FAILURE, "Cannot open `%s'", wsdev);
    126 
    127 	if (swtch) {
    128 		if (ioctl(wsfd, VT_ACTIVATE, idx) == -1)
    129 		    err(EXIT_FAILURE, "Cannot switch to %d", idx);
    130 	} else if (kbd) {
    131 		wmd.type = mux ? WSMUX_MUX : WSMUX_KBD;
    132 		wmd.idx = idx;
    133 		if (delete) {
    134 			if (ioctl(wsfd, WSMUX_REMOVE_DEVICE, &wmd) == -1)
    135 				err(EXIT_FAILURE, "WSMUX_REMOVE_DEVICE");
    136 		} else {
    137 			if (ioctl(wsfd, WSMUX_ADD_DEVICE, &wmd) == -1)
    138 				err(EXIT_FAILURE, "WSMUX_ADD_DEVICE");
    139 		}
    140 	} else if (delete) {
    141 		dsd.idx = idx;
    142 		if (ioctl(wsfd, WSDISPLAYIO_DELSCREEN, &dsd) == -1)
    143 			err(EXIT_FAILURE, "WSDISPLAYIO_DELSCREEN");
    144 	} else {
    145 		asd.idx = idx;
    146 		if (ioctl(wsfd, WSDISPLAYIO_ADDSCREEN, &asd) == -1) {
    147 			if (errno == EBUSY)
    148 				errx(EXIT_FAILURE,
    149 				    "screen %d is already configured", idx);
    150 			else
    151 				err(EXIT_FAILURE, "WSDISPLAYIO_ADDSCREEN");
    152 		}
    153 	}
    154 
    155 	return 0;
    156 }
    157