c3a82376 |
1 | /* faking/emulating gp2x by using xlib */ |
cc68a136 |
2 | #include <stdio.h> |
3 | #include <stdlib.h> |
4 | #include <string.h> |
c3a82376 |
5 | #include <stdarg.h> |
cc68a136 |
6 | #include <pthread.h> |
69996cb7 |
7 | #include <semaphore.h> |
cc68a136 |
8 | |
9 | #include <unistd.h> |
10 | #include <sys/types.h> |
11 | #include <sys/stat.h> |
cc68a136 |
12 | #include <fcntl.h> |
13 | #include <errno.h> |
14 | |
cc68a136 |
15 | #include "../gp2x/version.h" |
e2de9939 |
16 | #include "../common/emu.h" |
bcd94522 |
17 | #include "../common/menu.h" |
18 | #include "../common/readpng.h" |
e5ab6faf |
19 | #include "sndout_oss.h" |
cc68a136 |
20 | |
4f65685b |
21 | #include "log_io.h" |
22 | |
217d08bc |
23 | unsigned long current_keys = 0; |
cc68a136 |
24 | static int current_bpp = 8; |
25 | static int current_pal[256]; |
cc68a136 |
26 | static const char *verstring = "PicoDrive " VERSION; |
e2de9939 |
27 | static int scr_changed = 0, scr_w = SCREEN_WIDTH, scr_h = SCREEN_HEIGHT; |
645ec129 |
28 | void *gp2x_screens[4]; |
cc68a136 |
29 | |
30 | // dummies |
f53f286a |
31 | int mix_32_to_16l_level; |
bcd94522 |
32 | int crashed_940 = 0; |
33 | int default_cpu_clock = 123; |
c18c15ae |
34 | void *gp2x_memregs = NULL; |
cc68a136 |
35 | |
991473ad |
36 | /* faking GP2X pad */ |
37 | enum { GP2X_UP=0x1, GP2X_LEFT=0x4, GP2X_DOWN=0x10, GP2X_RIGHT=0x40, |
38 | GP2X_START=1<<8, GP2X_SELECT=1<<9, GP2X_L=1<<10, GP2X_R=1<<11, |
39 | GP2X_A=1<<12, GP2X_B=1<<13, GP2X_X=1<<14, GP2X_Y=1<<15, |
40 | GP2X_VOL_UP=1<<23, GP2X_VOL_DOWN=1<<22, GP2X_PUSH=1<<27 }; |
41 | |
c3a82376 |
42 | static void key_press_event(int keycode) |
cc68a136 |
43 | { |
c3a82376 |
44 | switch (keycode) |
cc68a136 |
45 | { |
217d08bc |
46 | case 111: |
cc68a136 |
47 | case 0x62: current_keys |= GP2X_UP; break; |
217d08bc |
48 | case 116: |
cc68a136 |
49 | case 0x68: current_keys |= GP2X_DOWN; break; |
217d08bc |
50 | case 113: |
cc68a136 |
51 | case 0x64: current_keys |= GP2X_LEFT; break; |
217d08bc |
52 | case 114: |
cc68a136 |
53 | case 0x66: current_keys |= GP2X_RIGHT; break; |
54 | case 0x24: current_keys |= GP2X_START; break; // enter |
55 | case 0x23: current_keys |= GP2X_SELECT;break; // ] |
56 | case 0x34: current_keys |= GP2X_A; break; // z |
57 | case 0x35: current_keys |= GP2X_X; break; // x |
58 | case 0x36: current_keys |= GP2X_B; break; // c |
59 | case 0x37: current_keys |= GP2X_Y; break; // v |
60 | case 0x27: current_keys |= GP2X_L; break; // s |
61 | case 0x28: current_keys |= GP2X_R; break; // d |
62 | case 0x29: current_keys |= GP2X_PUSH; break; // f |
63 | case 0x18: current_keys |= GP2X_VOL_DOWN;break; // q |
64 | case 0x19: current_keys |= GP2X_VOL_UP;break; // w |
4f65685b |
65 | case 0x2d: log_io_clear(); break; // k |
66 | case 0x2e: log_io_dump(); break; // l |
1e6b5e39 |
67 | case 0x17: { // tab |
68 | extern int PicoReset(void); |
69 | PicoReset(); |
70 | break; |
71 | } |
cc68a136 |
72 | } |
cc68a136 |
73 | } |
74 | |
c3a82376 |
75 | static void key_release_event(int keycode) |
cc68a136 |
76 | { |
c3a82376 |
77 | switch (keycode) |
cc68a136 |
78 | { |
217d08bc |
79 | case 111: |
cc68a136 |
80 | case 0x62: current_keys &= ~GP2X_UP; break; |
217d08bc |
81 | case 116: |
cc68a136 |
82 | case 0x68: current_keys &= ~GP2X_DOWN; break; |
217d08bc |
83 | case 113: |
cc68a136 |
84 | case 0x64: current_keys &= ~GP2X_LEFT; break; |
217d08bc |
85 | case 114: |
cc68a136 |
86 | case 0x66: current_keys &= ~GP2X_RIGHT; break; |
87 | case 0x24: current_keys &= ~GP2X_START; break; // enter |
88 | case 0x23: current_keys &= ~GP2X_SELECT;break; // ] |
89 | case 0x34: current_keys &= ~GP2X_A; break; // z |
90 | case 0x35: current_keys &= ~GP2X_X; break; // x |
91 | case 0x36: current_keys &= ~GP2X_B; break; // c |
92 | case 0x37: current_keys &= ~GP2X_Y; break; // v |
93 | case 0x27: current_keys &= ~GP2X_L; break; // s |
94 | case 0x28: current_keys &= ~GP2X_R; break; // d |
95 | case 0x29: current_keys &= ~GP2X_PUSH; break; // f |
96 | case 0x18: current_keys &= ~GP2X_VOL_DOWN;break; // q |
97 | case 0x19: current_keys &= ~GP2X_VOL_UP;break; // w |
98 | } |
cc68a136 |
99 | } |
100 | |
c3a82376 |
101 | /* --- */ |
102 | |
103 | #include <X11/Xlib.h> |
104 | #include <X11/Xutil.h> |
105 | |
106 | static Display *xlib_display; |
107 | static Window xlib_window; |
108 | static XImage *ximage; |
109 | |
110 | static void ximage_realloc(Display *display, Visual *visual) |
19e1d027 |
111 | { |
c3a82376 |
112 | void *xlib_screen; |
113 | |
114 | XLockDisplay(xlib_display); |
115 | |
116 | if (ximage != NULL) |
117 | XDestroyImage(ximage); |
118 | ximage = NULL; |
119 | |
120 | xlib_screen = calloc(scr_w * scr_h, 4); |
121 | if (xlib_screen != NULL) |
122 | ximage = XCreateImage(display, visual, 24, ZPixmap, 0, |
123 | xlib_screen, scr_w, scr_h, 32, 0); |
124 | if (ximage == NULL) |
125 | fprintf(stderr, "failed to alloc ximage\n"); |
126 | |
127 | XUnlockDisplay(xlib_display); |
19e1d027 |
128 | } |
129 | |
c3a82376 |
130 | static void xlib_update(void) |
cc68a136 |
131 | { |
c3a82376 |
132 | Status xstatus; |
cc68a136 |
133 | |
c3a82376 |
134 | XLockDisplay(xlib_display); |
cc68a136 |
135 | |
c3a82376 |
136 | xstatus = XPutImage(xlib_display, xlib_window, DefaultGC(xlib_display, 0), ximage, |
137 | 0, 0, 0, 0, g_screen_width, g_screen_height); |
138 | if (xstatus != 0) |
139 | fprintf(stderr, "XPutImage %d\n", xstatus); |
cc68a136 |
140 | |
c3a82376 |
141 | XUnlockDisplay(xlib_display); |
142 | } |
cc68a136 |
143 | |
c3a82376 |
144 | static void *xlib_threadf(void *targ) |
145 | { |
146 | unsigned int width, height, display_width, display_height; |
147 | sem_t *sem = targ; |
148 | XTextProperty windowName; |
149 | Window win; |
150 | XEvent report; |
151 | Display *display; |
152 | Visual *visual; |
153 | int screen; |
cc68a136 |
154 | |
c3a82376 |
155 | XInitThreads(); |
19e1d027 |
156 | |
c3a82376 |
157 | xlib_display = display = XOpenDisplay(NULL); |
158 | if (display == NULL) |
159 | { |
160 | fprintf(stderr, "cannot connect to X server %s\n", |
161 | XDisplayName(NULL)); |
162 | sem_post(sem); |
163 | return NULL; |
164 | } |
cc68a136 |
165 | |
c3a82376 |
166 | visual = DefaultVisual(display, 0); |
167 | if (visual->class != TrueColor) |
168 | { |
169 | fprintf(stderr, "cannot handle non true color visual\n"); |
170 | XCloseDisplay(display); |
171 | sem_post(sem); |
172 | return NULL; |
173 | } |
cc68a136 |
174 | |
c3a82376 |
175 | printf("X vendor: %s, rel: %d, display: %s, protocol ver: %d.%d\n", ServerVendor(display), |
176 | VendorRelease(display), DisplayString(display), ProtocolVersion(display), |
177 | ProtocolRevision(display)); |
cc68a136 |
178 | |
c3a82376 |
179 | screen = DefaultScreen(display); |
cc68a136 |
180 | |
c3a82376 |
181 | ximage_realloc(display, visual); |
69996cb7 |
182 | sem_post(sem); |
183 | |
c3a82376 |
184 | display_width = DisplayWidth(display, screen); |
185 | display_height = DisplayHeight(display, screen); |
69996cb7 |
186 | |
c3a82376 |
187 | xlib_window = win = XCreateSimpleWindow(display, |
188 | RootWindow(display, screen), |
189 | display_width / 2 - scr_w / 2, |
190 | display_height / 2 - scr_h / 2, |
191 | scr_w + 2, scr_h + 2, 1, |
192 | BlackPixel(display, screen), |
193 | BlackPixel(display, screen)); |
69996cb7 |
194 | |
c3a82376 |
195 | XStringListToTextProperty((char **)&verstring, 1, &windowName); |
196 | XSetWMName(display, win, &windowName); |
69996cb7 |
197 | |
c3a82376 |
198 | XSelectInput(display, win, ExposureMask | |
30eb4622 |
199 | KeyPressMask | KeyReleaseMask | |
c3a82376 |
200 | StructureNotifyMask); |
69996cb7 |
201 | |
c3a82376 |
202 | XMapWindow(display, win); |
cc68a136 |
203 | |
c3a82376 |
204 | while (1) |
205 | { |
206 | XNextEvent(display, &report); |
207 | switch (report.type) |
208 | { |
209 | case Expose: |
210 | while (XCheckTypedEvent(display, Expose, &report)) |
211 | ; |
212 | xlib_update(); |
213 | break; |
214 | |
215 | case ConfigureNotify: |
216 | width = report.xconfigure.width; |
217 | height = report.xconfigure.height; |
218 | if (scr_w != width - 2 || scr_h != height - 2) { |
219 | scr_w = width - 2; |
220 | scr_h = height - 2; |
221 | scr_changed = 1; |
222 | } |
223 | break; |
224 | |
225 | case ButtonPress: |
226 | break; |
227 | |
228 | case KeyPress: |
229 | key_press_event(report.xkey.keycode); |
230 | break; |
231 | |
232 | case KeyRelease: |
233 | key_release_event(report.xkey.keycode); |
234 | break; |
235 | |
236 | default: |
237 | break; |
238 | } |
239 | } |
cc68a136 |
240 | } |
241 | |
c3a82376 |
242 | static void xlib_init(void) |
cc68a136 |
243 | { |
c3a82376 |
244 | pthread_t x_thread; |
245 | sem_t xlib_sem; |
246 | |
247 | sem_init(&xlib_sem, 0, 0); |
248 | |
249 | pthread_create(&x_thread, NULL, xlib_threadf, &xlib_sem); |
250 | pthread_detach(x_thread); |
251 | |
252 | sem_wait(&xlib_sem); |
253 | sem_destroy(&xlib_sem); |
cc68a136 |
254 | } |
255 | |
256 | /* --- */ |
257 | |
e2de9939 |
258 | static void realloc_screen(void) |
259 | { |
260 | void *old = g_screen_ptr; |
645ec129 |
261 | int i; |
e2de9939 |
262 | g_screen_width = scr_w; |
263 | g_screen_height = scr_h; |
e096cf88 |
264 | g_screen_ptr = calloc(g_screen_width * g_screen_height * 2, 1); |
e2de9939 |
265 | free(old); |
266 | scr_changed = 0; |
645ec129 |
267 | |
268 | for (i = 0; i < 4; i++) |
269 | gp2x_screens[i] = g_screen_ptr; |
e2de9939 |
270 | } |
271 | |
bcd94522 |
272 | /* gp2x/emu.c stuff, most to be rm'd */ |
273 | static void gp2x_video_flip_(void) |
cc68a136 |
274 | { |
c3a82376 |
275 | unsigned int *image; |
276 | int pixel_count, i; |
cc68a136 |
277 | |
c3a82376 |
278 | if (ximage == NULL) |
cc68a136 |
279 | return; |
c3a82376 |
280 | |
281 | pixel_count = g_screen_width * g_screen_height; |
282 | image = (void *)ximage->data; |
cc68a136 |
283 | |
284 | if (current_bpp == 8) |
285 | { |
e2de9939 |
286 | unsigned char *pixels = g_screen_ptr; |
cc68a136 |
287 | int pix; |
288 | |
e2de9939 |
289 | for (i = 0; i < pixel_count; i++) |
cc68a136 |
290 | { |
291 | pix = current_pal[pixels[i]]; |
c3a82376 |
292 | image[i] = pix; |
cc68a136 |
293 | } |
294 | } |
295 | else |
296 | { |
e2de9939 |
297 | unsigned short *pixels = g_screen_ptr; |
cc68a136 |
298 | |
e2de9939 |
299 | for (i = 0; i < pixel_count; i++) |
cc68a136 |
300 | { |
301 | /* in: rrrr rggg gggb bbbb */ |
302 | /* out: rrrr r000 gggg gg00 bbbb b000 */ |
c3a82376 |
303 | image[i] = (pixels[i] << 8) & 0xf80000; |
304 | image[i] |= (pixels[i] << 5) & 0x00fc00; |
305 | image[i] |= (pixels[i] << 3) & 0x0000f8; |
cc68a136 |
306 | } |
307 | } |
c3a82376 |
308 | xlib_update(); |
cc68a136 |
309 | |
c3a82376 |
310 | if (scr_changed) { |
e2de9939 |
311 | realloc_screen(); |
c3a82376 |
312 | ximage_realloc(xlib_display, DefaultVisual(xlib_display, 0)); |
313 | } |
cc68a136 |
314 | } |
315 | |
bcd94522 |
316 | static void gp2x_video_changemode_ll_(int bpp) |
317 | { |
318 | current_bpp = bpp; |
319 | } |
320 | |
321 | static void gp2x_video_setpalette_(int *pal, int len) |
e11c5548 |
322 | { |
bcd94522 |
323 | memcpy(current_pal, pal, len*4); |
324 | } |
325 | |
326 | void gp2x_memcpy_all_buffers(void *data, int offset, int len) |
327 | { |
328 | } |
329 | |
330 | void gp2x_memset_all_buffers(int offset, int byte, int len) |
331 | { |
332 | memset((char *)g_screen_ptr + offset, byte, len); |
e11c5548 |
333 | } |
334 | |
cc68a136 |
335 | void gp2x_video_changemode(int bpp) |
336 | { |
bcd94522 |
337 | gp2x_video_changemode_ll_(bpp); |
cc68a136 |
338 | } |
339 | |
bcd94522 |
340 | void gp2x_make_fb_bufferable(int yes) |
e11c5548 |
341 | { |
e11c5548 |
342 | } |
343 | |
bcd94522 |
344 | int soc_detect(void) |
cc68a136 |
345 | { |
bcd94522 |
346 | return 0; |
cc68a136 |
347 | } |
348 | |
bcd94522 |
349 | /* plat */ |
350 | static char menu_bg_buffer[320*240*2]; |
351 | char cpu_clk_name[16] = "GP2X CPU clocks"; |
352 | |
353 | void plat_video_menu_enter(int is_rom_loaded) |
cc68a136 |
354 | { |
bcd94522 |
355 | if (is_rom_loaded) |
356 | { |
357 | // darken the active framebuffer |
358 | memset(g_screen_ptr, 0, 320*8*2); |
359 | menu_darken_bg((char *)g_screen_ptr + 320*8*2, 320*224, 1); |
360 | memset((char *)g_screen_ptr + 320*232*2, 0, 320*8*2); |
361 | } |
362 | else |
363 | { |
27701801 |
364 | char buff[256]; |
365 | |
bcd94522 |
366 | // should really only happen once, on startup.. |
27701801 |
367 | emu_make_path(buff, "skin/background.png", sizeof(buff)); |
368 | if (readpng(g_screen_ptr, buff, READPNG_BG) < 0) |
369 | memset(g_screen_ptr, 0, 320*240*2); |
bcd94522 |
370 | } |
371 | |
372 | memcpy(menu_bg_buffer, g_screen_ptr, 320*240*2); |
373 | |
374 | // switch to 16bpp |
375 | gp2x_video_changemode_ll_(16); |
376 | gp2x_video_flip_(); |
cc68a136 |
377 | } |
378 | |
bcd94522 |
379 | void plat_video_menu_begin(void) |
cc68a136 |
380 | { |
bcd94522 |
381 | memcpy(g_screen_ptr, menu_bg_buffer, 320*240*2); |
cc68a136 |
382 | } |
383 | |
bcd94522 |
384 | void plat_video_menu_end(void) |
85a4b5a4 |
385 | { |
bcd94522 |
386 | gp2x_video_flip_(); |
85a4b5a4 |
387 | } |
388 | |
bcd94522 |
389 | void plat_validate_config(void) |
cc68a136 |
390 | { |
bcd94522 |
391 | // PicoOpt &= ~POPT_EXT_FM; |
cc68a136 |
392 | } |
393 | |
bcd94522 |
394 | void plat_early_init(void) |
cc68a136 |
395 | { |
396 | } |
397 | |
bcd94522 |
398 | void plat_init(void) |
cc68a136 |
399 | { |
bcd94522 |
400 | realloc_screen(); |
401 | memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2); |
402 | |
403 | // snd |
404 | sndout_oss_init(); |
405 | |
c3a82376 |
406 | xlib_init(); |
cc68a136 |
407 | } |
408 | |
bcd94522 |
409 | void plat_finish(void) |
cc68a136 |
410 | { |
bcd94522 |
411 | free(g_screen_ptr); |
412 | sndout_oss_exit(); |
cc68a136 |
413 | } |
414 | |
bcd94522 |
415 | /* nasty */ |
416 | static void do_nothing() |
cc68a136 |
417 | { |
cc68a136 |
418 | } |
419 | |
bcd94522 |
420 | void *gp2x_video_flip = gp2x_video_flip_; |
421 | void *gp2x_video_flip2 = gp2x_video_flip_; |
422 | void *gp2x_video_changemode_ll = gp2x_video_changemode_ll_; |
423 | void *gp2x_video_setpalette = gp2x_video_setpalette_; |
424 | |
425 | void *gp2x_video_RGB_setscaling = do_nothing; |
426 | void *gp2x_video_wait_vsync = do_nothing; |
427 | void *gp2x_set_cpuclk = do_nothing; |
45f2f245 |
428 | void *gp2x_read_battery = do_nothing; |
bcd94522 |
429 | void *set_lcd_custom_rate = do_nothing; |
430 | void *unset_lcd_custom_rate = do_nothing; |
431 | void *set_lcd_gamma = do_nothing; |
432 | void *set_ram_timings = do_nothing; |
433 | void *unset_ram_timings = do_nothing; |
434 | |
435 | /* joy */ |
436 | int gp2x_touchpad_read(int *x, int *y) |
cc68a136 |
437 | { |
bcd94522 |
438 | return -1; |
cc68a136 |
439 | } |
440 | |
cc68a136 |
441 | /* misc */ |
442 | void spend_cycles(int c) |
443 | { |
46969540 |
444 | usleep(c/200); |
cc68a136 |
445 | } |
446 | |
bcd94522 |
447 | int mp3_get_bitrate(FILE *f, int size) |
448 | { |
449 | return 128; |
450 | } |
451 | |
452 | void mp3_start_play(FILE *f, int pos) |
453 | { |
454 | } |
455 | |
456 | void mp3_update(int *buffer, int length, int stereo) |
457 | { |
458 | } |
459 | |
ca61ee42 |
460 | /* lprintf */ |
461 | void lprintf(const char *fmt, ...) |
462 | { |
463 | va_list vl; |
cc68a136 |
464 | |
ca61ee42 |
465 | va_start(vl, fmt); |
466 | vprintf(fmt, vl); |
467 | va_end(vl); |
468 | } |
cc68a136 |
469 | |