gp2x.c 9.9 KB

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