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.3 2004/03/30 02:30:59 cpressey Exp $
32 #include <machine/console.h>
33 #include <sys/consio.h>
34 #include <sys/errno.h>
35 #include <sys/types.h>
49 #define _VESA_800x600_DFL_COLS 80
50 #define _VESA_800x600_DFL_ROWS 25
51 #define _VESA_800x600_DFL_FNSZ 16
56 #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"
66 int vesa_cols = _VESA_800x600_DFL_COLS;
67 int vesa_rows = _VESA_800x600_DFL_ROWS;
75 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
76 "usage: vidcontrol [-CdLPpx] [-b color] [-c appearance] [-f [size] file]",
77 " [-g geometry] [-h size] [-i adapter | mode] [-l screen_map]",
78 " [-M char] [-m on | off] [-r foreground background]",
79 " [-S on | off] [-s number] [-t N | off] [mode]",
80 " [foreground [background]] [show]");
85 nextarg(int ac, char **av, int *indp, int oc, int strict)
88 return(av[(*indp)++]);
90 errx(1, "option requires two arguments -- %c", oc);
95 openguess(char *a[], char *b[], char *c[], char *d[], char **name)
100 for (i = 0; a[i] != NULL; i++) {
101 for (j = 0; b[j] != NULL; j++) {
102 for (k = 0; c[k] != NULL; k++) {
103 for (l = 0; d[l] != NULL; l++) {
104 asprintf(name, "%s%s%s%s", a[i], b[j],
106 f = fopen(*name, "r");
118 load_scrnmap(char *filename)
124 char *a[] = {"", SCRNMAP_PATH, NULL};
125 char *b[] = {filename, NULL};
126 char *c[] = {"", ".scm", NULL};
127 char *d[] = {"", NULL};
129 fd = openguess(a, b, c, d, &name);
131 warn("screenmap file not found");
134 size = sizeof(scrnmap);
135 if (decode(fd, (char *)&scrnmap, size) != size) {
137 if (fread(&scrnmap, 1, size, fd) != size) {
138 warnx("bad screenmap file");
143 if (ioctl(0, PIO_SCRNMAP, &scrnmap) < 0)
144 warn("can't load screenmap");
149 load_default_scrnmap(void)
154 for (i=0; i<256; i++)
155 *((char*)&scrnmap + i) = i;
156 if (ioctl(0, PIO_SCRNMAP, &scrnmap) < 0)
157 warn("can't load default screenmap");
163 unsigned char map[256];
166 if (ioctl(0, GIO_SCRNMAP, &map) < 0) {
167 warn("getting screenmap");
170 for (i=0; i<sizeof(map); i++) {
171 if (i > 0 && i % 16 == 0)
172 fprintf(stdout, "\n");
174 fprintf(stdout, " %02x", map[i]);
176 fprintf(stdout, " %03d", map[i]);
178 fprintf(stdout, "\n");
187 if (fstat(fileno(file), &sb) == 0)
193 #define DATASIZE(x) ((x).w * (x).h * 256 / 8)
196 load_font(char *type, char *filename)
200 unsigned long io = 0; /* silence stupid gcc(1) in the Wall mode */
201 char *name, *fontmap, size_sufx[6];
202 char *a[] = {"", FONT_PATH, NULL};
203 char *b[] = {filename, NULL};
204 char *c[] = {"", size_sufx, NULL};
205 char *d[] = {"", ".fnt", NULL};
212 } sizes[] = {{8, 16, PIO_FONT8x16},
213 {8, 14, PIO_FONT8x14},
217 info.size = sizeof(info);
218 if (ioctl(0, CONS_GETINFO, &info) == -1) {
219 warn("failed to obtain current video mode parameters");
222 snprintf(size_sufx, sizeof(size_sufx), "-8x%d", info.font_size);
223 fd = openguess(a, b, c, d, &name);
225 warn("%s: can't load font file", filename);
230 if (sscanf(type, "%dx%d", &w, &h) == 2)
231 for (i = 0; sizes[i].w != 0; i++)
232 if (sizes[i].w == w && sizes[i].h == h) {
233 size = DATASIZE(sizes[i]);
238 warnx("%s: bad font size specification", type);
243 /* Apply heuristics */
247 size = DATASIZE(sizes[0]);
248 fontmap = (char*) malloc(size);
249 dsize[0] = decode(fd, fontmap, size);
250 dsize[1] = fsize(fd);
254 for (j = 0; j < 2; j++)
255 for (i = 0; sizes[i].w != 0; i++)
256 if (DATASIZE(sizes[i]) == dsize[j]) {
264 warnx("%s: can't guess font size", filename);
271 fontmap = (char*) malloc(size);
272 if (decode(fd, fontmap, size) != size) {
274 if (fsize(fd) != size || fread(fontmap, 1, size, fd) != size) {
275 warnx("%s: bad font file", filename);
281 if (ioctl(0, io, fontmap) < 0)
282 warn("can't load font");
288 set_screensaver_timeout(char *arg)
292 if (!strcmp(arg, "off"))
296 if ((*arg == '\0') || (nsec < 1)) {
297 warnx("argument must be a positive number");
301 if (ioctl(0, CONS_BLANKTIME, &nsec) == -1)
302 warn("setting screensaver period");
306 set_cursor_type(char *appearence)
310 if (!strcmp(appearence, "normal"))
312 else if (!strcmp(appearence, "blink"))
314 else if (!strcmp(appearence, "destructive"))
317 warnx("argument to -c must be normal, blink or destructive");
320 ioctl(0, CONS_CURSORTYPE, &type);
324 video_mode(int argc, char **argv, int *index)
330 { "80x25", SW_TEXT_80x25 },
331 { "80x30", SW_TEXT_80x30 },
332 { "80x43", SW_TEXT_80x43 },
333 { "80x50", SW_TEXT_80x50 },
334 { "80x60", SW_TEXT_80x60 },
335 { "132x25", SW_TEXT_132x25 },
336 { "132x30", SW_TEXT_132x30 },
337 { "132x43", SW_TEXT_132x43 },
338 { "132x50", SW_TEXT_132x50 },
339 { "132x60", SW_TEXT_132x60 },
340 { "VGA_40x25", SW_VGA_C40x25 },
341 { "VGA_80x25", SW_VGA_C80x25 },
342 { "VGA_80x30", SW_VGA_C80x30 },
343 { "VGA_80x50", SW_VGA_C80x50 },
344 { "VGA_80x60", SW_VGA_C80x60 },
346 { "VGA_90x25", SW_VGA_C90x25 },
347 { "VGA_90x30", SW_VGA_C90x30 },
348 { "VGA_90x43", SW_VGA_C90x43 },
349 { "VGA_90x50", SW_VGA_C90x50 },
350 { "VGA_90x60", SW_VGA_C90x60 },
352 { "VGA_320x200", SW_VGA_CG320 },
353 { "EGA_80x25", SW_ENH_C80x25 },
354 { "EGA_80x43", SW_ENH_C80x43 },
355 { "VESA_132x25", SW_VESA_C132x25 },
356 { "VESA_132x43", SW_VESA_C132x43 },
357 { "VESA_132x50", SW_VESA_C132x50 },
358 { "VESA_132x60", SW_VESA_C132x60 },
359 { "VESA_800x600", SW_VESA_800x600 },
362 unsigned long mode = 0;
368 if (ioctl(0, CONS_GET, &cur_mode) < 0)
369 err(1, "cannot get the current video mode");
371 for (i = 0; modes[i].name != NULL; ++i) {
372 if (!strcmp(argv[*index], modes[i].name)) {
373 mode = modes[i].mode;
377 if (modes[i].name == NULL)
379 if (ioctl(0, mode, NULL) < 0)
380 warn("cannot set videomode");
381 if (mode == SW_VESA_800x600) {
383 if ((vesa_cols * 8 > 800) || (vesa_cols <= 0)) {
384 warnx("incorrect number of columns: %d",
386 size[0] = _VESA_800x600_DFL_COLS;
391 if ((vesa_rows * _VESA_800x600_DFL_FNSZ > 600) ||
393 warnx("incorrect number of rows: %d",
395 size[1] = _VESA_800x600_DFL_ROWS;
400 size[2] = _VESA_800x600_DFL_FNSZ;
401 if (ioctl(0, KDRASTER, size)) {
403 if (cur_mode >= M_VESA_BASE)
405 _IO('V', cur_mode - M_VESA_BASE),
408 ioctl(0, _IO('S', cur_mode), NULL);
409 warnc(ioerr, "cannot activate raster display");
418 get_color_number(char *color)
423 if (!strcmp(color, legal_colors[i]))
429 set_normal_colors(int argc, char **argv, int *index)
433 if (*index < argc && (color = get_color_number(argv[*index])) != -1) {
435 fprintf(stderr, "
\e[=%dF", color);
437 && (color = get_color_number(argv[*index])) != -1
440 fprintf(stderr, "
\e[=%dG", color);
446 set_reverse_colors(int argc, char **argv, int *index)
450 if ((color = get_color_number(argv[*(index)-1])) != -1) {
451 fprintf(stderr, "
\e[=%dH", color);
453 && (color = get_color_number(argv[*index])) != -1
456 fprintf(stderr, "
\e[=%dI", color);
462 set_console(char *arg)
466 if( !arg || strspn(arg,"0123456789") != strlen(arg)) {
467 warnx("bad console number");
472 if (n < 1 || n > 16) {
473 warnx("console number out of range");
474 } else if (ioctl(0, VT_ACTIVATE, (caddr_t) (long) n) == -1)
475 warn("ioctl(VT_ACTIVATE)");
479 set_border_color(char *arg)
483 if ((color = get_color_number(arg)) != -1) {
484 fprintf(stderr, "
\e[=%dA", color);
491 set_mouse_char(char *arg)
493 struct mouse_info mouse;
496 l = strtol(arg, NULL, 0);
497 if ((l < 0) || (l > UCHAR_MAX)) {
498 warnx("argument to -M must be 0 through %d", UCHAR_MAX);
501 mouse.operation = MOUSE_MOUSECHAR;
502 mouse.u.mouse_char = (int)l;
503 ioctl(0, CONS_MOUSECTL, &mouse);
509 struct mouse_info mouse;
511 if (!strcmp(arg, "on"))
512 mouse.operation = MOUSE_SHOW;
513 else if (!strcmp(arg, "off"))
514 mouse.operation = MOUSE_HIDE;
516 warnx("argument to -m must be either on or off");
519 ioctl(0, CONS_MOUSECTL, &mouse);
523 set_lockswitch(char *arg)
527 if (!strcmp(arg, "off"))
529 else if (!strcmp(arg, "on"))
532 warnx("argument to -S must be either on or off");
535 if (ioctl(0, VT_LOCKSWITCH, &data) == -1)
536 warn("ioctl(VT_LOCKSWITCH)");
540 *adapter_name(int type)
547 { KD_HERCULES, "Hercules" },
551 { KD_PC98, "PC-98xx" },
557 for (i = 0; names[i].type != -1; ++i)
558 if (names[i].type == type)
560 return names[i].name;
564 show_adapter_info(void)
566 struct video_adapter_info ad;
569 if (ioctl(0, CONS_ADPINFO, &ad)) {
570 warn("failed to obtain adapter information");
574 printf("fb%d:\n", ad.va_index);
575 printf(" %.*s%d, type:%s%s (%d), flags:0x%x\n",
576 (int)sizeof(ad.va_name), ad.va_name, ad.va_unit,
577 (ad.va_flags & V_ADP_VESA) ? "VESA " : "",
578 adapter_name(ad.va_type), ad.va_type, ad.va_flags);
579 printf(" initial mode:%d, current mode:%d, BIOS mode:%d\n",
580 ad.va_initial_mode, ad.va_mode, ad.va_initial_bios_mode);
581 printf(" frame buffer window:0x%x, buffer size:0x%x\n",
582 ad.va_window, ad.va_buffer_size);
583 printf(" window size:0x%x, origin:0x%x\n",
584 ad.va_window_size, ad.va_window_orig);
585 printf(" display start address (%d, %d), scan line width:%d\n",
586 ad.va_disp_start.x, ad.va_disp_start.y, ad.va_line_width);
587 printf(" reserved:0x%x\n", ad.va_unused0);
593 struct video_info info;
598 printf(" mode# flags type size "
599 "font window linear buffer\n");
600 printf("---------------------------------------"
601 "---------------------------------------\n");
602 for (mode = 0; mode < M_VESA_MODE_MAX; ++mode) {
604 if (ioctl(0, CONS_MODEINFO, &info))
606 if (info.vi_mode != mode)
609 printf("%3d (0x%03x)", mode, mode);
610 printf(" 0x%08x", info.vi_flags);
611 if (info.vi_flags & V_INFO_GRAPHICS) {
613 snprintf(buf, sizeof(buf), "%dx%dx%d %d",
614 info.vi_width, info.vi_height,
615 info.vi_depth, info.vi_planes);
618 snprintf(buf, sizeof(buf), "%dx%d",
619 info.vi_width, info.vi_height);
621 printf(" %c %-15s", c, buf);
622 snprintf(buf, sizeof(buf), "%dx%d",
623 info.vi_cwidth, info.vi_cheight);
624 printf(" %-5s", buf);
625 printf(" 0x%05x %2dk %2dk",
626 info.vi_window, (int)info.vi_window_size/1024,
627 (int)info.vi_window_gran/1024);
628 printf(" 0x%08x %dk\n",
629 info.vi_buffer, (int)info.vi_buffer_size/1024);
636 if (!strcmp(arg, "adapter"))
638 else if (!strcmp(arg, "mode"))
641 warnx("argument to -i must be either adapter or mode");
651 fprintf(stdout, "
\e[=0G\n\n");
652 for (i=0; i<8; i++) {
653 fprintf(stdout, "
\e[=15F
\e[=0G %2d
\e[=%dF%-16s"
654 "
\e[=15F
\e[=0G %2d
\e[=%dF%-16s "
655 "
\e[=15F %2d
\e[=%dGBACKGROUND
\e[=0G\n",
656 i, i, legal_colors[i], i+8, i+8,
657 legal_colors[i+8], i, i);
659 fprintf(stdout, "
\e[=%dF
\e[=%dG
\e[=%dH
\e[=%dI\n",
660 info.mv_norm.fore, info.mv_norm.back,
661 info.mv_rev.fore, info.mv_rev.back);
665 * Snapshot the video memory of that terminal, using the CONS_SCRSHOT
666 * ioctl, and writes the results to stdout either in the special
667 * binary format (see manual page for details), or in the plain
671 dump_screen(int mode)
676 info.size = sizeof(info);
677 if (ioctl(0, CONS_GETINFO, &info) == -1) {
678 warn("failed to obtain current video mode parameters");
682 shot.buf = alloca(info.mv_csz * info.mv_rsz * sizeof(u_int16_t));
683 if (shot.buf == NULL) {
684 warn("failed to allocate memory for dump");
688 shot.xsize = info.mv_csz;
689 shot.ysize = info.mv_rsz;
690 if (ioctl(0, CONS_SCRSHOT, &shot) == -1) {
691 warn("failed to get dump of the screen");
695 if (mode == DUMP_RAW) {
696 printf("SCRSHOT_%c%c%c%c", DUMP_FMT_REV, 2,
697 shot.xsize, shot.ysize);
700 (void)write(STDOUT_FILENO, shot.buf,
701 shot.xsize * shot.ysize * sizeof(u_int16_t));
707 line = alloca(shot.xsize + 1);
709 warn("failed to allocate memory for line buffer");
713 for (y = 0; y < shot.ysize; y++) {
714 for (x = 0; x < shot.xsize; x++) {
715 ch = shot.buf[x + (y * shot.xsize)];
717 if (isprint(ch) == 0)
722 /* Trim trailing spaces */
725 } while (line[x] == ' ' && x != 0);
736 set_history(char *opt)
741 if ((*opt == '\0') || size < 0) {
742 warnx("argument must be a positive number");
745 if (ioctl(0, CONS_HISTORY, &size) == -1)
746 warn("setting history buffer size");
752 if (ioctl(0, CONS_CLRHIST) == -1)
753 warn("clear history buffer");
757 main(int argc, char **argv)
763 info.size = sizeof(info);
767 if (ioctl(0, CONS_GETINFO, &info) < 0)
768 err(1, "must be on a virtual console");
769 while((opt = getopt(argc, argv, "b:Cc:df:g:h:i:l:LM:m:pPr:S:s:t:x")) != -1)
772 set_border_color(optarg);
778 set_cursor_type(optarg);
785 font = nextarg(argc, argv, &optind, 'f', 0);
790 load_font(type, font);
793 if (sscanf(optarg, "%dx%d", &vesa_cols,
795 warnx("incorrect geometry: %s", optarg);
806 load_scrnmap(optarg);
809 load_default_scrnmap();
812 set_mouse_char(optarg);
818 dump_screen(DUMP_RAW);
821 dump_screen(DUMP_TXT);
824 set_reverse_colors(argc, argv, &optind);
827 set_lockswitch(optarg);
833 set_screensaver_timeout(optarg);
841 video_mode(argc, argv, &optind);
842 set_normal_colors(argc, argv, &optind);
843 if (optind < argc && !strcmp(argv[optind], "show")) {
847 if ((optind != argc) || (argc == 1))