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 vt4_mode = 0;
98 static int video_mode_changed;
99 static struct video_info new_mode_info;
103 * Initialize revert data.
105 * NOTE: the following parameters are not yet saved/restored:
107 * screen saver timeout
109 * mouse character and mouse show/hide state
110 * vty switching on/off state
111 * history buffer size
119 if (ioctl(0, VT_GETACTIVE, &cur_info.active_vty) == -1)
120 err(1, "getting active vty");
122 cur_info.console_info.size = sizeof(cur_info.console_info);
123 if (ioctl(0, CONS_GETINFO, &cur_info.console_info) == -1)
124 err(1, "getting console information");
126 /* vt(4) use unicode, so no screen mapping required. */
128 ioctl(0, GIO_SCRNMAP, &cur_info.screen_map) == -1)
129 err(1, "getting screen map");
131 if (ioctl(0, CONS_GET, &cur_info.video_mode_number) == -1)
132 err(1, "getting video mode number");
134 cur_info.video_mode_info.vi_mode = cur_info.video_mode_number;
136 if (ioctl(0, CONS_MODEINFO, &cur_info.video_mode_info) == -1)
137 err(1, "getting video mode parameters");
142 * If something goes wrong along the way we call revert() to go back to the
143 * console state we came from (which is assumed to be working).
145 * NOTE: please also read the comments of init().
151 int save_errno, size[3];
155 ioctl(0, VT_ACTIVATE, cur_info.active_vty);
157 fprintf(stderr, "\033[=%dA", cur_info.console_info.mv_ovscan);
158 fprintf(stderr, "\033[=%dH", cur_info.console_info.mv_rev.fore);
159 fprintf(stderr, "\033[=%dI", cur_info.console_info.mv_rev.back);
162 ioctl(0, PIO_SCRNMAP, &cur_info.screen_map);
164 if (video_mode_changed) {
165 if (cur_info.video_mode_number >= M_VESA_BASE)
167 _IO('V', cur_info.video_mode_number - M_VESA_BASE),
170 ioctl(0, _IO('S', cur_info.video_mode_number), NULL);
171 if (cur_info.video_mode_info.vi_flags & V_INFO_GRAPHICS) {
172 size[0] = cur_info.video_mode_info.vi_width / 8;
173 size[1] = cur_info.video_mode_info.vi_height /
174 cur_info.console_info.font_size;
175 size[2] = cur_info.console_info.font_size;
176 ioctl(0, KDRASTER, size);
180 /* Restore some colors last since mode setting forgets some. */
181 fprintf(stderr, "\033[=%dF", cur_info.console_info.mv_norm.fore);
182 fprintf(stderr, "\033[=%dG", cur_info.console_info.mv_norm.back);
189 * Print a short usage string describing all options, then exit.
196 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
197 "usage: vidcontrol [-CHPpx] [-b color] [-c appearance] [-f [[size] file]]",
198 " [-g geometry] [-h size] [-i active | adapter | mode]",
199 " [-M char] [-m on | off] [-r foreground background]",
200 " [-S on | off] [-s number] [-T xterm | cons25] [-t N | off]",
201 " [mode] [foreground [background]] [show]");
203 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
204 "usage: vidcontrol [-CdHLPpx] [-b color] [-c appearance] [-f [size] file]",
205 " [-g geometry] [-h size] [-i adapter | mode] [-l screen_map]",
206 " [-M char] [-m on | off] [-r foreground background]",
207 " [-S on | off] [-s number] [-T xterm | cons25] [-t N | off]",
208 " [mode] [foreground [background]] [show]");
212 /* Detect presence of vt(4). */
216 char vty_name[4] = "";
217 size_t len = sizeof(vty_name);
219 if (sysctlbyname("kern.vty", vty_name, &len, NULL, 0) != 0)
221 return (strcmp(vty_name, "vt") == 0);
225 * Retrieve the next argument from the command line (for options that require
226 * more than one argument).
230 nextarg(int ac, char **av, int *indp, int oc, int strict)
233 return(av[(*indp)++]);
237 errx(1, "option requires two arguments -- %c", oc);
245 * Guess which file to open. Try to open each combination of a specified set
246 * of file name components.
250 openguess(const char *a[], const char *b[], const char *c[], const char *d[], char **name)
255 for (i = 0; a[i] != NULL; i++) {
256 for (j = 0; b[j] != NULL; j++) {
257 for (k = 0; c[k] != NULL; k++) {
258 for (l = 0; d[l] != NULL; l++) {
259 asprintf(name, "%s%s%s%s",
260 a[i], b[j], c[k], d[l]);
262 f = fopen(*name, "r");
277 * Load a screenmap from a file and set it.
281 load_scrnmap(const char *filename)
287 const char *a[] = {"", SCRNMAP_PATH, NULL};
288 const char *b[] = {filename, NULL};
289 const char *c[] = {"", ".scm", NULL};
290 const char *d[] = {"", NULL};
292 fd = openguess(a, b, c, d, &name);
296 errx(1, "screenmap file not found");
299 size = sizeof(scrnmap);
301 if (decode(fd, (char *)&scrnmap, size) != size) {
304 if (fread(&scrnmap, 1, size, fd) != (size_t)size) {
307 errx(1, "bad screenmap file");
311 if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
313 err(1, "loading screenmap");
321 * Set the default screenmap.
325 load_default_scrnmap(void)
330 for (i=0; i<256; i++)
331 *((char*)&scrnmap + i) = i;
333 if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
335 err(1, "loading default screenmap");
341 * Print the current screenmap to stdout.
347 unsigned char map[256];
350 if (ioctl(0, GIO_SCRNMAP, &map) == -1) {
352 err(1, "getting screenmap");
354 for (i=0; i<sizeof(map); i++) {
355 if (i != 0 && i % 16 == 0)
356 fprintf(stdout, "\n");
359 fprintf(stdout, " %02x", map[i]);
361 fprintf(stdout, " %03d", map[i]);
363 fprintf(stdout, "\n");
369 * Determine a file's size.
377 if (fstat(fileno(file), &sb) == 0)
384 load_vt4mappingtable(unsigned int nmappings, FILE *f)
392 if ((t = calloc(nmappings, sizeof(*t))) == NULL) {
397 if (fread(t, sizeof *t * nmappings, 1, f) != 1) {
398 warn("read mappings");
403 for (i = 0; i < nmappings; i++) {
404 t[i].src = be32toh(t[i].src);
405 t[i].dst = be16toh(t[i].dst);
406 t[i].len = be16toh(t[i].len);
413 * Set the default vt font.
417 load_default_vt4font(void)
419 if (ioctl(0, PIO_VFONT_DEFAULT) == -1) {
421 err(1, "loading default vt font");
426 load_vt4font(FILE *f)
428 struct vt4font_header fh;
433 if (fread(&fh, sizeof fh, 1, f) != 1) {
434 warn("read file_header");
438 if (memcmp(fh.magic, "VFNT0002", 8) != 0) {
439 warnx("bad magic in font file\n");
443 for (i = 0; i < VFNT_MAPS; i++)
444 vfnt.map_count[i] = be32toh(fh.map_count[i]);
445 vfnt.glyph_count = be32toh(fh.glyph_count);
446 vfnt.width = fh.width;
447 vfnt.height = fh.height;
449 glyphsize = howmany(vfnt.width, 8) * vfnt.height * vfnt.glyph_count;
450 if ((vfnt.glyphs = malloc(glyphsize)) == NULL) {
455 if (fread(vfnt.glyphs, glyphsize, 1, f) != 1) {
461 for (i = 0; i < VFNT_MAPS; i++)
462 vfnt.map[i] = load_vt4mappingtable(vfnt.map_count[i], f);
464 if (ioctl(STDIN_FILENO, PIO_VFONT, &vfnt) == -1)
467 for (i = 0; i < VFNT_MAPS; i++)
473 * Load a font from file and set it.
477 load_font(const char *type, const char *filename)
481 unsigned long io = 0; /* silence stupid gcc(1) in the Wall mode */
482 char *name, *fontmap, size_sufx[6];
483 const char *a[] = {"", FONT_PATH, NULL};
484 const char *vt4a[] = {"", VT_FONT_PATH, NULL};
485 const char *b[] = {filename, NULL};
486 const char *c[] = {"", size_sufx, NULL};
487 const char *d[] = {"", ".fnt", NULL};
494 } sizes[] = {{8, 16, PIO_FONT8x16},
495 {8, 14, PIO_FONT8x14},
502 info.size = sizeof(info);
503 if (ioctl(0, CONS_GETINFO, &info) == -1) {
505 err(1, "getting console information");
508 snprintf(size_sufx, sizeof(size_sufx), "-8x%d", info.font_size);
510 fd = openguess((vt4_mode == 0) ? a : vt4a, b, c, d, &name);
514 errx(1, "%s: can't load font file", filename);
525 if (sscanf(type, "%dx%d", &w, &h) == 2) {
526 for (i = 0; sizes[i].w != 0; i++) {
527 if (sizes[i].w == w && sizes[i].h == h) {
528 size = DATASIZE(sizes[i]);
530 font_height = sizes[i].h;
537 errx(1, "%s: bad font size specification", type);
540 /* Apply heuristics */
545 size = DATASIZE(sizes[0]);
546 fontmap = (char*) malloc(size);
547 dsize[0] = decode(fd, fontmap, size);
548 dsize[1] = fsize(fd);
552 for (j = 0; j < 2; j++) {
553 for (i = 0; sizes[i].w != 0; i++) {
554 if (DATASIZE(sizes[i]) == dsize[j]) {
557 font_height = sizes[i].h;
567 errx(1, "%s: can't guess font size", filename);
573 fontmap = (char*) malloc(size);
575 if (decode(fd, fontmap, size) != size) {
577 if (fsize(fd) != size ||
578 fread(fontmap, 1, size, fd) != (size_t)size) {
582 errx(1, "%s: bad font file", filename);
586 if (ioctl(0, io, fontmap) == -1) {
588 err(1, "loading font");
597 * Set the timeout for the screensaver.
601 set_screensaver_timeout(char *arg)
605 if (!strcmp(arg, "off")) {
610 if ((*arg == '\0') || (nsec < 1)) {
612 errx(1, "argument must be a positive number");
616 if (ioctl(0, CONS_BLANKTIME, &nsec) == -1) {
618 err(1, "setting screensaver period");
624 * Set the cursor's shape/type.
628 set_cursor_type(char *appearance)
632 if (!strcmp(appearance, "normal"))
634 else if (!strcmp(appearance, "blink"))
636 else if (!strcmp(appearance, "destructive"))
640 errx(1, "argument to -c must be normal, blink or destructive");
643 if (ioctl(0, CONS_CURSORTYPE, &type) == -1) {
645 err(1, "setting cursor type");
651 * Set the video mode.
655 video_mode(int argc, char **argv, int *mode_index)
660 unsigned long mode_num;
662 { "80x25", SW_TEXT_80x25, M_TEXT_80x25 },
663 { "80x30", SW_TEXT_80x30, M_TEXT_80x30 },
664 { "80x43", SW_TEXT_80x43, M_TEXT_80x43 },
665 { "80x50", SW_TEXT_80x50, M_TEXT_80x50 },
666 { "80x60", SW_TEXT_80x60, M_TEXT_80x60 },
667 { "132x25", SW_TEXT_132x25, M_TEXT_132x25 },
668 { "132x30", SW_TEXT_132x30, M_TEXT_132x30 },
669 { "132x43", SW_TEXT_132x43, M_TEXT_132x43 },
670 { "132x50", SW_TEXT_132x50, M_TEXT_132x50 },
671 { "132x60", SW_TEXT_132x60, M_TEXT_132x60 },
672 { "VGA_40x25", SW_VGA_C40x25, M_VGA_C40x25 },
673 { "VGA_80x25", SW_VGA_C80x25, M_VGA_C80x25 },
674 { "VGA_80x30", SW_VGA_C80x30, M_VGA_C80x30 },
675 { "VGA_80x50", SW_VGA_C80x50, M_VGA_C80x50 },
676 { "VGA_80x60", SW_VGA_C80x60, M_VGA_C80x60 },
678 { "VGA_90x25", SW_VGA_C90x25, M_VGA_C90x25 },
679 { "VGA_90x30", SW_VGA_C90x30, M_VGA_C90x30 },
680 { "VGA_90x43", SW_VGA_C90x43, M_VGA_C90x43 },
681 { "VGA_90x50", SW_VGA_C90x50, M_VGA_C90x50 },
682 { "VGA_90x60", SW_VGA_C90x60, M_VGA_C90x60 },
684 { "VGA_320x200", SW_VGA_CG320, M_CG320 },
685 { "EGA_80x25", SW_ENH_C80x25, M_ENH_C80x25 },
686 { "EGA_80x43", SW_ENH_C80x43, M_ENH_C80x43 },
687 { "VESA_132x25", SW_VESA_C132x25,M_VESA_C132x25 },
688 { "VESA_132x43", SW_VESA_C132x43,M_VESA_C132x43 },
689 { "VESA_132x50", SW_VESA_C132x50,M_VESA_C132x50 },
690 { "VESA_132x60", SW_VESA_C132x60,M_VESA_C132x60 },
691 { "VESA_800x600", SW_VESA_800x600,M_VESA_800x600 },
695 int new_mode_num = 0;
696 unsigned long mode = 0;
702 if (ioctl(0, CONS_GET, &cur_mode) < 0)
703 err(1, "cannot get the current video mode");
706 * Parse the video mode argument...
709 if (*mode_index < argc) {
710 if (!strncmp(argv[*mode_index], "MODE_", 5)) {
711 if (!isdigit(argv[*mode_index][5]))
712 errx(1, "invalid video mode number");
714 new_mode_num = atoi(&argv[*mode_index][5]);
716 for (i = 0; modes[i].name != NULL; ++i) {
717 if (!strcmp(argv[*mode_index], modes[i].name)) {
718 mode = modes[i].mode;
719 new_mode_num = modes[i].mode_num;
724 if (modes[i].name == NULL)
726 if (ioctl(0, mode, NULL) < 0) {
728 err(1, "cannot set videomode");
730 video_mode_changed = 1;
734 * Collect enough information about the new video mode...
737 new_mode_info.vi_mode = new_mode_num;
739 if (ioctl(0, CONS_MODEINFO, &new_mode_info) == -1) {
741 err(1, "obtaining new video mode parameters");
745 if (new_mode_num >= M_VESA_BASE)
746 mode = _IO('V', new_mode_num - M_VESA_BASE);
748 mode = _IO('S', new_mode_num);
752 * Try setting the new mode.
755 if (ioctl(0, mode, NULL) == -1) {
757 err(1, "setting video mode");
759 video_mode_changed = 1;
762 * For raster modes it's not enough to just set the mode.
763 * We also need to explicitly set the raster mode.
766 if (new_mode_info.vi_flags & V_INFO_GRAPHICS) {
769 if (font_height == 0)
770 font_height = cur_info.console_info.font_size;
772 size[2] = font_height;
776 if ((vesa_cols * 8 > new_mode_info.vi_width) ||
778 size[0] = new_mode_info.vi_width / 8;
785 if ((vesa_rows * font_height > new_mode_info.vi_height) ||
787 size[1] = new_mode_info.vi_height /
793 /* set raster mode */
795 if (ioctl(0, KDRASTER, size)) {
797 if (cur_mode >= M_VESA_BASE)
799 _IO('V', cur_mode - M_VESA_BASE),
802 ioctl(0, _IO('S', cur_mode), NULL);
805 err(1, "cannot activate raster display");
809 /* Recover from mode setting forgetting colors. */
810 fprintf(stderr, "\033[=%dF",
811 cur_info.console_info.mv_norm.fore);
812 fprintf(stderr, "\033[=%dG",
813 cur_info.console_info.mv_norm.back);
821 * Return the number for a specified color name.
825 get_color_number(char *color)
829 for (i=0; i<16; i++) {
830 if (!strcmp(color, legal_colors[i]))
838 * Set normal text and background colors.
842 set_normal_colors(int argc, char **argv, int *_index)
846 if (*_index < argc && (color = get_color_number(argv[*_index])) != -1) {
848 fprintf(stderr, "\033[=%dF", color);
850 && (color = get_color_number(argv[*_index])) != -1) {
852 fprintf(stderr, "\033[=%dG", color);
859 * Set reverse text and background colors.
863 set_reverse_colors(int argc, char **argv, int *_index)
867 if ((color = get_color_number(argv[*(_index)-1])) != -1) {
868 fprintf(stderr, "\033[=%dH", color);
870 && (color = get_color_number(argv[*_index])) != -1) {
872 fprintf(stderr, "\033[=%dI", color);
879 * Switch to virtual terminal #arg.
883 set_console(char *arg)
887 if(!arg || strspn(arg,"0123456789") != strlen(arg)) {
889 errx(1, "bad console number");
894 if (n < 1 || n > 16) {
896 errx(1, "console number out of range");
897 } else if (ioctl(0, VT_ACTIVATE, n) == -1) {
899 err(1, "switching vty");
905 * Sets the border color.
909 set_border_color(char *arg)
913 if ((color = get_color_number(arg)) != -1) {
914 fprintf(stderr, "\033[=%dA", color);
921 set_mouse_char(char *arg)
923 struct mouse_info mouse;
926 l = strtol(arg, NULL, 0);
928 if ((l < 0) || (l > UCHAR_MAX - 3)) {
930 warnx("argument to -M must be 0 through %d", UCHAR_MAX - 3);
934 mouse.operation = MOUSE_MOUSECHAR;
935 mouse.u.mouse_char = (int)l;
937 if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) {
939 err(1, "setting mouse character");
945 * Show/hide the mouse.
951 struct mouse_info mouse;
953 if (!strcmp(arg, "on")) {
954 mouse.operation = MOUSE_SHOW;
955 } else if (!strcmp(arg, "off")) {
956 mouse.operation = MOUSE_HIDE;
959 errx(1, "argument to -m must be either on or off");
962 if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) {
964 err(1, "%sing the mouse",
965 mouse.operation == MOUSE_SHOW ? "show" : "hid");
971 set_lockswitch(char *arg)
975 if (!strcmp(arg, "off")) {
977 } else if (!strcmp(arg, "on")) {
981 errx(1, "argument to -S must be either on or off");
984 if (ioctl(0, VT_LOCKSWITCH, &data) == -1) {
986 err(1, "turning %s vty switching",
987 data == 0x01 ? "off" : "on");
993 * Return the adapter name for a specified type.
997 *adapter_name(int type)
1004 { KD_HERCULES, "Hercules" },
1014 for (i = 0; names[i].type != -1; ++i)
1015 if (names[i].type == type)
1017 return names[i].name;
1022 * Show active VTY, ie current console number.
1026 show_active_info(void)
1029 printf("%d\n", cur_info.active_vty);
1034 * Show graphics adapter information.
1038 show_adapter_info(void)
1040 struct video_adapter_info ad;
1044 if (ioctl(0, CONS_ADPINFO, &ad) == -1) {
1046 err(1, "obtaining adapter information");
1049 printf("fb%d:\n", ad.va_index);
1050 printf(" %.*s%d, type:%s%s (%d), flags:0x%x\n",
1051 (int)sizeof(ad.va_name), ad.va_name, ad.va_unit,
1052 (ad.va_flags & V_ADP_VESA) ? "VESA " : "",
1053 adapter_name(ad.va_type), ad.va_type, ad.va_flags);
1054 printf(" initial mode:%d, current mode:%d, BIOS mode:%d\n",
1055 ad.va_initial_mode, ad.va_mode, ad.va_initial_bios_mode);
1056 printf(" frame buffer window:0x%zx, buffer size:0x%zx\n",
1057 ad.va_window, ad.va_buffer_size);
1058 printf(" window size:0x%zx, origin:0x%x\n",
1059 ad.va_window_size, ad.va_window_orig);
1060 printf(" display start address (%d, %d), scan line width:%d\n",
1061 ad.va_disp_start.x, ad.va_disp_start.y, ad.va_line_width);
1062 printf(" reserved:0x%zx\n", ad.va_unused0);
1067 * Show video mode information.
1071 show_mode_info(void)
1074 struct video_info info;
1079 printf(" mode# flags type size "
1080 "font window linear buffer\n");
1081 printf("---------------------------------------"
1082 "---------------------------------------\n");
1084 memset(&info, 0, sizeof(info));
1085 for (mode = 0; mode <= M_VESA_MODE_MAX; ++mode) {
1086 info.vi_mode = mode;
1087 if (ioctl(0, CONS_MODEINFO, &info))
1089 if (info.vi_mode != mode)
1091 if (info.vi_width == 0 && info.vi_height == 0 &&
1092 info.vi_cwidth == 0 && info.vi_cheight == 0)
1095 printf("%3d (0x%03x)", mode, mode);
1096 printf(" 0x%08x", info.vi_flags);
1097 if (info.vi_flags & V_INFO_GRAPHICS) {
1100 if (info.vi_mem_model == V_INFO_MM_PLANAR)
1101 snprintf(buf, sizeof(buf), "%dx%dx%d %d",
1102 info.vi_width, info.vi_height,
1103 info.vi_depth, info.vi_planes);
1105 switch (info.vi_mem_model) {
1106 case V_INFO_MM_PACKED:
1109 case V_INFO_MM_DIRECT:
1118 case V_INFO_MM_VGAX:
1125 snprintf(buf, sizeof(buf), "%dx%dx%d %c",
1126 info.vi_width, info.vi_height,
1132 snprintf(buf, sizeof(buf), "%dx%d",
1133 info.vi_width, info.vi_height);
1136 printf(" %c %-15s", c, buf);
1137 snprintf(buf, sizeof(buf), "%dx%d",
1138 info.vi_cwidth, info.vi_cheight);
1139 printf(" %-5s", buf);
1140 printf(" 0x%05zx %2dk %2dk",
1141 info.vi_window, (int)info.vi_window_size/1024,
1142 (int)info.vi_window_gran/1024);
1143 printf(" 0x%08zx %dk\n",
1144 info.vi_buffer, (int)info.vi_buffer_size/1024);
1150 show_info(char *arg)
1153 if (!strcmp(arg, "active")) {
1155 } else if (!strcmp(arg, "adapter")) {
1156 show_adapter_info();
1157 } else if (!strcmp(arg, "mode")) {
1161 errx(1, "argument to -i must be active, adapter, or mode");
1170 const char *bg, *sep;
1173 info.size = sizeof(info);
1174 if (ioctl(0, CONS_GETINFO, &info) == -1)
1175 err(1, "getting console information");
1178 if (info.mv_csz < 80) {
1186 fprintf(stdout, "\033[=0G\n\n");
1187 for (i=0; i<8; i++) {
1189 "\033[=%dF\033[=0G%2d \033[=%dF%-7s%s"
1190 "\033[=%dF\033[=0G%2d \033[=%dF%-12s%s"
1191 "\033[=%dF%2d \033[=%dG%s\033[=0G%s"
1192 "\033[=%dF%2d \033[=%dG%s\033[=0G\n",
1193 fore, i, i, legal_colors[i], sep,
1194 fore, i + 8, i + 8, legal_colors[i + 8], sep,
1195 fore, i, i, bg, sep,
1196 fore, i + 8, i + 8, bg);
1198 fprintf(stdout, "\033[=%dF\033[=%dG\033[=%dH\033[=%dI\n",
1199 info.mv_norm.fore, info.mv_norm.back,
1200 info.mv_rev.fore, info.mv_rev.back);
1205 * Snapshot the video memory of that terminal, using the CONS_SCRSHOT
1206 * ioctl, and writes the results to stdout either in the special
1207 * binary format (see manual page for details), or in the plain
1212 dump_screen(int mode, int opt)
1217 info.size = sizeof(info);
1218 if (ioctl(0, CONS_GETINFO, &info) == -1) {
1220 err(1, "getting console information");
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 err(1, "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 err(1, "setting history buffer size");
1309 * Clear the console history buffer.
1315 if (ioctl(0, CONS_CLRHIST) == -1) {
1317 err(1, "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;
1339 vt4_mode = is_vt4();
1347 opts = "b:Cc:fg:h:Hi:M:m:pPr:S:s:T:t:x";
1349 opts = "b:Cc:dfg:h:Hi:l:LM:m:pPr:S:s:T:t:x";
1351 while ((opt = getopt(argc, argv, opts)) != -1)
1354 set_border_color(optarg);
1360 set_cursor_type(optarg);
1368 optarg = nextarg(argc, argv, &optind, 'f', 0);
1369 if (optarg != NULL) {
1370 font = nextarg(argc, argv, &optind, 'f', 0);
1378 load_font(type, font);
1381 usage(); /* Switch syscons to ROM? */
1383 load_default_vt4font();
1387 if (sscanf(optarg, "%dx%d",
1388 &vesa_cols, &vesa_rows) != 2) {
1390 warnx("incorrect geometry: %s", optarg);
1395 set_history(optarg);
1406 load_scrnmap(optarg);
1411 load_default_scrnmap();
1414 set_mouse_char(optarg);
1420 dumpmod = DUMP_FMT_RAW;
1423 dumpmod = DUMP_FMT_TXT;
1426 set_reverse_colors(argc, argv, &optind);
1429 set_lockswitch(optarg);
1432 set_console(optarg);
1435 if (strcmp(optarg, "xterm") != 0 &&
1436 strcmp(optarg, "cons25") != 0)
1441 set_screensaver_timeout(optarg);
1451 dump_screen(dumpmod, dumpopt);
1452 video_mode(argc, argv, &optind);
1453 set_normal_colors(argc, argv, &optind);
1455 if (optind < argc && !strcmp(argv[optind], "show")) {
1460 if (termmode != NULL)
1461 set_terminal_mode(termmode);
1463 if ((optind != argc) || (argc == 1))