Home | History | Annotate | Line # | Download | only in videomode
      1 /*	$NetBSD: videomode.c,v 1.9 2018/01/23 21:06:26 sevan 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 <err.h>
     42 #include <errno.h>
     43 #include <fcntl.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <unistd.h>
     47 
     48 void	dump_mode(int);
     49 void	dump_vm(struct grfvideo_mode *);
     50 int	get_grf(void);
     51 void	set_mode(int);
     52 void	usage(void) __dead;
     53 
     54 int
     55 main(int argc, 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:")) != -1) {
     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 (!S_ISCHR(stb.st_mode) || !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)snprintf(grfname, sizeof(grfname), "/dev/grf%u",
    105 	    (u_int)minor(stb.st_rdev) & 0x7);
    106 	if ((grffd = open(grfname, 2)) < 0)
    107 		err(1, "%s", grfname);
    108 	return (grffd);
    109 }
    110 
    111 void
    112 dump_mode(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 		(void)close(grffd);
    133 		return;
    134 	}
    135 	for (m = 1; m <= num_vm; m++) {
    136 		vm.mode_num = m;
    137 		if (ioctl(grffd, GRFGETVMODE, &vm) == -1)
    138 			break;
    139 		dump_vm(&vm);
    140 	}
    141 	(void)close(grffd);
    142 }
    143 
    144 void
    145 set_mode(int m)
    146 {
    147 	int grffd;
    148 
    149 	grffd = get_grf();
    150 	(void)ioctl(grffd, GRFSETVMODE, &m);
    151 	(void)close(grffd);
    152 }
    153 
    154 void
    155 dump_vm(struct grfvideo_mode *vm)
    156 {
    157 	(void)printf("%d: %s\n", vm->mode_num, vm->mode_descr);
    158 	(void)printf(
    159 	    "pixel_clock = %lu, width = %d, height = %d, depth = %d\n",
    160 	    vm->pixel_clock, vm->disp_width, vm->disp_height, vm->depth);
    161 }
    162 
    163 void
    164 usage()
    165 {
    166 	(void)fprintf(stderr, "usage: videomode [mode]\n");
    167 	(void)fprintf(stderr, "usage: videomode -a\n");
    168 	(void)fprintf(stderr, "usage: videomode -s mode\n");
    169 	exit(0);
    170 }
    171