xref: /netbsd-src/usr.sbin/iteconfig/iteconfig.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: iteconfig.c,v 1.8 2003/09/27 19:35:14 mhitch 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.8 2003/09/27 19:35:14 mhitch 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 <string.h>
65 #include <termios.h>
66 #include <unistd.h>
67 
68 #include "pathnames.h"
69 
70 int	initialize __P((char *, struct itewinsize *, struct itebell *,
71 			struct itewinsize *, struct itebell *));
72 int	main __P((int, char **));
73 void	printcmap __P((colormap_t *, int));
74 void	xioctl __P((int, int, void *));
75 colormap_t *xgetcmap __P((int, int));
76 long	xstrtol __P((char *));
77 void	usage __P((void));
78 
79 int
80 main(argc, argv)
81 	int argc;
82 	char **argv;
83 {
84 	struct itewinsize is, newis;
85 	struct itebell ib, newib;
86 	struct winsize ws;
87 	colormap_t *cm;
88 	char *file = _PATH_CONSOLE;
89 	int ch, fd, i, iflag, max_colors, did_reset;
90 	long val;
91 
92 	iflag = 0;
93 	did_reset = 0;
94 
95 	fd = initialize(_PATH_CONSOLE, &is, &ib, &newis, &newib);
96 
97 	while ((ch = getopt(argc, argv, "D:H:P:T:V:W:X:Y:d:f:h:ip:t:v:w:x:y:"))
98 	    != -1) {
99 		switch (ch) {
100 		case 'D':		/* undocumented backward compat */
101 		case 'd':
102 			newis.depth = xstrtol(optarg);
103 			break;
104 		case 'f':
105 			if (did_reset)
106 				break;
107 			if (fd != -1)
108 				close(fd);
109 			file = optarg;
110 			fd = initialize(optarg, &is, &ib, &newis, &newib);
111 			did_reset = optreset = optind = 1;
112 			break;
113 		case 'H':		/* undocumented backward compat */
114 		case 'h':
115 			newis.height = xstrtol(optarg);
116 			break;
117 		case 'i':
118 			iflag = 1;
119 			break;
120 		case 'p':
121 			newib.pitch = xstrtol(optarg);
122 			break;
123 		case 't':
124 			newib.msec = xstrtol(optarg);
125 			break;
126 		case 'V':		/* undocumented backward compat */
127 		case 'v':
128 			newib.volume = xstrtol(optarg);
129 			break;
130 		case 'W':		/* undocumented backward compat */
131 		case 'w':
132 			newis.width = xstrtol(optarg);
133 			break;
134 		case 'X':		/* undocumented backward compat */
135 		case 'x':
136 			newis.x = xstrtol(optarg);
137 			break;
138 		case 'Y':		/* undocumented backward compat */
139 		case 'y':
140 			newis.y = xstrtol(optarg);
141 			break;
142 		case '?':
143 		default:
144 			usage();
145 			/* NOTREACHED */
146 		}
147 	}
148 	argc -= optind;
149 	argv += optind;
150 	if(fd == -1)
151 		err(1, "open \"%s\"", file);
152 
153 	if (memcmp(&newis, &is, sizeof(is))) {
154 		xioctl(fd, ITEIOCSWINSZ, &newis);
155 		xioctl(fd, ITEIOCGWINSZ, &is);
156 	}
157 	if (memcmp(&newib, &ib, sizeof(ib))) {
158 		xioctl(fd, ITEIOCSBELL, &newib);
159 		xioctl(fd, ITEIOCGBELL, &ib);
160 	}
161 
162 	/*
163 	 * get, set and get colors again
164 	 */
165 	i = 0;
166 	max_colors = 1 << is.depth;
167 	cm = xgetcmap(fd, max_colors);
168 	while (argc--) {
169 		val = xstrtol(*argv++);
170 		if (i >= max_colors) {
171 			warnx("warning: too many colors");
172 			break;
173 		}
174 		cm->entry[i] = val;
175 		i++;
176 	}
177 	xioctl(fd, VIOCSCMAP, cm);
178 	free(cm);
179 	cm = xgetcmap(fd, max_colors);
180 
181 	/* do tty stuff to get it to register the changes. */
182 	xioctl(fd, TIOCGWINSZ, &ws);
183 
184 	if (iflag) {
185 		printf("tty size: rows %d cols %d\n", ws.ws_row, ws.ws_col);
186 		printf("ite size: w: %d  h: %d  d: %d  [x: %d  y: %d]\n",
187 		    is.width, is.height, is.depth, is.x, is.y);
188 		printf("ite bell: vol: %d  millisec: %d  pitch: %d\n",
189 		    ib.volume, ib.msec, ib.pitch);
190 		printcmap(cm, ws.ws_col);
191 	}
192 	close(fd);
193 	exit(0);
194 }
195 
196 void
197 xioctl(fd, cmd, addr)
198 	int fd, cmd;
199 	void *addr;
200 {
201 	if (ioctl(fd, cmd, addr) == -1)
202 		err(1, "ioctl");
203 }
204 
205 long
206 xstrtol(s)
207 	char *s;
208 {
209 	long rv;
210 
211 	errno = 0;
212 	rv = strtol(s, NULL, 0);
213 	if (errno == ERANGE && (rv == LONG_MIN || rv == LONG_MAX))
214 		err(1, "bad format: \"%s\"", s);
215 	return(rv);
216 }
217 
218 colormap_t *
219 xgetcmap(fd, ncolors)
220 	int fd;
221 	int ncolors;
222 {
223 	colormap_t *cm;
224 
225 	cm = malloc(sizeof(colormap_t) + ncolors * sizeof(u_long));
226 	if (cm == NULL)
227 		err(1, "malloc");
228 	cm->first = 0;
229 	cm->size = ncolors;
230 	cm->entry = (u_long *) & cm[1];
231 	xioctl(fd, VIOCGCMAP, cm);
232 	return(cm);
233 }
234 
235 void
236 printcmap(cm, ncols)
237 	colormap_t *cm;
238 	int ncols;
239 {
240 	int i, nel;
241 
242 	switch (cm->type) {
243 	case CM_MONO:
244 		printf("monochrome");
245 		return;
246 	case CM_COLOR:
247 		printf("color levels: red: %d  green: %d  blue: %d",
248 		    cm->red_mask + 1, cm->green_mask + 1, cm->blue_mask + 1);
249 		break;
250 	case CM_GREYSCALE:
251 		printf("greyscale levels: %d", cm->grey_mask + 1);
252 		break;
253 	}
254 	printf("\n");
255 
256 	nel = ncols / 11 - 1;
257 	for (i = 0; i < cm->size; i++) {
258 		printf("0x%08lx ", cm->entry[i]);
259 		if ((i + 1) % nel == 0)
260 			printf("\n");
261 	}
262 	if ((i + 1) % nel)
263 		printf("\n");
264 }
265 
266 int
267 initialize(file, is, ib, newis, newib)
268 	char	*file;
269 	struct itewinsize *is, *newis;
270 	struct itebell *ib, *newib;
271 {
272 	int fd;
273 
274 	fd = open(file, O_RDONLY | O_NONBLOCK);
275 	if (fd == -1)
276 		return(-1);
277 
278 	xioctl(fd, ITEIOCGWINSZ, is);
279 	xioctl(fd, ITEIOCGBELL, ib);
280 
281 	memcpy(newis, is, sizeof(*is));
282 	memcpy(newib, ib, sizeof(*ib));
283 	return(fd);
284 }
285 
286 void
287 usage()
288 {
289 	fprintf(stderr, "%s\n\t\t%s\n\t\t%s\n",
290 	    "usage: iteconfig [-i] [-f file] [-v volume] [-p pitch] [-t msec]",
291 	    "[-w width] [-h height] [-d depth] [-x off] [-y off]",
292 	    "[color ...]");
293 	exit(1);
294 }
295