Home | History | Annotate | Line # | Download | only in wsconscfg
wsconscfg.c revision 1.14
      1 /* $NetBSD: wsconscfg.c,v 1.14 2006/06/24 21:04:48 wiz 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__((__noreturn__));
     50 int main(int, char **);
     51 
     52 static void
     53 usage(void)
     54 {
     55 	const char *p = getprogname();
     56 	(void)fprintf(stderr,
     57 	     "Usage: %s [-e emul] [-f ctldev] [-t type] index\n"
     58 	     "\t%s -d [-F] [-f ctldev] index\n"
     59 	     "\t%s -g [-f ctldev]\n"
     60 	     "\t%s -k | -m [-d] [-f ctldev] [index]\n"
     61 	     "\t%s -s [-f ctldev] index\n", p, p, p, p, p);
     62 	exit(1);
     63 }
     64 
     65 int
     66 main(int argc, char **argv)
     67 {
     68 	const char *wsdev;
     69 	int c, delete, kbd, idx, wsfd, swtch, get, mux;
     70 	struct wsdisplay_addscreendata asd;
     71 	struct wsdisplay_delscreendata dsd;
     72 	struct wsmux_device wmd;
     73 
     74 	setprogname(argv[0]);
     75 	wsdev = DEFDEV;
     76 	delete = 0;
     77 	kbd = 0;
     78 	mux = 0;
     79 	swtch = 0;
     80 	idx = -1;
     81 	get = 0;
     82 	asd.screentype = 0;
     83 	asd.emul = 0;
     84 	dsd.flags = 0;
     85 
     86 	while ((c = getopt(argc, argv, "de:Ff:gkmst:")) != -1) {
     87 		switch (c) {
     88 		case 'd':
     89 			delete++;
     90 			break;
     91 		case 'e':
     92 			asd.emul = optarg;
     93 			break;
     94 		case 'F':
     95 			dsd.flags |= WSDISPLAY_DELSCR_FORCE;
     96 			break;
     97 		case 'f':
     98 			wsdev = optarg;
     99 			break;
    100 		case 'g':
    101 			get++;
    102 			break;
    103 		case 'k':
    104 			kbd++;
    105 			break;
    106 		case 'm':
    107 			mux++;
    108 			kbd++;
    109 			break;
    110 		case 's':
    111 			swtch++;
    112 			break;
    113 		case 't':
    114 			asd.screentype = optarg;
    115 			break;
    116 		case '?':
    117 		default:
    118 			usage();
    119 			break;
    120 		}
    121 	}
    122 	argc -= optind;
    123 	argv += optind;
    124 
    125 	if (!get || argc != 0) {
    126 		if ((kbd || swtch) ? (argc > 1) : (argc != 1))
    127 			usage();
    128 
    129 		if (argc > 0 && sscanf(argv[0], "%d", &idx) != 1)
    130 			errx(1, "invalid index");
    131 	}
    132 	if ((wsfd = open(wsdev, get ? O_RDONLY : O_RDWR)) == -1)
    133 		err(EXIT_FAILURE, "Cannot open `%s'", wsdev);
    134 
    135 
    136 	if (swtch) {
    137 		if (ioctl(wsfd, VT_ACTIVATE, idx) == -1)
    138 		    err(EXIT_FAILURE, "Cannot switch to %d", idx);
    139 	} else if (get) {
    140 		if (ioctl(wsfd, VT_GETACTIVE, &idx) == -1)
    141 		    err(EXIT_FAILURE, "Cannot get current screen");
    142 		(void)printf("%d\n", idx);
    143 	} else if (kbd) {
    144 		wmd.type = mux ? WSMUX_MUX : WSMUX_KBD;
    145 		wmd.idx = idx;
    146 		if (delete) {
    147 			if (ioctl(wsfd, WSMUX_REMOVE_DEVICE, &wmd) == -1)
    148 				err(EXIT_FAILURE, "WSMUX_REMOVE_DEVICE");
    149 		} else {
    150 			if (ioctl(wsfd, WSMUX_ADD_DEVICE, &wmd) == -1)
    151 				err(EXIT_FAILURE, "WSMUX_ADD_DEVICE");
    152 		}
    153 	} else if (delete) {
    154 		dsd.idx = idx;
    155 		if (ioctl(wsfd, WSDISPLAYIO_DELSCREEN, &dsd) == -1)
    156 			err(EXIT_FAILURE, "WSDISPLAYIO_DELSCREEN");
    157 	} else {
    158 		asd.idx = idx;
    159 		if (ioctl(wsfd, WSDISPLAYIO_ADDSCREEN, &asd) == -1) {
    160 			if (errno == EBUSY)
    161 				errx(EXIT_FAILURE,
    162 				    "screen %d is already configured", idx);
    163 			else
    164 				err(EXIT_FAILURE, "WSDISPLAYIO_ADDSCREEN");
    165 		}
    166 	}
    167 
    168 	return 0;
    169 }
    170