iteconfig.c revision 1.2
1/*
2 * Copyright (c) 1994 Christian E. Hopps
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *      This product includes software developed by Christian E. Hopps
16 * 4. The name of the author may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 *      $Id: iteconfig.c,v 1.2 1994/04/05 04:34:51 cgd Exp $
31 */
32
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <sys/ioctl.h>
36#include <sys/queue.h>
37#include <amiga/dev/grfabs_reg.h>
38#include <amiga/dev/viewioctl.h>
39#include <amiga/dev/iteioctl.h>
40
41#include <err.h>
42#include <errno.h>
43#include <fcntl.h>
44#include <limits.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <termios.h>
48#include <unistd.h>
49
50#include "pathnames.h"
51
52void	printcmap __P((colormap_t *, int));
53void	usage __P((void));
54void	xioctl __P((int, int, void *));
55colormap_t *xgetcmap __P((int, int));
56long	xstrtol __P((char *));
57
58int
59main(argc, argv)
60	int argc;
61	char **argv;
62{
63	struct ite_window_size is, newis;
64	struct ite_bell_values ib, newib;
65	struct winsize ws;
66	colormap_t *cm;
67	int ch, fd, i, iflag, max_colors;
68	long val;
69
70	iflag = 0;
71
72	fd = open(_PATH_AMIGACONSOLE, O_RDONLY | O_NONBLOCK);
73	if (fd == -1)
74		err(1, "open console");
75
76	xioctl(fd, ITE_GET_WINDOW_SIZE, &is);
77	xioctl(fd, ITE_GET_BELL_VALUES, &ib);
78
79	memcpy(&newis, &is, sizeof(is));
80	memcpy(&newib, &ib, sizeof(ib));
81
82	while ((ch = getopt(argc, argv, "D:H:P:T:V:W:X:Y:d:h:ip:t:v:w:x:y:"))
83	    != EOF) {
84		switch (ch) {
85		case 'D':		/* undocumented backward compat */
86		case 'd':
87			newis.depth = xstrtol(optarg);
88			break;
89		case 'H':		/* undocumented backward compat */
90		case 'h':
91			newis.height = xstrtol(optarg);
92			break;
93		case 'i':
94			iflag = 1;
95			break;
96		case 'P':		/* undocumented backward compat */
97		case 'p':
98			newib.period = xstrtol(optarg);
99			break;
100		case 'T':		/* undocumented backward compat */
101		case 't':
102			newib.time = xstrtol(optarg);
103			break;
104		case 'V':		/* undocumented backward compat */
105		case 'v':
106			newib.volume = xstrtol(optarg);
107			break;
108		case 'W':		/* undocumented backward compat */
109		case 'w':
110			newis.width = xstrtol(optarg);
111			break;
112		case 'X':		/* undocumented backward compat */
113		case 'x':
114			newis.x = xstrtol(optarg);
115			break;
116		case 'Y':		/* undocumented backward compat */
117		case 'y':
118			newis.y = xstrtol(optarg);
119			break;
120		case '?':
121		default:
122			usage();
123			/* NOTREACHED */
124		}
125	}
126	argc -= optind;
127	argv += optind;
128
129	if (memcmp(&newis, &is, sizeof(is))) {
130		xioctl(fd, ITE_SET_WINDOW_SIZE, &newis);
131		xioctl(fd, ITE_GET_WINDOW_SIZE, &is);
132	}
133	if (memcmp(&newib, &ib, sizeof(ib))) {
134		xioctl(fd, ITE_SET_BELL_VALUES, &newib);
135		xioctl(fd, ITE_GET_BELL_VALUES, &ib);
136	}
137
138	/*
139	 * get, set and get colors again
140	 */
141	i = 0;
142	max_colors = 1 << is.depth;
143	cm = xgetcmap(fd, max_colors);
144	while (argc--) {
145		val = xstrtol(*argv++);
146		if (i >= max_colors) {
147			warnx("warning: too many colors");
148			break;
149		}
150		cm->entry[i] = val;
151		i++;
152	}
153	xioctl(fd, VIEW_USECOLORMAP, cm);
154	free(cm);
155	cm = xgetcmap(fd, max_colors);
156
157	/* do tty stuff to get it to register the changes. */
158	xioctl(fd, TIOCGWINSZ, &ws);
159
160	if (iflag) {
161		printf("tty size: rows %d cols %d\n", ws.ws_row, ws.ws_col);
162		printf("ite size: w: %d  h: %d  d: %d  [x: %d  y: %d]\n",
163		    is.width, is.height, is.depth, is.x, is.y);
164		printf("ite bell: vol: %d  count: %d  period: %d\n",
165		    ib.volume, ib.time, ib.period);
166		printcmap(cm, ws.ws_col);
167	}
168	close(fd);
169	exit(0);
170}
171
172void
173xioctl(fd, cmd, addr)
174	int fd, cmd;
175	void *addr;
176{
177	if (ioctl(fd, cmd, addr) == -1)
178		err(1, "ioctl");
179}
180
181long
182xstrtol(s)
183	char *s;
184{
185	long rv;
186
187	rv = strtol(s, NULL, 0);
188	if (errno == ERANGE && (rv == LONG_MIN || rv == LONG_MAX))
189		err(1, "bad format: \"%s\"", s);
190	return(rv);
191}
192
193colormap_t *
194xgetcmap(fd, ncolors)
195	int fd;
196	int ncolors;
197{
198	colormap_t *cm;
199
200	cm = malloc(sizeof(colormap_t) + ncolors * sizeof(u_long));
201	if (cm == NULL)
202		err(1, "malloc");
203	cm->first = 0;
204	cm->size = ncolors;
205	cm->entry = (u_long *) & cm[1];
206	xioctl(fd, VIEW_GETCOLORMAP, cm);
207	return(cm);
208}
209
210void
211printcmap(cm, ncols)
212	colormap_t *cm;
213	int ncols;
214{
215	int i, nel;
216
217	switch (cm->type) {
218	case CM_MONO:
219		printf("monochrome");
220		return;
221	case CM_COLOR:
222		printf("color levels: red: %d  green: %d  blue: %d",
223		    cm->red_mask + 1, cm->green_mask + 1, cm->blue_mask + 1);
224		break;
225	case CM_GREYSCALE:
226		printf("greyscale levels: %d", cm->grey_mask + 1);
227		break;
228	}
229	printf("\n");
230
231	nel = ncols / 11 - 1;
232	for (i = 0; i < cm->size; i++) {
233		printf("0x%08lx ", cm->entry[i]);
234		if ((i + 1) % nel == 0)
235			printf("\n");
236	}
237	if ((i + 1) % nel)
238		printf("\n");
239}
240
241void
242usage()
243{
244	fprintf(stderr, "%s\n\t\t%s\n",
245	    "usage: iteconfig [-i] [-v volume] [-p period] [-t count]",
246	    "[-w width] [-h height] [-d depth] [-x off] [-y off] [color ...]");
247	exit(1);
248}
249