2 * Copyright (c) 1994-1996 Søren Schmidt
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * $FreeBSD: src/usr.sbin/vidcontrol/vidcontrol.c,v 1.32.2.7 2002/09/15 22:31:50 dd Exp $
31 #include <machine/console.h>
33 #include <sys/consio.h>
34 #include <sys/errno.h>
35 #include <sys/types.h>
50 #define DATASIZE(x) ((x).w * (x).h * 256 / 8)
55 #define DUMP_FMT_REV 1
58 char legal_colors[16][16] = {
59 "black", "blue", "green", "cyan",
60 "red", "magenta", "brown", "white",
61 "grey", "lightblue", "lightgreen", "lightcyan",
62 "lightred", "lightmagenta", "yellow", "lightwhite"
67 vid_info_t console_info;
68 unsigned char screen_map[256];
69 int video_mode_number;
70 struct video_info video_mode_info;
79 int video_mode_changed;
80 int normal_fore_color, normal_back_color;
81 int revers_fore_color, revers_back_color;
84 struct video_info new_mode_info;
88 * Initialize revert data.
90 * NOTE: the following parameters are not yet saved/restored:
92 * screen saver timeout
94 * mouse character and mouse show/hide state
95 * vty switching on/off state
104 if (ioctl(0, VT_GETACTIVE, &cur_info.active_vty) == -1)
105 errc(1, errno, "getting active vty");
107 cur_info.console_info.size = sizeof(cur_info.console_info);
109 if (ioctl(0, CONS_GETINFO, &cur_info.console_info) == -1)
110 errc(1, errno, "getting console information");
112 if (ioctl(0, GIO_SCRNMAP, &cur_info.screen_map) == -1)
113 errc(1, errno, "getting screen map");
115 if (ioctl(0, CONS_GET, &cur_info.video_mode_number) == -1)
116 errc(1, errno, "getting video mode number");
118 cur_info.video_mode_info.vi_mode = cur_info.video_mode_number;
120 if (ioctl(0, CONS_MODEINFO, &cur_info.video_mode_info) == -1)
121 errc(1, errno, "getting video mode parameters");
123 normal_fore_color = cur_info.console_info.mv_norm.fore;
124 normal_back_color = cur_info.console_info.mv_norm.back;
125 revers_fore_color = cur_info.console_info.mv_rev.fore;
126 revers_back_color = cur_info.console_info.mv_rev.back;
131 * If something goes wrong along the way we call revert() to go back to the
132 * console state we came from (which is assumed to be working).
134 * NOTE: please also read the comments of init().
142 ioctl(0, VT_ACTIVATE, (caddr_t) (long) cur_info.active_vty);
144 fprintf(stderr, "
\e[=%dA", cur_info.console_info.mv_ovscan);
145 fprintf(stderr, "
\e[=%dF", cur_info.console_info.mv_norm.fore);
146 fprintf(stderr, "
\e[=%dG", cur_info.console_info.mv_norm.back);
147 fprintf(stderr, "
\e[=%dH", cur_info.console_info.mv_rev.fore);
148 fprintf(stderr, "
\e[=%dI", cur_info.console_info.mv_rev.back);
150 ioctl(0, PIO_SCRNMAP, &cur_info.screen_map);
151 ioctl(0, CONS_SET, &cur_info.video_mode_number);
153 if (cur_info.video_mode_info.vi_flags & V_INFO_GRAPHICS) {
154 size[0] = cur_info.video_mode_info.vi_width / 8;
155 size[1] = cur_info.video_mode_info.vi_height /
156 cur_info.console_info.font_size;
157 size[2] = cur_info.console_info.font_size;
159 ioctl(0, KDRASTER, size);
165 * Print a short usage string describing all options, then exit.
172 "usage: vidcontrol [-CdLPpx] [-b color] [-c appearance]"
173 " [-f [size] file]\n"
174 " [-g geometry] [-h size] [-i adapter | mode]"
176 " [-M char] [-m on | off] [-r foreground"
178 " [-S on | off] [-s number] [-t N | off]"
180 " [foreground [background]] [show]\n");
187 * Retrieve the next argument from the command line (for options that require
188 * more than one argument).
192 nextarg(int ac, char **av, int *indp, int oc, int strict)
195 return(av[(*indp)++]);
199 errx(1, "option requires two arguments -- %c", oc);
207 * Guess which file to open. Try to open each combination of a specified set
208 * of file name components.
212 openguess(const char *a[], const char *b[], const char *c[], const char *d[], char **name)
217 for (i = 0; a[i] != NULL; i++) {
218 for (j = 0; b[j] != NULL; j++) {
219 for (k = 0; c[k] != NULL; k++) {
220 for (l = 0; d[l] != NULL; l++) {
221 asprintf(name, "%s%s%s%s",
222 a[i], b[j], c[k], d[l]);
224 f = fopen(*name, "r");
240 * Load a screenmap from a file and set it.
244 load_scrnmap(char *filename)
250 const char *a[] = {"", SCRNMAP_PATH, NULL};
251 const char *b[] = {filename, NULL};
252 const char *c[] = {"", ".scm", NULL};
253 const char *d[] = {"", NULL};
255 fd = openguess(a, b, c, d, &name);
259 errx(1, "screenmap file not found");
262 size = sizeof(scrnmap);
264 if (decode(fd, (char *)&scrnmap, size) != size) {
267 if (fread(&scrnmap, 1, size, fd) != (size_t)size) {
270 errx(1, "bad screenmap file");
274 if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
276 errc(1, errno, "loading screenmap");
284 * Set the default screenmap.
288 load_default_scrnmap(void)
293 for (i = 0; i < 256; i++)
294 *((char*)&scrnmap + i) = i;
296 if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
298 errc(1, errno, "loading default screenmap");
304 * Print the current screenmap to stdout.
310 unsigned char map[256];
313 if (ioctl(0, GIO_SCRNMAP, &map) == -1) {
315 errc(1, errno, "getting screenmap");
318 for (i=0; i<sizeof(map); i++) {
319 if (i > 0 && i % 16 == 0)
320 fprintf(stdout, "\n");
323 fprintf(stdout, " %02x", map[i]);
325 fprintf(stdout, " %03d", map[i]);
328 fprintf(stdout, "\n");
333 * Determine a file's size.
341 if (fstat(fileno(file), &sb) == 0)
349 * Load a font from file and set it.
353 load_font(char *type, char *filename)
357 unsigned long io = 0; /* silence stupid gcc(1) in the Wall mode */
358 char *name, *fontmap, size_sufx[6];
359 const char *a[] = {"", FONT_PATH, NULL};
360 const char *b[] = {filename, NULL};
361 const char *c[] = {"", size_sufx, NULL};
362 const char *d[] = {"", ".fnt", NULL};
369 } sizes[] = {{8, 16, PIO_FONT8x16},
370 {8, 14, PIO_FONT8x14},
374 vinfo.size = sizeof(vinfo);
376 if (ioctl(0, CONS_GETINFO, &vinfo) == -1) {
378 errc(1, errno, "obtaining current video mode parameters");
381 snprintf(size_sufx, sizeof(size_sufx), "-8x%hu", vinfo.font_size);
383 fd = openguess(a, b, c, d, &name);
387 errx(1, "%s: can't load font file", filename);
392 if (sscanf(type, "%dx%d", &w, &h) == 2) {
393 for (i = 0; sizes[i].w != 0; i++) {
394 if (sizes[i].w == w && sizes[i].h == h) {
395 size = DATASIZE(sizes[i]);
397 font_height = sizes[i].h;
404 errx(1, "%s: bad font size specification", type);
407 /* Apply heuristics */
412 size = DATASIZE(sizes[0]);
413 fontmap = (char*) malloc(size);
414 dsize[0] = decode(fd, fontmap, size);
415 dsize[1] = fsize(fd);
419 for (j = 0; j < 2; j++) {
420 for (i = 0; sizes[i].w != 0; i++) {
421 if (DATASIZE(sizes[i]) == dsize[j]) {
424 font_height = sizes[i].h;
434 errx(1, "%s: can't guess font size", filename);
440 fontmap = (char*) malloc(size);
442 if (decode(fd, fontmap, size) != size) {
444 if (fsize(fd) != size ||
445 fread(fontmap, 1, size, fd) != (size_t)size) {
449 errx(1, "%s: bad font file", filename);
453 if (ioctl(0, io, fontmap) == -1) {
455 errc(1, errno, "loading font");
464 * Set the timeout for the screensaver.
468 set_screensaver_timeout(char *arg)
472 if (!strcmp(arg, "off")) {
477 if ((*arg == '\0') || (nsec < 1)) {
479 errx(1, "argument must be a positive number");
483 if (ioctl(0, CONS_BLANKTIME, &nsec) == -1) {
485 errc(1, errno, "setting screensaver period");
491 * Set the cursor's shape/type.
495 set_cursor_type(char *appearance)
499 if (!strcmp(appearance, "normal"))
501 else if (!strcmp(appearance, "blink"))
503 else if (!strcmp(appearance, "destructive"))
507 errx(1, "argument to -c must be normal, blink or destructive");
510 if (ioctl(0, CONS_CURSORTYPE, &type) == -1) {
512 errc(1, errno, "setting cursor type");
518 * Set the video mode.
522 video_mode(int argc, char **argv, int *mode_index)
526 unsigned long mode_num;
527 } modes[] = {{ "80x25", M_VGA_C80x25 },
528 { "80x30", M_VGA_C80x30 },
529 { "80x43", M_ENH_C80x43 },
530 { "80x50", M_VGA_C80x50 },
531 { "80x60", M_VGA_C80x60 },
532 { "VGA_40x25", M_VGA_C40x25 },
533 { "VGA_80x25", M_VGA_C80x25 },
534 { "VGA_80x30", M_VGA_C80x30 },
535 { "VGA_80x50", M_VGA_C80x50 },
536 { "VGA_80x60", M_VGA_C80x60 },
538 { "VGA_90x25", M_VGA_C90x25 },
539 { "VGA_90x30", M_VGA_C90x30 },
540 { "VGA_90x43", M_VGA_C90x43 },
541 { "VGA_90x50", M_VGA_C90x50 },
542 { "VGA_90x60", M_VGA_C90x60 },
544 { "VGA_320x200", M_CG320 },
545 { "EGA_80x25", M_ENH_C80x25 },
546 { "EGA_80x43", M_ENH_C80x43 },
550 int new_mode_num = 0;
555 * Parse the video mode argument...
558 if (*mode_index < argc) {
559 if (!strncmp(argv[*mode_index], "MODE_", 5)) {
560 if (!isdigit(argv[*mode_index][5]))
561 errx(1, "invalid video mode number");
563 new_mode_num = atoi(&argv[*mode_index][5]);
565 for (i = 0; modes[i].name != NULL; ++i) {
566 if (!strcmp(argv[*mode_index], modes[i].name)) {
567 new_mode_num = modes[i].mode_num;
572 if (modes[i].name == NULL)
573 errx(1, "invalid video mode name");
577 * Collect enough information about the new video mode...
580 new_mode_info.vi_mode = new_mode_num;
582 if (ioctl(0, CONS_MODEINFO, &new_mode_info) == -1) {
584 errc(1, errno, "obtaining new video mode parameters");
588 * Try setting the new mode.
591 if (ioctl(0, CONS_SET, &new_mode_num) == -1) {
593 errc(1, errno, "setting video mode");
597 * For raster modes it's not enough to just set the mode.
598 * We also need to explicitly set the raster mode.
601 if (new_mode_info.vi_flags & V_INFO_GRAPHICS) {
604 if (font_height == 0)
605 font_height = cur_info.console_info.font_size;
607 size[2] = font_height;
611 if ((g_cols * 8 > new_mode_info.vi_width) ||
613 size[0] = new_mode_info.vi_width / 8;
620 if ((g_rows * font_height > new_mode_info.vi_height) ||
622 size[1] = new_mode_info.vi_height /
628 /* set raster mode */
630 if (ioctl(0, KDRASTER, size)) {
632 errc(1, errno, "activating raster display");
636 video_mode_changed = 1;
644 * Return the number for a specified color name.
648 get_color_number(char *color)
652 for (i=0; i<16; i++) {
653 if (!strcmp(color, legal_colors[i]))
661 * Get normal text and background colors.
665 get_normal_colors(int argc, char **argv, int *color_index)
669 if (*color_index < argc &&
670 (color = get_color_number(argv[*color_index])) != -1) {
672 normal_fore_color = color;
675 if (*color_index < argc &&
676 (color = get_color_number(argv[*color_index])) != -1) {
678 normal_back_color = color;
685 * Get reverse text and background colors.
689 get_reverse_colors(int argc, char **argv, int *color_index)
693 if ((color = get_color_number(argv[*(color_index)-1])) != -1) {
694 revers_fore_color = color;
697 if (*color_index < argc &&
698 (color = get_color_number(argv[*color_index])) != -1) {
700 revers_back_color = color;
707 * Set normal and reverse foreground and background colors.
713 fprintf(stderr, "
\e[=%dF", normal_fore_color);
714 fprintf(stderr, "
\e[=%dG", normal_back_color);
715 fprintf(stderr, "
\e[=%dH", revers_fore_color);
716 fprintf(stderr, "
\e[=%dI", revers_back_color);
721 * Switch to virtual terminal #arg.
725 set_console(char *arg)
729 if(!arg || strspn(arg,"0123456789") != strlen(arg)) {
731 errx(1, "bad console number");
736 if (n < 1 || n > 16) {
738 errx(1, "console number out of range");
739 } else if (ioctl(0, VT_ACTIVATE, (caddr_t) (long) n) == -1) {
741 errc(1, errno, "switching vty");
747 * Sets the border color.
751 set_border_color(char *arg)
755 if ((color = get_color_number(arg)) != -1)
756 fprintf(stderr, "
\e[=%dA", color);
763 set_mouse_char(char *arg)
765 struct mouse_info mouse;
768 l = strtol(arg, NULL, 0);
770 if ((l < 0) || (l > UCHAR_MAX)) {
772 errx(1, "argument to -M must be 0 through %d", UCHAR_MAX);
775 mouse.operation = MOUSE_MOUSECHAR;
776 mouse.u.mouse_char = (int)l;
778 if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) {
780 errc(1, errno, "setting mouse character");
786 * Show/hide the mouse.
792 struct mouse_info mouse;
794 if (!strcmp(arg, "on")) {
795 mouse.operation = MOUSE_SHOW;
796 } else if (!strcmp(arg, "off")) {
797 mouse.operation = MOUSE_HIDE;
800 errx(1, "argument to -m must be either on or off");
802 ioctl(0, CONS_MOUSECTL, &mouse);
807 set_lockswitch(char *arg)
811 if (!strcmp(arg, "off")) {
813 } else if (!strcmp(arg, "on")) {
817 errx(1, "argument to -S must be either on or off");
820 if (ioctl(0, VT_LOCKSWITCH, &data) == -1) {
822 errc(1, errno, "turning %s vty switching",
823 data == 0x01 ? "off" : "on");
829 * Return the adapter name for a specified type.
833 adapter_name(int type)
838 } names[] = {{ KD_MONO, "MDA" },
839 { KD_HERCULES, "Hercules" },
849 for (i = 0; names[i].type != -1; ++i) {
850 if (names[i].type == type)
854 return names[i].name;
859 * Show graphics adapter information.
863 show_adapter_info(void)
865 struct video_adapter_info ad;
869 if (ioctl(0, CONS_ADPINFO, &ad) == -1) {
871 errc(1, errno, "obtaining adapter information");
874 printf("fb%d:\n", ad.va_index);
875 printf(" %.*s%d, type:%s (%d), flags:0x%x\n",
876 (int)sizeof(ad.va_name), ad.va_name, ad.va_unit,
877 adapter_name(ad.va_type), ad.va_type, ad.va_flags);
878 printf(" initial mode:%d, current mode:%d, BIOS mode:%d\n",
879 ad.va_initial_mode, ad.va_mode, ad.va_initial_bios_mode);
880 printf(" frame buffer window:0x%x, buffer size:0x%zx\n",
881 ad.va_window, ad.va_buffer_size);
882 printf(" window size:0x%zx, origin:0x%x\n",
883 ad.va_window_size, ad.va_window_orig);
884 printf(" display start address (%d, %d), scan line width:%d\n",
885 ad.va_disp_start.x, ad.va_disp_start.y, ad.va_line_width);
886 printf(" reserved:0x%x\n", ad.va_unused0);
891 * Show video mode information.
897 struct video_info vinfo;
902 printf(" mode# flags type size "
903 "font window linear buffer\n");
904 printf("---------------------------------------"
905 "---------------------------------------\n");
907 for (mode = 0; mode <= M_MCA_MODE; ++mode) {
908 vinfo.vi_mode = mode;
910 if (ioctl(0, CONS_MODEINFO, &vinfo))
912 if (vinfo.vi_mode != mode)
915 printf("%3d (0x%03x)", mode, mode);
916 printf(" 0x%08x", vinfo.vi_flags);
918 if (vinfo.vi_flags & V_INFO_GRAPHICS) {
921 snprintf(buf, sizeof(buf), "%dx%dx%d %d",
922 vinfo.vi_width, vinfo.vi_height,
923 vinfo.vi_depth, vinfo.vi_planes);
927 snprintf(buf, sizeof(buf), "%dx%d",
928 vinfo.vi_width, vinfo.vi_height);
931 printf(" %c %-15s", c, buf);
932 snprintf(buf, sizeof(buf), "%dx%d",
933 vinfo.vi_cwidth, vinfo.vi_cheight);
934 printf(" %-5s", buf);
935 printf(" 0x%05x %2dk %2dk",
936 vinfo.vi_window, (int)vinfo.vi_window_size / 1024,
937 (int)vinfo.vi_window_gran/1024);
938 printf(" 0x%08x %dk\n",
939 vinfo.vi_buffer, (int)vinfo.vi_buffer_size / 1024);
947 if (!strcmp(arg, "adapter")) {
949 } else if (!strcmp(arg, "mode")) {
953 errx(1, "argument to -i must be either adapter or mode");
963 fprintf(stdout, "
\e[=0G\n\n");
965 for (i = 0; i < 8; i++) {
966 fprintf(stdout, "
\e[=15F
\e[=0G %2d
\e[=%dF%-16s"
967 "
\e[=15F
\e[=0G %2d
\e[=%dF%-16s "
968 "
\e[=15F %2d
\e[=%dGBACKGROUND
\e[=0G\n",
969 i, i, legal_colors[i], i+8, i+8,
970 legal_colors[i+8], i, i);
973 fprintf(stdout, "
\e[=%dF
\e[=%dG
\e[=%dH
\e[=%dI\n",
974 info.mv_norm.fore, info.mv_norm.back,
975 info.mv_rev.fore, info.mv_rev.back);
980 * Snapshot the video memory of that terminal, using the CONS_SCRSHOT
981 * ioctl, and writes the results to stdout either in the special
982 * binary format (see manual page for details), or in the plain
987 dump_screen(int mode)
992 vinfo.size = sizeof(vinfo);
994 if (ioctl(0, CONS_GETINFO, &vinfo) == -1) {
996 errc(1, errno, "obtaining current video mode parameters");
999 shot.buf = alloca(vinfo.mv_csz * vinfo.mv_rsz * sizeof(u_int16_t));
1001 if (shot.buf == NULL) {
1003 errx(1, "failed to allocate memory for dump");
1006 shot.xsize = vinfo.mv_csz;
1007 shot.ysize = vinfo.mv_rsz;
1009 if (ioctl(0, CONS_SCRSHOT, &shot) == -1) {
1011 errc(1, errno, "dumping screen");
1014 if (mode == DUMP_RAW) {
1015 printf("SCRSHOT_%c%c%c%c", DUMP_FMT_REV, 2,
1016 shot.xsize, shot.ysize);
1020 write(STDOUT_FILENO, shot.buf,
1021 shot.xsize * shot.ysize * sizeof(u_int16_t));
1027 line = alloca(shot.xsize + 1);
1031 errx(1, "failed to allocate memory for line buffer");
1034 for (y = 0; y < shot.ysize; y++) {
1035 for (x = 0; x < shot.xsize; x++) {
1036 ch = shot.buf[x + (y * shot.xsize)];
1039 if (isprint(ch) == 0)
1045 /* Trim trailing spaces */
1049 } while (line[x] == ' ' && x != 0);
1060 * Set the console history buffer size.
1064 set_history(char *opt)
1070 if ((*opt == '\0') || size < 0) {
1072 errx(1, "argument must be a positive number");
1075 if (ioctl(0, CONS_HISTORY, &size) == -1) {
1077 errc(1, errno, "setting history buffer size");
1083 * Clear the console history buffer.
1089 if (ioctl(0, CONS_CLRHIST) == -1) {
1091 errc(1, errno, "clearing history buffer");
1097 main(int argc, char **argv)
1107 info.size = sizeof(info);
1109 if (ioctl(0, CONS_GETINFO, &info) == -1)
1110 err(1, "must be on a virtual console");
1112 while((opt = getopt(argc, argv, "b:Cc:df:g:h:i:l:LM:m:pPr:S:s:t:x")) != -1) {
1115 set_border_color(optarg);
1121 set_cursor_type(optarg);
1128 font = nextarg(argc, argv, &optind, 'f', 0);
1135 load_font(type, font);
1138 if (sscanf(optarg, "%dx%d", &g_cols, &g_rows) != 2) {
1140 warnx("incorrect geometry: %s", optarg);
1145 set_history(optarg);
1151 load_scrnmap(optarg);
1154 load_default_scrnmap();
1157 set_mouse_char(optarg);
1163 dump_screen(DUMP_RAW);
1166 dump_screen(DUMP_TXT);
1169 get_reverse_colors(argc, argv, &optind);
1172 set_lockswitch(optarg);
1175 set_console(optarg);
1178 set_screensaver_timeout(optarg);
1188 if (optind < argc && !strcmp(argv[optind], "show")) {
1193 video_mode(argc, argv, &optind);
1195 get_normal_colors(argc, argv, &optind);
1197 if (colors_changed || video_mode_changed) {
1198 if (!(new_mode_info.vi_flags & V_INFO_GRAPHICS)) {
1199 if ((normal_back_color < 8) && (revers_back_color < 8)) {
1203 errx(1, "bg color for text modes must be < 8");
1210 if ((optind != argc) || (argc == 1))