vidcontrol.c revision 205855
1128032Stjr/*-
21573Srgrimes * Copyright (c) 1994-1996 S�ren Schmidt
31573Srgrimes * All rights reserved.
41573Srgrimes *
51573Srgrimes * Portions of this software are based in part on the work of
61573Srgrimes * Sascha Wildner <saw@online.de> contributed to The DragonFly Project
71573Srgrimes *
81573Srgrimes * Redistribution and use in source and binary forms, with or without
91573Srgrimes * modification, are permitted provided that the following conditions
101573Srgrimes * are met:
111573Srgrimes * 1. Redistributions of source code must retain the above copyright
121573Srgrimes *    notice, this list of conditions and the following disclaimer,
131573Srgrimes *    in this position and unchanged.
141573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151573Srgrimes *    notice, this list of conditions and the following disclaimer in the
161573Srgrimes *    documentation and/or other materials provided with the distribution.
171573Srgrimes * 3. The name of the author may not be used to endorse or promote products
181573Srgrimes *    derived from this software without specific prior written permission
191573Srgrimes *
201573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
211573Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
221573Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
231573Srgrimes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
241573Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
251573Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
261573Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
271573Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
281573Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
291573Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
301573Srgrimes *
311573Srgrimes * $DragonFly: src/usr.sbin/vidcontrol/vidcontrol.c,v 1.10 2005/03/02 06:08:29 joerg Exp $
321573Srgrimes */
3350476Speter
341573Srgrimes#ifndef lint
35128032Stjrstatic const char rcsid[] =
361573Srgrimes  "$FreeBSD: head/usr.sbin/vidcontrol/vidcontrol.c 205855 2010-03-29 20:12:44Z jkim $";
371573Srgrimes#endif /* not lint */
381573Srgrimes
39123222Stjr#include <ctype.h>
40123222Stjr#include <err.h>
4159460Sphantom#include <limits.h>
4259460Sphantom#include <stdio.h>
431573Srgrimes#include <stdlib.h>
44123222Stjr#include <string.h>
4584306Sru#include <unistd.h>
46123222Stjr#include <sys/fbio.h>
471573Srgrimes#include <sys/consio.h>
48123222Stjr#include <sys/errno.h>
491573Srgrimes#include <sys/types.h>
50131365Sru#include <sys/stat.h>
511573Srgrimes#include "path.h"
52123222Stjr#include "decode.h"
53123222Stjr
54123222Stjr
551573Srgrimes#define	DATASIZE(x)	((x).w * (x).h * 256 / 8)
561573Srgrimes
571573Srgrimes/* Screen dump modes */
58131365Sru#define DUMP_FMT_RAW	1
591573Srgrimes#define DUMP_FMT_TXT	2
601573Srgrimes/* Screen dump options */
61131365Sru#define DUMP_FBF	0
621573Srgrimes#define DUMP_ALL	1
6381251Sru/* Screen dump file format revision */
64102767Stjr#define DUMP_FMT_REV	1
651573Srgrimes
661573Srgrimeschar 	legal_colors[16][16] = {
671573Srgrimes	"black", "blue", "green", "cyan",
681573Srgrimes	"red", "magenta", "brown", "white",
691573Srgrimes	"grey", "lightblue", "lightgreen", "lightcyan",
701573Srgrimes	"lightred", "lightmagenta", "yellow", "lightwhite"
711573Srgrimes};
721573Srgrimes
731573Srgrimesstruct {
74131365Sru	int			active_vty;
751573Srgrimes	vid_info_t		console_info;
761573Srgrimes	unsigned char		screen_map[256];
771573Srgrimes	int			video_mode_number;
781573Srgrimes	struct video_info	video_mode_info;
791573Srgrimes} cur_info;
801573Srgrimes
811573Srgrimesint	hex = 0;
821573Srgrimesint	number;
831573Srgrimesint	vesa_cols;
841573Srgrimesint	vesa_rows;
851573Srgrimesint	font_height;
861573Srgrimesint	colors_changed;
871573Srgrimesint	video_mode_changed;
88131365Sruint	normal_fore_color, normal_back_color;
89131365Sruint	revers_fore_color, revers_back_color;
90131365Sruchar	letter;
911573Srgrimesstruct	vid_info info;
92131365Srustruct	video_info new_mode_info;
93123222Stjr
94131365Sru
95123222Stjr/*
96123222Stjr * Initialize revert data.
97123222Stjr *
98131365Sru * NOTE: the following parameters are not yet saved/restored:
991573Srgrimes *
1001573Srgrimes *   screen saver timeout
1011573Srgrimes *   cursor type
1021573Srgrimes *   mouse character and mouse show/hide state
1031573Srgrimes *   vty switching on/off state
1041573Srgrimes *   history buffer size
1051573Srgrimes *   history contents
1061573Srgrimes *   font maps
10781251Sru */
1081573Srgrimes
1091573Srgrimesstatic void
11081251Sruinit(void)
1111573Srgrimes{
1121573Srgrimes	if (ioctl(0, VT_GETACTIVE, &cur_info.active_vty) == -1)
1131573Srgrimes		errc(1, errno, "getting active vty");
114123222Stjr
115123222Stjr	cur_info.console_info.size = sizeof(cur_info.console_info);
116123222Stjr
117131365Sru	if (ioctl(0, CONS_GETINFO, &cur_info.console_info) == -1)
118131365Sru		errc(1, errno, "getting console information");
119131365Sru
120131365Sru	if (ioctl(0, GIO_SCRNMAP, &cur_info.screen_map) == -1)
121131365Sru		errc(1, errno, "getting screen map");
122131365Sru
123131365Sru	if (ioctl(0, CONS_GET, &cur_info.video_mode_number) == -1)
124131365Sru		errc(1, errno, "getting video mode number");
125131365Sru
126131365Sru	cur_info.video_mode_info.vi_mode = cur_info.video_mode_number;
127131365Sru
128123222Stjr	if (ioctl(0, CONS_MODEINFO, &cur_info.video_mode_info) == -1)
129110440Scharnier		errc(1, errno, "getting video mode parameters");
130123222Stjr
131131365Sru	normal_fore_color = cur_info.console_info.mv_norm.fore;
132123222Stjr	normal_back_color = cur_info.console_info.mv_norm.back;
133118719Stjr	revers_fore_color = cur_info.console_info.mv_rev.fore;
134122730Stjr	revers_back_color = cur_info.console_info.mv_rev.back;
135118719Stjr}
136122290Stjr
137118725Stjr
138118719Stjr/*
139104828Stjr * If something goes wrong along the way we call revert() to go back to the
1401573Srgrimes * console state we came from (which is assumed to be working).
141123222Stjr *
142128032Stjr * NOTE: please also read the comments of init().
143 */
144
145static void
146revert(void)
147{
148	int size[3];
149
150	ioctl(0, VT_ACTIVATE, cur_info.active_vty);
151
152	fprintf(stderr, "\033[=%dA", cur_info.console_info.mv_ovscan);
153	fprintf(stderr, "\033[=%dF", cur_info.console_info.mv_norm.fore);
154	fprintf(stderr, "\033[=%dG", cur_info.console_info.mv_norm.back);
155	fprintf(stderr, "\033[=%dH", cur_info.console_info.mv_rev.fore);
156	fprintf(stderr, "\033[=%dI", cur_info.console_info.mv_rev.back);
157
158	ioctl(0, PIO_SCRNMAP, &cur_info.screen_map);
159
160	if (cur_info.video_mode_number >= M_VESA_BASE)
161		ioctl(0, _IO('V', cur_info.video_mode_number - M_VESA_BASE),
162		      NULL);
163	else
164		ioctl(0, _IO('S', cur_info.video_mode_number), NULL);
165
166	if (cur_info.video_mode_info.vi_flags & V_INFO_GRAPHICS) {
167		size[0] = cur_info.video_mode_info.vi_width / 8;
168		size[1] = cur_info.video_mode_info.vi_height /
169			  cur_info.console_info.font_size;
170		size[2] = cur_info.console_info.font_size;
171
172		ioctl(0, KDRASTER, size);
173	}
174}
175
176
177/*
178 * Print a short usage string describing all options, then exit.
179 */
180
181static void
182usage(void)
183{
184	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
185"usage: vidcontrol [-CdHLPpx] [-b color] [-c appearance] [-f [size] file]",
186"                  [-g geometry] [-h size] [-i adapter | mode] [-l screen_map]",
187"                  [-M char] [-m on | off] [-r foreground background]",
188"                  [-S on | off] [-s number] [-T xterm | cons25] [-t N | off]",
189"                  [mode] [foreground [background]] [show]");
190	exit(1);
191}
192
193
194/*
195 * Retrieve the next argument from the command line (for options that require
196 * more than one argument).
197 */
198
199static char *
200nextarg(int ac, char **av, int *indp, int oc, int strict)
201{
202	if (*indp < ac)
203		return(av[(*indp)++]);
204
205	if (strict != 0) {
206		revert();
207		errx(1, "option requires two arguments -- %c", oc);
208	}
209
210	return(NULL);
211}
212
213
214/*
215 * Guess which file to open. Try to open each combination of a specified set
216 * of file name components.
217 */
218
219static FILE *
220openguess(const char *a[], const char *b[], const char *c[], const char *d[], char **name)
221{
222	FILE *f;
223	int i, j, k, l;
224
225	for (i = 0; a[i] != NULL; i++) {
226		for (j = 0; b[j] != NULL; j++) {
227			for (k = 0; c[k] != NULL; k++) {
228				for (l = 0; d[l] != NULL; l++) {
229					asprintf(name, "%s%s%s%s",
230						 a[i], b[j], c[k], d[l]);
231
232					f = fopen(*name, "r");
233
234					if (f != NULL)
235						return (f);
236
237					free(*name);
238				}
239			}
240		}
241	}
242	return (NULL);
243}
244
245
246/*
247 * Load a screenmap from a file and set it.
248 */
249
250static void
251load_scrnmap(const char *filename)
252{
253	FILE *fd;
254	int size;
255	char *name;
256	scrmap_t scrnmap;
257	const char *a[] = {"", SCRNMAP_PATH, NULL};
258	const char *b[] = {filename, NULL};
259	const char *c[] = {"", ".scm", NULL};
260	const char *d[] = {"", NULL};
261
262	fd = openguess(a, b, c, d, &name);
263
264	if (fd == NULL) {
265		revert();
266		errx(1, "screenmap file not found");
267	}
268
269	size = sizeof(scrnmap);
270
271	if (decode(fd, (char *)&scrnmap, size) != size) {
272		rewind(fd);
273
274		if (fread(&scrnmap, 1, size, fd) != (size_t)size) {
275			warnx("bad screenmap file");
276			fclose(fd);
277			revert();
278			errx(1, "bad screenmap file");
279		}
280	}
281
282	if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
283		revert();
284		errc(1, errno, "loading screenmap");
285	}
286
287	fclose(fd);
288}
289
290
291/*
292 * Set the default screenmap.
293 */
294
295static void
296load_default_scrnmap(void)
297{
298	scrmap_t scrnmap;
299	int i;
300
301	for (i=0; i<256; i++)
302		*((char*)&scrnmap + i) = i;
303
304	if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
305		revert();
306		errc(1, errno, "loading default screenmap");
307	}
308}
309
310
311/*
312 * Print the current screenmap to stdout.
313 */
314
315static void
316print_scrnmap(void)
317{
318	unsigned char map[256];
319	size_t i;
320
321	if (ioctl(0, GIO_SCRNMAP, &map) == -1) {
322		revert();
323		errc(1, errno, "getting screenmap");
324	}
325	for (i=0; i<sizeof(map); i++) {
326		if (i != 0 && i % 16 == 0)
327			fprintf(stdout, "\n");
328
329		if (hex != 0)
330			fprintf(stdout, " %02x", map[i]);
331		else
332			fprintf(stdout, " %03d", map[i]);
333	}
334	fprintf(stdout, "\n");
335
336}
337
338
339/*
340 * Determine a file's size.
341 */
342
343static int
344fsize(FILE *file)
345{
346	struct stat sb;
347
348	if (fstat(fileno(file), &sb) == 0)
349		return sb.st_size;
350	else
351		return -1;
352}
353
354
355/*
356 * Load a font from file and set it.
357 */
358
359static void
360load_font(const char *type, const char *filename)
361{
362	FILE	*fd;
363	int	h, i, size, w;
364	unsigned long io = 0;	/* silence stupid gcc(1) in the Wall mode */
365	char	*name, *fontmap, size_sufx[6];
366	const char	*a[] = {"", FONT_PATH, NULL};
367	const char	*b[] = {filename, NULL};
368	const char	*c[] = {"", size_sufx, NULL};
369	const char	*d[] = {"", ".fnt", NULL};
370	vid_info_t _info;
371
372	struct sizeinfo {
373		int w;
374		int h;
375		unsigned long io;
376	} sizes[] = {{8, 16, PIO_FONT8x16},
377		     {8, 14, PIO_FONT8x14},
378		     {8,  8,  PIO_FONT8x8},
379		     {0,  0,            0}};
380
381	_info.size = sizeof(_info);
382	if (ioctl(0, CONS_GETINFO, &_info) == -1) {
383		revert();
384		warn("failed to obtain current video mode parameters");
385		return;
386	}
387
388	snprintf(size_sufx, sizeof(size_sufx), "-8x%d", _info.font_size);
389	fd = openguess(a, b, c, d, &name);
390
391	if (fd == NULL) {
392		revert();
393		errx(1, "%s: can't load font file", filename);
394	}
395
396	if (type != NULL) {
397		size = 0;
398		if (sscanf(type, "%dx%d", &w, &h) == 2) {
399			for (i = 0; sizes[i].w != 0; i++) {
400				if (sizes[i].w == w && sizes[i].h == h) {
401					size = DATASIZE(sizes[i]);
402					io = sizes[i].io;
403					font_height = sizes[i].h;
404				}
405			}
406		}
407		if (size == 0) {
408			fclose(fd);
409			revert();
410			errx(1, "%s: bad font size specification", type);
411		}
412	} else {
413		/* Apply heuristics */
414
415		int j;
416		int dsize[2];
417
418		size = DATASIZE(sizes[0]);
419		fontmap = (char*) malloc(size);
420		dsize[0] = decode(fd, fontmap, size);
421		dsize[1] = fsize(fd);
422		free(fontmap);
423
424		size = 0;
425		for (j = 0; j < 2; j++) {
426			for (i = 0; sizes[i].w != 0; i++) {
427				if (DATASIZE(sizes[i]) == dsize[j]) {
428					size = dsize[j];
429					io = sizes[i].io;
430					font_height = sizes[i].h;
431					j = 2;	/* XXX */
432					break;
433				}
434			}
435		}
436
437		if (size == 0) {
438			fclose(fd);
439			revert();
440			errx(1, "%s: can't guess font size", filename);
441		}
442
443		rewind(fd);
444	}
445
446	fontmap = (char*) malloc(size);
447
448	if (decode(fd, fontmap, size) != size) {
449		rewind(fd);
450		if (fsize(fd) != size ||
451		    fread(fontmap, 1, size, fd) != (size_t)size) {
452			warnx("%s: bad font file", filename);
453			fclose(fd);
454			free(fontmap);
455			revert();
456			errx(1, "%s: bad font file", filename);
457		}
458	}
459
460	if (ioctl(0, io, fontmap) == -1) {
461		revert();
462		errc(1, errno, "loading font");
463	}
464
465	fclose(fd);
466	free(fontmap);
467}
468
469
470/*
471 * Set the timeout for the screensaver.
472 */
473
474static void
475set_screensaver_timeout(char *arg)
476{
477	int nsec;
478
479	if (!strcmp(arg, "off")) {
480		nsec = 0;
481	} else {
482		nsec = atoi(arg);
483
484		if ((*arg == '\0') || (nsec < 1)) {
485			revert();
486			errx(1, "argument must be a positive number");
487		}
488	}
489
490	if (ioctl(0, CONS_BLANKTIME, &nsec) == -1) {
491		revert();
492		errc(1, errno, "setting screensaver period");
493	}
494}
495
496
497/*
498 * Set the cursor's shape/type.
499 */
500
501static void
502set_cursor_type(char *appearence)
503{
504	int type;
505
506	if (!strcmp(appearence, "normal"))
507		type = 0;
508	else if (!strcmp(appearence, "blink"))
509		type = 1;
510	else if (!strcmp(appearence, "destructive"))
511		type = 3;
512	else {
513		revert();
514		errx(1, "argument to -c must be normal, blink or destructive");
515	}
516
517	if (ioctl(0, CONS_CURSORTYPE, &type) == -1) {
518		revert();
519		errc(1, errno, "setting cursor type");
520	}
521}
522
523
524/*
525 * Set the video mode.
526 */
527
528static int
529video_mode(int argc, char **argv, int *mode_index)
530{
531	static struct {
532		const char *name;
533		unsigned long mode;
534		unsigned long mode_num;
535	} modes[] = {
536		{ "80x25",        SW_TEXT_80x25,   M_TEXT_80x25 },
537		{ "80x30",        SW_TEXT_80x30,   M_TEXT_80x30 },
538		{ "80x43",        SW_TEXT_80x43,   M_TEXT_80x43 },
539		{ "80x50",        SW_TEXT_80x50,   M_TEXT_80x50 },
540		{ "80x60",        SW_TEXT_80x60,   M_TEXT_80x60 },
541		{ "132x25",       SW_TEXT_132x25,  M_TEXT_132x25 },
542		{ "132x30",       SW_TEXT_132x30,  M_TEXT_132x30 },
543		{ "132x43",       SW_TEXT_132x43,  M_TEXT_132x43 },
544		{ "132x50",       SW_TEXT_132x50,  M_TEXT_132x50 },
545		{ "132x60",       SW_TEXT_132x60,  M_TEXT_132x60 },
546		{ "VGA_40x25",    SW_VGA_C40x25,   M_VGA_C40x25 },
547		{ "VGA_80x25",    SW_VGA_C80x25,   M_VGA_C80x25 },
548		{ "VGA_80x30",    SW_VGA_C80x30,   M_VGA_C80x30 },
549		{ "VGA_80x50",    SW_VGA_C80x50,   M_VGA_C80x50 },
550		{ "VGA_80x60",    SW_VGA_C80x60,   M_VGA_C80x60 },
551#ifdef SW_VGA_C90x25
552		{ "VGA_90x25",    SW_VGA_C90x25,   M_VGA_C90x25 },
553		{ "VGA_90x30",    SW_VGA_C90x30,   M_VGA_C90x30 },
554		{ "VGA_90x43",    SW_VGA_C90x43,   M_VGA_C90x43 },
555		{ "VGA_90x50",    SW_VGA_C90x50,   M_VGA_C90x50 },
556		{ "VGA_90x60",    SW_VGA_C90x60,   M_VGA_C90x60 },
557#endif
558		{ "VGA_320x200",	SW_VGA_CG320,	M_CG320 },
559		{ "EGA_80x25",		SW_ENH_C80x25,	M_ENH_C80x25 },
560		{ "EGA_80x43",		SW_ENH_C80x43,	M_ENH_C80x43 },
561		{ "VESA_132x25",	SW_VESA_C132x25,M_VESA_C132x25 },
562		{ "VESA_132x43",	SW_VESA_C132x43,M_VESA_C132x43 },
563		{ "VESA_132x50",	SW_VESA_C132x50,M_VESA_C132x50 },
564		{ "VESA_132x60",	SW_VESA_C132x60,M_VESA_C132x60 },
565		{ "VESA_800x600",	SW_VESA_800x600,M_VESA_800x600 },
566		{ NULL, 0, 0 },
567	};
568
569	int new_mode_num = 0;
570	unsigned long mode = 0;
571	int cur_mode;
572	int ioerr;
573	int size[3];
574	int i;
575
576	if (ioctl(0, CONS_GET, &cur_mode) < 0)
577		err(1, "cannot get the current video mode");
578
579	/*
580	 * Parse the video mode argument...
581	 */
582
583	if (*mode_index < argc) {
584		if (!strncmp(argv[*mode_index], "MODE_", 5)) {
585			if (!isdigit(argv[*mode_index][5]))
586				errx(1, "invalid video mode number");
587
588			new_mode_num = atoi(&argv[*mode_index][5]);
589		} else {
590			for (i = 0; modes[i].name != NULL; ++i) {
591				if (!strcmp(argv[*mode_index], modes[i].name)) {
592					mode = modes[i].mode;
593					new_mode_num = modes[i].mode_num;
594					break;
595				}
596			}
597
598			if (modes[i].name == NULL)
599				return EXIT_FAILURE;
600			if (ioctl(0, mode, NULL) < 0) {
601				warn("cannot set videomode");
602				return EXIT_FAILURE;
603			}
604		}
605
606		/*
607		 * Collect enough information about the new video mode...
608		 */
609
610		new_mode_info.vi_mode = new_mode_num;
611
612		if (ioctl(0, CONS_MODEINFO, &new_mode_info) == -1) {
613			revert();
614			errc(1, errno, "obtaining new video mode parameters");
615		}
616
617		if (mode == 0) {
618			if (new_mode_num >= M_VESA_BASE)
619				mode = _IO('V', new_mode_num - M_VESA_BASE);
620			else
621				mode = _IO('S', new_mode_num);
622		}
623
624		/*
625		 * Try setting the new mode.
626		 */
627
628		if (ioctl(0, mode, NULL) == -1) {
629			revert();
630			errc(1, errno, "setting video mode");
631		}
632
633		/*
634		 * For raster modes it's not enough to just set the mode.
635		 * We also need to explicitly set the raster mode.
636		 */
637
638		if (new_mode_info.vi_flags & V_INFO_GRAPHICS) {
639			/* font size */
640
641			if (font_height == 0)
642				font_height = cur_info.console_info.font_size;
643
644			size[2] = font_height;
645
646			/* adjust columns */
647
648			if ((vesa_cols * 8 > new_mode_info.vi_width) ||
649			    (vesa_cols <= 0)) {
650				size[0] = new_mode_info.vi_width / 8;
651			} else {
652				size[0] = vesa_cols;
653			}
654
655			/* adjust rows */
656
657			if ((vesa_rows * font_height > new_mode_info.vi_height) ||
658			    (vesa_rows <= 0)) {
659				size[1] = new_mode_info.vi_height /
660					  font_height;
661			} else {
662				size[1] = vesa_rows;
663			}
664
665			/* set raster mode */
666
667			if (ioctl(0, KDRASTER, size)) {
668				ioerr = errno;
669				if (cur_mode >= M_VESA_BASE)
670					ioctl(0,
671					    _IO('V', cur_mode - M_VESA_BASE),
672					    NULL);
673				else
674					ioctl(0, _IO('S', cur_mode), NULL);
675				revert();
676				warnc(ioerr, "cannot activate raster display");
677				return EXIT_FAILURE;
678			}
679		}
680
681		video_mode_changed = 1;
682
683		(*mode_index)++;
684	}
685	return EXIT_SUCCESS;
686}
687
688
689/*
690 * Return the number for a specified color name.
691 */
692
693static int
694get_color_number(char *color)
695{
696	int i;
697
698	for (i=0; i<16; i++) {
699		if (!strcmp(color, legal_colors[i]))
700			return i;
701	}
702	return -1;
703}
704
705
706/*
707 * Get normal text and background colors.
708 */
709
710static void
711get_normal_colors(int argc, char **argv, int *_index)
712{
713	int color;
714
715	if (*_index < argc && (color = get_color_number(argv[*_index])) != -1) {
716		(*_index)++;
717		fprintf(stderr, "\033[=%dF", color);
718		normal_fore_color=color;
719		colors_changed = 1;
720		if (*_index < argc
721		    && (color = get_color_number(argv[*_index])) != -1
722		    && color < 8) {
723			(*_index)++;
724			fprintf(stderr, "\033[=%dG", color);
725			normal_back_color=color;
726		}
727	}
728}
729
730
731/*
732 * Get reverse text and background colors.
733 */
734
735static void
736get_reverse_colors(int argc, char **argv, int *_index)
737{
738	int color;
739
740	if ((color = get_color_number(argv[*(_index)-1])) != -1) {
741		fprintf(stderr, "\033[=%dH", color);
742		revers_fore_color=color;
743		colors_changed = 1;
744		if (*_index < argc
745		    && (color = get_color_number(argv[*_index])) != -1
746		    && color < 8) {
747			(*_index)++;
748			fprintf(stderr, "\033[=%dI", color);
749			revers_back_color=color;
750		}
751	}
752}
753
754
755/*
756 * Set normal and reverse foreground and background colors.
757 */
758
759static void
760set_colors(void)
761{
762	fprintf(stderr, "\033[=%dF", normal_fore_color);
763	fprintf(stderr, "\033[=%dG", normal_back_color);
764	fprintf(stderr, "\033[=%dH", revers_fore_color);
765	fprintf(stderr, "\033[=%dI", revers_back_color);
766}
767
768
769/*
770 * Switch to virtual terminal #arg.
771 */
772
773static void
774set_console(char *arg)
775{
776	int n;
777
778	if(!arg || strspn(arg,"0123456789") != strlen(arg)) {
779		revert();
780		errx(1, "bad console number");
781	}
782
783	n = atoi(arg);
784
785	if (n < 1 || n > 16) {
786		revert();
787		errx(1, "console number out of range");
788	} else if (ioctl(0, VT_ACTIVATE, n) == -1) {
789		revert();
790		errc(1, errno, "switching vty");
791	}
792}
793
794
795/*
796 * Sets the border color.
797 */
798
799static void
800set_border_color(char *arg)
801{
802	int color;
803
804	if ((color = get_color_number(arg)) != -1) {
805		fprintf(stderr, "\033[=%dA", color);
806	}
807	else
808		usage();
809}
810
811static void
812set_mouse_char(char *arg)
813{
814	struct mouse_info mouse;
815	long l;
816
817	l = strtol(arg, NULL, 0);
818
819	if ((l < 0) || (l > UCHAR_MAX - 3)) {
820		revert();
821		warnx("argument to -M must be 0 through %d", UCHAR_MAX - 3);
822		return;
823	}
824
825	mouse.operation = MOUSE_MOUSECHAR;
826	mouse.u.mouse_char = (int)l;
827
828	if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) {
829		revert();
830		errc(1, errno, "setting mouse character");
831	}
832}
833
834
835/*
836 * Show/hide the mouse.
837 */
838
839static void
840set_mouse(char *arg)
841{
842	struct mouse_info mouse;
843
844	if (!strcmp(arg, "on")) {
845		mouse.operation = MOUSE_SHOW;
846	} else if (!strcmp(arg, "off")) {
847		mouse.operation = MOUSE_HIDE;
848	} else {
849		revert();
850		errx(1, "argument to -m must be either on or off");
851	}
852
853	if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) {
854		revert();
855		errc(1, errno, "%sing the mouse",
856		     mouse.operation == MOUSE_SHOW ? "show" : "hid");
857	}
858}
859
860
861static void
862set_lockswitch(char *arg)
863{
864	int data;
865
866	if (!strcmp(arg, "off")) {
867		data = 0x01;
868	} else if (!strcmp(arg, "on")) {
869		data = 0x02;
870	} else {
871		revert();
872		errx(1, "argument to -S must be either on or off");
873	}
874
875	if (ioctl(0, VT_LOCKSWITCH, &data) == -1) {
876		revert();
877		errc(1, errno, "turning %s vty switching",
878		     data == 0x01 ? "off" : "on");
879	}
880}
881
882
883/*
884 * Return the adapter name for a specified type.
885 */
886
887static const char
888*adapter_name(int type)
889{
890    static struct {
891	int type;
892	const char *name;
893    } names[] = {
894	{ KD_MONO,	"MDA" },
895	{ KD_HERCULES,	"Hercules" },
896	{ KD_CGA,	"CGA" },
897	{ KD_EGA,	"EGA" },
898	{ KD_VGA,	"VGA" },
899	{ KD_PC98,	"PC-98xx" },
900	{ KD_TGA,	"TGA" },
901	{ -1,		"Unknown" },
902    };
903
904    int i;
905
906    for (i = 0; names[i].type != -1; ++i)
907	if (names[i].type == type)
908	    break;
909    return names[i].name;
910}
911
912
913/*
914 * Show graphics adapter information.
915 */
916
917static void
918show_adapter_info(void)
919{
920	struct video_adapter_info ad;
921
922	ad.va_index = 0;
923
924	if (ioctl(0, CONS_ADPINFO, &ad) == -1) {
925		revert();
926		errc(1, errno, "obtaining adapter information");
927	}
928
929	printf("fb%d:\n", ad.va_index);
930	printf("    %.*s%d, type:%s%s (%d), flags:0x%x\n",
931	       (int)sizeof(ad.va_name), ad.va_name, ad.va_unit,
932	       (ad.va_flags & V_ADP_VESA) ? "VESA " : "",
933	       adapter_name(ad.va_type), ad.va_type, ad.va_flags);
934	printf("    initial mode:%d, current mode:%d, BIOS mode:%d\n",
935	       ad.va_initial_mode, ad.va_mode, ad.va_initial_bios_mode);
936	printf("    frame buffer window:0x%zx, buffer size:0x%zx\n",
937	       ad.va_window, ad.va_buffer_size);
938	printf("    window size:0x%zx, origin:0x%x\n",
939	       ad.va_window_size, ad.va_window_orig);
940	printf("    display start address (%d, %d), scan line width:%d\n",
941	       ad.va_disp_start.x, ad.va_disp_start.y, ad.va_line_width);
942	printf("    reserved:0x%zx\n", ad.va_unused0);
943}
944
945
946/*
947 * Show video mode information.
948 */
949
950static void
951show_mode_info(void)
952{
953	char buf[80];
954	struct video_info _info;
955	int c;
956	int mm;
957	int mode;
958
959	printf("    mode#     flags   type    size       "
960	       "font      window      linear buffer\n");
961	printf("---------------------------------------"
962	       "---------------------------------------\n");
963
964	for (mode = 0; mode < M_VESA_MODE_MAX; ++mode) {
965		_info.vi_mode = mode;
966		if (ioctl(0, CONS_MODEINFO, &_info))
967			continue;
968		if (_info.vi_mode != mode)
969			continue;
970
971		printf("%3d (0x%03x)", mode, mode);
972    		printf(" 0x%08x", _info.vi_flags);
973		if (_info.vi_flags & V_INFO_GRAPHICS) {
974			c = 'G';
975
976			if (_info.vi_mem_model == V_INFO_MM_PLANAR)
977				snprintf(buf, sizeof(buf), "%dx%dx%d %d",
978				    _info.vi_width, _info.vi_height,
979				    _info.vi_depth, _info.vi_planes);
980			else {
981				switch (_info.vi_mem_model) {
982				case V_INFO_MM_PACKED:
983					mm = 'P';
984					break;
985				case V_INFO_MM_DIRECT:
986					mm = 'D';
987					break;
988				case V_INFO_MM_CGA:
989					mm = 'C';
990					break;
991				case V_INFO_MM_HGC:
992					mm = 'H';
993					break;
994				case V_INFO_MM_VGAX:
995					mm = 'V';
996					break;
997				default:
998					mm = ' ';
999					break;
1000				}
1001				snprintf(buf, sizeof(buf), "%dx%dx%d %c",
1002				    _info.vi_width, _info.vi_height,
1003				    _info.vi_depth, mm);
1004			}
1005		} else {
1006			c = 'T';
1007
1008			snprintf(buf, sizeof(buf), "%dx%d",
1009				 _info.vi_width, _info.vi_height);
1010		}
1011
1012		printf(" %c %-15s", c, buf);
1013		snprintf(buf, sizeof(buf), "%dx%d",
1014			 _info.vi_cwidth, _info.vi_cheight);
1015		printf(" %-5s", buf);
1016    		printf(" 0x%05zx %2dk %2dk",
1017		       _info.vi_window, (int)_info.vi_window_size/1024,
1018		       (int)_info.vi_window_gran/1024);
1019    		printf(" 0x%08zx %dk\n",
1020		       _info.vi_buffer, (int)_info.vi_buffer_size/1024);
1021	}
1022}
1023
1024
1025static void
1026show_info(char *arg)
1027{
1028	if (!strcmp(arg, "adapter")) {
1029		show_adapter_info();
1030	} else if (!strcmp(arg, "mode")) {
1031		show_mode_info();
1032	} else {
1033		revert();
1034		errx(1, "argument to -i must be either adapter or mode");
1035	}
1036}
1037
1038
1039static void
1040test_frame(void)
1041{
1042	int i, cur_mode, fore;
1043
1044	fore = 15;
1045
1046	if (ioctl(0, CONS_GET, &cur_mode) < 0)
1047		err(1, "must be on a virtual console");
1048	switch (cur_mode) {
1049	case M_PC98_80x25:
1050	case M_PC98_80x30:
1051		fore = 7;
1052		break;
1053	}
1054
1055	fprintf(stdout, "\033[=0G\n\n");
1056	for (i=0; i<8; i++) {
1057		fprintf(stdout, "\033[=%dF\033[=0G        %2d \033[=%dF%-16s"
1058				"\033[=%dF\033[=0G        %2d \033[=%dF%-16s        "
1059				"\033[=%dF %2d \033[=%dGBACKGROUND\033[=0G\n",
1060			fore, i, i, legal_colors[i],
1061			fore, i+8, i+8, legal_colors[i+8],
1062			fore, i, i);
1063	}
1064	fprintf(stdout, "\033[=%dF\033[=%dG\033[=%dH\033[=%dI\n",
1065		info.mv_norm.fore, info.mv_norm.back,
1066		info.mv_rev.fore, info.mv_rev.back);
1067}
1068
1069
1070/*
1071 * Snapshot the video memory of that terminal, using the CONS_SCRSHOT
1072 * ioctl, and writes the results to stdout either in the special
1073 * binary format (see manual page for details), or in the plain
1074 * text format.
1075 */
1076
1077static void
1078dump_screen(int mode, int opt)
1079{
1080	scrshot_t shot;
1081	vid_info_t _info;
1082
1083	_info.size = sizeof(_info);
1084
1085	if (ioctl(0, CONS_GETINFO, &_info) == -1) {
1086		revert();
1087		errc(1, errno, "obtaining current video mode parameters");
1088		return;
1089	}
1090
1091	shot.x = shot.y = 0;
1092	shot.xsize = _info.mv_csz;
1093	shot.ysize = _info.mv_rsz;
1094	if (opt == DUMP_ALL)
1095		shot.ysize += _info.mv_hsz;
1096
1097	shot.buf = alloca(shot.xsize * shot.ysize * sizeof(u_int16_t));
1098	if (shot.buf == NULL) {
1099		revert();
1100		errx(1, "failed to allocate memory for dump");
1101	}
1102
1103	if (ioctl(0, CONS_SCRSHOT, &shot) == -1) {
1104		revert();
1105		errc(1, errno, "dumping screen");
1106	}
1107
1108	if (mode == DUMP_FMT_RAW) {
1109		printf("SCRSHOT_%c%c%c%c", DUMP_FMT_REV, 2,
1110		       shot.xsize, shot.ysize);
1111
1112		fflush(stdout);
1113
1114		write(STDOUT_FILENO, shot.buf,
1115		      shot.xsize * shot.ysize * sizeof(u_int16_t));
1116	} else {
1117		char *line;
1118		int x, y;
1119		u_int16_t ch;
1120
1121		line = alloca(shot.xsize + 1);
1122
1123		if (line == NULL) {
1124			revert();
1125			errx(1, "failed to allocate memory for line buffer");
1126		}
1127
1128		for (y = 0; y < shot.ysize; y++) {
1129			for (x = 0; x < shot.xsize; x++) {
1130				ch = shot.buf[x + (y * shot.xsize)];
1131				ch &= 0xff;
1132
1133				if (isprint(ch) == 0)
1134					ch = ' ';
1135
1136				line[x] = (char)ch;
1137			}
1138
1139			/* Trim trailing spaces */
1140
1141			do {
1142				line[x--] = '\0';
1143			} while (line[x] == ' ' && x != 0);
1144
1145			puts(line);
1146		}
1147
1148		fflush(stdout);
1149	}
1150}
1151
1152
1153/*
1154 * Set the console history buffer size.
1155 */
1156
1157static void
1158set_history(char *opt)
1159{
1160	int size;
1161
1162	size = atoi(opt);
1163
1164	if ((*opt == '\0') || size < 0) {
1165		revert();
1166		errx(1, "argument must be a positive number");
1167	}
1168
1169	if (ioctl(0, CONS_HISTORY, &size) == -1) {
1170		revert();
1171		errc(1, errno, "setting history buffer size");
1172	}
1173}
1174
1175
1176/*
1177 * Clear the console history buffer.
1178 */
1179
1180static void
1181clear_history(void)
1182{
1183	if (ioctl(0, CONS_CLRHIST) == -1) {
1184		revert();
1185		errc(1, errno, "clearing history buffer");
1186	}
1187}
1188
1189static void
1190set_terminal_mode(char *arg)
1191{
1192
1193	if (strcmp(arg, "xterm") == 0)
1194		fprintf(stderr, "\033[=T");
1195	else if (strcmp(arg, "cons25") == 0)
1196		fprintf(stderr, "\033[=1T");
1197	else
1198		usage();
1199}
1200
1201
1202int
1203main(int argc, char **argv)
1204{
1205	char	*font, *type;
1206	int	dumpmod, dumpopt, opt;
1207	int	reterr;
1208
1209	init();
1210
1211	info.size = sizeof(info);
1212
1213	if (ioctl(0, CONS_GETINFO, &info) == -1)
1214		err(1, "must be on a virtual console");
1215	dumpmod = 0;
1216	dumpopt = DUMP_FBF;
1217	while ((opt = getopt(argc, argv,
1218	    "b:Cc:df:g:h:Hi:l:LM:m:pPr:S:s:T:t:x")) != -1)
1219		switch(opt) {
1220		case 'b':
1221			set_border_color(optarg);
1222			break;
1223		case 'C':
1224			clear_history();
1225			break;
1226		case 'c':
1227			set_cursor_type(optarg);
1228			break;
1229		case 'd':
1230			print_scrnmap();
1231			break;
1232		case 'f':
1233			type = optarg;
1234			font = nextarg(argc, argv, &optind, 'f', 0);
1235
1236			if (font == NULL) {
1237				type = NULL;
1238				font = optarg;
1239			}
1240
1241			load_font(type, font);
1242			break;
1243		case 'g':
1244			if (sscanf(optarg, "%dx%d",
1245			    &vesa_cols, &vesa_rows) != 2) {
1246				revert();
1247				warnx("incorrect geometry: %s", optarg);
1248				usage();
1249			}
1250                	break;
1251		case 'h':
1252			set_history(optarg);
1253			break;
1254		case 'H':
1255			dumpopt = DUMP_ALL;
1256			break;
1257		case 'i':
1258			show_info(optarg);
1259			break;
1260		case 'l':
1261			load_scrnmap(optarg);
1262			break;
1263		case 'L':
1264			load_default_scrnmap();
1265			break;
1266		case 'M':
1267			set_mouse_char(optarg);
1268			break;
1269		case 'm':
1270			set_mouse(optarg);
1271			break;
1272		case 'p':
1273			dumpmod = DUMP_FMT_RAW;
1274			break;
1275		case 'P':
1276			dumpmod = DUMP_FMT_TXT;
1277			break;
1278		case 'r':
1279			get_reverse_colors(argc, argv, &optind);
1280			break;
1281		case 'S':
1282			set_lockswitch(optarg);
1283			break;
1284		case 's':
1285			set_console(optarg);
1286			break;
1287		case 'T':
1288			set_terminal_mode(optarg);
1289			break;
1290		case 't':
1291			set_screensaver_timeout(optarg);
1292			break;
1293		case 'x':
1294			hex = 1;
1295			break;
1296		default:
1297			usage();
1298		}
1299
1300	if (dumpmod != 0)
1301		dump_screen(dumpmod, dumpopt);
1302	reterr = video_mode(argc, argv, &optind);
1303	get_normal_colors(argc, argv, &optind);
1304
1305	if (optind < argc && !strcmp(argv[optind], "show")) {
1306		test_frame();
1307		optind++;
1308	}
1309
1310	video_mode(argc, argv, &optind);
1311
1312	get_normal_colors(argc, argv, &optind);
1313
1314	if (colors_changed || video_mode_changed) {
1315		if (!(new_mode_info.vi_flags & V_INFO_GRAPHICS)) {
1316			if ((normal_back_color < 8) && (revers_back_color < 8)) {
1317				set_colors();
1318			} else {
1319				revert();
1320				errx(1, "bg color for text modes must be < 8");
1321			}
1322		} else {
1323			set_colors();
1324		}
1325	}
1326
1327	if ((optind != argc) || (argc == 1))
1328		usage();
1329	return reterr;
1330}
1331
1332