8a61516101fa040adb6fbb111ab0058088906d1e
[bootcensus] / src / test / vbetest.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "video.h"
4 #include "asmops.h"
5 #include "keyb.h"
6 #include "psaux.h"
7 #include "contty.h"
8 #include "audio.h"
9
10 static void draw_cursor(int x, int y, uint16_t col);
11 static int click_sound_callback(void *buffer, int size, void *cls);
12
13 static uint16_t *framebuf;
14
15 #define CURSOR_XSZ      12
16 #define CURSOR_YSZ      16
17 static uint16_t cursor[] = {
18         0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
19         0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
20         0xffff, 0x0001, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
21         0xffff, 0x0001, 0x0001, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
22         0xffff, 0x0001, 0x0001, 0x0001, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
23         0xffff, 0x0001, 0x0001, 0x0001, 0x0001, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
24         0xffff, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
25         0xffff, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000,
26         0xffff, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0xffff, 0x0000, 0x0000, 0x0000,
27         0xffff, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0xffff, 0xffff, 0xffff, 0xffff, 0x0000, 0x0000,
28         0xffff, 0x0001, 0x0001, 0x0001, 0x0001, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
29         0xffff, 0x0001, 0x0001, 0xffff, 0x0001, 0x0001, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
30         0xffff, 0x0001, 0xffff, 0xffff, 0x0001, 0x0001, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
31         0xffff, 0xffff, 0x0000, 0x0000, 0xffff, 0x0001, 0x0001, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000,
32         0xffff, 0x0000, 0x0000, 0x0000, 0xffff, 0x0001, 0x0001, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000,
33         0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
34 };
35
36 static int click;
37
38 /* defined in sndsamples.s */
39 extern signed char snd_click[];
40 extern int snd_click_size;
41
42 int vbetest(void)
43 {
44         int i, j, nmodes, mx, my;
45         unsigned int st, prev_st = 0;
46         struct video_mode vi;
47         uint16_t *fbptr;
48
49         nmodes = video_mode_count();
50         printf("%d video modes found:\n", nmodes);
51         for(i=0; i<nmodes; i++) {
52                 if(video_mode_info(i, &vi) == -1) {
53                         continue;
54                 }
55                 printf(" %04x: %dx%d %d bpp", vi.mode, vi.width, vi.height, vi.bpp);
56                 if(vi.bpp > 8) {
57                         printf(" (%d%d%d)\n", vi.rbits, vi.gbits, vi.bbits);
58                 } else {
59                         putchar('\n');
60                 }
61         }
62
63         if(!(framebuf = set_video_mode(find_video_mode(640, 480, 16)))) {
64                 return -1;
65         }
66         get_color_bits(&vi.rbits, &vi.gbits, &vi.bbits);
67         get_color_shift(&vi.rshift, &vi.gshift, &vi.bshift);
68         get_color_mask(&vi.rmask, &vi.gmask, &vi.bmask);
69
70         fbptr = framebuf;
71         for(i=0; i<480; i++) {
72                 for(j=0; j<640; j++) {
73                         int xor = i^j;
74                         uint16_t r = xor & 0xff;
75                         uint16_t g = (xor << 1) & 0xff;
76                         uint16_t b = (xor << 2) & 0xff;
77
78                         r >>= 8 - vi.rbits;
79                         g >>= 8 - vi.gbits;
80                         b >>= 8 - vi.bbits;
81
82                         *fbptr++ = ((r << vi.rshift) & vi.rmask) | ((g << vi.gshift) & vi.gmask) |
83                                 ((b << vi.bshift) & vi.bmask);
84                 }
85         }
86
87         set_mouse_bounds(0, 0, 639, 479);
88
89         audio_set_callback(click_sound_callback, 0);
90
91         /* empty the kb queue */
92         while(kb_getkey() != -1);
93
94         for(;;) {
95                 if(kb_getkey() != -1) {
96                         break;
97                 }
98
99                 st = mouse_state(&mx, &my);
100
101                 for(i=0; i<3; i++) {
102                         unsigned int bit = 1 << i;
103                         if(((st & bit) ^ (prev_st & bit)) & (st & bit)) {
104                                 click = 1;
105                         }
106                 }
107                 if(click) {
108                         printf("click!\n");
109                         audio_play(22050, 1);
110                 }
111
112                 prev_st = st;
113
114                 draw_cursor(mx, my, st & 1 ? 0xf800 : (st & 2 ? 0x7e0 : (st & 4 ? 0x00ff : 0)));
115
116                 halt_cpu();
117         }
118
119         set_vga_mode(3);
120         con_clear();
121         return 0;
122 }
123
124 static void draw_cursor(int x, int y, uint16_t col)
125 {
126         static uint16_t saved[CURSOR_XSZ * CURSOR_YSZ];
127         static int saved_x = -1, saved_y, saved_w, saved_h;
128
129         int i, j, w, h;
130         uint16_t *dest, *src, *savp;
131
132         if(saved_x >= 0) {
133                 dest = framebuf + saved_y * 640 + saved_x;
134                 src = saved;
135
136                 for(i=0; i<saved_h; i++) {
137                         for(j=0; j<saved_w; j++) {
138                                 *dest++ = *src++;
139                         }
140                         src += CURSOR_XSZ - saved_w;
141                         dest += 640 - saved_w;
142                 }
143         }
144
145         dest = framebuf + y * 640 + x;
146         src = cursor;
147         savp = saved;
148
149         w = 640 - x;
150         if(w > CURSOR_XSZ) w = CURSOR_XSZ;
151         h = 480 - y;
152         if(h > CURSOR_YSZ) h = CURSOR_YSZ;
153
154         saved_x = x;
155         saved_y = y;
156         saved_w = w;
157         saved_h = h;
158
159         for(i=0; i<h; i++) {
160                 for(j=0; j<w; j++) {
161                         uint16_t c = *src++;
162                         *savp++ = *dest;
163                         if(c) {
164                                 if(c == 1) c = col;
165                                 *dest = c;
166                         }
167                         dest++;
168                 }
169                 src += CURSOR_XSZ - w;
170                 dest += 640 - w;
171                 savp += CURSOR_XSZ - w;
172         }
173 }
174
175 /* snd_click_size is < 65536 so we can just throw it all at once in there */
176 static int click_sound_callback(void *buffer, int size, void *cls)
177 {
178         if(click) {
179                 memcpy(buffer, snd_click, snd_click_size);
180                 click = 0;
181                 return snd_click_size;
182         }
183         return 0;
184 }