videomode.c revision 1.9 1 1.9 sevan /* $NetBSD: videomode.c,v 1.9 2018/01/23 21:06:26 sevan 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.9 sevan void dump_mode(int);
49 1.9 sevan void dump_vm(struct grfvideo_mode *);
50 1.9 sevan int get_grf(void);
51 1.9 sevan void set_mode(int);
52 1.9 sevan void usage(void) __dead;
53 1.1 chopps
54 1.1 chopps int
55 1.9 sevan main(int argc, 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.3 lukem while ((c = getopt(argc, argv, "as:")) != -1) {
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.4 mycroft if (!S_ISCHR(stb.st_mode) || !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.7 he (void)snprintf(grfname, sizeof(grfname), "/dev/grf%u",
105 1.7 he (u_int)minor(stb.st_rdev) & 0x7);
106 1.1 chopps if ((grffd = open(grfname, 2)) < 0)
107 1.1 chopps err(1, "%s", grfname);
108 1.1 chopps return (grffd);
109 1.1 chopps }
110 1.1 chopps
111 1.1 chopps void
112 1.9 sevan dump_mode(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.8 wiz if (m >= 0) {
132 1.8 wiz (void)close(grffd);
133 1.1 chopps return;
134 1.8 wiz }
135 1.1 chopps for (m = 1; m <= num_vm; m++) {
136 1.1 chopps vm.mode_num = m;
137 1.1 chopps if (ioctl(grffd, GRFGETVMODE, &vm) == -1)
138 1.1 chopps break;
139 1.1 chopps dump_vm(&vm);
140 1.1 chopps }
141 1.8 wiz (void)close(grffd);
142 1.1 chopps }
143 1.1 chopps
144 1.1 chopps void
145 1.9 sevan set_mode(int m)
146 1.1 chopps {
147 1.1 chopps int grffd;
148 1.1 chopps
149 1.1 chopps grffd = get_grf();
150 1.1 chopps (void)ioctl(grffd, GRFSETVMODE, &m);
151 1.8 wiz (void)close(grffd);
152 1.1 chopps }
153 1.1 chopps
154 1.1 chopps void
155 1.9 sevan dump_vm(struct grfvideo_mode *vm)
156 1.1 chopps {
157 1.1 chopps (void)printf("%d: %s\n", vm->mode_num, vm->mode_descr);
158 1.3 lukem (void)printf(
159 1.3 lukem "pixel_clock = %lu, width = %d, height = %d, depth = %d\n",
160 1.1 chopps vm->pixel_clock, vm->disp_width, vm->disp_height, vm->depth);
161 1.1 chopps }
162 1.1 chopps
163 1.1 chopps void
164 1.1 chopps usage()
165 1.1 chopps {
166 1.1 chopps (void)fprintf(stderr, "usage: videomode [mode]\n");
167 1.1 chopps (void)fprintf(stderr, "usage: videomode -a\n");
168 1.1 chopps (void)fprintf(stderr, "usage: videomode -s mode\n");
169 1.1 chopps exit(0);
170 1.1 chopps }
171