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