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