input: move default bind handling to input core
[libpicofe.git] / linux / io.c
CommitLineData
720ee7f6 1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
720ee7f6 4
e31266dd 5#include "../common/emu.h"
046c4540 6#include "../common/menu.h"
b6eccda3 7#include "../common/plat.h"
c64c8d0e 8#include "../common/input.h"
b3972d82 9#include "sndout_oss.h"
d2f29611 10#include "version.h"
720ee7f6 11
15b55b4e 12#include "log_io.h"
13
b188c2b6 14int current_keys;
15unsigned char *PicoDraw2FB;
16
17#ifdef FBDEV
18
19#include "fbdev.h"
20
21#else
22
23#include <pthread.h>
24#include <semaphore.h>
25
d2f29611 26static int current_bpp = 16;
720ee7f6 27static int current_pal[256];
720ee7f6 28static const char *verstring = "PicoDrive " VERSION;
e31266dd 29static int scr_changed = 0, scr_w = SCREEN_WIDTH, scr_h = SCREEN_HEIGHT;
720ee7f6 30
82abf46f 31/* faking GP2X pad */
32enum { 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
a148806d 37static void key_press_event(int keycode)
720ee7f6 38{
a148806d 39 switch (keycode)
720ee7f6 40 {
13b692eb 41 case 111:
720ee7f6 42 case 0x62: current_keys |= GP2X_UP; break;
13b692eb 43 case 116:
720ee7f6 44 case 0x68: current_keys |= GP2X_DOWN; break;
13b692eb 45 case 113:
720ee7f6 46 case 0x64: current_keys |= GP2X_LEFT; break;
13b692eb 47 case 114:
720ee7f6 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
15b55b4e 60 case 0x2d: log_io_clear(); break; // k
61 case 0x2e: log_io_dump(); break; // l
6999d7eb 62 case 0x17: { // tab
63 extern int PicoReset(void);
64 PicoReset();
65 break;
66 }
720ee7f6 67 }
720ee7f6 68}
69
a148806d 70static void key_release_event(int keycode)
720ee7f6 71{
a148806d 72 switch (keycode)
720ee7f6 73 {
13b692eb 74 case 111:
720ee7f6 75 case 0x62: current_keys &= ~GP2X_UP; break;
13b692eb 76 case 116:
720ee7f6 77 case 0x68: current_keys &= ~GP2X_DOWN; break;
13b692eb 78 case 113:
720ee7f6 79 case 0x64: current_keys &= ~GP2X_LEFT; break;
13b692eb 80 case 114:
720ee7f6 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 }
720ee7f6 94}
95
a148806d 96/* --- */
97
98#include <X11/Xlib.h>
99#include <X11/Xutil.h>
100
101static Display *xlib_display;
102static Window xlib_window;
103static XImage *ximage;
104
105static void ximage_realloc(Display *display, Visual *visual)
73a85369 106{
a148806d 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);
73a85369 123}
124
a148806d 125static void xlib_update(void)
720ee7f6 126{
a148806d 127 Status xstatus;
720ee7f6 128
a148806d 129 XLockDisplay(xlib_display);
720ee7f6 130
a148806d 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);
720ee7f6 135
a148806d 136 XUnlockDisplay(xlib_display);
137}
720ee7f6 138
a148806d 139static 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;
720ee7f6 149
a148806d 150 XInitThreads();
73a85369 151
a148806d 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 }
720ee7f6 160
a148806d 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 }
720ee7f6 169
a148806d 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));
720ee7f6 173
a148806d 174 screen = DefaultScreen(display);
720ee7f6 175
a148806d 176 ximage_realloc(display, visual);
0d9652b5 177 sem_post(sem);
178
a148806d 179 display_width = DisplayWidth(display, screen);
180 display_height = DisplayHeight(display, screen);
0d9652b5 181
a148806d 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));
0d9652b5 189
a148806d 190 XStringListToTextProperty((char **)&verstring, 1, &windowName);
191 XSetWMName(display, win, &windowName);
0d9652b5 192
a148806d 193 XSelectInput(display, win, ExposureMask |
02e56d48 194 KeyPressMask | KeyReleaseMask |
a148806d 195 StructureNotifyMask);
0d9652b5 196
a148806d 197 XMapWindow(display, win);
720ee7f6 198
a148806d 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 }
720ee7f6 235}
236
a148806d 237static void xlib_init(void)
720ee7f6 238{
a148806d 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);
720ee7f6 249}
250
251/* --- */
252
e31266dd 253static void realloc_screen(void)
254{
b188c2b6 255 int size = scr_w * scr_h * 2;
b6eccda3 256 g_screen_width = g_menuscreen_w = scr_w;
257 g_screen_height = g_menuscreen_h = scr_h;
b188c2b6 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;
e31266dd 263 scr_changed = 0;
264}
265
b188c2b6 266void plat_video_flip(void)
720ee7f6 267{
a148806d 268 unsigned int *image;
269 int pixel_count, i;
720ee7f6 270
a148806d 271 if (ximage == NULL)
720ee7f6 272 return;
a148806d 273
274 pixel_count = g_screen_width * g_screen_height;
275 image = (void *)ximage->data;
720ee7f6 276
277 if (current_bpp == 8)
278 {
e31266dd 279 unsigned char *pixels = g_screen_ptr;
720ee7f6 280 int pix;
281
e31266dd 282 for (i = 0; i < pixel_count; i++)
720ee7f6 283 {
284 pix = current_pal[pixels[i]];
a148806d 285 image[i] = pix;
720ee7f6 286 }
287 }
288 else
289 {
e31266dd 290 unsigned short *pixels = g_screen_ptr;
720ee7f6 291
e31266dd 292 for (i = 0; i < pixel_count; i++)
720ee7f6 293 {
294 /* in: rrrr rggg gggb bbbb */
295 /* out: rrrr r000 gggg gg00 bbbb b000 */
a148806d 296 image[i] = (pixels[i] << 8) & 0xf80000;
297 image[i] |= (pixels[i] << 5) & 0x00fc00;
298 image[i] |= (pixels[i] << 3) & 0x0000f8;
720ee7f6 299 }
300 }
a148806d 301 xlib_update();
720ee7f6 302
a148806d 303 if (scr_changed) {
e31266dd 304 realloc_screen();
a148806d 305 ximage_realloc(xlib_display, DefaultVisual(xlib_display, 0));
b6eccda3 306
307 // propagate new ponters to renderers
308 plat_video_toggle_renderer(0, 0);
a148806d 309 }
720ee7f6 310}
311
b188c2b6 312void plat_video_wait_vsync(void)
313{
314}
315
316#endif // !FBDEV
317
046c4540 318void plat_early_init(void)
720ee7f6 319{
320}
321
046c4540 322void plat_init(void)
720ee7f6 323{
b188c2b6 324#ifdef FBDEV
325 int ret, w, h;
326 ret = vout_fbdev_init(&w, &h);
327 if (ret != 0)
328 exit(1);
b6eccda3 329 g_screen_width = g_menuscreen_w = w;
330 g_screen_height = g_menuscreen_h = h;
b188c2b6 331 g_menubg_ptr = realloc(g_menubg_ptr, w * g_screen_height * 2);
332 PicoDraw2FB = g_menubg_ptr;
333#else
046c4540 334 realloc_screen();
335 memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2);
b188c2b6 336 xlib_init();
337#endif
046c4540 338
339 // snd
340 sndout_oss_init();
720ee7f6 341}
342
046c4540 343void plat_finish(void)
720ee7f6 344{
b188c2b6 345#ifdef FBDEV
346 vout_fbdev_finish();
347#else
046c4540 348 free(g_screen_ptr);
b188c2b6 349#endif
046c4540 350 sndout_oss_exit();
720ee7f6 351}
352
720ee7f6 353/* misc */
69dfeea1 354int mp3_get_bitrate(void *f, int size)
046c4540 355{
356 return 128;
357}
358
69dfeea1 359void mp3_start_play(void *f, int pos)
046c4540 360{
361}
362
363void mp3_update(int *buffer, int length, int stereo)
364{
365}
366
c64c8d0e 367#include <linux/input.h>
368
369struct 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