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.
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 mkfullname(const char *s1, const char *s2, const char *s3)
96 static char *buf = NULL;
100 f = strlen(s1) + strlen(s2) + strlen(s3) + 1;
103 buf = (char *)realloc(buf, f);
105 buf = (char *)malloc(f);
120 load_scrnmap(char *filename)
126 char *prefix[] = {"", "", SCRNMAP_PATH, SCRNMAP_PATH, NULL};
127 char *postfix[] = {"", ".scm", "", ".scm"};
129 for (i=0; prefix[i]; i++) {
130 name = mkfullname(prefix[i], filename, postfix[i]);
131 fd = fopen(name, "r");
136 warn("screenmap file not found");
139 size = sizeof(scrnmap);
140 if (decode(fd, (char *)&scrnmap, size) != size) {
142 if (fread(&scrnmap, 1, size, fd) != size) {
143 warnx("bad screenmap file");
148 if (ioctl(0, PIO_SCRNMAP, &scrnmap) < 0)
149 warn("can't load screenmap");
154 load_default_scrnmap()
159 for (i=0; i<256; i++)
160 *((char*)&scrnmap + i) = i;
161 if (ioctl(0, PIO_SCRNMAP, &scrnmap) < 0)
162 warn("can't load default screenmap");
168 unsigned char map[256];
171 if (ioctl(0, GIO_SCRNMAP, &map) < 0) {
172 warn("getting screenmap");
175 for (i=0; i<sizeof(map); i++) {
176 if (i > 0 && i % 16 == 0)
177 fprintf(stdout, "\n");
179 fprintf(stdout, " %02x", map[i]);
181 fprintf(stdout, " %03d", map[i]);
183 fprintf(stdout, "\n");
192 if (fstat(fileno(file), &sb) == 0)
198 #define DATASIZE(x) ((x).w * (x).h * 256 / 8)
201 load_font(char *type, char *filename)
205 unsigned long io = 0; /* silence stupid gcc(1) in the Wall mode */
206 char *name, *fontmap;
207 char *prefix[] = {"", "", FONT_PATH, FONT_PATH, NULL};
208 char *postfix[] = {"", ".fnt", "", ".fnt"};
214 } sizes[] = {{8, 16, PIO_FONT8x16},
215 {8, 14, PIO_FONT8x14},
219 for (i=0; prefix[i]; i++) {
220 name = mkfullname(prefix[i], filename, postfix[i]);
221 fd = fopen(name, "r");
226 warn("%s: can't load font file", filename);
231 if (sscanf(type, "%dx%d", &w, &h) == 2)
232 for (i = 0; sizes[i].w != 0; i++)
233 if (sizes[i].w == w && sizes[i].h == h) {
234 size = DATASIZE(sizes[i]);
239 warnx("%s: bad font size specification", type);
244 /* Apply heuristics */
248 size = DATASIZE(sizes[0]);
249 fontmap = (char*) malloc(size);
250 dsize[0] = decode(fd, fontmap, size);
251 dsize[1] = fsize(fd);
255 for (j = 0; j < 2; j++)
256 for (i = 0; sizes[i].w != 0; i++)
257 if (DATASIZE(sizes[i]) == dsize[j]) {
265 warnx("%s: can't guess font size", filename);
272 fontmap = (char*) malloc(size);
273 if (decode(fd, fontmap, size) != size) {
275 if (fsize(fd) != size || fread(fontmap, 1, size, fd) != size) {
276 warnx("%s: bad font file", filename);
282 if (ioctl(0, io, fontmap) < 0)
283 warn("can't load font");
289 set_screensaver_timeout(char *arg)
293 if (!strcmp(arg, "off"))
297 if ((*arg == '\0') || (nsec < 1)) {
298 warnx("argument must be a positive number");
302 if (ioctl(0, CONS_BLANKTIME, &nsec) == -1)
303 warn("setting screensaver period");
307 set_cursor_type(char *appearence)
311 if (!strcmp(appearence, "normal"))
313 else if (!strcmp(appearence, "blink"))
315 else if (!strcmp(appearence, "destructive"))
318 warnx("argument to -c must be normal, blink or destructive");
321 ioctl(0, CONS_CURSORTYPE, &type);
325 video_mode(int argc, char **argv, int *index)
331 { "80x25", SW_TEXT_80x25 },
332 { "80x30", SW_TEXT_80x30 },
333 { "80x43", SW_TEXT_80x43 },
334 { "80x50", SW_TEXT_80x50 },
335 { "80x60", SW_TEXT_80x60 },
336 { "132x25", SW_TEXT_132x25 },
337 { "132x30", SW_TEXT_132x30 },
338 { "132x43", SW_TEXT_132x43 },
339 { "132x50", SW_TEXT_132x50 },
340 { "132x60", SW_TEXT_132x60 },
341 { "VGA_40x25", SW_VGA_C40x25 },
342 { "VGA_80x25", SW_VGA_C80x25 },
343 { "VGA_80x30", SW_VGA_C80x30 },
344 { "VGA_80x50", SW_VGA_C80x50 },
345 { "VGA_80x60", SW_VGA_C80x60 },
347 { "VGA_90x25", SW_VGA_C90x25 },
348 { "VGA_90x30", SW_VGA_C90x30 },
349 { "VGA_90x43", SW_VGA_C90x43 },
350 { "VGA_90x50", SW_VGA_C90x50 },
351 { "VGA_90x60", SW_VGA_C90x60 },
353 { "VGA_320x200", SW_VGA_CG320 },
354 { "EGA_80x25", SW_ENH_C80x25 },
355 { "EGA_80x43", SW_ENH_C80x43 },
356 { "VESA_132x25", SW_VESA_C132x25 },
357 { "VESA_132x43", SW_VESA_C132x43 },
358 { "VESA_132x50", SW_VESA_C132x50 },
359 { "VESA_132x60", SW_VESA_C132x60 },
360 { "VESA_800x600", SW_VESA_800x600 },
363 unsigned long mode = 0;
369 if (ioctl(0, CONS_GET, &cur_mode) < 0)
370 err(1, "cannot get the current video mode");
372 for (i = 0; modes[i].name != NULL; ++i) {
373 if (!strcmp(argv[*index], modes[i].name)) {
374 mode = modes[i].mode;
378 if (modes[i].name == NULL)
380 if (ioctl(0, mode, NULL) < 0)
381 warn("cannot set videomode");
382 if (mode == SW_VESA_800x600) {
384 if ((vesa_cols * 8 > 800) || (vesa_cols <= 0)) {
385 warnx("incorrect number of columns: %d",
387 size[0] = _VESA_800x600_DFL_COLS;
392 if ((vesa_rows * _VESA_800x600_DFL_FNSZ > 600) ||
394 warnx("incorrect number of rows: %d",
396 size[1] = _VESA_800x600_DFL_ROWS;
401 size[2] = _VESA_800x600_DFL_FNSZ;
402 if (ioctl(0, KDRASTER, size)) {
404 if (cur_mode >= M_VESA_BASE)
406 _IO('V', cur_mode - M_VESA_BASE),
409 ioctl(0, _IO('S', cur_mode), NULL);
410 warnc(ioerr, "cannot activate raster display");
419 get_color_number(char *color)
424 if (!strcmp(color, legal_colors[i]))
430 set_normal_colors(int argc, char **argv, int *index)
434 if (*index < argc && (color = get_color_number(argv[*index])) != -1) {
436 fprintf(stderr, "
\e[=%dF", color);
438 && (color = get_color_number(argv[*index])) != -1
441 fprintf(stderr, "
\e[=%dG", color);
447 set_reverse_colors(int argc, char **argv, int *index)
451 if ((color = get_color_number(argv[*(index)-1])) != -1) {
452 fprintf(stderr, "
\e[=%dH", color);
454 && (color = get_color_number(argv[*index])) != -1
457 fprintf(stderr, "
\e[=%dI", color);
463 set_console(char *arg)
467 if( !arg || strspn(arg,"0123456789") != strlen(arg)) {
468 warnx("bad console number");
473 if (n < 1 || n > 16) {
474 warnx("console number out of range");
475 } else if (ioctl(0, VT_ACTIVATE, (caddr_t) (long) n) == -1)
476 warn("ioctl(VT_ACTIVATE)");
480 set_border_color(char *arg)
484 if ((color = get_color_number(arg)) != -1) {
485 fprintf(stderr, "
\e[=%dA", color);
492 set_mouse_char(char *arg)
494 struct mouse_info mouse;
497 l = strtol(arg, NULL, 0);
498 if ((l < 0) || (l > UCHAR_MAX - 3)) {
499 warnx("argument to -M must be 0 through %d", UCHAR_MAX - 3);
502 mouse.operation = MOUSE_MOUSECHAR;
503 mouse.u.mouse_char = (int)l;
504 ioctl(0, CONS_MOUSECTL, &mouse);
510 struct mouse_info mouse;
512 if (!strcmp(arg, "on"))
513 mouse.operation = MOUSE_SHOW;
514 else if (!strcmp(arg, "off"))
515 mouse.operation = MOUSE_HIDE;
517 warnx("argument to -m must either on or off");
520 ioctl(0, CONS_MOUSECTL, &mouse);
524 *adapter_name(int type)
531 { KD_HERCULES, "Hercules" },
535 { KD_PC98, "PC-98xx" },
541 for (i = 0; names[i].type != -1; ++i)
542 if (names[i].type == type)
544 return names[i].name;
548 show_adapter_info(void)
550 struct video_adapter_info ad;
553 if (ioctl(0, CONS_ADPINFO, &ad)) {
554 warn("failed to obtain adapter information");
558 printf("fb%d:\n", ad.va_index);
559 printf(" %.*s%d, type:%s%s (%d), flags:0x%x\n",
560 (int)sizeof(ad.va_name), ad.va_name, ad.va_unit,
561 (ad.va_flags & V_ADP_VESA) ? "VESA " : "",
562 adapter_name(ad.va_type), ad.va_type, ad.va_flags);
563 printf(" initial mode:%d, current mode:%d, BIOS mode:%d\n",
564 ad.va_initial_mode, ad.va_mode, ad.va_initial_bios_mode);
565 printf(" frame buffer window:0x%x, buffer size:0x%x\n",
566 ad.va_window, ad.va_buffer_size);
567 printf(" window size:0x%x, origin:0x%x\n",
568 ad.va_window_size, ad.va_window_orig);
569 printf(" display start address (%d, %d), scan line width:%d\n",
570 ad.va_disp_start.x, ad.va_disp_start.y, ad.va_line_width);
571 printf(" reserved:0x%x\n", ad.va_unused0);
577 struct video_info info;
582 printf(" mode# flags type size "
583 "font window linear buffer\n");
584 printf("---------------------------------------"
585 "---------------------------------------\n");
586 for (mode = 0; mode < M_VESA_MODE_MAX; ++mode) {
588 if (ioctl(0, CONS_MODEINFO, &info))
590 if (info.vi_mode != mode)
593 printf("%3d (0x%03x)", mode, mode);
594 printf(" 0x%08x", info.vi_flags);
595 if (info.vi_flags & V_INFO_GRAPHICS) {
597 snprintf(buf, sizeof(buf), "%dx%dx%d %d",
598 info.vi_width, info.vi_height,
599 info.vi_depth, info.vi_planes);
602 snprintf(buf, sizeof(buf), "%dx%d",
603 info.vi_width, info.vi_height);
605 printf(" %c %-15s", c, buf);
606 snprintf(buf, sizeof(buf), "%dx%d",
607 info.vi_cwidth, info.vi_cheight);
608 printf(" %-5s", buf);
609 printf(" 0x%05x %2dk %2dk",
610 info.vi_window, (int)info.vi_window_size/1024,
611 (int)info.vi_window_gran/1024);
612 printf(" 0x%08x %dk\n",
613 info.vi_buffer, (int)info.vi_buffer_size/1024);
620 if (!strcmp(arg, "adapter"))
622 else if (!strcmp(arg, "mode"))
625 warnx("argument to -i must either adapter or mode");
635 fprintf(stdout, "
\e[=0G\n\n");
636 for (i=0; i<8; i++) {
637 fprintf(stdout, "
\e[=15F
\e[=0G %2d
\e[=%dF%-16s"
638 "
\e[=15F
\e[=0G %2d
\e[=%dF%-16s "
639 "
\e[=15F %2d
\e[=%dGBACKGROUND
\e[=0G\n",
640 i, i, legal_colors[i], i+8, i+8,
641 legal_colors[i+8], i, i);
643 fprintf(stdout, "
\e[=%dF
\e[=%dG
\e[=%dH
\e[=%dI\n",
644 info.mv_norm.fore, info.mv_norm.back,
645 info.mv_rev.fore, info.mv_rev.back);
649 * Snapshot the video memory of that terminal, using the CONS_SCRSHOT
650 * ioctl, and writes the results to stdout either in the special
651 * binary format (see manual page for details), or in the plain
655 dump_screen(int mode)
660 info.size = sizeof(info);
661 if (ioctl(0, CONS_GETINFO, &info) == -1) {
662 warn("failed to obtain current video mode parameters");
666 shot.buf = alloca(info.mv_csz * info.mv_rsz * sizeof(u_int16_t));
667 if (shot.buf == NULL) {
668 warn("failed to allocate memory for dump");
672 shot.xsize = info.mv_csz;
673 shot.ysize = info.mv_rsz;
674 if (ioctl(0, CONS_SCRSHOT, &shot) == -1) {
675 warn("failed to get dump of the screen");
679 if (mode == DUMP_RAW) {
680 printf("SCRSHOT_%c%c%c%c", DUMP_FMT_REV, 2,
681 shot.xsize, shot.ysize);
684 (void)write(STDOUT_FILENO, shot.buf,
685 shot.xsize * shot.ysize * sizeof(u_int16_t));
691 line = alloca(shot.xsize + 1);
693 warn("failed to allocate memory for line buffer");
697 for (y = 0; y < shot.ysize; y++) {
698 for (x = 0; x < shot.xsize; x++) {
699 ch = shot.buf[x + (y * shot.xsize)];
701 if (isprint(ch) == 0)
706 /* Trim trailing spaces */
709 } while (line[x] == ' ' && x != 0);
720 set_history(char *opt)
725 if ((*opt == '\0') || size < 0) {
726 warnx("argument must be a positive number");
729 if (ioctl(0, CONS_HISTORY, &size) == -1)
730 warn("setting history buffer size");
737 if (ioctl(0, CONS_CLRHIST) == -1)
738 warn("clear history buffer");
742 main(int argc, char **argv)
748 info.size = sizeof(info);
752 if (ioctl(0, CONS_GETINFO, &info) < 0)
753 err(1, "must be on a virtual console");
754 while((opt = getopt(argc, argv, "b:Cc:df:g:h:i:l:LM:m:pPr:s:t:x")) != -1)
757 set_border_color(optarg);
763 set_cursor_type(optarg);
770 font = nextarg(argc, argv, &optind, 'f', 0);
775 load_font(type, font);
778 if (sscanf(optarg, "%dx%d", &vesa_cols,
780 warnx("incorrect geometry: %s", optarg);
791 load_scrnmap(optarg);
794 load_default_scrnmap();
797 set_mouse_char(optarg);
803 dump_screen(DUMP_RAW);
806 dump_screen(DUMP_TXT);
809 set_reverse_colors(argc, argv, &optind);
815 set_screensaver_timeout(optarg);
823 video_mode(argc, argv, &optind);
824 set_normal_colors(argc, argv, &optind);
825 if (optind < argc && !strcmp(argv[optind], "show")) {
829 if ((optind != argc) || (argc == 1))