720ee7f6 |
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
3 | #include <string.h> |
a148806d |
4 | #include <stdarg.h> |
720ee7f6 |
5 | #include <pthread.h> |
0d9652b5 |
6 | #include <semaphore.h> |
720ee7f6 |
7 | |
8 | #include <unistd.h> |
9 | #include <sys/types.h> |
10 | #include <sys/stat.h> |
720ee7f6 |
11 | #include <fcntl.h> |
720ee7f6 |
12 | |
e31266dd |
13 | #include "../common/emu.h" |
046c4540 |
14 | #include "../common/menu.h" |
b3972d82 |
15 | #include "sndout_oss.h" |
d2f29611 |
16 | #include "version.h" |
720ee7f6 |
17 | |
15b55b4e |
18 | #include "log_io.h" |
19 | |
13b692eb |
20 | unsigned long current_keys = 0; |
d2f29611 |
21 | static int current_bpp = 16; |
720ee7f6 |
22 | static int current_pal[256]; |
720ee7f6 |
23 | static const char *verstring = "PicoDrive " VERSION; |
e31266dd |
24 | static int scr_changed = 0, scr_w = SCREEN_WIDTH, scr_h = SCREEN_HEIGHT; |
720ee7f6 |
25 | |
6ad8f1d6 |
26 | /* ifndef is for qemu build without video out */ |
27 | #ifndef ARM |
28 | |
82abf46f |
29 | /* faking GP2X pad */ |
30 | enum { GP2X_UP=0x1, GP2X_LEFT=0x4, GP2X_DOWN=0x10, GP2X_RIGHT=0x40, |
31 | GP2X_START=1<<8, GP2X_SELECT=1<<9, GP2X_L=1<<10, GP2X_R=1<<11, |
32 | GP2X_A=1<<12, GP2X_B=1<<13, GP2X_X=1<<14, GP2X_Y=1<<15, |
33 | GP2X_VOL_UP=1<<23, GP2X_VOL_DOWN=1<<22, GP2X_PUSH=1<<27 }; |
34 | |
a148806d |
35 | static void key_press_event(int keycode) |
720ee7f6 |
36 | { |
a148806d |
37 | switch (keycode) |
720ee7f6 |
38 | { |
13b692eb |
39 | case 111: |
720ee7f6 |
40 | case 0x62: current_keys |= GP2X_UP; break; |
13b692eb |
41 | case 116: |
720ee7f6 |
42 | case 0x68: current_keys |= GP2X_DOWN; break; |
13b692eb |
43 | case 113: |
720ee7f6 |
44 | case 0x64: current_keys |= GP2X_LEFT; break; |
13b692eb |
45 | case 114: |
720ee7f6 |
46 | case 0x66: current_keys |= GP2X_RIGHT; break; |
47 | case 0x24: current_keys |= GP2X_START; break; // enter |
48 | case 0x23: current_keys |= GP2X_SELECT;break; // ] |
49 | case 0x34: current_keys |= GP2X_A; break; // z |
50 | case 0x35: current_keys |= GP2X_X; break; // x |
51 | case 0x36: current_keys |= GP2X_B; break; // c |
52 | case 0x37: current_keys |= GP2X_Y; break; // v |
53 | case 0x27: current_keys |= GP2X_L; break; // s |
54 | case 0x28: current_keys |= GP2X_R; break; // d |
55 | case 0x29: current_keys |= GP2X_PUSH; break; // f |
56 | case 0x18: current_keys |= GP2X_VOL_DOWN;break; // q |
57 | case 0x19: current_keys |= GP2X_VOL_UP;break; // w |
15b55b4e |
58 | case 0x2d: log_io_clear(); break; // k |
59 | case 0x2e: log_io_dump(); break; // l |
6999d7eb |
60 | case 0x17: { // tab |
61 | extern int PicoReset(void); |
62 | PicoReset(); |
63 | break; |
64 | } |
720ee7f6 |
65 | } |
720ee7f6 |
66 | } |
67 | |
a148806d |
68 | static void key_release_event(int keycode) |
720ee7f6 |
69 | { |
a148806d |
70 | switch (keycode) |
720ee7f6 |
71 | { |
13b692eb |
72 | case 111: |
720ee7f6 |
73 | case 0x62: current_keys &= ~GP2X_UP; break; |
13b692eb |
74 | case 116: |
720ee7f6 |
75 | case 0x68: current_keys &= ~GP2X_DOWN; break; |
13b692eb |
76 | case 113: |
720ee7f6 |
77 | case 0x64: current_keys &= ~GP2X_LEFT; break; |
13b692eb |
78 | case 114: |
720ee7f6 |
79 | case 0x66: current_keys &= ~GP2X_RIGHT; break; |
80 | case 0x24: current_keys &= ~GP2X_START; break; // enter |
81 | case 0x23: current_keys &= ~GP2X_SELECT;break; // ] |
82 | case 0x34: current_keys &= ~GP2X_A; break; // z |
83 | case 0x35: current_keys &= ~GP2X_X; break; // x |
84 | case 0x36: current_keys &= ~GP2X_B; break; // c |
85 | case 0x37: current_keys &= ~GP2X_Y; break; // v |
86 | case 0x27: current_keys &= ~GP2X_L; break; // s |
87 | case 0x28: current_keys &= ~GP2X_R; break; // d |
88 | case 0x29: current_keys &= ~GP2X_PUSH; break; // f |
89 | case 0x18: current_keys &= ~GP2X_VOL_DOWN;break; // q |
90 | case 0x19: current_keys &= ~GP2X_VOL_UP;break; // w |
91 | } |
720ee7f6 |
92 | } |
93 | |
a148806d |
94 | /* --- */ |
95 | |
96 | #include <X11/Xlib.h> |
97 | #include <X11/Xutil.h> |
98 | |
99 | static Display *xlib_display; |
100 | static Window xlib_window; |
101 | static XImage *ximage; |
102 | |
103 | static void ximage_realloc(Display *display, Visual *visual) |
73a85369 |
104 | { |
a148806d |
105 | void *xlib_screen; |
106 | |
107 | XLockDisplay(xlib_display); |
108 | |
109 | if (ximage != NULL) |
110 | XDestroyImage(ximage); |
111 | ximage = NULL; |
112 | |
113 | xlib_screen = calloc(scr_w * scr_h, 4); |
114 | if (xlib_screen != NULL) |
115 | ximage = XCreateImage(display, visual, 24, ZPixmap, 0, |
116 | xlib_screen, scr_w, scr_h, 32, 0); |
117 | if (ximage == NULL) |
118 | fprintf(stderr, "failed to alloc ximage\n"); |
119 | |
120 | XUnlockDisplay(xlib_display); |
73a85369 |
121 | } |
122 | |
a148806d |
123 | static void xlib_update(void) |
720ee7f6 |
124 | { |
a148806d |
125 | Status xstatus; |
720ee7f6 |
126 | |
a148806d |
127 | XLockDisplay(xlib_display); |
720ee7f6 |
128 | |
a148806d |
129 | xstatus = XPutImage(xlib_display, xlib_window, DefaultGC(xlib_display, 0), ximage, |
130 | 0, 0, 0, 0, g_screen_width, g_screen_height); |
131 | if (xstatus != 0) |
132 | fprintf(stderr, "XPutImage %d\n", xstatus); |
720ee7f6 |
133 | |
a148806d |
134 | XUnlockDisplay(xlib_display); |
135 | } |
720ee7f6 |
136 | |
a148806d |
137 | static void *xlib_threadf(void *targ) |
138 | { |
139 | unsigned int width, height, display_width, display_height; |
140 | sem_t *sem = targ; |
141 | XTextProperty windowName; |
142 | Window win; |
143 | XEvent report; |
144 | Display *display; |
145 | Visual *visual; |
146 | int screen; |
720ee7f6 |
147 | |
a148806d |
148 | XInitThreads(); |
73a85369 |
149 | |
a148806d |
150 | xlib_display = display = XOpenDisplay(NULL); |
151 | if (display == NULL) |
152 | { |
153 | fprintf(stderr, "cannot connect to X server %s\n", |
154 | XDisplayName(NULL)); |
155 | sem_post(sem); |
156 | return NULL; |
157 | } |
720ee7f6 |
158 | |
a148806d |
159 | visual = DefaultVisual(display, 0); |
160 | if (visual->class != TrueColor) |
161 | { |
162 | fprintf(stderr, "cannot handle non true color visual\n"); |
163 | XCloseDisplay(display); |
164 | sem_post(sem); |
165 | return NULL; |
166 | } |
720ee7f6 |
167 | |
a148806d |
168 | printf("X vendor: %s, rel: %d, display: %s, protocol ver: %d.%d\n", ServerVendor(display), |
169 | VendorRelease(display), DisplayString(display), ProtocolVersion(display), |
170 | ProtocolRevision(display)); |
720ee7f6 |
171 | |
a148806d |
172 | screen = DefaultScreen(display); |
720ee7f6 |
173 | |
a148806d |
174 | ximage_realloc(display, visual); |
0d9652b5 |
175 | sem_post(sem); |
176 | |
a148806d |
177 | display_width = DisplayWidth(display, screen); |
178 | display_height = DisplayHeight(display, screen); |
0d9652b5 |
179 | |
a148806d |
180 | xlib_window = win = XCreateSimpleWindow(display, |
181 | RootWindow(display, screen), |
182 | display_width / 2 - scr_w / 2, |
183 | display_height / 2 - scr_h / 2, |
184 | scr_w + 2, scr_h + 2, 1, |
185 | BlackPixel(display, screen), |
186 | BlackPixel(display, screen)); |
0d9652b5 |
187 | |
a148806d |
188 | XStringListToTextProperty((char **)&verstring, 1, &windowName); |
189 | XSetWMName(display, win, &windowName); |
0d9652b5 |
190 | |
a148806d |
191 | XSelectInput(display, win, ExposureMask | |
02e56d48 |
192 | KeyPressMask | KeyReleaseMask | |
a148806d |
193 | StructureNotifyMask); |
0d9652b5 |
194 | |
a148806d |
195 | XMapWindow(display, win); |
720ee7f6 |
196 | |
a148806d |
197 | while (1) |
198 | { |
199 | XNextEvent(display, &report); |
200 | switch (report.type) |
201 | { |
202 | case Expose: |
203 | while (XCheckTypedEvent(display, Expose, &report)) |
204 | ; |
205 | xlib_update(); |
206 | break; |
207 | |
208 | case ConfigureNotify: |
209 | width = report.xconfigure.width; |
210 | height = report.xconfigure.height; |
211 | if (scr_w != width - 2 || scr_h != height - 2) { |
212 | scr_w = width - 2; |
213 | scr_h = height - 2; |
214 | scr_changed = 1; |
215 | } |
216 | break; |
217 | |
218 | case ButtonPress: |
219 | break; |
220 | |
221 | case KeyPress: |
222 | key_press_event(report.xkey.keycode); |
223 | break; |
224 | |
225 | case KeyRelease: |
226 | key_release_event(report.xkey.keycode); |
227 | break; |
228 | |
229 | default: |
230 | break; |
231 | } |
232 | } |
720ee7f6 |
233 | } |
234 | |
a148806d |
235 | static void xlib_init(void) |
720ee7f6 |
236 | { |
a148806d |
237 | pthread_t x_thread; |
238 | sem_t xlib_sem; |
239 | |
240 | sem_init(&xlib_sem, 0, 0); |
241 | |
242 | pthread_create(&x_thread, NULL, xlib_threadf, &xlib_sem); |
243 | pthread_detach(x_thread); |
244 | |
245 | sem_wait(&xlib_sem); |
246 | sem_destroy(&xlib_sem); |
720ee7f6 |
247 | } |
6ad8f1d6 |
248 | #endif // !ARM |
720ee7f6 |
249 | |
250 | /* --- */ |
251 | |
e31266dd |
252 | static void realloc_screen(void) |
253 | { |
254 | void *old = g_screen_ptr; |
255 | g_screen_width = scr_w; |
256 | g_screen_height = scr_h; |
44bfd52f |
257 | g_screen_ptr = calloc(g_screen_width * g_screen_height * 2, 1); |
e31266dd |
258 | free(old); |
259 | scr_changed = 0; |
260 | } |
261 | |
d2f29611 |
262 | void update_screen(void) |
720ee7f6 |
263 | { |
6ad8f1d6 |
264 | #ifndef ARM |
a148806d |
265 | unsigned int *image; |
266 | int pixel_count, i; |
720ee7f6 |
267 | |
a148806d |
268 | if (ximage == NULL) |
720ee7f6 |
269 | return; |
a148806d |
270 | |
271 | pixel_count = g_screen_width * g_screen_height; |
272 | image = (void *)ximage->data; |
720ee7f6 |
273 | |
274 | if (current_bpp == 8) |
275 | { |
e31266dd |
276 | unsigned char *pixels = g_screen_ptr; |
720ee7f6 |
277 | int pix; |
278 | |
e31266dd |
279 | for (i = 0; i < pixel_count; i++) |
720ee7f6 |
280 | { |
281 | pix = current_pal[pixels[i]]; |
a148806d |
282 | image[i] = pix; |
720ee7f6 |
283 | } |
284 | } |
285 | else |
286 | { |
e31266dd |
287 | unsigned short *pixels = g_screen_ptr; |
720ee7f6 |
288 | |
e31266dd |
289 | for (i = 0; i < pixel_count; i++) |
720ee7f6 |
290 | { |
291 | /* in: rrrr rggg gggb bbbb */ |
292 | /* out: rrrr r000 gggg gg00 bbbb b000 */ |
a148806d |
293 | image[i] = (pixels[i] << 8) & 0xf80000; |
294 | image[i] |= (pixels[i] << 5) & 0x00fc00; |
295 | image[i] |= (pixels[i] << 3) & 0x0000f8; |
720ee7f6 |
296 | } |
297 | } |
a148806d |
298 | xlib_update(); |
720ee7f6 |
299 | |
a148806d |
300 | if (scr_changed) { |
e31266dd |
301 | realloc_screen(); |
a148806d |
302 | ximage_realloc(xlib_display, DefaultVisual(xlib_display, 0)); |
303 | } |
6ad8f1d6 |
304 | #endif |
720ee7f6 |
305 | } |
306 | |
046c4540 |
307 | void plat_early_init(void) |
720ee7f6 |
308 | { |
309 | } |
310 | |
046c4540 |
311 | void plat_init(void) |
720ee7f6 |
312 | { |
046c4540 |
313 | realloc_screen(); |
314 | memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2); |
315 | |
316 | // snd |
317 | sndout_oss_init(); |
318 | |
6ad8f1d6 |
319 | #ifndef ARM |
a148806d |
320 | xlib_init(); |
6ad8f1d6 |
321 | #endif |
720ee7f6 |
322 | } |
323 | |
046c4540 |
324 | void plat_finish(void) |
720ee7f6 |
325 | { |
046c4540 |
326 | free(g_screen_ptr); |
327 | sndout_oss_exit(); |
720ee7f6 |
328 | } |
329 | |
720ee7f6 |
330 | /* misc */ |
046c4540 |
331 | int mp3_get_bitrate(FILE *f, int size) |
332 | { |
333 | return 128; |
334 | } |
335 | |
336 | void mp3_start_play(FILE *f, int pos) |
337 | { |
338 | } |
339 | |
340 | void mp3_update(int *buffer, int length, int stereo) |
341 | { |
342 | } |
343 | |
6245d5a0 |
344 | /* lprintf */ |
345 | void lprintf(const char *fmt, ...) |
346 | { |
347 | va_list vl; |
720ee7f6 |
348 | |
6245d5a0 |
349 | va_start(vl, fmt); |
350 | vprintf(fmt, vl); |
351 | va_end(vl); |
352 | } |
720ee7f6 |
353 | |