Make the platform code provide the key mapping
[picodrive.git] / platform / common / plat_sdl.c
1 /*
2  * PicoDrive
3  * (C) notaz, 2013
4  *
5  * This work is licensed under the terms of MAME license.
6  * See COPYING file in the top-level directory.
7  */
8
9 #include <stdio.h>
10
11 #include "../libpicofe/input.h"
12 #include "../libpicofe/plat_sdl.h"
13 #include "../libpicofe/in_sdl.h"
14 #include "../libpicofe/gl.h"
15 #include "emu.h"
16 #include "menu_pico.h"
17 #include "input_pico.h"
18 #include "version.h"
19
20 static void *shadow_fb;
21
22 const struct in_default_bind in_sdl_defbinds[] __attribute__((weak)) = {
23         { SDLK_UP,     IN_BINDTYPE_PLAYER12, GBTN_UP },
24         { SDLK_DOWN,   IN_BINDTYPE_PLAYER12, GBTN_DOWN },
25         { SDLK_LEFT,   IN_BINDTYPE_PLAYER12, GBTN_LEFT },
26         { SDLK_RIGHT,  IN_BINDTYPE_PLAYER12, GBTN_RIGHT },
27         { SDLK_z,      IN_BINDTYPE_PLAYER12, GBTN_A },
28         { SDLK_x,      IN_BINDTYPE_PLAYER12, GBTN_B },
29         { SDLK_c,      IN_BINDTYPE_PLAYER12, GBTN_C },
30         { SDLK_a,      IN_BINDTYPE_PLAYER12, GBTN_X },
31         { SDLK_s,      IN_BINDTYPE_PLAYER12, GBTN_Y },
32         { SDLK_d,      IN_BINDTYPE_PLAYER12, GBTN_Z },
33         { SDLK_RETURN, IN_BINDTYPE_PLAYER12, GBTN_START },
34         { SDLK_f,      IN_BINDTYPE_PLAYER12, GBTN_MODE },
35         { SDLK_ESCAPE, IN_BINDTYPE_EMU, PEVB_MENU },
36         { SDLK_F1,     IN_BINDTYPE_EMU, PEVB_STATE_SAVE },
37         { SDLK_F2,     IN_BINDTYPE_EMU, PEVB_STATE_LOAD },
38         { SDLK_F3,     IN_BINDTYPE_EMU, PEVB_SSLOT_PREV },
39         { SDLK_F4,     IN_BINDTYPE_EMU, PEVB_SSLOT_NEXT },
40         { SDLK_F5,     IN_BINDTYPE_EMU, PEVB_SWITCH_RND },
41         { SDLK_F6,     IN_BINDTYPE_EMU, PEVB_PICO_PPREV },
42         { SDLK_F7,     IN_BINDTYPE_EMU, PEVB_PICO_PNEXT },
43         { SDLK_F8,     IN_BINDTYPE_EMU, PEVB_PICO_SWINP },
44         { SDLK_BACKSPACE, IN_BINDTYPE_EMU, PEVB_FF },
45         { 0, 0, 0 }
46 };
47
48 const struct menu_keymap in_sdl_key_map[] __attribute__((weak)) =
49 {
50         { SDLK_UP,      PBTN_UP },
51         { SDLK_DOWN,    PBTN_DOWN },
52         { SDLK_LEFT,    PBTN_LEFT },
53         { SDLK_RIGHT,   PBTN_RIGHT },
54         { SDLK_RETURN,  PBTN_MOK },
55         { SDLK_ESCAPE,  PBTN_MBACK },
56         { SDLK_SEMICOLON,       PBTN_MA2 },
57         { SDLK_QUOTE,   PBTN_MA3 },
58         { SDLK_LEFTBRACKET,  PBTN_L },
59         { SDLK_RIGHTBRACKET, PBTN_R },
60 };
61
62 const struct menu_keymap in_sdl_joy_map[] __attribute__((weak)) =
63 {
64         { SDLK_UP,      PBTN_UP },
65         { SDLK_DOWN,    PBTN_DOWN },
66         { SDLK_LEFT,    PBTN_LEFT },
67         { SDLK_RIGHT,   PBTN_RIGHT },
68         /* joystick */
69         { SDLK_WORLD_0, PBTN_MOK },
70         { SDLK_WORLD_1, PBTN_MBACK },
71         { SDLK_WORLD_2, PBTN_MA2 },
72         { SDLK_WORLD_3, PBTN_MA3 },
73 };
74
75 static const struct in_pdata in_sdl_platform_data = {
76         .defbinds = in_sdl_defbinds,
77         .key_map = in_sdl_key_map,
78         .kmap_size = sizeof(in_sdl_key_map) / sizeof(in_sdl_key_map[0]),
79         .joy_map = in_sdl_joy_map,
80         .jmap_size = sizeof(in_sdl_joy_map) / sizeof(in_sdl_joy_map[0]),
81 };
82
83 /* YUV stuff */
84 static int yuv_ry[32], yuv_gy[32], yuv_by[32];
85 static unsigned char yuv_u[32 * 2], yuv_v[32 * 2];
86
87 void bgr_to_uyvy_init(void)
88 {
89   int i, v;
90
91   /* init yuv converter:
92     y0 = (int)((0.299f * r0) + (0.587f * g0) + (0.114f * b0));
93     y1 = (int)((0.299f * r1) + (0.587f * g1) + (0.114f * b1));
94     u = (int)(8 * 0.565f * (b0 - y0)) + 128;
95     v = (int)(8 * 0.713f * (r0 - y0)) + 128;
96   */
97   for (i = 0; i < 32; i++) {
98     yuv_ry[i] = (int)(0.299f * i * 65536.0f + 0.5f);
99     yuv_gy[i] = (int)(0.587f * i * 65536.0f + 0.5f);
100     yuv_by[i] = (int)(0.114f * i * 65536.0f + 0.5f);
101   }
102   for (i = -32; i < 32; i++) {
103     v = (int)(8 * 0.565f * i) + 128;
104     if (v < 0)
105       v = 0;
106     if (v > 255)
107       v = 255;
108     yuv_u[i + 32] = v;
109     v = (int)(8 * 0.713f * i) + 128;
110     if (v < 0)
111       v = 0;
112     if (v > 255)
113       v = 255;
114     yuv_v[i + 32] = v;
115   }
116 }
117
118 void rgb565_to_uyvy(void *d, const void *s, int pixels)
119 {
120   unsigned int *dst = d;
121   const unsigned short *src = s;
122   const unsigned char *yu = yuv_u + 32;
123   const unsigned char *yv = yuv_v + 32;
124   int r0, g0, b0, r1, g1, b1;
125   int y0, y1, u, v;
126
127   for (; pixels > 0; src += 2, dst++, pixels -= 2)
128   {
129     r0 = (src[0] >> 11) & 0x1f;
130     g0 = (src[0] >> 6) & 0x1f;
131     b0 =  src[0] & 0x1f;
132     r1 = (src[1] >> 11) & 0x1f;
133     g1 = (src[1] >> 6) & 0x1f;
134     b1 =  src[1] & 0x1f;
135     y0 = (yuv_ry[r0] + yuv_gy[g0] + yuv_by[b0]) >> 16;
136     y1 = (yuv_ry[r1] + yuv_gy[g1] + yuv_by[b1]) >> 16;
137     u = yu[b0 - y0];
138     v = yv[r0 - y0];
139     // valid Y range seems to be 16..235
140     y0 = 16 + 219 * y0 / 31;
141     y1 = 16 + 219 * y1 / 31;
142
143     *dst = (y1 << 24) | (v << 16) | (y0 << 8) | u;
144   }
145 }
146
147 void plat_video_flip(void)
148 {
149         if (plat_sdl_overlay != NULL) {
150                 SDL_Rect dstrect =
151                         { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
152
153                 SDL_LockYUVOverlay(plat_sdl_overlay);
154                 rgb565_to_uyvy(plat_sdl_overlay->pixels[0], shadow_fb,
155                                 g_screen_width * g_screen_height);
156                 SDL_UnlockYUVOverlay(plat_sdl_overlay);
157                 SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
158         }
159         else if (plat_sdl_gl_active) {
160                 gl_flip(shadow_fb, g_screen_width, g_screen_height);
161         }
162         else {
163                 if (SDL_MUSTLOCK(plat_sdl_screen))
164                         SDL_UnlockSurface(plat_sdl_screen);
165                 SDL_Flip(plat_sdl_screen);
166                 g_screen_ptr = plat_sdl_screen->pixels;
167                 PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);
168         }
169 }
170
171 void plat_video_wait_vsync(void)
172 {
173 }
174
175 void plat_video_menu_enter(int is_rom_loaded)
176 {
177         plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 0);
178         g_screen_ptr = shadow_fb;
179 }
180
181 void plat_video_menu_begin(void)
182 {
183         if (plat_sdl_overlay != NULL || plat_sdl_gl_active) {
184                 g_menuscreen_ptr = shadow_fb;
185         }
186         else {
187                 if (SDL_MUSTLOCK(plat_sdl_screen))
188                         SDL_LockSurface(plat_sdl_screen);
189                 g_menuscreen_ptr = plat_sdl_screen->pixels;
190         }
191 }
192
193 void plat_video_menu_end(void)
194 {
195         if (plat_sdl_overlay != NULL) {
196                 SDL_Rect dstrect =
197                         { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
198
199                 SDL_LockYUVOverlay(plat_sdl_overlay);
200                 rgb565_to_uyvy(plat_sdl_overlay->pixels[0], shadow_fb,
201                                 g_menuscreen_w * g_menuscreen_h);
202                 SDL_UnlockYUVOverlay(plat_sdl_overlay);
203
204                 SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
205         }
206         else if (plat_sdl_gl_active) {
207                 gl_flip(g_menuscreen_ptr, g_menuscreen_w, g_menuscreen_h);
208         }
209         else {
210                 if (SDL_MUSTLOCK(plat_sdl_screen))
211                         SDL_UnlockSurface(plat_sdl_screen);
212                 SDL_Flip(plat_sdl_screen);
213         }
214         g_menuscreen_ptr = NULL;
215
216 }
217
218 void plat_video_menu_leave(void)
219 {
220 }
221
222 void plat_video_loop_prepare(void)
223 {
224         plat_sdl_change_video_mode(g_screen_width, g_screen_height, 0);
225
226         if (plat_sdl_overlay != NULL || plat_sdl_gl_active) {
227                 g_screen_ptr = shadow_fb;
228         }
229         else {
230                 if (SDL_MUSTLOCK(plat_sdl_screen))
231                         SDL_LockSurface(plat_sdl_screen);
232                 g_screen_ptr = plat_sdl_screen->pixels;
233         }
234         PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);
235 }
236
237 void plat_early_init(void)
238 {
239 }
240
241 static void plat_sdl_quit(void)
242 {
243         // for now..
244         exit(1);
245 }
246
247 void plat_init(void)
248 {
249         int shadow_size;
250         int ret;
251
252         ret = plat_sdl_init();
253         if (ret != 0)
254                 exit(1);
255
256         plat_sdl_quit_cb = plat_sdl_quit;
257
258         SDL_WM_SetCaption("PicoDrive " VERSION, NULL);
259
260         g_menuscreen_w = plat_sdl_screen->w;
261         g_menuscreen_h = plat_sdl_screen->h;
262         g_menuscreen_ptr = NULL;
263
264         shadow_size = g_menuscreen_w * g_menuscreen_h * 2;
265         if (shadow_size < 320 * 480 * 2)
266                 shadow_size = 320 * 480 * 2;
267
268         shadow_fb = malloc(shadow_size);
269         g_menubg_ptr = malloc(shadow_size);
270         if (shadow_fb == NULL || g_menubg_ptr == NULL) {
271                 fprintf(stderr, "OOM\n");
272                 exit(1);
273         }
274
275         g_screen_width = 320;
276         g_screen_height = 240;
277         g_screen_ptr = shadow_fb;
278
279         in_sdl_init(&in_sdl_platform_data, plat_sdl_event_handler);
280         in_probe();
281
282         bgr_to_uyvy_init();
283 }
284
285 void plat_finish(void)
286 {
287         free(shadow_fb);
288         shadow_fb = NULL;
289         free(g_menubg_ptr);
290         g_menubg_ptr = NULL;
291         plat_sdl_finish();
292 }