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 active | 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 if ((t = calloc(nmappings, sizeof(*t))) == NULL) { 397 warn("calloc"); 398 return (NULL); 399 } 400 401 if (fread(t, sizeof *t * nmappings, 1, f) != 1) { 402 warn("read mappings"); 403 free(t); 404 return (NULL); 405 } 406 407 for (i = 0; i < nmappings; i++) { 408 t[i].src = be32toh(t[i].src); 409 t[i].dst = be16toh(t[i].dst); 410 t[i].len = be16toh(t[i].len); 411 } 412 413 return (t); 414 } 415 416 /* 417 * Set the default vt font. 418 */ 419 420 static void 421 load_default_vt4font(void) 422 { 423 if (ioctl(0, PIO_VFONT_DEFAULT) == -1) { 424 revert(); 425 errc(1, errno, "loading default vt font"); 426 } 427 } 428 429 static void 430 load_vt4font(FILE *f) 431 { 432 struct vt4font_header fh; 433 static vfnt_t vfnt; 434 size_t glyphsize; 435 unsigned int i; 436 437 if (fread(&fh, sizeof fh, 1, f) != 1) { 438 warn("read file_header"); 439 return; 440 } 441 442 if (memcmp(fh.magic, "VFNT0002", 8) != 0) { 443 warnx("bad magic in font file\n"); 444 return; 445 } 446 447 for (i = 0; i < VFNT_MAPS; i++) 448 vfnt.map_count[i] = be32toh(fh.map_count[i]); 449 vfnt.glyph_count = be32toh(fh.glyph_count); 450 vfnt.width = fh.width; 451 vfnt.height = fh.height; 452 453 glyphsize = howmany(vfnt.width, 8) * vfnt.height * vfnt.glyph_count; 454 if ((vfnt.glyphs = malloc(glyphsize)) == NULL) { 455 warn("malloc"); 456 return; 457 } 458 459 if (fread(vfnt.glyphs, glyphsize, 1, f) != 1) { 460 warn("read glyphs"); 461 free(vfnt.glyphs); 462 return; 463 } 464 465 for (i = 0; i < VFNT_MAPS; i++) 466 vfnt.map[i] = load_vt4mappingtable(vfnt.map_count[i], f); 467 468 if (ioctl(STDIN_FILENO, PIO_VFONT, &vfnt) == -1) 469 warn("PIO_VFONT"); 470 471 for (i = 0; i < VFNT_MAPS; i++) 472 free(vfnt.map[i]); 473 free(vfnt.glyphs); 474 } 475 476 /* 477 * Load a font from file and set it. 478 */ 479 480 static void 481 load_font(const char *type, const char *filename) 482 { 483 FILE *fd; 484 int h, i, size, w; 485 unsigned long io = 0; /* silence stupid gcc(1) in the Wall mode */ 486 char *name, *fontmap, size_sufx[6]; 487 const char *a[] = {"", FONT_PATH, NULL}; 488 const char *vt4a[] = {"", VT_FONT_PATH, NULL}; 489 const char *b[] = {filename, NULL}; 490 const char *c[] = {"", size_sufx, NULL}; 491 const char *d[] = {"", ".fnt", NULL}; 492 vid_info_t _info; 493 494 struct sizeinfo { 495 int w; 496 int h; 497 unsigned long io; 498 } sizes[] = {{8, 16, PIO_FONT8x16}, 499 {8, 14, PIO_FONT8x14}, 500 {8, 8, PIO_FONT8x8}, 501 {0, 0, 0}}; 502 503 if (vt4_mode) { 504 size_sufx[0] = '\0'; 505 } else { 506 _info.size = sizeof(_info); 507 if (ioctl(0, CONS_GETINFO, &_info) == -1) { 508 revert(); 509 warn("failed to obtain current video mode parameters"); 510 return; 511 } 512 513 snprintf(size_sufx, sizeof(size_sufx), "-8x%d", _info.font_size); 514 } 515 fd = openguess((vt4_mode == 0) ? a : vt4a, b, c, d, &name); 516 517 if (fd == NULL) { 518 revert(); 519 errx(1, "%s: can't load font file", filename); 520 } 521 522 if (vt4_mode) { 523 load_vt4font(fd); 524 fclose(fd); 525 return; 526 } 527 528 if (type != NULL) { 529 size = 0; 530 if (sscanf(type, "%dx%d", &w, &h) == 2) { 531 for (i = 0; sizes[i].w != 0; i++) { 532 if (sizes[i].w == w && sizes[i].h == h) { 533 size = DATASIZE(sizes[i]); 534 io = sizes[i].io; 535 font_height = sizes[i].h; 536 } 537 } 538 } 539 if (size == 0) { 540 fclose(fd); 541 revert(); 542 errx(1, "%s: bad font size specification", type); 543 } 544 } else { 545 /* Apply heuristics */ 546 547 int j; 548 int dsize[2]; 549 550 size = DATASIZE(sizes[0]); 551 fontmap = (char*) malloc(size); 552 dsize[0] = decode(fd, fontmap, size); 553 dsize[1] = fsize(fd); 554 free(fontmap); 555 556 size = 0; 557 for (j = 0; j < 2; j++) { 558 for (i = 0; sizes[i].w != 0; i++) { 559 if (DATASIZE(sizes[i]) == dsize[j]) { 560 size = dsize[j]; 561 io = sizes[i].io; 562 font_height = sizes[i].h; 563 j = 2; /* XXX */ 564 break; 565 } 566 } 567 } 568 569 if (size == 0) { 570 fclose(fd); 571 revert(); 572 errx(1, "%s: can't guess font size", filename); 573 } 574 575 rewind(fd); 576 } 577 578 fontmap = (char*) malloc(size); 579 580 if (decode(fd, fontmap, size) != size) { 581 rewind(fd); 582 if (fsize(fd) != size || 583 fread(fontmap, 1, size, fd) != (size_t)size) { 584 warnx("%s: bad font file", filename); 585 fclose(fd); 586 free(fontmap); 587 revert(); 588 errx(1, "%s: bad font file", filename); 589 } 590 } 591 592 if (ioctl(0, io, fontmap) == -1) { 593 revert(); 594 errc(1, errno, "loading font"); 595 } 596 597 fclose(fd); 598 free(fontmap); 599 } 600 601 602 /* 603 * Set the timeout for the screensaver. 604 */ 605 606 static void 607 set_screensaver_timeout(char *arg) 608 { 609 int nsec; 610 611 if (!strcmp(arg, "off")) { 612 nsec = 0; 613 } else { 614 nsec = atoi(arg); 615 616 if ((*arg == '\0') || (nsec < 1)) { 617 revert(); 618 errx(1, "argument must be a positive number"); 619 } 620 } 621 622 if (ioctl(0, CONS_BLANKTIME, &nsec) == -1) { 623 revert(); 624 errc(1, errno, "setting screensaver period"); 625 } 626 } 627 628 629 /* 630 * Set the cursor's shape/type. 631 */ 632 633 static void 634 set_cursor_type(char *appearance) 635 { 636 int type; 637 638 if (!strcmp(appearance, "normal")) 639 type = 0; 640 else if (!strcmp(appearance, "blink")) 641 type = 1; 642 else if (!strcmp(appearance, "destructive")) 643 type = 3; 644 else { 645 revert(); 646 errx(1, "argument to -c must be normal, blink or destructive"); 647 } 648 649 if (ioctl(0, CONS_CURSORTYPE, &type) == -1) { 650 revert(); 651 errc(1, errno, "setting cursor type"); 652 } 653 } 654 655 656 /* 657 * Set the video mode. 658 */ 659 660 static int 661 video_mode(int argc, char **argv, int *mode_index) 662 { 663 static struct { 664 const char *name; 665 unsigned long mode; 666 unsigned long mode_num; 667 } modes[] = { 668 { "80x25", SW_TEXT_80x25, M_TEXT_80x25 }, 669 { "80x30", SW_TEXT_80x30, M_TEXT_80x30 }, 670 { "80x43", SW_TEXT_80x43, M_TEXT_80x43 }, 671 { "80x50", SW_TEXT_80x50, M_TEXT_80x50 }, 672 { "80x60", SW_TEXT_80x60, M_TEXT_80x60 }, 673 { "132x25", SW_TEXT_132x25, M_TEXT_132x25 }, 674 { "132x30", SW_TEXT_132x30, M_TEXT_132x30 }, 675 { "132x43", SW_TEXT_132x43, M_TEXT_132x43 }, 676 { "132x50", SW_TEXT_132x50, M_TEXT_132x50 }, 677 { "132x60", SW_TEXT_132x60, M_TEXT_132x60 }, 678 { "VGA_40x25", SW_VGA_C40x25, M_VGA_C40x25 }, 679 { "VGA_80x25", SW_VGA_C80x25, M_VGA_C80x25 }, 680 { "VGA_80x30", SW_VGA_C80x30, M_VGA_C80x30 }, 681 { "VGA_80x50", SW_VGA_C80x50, M_VGA_C80x50 }, 682 { "VGA_80x60", SW_VGA_C80x60, M_VGA_C80x60 }, 683 #ifdef SW_VGA_C90x25 684 { "VGA_90x25", SW_VGA_C90x25, M_VGA_C90x25 }, 685 { "VGA_90x30", SW_VGA_C90x30, M_VGA_C90x30 }, 686 { "VGA_90x43", SW_VGA_C90x43, M_VGA_C90x43 }, 687 { "VGA_90x50", SW_VGA_C90x50, M_VGA_C90x50 }, 688 { "VGA_90x60", SW_VGA_C90x60, M_VGA_C90x60 }, 689 #endif 690 { "VGA_320x200", SW_VGA_CG320, M_CG320 }, 691 { "EGA_80x25", SW_ENH_C80x25, M_ENH_C80x25 }, 692 { "EGA_80x43", SW_ENH_C80x43, M_ENH_C80x43 }, 693 { "VESA_132x25", SW_VESA_C132x25,M_VESA_C132x25 }, 694 { "VESA_132x43", SW_VESA_C132x43,M_VESA_C132x43 }, 695 { "VESA_132x50", SW_VESA_C132x50,M_VESA_C132x50 }, 696 { "VESA_132x60", SW_VESA_C132x60,M_VESA_C132x60 }, 697 { "VESA_800x600", SW_VESA_800x600,M_VESA_800x600 }, 698 { NULL, 0, 0 }, 699 }; 700 701 int new_mode_num = 0; 702 unsigned long mode = 0; 703 int cur_mode; 704 int ioerr; 705 int size[3]; 706 int i; 707 708 if (ioctl(0, CONS_GET, &cur_mode) < 0) 709 err(1, "cannot get the current video mode"); 710 711 /* 712 * Parse the video mode argument... 713 */ 714 715 if (*mode_index < argc) { 716 if (!strncmp(argv[*mode_index], "MODE_", 5)) { 717 if (!isdigit(argv[*mode_index][5])) 718 errx(1, "invalid video mode number"); 719 720 new_mode_num = atoi(&argv[*mode_index][5]); 721 } else { 722 for (i = 0; modes[i].name != NULL; ++i) { 723 if (!strcmp(argv[*mode_index], modes[i].name)) { 724 mode = modes[i].mode; 725 new_mode_num = modes[i].mode_num; 726 break; 727 } 728 } 729 730 if (modes[i].name == NULL) 731 return EXIT_FAILURE; 732 if (ioctl(0, mode, NULL) < 0) { 733 warn("cannot set videomode"); 734 return EXIT_FAILURE; 735 } 736 } 737 738 /* 739 * Collect enough information about the new video mode... 740 */ 741 742 new_mode_info.vi_mode = new_mode_num; 743 744 if (ioctl(0, CONS_MODEINFO, &new_mode_info) == -1) { 745 revert(); 746 errc(1, errno, "obtaining new video mode parameters"); 747 } 748 749 if (mode == 0) { 750 if (new_mode_num >= M_VESA_BASE) 751 mode = _IO('V', new_mode_num - M_VESA_BASE); 752 else 753 mode = _IO('S', new_mode_num); 754 } 755 756 /* 757 * Try setting the new mode. 758 */ 759 760 if (ioctl(0, mode, NULL) == -1) { 761 revert(); 762 errc(1, errno, "setting video mode"); 763 } 764 765 /* 766 * For raster modes it's not enough to just set the mode. 767 * We also need to explicitly set the raster mode. 768 */ 769 770 if (new_mode_info.vi_flags & V_INFO_GRAPHICS) { 771 /* font size */ 772 773 if (font_height == 0) 774 font_height = cur_info.console_info.font_size; 775 776 size[2] = font_height; 777 778 /* adjust columns */ 779 780 if ((vesa_cols * 8 > new_mode_info.vi_width) || 781 (vesa_cols <= 0)) { 782 size[0] = new_mode_info.vi_width / 8; 783 } else { 784 size[0] = vesa_cols; 785 } 786 787 /* adjust rows */ 788 789 if ((vesa_rows * font_height > new_mode_info.vi_height) || 790 (vesa_rows <= 0)) { 791 size[1] = new_mode_info.vi_height / 792 font_height; 793 } else { 794 size[1] = vesa_rows; 795 } 796 797 /* set raster mode */ 798 799 if (ioctl(0, KDRASTER, size)) { 800 ioerr = errno; 801 if (cur_mode >= M_VESA_BASE) 802 ioctl(0, 803 _IO('V', cur_mode - M_VESA_BASE), 804 NULL); 805 else 806 ioctl(0, _IO('S', cur_mode), NULL); 807 revert(); 808 warnc(ioerr, "cannot activate raster display"); 809 return EXIT_FAILURE; 810 } 811 } 812 813 video_mode_changed = 1; 814 815 (*mode_index)++; 816 } 817 return EXIT_SUCCESS; 818 } 819 820 821 /* 822 * Return the number for a specified color name. 823 */ 824 825 static int 826 get_color_number(char *color) 827 { 828 int i; 829 830 for (i=0; i<16; i++) { 831 if (!strcmp(color, legal_colors[i])) 832 return i; 833 } 834 return -1; 835 } 836 837 838 /* 839 * Get normal text and background colors. 840 */ 841 842 static void 843 get_normal_colors(int argc, char **argv, int *_index) 844 { 845 int color; 846 847 if (*_index < argc && (color = get_color_number(argv[*_index])) != -1) { 848 (*_index)++; 849 fprintf(stderr, "\033[=%dF", color); 850 normal_fore_color=color; 851 colors_changed = 1; 852 if (*_index < argc 853 && (color = get_color_number(argv[*_index])) != -1 854 && color < 8) { 855 (*_index)++; 856 fprintf(stderr, "\033[=%dG", color); 857 normal_back_color=color; 858 } 859 } 860 } 861 862 863 /* 864 * Get reverse text and background colors. 865 */ 866 867 static void 868 get_reverse_colors(int argc, char **argv, int *_index) 869 { 870 int color; 871 872 if ((color = get_color_number(argv[*(_index)-1])) != -1) { 873 fprintf(stderr, "\033[=%dH", color); 874 revers_fore_color=color; 875 colors_changed = 1; 876 if (*_index < argc 877 && (color = get_color_number(argv[*_index])) != -1 878 && color < 8) { 879 (*_index)++; 880 fprintf(stderr, "\033[=%dI", color); 881 revers_back_color=color; 882 } 883 } 884 } 885 886 887 /* 888 * Set normal and reverse foreground and background colors. 889 */ 890 891 static void 892 set_colors(void) 893 { 894 fprintf(stderr, "\033[=%dF", normal_fore_color); 895 fprintf(stderr, "\033[=%dG", normal_back_color); 896 fprintf(stderr, "\033[=%dH", revers_fore_color); 897 fprintf(stderr, "\033[=%dI", revers_back_color); 898 } 899 900 901 /* 902 * Switch to virtual terminal #arg. 903 */ 904 905 static void 906 set_console(char *arg) 907 { 908 int n; 909 910 if(!arg || strspn(arg,"0123456789") != strlen(arg)) { 911 revert(); 912 errx(1, "bad console number"); 913 } 914 915 n = atoi(arg); 916 917 if (n < 1 || n > 16) { 918 revert(); 919 errx(1, "console number out of range"); 920 } else if (ioctl(0, VT_ACTIVATE, n) == -1) { 921 revert(); 922 errc(1, errno, "switching vty"); 923 } 924 } 925 926 927 /* 928 * Sets the border color. 929 */ 930 931 static void 932 set_border_color(char *arg) 933 { 934 int color; 935 936 if ((color = get_color_number(arg)) != -1) { 937 fprintf(stderr, "\033[=%dA", color); 938 } 939 else 940 usage(); 941 } 942 943 static void 944 set_mouse_char(char *arg) 945 { 946 struct mouse_info mouse; 947 long l; 948 949 l = strtol(arg, NULL, 0); 950 951 if ((l < 0) || (l > UCHAR_MAX - 3)) { 952 revert(); 953 warnx("argument to -M must be 0 through %d", UCHAR_MAX - 3); 954 return; 955 } 956 957 mouse.operation = MOUSE_MOUSECHAR; 958 mouse.u.mouse_char = (int)l; 959 960 if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) { 961 revert(); 962 errc(1, errno, "setting mouse character"); 963 } 964 } 965 966 967 /* 968 * Show/hide the mouse. 969 */ 970 971 static void 972 set_mouse(char *arg) 973 { 974 struct mouse_info mouse; 975 976 if (!strcmp(arg, "on")) { 977 mouse.operation = MOUSE_SHOW; 978 } else if (!strcmp(arg, "off")) { 979 mouse.operation = MOUSE_HIDE; 980 } else { 981 revert(); 982 errx(1, "argument to -m must be either on or off"); 983 } 984 985 if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) { 986 revert(); 987 errc(1, errno, "%sing the mouse", 988 mouse.operation == MOUSE_SHOW ? "show" : "hid"); 989 } 990 } 991 992 993 static void 994 set_lockswitch(char *arg) 995 { 996 int data; 997 998 if (!strcmp(arg, "off")) { 999 data = 0x01; 1000 } else if (!strcmp(arg, "on")) { 1001 data = 0x02; 1002 } else { 1003 revert(); 1004 errx(1, "argument to -S must be either on or off"); 1005 } 1006 1007 if (ioctl(0, VT_LOCKSWITCH, &data) == -1) { 1008 revert(); 1009 errc(1, errno, "turning %s vty switching", 1010 data == 0x01 ? "off" : "on"); 1011 } 1012 } 1013 1014 1015 /* 1016 * Return the adapter name for a specified type. 1017 */ 1018 1019 static const char 1020 *adapter_name(int type) 1021 { 1022 static struct { 1023 int type; 1024 const char *name; 1025 } names[] = { 1026 { KD_MONO, "MDA" }, 1027 { KD_HERCULES, "Hercules" }, 1028 { KD_CGA, "CGA" }, 1029 { KD_EGA, "EGA" }, 1030 { KD_VGA, "VGA" }, 1031 { KD_TGA, "TGA" }, 1032 { -1, "Unknown" }, 1033 }; 1034 1035 int i; 1036 1037 for (i = 0; names[i].type != -1; ++i) 1038 if (names[i].type == type) 1039 break; 1040 return names[i].name; 1041 } 1042 1043 1044 /* 1045 * Show active VTY, ie current console number. 1046 */ 1047 1048 static void 1049 show_active_info(void) 1050 { 1051 1052 printf("%d\n", cur_info.active_vty); 1053 } 1054 1055 1056 /* 1057 * Show graphics adapter information. 1058 */ 1059 1060 static void 1061 show_adapter_info(void) 1062 { 1063 struct video_adapter_info ad; 1064 1065 ad.va_index = 0; 1066 1067 if (ioctl(0, CONS_ADPINFO, &ad) == -1) { 1068 revert(); 1069 errc(1, errno, "obtaining adapter information"); 1070 } 1071 1072 printf("fb%d:\n", ad.va_index); 1073 printf(" %.*s%d, type:%s%s (%d), flags:0x%x\n", 1074 (int)sizeof(ad.va_name), ad.va_name, ad.va_unit, 1075 (ad.va_flags & V_ADP_VESA) ? "VESA " : "", 1076 adapter_name(ad.va_type), ad.va_type, ad.va_flags); 1077 printf(" initial mode:%d, current mode:%d, BIOS mode:%d\n", 1078 ad.va_initial_mode, ad.va_mode, ad.va_initial_bios_mode); 1079 printf(" frame buffer window:0x%zx, buffer size:0x%zx\n", 1080 ad.va_window, ad.va_buffer_size); 1081 printf(" window size:0x%zx, origin:0x%x\n", 1082 ad.va_window_size, ad.va_window_orig); 1083 printf(" display start address (%d, %d), scan line width:%d\n", 1084 ad.va_disp_start.x, ad.va_disp_start.y, ad.va_line_width); 1085 printf(" reserved:0x%zx\n", ad.va_unused0); 1086 } 1087 1088 1089 /* 1090 * Show video mode information. 1091 */ 1092 1093 static void 1094 show_mode_info(void) 1095 { 1096 char buf[80]; 1097 struct video_info _info; 1098 int c; 1099 int mm; 1100 int mode; 1101 1102 printf(" mode# flags type size " 1103 "font window linear buffer\n"); 1104 printf("---------------------------------------" 1105 "---------------------------------------\n"); 1106 1107 memset(&_info, 0, sizeof(_info)); 1108 for (mode = 0; mode <= M_VESA_MODE_MAX; ++mode) { 1109 _info.vi_mode = mode; 1110 if (ioctl(0, CONS_MODEINFO, &_info)) 1111 continue; 1112 if (_info.vi_mode != mode) 1113 continue; 1114 if (_info.vi_width == 0 && _info.vi_height == 0 && 1115 _info.vi_cwidth == 0 && _info.vi_cheight == 0) 1116 continue; 1117 1118 printf("%3d (0x%03x)", mode, mode); 1119 printf(" 0x%08x", _info.vi_flags); 1120 if (_info.vi_flags & V_INFO_GRAPHICS) { 1121 c = 'G'; 1122 1123 if (_info.vi_mem_model == V_INFO_MM_PLANAR) 1124 snprintf(buf, sizeof(buf), "%dx%dx%d %d", 1125 _info.vi_width, _info.vi_height, 1126 _info.vi_depth, _info.vi_planes); 1127 else { 1128 switch (_info.vi_mem_model) { 1129 case V_INFO_MM_PACKED: 1130 mm = 'P'; 1131 break; 1132 case V_INFO_MM_DIRECT: 1133 mm = 'D'; 1134 break; 1135 case V_INFO_MM_CGA: 1136 mm = 'C'; 1137 break; 1138 case V_INFO_MM_HGC: 1139 mm = 'H'; 1140 break; 1141 case V_INFO_MM_VGAX: 1142 mm = 'V'; 1143 break; 1144 default: 1145 mm = ' '; 1146 break; 1147 } 1148 snprintf(buf, sizeof(buf), "%dx%dx%d %c", 1149 _info.vi_width, _info.vi_height, 1150 _info.vi_depth, mm); 1151 } 1152 } else { 1153 c = 'T'; 1154 1155 snprintf(buf, sizeof(buf), "%dx%d", 1156 _info.vi_width, _info.vi_height); 1157 } 1158 1159 printf(" %c %-15s", c, buf); 1160 snprintf(buf, sizeof(buf), "%dx%d", 1161 _info.vi_cwidth, _info.vi_cheight); 1162 printf(" %-5s", buf); 1163 printf(" 0x%05zx %2dk %2dk", 1164 _info.vi_window, (int)_info.vi_window_size/1024, 1165 (int)_info.vi_window_gran/1024); 1166 printf(" 0x%08zx %dk\n", 1167 _info.vi_buffer, (int)_info.vi_buffer_size/1024); 1168 } 1169 } 1170 1171 1172 static void 1173 show_info(char *arg) 1174 { 1175 1176 if (!strcmp(arg, "active")) { 1177 show_active_info(); 1178 } else if (!strcmp(arg, "adapter")) { 1179 show_adapter_info(); 1180 } else if (!strcmp(arg, "mode")) { 1181 show_mode_info(); 1182 } else { 1183 revert(); 1184 errx(1, "argument to -i must be active, adapter, or mode"); 1185 } 1186 } 1187 1188 1189 static void 1190 test_frame(void) 1191 { 1192 int i, fore; 1193 1194 fore = 15; 1195 1196 fprintf(stdout, "\033[=0G\n\n"); 1197 for (i=0; i<8; i++) { 1198 fprintf(stdout, "\033[=%dF\033[=0G %2d \033[=%dF%-16s" 1199 "\033[=%dF\033[=0G %2d \033[=%dF%-16s " 1200 "\033[=%dF %2d \033[=%dGBACKGROUND\033[=0G\n", 1201 fore, i, i, legal_colors[i], 1202 fore, i+8, i+8, legal_colors[i+8], 1203 fore, i, i); 1204 } 1205 fprintf(stdout, "\033[=%dF\033[=%dG\033[=%dH\033[=%dI\n", 1206 info.mv_norm.fore, info.mv_norm.back, 1207 info.mv_rev.fore, info.mv_rev.back); 1208 } 1209 1210 1211 /* 1212 * Snapshot the video memory of that terminal, using the CONS_SCRSHOT 1213 * ioctl, and writes the results to stdout either in the special 1214 * binary format (see manual page for details), or in the plain 1215 * text format. 1216 */ 1217 1218 static void 1219 dump_screen(int mode, int opt) 1220 { 1221 scrshot_t shot; 1222 vid_info_t _info; 1223 1224 _info.size = sizeof(_info); 1225 1226 if (ioctl(0, CONS_GETINFO, &_info) == -1) { 1227 revert(); 1228 errc(1, errno, "obtaining current video mode parameters"); 1229 return; 1230 } 1231 1232 shot.x = shot.y = 0; 1233 shot.xsize = _info.mv_csz; 1234 shot.ysize = _info.mv_rsz; 1235 if (opt == DUMP_ALL) 1236 shot.ysize += _info.mv_hsz; 1237 1238 shot.buf = alloca(shot.xsize * shot.ysize * sizeof(u_int16_t)); 1239 if (shot.buf == NULL) { 1240 revert(); 1241 errx(1, "failed to allocate memory for dump"); 1242 } 1243 1244 if (ioctl(0, CONS_SCRSHOT, &shot) == -1) { 1245 revert(); 1246 errc(1, errno, "dumping screen"); 1247 } 1248 1249 if (mode == DUMP_FMT_RAW) { 1250 printf("SCRSHOT_%c%c%c%c", DUMP_FMT_REV, 2, 1251 shot.xsize, shot.ysize); 1252 1253 fflush(stdout); 1254 1255 write(STDOUT_FILENO, shot.buf, 1256 shot.xsize * shot.ysize * sizeof(u_int16_t)); 1257 } else { 1258 char *line; 1259 int x, y; 1260 u_int16_t ch; 1261 1262 line = alloca(shot.xsize + 1); 1263 1264 if (line == NULL) { 1265 revert(); 1266 errx(1, "failed to allocate memory for line buffer"); 1267 } 1268 1269 for (y = 0; y < shot.ysize; y++) { 1270 for (x = 0; x < shot.xsize; x++) { 1271 ch = shot.buf[x + (y * shot.xsize)]; 1272 ch &= 0xff; 1273 1274 if (isprint(ch) == 0) 1275 ch = ' '; 1276 1277 line[x] = (char)ch; 1278 } 1279 1280 /* Trim trailing spaces */ 1281 1282 do { 1283 line[x--] = '\0'; 1284 } while (line[x] == ' ' && x != 0); 1285 1286 puts(line); 1287 } 1288 1289 fflush(stdout); 1290 } 1291 } 1292 1293 1294 /* 1295 * Set the console history buffer size. 1296 */ 1297 1298 static void 1299 set_history(char *opt) 1300 { 1301 int size; 1302 1303 size = atoi(opt); 1304 1305 if ((*opt == '\0') || size < 0) { 1306 revert(); 1307 errx(1, "argument must be a positive number"); 1308 } 1309 1310 if (ioctl(0, CONS_HISTORY, &size) == -1) { 1311 revert(); 1312 errc(1, errno, "setting history buffer size"); 1313 } 1314 } 1315 1316 1317 /* 1318 * Clear the console history buffer. 1319 */ 1320 1321 static void 1322 clear_history(void) 1323 { 1324 if (ioctl(0, CONS_CLRHIST) == -1) { 1325 revert(); 1326 errc(1, errno, "clearing history buffer"); 1327 } 1328 } 1329 1330 static void 1331 set_terminal_mode(char *arg) 1332 { 1333 1334 if (strcmp(arg, "xterm") == 0) 1335 fprintf(stderr, "\033[=T"); 1336 else if (strcmp(arg, "cons25") == 0) 1337 fprintf(stderr, "\033[=1T"); 1338 } 1339 1340 1341 int 1342 main(int argc, char **argv) 1343 { 1344 char *font, *type, *termmode; 1345 const char *opts; 1346 int dumpmod, dumpopt, opt; 1347 int reterr; 1348 1349 vt4_mode = is_vt4(); 1350 1351 init(); 1352 1353 info.size = sizeof(info); 1354 1355 if (ioctl(0, CONS_GETINFO, &info) == -1) 1356 err(1, "must be on a virtual console"); 1357 dumpmod = 0; 1358 dumpopt = DUMP_FBF; 1359 termmode = NULL; 1360 if (vt4_mode) 1361 opts = "b:Cc:fg:h:Hi:M:m:pPr:S:s:T:t:x"; 1362 else 1363 opts = "b:Cc:dfg:h:Hi:l:LM:m:pPr:S:s:T:t:x"; 1364 1365 while ((opt = getopt(argc, argv, opts)) != -1) 1366 switch(opt) { 1367 case 'b': 1368 set_border_color(optarg); 1369 break; 1370 case 'C': 1371 clear_history(); 1372 break; 1373 case 'c': 1374 set_cursor_type(optarg); 1375 break; 1376 case 'd': 1377 if (vt4_mode) 1378 break; 1379 print_scrnmap(); 1380 break; 1381 case 'f': 1382 optarg = nextarg(argc, argv, &optind, 'f', 0); 1383 if (optarg != NULL) { 1384 font = nextarg(argc, argv, &optind, 'f', 0); 1385 1386 if (font == NULL) { 1387 type = NULL; 1388 font = optarg; 1389 } else 1390 type = optarg; 1391 1392 load_font(type, font); 1393 } else { 1394 if (!vt4_mode) 1395 usage(); /* Switch syscons to ROM? */ 1396 1397 load_default_vt4font(); 1398 } 1399 break; 1400 case 'g': 1401 if (sscanf(optarg, "%dx%d", 1402 &vesa_cols, &vesa_rows) != 2) { 1403 revert(); 1404 warnx("incorrect geometry: %s", optarg); 1405 usage(); 1406 } 1407 break; 1408 case 'h': 1409 set_history(optarg); 1410 break; 1411 case 'H': 1412 dumpopt = DUMP_ALL; 1413 break; 1414 case 'i': 1415 show_info(optarg); 1416 break; 1417 case 'l': 1418 if (vt4_mode) 1419 break; 1420 load_scrnmap(optarg); 1421 break; 1422 case 'L': 1423 if (vt4_mode) 1424 break; 1425 load_default_scrnmap(); 1426 break; 1427 case 'M': 1428 set_mouse_char(optarg); 1429 break; 1430 case 'm': 1431 set_mouse(optarg); 1432 break; 1433 case 'p': 1434 dumpmod = DUMP_FMT_RAW; 1435 break; 1436 case 'P': 1437 dumpmod = DUMP_FMT_TXT; 1438 break; 1439 case 'r': 1440 get_reverse_colors(argc, argv, &optind); 1441 break; 1442 case 'S': 1443 set_lockswitch(optarg); 1444 break; 1445 case 's': 1446 set_console(optarg); 1447 break; 1448 case 'T': 1449 if (strcmp(optarg, "xterm") != 0 && 1450 strcmp(optarg, "cons25") != 0) 1451 usage(); 1452 termmode = optarg; 1453 break; 1454 case 't': 1455 set_screensaver_timeout(optarg); 1456 break; 1457 case 'x': 1458 hex = 1; 1459 break; 1460 default: 1461 usage(); 1462 } 1463 1464 if (dumpmod != 0) 1465 dump_screen(dumpmod, dumpopt); 1466 reterr = video_mode(argc, argv, &optind); 1467 get_normal_colors(argc, argv, &optind); 1468 1469 if (optind < argc && !strcmp(argv[optind], "show")) { 1470 test_frame(); 1471 optind++; 1472 } 1473 1474 video_mode(argc, argv, &optind); 1475 if (termmode != NULL) 1476 set_terminal_mode(termmode); 1477 1478 get_normal_colors(argc, argv, &optind); 1479 1480 if (colors_changed || video_mode_changed) { 1481 if (!(new_mode_info.vi_flags & V_INFO_GRAPHICS)) { 1482 if ((normal_back_color < 8) && (revers_back_color < 8)) { 1483 set_colors(); 1484 } else { 1485 revert(); 1486 errx(1, "bg color for text modes must be < 8"); 1487 } 1488 } else { 1489 set_colors(); 1490 } 1491 } 1492 1493 if ((optind != argc) || (argc == 1)) 1494 usage(); 1495 return reterr; 1496 } 1497 1498