dfsound: add PulseAudio workaround
[pcsx_rearmed.git] / frontend / plat_sdl.c
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>
13
14 #include "libpicofe/input.h"
15 #include "libpicofe/in_sdl.h"
16 #include "libpicofe/menu.h"
17 #include "libpicofe/fonts.h"
18 #include "../plugins/gpulib/cspace.h"
19 #include "plugin_lib.h"
20 #include "main.h"
21 #include "menu.h"
22 #include "plat.h"
23 #include "revision.h"
24
25 static const struct in_default_bind in_sdl_defbinds[] = {
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 },
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 },
47   { SDLK_F7,     IN_BINDTYPE_EMU, SACTION_TOGGLE_FPS },
48   { SDLK_F8,     IN_BINDTYPE_EMU, SACTION_SWITCH_DISPMODE },
49   { SDLK_F11,    IN_BINDTYPE_EMU, SACTION_TOGGLE_FULLSCREEN },
50   { SDLK_BACKSPACE, IN_BINDTYPE_EMU, SACTION_FAST_FORWARD },
51   { 0, 0, 0 }
52 };
53
54 // XXX: maybe determine this instead..
55 #define WM_DECORATION_H 32
56
57 static SDL_Surface *screen;
58 static SDL_Overlay *overlay;
59 static int window_w, window_h;
60 static int fs_w, fs_h;
61 static int psx_w, psx_h;
62 static void *menubg_img;
63 static int in_menu, old_fullscreen;
64
65 static void overlay_clear(void);
66
67 static int change_video_mode(int w, int h)
68 {
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");
105
106       overlay_clear();
107     }
108     else {
109       fprintf(stderr, "warning: could not create overlay.\n");
110     }
111   }
112
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;
127   return 0;
128 }
129
130 static 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;
139       change_video_mode(psx_w, psx_h);
140     }
141   }
142 }
143
144 void plat_init(void)
145 {
146   const SDL_VideoInfo *info;
147   int ret, h;
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
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;
162   g_menuscreen_w = 640;
163   if (fs_w != 0 && g_menuscreen_w > fs_w)
164     g_menuscreen_w = fs_w;
165   g_menuscreen_h = 480;
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
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     }
185   }
186   g_menuscreen_w = window_w = screen->w;
187   g_menuscreen_h = window_h = screen->h;
188
189   SDL_WM_SetCaption("PCSX-ReARMed " REV, NULL);
190
191   menubg_img = malloc(640 * 512 * 2);
192   if (menubg_img == NULL)
193     goto fail;
194
195   in_sdl_init(in_sdl_defbinds, event_handler);
196   in_probe();
197   pl_rearmed_cbs.only_16bpp = 1;
198
199   bgr_to_uyvy_init();
200   return;
201
202 fail:
203   SDL_Quit();
204   exit(1);
205 }
206
207 void plat_finish(void)
208 {
209   free(menubg_img);
210   menubg_img = NULL;
211   SDL_Quit();
212 }
213
214 void plat_gvideo_open(int is_pal)
215 {
216 }
217
218 static void uyvy_to_rgb565(void *d, const void *s, int pixels)
219 {
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   }
234 }
235
236 static void overlay_clear(void)
237 {
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
249 static 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
272 static 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
279 void *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;
293   }
294 }
295
296 void *plat_gvideo_flip(void)
297 {
298   if (!in_menu && overlay != NULL) {
299     SDL_Rect dstrect = { 0, 0, screen->w, screen->h };
300     SDL_DisplayYUVOverlay(overlay, &dstrect);
301     return NULL;
302   }
303   else {
304     // XXX: missing SDL_LockSurface()
305     SDL_Flip(screen);
306     return screen->pixels;
307   }
308 }
309
310 void plat_gvideo_close(void)
311 {
312 }
313
314 void plat_video_menu_enter(int is_rom_loaded)
315 {
316   in_menu = 1;
317
318   /* surface will be lost, must adjust pl_vout_buf for menu bg */
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);
323   pl_vout_buf = menubg_img;
324
325   change_video_mode(g_menuscreen_w, g_menuscreen_h);
326 }
327
328 void plat_video_menu_begin(void)
329 {
330   SDL_LockSurface(screen);
331   g_menuscreen_ptr = screen->pixels;
332 }
333
334 void plat_video_menu_end(void)
335 {
336   SDL_UnlockSurface(screen);
337   SDL_Flip(screen);
338   g_menuscreen_ptr = NULL;
339 }
340
341 void plat_video_menu_leave(void)
342 {
343   in_menu = 0;
344 }
345
346 /* unused stuff */
347 void *plat_prepare_screenshot(int *w, int *h, int *bpp)
348 {
349   return 0;
350 }
351
352 void plat_trigger_vibrate(int is_strong)
353 {
354 }
355
356 void plat_minimize(void)
357 {
358 }
359
360 // vim:shiftwidth=2:expandtab