frontend: overlay improvements
[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"
18#include "../plugins/gpulib/cspace.h"
b07c18e8 19#include "plugin_lib.h"
20#include "main.h"
a8376201 21#include "menu.h"
7badc935 22#include "plat.h"
23#include "revision.h"
24
b07c18e8 25static const struct in_default_bind in_sdl_defbinds[] = {
2e6189bc 26 { SDLK_UP, IN_BINDTYPE_PLAYER12, DKEY_UP },
27 { SDLK_DOWN, IN_BINDTYPE_PLAYER12, DKEY_DOWN },
28 { SDLK_LEFT, IN_BINDTYPE_PLAYER12, DKEY_LEFT },
29 { SDLK_RIGHT, IN_BINDTYPE_PLAYER12, DKEY_RIGHT },
30 { SDLK_d, IN_BINDTYPE_PLAYER12, DKEY_TRIANGLE },
31 { SDLK_z, IN_BINDTYPE_PLAYER12, DKEY_CROSS },
32 { SDLK_x, IN_BINDTYPE_PLAYER12, DKEY_CIRCLE },
33 { SDLK_s, IN_BINDTYPE_PLAYER12, DKEY_SQUARE },
34 { SDLK_v, IN_BINDTYPE_PLAYER12, DKEY_START },
35 { SDLK_c, IN_BINDTYPE_PLAYER12, DKEY_SELECT },
36 { SDLK_w, IN_BINDTYPE_PLAYER12, DKEY_L1 },
37 { SDLK_r, IN_BINDTYPE_PLAYER12, DKEY_R1 },
38 { SDLK_e, IN_BINDTYPE_PLAYER12, DKEY_L2 },
39 { SDLK_t, IN_BINDTYPE_PLAYER12, DKEY_R2 },
40 { SDLK_ESCAPE, IN_BINDTYPE_EMU, SACTION_ENTER_MENU },
456f1b86 41 { SDLK_F1, IN_BINDTYPE_EMU, SACTION_SAVE_STATE },
42 { SDLK_F2, IN_BINDTYPE_EMU, SACTION_LOAD_STATE },
43 { SDLK_F3, IN_BINDTYPE_EMU, SACTION_PREV_SSLOT },
44 { SDLK_F4, IN_BINDTYPE_EMU, SACTION_NEXT_SSLOT },
45 { SDLK_F5, IN_BINDTYPE_EMU, SACTION_TOGGLE_FSKIP },
46 { SDLK_F6, IN_BINDTYPE_EMU, SACTION_SCREENSHOT },
dde7da71 47 { SDLK_F7, IN_BINDTYPE_EMU, SACTION_TOGGLE_FPS },
456f1b86 48 { SDLK_F8, IN_BINDTYPE_EMU, SACTION_SWITCH_DISPMODE },
a8376201 49 { SDLK_F11, IN_BINDTYPE_EMU, SACTION_TOGGLE_FULLSCREEN },
dde7da71 50 { SDLK_BACKSPACE, IN_BINDTYPE_EMU, SACTION_FAST_FORWARD },
2e6189bc 51 { 0, 0, 0 }
7badc935 52};
53
a8376201 54// XXX: maybe determine this instead..
55#define WM_DECORATION_H 32
56
7badc935 57static SDL_Surface *screen;
a8376201 58static SDL_Overlay *overlay;
59static int window_w, window_h;
60static int fs_w, fs_h;
61static int psx_w, psx_h;
2e6189bc 62static void *menubg_img;
c9099d02 63static int in_menu, old_fullscreen;
64
65static void overlay_clear(void);
7badc935 66
67static int change_video_mode(int w, int h)
68{
a8376201 69 psx_w = w;
70 psx_h = h;
71
72 if (overlay != NULL) {
73 SDL_FreeYUVOverlay(overlay);
74 overlay = NULL;
75 }
76
77 if (g_use_overlay && !in_menu) {
78 Uint32 flags = SDL_RESIZABLE;
79 int win_w = window_w;
80 int win_h = window_h;
81
82 if (g_fullscreen) {
83 flags |= SDL_FULLSCREEN;
84 win_w = fs_w;
85 win_h = fs_h;
86 }
87
88 screen = SDL_SetVideoMode(win_w, win_h, 0, flags);
89 if (screen == NULL) {
90 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
91 return -1;
92 }
93
94 overlay = SDL_CreateYUVOverlay(w, h, SDL_UYVY_OVERLAY, screen);
95 if (overlay != NULL) {
96 /*printf("overlay: fmt %x, planes: %d, pitch: %d, hw: %d\n",
97 overlay->format, overlay->planes, *overlay->pitches,
98 overlay->hw_overlay);*/
99
100 if ((long)overlay->pixels[0] & 3)
101 fprintf(stderr, "warning: overlay pointer is unaligned\n");
102 if (!overlay->hw_overlay)
103 fprintf(stderr, "warning: video overlay is not hardware accelerated,"
104 " you may want to disable it.\n");
c9099d02 105
106 overlay_clear();
a8376201 107 }
108 else {
109 fprintf(stderr, "warning: could not create overlay.\n");
110 }
7badc935 111 }
112
a8376201 113 if (overlay == NULL) {
114 screen = SDL_SetVideoMode(w, h, 16, 0);
115 if (screen == NULL) {
116 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
117 return -1;
118 }
119
120 if (!in_menu) {
121 window_w = screen->w;
122 window_h = screen->h;
123 }
124 }
125
126 old_fullscreen = g_fullscreen;
7badc935 127 return 0;
128}
129
a8376201 130static void event_handler(void *event_)
131{
132 SDL_Event *event = event_;
133
134 if (event->type == SDL_VIDEORESIZE) {
135 //printf("%dx%d\n", event->resize.w, event->resize.h);
136 if (overlay != NULL && !g_fullscreen && !old_fullscreen) {
137 window_w = event->resize.w;
138 window_h = event->resize.h;
c9099d02 139 change_video_mode(psx_w, psx_h);
a8376201 140 }
141 }
142}
143
7badc935 144void plat_init(void)
145{
a8376201 146 const SDL_VideoInfo *info;
147 int ret, h;
7badc935 148
149 ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
150 if (ret != 0) {
151 fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
152 exit(1);
153 }
154
a8376201 155 info = SDL_GetVideoInfo();
156 if (info != NULL) {
157 fs_w = info->current_w;
158 fs_h = info->current_h;
159 }
160
161 in_menu = 1;
7badc935 162 g_menuscreen_w = 640;
a8376201 163 if (fs_w != 0 && g_menuscreen_w > fs_w)
164 g_menuscreen_w = fs_w;
7badc935 165 g_menuscreen_h = 480;
a8376201 166 if (fs_h != 0) {
167 h = fs_h;
168 if (info && info->wm_available && h > WM_DECORATION_H)
169 h -= WM_DECORATION_H;
170 if (g_menuscreen_h > h)
171 g_menuscreen_h = h;
172 }
173
7badc935 174 ret = change_video_mode(g_menuscreen_w, g_menuscreen_h);
175 if (ret != 0) {
176 ret = change_video_mode(0, 0);
177 if (ret != 0)
178 goto fail;
179
180 if (screen->w < 320 || screen->h < 240) {
181 fprintf(stderr, "resolution %dx%d is too small, sorry.\n",
182 screen->w, screen->h);
183 goto fail;
184 }
7badc935 185 }
a8376201 186 g_menuscreen_w = window_w = screen->w;
187 g_menuscreen_h = window_h = screen->h;
188
7badc935 189 SDL_WM_SetCaption("PCSX-ReARMed " REV, NULL);
190
2e6189bc 191 menubg_img = malloc(640 * 512 * 2);
192 if (menubg_img == NULL)
193 goto fail;
194
a8376201 195 in_sdl_init(in_sdl_defbinds, event_handler);
7badc935 196 in_probe();
4ea7de6a 197 pl_rearmed_cbs.only_16bpp = 1;
c9099d02 198
199 bgr_to_uyvy_init();
7badc935 200 return;
201
202fail:
203 SDL_Quit();
204 exit(1);
205}
206
207void plat_finish(void)
208{
2e6189bc 209 free(menubg_img);
210 menubg_img = NULL;
7badc935 211 SDL_Quit();
212}
213
ab423939 214void plat_gvideo_open(int is_pal)
7badc935 215{
216}
217
c9099d02 218static void uyvy_to_rgb565(void *d, const void *s, int pixels)
7badc935 219{
c9099d02 220 unsigned short *dst = d;
221 const unsigned int *src = s;
222 int v;
223
224 // no colors, for now
225 for (; pixels > 0; src++, dst += 2, pixels -= 2) {
226 v = (*src >> 8) & 0xff;
227 v = (v - 16) * 255 / 219 / 8;
228 dst[0] = (v << 11) | (v << 6) | v;
229
230 v = (*src >> 24) & 0xff;
231 v = (v - 16) * 255 / 219 / 8;
232 dst[1] = (v << 11) | (v << 6) | v;
233 }
7badc935 234}
235
c9099d02 236static void overlay_clear(void)
a8376201 237{
c9099d02 238 int pixels = overlay->w * overlay->h;
239 int *dst = (int *)overlay->pixels[0];
240 int v = 0x10801080;
241
242 for (; pixels > 0; dst += 4, pixels -= 2 * 4)
243 dst[0] = dst[1] = dst[2] = dst[3] = v;
244
245 for (; pixels > 0; dst++, pixels -= 2)
246 *dst = v;
247}
248
249static void overlay_blit(int doffs, const void *src_, int w, int h,
250 int sstride, int bgr24)
251{
252 const unsigned short *src = src_;
253 unsigned short *dst;
254 int dstride = overlay->w;
255
256 SDL_LockYUVOverlay(overlay);
257 dst = (void *)overlay->pixels[0];
258
259 dst += doffs;
260 if (bgr24) {
261 for (; h > 0; dst += dstride, src += sstride, h--)
262 bgr888_to_uyvy(dst, src, w);
263 }
264 else {
265 for (; h > 0; dst += dstride, src += sstride, h--)
266 bgr555_to_uyvy(dst, src, w);
267 }
268
269 SDL_UnlockYUVOverlay(overlay);
270}
271
272static void overlay_hud_print(int x, int y, const char *str, int bpp)
273{
274 SDL_LockYUVOverlay(overlay);
275 basic_text_out_uyvy_nf(overlay->pixels[0], overlay->w, x, y, str);
276 SDL_UnlockYUVOverlay(overlay);
277}
278
279void *plat_gvideo_set_mode(int *w, int *h, int *bpp)
280{
281 change_video_mode(*w, *h);
282 if (overlay != NULL) {
283 pl_plat_clear = overlay_clear;
284 pl_plat_blit = overlay_blit;
285 pl_plat_hud_print = overlay_hud_print;
286 return NULL;
287 }
288 else {
289 pl_plat_clear = NULL;
290 pl_plat_blit = NULL;
291 pl_plat_hud_print = NULL;
292 return screen->pixels;
a8376201 293 }
294}
295
7badc935 296void *plat_gvideo_flip(void)
297{
a8376201 298 if (!in_menu && overlay != NULL) {
299 SDL_Rect dstrect = { 0, 0, screen->w, screen->h };
a8376201 300 SDL_DisplayYUVOverlay(overlay, &dstrect);
c9099d02 301 return NULL;
a8376201 302 }
c9099d02 303 else {
304 // XXX: missing SDL_LockSurface()
a8376201 305 SDL_Flip(screen);
c9099d02 306 return screen->pixels;
a8376201 307 }
7badc935 308}
309
310void plat_gvideo_close(void)
311{
312}
313
314void plat_video_menu_enter(int is_rom_loaded)
315{
a8376201 316 in_menu = 1;
317
2e6189bc 318 /* surface will be lost, must adjust pl_vout_buf for menu bg */
c9099d02 319 if (overlay != NULL)
320 uyvy_to_rgb565(menubg_img, overlay->pixels[0], psx_w * psx_h);
321 else
322 memcpy(menubg_img, screen->pixels, psx_w * psx_h * 2);
2e6189bc 323 pl_vout_buf = menubg_img;
324
7badc935 325 change_video_mode(g_menuscreen_w, g_menuscreen_h);
326}
327
328void plat_video_menu_begin(void)
329{
330 SDL_LockSurface(screen);
331 g_menuscreen_ptr = screen->pixels;
332}
333
334void plat_video_menu_end(void)
335{
336 SDL_UnlockSurface(screen);
337 SDL_Flip(screen);
338 g_menuscreen_ptr = NULL;
339}
340
341void plat_video_menu_leave(void)
342{
a8376201 343 in_menu = 0;
7badc935 344}
345
346/* unused stuff */
347void *plat_prepare_screenshot(int *w, int *h, int *bpp)
348{
349 return 0;
350}
351
7badc935 352void plat_trigger_vibrate(int is_strong)
353{
354}
355
356void plat_minimize(void)
357{
358}
359
360// vim:shiftwidth=2:expandtab