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