2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1994-1996 Søren Schmidt
7 * Portions of this software are based in part on the work of
8 * Sascha Wildner <saw@online.de> contributed to The DragonFly Project
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer,
15 * in this position and unchanged.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * $DragonFly: src/usr.sbin/vidcontrol/vidcontrol.c,v 1.10 2005/03/02 06:08:29 joerg Exp $
37 static const char rcsid[] =
49 #include <sys/consio.h>
50 #include <sys/endian.h>
51 #include <sys/errno.h>
52 #include <sys/param.h>
53 #include <sys/types.h>
55 #include <sys/sysctl.h>
60 #define DATASIZE(x) ((x).w * (x).h * 256 / 8)
62 /* Screen dump modes */
63 #define DUMP_FMT_RAW 1
64 #define DUMP_FMT_TXT 2
65 /* Screen dump options */
68 /* Screen dump file format revision */
69 #define DUMP_FMT_REV 1
71 static const char *legal_colors[16] = {
72 "black", "blue", "green", "cyan",
73 "red", "magenta", "brown", "white",
74 "grey", "lightblue", "lightgreen", "lightcyan",
75 "lightred", "lightmagenta", "yellow", "lightwhite"
80 vid_info_t console_info;
81 unsigned char screen_map[256];
82 int video_mode_number;
83 struct video_info video_mode_info;
86 struct vt4font_header {
92 uint32_t map_count[4];
98 static int font_height;
99 static int colors_changed;
100 static int video_mode_changed;
101 static int normal_fore_color, normal_back_color;
102 static int revers_fore_color, revers_back_color;
103 static int vt4_mode = 0;
104 static struct vid_info info;
105 static struct video_info new_mode_info;
109 * Initialize revert data.
111 * NOTE: the following parameters are not yet saved/restored:
113 * screen saver timeout
115 * mouse character and mouse show/hide state
116 * vty switching on/off state
117 * history buffer size
125 if (ioctl(0, VT_GETACTIVE, &cur_info.active_vty) == -1)
126 errc(1, errno, "getting active vty");
128 cur_info.console_info.size = sizeof(cur_info.console_info);
130 if (ioctl(0, CONS_GETINFO, &cur_info.console_info) == -1)
131 errc(1, errno, "getting console information");
133 /* vt(4) use unicode, so no screen mapping required. */
135 ioctl(0, GIO_SCRNMAP, &cur_info.screen_map) == -1)
136 errc(1, errno, "getting screen map");
138 if (ioctl(0, CONS_GET, &cur_info.video_mode_number) == -1)
139 errc(1, errno, "getting video mode number");
141 cur_info.video_mode_info.vi_mode = cur_info.video_mode_number;
143 if (ioctl(0, CONS_MODEINFO, &cur_info.video_mode_info) == -1)
144 errc(1, errno, "getting video mode parameters");
146 normal_fore_color = cur_info.console_info.mv_norm.fore;
147 normal_back_color = cur_info.console_info.mv_norm.back;
148 revers_fore_color = cur_info.console_info.mv_rev.fore;
149 revers_back_color = cur_info.console_info.mv_rev.back;
154 * If something goes wrong along the way we call revert() to go back to the
155 * console state we came from (which is assumed to be working).
157 * NOTE: please also read the comments of init().
165 ioctl(0, VT_ACTIVATE, cur_info.active_vty);
167 fprintf(stderr, "\033[=%dA", cur_info.console_info.mv_ovscan);
168 fprintf(stderr, "\033[=%dF", cur_info.console_info.mv_norm.fore);
169 fprintf(stderr, "\033[=%dG", cur_info.console_info.mv_norm.back);
170 fprintf(stderr, "\033[=%dH", cur_info.console_info.mv_rev.fore);
171 fprintf(stderr, "\033[=%dI", cur_info.console_info.mv_rev.back);
174 ioctl(0, PIO_SCRNMAP, &cur_info.screen_map);
176 if (cur_info.video_mode_number >= M_VESA_BASE)
177 ioctl(0, _IO('V', cur_info.video_mode_number - M_VESA_BASE),
180 ioctl(0, _IO('S', cur_info.video_mode_number), NULL);
182 if (cur_info.video_mode_info.vi_flags & V_INFO_GRAPHICS) {
183 size[0] = cur_info.video_mode_info.vi_width / 8;
184 size[1] = cur_info.video_mode_info.vi_height /
185 cur_info.console_info.font_size;
186 size[2] = cur_info.console_info.font_size;
188 ioctl(0, KDRASTER, size);
194 * Print a short usage string describing all options, then exit.
201 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
202 "usage: vidcontrol [-CHPpx] [-b color] [-c appearance] [-f [[size] file]]",
203 " [-g geometry] [-h size] [-i active | adapter | mode]",
204 " [-M char] [-m on | off] [-r foreground background]",
205 " [-S on | off] [-s number] [-T xterm | cons25] [-t N | off]",
206 " [mode] [foreground [background]] [show]");
208 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
209 "usage: vidcontrol [-CdHLPpx] [-b color] [-c appearance] [-f [size] file]",
210 " [-g geometry] [-h size] [-i adapter | mode] [-l screen_map]",
211 " [-M char] [-m on | off] [-r foreground background]",
212 " [-S on | off] [-s number] [-T xterm | cons25] [-t N | off]",
213 " [mode] [foreground [background]] [show]");
217 /* Detect presence of vt(4). */
221 char vty_name[4] = "";
222 size_t len = sizeof(vty_name);
224 if (sysctlbyname("kern.vty", vty_name, &len, NULL, 0) != 0)
226 return (strcmp(vty_name, "vt") == 0);
230 * Retrieve the next argument from the command line (for options that require
231 * more than one argument).
235 nextarg(int ac, char **av, int *indp, int oc, int strict)
238 return(av[(*indp)++]);
242 errx(1, "option requires two arguments -- %c", oc);
250 * Guess which file to open. Try to open each combination of a specified set
251 * of file name components.
255 openguess(const char *a[], const char *b[], const char *c[], const char *d[], char **name)
260 for (i = 0; a[i] != NULL; i++) {
261 for (j = 0; b[j] != NULL; j++) {
262 for (k = 0; c[k] != NULL; k++) {
263 for (l = 0; d[l] != NULL; l++) {
264 asprintf(name, "%s%s%s%s",
265 a[i], b[j], c[k], d[l]);
267 f = fopen(*name, "r");
282 * Load a screenmap from a file and set it.
286 load_scrnmap(const char *filename)
292 const char *a[] = {"", SCRNMAP_PATH, NULL};
293 const char *b[] = {filename, NULL};
294 const char *c[] = {"", ".scm", NULL};
295 const char *d[] = {"", NULL};
297 fd = openguess(a, b, c, d, &name);
301 errx(1, "screenmap file not found");
304 size = sizeof(scrnmap);
306 if (decode(fd, (char *)&scrnmap, size) != size) {
309 if (fread(&scrnmap, 1, size, fd) != (size_t)size) {
310 warnx("bad screenmap file");
313 errx(1, "bad screenmap file");
317 if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
319 errc(1, errno, "loading screenmap");
327 * Set the default screenmap.
331 load_default_scrnmap(void)
336 for (i=0; i<256; i++)
337 *((char*)&scrnmap + i) = i;
339 if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
341 errc(1, errno, "loading default screenmap");
347 * Print the current screenmap to stdout.
353 unsigned char map[256];
356 if (ioctl(0, GIO_SCRNMAP, &map) == -1) {
358 errc(1, errno, "getting screenmap");
360 for (i=0; i<sizeof(map); i++) {
361 if (i != 0 && i % 16 == 0)
362 fprintf(stdout, "\n");
365 fprintf(stdout, " %02x", map[i]);
367 fprintf(stdout, " %03d", map[i]);
369 fprintf(stdout, "\n");
375 * Determine a file's size.
383 if (fstat(fileno(file), &sb) == 0)
390 load_vt4mappingtable(unsigned int nmappings, FILE *f)
398 if ((t = malloc(sizeof *t * nmappings)) == NULL) {
403 if (fread(t, sizeof *t * nmappings, 1, f) != 1) {
404 warn("read mappings");
409 for (i = 0; i < nmappings; i++) {
410 t[i].src = be32toh(t[i].src);
411 t[i].dst = be16toh(t[i].dst);
412 t[i].len = be16toh(t[i].len);
419 * Set the default vt font.
423 load_default_vt4font(void)
425 if (ioctl(0, PIO_VFONT_DEFAULT) == -1) {
427 errc(1, errno, "loading default vt font");
432 load_vt4font(FILE *f)
434 struct vt4font_header fh;
439 if (fread(&fh, sizeof fh, 1, f) != 1) {
440 warn("read file_header");
444 if (memcmp(fh.magic, "VFNT0002", 8) != 0) {
445 warnx("bad magic in font file\n");
449 for (i = 0; i < VFNT_MAPS; i++)
450 vfnt.map_count[i] = be32toh(fh.map_count[i]);
451 vfnt.glyph_count = be32toh(fh.glyph_count);
452 vfnt.width = fh.width;
453 vfnt.height = fh.height;
455 glyphsize = howmany(vfnt.width, 8) * vfnt.height * vfnt.glyph_count;
456 if ((vfnt.glyphs = malloc(glyphsize)) == NULL) {
461 if (fread(vfnt.glyphs, glyphsize, 1, f) != 1) {
467 for (i = 0; i < VFNT_MAPS; i++)
468 vfnt.map[i] = load_vt4mappingtable(vfnt.map_count[i], f);
470 if (ioctl(STDIN_FILENO, PIO_VFONT, &vfnt) == -1)
473 for (i = 0; i < VFNT_MAPS; i++)
479 * Load a font from file and set it.
483 load_font(const char *type, const char *filename)
487 unsigned long io = 0; /* silence stupid gcc(1) in the Wall mode */
488 char *name, *fontmap, size_sufx[6];
489 const char *a[] = {"", FONT_PATH, NULL};
490 const char *vt4a[] = {"", VT_FONT_PATH, NULL};
491 const char *b[] = {filename, NULL};
492 const char *c[] = {"", size_sufx, NULL};
493 const char *d[] = {"", ".fnt", NULL};
500 } sizes[] = {{8, 16, PIO_FONT8x16},
501 {8, 14, PIO_FONT8x14},
508 _info.size = sizeof(_info);
509 if (ioctl(0, CONS_GETINFO, &_info) == -1) {
511 warn("failed to obtain current video mode parameters");
515 snprintf(size_sufx, sizeof(size_sufx), "-8x%d", _info.font_size);
517 fd = openguess((vt4_mode == 0) ? a : vt4a, b, c, d, &name);
521 errx(1, "%s: can't load font file", filename);
532 if (sscanf(type, "%dx%d", &w, &h) == 2) {
533 for (i = 0; sizes[i].w != 0; i++) {
534 if (sizes[i].w == w && sizes[i].h == h) {
535 size = DATASIZE(sizes[i]);
537 font_height = sizes[i].h;
544 errx(1, "%s: bad font size specification", type);
547 /* Apply heuristics */
552 size = DATASIZE(sizes[0]);
553 fontmap = (char*) malloc(size);
554 dsize[0] = decode(fd, fontmap, size);
555 dsize[1] = fsize(fd);
559 for (j = 0; j < 2; j++) {
560 for (i = 0; sizes[i].w != 0; i++) {
561 if (DATASIZE(sizes[i]) == dsize[j]) {
564 font_height = sizes[i].h;
574 errx(1, "%s: can't guess font size", filename);
580 fontmap = (char*) malloc(size);
582 if (decode(fd, fontmap, size) != size) {
584 if (fsize(fd) != size ||
585 fread(fontmap, 1, size, fd) != (size_t)size) {
586 warnx("%s: bad font file", filename);
590 errx(1, "%s: bad font file", filename);
594 if (ioctl(0, io, fontmap) == -1) {
596 errc(1, errno, "loading font");
605 * Set the timeout for the screensaver.
609 set_screensaver_timeout(char *arg)
613 if (!strcmp(arg, "off")) {
618 if ((*arg == '\0') || (nsec < 1)) {
620 errx(1, "argument must be a positive number");
624 if (ioctl(0, CONS_BLANKTIME, &nsec) == -1) {
626 errc(1, errno, "setting screensaver period");
632 * Set the cursor's shape/type.
636 set_cursor_type(char *appearance)
640 if (!strcmp(appearance, "normal"))
642 else if (!strcmp(appearance, "blink"))
644 else if (!strcmp(appearance, "destructive"))
648 errx(1, "argument to -c must be normal, blink or destructive");
651 if (ioctl(0, CONS_CURSORTYPE, &type) == -1) {
653 errc(1, errno, "setting cursor type");
659 * Set the video mode.
663 video_mode(int argc, char **argv, int *mode_index)
668 unsigned long mode_num;
670 { "80x25", SW_TEXT_80x25, M_TEXT_80x25 },
671 { "80x30", SW_TEXT_80x30, M_TEXT_80x30 },
672 { "80x43", SW_TEXT_80x43, M_TEXT_80x43 },
673 { "80x50", SW_TEXT_80x50, M_TEXT_80x50 },
674 { "80x60", SW_TEXT_80x60, M_TEXT_80x60 },
675 { "132x25", SW_TEXT_132x25, M_TEXT_132x25 },
676 { "132x30", SW_TEXT_132x30, M_TEXT_132x30 },
677 { "132x43", SW_TEXT_132x43, M_TEXT_132x43 },
678 { "132x50", SW_TEXT_132x50, M_TEXT_132x50 },
679 { "132x60", SW_TEXT_132x60, M_TEXT_132x60 },
680 { "VGA_40x25", SW_VGA_C40x25, M_VGA_C40x25 },
681 { "VGA_80x25", SW_VGA_C80x25, M_VGA_C80x25 },
682 { "VGA_80x30", SW_VGA_C80x30, M_VGA_C80x30 },
683 { "VGA_80x50", SW_VGA_C80x50, M_VGA_C80x50 },
684 { "VGA_80x60", SW_VGA_C80x60, M_VGA_C80x60 },
686 { "VGA_90x25", SW_VGA_C90x25, M_VGA_C90x25 },
687 { "VGA_90x30", SW_VGA_C90x30, M_VGA_C90x30 },
688 { "VGA_90x43", SW_VGA_C90x43, M_VGA_C90x43 },
689 { "VGA_90x50", SW_VGA_C90x50, M_VGA_C90x50 },
690 { "VGA_90x60", SW_VGA_C90x60, M_VGA_C90x60 },
692 { "VGA_320x200", SW_VGA_CG320, M_CG320 },
693 { "EGA_80x25", SW_ENH_C80x25, M_ENH_C80x25 },
694 { "EGA_80x43", SW_ENH_C80x43, M_ENH_C80x43 },
695 { "VESA_132x25", SW_VESA_C132x25,M_VESA_C132x25 },
696 { "VESA_132x43", SW_VESA_C132x43,M_VESA_C132x43 },
697 { "VESA_132x50", SW_VESA_C132x50,M_VESA_C132x50 },
698 { "VESA_132x60", SW_VESA_C132x60,M_VESA_C132x60 },
699 { "VESA_800x600", SW_VESA_800x600,M_VESA_800x600 },
703 int new_mode_num = 0;
704 unsigned long mode = 0;
710 if (ioctl(0, CONS_GET, &cur_mode) < 0)
711 err(1, "cannot get the current video mode");
714 * Parse the video mode argument...
717 if (*mode_index < argc) {
718 if (!strncmp(argv[*mode_index], "MODE_", 5)) {
719 if (!isdigit(argv[*mode_index][5]))
720 errx(1, "invalid video mode number");
722 new_mode_num = atoi(&argv[*mode_index][5]);
724 for (i = 0; modes[i].name != NULL; ++i) {
725 if (!strcmp(argv[*mode_index], modes[i].name)) {
726 mode = modes[i].mode;
727 new_mode_num = modes[i].mode_num;
732 if (modes[i].name == NULL)
734 if (ioctl(0, mode, NULL) < 0) {
735 warn("cannot set videomode");
741 * Collect enough information about the new video mode...
744 new_mode_info.vi_mode = new_mode_num;
746 if (ioctl(0, CONS_MODEINFO, &new_mode_info) == -1) {
748 errc(1, errno, "obtaining new video mode parameters");
752 if (new_mode_num >= M_VESA_BASE)
753 mode = _IO('V', new_mode_num - M_VESA_BASE);
755 mode = _IO('S', new_mode_num);
759 * Try setting the new mode.
762 if (ioctl(0, mode, NULL) == -1) {
764 errc(1, errno, "setting video mode");
768 * For raster modes it's not enough to just set the mode.
769 * We also need to explicitly set the raster mode.
772 if (new_mode_info.vi_flags & V_INFO_GRAPHICS) {
775 if (font_height == 0)
776 font_height = cur_info.console_info.font_size;
778 size[2] = font_height;
782 if ((vesa_cols * 8 > new_mode_info.vi_width) ||
784 size[0] = new_mode_info.vi_width / 8;
791 if ((vesa_rows * font_height > new_mode_info.vi_height) ||
793 size[1] = new_mode_info.vi_height /
799 /* set raster mode */
801 if (ioctl(0, KDRASTER, size)) {
803 if (cur_mode >= M_VESA_BASE)
805 _IO('V', cur_mode - M_VESA_BASE),
808 ioctl(0, _IO('S', cur_mode), NULL);
810 warnc(ioerr, "cannot activate raster display");
815 video_mode_changed = 1;
824 * Return the number for a specified color name.
828 get_color_number(char *color)
832 for (i=0; i<16; i++) {
833 if (!strcmp(color, legal_colors[i]))
841 * Get normal text and background colors.
845 get_normal_colors(int argc, char **argv, int *_index)
849 if (*_index < argc && (color = get_color_number(argv[*_index])) != -1) {
851 fprintf(stderr, "\033[=%dF", color);
852 normal_fore_color=color;
855 && (color = get_color_number(argv[*_index])) != -1) {
857 fprintf(stderr, "\033[=%dG", color);
858 normal_back_color=color;
865 * Get reverse text and background colors.
869 get_reverse_colors(int argc, char **argv, int *_index)
873 if ((color = get_color_number(argv[*(_index)-1])) != -1) {
874 fprintf(stderr, "\033[=%dH", color);
875 revers_fore_color=color;
878 && (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 active VTY, ie current console number.
1050 show_active_info(void)
1053 printf("%d\n", cur_info.active_vty);
1058 * Show graphics adapter information.
1062 show_adapter_info(void)
1064 struct video_adapter_info ad;
1068 if (ioctl(0, CONS_ADPINFO, &ad) == -1) {
1070 errc(1, errno, "obtaining adapter information");
1073 printf("fb%d:\n", ad.va_index);
1074 printf(" %.*s%d, type:%s%s (%d), flags:0x%x\n",
1075 (int)sizeof(ad.va_name), ad.va_name, ad.va_unit,
1076 (ad.va_flags & V_ADP_VESA) ? "VESA " : "",
1077 adapter_name(ad.va_type), ad.va_type, ad.va_flags);
1078 printf(" initial mode:%d, current mode:%d, BIOS mode:%d\n",
1079 ad.va_initial_mode, ad.va_mode, ad.va_initial_bios_mode);
1080 printf(" frame buffer window:0x%zx, buffer size:0x%zx\n",
1081 ad.va_window, ad.va_buffer_size);
1082 printf(" window size:0x%zx, origin:0x%x\n",
1083 ad.va_window_size, ad.va_window_orig);
1084 printf(" display start address (%d, %d), scan line width:%d\n",
1085 ad.va_disp_start.x, ad.va_disp_start.y, ad.va_line_width);
1086 printf(" reserved:0x%zx\n", ad.va_unused0);
1091 * Show video mode information.
1095 show_mode_info(void)
1098 struct video_info _info;
1103 printf(" mode# flags type size "
1104 "font window linear buffer\n");
1105 printf("---------------------------------------"
1106 "---------------------------------------\n");
1108 memset(&_info, 0, sizeof(_info));
1109 for (mode = 0; mode <= M_VESA_MODE_MAX; ++mode) {
1110 _info.vi_mode = mode;
1111 if (ioctl(0, CONS_MODEINFO, &_info))
1113 if (_info.vi_mode != mode)
1115 if (_info.vi_width == 0 && _info.vi_height == 0 &&
1116 _info.vi_cwidth == 0 && _info.vi_cheight == 0)
1119 printf("%3d (0x%03x)", mode, mode);
1120 printf(" 0x%08x", _info.vi_flags);
1121 if (_info.vi_flags & V_INFO_GRAPHICS) {
1124 if (_info.vi_mem_model == V_INFO_MM_PLANAR)
1125 snprintf(buf, sizeof(buf), "%dx%dx%d %d",
1126 _info.vi_width, _info.vi_height,
1127 _info.vi_depth, _info.vi_planes);
1129 switch (_info.vi_mem_model) {
1130 case V_INFO_MM_PACKED:
1133 case V_INFO_MM_DIRECT:
1142 case V_INFO_MM_VGAX:
1149 snprintf(buf, sizeof(buf), "%dx%dx%d %c",
1150 _info.vi_width, _info.vi_height,
1151 _info.vi_depth, mm);
1156 snprintf(buf, sizeof(buf), "%dx%d",
1157 _info.vi_width, _info.vi_height);
1160 printf(" %c %-15s", c, buf);
1161 snprintf(buf, sizeof(buf), "%dx%d",
1162 _info.vi_cwidth, _info.vi_cheight);
1163 printf(" %-5s", buf);
1164 printf(" 0x%05zx %2dk %2dk",
1165 _info.vi_window, (int)_info.vi_window_size/1024,
1166 (int)_info.vi_window_gran/1024);
1167 printf(" 0x%08zx %dk\n",
1168 _info.vi_buffer, (int)_info.vi_buffer_size/1024);
1174 show_info(char *arg)
1177 if (!strcmp(arg, "active")) {
1179 } else if (!strcmp(arg, "adapter")) {
1180 show_adapter_info();
1181 } else if (!strcmp(arg, "mode")) {
1185 errx(1, "argument to -i must be active, adapter, or mode");
1193 int i, cur_mode, fore;
1197 if (ioctl(0, CONS_GET, &cur_mode) < 0)
1198 err(1, "must be on a virtual console");
1206 fprintf(stdout, "\033[=0G\n\n");
1207 for (i=0; i<8; i++) {
1208 fprintf(stdout, "\033[=%dF\033[=0G %2d \033[=%dF%-16s"
1209 "\033[=%dF\033[=0G %2d \033[=%dF%-16s "
1210 "\033[=%dF %2d \033[=%dGBACKGROUND\033[=0G\n",
1211 fore, i, i, legal_colors[i],
1212 fore, i+8, i+8, legal_colors[i+8],
1215 fprintf(stdout, "\033[=%dF\033[=%dG\033[=%dH\033[=%dI\n",
1216 info.mv_norm.fore, info.mv_norm.back,
1217 info.mv_rev.fore, info.mv_rev.back);
1222 * Snapshot the video memory of that terminal, using the CONS_SCRSHOT
1223 * ioctl, and writes the results to stdout either in the special
1224 * binary format (see manual page for details), or in the plain
1229 dump_screen(int mode, int opt)
1234 _info.size = sizeof(_info);
1236 if (ioctl(0, CONS_GETINFO, &_info) == -1) {
1238 errc(1, errno, "obtaining current video mode parameters");
1242 shot.x = shot.y = 0;
1243 shot.xsize = _info.mv_csz;
1244 shot.ysize = _info.mv_rsz;
1245 if (opt == DUMP_ALL)
1246 shot.ysize += _info.mv_hsz;
1248 shot.buf = alloca(shot.xsize * shot.ysize * sizeof(u_int16_t));
1249 if (shot.buf == NULL) {
1251 errx(1, "failed to allocate memory for dump");
1254 if (ioctl(0, CONS_SCRSHOT, &shot) == -1) {
1256 errc(1, errno, "dumping screen");
1259 if (mode == DUMP_FMT_RAW) {
1260 printf("SCRSHOT_%c%c%c%c", DUMP_FMT_REV, 2,
1261 shot.xsize, shot.ysize);
1265 write(STDOUT_FILENO, shot.buf,
1266 shot.xsize * shot.ysize * sizeof(u_int16_t));
1272 line = alloca(shot.xsize + 1);
1276 errx(1, "failed to allocate memory for line buffer");
1279 for (y = 0; y < shot.ysize; y++) {
1280 for (x = 0; x < shot.xsize; x++) {
1281 ch = shot.buf[x + (y * shot.xsize)];
1284 if (isprint(ch) == 0)
1290 /* Trim trailing spaces */
1294 } while (line[x] == ' ' && x != 0);
1305 * Set the console history buffer size.
1309 set_history(char *opt)
1315 if ((*opt == '\0') || size < 0) {
1317 errx(1, "argument must be a positive number");
1320 if (ioctl(0, CONS_HISTORY, &size) == -1) {
1322 errc(1, errno, "setting history buffer size");
1328 * Clear the console history buffer.
1334 if (ioctl(0, CONS_CLRHIST) == -1) {
1336 errc(1, errno, "clearing history buffer");
1341 set_terminal_mode(char *arg)
1344 if (strcmp(arg, "xterm") == 0)
1345 fprintf(stderr, "\033[=T");
1346 else if (strcmp(arg, "cons25") == 0)
1347 fprintf(stderr, "\033[=1T");
1352 main(int argc, char **argv)
1354 char *font, *type, *termmode;
1356 int dumpmod, dumpopt, opt;
1359 vt4_mode = is_vt4();
1363 info.size = sizeof(info);
1365 if (ioctl(0, CONS_GETINFO, &info) == -1)
1366 err(1, "must be on a virtual console");
1371 opts = "b:Cc:fg:h:Hi:M:m:pPr:S:s:T:t:x";
1373 opts = "b:Cc:dfg:h:Hi:l:LM:m:pPr:S:s:T:t:x";
1375 while ((opt = getopt(argc, argv, opts)) != -1)
1378 set_border_color(optarg);
1384 set_cursor_type(optarg);
1392 optarg = nextarg(argc, argv, &optind, 'f', 0);
1393 if (optarg != NULL) {
1394 font = nextarg(argc, argv, &optind, 'f', 0);
1402 load_font(type, font);
1405 usage(); /* Switch syscons to ROM? */
1407 load_default_vt4font();
1411 if (sscanf(optarg, "%dx%d",
1412 &vesa_cols, &vesa_rows) != 2) {
1414 warnx("incorrect geometry: %s", optarg);
1419 set_history(optarg);
1430 load_scrnmap(optarg);
1435 load_default_scrnmap();
1438 set_mouse_char(optarg);
1444 dumpmod = DUMP_FMT_RAW;
1447 dumpmod = DUMP_FMT_TXT;
1450 get_reverse_colors(argc, argv, &optind);
1453 set_lockswitch(optarg);
1456 set_console(optarg);
1459 if (strcmp(optarg, "xterm") != 0 &&
1460 strcmp(optarg, "cons25") != 0)
1465 set_screensaver_timeout(optarg);
1475 dump_screen(dumpmod, dumpopt);
1476 reterr = video_mode(argc, argv, &optind);
1477 get_normal_colors(argc, argv, &optind);
1479 if (optind < argc && !strcmp(argv[optind], "show")) {
1484 video_mode(argc, argv, &optind);
1485 if (termmode != NULL)
1486 set_terminal_mode(termmode);
1488 get_normal_colors(argc, argv, &optind);
1490 if (colors_changed || video_mode_changed)
1493 if ((optind != argc) || (argc == 1))