rm unused file
[picodrive.git] / platform / linux / gp2x.c
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
9 #include <unistd.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <errno.h>
14
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"
20
21 #include "log_io.h"
22
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];
29
30 // dummies
31 int mix_32_to_16l_level;
32 int crashed_940 = 0;
33 int default_cpu_clock = 123;
34 void *gp2x_memregs = NULL;
35
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
42 static void key_press_event(int keycode)
43 {
44         switch (keycode)
45         {
46                 case 111:
47                 case 0x62: current_keys |= GP2X_UP;    break;
48                 case 116:
49                 case 0x68: current_keys |= GP2X_DOWN;  break;
50                 case 113:
51                 case 0x64: current_keys |= GP2X_LEFT;  break;
52                 case 114:
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
67                 case 0x17: { // tab
68                         extern int PicoReset(void);
69                         PicoReset();
70                         break;
71                 }
72         }
73 }
74
75 static void key_release_event(int keycode)
76 {
77         switch (keycode)
78         {
79                 case 111:
80                 case 0x62: current_keys &= ~GP2X_UP;    break;
81                 case 116:
82                 case 0x68: current_keys &= ~GP2X_DOWN;  break;
83                 case 113:
84                 case 0x64: current_keys &= ~GP2X_LEFT;  break;
85                 case 114:
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         }
99 }
100
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)
111 {
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);
128 }
129
130 static void xlib_update(void)
131 {
132         Status xstatus;
133
134         XLockDisplay(xlib_display);
135
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);
140
141         XUnlockDisplay(xlib_display);
142 }
143
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;
154
155         XInitThreads();
156
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         }
165
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         }
174
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));
178
179         screen = DefaultScreen(display);
180
181         ximage_realloc(display, visual);
182         sem_post(sem);
183
184         display_width = DisplayWidth(display, screen);
185         display_height = DisplayHeight(display, screen);
186
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));
194
195         XStringListToTextProperty((char **)&verstring, 1, &windowName);
196         XSetWMName(display, win, &windowName);
197
198         XSelectInput(display, win, ExposureMask |
199                         KeyPressMask | KeyReleaseMask |
200                         StructureNotifyMask);
201
202         XMapWindow(display, win);
203
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         }
240 }
241
242 static void xlib_init(void)
243 {
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);
254 }
255
256 /* --- */
257
258 static void realloc_screen(void)
259 {
260         void *old = g_screen_ptr;
261         int i;
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);
265         free(old);
266         scr_changed = 0;
267
268         for (i = 0; i < 4; i++)
269                 gp2x_screens[i] = g_screen_ptr;
270 }
271
272 /* gp2x/emu.c stuff, most to be rm'd */
273 static void gp2x_video_flip_(void)
274 {
275         unsigned int *image;
276         int pixel_count, i;
277
278         if (ximage == NULL)
279                 return;
280
281         pixel_count = g_screen_width * g_screen_height;
282         image = (void *)ximage->data;
283
284         if (current_bpp == 8)
285         {
286                 unsigned char *pixels = g_screen_ptr;
287                 int pix;
288
289                 for (i = 0; i < pixel_count; i++)
290                 {
291                         pix = current_pal[pixels[i]];
292                         image[i] = pix;
293                 }
294         }
295         else
296         {
297                 unsigned short *pixels = g_screen_ptr;
298
299                 for (i = 0; i < pixel_count; i++)
300                 {
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;
306                 }
307         }
308         xlib_update();
309
310         if (scr_changed) {
311                 realloc_screen();
312                 ximage_realloc(xlib_display, DefaultVisual(xlib_display, 0));
313         }
314 }
315
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)
322 {
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);
333 }
334
335 void gp2x_video_changemode(int bpp)
336 {
337         gp2x_video_changemode_ll_(bpp);
338 }
339
340 void gp2x_make_fb_bufferable(int yes)
341 {
342 }
343
344 int soc_detect(void)
345 {
346         return 0;
347 }
348
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)
354 {
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         {
364                 char buff[256];
365
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);
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_();
377 }
378
379 void plat_video_menu_begin(void)
380 {
381         memcpy(g_screen_ptr, menu_bg_buffer, 320*240*2);
382 }
383
384 void plat_video_menu_end(void)
385 {
386         gp2x_video_flip_();
387 }
388
389 void plat_validate_config(void)
390 {
391 //      PicoOpt &= ~POPT_EXT_FM;
392 }
393
394 void plat_early_init(void)
395 {
396 }
397
398 void plat_init(void)
399 {
400         realloc_screen();
401         memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2);
402
403         // snd
404         sndout_oss_init();
405
406         xlib_init();
407 }
408
409 void plat_finish(void)
410 {
411         free(g_screen_ptr);
412         sndout_oss_exit();
413 }
414
415 /* nasty */
416 static void do_nothing()
417 {
418 }
419
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;
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;
434
435 /* joy */
436 int gp2x_touchpad_read(int *x, int *y)
437 {
438         return -1;
439 }
440
441 /* misc */
442 void spend_cycles(int c)
443 {
444         usleep(c/200);
445 }
446
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
460 /* lprintf */
461 void lprintf(const char *fmt, ...)
462 {
463         va_list vl;
464
465         va_start(vl, fmt);
466         vprintf(fmt, vl);
467         va_end(vl);
468 }
469