1*421949a3Ssevan /* $NetBSD: videomode.c,v 1.9 2018/01/23 21:06:26 sevan Exp $ */
213e967a6Schopps
313e967a6Schopps /*
413e967a6Schopps * Copyright (c) 1995 Christian E. Hopps
5b93472a2Schopps * Copyright (c) 1994 Markus Wild
613e967a6Schopps * All rights reserved.
713e967a6Schopps *
813e967a6Schopps * Redistribution and use in source and binary forms, with or without
913e967a6Schopps * modification, are permitted provided that the following conditions
1013e967a6Schopps * are met:
1113e967a6Schopps * 1. Redistributions of source code must retain the above copyright
1213e967a6Schopps * notice, this list of conditions and the following disclaimer.
1313e967a6Schopps * 2. Redistributions in binary form must reproduce the above copyright
1413e967a6Schopps * notice, this list of conditions and the following disclaimer in the
1513e967a6Schopps * documentation and/or other materials provided with the distribution.
1613e967a6Schopps * 3. All advertising materials mentioning features or use of this software
1713e967a6Schopps * must display the following acknowledgement:
18b93472a2Schopps * This product includes software developed by Markus Wild
1913e967a6Schopps * 4. The name of the author may not be used to endorse or promote products
2013e967a6Schopps * derived from this software without specific prior written permission
2113e967a6Schopps *
2213e967a6Schopps * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2313e967a6Schopps * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2413e967a6Schopps * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2513e967a6Schopps * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2613e967a6Schopps * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2713e967a6Schopps * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2813e967a6Schopps * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2913e967a6Schopps * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3013e967a6Schopps * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3113e967a6Schopps * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3213e967a6Schopps */
3313e967a6Schopps
3413e967a6Schopps #include <sys/types.h>
3513e967a6Schopps #include <sys/stat.h>
3613e967a6Schopps #include <sys/ioctl.h>
3713e967a6Schopps #include <sys/device.h>
3813e967a6Schopps #include <amiga/dev/grfioctl.h>
3913e967a6Schopps #include <amiga/dev/grfvar.h>
4013e967a6Schopps
4113e967a6Schopps #include <err.h>
4231bc9c50Slukem #include <errno.h>
4331bc9c50Slukem #include <fcntl.h>
4413e967a6Schopps #include <stdio.h>
4531bc9c50Slukem #include <stdlib.h>
4631bc9c50Slukem #include <unistd.h>
4713e967a6Schopps
48*421949a3Ssevan void dump_mode(int);
49*421949a3Ssevan void dump_vm(struct grfvideo_mode *);
50*421949a3Ssevan int get_grf(void);
51*421949a3Ssevan void set_mode(int);
52*421949a3Ssevan void usage(void) __dead;
5313e967a6Schopps
5413e967a6Schopps int
main(int argc,char * argv[])55*421949a3Ssevan main(int argc, char *argv[])
5613e967a6Schopps {
5713e967a6Schopps int m;
5813e967a6Schopps int c;
5913e967a6Schopps
6013e967a6Schopps if (argc == 1) {
6113e967a6Schopps dump_mode(0);
6213e967a6Schopps return (0);
6313e967a6Schopps }
6431bc9c50Slukem while ((c = getopt(argc, argv, "as:")) != -1) {
6513e967a6Schopps switch (c) {
6613e967a6Schopps case 'a':
6713e967a6Schopps if (optind < argc)
6813e967a6Schopps usage();
6913e967a6Schopps dump_mode(-1);
7013e967a6Schopps return (0);
7113e967a6Schopps case 's':
7213e967a6Schopps m = atoi(optarg);
7313e967a6Schopps if (m == 0 || optind < argc)
7413e967a6Schopps usage();
7513e967a6Schopps set_mode(m);
7613e967a6Schopps return (0);
7713e967a6Schopps }
7813e967a6Schopps }
7913e967a6Schopps
8013e967a6Schopps argc -= optind;
8113e967a6Schopps argv += optind;
8213e967a6Schopps if (argc != 1)
8313e967a6Schopps usage();
8413e967a6Schopps
8513e967a6Schopps dump_mode(atoi(*argv));
8613e967a6Schopps return (0);
8713e967a6Schopps }
8813e967a6Schopps
8913e967a6Schopps
9013e967a6Schopps int
get_grf()9113e967a6Schopps get_grf()
9213e967a6Schopps {
9313e967a6Schopps struct stat stb;
9413e967a6Schopps char grfname[80];
9513e967a6Schopps int grffd;
9613e967a6Schopps
9713e967a6Schopps /* find out on which ite/grf we are */
9813e967a6Schopps if (fstat(0, &stb) == -1)
9913e967a6Schopps err(1, "fstat 0");
1006af5bbb0Smycroft if (!S_ISCHR(stb.st_mode) || !isatty(0))
10113e967a6Schopps errx(1, "stdin not a tty");
10213e967a6Schopps if (major(stb.st_rdev) != 13)
10313e967a6Schopps errx(1, "stdin not an ite device");
10438068400She (void)snprintf(grfname, sizeof(grfname), "/dev/grf%u",
10538068400She (u_int)minor(stb.st_rdev) & 0x7);
10613e967a6Schopps if ((grffd = open(grfname, 2)) < 0)
10713e967a6Schopps err(1, "%s", grfname);
10813e967a6Schopps return (grffd);
10913e967a6Schopps }
11013e967a6Schopps
11113e967a6Schopps void
dump_mode(int m)112*421949a3Ssevan dump_mode(int m)
11313e967a6Schopps {
11413e967a6Schopps struct grfvideo_mode vm;
11513e967a6Schopps int num_vm;
11613e967a6Schopps int grffd;
11713e967a6Schopps
11813e967a6Schopps grffd = get_grf();
11913e967a6Schopps
12013e967a6Schopps if (ioctl(grffd, GRFGETNUMVM, &num_vm) < 0)
12113e967a6Schopps err(1, "GRFGETNUMVM");
12213e967a6Schopps if (m > 0 && m > num_vm)
12313e967a6Schopps errx(1, "no such mode");
12413e967a6Schopps if (m <= 0) {
12513e967a6Schopps (void)printf("Current mode:\n");
12613e967a6Schopps vm.mode_num = 0;
12713e967a6Schopps if (ioctl(grffd, GRFGETVMODE, &vm) == 0)
12813e967a6Schopps dump_vm(&vm);
12913e967a6Schopps (void)printf("\n");
13013e967a6Schopps }
131b6d5645aSwiz if (m >= 0) {
132b6d5645aSwiz (void)close(grffd);
13313e967a6Schopps return;
134b6d5645aSwiz }
13513e967a6Schopps for (m = 1; m <= num_vm; m++) {
13613e967a6Schopps vm.mode_num = m;
13713e967a6Schopps if (ioctl(grffd, GRFGETVMODE, &vm) == -1)
13813e967a6Schopps break;
13913e967a6Schopps dump_vm(&vm);
14013e967a6Schopps }
141b6d5645aSwiz (void)close(grffd);
14213e967a6Schopps }
14313e967a6Schopps
14413e967a6Schopps void
set_mode(int m)145*421949a3Ssevan set_mode(int m)
14613e967a6Schopps {
14713e967a6Schopps int grffd;
14813e967a6Schopps
14913e967a6Schopps grffd = get_grf();
15013e967a6Schopps (void)ioctl(grffd, GRFSETVMODE, &m);
151b6d5645aSwiz (void)close(grffd);
15213e967a6Schopps }
15313e967a6Schopps
15413e967a6Schopps void
dump_vm(struct grfvideo_mode * vm)155*421949a3Ssevan dump_vm(struct grfvideo_mode *vm)
15613e967a6Schopps {
15713e967a6Schopps (void)printf("%d: %s\n", vm->mode_num, vm->mode_descr);
15831bc9c50Slukem (void)printf(
15931bc9c50Slukem "pixel_clock = %lu, width = %d, height = %d, depth = %d\n",
16013e967a6Schopps vm->pixel_clock, vm->disp_width, vm->disp_height, vm->depth);
16113e967a6Schopps }
16213e967a6Schopps
16313e967a6Schopps void
usage()16413e967a6Schopps usage()
16513e967a6Schopps {
16613e967a6Schopps (void)fprintf(stderr, "usage: videomode [mode]\n");
16713e967a6Schopps (void)fprintf(stderr, "usage: videomode -a\n");
16813e967a6Schopps (void)fprintf(stderr, "usage: videomode -s mode\n");
16913e967a6Schopps exit(0);
17013e967a6Schopps }
171