replace gtk frontend with xlib
[libpicofe.git] / linux / gp2x.c
... / ...
CommitLineData
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
23unsigned long current_keys = 0;
24static int current_bpp = 8;
25static int current_pal[256];
26static const char *verstring = "PicoDrive " VERSION;
27static int scr_changed = 0, scr_w = SCREEN_WIDTH, scr_h = SCREEN_HEIGHT;
28void *gp2x_screens[4];
29
30// dummies
31int mix_32_to_16l_level;
32int crashed_940 = 0;
33int default_cpu_clock = 123;
34void *gp2x_memregs = NULL;
35
36/* faking GP2X pad */
37enum { 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
42static 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
75static 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
106static Display *xlib_display;
107static Window xlib_window;
108static XImage *ximage;
109
110static 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
130static 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
144static 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 |
200 ButtonPressMask |
201 StructureNotifyMask);
202
203 XMapWindow(display, win);
204
205 while (1)
206 {
207 XNextEvent(display, &report);
208 switch (report.type)
209 {
210 case Expose:
211 while (XCheckTypedEvent(display, Expose, &report))
212 ;
213 xlib_update();
214 break;
215
216 case ConfigureNotify:
217 width = report.xconfigure.width;
218 height = report.xconfigure.height;
219 if (scr_w != width - 2 || scr_h != height - 2) {
220 scr_w = width - 2;
221 scr_h = height - 2;
222 scr_changed = 1;
223 }
224 break;
225
226 case ButtonPress:
227 break;
228
229 case KeyPress:
230 key_press_event(report.xkey.keycode);
231 break;
232
233 case KeyRelease:
234 key_release_event(report.xkey.keycode);
235 break;
236
237 default:
238 break;
239 }
240 }
241}
242
243static void xlib_init(void)
244{
245 pthread_t x_thread;
246 sem_t xlib_sem;
247
248 sem_init(&xlib_sem, 0, 0);
249
250 pthread_create(&x_thread, NULL, xlib_threadf, &xlib_sem);
251 pthread_detach(x_thread);
252
253 sem_wait(&xlib_sem);
254 sem_destroy(&xlib_sem);
255}
256
257/* --- */
258
259static void realloc_screen(void)
260{
261 void *old = g_screen_ptr;
262 int i;
263 g_screen_width = scr_w;
264 g_screen_height = scr_h;
265 g_screen_ptr = calloc(g_screen_width * g_screen_height * 2, 1);
266 free(old);
267 scr_changed = 0;
268
269 for (i = 0; i < 4; i++)
270 gp2x_screens[i] = g_screen_ptr;
271}
272
273/* gp2x/emu.c stuff, most to be rm'd */
274static void gp2x_video_flip_(void)
275{
276 unsigned int *image;
277 int pixel_count, i;
278
279 if (ximage == NULL)
280 return;
281
282 pixel_count = g_screen_width * g_screen_height;
283 image = (void *)ximage->data;
284
285 if (current_bpp == 8)
286 {
287 unsigned char *pixels = g_screen_ptr;
288 int pix;
289
290 for (i = 0; i < pixel_count; i++)
291 {
292 pix = current_pal[pixels[i]];
293 image[i] = pix;
294 }
295 }
296 else
297 {
298 unsigned short *pixels = g_screen_ptr;
299
300 for (i = 0; i < pixel_count; i++)
301 {
302 /* in: rrrr rggg gggb bbbb */
303 /* out: rrrr r000 gggg gg00 bbbb b000 */
304 image[i] = (pixels[i] << 8) & 0xf80000;
305 image[i] |= (pixels[i] << 5) & 0x00fc00;
306 image[i] |= (pixels[i] << 3) & 0x0000f8;
307 }
308 }
309 xlib_update();
310
311 if (scr_changed) {
312 realloc_screen();
313 ximage_realloc(xlib_display, DefaultVisual(xlib_display, 0));
314 }
315}
316
317static void gp2x_video_changemode_ll_(int bpp)
318{
319 current_bpp = bpp;
320}
321
322static void gp2x_video_setpalette_(int *pal, int len)
323{
324 memcpy(current_pal, pal, len*4);
325}
326
327void gp2x_memcpy_all_buffers(void *data, int offset, int len)
328{
329}
330
331void gp2x_memset_all_buffers(int offset, int byte, int len)
332{
333 memset((char *)g_screen_ptr + offset, byte, len);
334}
335
336void gp2x_video_changemode(int bpp)
337{
338 gp2x_video_changemode_ll_(bpp);
339}
340
341void gp2x_make_fb_bufferable(int yes)
342{
343}
344
345int soc_detect(void)
346{
347 return 0;
348}
349
350/* plat */
351static char menu_bg_buffer[320*240*2];
352char cpu_clk_name[16] = "GP2X CPU clocks";
353
354void plat_video_menu_enter(int is_rom_loaded)
355{
356 if (is_rom_loaded)
357 {
358 // darken the active framebuffer
359 memset(g_screen_ptr, 0, 320*8*2);
360 menu_darken_bg((char *)g_screen_ptr + 320*8*2, 320*224, 1);
361 memset((char *)g_screen_ptr + 320*232*2, 0, 320*8*2);
362 }
363 else
364 {
365 char buff[256];
366
367 // should really only happen once, on startup..
368 emu_make_path(buff, "skin/background.png", sizeof(buff));
369 if (readpng(g_screen_ptr, buff, READPNG_BG) < 0)
370 memset(g_screen_ptr, 0, 320*240*2);
371 }
372
373 memcpy(menu_bg_buffer, g_screen_ptr, 320*240*2);
374
375 // switch to 16bpp
376 gp2x_video_changemode_ll_(16);
377 gp2x_video_flip_();
378}
379
380void plat_video_menu_begin(void)
381{
382 memcpy(g_screen_ptr, menu_bg_buffer, 320*240*2);
383}
384
385void plat_video_menu_end(void)
386{
387 gp2x_video_flip_();
388}
389
390void plat_validate_config(void)
391{
392// PicoOpt &= ~POPT_EXT_FM;
393}
394
395void plat_early_init(void)
396{
397}
398
399void plat_init(void)
400{
401 realloc_screen();
402 memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2);
403
404 // snd
405 sndout_oss_init();
406
407 xlib_init();
408}
409
410void plat_finish(void)
411{
412 free(g_screen_ptr);
413 sndout_oss_exit();
414}
415
416/* nasty */
417static void do_nothing()
418{
419}
420
421void *gp2x_video_flip = gp2x_video_flip_;
422void *gp2x_video_flip2 = gp2x_video_flip_;
423void *gp2x_video_changemode_ll = gp2x_video_changemode_ll_;
424void *gp2x_video_setpalette = gp2x_video_setpalette_;
425
426void *gp2x_video_RGB_setscaling = do_nothing;
427void *gp2x_video_wait_vsync = do_nothing;
428void *gp2x_set_cpuclk = do_nothing;
429void *set_lcd_custom_rate = do_nothing;
430void *unset_lcd_custom_rate = do_nothing;
431void *set_lcd_gamma = do_nothing;
432void *set_ram_timings = do_nothing;
433void *unset_ram_timings = do_nothing;
434
435/* joy */
436int gp2x_touchpad_read(int *x, int *y)
437{
438 return -1;
439}
440
441/* misc */
442void spend_cycles(int c)
443{
444 usleep(c/200);
445}
446
447int mp3_get_bitrate(FILE *f, int size)
448{
449 return 128;
450}
451
452void mp3_start_play(FILE *f, int pos)
453{
454}
455
456void mp3_update(int *buffer, int length, int stereo)
457{
458}
459
460/* lprintf */
461void 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