frontend: change how exit is done
[pcsx_rearmed.git] / frontend / plat_sdl.c
CommitLineData
7badc935 1/*
2 * (C) GraÅžvydas "notaz" Ignotas, 2011,2012
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"
c9099d02 20#include "../plugins/gpulib/cspace.h"
b07c18e8 21#include "plugin_lib.h"
22#include "main.h"
7badc935 23#include "plat.h"
24#include "revision.h"
25
b07c18e8 26static const struct in_default_bind in_sdl_defbinds[] = {
2e6189bc 27 { SDLK_UP, IN_BINDTYPE_PLAYER12, DKEY_UP },
28 { SDLK_DOWN, IN_BINDTYPE_PLAYER12, DKEY_DOWN },
29 { SDLK_LEFT, IN_BINDTYPE_PLAYER12, DKEY_LEFT },
30 { SDLK_RIGHT, IN_BINDTYPE_PLAYER12, DKEY_RIGHT },
31 { SDLK_d, IN_BINDTYPE_PLAYER12, DKEY_TRIANGLE },
32 { SDLK_z, IN_BINDTYPE_PLAYER12, DKEY_CROSS },
33 { SDLK_x, IN_BINDTYPE_PLAYER12, DKEY_CIRCLE },
34 { SDLK_s, IN_BINDTYPE_PLAYER12, DKEY_SQUARE },
35 { SDLK_v, IN_BINDTYPE_PLAYER12, DKEY_START },
36 { SDLK_c, IN_BINDTYPE_PLAYER12, DKEY_SELECT },
37 { SDLK_w, IN_BINDTYPE_PLAYER12, DKEY_L1 },
38 { SDLK_r, IN_BINDTYPE_PLAYER12, DKEY_R1 },
39 { SDLK_e, IN_BINDTYPE_PLAYER12, DKEY_L2 },
40 { SDLK_t, IN_BINDTYPE_PLAYER12, DKEY_R2 },
41 { SDLK_ESCAPE, IN_BINDTYPE_EMU, SACTION_ENTER_MENU },
456f1b86 42 { SDLK_F1, IN_BINDTYPE_EMU, SACTION_SAVE_STATE },
43 { SDLK_F2, IN_BINDTYPE_EMU, SACTION_LOAD_STATE },
44 { SDLK_F3, IN_BINDTYPE_EMU, SACTION_PREV_SSLOT },
45 { SDLK_F4, IN_BINDTYPE_EMU, SACTION_NEXT_SSLOT },
46 { SDLK_F5, IN_BINDTYPE_EMU, SACTION_TOGGLE_FSKIP },
47 { SDLK_F6, IN_BINDTYPE_EMU, SACTION_SCREENSHOT },
dde7da71 48 { SDLK_F7, IN_BINDTYPE_EMU, SACTION_TOGGLE_FPS },
456f1b86 49 { SDLK_F8, IN_BINDTYPE_EMU, SACTION_SWITCH_DISPMODE },
a8376201 50 { SDLK_F11, IN_BINDTYPE_EMU, SACTION_TOGGLE_FULLSCREEN },
dde7da71 51 { SDLK_BACKSPACE, IN_BINDTYPE_EMU, SACTION_FAST_FORWARD },
2e6189bc 52 { 0, 0, 0 }
7badc935 53};
54
a8376201 55static int psx_w, psx_h;
5b9aa749 56static void *shadow_fb, *menubg_img;
57static int in_menu;
7badc935 58
5b9aa749 59static int change_video_mode(void)
7badc935 60{
5b9aa749 61 int w, h;
a8376201 62
5b9aa749 63 if (in_menu) {
64 w = g_menuscreen_w;
65 h = g_menuscreen_h;
a8376201 66 }
5b9aa749 67 else {
68 w = psx_w;
69 h = psx_h;
a8376201 70 }
71
2c616080 72 return plat_sdl_change_video_mode(w, h, 0);
a8376201 73}
74
69e482e3 75static void quit_cb(void)
76{
77 emu_core_ask_exit();
78}
79
7badc935 80void plat_init(void)
81{
5b9aa749 82 int ret;
7badc935 83
5b9aa749 84 ret = plat_sdl_init();
85 if (ret != 0)
7badc935 86 exit(1);
a8376201 87
88 in_menu = 1;
7badc935 89 SDL_WM_SetCaption("PCSX-ReARMed " REV, NULL);
90
5b9aa749 91 shadow_fb = malloc(640 * 512 * 2);
2e6189bc 92 menubg_img = malloc(640 * 512 * 2);
5b9aa749 93 if (shadow_fb == NULL || menubg_img == NULL) {
94 fprintf(stderr, "OOM\n");
95 exit(1);
96 }
2e6189bc 97
5b9aa749 98 in_sdl_init(in_sdl_defbinds, plat_sdl_event_handler);
7badc935 99 in_probe();
4ea7de6a 100 pl_rearmed_cbs.only_16bpp = 1;
c9099d02 101
102 bgr_to_uyvy_init();
69e482e3 103 plat_sdl_quit_cb = quit_cb;
7badc935 104}
105
106void plat_finish(void)
107{
5b9aa749 108 free(shadow_fb);
109 shadow_fb = NULL;
2e6189bc 110 free(menubg_img);
111 menubg_img = NULL;
5b9aa749 112 plat_sdl_finish();
7badc935 113}
114
ab423939 115void plat_gvideo_open(int is_pal)
7badc935 116{
117}
118
c9099d02 119static void uyvy_to_rgb565(void *d, const void *s, int pixels)
7badc935 120{
c9099d02 121 unsigned short *dst = d;
122 const unsigned int *src = s;
123 int v;
124
125 // no colors, for now
126 for (; pixels > 0; src++, dst += 2, pixels -= 2) {
127 v = (*src >> 8) & 0xff;
128 v = (v - 16) * 255 / 219 / 8;
129 dst[0] = (v << 11) | (v << 6) | v;
130
131 v = (*src >> 24) & 0xff;
132 v = (v - 16) * 255 / 219 / 8;
133 dst[1] = (v << 11) | (v << 6) | v;
134 }
7badc935 135}
136
c9099d02 137static void overlay_blit(int doffs, const void *src_, int w, int h,
138 int sstride, int bgr24)
139{
140 const unsigned short *src = src_;
141 unsigned short *dst;
5b9aa749 142 int dstride = plat_sdl_overlay->w;
c9099d02 143
5b9aa749 144 SDL_LockYUVOverlay(plat_sdl_overlay);
145 dst = (void *)plat_sdl_overlay->pixels[0];
c9099d02 146
147 dst += doffs;
148 if (bgr24) {
149 for (; h > 0; dst += dstride, src += sstride, h--)
150 bgr888_to_uyvy(dst, src, w);
151 }
152 else {
153 for (; h > 0; dst += dstride, src += sstride, h--)
154 bgr555_to_uyvy(dst, src, w);
155 }
156
5b9aa749 157 SDL_UnlockYUVOverlay(plat_sdl_overlay);
c9099d02 158}
159
160static void overlay_hud_print(int x, int y, const char *str, int bpp)
161{
5b9aa749 162 SDL_LockYUVOverlay(plat_sdl_overlay);
163 basic_text_out_uyvy_nf(plat_sdl_overlay->pixels[0], plat_sdl_overlay->w, x, y, str);
164 SDL_UnlockYUVOverlay(plat_sdl_overlay);
c9099d02 165}
166
167void *plat_gvideo_set_mode(int *w, int *h, int *bpp)
168{
5b9aa749 169 psx_w = *w;
170 psx_h = *h;
171 change_video_mode();
172 if (plat_sdl_overlay != NULL) {
173 pl_plat_clear = plat_sdl_overlay_clear;
c9099d02 174 pl_plat_blit = overlay_blit;
175 pl_plat_hud_print = overlay_hud_print;
176 return NULL;
177 }
178 else {
179 pl_plat_clear = NULL;
180 pl_plat_blit = NULL;
181 pl_plat_hud_print = NULL;
5b9aa749 182 if (plat_sdl_gl_active)
183 return shadow_fb;
184 else
185 return plat_sdl_screen->pixels;
a8376201 186 }
187}
188
7badc935 189void *plat_gvideo_flip(void)
190{
5b9aa749 191 if (plat_sdl_overlay != NULL) {
192 SDL_Rect dstrect = { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
193 SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
c9099d02 194 return NULL;
a8376201 195 }
5b9aa749 196 else if (plat_sdl_gl_active) {
197 gl_flip(shadow_fb, psx_w, psx_h);
198 return shadow_fb;
199 }
c9099d02 200 else {
5b9aa749 201 // XXX: no locking, but should be fine with SDL_SWSURFACE?
202 SDL_Flip(plat_sdl_screen);
203 return plat_sdl_screen->pixels;
a8376201 204 }
7badc935 205}
206
207void plat_gvideo_close(void)
208{
209}
210
211void plat_video_menu_enter(int is_rom_loaded)
212{
a8376201 213 in_menu = 1;
214
2e6189bc 215 /* surface will be lost, must adjust pl_vout_buf for menu bg */
5b9aa749 216 if (plat_sdl_overlay != NULL)
217 uyvy_to_rgb565(menubg_img, plat_sdl_overlay->pixels[0], psx_w * psx_h);
218 else if (plat_sdl_gl_active)
219 memcpy(menubg_img, shadow_fb, psx_w * psx_h * 2);
c9099d02 220 else
5b9aa749 221 memcpy(menubg_img, plat_sdl_screen->pixels, psx_w * psx_h * 2);
2e6189bc 222 pl_vout_buf = menubg_img;
223
5b9aa749 224 change_video_mode();
7badc935 225}
226
227void plat_video_menu_begin(void)
228{
5b9aa749 229 if (plat_sdl_overlay != NULL || plat_sdl_gl_active) {
230 g_menuscreen_ptr = shadow_fb;
231 }
232 else {
233 SDL_LockSurface(plat_sdl_screen);
234 g_menuscreen_ptr = plat_sdl_screen->pixels;
235 }
7badc935 236}
237
238void plat_video_menu_end(void)
239{
5b9aa749 240 if (plat_sdl_overlay != NULL) {
241 SDL_Rect dstrect = { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
242
243 SDL_LockYUVOverlay(plat_sdl_overlay);
244 rgb565_to_uyvy(plat_sdl_overlay->pixels[0], shadow_fb,
245 g_menuscreen_w * g_menuscreen_h);
246 SDL_UnlockYUVOverlay(plat_sdl_overlay);
247
248 SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
249 }
250 else if (plat_sdl_gl_active) {
251 gl_flip(g_menuscreen_ptr, g_menuscreen_w, g_menuscreen_h);
252 }
253 else {
254 SDL_UnlockSurface(plat_sdl_screen);
255 SDL_Flip(plat_sdl_screen);
256 }
7badc935 257 g_menuscreen_ptr = NULL;
258}
259
260void plat_video_menu_leave(void)
261{
a8376201 262 in_menu = 0;
7badc935 263}
264
265/* unused stuff */
266void *plat_prepare_screenshot(int *w, int *h, int *bpp)
267{
268 return 0;
269}
270
7badc935 271void plat_trigger_vibrate(int is_strong)
272{
273}
274
275void plat_minimize(void)
276{
277}
278
279// vim:shiftwidth=2:expandtab