1 /*- 2 * Copyright (c) 1994-1996 S�ren Schmidt 3 * All rights reserved. 4 * 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 16 * 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. 27 * 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.12 2007/08/19 11:39:11 swildner Exp $ 30 */ 31 32 #include <machine/console.h> 33 34 #include <sys/consio.h> 35 #include <sys/errno.h> 36 #include <sys/types.h> 37 #include <sys/stat.h> 38 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> 46 47 #include "path.h" 48 #include "decode.h" 49 50 51 #define DATASIZE(x) ((x).w * (x).h * 256 / 8) 52 53 #define DUMP_RAW 0 54 #define DUMP_TXT 1 55 56 #define DUMP_FMT_REV 1 57 58 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" 64 }; 65 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; 73 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; 86 87 88 /* 89 * Initialize revert data. 90 * 91 * NOTE: the following parameters are not yet saved/restored: 92 * 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 100 */ 101 102 static void 103 init(void) 104 { 105 if (ioctl(0, VT_GETACTIVE, &cur_info.active_vty) == -1) 106 errc(1, errno, "getting active vty"); 107 108 cur_info.console_info.size = sizeof(cur_info.console_info); 109 110 if (ioctl(0, CONS_GETINFO, &cur_info.console_info) == -1) 111 errc(1, errno, "getting console information"); 112 113 if (ioctl(0, GIO_SCRNMAP, &cur_info.screen_map) == -1) 114 errc(1, errno, "getting screen map"); 115 116 if (ioctl(0, CONS_GET, &cur_info.video_mode_number) == -1) 117 errc(1, errno, "getting video mode number"); 118 119 cur_info.video_mode_info.vi_mode = cur_info.video_mode_number; 120 121 if (ioctl(0, CONS_MODEINFO, &cur_info.video_mode_info) == -1) 122 errc(1, errno, "getting video mode parameters"); 123 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; 128 } 129 130 131 /* 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). 134 * 135 * NOTE: please also read the comments of init(). 136 */ 137 138 static void 139 revert(void) 140 { 141 int size[3]; 142 143 ioctl(0, VT_ACTIVATE, (caddr_t) (long) cur_info.active_vty); 144 145 fprintf(stderr, "[=%dA", cur_info.console_info.mv_ovscan); 146 fprintf(stderr, "[=%dF", cur_info.console_info.mv_norm.fore); 147 fprintf(stderr, "[=%dG", cur_info.console_info.mv_norm.back); 148 fprintf(stderr, "[=%dH", cur_info.console_info.mv_rev.fore); 149 fprintf(stderr, "[=%dI", cur_info.console_info.mv_rev.back); 150 151 ioctl(0, PIO_SCRNMAP, &cur_info.screen_map); 152 ioctl(0, CONS_SET, &cur_info.video_mode_number); 153 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; 159 160 ioctl(0, KDRASTER, size); 161 } 162 } 163 164 165 /* 166 * Print a short usage string describing all options, then exit. 167 */ 168 169 static void 170 usage(void) 171 { 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"); 182 183 exit(1); 184 } 185 186 187 /* 188 * Retrieve the next argument from the command line (for options that require 189 * more than one argument). 190 */ 191 192 static char * 193 nextarg(int ac, char **av, int *indp, int oc, int strict) 194 { 195 if (*indp < ac) 196 return(av[(*indp)++]); 197 198 if (strict != 0) { 199 revert(); 200 errx(1, "option requires two arguments -- %c", oc); 201 } 202 203 return(NULL); 204 } 205 206 207 /* 208 * Guess which file to open. Try to open each combination of a specified set 209 * of file name components. 210 */ 211 212 static FILE * 213 openguess(const char *a[], const char *b[], const char *c[], const char *d[], char **name) 214 { 215 FILE *f; 216 int i, j, k, l; 217 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]); 224 225 f = fopen(*name, "r"); 226 227 if (f != NULL) 228 return (f); 229 230 free(*name); 231 } 232 } 233 } 234 } 235 236 return (NULL); 237 } 238 239 240 /* 241 * Load a screenmap from a file and set it. 242 */ 243 244 static void 245 load_scrnmap(char *filename) 246 { 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}; 255 256 fd = openguess(a, b, c, d, &name); 257 258 if (fd == NULL) { 259 revert(); 260 errx(1, "screenmap file not found"); 261 } 262 263 size = sizeof(scrnmap); 264 265 if (decode(fd, (char *)&scrnmap, size) != size) { 266 rewind(fd); 267 268 if (fread(&scrnmap, 1, size, fd) != (size_t)size) { 269 fclose(fd); 270 revert(); 271 errx(1, "bad screenmap file"); 272 } 273 } 274 275 if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) { 276 revert(); 277 errc(1, errno, "loading screenmap"); 278 } 279 280 fclose(fd); 281 } 282 283 284 /* 285 * Set the default screenmap. 286 */ 287 288 static void 289 load_default_scrnmap(void) 290 { 291 scrmap_t scrnmap; 292 int i; 293 294 for (i = 0; i < 256; i++) 295 *((char*)&scrnmap + i) = i; 296 297 if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) { 298 revert(); 299 errc(1, errno, "loading default screenmap"); 300 } 301 } 302 303 304 /* 305 * Print the current screenmap to stdout. 306 */ 307 308 static void 309 print_scrnmap(void) 310 { 311 unsigned char map[256]; 312 size_t i; 313 314 if (ioctl(0, GIO_SCRNMAP, &map) == -1) { 315 revert(); 316 errc(1, errno, "getting screenmap"); 317 } 318 319 for (i=0; i<sizeof(map); i++) { 320 if (i > 0 && i % 16 == 0) 321 fprintf(stdout, "\n"); 322 323 if (hex != NULL) 324 fprintf(stdout, " %02x", map[i]); 325 else 326 fprintf(stdout, " %03d", map[i]); 327 } 328 329 fprintf(stdout, "\n"); 330 } 331 332 333 /* 334 * Determine a file's size. 335 */ 336 337 static int 338 fsize(FILE *file) 339 { 340 struct stat sb; 341 342 if (fstat(fileno(file), &sb) == 0) 343 return sb.st_size; 344 else 345 return -1; 346 } 347 348 349 /* 350 * Load a font from file and set it. 351 */ 352 353 static void 354 load_font(char *type, char *filename) 355 { 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; 365 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}}; 374 375 vinfo.size = sizeof(vinfo); 376 377 if (ioctl(0, CONS_GETINFO, &vinfo) == -1) { 378 revert(); 379 errc(1, errno, "obtaining current video mode parameters"); 380 } 381 382 snprintf(size_sufx, sizeof(size_sufx), "-8x%d", vinfo.font_size); 383 384 fd = openguess(a, b, c, d, &name); 385 386 if (fd == NULL) { 387 revert(); 388 errx(1, "%s: can't load font file", filename); 389 } 390 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; 399 } 400 } 401 } 402 if (size == 0) { 403 fclose(fd); 404 revert(); 405 errx(1, "%s: bad font size specification", type); 406 } 407 } else { 408 /* Apply heuristics */ 409 410 int j; 411 int dsize[2]; 412 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); 418 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; 428 } 429 } 430 } 431 432 if (size == 0) { 433 fclose(fd); 434 revert(); 435 errx(1, "%s: can't guess font size", filename); 436 } 437 438 rewind(fd); 439 } 440 441 fontmap = (char*) malloc(size); 442 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); 451 } 452 } 453 454 if (ioctl(0, io, fontmap) == -1) { 455 revert(); 456 errc(1, errno, "loading font"); 457 } 458 459 fclose(fd); 460 free(fontmap); 461 } 462 463 464 /* 465 * Set the timeout for the screensaver. 466 */ 467 468 static void 469 set_screensaver_timeout(char *arg) 470 { 471 int nsec; 472 473 if (!strcmp(arg, "off")) { 474 nsec = 0; 475 } else { 476 nsec = atoi(arg); 477 478 if ((*arg == '\0') || (nsec < 1)) { 479 revert(); 480 errx(1, "argument must be a positive number"); 481 } 482 } 483 484 if (ioctl(0, CONS_BLANKTIME, &nsec) == -1) { 485 revert(); 486 errc(1, errno, "setting screensaver period"); 487 } 488 } 489 490 491 /* 492 * Set the cursor's shape/type. 493 */ 494 495 static void 496 set_cursor_type(char *appearance) 497 { 498 int type; 499 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"); 509 } 510 511 if (ioctl(0, CONS_CURSORTYPE, &type) == -1) { 512 revert(); 513 errc(1, errno, "setting cursor type"); 514 } 515 } 516 517 518 /* 519 * Set the video mode. 520 */ 521 522 static void 523 video_mode(int argc, char **argv, int *mode_index) 524 { 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, NULL }, 558 }; 559 560 int new_mode_num = 0; 561 int size[3]; 562 int i; 563 564 /* 565 * Parse the video mode argument... 566 */ 567 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"); 572 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; 579 } 580 } 581 582 if (modes[i].name == NULL) 583 errx(1, "invalid video mode name"); 584 } 585 586 /* 587 * Collect enough information about the new video mode... 588 */ 589 590 new_mode_info.vi_mode = new_mode_num; 591 592 if (ioctl(0, CONS_MODEINFO, &new_mode_info) == -1) { 593 revert(); 594 errc(1, errno, "obtaining new video mode parameters"); 595 } 596 597 /* 598 * Try setting the new mode. 599 */ 600 601 if (ioctl(0, CONS_SET, &new_mode_num) == -1) { 602 revert(); 603 errc(1, errno, "setting video mode"); 604 } 605 606 /* 607 * For raster modes it's not enough to just set the mode. 608 * We also need to explicitly set the raster mode. 609 */ 610 611 if (new_mode_info.vi_flags & V_INFO_GRAPHICS) { 612 /* font size */ 613 614 if (font_height == 0) 615 font_height = cur_info.console_info.font_size; 616 617 size[2] = font_height; 618 619 /* adjust columns */ 620 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; 626 } 627 628 /* adjust rows */ 629 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; 636 } 637 638 /* set raster mode */ 639 640 if (ioctl(0, KDRASTER, size)) { 641 revert(); 642 errc(1, errno, "activating raster display"); 643 } 644 } 645 646 video_mode_changed = 1; 647 648 (*mode_index)++; 649 } 650 } 651 652 653 /* 654 * Return the number for a specified color name. 655 */ 656 657 static int 658 get_color_number(char *color) 659 { 660 int i; 661 662 for (i=0; i<16; i++) { 663 if (!strcmp(color, legal_colors[i])) 664 return i; 665 } 666 return -1; 667 } 668 669 670 /* 671 * Get normal text and background colors. 672 */ 673 674 static void 675 get_normal_colors(int argc, char **argv, int *color_index) 676 { 677 int color; 678 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; 684 685 if (*color_index < argc && 686 (color = get_color_number(argv[*color_index])) != -1) { 687 (*color_index)++; 688 normal_back_color = color; 689 } 690 } 691 } 692 693 694 /* 695 * Get reverse text and background colors. 696 */ 697 698 static void 699 get_reverse_colors(int argc, char **argv, int *color_index) 700 { 701 int color; 702 703 if ((color = get_color_number(argv[*(color_index)-1])) != -1) { 704 revers_fore_color = color; 705 colors_changed = 1; 706 707 if (*color_index < argc && 708 (color = get_color_number(argv[*color_index])) != -1) { 709 (*color_index)++; 710 revers_back_color = color; 711 } 712 } 713 } 714 715 716 /* 717 * Set normal and reverse foreground and background colors. 718 */ 719 720 static void 721 set_colors(void) 722 { 723 fprintf(stderr, "[=%dF", normal_fore_color); 724 fprintf(stderr, "[=%dG", normal_back_color); 725 fprintf(stderr, "[=%dH", revers_fore_color); 726 fprintf(stderr, "[=%dI", revers_back_color); 727 } 728 729 730 /* 731 * Switch to virtual terminal #arg. 732 */ 733 734 static void 735 set_console(char *arg) 736 { 737 int n; 738 739 if(!arg || strspn(arg,"0123456789") != strlen(arg)) { 740 revert(); 741 errx(1, "bad console number"); 742 } 743 744 n = atoi(arg); 745 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"); 752 } 753 } 754 755 756 /* 757 * Sets the border color. 758 */ 759 760 static void 761 set_border_color(char *arg) 762 { 763 int color; 764 765 if ((color = get_color_number(arg)) != -1) 766 fprintf(stderr, "[=%dA", color); 767 else 768 usage(); 769 } 770 771 772 static void 773 set_mouse_char(char *arg) 774 { 775 struct mouse_info mouse; 776 long l; 777 778 l = strtol(arg, NULL, 0); 779 780 if ((l < 0) || (l > UCHAR_MAX)) { 781 revert(); 782 errx(1, "argument to -M must be 0 through %d", UCHAR_MAX); 783 } 784 785 mouse.operation = MOUSE_MOUSECHAR; 786 mouse.u.mouse_char = (int)l; 787 788 if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) { 789 revert(); 790 errc(1, errno, "setting mouse character"); 791 } 792 } 793 794 795 /* 796 * Show/hide the mouse. 797 */ 798 799 static void 800 set_mouse(char *arg) 801 { 802 struct mouse_info mouse; 803 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"); 811 } 812 813 if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) { 814 revert(); 815 errc(1, errno, "%sing the mouse", 816 mouse.operation == MOUSE_SHOW ? "show" : "hid"); 817 } 818 } 819 820 821 static void 822 set_lockswitch(char *arg) 823 { 824 int data; 825 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"); 833 } 834 835 if (ioctl(0, VT_LOCKSWITCH, &data) == -1) { 836 revert(); 837 errc(1, errno, "turning %s vty switching", 838 data == 0x01 ? "off" : "on"); 839 } 840 } 841 842 843 /* 844 * Return the adapter name for a specified type. 845 */ 846 847 static const char * 848 adapter_name(int type) 849 { 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" }, 860 }; 861 862 int i; 863 864 for (i = 0; names[i].type != -1; ++i) { 865 if (names[i].type == type) 866 break; 867 } 868 869 return names[i].name; 870 } 871 872 873 /* 874 * Show graphics adapter information. 875 */ 876 877 static void 878 show_adapter_info(void) 879 { 880 struct video_adapter_info ad; 881 882 ad.va_index = 0; 883 884 if (ioctl(0, CONS_ADPINFO, &ad) == -1) { 885 revert(); 886 errc(1, errno, "obtaining adapter information"); 887 } 888 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); 903 } 904 905 906 /* 907 * Show video mode information. 908 */ 909 910 static void 911 show_mode_info(void) 912 { 913 struct video_info vinfo; 914 char buf[80]; 915 int mode; 916 int c; 917 918 printf(" mode# flags type size " 919 "font window linear buffer\n"); 920 printf("---------------------------------------" 921 "---------------------------------------\n"); 922 923 for (mode = 0; mode < M_VESA_MODE_MAX; ++mode) { 924 vinfo.vi_mode = mode; 925 926 if (ioctl(0, CONS_MODEINFO, &vinfo)) 927 continue; 928 if (vinfo.vi_mode != mode) 929 continue; 930 931 printf("%3d (0x%03x)", mode, mode); 932 printf(" 0x%08x", vinfo.vi_flags); 933 934 if (vinfo.vi_flags & V_INFO_GRAPHICS) { 935 c = 'G'; 936 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'; 942 943 snprintf(buf, sizeof(buf), "%dx%d", 944 vinfo.vi_width, vinfo.vi_height); 945 } 946 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); 956 } 957 } 958 959 960 static void 961 show_info(char *arg) 962 { 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"); 970 } 971 } 972 973 974 static void 975 test_frame(void) 976 { 977 int i; 978 979 fprintf(stdout, "[=0G\n\n"); 980 981 for (i = 0; i < 8; i++) { 982 fprintf(stdout, "[=15F[=0G %2d [=%dF%-16s" 983 "[=15F[=0G %2d [=%dF%-16s " 984 "[=15F %2d [=%dGBACKGROUND[=0G\n", 985 i, i, legal_colors[i], i+8, i+8, 986 legal_colors[i+8], i, i); 987 } 988 989 fprintf(stdout, "[=%dF[=%dG[=%dH[=%dI\n", 990 info.mv_norm.fore, info.mv_norm.back, 991 info.mv_rev.fore, info.mv_rev.back); 992 } 993 994 995 /* 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. 1000 */ 1001 1002 static void 1003 dump_screen(int mode) 1004 { 1005 scrshot_t shot; 1006 vid_info_t vinfo; 1007 1008 vinfo.size = sizeof(vinfo); 1009 1010 if (ioctl(0, CONS_GETINFO, &vinfo) == -1) { 1011 revert(); 1012 errc(1, errno, "obtaining current video mode parameters"); 1013 } 1014 1015 shot.buf = alloca(vinfo.mv_csz * vinfo.mv_rsz * sizeof(u_int16_t)); 1016 1017 if (shot.buf == NULL) { 1018 revert(); 1019 errx(1, "failed to allocate memory for dump"); 1020 } 1021 1022 shot.xsize = vinfo.mv_csz; 1023 shot.ysize = vinfo.mv_rsz; 1024 1025 if (ioctl(0, CONS_SCRSHOT, &shot) == -1) { 1026 revert(); 1027 errc(1, errno, "dumping screen"); 1028 } 1029 1030 if (mode == DUMP_RAW) { 1031 printf("SCRSHOT_%c%c%c%c", DUMP_FMT_REV, 2, 1032 shot.xsize, shot.ysize); 1033 1034 fflush(stdout); 1035 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; 1042 1043 line = alloca(shot.xsize + 1); 1044 1045 if (line == NULL) { 1046 revert(); 1047 errx(1, "failed to allocate memory for line buffer"); 1048 } 1049 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; 1054 1055 if (isprint(ch) == 0) 1056 ch = ' '; 1057 1058 line[x] = (char)ch; 1059 } 1060 1061 /* Trim trailing spaces */ 1062 1063 do { 1064 line[x--] = '\0'; 1065 } while (line[x] == ' ' && x != 0); 1066 1067 puts(line); 1068 } 1069 1070 fflush(stdout); 1071 } 1072 } 1073 1074 1075 /* 1076 * Set the console history buffer size. 1077 */ 1078 1079 static void 1080 set_history(char *opt) 1081 { 1082 int size; 1083 1084 size = atoi(opt); 1085 1086 if ((*opt == '\0') || size < 0) { 1087 revert(); 1088 errx(1, "argument must be a positive number"); 1089 } 1090 1091 if (ioctl(0, CONS_HISTORY, &size) == -1) { 1092 revert(); 1093 errc(1, errno, "setting history buffer size"); 1094 } 1095 } 1096 1097 1098 /* 1099 * Clear the console history buffer. 1100 */ 1101 1102 static void 1103 clear_history(void) 1104 { 1105 if (ioctl(0, CONS_CLRHIST) == -1) { 1106 revert(); 1107 errc(1, errno, "clearing history buffer"); 1108 } 1109 } 1110 1111 1112 int 1113 main(int argc, char **argv) 1114 { 1115 char *font, *type; 1116 int opt; 1117 1118 if (argc == 1) 1119 usage(); 1120 1121 init(); 1122 1123 info.size = sizeof(info); 1124 1125 if (ioctl(0, CONS_GETINFO, &info) == -1) 1126 err(1, "must be on a virtual console"); 1127 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); 1145 1146 if (font == NULL) { 1147 type = NULL; 1148 font = optarg; 1149 } 1150 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(); 1159 } 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(); 1202 } 1203 } 1204 1205 if (optind < argc && !strcmp(argv[optind], "show")) { 1206 test_frame(); 1207 optind++; 1208 } 1209 1210 video_mode(argc, argv, &optind); 1211 1212 get_normal_colors(argc, argv, &optind); 1213 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"); 1221 } 1222 } else { 1223 set_colors(); 1224 } 1225 } 1226 1227 if ((optind != argc) || (argc == 1)) 1228 usage(); 1229 1230 return 0; 1231 } 1232