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