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