root/morphix/trunk/ddcxinfo/modetest.c

Revision 2, 3.2 kB (checked in by nextime, 2 years ago)

Initial import, branching from morphix svn

Line 
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <sys/mman.h>
4 #include <sys/ioctl.h>
5 #include <linux/kd.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <string.h>
10 #include <assert.h>
11 #include <fcntl.h>
12 #include "vbe.h"
13 #ident "$Id: modetest.c 1815 2005-04-25 08:27:54Z alextreme $"
14
15 #define VESA_MODE 0x112
16
17 int main(int argc, char **argv)
18 {
19         const void *start_state;
20         u_int16_t start_mode;
21         struct vbe_info *info;
22         struct vbe_mode_info *mode_info;
23         char fontdata[32 * 512];
24         struct consolefontdesc font = {512, 32, fontdata};
25         int fd, tty_fd;
26         long tty_mode;
27         char *lfb;
28         int i, j;
29
30         /* Make sure we have VESA on this machine. */
31         info = vbe_get_vbe_info();
32         if(info == NULL) {
33                 fprintf(stderr, "VESA BIOS Extensions not detected.\n");
34                 exit(1);
35         }
36         fprintf(stderr, "Detected %c%c%c%c %d.%d\n",
37                 info->signature[0], info->signature[1],
38                 info->signature[2], info->signature[3],
39                 info->version[1], info->version[0]);
40
41         /* Open the current tty.  We'll need this for setting fonts. */
42         tty_fd = open("/dev/tty", O_RDWR);
43         if(tty_fd == -1) {
44                 perror("opening tty");
45                 exit(1);
46         }
47
48         /* Save the current VESA state and mode. */
49         start_state = vbe_save_svga_state();
50         start_mode = vbe_get_mode();
51
52         /* Make sure we don't have garbage values for these. */
53         assert(start_state);
54         assert(start_mode);
55         printf("Started in mode 0x%04x.\n", start_mode);
56
57         /* Make sure the desired mode is available. */
58         mode_info = vbe_get_mode_info(VBE_LINEAR_FRAMEBUFFER | VESA_MODE);
59         assert(mode_info != NULL);
60         assert(mode_info->linear_buffer_address != 0);
61
62         /* Memory-map the framebuffer for direct access (whee!) */
63         fd = open("/dev/mem", O_RDWR);
64         if(fd == -1) {
65                 perror("opening framebuffer");
66                 exit(1);
67         }
68         lfb = mmap(NULL,
69                    mode_info->bytes_per_scanline * mode_info->h,
70                    PROT_WRITE,
71                    MAP_SHARED,
72                    fd,
73                    mode_info->linear_buffer_address);
74         if(lfb == MAP_FAILED) {
75                 perror("memory-mapping framebuffer");
76                 exit(1);
77         }
78
79         /* Get the console's current mode and font for restoring when we're
80            finished messing with it. */
81         if(ioctl(tty_fd, KDGETMODE, &tty_mode) != 0) {
82                 perror("getting console mode");
83                 exit(1);
84         }
85         if(ioctl(tty_fd, GIO_FONTX, &font) != 0) {
86                 perror("saving console font");
87                 exit(1);
88         }
89
90         /* Tell the console we're going into graphics mode. */
91         if(ioctl(tty_fd, KDSETMODE, KD_GRAPHICS) != 0) {
92                 perror("preparing for graphics");
93                 exit(1);
94         }
95
96         /* Do the switch. */
97         fprintf(stderr, "Switching to mode 0x%04x: %dx%d, %d-bit...\n",
98                 VBE_LINEAR_FRAMEBUFFER | VESA_MODE,
99                 mode_info->w, mode_info->h, mode_info->bpp);
100         vbe_set_mode(VBE_LINEAR_FRAMEBUFFER | VESA_MODE);
101
102         /* Test pattern time! */
103         for(i = 0; i < mode_info->h; i++)
104         for(j = 0; j < mode_info->w; j++) {
105                 lfb[3 * (i * mode_info->w + j) + 0] = j % 256;
106                 lfb[3 * (i * mode_info->w + j) + 1] = j % 128;
107                 lfb[3 * (i * mode_info->w + j) + 2] = j % 64;
108         }
109
110         /* Pause to admire the display. */
111         sleep(10);
112
113         /* Restore the original video mode, hardware settings,
114            VT mode and font. */
115         vbe_set_mode(start_mode);
116         vbe_restore_svga_state(start_state);
117         fprintf(stderr, "Back to normal.\n");
118
119         if(ioctl(tty_fd, KDSETMODE, tty_mode) != 0) {
120                 perror("switching vt back to normal mode");
121         }
122         if(ioctl(tty_fd, PIO_FONTX, &font) != 0) {
123                 perror("restoring console font");
124                 system("setfont");
125         }
126
127         return 0;
128 }
Note: See TracBrowser for help on using the browser.