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