This is litterally all that was required to build on Solaris 11. Portability FTW.
[pcsx_rearmed.git] / frontend / plat_sdl.c
CommitLineData
7badc935 1/*
418caf43 2 * (C) GraÅžvydas "notaz" Ignotas, 2011-2013
7badc935 3 *
4 * This work is licensed under the terms of any of these licenses
5 * (at your option):
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
8 * See the COPYING file in the top-level directory.
9 */
10
11#include <stdio.h>
12#include <SDL.h>
cc56203b 13
14#include "libpicofe/input.h"
15#include "libpicofe/in_sdl.h"
16#include "libpicofe/menu.h"
c9099d02 17#include "libpicofe/fonts.h"
5b9aa749 18#include "libpicofe/plat_sdl.h"
19#include "libpicofe/gl.h"
c82f907a 20#include "cspace.h"
b07c18e8 21#include "plugin_lib.h"
418caf43 22#include "plugin.h"
b07c18e8 23#include "main.h"
7badc935 24#include "plat.h"
25#include "revision.h"
26
b07c18e8 27static const struct in_default_bind in_sdl_defbinds[] = {
2e6189bc 28 { SDLK_UP, IN_BINDTYPE_PLAYER12, DKEY_UP },
29 { SDLK_DOWN, IN_BINDTYPE_PLAYER12, DKEY_DOWN },
30 { SDLK_LEFT, IN_BINDTYPE_PLAYER12, DKEY_LEFT },
31 { SDLK_RIGHT, IN_BINDTYPE_PLAYER12, DKEY_RIGHT },
32 { SDLK_d, IN_BINDTYPE_PLAYER12, DKEY_TRIANGLE },
33 { SDLK_z, IN_BINDTYPE_PLAYER12, DKEY_CROSS },
34 { SDLK_x, IN_BINDTYPE_PLAYER12, DKEY_CIRCLE },
35 { SDLK_s, IN_BINDTYPE_PLAYER12, DKEY_SQUARE },
36 { SDLK_v, IN_BINDTYPE_PLAYER12, DKEY_START },
37 { SDLK_c, IN_BINDTYPE_PLAYER12, DKEY_SELECT },
38 { SDLK_w, IN_BINDTYPE_PLAYER12, DKEY_L1 },
39 { SDLK_r, IN_BINDTYPE_PLAYER12, DKEY_R1 },
40 { SDLK_e, IN_BINDTYPE_PLAYER12, DKEY_L2 },
41 { SDLK_t, IN_BINDTYPE_PLAYER12, DKEY_R2 },
42 { SDLK_ESCAPE, IN_BINDTYPE_EMU, SACTION_ENTER_MENU },
456f1b86 43 { SDLK_F1, IN_BINDTYPE_EMU, SACTION_SAVE_STATE },
44 { SDLK_F2, IN_BINDTYPE_EMU, SACTION_LOAD_STATE },
45 { SDLK_F3, IN_BINDTYPE_EMU, SACTION_PREV_SSLOT },
46 { SDLK_F4, IN_BINDTYPE_EMU, SACTION_NEXT_SSLOT },
47 { SDLK_F5, IN_BINDTYPE_EMU, SACTION_TOGGLE_FSKIP },
48 { SDLK_F6, IN_BINDTYPE_EMU, SACTION_SCREENSHOT },
dde7da71 49 { SDLK_F7, IN_BINDTYPE_EMU, SACTION_TOGGLE_FPS },
456f1b86 50 { SDLK_F8, IN_BINDTYPE_EMU, SACTION_SWITCH_DISPMODE },
a8376201 51 { SDLK_F11, IN_BINDTYPE_EMU, SACTION_TOGGLE_FULLSCREEN },
dde7da71 52 { SDLK_BACKSPACE, IN_BINDTYPE_EMU, SACTION_FAST_FORWARD },
2e6189bc 53 { 0, 0, 0 }
7badc935 54};
55
bcfc48e3 56const struct menu_keymap in_sdl_key_map[] =
57{
58 { SDLK_UP, PBTN_UP },
59 { SDLK_DOWN, PBTN_DOWN },
60 { SDLK_LEFT, PBTN_LEFT },
61 { SDLK_RIGHT, PBTN_RIGHT },
62 { SDLK_RETURN, PBTN_MOK },
63 { SDLK_ESCAPE, PBTN_MBACK },
64 { SDLK_SEMICOLON, PBTN_MA2 },
65 { SDLK_QUOTE, PBTN_MA3 },
66 { SDLK_LEFTBRACKET, PBTN_L },
67 { SDLK_RIGHTBRACKET, PBTN_R },
68};
69
70const struct menu_keymap in_sdl_joy_map[] =
71{
72 { SDLK_UP, PBTN_UP },
73 { SDLK_DOWN, PBTN_DOWN },
74 { SDLK_LEFT, PBTN_LEFT },
75 { SDLK_RIGHT, PBTN_RIGHT },
76 /* joystick */
77 { SDLK_WORLD_0, PBTN_MOK },
78 { SDLK_WORLD_1, PBTN_MBACK },
79 { SDLK_WORLD_2, PBTN_MA2 },
80 { SDLK_WORLD_3, PBTN_MA3 },
81};
82
83static const struct in_pdata in_sdl_platform_data = {
84 .defbinds = in_sdl_defbinds,
85 .key_map = in_sdl_key_map,
86 .kmap_size = sizeof(in_sdl_key_map) / sizeof(in_sdl_key_map[0]),
87 .joy_map = in_sdl_joy_map,
88 .jmap_size = sizeof(in_sdl_joy_map) / sizeof(in_sdl_joy_map[0]),
89};
90
a8376201 91static int psx_w, psx_h;
5b9aa749 92static void *shadow_fb, *menubg_img;
93static int in_menu;
7badc935 94
418caf43 95static int change_video_mode(int force)
7badc935 96{
5b9aa749 97 int w, h;
a8376201 98
5b9aa749 99 if (in_menu) {
100 w = g_menuscreen_w;
101 h = g_menuscreen_h;
a8376201 102 }
5b9aa749 103 else {
104 w = psx_w;
105 h = psx_h;
a8376201 106 }
107
418caf43 108 return plat_sdl_change_video_mode(w, h, force);
109}
110
111static void resize_cb(int w, int h)
112{
113 // used by some plugins..
114 pl_rearmed_cbs.screen_w = w;
115 pl_rearmed_cbs.screen_h = h;
116 pl_rearmed_cbs.gles_display = gl_es_display;
117 pl_rearmed_cbs.gles_surface = gl_es_surface;
118 plugin_call_rearmed_cbs();
a8376201 119}
120
69e482e3 121static void quit_cb(void)
122{
123 emu_core_ask_exit();
124}
125
418caf43 126static void get_layer_pos(int *x, int *y, int *w, int *h)
127{
128 // always fill entire SDL window
129 *x = *y = 0;
130 *w = pl_rearmed_cbs.screen_w;
131 *h = pl_rearmed_cbs.screen_h;
132}
133
7badc935 134void plat_init(void)
135{
8e7632dd 136 int shadow_size;
5b9aa749 137 int ret;
7badc935 138
418caf43 139 plat_sdl_quit_cb = quit_cb;
140 plat_sdl_resize_cb = resize_cb;
141
5b9aa749 142 ret = plat_sdl_init();
143 if (ret != 0)
7badc935 144 exit(1);
a8376201 145
146 in_menu = 1;
7badc935 147 SDL_WM_SetCaption("PCSX-ReARMed " REV, NULL);
148
8e7632dd 149 shadow_size = g_menuscreen_w * g_menuscreen_h * 2;
150 if (shadow_size < 640 * 512 * 2)
151 shadow_size = 640 * 512 * 2;
152
153 shadow_fb = malloc(shadow_size);
154 menubg_img = malloc(shadow_size);
5b9aa749 155 if (shadow_fb == NULL || menubg_img == NULL) {
156 fprintf(stderr, "OOM\n");
157 exit(1);
158 }
2e6189bc 159
bcfc48e3 160 in_sdl_init(&in_sdl_platform_data, plat_sdl_event_handler);
7badc935 161 in_probe();
4ea7de6a 162 pl_rearmed_cbs.only_16bpp = 1;
418caf43 163 pl_rearmed_cbs.pl_get_layer_pos = get_layer_pos;
c9099d02 164
165 bgr_to_uyvy_init();
7badc935 166}
167
168void plat_finish(void)
169{
5b9aa749 170 free(shadow_fb);
171 shadow_fb = NULL;
2e6189bc 172 free(menubg_img);
173 menubg_img = NULL;
5b9aa749 174 plat_sdl_finish();
7badc935 175}
176
ab423939 177void plat_gvideo_open(int is_pal)
7badc935 178{
179}
180
c9099d02 181static void uyvy_to_rgb565(void *d, const void *s, int pixels)
7badc935 182{
c9099d02 183 unsigned short *dst = d;
184 const unsigned int *src = s;
185 int v;
186
187 // no colors, for now
188 for (; pixels > 0; src++, dst += 2, pixels -= 2) {
189 v = (*src >> 8) & 0xff;
190 v = (v - 16) * 255 / 219 / 8;
191 dst[0] = (v << 11) | (v << 6) | v;
192
193 v = (*src >> 24) & 0xff;
194 v = (v - 16) * 255 / 219 / 8;
195 dst[1] = (v << 11) | (v << 6) | v;
196 }
7badc935 197}
198
c9099d02 199static void overlay_blit(int doffs, const void *src_, int w, int h,
200 int sstride, int bgr24)
201{
202 const unsigned short *src = src_;
203 unsigned short *dst;
5b9aa749 204 int dstride = plat_sdl_overlay->w;
c9099d02 205
5b9aa749 206 SDL_LockYUVOverlay(plat_sdl_overlay);
207 dst = (void *)plat_sdl_overlay->pixels[0];
c9099d02 208
209 dst += doffs;
210 if (bgr24) {
211 for (; h > 0; dst += dstride, src += sstride, h--)
212 bgr888_to_uyvy(dst, src, w);
213 }
214 else {
215 for (; h > 0; dst += dstride, src += sstride, h--)
216 bgr555_to_uyvy(dst, src, w);
217 }
218
5b9aa749 219 SDL_UnlockYUVOverlay(plat_sdl_overlay);
c9099d02 220}
221
222static void overlay_hud_print(int x, int y, const char *str, int bpp)
223{
5b9aa749 224 SDL_LockYUVOverlay(plat_sdl_overlay);
225 basic_text_out_uyvy_nf(plat_sdl_overlay->pixels[0], plat_sdl_overlay->w, x, y, str);
226 SDL_UnlockYUVOverlay(plat_sdl_overlay);
c9099d02 227}
228
229void *plat_gvideo_set_mode(int *w, int *h, int *bpp)
230{
5b9aa749 231 psx_w = *w;
232 psx_h = *h;
418caf43 233 change_video_mode(0);
5b9aa749 234 if (plat_sdl_overlay != NULL) {
235 pl_plat_clear = plat_sdl_overlay_clear;
c9099d02 236 pl_plat_blit = overlay_blit;
237 pl_plat_hud_print = overlay_hud_print;
238 return NULL;
239 }
240 else {
241 pl_plat_clear = NULL;
242 pl_plat_blit = NULL;
243 pl_plat_hud_print = NULL;
5b9aa749 244 if (plat_sdl_gl_active)
245 return shadow_fb;
246 else
247 return plat_sdl_screen->pixels;
a8376201 248 }
249}
250
7badc935 251void *plat_gvideo_flip(void)
252{
5b9aa749 253 if (plat_sdl_overlay != NULL) {
254 SDL_Rect dstrect = { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
255 SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
c9099d02 256 return NULL;
a8376201 257 }
5b9aa749 258 else if (plat_sdl_gl_active) {
259 gl_flip(shadow_fb, psx_w, psx_h);
260 return shadow_fb;
261 }
c9099d02 262 else {
5b9aa749 263 // XXX: no locking, but should be fine with SDL_SWSURFACE?
264 SDL_Flip(plat_sdl_screen);
265 return plat_sdl_screen->pixels;
a8376201 266 }
7badc935 267}
268
269void plat_gvideo_close(void)
270{
271}
272
273void plat_video_menu_enter(int is_rom_loaded)
274{
418caf43 275 int force_mode_change = 0;
276
a8376201 277 in_menu = 1;
278
2e6189bc 279 /* surface will be lost, must adjust pl_vout_buf for menu bg */
5b9aa749 280 if (plat_sdl_overlay != NULL)
281 uyvy_to_rgb565(menubg_img, plat_sdl_overlay->pixels[0], psx_w * psx_h);
282 else if (plat_sdl_gl_active)
283 memcpy(menubg_img, shadow_fb, psx_w * psx_h * 2);
c9099d02 284 else
5b9aa749 285 memcpy(menubg_img, plat_sdl_screen->pixels, psx_w * psx_h * 2);
2e6189bc 286 pl_vout_buf = menubg_img;
287
418caf43 288 /* gles plugin messes stuff up.. */
289 if (pl_rearmed_cbs.gpu_caps & GPU_CAP_OWNS_DISPLAY)
290 force_mode_change = 1;
291
292 change_video_mode(force_mode_change);
7badc935 293}
294
295void plat_video_menu_begin(void)
296{
5b9aa749 297 if (plat_sdl_overlay != NULL || plat_sdl_gl_active) {
298 g_menuscreen_ptr = shadow_fb;
299 }
300 else {
301 SDL_LockSurface(plat_sdl_screen);
302 g_menuscreen_ptr = plat_sdl_screen->pixels;
303 }
7badc935 304}
305
306void plat_video_menu_end(void)
307{
5b9aa749 308 if (plat_sdl_overlay != NULL) {
309 SDL_Rect dstrect = { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
310
311 SDL_LockYUVOverlay(plat_sdl_overlay);
312 rgb565_to_uyvy(plat_sdl_overlay->pixels[0], shadow_fb,
313 g_menuscreen_w * g_menuscreen_h);
314 SDL_UnlockYUVOverlay(plat_sdl_overlay);
315
316 SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
317 }
318 else if (plat_sdl_gl_active) {
319 gl_flip(g_menuscreen_ptr, g_menuscreen_w, g_menuscreen_h);
320 }
321 else {
322 SDL_UnlockSurface(plat_sdl_screen);
323 SDL_Flip(plat_sdl_screen);
324 }
7badc935 325 g_menuscreen_ptr = NULL;
326}
327
328void plat_video_menu_leave(void)
329{
a8376201 330 in_menu = 0;
7badc935 331}
332
333/* unused stuff */
334void *plat_prepare_screenshot(int *w, int *h, int *bpp)
335{
336 return 0;
337}
338
d71c7095 339void plat_trigger_vibrate(int pad, int low, int high)
7badc935 340{
341}
342
343void plat_minimize(void)
344{
345}
346
347// vim:shiftwidth=2:expandtab