summaryrefslogtreecommitdiff
path: root/usr.sbin/grfconfig/grfconfig.c
blob: 53ae7aff413b194b79b3ba254d92f17774011157 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*	$NetBSD: grfconfig.c,v 1.2 1995/10/09 04:11:20 chopps Exp $	*/

/*
 * Copyright (c) 1995 Ezra Story
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Ezra Story.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/ioctl.h>

#include <amiga/dev/grfioctl.h>

extern char *optarg;
extern int optind;

/*
 * Dynamic mode loader for NetBSD/Amiga grf devices.
 */
int
main(ac, av)
	int     ac;
	char  **av;
{
	int     c, y, grffd;
	char    rawdata = 0;
    	char    oldmode = 1;
	char   *grfdevice = 0;
	char   *modefile = 0;
	char    buf[102];
	char    ystr[20];
	FILE   *fp;
	struct grfvideo_mode gv[1];

	while ((c = getopt(ac, av, "ro")) != -1) {
		switch (c) {
		case 'r':	/* raw output */
			rawdata = 1;
			break;
    	    	case 'o':
    	    	    	oldmode = 8;
    	    	    	break;
		default:
			printf("grfconfig [-r] device [file]\n");
			return (1);
		}
	}
	ac -= optind;
	av += optind;


	if (ac >= 1)
		grfdevice = av[0];
	else {
		printf("grfconfig: No grf device specified.\n");
		return (1);
	}

	if (ac >= 2)
		modefile = av[1];

	if ((grffd = open(grfdevice, O_RDWR)) < 0) {
		printf("grfconfig: can't open grf device.\n");
		return (1);
	}
	/* If a mode file is specificied, load it in, don't display any info. */

	if (modefile) {
		if (!(fp = fopen(modefile, "r"))) {
			printf("grfconfig: Cannot open mode definition file.\n");
			return (1);
		}
		while (fgets(buf, 300, fp)) {
			if (buf[0] == '#')
				continue;

			/* num clk wid hi dep hbs hss hse hbe ht vbs vss vse
			 * vbe vt */

			c = sscanf(buf, "%9s %d %hd %hd %hd %hd %hd %hd "
			    "%hd %hd %hd %hd %hd %hd %hd",
			    ystr,
			    &gv->pixel_clock,
			    &gv->disp_width,
			    &gv->disp_height,
			    &gv->depth,
			    &gv->hblank_start,
			    &gv->hsync_start,
			    &gv->hsync_stop,
			    &gv->hblank_stop,
			    &gv->htotal,
			    &gv->vblank_start,
			    &gv->vsync_start,
			    &gv->vsync_stop,
			    &gv->vblank_stop,
			    &gv->vtotal);
			if (c == 15) {
				if (y = atoi(ystr))
					gv->mode_num = y;
				else
					if (ystr[0] == 'c') {
						gv->mode_num = 255;
						gv->depth = 4;
					}
				gv->mode_descr[0] = 0;
				if (ioctl(grffd, GRFIOCSETMON, (char *) gv) < 0)
					printf("grfocnfig: bad monitor "
					    "definition.\n");
			} else {
				printf("grfconfig: bad line in mode "
				    "definition file.\n");
			}
		}
		fclose(fp);
	} else {
		ioctl(grffd, GRFGETNUMVM, &y);
		y += 2;
		for (c = 1; c < y; c++) {
			c = gv->mode_num = (c != (y - 1)) ? c : 255;
			if (ioctl(grffd, GRFGETVMODE, gv) < 0)
				continue;
			if (rawdata) {
				if (c == 255)
					printf("c ");
				else
					printf("%d ", c);
				printf("%d %d %d %d %d %d %d "
				    "%d %d %d %d %d %d %d\n",
				    gv->pixel_clock,
				    gv->disp_width,
				    gv->disp_height,
				    gv->depth,
				    gv->hblank_start,
				    gv->hsync_start,
				    gv->hsync_stop,
				    gv->hblank_stop,
				    gv->htotal,
				    gv->vblank_start,
				    gv->vsync_start,
				    gv->vsync_stop,
				    gv->vblank_stop,
				    gv->vtotal
				    );
				continue;
			}
			if (c == 255)
				printf("Console: ");
			else
				printf("%2d: ", gv->mode_num);

			printf("%dx%d",
			    gv->disp_width,
			    gv->disp_height);

			if (c != 255)
				printf("x%d", gv->depth);
			else
				printf(" (%dx%d)",
				    gv->disp_width / 8,
				    gv->disp_height / gv->depth);

			printf("\t%d.%dkHz @ %dHz %s\n",
			    gv->pixel_clock / (gv->htotal * 1000 * oldmode),
			    (gv->pixel_clock / (gv->htotal * 100 * oldmode)) 
    	    	    	    	% 10,
			    gv->pixel_clock / (gv->htotal * gv->vtotal * 
    	    	    	    	oldmode),
			    gv->vblank_start + 100 < gv->disp_height ?
			    "I" :
			    (gv->vblank_start - 100) > gv->disp_height ?
			    "SD" :
			    "NI"
			    );
		}
	}

	close(grffd);
	return (0);
}