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