Home | History | Annotate | Line # | Download | only in iteconfig
iteconfig.c revision 1.7
      1 /*	$NetBSD: iteconfig.c,v 1.7 2003/04/17 02:41:21 lukem Exp $	*/
      2 /*
      3  * Copyright (c) 1994 Christian E. Hopps
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *      This product includes software developed by Christian E. Hopps
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  *
     31  */
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __RCSID("$NetBSD: iteconfig.c,v 1.7 2003/04/17 02:41:21 lukem Exp $");
     35 #endif
     36 
     37 #include <sys/types.h>
     38 #include <sys/stat.h>
     39 #include <sys/ioctl.h>
     40 #include <sys/queue.h>
     41 
     42 #if !defined(amiga) && !defined(atari)
     43 #error "This source is not suitable for this architecture!"
     44 #endif
     45 
     46 #if defined(amiga)
     47 #include <amiga/dev/grfabs_reg.h>
     48 #include <amiga/dev/viewioctl.h>
     49 #include <amiga/dev/iteioctl.h>
     50 #endif /* defined(amiga)	*/
     51 
     52 #if defined(atari)
     53 #include <atari/dev/grfabs_reg.h>
     54 #include <atari/dev/viewioctl.h>
     55 #include <atari/dev/iteioctl.h>
     56 #endif /* defined(atari)	*/
     57 
     58 #include <err.h>
     59 #include <errno.h>
     60 #include <fcntl.h>
     61 #include <limits.h>
     62 #include <stdio.h>
     63 #include <stdlib.h>
     64 #include <termios.h>
     65 #include <unistd.h>
     66 
     67 #include "pathnames.h"
     68 
     69 int	initialize __P((char *, struct itewinsize *, struct itebell *,
     70 			struct itewinsize *, struct itebell *));
     71 int	main __P((int, char **));
     72 void	printcmap __P((colormap_t *, int));
     73 void	xioctl __P((int, int, void *));
     74 colormap_t *xgetcmap __P((int, int));
     75 long	xstrtol __P((char *));
     76 void	usage __P((void));
     77 
     78 int
     79 main(argc, argv)
     80 	int argc;
     81 	char **argv;
     82 {
     83 	struct itewinsize is, newis;
     84 	struct itebell ib, newib;
     85 	struct winsize ws;
     86 	colormap_t *cm;
     87 	char *file = _PATH_CONSOLE;
     88 	int ch, fd, i, iflag, max_colors, did_reset;
     89 	long val;
     90 
     91 	iflag = 0;
     92 	did_reset = 0;
     93 
     94 	fd = initialize(_PATH_CONSOLE, &is, &ib, &newis, &newib);
     95 
     96 	while ((ch = getopt(argc, argv, "D:H:P:T:V:W:X:Y:d:f:h:ip:t:v:w:x:y:"))
     97 	    != -1) {
     98 		switch (ch) {
     99 		case 'D':		/* undocumented backward compat */
    100 		case 'd':
    101 			newis.depth = xstrtol(optarg);
    102 			break;
    103 		case 'f':
    104 			if (did_reset)
    105 				break;
    106 			if (fd != -1)
    107 				close(fd);
    108 			file = optarg;
    109 			fd = initialize(optarg, &is, &ib, &newis, &newib);
    110 			did_reset = optreset = optind = 1;
    111 			break;
    112 		case 'H':		/* undocumented backward compat */
    113 		case 'h':
    114 			newis.height = xstrtol(optarg);
    115 			break;
    116 		case 'i':
    117 			iflag = 1;
    118 			break;
    119 		case 'p':
    120 			newib.pitch = xstrtol(optarg);
    121 			break;
    122 		case 't':
    123 			newib.msec = xstrtol(optarg);
    124 			break;
    125 		case 'V':		/* undocumented backward compat */
    126 		case 'v':
    127 			newib.volume = xstrtol(optarg);
    128 			break;
    129 		case 'W':		/* undocumented backward compat */
    130 		case 'w':
    131 			newis.width = xstrtol(optarg);
    132 			break;
    133 		case 'X':		/* undocumented backward compat */
    134 		case 'x':
    135 			newis.x = xstrtol(optarg);
    136 			break;
    137 		case 'Y':		/* undocumented backward compat */
    138 		case 'y':
    139 			newis.y = xstrtol(optarg);
    140 			break;
    141 		case '?':
    142 		default:
    143 			usage();
    144 			/* NOTREACHED */
    145 		}
    146 	}
    147 	argc -= optind;
    148 	argv += optind;
    149 	if(fd == -1)
    150 		err(1, "open \"%s\"", file);
    151 
    152 	if (memcmp(&newis, &is, sizeof(is))) {
    153 		xioctl(fd, ITEIOCSWINSZ, &newis);
    154 		xioctl(fd, ITEIOCGWINSZ, &is);
    155 	}
    156 	if (memcmp(&newib, &ib, sizeof(ib))) {
    157 		xioctl(fd, ITEIOCSBELL, &newib);
    158 		xioctl(fd, ITEIOCGBELL, &ib);
    159 	}
    160 
    161 	/*
    162 	 * get, set and get colors again
    163 	 */
    164 	i = 0;
    165 	max_colors = 1 << is.depth;
    166 	cm = xgetcmap(fd, max_colors);
    167 	while (argc--) {
    168 		val = xstrtol(*argv++);
    169 		if (i >= max_colors) {
    170 			warnx("warning: too many colors");
    171 			break;
    172 		}
    173 		cm->entry[i] = val;
    174 		i++;
    175 	}
    176 	xioctl(fd, VIOCSCMAP, cm);
    177 	free(cm);
    178 	cm = xgetcmap(fd, max_colors);
    179 
    180 	/* do tty stuff to get it to register the changes. */
    181 	xioctl(fd, TIOCGWINSZ, &ws);
    182 
    183 	if (iflag) {
    184 		printf("tty size: rows %d cols %d\n", ws.ws_row, ws.ws_col);
    185 		printf("ite size: w: %d  h: %d  d: %d  [x: %d  y: %d]\n",
    186 		    is.width, is.height, is.depth, is.x, is.y);
    187 		printf("ite bell: vol: %d  millisec: %d  pitch: %d\n",
    188 		    ib.volume, ib.msec, ib.pitch);
    189 		printcmap(cm, ws.ws_col);
    190 	}
    191 	close(fd);
    192 	exit(0);
    193 }
    194 
    195 void
    196 xioctl(fd, cmd, addr)
    197 	int fd, cmd;
    198 	void *addr;
    199 {
    200 	if (ioctl(fd, cmd, addr) == -1)
    201 		err(1, "ioctl");
    202 }
    203 
    204 long
    205 xstrtol(s)
    206 	char *s;
    207 {
    208 	long rv;
    209 
    210 	errno = 0;
    211 	rv = strtol(s, NULL, 0);
    212 	if (errno == ERANGE && (rv == LONG_MIN || rv == LONG_MAX))
    213 		err(1, "bad format: \"%s\"", s);
    214 	return(rv);
    215 }
    216 
    217 colormap_t *
    218 xgetcmap(fd, ncolors)
    219 	int fd;
    220 	int ncolors;
    221 {
    222 	colormap_t *cm;
    223 
    224 	cm = malloc(sizeof(colormap_t) + ncolors * sizeof(u_long));
    225 	if (cm == NULL)
    226 		err(1, "malloc");
    227 	cm->first = 0;
    228 	cm->size = ncolors;
    229 	cm->entry = (u_long *) & cm[1];
    230 	xioctl(fd, VIOCGCMAP, cm);
    231 	return(cm);
    232 }
    233 
    234 void
    235 printcmap(cm, ncols)
    236 	colormap_t *cm;
    237 	int ncols;
    238 {
    239 	int i, nel;
    240 
    241 	switch (cm->type) {
    242 	case CM_MONO:
    243 		printf("monochrome");
    244 		return;
    245 	case CM_COLOR:
    246 		printf("color levels: red: %d  green: %d  blue: %d",
    247 		    cm->red_mask + 1, cm->green_mask + 1, cm->blue_mask + 1);
    248 		break;
    249 	case CM_GREYSCALE:
    250 		printf("greyscale levels: %d", cm->grey_mask + 1);
    251 		break;
    252 	}
    253 	printf("\n");
    254 
    255 	nel = ncols / 11 - 1;
    256 	for (i = 0; i < cm->size; i++) {
    257 		printf("0x%08lx ", cm->entry[i]);
    258 		if ((i + 1) % nel == 0)
    259 			printf("\n");
    260 	}
    261 	if ((i + 1) % nel)
    262 		printf("\n");
    263 }
    264 
    265 int
    266 initialize(file, is, ib, newis, newib)
    267 	char	*file;
    268 	struct itewinsize *is, *newis;
    269 	struct itebell *ib, *newib;
    270 {
    271 	int fd;
    272 
    273 	fd = open(file, O_RDONLY | O_NONBLOCK);
    274 	if (fd == -1)
    275 		return(-1);
    276 
    277 	xioctl(fd, ITEIOCGWINSZ, is);
    278 	xioctl(fd, ITEIOCGBELL, ib);
    279 
    280 	memcpy(newis, is, sizeof(*is));
    281 	memcpy(newib, ib, sizeof(*ib));
    282 	return(fd);
    283 }
    284 
    285 void
    286 usage()
    287 {
    288 	fprintf(stderr, "%s\n\t\t%s\n\t\t%s\n",
    289 	    "usage: iteconfig [-i] [-f file] [-v volume] [-p pitch] [-t msec]",
    290 	    "[-w width] [-h height] [-d depth] [-x off] [-y off]",
    291 	    "[color ...]");
    292 	exit(1);
    293 }
    294