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.
30 static const char rcsid[] =
42 #include <sys/consio.h>
43 #include <sys/errno.h>
44 #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",
76 "usage: vidcontrol [-CdLPpx] [-b color] [-c appearance] [-f [size] file]",
77 " [-g geometry] [-h size] [-i adapter | mode] [-l screen_map]",
78 " [-m on | off] [-M char] [-r foreground background] [-s num]",
79 " [-t N | off] [mode] [foreground [background]] [show]");
84 nextarg(int ac, char **av, int *indp, int oc, int strict)
87 return(av[(*indp)++]);
89 errx(1, "option requires two arguments -- %c", oc);
94 openguess(char *a[], char *b[], char *c[], char *d[], char **name)
99 for (i = 0; a[i] != NULL; i++) {
100 for (j = 0; b[j] != NULL; j++) {
101 for (k = 0; c[k] != NULL; k++) {
102 for (l = 0; d[l] != NULL; l++) {
103 asprintf(name, "%s%s%s%s", a[i], b[j],
105 f = fopen(*name, "r");
117 load_scrnmap(char *filename)
123 char *a[] = {"", SCRNMAP_PATH, NULL};
124 char *b[] = {filename, NULL};
125 char *c[] = {"", ".scm", NULL};
126 char *d[] = {"", NULL};
128 fd = openguess(a, b, c, d, &name);
130 warn("screenmap file not found");
133 size = sizeof(scrnmap);
134 if (decode(fd, (char *)&scrnmap, size) != size) {
136 if (fread(&scrnmap, 1, size, fd) != size) {
137 warnx("bad screenmap file");
142 if (ioctl(0, PIO_SCRNMAP, &scrnmap) < 0)
143 warn("can't load screenmap");
148 load_default_scrnmap()
153 for (i=0; i<256; i++)
154 *((char*)&scrnmap + i) = i;
155 if (ioctl(0, PIO_SCRNMAP, &scrnmap) < 0)
156 warn("can't load default screenmap");
162 unsigned char map[256];
165 if (ioctl(0, GIO_SCRNMAP, &map) < 0) {
166 warn("getting screenmap");
169 for (i=0; i<sizeof(map); i++) {
170 if (i > 0 && i % 16 == 0)
171 fprintf(stdout, "\n");
173 fprintf(stdout, " %02x", map[i]);
175 fprintf(stdout, " %03d", map[i]);
177 fprintf(stdout, "\n");
186 if (fstat(fileno(file), &sb) == 0)
192 #define DATASIZE(x) ((x).w * (x).h * 256 / 8)
195 load_font(char *type, char *filename)
199 unsigned long io = 0; /* silence stupid gcc(1) in the Wall mode */
200 char *name, *fontmap, size_sufx[6];
201 char *a[] = {"", FONT_PATH, NULL};
202 char *b[] = {filename, NULL};
203 char *c[] = {"", size_sufx, NULL};
204 char *d[] = {"", ".fnt", NULL};
211 } sizes[] = {{8, 16, PIO_FONT8x16},
212 {8, 14, PIO_FONT8x14},
216 info.size = sizeof(info);
217 if (ioctl(0, CONS_GETINFO, &info) == -1) {
218 warn("failed to obtain current video mode parameters");
221 snprintf(size_sufx, sizeof(size_sufx), "-8x%d", info.font_size);
222 fd = openguess(a, b, c, d, &name);
224 warn("%s: can't load font file", filename);
229 if (sscanf(type, "%dx%d", &w, &h) == 2)
230 for (i = 0; sizes[i].w != 0; i++)
231 if (sizes[i].w == w && sizes[i].h == h) {
232 size = DATASIZE(sizes[i]);
237 warnx("%s: bad font size specification", type);
242 /* Apply heuristics */
246 size = DATASIZE(sizes[0]);
247 fontmap = (char*) malloc(size);
248 dsize[0] = decode(fd, fontmap, size);
249 dsize[1] = fsize(fd);
253 for (j = 0; j < 2; j++)
254 for (i = 0; sizes[i].w != 0; i++)
255 if (DATASIZE(sizes[i]) == dsize[j]) {
263 warnx("%s: can't guess font size", filename);
270 fontmap = (char*) malloc(size);
271 if (decode(fd, fontmap, size) != size) {
273 if (fsize(fd) != size || fread(fontmap, 1, size, fd) != size) {
274 warnx("%s: bad font file", filename);
280 if (ioctl(0, io, fontmap) < 0)
281 warn("can't load font");
287 set_screensaver_timeout(char *arg)
291 if (!strcmp(arg, "off"))
295 if ((*arg == '\0') || (nsec < 1)) {
296 warnx("argument must be a positive number");
300 if (ioctl(0, CONS_BLANKTIME, &nsec) == -1)
301 warn("setting screensaver period");
305 set_cursor_type(char *appearence)
309 if (!strcmp(appearence, "normal"))
311 else if (!strcmp(appearence, "blink"))
313 else if (!strcmp(appearence, "destructive"))
316 warnx("argument to -c must be normal, blink or destructive");
319 ioctl(0, CONS_CURSORTYPE, &type);
323 video_mode(int argc, char **argv, int *index)
329 { "80x25", SW_TEXT_80x25 },
330 { "80x30", SW_TEXT_80x30 },
331 { "80x43", SW_TEXT_80x43 },
332 { "80x50", SW_TEXT_80x50 },
333 { "80x60", SW_TEXT_80x60 },
334 { "132x25", SW_TEXT_132x25 },
335 { "132x30", SW_TEXT_132x30 },
336 { "132x43", SW_TEXT_132x43 },
337 { "132x50", SW_TEXT_132x50 },
338 { "132x60", SW_TEXT_132x60 },
339 { "VGA_40x25", SW_VGA_C40x25 },
340 { "VGA_80x25", SW_VGA_C80x25 },
341 { "VGA_80x30", SW_VGA_C80x30 },
342 { "VGA_80x50", SW_VGA_C80x50 },
343 { "VGA_80x60", SW_VGA_C80x60 },
345 { "VGA_90x25", SW_VGA_C90x25 },
346 { "VGA_90x30", SW_VGA_C90x30 },
347 { "VGA_90x43", SW_VGA_C90x43 },
348 { "VGA_90x50", SW_VGA_C90x50 },
349 { "VGA_90x60", SW_VGA_C90x60 },
351 { "VGA_320x200", SW_VGA_CG320 },
352 { "EGA_80x25", SW_ENH_C80x25 },
353 { "EGA_80x43", SW_ENH_C80x43 },
354 { "VESA_132x25", SW_VESA_C132x25 },
355 { "VESA_132x43", SW_VESA_C132x43 },
356 { "VESA_132x50", SW_VESA_C132x50 },
357 { "VESA_132x60", SW_VESA_C132x60 },
358 { "VESA_800x600", SW_VESA_800x600 },
361 unsigned long mode = 0;
367 if (ioctl(0, CONS_GET, &cur_mode) < 0)
368 err(1, "cannot get the current video mode");
370 for (i = 0; modes[i].name != NULL; ++i) {
371 if (!strcmp(argv[*index], modes[i].name)) {
372 mode = modes[i].mode;
376 if (modes[i].name == NULL)
378 if (ioctl(0, mode, NULL) < 0)
379 warn("cannot set videomode");
380 if (mode == SW_VESA_800x600) {
382 if ((vesa_cols * 8 > 800) || (vesa_cols <= 0)) {
383 warnx("incorrect number of columns: %d",
385 size[0] = _VESA_800x600_DFL_COLS;
390 if ((vesa_rows * _VESA_800x600_DFL_FNSZ > 600) ||
392 warnx("incorrect number of rows: %d",
394 size[1] = _VESA_800x600_DFL_ROWS;
399 size[2] = _VESA_800x600_DFL_FNSZ;
400 if (ioctl(0, KDRASTER, size)) {
402 if (cur_mode >= M_VESA_BASE)
404 _IO('V', cur_mode - M_VESA_BASE),
407 ioctl(0, _IO('S', cur_mode), NULL);
408 warnc(ioerr, "cannot activate raster display");
417 get_color_number(char *color)
422 if (!strcmp(color, legal_colors[i]))
428 set_normal_colors(int argc, char **argv, int *index)
432 if (*index < argc && (color = get_color_number(argv[*index])) != -1) {
434 fprintf(stderr, "
\e[=%dF", color);
436 && (color = get_color_number(argv[*index])) != -1
439 fprintf(stderr, "
\e[=%dG", color);
445 set_reverse_colors(int argc, char **argv, int *index)
449 if ((color = get_color_number(argv[*(index)-1])) != -1) {
450 fprintf(stderr, "
\e[=%dH", color);
452 && (color = get_color_number(argv[*index])) != -1
455 fprintf(stderr, "
\e[=%dI", color);
461 set_console(char *arg)
465 if( !arg || strspn(arg,"0123456789") != strlen(arg)) {
466 warnx("bad console number");
471 if (n < 1 || n > 16) {
472 warnx("console number out of range");
473 } else if (ioctl(0, VT_ACTIVATE, (caddr_t) (long) n) == -1)
474 warn("ioctl(VT_ACTIVATE)");
478 set_border_color(char *arg)
482 if ((color = get_color_number(arg)) != -1) {
483 fprintf(stderr, "
\e[=%dA", color);
490 set_mouse_char(char *arg)
492 struct mouse_info mouse;
495 l = strtol(arg, NULL, 0);
496 if ((l < 0) || (l > UCHAR_MAX - 3)) {
497 warnx("argument to -M must be 0 through %d", UCHAR_MAX - 3);
500 mouse.operation = MOUSE_MOUSECHAR;
501 mouse.u.mouse_char = (int)l;
502 ioctl(0, CONS_MOUSECTL, &mouse);
508 struct mouse_info mouse;
510 if (!strcmp(arg, "on"))
511 mouse.operation = MOUSE_SHOW;
512 else if (!strcmp(arg, "off"))
513 mouse.operation = MOUSE_HIDE;
515 warnx("argument to -m must either on or off");
518 ioctl(0, CONS_MOUSECTL, &mouse);
522 *adapter_name(int type)
529 { KD_HERCULES, "Hercules" },
533 { KD_PC98, "PC-98xx" },
539 for (i = 0; names[i].type != -1; ++i)
540 if (names[i].type == type)
542 return names[i].name;
546 show_adapter_info(void)
548 struct video_adapter_info ad;
551 if (ioctl(0, CONS_ADPINFO, &ad)) {
552 warn("failed to obtain adapter information");
556 printf("fb%d:\n", ad.va_index);
557 printf(" %.*s%d, type:%s%s (%d), flags:0x%x\n",
558 (int)sizeof(ad.va_name), ad.va_name, ad.va_unit,
559 (ad.va_flags & V_ADP_VESA) ? "VESA " : "",
560 adapter_name(ad.va_type), ad.va_type, ad.va_flags);
561 printf(" initial mode:%d, current mode:%d, BIOS mode:%d\n",
562 ad.va_initial_mode, ad.va_mode, ad.va_initial_bios_mode);
563 printf(" frame buffer window:0x%x, buffer size:0x%x\n",
564 ad.va_window, ad.va_buffer_size);
565 printf(" window size:0x%x, origin:0x%x\n",
566 ad.va_window_size, ad.va_window_orig);
567 printf(" display start address (%d, %d), scan line width:%d\n",
568 ad.va_disp_start.x, ad.va_disp_start.y, ad.va_line_width);
569 printf(" reserved:0x%x\n", ad.va_unused0);
575 struct video_info info;
580 printf(" mode# flags type size "
581 "font window linear buffer\n");
582 printf("---------------------------------------"
583 "---------------------------------------\n");
584 for (mode = 0; mode < M_VESA_MODE_MAX; ++mode) {
586 if (ioctl(0, CONS_MODEINFO, &info))
588 if (info.vi_mode != mode)
591 printf("%3d (0x%03x)", mode, mode);
592 printf(" 0x%08x", info.vi_flags);
593 if (info.vi_flags & V_INFO_GRAPHICS) {
595 snprintf(buf, sizeof(buf), "%dx%dx%d %d",
596 info.vi_width, info.vi_height,
597 info.vi_depth, info.vi_planes);
600 snprintf(buf, sizeof(buf), "%dx%d",
601 info.vi_width, info.vi_height);
603 printf(" %c %-15s", c, buf);
604 snprintf(buf, sizeof(buf), "%dx%d",
605 info.vi_cwidth, info.vi_cheight);
606 printf(" %-5s", buf);
607 printf(" 0x%05x %2dk %2dk",
608 info.vi_window, (int)info.vi_window_size/1024,
609 (int)info.vi_window_gran/1024);
610 printf(" 0x%08x %dk\n",
611 info.vi_buffer, (int)info.vi_buffer_size/1024);
618 if (!strcmp(arg, "adapter"))
620 else if (!strcmp(arg, "mode"))
623 warnx("argument to -i must either adapter or mode");
633 fprintf(stdout, "
\e[=0G\n\n");
634 for (i=0; i<8; i++) {
635 fprintf(stdout, "
\e[=15F
\e[=0G %2d
\e[=%dF%-16s"
636 "
\e[=15F
\e[=0G %2d
\e[=%dF%-16s "
637 "
\e[=15F %2d
\e[=%dGBACKGROUND
\e[=0G\n",
638 i, i, legal_colors[i], i+8, i+8,
639 legal_colors[i+8], i, i);
641 fprintf(stdout, "
\e[=%dF
\e[=%dG
\e[=%dH
\e[=%dI\n",
642 info.mv_norm.fore, info.mv_norm.back,
643 info.mv_rev.fore, info.mv_rev.back);
647 * Snapshot the video memory of that terminal, using the CONS_SCRSHOT
648 * ioctl, and writes the results to stdout either in the special
649 * binary format (see manual page for details), or in the plain
653 dump_screen(int mode)
658 info.size = sizeof(info);
659 if (ioctl(0, CONS_GETINFO, &info) == -1) {
660 warn("failed to obtain current video mode parameters");
664 shot.buf = alloca(info.mv_csz * info.mv_rsz * sizeof(u_int16_t));
665 if (shot.buf == NULL) {
666 warn("failed to allocate memory for dump");
670 shot.xsize = info.mv_csz;
671 shot.ysize = info.mv_rsz;
672 if (ioctl(0, CONS_SCRSHOT, &shot) == -1) {
673 warn("failed to get dump of the screen");
677 if (mode == DUMP_RAW) {
678 printf("SCRSHOT_%c%c%c%c", DUMP_FMT_REV, 2,
679 shot.xsize, shot.ysize);
682 (void)write(STDOUT_FILENO, shot.buf,
683 shot.xsize * shot.ysize * sizeof(u_int16_t));
689 line = alloca(shot.xsize + 1);
691 warn("failed to allocate memory for line buffer");
695 for (y = 0; y < shot.ysize; y++) {
696 for (x = 0; x < shot.xsize; x++) {
697 ch = shot.buf[x + (y * shot.xsize)];
699 if (isprint(ch) == 0)
704 /* Trim trailing spaces */
707 } while (line[x] == ' ' && x != 0);
718 set_history(char *opt)
723 if ((*opt == '\0') || size < 0) {
724 warnx("argument must be a positive number");
727 if (ioctl(0, CONS_HISTORY, &size) == -1)
728 warn("setting history buffer size");
735 if (ioctl(0, CONS_CLRHIST) == -1)
736 warn("clear history buffer");
740 main(int argc, char **argv)
746 info.size = sizeof(info);
750 if (ioctl(0, CONS_GETINFO, &info) < 0)
751 err(1, "must be on a virtual console");
752 while((opt = getopt(argc, argv, "b:Cc:df:g:h:i:l:LM:m:pPr:s:t:x")) != -1)
755 set_border_color(optarg);
761 set_cursor_type(optarg);
768 font = nextarg(argc, argv, &optind, 'f', 0);
773 load_font(type, font);
776 if (sscanf(optarg, "%dx%d", &vesa_cols,
778 warnx("incorrect geometry: %s", optarg);
789 load_scrnmap(optarg);
792 load_default_scrnmap();
795 set_mouse_char(optarg);
801 dump_screen(DUMP_RAW);
804 dump_screen(DUMP_TXT);
807 set_reverse_colors(argc, argv, &optind);
813 set_screensaver_timeout(optarg);
821 video_mode(argc, argv, &optind);
822 set_normal_colors(argc, argv, &optind);
823 if (optind < argc && !strcmp(argv[optind], "show")) {
827 if ((optind != argc) || (argc == 1))