2 * Copyright (c) 1994-1996 Søren Schmidt
5 * Portions of this software are based in part on the work of
6 * Sascha Wildner <saw@online.de> contributed to The DragonFly Project
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer,
13 * in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * $DragonFly: src/usr.sbin/vidcontrol/vidcontrol.c,v 1.10 2005/03/02 06:08:29 joerg Exp $
35 static const char rcsid[] =
47 #include <sys/consio.h>
48 #include <sys/endian.h>
49 #include <sys/errno.h>
50 #include <sys/param.h>
51 #include <sys/types.h>
53 #include <sys/sysctl.h>
58 #define DATASIZE(x) ((x).w * (x).h * 256 / 8)
60 /* Screen dump modes */
61 #define DUMP_FMT_RAW 1
62 #define DUMP_FMT_TXT 2
63 /* Screen dump options */
66 /* Screen dump file format revision */
67 #define DUMP_FMT_REV 1
69 static const char *legal_colors[16] = {
70 "black", "blue", "green", "cyan",
71 "red", "magenta", "brown", "white",
72 "grey", "lightblue", "lightgreen", "lightcyan",
73 "lightred", "lightmagenta", "yellow", "lightwhite"
78 vid_info_t console_info;
79 unsigned char screen_map[256];
80 int video_mode_number;
81 struct video_info video_mode_info;
84 struct vt4font_header {
90 uint32_t map_count[4];
96 static int font_height;
97 static int colors_changed;
98 static int video_mode_changed;
99 static int normal_fore_color, normal_back_color;
100 static int revers_fore_color, revers_back_color;
101 static int vt4_mode = 0;
102 static struct vid_info info;
103 static struct video_info new_mode_info;
107 * Initialize revert data.
109 * NOTE: the following parameters are not yet saved/restored:
111 * screen saver timeout
113 * mouse character and mouse show/hide state
114 * vty switching on/off state
115 * history buffer size
123 if (ioctl(0, VT_GETACTIVE, &cur_info.active_vty) == -1)
124 errc(1, errno, "getting active vty");
126 cur_info.console_info.size = sizeof(cur_info.console_info);
128 if (ioctl(0, CONS_GETINFO, &cur_info.console_info) == -1)
129 errc(1, errno, "getting console information");
131 /* vt(4) use unicode, so no screen mapping required. */
133 ioctl(0, GIO_SCRNMAP, &cur_info.screen_map) == -1)
134 errc(1, errno, "getting screen map");
136 if (ioctl(0, CONS_GET, &cur_info.video_mode_number) == -1)
137 errc(1, errno, "getting video mode number");
139 cur_info.video_mode_info.vi_mode = cur_info.video_mode_number;
141 if (ioctl(0, CONS_MODEINFO, &cur_info.video_mode_info) == -1)
142 errc(1, errno, "getting video mode parameters");
144 normal_fore_color = cur_info.console_info.mv_norm.fore;
145 normal_back_color = cur_info.console_info.mv_norm.back;
146 revers_fore_color = cur_info.console_info.mv_rev.fore;
147 revers_back_color = cur_info.console_info.mv_rev.back;
152 * If something goes wrong along the way we call revert() to go back to the
153 * console state we came from (which is assumed to be working).
155 * NOTE: please also read the comments of init().
163 ioctl(0, VT_ACTIVATE, cur_info.active_vty);
165 fprintf(stderr, "\033[=%dA", cur_info.console_info.mv_ovscan);
166 fprintf(stderr, "\033[=%dF", cur_info.console_info.mv_norm.fore);
167 fprintf(stderr, "\033[=%dG", cur_info.console_info.mv_norm.back);
168 fprintf(stderr, "\033[=%dH", cur_info.console_info.mv_rev.fore);
169 fprintf(stderr, "\033[=%dI", cur_info.console_info.mv_rev.back);
172 ioctl(0, PIO_SCRNMAP, &cur_info.screen_map);
174 if (cur_info.video_mode_number >= M_VESA_BASE)
175 ioctl(0, _IO('V', cur_info.video_mode_number - M_VESA_BASE),
178 ioctl(0, _IO('S', cur_info.video_mode_number), NULL);
180 if (cur_info.video_mode_info.vi_flags & V_INFO_GRAPHICS) {
181 size[0] = cur_info.video_mode_info.vi_width / 8;
182 size[1] = cur_info.video_mode_info.vi_height /
183 cur_info.console_info.font_size;
184 size[2] = cur_info.console_info.font_size;
186 ioctl(0, KDRASTER, size);
192 * Print a short usage string describing all options, then exit.
199 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
200 "usage: vidcontrol [-CHPpx] [-b color] [-c appearance] [-f [[size] file]]",
201 " [-g geometry] [-h size] [-i adapter | mode]",
202 " [-M char] [-m on | off] [-r foreground background]",
203 " [-S on | off] [-s number] [-T xterm | cons25] [-t N | off]",
204 " [mode] [foreground [background]] [show]");
206 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
207 "usage: vidcontrol [-CdHLPpx] [-b color] [-c appearance] [-f [size] file]",
208 " [-g geometry] [-h size] [-i adapter | mode] [-l screen_map]",
209 " [-M char] [-m on | off] [-r foreground background]",
210 " [-S on | off] [-s number] [-T xterm | cons25] [-t N | off]",
211 " [mode] [foreground [background]] [show]");
215 /* Detect presence of vt(4). */
219 char vty_name[4] = "";
220 size_t len = sizeof(vty_name);
222 if (sysctlbyname("kern.vty", vty_name, &len, NULL, 0) != 0)
224 return (strcmp(vty_name, "vt") == 0);
228 * Retrieve the next argument from the command line (for options that require
229 * more than one argument).
233 nextarg(int ac, char **av, int *indp, int oc, int strict)
236 return(av[(*indp)++]);
240 errx(1, "option requires two arguments -- %c", oc);
248 * Guess which file to open. Try to open each combination of a specified set
249 * of file name components.
253 openguess(const char *a[], const char *b[], const char *c[], const char *d[], char **name)
258 for (i = 0; a[i] != NULL; i++) {
259 for (j = 0; b[j] != NULL; j++) {
260 for (k = 0; c[k] != NULL; k++) {
261 for (l = 0; d[l] != NULL; l++) {
262 asprintf(name, "%s%s%s%s",
263 a[i], b[j], c[k], d[l]);
265 f = fopen(*name, "r");
280 * Load a screenmap from a file and set it.
284 load_scrnmap(const char *filename)
290 const char *a[] = {"", SCRNMAP_PATH, NULL};
291 const char *b[] = {filename, NULL};
292 const char *c[] = {"", ".scm", NULL};
293 const char *d[] = {"", NULL};
295 fd = openguess(a, b, c, d, &name);
299 errx(1, "screenmap file not found");
302 size = sizeof(scrnmap);
304 if (decode(fd, (char *)&scrnmap, size) != size) {
307 if (fread(&scrnmap, 1, size, fd) != (size_t)size) {
308 warnx("bad screenmap file");
311 errx(1, "bad screenmap file");
315 if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
317 errc(1, errno, "loading screenmap");
325 * Set the default screenmap.
329 load_default_scrnmap(void)
334 for (i=0; i<256; i++)
335 *((char*)&scrnmap + i) = i;
337 if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
339 errc(1, errno, "loading default screenmap");
345 * Print the current screenmap to stdout.
351 unsigned char map[256];
354 if (ioctl(0, GIO_SCRNMAP, &map) == -1) {
356 errc(1, errno, "getting screenmap");
358 for (i=0; i<sizeof(map); i++) {
359 if (i != 0 && i % 16 == 0)
360 fprintf(stdout, "\n");
363 fprintf(stdout, " %02x", map[i]);
365 fprintf(stdout, " %03d", map[i]);
367 fprintf(stdout, "\n");
373 * Determine a file's size.
381 if (fstat(fileno(file), &sb) == 0)
388 load_vt4mappingtable(unsigned int nmappings, FILE *f)
396 if ((t = malloc(sizeof *t * nmappings)) == NULL) {
401 if (fread(t, sizeof *t * nmappings, 1, f) != 1) {
402 warn("read mappings");
407 for (i = 0; i < nmappings; i++) {
408 t[i].src = be32toh(t[i].src);
409 t[i].dst = be16toh(t[i].dst);
410 t[i].len = be16toh(t[i].len);
417 * Set the default vt font.
421 load_default_vt4font(void)
423 if (ioctl(0, PIO_VFONT_DEFAULT) == -1) {
425 errc(1, errno, "loading default vt font");
430 load_vt4font(FILE *f)
432 struct vt4font_header fh;
437 if (fread(&fh, sizeof fh, 1, f) != 1) {
438 warn("read file_header");
442 if (memcmp(fh.magic, "VFNT0002", 8) != 0) {
443 warnx("bad magic in font file\n");
447 for (i = 0; i < VFNT_MAPS; i++)
448 vfnt.map_count[i] = be32toh(fh.map_count[i]);
449 vfnt.glyph_count = be32toh(fh.glyph_count);
450 vfnt.width = fh.width;
451 vfnt.height = fh.height;
453 glyphsize = howmany(vfnt.width, 8) * vfnt.height * vfnt.glyph_count;
454 if ((vfnt.glyphs = malloc(glyphsize)) == NULL) {
459 if (fread(vfnt.glyphs, glyphsize, 1, f) != 1) {
465 for (i = 0; i < VFNT_MAPS; i++)
466 vfnt.map[i] = load_vt4mappingtable(vfnt.map_count[i], f);
468 if (ioctl(STDIN_FILENO, PIO_VFONT, &vfnt) == -1)
471 for (i = 0; i < VFNT_MAPS; i++)
477 * Load a font from file and set it.
481 load_font(const char *type, const char *filename)
485 unsigned long io = 0; /* silence stupid gcc(1) in the Wall mode */
486 char *name, *fontmap, size_sufx[6];
487 const char *a[] = {"", FONT_PATH, NULL};
488 const char *vt4a[] = {"", VT_FONT_PATH, NULL};
489 const char *b[] = {filename, NULL};
490 const char *c[] = {"", size_sufx, NULL};
491 const char *d[] = {"", ".fnt", NULL};
498 } sizes[] = {{8, 16, PIO_FONT8x16},
499 {8, 14, PIO_FONT8x14},
506 _info.size = sizeof(_info);
507 if (ioctl(0, CONS_GETINFO, &_info) == -1) {
509 warn("failed to obtain current video mode parameters");
513 snprintf(size_sufx, sizeof(size_sufx), "-8x%d", _info.font_size);
515 fd = openguess((vt4_mode == 0) ? a : vt4a, b, c, d, &name);
519 errx(1, "%s: can't load font file", filename);
530 if (sscanf(type, "%dx%d", &w, &h) == 2) {
531 for (i = 0; sizes[i].w != 0; i++) {
532 if (sizes[i].w == w && sizes[i].h == h) {
533 size = DATASIZE(sizes[i]);
535 font_height = sizes[i].h;
542 errx(1, "%s: bad font size specification", type);
545 /* Apply heuristics */
550 size = DATASIZE(sizes[0]);
551 fontmap = (char*) malloc(size);
552 dsize[0] = decode(fd, fontmap, size);
553 dsize[1] = fsize(fd);
557 for (j = 0; j < 2; j++) {
558 for (i = 0; sizes[i].w != 0; i++) {
559 if (DATASIZE(sizes[i]) == dsize[j]) {
562 font_height = sizes[i].h;
572 errx(1, "%s: can't guess font size", filename);
578 fontmap = (char*) malloc(size);
580 if (decode(fd, fontmap, size) != size) {
582 if (fsize(fd) != size ||
583 fread(fontmap, 1, size, fd) != (size_t)size) {
584 warnx("%s: bad font file", filename);
588 errx(1, "%s: bad font file", filename);
592 if (ioctl(0, io, fontmap) == -1) {
594 errc(1, errno, "loading font");
603 * Set the timeout for the screensaver.
607 set_screensaver_timeout(char *arg)
611 if (!strcmp(arg, "off")) {
616 if ((*arg == '\0') || (nsec < 1)) {
618 errx(1, "argument must be a positive number");
622 if (ioctl(0, CONS_BLANKTIME, &nsec) == -1) {
624 errc(1, errno, "setting screensaver period");
630 * Set the cursor's shape/type.
634 set_cursor_type(char *appearance)
638 if (!strcmp(appearance, "normal"))
640 else if (!strcmp(appearance, "blink"))
642 else if (!strcmp(appearance, "destructive"))
646 errx(1, "argument to -c must be normal, blink or destructive");
649 if (ioctl(0, CONS_CURSORTYPE, &type) == -1) {
651 errc(1, errno, "setting cursor type");
657 * Set the video mode.
661 video_mode(int argc, char **argv, int *mode_index)
666 unsigned long mode_num;
668 { "80x25", SW_TEXT_80x25, M_TEXT_80x25 },
669 { "80x30", SW_TEXT_80x30, M_TEXT_80x30 },
670 { "80x43", SW_TEXT_80x43, M_TEXT_80x43 },
671 { "80x50", SW_TEXT_80x50, M_TEXT_80x50 },
672 { "80x60", SW_TEXT_80x60, M_TEXT_80x60 },
673 { "132x25", SW_TEXT_132x25, M_TEXT_132x25 },
674 { "132x30", SW_TEXT_132x30, M_TEXT_132x30 },
675 { "132x43", SW_TEXT_132x43, M_TEXT_132x43 },
676 { "132x50", SW_TEXT_132x50, M_TEXT_132x50 },
677 { "132x60", SW_TEXT_132x60, M_TEXT_132x60 },
678 { "VGA_40x25", SW_VGA_C40x25, M_VGA_C40x25 },
679 { "VGA_80x25", SW_VGA_C80x25, M_VGA_C80x25 },
680 { "VGA_80x30", SW_VGA_C80x30, M_VGA_C80x30 },
681 { "VGA_80x50", SW_VGA_C80x50, M_VGA_C80x50 },
682 { "VGA_80x60", SW_VGA_C80x60, M_VGA_C80x60 },
684 { "VGA_90x25", SW_VGA_C90x25, M_VGA_C90x25 },
685 { "VGA_90x30", SW_VGA_C90x30, M_VGA_C90x30 },
686 { "VGA_90x43", SW_VGA_C90x43, M_VGA_C90x43 },
687 { "VGA_90x50", SW_VGA_C90x50, M_VGA_C90x50 },
688 { "VGA_90x60", SW_VGA_C90x60, M_VGA_C90x60 },
690 { "VGA_320x200", SW_VGA_CG320, M_CG320 },
691 { "EGA_80x25", SW_ENH_C80x25, M_ENH_C80x25 },
692 { "EGA_80x43", SW_ENH_C80x43, M_ENH_C80x43 },
693 { "VESA_132x25", SW_VESA_C132x25,M_VESA_C132x25 },
694 { "VESA_132x43", SW_VESA_C132x43,M_VESA_C132x43 },
695 { "VESA_132x50", SW_VESA_C132x50,M_VESA_C132x50 },
696 { "VESA_132x60", SW_VESA_C132x60,M_VESA_C132x60 },
697 { "VESA_800x600", SW_VESA_800x600,M_VESA_800x600 },
701 int new_mode_num = 0;
702 unsigned long mode = 0;
708 if (ioctl(0, CONS_GET, &cur_mode) < 0)
709 err(1, "cannot get the current video mode");
712 * Parse the video mode argument...
715 if (*mode_index < argc) {
716 if (!strncmp(argv[*mode_index], "MODE_", 5)) {
717 if (!isdigit(argv[*mode_index][5]))
718 errx(1, "invalid video mode number");
720 new_mode_num = atoi(&argv[*mode_index][5]);
722 for (i = 0; modes[i].name != NULL; ++i) {
723 if (!strcmp(argv[*mode_index], modes[i].name)) {
724 mode = modes[i].mode;
725 new_mode_num = modes[i].mode_num;
730 if (modes[i].name == NULL)
732 if (ioctl(0, mode, NULL) < 0) {
733 warn("cannot set videomode");
739 * Collect enough information about the new video mode...
742 new_mode_info.vi_mode = new_mode_num;
744 if (ioctl(0, CONS_MODEINFO, &new_mode_info) == -1) {
746 errc(1, errno, "obtaining new video mode parameters");
750 if (new_mode_num >= M_VESA_BASE)
751 mode = _IO('V', new_mode_num - M_VESA_BASE);
753 mode = _IO('S', new_mode_num);
757 * Try setting the new mode.
760 if (ioctl(0, mode, NULL) == -1) {
762 errc(1, errno, "setting video mode");
766 * For raster modes it's not enough to just set the mode.
767 * We also need to explicitly set the raster mode.
770 if (new_mode_info.vi_flags & V_INFO_GRAPHICS) {
773 if (font_height == 0)
774 font_height = cur_info.console_info.font_size;
776 size[2] = font_height;
780 if ((vesa_cols * 8 > new_mode_info.vi_width) ||
782 size[0] = new_mode_info.vi_width / 8;
789 if ((vesa_rows * font_height > new_mode_info.vi_height) ||
791 size[1] = new_mode_info.vi_height /
797 /* set raster mode */
799 if (ioctl(0, KDRASTER, size)) {
801 if (cur_mode >= M_VESA_BASE)
803 _IO('V', cur_mode - M_VESA_BASE),
806 ioctl(0, _IO('S', cur_mode), NULL);
808 warnc(ioerr, "cannot activate raster display");
813 video_mode_changed = 1;
822 * Return the number for a specified color name.
826 get_color_number(char *color)
830 for (i=0; i<16; i++) {
831 if (!strcmp(color, legal_colors[i]))
839 * Get normal text and background colors.
843 get_normal_colors(int argc, char **argv, int *_index)
847 if (*_index < argc && (color = get_color_number(argv[*_index])) != -1) {
849 fprintf(stderr, "\033[=%dF", color);
850 normal_fore_color=color;
853 && (color = get_color_number(argv[*_index])) != -1
856 fprintf(stderr, "\033[=%dG", color);
857 normal_back_color=color;
864 * Get reverse text and background colors.
868 get_reverse_colors(int argc, char **argv, int *_index)
872 if ((color = get_color_number(argv[*(_index)-1])) != -1) {
873 fprintf(stderr, "\033[=%dH", color);
874 revers_fore_color=color;
877 && (color = get_color_number(argv[*_index])) != -1
880 fprintf(stderr, "\033[=%dI", color);
881 revers_back_color=color;
888 * Set normal and reverse foreground and background colors.
894 fprintf(stderr, "\033[=%dF", normal_fore_color);
895 fprintf(stderr, "\033[=%dG", normal_back_color);
896 fprintf(stderr, "\033[=%dH", revers_fore_color);
897 fprintf(stderr, "\033[=%dI", revers_back_color);
902 * Switch to virtual terminal #arg.
906 set_console(char *arg)
910 if(!arg || strspn(arg,"0123456789") != strlen(arg)) {
912 errx(1, "bad console number");
917 if (n < 1 || n > 16) {
919 errx(1, "console number out of range");
920 } else if (ioctl(0, VT_ACTIVATE, n) == -1) {
922 errc(1, errno, "switching vty");
928 * Sets the border color.
932 set_border_color(char *arg)
936 if ((color = get_color_number(arg)) != -1) {
937 fprintf(stderr, "\033[=%dA", color);
944 set_mouse_char(char *arg)
946 struct mouse_info mouse;
949 l = strtol(arg, NULL, 0);
951 if ((l < 0) || (l > UCHAR_MAX - 3)) {
953 warnx("argument to -M must be 0 through %d", UCHAR_MAX - 3);
957 mouse.operation = MOUSE_MOUSECHAR;
958 mouse.u.mouse_char = (int)l;
960 if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) {
962 errc(1, errno, "setting mouse character");
968 * Show/hide the mouse.
974 struct mouse_info mouse;
976 if (!strcmp(arg, "on")) {
977 mouse.operation = MOUSE_SHOW;
978 } else if (!strcmp(arg, "off")) {
979 mouse.operation = MOUSE_HIDE;
982 errx(1, "argument to -m must be either on or off");
985 if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) {
987 errc(1, errno, "%sing the mouse",
988 mouse.operation == MOUSE_SHOW ? "show" : "hid");
994 set_lockswitch(char *arg)
998 if (!strcmp(arg, "off")) {
1000 } else if (!strcmp(arg, "on")) {
1004 errx(1, "argument to -S must be either on or off");
1007 if (ioctl(0, VT_LOCKSWITCH, &data) == -1) {
1009 errc(1, errno, "turning %s vty switching",
1010 data == 0x01 ? "off" : "on");
1016 * Return the adapter name for a specified type.
1020 *adapter_name(int type)
1027 { KD_HERCULES, "Hercules" },
1031 { KD_PC98, "PC-98xx" },
1038 for (i = 0; names[i].type != -1; ++i)
1039 if (names[i].type == type)
1041 return names[i].name;
1046 * Show graphics adapter information.
1050 show_adapter_info(void)
1052 struct video_adapter_info ad;
1056 if (ioctl(0, CONS_ADPINFO, &ad) == -1) {
1058 errc(1, errno, "obtaining adapter information");
1061 printf("fb%d:\n", ad.va_index);
1062 printf(" %.*s%d, type:%s%s (%d), flags:0x%x\n",
1063 (int)sizeof(ad.va_name), ad.va_name, ad.va_unit,
1064 (ad.va_flags & V_ADP_VESA) ? "VESA " : "",
1065 adapter_name(ad.va_type), ad.va_type, ad.va_flags);
1066 printf(" initial mode:%d, current mode:%d, BIOS mode:%d\n",
1067 ad.va_initial_mode, ad.va_mode, ad.va_initial_bios_mode);
1068 printf(" frame buffer window:0x%zx, buffer size:0x%zx\n",
1069 ad.va_window, ad.va_buffer_size);
1070 printf(" window size:0x%zx, origin:0x%x\n",
1071 ad.va_window_size, ad.va_window_orig);
1072 printf(" display start address (%d, %d), scan line width:%d\n",
1073 ad.va_disp_start.x, ad.va_disp_start.y, ad.va_line_width);
1074 printf(" reserved:0x%zx\n", ad.va_unused0);
1079 * Show video mode information.
1083 show_mode_info(void)
1086 struct video_info _info;
1091 printf(" mode# flags type size "
1092 "font window linear buffer\n");
1093 printf("---------------------------------------"
1094 "---------------------------------------\n");
1096 for (mode = 0; mode <= M_VESA_MODE_MAX; ++mode) {
1097 _info.vi_mode = mode;
1098 if (ioctl(0, CONS_MODEINFO, &_info))
1100 if (_info.vi_mode != mode)
1103 printf("%3d (0x%03x)", mode, mode);
1104 printf(" 0x%08x", _info.vi_flags);
1105 if (_info.vi_flags & V_INFO_GRAPHICS) {
1108 if (_info.vi_mem_model == V_INFO_MM_PLANAR)
1109 snprintf(buf, sizeof(buf), "%dx%dx%d %d",
1110 _info.vi_width, _info.vi_height,
1111 _info.vi_depth, _info.vi_planes);
1113 switch (_info.vi_mem_model) {
1114 case V_INFO_MM_PACKED:
1117 case V_INFO_MM_DIRECT:
1126 case V_INFO_MM_VGAX:
1133 snprintf(buf, sizeof(buf), "%dx%dx%d %c",
1134 _info.vi_width, _info.vi_height,
1135 _info.vi_depth, mm);
1140 snprintf(buf, sizeof(buf), "%dx%d",
1141 _info.vi_width, _info.vi_height);
1144 printf(" %c %-15s", c, buf);
1145 snprintf(buf, sizeof(buf), "%dx%d",
1146 _info.vi_cwidth, _info.vi_cheight);
1147 printf(" %-5s", buf);
1148 printf(" 0x%05zx %2dk %2dk",
1149 _info.vi_window, (int)_info.vi_window_size/1024,
1150 (int)_info.vi_window_gran/1024);
1151 printf(" 0x%08zx %dk\n",
1152 _info.vi_buffer, (int)_info.vi_buffer_size/1024);
1158 show_info(char *arg)
1160 if (!strcmp(arg, "adapter")) {
1161 show_adapter_info();
1162 } else if (!strcmp(arg, "mode")) {
1166 errx(1, "argument to -i must be either adapter or mode");
1174 int i, cur_mode, fore;
1178 if (ioctl(0, CONS_GET, &cur_mode) < 0)
1179 err(1, "must be on a virtual console");
1187 fprintf(stdout, "\033[=0G\n\n");
1188 for (i=0; i<8; i++) {
1189 fprintf(stdout, "\033[=%dF\033[=0G %2d \033[=%dF%-16s"
1190 "\033[=%dF\033[=0G %2d \033[=%dF%-16s "
1191 "\033[=%dF %2d \033[=%dGBACKGROUND\033[=0G\n",
1192 fore, i, i, legal_colors[i],
1193 fore, i+8, i+8, legal_colors[i+8],
1196 fprintf(stdout, "\033[=%dF\033[=%dG\033[=%dH\033[=%dI\n",
1197 info.mv_norm.fore, info.mv_norm.back,
1198 info.mv_rev.fore, info.mv_rev.back);
1203 * Snapshot the video memory of that terminal, using the CONS_SCRSHOT
1204 * ioctl, and writes the results to stdout either in the special
1205 * binary format (see manual page for details), or in the plain
1210 dump_screen(int mode, int opt)
1215 _info.size = sizeof(_info);
1217 if (ioctl(0, CONS_GETINFO, &_info) == -1) {
1219 errc(1, errno, "obtaining current video mode parameters");
1223 shot.x = shot.y = 0;
1224 shot.xsize = _info.mv_csz;
1225 shot.ysize = _info.mv_rsz;
1226 if (opt == DUMP_ALL)
1227 shot.ysize += _info.mv_hsz;
1229 shot.buf = alloca(shot.xsize * shot.ysize * sizeof(u_int16_t));
1230 if (shot.buf == NULL) {
1232 errx(1, "failed to allocate memory for dump");
1235 if (ioctl(0, CONS_SCRSHOT, &shot) == -1) {
1237 errc(1, errno, "dumping screen");
1240 if (mode == DUMP_FMT_RAW) {
1241 printf("SCRSHOT_%c%c%c%c", DUMP_FMT_REV, 2,
1242 shot.xsize, shot.ysize);
1246 write(STDOUT_FILENO, shot.buf,
1247 shot.xsize * shot.ysize * sizeof(u_int16_t));
1253 line = alloca(shot.xsize + 1);
1257 errx(1, "failed to allocate memory for line buffer");
1260 for (y = 0; y < shot.ysize; y++) {
1261 for (x = 0; x < shot.xsize; x++) {
1262 ch = shot.buf[x + (y * shot.xsize)];
1265 if (isprint(ch) == 0)
1271 /* Trim trailing spaces */
1275 } while (line[x] == ' ' && x != 0);
1286 * Set the console history buffer size.
1290 set_history(char *opt)
1296 if ((*opt == '\0') || size < 0) {
1298 errx(1, "argument must be a positive number");
1301 if (ioctl(0, CONS_HISTORY, &size) == -1) {
1303 errc(1, errno, "setting history buffer size");
1309 * Clear the console history buffer.
1315 if (ioctl(0, CONS_CLRHIST) == -1) {
1317 errc(1, errno, "clearing history buffer");
1322 set_terminal_mode(char *arg)
1325 if (strcmp(arg, "xterm") == 0)
1326 fprintf(stderr, "\033[=T");
1327 else if (strcmp(arg, "cons25") == 0)
1328 fprintf(stderr, "\033[=1T");
1333 main(int argc, char **argv)
1335 char *font, *type, *termmode;
1337 int dumpmod, dumpopt, opt;
1340 vt4_mode = is_vt4();
1344 info.size = sizeof(info);
1346 if (ioctl(0, CONS_GETINFO, &info) == -1)
1347 err(1, "must be on a virtual console");
1352 opts = "b:Cc:fg:h:Hi:M:m:pPr:S:s:T:t:x";
1354 opts = "b:Cc:dfg:h:Hi:l:LM:m:pPr:S:s:T:t:x";
1356 while ((opt = getopt(argc, argv, opts)) != -1)
1359 set_border_color(optarg);
1365 set_cursor_type(optarg);
1373 optarg = nextarg(argc, argv, &optind, 'f', 0);
1374 if (optarg != NULL) {
1375 font = nextarg(argc, argv, &optind, 'f', 0);
1383 load_font(type, font);
1386 usage(); /* Switch syscons to ROM? */
1388 load_default_vt4font();
1392 if (sscanf(optarg, "%dx%d",
1393 &vesa_cols, &vesa_rows) != 2) {
1395 warnx("incorrect geometry: %s", optarg);
1400 set_history(optarg);
1411 load_scrnmap(optarg);
1416 load_default_scrnmap();
1419 set_mouse_char(optarg);
1425 dumpmod = DUMP_FMT_RAW;
1428 dumpmod = DUMP_FMT_TXT;
1431 get_reverse_colors(argc, argv, &optind);
1434 set_lockswitch(optarg);
1437 set_console(optarg);
1440 if (strcmp(optarg, "xterm") != 0 &&
1441 strcmp(optarg, "cons25") != 0)
1446 set_screensaver_timeout(optarg);
1456 dump_screen(dumpmod, dumpopt);
1457 reterr = video_mode(argc, argv, &optind);
1458 get_normal_colors(argc, argv, &optind);
1460 if (optind < argc && !strcmp(argv[optind], "show")) {
1465 video_mode(argc, argv, &optind);
1466 if (termmode != NULL)
1467 set_terminal_mode(termmode);
1469 get_normal_colors(argc, argv, &optind);
1471 if (colors_changed || video_mode_changed) {
1472 if (!(new_mode_info.vi_flags & V_INFO_GRAPHICS)) {
1473 if ((normal_back_color < 8) && (revers_back_color < 8)) {
1477 errx(1, "bg color for text modes must be < 8");
1484 if ((optind != argc) || (argc == 1))