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