added makefile rule to create bootable CDs and removed a printf call in
[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                         audio_play(22050, 1);
109                 }
110
111                 prev_st = st;
112
113                 draw_cursor(mx, my, st & 1 ? 0xf800 : (st & 2 ? 0x7e0 : (st & 4 ? 0x00ff : 0)));
114
115                 halt_cpu();
116         }
117
118         set_vga_mode(3);
119         con_clear();
120         return 0;
121 }
122
123 static void draw_cursor(int x, int y, uint16_t col)
124 {
125         static uint16_t saved[CURSOR_XSZ * CURSOR_YSZ];
126         static int saved_x = -1, saved_y, saved_w, saved_h;
127
128         int i, j, w, h;
129         uint16_t *dest, *src, *savp;
130
131         if(saved_x >= 0) {
132                 dest = framebuf + saved_y * 640 + saved_x;
133                 src = saved;
134
135                 for(i=0; i<saved_h; i++) {
136                         for(j=0; j<saved_w; j++) {
137                                 *dest++ = *src++;
138                         }
139                         src += CURSOR_XSZ - saved_w;
140                         dest += 640 - saved_w;
141                 }
142         }
143
144         dest = framebuf + y * 640 + x;
145         src = cursor;
146         savp = saved;
147
148         w = 640 - x;
149         if(w > CURSOR_XSZ) w = CURSOR_XSZ;
150         h = 480 - y;
151         if(h > CURSOR_YSZ) h = CURSOR_YSZ;
152
153         saved_x = x;
154         saved_y = y;
155         saved_w = w;
156         saved_h = h;
157
158         for(i=0; i<h; i++) {
159                 for(j=0; j<w; j++) {
160                         uint16_t c = *src++;
161                         *savp++ = *dest;
162                         if(c) {
163                                 if(c == 1) c = col;
164                                 *dest = c;
165                         }
166                         dest++;
167                 }
168                 src += CURSOR_XSZ - w;
169                 dest += 640 - w;
170                 savp += CURSOR_XSZ - w;
171         }
172 }
173
174 /* snd_click_size is < 65536 so we can just throw it all at once in there */
175 static int click_sound_callback(void *buffer, int size, void *cls)
176 {
177         if(click) {
178                 memcpy(buffer, snd_click, snd_click_size);
179                 click = 0;
180                 return snd_click_size;
181         }
182         return 0;
183 }