videomode.c revision 1.2 1 1.2 chopps /* $NetBSD: videomode.c,v 1.2 1995/10/09 13:52:49 chopps 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.1 chopps #include <unistd.h>
42 1.1 chopps #include <errno.h>
43 1.1 chopps #include <err.h>
44 1.1 chopps #include <stdio.h>
45 1.1 chopps
46 1.1 chopps void dump_mode __P((int));
47 1.1 chopps void dump_vm __P((struct grfvideo_mode *));
48 1.1 chopps int get_grf __P((void));
49 1.1 chopps void set_mode __P((int));
50 1.1 chopps void usage __P((void));
51 1.1 chopps
52 1.1 chopps int
53 1.1 chopps main(argc, argv)
54 1.1 chopps int argc;
55 1.1 chopps char *argv[];
56 1.1 chopps {
57 1.1 chopps int m;
58 1.1 chopps int c;
59 1.1 chopps
60 1.1 chopps if (argc == 1) {
61 1.1 chopps dump_mode(0);
62 1.1 chopps return (0);
63 1.1 chopps }
64 1.1 chopps while ((c = getopt(argc, argv, "as:")) != EOF) {
65 1.1 chopps switch (c) {
66 1.1 chopps case 'a':
67 1.1 chopps if (optind < argc)
68 1.1 chopps usage();
69 1.1 chopps dump_mode(-1);
70 1.1 chopps return (0);
71 1.1 chopps case 's':
72 1.1 chopps m = atoi(optarg);
73 1.1 chopps if (m == 0 || optind < argc)
74 1.1 chopps usage();
75 1.1 chopps set_mode(m);
76 1.1 chopps return (0);
77 1.1 chopps }
78 1.1 chopps }
79 1.1 chopps
80 1.1 chopps argc -= optind;
81 1.1 chopps argv += optind;
82 1.1 chopps if (argc != 1)
83 1.1 chopps usage();
84 1.1 chopps
85 1.1 chopps dump_mode(atoi(*argv));
86 1.1 chopps return (0);
87 1.1 chopps }
88 1.1 chopps
89 1.1 chopps
90 1.1 chopps int
91 1.1 chopps get_grf()
92 1.1 chopps {
93 1.1 chopps struct stat stb;
94 1.1 chopps char grfname[80];
95 1.1 chopps int grffd;
96 1.1 chopps
97 1.1 chopps /* find out on which ite/grf we are */
98 1.1 chopps if (fstat(0, &stb) == -1)
99 1.1 chopps err(1, "fstat 0");
100 1.1 chopps if (((stb.st_mode & S_IFMT) != S_IFCHR) || !isatty(0))
101 1.1 chopps errx(1, "stdin not a tty");
102 1.1 chopps if (major(stb.st_rdev) != 13)
103 1.1 chopps errx(1, "stdin not an ite device");
104 1.1 chopps (void)sprintf(grfname, "/dev/grf%d", minor(stb.st_rdev) & 0x7);
105 1.1 chopps if ((grffd = open(grfname, 2)) < 0)
106 1.1 chopps err(1, "%s", grfname);
107 1.1 chopps return (grffd);
108 1.1 chopps }
109 1.1 chopps
110 1.1 chopps void
111 1.1 chopps dump_mode(m)
112 1.1 chopps int m;
113 1.1 chopps {
114 1.1 chopps struct grfvideo_mode vm;
115 1.1 chopps int num_vm;
116 1.1 chopps int grffd;
117 1.1 chopps
118 1.1 chopps grffd = get_grf();
119 1.1 chopps
120 1.1 chopps if (ioctl(grffd, GRFGETNUMVM, &num_vm) < 0)
121 1.1 chopps err(1, "GRFGETNUMVM");
122 1.1 chopps if (m > 0 && m > num_vm)
123 1.1 chopps errx(1, "no such mode");
124 1.1 chopps if (m <= 0) {
125 1.1 chopps (void)printf("Current mode:\n");
126 1.1 chopps vm.mode_num = 0;
127 1.1 chopps if (ioctl(grffd, GRFGETVMODE, &vm) == 0)
128 1.1 chopps dump_vm(&vm);
129 1.1 chopps (void)printf("\n");
130 1.1 chopps }
131 1.1 chopps if (m >= 0)
132 1.1 chopps return;
133 1.1 chopps for (m = 1; m <= num_vm; m++) {
134 1.1 chopps vm.mode_num = m;
135 1.1 chopps if (ioctl(grffd, GRFGETVMODE, &vm) == -1)
136 1.1 chopps break;
137 1.1 chopps dump_vm(&vm);
138 1.1 chopps }
139 1.1 chopps }
140 1.1 chopps
141 1.1 chopps void
142 1.1 chopps set_mode(m)
143 1.1 chopps int m;
144 1.1 chopps {
145 1.1 chopps int grffd;
146 1.1 chopps
147 1.1 chopps grffd = get_grf();
148 1.1 chopps (void)ioctl(grffd, GRFSETVMODE, &m);
149 1.1 chopps }
150 1.1 chopps
151 1.1 chopps void
152 1.1 chopps dump_vm(vm)
153 1.1 chopps struct grfvideo_mode *vm;
154 1.1 chopps {
155 1.1 chopps (void)printf("%d: %s\n", vm->mode_num, vm->mode_descr);
156 1.1 chopps (void)printf("pixel_clock = %u, width = %d, height = %d, depth = %d\n",
157 1.1 chopps vm->pixel_clock, vm->disp_width, vm->disp_height, vm->depth);
158 1.1 chopps }
159 1.1 chopps
160 1.1 chopps void
161 1.1 chopps usage()
162 1.1 chopps {
163 1.1 chopps (void)fprintf(stderr, "usage: videomode [mode]\n");
164 1.1 chopps (void)fprintf(stderr, "usage: videomode -a\n");
165 1.1 chopps (void)fprintf(stderr, "usage: videomode -s mode\n");
166 1.1 chopps exit(0);
167 1.1 chopps }
168