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