xref: /netbsd-src/usr.sbin/iteconfig/iteconfig.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
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.3 1994/04/10 00:56:57 chopps 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 
52 void	printcmap __P((colormap_t *, int));
53 void	usage __P((void));
54 void	xioctl __P((int, int, void *));
55 colormap_t *xgetcmap __P((int, int));
56 long	xstrtol __P((char *));
57 
58 int
59 main(argc, argv)
60 	int argc;
61 	char **argv;
62 {
63 	struct itewinsize is, newis;
64 	struct itebell 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, ITEIOCGWINSZ, &is);
77 	xioctl(fd, ITEIOCGBELL, &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':
97 			newib.pitch = xstrtol(optarg);
98 			break;
99 		case 't':
100 			newib.msec = xstrtol(optarg);
101 			break;
102 		case 'V':		/* undocumented backward compat */
103 		case 'v':
104 			newib.volume = xstrtol(optarg);
105 			break;
106 		case 'W':		/* undocumented backward compat */
107 		case 'w':
108 			newis.width = xstrtol(optarg);
109 			break;
110 		case 'X':		/* undocumented backward compat */
111 		case 'x':
112 			newis.x = xstrtol(optarg);
113 			break;
114 		case 'Y':		/* undocumented backward compat */
115 		case 'y':
116 			newis.y = xstrtol(optarg);
117 			break;
118 		case '?':
119 		default:
120 			usage();
121 			/* NOTREACHED */
122 		}
123 	}
124 	argc -= optind;
125 	argv += optind;
126 
127 	if (memcmp(&newis, &is, sizeof(is))) {
128 		xioctl(fd, ITEIOCSWINSZ, &newis);
129 		xioctl(fd, ITEIOCGWINSZ, &is);
130 	}
131 	if (memcmp(&newib, &ib, sizeof(ib))) {
132 		xioctl(fd, ITEIOCSBELL, &newib);
133 		xioctl(fd, ITEIOCGBELL, &ib);
134 	}
135 
136 	/*
137 	 * get, set and get colors again
138 	 */
139 	i = 0;
140 	max_colors = 1 << is.depth;
141 	cm = xgetcmap(fd, max_colors);
142 	while (argc--) {
143 		val = xstrtol(*argv++);
144 		if (i >= max_colors) {
145 			warnx("warning: too many colors");
146 			break;
147 		}
148 		cm->entry[i] = val;
149 		i++;
150 	}
151 	xioctl(fd, VIOCSCMAP, cm);
152 	free(cm);
153 	cm = xgetcmap(fd, max_colors);
154 
155 	/* do tty stuff to get it to register the changes. */
156 	xioctl(fd, TIOCGWINSZ, &ws);
157 
158 	if (iflag) {
159 		printf("tty size: rows %d cols %d\n", ws.ws_row, ws.ws_col);
160 		printf("ite size: w: %d  h: %d  d: %d  [x: %d  y: %d]\n",
161 		    is.width, is.height, is.depth, is.x, is.y);
162 		printf("ite bell: vol: %d  millisec: %d  pitch: %d\n",
163 		    ib.volume, ib.msec, ib.pitch);
164 		printcmap(cm, ws.ws_col);
165 	}
166 	close(fd);
167 	exit(0);
168 }
169 
170 void
171 xioctl(fd, cmd, addr)
172 	int fd, cmd;
173 	void *addr;
174 {
175 	if (ioctl(fd, cmd, addr) == -1)
176 		err(1, "ioctl");
177 }
178 
179 long
180 xstrtol(s)
181 	char *s;
182 {
183 	long rv;
184 
185 	rv = strtol(s, NULL, 0);
186 	if (errno == ERANGE && (rv == LONG_MIN || rv == LONG_MAX))
187 		err(1, "bad format: \"%s\"", s);
188 	return(rv);
189 }
190 
191 colormap_t *
192 xgetcmap(fd, ncolors)
193 	int fd;
194 	int ncolors;
195 {
196 	colormap_t *cm;
197 
198 	cm = malloc(sizeof(colormap_t) + ncolors * sizeof(u_long));
199 	if (cm == NULL)
200 		err(1, "malloc");
201 	cm->first = 0;
202 	cm->size = ncolors;
203 	cm->entry = (u_long *) & cm[1];
204 	xioctl(fd, VIOCGCMAP, cm);
205 	return(cm);
206 }
207 
208 void
209 printcmap(cm, ncols)
210 	colormap_t *cm;
211 	int ncols;
212 {
213 	int i, nel;
214 
215 	switch (cm->type) {
216 	case CM_MONO:
217 		printf("monochrome");
218 		return;
219 	case CM_COLOR:
220 		printf("color levels: red: %d  green: %d  blue: %d",
221 		    cm->red_mask + 1, cm->green_mask + 1, cm->blue_mask + 1);
222 		break;
223 	case CM_GREYSCALE:
224 		printf("greyscale levels: %d", cm->grey_mask + 1);
225 		break;
226 	}
227 	printf("\n");
228 
229 	nel = ncols / 11 - 1;
230 	for (i = 0; i < cm->size; i++) {
231 		printf("0x%08lx ", cm->entry[i]);
232 		if ((i + 1) % nel == 0)
233 			printf("\n");
234 	}
235 	if ((i + 1) % nel)
236 		printf("\n");
237 }
238 
239 void
240 usage()
241 {
242 	fprintf(stderr, "%s\n\t\t%s\n",
243 	    "usage: iteconfig [-i] [-v volume] [-p period] [-t count]",
244 	    "[-w width] [-h height] [-d depth] [-x off] [-y off] [color ...]");
245 	exit(1);
246 }
247