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