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