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 $
44 #include <sys/consio.h>
45 #include <sys/endian.h>
46 #include <sys/errno.h>
47 #include <sys/param.h>
48 #include <sys/types.h>
50 #include <sys/sysctl.h>
55 #define DATASIZE(x) ((x).w * (x).h * 256 / 8)
57 /* Screen dump modes */
58 #define DUMP_FMT_RAW 1
59 #define DUMP_FMT_TXT 2
60 /* Screen dump options */
63 /* Screen dump file format revision */
64 #define DUMP_FMT_REV 1
66 static const char *legal_colors[16] = {
67 "black", "blue", "green", "cyan",
68 "red", "magenta", "brown", "white",
69 "grey", "lightblue", "lightgreen", "lightcyan",
70 "lightred", "lightmagenta", "yellow", "lightwhite"
75 vid_info_t console_info;
76 unsigned char screen_map[256];
77 int video_mode_number;
78 struct video_info video_mode_info;
84 static int font_height;
85 static int vt4_mode = 0;
86 static int video_mode_changed;
87 static struct video_info new_mode_info;
91 * Initialize revert data.
93 * NOTE: the following parameters are not yet saved/restored:
95 * screen saver timeout
97 * mouse character and mouse show/hide state
98 * vty switching on/off state
107 if (ioctl(0, VT_GETACTIVE, &cur_info.active_vty) == -1)
108 err(1, "getting active vty");
110 cur_info.console_info.size = sizeof(cur_info.console_info);
111 if (ioctl(0, CONS_GETINFO, &cur_info.console_info) == -1)
112 err(1, "getting console information");
114 /* vt(4) use unicode, so no screen mapping required. */
116 ioctl(0, GIO_SCRNMAP, &cur_info.screen_map) == -1)
117 err(1, "getting screen map");
119 if (ioctl(0, CONS_GET, &cur_info.video_mode_number) == -1)
120 err(1, "getting video mode number");
122 cur_info.video_mode_info.vi_mode = cur_info.video_mode_number;
124 if (ioctl(0, CONS_MODEINFO, &cur_info.video_mode_info) == -1)
125 err(1, "getting video mode parameters");
130 * If something goes wrong along the way we call revert() to go back to the
131 * console state we came from (which is assumed to be working).
133 * NOTE: please also read the comments of init().
139 int save_errno, size[3];
143 ioctl(0, VT_ACTIVATE, cur_info.active_vty);
145 ioctl(0, KDSBORDER, cur_info.console_info.mv_ovscan);
146 fprintf(stderr, "\033[=%dH", cur_info.console_info.mv_rev.fore);
147 fprintf(stderr, "\033[=%dI", cur_info.console_info.mv_rev.back);
150 ioctl(0, PIO_SCRNMAP, &cur_info.screen_map);
152 if (video_mode_changed) {
153 if (cur_info.video_mode_number >= M_VESA_BASE)
155 _IO('V', cur_info.video_mode_number - M_VESA_BASE),
158 ioctl(0, _IO('S', cur_info.video_mode_number), NULL);
159 if (cur_info.video_mode_info.vi_flags & V_INFO_GRAPHICS) {
160 size[0] = cur_info.console_info.mv_csz;
161 size[1] = cur_info.console_info.mv_rsz;
162 size[2] = cur_info.console_info.font_size;
163 ioctl(0, KDRASTER, size);
167 /* Restore some colors last since mode setting forgets some. */
168 fprintf(stderr, "\033[=%dF", cur_info.console_info.mv_norm.fore);
169 fprintf(stderr, "\033[=%dG", cur_info.console_info.mv_norm.back);
176 * Print a short usage string describing all options, then exit.
183 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
184 "usage: vidcontrol [-Cx] [-b color] [-c appearance] [-f [[size] file]]",
185 " [-g geometry] [-h size] [-i active | adapter | mode]",
186 " [-M char] [-m on | off]",
187 " [-r foreground background] [-S on | off] [-s number]",
188 " [-T xterm | cons25] [-t N | off] [mode]",
189 " [foreground [background]] [show]");
191 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
192 "usage: vidcontrol [-CdHLPpx] [-b color] [-c appearance] [-E emulator]",
193 " [-f [[size] file]] [-g geometry] [-h size]",
194 " [-i active | adapter | mode] [-l screen_map] [-M char]",
195 " [-m on | off] [-r foreground background] [-S on | off]",
196 " [-s number] [-T xterm | cons25] [-t N | off] [mode]",
197 " [foreground [background]] [show]");
201 /* Detect presence of vt(4). */
205 char vty_name[4] = "";
206 size_t len = sizeof(vty_name);
208 if (sysctlbyname("kern.vty", vty_name, &len, NULL, 0) != 0)
210 return (strcmp(vty_name, "vt") == 0);
214 * Retrieve the next argument from the command line (for options that require
215 * more than one argument).
219 nextarg(int ac, char **av, int *indp, int oc, int strict)
222 return(av[(*indp)++]);
226 errx(1, "option requires two arguments -- %c", oc);
234 * Guess which file to open. Try to open each combination of a specified set
235 * of file name components.
239 openguess(const char *a[], const char *b[], const char *c[], const char *d[], char **name)
244 for (i = 0; a[i] != NULL; i++) {
245 for (j = 0; b[j] != NULL; j++) {
246 for (k = 0; c[k] != NULL; k++) {
247 for (l = 0; d[l] != NULL; l++) {
248 asprintf(name, "%s%s%s%s",
249 a[i], b[j], c[k], d[l]);
251 f = fopen(*name, "r");
266 * Load a screenmap from a file and set it.
270 load_scrnmap(const char *filename)
276 const char *a[] = {"", SCRNMAP_PATH, NULL};
277 const char *b[] = {filename, NULL};
278 const char *c[] = {"", ".scm", NULL};
279 const char *d[] = {"", NULL};
281 fd = openguess(a, b, c, d, &name);
285 errx(1, "screenmap file not found");
288 size = sizeof(scrnmap);
290 if (decode(fd, (char *)&scrnmap, size) != size) {
293 if (fread(&scrnmap, 1, size, fd) != (size_t)size) {
296 errx(1, "bad screenmap file");
300 if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
302 err(1, "loading screenmap");
310 * Set the default screenmap.
314 load_default_scrnmap(void)
319 for (i=0; i<256; i++)
320 *((char*)&scrnmap + i) = i;
322 if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
324 err(1, "loading default screenmap");
330 * Print the current screenmap to stdout.
336 unsigned char map[256];
339 if (ioctl(0, GIO_SCRNMAP, &map) == -1) {
341 err(1, "getting screenmap");
343 for (i=0; i<sizeof(map); i++) {
344 if (i != 0 && i % 16 == 0)
345 fprintf(stdout, "\n");
348 fprintf(stdout, " %02x", map[i]);
350 fprintf(stdout, " %03d", map[i]);
352 fprintf(stdout, "\n");
358 * Determine a file's size.
366 if (fstat(fileno(file), &sb) == 0)
373 load_vt4mappingtable(unsigned int nmappings, FILE *f)
381 if ((t = calloc(nmappings, sizeof(*t))) == NULL) {
386 if (fread(t, sizeof *t * nmappings, 1, f) != 1) {
387 warn("read mappings");
392 for (i = 0; i < nmappings; i++) {
393 t[i].vfm_src = be32toh(t[i].vfm_src);
394 t[i].vfm_dst = be16toh(t[i].vfm_dst);
395 t[i].vfm_len = be16toh(t[i].vfm_len);
402 * Set the default vt font.
406 load_default_vt4font(void)
408 if (ioctl(0, PIO_VFONT_DEFAULT) == -1) {
410 err(1, "loading default vt font");
415 load_vt4font(FILE *f)
417 struct font_header fh;
422 if (fread(&fh, sizeof fh, 1, f) != 1) {
423 warn("read file_header");
427 if (memcmp(fh.fh_magic, "VFNT0002", 8) != 0) {
428 warnx("bad magic in font file\n");
432 for (i = 0; i < VFNT_MAPS; i++)
433 vfnt.map_count[i] = be32toh(fh.fh_map_count[i]);
434 vfnt.glyph_count = be32toh(fh.fh_glyph_count);
435 vfnt.width = fh.fh_width;
436 vfnt.height = fh.fh_height;
438 glyphsize = howmany(vfnt.width, 8) * vfnt.height * vfnt.glyph_count;
439 if ((vfnt.glyphs = malloc(glyphsize)) == NULL) {
444 if (fread(vfnt.glyphs, glyphsize, 1, f) != 1) {
450 for (i = 0; i < VFNT_MAPS; i++)
451 vfnt.map[i] = load_vt4mappingtable(vfnt.map_count[i], f);
453 if (ioctl(STDIN_FILENO, PIO_VFONT, &vfnt) == -1)
456 for (i = 0; i < VFNT_MAPS; i++)
462 * Load a font from file and set it.
466 load_font(const char *type, const char *filename)
470 unsigned long io = 0; /* silence stupid gcc(1) in the Wall mode */
471 char *name, *fontmap, size_sufx[6];
472 const char *a[] = {"", FONT_PATH, NULL};
473 const char *vt4a[] = {"", VT_FONT_PATH, NULL};
474 const char *b[] = {filename, NULL};
475 const char *c[] = {"", size_sufx, NULL};
476 const char *d[] = {"", ".fnt", NULL};
483 } sizes[] = {{8, 16, PIO_FONT8x16},
484 {8, 14, PIO_FONT8x14},
491 info.size = sizeof(info);
492 if (ioctl(0, CONS_GETINFO, &info) == -1) {
494 err(1, "getting console information");
497 snprintf(size_sufx, sizeof(size_sufx), "-8x%d", info.font_size);
499 fd = openguess((vt4_mode == 0) ? a : vt4a, b, c, d, &name);
503 errx(1, "%s: can't load font file", filename);
514 if (sscanf(type, "%dx%d", &w, &h) == 2) {
515 for (i = 0; sizes[i].w != 0; i++) {
516 if (sizes[i].w == w && sizes[i].h == h) {
517 size = DATASIZE(sizes[i]);
519 font_height = sizes[i].h;
526 errx(1, "%s: bad font size specification", type);
529 /* Apply heuristics */
534 size = DATASIZE(sizes[0]);
535 fontmap = (char*) malloc(size);
536 dsize[0] = decode(fd, fontmap, size);
537 dsize[1] = fsize(fd);
541 for (j = 0; j < 2; j++) {
542 for (i = 0; sizes[i].w != 0; i++) {
543 if (DATASIZE(sizes[i]) == dsize[j]) {
546 font_height = sizes[i].h;
556 errx(1, "%s: can't guess font size", filename);
562 fontmap = (char*) malloc(size);
564 if (decode(fd, fontmap, size) != size) {
566 if (fsize(fd) != size ||
567 fread(fontmap, 1, size, fd) != (size_t)size) {
571 errx(1, "%s: bad font file", filename);
575 if (ioctl(0, io, fontmap) == -1) {
577 err(1, "loading font");
586 * Set the timeout for the screensaver.
590 set_screensaver_timeout(char *arg)
594 if (!strcmp(arg, "off")) {
599 if ((*arg == '\0') || (nsec < 1)) {
601 errx(1, "argument must be a positive number");
605 if (ioctl(0, CONS_BLANKTIME, &nsec) == -1) {
607 err(1, "setting screensaver period");
612 parse_cursor_params(char *param, struct cshape *shape)
614 char *dupparam, *word;
617 param = dupparam = strdup(param);
618 type = shape->shape[0];
619 while ((word = strsep(¶m, ",")) != NULL) {
620 if (strcmp(word, "normal") == 0)
622 else if (strcmp(word, "destructive") == 0)
623 type = CONS_BLINK_CURSOR | CONS_CHAR_CURSOR;
624 else if (strcmp(word, "blink") == 0)
625 type |= CONS_BLINK_CURSOR;
626 else if (strcmp(word, "noblink") == 0)
627 type &= ~CONS_BLINK_CURSOR;
628 else if (strcmp(word, "block") == 0)
629 type &= ~CONS_CHAR_CURSOR;
630 else if (strcmp(word, "noblock") == 0)
631 type |= CONS_CHAR_CURSOR;
632 else if (strcmp(word, "hidden") == 0)
633 type |= CONS_HIDDEN_CURSOR;
634 else if (strcmp(word, "nohidden") == 0)
635 type &= ~CONS_HIDDEN_CURSOR;
636 else if (strncmp(word, "base=", 5) == 0)
637 shape->shape[1] = strtol(word + 5, NULL, 0);
638 else if (strncmp(word, "height=", 7) == 0)
639 shape->shape[2] = strtol(word + 7, NULL, 0);
640 else if (strcmp(word, "charcolors") == 0)
641 type |= CONS_CHARCURSOR_COLORS;
642 else if (strcmp(word, "mousecolors") == 0)
643 type |= CONS_MOUSECURSOR_COLORS;
644 else if (strcmp(word, "default") == 0)
645 type |= CONS_DEFAULT_CURSOR;
646 else if (strcmp(word, "shapeonly") == 0)
647 type |= CONS_SHAPEONLY_CURSOR;
648 else if (strcmp(word, "local") == 0)
649 type |= CONS_LOCAL_CURSOR;
650 else if (strcmp(word, "reset") == 0)
651 type |= CONS_RESET_CURSOR;
652 else if (strcmp(word, "show") == 0)
653 printf("flags %#x, base %d, height %d\n",
654 type, shape->shape[1], shape->shape[2]);
658 "invalid parameters for -c starting at '%s%s%s'",
659 word, param != NULL ? "," : "",
660 param != NULL ? param : "");
664 shape->shape[0] = type;
669 * Set the cursor's shape/type.
673 set_cursor_type(char *param)
677 /* Dry run to determine color, default and local flags. */
681 parse_cursor_params(param, &shape);
683 /* Get the relevant old setting. */
684 if (ioctl(0, CONS_GETCURSORSHAPE, &shape) != 0) {
686 err(1, "ioctl(CONS_GETCURSORSHAPE)");
689 parse_cursor_params(param, &shape);
690 if (ioctl(0, CONS_SETCURSORSHAPE, &shape) != 0) {
692 err(1, "ioctl(CONS_SETCURSORSHAPE)");
698 * Set the video mode.
702 video_mode(int argc, char **argv, int *mode_index)
707 unsigned long mode_num;
709 { "80x25", SW_TEXT_80x25, M_TEXT_80x25 },
710 { "80x30", SW_TEXT_80x30, M_TEXT_80x30 },
711 { "80x43", SW_TEXT_80x43, M_TEXT_80x43 },
712 { "80x50", SW_TEXT_80x50, M_TEXT_80x50 },
713 { "80x60", SW_TEXT_80x60, M_TEXT_80x60 },
714 { "132x25", SW_TEXT_132x25, M_TEXT_132x25 },
715 { "132x30", SW_TEXT_132x30, M_TEXT_132x30 },
716 { "132x43", SW_TEXT_132x43, M_TEXT_132x43 },
717 { "132x50", SW_TEXT_132x50, M_TEXT_132x50 },
718 { "132x60", SW_TEXT_132x60, M_TEXT_132x60 },
719 { "VGA_40x25", SW_VGA_C40x25, M_VGA_C40x25 },
720 { "VGA_80x25", SW_VGA_C80x25, M_VGA_C80x25 },
721 { "VGA_80x30", SW_VGA_C80x30, M_VGA_C80x30 },
722 { "VGA_80x50", SW_VGA_C80x50, M_VGA_C80x50 },
723 { "VGA_80x60", SW_VGA_C80x60, M_VGA_C80x60 },
725 { "VGA_90x25", SW_VGA_C90x25, M_VGA_C90x25 },
726 { "VGA_90x30", SW_VGA_C90x30, M_VGA_C90x30 },
727 { "VGA_90x43", SW_VGA_C90x43, M_VGA_C90x43 },
728 { "VGA_90x50", SW_VGA_C90x50, M_VGA_C90x50 },
729 { "VGA_90x60", SW_VGA_C90x60, M_VGA_C90x60 },
731 { "VGA_320x200", SW_VGA_CG320, M_CG320 },
732 { "EGA_80x25", SW_ENH_C80x25, M_ENH_C80x25 },
733 { "EGA_80x43", SW_ENH_C80x43, M_ENH_C80x43 },
734 { "VESA_132x25", SW_VESA_C132x25,M_VESA_C132x25 },
735 { "VESA_132x43", SW_VESA_C132x43,M_VESA_C132x43 },
736 { "VESA_132x50", SW_VESA_C132x50,M_VESA_C132x50 },
737 { "VESA_132x60", SW_VESA_C132x60,M_VESA_C132x60 },
738 { "VESA_800x600", SW_VESA_800x600,M_VESA_800x600 },
742 int new_mode_num = 0;
743 unsigned long mode = 0;
749 if (ioctl(0, CONS_GET, &cur_mode) < 0)
750 err(1, "cannot get the current video mode");
753 * Parse the video mode argument...
756 if (*mode_index < argc) {
757 if (!strncmp(argv[*mode_index], "MODE_", 5)) {
758 if (!isdigit(argv[*mode_index][5]))
759 errx(1, "invalid video mode number");
761 new_mode_num = atoi(&argv[*mode_index][5]);
763 for (i = 0; modes[i].name != NULL; ++i) {
764 if (!strcmp(argv[*mode_index], modes[i].name)) {
765 mode = modes[i].mode;
766 new_mode_num = modes[i].mode_num;
771 if (modes[i].name == NULL)
773 if (ioctl(0, mode, NULL) < 0) {
775 err(1, "cannot set videomode");
777 video_mode_changed = 1;
781 * Collect enough information about the new video mode...
784 new_mode_info.vi_mode = new_mode_num;
786 if (ioctl(0, CONS_MODEINFO, &new_mode_info) == -1) {
788 err(1, "obtaining new video mode parameters");
792 if (new_mode_num >= M_VESA_BASE)
793 mode = _IO('V', new_mode_num - M_VESA_BASE);
795 mode = _IO('S', new_mode_num);
799 * Try setting the new mode.
802 if (ioctl(0, mode, NULL) == -1) {
804 err(1, "setting video mode");
806 video_mode_changed = 1;
809 * For raster modes it's not enough to just set the mode.
810 * We also need to explicitly set the raster mode.
813 if (new_mode_info.vi_flags & V_INFO_GRAPHICS) {
816 if (font_height == 0)
817 font_height = cur_info.console_info.font_size;
819 size[2] = font_height;
823 if ((vesa_cols * 8 > new_mode_info.vi_width) ||
825 size[0] = new_mode_info.vi_width / 8;
832 if ((vesa_rows * font_height > new_mode_info.vi_height) ||
834 size[1] = new_mode_info.vi_height /
840 /* set raster mode */
842 if (ioctl(0, KDRASTER, size)) {
844 if (cur_mode >= M_VESA_BASE)
846 _IO('V', cur_mode - M_VESA_BASE),
849 ioctl(0, _IO('S', cur_mode), NULL);
852 err(1, "cannot activate raster display");
856 /* Recover from mode setting forgetting colors. */
857 fprintf(stderr, "\033[=%dF",
858 cur_info.console_info.mv_norm.fore);
859 fprintf(stderr, "\033[=%dG",
860 cur_info.console_info.mv_norm.back);
868 * Return the number for a specified color name.
872 get_color_number(char *color)
876 for (i=0; i<16; i++) {
877 if (!strcmp(color, legal_colors[i]))
885 * Set normal text and background colors.
889 set_normal_colors(int argc, char **argv, int *_index)
893 if (*_index < argc && (color = get_color_number(argv[*_index])) != -1) {
895 fprintf(stderr, "\033[=%dF", color);
897 && (color = get_color_number(argv[*_index])) != -1) {
899 fprintf(stderr, "\033[=%dG", color);
906 * Set reverse text and background colors.
910 set_reverse_colors(int argc, char **argv, int *_index)
914 if ((color = get_color_number(argv[*(_index)-1])) != -1) {
915 fprintf(stderr, "\033[=%dH", color);
917 && (color = get_color_number(argv[*_index])) != -1) {
919 fprintf(stderr, "\033[=%dI", color);
926 * Switch to virtual terminal #arg.
930 set_console(char *arg)
934 if(!arg || strspn(arg,"0123456789") != strlen(arg)) {
936 errx(1, "bad console number");
941 if (n < 1 || n > 16) {
943 errx(1, "console number out of range");
944 } else if (ioctl(0, VT_ACTIVATE, n) == -1) {
946 err(1, "switching vty");
952 * Sets the border color.
956 set_border_color(char *arg)
960 color = get_color_number(arg);
963 errx(1, "invalid color '%s'", arg);
965 if (ioctl(0, KDSBORDER, color) != 0) {
967 err(1, "ioctl(KD_SBORDER)");
972 set_mouse_char(char *arg)
974 struct mouse_info mouse;
977 l = strtol(arg, NULL, 0);
979 if ((l < 0) || (l > UCHAR_MAX - 3)) {
981 warnx("argument to -M must be 0 through %d", UCHAR_MAX - 3);
985 mouse.operation = MOUSE_MOUSECHAR;
986 mouse.u.mouse_char = (int)l;
988 if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) {
990 err(1, "setting mouse character");
996 * Show/hide the mouse.
1000 set_mouse(char *arg)
1002 struct mouse_info mouse;
1004 if (!strcmp(arg, "on")) {
1005 mouse.operation = MOUSE_SHOW;
1006 } else if (!strcmp(arg, "off")) {
1007 mouse.operation = MOUSE_HIDE;
1010 errx(1, "argument to -m must be either on or off");
1013 if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) {
1015 err(1, "%sing the mouse",
1016 mouse.operation == MOUSE_SHOW ? "show" : "hid");
1022 set_lockswitch(char *arg)
1026 if (!strcmp(arg, "off")) {
1028 } else if (!strcmp(arg, "on")) {
1032 errx(1, "argument to -S must be either on or off");
1035 if (ioctl(0, VT_LOCKSWITCH, &data) == -1) {
1037 err(1, "turning %s vty switching",
1038 data == 0x01 ? "off" : "on");
1044 * Return the adapter name for a specified type.
1048 *adapter_name(int type)
1055 { KD_HERCULES, "Hercules" },
1065 for (i = 0; names[i].type != -1; ++i)
1066 if (names[i].type == type)
1068 return names[i].name;
1073 * Show active VTY, ie current console number.
1077 show_active_info(void)
1080 printf("%d\n", cur_info.active_vty);
1085 * Show graphics adapter information.
1089 show_adapter_info(void)
1091 struct video_adapter_info ad;
1095 if (ioctl(0, CONS_ADPINFO, &ad) == -1) {
1097 err(1, "obtaining adapter information");
1100 printf("fb%d:\n", ad.va_index);
1101 printf(" %.*s%d, type:%s%s (%d), flags:0x%x\n",
1102 (int)sizeof(ad.va_name), ad.va_name, ad.va_unit,
1103 (ad.va_flags & V_ADP_VESA) ? "VESA " : "",
1104 adapter_name(ad.va_type), ad.va_type, ad.va_flags);
1105 printf(" initial mode:%d, current mode:%d, BIOS mode:%d\n",
1106 ad.va_initial_mode, ad.va_mode, ad.va_initial_bios_mode);
1107 printf(" frame buffer window:0x%zx, buffer size:0x%zx\n",
1108 ad.va_window, ad.va_buffer_size);
1109 printf(" window size:0x%zx, origin:0x%x\n",
1110 ad.va_window_size, ad.va_window_orig);
1111 printf(" display start address (%d, %d), scan line width:%d\n",
1112 ad.va_disp_start.x, ad.va_disp_start.y, ad.va_line_width);
1113 printf(" reserved:0x%zx\n", ad.va_unused0);
1118 * Show video mode information.
1122 show_mode_info(void)
1125 struct video_info info;
1130 printf(" mode# flags type size "
1131 "font window linear buffer\n");
1132 printf("---------------------------------------"
1133 "---------------------------------------\n");
1135 memset(&info, 0, sizeof(info));
1136 for (mode = 0; mode <= M_VESA_MODE_MAX; ++mode) {
1137 info.vi_mode = mode;
1138 if (ioctl(0, CONS_MODEINFO, &info))
1140 if (info.vi_mode != mode)
1142 if (info.vi_width == 0 && info.vi_height == 0 &&
1143 info.vi_cwidth == 0 && info.vi_cheight == 0)
1146 printf("%3d (0x%03x)", mode, mode);
1147 printf(" 0x%08x", info.vi_flags);
1148 if (info.vi_flags & V_INFO_GRAPHICS) {
1151 if (info.vi_mem_model == V_INFO_MM_PLANAR)
1152 snprintf(buf, sizeof(buf), "%dx%dx%d %d",
1153 info.vi_width, info.vi_height,
1154 info.vi_depth, info.vi_planes);
1156 switch (info.vi_mem_model) {
1157 case V_INFO_MM_PACKED:
1160 case V_INFO_MM_DIRECT:
1169 case V_INFO_MM_VGAX:
1176 snprintf(buf, sizeof(buf), "%dx%dx%d %c",
1177 info.vi_width, info.vi_height,
1183 snprintf(buf, sizeof(buf), "%dx%d",
1184 info.vi_width, info.vi_height);
1187 printf(" %c %-15s", c, buf);
1188 snprintf(buf, sizeof(buf), "%dx%d",
1189 info.vi_cwidth, info.vi_cheight);
1190 printf(" %-5s", buf);
1191 printf(" 0x%05zx %2dk %2dk",
1192 info.vi_window, (int)info.vi_window_size/1024,
1193 (int)info.vi_window_gran/1024);
1194 printf(" 0x%08zx %dk\n",
1195 info.vi_buffer, (int)info.vi_buffer_size/1024);
1201 show_info(char *arg)
1204 if (!strcmp(arg, "active")) {
1206 } else if (!strcmp(arg, "adapter")) {
1207 show_adapter_info();
1208 } else if (!strcmp(arg, "mode")) {
1212 errx(1, "argument to -i must be active, adapter, or mode");
1221 const char *bg, *sep;
1224 info.size = sizeof(info);
1225 if (ioctl(0, CONS_GETINFO, &info) == -1)
1226 err(1, "getting console information");
1229 if (info.mv_csz < 80) {
1237 fprintf(stdout, "\033[=0G\n\n");
1238 for (i=0; i<8; i++) {
1240 "\033[=%dF\033[=0G%2d \033[=%dF%-7s%s"
1241 "\033[=%dF\033[=0G%2d \033[=%dF%-12s%s"
1242 "\033[=%dF%2d \033[=%dG%s\033[=0G%s"
1243 "\033[=%dF%2d \033[=%dG%s\033[=0G\n",
1244 fore, i, i, legal_colors[i], sep,
1245 fore, i + 8, i + 8, legal_colors[i + 8], sep,
1246 fore, i, i, bg, sep,
1247 fore, i + 8, i + 8, bg);
1249 fprintf(stdout, "\033[=%dF\033[=%dG\033[=%dH\033[=%dI\n",
1250 info.mv_norm.fore, info.mv_norm.back,
1251 info.mv_rev.fore, info.mv_rev.back);
1256 * Snapshot the video memory of that terminal, using the CONS_SCRSHOT
1257 * ioctl, and writes the results to stdout either in the special
1258 * binary format (see manual page for details), or in the plain
1263 dump_screen(int mode, int opt)
1268 info.size = sizeof(info);
1269 if (ioctl(0, CONS_GETINFO, &info) == -1) {
1271 err(1, "getting console information");
1274 shot.x = shot.y = 0;
1275 shot.xsize = info.mv_csz;
1276 shot.ysize = info.mv_rsz;
1277 if (opt == DUMP_ALL)
1278 shot.ysize += info.mv_hsz;
1280 shot.buf = alloca(shot.xsize * shot.ysize * sizeof(u_int16_t));
1281 if (shot.buf == NULL) {
1283 errx(1, "failed to allocate memory for dump");
1286 if (ioctl(0, CONS_SCRSHOT, &shot) == -1) {
1288 err(1, "dumping screen");
1291 if (mode == DUMP_FMT_RAW) {
1292 printf("SCRSHOT_%c%c%c%c", DUMP_FMT_REV, 2,
1293 shot.xsize, shot.ysize);
1297 write(STDOUT_FILENO, shot.buf,
1298 shot.xsize * shot.ysize * sizeof(u_int16_t));
1304 line = alloca(shot.xsize + 1);
1308 errx(1, "failed to allocate memory for line buffer");
1311 for (y = 0; y < shot.ysize; y++) {
1312 for (x = 0; x < shot.xsize; x++) {
1313 ch = shot.buf[x + (y * shot.xsize)];
1316 if (isprint(ch) == 0)
1322 /* Trim trailing spaces */
1326 } while (line[x] == ' ' && x != 0);
1337 * Set the console history buffer size.
1341 set_history(char *opt)
1347 if ((*opt == '\0') || size < 0) {
1349 errx(1, "argument must not be less than zero");
1352 if (ioctl(0, CONS_HISTORY, &size) == -1) {
1354 err(1, "setting history buffer size");
1360 * Clear the console history buffer.
1366 if (ioctl(0, CONS_CLRHIST) == -1) {
1368 err(1, "clearing history buffer");
1373 get_terminal_emulator(int i, struct term_info *tip)
1376 if (ioctl(0, CONS_GETTERM, tip) == 0)
1378 strlcpy((char *)tip->ti_name, "unknown", sizeof(tip->ti_name));
1379 strlcpy((char *)tip->ti_desc, "unknown", sizeof(tip->ti_desc));
1384 get_terminal_emulators(void)
1386 struct term_info ti;
1389 for (i = 0; i < 10; i++) {
1390 if (get_terminal_emulator(i, &ti) == 0)
1392 printf("%d: %s (%s)%s\n", i, ti.ti_name, ti.ti_desc,
1393 i == 0 ? " (active)" : "");
1398 set_terminal_emulator(const char *name)
1400 struct term_info old_ti, ti;
1402 get_terminal_emulator(0, &old_ti);
1403 strlcpy((char *)ti.ti_name, name, sizeof(ti.ti_name));
1404 if (ioctl(0, CONS_SETTERM, &ti) != 0)
1405 warn("SETTERM '%s'", name);
1406 get_terminal_emulator(0, &ti);
1407 printf("%s (%s) -> %s (%s)\n", old_ti.ti_name, old_ti.ti_desc,
1408 ti.ti_name, ti.ti_desc);
1412 set_terminal_mode(char *arg)
1415 if (strcmp(arg, "xterm") == 0)
1416 fprintf(stderr, "\033[=T");
1417 else if (strcmp(arg, "cons25") == 0)
1418 fprintf(stderr, "\033[=1T");
1423 main(int argc, char **argv)
1425 char *font, *type, *termmode;
1427 int dumpmod, dumpopt, opt;
1429 vt4_mode = is_vt4();
1437 opts = "b:Cc:fg:h:i:M:m:r:S:s:T:t:x";
1439 opts = "b:Cc:deE:fg:h:Hi:l:LM:m:pPr:S:s:T:t:x";
1441 while ((opt = getopt(argc, argv, opts)) != -1)
1444 set_border_color(optarg);
1450 set_cursor_type(optarg);
1460 set_terminal_emulator(optarg);
1465 get_terminal_emulators();
1468 optarg = nextarg(argc, argv, &optind, 'f', 0);
1469 if (optarg != NULL) {
1470 font = nextarg(argc, argv, &optind, 'f', 0);
1478 load_font(type, font);
1481 usage(); /* Switch syscons to ROM? */
1483 load_default_vt4font();
1487 if (sscanf(optarg, "%dx%d",
1488 &vesa_cols, &vesa_rows) != 2) {
1490 warnx("incorrect geometry: %s", optarg);
1495 set_history(optarg);
1506 load_scrnmap(optarg);
1511 load_default_scrnmap();
1514 set_mouse_char(optarg);
1520 dumpmod = DUMP_FMT_RAW;
1523 dumpmod = DUMP_FMT_TXT;
1526 set_reverse_colors(argc, argv, &optind);
1529 set_lockswitch(optarg);
1532 set_console(optarg);
1535 if (strcmp(optarg, "xterm") != 0 &&
1536 strcmp(optarg, "cons25") != 0)
1541 set_screensaver_timeout(optarg);
1551 dump_screen(dumpmod, dumpopt);
1552 video_mode(argc, argv, &optind);
1553 set_normal_colors(argc, argv, &optind);
1555 if (optind < argc && !strcmp(argv[optind], "show")) {
1560 if (termmode != NULL)
1561 set_terminal_mode(termmode);
1563 if ((optind != argc) || (argc == 1))