grfconfig.c revision 1.8 1 /* $NetBSD: grfconfig.c,v 1.8 1997/10/17 05:58:29 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Ezra Story and Bernd Ernesti.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __COPYRIGHT("@(#) Copyright (c) 1997 The NetBSD Foundation, Inc.\n\
42 All rights reserved.\n");
43 #endif /* not lint */
44
45 #ifndef lint
46 __RCSID("$NetBSD: grfconfig.c,v 1.8 1997/10/17 05:58:29 lukem Exp $");
47 #endif /* not lint */
48
49 #include <sys/file.h>
50 #include <sys/ioctl.h>
51 #include <ctype.h>
52 #include <limits.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57
58 #include <amiga/dev/grfioctl.h>
59
60 int main __P((int, char **));
61 static void print_rawdata __P((struct grfvideo_mode *, int));
62
63 static struct grf_flag {
64 u_short grf_flag_number;
65 char *grf_flag_name;
66 } grf_flags[] = {
67 {GRF_FLAGS_DBLSCAN, "doublescan"},
68 {GRF_FLAGS_LACE, "interlace"},
69 {GRF_FLAGS_PHSYNC, "+hsync"},
70 {GRF_FLAGS_NHSYNC, "-hsync"},
71 {GRF_FLAGS_PVSYNC, "+vsync"},
72 {GRF_FLAGS_NVSYNC, "-vsync"},
73 {GRF_FLAGS_SYNC_ON_GREEN, "sync-on-green"},
74 {0, 0}
75 };
76
77 /*
78 * Dynamic mode loader for NetBSD/Amiga grf devices.
79 */
80 int
81 main(ac, av)
82 int ac;
83 char **av;
84 {
85 struct grfvideo_mode gv[1];
86 struct grf_flag *grf_flagp;
87 FILE *fp;
88 int c, y, grffd;
89 int i, lineno = 0;
90 int uplim, lowlim;
91 char rawdata = 0, testmode = 0;
92 char *grfdevice = 0;
93 char *modefile = 0;
94 char buf[_POSIX2_LINE_MAX], obuf[_POSIX2_LINE_MAX];
95 char *cps[31];
96 char *p;
97 char *errortext;
98
99
100 while ((c = getopt(ac, av, "rt")) != -1) {
101 switch (c) {
102 case 'r': /* raw output */
103 rawdata = 1;
104 break;
105 case 't': /* test the modefile without setting it */
106 testmode = 1;
107 break;
108 default:
109 printf("grfconfig [-r] device [file]\n");
110 return (1);
111 }
112 }
113 ac -= optind;
114 av += optind;
115
116
117 if (ac >= 1)
118 grfdevice = av[0];
119 else {
120 printf("grfconfig: No grf device specified.\n");
121 return (1);
122 }
123
124 if (ac >= 2)
125 modefile = av[1];
126
127 if ((grffd = open(grfdevice, O_RDWR)) < 0) {
128 printf("grfconfig: can't open grf device.\n");
129 return (1);
130 }
131 /* If a mode file is specificied, load it in, don't display any info. */
132
133 if (modefile) {
134 if (!(fp = fopen(modefile, "r"))) {
135 printf("grfconfig: Cannot open mode definition "
136 "file.\n");
137 return (1);
138 }
139 while (fgets(buf, sizeof(buf), fp)) {
140 /*
141 * check for end-of-section, comments, strip off trailing
142 * spaces and newline character.
143 */
144 for (p = buf; isspace(*p); ++p)
145 continue;
146 if (*p == '\0' || *p == '#')
147 continue;
148 for (p = strchr(buf, '\0'); isspace(*--p);)
149 continue;
150 *++p = '\0';
151
152 sprintf(obuf, "%s", buf);
153 lineno = lineno + 1;
154
155 for (i = 0, *cps = strtok(buf, " \b\t\r\n");
156 cps[i] != NULL && i < 30; i++)
157 cps[i + 1] = strtok(NULL, " \b\t\r\n");
158 cps[i] = NULL;
159
160 if (cps[13] == NULL) {
161 printf("grfconfig: too few values in mode "
162 "definition file:\n %s\n", obuf);
163 return (1);
164 }
165
166 gv->pixel_clock = atoi(cps[1]);
167 gv->disp_width = atoi(cps[2]);
168 gv->disp_height = atoi(cps[3]);
169 gv->depth = atoi(cps[4]);
170 gv->hblank_start = atoi(cps[5]);
171 gv->hsync_start = atoi(cps[6]);
172 gv->hsync_stop = atoi(cps[7]);
173 gv->htotal = atoi(cps[8]);
174 gv->vblank_start = atoi(cps[9]);
175 gv->vsync_start = atoi(cps[10]);
176 gv->vsync_stop = atoi(cps[11]);
177 gv->vtotal = atoi(cps[12]);
178
179 if ((y = atoi(cps[0])))
180 gv->mode_num = y;
181 else
182 if (strncasecmp("c", cps[0], 1) == 0) {
183 gv->mode_num = 255;
184 gv->depth = 4;
185 } else {
186 printf("grfconfig: Illegal mode "
187 "number: %s\n", cps[0]);
188 return (1);
189 }
190
191 if ((gv->pixel_clock == 0) ||
192 (gv->disp_width == 0) ||
193 (gv->disp_height == 0) ||
194 (gv->depth == 0) ||
195 (gv->hblank_start == 0) ||
196 (gv->hsync_start == 0) ||
197 (gv->hsync_stop == 0) ||
198 (gv->htotal == 0) ||
199 (gv->vblank_start == 0) ||
200 (gv->vsync_start == 0) ||
201 (gv->vsync_stop == 0) ||
202 (gv->vtotal == 0)) {
203 printf("grfconfig: Illegal value in "
204 "mode #%d:\n %s\n", gv->mode_num, obuf);
205 return (1);
206 }
207
208 if (strstr(obuf, "default") != NULL) {
209 gv->disp_flags = GRF_FLAGS_DEFAULT;
210 } else {
211 gv->disp_flags = GRF_FLAGS_DEFAULT;
212 for (grf_flagp = grf_flags;
213 grf_flagp->grf_flag_number; grf_flagp++) {
214 if (strstr(obuf, grf_flagp->grf_flag_name) != NULL) {
215 gv->disp_flags |= grf_flagp->grf_flag_number;
216 }
217 }
218 if (gv->disp_flags == GRF_FLAGS_DEFAULT) {
219 printf("grfconfig: Your are using an "
220 "mode file with an obsolete "
221 "format.\n See the manpage of "
222 "grfconfig for more information "
223 "about the new mode definition "
224 "file.\n");
225 return (1);
226 }
227 }
228
229 /*
230 * Check for impossible gv->disp_flags:
231 * doublescan and interlace,
232 * +hsync and -hsync
233 * +vsync and -vsync.
234 */
235 errortext = NULL;
236 if ((gv->disp_flags & GRF_FLAGS_DBLSCAN) &&
237 (gv->disp_flags & GRF_FLAGS_LACE))
238 errortext = "Interlace and Doublescan";
239 if ((gv->disp_flags & GRF_FLAGS_PHSYNC) &&
240 (gv->disp_flags & GRF_FLAGS_NHSYNC))
241 errortext = "+hsync and -hsync";
242 if ((gv->disp_flags & GRF_FLAGS_PVSYNC) &&
243 (gv->disp_flags & GRF_FLAGS_NVSYNC))
244 errortext = "+vsync and -vsync";
245
246 if (errortext != NULL) {
247 printf("grfconfig: Illegal flags in "
248 "mode #%d: %s are both defined!\n",
249 gv->mode_num, errortext);
250 return (1);
251 }
252
253 /* Check for old horizontal cycle values */
254 if ((gv->htotal < (gv->disp_width / 4))) {
255 gv->hblank_start *= 8;
256 gv->hsync_start *= 8;
257 gv->hsync_stop *= 8;
258 gv->htotal *= 8;
259 printf("grfconfig: Old and no longer "
260 "supported horizontal videoclock cycle "
261 "values.\n Wrong mode line:\n %s\n "
262 "This could be a possible good mode "
263 "line:\n ", obuf);
264 printf("%d ", gv->mode_num);
265 print_rawdata(gv, 0);
266 printf(" See the manpage of grfconfig for "
267 "more information about the new mode "
268 "definition file.\n");
269 return (1);
270 }
271
272 /* Check for old interlace or doublescan modes */
273 uplim = gv->disp_height + (gv->disp_height / 4);
274 lowlim = gv->disp_height - (gv->disp_height / 4);
275 if (((gv->vtotal * 2) > lowlim) &&
276 ((gv->vtotal * 2) < uplim)) {
277 gv->vblank_start *= 2;
278 gv->vsync_start *= 2;
279 gv->vsync_stop *= 2;
280 gv->vtotal *= 2;
281 gv->disp_flags &= ~GRF_FLAGS_DBLSCAN;
282 gv->disp_flags |= GRF_FLAGS_LACE;
283 printf("grfconfig: Old and no longer "
284 "supported vertical values for "
285 "interlace modes.\n Wrong mode "
286 "line:\n %s\n This could be a "
287 "possible good mode line:\n ", obuf);
288 printf("%d ", gv->mode_num);
289 print_rawdata(gv, 0);
290 printf(" See the manpage of grfconfig for "
291 "more information about the new mode "
292 "definition file.\n");
293 return (1);
294 } else if (((gv->vtotal / 2) > lowlim) &&
295 ((gv->vtotal / 2) < uplim)) {
296 gv->vblank_start /= 2;
297 gv->vsync_start /= 2;
298 gv->vsync_stop /= 2;
299 gv->vtotal /= 2;
300 gv->disp_flags &= ~GRF_FLAGS_LACE;
301 gv->disp_flags |= GRF_FLAGS_DBLSCAN;
302 printf("grfconfig: Old and no longer "
303 "supported vertical values for "
304 "doublescan modes.\n Wrong mode "
305 "line:\n %s\n This could be a "
306 "possible good mode line:\n ", obuf);
307 printf("%d ", gv->mode_num);
308 print_rawdata(gv, 0);
309 printf(" See the manpage of grfconfig for "
310 "more information about the new mode "
311 "definition file.\n");
312 return (1);
313 }
314
315 if (testmode == 1) {
316 if (lineno == 1)
317 printf("num clk wid hi dep hbs "
318 "hss hse ht vbs vss vse vt "
319 "flags\n");
320 printf("%d ", gv->mode_num);
321 print_rawdata(gv, 1);
322 } else {
323 gv->mode_descr[0] = 0;
324 if (ioctl(grffd, GRFIOCSETMON, (char *) gv) < 0)
325 printf("grfconfig: bad monitor "
326 "definition for mode #%d.\n",
327 gv->mode_num);
328 }
329 }
330 fclose(fp);
331 } else {
332 ioctl(grffd, GRFGETNUMVM, &y);
333 y += 2;
334 for (c = 1; c < y; c++) {
335 c = gv->mode_num = (c != (y - 1)) ? c : 255;
336 if (ioctl(grffd, GRFGETVMODE, gv) < 0)
337 continue;
338 if (rawdata) {
339 if (c == 255)
340 printf("c ");
341 else
342 printf("%d ", c);
343 print_rawdata(gv, 0);
344 continue;
345 }
346 if (c == 255)
347 printf("Console: ");
348 else
349 printf("%2d: ", gv->mode_num);
350
351 printf("%dx%d",
352 gv->disp_width,
353 gv->disp_height);
354
355 if (c != 255)
356 printf("x%d", gv->depth);
357 else
358 printf(" (%dx%d)",
359 gv->disp_width / 8,
360 gv->disp_height / gv->depth);
361
362 printf("\t%ld.%ldkHz @ %ldHz",
363 gv->pixel_clock / (gv->htotal * 1000),
364 (gv->pixel_clock / (gv->htotal * 100))
365 % 10,
366 gv->pixel_clock / (gv->htotal * gv->vtotal));
367 printf(" flags:");
368
369 if (gv->disp_flags == GRF_FLAGS_DEFAULT) {
370 printf(" default");
371 } else {
372 for (grf_flagp = grf_flags;
373 grf_flagp->grf_flag_number; grf_flagp++) {
374 if (gv->disp_flags & grf_flagp->grf_flag_number) {
375 printf(" %s", grf_flagp->grf_flag_name);
376 }
377 }
378 }
379 printf("\n");
380 }
381 }
382
383 close(grffd);
384 return (0);
385 }
386
387 static void
388 print_rawdata(gv, rawflags)
389 struct grfvideo_mode *gv;
390 int rawflags;
391 {
392 struct grf_flag *grf_flagp;
393
394 printf("%ld %d %d %d %d %d %d %d %d %d %d %d",
395 gv->pixel_clock,
396 gv->disp_width,
397 gv->disp_height,
398 gv->depth,
399 gv->hblank_start,
400 gv->hsync_start,
401 gv->hsync_stop,
402 gv->htotal,
403 gv->vblank_start,
404 gv->vsync_start,
405 gv->vsync_stop,
406 gv->vtotal);
407 if (rawflags) {
408 printf(" 0x%.2x", gv->disp_flags);
409 } else {
410 if (gv->disp_flags == GRF_FLAGS_DEFAULT) {
411 printf(" default");
412 } else {
413 for (grf_flagp = grf_flags;
414 grf_flagp->grf_flag_number; grf_flagp++) {
415 if (gv->disp_flags & grf_flagp->grf_flag_number) {
416 printf(" %s", grf_flagp->grf_flag_name);
417 }
418 }
419 }
420 }
421 printf("\n");
422 }
423