vidcontrol.c revision 5536
12089Ssos/*-
25536Ssos * Copyright (c) 1994-1995 S�ren Schmidt
32089Ssos * All rights reserved.
42089Ssos *
52089Ssos * Redistribution and use in source and binary forms, with or without
62089Ssos * modification, are permitted provided that the following conditions
72089Ssos * are met:
82089Ssos * 1. Redistributions of source code must retain the above copyright
92089Ssos *    notice, this list of conditions and the following disclaimer.
102089Ssos * 2. Redistributions in binary form must reproduce the above copyright
112089Ssos *    notice, this list of conditions and the following disclaimer in the
122089Ssos *    documentation and/or other materials provided with the distribution.
132089Ssos * 3. The name of the author may not be used to endorse or promote products
142089Ssos *    derived from this software withough specific prior written permission
152089Ssos *
162089Ssos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
172089Ssos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
182089Ssos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
192089Ssos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
202089Ssos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
212089Ssos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
222089Ssos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
232089Ssos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
242089Ssos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
252089Ssos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262089Ssos *
275536Ssos *	$Id: vidcontrol.c,v 1.3 1994/09/26 20:20:44 ache Exp $
282089Ssos */
292089Ssos
302089Ssos#include <ctype.h>
312089Ssos#include <stdio.h>
322089Ssos#include <machine/console.h>
332089Ssos#include <sys/errno.h>
342089Ssos#include "path.h"
352089Ssos
362089Ssos
372089Ssoschar 	legal_colors[16][16] = {
382089Ssos	"black", "blue", "green", "cyan",
392089Ssos	"red", "magenta", "brown", "white",
402089Ssos	"grey", "lightblue", "lightgreen", "lightcyan",
412089Ssos	"lightred", "lightmagenta", "yellow", "lightwhite"
422089Ssos	};
432089Ssosint 	hex = 0;
442089Ssosint 	number, verbose = 0;
452089Ssoschar 	letter;
462089Ssosstruct 	vid_info info;
472089Ssos
482089Ssos
492089Ssoschar *
502089Ssosnextarg(int ac, char **av, int *indp, int oc)
512089Ssos{
522089Ssos	if (*indp < ac)
532089Ssos		return(av[(*indp)++]);
542089Ssos	fprintf(stderr, "%s: option requires two arguments -- %c\n", av[0], oc);
552089Ssos	usage();
562089Ssos	exit(1);
572089Ssos	return("");
582089Ssos}
592089Ssos
602089Ssos
612089Ssoschar *
622089Ssosmkfullname(const char *s1, const char *s2, const char *s3)
632089Ssos{
642089Ssosstatic char	*buf = NULL;
652089Ssosstatic int	bufl = 0;
662089Ssosint		f;
672089Ssos
682089Ssos
692089Ssos	f = strlen(s1) + strlen(s2) + strlen(s3) + 1;
702089Ssos	if (f > bufl)
712089Ssos		if (buf)
722089Ssos			buf = (char *)realloc(buf, f);
732089Ssos		else
742089Ssos			buf = (char *)malloc(f);
752089Ssos	if (!buf) {
762089Ssos		bufl = 0;
772089Ssos		return(NULL);
782089Ssos	}
792089Ssos
802089Ssos	bufl = f;
812089Ssos	strcpy(buf, s1);
822089Ssos	strcat(buf, s2);
832089Ssos	strcat(buf, s3);
842089Ssos	return(buf);
852089Ssos}
862089Ssos
872089Ssos
882089Ssosvoid
892089Ssosload_scrnmap(char *filename)
902089Ssos{
912089Ssos	FILE *fd;
922089Ssos	int i, size;
932089Ssos	char *name;
942089Ssos	scrmap_t scrnmap;
952089Ssos	char *prefix[]  = {"", "", SCRNMAP_PATH, SCRNMAP_PATH, NULL};
962089Ssos	char *postfix[] = {"", ".scm", "", ".scm"};
972089Ssos
982089Ssos	for (i=0; prefix[i]; i++) {
992089Ssos		name = mkfullname(prefix[i], filename, postfix[i]);
1002089Ssos		if (fd = fopen(name, "r"))
1012089Ssos			break;
1022089Ssos	}
1032089Ssos	if (fd == NULL) {
1042089Ssos		perror("screenmap file not found");
1052089Ssos		return;
1062089Ssos	}
1072089Ssos	size = sizeof(scrnmap);
1082089Ssos	if (decode(fd, &scrnmap) != size) {
1092089Ssos		rewind(fd);
1102089Ssos		if (fread(&scrnmap, 1, size, fd) != size) {
1112089Ssos			fprintf(stderr, "bad scrnmap file\n");
1122089Ssos			close(fd);
1132089Ssos			return;
1142089Ssos		}
1152089Ssos	}
1162089Ssos	if (ioctl(0, PIO_SCRNMAP, &scrnmap) < 0)
1172089Ssos		perror("can't load screenmap");
1182089Ssos	close(fd);
1192089Ssos}
1202089Ssos
1212089Ssos
1222089Ssosvoid
1232089Ssosload_default_scrnmap()
1242089Ssos{
1252089Ssos	int i;
1262089Ssos	scrmap_t scrnmap;
1272089Ssos
1282089Ssos	for (i=0; i<256; i++)
1292089Ssos		*((char*)&scrnmap + i) = i;
1302089Ssos	if (ioctl(0, PIO_SCRNMAP, &scrnmap) < 0)
1312089Ssos		perror("can't load default screenmap");
1322089Ssos}
1332089Ssos
1342089Ssos
1352089Ssosvoid
1362089Ssosprint_scrnmap()
1372089Ssos{
1382089Ssos	unsigned char map[256];
1392089Ssos	int i;
1402089Ssos
1412089Ssos	if (ioctl(0, GIO_SCRNMAP, &map) < 0) {
1422089Ssos		perror("getting scrnmap");
1432089Ssos		return;
1442089Ssos	}
1452089Ssos	for (i=0; i<sizeof(map); i++) {
1462089Ssos		if (i > 0 && i % 16 == 0)
1472089Ssos			fprintf(stdout, "\n");
1482089Ssos		if (hex)
1492089Ssos			fprintf(stdout, " %02x", map[i]);
1502089Ssos		else
1512089Ssos			fprintf(stdout, " %03d", map[i]);
1522089Ssos	}
1532089Ssos	fprintf(stdout, "\n");
1542089Ssos
1552089Ssos}
1562089Ssos
1572089Ssos
1582089Ssosvoid
1592089Ssosload_font(char *type, char *filename)
1602089Ssos{
1612089Ssos	FILE	*fd;
1622089Ssos	int	i, io, size;
1632089Ssos	char	*name, *fontmap;
1642089Ssos	char	*prefix[]  = {"", "", FONT_PATH, FONT_PATH, NULL};
1652089Ssos	char	*postfix[] = {"", ".fnt", "", ".fnt"};
1662089Ssos
1672089Ssos	for (i=0; prefix[i]; i++) {
1682089Ssos		name = mkfullname(prefix[i], filename, postfix[i]);
1692089Ssos		if (fd = fopen(name, "r"))
1702089Ssos			break;
1712089Ssos	}
1722089Ssos	if (fd == NULL) {
1732089Ssos		perror("font file not found");
1742089Ssos		return;
1752089Ssos	}
1762089Ssos	if (!strcmp(type, "8x8")) {
1772089Ssos		size = 8*256;
1782089Ssos		io = PIO_FONT8x8;
1792089Ssos	}
1802089Ssos	else if (!strcmp(type, "8x14")) {
1812089Ssos		size = 14*256;
1822089Ssos		io = PIO_FONT8x14;
1832089Ssos	}
1842089Ssos	else if (!strcmp(type, "8x16")) {
1852089Ssos		size = 16*256;
1862089Ssos		io = PIO_FONT8x16;
1872089Ssos	}
1882089Ssos	else {
1892089Ssos		perror("bad font size specification");
1902089Ssos		close(fd);
1912089Ssos		return;
1922089Ssos	}
1932089Ssos	fontmap = (char*) malloc(size);
1942089Ssos	if (decode(fd, fontmap) != size) {
1952089Ssos		rewind(fd);
1962089Ssos		if (fread(fontmap, 1, size, fd) != size) {
1972089Ssos			fprintf(stderr, "bad font file\n");
1982089Ssos			close(fd);
1992089Ssos			free(fontmap);
2002089Ssos			return;
2012089Ssos		}
2022089Ssos	}
2032089Ssos	if (ioctl(0, io, fontmap) < 0)
2042089Ssos		perror("can't load font");
2052089Ssos	close(fd);
2062089Ssos	free(fontmap);
2072089Ssos}
2082089Ssos
2092089Ssos
2102089Ssosvoid
2112089Ssosset_screensaver_timeout(char *arg)
2122089Ssos{
2132089Ssos	int nsec;
2142089Ssos
2152089Ssos	if (!strcmp(arg, "off"))
2162089Ssos		nsec = 0;
2172089Ssos	else {
2182089Ssos		nsec = atoi(arg);
2192089Ssos		if ((*arg == '\0') || (nsec < 1)) {
2202089Ssos			fprintf(stderr, "argument must be a positive number\n");
2212089Ssos			return;
2222089Ssos		}
2232089Ssos	}
2242089Ssos	if (ioctl(0, CONS_BLANKTIME, &nsec) == -1)
2252089Ssos		perror("setting screensaver period");
2262089Ssos}
2272089Ssos
2282089Ssos
2292089Ssosvoid
2302089Ssosset_screensaver_type(char *type)
2312089Ssos{
2322089Ssos	ssaver_t saver;
2332089Ssos	int i, e;
2342089Ssos
2352089Ssos	if (!strcmp(type, "help")) {
2362089Ssos		i = 0;
2372089Ssos		printf("available screen saver types:\n");
2382089Ssos		do {
2392089Ssos			saver.num = i;
2402089Ssos			e = ioctl(0, CONS_GSAVER, &saver);
2412089Ssos			i ++;
2422089Ssos			if (e == 0)
2432089Ssos				printf("\t%s\n", saver.name);
2442089Ssos		} while (e == 0);
2452089Ssos		if (e == -1 && errno != EIO)
2462089Ssos			perror("getting screensaver info");
2472089Ssos	} else {
2482089Ssos		i = 0;
2492089Ssos		do {
2502089Ssos			saver.num = i;
2512089Ssos			e = ioctl(0, CONS_GSAVER, &saver);
2522089Ssos			i ++;
2532089Ssos			if (e == 0 && !strcmp(type, saver.name)) {
2542089Ssos				if (ioctl(0, CONS_SSAVER, &saver) == -1)
2552089Ssos					perror("setting screensaver type");
2562089Ssos				return;
2572089Ssos			}
2582089Ssos		} while (e == 0);
2592089Ssos		if (e == -1 && errno != EIO)
2602089Ssos			perror("getting screensaver info");
2612089Ssos		else
2622089Ssos			fprintf(stderr, "%s: No such screensaver\n", type);
2632089Ssos	}
2642089Ssos}
2652089Ssos
2662089Ssosvoid
2675536Ssosset_cursor_type(char *appearence)
2682089Ssos{
2695536Ssos	int type;
2702089Ssos
2715536Ssos	if (!strcmp(appearence, "blink"))
2725536Ssos		type = 1;
2735536Ssos	else if (!strcmp(appearence, "noblink"))
2745536Ssos		type = 0;
2755536Ssos	else {
2765536Ssos		fprintf(stderr, "argument to -c must be blink or noblink\n");
2772089Ssos		return;
2782089Ssos	}
2795536Ssos	ioctl(0, CONS_CURSORTYPE, &type);
2802089Ssos}
2812089Ssos
2822089Ssos
2832089Ssosint
2842089Ssosvideo_mode(int argc, char **argv, int *index)
2852089Ssos{
2862089Ssos	int mode;
2872089Ssos
2882089Ssos	if (*index < argc) {
2892784Ssos		if (!strcmp(argv[*index], "VGA_40x25"))
2902784Ssos			mode = SW_VGA_C40x25;
2912784Ssos		else if (!strcmp(argv[*index], "VGA_80x25"))
2922784Ssos			mode = SW_VGA_C80x25;
2935536Ssos		else if (!strcmp(argv[*index], "VGA_80x30"))
2945536Ssos			mode = SW_VGA_C80x30;
2952784Ssos		else if (!strcmp(argv[*index], "VGA_80x50"))
2962784Ssos			mode = SW_VGA_C80x50;
2975536Ssos		else if (!strcmp(argv[*index], "VGA_80x60"))
2985536Ssos			mode = SW_VGA_C80x60;
2992784Ssos		else if (!strcmp(argv[*index], "VGA_320x200"))
3002784Ssos			mode = SW_VGA_CG320;
3012784Ssos		else if (!strcmp(argv[*index], "EGA_80x25"))
3022784Ssos			mode = SW_ENH_C80x25;
3032784Ssos		else if (!strcmp(argv[*index], "EGA_80x43"))
3042784Ssos			mode = SW_ENH_C80x43;
3052089Ssos		else
3062089Ssos			return;
3072089Ssos		if (ioctl(0, mode, NULL) < 0)
3082089Ssos			perror("Cannot set videomode");
3092089Ssos		(*index)++;
3102089Ssos	}
3112089Ssos	return;
3122089Ssos}
3132089Ssos
3142089Ssos
3152089Ssosint
3162089Ssosget_color_number(char *color)
3172089Ssos{
3182089Ssos	int i;
3192089Ssos
3202089Ssos	for (i=0; i<16; i++)
3212089Ssos		if (!strcmp(color, legal_colors[i]))
3222089Ssos			return i;
3232089Ssos	return -1;
3242089Ssos}
3252089Ssos
3262089Ssos
3272089Ssosint
3282089Ssosset_normal_colors(int argc, char **argv, int *index)
3292089Ssos{
3302089Ssos	int color;
3312089Ssos
3322089Ssos	if (*index < argc && (color = get_color_number(argv[*index])) != -1) {
3332089Ssos		(*index)++;
3342089Ssos		fprintf(stderr, "[=%dF", color);
3352089Ssos		if (*index < argc
3362089Ssos		    && (color = get_color_number(argv[*index])) != -1
3372089Ssos		    && color < 8) {
3382089Ssos			(*index)++;
3392089Ssos			fprintf(stderr, "[=%dG", color);
3402089Ssos		}
3412089Ssos	}
3422089Ssos}
3432089Ssos
3442089Ssos
3452089Ssosset_reverse_colors(int argc, char **argv, int *index)
3462089Ssos{
3472089Ssos	int color;
3482089Ssos
3492089Ssos	if ((color = get_color_number(argv[*(index)-1])) != -1) {
3502089Ssos		fprintf(stderr, "[=%dH", color);
3512089Ssos		if (*index < argc
3522089Ssos		    && (color = get_color_number(argv[*index])) != -1
3532089Ssos		    && color < 8) {
3542089Ssos			(*index)++;
3552089Ssos			fprintf(stderr, "[=%dI", color);
3562089Ssos		}
3572089Ssos	}
3582089Ssos}
3592089Ssos
3602089Ssos
3612089Ssosset_border_color(char *arg)
3622089Ssos{
3632089Ssos	int color;
3642089Ssos
3652089Ssos	if ((color = get_color_number(arg)) != -1) {
3662089Ssos		fprintf(stderr, "[=%dA", color);
3672089Ssos	}
3682089Ssos	else
3692089Ssos		usage();
3702089Ssos}
3712089Ssos
3722089Ssos
3732089Ssostest_frame()
3742089Ssos{
3752089Ssos	int i;
3762089Ssos
3772089Ssos	fprintf(stdout, "[=0G\n\n");
3782089Ssos	for (i=0; i<8; i++) {
3792089Ssos		fprintf(stdout, "[=15F[=0G        %2d [=%dF%-16s"
3802089Ssos				"[=15F[=0G        %2d [=%dF%-16s        "
3812089Ssos				"[=15F %2d [=%dGBACKGROUND[=0G\n",
3822089Ssos			i, i, legal_colors[i], i+8, i+8,
3832089Ssos			legal_colors[i+8], i, i);
3842089Ssos	}
3852089Ssos	fprintf(stdout, "[=%dF[=%dG[=%dH[=%dI\n",
3862089Ssos		info.mv_norm.fore, info.mv_norm.back,
3872089Ssos		info.mv_rev.fore, info.mv_rev.back);
3882089Ssos}
3892089Ssos
3902089Ssos
3912089Ssosusage()
3922089Ssos{
3932089Ssos	fprintf(stderr,
3942784Ssos"Usage: vidcontrol mode             (available modes: VGA_40x25, VGA_80x25,\n"
3952784Ssos"                                                     VGA_80x50, VGA_320x200,\n"
3962784Ssos"                                                     EGA_80x25, EGA_80x43)\n"
3975536Ssos"                  show               (show available colors)\n"
3985536Ssos"                  fgcol bgcol        (set fore- & background colors)\n"
3995536Ssos"                  -r fgcol bgcol     (set reverse fore- & background colors)\n"
4005536Ssos"                  -b color           (set border color)\n"
4015536Ssos"                  -c blink | noblink (set cursor type)\n"
4025536Ssos"                  -d                 (dump screenmap to stdout)\n"
4035536Ssos"                  -l filename        (load srceenmap file filename)\n"
4045536Ssos"                  -L                 (load default screenmap)\n"
4055536Ssos"                  -f DxL filename    (load font, D dots wide & L lines high)\n"
4065536Ssos"                  -s saver | help    (set screensaver type or help for a list)\n"
4075536Ssos"                  -t N               (set screensaver timeout in seconds)\n"
4085536Ssos"                  -x                 (use hex numbers for output)\n"
4092089Ssos	);
4102089Ssos}
4112089Ssos
4122089Ssos
4132089Ssosvoid
4142089Ssosmain(int argc, char **argv)
4152089Ssos{
4162089Ssos	extern char	*optarg;
4172089Ssos	extern int	optind;
4182089Ssos	int		opt;
4192089Ssos
4202089Ssos
4212089Ssos	info.size = sizeof(info);
4222089Ssos	if (ioctl(0, CONS_GETINFO, &info) < 0) {
4235536Ssos		perror("Must be on a vrtual console");
4242089Ssos		exit(1);
4252089Ssos	}
4262089Ssos	while((opt = getopt(argc, argv, "b:c:df:l:Lr:s:t:vx")) != -1)
4272089Ssos		switch(opt) {
4282089Ssos			case 'c':
4295536Ssos				set_cursor_type(optarg);
4302089Ssos				break;
4312089Ssos			case 'b':
4322089Ssos				set_border_color(optarg);
4332089Ssos				break;
4342089Ssos			case 'd':
4352089Ssos				print_scrnmap();
4362089Ssos				break;
4372089Ssos			case 'f':
4382089Ssos				load_font(optarg,
4392089Ssos					nextarg(argc, argv, &optind, 'f'));
4402089Ssos				break;
4412089Ssos			case 'l':
4422089Ssos				load_scrnmap(optarg);
4432089Ssos				break;
4442089Ssos			case 'L':
4452089Ssos				load_default_scrnmap();
4462089Ssos				break;
4472089Ssos			case 'r':
4482089Ssos				set_reverse_colors(argc, argv, &optind);
4492089Ssos				break;
4502089Ssos			case 's':
4512089Ssos				set_screensaver_type(optarg);
4522089Ssos				break;
4532089Ssos			case 't':
4542089Ssos				set_screensaver_timeout(optarg);
4552089Ssos				break;
4562089Ssos			case 'v':
4572089Ssos				verbose = 1;
4582089Ssos				break;
4592089Ssos			case 'x':
4602089Ssos				hex = 1;
4612089Ssos				break;
4622089Ssos			default:
4632089Ssos				usage();
4642089Ssos				exit(1);
4652089Ssos		}
4662089Ssos	if (video_mode(argc, argv, &optind)) ;
4672089Ssos	if (set_normal_colors(argc, argv, &optind)) ;
4682089Ssos	if (optind < argc && !strcmp(argv[optind], "show")) {
4692089Ssos		test_frame();
4702089Ssos		optind++;
4712089Ssos	}
4722089Ssos	if ((optind != argc) || (argc == 1)) {
4732089Ssos		usage();
4742089Ssos		exit(1);
4752089Ssos	}
4762089Ssos	exit(0);
4772089Ssos}
4782089Ssos
479