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