1 /*- 2 * Copyright (c) 1994-1996 Søren Schmidt 3 * All rights reserved. 4 * 5 * Portions of this software are based in part on the work of 6 * Sascha Wildner <saw@online.de> contributed to The DragonFly Project 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer, 13 * in this position and unchanged. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 * $DragonFly: src/usr.sbin/vidcontrol/vidcontrol.c,v 1.10 2005/03/02 06:08:29 joerg Exp $ 32 */ 33 34 #ifndef lint 35 static const char rcsid[] = 36 "$FreeBSD$"; 37 #endif /* not lint */ 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 #include <sys/fbio.h> 47 #include <sys/consio.h> 48 #include <sys/endian.h> 49 #include <sys/errno.h> 50 #include <sys/param.h> 51 #include <sys/types.h> 52 #include <sys/stat.h> 53 #include <sys/sysctl.h> 54 #include "path.h" 55 #include "decode.h" 56 57 58 #define DATASIZE(x) ((x).w * (x).h * 256 / 8) 59 60 /* Screen dump modes */ 61 #define DUMP_FMT_RAW 1 62 #define DUMP_FMT_TXT 2 63 /* Screen dump options */ 64 #define DUMP_FBF 0 65 #define DUMP_ALL 1 66 /* Screen dump file format revision */ 67 #define DUMP_FMT_REV 1 68 69 static const char *legal_colors[16] = { 70 "black", "blue", "green", "cyan", 71 "red", "magenta", "brown", "white", 72 "grey", "lightblue", "lightgreen", "lightcyan", 73 "lightred", "lightmagenta", "yellow", "lightwhite" 74 }; 75 76 static struct { 77 int active_vty; 78 vid_info_t console_info; 79 unsigned char screen_map[256]; 80 int video_mode_number; 81 struct video_info video_mode_info; 82 } cur_info; 83 84 struct vt4font_header { 85 uint8_t magic[8]; 86 uint8_t width; 87 uint8_t height; 88 uint16_t pad; 89 uint32_t glyph_count; 90 uint32_t map_count[4]; 91 } __packed; 92 93 static int hex = 0; 94 static int vesa_cols; 95 static int vesa_rows; 96 static int font_height; 97 static int colors_changed; 98 static int video_mode_changed; 99 static int normal_fore_color, normal_back_color; 100 static int revers_fore_color, revers_back_color; 101 static int vt4_mode = 0; 102 static struct vid_info info; 103 static struct video_info new_mode_info; 104 105 106 /* 107 * Initialize revert data. 108 * 109 * NOTE: the following parameters are not yet saved/restored: 110 * 111 * screen saver timeout 112 * cursor type 113 * mouse character and mouse show/hide state 114 * vty switching on/off state 115 * history buffer size 116 * history contents 117 * font maps 118 */ 119 120 static void 121 init(void) 122 { 123 if (ioctl(0, VT_GETACTIVE, &cur_info.active_vty) == -1) 124 errc(1, errno, "getting active vty"); 125 126 cur_info.console_info.size = sizeof(cur_info.console_info); 127 128 if (ioctl(0, CONS_GETINFO, &cur_info.console_info) == -1) 129 errc(1, errno, "getting console information"); 130 131 /* vt(4) use unicode, so no screen mapping required. */ 132 if (vt4_mode == 0 && 133 ioctl(0, GIO_SCRNMAP, &cur_info.screen_map) == -1) 134 errc(1, errno, "getting screen map"); 135 136 if (ioctl(0, CONS_GET, &cur_info.video_mode_number) == -1) 137 errc(1, errno, "getting video mode number"); 138 139 cur_info.video_mode_info.vi_mode = cur_info.video_mode_number; 140 141 if (ioctl(0, CONS_MODEINFO, &cur_info.video_mode_info) == -1) 142 errc(1, errno, "getting video mode parameters"); 143 144 normal_fore_color = cur_info.console_info.mv_norm.fore; 145 normal_back_color = cur_info.console_info.mv_norm.back; 146 revers_fore_color = cur_info.console_info.mv_rev.fore; 147 revers_back_color = cur_info.console_info.mv_rev.back; 148 } 149 150 151 /* 152 * If something goes wrong along the way we call revert() to go back to the 153 * console state we came from (which is assumed to be working). 154 * 155 * NOTE: please also read the comments of init(). 156 */ 157 158 static void 159 revert(void) 160 { 161 int size[3]; 162 163 ioctl(0, VT_ACTIVATE, cur_info.active_vty); 164 165 fprintf(stderr, "\033[=%dA", cur_info.console_info.mv_ovscan); 166 fprintf(stderr, "\033[=%dF", cur_info.console_info.mv_norm.fore); 167 fprintf(stderr, "\033[=%dG", cur_info.console_info.mv_norm.back); 168 fprintf(stderr, "\033[=%dH", cur_info.console_info.mv_rev.fore); 169 fprintf(stderr, "\033[=%dI", cur_info.console_info.mv_rev.back); 170 171 if (vt4_mode == 0) 172 ioctl(0, PIO_SCRNMAP, &cur_info.screen_map); 173 174 if (cur_info.video_mode_number >= M_VESA_BASE) 175 ioctl(0, _IO('V', cur_info.video_mode_number - M_VESA_BASE), 176 NULL); 177 else 178 ioctl(0, _IO('S', cur_info.video_mode_number), NULL); 179 180 if (cur_info.video_mode_info.vi_flags & V_INFO_GRAPHICS) { 181 size[0] = cur_info.video_mode_info.vi_width / 8; 182 size[1] = cur_info.video_mode_info.vi_height / 183 cur_info.console_info.font_size; 184 size[2] = cur_info.console_info.font_size; 185 186 ioctl(0, KDRASTER, size); 187 } 188 } 189 190 191 /* 192 * Print a short usage string describing all options, then exit. 193 */ 194 195 static void 196 usage(void) 197 { 198 if (vt4_mode) 199 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n", 200 "usage: vidcontrol [-CHPpx] [-b color] [-c appearance] [-f [[size] file]]", 201 " [-g geometry] [-h size] [-i adapter | mode]", 202 " [-M char] [-m on | off] [-r foreground background]", 203 " [-S on | off] [-s number] [-T xterm | cons25] [-t N | off]", 204 " [mode] [foreground [background]] [show]"); 205 else 206 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n", 207 "usage: vidcontrol [-CdHLPpx] [-b color] [-c appearance] [-f [size] file]", 208 " [-g geometry] [-h size] [-i adapter | mode] [-l screen_map]", 209 " [-M char] [-m on | off] [-r foreground background]", 210 " [-S on | off] [-s number] [-T xterm | cons25] [-t N | off]", 211 " [mode] [foreground [background]] [show]"); 212 exit(1); 213 } 214 215 /* Detect presence of vt(4). */ 216 static int 217 is_vt4(void) 218 { 219 char vty_name[4] = ""; 220 size_t len = sizeof(vty_name); 221 222 if (sysctlbyname("kern.vty", vty_name, &len, NULL, 0) != 0) 223 return (0); 224 return (strcmp(vty_name, "vt") == 0); 225 } 226 227 /* 228 * Retrieve the next argument from the command line (for options that require 229 * more than one argument). 230 */ 231 232 static char * 233 nextarg(int ac, char **av, int *indp, int oc, int strict) 234 { 235 if (*indp < ac) 236 return(av[(*indp)++]); 237 238 if (strict != 0) { 239 revert(); 240 errx(1, "option requires two arguments -- %c", oc); 241 } 242 243 return(NULL); 244 } 245 246 247 /* 248 * Guess which file to open. Try to open each combination of a specified set 249 * of file name components. 250 */ 251 252 static FILE * 253 openguess(const char *a[], const char *b[], const char *c[], const char *d[], char **name) 254 { 255 FILE *f; 256 int i, j, k, l; 257 258 for (i = 0; a[i] != NULL; i++) { 259 for (j = 0; b[j] != NULL; j++) { 260 for (k = 0; c[k] != NULL; k++) { 261 for (l = 0; d[l] != NULL; l++) { 262 asprintf(name, "%s%s%s%s", 263 a[i], b[j], c[k], d[l]); 264 265 f = fopen(*name, "r"); 266 267 if (f != NULL) 268 return (f); 269 270 free(*name); 271 } 272 } 273 } 274 } 275 return (NULL); 276 } 277 278 279 /* 280 * Load a screenmap from a file and set it. 281 */ 282 283 static void 284 load_scrnmap(const char *filename) 285 { 286 FILE *fd; 287 int size; 288 char *name; 289 scrmap_t scrnmap; 290 const char *a[] = {"", SCRNMAP_PATH, NULL}; 291 const char *b[] = {filename, NULL}; 292 const char *c[] = {"", ".scm", NULL}; 293 const char *d[] = {"", NULL}; 294 295 fd = openguess(a, b, c, d, &name); 296 297 if (fd == NULL) { 298 revert(); 299 errx(1, "screenmap file not found"); 300 } 301 302 size = sizeof(scrnmap); 303 304 if (decode(fd, (char *)&scrnmap, size) != size) { 305 rewind(fd); 306 307 if (fread(&scrnmap, 1, size, fd) != (size_t)size) { 308 warnx("bad screenmap file"); 309 fclose(fd); 310 revert(); 311 errx(1, "bad screenmap file"); 312 } 313 } 314 315 if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) { 316 revert(); 317 errc(1, errno, "loading screenmap"); 318 } 319 320 fclose(fd); 321 } 322 323 324 /* 325 * Set the default screenmap. 326 */ 327 328 static void 329 load_default_scrnmap(void) 330 { 331 scrmap_t scrnmap; 332 int i; 333 334 for (i=0; i<256; i++) 335 *((char*)&scrnmap + i) = i; 336 337 if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) { 338 revert(); 339 errc(1, errno, "loading default screenmap"); 340 } 341 } 342 343 344 /* 345 * Print the current screenmap to stdout. 346 */ 347 348 static void 349 print_scrnmap(void) 350 { 351 unsigned char map[256]; 352 size_t i; 353 354 if (ioctl(0, GIO_SCRNMAP, &map) == -1) { 355 revert(); 356 errc(1, errno, "getting screenmap"); 357 } 358 for (i=0; i<sizeof(map); i++) { 359 if (i != 0 && i % 16 == 0) 360 fprintf(stdout, "\n"); 361 362 if (hex != 0) 363 fprintf(stdout, " %02x", map[i]); 364 else 365 fprintf(stdout, " %03d", map[i]); 366 } 367 fprintf(stdout, "\n"); 368 369 } 370 371 372 /* 373 * Determine a file's size. 374 */ 375 376 static int 377 fsize(FILE *file) 378 { 379 struct stat sb; 380 381 if (fstat(fileno(file), &sb) == 0) 382 return sb.st_size; 383 else 384 return -1; 385 } 386 387 static vfnt_map_t * 388 load_vt4mappingtable(unsigned int nmappings, FILE *f) 389 { 390 vfnt_map_t *t; 391 unsigned int i; 392 393 if (nmappings == 0) 394 return (NULL); 395 396 t = malloc(sizeof *t * nmappings); 397 398 if (fread(t, sizeof *t * nmappings, 1, f) != 1) { 399 perror("mappings"); 400 exit(1); 401 } 402 403 for (i = 0; i < nmappings; i++) { 404 t[i].src = be32toh(t[i].src); 405 t[i].dst = be16toh(t[i].dst); 406 t[i].len = be16toh(t[i].len); 407 } 408 409 return (t); 410 } 411 412 /* 413 * Set the default vt font. 414 */ 415 416 static void 417 load_default_vt4font(void) 418 { 419 if (ioctl(0, PIO_VFONT_DEFAULT) == -1) { 420 revert(); 421 errc(1, errno, "loading default vt font"); 422 } 423 } 424 425 static int 426 load_vt4font(FILE *f) 427 { 428 struct vt4font_header fh; 429 static vfnt_t vfnt; 430 size_t glyphsize; 431 unsigned int i; 432 433 if (fread(&fh, sizeof fh, 1, f) != 1) { 434 perror("file_header"); 435 return (1); 436 } 437 438 if (memcmp(fh.magic, "VFNT0002", 8) != 0) { 439 fprintf(stderr, "Bad magic\n"); 440 return (1); 441 } 442 443 for (i = 0; i < VFNT_MAPS; i++) 444 vfnt.map_count[i] = be32toh(fh.map_count[i]); 445 vfnt.glyph_count = be32toh(fh.glyph_count); 446 vfnt.width = fh.width; 447 vfnt.height = fh.height; 448 449 glyphsize = howmany(vfnt.width, 8) * vfnt.height * vfnt.glyph_count; 450 vfnt.glyphs = malloc(glyphsize); 451 452 if (fread(vfnt.glyphs, glyphsize, 1, f) != 1) { 453 perror("glyphs"); 454 return (1); 455 } 456 457 for (i = 0; i < VFNT_MAPS; i++) 458 vfnt.map[i] = load_vt4mappingtable(vfnt.map_count[i], f); 459 460 if (ioctl(STDIN_FILENO, PIO_VFONT, &vfnt) == -1) { 461 perror("PIO_VFONT"); 462 return (1); 463 } 464 return (0); 465 } 466 467 /* 468 * Load a font from file and set it. 469 */ 470 471 static void 472 load_font(const char *type, const char *filename) 473 { 474 FILE *fd; 475 int h, i, size, w; 476 unsigned long io = 0; /* silence stupid gcc(1) in the Wall mode */ 477 char *name, *fontmap, size_sufx[6]; 478 const char *a[] = {"", FONT_PATH, NULL}; 479 const char *vt4a[] = {"", VT_FONT_PATH, NULL}; 480 const char *b[] = {filename, NULL}; 481 const char *c[] = {"", size_sufx, NULL}; 482 const char *d[] = {"", ".fnt", NULL}; 483 vid_info_t _info; 484 485 struct sizeinfo { 486 int w; 487 int h; 488 unsigned long io; 489 } sizes[] = {{8, 16, PIO_FONT8x16}, 490 {8, 14, PIO_FONT8x14}, 491 {8, 8, PIO_FONT8x8}, 492 {0, 0, 0}}; 493 494 if (vt4_mode) { 495 size_sufx[0] = '\0'; 496 } else { 497 _info.size = sizeof(_info); 498 if (ioctl(0, CONS_GETINFO, &_info) == -1) { 499 revert(); 500 warn("failed to obtain current video mode parameters"); 501 return; 502 } 503 504 snprintf(size_sufx, sizeof(size_sufx), "-8x%d", _info.font_size); 505 } 506 fd = openguess((vt4_mode == 0) ? a : vt4a, b, c, d, &name); 507 508 if (fd == NULL) { 509 revert(); 510 errx(1, "%s: can't load font file", filename); 511 } 512 513 if (vt4_mode) { 514 if(load_vt4font(fd)) 515 warn("failed to load font \"%s\"", filename); 516 fclose(fd); 517 return; 518 } 519 520 if (type != NULL) { 521 size = 0; 522 if (sscanf(type, "%dx%d", &w, &h) == 2) { 523 for (i = 0; sizes[i].w != 0; i++) { 524 if (sizes[i].w == w && sizes[i].h == h) { 525 size = DATASIZE(sizes[i]); 526 io = sizes[i].io; 527 font_height = sizes[i].h; 528 } 529 } 530 } 531 if (size == 0) { 532 fclose(fd); 533 revert(); 534 errx(1, "%s: bad font size specification", type); 535 } 536 } else { 537 /* Apply heuristics */ 538 539 int j; 540 int dsize[2]; 541 542 size = DATASIZE(sizes[0]); 543 fontmap = (char*) malloc(size); 544 dsize[0] = decode(fd, fontmap, size); 545 dsize[1] = fsize(fd); 546 free(fontmap); 547 548 size = 0; 549 for (j = 0; j < 2; j++) { 550 for (i = 0; sizes[i].w != 0; i++) { 551 if (DATASIZE(sizes[i]) == dsize[j]) { 552 size = dsize[j]; 553 io = sizes[i].io; 554 font_height = sizes[i].h; 555 j = 2; /* XXX */ 556 break; 557 } 558 } 559 } 560 561 if (size == 0) { 562 fclose(fd); 563 revert(); 564 errx(1, "%s: can't guess font size", filename); 565 } 566 567 rewind(fd); 568 } 569 570 fontmap = (char*) malloc(size); 571 572 if (decode(fd, fontmap, size) != size) { 573 rewind(fd); 574 if (fsize(fd) != size || 575 fread(fontmap, 1, size, fd) != (size_t)size) { 576 warnx("%s: bad font file", filename); 577 fclose(fd); 578 free(fontmap); 579 revert(); 580 errx(1, "%s: bad font file", filename); 581 } 582 } 583 584 if (ioctl(0, io, fontmap) == -1) { 585 revert(); 586 errc(1, errno, "loading font"); 587 } 588 589 fclose(fd); 590 free(fontmap); 591 } 592 593 594 /* 595 * Set the timeout for the screensaver. 596 */ 597 598 static void 599 set_screensaver_timeout(char *arg) 600 { 601 int nsec; 602 603 if (!strcmp(arg, "off")) { 604 nsec = 0; 605 } else { 606 nsec = atoi(arg); 607 608 if ((*arg == '\0') || (nsec < 1)) { 609 revert(); 610 errx(1, "argument must be a positive number"); 611 } 612 } 613 614 if (ioctl(0, CONS_BLANKTIME, &nsec) == -1) { 615 revert(); 616 errc(1, errno, "setting screensaver period"); 617 } 618 } 619 620 621 /* 622 * Set the cursor's shape/type. 623 */ 624 625 static void 626 set_cursor_type(char *appearance) 627 { 628 int type; 629 630 if (!strcmp(appearance, "normal")) 631 type = 0; 632 else if (!strcmp(appearance, "blink")) 633 type = 1; 634 else if (!strcmp(appearance, "destructive")) 635 type = 3; 636 else { 637 revert(); 638 errx(1, "argument to -c must be normal, blink or destructive"); 639 } 640 641 if (ioctl(0, CONS_CURSORTYPE, &type) == -1) { 642 revert(); 643 errc(1, errno, "setting cursor type"); 644 } 645 } 646 647 648 /* 649 * Set the video mode. 650 */ 651 652 static int 653 video_mode(int argc, char **argv, int *mode_index) 654 { 655 static struct { 656 const char *name; 657 unsigned long mode; 658 unsigned long mode_num; 659 } modes[] = { 660 { "80x25", SW_TEXT_80x25, M_TEXT_80x25 }, 661 { "80x30", SW_TEXT_80x30, M_TEXT_80x30 }, 662 { "80x43", SW_TEXT_80x43, M_TEXT_80x43 }, 663 { "80x50", SW_TEXT_80x50, M_TEXT_80x50 }, 664 { "80x60", SW_TEXT_80x60, M_TEXT_80x60 }, 665 { "132x25", SW_TEXT_132x25, M_TEXT_132x25 }, 666 { "132x30", SW_TEXT_132x30, M_TEXT_132x30 }, 667 { "132x43", SW_TEXT_132x43, M_TEXT_132x43 }, 668 { "132x50", SW_TEXT_132x50, M_TEXT_132x50 }, 669 { "132x60", SW_TEXT_132x60, M_TEXT_132x60 }, 670 { "VGA_40x25", SW_VGA_C40x25, M_VGA_C40x25 }, 671 { "VGA_80x25", SW_VGA_C80x25, M_VGA_C80x25 }, 672 { "VGA_80x30", SW_VGA_C80x30, M_VGA_C80x30 }, 673 { "VGA_80x50", SW_VGA_C80x50, M_VGA_C80x50 }, 674 { "VGA_80x60", SW_VGA_C80x60, M_VGA_C80x60 }, 675 #ifdef SW_VGA_C90x25 676 { "VGA_90x25", SW_VGA_C90x25, M_VGA_C90x25 }, 677 { "VGA_90x30", SW_VGA_C90x30, M_VGA_C90x30 }, 678 { "VGA_90x43", SW_VGA_C90x43, M_VGA_C90x43 }, 679 { "VGA_90x50", SW_VGA_C90x50, M_VGA_C90x50 }, 680 { "VGA_90x60", SW_VGA_C90x60, M_VGA_C90x60 }, 681 #endif 682 { "VGA_320x200", SW_VGA_CG320, M_CG320 }, 683 { "EGA_80x25", SW_ENH_C80x25, M_ENH_C80x25 }, 684 { "EGA_80x43", SW_ENH_C80x43, M_ENH_C80x43 }, 685 { "VESA_132x25", SW_VESA_C132x25,M_VESA_C132x25 }, 686 { "VESA_132x43", SW_VESA_C132x43,M_VESA_C132x43 }, 687 { "VESA_132x50", SW_VESA_C132x50,M_VESA_C132x50 }, 688 { "VESA_132x60", SW_VESA_C132x60,M_VESA_C132x60 }, 689 { "VESA_800x600", SW_VESA_800x600,M_VESA_800x600 }, 690 { NULL, 0, 0 }, 691 }; 692 693 int new_mode_num = 0; 694 unsigned long mode = 0; 695 int cur_mode; 696 int ioerr; 697 int size[3]; 698 int i; 699 700 if (ioctl(0, CONS_GET, &cur_mode) < 0) 701 err(1, "cannot get the current video mode"); 702 703 /* 704 * Parse the video mode argument... 705 */ 706 707 if (*mode_index < argc) { 708 if (!strncmp(argv[*mode_index], "MODE_", 5)) { 709 if (!isdigit(argv[*mode_index][5])) 710 errx(1, "invalid video mode number"); 711 712 new_mode_num = atoi(&argv[*mode_index][5]); 713 } else { 714 for (i = 0; modes[i].name != NULL; ++i) { 715 if (!strcmp(argv[*mode_index], modes[i].name)) { 716 mode = modes[i].mode; 717 new_mode_num = modes[i].mode_num; 718 break; 719 } 720 } 721 722 if (modes[i].name == NULL) 723 return EXIT_FAILURE; 724 if (ioctl(0, mode, NULL) < 0) { 725 warn("cannot set videomode"); 726 return EXIT_FAILURE; 727 } 728 } 729 730 /* 731 * Collect enough information about the new video mode... 732 */ 733 734 new_mode_info.vi_mode = new_mode_num; 735 736 if (ioctl(0, CONS_MODEINFO, &new_mode_info) == -1) { 737 revert(); 738 errc(1, errno, "obtaining new video mode parameters"); 739 } 740 741 if (mode == 0) { 742 if (new_mode_num >= M_VESA_BASE) 743 mode = _IO('V', new_mode_num - M_VESA_BASE); 744 else 745 mode = _IO('S', new_mode_num); 746 } 747 748 /* 749 * Try setting the new mode. 750 */ 751 752 if (ioctl(0, mode, NULL) == -1) { 753 revert(); 754 errc(1, errno, "setting video mode"); 755 } 756 757 /* 758 * For raster modes it's not enough to just set the mode. 759 * We also need to explicitly set the raster mode. 760 */ 761 762 if (new_mode_info.vi_flags & V_INFO_GRAPHICS) { 763 /* font size */ 764 765 if (font_height == 0) 766 font_height = cur_info.console_info.font_size; 767 768 size[2] = font_height; 769 770 /* adjust columns */ 771 772 if ((vesa_cols * 8 > new_mode_info.vi_width) || 773 (vesa_cols <= 0)) { 774 size[0] = new_mode_info.vi_width / 8; 775 } else { 776 size[0] = vesa_cols; 777 } 778 779 /* adjust rows */ 780 781 if ((vesa_rows * font_height > new_mode_info.vi_height) || 782 (vesa_rows <= 0)) { 783 size[1] = new_mode_info.vi_height / 784 font_height; 785 } else { 786 size[1] = vesa_rows; 787 } 788 789 /* set raster mode */ 790 791 if (ioctl(0, KDRASTER, size)) { 792 ioerr = errno; 793 if (cur_mode >= M_VESA_BASE) 794 ioctl(0, 795 _IO('V', cur_mode - M_VESA_BASE), 796 NULL); 797 else 798 ioctl(0, _IO('S', cur_mode), NULL); 799 revert(); 800 warnc(ioerr, "cannot activate raster display"); 801 return EXIT_FAILURE; 802 } 803 } 804 805 video_mode_changed = 1; 806 807 (*mode_index)++; 808 } 809 return EXIT_SUCCESS; 810 } 811 812 813 /* 814 * Return the number for a specified color name. 815 */ 816 817 static int 818 get_color_number(char *color) 819 { 820 int i; 821 822 for (i=0; i<16; i++) { 823 if (!strcmp(color, legal_colors[i])) 824 return i; 825 } 826 return -1; 827 } 828 829 830 /* 831 * Get normal text and background colors. 832 */ 833 834 static void 835 get_normal_colors(int argc, char **argv, int *_index) 836 { 837 int color; 838 839 if (*_index < argc && (color = get_color_number(argv[*_index])) != -1) { 840 (*_index)++; 841 fprintf(stderr, "\033[=%dF", color); 842 normal_fore_color=color; 843 colors_changed = 1; 844 if (*_index < argc 845 && (color = get_color_number(argv[*_index])) != -1 846 && color < 8) { 847 (*_index)++; 848 fprintf(stderr, "\033[=%dG", color); 849 normal_back_color=color; 850 } 851 } 852 } 853 854 855 /* 856 * Get reverse text and background colors. 857 */ 858 859 static void 860 get_reverse_colors(int argc, char **argv, int *_index) 861 { 862 int color; 863 864 if ((color = get_color_number(argv[*(_index)-1])) != -1) { 865 fprintf(stderr, "\033[=%dH", color); 866 revers_fore_color=color; 867 colors_changed = 1; 868 if (*_index < argc 869 && (color = get_color_number(argv[*_index])) != -1 870 && color < 8) { 871 (*_index)++; 872 fprintf(stderr, "\033[=%dI", color); 873 revers_back_color=color; 874 } 875 } 876 } 877 878 879 /* 880 * Set normal and reverse foreground and background colors. 881 */ 882 883 static void 884 set_colors(void) 885 { 886 fprintf(stderr, "\033[=%dF", normal_fore_color); 887 fprintf(stderr, "\033[=%dG", normal_back_color); 888 fprintf(stderr, "\033[=%dH", revers_fore_color); 889 fprintf(stderr, "\033[=%dI", revers_back_color); 890 } 891 892 893 /* 894 * Switch to virtual terminal #arg. 895 */ 896 897 static void 898 set_console(char *arg) 899 { 900 int n; 901 902 if(!arg || strspn(arg,"0123456789") != strlen(arg)) { 903 revert(); 904 errx(1, "bad console number"); 905 } 906 907 n = atoi(arg); 908 909 if (n < 1 || n > 16) { 910 revert(); 911 errx(1, "console number out of range"); 912 } else if (ioctl(0, VT_ACTIVATE, n) == -1) { 913 revert(); 914 errc(1, errno, "switching vty"); 915 } 916 } 917 918 919 /* 920 * Sets the border color. 921 */ 922 923 static void 924 set_border_color(char *arg) 925 { 926 int color; 927 928 if ((color = get_color_number(arg)) != -1) { 929 fprintf(stderr, "\033[=%dA", color); 930 } 931 else 932 usage(); 933 } 934 935 static void 936 set_mouse_char(char *arg) 937 { 938 struct mouse_info mouse; 939 long l; 940 941 l = strtol(arg, NULL, 0); 942 943 if ((l < 0) || (l > UCHAR_MAX - 3)) { 944 revert(); 945 warnx("argument to -M must be 0 through %d", UCHAR_MAX - 3); 946 return; 947 } 948 949 mouse.operation = MOUSE_MOUSECHAR; 950 mouse.u.mouse_char = (int)l; 951 952 if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) { 953 revert(); 954 errc(1, errno, "setting mouse character"); 955 } 956 } 957 958 959 /* 960 * Show/hide the mouse. 961 */ 962 963 static void 964 set_mouse(char *arg) 965 { 966 struct mouse_info mouse; 967 968 if (!strcmp(arg, "on")) { 969 mouse.operation = MOUSE_SHOW; 970 } else if (!strcmp(arg, "off")) { 971 mouse.operation = MOUSE_HIDE; 972 } else { 973 revert(); 974 errx(1, "argument to -m must be either on or off"); 975 } 976 977 if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) { 978 revert(); 979 errc(1, errno, "%sing the mouse", 980 mouse.operation == MOUSE_SHOW ? "show" : "hid"); 981 } 982 } 983 984 985 static void 986 set_lockswitch(char *arg) 987 { 988 int data; 989 990 if (!strcmp(arg, "off")) { 991 data = 0x01; 992 } else if (!strcmp(arg, "on")) { 993 data = 0x02; 994 } else { 995 revert(); 996 errx(1, "argument to -S must be either on or off"); 997 } 998 999 if (ioctl(0, VT_LOCKSWITCH, &data) == -1) { 1000 revert(); 1001 errc(1, errno, "turning %s vty switching", 1002 data == 0x01 ? "off" : "on"); 1003 } 1004 } 1005 1006 1007 /* 1008 * Return the adapter name for a specified type. 1009 */ 1010 1011 static const char 1012 *adapter_name(int type) 1013 { 1014 static struct { 1015 int type; 1016 const char *name; 1017 } names[] = { 1018 { KD_MONO, "MDA" }, 1019 { KD_HERCULES, "Hercules" }, 1020 { KD_CGA, "CGA" }, 1021 { KD_EGA, "EGA" }, 1022 { KD_VGA, "VGA" }, 1023 { KD_PC98, "PC-98xx" }, 1024 { KD_TGA, "TGA" }, 1025 { -1, "Unknown" }, 1026 }; 1027 1028 int i; 1029 1030 for (i = 0; names[i].type != -1; ++i) 1031 if (names[i].type == type) 1032 break; 1033 return names[i].name; 1034 } 1035 1036 1037 /* 1038 * Show graphics adapter information. 1039 */ 1040 1041 static void 1042 show_adapter_info(void) 1043 { 1044 struct video_adapter_info ad; 1045 1046 ad.va_index = 0; 1047 1048 if (ioctl(0, CONS_ADPINFO, &ad) == -1) { 1049 revert(); 1050 errc(1, errno, "obtaining adapter information"); 1051 } 1052 1053 printf("fb%d:\n", ad.va_index); 1054 printf(" %.*s%d, type:%s%s (%d), flags:0x%x\n", 1055 (int)sizeof(ad.va_name), ad.va_name, ad.va_unit, 1056 (ad.va_flags & V_ADP_VESA) ? "VESA " : "", 1057 adapter_name(ad.va_type), ad.va_type, ad.va_flags); 1058 printf(" initial mode:%d, current mode:%d, BIOS mode:%d\n", 1059 ad.va_initial_mode, ad.va_mode, ad.va_initial_bios_mode); 1060 printf(" frame buffer window:0x%zx, buffer size:0x%zx\n", 1061 ad.va_window, ad.va_buffer_size); 1062 printf(" window size:0x%zx, origin:0x%x\n", 1063 ad.va_window_size, ad.va_window_orig); 1064 printf(" display start address (%d, %d), scan line width:%d\n", 1065 ad.va_disp_start.x, ad.va_disp_start.y, ad.va_line_width); 1066 printf(" reserved:0x%zx\n", ad.va_unused0); 1067 } 1068 1069 1070 /* 1071 * Show video mode information. 1072 */ 1073 1074 static void 1075 show_mode_info(void) 1076 { 1077 char buf[80]; 1078 struct video_info _info; 1079 int c; 1080 int mm; 1081 int mode; 1082 1083 printf(" mode# flags type size " 1084 "font window linear buffer\n"); 1085 printf("---------------------------------------" 1086 "---------------------------------------\n"); 1087 1088 for (mode = 0; mode <= M_VESA_MODE_MAX; ++mode) { 1089 _info.vi_mode = mode; 1090 if (ioctl(0, CONS_MODEINFO, &_info)) 1091 continue; 1092 if (_info.vi_mode != mode) 1093 continue; 1094 1095 printf("%3d (0x%03x)", mode, mode); 1096 printf(" 0x%08x", _info.vi_flags); 1097 if (_info.vi_flags & V_INFO_GRAPHICS) { 1098 c = 'G'; 1099 1100 if (_info.vi_mem_model == V_INFO_MM_PLANAR) 1101 snprintf(buf, sizeof(buf), "%dx%dx%d %d", 1102 _info.vi_width, _info.vi_height, 1103 _info.vi_depth, _info.vi_planes); 1104 else { 1105 switch (_info.vi_mem_model) { 1106 case V_INFO_MM_PACKED: 1107 mm = 'P'; 1108 break; 1109 case V_INFO_MM_DIRECT: 1110 mm = 'D'; 1111 break; 1112 case V_INFO_MM_CGA: 1113 mm = 'C'; 1114 break; 1115 case V_INFO_MM_HGC: 1116 mm = 'H'; 1117 break; 1118 case V_INFO_MM_VGAX: 1119 mm = 'V'; 1120 break; 1121 default: 1122 mm = ' '; 1123 break; 1124 } 1125 snprintf(buf, sizeof(buf), "%dx%dx%d %c", 1126 _info.vi_width, _info.vi_height, 1127 _info.vi_depth, mm); 1128 } 1129 } else { 1130 c = 'T'; 1131 1132 snprintf(buf, sizeof(buf), "%dx%d", 1133 _info.vi_width, _info.vi_height); 1134 } 1135 1136 printf(" %c %-15s", c, buf); 1137 snprintf(buf, sizeof(buf), "%dx%d", 1138 _info.vi_cwidth, _info.vi_cheight); 1139 printf(" %-5s", buf); 1140 printf(" 0x%05zx %2dk %2dk", 1141 _info.vi_window, (int)_info.vi_window_size/1024, 1142 (int)_info.vi_window_gran/1024); 1143 printf(" 0x%08zx %dk\n", 1144 _info.vi_buffer, (int)_info.vi_buffer_size/1024); 1145 } 1146 } 1147 1148 1149 static void 1150 show_info(char *arg) 1151 { 1152 if (!strcmp(arg, "adapter")) { 1153 show_adapter_info(); 1154 } else if (!strcmp(arg, "mode")) { 1155 show_mode_info(); 1156 } else { 1157 revert(); 1158 errx(1, "argument to -i must be either adapter or mode"); 1159 } 1160 } 1161 1162 1163 static void 1164 test_frame(void) 1165 { 1166 int i, cur_mode, fore; 1167 1168 fore = 15; 1169 1170 if (ioctl(0, CONS_GET, &cur_mode) < 0) 1171 err(1, "must be on a virtual console"); 1172 switch (cur_mode) { 1173 case M_PC98_80x25: 1174 case M_PC98_80x30: 1175 fore = 7; 1176 break; 1177 } 1178 1179 fprintf(stdout, "\033[=0G\n\n"); 1180 for (i=0; i<8; i++) { 1181 fprintf(stdout, "\033[=%dF\033[=0G %2d \033[=%dF%-16s" 1182 "\033[=%dF\033[=0G %2d \033[=%dF%-16s " 1183 "\033[=%dF %2d \033[=%dGBACKGROUND\033[=0G\n", 1184 fore, i, i, legal_colors[i], 1185 fore, i+8, i+8, legal_colors[i+8], 1186 fore, i, i); 1187 } 1188 fprintf(stdout, "\033[=%dF\033[=%dG\033[=%dH\033[=%dI\n", 1189 info.mv_norm.fore, info.mv_norm.back, 1190 info.mv_rev.fore, info.mv_rev.back); 1191 } 1192 1193 1194 /* 1195 * Snapshot the video memory of that terminal, using the CONS_SCRSHOT 1196 * ioctl, and writes the results to stdout either in the special 1197 * binary format (see manual page for details), or in the plain 1198 * text format. 1199 */ 1200 1201 static void 1202 dump_screen(int mode, int opt) 1203 { 1204 scrshot_t shot; 1205 vid_info_t _info; 1206 1207 _info.size = sizeof(_info); 1208 1209 if (ioctl(0, CONS_GETINFO, &_info) == -1) { 1210 revert(); 1211 errc(1, errno, "obtaining current video mode parameters"); 1212 return; 1213 } 1214 1215 shot.x = shot.y = 0; 1216 shot.xsize = _info.mv_csz; 1217 shot.ysize = _info.mv_rsz; 1218 if (opt == DUMP_ALL) 1219 shot.ysize += _info.mv_hsz; 1220 1221 shot.buf = alloca(shot.xsize * shot.ysize * sizeof(u_int16_t)); 1222 if (shot.buf == NULL) { 1223 revert(); 1224 errx(1, "failed to allocate memory for dump"); 1225 } 1226 1227 if (ioctl(0, CONS_SCRSHOT, &shot) == -1) { 1228 revert(); 1229 errc(1, errno, "dumping screen"); 1230 } 1231 1232 if (mode == DUMP_FMT_RAW) { 1233 printf("SCRSHOT_%c%c%c%c", DUMP_FMT_REV, 2, 1234 shot.xsize, shot.ysize); 1235 1236 fflush(stdout); 1237 1238 write(STDOUT_FILENO, shot.buf, 1239 shot.xsize * shot.ysize * sizeof(u_int16_t)); 1240 } else { 1241 char *line; 1242 int x, y; 1243 u_int16_t ch; 1244 1245 line = alloca(shot.xsize + 1); 1246 1247 if (line == NULL) { 1248 revert(); 1249 errx(1, "failed to allocate memory for line buffer"); 1250 } 1251 1252 for (y = 0; y < shot.ysize; y++) { 1253 for (x = 0; x < shot.xsize; x++) { 1254 ch = shot.buf[x + (y * shot.xsize)]; 1255 ch &= 0xff; 1256 1257 if (isprint(ch) == 0) 1258 ch = ' '; 1259 1260 line[x] = (char)ch; 1261 } 1262 1263 /* Trim trailing spaces */ 1264 1265 do { 1266 line[x--] = '\0'; 1267 } while (line[x] == ' ' && x != 0); 1268 1269 puts(line); 1270 } 1271 1272 fflush(stdout); 1273 } 1274 } 1275 1276 1277 /* 1278 * Set the console history buffer size. 1279 */ 1280 1281 static void 1282 set_history(char *opt) 1283 { 1284 int size; 1285 1286 size = atoi(opt); 1287 1288 if ((*opt == '\0') || size < 0) { 1289 revert(); 1290 errx(1, "argument must be a positive number"); 1291 } 1292 1293 if (ioctl(0, CONS_HISTORY, &size) == -1) { 1294 revert(); 1295 errc(1, errno, "setting history buffer size"); 1296 } 1297 } 1298 1299 1300 /* 1301 * Clear the console history buffer. 1302 */ 1303 1304 static void 1305 clear_history(void) 1306 { 1307 if (ioctl(0, CONS_CLRHIST) == -1) { 1308 revert(); 1309 errc(1, errno, "clearing history buffer"); 1310 } 1311 } 1312 1313 static void 1314 set_terminal_mode(char *arg) 1315 { 1316 1317 if (strcmp(arg, "xterm") == 0) 1318 fprintf(stderr, "\033[=T"); 1319 else if (strcmp(arg, "cons25") == 0) 1320 fprintf(stderr, "\033[=1T"); 1321 } 1322 1323 1324 int 1325 main(int argc, char **argv) 1326 { 1327 char *font, *type, *termmode; 1328 const char *opts; 1329 int dumpmod, dumpopt, opt; 1330 int reterr; 1331 1332 vt4_mode = is_vt4(); 1333 1334 init(); 1335 1336 info.size = sizeof(info); 1337 1338 if (ioctl(0, CONS_GETINFO, &info) == -1) 1339 err(1, "must be on a virtual console"); 1340 dumpmod = 0; 1341 dumpopt = DUMP_FBF; 1342 termmode = NULL; 1343 if (vt4_mode) 1344 opts = "b:Cc:fg:h:Hi:M:m:pPr:S:s:T:t:x"; 1345 else 1346 opts = "b:Cc:df:g:h:Hi:l:LM:m:pPr:S:s:T:t:x"; 1347 1348 while ((opt = getopt(argc, argv, opts)) != -1) 1349 switch(opt) { 1350 case 'b': 1351 set_border_color(optarg); 1352 break; 1353 case 'C': 1354 clear_history(); 1355 break; 1356 case 'c': 1357 set_cursor_type(optarg); 1358 break; 1359 case 'd': 1360 if (vt4_mode) 1361 break; 1362 print_scrnmap(); 1363 break; 1364 case 'f': 1365 optarg = nextarg(argc, argv, &optind, 'f', 0); 1366 if (optarg != NULL) { 1367 font = nextarg(argc, argv, &optind, 'f', 0); 1368 1369 if (font == NULL) { 1370 type = NULL; 1371 font = optarg; 1372 } else 1373 type = optarg; 1374 1375 load_font(type, font); 1376 } else { 1377 if (!vt4_mode) 1378 usage(); /* Switch syscons to ROM? */ 1379 1380 load_default_vt4font(); 1381 } 1382 break; 1383 case 'g': 1384 if (sscanf(optarg, "%dx%d", 1385 &vesa_cols, &vesa_rows) != 2) { 1386 revert(); 1387 warnx("incorrect geometry: %s", optarg); 1388 usage(); 1389 } 1390 break; 1391 case 'h': 1392 set_history(optarg); 1393 break; 1394 case 'H': 1395 dumpopt = DUMP_ALL; 1396 break; 1397 case 'i': 1398 show_info(optarg); 1399 break; 1400 case 'l': 1401 if (vt4_mode) 1402 break; 1403 load_scrnmap(optarg); 1404 break; 1405 case 'L': 1406 if (vt4_mode) 1407 break; 1408 load_default_scrnmap(); 1409 break; 1410 case 'M': 1411 set_mouse_char(optarg); 1412 break; 1413 case 'm': 1414 set_mouse(optarg); 1415 break; 1416 case 'p': 1417 dumpmod = DUMP_FMT_RAW; 1418 break; 1419 case 'P': 1420 dumpmod = DUMP_FMT_TXT; 1421 break; 1422 case 'r': 1423 get_reverse_colors(argc, argv, &optind); 1424 break; 1425 case 'S': 1426 set_lockswitch(optarg); 1427 break; 1428 case 's': 1429 set_console(optarg); 1430 break; 1431 case 'T': 1432 if (strcmp(optarg, "xterm") != 0 && 1433 strcmp(optarg, "cons25") != 0) 1434 usage(); 1435 termmode = optarg; 1436 break; 1437 case 't': 1438 set_screensaver_timeout(optarg); 1439 break; 1440 case 'x': 1441 hex = 1; 1442 break; 1443 default: 1444 usage(); 1445 } 1446 1447 if (dumpmod != 0) 1448 dump_screen(dumpmod, dumpopt); 1449 reterr = video_mode(argc, argv, &optind); 1450 get_normal_colors(argc, argv, &optind); 1451 1452 if (optind < argc && !strcmp(argv[optind], "show")) { 1453 test_frame(); 1454 optind++; 1455 } 1456 1457 video_mode(argc, argv, &optind); 1458 if (termmode != NULL) 1459 set_terminal_mode(termmode); 1460 1461 get_normal_colors(argc, argv, &optind); 1462 1463 if (colors_changed || video_mode_changed) { 1464 if (!(new_mode_info.vi_flags & V_INFO_GRAPHICS)) { 1465 if ((normal_back_color < 8) && (revers_back_color < 8)) { 1466 set_colors(); 1467 } else { 1468 revert(); 1469 errx(1, "bg color for text modes must be < 8"); 1470 } 1471 } else { 1472 set_colors(); 1473 } 1474 } 1475 1476 if ((optind != argc) || (argc == 1)) 1477 usage(); 1478 return reterr; 1479 } 1480 1481