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