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 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.2 2003/06/17 04:30:03 dillon Exp $
39 #include <machine/console.h>
40 #include <sys/consio.h>
41 #include <sys/errno.h>
42 #include <sys/types.h>
47 #define _VESA_800x600_DFL_COLS 80
48 #define _VESA_800x600_DFL_ROWS 25
49 #define _VESA_800x600_DFL_FNSZ 16
54 #define DUMP_FMT_REV 1
56 char legal_colors[16][16] = {
57 "black", "blue", "green", "cyan",
58 "red", "magenta", "brown", "white",
59 "grey", "lightblue", "lightgreen", "lightcyan",
60 "lightred", "lightmagenta", "yellow", "lightwhite"
64 int vesa_cols = _VESA_800x600_DFL_COLS;
65 int vesa_rows = _VESA_800x600_DFL_ROWS;
73 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
74 "usage: vidcontrol [-CdLPpx] [-b color] [-c appearance] [-f [size] file]",
75 " [-g geometry] [-h size] [-i adapter | mode] [-l screen_map]",
76 " [-M char] [-m on | off] [-r foreground background]",
77 " [-S on | off] [-s number] [-t N | off] [mode]",
78 " [foreground [background]] [show]");
83 nextarg(int ac, char **av, int *indp, int oc, int strict)
86 return(av[(*indp)++]);
88 errx(1, "option requires two arguments -- %c", oc);
93 openguess(char *a[], char *b[], char *c[], char *d[], char **name)
98 for (i = 0; a[i] != NULL; i++) {
99 for (j = 0; b[j] != NULL; j++) {
100 for (k = 0; c[k] != NULL; k++) {
101 for (l = 0; d[l] != NULL; l++) {
102 asprintf(name, "%s%s%s%s", a[i], b[j],
104 f = fopen(*name, "r");
116 load_scrnmap(char *filename)
122 char *a[] = {"", SCRNMAP_PATH, NULL};
123 char *b[] = {filename, NULL};
124 char *c[] = {"", ".scm", NULL};
125 char *d[] = {"", NULL};
127 fd = openguess(a, b, c, d, &name);
129 warn("screenmap file not found");
132 size = sizeof(scrnmap);
133 if (decode(fd, (char *)&scrnmap, size) != size) {
135 if (fread(&scrnmap, 1, size, fd) != size) {
136 warnx("bad screenmap file");
141 if (ioctl(0, PIO_SCRNMAP, &scrnmap) < 0)
142 warn("can't load screenmap");
147 load_default_scrnmap()
152 for (i=0; i<256; i++)
153 *((char*)&scrnmap + i) = i;
154 if (ioctl(0, PIO_SCRNMAP, &scrnmap) < 0)
155 warn("can't load default screenmap");
161 unsigned char map[256];
164 if (ioctl(0, GIO_SCRNMAP, &map) < 0) {
165 warn("getting screenmap");
168 for (i=0; i<sizeof(map); i++) {
169 if (i > 0 && i % 16 == 0)
170 fprintf(stdout, "\n");
172 fprintf(stdout, " %02x", map[i]);
174 fprintf(stdout, " %03d", map[i]);
176 fprintf(stdout, "\n");
185 if (fstat(fileno(file), &sb) == 0)
191 #define DATASIZE(x) ((x).w * (x).h * 256 / 8)
194 load_font(char *type, char *filename)
198 unsigned long io = 0; /* silence stupid gcc(1) in the Wall mode */
199 char *name, *fontmap, size_sufx[6];
200 char *a[] = {"", FONT_PATH, NULL};
201 char *b[] = {filename, NULL};
202 char *c[] = {"", size_sufx, NULL};
203 char *d[] = {"", ".fnt", NULL};
210 } sizes[] = {{8, 16, PIO_FONT8x16},
211 {8, 14, PIO_FONT8x14},
215 info.size = sizeof(info);
216 if (ioctl(0, CONS_GETINFO, &info) == -1) {
217 warn("failed to obtain current video mode parameters");
220 snprintf(size_sufx, sizeof(size_sufx), "-8x%d", info.font_size);
221 fd = openguess(a, b, c, d, &name);
223 warn("%s: can't load font file", filename);
228 if (sscanf(type, "%dx%d", &w, &h) == 2)
229 for (i = 0; sizes[i].w != 0; i++)
230 if (sizes[i].w == w && sizes[i].h == h) {
231 size = DATASIZE(sizes[i]);
236 warnx("%s: bad font size specification", type);
241 /* Apply heuristics */
245 size = DATASIZE(sizes[0]);
246 fontmap = (char*) malloc(size);
247 dsize[0] = decode(fd, fontmap, size);
248 dsize[1] = fsize(fd);
252 for (j = 0; j < 2; j++)
253 for (i = 0; sizes[i].w != 0; i++)
254 if (DATASIZE(sizes[i]) == dsize[j]) {
262 warnx("%s: can't guess font size", filename);
269 fontmap = (char*) malloc(size);
270 if (decode(fd, fontmap, size) != size) {
272 if (fsize(fd) != size || fread(fontmap, 1, size, fd) != size) {
273 warnx("%s: bad font file", filename);
279 if (ioctl(0, io, fontmap) < 0)
280 warn("can't load font");
286 set_screensaver_timeout(char *arg)
290 if (!strcmp(arg, "off"))
294 if ((*arg == '\0') || (nsec < 1)) {
295 warnx("argument must be a positive number");
299 if (ioctl(0, CONS_BLANKTIME, &nsec) == -1)
300 warn("setting screensaver period");
304 set_cursor_type(char *appearence)
308 if (!strcmp(appearence, "normal"))
310 else if (!strcmp(appearence, "blink"))
312 else if (!strcmp(appearence, "destructive"))
315 warnx("argument to -c must be normal, blink or destructive");
318 ioctl(0, CONS_CURSORTYPE, &type);
322 video_mode(int argc, char **argv, int *index)
328 { "80x25", SW_TEXT_80x25 },
329 { "80x30", SW_TEXT_80x30 },
330 { "80x43", SW_TEXT_80x43 },
331 { "80x50", SW_TEXT_80x50 },
332 { "80x60", SW_TEXT_80x60 },
333 { "132x25", SW_TEXT_132x25 },
334 { "132x30", SW_TEXT_132x30 },
335 { "132x43", SW_TEXT_132x43 },
336 { "132x50", SW_TEXT_132x50 },
337 { "132x60", SW_TEXT_132x60 },
338 { "VGA_40x25", SW_VGA_C40x25 },
339 { "VGA_80x25", SW_VGA_C80x25 },
340 { "VGA_80x30", SW_VGA_C80x30 },
341 { "VGA_80x50", SW_VGA_C80x50 },
342 { "VGA_80x60", SW_VGA_C80x60 },
344 { "VGA_90x25", SW_VGA_C90x25 },
345 { "VGA_90x30", SW_VGA_C90x30 },
346 { "VGA_90x43", SW_VGA_C90x43 },
347 { "VGA_90x50", SW_VGA_C90x50 },
348 { "VGA_90x60", SW_VGA_C90x60 },
350 { "VGA_320x200", SW_VGA_CG320 },
351 { "EGA_80x25", SW_ENH_C80x25 },
352 { "EGA_80x43", SW_ENH_C80x43 },
353 { "VESA_132x25", SW_VESA_C132x25 },
354 { "VESA_132x43", SW_VESA_C132x43 },
355 { "VESA_132x50", SW_VESA_C132x50 },
356 { "VESA_132x60", SW_VESA_C132x60 },
357 { "VESA_800x600", SW_VESA_800x600 },
360 unsigned long mode = 0;
366 if (ioctl(0, CONS_GET, &cur_mode) < 0)
367 err(1, "cannot get the current video mode");
369 for (i = 0; modes[i].name != NULL; ++i) {
370 if (!strcmp(argv[*index], modes[i].name)) {
371 mode = modes[i].mode;
375 if (modes[i].name == NULL)
377 if (ioctl(0, mode, NULL) < 0)
378 warn("cannot set videomode");
379 if (mode == SW_VESA_800x600) {
381 if ((vesa_cols * 8 > 800) || (vesa_cols <= 0)) {
382 warnx("incorrect number of columns: %d",
384 size[0] = _VESA_800x600_DFL_COLS;
389 if ((vesa_rows * _VESA_800x600_DFL_FNSZ > 600) ||
391 warnx("incorrect number of rows: %d",
393 size[1] = _VESA_800x600_DFL_ROWS;
398 size[2] = _VESA_800x600_DFL_FNSZ;
399 if (ioctl(0, KDRASTER, size)) {
401 if (cur_mode >= M_VESA_BASE)
403 _IO('V', cur_mode - M_VESA_BASE),
406 ioctl(0, _IO('S', cur_mode), NULL);
407 warnc(ioerr, "cannot activate raster display");
416 get_color_number(char *color)
421 if (!strcmp(color, legal_colors[i]))
427 set_normal_colors(int argc, char **argv, int *index)
431 if (*index < argc && (color = get_color_number(argv[*index])) != -1) {
433 fprintf(stderr, "
\e[=%dF", color);
435 && (color = get_color_number(argv[*index])) != -1
438 fprintf(stderr, "
\e[=%dG", color);
444 set_reverse_colors(int argc, char **argv, int *index)
448 if ((color = get_color_number(argv[*(index)-1])) != -1) {
449 fprintf(stderr, "
\e[=%dH", color);
451 && (color = get_color_number(argv[*index])) != -1
454 fprintf(stderr, "
\e[=%dI", color);
460 set_console(char *arg)
464 if( !arg || strspn(arg,"0123456789") != strlen(arg)) {
465 warnx("bad console number");
470 if (n < 1 || n > 16) {
471 warnx("console number out of range");
472 } else if (ioctl(0, VT_ACTIVATE, (caddr_t) (long) n) == -1)
473 warn("ioctl(VT_ACTIVATE)");
477 set_border_color(char *arg)
481 if ((color = get_color_number(arg)) != -1) {
482 fprintf(stderr, "
\e[=%dA", color);
489 set_mouse_char(char *arg)
491 struct mouse_info mouse;
494 l = strtol(arg, NULL, 0);
495 if ((l < 0) || (l > UCHAR_MAX)) {
496 warnx("argument to -M must be 0 through %d", UCHAR_MAX);
499 mouse.operation = MOUSE_MOUSECHAR;
500 mouse.u.mouse_char = (int)l;
501 ioctl(0, CONS_MOUSECTL, &mouse);
507 struct mouse_info mouse;
509 if (!strcmp(arg, "on"))
510 mouse.operation = MOUSE_SHOW;
511 else if (!strcmp(arg, "off"))
512 mouse.operation = MOUSE_HIDE;
514 warnx("argument to -m must be either on or off");
517 ioctl(0, CONS_MOUSECTL, &mouse);
521 set_lockswitch(char *arg)
525 if (!strcmp(arg, "off"))
527 else if (!strcmp(arg, "on"))
530 warnx("argument to -S must be either on or off");
533 if (ioctl(0, VT_LOCKSWITCH, &data) == -1)
534 warn("ioctl(VT_LOCKSWITCH)");
538 *adapter_name(int type)
545 { KD_HERCULES, "Hercules" },
549 { KD_PC98, "PC-98xx" },
555 for (i = 0; names[i].type != -1; ++i)
556 if (names[i].type == type)
558 return names[i].name;
562 show_adapter_info(void)
564 struct video_adapter_info ad;
567 if (ioctl(0, CONS_ADPINFO, &ad)) {
568 warn("failed to obtain adapter information");
572 printf("fb%d:\n", ad.va_index);
573 printf(" %.*s%d, type:%s%s (%d), flags:0x%x\n",
574 (int)sizeof(ad.va_name), ad.va_name, ad.va_unit,
575 (ad.va_flags & V_ADP_VESA) ? "VESA " : "",
576 adapter_name(ad.va_type), ad.va_type, ad.va_flags);
577 printf(" initial mode:%d, current mode:%d, BIOS mode:%d\n",
578 ad.va_initial_mode, ad.va_mode, ad.va_initial_bios_mode);
579 printf(" frame buffer window:0x%x, buffer size:0x%x\n",
580 ad.va_window, ad.va_buffer_size);
581 printf(" window size:0x%x, origin:0x%x\n",
582 ad.va_window_size, ad.va_window_orig);
583 printf(" display start address (%d, %d), scan line width:%d\n",
584 ad.va_disp_start.x, ad.va_disp_start.y, ad.va_line_width);
585 printf(" reserved:0x%x\n", ad.va_unused0);
591 struct video_info info;
596 printf(" mode# flags type size "
597 "font window linear buffer\n");
598 printf("---------------------------------------"
599 "---------------------------------------\n");
600 for (mode = 0; mode < M_VESA_MODE_MAX; ++mode) {
602 if (ioctl(0, CONS_MODEINFO, &info))
604 if (info.vi_mode != mode)
607 printf("%3d (0x%03x)", mode, mode);
608 printf(" 0x%08x", info.vi_flags);
609 if (info.vi_flags & V_INFO_GRAPHICS) {
611 snprintf(buf, sizeof(buf), "%dx%dx%d %d",
612 info.vi_width, info.vi_height,
613 info.vi_depth, info.vi_planes);
616 snprintf(buf, sizeof(buf), "%dx%d",
617 info.vi_width, info.vi_height);
619 printf(" %c %-15s", c, buf);
620 snprintf(buf, sizeof(buf), "%dx%d",
621 info.vi_cwidth, info.vi_cheight);
622 printf(" %-5s", buf);
623 printf(" 0x%05x %2dk %2dk",
624 info.vi_window, (int)info.vi_window_size/1024,
625 (int)info.vi_window_gran/1024);
626 printf(" 0x%08x %dk\n",
627 info.vi_buffer, (int)info.vi_buffer_size/1024);
634 if (!strcmp(arg, "adapter"))
636 else if (!strcmp(arg, "mode"))
639 warnx("argument to -i must be either adapter or mode");
649 fprintf(stdout, "
\e[=0G\n\n");
650 for (i=0; i<8; i++) {
651 fprintf(stdout, "
\e[=15F
\e[=0G %2d
\e[=%dF%-16s"
652 "
\e[=15F
\e[=0G %2d
\e[=%dF%-16s "
653 "
\e[=15F %2d
\e[=%dGBACKGROUND
\e[=0G\n",
654 i, i, legal_colors[i], i+8, i+8,
655 legal_colors[i+8], i, i);
657 fprintf(stdout, "
\e[=%dF
\e[=%dG
\e[=%dH
\e[=%dI\n",
658 info.mv_norm.fore, info.mv_norm.back,
659 info.mv_rev.fore, info.mv_rev.back);
663 * Snapshot the video memory of that terminal, using the CONS_SCRSHOT
664 * ioctl, and writes the results to stdout either in the special
665 * binary format (see manual page for details), or in the plain
669 dump_screen(int mode)
674 info.size = sizeof(info);
675 if (ioctl(0, CONS_GETINFO, &info) == -1) {
676 warn("failed to obtain current video mode parameters");
680 shot.buf = alloca(info.mv_csz * info.mv_rsz * sizeof(u_int16_t));
681 if (shot.buf == NULL) {
682 warn("failed to allocate memory for dump");
686 shot.xsize = info.mv_csz;
687 shot.ysize = info.mv_rsz;
688 if (ioctl(0, CONS_SCRSHOT, &shot) == -1) {
689 warn("failed to get dump of the screen");
693 if (mode == DUMP_RAW) {
694 printf("SCRSHOT_%c%c%c%c", DUMP_FMT_REV, 2,
695 shot.xsize, shot.ysize);
698 (void)write(STDOUT_FILENO, shot.buf,
699 shot.xsize * shot.ysize * sizeof(u_int16_t));
705 line = alloca(shot.xsize + 1);
707 warn("failed to allocate memory for line buffer");
711 for (y = 0; y < shot.ysize; y++) {
712 for (x = 0; x < shot.xsize; x++) {
713 ch = shot.buf[x + (y * shot.xsize)];
715 if (isprint(ch) == 0)
720 /* Trim trailing spaces */
723 } while (line[x] == ' ' && x != 0);
734 set_history(char *opt)
739 if ((*opt == '\0') || size < 0) {
740 warnx("argument must be a positive number");
743 if (ioctl(0, CONS_HISTORY, &size) == -1)
744 warn("setting history buffer size");
751 if (ioctl(0, CONS_CLRHIST) == -1)
752 warn("clear history buffer");
756 main(int argc, char **argv)
762 info.size = sizeof(info);
766 if (ioctl(0, CONS_GETINFO, &info) < 0)
767 err(1, "must be on a virtual console");
768 while((opt = getopt(argc, argv, "b:Cc:df:g:h:i:l:LM:m:pPr:S:s:t:x")) != -1)
771 set_border_color(optarg);
777 set_cursor_type(optarg);
784 font = nextarg(argc, argv, &optind, 'f', 0);
789 load_font(type, font);
792 if (sscanf(optarg, "%dx%d", &vesa_cols,
794 warnx("incorrect geometry: %s", optarg);
805 load_scrnmap(optarg);
808 load_default_scrnmap();
811 set_mouse_char(optarg);
817 dump_screen(DUMP_RAW);
820 dump_screen(DUMP_TXT);
823 set_reverse_colors(argc, argv, &optind);
826 set_lockswitch(optarg);
832 set_screensaver_timeout(optarg);
840 video_mode(argc, argv, &optind);
841 set_normal_colors(argc, argv, &optind);
842 if (optind < argc && !strcmp(argv[optind], "show")) {
846 if ((optind != argc) || (argc == 1))