Rework stop path.
[dragonfly.git] / usr.sbin / vidcontrol / vidcontrol.c
blob45221b06f50d59c7353f22c3c0f801722f87be80
1 /*-
2 * Copyright (c) 1994-1996 Søren Schmidt
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 withough 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 $
29 * $DragonFly: src/usr.sbin/vidcontrol/vidcontrol.c,v 1.13 2008/06/05 18:06:33 swildner Exp $
32 #include <machine/console.h>
34 #include <sys/consio.h>
35 #include <sys/errno.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
39 #include <ctype.h>
40 #include <err.h>
41 #include <limits.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
47 #include "path.h"
48 #include "decode.h"
51 #define DATASIZE(x) ((x).w * (x).h * 256 / 8)
53 #define DUMP_RAW 0
54 #define DUMP_TXT 1
56 #define DUMP_FMT_REV 1
59 char legal_colors[16][16] = {
60 "black", "blue", "green", "cyan",
61 "red", "magenta", "brown", "white",
62 "grey", "lightblue", "lightgreen", "lightcyan",
63 "lightred", "lightmagenta", "yellow", "lightwhite"
66 struct {
67 int active_vty;
68 vid_info_t console_info;
69 unsigned char screen_map[256];
70 int video_mode_number;
71 struct video_info video_mode_info;
72 } cur_info;
74 int hex = 0;
75 int number;
76 int vesa_cols;
77 int vesa_rows;
78 int font_height;
79 int colors_changed;
80 int video_mode_changed;
81 int normal_fore_color, normal_back_color;
82 int revers_fore_color, revers_back_color;
83 char letter;
84 struct vid_info info;
85 struct video_info new_mode_info;
89 * Initialize revert data.
91 * NOTE: the following parameters are not yet saved/restored:
93 * screen saver timeout
94 * cursor type
95 * mouse character and mouse show/hide state
96 * vty switching on/off state
97 * history buffer size
98 * history contents
99 * font maps
102 static void
103 init(void)
105 if (ioctl(0, VT_GETACTIVE, &cur_info.active_vty) == -1)
106 errc(1, errno, "getting active vty");
108 cur_info.console_info.size = sizeof(cur_info.console_info);
110 if (ioctl(0, CONS_GETINFO, &cur_info.console_info) == -1)
111 errc(1, errno, "getting console information");
113 if (ioctl(0, GIO_SCRNMAP, &cur_info.screen_map) == -1)
114 errc(1, errno, "getting screen map");
116 if (ioctl(0, CONS_GET, &cur_info.video_mode_number) == -1)
117 errc(1, errno, "getting video mode number");
119 cur_info.video_mode_info.vi_mode = cur_info.video_mode_number;
121 if (ioctl(0, CONS_MODEINFO, &cur_info.video_mode_info) == -1)
122 errc(1, errno, "getting video mode parameters");
124 normal_fore_color = cur_info.console_info.mv_norm.fore;
125 normal_back_color = cur_info.console_info.mv_norm.back;
126 revers_fore_color = cur_info.console_info.mv_rev.fore;
127 revers_back_color = cur_info.console_info.mv_rev.back;
132 * If something goes wrong along the way we call revert() to go back to the
133 * console state we came from (which is assumed to be working).
135 * NOTE: please also read the comments of init().
138 static void
139 revert(void)
141 int size[3];
143 ioctl(0, VT_ACTIVATE, (caddr_t) (long) cur_info.active_vty);
145 fprintf(stderr, "\e[=%dA", cur_info.console_info.mv_ovscan);
146 fprintf(stderr, "\e[=%dF", cur_info.console_info.mv_norm.fore);
147 fprintf(stderr, "\e[=%dG", cur_info.console_info.mv_norm.back);
148 fprintf(stderr, "\e[=%dH", cur_info.console_info.mv_rev.fore);
149 fprintf(stderr, "\e[=%dI", cur_info.console_info.mv_rev.back);
151 ioctl(0, PIO_SCRNMAP, &cur_info.screen_map);
152 ioctl(0, CONS_SET, &cur_info.video_mode_number);
154 if (cur_info.video_mode_info.vi_flags & V_INFO_GRAPHICS) {
155 size[0] = cur_info.video_mode_info.vi_width / 8;
156 size[1] = cur_info.video_mode_info.vi_height /
157 cur_info.console_info.font_size;
158 size[2] = cur_info.console_info.font_size;
160 ioctl(0, KDRASTER, size);
166 * Print a short usage string describing all options, then exit.
169 static void
170 usage(void)
172 fprintf(stderr,
173 "usage: vidcontrol [-CdLPpx] [-b color] [-c appearance]"
174 " [-f [size] file]\n"
175 " [-g geometry] [-h size] [-i adapter | mode]"
176 " [-l screen_map]\n"
177 " [-M char] [-m on | off] [-r foreground"
178 " background]\n"
179 " [-S on | off] [-s number] [-t N | off]"
180 " [mode]\n"
181 " [foreground [background]] [show]\n");
183 exit(1);
188 * Retrieve the next argument from the command line (for options that require
189 * more than one argument).
192 static char *
193 nextarg(int ac, char **av, int *indp, int oc, int strict)
195 if (*indp < ac)
196 return(av[(*indp)++]);
198 if (strict != 0) {
199 revert();
200 errx(1, "option requires two arguments -- %c", oc);
203 return(NULL);
208 * Guess which file to open. Try to open each combination of a specified set
209 * of file name components.
212 static FILE *
213 openguess(const char *a[], const char *b[], const char *c[], const char *d[], char **name)
215 FILE *f;
216 int i, j, k, l;
218 for (i = 0; a[i] != NULL; i++) {
219 for (j = 0; b[j] != NULL; j++) {
220 for (k = 0; c[k] != NULL; k++) {
221 for (l = 0; d[l] != NULL; l++) {
222 asprintf(name, "%s%s%s%s",
223 a[i], b[j], c[k], d[l]);
225 f = fopen(*name, "r");
227 if (f != NULL)
228 return (f);
230 free(*name);
236 return (NULL);
241 * Load a screenmap from a file and set it.
244 static void
245 load_scrnmap(char *filename)
247 FILE *fd;
248 int size;
249 char *name;
250 scrmap_t scrnmap;
251 const char *a[] = {"", SCRNMAP_PATH, NULL};
252 const char *b[] = {filename, NULL};
253 const char *c[] = {"", ".scm", NULL};
254 const char *d[] = {"", NULL};
256 fd = openguess(a, b, c, d, &name);
258 if (fd == NULL) {
259 revert();
260 errx(1, "screenmap file not found");
263 size = sizeof(scrnmap);
265 if (decode(fd, (char *)&scrnmap, size) != size) {
266 rewind(fd);
268 if (fread(&scrnmap, 1, size, fd) != (size_t)size) {
269 fclose(fd);
270 revert();
271 errx(1, "bad screenmap file");
275 if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
276 revert();
277 errc(1, errno, "loading screenmap");
280 fclose(fd);
285 * Set the default screenmap.
288 static void
289 load_default_scrnmap(void)
291 scrmap_t scrnmap;
292 int i;
294 for (i = 0; i < 256; i++)
295 *((char*)&scrnmap + i) = i;
297 if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
298 revert();
299 errc(1, errno, "loading default screenmap");
305 * Print the current screenmap to stdout.
308 static void
309 print_scrnmap(void)
311 unsigned char map[256];
312 size_t i;
314 if (ioctl(0, GIO_SCRNMAP, &map) == -1) {
315 revert();
316 errc(1, errno, "getting screenmap");
319 for (i=0; i<sizeof(map); i++) {
320 if (i > 0 && i % 16 == 0)
321 fprintf(stdout, "\n");
323 if (hex != 0)
324 fprintf(stdout, " %02x", map[i]);
325 else
326 fprintf(stdout, " %03d", map[i]);
329 fprintf(stdout, "\n");
334 * Determine a file's size.
337 static int
338 fsize(FILE *file)
340 struct stat sb;
342 if (fstat(fileno(file), &sb) == 0)
343 return sb.st_size;
344 else
345 return -1;
350 * Load a font from file and set it.
353 static void
354 load_font(char *type, char *filename)
356 FILE *fd;
357 int h, i, size, w;
358 unsigned long io = 0; /* silence stupid gcc(1) in the Wall mode */
359 char *name, *fontmap, size_sufx[6];
360 const char *a[] = {"", FONT_PATH, NULL};
361 const char *b[] = {filename, NULL};
362 const char *c[] = {"", size_sufx, NULL};
363 const char *d[] = {"", ".fnt", NULL};
364 vid_info_t vinfo;
366 struct sizeinfo {
367 int w;
368 int h;
369 unsigned long io;
370 } sizes[] = {{8, 16, PIO_FONT8x16},
371 {8, 14, PIO_FONT8x14},
372 {8, 8, PIO_FONT8x8},
373 {0, 0, 0}};
375 vinfo.size = sizeof(vinfo);
377 if (ioctl(0, CONS_GETINFO, &vinfo) == -1) {
378 revert();
379 errc(1, errno, "obtaining current video mode parameters");
382 snprintf(size_sufx, sizeof(size_sufx), "-8x%d", vinfo.font_size);
384 fd = openguess(a, b, c, d, &name);
386 if (fd == NULL) {
387 revert();
388 errx(1, "%s: can't load font file", filename);
391 if (type != NULL) {
392 size = 0;
393 if (sscanf(type, "%dx%d", &w, &h) == 2) {
394 for (i = 0; sizes[i].w != 0; i++) {
395 if (sizes[i].w == w && sizes[i].h == h) {
396 size = DATASIZE(sizes[i]);
397 io = sizes[i].io;
398 font_height = sizes[i].h;
402 if (size == 0) {
403 fclose(fd);
404 revert();
405 errx(1, "%s: bad font size specification", type);
407 } else {
408 /* Apply heuristics */
410 int j;
411 int dsize[2];
413 size = DATASIZE(sizes[0]);
414 fontmap = (char*) malloc(size);
415 dsize[0] = decode(fd, fontmap, size);
416 dsize[1] = fsize(fd);
417 free(fontmap);
419 size = 0;
420 for (j = 0; j < 2; j++) {
421 for (i = 0; sizes[i].w != 0; i++) {
422 if (DATASIZE(sizes[i]) == dsize[j]) {
423 size = dsize[j];
424 io = sizes[i].io;
425 font_height = sizes[i].h;
426 j = 2; /* XXX */
427 break;
432 if (size == 0) {
433 fclose(fd);
434 revert();
435 errx(1, "%s: can't guess font size", filename);
438 rewind(fd);
441 fontmap = (char*) malloc(size);
443 if (decode(fd, fontmap, size) != size) {
444 rewind(fd);
445 if (fsize(fd) != size ||
446 fread(fontmap, 1, size, fd) != (size_t)size) {
447 fclose(fd);
448 free(fontmap);
449 revert();
450 errx(1, "%s: bad font file", filename);
454 if (ioctl(0, io, fontmap) == -1) {
455 revert();
456 errc(1, errno, "loading font");
459 fclose(fd);
460 free(fontmap);
465 * Set the timeout for the screensaver.
468 static void
469 set_screensaver_timeout(char *arg)
471 int nsec;
473 if (!strcmp(arg, "off")) {
474 nsec = 0;
475 } else {
476 nsec = atoi(arg);
478 if ((*arg == '\0') || (nsec < 1)) {
479 revert();
480 errx(1, "argument must be a positive number");
484 if (ioctl(0, CONS_BLANKTIME, &nsec) == -1) {
485 revert();
486 errc(1, errno, "setting screensaver period");
492 * Set the cursor's shape/type.
495 static void
496 set_cursor_type(char *appearance)
498 int type;
500 if (!strcmp(appearance, "normal"))
501 type = 0;
502 else if (!strcmp(appearance, "blink"))
503 type = 1;
504 else if (!strcmp(appearance, "destructive"))
505 type = 3;
506 else {
507 revert();
508 errx(1, "argument to -c must be normal, blink or destructive");
511 if (ioctl(0, CONS_CURSORTYPE, &type) == -1) {
512 revert();
513 errc(1, errno, "setting cursor type");
519 * Set the video mode.
522 static void
523 video_mode(int argc, char **argv, int *mode_index)
525 static struct {
526 const char *name;
527 unsigned long mode_num;
528 } modes[] = {{ "80x25", M_VGA_C80x25 },
529 { "80x30", M_VGA_C80x30 },
530 { "80x43", M_ENH_C80x43 },
531 { "80x50", M_VGA_C80x50 },
532 { "80x60", M_VGA_C80x60 },
533 { "132x25", M_VESA_C132x25 },
534 { "132x43", M_VESA_C132x43 },
535 { "132x50", M_VESA_C132x50 },
536 { "132x60", M_VESA_C132x60 },
537 { "VGA_40x25", M_VGA_C40x25 },
538 { "VGA_80x25", M_VGA_C80x25 },
539 { "VGA_80x30", M_VGA_C80x30 },
540 { "VGA_80x50", M_VGA_C80x50 },
541 { "VGA_80x60", M_VGA_C80x60 },
542 #ifdef SW_VGA_C90x25
543 { "VGA_90x25", M_VGA_C90x25 },
544 { "VGA_90x30", M_VGA_C90x30 },
545 { "VGA_90x43", M_VGA_C90x43 },
546 { "VGA_90x50", M_VGA_C90x50 },
547 { "VGA_90x60", M_VGA_C90x60 },
548 #endif
549 { "VGA_320x200", M_CG320 },
550 { "EGA_80x25", M_ENH_C80x25 },
551 { "EGA_80x43", M_ENH_C80x43 },
552 { "VESA_132x25", M_VESA_C132x25 },
553 { "VESA_132x43", M_VESA_C132x43 },
554 { "VESA_132x50", M_VESA_C132x50 },
555 { "VESA_132x60", M_VESA_C132x60 },
556 { "VESA_800x600", M_VESA_800x600 },
557 { NULL, 0 },
560 int new_mode_num = 0;
561 int size[3];
562 int i;
565 * Parse the video mode argument...
568 if (*mode_index < argc) {
569 if (!strncmp(argv[*mode_index], "MODE_", 5)) {
570 if (!isdigit(argv[*mode_index][5]))
571 errx(1, "invalid video mode number");
573 new_mode_num = atoi(&argv[*mode_index][5]);
574 } else {
575 for (i = 0; modes[i].name != NULL; ++i) {
576 if (!strcmp(argv[*mode_index], modes[i].name)) {
577 new_mode_num = modes[i].mode_num;
578 break;
582 if (modes[i].name == NULL)
583 errx(1, "invalid video mode name");
587 * Collect enough information about the new video mode...
590 new_mode_info.vi_mode = new_mode_num;
592 if (ioctl(0, CONS_MODEINFO, &new_mode_info) == -1) {
593 revert();
594 errc(1, errno, "obtaining new video mode parameters");
598 * Try setting the new mode.
601 if (ioctl(0, CONS_SET, &new_mode_num) == -1) {
602 revert();
603 errc(1, errno, "setting video mode");
607 * For raster modes it's not enough to just set the mode.
608 * We also need to explicitly set the raster mode.
611 if (new_mode_info.vi_flags & V_INFO_GRAPHICS) {
612 /* font size */
614 if (font_height == 0)
615 font_height = cur_info.console_info.font_size;
617 size[2] = font_height;
619 /* adjust columns */
621 if ((vesa_cols * 8 > new_mode_info.vi_width) ||
622 (vesa_cols <= 0)) {
623 size[0] = new_mode_info.vi_width / 8;
624 } else {
625 size[0] = vesa_cols;
628 /* adjust rows */
630 if ((vesa_rows * font_height > new_mode_info.vi_height) ||
631 (vesa_rows <= 0)) {
632 size[1] = new_mode_info.vi_height /
633 font_height;
634 } else {
635 size[1] = vesa_rows;
638 /* set raster mode */
640 if (ioctl(0, KDRASTER, size)) {
641 revert();
642 errc(1, errno, "activating raster display");
646 video_mode_changed = 1;
648 (*mode_index)++;
654 * Return the number for a specified color name.
657 static int
658 get_color_number(char *color)
660 int i;
662 for (i=0; i<16; i++) {
663 if (!strcmp(color, legal_colors[i]))
664 return i;
666 return -1;
671 * Get normal text and background colors.
674 static void
675 get_normal_colors(int argc, char **argv, int *color_index)
677 int color;
679 if (*color_index < argc &&
680 (color = get_color_number(argv[*color_index])) != -1) {
681 (*color_index)++;
682 normal_fore_color = color;
683 colors_changed = 1;
685 if (*color_index < argc &&
686 (color = get_color_number(argv[*color_index])) != -1) {
687 (*color_index)++;
688 normal_back_color = color;
695 * Get reverse text and background colors.
698 static void
699 get_reverse_colors(int argc, char **argv, int *color_index)
701 int color;
703 if ((color = get_color_number(argv[*(color_index)-1])) != -1) {
704 revers_fore_color = color;
705 colors_changed = 1;
707 if (*color_index < argc &&
708 (color = get_color_number(argv[*color_index])) != -1) {
709 (*color_index)++;
710 revers_back_color = color;
717 * Set normal and reverse foreground and background colors.
720 static void
721 set_colors(void)
723 fprintf(stderr, "\e[=%dF", normal_fore_color);
724 fprintf(stderr, "\e[=%dG", normal_back_color);
725 fprintf(stderr, "\e[=%dH", revers_fore_color);
726 fprintf(stderr, "\e[=%dI", revers_back_color);
731 * Switch to virtual terminal #arg.
734 static void
735 set_console(char *arg)
737 int n;
739 if(!arg || strspn(arg,"0123456789") != strlen(arg)) {
740 revert();
741 errx(1, "bad console number");
744 n = atoi(arg);
746 if (n < 1 || n > 16) {
747 revert();
748 errx(1, "console number out of range");
749 } else if (ioctl(0, VT_ACTIVATE, (caddr_t) (long) n) == -1) {
750 revert();
751 errc(1, errno, "switching vty");
757 * Sets the border color.
760 static void
761 set_border_color(char *arg)
763 int color;
765 if ((color = get_color_number(arg)) != -1)
766 fprintf(stderr, "\e[=%dA", color);
767 else
768 usage();
772 static void
773 set_mouse_char(char *arg)
775 struct mouse_info mouse;
776 long l;
778 l = strtol(arg, NULL, 0);
780 if ((l < 0) || (l > UCHAR_MAX)) {
781 revert();
782 errx(1, "argument to -M must be 0 through %d", UCHAR_MAX);
785 mouse.operation = MOUSE_MOUSECHAR;
786 mouse.u.mouse_char = (int)l;
788 if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) {
789 revert();
790 errc(1, errno, "setting mouse character");
796 * Show/hide the mouse.
799 static void
800 set_mouse(char *arg)
802 struct mouse_info mouse;
804 if (!strcmp(arg, "on")) {
805 mouse.operation = MOUSE_SHOW;
806 } else if (!strcmp(arg, "off")) {
807 mouse.operation = MOUSE_HIDE;
808 } else {
809 revert();
810 errx(1, "argument to -m must be either on or off");
813 if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) {
814 revert();
815 errc(1, errno, "%sing the mouse",
816 mouse.operation == MOUSE_SHOW ? "show" : "hid");
821 static void
822 set_lockswitch(char *arg)
824 int data;
826 if (!strcmp(arg, "off")) {
827 data = 0x01;
828 } else if (!strcmp(arg, "on")) {
829 data = 0x02;
830 } else {
831 revert();
832 errx(1, "argument to -S must be either on or off");
835 if (ioctl(0, VT_LOCKSWITCH, &data) == -1) {
836 revert();
837 errc(1, errno, "turning %s vty switching",
838 data == 0x01 ? "off" : "on");
844 * Return the adapter name for a specified type.
847 static const char *
848 adapter_name(int type)
850 static struct {
851 int type;
852 const char *name;
853 } names[] = {{ KD_MONO, "MDA" },
854 { KD_HERCULES, "Hercules" },
855 { KD_CGA, "CGA" },
856 { KD_EGA, "EGA" },
857 { KD_VGA, "VGA" },
858 { KD_TGA, "TGA" },
859 { -1, "Unknown" },
862 int i;
864 for (i = 0; names[i].type != -1; ++i) {
865 if (names[i].type == type)
866 break;
869 return names[i].name;
874 * Show graphics adapter information.
877 static void
878 show_adapter_info(void)
880 struct video_adapter_info ad;
882 ad.va_index = 0;
884 if (ioctl(0, CONS_ADPINFO, &ad) == -1) {
885 revert();
886 errc(1, errno, "obtaining adapter information");
889 printf("fb%d:\n", ad.va_index);
890 printf(" %.*s%d, type:%s%s (%d), flags:0x%x\n",
891 (int)sizeof(ad.va_name), ad.va_name, ad.va_unit,
892 (ad.va_flags & V_ADP_VESA) ? "VESA " : "",
893 adapter_name(ad.va_type), ad.va_type, ad.va_flags);
894 printf(" initial mode:%d, current mode:%d, BIOS mode:%d\n",
895 ad.va_initial_mode, ad.va_mode, ad.va_initial_bios_mode);
896 printf(" frame buffer window:0x%x, buffer size:0x%x\n",
897 ad.va_window, ad.va_buffer_size);
898 printf(" window size:0x%x, origin:0x%x\n",
899 ad.va_window_size, ad.va_window_orig);
900 printf(" display start address (%d, %d), scan line width:%d\n",
901 ad.va_disp_start.x, ad.va_disp_start.y, ad.va_line_width);
902 printf(" reserved:0x%x\n", ad.va_unused0);
907 * Show video mode information.
910 static void
911 show_mode_info(void)
913 struct video_info vinfo;
914 char buf[80];
915 int mode;
916 int c;
918 printf(" mode# flags type size "
919 "font window linear buffer\n");
920 printf("---------------------------------------"
921 "---------------------------------------\n");
923 for (mode = 0; mode < M_VESA_MODE_MAX; ++mode) {
924 vinfo.vi_mode = mode;
926 if (ioctl(0, CONS_MODEINFO, &vinfo))
927 continue;
928 if (vinfo.vi_mode != mode)
929 continue;
931 printf("%3d (0x%03x)", mode, mode);
932 printf(" 0x%08x", vinfo.vi_flags);
934 if (vinfo.vi_flags & V_INFO_GRAPHICS) {
935 c = 'G';
937 snprintf(buf, sizeof(buf), "%dx%dx%d %d",
938 vinfo.vi_width, vinfo.vi_height,
939 vinfo.vi_depth, vinfo.vi_planes);
940 } else {
941 c = 'T';
943 snprintf(buf, sizeof(buf), "%dx%d",
944 vinfo.vi_width, vinfo.vi_height);
947 printf(" %c %-15s", c, buf);
948 snprintf(buf, sizeof(buf), "%dx%d",
949 vinfo.vi_cwidth, vinfo.vi_cheight);
950 printf(" %-5s", buf);
951 printf(" 0x%05x %2dk %2dk",
952 vinfo.vi_window, (int)vinfo.vi_window_size / 1024,
953 (int)vinfo.vi_window_gran/1024);
954 printf(" 0x%08x %dk\n",
955 vinfo.vi_buffer, (int)vinfo.vi_buffer_size / 1024);
960 static void
961 show_info(char *arg)
963 if (!strcmp(arg, "adapter")) {
964 show_adapter_info();
965 } else if (!strcmp(arg, "mode")) {
966 show_mode_info();
967 } else {
968 revert();
969 errx(1, "argument to -i must be either adapter or mode");
974 static void
975 test_frame(void)
977 int i;
979 fprintf(stdout, "\e[=0G\n\n");
981 for (i = 0; i < 8; i++) {
982 fprintf(stdout, "\e[=15F\e[=0G %2d \e[=%dF%-16s"
983 "\e[=15F\e[=0G %2d \e[=%dF%-16s "
984 "\e[=15F %2d \e[=%dGBACKGROUND\e[=0G\n",
985 i, i, legal_colors[i], i+8, i+8,
986 legal_colors[i+8], i, i);
989 fprintf(stdout, "\e[=%dF\e[=%dG\e[=%dH\e[=%dI\n",
990 info.mv_norm.fore, info.mv_norm.back,
991 info.mv_rev.fore, info.mv_rev.back);
996 * Snapshot the video memory of that terminal, using the CONS_SCRSHOT
997 * ioctl, and writes the results to stdout either in the special
998 * binary format (see manual page for details), or in the plain
999 * text format.
1002 static void
1003 dump_screen(int mode)
1005 scrshot_t shot;
1006 vid_info_t vinfo;
1008 vinfo.size = sizeof(vinfo);
1010 if (ioctl(0, CONS_GETINFO, &vinfo) == -1) {
1011 revert();
1012 errc(1, errno, "obtaining current video mode parameters");
1015 shot.buf = alloca(vinfo.mv_csz * vinfo.mv_rsz * sizeof(u_int16_t));
1017 if (shot.buf == NULL) {
1018 revert();
1019 errx(1, "failed to allocate memory for dump");
1022 shot.xsize = vinfo.mv_csz;
1023 shot.ysize = vinfo.mv_rsz;
1025 if (ioctl(0, CONS_SCRSHOT, &shot) == -1) {
1026 revert();
1027 errc(1, errno, "dumping screen");
1030 if (mode == DUMP_RAW) {
1031 printf("SCRSHOT_%c%c%c%c", DUMP_FMT_REV, 2,
1032 shot.xsize, shot.ysize);
1034 fflush(stdout);
1036 write(STDOUT_FILENO, shot.buf,
1037 shot.xsize * shot.ysize * sizeof(u_int16_t));
1038 } else {
1039 char *line;
1040 int x, y;
1041 u_int16_t ch;
1043 line = alloca(shot.xsize + 1);
1045 if (line == NULL) {
1046 revert();
1047 errx(1, "failed to allocate memory for line buffer");
1050 for (y = 0; y < shot.ysize; y++) {
1051 for (x = 0; x < shot.xsize; x++) {
1052 ch = shot.buf[x + (y * shot.xsize)];
1053 ch &= 0xff;
1055 if (isprint(ch) == 0)
1056 ch = ' ';
1058 line[x] = (char)ch;
1061 /* Trim trailing spaces */
1063 do {
1064 line[x--] = '\0';
1065 } while (line[x] == ' ' && x != 0);
1067 puts(line);
1070 fflush(stdout);
1076 * Set the console history buffer size.
1079 static void
1080 set_history(char *opt)
1082 int size;
1084 size = atoi(opt);
1086 if ((*opt == '\0') || size < 0) {
1087 revert();
1088 errx(1, "argument must be a positive number");
1091 if (ioctl(0, CONS_HISTORY, &size) == -1) {
1092 revert();
1093 errc(1, errno, "setting history buffer size");
1099 * Clear the console history buffer.
1102 static void
1103 clear_history(void)
1105 if (ioctl(0, CONS_CLRHIST) == -1) {
1106 revert();
1107 errc(1, errno, "clearing history buffer");
1113 main(int argc, char **argv)
1115 char *font, *type;
1116 int opt;
1118 if (argc == 1)
1119 usage();
1121 init();
1123 info.size = sizeof(info);
1125 if (ioctl(0, CONS_GETINFO, &info) == -1)
1126 err(1, "must be on a virtual console");
1128 while((opt = getopt(argc, argv, "b:Cc:df:g:h:i:l:LM:m:pPr:S:s:t:x")) != -1) {
1129 switch(opt) {
1130 case 'b':
1131 set_border_color(optarg);
1132 break;
1133 case 'C':
1134 clear_history();
1135 break;
1136 case 'c':
1137 set_cursor_type(optarg);
1138 break;
1139 case 'd':
1140 print_scrnmap();
1141 break;
1142 case 'f':
1143 type = optarg;
1144 font = nextarg(argc, argv, &optind, 'f', 0);
1146 if (font == NULL) {
1147 type = NULL;
1148 font = optarg;
1151 load_font(type, font);
1152 break;
1153 case 'g':
1154 if (sscanf(optarg, "%dx%d",
1155 &vesa_cols, &vesa_rows) != 2) {
1156 revert();
1157 warnx("incorrect geometry: %s", optarg);
1158 usage();
1160 break;
1161 case 'h':
1162 set_history(optarg);
1163 break;
1164 case 'i':
1165 show_info(optarg);
1166 break;
1167 case 'l':
1168 load_scrnmap(optarg);
1169 break;
1170 case 'L':
1171 load_default_scrnmap();
1172 break;
1173 case 'M':
1174 set_mouse_char(optarg);
1175 break;
1176 case 'm':
1177 set_mouse(optarg);
1178 break;
1179 case 'p':
1180 dump_screen(DUMP_RAW);
1181 break;
1182 case 'P':
1183 dump_screen(DUMP_TXT);
1184 break;
1185 case 'r':
1186 get_reverse_colors(argc, argv, &optind);
1187 break;
1188 case 'S':
1189 set_lockswitch(optarg);
1190 break;
1191 case 's':
1192 set_console(optarg);
1193 break;
1194 case 't':
1195 set_screensaver_timeout(optarg);
1196 break;
1197 case 'x':
1198 hex = 1;
1199 break;
1200 default:
1201 usage();
1205 if (optind < argc && !strcmp(argv[optind], "show")) {
1206 test_frame();
1207 optind++;
1210 video_mode(argc, argv, &optind);
1212 get_normal_colors(argc, argv, &optind);
1214 if (colors_changed || video_mode_changed) {
1215 if (!(new_mode_info.vi_flags & V_INFO_GRAPHICS)) {
1216 if ((normal_back_color < 8) && (revers_back_color < 8)) {
1217 set_colors();
1218 } else {
1219 revert();
1220 errx(1, "bg color for text modes must be < 8");
1222 } else {
1223 set_colors();
1227 if ((optind != argc) || (argc == 1))
1228 usage();
1230 return 0;