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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29#include <sys/cdefs.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <fcntl.h>
33#include <unistd.h>
34#include <sys/types.h>
35#include <sys/ioctl.h>
36#include <err.h>
37#include <errno.h>
38
39#include <dev/wscons/wsconsio.h>
40#include <dev/wscons/wsdisplay_usl_io.h>
41
42#define DEFDEV "/dev/ttyEcfg"
43
44static void usage(void) __dead;
45int main(int, char **);
46
47static void
48usage(void)
49{
50	const char *p = getprogname();
51	(void)fprintf(stderr,
52	     "Usage: %s [-e emul] [-f ctldev] [-t type] index\n"
53	     "\t%s -d [-F] [-f ctldev] index\n"
54	     "\t%s -g [-f ctldev]\n"
55	     "\t%s -k | -m [-d] [-f ctldev] [index]\n"
56	     "\t%s -s [-f ctldev] index\n", p, p, p, p, p);
57	exit(1);
58}
59
60int
61main(int argc, char **argv)
62{
63	const char *wsdev;
64	int c, delete, kbd, idx, wsfd, swtch, get, mux;
65	struct wsdisplay_addscreendata asd;
66	struct wsdisplay_delscreendata dsd;
67	struct wsmux_device wmd;
68
69	setprogname(argv[0]);
70	wsdev = DEFDEV;
71	delete = 0;
72	kbd = 0;
73	mux = 0;
74	swtch = 0;
75	idx = -1;
76	get = 0;
77	asd.screentype = 0;
78	asd.emul = 0;
79	dsd.flags = 0;
80
81	while ((c = getopt(argc, argv, "de:Ff:gkmst:")) != -1) {
82		switch (c) {
83		case 'd':
84			delete++;
85			break;
86		case 'e':
87			asd.emul = optarg;
88			break;
89		case 'F':
90			dsd.flags |= WSDISPLAY_DELSCR_FORCE;
91			break;
92		case 'f':
93			wsdev = optarg;
94			break;
95		case 'g':
96			get++;
97			break;
98		case 'k':
99			kbd++;
100			break;
101		case 'm':
102			mux++;
103			kbd++;
104			break;
105		case 's':
106			swtch++;
107			break;
108		case 't':
109			asd.screentype = optarg;
110			break;
111		case '?':
112		default:
113			usage();
114			break;
115		}
116	}
117	argc -= optind;
118	argv += optind;
119
120	if (!get || argc != 0) {
121		if ((kbd || swtch) ? (argc > 1) : (argc != 1))
122			usage();
123
124		if (argc > 0 && sscanf(argv[0], "%d", &idx) != 1)
125			errx(1, "invalid index");
126	}
127	if ((wsfd = open(wsdev, get ? O_RDONLY : O_RDWR)) == -1)
128		err(EXIT_FAILURE, "Cannot open `%s'", wsdev);
129
130
131	if (swtch) {
132		if (ioctl(wsfd, VT_ACTIVATE, idx) == -1)
133		    err(EXIT_FAILURE, "Cannot switch to %d", idx);
134	} else if (get) {
135		if (ioctl(wsfd, VT_GETACTIVE, &idx) == -1)
136		    err(EXIT_FAILURE, "Cannot get current screen");
137		(void)printf("%d\n", idx);
138	} else if (kbd) {
139		wmd.type = mux ? WSMUX_MUX : WSMUX_KBD;
140		wmd.idx = idx;
141		if (delete) {
142			if (ioctl(wsfd, WSMUX_REMOVE_DEVICE, &wmd) == -1)
143				err(EXIT_FAILURE, "WSMUX_REMOVE_DEVICE");
144		} else {
145			if (ioctl(wsfd, WSMUX_ADD_DEVICE, &wmd) == -1)
146				err(EXIT_FAILURE, "WSMUX_ADD_DEVICE");
147		}
148	} else if (delete) {
149		dsd.idx = idx;
150		if (ioctl(wsfd, WSDISPLAYIO_DELSCREEN, &dsd) == -1)
151			err(EXIT_FAILURE, "WSDISPLAYIO_DELSCREEN");
152	} else {
153		asd.idx = idx;
154		if (ioctl(wsfd, WSDISPLAYIO_ADDSCREEN, &asd) == -1) {
155			if (errno == EBUSY)
156				errx(EXIT_FAILURE,
157				    "screen %d is already configured", idx);
158			else
159				err(EXIT_FAILURE, "WSDISPLAYIO_ADDSCREEN");
160		}
161	}
162
163	return 0;
164}
165