Home | History | Annotate | Line # | Download | only in iteconfig
iteconfig.c revision 1.4
      1 /*	$NetBSD: iteconfig.c,v 1.4 1995/05/12 21:04:29 leo 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 
     33 #include <sys/types.h>
     34 #include <sys/stat.h>
     35 #include <sys/ioctl.h>
     36 #include <sys/queue.h>
     37 
     38 #if !defined(amiga) && !defined(atari)
     39 #error "This source is not suitable for this architecture!"
     40 #endif
     41 
     42 #if defined(amiga)
     43 #include <amiga/dev/grfabs_reg.h>
     44 #include <amiga/dev/viewioctl.h>
     45 #include <amiga/dev/iteioctl.h>
     46 #endif /* defined(amiga)	*/
     47 
     48 #if defined(atari)
     49 #include <atari/dev/grfabs_reg.h>
     50 #include <atari/dev/viewioctl.h>
     51 #include <atari/dev/iteioctl.h>
     52 #endif /* defined(atari)	*/
     53 
     54 #include <err.h>
     55 #include <errno.h>
     56 #include <fcntl.h>
     57 #include <limits.h>
     58 #include <stdio.h>
     59 #include <stdlib.h>
     60 #include <termios.h>
     61 #include <unistd.h>
     62 
     63 #include "pathnames.h"
     64 
     65 void	printcmap __P((colormap_t *, int));
     66 void	usage __P((void));
     67 void	xioctl __P((int, int, void *));
     68 colormap_t *xgetcmap __P((int, int));
     69 long	xstrtol __P((char *));
     70 int	initialize __P((char *, struct itewinsize *, struct itebell *,
     71 			struct itewinsize *, struct itebell *));
     72 
     73 int
     74 main(argc, argv)
     75 	int argc;
     76 	char **argv;
     77 {
     78 	struct itewinsize is, newis;
     79 	struct itebell ib, newib;
     80 	struct winsize ws;
     81 	colormap_t *cm;
     82 	char *file = _PATH_CONSOLE;
     83 	int ch, fd, i, iflag, max_colors, did_reset;
     84 	long val;
     85 
     86 	iflag = 0;
     87 	did_reset = 0;
     88 
     89 	fd = initialize(_PATH_CONSOLE, &is, &ib, &newis, &newib);
     90 
     91 	while ((ch = getopt(argc, argv, "D:H:P:T:V:W:X:Y:d:f:h:ip:t:v:w:x:y:"))
     92 	    != EOF) {
     93 		switch (ch) {
     94 		case 'D':		/* undocumented backward compat */
     95 		case 'd':
     96 			newis.depth = xstrtol(optarg);
     97 			break;
     98 		case 'f':
     99 			if (did_reset)
    100 				break;
    101 			if (fd != -1)
    102 				close(fd);
    103 			file = optarg;
    104 			fd = initialize(optarg, &is, &ib, &newis, &newib);
    105 			did_reset = optreset = optind = 1;
    106 			break;
    107 		case 'H':		/* undocumented backward compat */
    108 		case 'h':
    109 			newis.height = xstrtol(optarg);
    110 			break;
    111 		case 'i':
    112 			iflag = 1;
    113 			break;
    114 		case 'p':
    115 			newib.pitch = xstrtol(optarg);
    116 			break;
    117 		case 't':
    118 			newib.msec = xstrtol(optarg);
    119 			break;
    120 		case 'V':		/* undocumented backward compat */
    121 		case 'v':
    122 			newib.volume = xstrtol(optarg);
    123 			break;
    124 		case 'W':		/* undocumented backward compat */
    125 		case 'w':
    126 			newis.width = xstrtol(optarg);
    127 			break;
    128 		case 'X':		/* undocumented backward compat */
    129 		case 'x':
    130 			newis.x = xstrtol(optarg);
    131 			break;
    132 		case 'Y':		/* undocumented backward compat */
    133 		case 'y':
    134 			newis.y = xstrtol(optarg);
    135 			break;
    136 		case '?':
    137 		default:
    138 			usage();
    139 			/* NOTREACHED */
    140 		}
    141 	}
    142 	argc -= optind;
    143 	argv += optind;
    144 	if(fd == -1)
    145 		err(1, "open \"%s\"", file);
    146 
    147 	if (memcmp(&newis, &is, sizeof(is))) {
    148 		xioctl(fd, ITEIOCSWINSZ, &newis);
    149 		xioctl(fd, ITEIOCGWINSZ, &is);
    150 	}
    151 	if (memcmp(&newib, &ib, sizeof(ib))) {
    152 		xioctl(fd, ITEIOCSBELL, &newib);
    153 		xioctl(fd, ITEIOCGBELL, &ib);
    154 	}
    155 
    156 	/*
    157 	 * get, set and get colors again
    158 	 */
    159 	i = 0;
    160 	max_colors = 1 << is.depth;
    161 	cm = xgetcmap(fd, max_colors);
    162 	while (argc--) {
    163 		val = xstrtol(*argv++);
    164 		if (i >= max_colors) {
    165 			warnx("warning: too many colors");
    166 			break;
    167 		}
    168 		cm->entry[i] = val;
    169 		i++;
    170 	}
    171 	xioctl(fd, VIOCSCMAP, cm);
    172 	free(cm);
    173 	cm = xgetcmap(fd, max_colors);
    174 
    175 	/* do tty stuff to get it to register the changes. */
    176 	xioctl(fd, TIOCGWINSZ, &ws);
    177 
    178 	if (iflag) {
    179 		printf("tty size: rows %d cols %d\n", ws.ws_row, ws.ws_col);
    180 		printf("ite size: w: %d  h: %d  d: %d  [x: %d  y: %d]\n",
    181 		    is.width, is.height, is.depth, is.x, is.y);
    182 		printf("ite bell: vol: %d  millisec: %d  pitch: %d\n",
    183 		    ib.volume, ib.msec, ib.pitch);
    184 		printcmap(cm, ws.ws_col);
    185 	}
    186 	close(fd);
    187 	exit(0);
    188 }
    189 
    190 void
    191 xioctl(fd, cmd, addr)
    192 	int fd, cmd;
    193 	void *addr;
    194 {
    195 	if (ioctl(fd, cmd, addr) == -1)
    196 		err(1, "ioctl");
    197 }
    198 
    199 long
    200 xstrtol(s)
    201 	char *s;
    202 {
    203 	long rv;
    204 
    205 	rv = strtol(s, NULL, 0);
    206 	if (errno == ERANGE && (rv == LONG_MIN || rv == LONG_MAX))
    207 		err(1, "bad format: \"%s\"", s);
    208 	return(rv);
    209 }
    210 
    211 colormap_t *
    212 xgetcmap(fd, ncolors)
    213 	int fd;
    214 	int ncolors;
    215 {
    216 	colormap_t *cm;
    217 
    218 	cm = malloc(sizeof(colormap_t) + ncolors * sizeof(u_long));
    219 	if (cm == NULL)
    220 		err(1, "malloc");
    221 	cm->first = 0;
    222 	cm->size = ncolors;
    223 	cm->entry = (u_long *) & cm[1];
    224 	xioctl(fd, VIOCGCMAP, cm);
    225 	return(cm);
    226 }
    227 
    228 void
    229 printcmap(cm, ncols)
    230 	colormap_t *cm;
    231 	int ncols;
    232 {
    233 	int i, nel;
    234 
    235 	switch (cm->type) {
    236 	case CM_MONO:
    237 		printf("monochrome");
    238 		return;
    239 	case CM_COLOR:
    240 		printf("color levels: red: %d  green: %d  blue: %d",
    241 		    cm->red_mask + 1, cm->green_mask + 1, cm->blue_mask + 1);
    242 		break;
    243 	case CM_GREYSCALE:
    244 		printf("greyscale levels: %d", cm->grey_mask + 1);
    245 		break;
    246 	}
    247 	printf("\n");
    248 
    249 	nel = ncols / 11 - 1;
    250 	for (i = 0; i < cm->size; i++) {
    251 		printf("0x%08lx ", cm->entry[i]);
    252 		if ((i + 1) % nel == 0)
    253 			printf("\n");
    254 	}
    255 	if ((i + 1) % nel)
    256 		printf("\n");
    257 }
    258 
    259 int
    260 initialize(file, is, ib, newis, newib)
    261 	char	*file;
    262 	struct itewinsize *is, *newis;
    263 	struct itebell *ib, *newib;
    264 {
    265 	int fd;
    266 
    267 	fd = open(file, O_RDONLY | O_NONBLOCK);
    268 	if (fd == -1)
    269 		return(-1);
    270 
    271 	xioctl(fd, ITEIOCGWINSZ, is);
    272 	xioctl(fd, ITEIOCGBELL, ib);
    273 
    274 	memcpy(newis, is, sizeof(*is));
    275 	memcpy(newib, ib, sizeof(*ib));
    276 	return(fd);
    277 }
    278 
    279 void
    280 usage()
    281 {
    282 	fprintf(stderr, "%s\n\t\t%s\n",
    283 	    "usage: iteconfig [-i] [-v volume] [-p period] [-t count]",
    284 	    "[-w width] [-h height] [-d depth] [-x off] [-y off] [color ...]");
    285 	exit(1);
    286 }
    287