1 /* faking/emulating gp2x by using xlib */
10 #include <sys/types.h>
15 #include "../gp2x/version.h"
16 #include "../common/emu.h"
17 #include "../common/menu.h"
18 #include "../common/readpng.h"
19 #include "sndout_oss.h"
23 unsigned long current_keys = 0;
24 static int current_bpp = 8;
25 static int current_pal[256];
26 static const char *verstring = "PicoDrive " VERSION;
27 static int scr_changed = 0, scr_w = SCREEN_WIDTH, scr_h = SCREEN_HEIGHT;
28 void *gp2x_screens[4];
31 int mix_32_to_16l_level;
33 int default_cpu_clock = 123;
34 void *gp2x_memregs = NULL;
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 };
42 static void key_press_event(int keycode)
47 case 0x62: current_keys |= GP2X_UP; break;
49 case 0x68: current_keys |= GP2X_DOWN; break;
51 case 0x64: current_keys |= GP2X_LEFT; break;
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
65 case 0x2d: log_io_clear(); break; // k
66 case 0x2e: log_io_dump(); break; // l
68 extern int PicoReset(void);
75 static void key_release_event(int keycode)
80 case 0x62: current_keys &= ~GP2X_UP; break;
82 case 0x68: current_keys &= ~GP2X_DOWN; break;
84 case 0x64: current_keys &= ~GP2X_LEFT; break;
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
103 #include <X11/Xlib.h>
104 #include <X11/Xutil.h>
106 static Display *xlib_display;
107 static Window xlib_window;
108 static XImage *ximage;
110 static void ximage_realloc(Display *display, Visual *visual)
114 XLockDisplay(xlib_display);
117 XDestroyImage(ximage);
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);
125 fprintf(stderr, "failed to alloc ximage\n");
127 XUnlockDisplay(xlib_display);
130 static void xlib_update(void)
134 XLockDisplay(xlib_display);
136 xstatus = XPutImage(xlib_display, xlib_window, DefaultGC(xlib_display, 0), ximage,
137 0, 0, 0, 0, g_screen_width, g_screen_height);
139 fprintf(stderr, "XPutImage %d\n", xstatus);
141 XUnlockDisplay(xlib_display);
144 static void *xlib_threadf(void *targ)
146 unsigned int width, height, display_width, display_height;
148 XTextProperty windowName;
157 xlib_display = display = XOpenDisplay(NULL);
160 fprintf(stderr, "cannot connect to X server %s\n",
166 visual = DefaultVisual(display, 0);
167 if (visual->class != TrueColor)
169 fprintf(stderr, "cannot handle non true color visual\n");
170 XCloseDisplay(display);
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));
179 screen = DefaultScreen(display);
181 ximage_realloc(display, visual);
184 display_width = DisplayWidth(display, screen);
185 display_height = DisplayHeight(display, screen);
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));
195 XStringListToTextProperty((char **)&verstring, 1, &windowName);
196 XSetWMName(display, win, &windowName);
198 XSelectInput(display, win, ExposureMask |
199 KeyPressMask | KeyReleaseMask |
200 StructureNotifyMask);
202 XMapWindow(display, win);
206 XNextEvent(display, &report);
210 while (XCheckTypedEvent(display, Expose, &report))
215 case ConfigureNotify:
216 width = report.xconfigure.width;
217 height = report.xconfigure.height;
218 if (scr_w != width - 2 || scr_h != height - 2) {
229 key_press_event(report.xkey.keycode);
233 key_release_event(report.xkey.keycode);
242 static void xlib_init(void)
247 sem_init(&xlib_sem, 0, 0);
249 pthread_create(&x_thread, NULL, xlib_threadf, &xlib_sem);
250 pthread_detach(x_thread);
253 sem_destroy(&xlib_sem);
258 static void realloc_screen(void)
260 void *old = g_screen_ptr;
262 g_screen_width = scr_w;
263 g_screen_height = scr_h;
264 g_screen_ptr = calloc(g_screen_width * g_screen_height * 2, 1);
268 for (i = 0; i < 4; i++)
269 gp2x_screens[i] = g_screen_ptr;
272 /* gp2x/emu.c stuff, most to be rm'd */
273 static void gp2x_video_flip_(void)
281 pixel_count = g_screen_width * g_screen_height;
282 image = (void *)ximage->data;
284 if (current_bpp == 8)
286 unsigned char *pixels = g_screen_ptr;
289 for (i = 0; i < pixel_count; i++)
291 pix = current_pal[pixels[i]];
297 unsigned short *pixels = g_screen_ptr;
299 for (i = 0; i < pixel_count; i++)
301 /* in: rrrr rggg gggb bbbb */
302 /* out: rrrr r000 gggg gg00 bbbb b000 */
303 image[i] = (pixels[i] << 8) & 0xf80000;
304 image[i] |= (pixels[i] << 5) & 0x00fc00;
305 image[i] |= (pixels[i] << 3) & 0x0000f8;
312 ximage_realloc(xlib_display, DefaultVisual(xlib_display, 0));
316 static void gp2x_video_changemode_ll_(int bpp)
321 static void gp2x_video_setpalette_(int *pal, int len)
323 memcpy(current_pal, pal, len*4);
326 void gp2x_memcpy_all_buffers(void *data, int offset, int len)
330 void gp2x_memset_all_buffers(int offset, int byte, int len)
332 memset((char *)g_screen_ptr + offset, byte, len);
335 void gp2x_video_changemode(int bpp)
337 gp2x_video_changemode_ll_(bpp);
340 void gp2x_make_fb_bufferable(int yes)
350 static char menu_bg_buffer[320*240*2];
351 char cpu_clk_name[16] = "GP2X CPU clocks";
353 void plat_video_menu_enter(int is_rom_loaded)
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);
366 // should really only happen once, on startup..
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);
372 memcpy(menu_bg_buffer, g_screen_ptr, 320*240*2);
375 gp2x_video_changemode_ll_(16);
379 void plat_video_menu_begin(void)
381 memcpy(g_screen_ptr, menu_bg_buffer, 320*240*2);
384 void plat_video_menu_end(void)
389 void plat_validate_config(void)
391 // PicoOpt &= ~POPT_EXT_FM;
394 void plat_early_init(void)
401 memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2);
409 void plat_finish(void)
416 static void do_nothing()
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_;
425 void *gp2x_video_RGB_setscaling = do_nothing;
426 void *gp2x_video_wait_vsync = do_nothing;
427 void *gp2x_set_cpuclk = do_nothing;
428 void *gp2x_read_battery = do_nothing;
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;
436 int gp2x_touchpad_read(int *x, int *y)
442 void spend_cycles(int c)
447 int mp3_get_bitrate(FILE *f, int size)
452 void mp3_start_play(FILE *f, int pos)
456 void mp3_update(int *buffer, int length, int stereo)
461 void lprintf(const char *fmt, ...)