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