1 /*-
2  * Copyright (c) 1994-1996 S�ren Schmidt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software withough specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD: src/usr.sbin/vidcontrol/vidcontrol.c,v 1.32.2.7 2002/09/15 22:31:50 dd Exp $
29  * $DragonFly: src/usr.sbin/vidcontrol/vidcontrol.c,v 1.15 2008/11/02 21:52:46 swildner Exp $
30  */
31 
32 #include <machine/console.h>
33 
34 #include <sys/consio.h>
35 #include <sys/errno.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 
39 #include <ctype.h>
40 #include <err.h>
41 #include <limits.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 #include "path.h"
48 #include "decode.h"
49 
50 
51 #define	DATASIZE(x)	((x).w * (x).h * 256 / 8)
52 
53 #define	DUMP_RAW	0
54 #define	DUMP_TXT	1
55 
56 #define	DUMP_FMT_REV	1
57 
58 
59 char legal_colors[16][16] = {
60 	"black", "blue", "green", "cyan",
61 	"red", "magenta", "brown", "white",
62 	"grey", "lightblue", "lightgreen", "lightcyan",
63 	"lightred", "lightmagenta", "yellow", "lightwhite"
64 };
65 
66 struct {
67 	int			active_vty;
68 	vid_info_t		console_info;
69 	unsigned char		screen_map[256];
70 	int			video_mode_number;
71 	struct video_info	video_mode_info;
72 } cur_info;
73 
74 int hex = 0;
75 int number;
76 int vesa_cols;
77 int vesa_rows;
78 int font_height;
79 int colors_changed;
80 int video_mode_changed;
81 int normal_fore_color, normal_back_color;
82 int revers_fore_color, revers_back_color;
83 char letter;
84 struct vid_info info;
85 struct video_info new_mode_info;
86 
87 
88 /*
89  * Initialize revert data.
90  *
91  * NOTE: the following parameters are not yet saved/restored:
92  *
93  *   screen saver timeout
94  *   cursor type
95  *   mouse character and mouse show/hide state
96  *   vty switching on/off state
97  *   history buffer size
98  *   history contents
99  *   font maps
100  */
101 
102 static void
103 init(void)
104 {
105 	if (ioctl(0, VT_GETACTIVE, &cur_info.active_vty) == -1)
106 		errc(1, errno, "getting active vty");
107 
108 	cur_info.console_info.size = sizeof(cur_info.console_info);
109 
110 	if (ioctl(0, CONS_GETINFO, &cur_info.console_info) == -1)
111 		errc(1, errno, "getting console information");
112 
113 	if (ioctl(0, GIO_SCRNMAP, &cur_info.screen_map) == -1)
114 		errc(1, errno, "getting screen map");
115 
116 	if (ioctl(0, CONS_GET, &cur_info.video_mode_number) == -1)
117 		errc(1, errno, "getting video mode number");
118 
119 	cur_info.video_mode_info.vi_mode = cur_info.video_mode_number;
120 
121 	if (ioctl(0, CONS_MODEINFO, &cur_info.video_mode_info) == -1)
122 		errc(1, errno, "getting video mode parameters");
123 
124 	normal_fore_color = cur_info.console_info.mv_norm.fore;
125 	normal_back_color = cur_info.console_info.mv_norm.back;
126 	revers_fore_color = cur_info.console_info.mv_rev.fore;
127 	revers_back_color = cur_info.console_info.mv_rev.back;
128 }
129 
130 
131 /*
132  * If something goes wrong along the way we call revert() to go back to the
133  * console state we came from (which is assumed to be working).
134  *
135  * NOTE: please also read the comments of init().
136  */
137 
138 static void
139 revert(void)
140 {
141 	int size[3];
142 
143 	ioctl(0, VT_ACTIVATE, (caddr_t) (long) cur_info.active_vty);
144 
145 	fprintf(stderr, "[=%dA", cur_info.console_info.mv_ovscan);
146 	fprintf(stderr, "[=%dF", cur_info.console_info.mv_norm.fore);
147 	fprintf(stderr, "[=%dG", cur_info.console_info.mv_norm.back);
148 	fprintf(stderr, "[=%dH", cur_info.console_info.mv_rev.fore);
149 	fprintf(stderr, "[=%dI", cur_info.console_info.mv_rev.back);
150 
151 	ioctl(0, PIO_SCRNMAP, &cur_info.screen_map);
152 	ioctl(0, CONS_SET, &cur_info.video_mode_number);
153 
154 	if (cur_info.video_mode_info.vi_flags & V_INFO_GRAPHICS) {
155 		size[0] = cur_info.video_mode_info.vi_width / 8;
156 		size[1] = cur_info.video_mode_info.vi_height /
157 			  cur_info.console_info.font_size;
158 		size[2] = cur_info.console_info.font_size;
159 
160 		ioctl(0, KDRASTER, size);
161 	}
162 }
163 
164 
165 /*
166  * Print a short usage string describing all options, then exit.
167  */
168 
169 static void
170 usage(void)
171 {
172 	fprintf(stderr,
173 		"usage: vidcontrol [-CdLPpx] [-b color] [-c appearance]"
174 		" [-f [size] file]\n"
175 		"                  [-g geometry] [-h size] [-i adapter | mode]"
176 		" [-l screen_map]\n"
177 		"                  [-M char] [-m on | off] [-r foreground"
178 		" background]\n"
179 		"                  [-S on | off] [-s number] [-t N | off]"
180 		" [mode]\n"
181 		"                  [foreground [background]] [show]\n");
182 
183 	exit(1);
184 }
185 
186 
187 /*
188  * Retrieve the next argument from the command line (for options that require
189  * more than one argument).
190  */
191 
192 static char *
193 nextarg(int ac, char **av, int *indp, int oc, int strict)
194 {
195 	if (*indp < ac)
196 		return(av[(*indp)++]);
197 
198 	if (strict != 0) {
199 		revert();
200 		errx(1, "option requires two arguments -- %c", oc);
201 	}
202 
203 	return(NULL);
204 }
205 
206 
207 /*
208  * Guess which file to open. Try to open each combination of a specified set
209  * of file name components.
210  */
211 
212 static FILE *
213 openguess(const char *a[], const char *b[], const char *c[], const char *d[], char **name)
214 {
215 	FILE *f;
216 	int i, j, k, l;
217 
218 	for (i = 0; a[i] != NULL; i++) {
219 		for (j = 0; b[j] != NULL; j++) {
220 			for (k = 0; c[k] != NULL; k++) {
221 				for (l = 0; d[l] != NULL; l++) {
222 					asprintf(name, "%s%s%s%s",
223 						 a[i], b[j], c[k], d[l]);
224 
225 					f = fopen(*name, "r");
226 
227 					if (f != NULL)
228 						return (f);
229 
230 					free(*name);
231 				}
232 			}
233 		}
234 	}
235 
236 	return (NULL);
237 }
238 
239 
240 /*
241  * Load a screenmap from a file and set it.
242  */
243 
244 static void
245 load_scrnmap(char *filename)
246 {
247 	FILE *fd;
248 	int size;
249 	char *name;
250 	scrmap_t scrnmap;
251 	const char *a[] = {"", SCRNMAP_PATH, NULL};
252 	const char *b[] = {filename, NULL};
253 	const char *c[] = {"", ".scm", NULL};
254 	const char *d[] = {"", NULL};
255 
256 	fd = openguess(a, b, c, d, &name);
257 
258 	if (fd == NULL) {
259 		revert();
260 		errx(1, "screenmap file not found");
261 	}
262 
263 	size = sizeof(scrnmap);
264 
265 	if (decode(fd, (char *)&scrnmap, size) != size) {
266 		rewind(fd);
267 
268 		if (fread(&scrnmap, 1, size, fd) != (size_t)size) {
269 			fclose(fd);
270 			revert();
271 			errx(1, "bad screenmap file");
272 		}
273 	}
274 
275 	if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
276 		revert();
277 		errc(1, errno, "loading screenmap");
278 	}
279 
280 	fclose(fd);
281 }
282 
283 
284 /*
285  * Set the default screenmap.
286  */
287 
288 static void
289 load_default_scrnmap(void)
290 {
291 	scrmap_t scrnmap;
292 	int i;
293 
294 	for (i = 0; i < 256; i++)
295 		*((char*)&scrnmap + i) = i;
296 
297 	if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
298 		revert();
299 		errc(1, errno, "loading default screenmap");
300 	}
301 }
302 
303 
304 /*
305  * Print the current screenmap to stdout.
306  */
307 
308 static void
309 print_scrnmap(void)
310 {
311 	unsigned char map[256];
312 	size_t i;
313 
314 	if (ioctl(0, GIO_SCRNMAP, &map) == -1) {
315 		revert();
316 		errc(1, errno, "getting screenmap");
317 	}
318 
319 	for (i=0; i<sizeof(map); i++) {
320 		if (i > 0 && i % 16 == 0)
321 			fprintf(stdout, "\n");
322 
323 		if (hex != 0)
324 			fprintf(stdout, " %02x", map[i]);
325 		else
326 			fprintf(stdout, " %03d", map[i]);
327 	}
328 
329 	fprintf(stdout, "\n");
330 }
331 
332 
333 /*
334  * Determine a file's size.
335  */
336 
337 static int
338 fsize(FILE *file)
339 {
340 	struct stat sb;
341 
342 	if (fstat(fileno(file), &sb) == 0)
343 		return sb.st_size;
344 	else
345 		return -1;
346 }
347 
348 
349 /*
350  * Load a font from file and set it.
351  */
352 
353 static void
354 load_font(char *type, char *filename)
355 {
356 	FILE *fd;
357 	int h, i, size, w;
358 	unsigned long io = 0;	/* silence stupid gcc(1) in the Wall mode */
359 	char *name, *fontmap, size_sufx[6];
360 	const char *a[] = {"", FONT_PATH, NULL};
361 	const char *b[] = {filename, NULL};
362 	const char *c[] = {"", size_sufx, NULL};
363 	const char *d[] = {"", ".fnt", NULL};
364 	vid_info_t vinfo;
365 
366 	struct sizeinfo {
367 		int w;
368 		int h;
369 		unsigned long io;
370 	} sizes[] = {{8, 16, PIO_FONT8x16},
371 		     {8, 14, PIO_FONT8x14},
372 		     {8,  8, PIO_FONT8x8},
373 		     {0,  0, 0}};
374 
375 	vinfo.size = sizeof(vinfo);
376 
377 	if (ioctl(0, CONS_GETINFO, &vinfo) == -1) {
378 		revert();
379 		errc(1, errno, "obtaining current video mode parameters");
380 	}
381 
382 	snprintf(size_sufx, sizeof(size_sufx), "-8x%d", vinfo.font_size);
383 
384 	fd = openguess(a, b, c, d, &name);
385 
386 	if (fd == NULL) {
387 		revert();
388 		errx(1, "%s: can't load font file", filename);
389 	}
390 
391 	if (type != NULL) {
392 		size = 0;
393 		if (sscanf(type, "%dx%d", &w, &h) == 2) {
394 			for (i = 0; sizes[i].w != 0; i++) {
395 				if (sizes[i].w == w && sizes[i].h == h) {
396 					size = DATASIZE(sizes[i]);
397 					io = sizes[i].io;
398 					font_height = sizes[i].h;
399 				}
400 			}
401 		}
402 		if (size == 0) {
403 			fclose(fd);
404 			revert();
405 			errx(1, "%s: bad font size specification", type);
406 		}
407 	} else {
408 		/* Apply heuristics */
409 
410 		int j;
411 		int dsize[2];
412 
413 		size = DATASIZE(sizes[0]);
414 		fontmap = (char*) malloc(size);
415 		dsize[0] = decode(fd, fontmap, size);
416 		dsize[1] = fsize(fd);
417 		free(fontmap);
418 
419 		size = 0;
420 		for (j = 0; j < 2; j++) {
421 			for (i = 0; sizes[i].w != 0; i++) {
422 				if (DATASIZE(sizes[i]) == dsize[j]) {
423 					size = dsize[j];
424 					io = sizes[i].io;
425 					font_height = sizes[i].h;
426 					j = 2;	/* XXX */
427 					break;
428 				}
429 			}
430 		}
431 
432 		if (size == 0) {
433 			fclose(fd);
434 			revert();
435 			errx(1, "%s: can't guess font size", filename);
436 		}
437 
438 		rewind(fd);
439 	}
440 
441 	fontmap = (char*) malloc(size);
442 
443 	if (decode(fd, fontmap, size) != size) {
444 		rewind(fd);
445 		if (fsize(fd) != size ||
446 		    fread(fontmap, 1, size, fd) != (size_t)size) {
447 			fclose(fd);
448 			free(fontmap);
449 			revert();
450 			errx(1, "%s: bad font file", filename);
451 		}
452 	}
453 
454 	if (ioctl(0, io, fontmap) == -1) {
455 		revert();
456 		errc(1, errno, "loading font");
457 	}
458 
459 	fclose(fd);
460 	free(fontmap);
461 }
462 
463 
464 /*
465  * Set the timeout for the screensaver.
466  */
467 
468 static void
469 set_screensaver_timeout(char *arg)
470 {
471 	int nsec;
472 
473 	if (!strcmp(arg, "off")) {
474 		nsec = 0;
475 	} else {
476 		nsec = atoi(arg);
477 
478 		if ((*arg == '\0') || (nsec < 1)) {
479 			revert();
480 			errx(1, "argument must be a positive number");
481 		}
482 	}
483 
484 	if (ioctl(0, CONS_BLANKTIME, &nsec) == -1) {
485 		revert();
486 		errc(1, errno, "setting screensaver period");
487 	}
488 }
489 
490 
491 /*
492  * Set the cursor's shape/type.
493  */
494 
495 static void
496 set_cursor_type(char *appearance)
497 {
498 	int type;
499 
500 	if (!strcmp(appearance, "normal"))
501 		type = 0;
502 	else if (!strcmp(appearance, "blink"))
503 		type = 1;
504 	else if (!strcmp(appearance, "destructive"))
505 		type = 3;
506 	else {
507 		revert();
508 		errx(1, "argument to -c must be normal, blink or destructive");
509 	}
510 
511 	if (ioctl(0, CONS_CURSORTYPE, &type) == -1) {
512 		revert();
513 		errc(1, errno, "setting cursor type");
514 	}
515 }
516 
517 
518 /*
519  * Set the video mode.
520  */
521 
522 static void
523 video_mode(int argc, char **argv, int *mode_index)
524 {
525 	static struct {
526 		const char *name;
527 		unsigned long mode_num;
528 	} modes[] = {{ "80x25",		M_VGA_C80x25 },
529 		     { "80x30",		M_VGA_C80x30 },
530 		     { "80x43",		M_ENH_C80x43 },
531 		     { "80x50",		M_VGA_C80x50 },
532 		     { "80x60",		M_VGA_C80x60 },
533 		     { "132x25",	M_VESA_C132x25 },
534 		     { "132x43",	M_VESA_C132x43 },
535 		     { "132x50",	M_VESA_C132x50 },
536 		     { "132x60",	M_VESA_C132x60 },
537 		     { "VGA_40x25",	M_VGA_C40x25 },
538 		     { "VGA_80x25",	M_VGA_C80x25 },
539 		     { "VGA_80x30",	M_VGA_C80x30 },
540 		     { "VGA_80x50",	M_VGA_C80x50 },
541 		     { "VGA_80x60",	M_VGA_C80x60 },
542 #ifdef SW_VGA_C90x25
543 		     { "VGA_90x25",	M_VGA_C90x25 },
544 		     { "VGA_90x30",	M_VGA_C90x30 },
545 		     { "VGA_90x43",	M_VGA_C90x43 },
546 		     { "VGA_90x50",	M_VGA_C90x50 },
547 		     { "VGA_90x60",	M_VGA_C90x60 },
548 #endif
549 		     { "VGA_320x200",	M_CG320 },
550 		     { "EGA_80x25",	M_ENH_C80x25 },
551 		     { "EGA_80x43",	M_ENH_C80x43 },
552 		     { "VESA_132x25",	M_VESA_C132x25 },
553 		     { "VESA_132x43",	M_VESA_C132x43 },
554 		     { "VESA_132x50",	M_VESA_C132x50 },
555 		     { "VESA_132x60",	M_VESA_C132x60 },
556 		     { "VESA_800x600",	M_VESA_800x600 },
557 		     { NULL, 0 },
558 	};
559 
560 	int new_mode_num = 0;
561 	int size[3];
562 	int i;
563 
564 	/*
565 	 * Parse the video mode argument...
566 	 */
567 
568 	if (*mode_index < argc) {
569 		if (!strncmp(argv[*mode_index], "MODE_", 5)) {
570 			if (!isdigit(argv[*mode_index][5]))
571 				errx(1, "invalid video mode number");
572 
573 			new_mode_num = atoi(&argv[*mode_index][5]);
574 		} else {
575 			for (i = 0; modes[i].name != NULL; ++i) {
576 				if (!strcmp(argv[*mode_index], modes[i].name)) {
577 					new_mode_num = modes[i].mode_num;
578 					break;
579 				}
580 			}
581 
582 			if (modes[i].name == NULL)
583 				errx(1, "invalid video mode name");
584 		}
585 
586 		/*
587 		 * Collect enough information about the new video mode...
588 		 */
589 
590 		new_mode_info.vi_mode = new_mode_num;
591 
592 		if (ioctl(0, CONS_MODEINFO, &new_mode_info) == -1) {
593 			revert();
594 			errc(1, errno, "obtaining new video mode parameters");
595 		}
596 
597 		/*
598 		 * Try setting the new mode.
599 		 */
600 
601 		if (ioctl(0, CONS_SET, &new_mode_num) == -1) {
602 			revert();
603 			errc(1, errno, "setting video mode");
604 		}
605 
606 		/*
607 		 * For raster modes it's not enough to just set the mode.
608 		 * We also need to explicitly set the raster mode.
609 		 */
610 
611 		if (new_mode_info.vi_flags & V_INFO_GRAPHICS) {
612 			/* font size */
613 
614 			if (font_height == 0)
615 				font_height = cur_info.console_info.font_size;
616 
617 			size[2] = font_height;
618 
619 			/* adjust columns */
620 
621 			if ((vesa_cols * 8 > new_mode_info.vi_width) ||
622 			    (vesa_cols <= 0)) {
623 				size[0] = new_mode_info.vi_width / 8;
624 			} else {
625 				size[0] = vesa_cols;
626 			}
627 
628 			/* adjust rows */
629 
630 			if ((vesa_rows * font_height > new_mode_info.vi_height) ||
631 			    (vesa_rows <= 0)) {
632 				size[1] = new_mode_info.vi_height /
633 					  font_height;
634 			} else {
635 				size[1] = vesa_rows;
636 			}
637 
638 			/* set raster mode */
639 
640 			if (ioctl(0, KDRASTER, size)) {
641 				revert();
642 				errc(1, errno, "activating raster display");
643 			}
644 		}
645 
646 		video_mode_changed = 1;
647 
648 		(*mode_index)++;
649 	}
650 }
651 
652 
653 /*
654  * Return the number for a specified color name.
655  */
656 
657 static int
658 get_color_number(char *color)
659 {
660 	int i;
661 
662 	for (i=0; i<16; i++) {
663 		if (!strcmp(color, legal_colors[i]))
664 			return i;
665 	}
666 	return -1;
667 }
668 
669 
670 /*
671  * Get normal text and background colors.
672  */
673 
674 static void
675 get_normal_colors(int argc, char **argv, int *color_index)
676 {
677 	int color;
678 
679 	if (*color_index < argc &&
680 	    (color = get_color_number(argv[*color_index])) != -1) {
681 		(*color_index)++;
682 		normal_fore_color = color;
683 		colors_changed = 1;
684 
685 		if (*color_index < argc &&
686 		    (color = get_color_number(argv[*color_index])) != -1) {
687 			(*color_index)++;
688 			normal_back_color = color;
689 		}
690 	}
691 }
692 
693 
694 /*
695  * Get reverse text and background colors.
696  */
697 
698 static void
699 get_reverse_colors(int argc, char **argv, int *color_index)
700 {
701 	int color;
702 
703 	if ((color = get_color_number(argv[*(color_index)-1])) != -1) {
704 		revers_fore_color = color;
705 		colors_changed = 1;
706 
707 		if (*color_index < argc &&
708 		    (color = get_color_number(argv[*color_index])) != -1) {
709 			(*color_index)++;
710 			revers_back_color = color;
711 		}
712 	}
713 }
714 
715 
716 /*
717  * Set normal and reverse foreground and background colors.
718  */
719 
720 static void
721 set_colors(void)
722 {
723 	fprintf(stderr, "[=%dF", normal_fore_color);
724 	fprintf(stderr, "[=%dG", normal_back_color);
725 	fprintf(stderr, "[=%dH", revers_fore_color);
726 	fprintf(stderr, "[=%dI", revers_back_color);
727 }
728 
729 
730 /*
731  * Switch to virtual terminal #arg.
732  */
733 
734 static void
735 set_console(char *arg)
736 {
737 	int n;
738 
739 	if(!arg || strspn(arg,"0123456789") != strlen(arg)) {
740 		revert();
741 		errx(1, "bad console number");
742 	}
743 
744 	n = atoi(arg);
745 
746 	if (n < 1 || n > 16) {
747 		revert();
748 		errx(1, "console number out of range");
749 	} else if (ioctl(0, VT_ACTIVATE, (caddr_t) (long) n) == -1) {
750 		revert();
751 		errc(1, errno, "switching vty");
752 	}
753 }
754 
755 
756 /*
757  * Sets the border color.
758  */
759 
760 static void
761 set_border_color(char *arg)
762 {
763 	int color;
764 
765 	if ((color = get_color_number(arg)) != -1)
766 		fprintf(stderr, "[=%dA", color);
767 	else
768 		usage();
769 }
770 
771 
772 static void
773 set_mouse_char(char *arg)
774 {
775 	struct mouse_info mouse;
776 	long l;
777 
778 	l = strtol(arg, NULL, 0);
779 
780 	if ((l < 0) || (l > UCHAR_MAX)) {
781 		revert();
782 		errx(1, "argument to -M must be 0 through %d", UCHAR_MAX);
783 	}
784 
785 	mouse.operation = MOUSE_MOUSECHAR;
786 	mouse.u.mouse_char = (int)l;
787 
788 	if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) {
789 		revert();
790 		errc(1, errno, "setting mouse character");
791 	}
792 }
793 
794 
795 /*
796  * Show/hide the mouse.
797  */
798 
799 static void
800 set_mouse(char *arg)
801 {
802 	struct mouse_info mouse;
803 
804 	if (!strcmp(arg, "on")) {
805 		mouse.operation = MOUSE_SHOW;
806 	} else if (!strcmp(arg, "off")) {
807 		mouse.operation = MOUSE_HIDE;
808 	} else {
809 		revert();
810 		errx(1, "argument to -m must be either on or off");
811 	}
812 	ioctl(0, CONS_MOUSECTL, &mouse);
813 }
814 
815 
816 static void
817 set_lockswitch(char *arg)
818 {
819 	int data;
820 
821 	if (!strcmp(arg, "off")) {
822 		data = 0x01;
823 	} else if (!strcmp(arg, "on")) {
824 		data = 0x02;
825 	} else {
826 		revert();
827 		errx(1, "argument to -S must be either on or off");
828 	}
829 
830 	if (ioctl(0, VT_LOCKSWITCH, &data) == -1) {
831 		revert();
832 		errc(1, errno, "turning %s vty switching",
833 		     data == 0x01 ? "off" : "on");
834 	}
835 }
836 
837 
838 /*
839  * Return the adapter name for a specified type.
840  */
841 
842 static const char *
843 adapter_name(int type)
844 {
845 	static struct {
846 		int type;
847 		const char *name;
848 	} names[] = {{ KD_MONO,     "MDA" },
849 		     { KD_HERCULES, "Hercules" },
850 		     { KD_CGA,      "CGA" },
851 		     { KD_EGA,      "EGA" },
852 		     { KD_VGA,      "VGA" },
853 		     { KD_TGA,      "TGA" },
854 		     { -1,          "Unknown" },
855 	};
856 
857 	int i;
858 
859 	for (i = 0; names[i].type != -1; ++i) {
860 		if (names[i].type == type)
861 			break;
862 	}
863 
864 	return names[i].name;
865 }
866 
867 
868 /*
869  * Show graphics adapter information.
870  */
871 
872 static void
873 show_adapter_info(void)
874 {
875 	struct video_adapter_info ad;
876 
877 	ad.va_index = 0;
878 
879 	if (ioctl(0, CONS_ADPINFO, &ad) == -1) {
880 		revert();
881 		errc(1, errno, "obtaining adapter information");
882 	}
883 
884 	printf("fb%d:\n", ad.va_index);
885 	printf("    %.*s%d, type:%s%s (%d), flags:0x%x\n",
886 	       (int)sizeof(ad.va_name), ad.va_name, ad.va_unit,
887 	       (ad.va_flags & V_ADP_VESA) ? "VESA " : "",
888 	       adapter_name(ad.va_type), ad.va_type, ad.va_flags);
889 	printf("    initial mode:%d, current mode:%d, BIOS mode:%d\n",
890 	       ad.va_initial_mode, ad.va_mode, ad.va_initial_bios_mode);
891 	printf("    frame buffer window:0x%x, buffer size:0x%zx\n",
892 	       ad.va_window, ad.va_buffer_size);
893 	printf("    window size:0x%zx, origin:0x%x\n",
894 	       ad.va_window_size, ad.va_window_orig);
895 	printf("    display start address (%d, %d), scan line width:%d\n",
896 	       ad.va_disp_start.x, ad.va_disp_start.y, ad.va_line_width);
897 	printf("    reserved:0x%x\n", ad.va_unused0);
898 }
899 
900 
901 /*
902  * Show video mode information.
903  */
904 
905 static void
906 show_mode_info(void)
907 {
908 	struct video_info vinfo;
909 	char buf[80];
910 	int mode;
911 	int c;
912 
913 	printf("    mode#     flags   type    size       "
914 	       "font      window      linear buffer\n");
915 	printf("---------------------------------------"
916 	       "---------------------------------------\n");
917 
918 	for (mode = 0; mode < M_VESA_MODE_MAX; ++mode) {
919 		vinfo.vi_mode = mode;
920 
921 		if (ioctl(0, CONS_MODEINFO, &vinfo))
922 			continue;
923 		if (vinfo.vi_mode != mode)
924 			continue;
925 
926 		printf("%3d (0x%03x)", mode, mode);
927 		printf(" 0x%08x", vinfo.vi_flags);
928 
929 		if (vinfo.vi_flags & V_INFO_GRAPHICS) {
930 			c = 'G';
931 
932 			snprintf(buf, sizeof(buf), "%dx%dx%d %d",
933 				 vinfo.vi_width, vinfo.vi_height,
934 				 vinfo.vi_depth, vinfo.vi_planes);
935 		} else {
936 			c = 'T';
937 
938 			snprintf(buf, sizeof(buf), "%dx%d",
939 				 vinfo.vi_width, vinfo.vi_height);
940 		}
941 
942 		printf(" %c %-15s", c, buf);
943 		snprintf(buf, sizeof(buf), "%dx%d",
944 			 vinfo.vi_cwidth, vinfo.vi_cheight);
945 		printf(" %-5s", buf);
946 		printf(" 0x%05x %2dk %2dk",
947 		       vinfo.vi_window, (int)vinfo.vi_window_size / 1024,
948 		       (int)vinfo.vi_window_gran/1024);
949 		printf(" 0x%08x %dk\n",
950 		       vinfo.vi_buffer, (int)vinfo.vi_buffer_size / 1024);
951 	}
952 }
953 
954 
955 static void
956 show_info(char *arg)
957 {
958 	if (!strcmp(arg, "adapter")) {
959 		show_adapter_info();
960 	} else if (!strcmp(arg, "mode")) {
961 		show_mode_info();
962 	} else {
963 		revert();
964 		errx(1, "argument to -i must be either adapter or mode");
965 	}
966 }
967 
968 
969 static void
970 test_frame(void)
971 {
972 	int i;
973 
974 	fprintf(stdout, "[=0G\n\n");
975 
976 	for (i = 0; i < 8; i++) {
977 		fprintf(stdout, "[=15F[=0G        %2d [=%dF%-16s"
978 			"[=15F[=0G        %2d [=%dF%-16s        "
979 			"[=15F %2d [=%dGBACKGROUND[=0G\n",
980 			i, i, legal_colors[i], i+8, i+8,
981 			legal_colors[i+8], i, i);
982 	}
983 
984 	fprintf(stdout, "[=%dF[=%dG[=%dH[=%dI\n",
985 		info.mv_norm.fore, info.mv_norm.back,
986 		info.mv_rev.fore, info.mv_rev.back);
987 }
988 
989 
990 /*
991  * Snapshot the video memory of that terminal, using the CONS_SCRSHOT
992  * ioctl, and writes the results to stdout either in the special
993  * binary format (see manual page for details), or in the plain
994  * text format.
995  */
996 
997 static void
998 dump_screen(int mode)
999 {
1000 	scrshot_t shot;
1001 	vid_info_t vinfo;
1002 
1003 	vinfo.size = sizeof(vinfo);
1004 
1005 	if (ioctl(0, CONS_GETINFO, &vinfo) == -1) {
1006 		revert();
1007 		errc(1, errno, "obtaining current video mode parameters");
1008 	}
1009 
1010 	shot.buf = alloca(vinfo.mv_csz * vinfo.mv_rsz * sizeof(u_int16_t));
1011 
1012 	if (shot.buf == NULL) {
1013 		revert();
1014 		errx(1, "failed to allocate memory for dump");
1015 	}
1016 
1017 	shot.xsize = vinfo.mv_csz;
1018 	shot.ysize = vinfo.mv_rsz;
1019 
1020 	if (ioctl(0, CONS_SCRSHOT, &shot) == -1) {
1021 		revert();
1022 		errc(1, errno, "dumping screen");
1023 	}
1024 
1025 	if (mode == DUMP_RAW) {
1026 		printf("SCRSHOT_%c%c%c%c", DUMP_FMT_REV, 2,
1027 		       shot.xsize, shot.ysize);
1028 
1029 		fflush(stdout);
1030 
1031 		write(STDOUT_FILENO, shot.buf,
1032 		      shot.xsize * shot.ysize * sizeof(u_int16_t));
1033 	} else {
1034 		char *line;
1035 		int x, y;
1036 		u_int16_t ch;
1037 
1038 		line = alloca(shot.xsize + 1);
1039 
1040 		if (line == NULL) {
1041 			revert();
1042 			errx(1, "failed to allocate memory for line buffer");
1043 		}
1044 
1045 		for (y = 0; y < shot.ysize; y++) {
1046 			for (x = 0; x < shot.xsize; x++) {
1047 				ch = shot.buf[x + (y * shot.xsize)];
1048 				ch &= 0xff;
1049 
1050 				if (isprint(ch) == 0)
1051 					ch = ' ';
1052 
1053 				line[x] = (char)ch;
1054 			}
1055 
1056 			/* Trim trailing spaces */
1057 
1058 			do {
1059 				line[x--] = '\0';
1060 			} while (line[x] == ' ' && x != 0);
1061 
1062 			puts(line);
1063 		}
1064 
1065 		fflush(stdout);
1066 	}
1067 }
1068 
1069 
1070 /*
1071  * Set the console history buffer size.
1072  */
1073 
1074 static void
1075 set_history(char *opt)
1076 {
1077 	int size;
1078 
1079 	size = atoi(opt);
1080 
1081 	if ((*opt == '\0') || size < 0) {
1082 		revert();
1083 		errx(1, "argument must be a positive number");
1084 	}
1085 
1086 	if (ioctl(0, CONS_HISTORY, &size) == -1) {
1087 		revert();
1088 		errc(1, errno, "setting history buffer size");
1089 	}
1090 }
1091 
1092 
1093 /*
1094  * Clear the console history buffer.
1095  */
1096 
1097 static void
1098 clear_history(void)
1099 {
1100 	if (ioctl(0, CONS_CLRHIST) == -1) {
1101 		revert();
1102 		errc(1, errno, "clearing history buffer");
1103 	}
1104 }
1105 
1106 
1107 int
1108 main(int argc, char **argv)
1109 {
1110 	char *font, *type;
1111 	int opt;
1112 
1113 	if (argc == 1)
1114 		usage();
1115 
1116 	init();
1117 
1118 	info.size = sizeof(info);
1119 
1120 	if (ioctl(0, CONS_GETINFO, &info) == -1)
1121 		err(1, "must be on a virtual console");
1122 
1123 	while((opt = getopt(argc, argv, "b:Cc:df:g:h:i:l:LM:m:pPr:S:s:t:x")) != -1) {
1124 		switch(opt) {
1125 		case 'b':
1126 			set_border_color(optarg);
1127 			break;
1128 		case 'C':
1129 			clear_history();
1130 			break;
1131 		case 'c':
1132 			set_cursor_type(optarg);
1133 			break;
1134 		case 'd':
1135 			print_scrnmap();
1136 			break;
1137 		case 'f':
1138 			type = optarg;
1139 			font = nextarg(argc, argv, &optind, 'f', 0);
1140 
1141 			if (font == NULL) {
1142 				type = NULL;
1143 				font = optarg;
1144 			}
1145 
1146 			load_font(type, font);
1147 			break;
1148 		case 'g':
1149 			if (sscanf(optarg, "%dx%d",
1150 			    &vesa_cols, &vesa_rows) != 2) {
1151 				revert();
1152 				warnx("incorrect geometry: %s", optarg);
1153 				usage();
1154 			}
1155                 	break;
1156 		case 'h':
1157 			set_history(optarg);
1158 			break;
1159 		case 'i':
1160 			show_info(optarg);
1161 			break;
1162 		case 'l':
1163 			load_scrnmap(optarg);
1164 			break;
1165 		case 'L':
1166 			load_default_scrnmap();
1167 			break;
1168 		case 'M':
1169 			set_mouse_char(optarg);
1170 			break;
1171 		case 'm':
1172 			set_mouse(optarg);
1173 			break;
1174 		case 'p':
1175 			dump_screen(DUMP_RAW);
1176 			break;
1177 		case 'P':
1178 			dump_screen(DUMP_TXT);
1179 			break;
1180 		case 'r':
1181 			get_reverse_colors(argc, argv, &optind);
1182 			break;
1183 		case 'S':
1184 			set_lockswitch(optarg);
1185 			break;
1186 		case 's':
1187 			set_console(optarg);
1188 			break;
1189 		case 't':
1190 			set_screensaver_timeout(optarg);
1191 			break;
1192 		case 'x':
1193 			hex = 1;
1194 			break;
1195 		default:
1196 			usage();
1197 		}
1198 	}
1199 
1200 	if (optind < argc && !strcmp(argv[optind], "show")) {
1201 		test_frame();
1202 		optind++;
1203 	}
1204 
1205 	video_mode(argc, argv, &optind);
1206 
1207 	get_normal_colors(argc, argv, &optind);
1208 
1209 	if (colors_changed || video_mode_changed) {
1210 		if (!(new_mode_info.vi_flags & V_INFO_GRAPHICS)) {
1211 			if ((normal_back_color < 8) && (revers_back_color < 8)) {
1212 				set_colors();
1213 			} else {
1214 				revert();
1215 				errx(1, "bg color for text modes must be < 8");
1216 			}
1217 		} else {
1218 			set_colors();
1219 		}
1220 	}
1221 
1222 	if ((optind != argc) || (argc == 1))
1223 		usage();
1224 
1225 	return 0;
1226 }
1227