vidcontrol.c revision 30764
12089Ssos/*-
216565Ssos * Copyright (c) 1994-1996 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
95994Ssos *    notice, this list of conditions and the following disclaimer,
105994Ssos *    in this position and unchanged.
112089Ssos * 2. Redistributions in binary form must reproduce the above copyright
122089Ssos *    notice, this list of conditions and the following disclaimer in the
132089Ssos *    documentation and/or other materials provided with the distribution.
142089Ssos * 3. The name of the author may not be used to endorse or promote products
152089Ssos *    derived from this software withough specific prior written permission
162089Ssos *
172089Ssos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
182089Ssos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
192089Ssos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
202089Ssos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
212089Ssos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
222089Ssos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232089Ssos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242089Ssos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252089Ssos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
262089Ssos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272089Ssos */
282089Ssos
2930764Scharnier#ifndef lint
3030764Scharnierstatic const char rcsid[] =
3130764Scharnier	"$Id$";
3230764Scharnier#endif /* not lint */
3330764Scharnier
342089Ssos#include <ctype.h>
3530764Scharnier#include <err.h>
362089Ssos#include <stdio.h>
3723457Sbrian#include <stdlib.h>
3830764Scharnier#include <string.h>
3923702Speter#include <unistd.h>
402089Ssos#include <machine/console.h>
412089Ssos#include <sys/errno.h>
422089Ssos#include "path.h"
4323457Sbrian#include "decode.h"
442089Ssos
452089Ssoschar 	legal_colors[16][16] = {
462089Ssos	"black", "blue", "green", "cyan",
472089Ssos	"red", "magenta", "brown", "white",
482089Ssos	"grey", "lightblue", "lightgreen", "lightcyan",
492089Ssos	"lightred", "lightmagenta", "yellow", "lightwhite"
506628Ssos};
512089Ssosint 	hex = 0;
526047Ssosint 	number;
532089Ssoschar 	letter;
542089Ssosstruct 	vid_info info;
552089Ssos
562089Ssos
5730764Scharnierstatic void
586628Ssosusage()
596628Ssos{
6030764Scharnier	fprintf(stderr, "%s\n%s\n%s\n",
6130764Scharnier"usage: vidcontrol [-r fg bg] [-b color] [-c appearance] [-d] [-l scrmap]",
6230764Scharnier"                  [-L] [-m on|off] [-f size file] [-s number] [-t N|off]",
6330764Scharnier"                  [-x] [mode] [fgcol [bgcol]] [show]");
6430764Scharnier	exit(1);
656628Ssos}
666628Ssos
672089Ssoschar *
682089Ssosnextarg(int ac, char **av, int *indp, int oc)
692089Ssos{
702089Ssos	if (*indp < ac)
712089Ssos		return(av[(*indp)++]);
7230764Scharnier	errx(1, "option requires two arguments -- %c", oc);
732089Ssos	return("");
742089Ssos}
752089Ssos
762089Ssoschar *
772089Ssosmkfullname(const char *s1, const char *s2, const char *s3)
782089Ssos{
796628Ssos	static char *buf = NULL;
806628Ssos	static int bufl = 0;
816628Ssos	int f;
822089Ssos
832089Ssos	f = strlen(s1) + strlen(s2) + strlen(s3) + 1;
842089Ssos	if (f > bufl)
852089Ssos		if (buf)
862089Ssos			buf = (char *)realloc(buf, f);
872089Ssos		else
882089Ssos			buf = (char *)malloc(f);
892089Ssos	if (!buf) {
902089Ssos		bufl = 0;
912089Ssos		return(NULL);
922089Ssos	}
932089Ssos
942089Ssos	bufl = f;
952089Ssos	strcpy(buf, s1);
962089Ssos	strcat(buf, s2);
972089Ssos	strcat(buf, s3);
982089Ssos	return(buf);
992089Ssos}
1002089Ssos
1012089Ssosvoid
1022089Ssosload_scrnmap(char *filename)
1032089Ssos{
10423457Sbrian	FILE *fd = 0;
1052089Ssos	int i, size;
1062089Ssos	char *name;
1072089Ssos	scrmap_t scrnmap;
1082089Ssos	char *prefix[]  = {"", "", SCRNMAP_PATH, SCRNMAP_PATH, NULL};
1092089Ssos	char *postfix[] = {"", ".scm", "", ".scm"};
1102089Ssos
1112089Ssos	for (i=0; prefix[i]; i++) {
1122089Ssos		name = mkfullname(prefix[i], filename, postfix[i]);
11323457Sbrian		fd = fopen(name, "r");
11423457Sbrian		if (fd)
1152089Ssos			break;
1162089Ssos	}
1172089Ssos	if (fd == NULL) {
11830764Scharnier		warn("screenmap file not found");
1192089Ssos		return;
1202089Ssos	}
1212089Ssos	size = sizeof(scrnmap);
12223457Sbrian	if (decode(fd, (char *)&scrnmap) != size) {
1232089Ssos		rewind(fd);
1242089Ssos		if (fread(&scrnmap, 1, size, fd) != size) {
12530764Scharnier			warnx("bad screenmap file");
12623457Sbrian			fclose(fd);
1272089Ssos			return;
1282089Ssos		}
1292089Ssos	}
1302089Ssos	if (ioctl(0, PIO_SCRNMAP, &scrnmap) < 0)
13130764Scharnier		warn("can't load screenmap");
13223457Sbrian	fclose(fd);
1332089Ssos}
1342089Ssos
1352089Ssosvoid
1362089Ssosload_default_scrnmap()
1372089Ssos{
1386628Ssos	scrmap_t scrnmap;
1392089Ssos	int i;
1402089Ssos
1412089Ssos	for (i=0; i<256; i++)
1422089Ssos		*((char*)&scrnmap + i) = i;
1432089Ssos	if (ioctl(0, PIO_SCRNMAP, &scrnmap) < 0)
14430764Scharnier		warn("can't load default screenmap");
1452089Ssos}
1462089Ssos
1472089Ssosvoid
1482089Ssosprint_scrnmap()
1492089Ssos{
1502089Ssos	unsigned char map[256];
1512089Ssos	int i;
1522089Ssos
1532089Ssos	if (ioctl(0, GIO_SCRNMAP, &map) < 0) {
15430764Scharnier		warn("getting screenmap");
1552089Ssos		return;
1562089Ssos	}
1572089Ssos	for (i=0; i<sizeof(map); i++) {
1582089Ssos		if (i > 0 && i % 16 == 0)
1592089Ssos			fprintf(stdout, "\n");
1602089Ssos		if (hex)
1618857Srgrimes			fprintf(stdout, " %02x", map[i]);
1622089Ssos		else
1632089Ssos			fprintf(stdout, " %03d", map[i]);
1642089Ssos	}
1652089Ssos	fprintf(stdout, "\n");
1662089Ssos
1672089Ssos}
1682089Ssos
1698857Srgrimesvoid
1702089Ssosload_font(char *type, char *filename)
1712089Ssos{
17223457Sbrian	FILE	*fd = 0;
1732089Ssos	int	i, io, size;
1742089Ssos	char	*name, *fontmap;
1752089Ssos	char	*prefix[]  = {"", "", FONT_PATH, FONT_PATH, NULL};
1762089Ssos	char	*postfix[] = {"", ".fnt", "", ".fnt"};
1772089Ssos
1782089Ssos	for (i=0; prefix[i]; i++) {
1792089Ssos		name = mkfullname(prefix[i], filename, postfix[i]);
18023457Sbrian		fd = fopen(name, "r");
18123457Sbrian		if (fd)
1822089Ssos			break;
1832089Ssos	}
1842089Ssos	if (fd == NULL) {
18530764Scharnier		warn("font file not found");
1862089Ssos		return;
1872089Ssos	}
1882089Ssos	if (!strcmp(type, "8x8")) {
1892089Ssos		size = 8*256;
1902089Ssos		io = PIO_FONT8x8;
1912089Ssos	}
1922089Ssos	else if (!strcmp(type, "8x14")) {
1932089Ssos		size = 14*256;
1942089Ssos		io = PIO_FONT8x14;
1952089Ssos	}
1962089Ssos	else if (!strcmp(type, "8x16")) {
1972089Ssos		size = 16*256;
1982089Ssos		io = PIO_FONT8x16;
1992089Ssos	}
2002089Ssos	else {
20130764Scharnier		warn("bad font size specification");
20223457Sbrian		fclose(fd);
2032089Ssos		return;
2042089Ssos	}
2052089Ssos	fontmap = (char*) malloc(size);
2062089Ssos	if (decode(fd, fontmap) != size) {
2072089Ssos		rewind(fd);
2082089Ssos		if (fread(fontmap, 1, size, fd) != size) {
20930764Scharnier			warnx("bad font file");
21023457Sbrian			fclose(fd);
2112089Ssos			free(fontmap);
2122089Ssos			return;
2132089Ssos		}
2142089Ssos	}
2152089Ssos	if (ioctl(0, io, fontmap) < 0)
21630764Scharnier		warn("can't load font");
21723457Sbrian	fclose(fd);
2182089Ssos	free(fontmap);
2192089Ssos}
2202089Ssos
2212089Ssosvoid
2222089Ssosset_screensaver_timeout(char *arg)
2232089Ssos{
2242089Ssos	int nsec;
2252089Ssos
2262089Ssos	if (!strcmp(arg, "off"))
2272089Ssos		nsec = 0;
2282089Ssos	else {
2292089Ssos		nsec = atoi(arg);
2302089Ssos		if ((*arg == '\0') || (nsec < 1)) {
23130764Scharnier			warnx("argument must be a positive number");
2322089Ssos			return;
2332089Ssos		}
2342089Ssos	}
2352089Ssos	if (ioctl(0, CONS_BLANKTIME, &nsec) == -1)
23630764Scharnier		warn("setting screensaver period");
2372089Ssos}
2382089Ssos
2392089Ssosvoid
2405536Ssosset_cursor_type(char *appearence)
2412089Ssos{
2425536Ssos	int type;
2432089Ssos
2446230Ssos	if (!strcmp(appearence, "normal"))
2456230Ssos		type = 0;
2466230Ssos	else if (!strcmp(appearence, "blink"))
2475536Ssos		type = 1;
2486230Ssos	else if (!strcmp(appearence, "destructive"))
2496230Ssos		type = 3;
2505536Ssos	else {
25130764Scharnier		warnx("argument to -c must be normal, blink or destructive");
2522089Ssos		return;
2532089Ssos	}
2545536Ssos	ioctl(0, CONS_CURSORTYPE, &type);
2552089Ssos}
2562089Ssos
25723457Sbrianvoid
2582089Ssosvideo_mode(int argc, char **argv, int *index)
2592089Ssos{
2602089Ssos	int mode;
2612089Ssos
2622089Ssos	if (*index < argc) {
2632784Ssos		if (!strcmp(argv[*index], "VGA_40x25"))
2642784Ssos			mode = SW_VGA_C40x25;
2652784Ssos		else if (!strcmp(argv[*index], "VGA_80x25"))
2662784Ssos			mode = SW_VGA_C80x25;
2675536Ssos		else if (!strcmp(argv[*index], "VGA_80x30"))
2685536Ssos			mode = SW_VGA_C80x30;
2692784Ssos		else if (!strcmp(argv[*index], "VGA_80x50"))
2702784Ssos			mode = SW_VGA_C80x50;
2715536Ssos		else if (!strcmp(argv[*index], "VGA_80x60"))
2725536Ssos			mode = SW_VGA_C80x60;
2732784Ssos		else if (!strcmp(argv[*index], "VGA_320x200"))
2742784Ssos			mode = SW_VGA_CG320;
2752784Ssos		else if (!strcmp(argv[*index], "EGA_80x25"))
2762784Ssos			mode = SW_ENH_C80x25;
2772784Ssos		else if (!strcmp(argv[*index], "EGA_80x43"))
2782784Ssos			mode = SW_ENH_C80x43;
2792089Ssos		else
2802089Ssos			return;
2812089Ssos		if (ioctl(0, mode, NULL) < 0)
28230764Scharnier			warn("cannot set videomode");
2832089Ssos		(*index)++;
2842089Ssos	}
2852089Ssos	return;
2862089Ssos}
2878857Srgrimes
2882089Ssosint
2892089Ssosget_color_number(char *color)
2902089Ssos{
2912089Ssos	int i;
2922089Ssos
2932089Ssos	for (i=0; i<16; i++)
2942089Ssos		if (!strcmp(color, legal_colors[i]))
2952089Ssos			return i;
2962089Ssos	return -1;
2972089Ssos}
2982089Ssos
29923457Sbrianvoid
3002089Ssosset_normal_colors(int argc, char **argv, int *index)
3012089Ssos{
3022089Ssos	int color;
3032089Ssos
3042089Ssos	if (*index < argc && (color = get_color_number(argv[*index])) != -1) {
3052089Ssos		(*index)++;
3062089Ssos		fprintf(stderr, "[=%dF", color);
3078857Srgrimes		if (*index < argc
3088857Srgrimes		    && (color = get_color_number(argv[*index])) != -1
3092089Ssos		    && color < 8) {
3102089Ssos			(*index)++;
3112089Ssos			fprintf(stderr, "[=%dG", color);
3122089Ssos		}
3132089Ssos	}
3142089Ssos}
3152089Ssos
31623457Sbrianvoid
3172089Ssosset_reverse_colors(int argc, char **argv, int *index)
3182089Ssos{
3192089Ssos	int color;
3202089Ssos
3212089Ssos	if ((color = get_color_number(argv[*(index)-1])) != -1) {
3222089Ssos		fprintf(stderr, "[=%dH", color);
3238857Srgrimes		if (*index < argc
3248857Srgrimes		    && (color = get_color_number(argv[*index])) != -1
3252089Ssos		    && color < 8) {
3262089Ssos			(*index)++;
3272089Ssos			fprintf(stderr, "[=%dI", color);
3282089Ssos		}
3292089Ssos	}
3302089Ssos}
3312089Ssos
33223457Sbrianvoid
33323457Sbrianset_console(char *arg)
33423457Sbrian{
33523457Sbrian	int n;
33623457Sbrian
33723457Sbrian	if( !arg || strspn(arg,"0123456789") != strlen(arg)) {
33830764Scharnier		warnx("bad console number");
33923457Sbrian		return;
34023457Sbrian	}
34123457Sbrian
34223457Sbrian	n = atoi(arg);
34323457Sbrian	if (n < 1 || n > 12) {
34430764Scharnier		warnx("console number out of range");
34523457Sbrian	} else if (ioctl(0,VT_ACTIVATE,(char *)n) == -1)
34630764Scharnier		warn("ioctl(VT_ACTIVATE)");
34723457Sbrian}
34823457Sbrian
34923457Sbrianvoid
3502089Ssosset_border_color(char *arg)
3512089Ssos{
3522089Ssos	int color;
3532089Ssos
3542089Ssos	if ((color = get_color_number(arg)) != -1) {
3552089Ssos		fprintf(stderr, "[=%dA", color);
3562089Ssos	}
3572089Ssos	else
3588857Srgrimes		usage();
3592089Ssos}
3602089Ssos
36116565Ssosvoid
36216565Ssosset_mouse(char *arg)
36316565Ssos{
36416565Ssos	struct mouse_info mouse;
36516565Ssos
36616565Ssos	if (!strcmp(arg, "on"))
36716565Ssos		mouse.operation = MOUSE_SHOW;
36816565Ssos	else if (!strcmp(arg, "off"))
36916565Ssos		mouse.operation = MOUSE_HIDE;
37016565Ssos	else {
37130764Scharnier		warnx("argument to -m must either on or off");
37216565Ssos		return;
37316565Ssos	}
37416565Ssos	ioctl(0, CONS_MOUSECTL, &mouse);
37516565Ssos}
37616565Ssos
37723457Sbrianvoid
3782089Ssostest_frame()
3792089Ssos{
3802089Ssos	int i;
3812089Ssos
3822089Ssos	fprintf(stdout, "[=0G\n\n");
3832089Ssos	for (i=0; i<8; i++) {
3842089Ssos		fprintf(stdout, "[=15F[=0G        %2d [=%dF%-16s"
3852089Ssos				"[=15F[=0G        %2d [=%dF%-16s        "
3862089Ssos				"[=15F %2d [=%dGBACKGROUND[=0G\n",
3878857Srgrimes			i, i, legal_colors[i], i+8, i+8,
3888857Srgrimes			legal_colors[i+8], i, i);
3892089Ssos	}
3902089Ssos	fprintf(stdout, "[=%dF[=%dG[=%dH[=%dI\n",
3918857Srgrimes		info.mv_norm.fore, info.mv_norm.back,
3922089Ssos		info.mv_rev.fore, info.mv_rev.back);
3932089Ssos}
3942089Ssos
39523457Sbrianint
3962089Ssosmain(int argc, char **argv)
3972089Ssos{
3982089Ssos	int		opt;
3992089Ssos
4008857Srgrimes
4012089Ssos	info.size = sizeof(info);
40230764Scharnier	if (ioctl(0, CONS_GETINFO, &info) < 0)
40330764Scharnier		err(1, "must be on a virtual console");
40423457Sbrian	while((opt = getopt(argc, argv, "b:c:df:l:Lm:r:s:t:x")) != -1)
4052089Ssos		switch(opt) {
40616565Ssos			case 'b':
40716565Ssos				set_border_color(optarg);
40816565Ssos				break;
4092089Ssos			case 'c':
4105536Ssos				set_cursor_type(optarg);
4112089Ssos				break;
4122089Ssos			case 'd':
4132089Ssos				print_scrnmap();
4142089Ssos				break;
4152089Ssos			case 'f':
4162089Ssos				load_font(optarg,
4172089Ssos					nextarg(argc, argv, &optind, 'f'));
4182089Ssos				break;
4192089Ssos			case 'l':
4202089Ssos				load_scrnmap(optarg);
4212089Ssos				break;
4222089Ssos			case 'L':
4232089Ssos				load_default_scrnmap();
4242089Ssos				break;
42516565Ssos			case 'm':
42616565Ssos				set_mouse(optarg);
42716565Ssos				break;
4282089Ssos			case 'r':
4292089Ssos				set_reverse_colors(argc, argv, &optind);
4302089Ssos				break;
43123457Sbrian			case 's':
43223457Sbrian				set_console(optarg);
43323457Sbrian				break;
4342089Ssos			case 't':
4352089Ssos				set_screensaver_timeout(optarg);
4362089Ssos				break;
4372089Ssos			case 'x':
4382089Ssos				hex = 1;
4392089Ssos				break;
4402089Ssos			default:
4412089Ssos				usage();
4422089Ssos		}
44323457Sbrian	video_mode(argc, argv, &optind);
44423457Sbrian	set_normal_colors(argc, argv, &optind);
4452089Ssos	if (optind < argc && !strcmp(argv[optind], "show")) {
4462089Ssos		test_frame();
4472089Ssos		optind++;
4482089Ssos	}
44930764Scharnier	if ((optind != argc) || (argc == 1))
4502089Ssos		usage();
45123457Sbrian	return 0;
4522089Ssos}
4532089Ssos
454