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