iteconfig.c revision 1.1 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.1 chopps * $Id: iteconfig.c,v 1.1 1994/04/05 01:56:46 chopps 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.1 chopps #include <unistd.h>
38 1.1 chopps #include <termios.h>
39 1.1 chopps #include <fcntl.h>
40 1.1 chopps #include <stdio.h>
41 1.1 chopps #include <stdlib.h>
42 1.1 chopps #include <limits.h>
43 1.1 chopps #include <errno.h>
44 1.1 chopps
45 1.1 chopps #include <amiga/dev/grfabs_reg.h>
46 1.1 chopps #include <amiga/dev/viewioctl.h>
47 1.1 chopps #include <amiga/dev/iteioctl.h>
48 1.1 chopps
49 1.1 chopps colormap_t *xgetcmap __P((int, int));
50 1.1 chopps void printcmap __P((colormap_t *, int));
51 1.1 chopps void xioctl __P((int, int, void *));
52 1.1 chopps long xstrtol __P((char *));
53 1.1 chopps void xusage __P((void));
54 1.1 chopps
55 1.1 chopps char *pname;
56 1.1 chopps char *optstr = "W:w:H:h:D:d:V:v:T:t:P:p:X:x:Y:y:i";
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 extern int optind;
64 1.1 chopps extern char *optarg;
65 1.1 chopps struct ite_window_size is, newis;
66 1.1 chopps struct ite_bell_values ib, newib;
67 1.1 chopps struct winsize ws;
68 1.1 chopps colormap_t *cm;
69 1.1 chopps int opt, fd, f_info, i, max_colors, rv;
70 1.1 chopps long val;
71 1.1 chopps
72 1.1 chopps f_info = 0;
73 1.1 chopps pname = argv[0];
74 1.1 chopps
75 1.1 chopps fd = open("/dev/ite0", O_NONBLOCK | O_RDONLY);
76 1.1 chopps if (fd == -1) {
77 1.1 chopps perror("open console");
78 1.1 chopps exit(1);
79 1.1 chopps }
80 1.1 chopps
81 1.1 chopps xioctl(fd, ITE_GET_WINDOW_SIZE, &is);
82 1.1 chopps xioctl(fd, ITE_GET_BELL_VALUES, &ib);
83 1.1 chopps
84 1.1 chopps bcopy(&is, &newis, sizeof(is));
85 1.1 chopps bcopy(&ib, &newib, sizeof(ib));
86 1.1 chopps
87 1.1 chopps while ((opt = getopt(argc, argv, optstr)) != EOF) {
88 1.1 chopps switch (opt) {
89 1.1 chopps case 'i':
90 1.1 chopps f_info = 1;
91 1.1 chopps break;
92 1.1 chopps case 'X':
93 1.1 chopps case 'x':
94 1.1 chopps newis.x = xstrtol(optarg);
95 1.1 chopps break;
96 1.1 chopps case 'Y':
97 1.1 chopps case 'y':
98 1.1 chopps newis.y = xstrtol(optarg);
99 1.1 chopps break;
100 1.1 chopps case 'W':
101 1.1 chopps case 'w':
102 1.1 chopps newis.width = xstrtol(optarg);
103 1.1 chopps break;
104 1.1 chopps case 'H':
105 1.1 chopps case 'h':
106 1.1 chopps newis.height = xstrtol(optarg);
107 1.1 chopps break;
108 1.1 chopps case 'D':
109 1.1 chopps case 'd':
110 1.1 chopps newis.depth = xstrtol(optarg);
111 1.1 chopps break;
112 1.1 chopps case 'V':
113 1.1 chopps case 'v':
114 1.1 chopps newib.volume = xstrtol(optarg);
115 1.1 chopps break;
116 1.1 chopps case 'P':
117 1.1 chopps case 'p':
118 1.1 chopps newib.period = xstrtol(optarg);
119 1.1 chopps break;
120 1.1 chopps case 'T':
121 1.1 chopps case 't':
122 1.1 chopps newib.time = xstrtol(optarg);
123 1.1 chopps break;
124 1.1 chopps default:
125 1.1 chopps case '?':
126 1.1 chopps xusage();
127 1.1 chopps /* NOTREACHED */
128 1.1 chopps }
129 1.1 chopps }
130 1.1 chopps argc -= optind;
131 1.1 chopps argv += optind;
132 1.1 chopps
133 1.1 chopps if (bcmp(&newis, &is, sizeof(is))) {
134 1.1 chopps xioctl(fd, ITE_SET_WINDOW_SIZE, &newis);
135 1.1 chopps xioctl(fd, ITE_GET_WINDOW_SIZE, &is);
136 1.1 chopps }
137 1.1 chopps if (bcmp(&newib, &ib, sizeof(ib))) {
138 1.1 chopps xioctl(fd, ITE_SET_BELL_VALUES, &newib);
139 1.1 chopps xioctl(fd, ITE_GET_BELL_VALUES, &ib);
140 1.1 chopps }
141 1.1 chopps
142 1.1 chopps /*
143 1.1 chopps * get, set and get colors again
144 1.1 chopps */
145 1.1 chopps i = 0;
146 1.1 chopps max_colors = 1 << is.depth;
147 1.1 chopps cm = xgetcmap(fd, max_colors);
148 1.1 chopps while (argc--) {
149 1.1 chopps val = xstrtol(*argv++);
150 1.1 chopps if (i >= max_colors) {
151 1.1 chopps fprintf(stderr, "%s: warn: to many colors\n", pname);
152 1.1 chopps break;
153 1.1 chopps }
154 1.1 chopps cm->entry[i] = val;
155 1.1 chopps i++;
156 1.1 chopps }
157 1.1 chopps xioctl(fd, VIEW_USECOLORMAP, cm);
158 1.1 chopps free(cm);
159 1.1 chopps cm = xgetcmap(fd, max_colors);
160 1.1 chopps
161 1.1 chopps /* do tty stuff to get it to register the changes. */
162 1.1 chopps xioctl(fd, TIOCGWINSZ, &ws);
163 1.1 chopps
164 1.1 chopps if (f_info) {
165 1.1 chopps printf("tty size: rows %d cols %d\n", ws.ws_row, ws.ws_col);
166 1.1 chopps printf("ite size: w: %d h: %d d: %d [x: %d y: %d]\n",
167 1.1 chopps is.width, is.height, is.depth, is.x, is.y);
168 1.1 chopps printf("ite bell: vol: %d count: %d period: %d\n",
169 1.1 chopps ib.volume, ib.time, ib.period);
170 1.1 chopps printcmap(cm, ws.ws_col);
171 1.1 chopps
172 1.1 chopps }
173 1.1 chopps close(fd);
174 1.1 chopps exit(0);
175 1.1 chopps }
176 1.1 chopps
177 1.1 chopps void
178 1.1 chopps xioctl(fd, cmd, addr)
179 1.1 chopps int fd, cmd;
180 1.1 chopps void *addr;
181 1.1 chopps {
182 1.1 chopps if (ioctl(fd, cmd, addr) != -1)
183 1.1 chopps return;
184 1.1 chopps
185 1.1 chopps perror("ioctl");
186 1.1 chopps exit(1);
187 1.1 chopps }
188 1.1 chopps
189 1.1 chopps long
190 1.1 chopps xstrtol(s)
191 1.1 chopps char *s;
192 1.1 chopps {
193 1.1 chopps long rv;
194 1.1 chopps
195 1.1 chopps rv = strtol(s, NULL, 0);
196 1.1 chopps if (errno != ERANGE || (rv != LONG_MAX && rv != LONG_MIN))
197 1.1 chopps return(rv);
198 1.1 chopps
199 1.1 chopps fprintf(stderr, "%s: bad format \"%s\"\n", pname, s);
200 1.1 chopps exit(1);
201 1.1 chopps }
202 1.1 chopps
203 1.1 chopps colormap_t *
204 1.1 chopps xgetcmap(fd, ncolors)
205 1.1 chopps int fd;
206 1.1 chopps int ncolors;
207 1.1 chopps {
208 1.1 chopps colormap_t *cm;
209 1.1 chopps
210 1.1 chopps cm = malloc(sizeof(colormap_t) + ncolors * sizeof(u_long));
211 1.1 chopps if (cm == NULL) {
212 1.1 chopps perror("malloc");
213 1.1 chopps exit(1);
214 1.1 chopps }
215 1.1 chopps cm->first = 0;
216 1.1 chopps cm->size = ncolors;
217 1.1 chopps cm->entry = (u_long *) & cm[1];
218 1.1 chopps xioctl(fd, VIEW_GETCOLORMAP, cm);
219 1.1 chopps return(cm);
220 1.1 chopps }
221 1.1 chopps
222 1.1 chopps void
223 1.1 chopps printcmap(cm, ncols)
224 1.1 chopps colormap_t *cm;
225 1.1 chopps int ncols;
226 1.1 chopps {
227 1.1 chopps int i, nel;
228 1.1 chopps
229 1.1 chopps switch (cm->type) {
230 1.1 chopps case CM_MONO:
231 1.1 chopps printf("monochrome\n");
232 1.1 chopps return;
233 1.1 chopps case CM_COLOR:
234 1.1 chopps printf("color levels: red: %d green: %d blue: %d\n",
235 1.1 chopps cm->red_mask + 1, cm->green_mask + 1, cm->blue_mask + 1);
236 1.1 chopps break;
237 1.1 chopps case CM_GREYSCALE:
238 1.1 chopps printf("greyscale levels: %d\n", cm->grey_mask + 1);
239 1.1 chopps break;
240 1.1 chopps }
241 1.1 chopps
242 1.1 chopps nel = ncols / 11 - 1;
243 1.1 chopps for (i = 0; i < cm->size; i++) {
244 1.1 chopps printf("0x%08lx ", cm->entry[i]);
245 1.1 chopps if ((i + 1) % nel == 0)
246 1.1 chopps printf("\n");
247 1.1 chopps }
248 1.1 chopps if ((i + 1) % nel)
249 1.1 chopps printf("\n");
250 1.1 chopps }
251 1.1 chopps
252 1.1 chopps void
253 1.1 chopps xusage()
254 1.1 chopps {
255 1.1 chopps fprintf(stderr, "usage: %s [-i] [-w width] [-h height] [-d depth]"
256 1.1 chopps " [-x off] [-y off] [-v volume] [-p period] [-t count]"
257 1.1 chopps " [color ...]\n", pname);
258 1.1 chopps exit(1);
259 1.1 chopps }
260