videomode.c revision 1.4 1 1.4 mycroft /* $NetBSD: videomode.c,v 1.4 1997/10/19 19:55:56 mycroft Exp $ */
2 1.1 chopps
3 1.1 chopps /*
4 1.1 chopps * Copyright (c) 1995 Christian E. Hopps
5 1.2 chopps * Copyright (c) 1994 Markus Wild
6 1.1 chopps * All rights reserved.
7 1.1 chopps *
8 1.1 chopps * Redistribution and use in source and binary forms, with or without
9 1.1 chopps * modification, are permitted provided that the following conditions
10 1.1 chopps * are met:
11 1.1 chopps * 1. Redistributions of source code must retain the above copyright
12 1.1 chopps * notice, this list of conditions and the following disclaimer.
13 1.1 chopps * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 chopps * notice, this list of conditions and the following disclaimer in the
15 1.1 chopps * documentation and/or other materials provided with the distribution.
16 1.1 chopps * 3. All advertising materials mentioning features or use of this software
17 1.1 chopps * must display the following acknowledgement:
18 1.2 chopps * This product includes software developed by Markus Wild
19 1.1 chopps * 4. The name of the author may not be used to endorse or promote products
20 1.1 chopps * derived from this software without specific prior written permission
21 1.1 chopps *
22 1.1 chopps * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 chopps * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 chopps * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 chopps * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 chopps * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 chopps * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 chopps * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 chopps * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 chopps * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 chopps * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 chopps */
33 1.1 chopps
34 1.1 chopps #include <sys/types.h>
35 1.1 chopps #include <sys/stat.h>
36 1.1 chopps #include <sys/ioctl.h>
37 1.1 chopps #include <sys/device.h>
38 1.1 chopps #include <amiga/dev/grfioctl.h>
39 1.1 chopps #include <amiga/dev/grfvar.h>
40 1.1 chopps
41 1.3 lukem #include <err.h>
42 1.1 chopps #include <errno.h>
43 1.3 lukem #include <fcntl.h>
44 1.1 chopps #include <stdio.h>
45 1.3 lukem #include <stdlib.h>
46 1.3 lukem #include <unistd.h>
47 1.1 chopps
48 1.3 lukem void dump_mode __P((int));
49 1.3 lukem void dump_vm __P((struct grfvideo_mode *));
50 1.3 lukem int get_grf __P((void));
51 1.3 lukem int main __P((int, char **));
52 1.3 lukem void set_mode __P((int));
53 1.3 lukem void usage __P((void));
54 1.1 chopps
55 1.1 chopps int
56 1.1 chopps main(argc, argv)
57 1.1 chopps int argc;
58 1.1 chopps char *argv[];
59 1.1 chopps {
60 1.1 chopps int m;
61 1.1 chopps int c;
62 1.1 chopps
63 1.1 chopps if (argc == 1) {
64 1.1 chopps dump_mode(0);
65 1.1 chopps return (0);
66 1.1 chopps }
67 1.3 lukem while ((c = getopt(argc, argv, "as:")) != -1) {
68 1.1 chopps switch (c) {
69 1.1 chopps case 'a':
70 1.1 chopps if (optind < argc)
71 1.1 chopps usage();
72 1.1 chopps dump_mode(-1);
73 1.1 chopps return (0);
74 1.1 chopps case 's':
75 1.1 chopps m = atoi(optarg);
76 1.1 chopps if (m == 0 || optind < argc)
77 1.1 chopps usage();
78 1.1 chopps set_mode(m);
79 1.1 chopps return (0);
80 1.1 chopps }
81 1.1 chopps }
82 1.1 chopps
83 1.1 chopps argc -= optind;
84 1.1 chopps argv += optind;
85 1.1 chopps if (argc != 1)
86 1.1 chopps usage();
87 1.1 chopps
88 1.1 chopps dump_mode(atoi(*argv));
89 1.1 chopps return (0);
90 1.1 chopps }
91 1.1 chopps
92 1.1 chopps
93 1.1 chopps int
94 1.1 chopps get_grf()
95 1.1 chopps {
96 1.1 chopps struct stat stb;
97 1.1 chopps char grfname[80];
98 1.1 chopps int grffd;
99 1.1 chopps
100 1.1 chopps /* find out on which ite/grf we are */
101 1.1 chopps if (fstat(0, &stb) == -1)
102 1.1 chopps err(1, "fstat 0");
103 1.4 mycroft if (!S_ISCHR(stb.st_mode) || !isatty(0))
104 1.1 chopps errx(1, "stdin not a tty");
105 1.1 chopps if (major(stb.st_rdev) != 13)
106 1.1 chopps errx(1, "stdin not an ite device");
107 1.1 chopps (void)sprintf(grfname, "/dev/grf%d", minor(stb.st_rdev) & 0x7);
108 1.1 chopps if ((grffd = open(grfname, 2)) < 0)
109 1.1 chopps err(1, "%s", grfname);
110 1.1 chopps return (grffd);
111 1.1 chopps }
112 1.1 chopps
113 1.1 chopps void
114 1.1 chopps dump_mode(m)
115 1.1 chopps int m;
116 1.1 chopps {
117 1.1 chopps struct grfvideo_mode vm;
118 1.1 chopps int num_vm;
119 1.1 chopps int grffd;
120 1.1 chopps
121 1.1 chopps grffd = get_grf();
122 1.1 chopps
123 1.1 chopps if (ioctl(grffd, GRFGETNUMVM, &num_vm) < 0)
124 1.1 chopps err(1, "GRFGETNUMVM");
125 1.1 chopps if (m > 0 && m > num_vm)
126 1.1 chopps errx(1, "no such mode");
127 1.1 chopps if (m <= 0) {
128 1.1 chopps (void)printf("Current mode:\n");
129 1.1 chopps vm.mode_num = 0;
130 1.1 chopps if (ioctl(grffd, GRFGETVMODE, &vm) == 0)
131 1.1 chopps dump_vm(&vm);
132 1.1 chopps (void)printf("\n");
133 1.1 chopps }
134 1.1 chopps if (m >= 0)
135 1.1 chopps return;
136 1.1 chopps for (m = 1; m <= num_vm; m++) {
137 1.1 chopps vm.mode_num = m;
138 1.1 chopps if (ioctl(grffd, GRFGETVMODE, &vm) == -1)
139 1.1 chopps break;
140 1.1 chopps dump_vm(&vm);
141 1.1 chopps }
142 1.1 chopps }
143 1.1 chopps
144 1.1 chopps void
145 1.1 chopps set_mode(m)
146 1.1 chopps int m;
147 1.1 chopps {
148 1.1 chopps int grffd;
149 1.1 chopps
150 1.1 chopps grffd = get_grf();
151 1.1 chopps (void)ioctl(grffd, GRFSETVMODE, &m);
152 1.1 chopps }
153 1.1 chopps
154 1.1 chopps void
155 1.1 chopps dump_vm(vm)
156 1.1 chopps struct grfvideo_mode *vm;
157 1.1 chopps {
158 1.1 chopps (void)printf("%d: %s\n", vm->mode_num, vm->mode_descr);
159 1.3 lukem (void)printf(
160 1.3 lukem "pixel_clock = %lu, width = %d, height = %d, depth = %d\n",
161 1.1 chopps vm->pixel_clock, vm->disp_width, vm->disp_height, vm->depth);
162 1.1 chopps }
163 1.1 chopps
164 1.1 chopps void
165 1.1 chopps usage()
166 1.1 chopps {
167 1.1 chopps (void)fprintf(stderr, "usage: videomode [mode]\n");
168 1.1 chopps (void)fprintf(stderr, "usage: videomode -a\n");
169 1.1 chopps (void)fprintf(stderr, "usage: videomode -s mode\n");
170 1.1 chopps exit(0);
171 1.1 chopps }
172