vidcontrol.c revision 120201
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
1597748Sschweikh *    derived from this software without 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[] =
3150479Speter  "$FreeBSD: head/usr.sbin/vidcontrol/vidcontrol.c 120201 2003-09-18 16:20:32Z eivind $";
3230764Scharnier#endif /* not lint */
3330764Scharnier
342089Ssos#include <ctype.h>
3530764Scharnier#include <err.h>
3655849Syokota#include <limits.h>
372089Ssos#include <stdio.h>
3823457Sbrian#include <stdlib.h>
3930764Scharnier#include <string.h>
4023702Speter#include <unistd.h>
4166834Sphk#include <sys/fbio.h>
4266834Sphk#include <sys/consio.h>
432089Ssos#include <sys/errno.h>
4475344Ssobomax#include <sys/types.h>
4575344Ssobomax#include <sys/stat.h>
462089Ssos#include "path.h"
4723457Sbrian#include "decode.h"
482089Ssos
4971642Ssobomax#define _VESA_800x600_DFL_COLS 80
5071642Ssobomax#define _VESA_800x600_DFL_ROWS 25
5171642Ssobomax#define _VESA_800x600_DFL_FNSZ 16
5271642Ssobomax
53102111Ssobomax/* Screen dump modes */
54102111Ssobomax#define DUMP_FMT_RAW	1
55102111Ssobomax#define DUMP_FMT_TXT	2
56102111Ssobomax/* Screen dump options */
57102111Ssobomax#define DUMP_FBF	0
58102111Ssobomax#define DUMP_ALL	1
59102111Ssobomax/* Screen dump file format revision */
6076845Ssobomax#define DUMP_FMT_REV	1
6176845Ssobomax
622089Ssoschar 	legal_colors[16][16] = {
632089Ssos	"black", "blue", "green", "cyan",
642089Ssos	"red", "magenta", "brown", "white",
652089Ssos	"grey", "lightblue", "lightgreen", "lightcyan",
662089Ssos	"lightred", "lightmagenta", "yellow", "lightwhite"
676628Ssos};
682089Ssosint 	hex = 0;
696047Ssosint 	number;
7071642Ssobomaxint	vesa_cols = _VESA_800x600_DFL_COLS;
7171642Ssobomaxint	vesa_rows = _VESA_800x600_DFL_ROWS;
722089Ssoschar 	letter;
732089Ssosstruct 	vid_info info;
742089Ssos
752089Ssos
7630764Scharnierstatic void
776628Ssosusage()
786628Ssos{
7999706Sdd	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
80102111Ssobomax"usage: vidcontrol [-CdHLPpx] [-b color] [-c appearance] [-f [size] file]",
8177329Sdes"                  [-g geometry] [-h size] [-i adapter | mode] [-l screen_map]",
8299706Sdd"                  [-M char] [-m on | off] [-r foreground background]",
8399706Sdd"                  [-S on | off] [-s number] [-t N | off] [mode]",
8499706Sdd"                  [foreground [background]] [show]");
8530764Scharnier	exit(1);
866628Ssos}
876628Ssos
882089Ssoschar *
8975344Ssobomaxnextarg(int ac, char **av, int *indp, int oc, int strict)
902089Ssos{
912089Ssos	if (*indp < ac)
922089Ssos		return(av[(*indp)++]);
9375344Ssobomax	if (strict != 0)
9475344Ssobomax		errx(1, "option requires two arguments -- %c", oc);
9575344Ssobomax	return(NULL);
962089Ssos}
972089Ssos
9892460SsobomaxFILE *
9992460Ssobomaxopenguess(char *a[], char *b[], char *c[], char *d[], char **name)
1002089Ssos{
10192460Ssobomax	FILE *f;
10292460Ssobomax	int i, j, k, l;
1032089Ssos
10492460Ssobomax	for (i = 0; a[i] != NULL; i++) {
10592460Ssobomax		for (j = 0; b[j] != NULL; j++) {
10692460Ssobomax			for (k = 0; c[k] != NULL; k++) {
10792460Ssobomax				for (l = 0; d[l] != NULL; l++) {
10892460Ssobomax					asprintf(name, "%s%s%s%s", a[i], b[j],
10992460Ssobomax					    c[k], d[l]);
11092460Ssobomax					f = fopen(*name, "r");
11192460Ssobomax					if (f != NULL)
11292460Ssobomax						return (f);
11392460Ssobomax					free(*name);
11492460Ssobomax				}
11592460Ssobomax			}
11692460Ssobomax		}
11752262Sbillf	}
11892460Ssobomax	return (NULL);
1192089Ssos}
1202089Ssos
1212089Ssosvoid
1222089Ssosload_scrnmap(char *filename)
1232089Ssos{
12492460Ssobomax	FILE *fd;
12592460Ssobomax	int size;
1262089Ssos	char *name;
1272089Ssos	scrmap_t scrnmap;
12892460Ssobomax	char *a[] = {"", SCRNMAP_PATH, NULL};
12992460Ssobomax	char *b[] = {filename, NULL};
13092460Ssobomax	char *c[] = {"", ".scm", NULL};
13192460Ssobomax	char *d[] = {"", NULL};
1322089Ssos
13392460Ssobomax	fd = openguess(a, b, c, d, &name);
1342089Ssos	if (fd == NULL) {
13530764Scharnier		warn("screenmap file not found");
1362089Ssos		return;
1372089Ssos	}
1382089Ssos	size = sizeof(scrnmap);
13975344Ssobomax	if (decode(fd, (char *)&scrnmap, size) != size) {
1402089Ssos		rewind(fd);
1412089Ssos		if (fread(&scrnmap, 1, size, fd) != size) {
14230764Scharnier			warnx("bad screenmap file");
14323457Sbrian			fclose(fd);
1442089Ssos			return;
1452089Ssos		}
1462089Ssos	}
1472089Ssos	if (ioctl(0, PIO_SCRNMAP, &scrnmap) < 0)
14830764Scharnier		warn("can't load screenmap");
14923457Sbrian	fclose(fd);
1502089Ssos}
1512089Ssos
1522089Ssosvoid
1532089Ssosload_default_scrnmap()
1542089Ssos{
1556628Ssos	scrmap_t scrnmap;
1562089Ssos	int i;
1572089Ssos
1582089Ssos	for (i=0; i<256; i++)
1592089Ssos		*((char*)&scrnmap + i) = i;
1602089Ssos	if (ioctl(0, PIO_SCRNMAP, &scrnmap) < 0)
16130764Scharnier		warn("can't load default screenmap");
1622089Ssos}
1632089Ssos
1642089Ssosvoid
1652089Ssosprint_scrnmap()
1662089Ssos{
1672089Ssos	unsigned char map[256];
1682089Ssos	int i;
1692089Ssos
1702089Ssos	if (ioctl(0, GIO_SCRNMAP, &map) < 0) {
17130764Scharnier		warn("getting screenmap");
1722089Ssos		return;
1732089Ssos	}
1742089Ssos	for (i=0; i<sizeof(map); i++) {
1752089Ssos		if (i > 0 && i % 16 == 0)
1762089Ssos			fprintf(stdout, "\n");
1772089Ssos		if (hex)
1788857Srgrimes			fprintf(stdout, " %02x", map[i]);
1792089Ssos		else
1802089Ssos			fprintf(stdout, " %03d", map[i]);
1812089Ssos	}
1822089Ssos	fprintf(stdout, "\n");
1832089Ssos
1842089Ssos}
1852089Ssos
18675344Ssobomaxint
18775344Ssobomaxfsize(FILE *file)
18875344Ssobomax{
18975344Ssobomax	struct stat sb;
19075344Ssobomax
19175344Ssobomax	if (fstat(fileno(file), &sb) == 0)
19275344Ssobomax		return sb.st_size;
19375344Ssobomax	else
19475344Ssobomax		return -1;
19575344Ssobomax}
19675344Ssobomax
19775344Ssobomax#define DATASIZE(x) ((x).w * (x).h * 256 / 8)
19875344Ssobomax
1998857Srgrimesvoid
2002089Ssosload_font(char *type, char *filename)
2012089Ssos{
20292460Ssobomax	FILE	*fd;
20375344Ssobomax	int	h, i, size, w;
20475344Ssobomax	unsigned long io = 0;	/* silence stupid gcc(1) in the Wall mode */
20592460Ssobomax	char	*name, *fontmap, size_sufx[6];
20692460Ssobomax	char	*a[] = {"", FONT_PATH, NULL};
20792460Ssobomax	char	*b[] = {filename, NULL};
20892460Ssobomax	char	*c[] = {"", size_sufx, NULL};
20992460Ssobomax	char	*d[] = {"", ".fnt", NULL};
21092460Ssobomax	vid_info_t info;
2112089Ssos
21275344Ssobomax	struct sizeinfo {
21375344Ssobomax		int w;
21475344Ssobomax		int h;
21575344Ssobomax		unsigned long io;
21675344Ssobomax	} sizes[] = {{8, 16, PIO_FONT8x16},
21775344Ssobomax		     {8, 14, PIO_FONT8x14},
21875344Ssobomax		     {8,  8,  PIO_FONT8x8},
21975344Ssobomax		     {0,  0,            0}};
22075344Ssobomax
22192460Ssobomax	info.size = sizeof(info);
22292460Ssobomax	if (ioctl(0, CONS_GETINFO, &info) == -1) {
22392460Ssobomax		warn("failed to obtain current video mode parameters");
22492460Ssobomax		return;
2252089Ssos	}
22692460Ssobomax	snprintf(size_sufx, sizeof(size_sufx), "-8x%d", info.font_size);
22792460Ssobomax	fd = openguess(a, b, c, d, &name);
2282089Ssos	if (fd == NULL) {
22975344Ssobomax		warn("%s: can't load font file", filename);
2302089Ssos		return;
2312089Ssos	}
23275344Ssobomax	if (type != NULL) {
23375344Ssobomax		size = 0;
23475344Ssobomax		if (sscanf(type, "%dx%d", &w, &h) == 2)
23575344Ssobomax			for (i = 0; sizes[i].w != 0; i++)
23675344Ssobomax				if (sizes[i].w == w && sizes[i].h == h) {
23775344Ssobomax					size = DATASIZE(sizes[i]);
23875344Ssobomax					io = sizes[i].io;
23975344Ssobomax				}
24075344Ssobomax
24175344Ssobomax		if (size == 0) {
24275344Ssobomax			warnx("%s: bad font size specification", type);
24375344Ssobomax			fclose(fd);
24475344Ssobomax			return;
24575344Ssobomax		}
24675344Ssobomax	} else {
24775344Ssobomax		/* Apply heuristics */
24875344Ssobomax		int j;
24975344Ssobomax		int dsize[2];
25075344Ssobomax
25175344Ssobomax		size = DATASIZE(sizes[0]);
25275344Ssobomax		fontmap = (char*) malloc(size);
25375344Ssobomax		dsize[0] = decode(fd, fontmap, size);
25475344Ssobomax		dsize[1] = fsize(fd);
25575344Ssobomax		free(fontmap);
25675344Ssobomax
25775344Ssobomax		size = 0;
25875344Ssobomax		for (j = 0; j < 2; j++)
25975344Ssobomax			for (i = 0; sizes[i].w != 0; i++)
26075344Ssobomax				if (DATASIZE(sizes[i]) == dsize[j]) {
26175344Ssobomax					size = dsize[j];
26275344Ssobomax					io = sizes[i].io;
26375344Ssobomax					j = 2;	/* XXX */
26475344Ssobomax					break;
26575344Ssobomax				}
26675344Ssobomax
26775344Ssobomax		if (size == 0) {
26875344Ssobomax			warnx("%s: can't guess font size", filename);
26975344Ssobomax			fclose(fd);
27075344Ssobomax			return;
27175344Ssobomax		}
27275344Ssobomax		rewind(fd);
2732089Ssos	}
27475344Ssobomax
2752089Ssos	fontmap = (char*) malloc(size);
27675344Ssobomax	if (decode(fd, fontmap, size) != size) {
2772089Ssos		rewind(fd);
27875344Ssobomax		if (fsize(fd) != size || fread(fontmap, 1, size, fd) != size) {
27975344Ssobomax			warnx("%s: bad font file", filename);
28023457Sbrian			fclose(fd);
2812089Ssos			free(fontmap);
2822089Ssos			return;
2832089Ssos		}
2842089Ssos	}
2852089Ssos	if (ioctl(0, io, fontmap) < 0)
28630764Scharnier		warn("can't load font");
28723457Sbrian	fclose(fd);
2882089Ssos	free(fontmap);
2892089Ssos}
2902089Ssos
2912089Ssosvoid
2922089Ssosset_screensaver_timeout(char *arg)
2932089Ssos{
2942089Ssos	int nsec;
2952089Ssos
2962089Ssos	if (!strcmp(arg, "off"))
2972089Ssos		nsec = 0;
2982089Ssos	else {
2992089Ssos		nsec = atoi(arg);
3002089Ssos		if ((*arg == '\0') || (nsec < 1)) {
30130764Scharnier			warnx("argument must be a positive number");
3022089Ssos			return;
3032089Ssos		}
3042089Ssos	}
3052089Ssos	if (ioctl(0, CONS_BLANKTIME, &nsec) == -1)
30630764Scharnier		warn("setting screensaver period");
3072089Ssos}
3082089Ssos
3092089Ssosvoid
3105536Ssosset_cursor_type(char *appearence)
3112089Ssos{
3125536Ssos	int type;
3132089Ssos
3146230Ssos	if (!strcmp(appearence, "normal"))
3156230Ssos		type = 0;
3166230Ssos	else if (!strcmp(appearence, "blink"))
3175536Ssos		type = 1;
3186230Ssos	else if (!strcmp(appearence, "destructive"))
3196230Ssos		type = 3;
3205536Ssos	else {
32130764Scharnier		warnx("argument to -c must be normal, blink or destructive");
3222089Ssos		return;
3232089Ssos	}
3245536Ssos	ioctl(0, CONS_CURSORTYPE, &type);
3252089Ssos}
3262089Ssos
327120201Seivindint
3282089Ssosvideo_mode(int argc, char **argv, int *index)
3292089Ssos{
33039592Syokota	static struct {
33139592Syokota		char *name;
33239592Syokota		unsigned long mode;
33339592Syokota	} modes[] = {
33439592Syokota		{ "80x25",		SW_TEXT_80x25 },
33539592Syokota		{ "80x30",		SW_TEXT_80x30 },
33639592Syokota		{ "80x43",		SW_TEXT_80x43 },
33739592Syokota		{ "80x50",		SW_TEXT_80x50 },
33839592Syokota		{ "80x60",		SW_TEXT_80x60 },
33939592Syokota		{ "132x25",		SW_TEXT_132x25 },
34039592Syokota		{ "132x30",		SW_TEXT_132x30 },
34139592Syokota		{ "132x43",		SW_TEXT_132x43 },
34239592Syokota		{ "132x50",		SW_TEXT_132x50 },
34339592Syokota		{ "132x60",		SW_TEXT_132x60 },
34439592Syokota		{ "VGA_40x25",		SW_VGA_C40x25 },
34539592Syokota		{ "VGA_80x25",		SW_VGA_C80x25 },
34639592Syokota		{ "VGA_80x30",		SW_VGA_C80x30 },
34739592Syokota		{ "VGA_80x50",		SW_VGA_C80x50 },
34839592Syokota		{ "VGA_80x60",		SW_VGA_C80x60 },
34948105Syokota#ifdef SW_VGA_C90x25
35048105Syokota		{ "VGA_90x25",		SW_VGA_C90x25 },
35148105Syokota		{ "VGA_90x30",		SW_VGA_C90x30 },
35248105Syokota		{ "VGA_90x43",		SW_VGA_C90x43 },
35348105Syokota		{ "VGA_90x50",		SW_VGA_C90x50 },
35448105Syokota		{ "VGA_90x60",		SW_VGA_C90x60 },
35548105Syokota#endif
35639592Syokota		{ "VGA_320x200",	SW_VGA_CG320 },
35739592Syokota		{ "EGA_80x25",		SW_ENH_C80x25 },
35839592Syokota		{ "EGA_80x43",		SW_ENH_C80x43 },
35939592Syokota		{ "VESA_132x25",	SW_VESA_C132x25 },
36039592Syokota		{ "VESA_132x43",	SW_VESA_C132x43 },
36139592Syokota		{ "VESA_132x50",	SW_VESA_C132x50 },
36239592Syokota		{ "VESA_132x60",	SW_VESA_C132x60 },
36339592Syokota		{ "VESA_800x600",	SW_VESA_800x600 },
36439592Syokota		{ NULL },
36539592Syokota	};
36642605Smjacob	unsigned long mode = 0;
36748105Syokota	int cur_mode;
36848105Syokota	int ioerr;
36939287Ssos	int size[3];
37039592Syokota	int i;
3712089Ssos
37248105Syokota	if (ioctl(0, CONS_GET, &cur_mode) < 0)
37348105Syokota		err(1, "cannot get the current video mode");
3742089Ssos	if (*index < argc) {
37539592Syokota		for (i = 0; modes[i].name != NULL; ++i) {
37639592Syokota			if (!strcmp(argv[*index], modes[i].name)) {
37739592Syokota				mode = modes[i].mode;
37839592Syokota				break;
37939592Syokota			}
38039592Syokota		}
38139592Syokota		if (modes[i].name == NULL)
382120201Seivind			return EXIT_FAILURE;
383120201Seivind		if (ioctl(0, mode, NULL) < 0) {
38430764Scharnier			warn("cannot set videomode");
385120201Seivind			return EXIT_FAILURE;
386120201Seivind		}
38739287Ssos		if (mode == SW_VESA_800x600) {
38871642Ssobomax			/* columns */
38971642Ssobomax			if ((vesa_cols * 8 > 800) || (vesa_cols <= 0)) {
39071642Ssobomax				warnx("incorrect number of columns: %d",
39171642Ssobomax				      vesa_cols);
39271642Ssobomax				size[0] = _VESA_800x600_DFL_COLS;
39371642Ssobomax			} else {
39471642Ssobomax				size[0] = vesa_cols;
39571642Ssobomax			}
39671642Ssobomax			/* rows */
39771642Ssobomax			if ((vesa_rows * _VESA_800x600_DFL_FNSZ > 600) ||
39871642Ssobomax			    (vesa_rows <=0)) {
39971642Ssobomax				warnx("incorrect number of rows: %d",
40071642Ssobomax				      vesa_rows);
40171642Ssobomax				size[1] = _VESA_800x600_DFL_ROWS;
40271642Ssobomax			} else {
40371642Ssobomax				size[1] = vesa_rows;
40471642Ssobomax			}
40571642Ssobomax			/* font size */
40671642Ssobomax			size[2] = _VESA_800x600_DFL_FNSZ;
40748105Syokota			if (ioctl(0, KDRASTER, size)) {
40848105Syokota				ioerr = errno;
40948105Syokota				if (cur_mode >= M_VESA_BASE)
41080148Syokota					ioctl(0,
41180148Syokota					    _IO('V', cur_mode - M_VESA_BASE),
41280148Syokota					    NULL);
41348105Syokota				else
41448105Syokota					ioctl(0, _IO('S', cur_mode), NULL);
41548105Syokota				warnc(ioerr, "cannot activate raster display");
416120201Seivind				return EXIT_FAILURE;
41748105Syokota			}
41839287Ssos		}
4192089Ssos		(*index)++;
4202089Ssos	}
421120201Seivind	return EXIT_SUCCESS;
4222089Ssos}
4238857Srgrimes
4242089Ssosint
4252089Ssosget_color_number(char *color)
4262089Ssos{
4272089Ssos	int i;
4282089Ssos
4292089Ssos	for (i=0; i<16; i++)
4302089Ssos		if (!strcmp(color, legal_colors[i]))
4312089Ssos			return i;
4322089Ssos	return -1;
4332089Ssos}
4342089Ssos
43523457Sbrianvoid
4362089Ssosset_normal_colors(int argc, char **argv, int *index)
4372089Ssos{
4382089Ssos	int color;
4392089Ssos
4402089Ssos	if (*index < argc && (color = get_color_number(argv[*index])) != -1) {
4412089Ssos		(*index)++;
4422089Ssos		fprintf(stderr, "[=%dF", color);
4438857Srgrimes		if (*index < argc
4448857Srgrimes		    && (color = get_color_number(argv[*index])) != -1
4452089Ssos		    && color < 8) {
4462089Ssos			(*index)++;
4472089Ssos			fprintf(stderr, "[=%dG", color);
4482089Ssos		}
4492089Ssos	}
4502089Ssos}
4512089Ssos
45223457Sbrianvoid
4532089Ssosset_reverse_colors(int argc, char **argv, int *index)
4542089Ssos{
4552089Ssos	int color;
4562089Ssos
4572089Ssos	if ((color = get_color_number(argv[*(index)-1])) != -1) {
4582089Ssos		fprintf(stderr, "[=%dH", color);
4598857Srgrimes		if (*index < argc
4608857Srgrimes		    && (color = get_color_number(argv[*index])) != -1
4612089Ssos		    && color < 8) {
4622089Ssos			(*index)++;
4632089Ssos			fprintf(stderr, "[=%dI", color);
4642089Ssos		}
4652089Ssos	}
4662089Ssos}
4672089Ssos
46823457Sbrianvoid
46923457Sbrianset_console(char *arg)
47023457Sbrian{
47123457Sbrian	int n;
47223457Sbrian
47323457Sbrian	if( !arg || strspn(arg,"0123456789") != strlen(arg)) {
47430764Scharnier		warnx("bad console number");
47523457Sbrian		return;
47623457Sbrian	}
47723457Sbrian
47823457Sbrian	n = atoi(arg);
47951393Syokota	if (n < 1 || n > 16) {
48030764Scharnier		warnx("console number out of range");
48142605Smjacob	} else if (ioctl(0, VT_ACTIVATE, (caddr_t) (long) n) == -1)
48230764Scharnier		warn("ioctl(VT_ACTIVATE)");
48323457Sbrian}
48423457Sbrian
48523457Sbrianvoid
4862089Ssosset_border_color(char *arg)
4872089Ssos{
4882089Ssos	int color;
4892089Ssos
4902089Ssos	if ((color = get_color_number(arg)) != -1) {
4912089Ssos		fprintf(stderr, "[=%dA", color);
4922089Ssos	}
4932089Ssos	else
4948857Srgrimes		usage();
4952089Ssos}
4962089Ssos
49716565Ssosvoid
49855849Syokotaset_mouse_char(char *arg)
49955849Syokota{
50055849Syokota	struct mouse_info mouse;
50155849Syokota	long l;
50255849Syokota
50355849Syokota	l = strtol(arg, NULL, 0);
50475788Sache	if ((l < 0) || (l > UCHAR_MAX - 3)) {
50575788Sache		warnx("argument to -M must be 0 through %d", UCHAR_MAX - 3);
50655849Syokota		return;
50755849Syokota	}
50855849Syokota	mouse.operation = MOUSE_MOUSECHAR;
50955849Syokota	mouse.u.mouse_char = (int)l;
51055849Syokota	ioctl(0, CONS_MOUSECTL, &mouse);
51155849Syokota}
51255849Syokota
51355849Syokotavoid
51416565Ssosset_mouse(char *arg)
51516565Ssos{
51616565Ssos	struct mouse_info mouse;
51716565Ssos
51816565Ssos	if (!strcmp(arg, "on"))
51916565Ssos		mouse.operation = MOUSE_SHOW;
52016565Ssos	else if (!strcmp(arg, "off"))
52116565Ssos		mouse.operation = MOUSE_HIDE;
52216565Ssos	else {
52399707Sdd		warnx("argument to -m must be either on or off");
52416565Ssos		return;
52516565Ssos	}
52616565Ssos	ioctl(0, CONS_MOUSECTL, &mouse);
52716565Ssos}
52816565Ssos
52999705Sddvoid
53099705Sddset_lockswitch(char *arg)
53199705Sdd{
53299705Sdd	int data;
53399705Sdd
53499705Sdd	if (!strcmp(arg, "off"))
53599705Sdd		data = 0x01;
53699705Sdd	else if (!strcmp(arg, "on"))
53799705Sdd		data = 0x02;
53899705Sdd	else {
53999707Sdd		warnx("argument to -S must be either on or off");
54099705Sdd		return;
54199705Sdd	}
54299705Sdd	if (ioctl(0, VT_LOCKSWITCH, &data) == -1)
54399705Sdd		warn("ioctl(VT_LOCKSWITCH)");
54499705Sdd}
54599705Sdd
54639287Ssosstatic char
54739287Ssos*adapter_name(int type)
54839287Ssos{
54939287Ssos    static struct {
55039287Ssos	int type;
55139287Ssos	char *name;
55239287Ssos    } names[] = {
55339287Ssos	{ KD_MONO,	"MDA" },
55439287Ssos	{ KD_HERCULES,	"Hercules" },
55539287Ssos	{ KD_CGA,	"CGA" },
55639287Ssos	{ KD_EGA,	"EGA" },
55739287Ssos	{ KD_VGA,	"VGA" },
55839287Ssos	{ KD_PC98,	"PC-98xx" },
55948105Syokota	{ KD_TGA,	"TGA" },
56039287Ssos	{ -1,		"Unknown" },
56139287Ssos    };
56239287Ssos    int i;
56339287Ssos
56439287Ssos    for (i = 0; names[i].type != -1; ++i)
56539287Ssos	if (names[i].type == type)
56639287Ssos	    break;
56739287Ssos    return names[i].name;
56839287Ssos}
56939287Ssos
57023457Sbrianvoid
57139287Ssosshow_adapter_info(void)
57239287Ssos{
57342505Syokota	struct video_adapter_info ad;
57439287Ssos
57539287Ssos	ad.va_index = 0;
57639287Ssos	if (ioctl(0, CONS_ADPINFO, &ad)) {
57739287Ssos		warn("failed to obtain adapter information");
57839287Ssos		return;
57939287Ssos	}
58039287Ssos
58142505Syokota	printf("fb%d:\n", ad.va_index);
58242505Syokota	printf("    %.*s%d, type:%s%s (%d), flags:0x%x\n",
58342505Syokota	       (int)sizeof(ad.va_name), ad.va_name, ad.va_unit,
58439287Ssos	       (ad.va_flags & V_ADP_VESA) ? "VESA " : "",
58542505Syokota	       adapter_name(ad.va_type), ad.va_type, ad.va_flags);
58639287Ssos	printf("    initial mode:%d, current mode:%d, BIOS mode:%d\n",
58739287Ssos	       ad.va_initial_mode, ad.va_mode, ad.va_initial_bios_mode);
58848105Syokota	printf("    frame buffer window:0x%x, buffer size:0x%x\n",
58948105Syokota	       ad.va_window, ad.va_buffer_size);
59048105Syokota	printf("    window size:0x%x, origin:0x%x\n",
59148105Syokota	       ad.va_window_size, ad.va_window_orig);
59248105Syokota	printf("    display start address (%d, %d), scan line width:%d\n",
59348105Syokota	       ad.va_disp_start.x, ad.va_disp_start.y, ad.va_line_width);
59448105Syokota	printf("    reserved:0x%x\n", ad.va_unused0);
59539287Ssos}
59639287Ssos
59739287Ssosvoid
59839287Ssosshow_mode_info(void)
59939287Ssos{
60039287Ssos	struct video_info info;
60139287Ssos	char buf[80];
60239287Ssos	int mode;
60339287Ssos	int c;
60439287Ssos
60539287Ssos	printf("    mode#     flags   type    size       "
60639287Ssos	       "font      window      linear buffer\n");
60739287Ssos	printf("---------------------------------------"
60839287Ssos	       "---------------------------------------\n");
60939287Ssos	for (mode = 0; mode < M_VESA_MODE_MAX; ++mode) {
61039287Ssos		info.vi_mode = mode;
61139287Ssos		if (ioctl(0, CONS_MODEINFO, &info))
61239287Ssos			continue;
61348105Syokota		if (info.vi_mode != mode)
61448105Syokota			continue;
61539287Ssos
61639287Ssos		printf("%3d (0x%03x)", mode, mode);
61739287Ssos    		printf(" 0x%08x", info.vi_flags);
61839287Ssos		if (info.vi_flags & V_INFO_GRAPHICS) {
61939287Ssos			c = 'G';
62039287Ssos			snprintf(buf, sizeof(buf), "%dx%dx%d %d",
62139287Ssos				 info.vi_width, info.vi_height,
62239287Ssos				 info.vi_depth, info.vi_planes);
62339287Ssos		} else {
62439287Ssos			c = 'T';
62539287Ssos			snprintf(buf, sizeof(buf), "%dx%d",
62639287Ssos				 info.vi_width, info.vi_height);
62739287Ssos		}
62839287Ssos		printf(" %c %-15s", c, buf);
62939287Ssos		snprintf(buf, sizeof(buf), "%dx%d",
63039287Ssos			 info.vi_cwidth, info.vi_cheight);
63139287Ssos		printf(" %-5s", buf);
63239287Ssos    		printf(" 0x%05x %2dk %2dk",
63343186Sdfr		       info.vi_window, (int)info.vi_window_size/1024,
63443186Sdfr		       (int)info.vi_window_gran/1024);
63548105Syokota    		printf(" 0x%08x %dk\n",
63643186Sdfr		       info.vi_buffer, (int)info.vi_buffer_size/1024);
63739287Ssos	}
63839287Ssos}
63939287Ssos
64039287Ssosvoid
64139287Ssosshow_info(char *arg)
64239287Ssos{
64339287Ssos	if (!strcmp(arg, "adapter"))
64439287Ssos		show_adapter_info();
64539287Ssos	else if (!strcmp(arg, "mode"))
64639287Ssos		show_mode_info();
64739287Ssos	else {
64899707Sdd		warnx("argument to -i must be either adapter or mode");
64939287Ssos		return;
65039287Ssos	}
65139287Ssos}
65239287Ssos
65339287Ssosvoid
6542089Ssostest_frame()
6552089Ssos{
6562089Ssos	int i;
6572089Ssos
6582089Ssos	fprintf(stdout, "[=0G\n\n");
6592089Ssos	for (i=0; i<8; i++) {
6602089Ssos		fprintf(stdout, "[=15F[=0G        %2d [=%dF%-16s"
6612089Ssos				"[=15F[=0G        %2d [=%dF%-16s        "
6622089Ssos				"[=15F %2d [=%dGBACKGROUND[=0G\n",
6638857Srgrimes			i, i, legal_colors[i], i+8, i+8,
6648857Srgrimes			legal_colors[i+8], i, i);
6652089Ssos	}
6662089Ssos	fprintf(stdout, "[=%dF[=%dG[=%dH[=%dI\n",
6678857Srgrimes		info.mv_norm.fore, info.mv_norm.back,
6682089Ssos		info.mv_rev.fore, info.mv_rev.back);
6692089Ssos}
6702089Ssos
67176845Ssobomax/*
67276845Ssobomax * Snapshot the video memory of that terminal, using the CONS_SCRSHOT
67376845Ssobomax * ioctl, and writes the results to stdout either in the special
67476845Ssobomax * binary format (see manual page for details), or in the plain
67576845Ssobomax * text format.
67676845Ssobomax */
67776845Ssobomaxvoid
678102111Ssobomaxdump_screen(int mode, int opt)
67976845Ssobomax{
68076845Ssobomax	scrshot_t shot;
68176845Ssobomax	vid_info_t info;
68276845Ssobomax
68376845Ssobomax	info.size = sizeof(info);
68476845Ssobomax	if (ioctl(0, CONS_GETINFO, &info) == -1) {
68576845Ssobomax		warn("failed to obtain current video mode parameters");
68676845Ssobomax		return;
68776845Ssobomax	}
68876845Ssobomax
689102111Ssobomax	shot.x = shot.y = 0;
690102111Ssobomax	shot.xsize = info.mv_csz;
691102111Ssobomax	shot.ysize = info.mv_rsz;
692102111Ssobomax	if (opt == DUMP_ALL)
693102111Ssobomax		shot.ysize += info.mv_hsz;
694102111Ssobomax
695102111Ssobomax	shot.buf = alloca(shot.xsize * shot.ysize * sizeof(u_int16_t));
69676845Ssobomax	if (shot.buf == NULL) {
69776845Ssobomax		warn("failed to allocate memory for dump");
69876845Ssobomax		return;
69976845Ssobomax	}
70076845Ssobomax
70176845Ssobomax	if (ioctl(0, CONS_SCRSHOT, &shot) == -1) {
70276845Ssobomax		warn("failed to get dump of the screen");
70376845Ssobomax		return;
70476845Ssobomax	}
70576845Ssobomax
706102111Ssobomax	if (mode == DUMP_FMT_RAW) {
70776845Ssobomax		printf("SCRSHOT_%c%c%c%c", DUMP_FMT_REV, 2,
70876845Ssobomax		       shot.xsize, shot.ysize);
70976845Ssobomax		fflush(stdout);
71076845Ssobomax
71176845Ssobomax		(void)write(STDOUT_FILENO, shot.buf,
71276845Ssobomax			    shot.xsize * shot.ysize * sizeof(u_int16_t));
71376845Ssobomax	} else {
71476845Ssobomax		char *line;
71576845Ssobomax		int x, y;
71676845Ssobomax		u_int16_t ch;
71776845Ssobomax
71876845Ssobomax		line = alloca(shot.xsize + 1);
71976845Ssobomax		if (line == NULL) {
72076845Ssobomax			warn("failed to allocate memory for line buffer");
72176845Ssobomax			return;
72276845Ssobomax		}
72376845Ssobomax
72476845Ssobomax		for (y = 0; y < shot.ysize; y++) {
72576845Ssobomax			for (x = 0; x < shot.xsize; x++) {
72676845Ssobomax				ch = shot.buf[x + (y * shot.xsize)];
72776845Ssobomax				ch &= 0xff;
72876845Ssobomax				if (isprint(ch) == 0)
72976845Ssobomax					ch = ' ';
73076845Ssobomax				line[x] = (char)ch;
73176845Ssobomax			}
73276845Ssobomax
73376845Ssobomax			/* Trim trailing spaces */
73476845Ssobomax			do {
73576845Ssobomax				line[x--] = '\0';
73676845Ssobomax			} while (line[x] == ' ' && x != 0);
73776845Ssobomax
73876845Ssobomax			puts(line);
73976845Ssobomax		}
74076845Ssobomax		fflush(stdout);
74176845Ssobomax	}
74276845Ssobomax
74376845Ssobomax	return;
74476845Ssobomax}
74576845Ssobomax
74677329Sdesvoid
74777329Sdesset_history(char *opt)
74877329Sdes{
74977329Sdes	int size;
75077329Sdes
75177329Sdes	size = atoi(opt);
75277329Sdes	if ((*opt == '\0') || size < 0) {
75377329Sdes		warnx("argument must be a positive number");
75477329Sdes		return;
75577329Sdes	}
75677329Sdes	if (ioctl(0, CONS_HISTORY, &size) == -1)
75777329Sdes		warn("setting history buffer size");
75877329Sdes}
75977329Sdes
76077329Sdesvoid
76177329Sdesclear_history()
76277329Sdes{
76377329Sdes
76477329Sdes	if (ioctl(0, CONS_CLRHIST) == -1)
76577329Sdes		warn("clear history buffer");
76677329Sdes}
76777329Sdes
76823457Sbrianint
7692089Ssosmain(int argc, char **argv)
7702089Ssos{
77175344Ssobomax	char	*font, *type;
772102111Ssobomax	int	dumpmod, dumpopt, opt;
773120201Seivind	int	reterr;
7742089Ssos
7752089Ssos	info.size = sizeof(info);
77676897Ssobomax	if (argc == 1)
77776897Ssobomax		usage();
77876897Ssobomax		/* Not reached */
77930764Scharnier	if (ioctl(0, CONS_GETINFO, &info) < 0)
78030764Scharnier		err(1, "must be on a virtual console");
781102111Ssobomax	dumpmod = 0;
782102111Ssobomax	dumpopt = DUMP_FBF;
783102111Ssobomax	while((opt = getopt(argc, argv, "b:Cc:df:g:h:Hi:l:LM:m:pPr:S:s:t:x")) != -1)
7842089Ssos		switch(opt) {
78577329Sdes		case 'b':
78677329Sdes			set_border_color(optarg);
78777329Sdes			break;
78877329Sdes		case 'C':
78977329Sdes			clear_history();
79077329Sdes			break;
79177329Sdes		case 'c':
79277329Sdes			set_cursor_type(optarg);
79377329Sdes			break;
79477329Sdes		case 'd':
79577329Sdes			print_scrnmap();
79677329Sdes			break;
79777329Sdes		case 'f':
79877329Sdes			type = optarg;
79977329Sdes			font = nextarg(argc, argv, &optind, 'f', 0);
80077329Sdes			if (font == NULL) {
80177329Sdes				type = NULL;
80277329Sdes				font = optarg;
80377329Sdes			}
80477329Sdes			load_font(type, font);
80577329Sdes			break;
80677329Sdes		case 'g':
80777329Sdes			if (sscanf(optarg, "%dx%d", &vesa_cols,
80877329Sdes			    &vesa_rows) != 2) {
80977329Sdes				warnx("incorrect geometry: %s", optarg);
8102089Ssos				usage();
81177329Sdes			}
81277329Sdes			break;
81377329Sdes		case 'h':
81477329Sdes			set_history(optarg);
81577329Sdes			break;
816102111Ssobomax		case 'H':
817102111Ssobomax			dumpopt = DUMP_ALL;
818102111Ssobomax			break;
81977329Sdes		case 'i':
82077329Sdes			show_info(optarg);
82177329Sdes			break;
82277329Sdes		case 'l':
82377329Sdes			load_scrnmap(optarg);
82477329Sdes			break;
82577329Sdes		case 'L':
82677329Sdes			load_default_scrnmap();
82777329Sdes			break;
82877329Sdes		case 'M':
82977329Sdes			set_mouse_char(optarg);
83077329Sdes			break;
83177329Sdes		case 'm':
83277329Sdes			set_mouse(optarg);
83377329Sdes			break;
83477329Sdes		case 'p':
835102111Ssobomax			dumpmod = DUMP_FMT_RAW;
83677329Sdes			break;
83777329Sdes		case 'P':
838102111Ssobomax			dumpmod = DUMP_FMT_TXT;
83977329Sdes			break;
84077329Sdes		case 'r':
84177329Sdes			set_reverse_colors(argc, argv, &optind);
84277329Sdes			break;
84399705Sdd		case 'S':
84499705Sdd			set_lockswitch(optarg);
84599705Sdd			break;
84677329Sdes		case 's':
84777329Sdes			set_console(optarg);
84877329Sdes			break;
84977329Sdes		case 't':
85077329Sdes			set_screensaver_timeout(optarg);
85177329Sdes			break;
85277329Sdes		case 'x':
85377329Sdes			hex = 1;
85477329Sdes			break;
85577329Sdes		default:
85677329Sdes			usage();
8572089Ssos		}
858102111Ssobomax	if (dumpmod != 0)
859102111Ssobomax		dump_screen(dumpmod, dumpopt);
860120201Seivind	reterr = video_mode(argc, argv, &optind);
86123457Sbrian	set_normal_colors(argc, argv, &optind);
8622089Ssos	if (optind < argc && !strcmp(argv[optind], "show")) {
8632089Ssos		test_frame();
8642089Ssos		optind++;
8652089Ssos	}
86630764Scharnier	if ((optind != argc) || (argc == 1))
8672089Ssos		usage();
868120201Seivind	return reterr;
8692089Ssos}
8702089Ssos
871