HAMMER utility - Add ^C support, improve verbosity.
[dragonfly.git] / usr.sbin / vidcontrol / vidcontrol.c
blob682701e12ed386e5693179f5fbae5e113d2eb951
1 /*-
2 * Copyright (c) 1994-1996 Søren Schmidt
3 * All rights reserved.
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
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.
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 $
32 #include <machine/console.h>
34 #include <sys/consio.h>
35 #include <sys/errno.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
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>
47 #include "path.h"
48 #include "decode.h"
51 #define DATASIZE(x) ((x).w * (x).h * 256 / 8)
53 #define DUMP_RAW 0
54 #define DUMP_TXT 1
56 #define DUMP_FMT_REV 1
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"
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;
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;
89 * Initialize revert data.
91 * NOTE: the following parameters are not yet saved/restored:
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
102 static void
103 init(void)
105 if (ioctl(0, VT_GETACTIVE, &cur_info.active_vty) == -1)
106 errc(1, errno, "getting active vty");
108 cur_info.console_info.size = sizeof(cur_info.console_info);
110 if (ioctl(0, CONS_GETINFO, &cur_info.console_info) == -1)
111 errc(1, errno, "getting console information");
113 if (ioctl(0, GIO_SCRNMAP, &cur_info.screen_map) == -1)
114 errc(1, errno, "getting screen map");
116 if (ioctl(0, CONS_GET, &cur_info.video_mode_number) == -1)
117 errc(1, errno, "getting video mode number");
119 cur_info.video_mode_info.vi_mode = cur_info.video_mode_number;
121 if (ioctl(0, CONS_MODEINFO, &cur_info.video_mode_info) == -1)
122 errc(1, errno, "getting video mode parameters");
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;
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).
135 * NOTE: please also read the comments of init().
138 static void
139 revert(void)
141 int size[3];
143 ioctl(0, VT_ACTIVATE, (caddr_t) (long) cur_info.active_vty);
145 fprintf(stderr, "\e[=%dA", cur_info.console_info.mv_ovscan);
146 fprintf(stderr, "\e[=%dF", cur_info.console_info.mv_norm.fore);
147 fprintf(stderr, "\e[=%dG", cur_info.console_info.mv_norm.back);
148 fprintf(stderr, "\e[=%dH", cur_info.console_info.mv_rev.fore);
149 fprintf(stderr, "\e[=%dI", cur_info.console_info.mv_rev.back);
151 ioctl(0, PIO_SCRNMAP, &cur_info.screen_map);
152 ioctl(0, CONS_SET, &cur_info.video_mode_number);
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;
160 ioctl(0, KDRASTER, size);
166 * Print a short usage string describing all options, then exit.
169 static void
170 usage(void)
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");
183 exit(1);
188 * Retrieve the next argument from the command line (for options that require
189 * more than one argument).
192 static char *
193 nextarg(int ac, char **av, int *indp, int oc, int strict)
195 if (*indp < ac)
196 return(av[(*indp)++]);
198 if (strict != 0) {
199 revert();
200 errx(1, "option requires two arguments -- %c", oc);
203 return(NULL);
208 * Guess which file to open. Try to open each combination of a specified set
209 * of file name components.
212 static FILE *
213 openguess(const char *a[], const char *b[], const char *c[], const char *d[], char **name)
215 FILE *f;
216 int i, j, k, l;
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]);
225 f = fopen(*name, "r");
227 if (f != NULL)
228 return (f);
230 free(*name);
236 return (NULL);
241 * Load a screenmap from a file and set it.
244 static void
245 load_scrnmap(char *filename)
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};
256 fd = openguess(a, b, c, d, &name);
258 if (fd == NULL) {
259 revert();
260 errx(1, "screenmap file not found");
263 size = sizeof(scrnmap);
265 if (decode(fd, (char *)&scrnmap, size) != size) {
266 rewind(fd);
268 if (fread(&scrnmap, 1, size, fd) != (size_t)size) {
269 fclose(fd);
270 revert();
271 errx(1, "bad screenmap file");
275 if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
276 revert();
277 errc(1, errno, "loading screenmap");
280 fclose(fd);
285 * Set the default screenmap.
288 static void
289 load_default_scrnmap(void)
291 scrmap_t scrnmap;
292 int i;
294 for (i = 0; i < 256; i++)
295 *((char*)&scrnmap + i) = i;
297 if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
298 revert();
299 errc(1, errno, "loading default screenmap");
305 * Print the current screenmap to stdout.
308 static void
309 print_scrnmap(void)
311 unsigned char map[256];
312 size_t i;
314 if (ioctl(0, GIO_SCRNMAP, &map) == -1) {
315 revert();
316 errc(1, errno, "getting screenmap");
319 for (i=0; i<sizeof(map); i++) {
320 if (i > 0 && i % 16 == 0)
321 fprintf(stdout, "\n");
323 if (hex != 0)
324 fprintf(stdout, " %02x", map[i]);
325 else
326 fprintf(stdout, " %03d", map[i]);
329 fprintf(stdout, "\n");
334 * Determine a file's size.
337 static int
338 fsize(FILE *file)
340 struct stat sb;
342 if (fstat(fileno(file), &sb) == 0)
343 return sb.st_size;
344 else
345 return -1;
350 * Load a font from file and set it.
353 static void
354 load_font(char *type, char *filename)
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;
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}};
375 vinfo.size = sizeof(vinfo);
377 if (ioctl(0, CONS_GETINFO, &vinfo) == -1) {
378 revert();
379 errc(1, errno, "obtaining current video mode parameters");
382 snprintf(size_sufx, sizeof(size_sufx), "-8x%d", vinfo.font_size);
384 fd = openguess(a, b, c, d, &name);
386 if (fd == NULL) {
387 revert();
388 errx(1, "%s: can't load font file", filename);
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;
402 if (size == 0) {
403 fclose(fd);
404 revert();
405 errx(1, "%s: bad font size specification", type);
407 } else {
408 /* Apply heuristics */
410 int j;
411 int dsize[2];
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);
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;
432 if (size == 0) {
433 fclose(fd);
434 revert();
435 errx(1, "%s: can't guess font size", filename);
438 rewind(fd);
441 fontmap = (char*) malloc(size);
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);
454 if (ioctl(0, io, fontmap) == -1) {
455 revert();
456 errc(1, errno, "loading font");
459 fclose(fd);
460 free(fontmap);
465 * Set the timeout for the screensaver.
468 static void
469 set_screensaver_timeout(char *arg)
471 int nsec;
473 if (!strcmp(arg, "off")) {
474 nsec = 0;
475 } else {
476 nsec = atoi(arg);
478 if ((*arg == '\0') || (nsec < 1)) {
479 revert();
480 errx(1, "argument must be a positive number");
484 if (ioctl(0, CONS_BLANKTIME, &nsec) == -1) {
485 revert();
486 errc(1, errno, "setting screensaver period");
492 * Set the cursor's shape/type.
495 static void
496 set_cursor_type(char *appearance)
498 int type;
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");
511 if (ioctl(0, CONS_CURSORTYPE, &type) == -1) {
512 revert();
513 errc(1, errno, "setting cursor type");
519 * Set the video mode.
522 static void
523 video_mode(int argc, char **argv, int *mode_index)
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 },
560 int new_mode_num = 0;
561 int size[3];
562 int i;
565 * Parse the video mode argument...
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");
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;
582 if (modes[i].name == NULL)
583 errx(1, "invalid video mode name");
587 * Collect enough information about the new video mode...
590 new_mode_info.vi_mode = new_mode_num;
592 if (ioctl(0, CONS_MODEINFO, &new_mode_info) == -1) {
593 revert();
594 errc(1, errno, "obtaining new video mode parameters");
598 * Try setting the new mode.
601 if (ioctl(0, CONS_SET, &new_mode_num) == -1) {
602 revert();
603 errc(1, errno, "setting video mode");
607 * For raster modes it's not enough to just set the mode.
608 * We also need to explicitly set the raster mode.
611 if (new_mode_info.vi_flags & V_INFO_GRAPHICS) {
612 /* font size */
614 if (font_height == 0)
615 font_height = cur_info.console_info.font_size;
617 size[2] = font_height;
619 /* adjust columns */
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;
628 /* adjust rows */
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;
638 /* set raster mode */
640 if (ioctl(0, KDRASTER, size)) {
641 revert();
642 errc(1, errno, "activating raster display");
646 video_mode_changed = 1;
648 (*mode_index)++;
654 * Return the number for a specified color name.
657 static int
658 get_color_number(char *color)
660 int i;
662 for (i=0; i<16; i++) {
663 if (!strcmp(color, legal_colors[i]))
664 return i;
666 return -1;
671 * Get normal text and background colors.
674 static void
675 get_normal_colors(int argc, char **argv, int *color_index)
677 int color;
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;
685 if (*color_index < argc &&
686 (color = get_color_number(argv[*color_index])) != -1) {
687 (*color_index)++;
688 normal_back_color = color;
695 * Get reverse text and background colors.
698 static void
699 get_reverse_colors(int argc, char **argv, int *color_index)
701 int color;
703 if ((color = get_color_number(argv[*(color_index)-1])) != -1) {
704 revers_fore_color = color;
705 colors_changed = 1;
707 if (*color_index < argc &&
708 (color = get_color_number(argv[*color_index])) != -1) {
709 (*color_index)++;
710 revers_back_color = color;
717 * Set normal and reverse foreground and background colors.
720 static void
721 set_colors(void)
723 fprintf(stderr, "\e[=%dF", normal_fore_color);
724 fprintf(stderr, "\e[=%dG", normal_back_color);
725 fprintf(stderr, "\e[=%dH", revers_fore_color);
726 fprintf(stderr, "\e[=%dI", revers_back_color);
731 * Switch to virtual terminal #arg.
734 static void
735 set_console(char *arg)
737 int n;
739 if(!arg || strspn(arg,"0123456789") != strlen(arg)) {
740 revert();
741 errx(1, "bad console number");
744 n = atoi(arg);
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");
757 * Sets the border color.
760 static void
761 set_border_color(char *arg)
763 int color;
765 if ((color = get_color_number(arg)) != -1)
766 fprintf(stderr, "\e[=%dA", color);
767 else
768 usage();
772 static void
773 set_mouse_char(char *arg)
775 struct mouse_info mouse;
776 long l;
778 l = strtol(arg, NULL, 0);
780 if ((l < 0) || (l > UCHAR_MAX)) {
781 revert();
782 errx(1, "argument to -M must be 0 through %d", UCHAR_MAX);
785 mouse.operation = MOUSE_MOUSECHAR;
786 mouse.u.mouse_char = (int)l;
788 if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) {
789 revert();
790 errc(1, errno, "setting mouse character");
796 * Show/hide the mouse.
799 static void
800 set_mouse(char *arg)
802 struct mouse_info mouse;
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");
812 ioctl(0, CONS_MOUSECTL, &mouse);
816 static void
817 set_lockswitch(char *arg)
819 int data;
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");
830 if (ioctl(0, VT_LOCKSWITCH, &data) == -1) {
831 revert();
832 errc(1, errno, "turning %s vty switching",
833 data == 0x01 ? "off" : "on");
839 * Return the adapter name for a specified type.
842 static const char *
843 adapter_name(int type)
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" },
857 int i;
859 for (i = 0; names[i].type != -1; ++i) {
860 if (names[i].type == type)
861 break;
864 return names[i].name;
869 * Show graphics adapter information.
872 static void
873 show_adapter_info(void)
875 struct video_adapter_info ad;
877 ad.va_index = 0;
879 if (ioctl(0, CONS_ADPINFO, &ad) == -1) {
880 revert();
881 errc(1, errno, "obtaining adapter information");
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);
902 * Show video mode information.
905 static void
906 show_mode_info(void)
908 struct video_info vinfo;
909 char buf[80];
910 int mode;
911 int c;
913 printf(" mode# flags type size "
914 "font window linear buffer\n");
915 printf("---------------------------------------"
916 "---------------------------------------\n");
918 for (mode = 0; mode < M_VESA_MODE_MAX; ++mode) {
919 vinfo.vi_mode = mode;
921 if (ioctl(0, CONS_MODEINFO, &vinfo))
922 continue;
923 if (vinfo.vi_mode != mode)
924 continue;
926 printf("%3d (0x%03x)", mode, mode);
927 printf(" 0x%08x", vinfo.vi_flags);
929 if (vinfo.vi_flags & V_INFO_GRAPHICS) {
930 c = 'G';
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';
938 snprintf(buf, sizeof(buf), "%dx%d",
939 vinfo.vi_width, vinfo.vi_height);
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);
955 static void
956 show_info(char *arg)
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");
969 static void
970 test_frame(void)
972 int i;
974 fprintf(stdout, "\e[=0G\n\n");
976 for (i = 0; i < 8; i++) {
977 fprintf(stdout, "\e[=15F\e[=0G %2d \e[=%dF%-16s"
978 "\e[=15F\e[=0G %2d \e[=%dF%-16s "
979 "\e[=15F %2d \e[=%dGBACKGROUND\e[=0G\n",
980 i, i, legal_colors[i], i+8, i+8,
981 legal_colors[i+8], i, i);
984 fprintf(stdout, "\e[=%dF\e[=%dG\e[=%dH\e[=%dI\n",
985 info.mv_norm.fore, info.mv_norm.back,
986 info.mv_rev.fore, info.mv_rev.back);
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.
997 static void
998 dump_screen(int mode)
1000 scrshot_t shot;
1001 vid_info_t vinfo;
1003 vinfo.size = sizeof(vinfo);
1005 if (ioctl(0, CONS_GETINFO, &vinfo) == -1) {
1006 revert();
1007 errc(1, errno, "obtaining current video mode parameters");
1010 shot.buf = alloca(vinfo.mv_csz * vinfo.mv_rsz * sizeof(u_int16_t));
1012 if (shot.buf == NULL) {
1013 revert();
1014 errx(1, "failed to allocate memory for dump");
1017 shot.xsize = vinfo.mv_csz;
1018 shot.ysize = vinfo.mv_rsz;
1020 if (ioctl(0, CONS_SCRSHOT, &shot) == -1) {
1021 revert();
1022 errc(1, errno, "dumping screen");
1025 if (mode == DUMP_RAW) {
1026 printf("SCRSHOT_%c%c%c%c", DUMP_FMT_REV, 2,
1027 shot.xsize, shot.ysize);
1029 fflush(stdout);
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;
1038 line = alloca(shot.xsize + 1);
1040 if (line == NULL) {
1041 revert();
1042 errx(1, "failed to allocate memory for line buffer");
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;
1050 if (isprint(ch) == 0)
1051 ch = ' ';
1053 line[x] = (char)ch;
1056 /* Trim trailing spaces */
1058 do {
1059 line[x--] = '\0';
1060 } while (line[x] == ' ' && x != 0);
1062 puts(line);
1065 fflush(stdout);
1071 * Set the console history buffer size.
1074 static void
1075 set_history(char *opt)
1077 int size;
1079 size = atoi(opt);
1081 if ((*opt == '\0') || size < 0) {
1082 revert();
1083 errx(1, "argument must be a positive number");
1086 if (ioctl(0, CONS_HISTORY, &size) == -1) {
1087 revert();
1088 errc(1, errno, "setting history buffer size");
1094 * Clear the console history buffer.
1097 static void
1098 clear_history(void)
1100 if (ioctl(0, CONS_CLRHIST) == -1) {
1101 revert();
1102 errc(1, errno, "clearing history buffer");
1108 main(int argc, char **argv)
1110 char *font, *type;
1111 int opt;
1113 if (argc == 1)
1114 usage();
1116 init();
1118 info.size = sizeof(info);
1120 if (ioctl(0, CONS_GETINFO, &info) == -1)
1121 err(1, "must be on a virtual console");
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);
1141 if (font == NULL) {
1142 type = NULL;
1143 font = optarg;
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();
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();
1200 if (optind < argc && !strcmp(argv[optind], "show")) {
1201 test_frame();
1202 optind++;
1205 video_mode(argc, argv, &optind);
1207 get_normal_colors(argc, argv, &optind);
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");
1217 } else {
1218 set_colors();
1222 if ((optind != argc) || (argc == 1))
1223 usage();
1225 return 0;