2767e68136fa6d1586a7ae0dc89032536e5c803c
[libpicofe.git] / io.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "../common/emu.h"
6 #include "../common/menu.h"
7 #include "../common/plat.h"
8 #include "../common/input.h"
9 #include "sndout_oss.h"
10 #include "version.h"
11
12 #include "log_io.h"
13
14 int current_keys;
15 unsigned char *PicoDraw2FB;
16
17 #ifdef FBDEV
18
19 #include "fbdev.h"
20
21 #else
22
23 #include <pthread.h>
24 #include <semaphore.h>
25
26 static int current_bpp = 16;
27 static int current_pal[256];
28 static const char *verstring = "PicoDrive " VERSION;
29 static int scr_changed = 0, scr_w = SCREEN_WIDTH, scr_h = SCREEN_HEIGHT;
30
31 /* faking GP2X pad */
32 enum  { GP2X_UP=0x1,       GP2X_LEFT=0x4,       GP2X_DOWN=0x10,  GP2X_RIGHT=0x40,
33         GP2X_START=1<<8,   GP2X_SELECT=1<<9,    GP2X_L=1<<10,    GP2X_R=1<<11,
34         GP2X_A=1<<12,      GP2X_B=1<<13,        GP2X_X=1<<14,    GP2X_Y=1<<15,
35         GP2X_VOL_UP=1<<23, GP2X_VOL_DOWN=1<<22, GP2X_PUSH=1<<27 };
36
37 static void key_press_event(int keycode)
38 {
39         switch (keycode)
40         {
41                 case 111:
42                 case 0x62: current_keys |= GP2X_UP;    break;
43                 case 116:
44                 case 0x68: current_keys |= GP2X_DOWN;  break;
45                 case 113:
46                 case 0x64: current_keys |= GP2X_LEFT;  break;
47                 case 114:
48                 case 0x66: current_keys |= GP2X_RIGHT; break;
49                 case 0x24: current_keys |= GP2X_START; break; // enter
50                 case 0x23: current_keys |= GP2X_SELECT;break; // ]
51                 case 0x34: current_keys |= GP2X_A;     break; // z
52                 case 0x35: current_keys |= GP2X_X;     break; // x
53                 case 0x36: current_keys |= GP2X_B;     break; // c
54                 case 0x37: current_keys |= GP2X_Y;     break; // v
55                 case 0x27: current_keys |= GP2X_L;     break; // s
56                 case 0x28: current_keys |= GP2X_R;     break; // d
57                 case 0x29: current_keys |= GP2X_PUSH;  break; // f
58                 case 0x18: current_keys |= GP2X_VOL_DOWN;break; // q
59                 case 0x19: current_keys |= GP2X_VOL_UP;break; // w
60                 case 0x2d: log_io_clear(); break; // k
61                 case 0x2e: log_io_dump();  break; // l
62                 case 0x17: { // tab
63                         extern int PicoReset(void);
64                         PicoReset();
65                         break;
66                 }
67         }
68 }
69
70 static void key_release_event(int keycode)
71 {
72         switch (keycode)
73         {
74                 case 111:
75                 case 0x62: current_keys &= ~GP2X_UP;    break;
76                 case 116:
77                 case 0x68: current_keys &= ~GP2X_DOWN;  break;
78                 case 113:
79                 case 0x64: current_keys &= ~GP2X_LEFT;  break;
80                 case 114:
81                 case 0x66: current_keys &= ~GP2X_RIGHT; break;
82                 case 0x24: current_keys &= ~GP2X_START; break; // enter
83                 case 0x23: current_keys &= ~GP2X_SELECT;break; // ]
84                 case 0x34: current_keys &= ~GP2X_A;     break; // z
85                 case 0x35: current_keys &= ~GP2X_X;     break; // x
86                 case 0x36: current_keys &= ~GP2X_B;     break; // c
87                 case 0x37: current_keys &= ~GP2X_Y;     break; // v
88                 case 0x27: current_keys &= ~GP2X_L;     break; // s
89                 case 0x28: current_keys &= ~GP2X_R;     break; // d
90                 case 0x29: current_keys &= ~GP2X_PUSH;  break; // f
91                 case 0x18: current_keys &= ~GP2X_VOL_DOWN;break; // q
92                 case 0x19: current_keys &= ~GP2X_VOL_UP;break; // w
93         }
94 }
95
96 /* --- */
97
98 #include <X11/Xlib.h>
99 #include <X11/Xutil.h>
100
101 static Display *xlib_display;
102 static Window xlib_window;
103 static XImage *ximage;
104
105 static void ximage_realloc(Display *display, Visual *visual)
106 {
107         void *xlib_screen;
108
109         XLockDisplay(xlib_display);
110
111         if (ximage != NULL)
112                 XDestroyImage(ximage);
113         ximage = NULL;
114
115         xlib_screen = calloc(scr_w * scr_h, 4);
116         if (xlib_screen != NULL)
117                 ximage = XCreateImage(display, visual, 24, ZPixmap, 0,
118                                 xlib_screen, scr_w, scr_h, 32, 0);
119         if (ximage == NULL)
120                 fprintf(stderr, "failed to alloc ximage\n");
121
122         XUnlockDisplay(xlib_display);
123 }
124
125 static void xlib_update(void)
126 {
127         Status xstatus;
128
129         XLockDisplay(xlib_display);
130
131         xstatus = XPutImage(xlib_display, xlib_window, DefaultGC(xlib_display, 0), ximage,
132                 0, 0, 0, 0, g_screen_width, g_screen_height);
133         if (xstatus != 0)
134                 fprintf(stderr, "XPutImage %d\n", xstatus);
135
136         XUnlockDisplay(xlib_display);
137 }
138
139 static void *xlib_threadf(void *targ)
140 {
141         unsigned int width, height, display_width, display_height;
142         sem_t *sem = targ;
143         XTextProperty windowName;
144         Window win;
145         XEvent report;
146         Display *display;
147         Visual *visual;
148         int screen;
149
150         XInitThreads();
151
152         xlib_display = display = XOpenDisplay(NULL);
153         if (display == NULL)
154         {
155                 fprintf(stderr, "cannot connect to X server %s\n",
156                                 XDisplayName(NULL));
157                 sem_post(sem);
158                 return NULL;
159         }
160
161         visual = DefaultVisual(display, 0);
162         if (visual->class != TrueColor)
163         {
164                 fprintf(stderr, "cannot handle non true color visual\n");
165                 XCloseDisplay(display);
166                 sem_post(sem);
167                 return NULL;
168         }
169
170         printf("X vendor: %s, rel: %d, display: %s, protocol ver: %d.%d\n", ServerVendor(display),
171                 VendorRelease(display), DisplayString(display), ProtocolVersion(display),
172                 ProtocolRevision(display));
173
174         screen = DefaultScreen(display);
175
176         ximage_realloc(display, visual);
177         sem_post(sem);
178
179         display_width = DisplayWidth(display, screen);
180         display_height = DisplayHeight(display, screen);
181
182         xlib_window = win = XCreateSimpleWindow(display,
183                         RootWindow(display, screen),
184                         display_width / 2 - scr_w / 2,
185                         display_height / 2 - scr_h / 2,
186                         scr_w + 2, scr_h + 2, 1,
187                         BlackPixel(display, screen),
188                         BlackPixel(display, screen));
189
190         XStringListToTextProperty((char **)&verstring, 1, &windowName);
191         XSetWMName(display, win, &windowName);
192
193         XSelectInput(display, win, ExposureMask |
194                         KeyPressMask | KeyReleaseMask |
195                         StructureNotifyMask);
196
197         XMapWindow(display, win);
198
199         while (1)
200         {
201                 XNextEvent(display, &report);
202                 switch (report.type)
203                 {
204                         case Expose:
205                                 while (XCheckTypedEvent(display, Expose, &report))
206                                         ;
207                                 xlib_update();
208                                 break;
209
210                         case ConfigureNotify:
211                                 width = report.xconfigure.width;
212                                 height = report.xconfigure.height;
213                                 if (scr_w != width - 2 || scr_h != height - 2) {
214                                         scr_w = width - 2;
215                                         scr_h = height - 2;
216                                         scr_changed = 1;
217                                 }
218                                 break;
219
220                         case ButtonPress:
221                                 break;
222
223                         case KeyPress:
224                                 key_press_event(report.xkey.keycode);
225                                 break;
226
227                         case KeyRelease:
228                                 key_release_event(report.xkey.keycode);
229                                 break;
230
231                         default:
232                                 break;
233                 }
234         }
235 }
236
237 static void xlib_init(void)
238 {
239         pthread_t x_thread;
240         sem_t xlib_sem;
241
242         sem_init(&xlib_sem, 0, 0);
243
244         pthread_create(&x_thread, NULL, xlib_threadf, &xlib_sem);
245         pthread_detach(x_thread);
246
247         sem_wait(&xlib_sem);
248         sem_destroy(&xlib_sem);
249 }
250
251 /* --- */
252
253 static void realloc_screen(void)
254 {
255         int size = scr_w * scr_h * 2;
256         g_screen_width = g_menuscreen_w = scr_w;
257         g_screen_height = g_menuscreen_h = scr_h;
258         g_screen_ptr = realloc(g_screen_ptr, size);
259         g_menubg_ptr = realloc(g_menubg_ptr, size);
260         memset(g_screen_ptr, 0, size);
261         memset(g_menubg_ptr, 0, size);
262         PicoDraw2FB = g_menubg_ptr;
263         scr_changed = 0;
264 }
265
266 void plat_video_flip(void)
267 {
268         unsigned int *image;
269         int pixel_count, i;
270
271         if (ximage == NULL)
272                 return;
273
274         pixel_count = g_screen_width * g_screen_height;
275         image = (void *)ximage->data;
276
277         if (current_bpp == 8)
278         {
279                 unsigned char *pixels = g_screen_ptr;
280                 int pix;
281
282                 for (i = 0; i < pixel_count; i++)
283                 {
284                         pix = current_pal[pixels[i]];
285                         image[i] = pix;
286                 }
287         }
288         else
289         {
290                 unsigned short *pixels = g_screen_ptr;
291
292                 for (i = 0; i < pixel_count; i++)
293                 {
294                         /*  in:           rrrr rggg gggb bbbb */
295                         /* out: rrrr r000 gggg gg00 bbbb b000 */
296                         image[i]  = (pixels[i] << 8) & 0xf80000;
297                         image[i] |= (pixels[i] << 5) & 0x00fc00;
298                         image[i] |= (pixels[i] << 3) & 0x0000f8;
299                 }
300         }
301         xlib_update();
302
303         if (scr_changed) {
304                 realloc_screen();
305                 ximage_realloc(xlib_display, DefaultVisual(xlib_display, 0));
306
307                 // propagate new ponters to renderers
308                 plat_video_toggle_renderer(0, 0);
309         }
310 }
311
312 void plat_video_wait_vsync(void)
313 {
314 }
315
316 #endif // !FBDEV
317
318 void plat_early_init(void)
319 {
320 }
321
322 void plat_init(void)
323 {
324 #ifdef FBDEV
325         int ret, w, h;
326         ret = vout_fbdev_init(&w, &h);
327         if (ret != 0)
328                 exit(1);
329         g_screen_width = g_menuscreen_w = w;
330         g_screen_height = g_menuscreen_h = h;
331         g_menubg_ptr = realloc(g_menubg_ptr, w * g_screen_height * 2);
332         PicoDraw2FB = g_menubg_ptr;
333 #else
334         realloc_screen();
335         memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2);
336         xlib_init();
337 #endif
338
339         // snd
340         sndout_oss_init();
341 }
342
343 void plat_finish(void)
344 {
345 #ifdef FBDEV
346         vout_fbdev_finish();
347 #else
348         free(g_screen_ptr);
349 #endif
350         sndout_oss_exit();
351 }
352
353 /* misc */
354 int mp3_get_bitrate(void *f, int size)
355 {
356         return 128;
357 }
358
359 void mp3_start_play(void *f, int pos)
360 {
361 }
362
363 void mp3_update(int *buffer, int length, int stereo)
364 {
365 }
366
367 #include <linux/input.h>
368
369 struct in_default_bind in_evdev_defbinds[] =
370 {
371         /* MXYZ SACB RLDU */
372         { KEY_UP,       IN_BINDTYPE_PLAYER12, 0 },
373         { KEY_DOWN,     IN_BINDTYPE_PLAYER12, 1 },
374         { KEY_LEFT,     IN_BINDTYPE_PLAYER12, 2 },
375         { KEY_RIGHT,    IN_BINDTYPE_PLAYER12, 3 },
376         { KEY_S,        IN_BINDTYPE_PLAYER12, 4 },      /* B */
377         { KEY_D,        IN_BINDTYPE_PLAYER12, 5 },      /* C */
378         { KEY_A,        IN_BINDTYPE_PLAYER12, 6 },      /* A */
379         { KEY_ENTER,    IN_BINDTYPE_PLAYER12, 7 },
380         { KEY_BACKSLASH, IN_BINDTYPE_EMU, PEVB_MENU },
381         { 0, 0, 0 }
382 };
383