in_sdl: give names to gamepad buttons
[libpicofe.git] / plat_sdl.c
CommitLineData
e81b987f 1/*
2 * (C) GraÅžvydas "notaz" Ignotas, 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 * - MAME license.
9 * See the COPYING file in the top-level directory.
10 */
11
12#include <stdio.h>
9fba90ac 13#include <stdint.h>
e81b987f 14#include <SDL.h>
15#include <SDL_syswm.h>
16
17#include "menu.h"
18#include "plat.h"
19#include "gl.h"
20#include "plat_sdl.h"
21
22// XXX: maybe determine this instead..
23#define WM_DECORATION_H 32
24
25SDL_Surface *plat_sdl_screen;
26SDL_Overlay *plat_sdl_overlay;
27int plat_sdl_gl_active;
e288d776 28void (*plat_sdl_resize_cb)(int w, int h);
1f84ba9f 29void (*plat_sdl_quit_cb)(void);
38284238 30int plat_sdl_no_overlay2x;
e81b987f 31
63f173a2 32static char vid_drv_name[32];
533726df 33static int window_w, window_h, window_b;
e81b987f 34static int fs_w, fs_h;
35static int old_fullscreen;
9ba08314 36static int vout_mode_overlay = -1, vout_mode_overlay2x = -1, vout_mode_gl = -1;
e81b987f 37static void *display, *window;
0d645bc5 38static int gl_quirks;
528f2522 39static Uint32 screen_flags =
40#if defined(SDL_SURFACE_SW)
41 SDL_SWSURFACE;
42#elif defined(SDL_TRIPLEBUF) && defined(SDL_BUFFER_3X)
43 SDL_HWSURFACE | SDL_TRIPLEBUF;
44#else
45 SDL_HWSURFACE | SDL_DOUBLEBUF;
46#endif
47
48static Uint32 get_screen_flags(void)
49{
50 Uint32 flags = screen_flags;
51 flags &= ~(SDL_FULLSCREEN | SDL_RESIZABLE);
52 if (plat_target.vout_fullscreen && fs_w && fs_h)
53 flags |= SDL_FULLSCREEN;
54 else if (window_b)
55 flags |= SDL_RESIZABLE;
56 return flags;
57}
e81b987f 58
2607df8a 59static void update_wm_display_window(void)
60{
61 // get x11 display/window for GL
62#ifdef SDL_VIDEO_DRIVER_X11
63 SDL_SysWMinfo wminfo;
64 int ret;
65 if (strcmp(vid_drv_name, "x11") == 0) {
66 SDL_VERSION(&wminfo.version);
67 ret = SDL_GetWMInfo(&wminfo);
68 if (ret > 0) {
69 display = wminfo.info.x11.display;
70 window = (void *)wminfo.info.x11.window;
71 }
72 }
73#endif
74}
75
e54719ef 76/* w, h is layer resolution */
77int plat_sdl_change_video_mode(int w, int h, int force)
e81b987f 78{
79 static int prev_w, prev_h;
80
bededcb4 81 // skip GL recreation if window doesn't change - avoids flicker
82 if (plat_target.vout_method == vout_mode_gl && plat_sdl_gl_active
83 && plat_target.vout_fullscreen == old_fullscreen
84 && w == prev_w && h == prev_h && !force)
85 {
86 return 0;
87 }
88
e81b987f 89 if (w == 0)
90 w = prev_w;
91 else
92 prev_w = w;
93 if (h == 0)
94 h = prev_h;
95 else
96 prev_h = h;
97
da0cc556 98 // invalid method might come from config..
99 if (plat_target.vout_method != 0
100 && plat_target.vout_method != vout_mode_overlay
9ba08314 101 && plat_target.vout_method != vout_mode_overlay2x
da0cc556 102 && plat_target.vout_method != vout_mode_gl)
103 {
104 fprintf(stderr, "invalid vout_method: %d\n", plat_target.vout_method);
105 plat_target.vout_method = 0;
106 }
107
e81b987f 108 if (plat_sdl_overlay != NULL) {
109 SDL_FreeYUVOverlay(plat_sdl_overlay);
110 plat_sdl_overlay = NULL;
111 }
112 if (plat_sdl_gl_active) {
4388a645 113 gl_destroy();
e81b987f 114 plat_sdl_gl_active = 0;
115 }
116
ee0dca9e 117 if (plat_target.vout_method == 0 || (force && (window_w != w || window_h != h
ad028217 118 || plat_target.vout_fullscreen != old_fullscreen
119 || !plat_sdl_screen->format || plat_sdl_screen->format->BitsPerPixel != 16))) {
528f2522 120 Uint32 flags = get_screen_flags();
023d7e89 121 int win_w = w;
122 int win_h = h;
123
023d7e89 124 if (plat_target.vout_fullscreen && fs_w && fs_h) {
023d7e89 125 win_w = fs_w;
126 win_h = fs_h;
528f2522 127 }
023d7e89 128
248b5275 129 // XXX: workaround some occasional mysterious deadlock in SDL_SetVideoMode
023d7e89 130 SDL_PumpEvents();
131
132 if (!plat_sdl_screen || screen_flags != flags ||
528f2522 133 plat_sdl_screen->w != win_w || plat_sdl_screen->h != win_h ||
134 (!plat_sdl_screen->format || plat_sdl_screen->format->BitsPerPixel != 16))
023d7e89 135 plat_sdl_screen = SDL_SetVideoMode(win_w, win_h, 16, flags);
023d7e89 136 if (plat_sdl_screen == NULL) {
137 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
138 return -1;
139 }
2607df8a 140 if (vout_mode_gl != -1)
141 update_wm_display_window();
528f2522 142 screen_flags = flags;
143 window_w = win_w;
144 window_h = win_h;
023d7e89 145 }
146
9ba08314 147 if (plat_target.vout_method == vout_mode_overlay
148 || plat_target.vout_method == vout_mode_overlay2x) {
38284238 149 int W = plat_target.vout_method == vout_mode_overlay2x && !plat_sdl_no_overlay2x ? 2*w : w;
9ba08314 150 plat_sdl_overlay = SDL_CreateYUVOverlay(W, h, SDL_UYVY_OVERLAY, plat_sdl_screen);
66ef1764 151 if (plat_sdl_overlay != NULL && SDL_LockYUVOverlay(plat_sdl_overlay) == 0) {
9fba90ac 152 if ((uintptr_t)plat_sdl_overlay->pixels[0] & 3)
e81b987f 153 fprintf(stderr, "warning: overlay pointer is unaligned\n");
154
155 plat_sdl_overlay_clear();
66ef1764 156
09fcfbaa 157 SDL_UnlockYUVOverlay(plat_sdl_overlay);
e81b987f 158 }
159 else {
160 fprintf(stderr, "warning: could not create overlay.\n");
161 plat_target.vout_method = 0;
162 }
163 }
164 else if (plat_target.vout_method == vout_mode_gl) {
c7228611 165 int sw = plat_sdl_screen->w, sh = plat_sdl_screen->h;
4388a645 166 plat_sdl_gl_active = (gl_create(window, &gl_quirks, sw, sh) == 0);
e81b987f 167 if (!plat_sdl_gl_active) {
168 fprintf(stderr, "warning: could not init GL.\n");
169 plat_target.vout_method = 0;
170 }
171 }
172
e81b987f 173 old_fullscreen = plat_target.vout_fullscreen;
e288d776 174 if (plat_sdl_resize_cb != NULL)
175 plat_sdl_resize_cb(plat_sdl_screen->w, plat_sdl_screen->h);
176
e81b987f 177 return 0;
178}
179
180void plat_sdl_event_handler(void *event_)
181{
e54719ef 182 static int was_active;
e81b987f 183 SDL_Event *event = event_;
184
1f84ba9f 185 switch (event->type) {
186 case SDL_VIDEORESIZE:
e81b987f 187 //printf("resize %dx%d\n", event->resize.w, event->resize.h);
248b5275 188 if ((plat_target.vout_method != 0 || window_b) && !plat_target.vout_fullscreen)
e81b987f 189 {
248b5275 190 int win_w = event->resize.w & ~3;
191 int win_h = event->resize.h & ~3;
ee0dca9e 192 plat_sdl_change_video_mode(win_w, win_h, 1);
e54719ef 193 }
1f84ba9f 194 break;
195 case SDL_ACTIVEEVENT:
e54719ef 196 if (event->active.gain && !was_active) {
197 if (plat_sdl_overlay != NULL) {
198 SDL_Rect dstrect = { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
199 SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
200 }
201 else if (plat_sdl_gl_active) {
0d645bc5 202 if (gl_quirks & GL_QUIRK_ACTIVATE_RECREATE) {
c7228611 203 int sw = plat_sdl_screen->w, sh = plat_sdl_screen->h;
4388a645 204 gl_destroy();
205 plat_sdl_gl_active = (gl_create(window, &gl_quirks, sw, sh) == 0);
0d645bc5 206 }
e54719ef 207 gl_flip(NULL, 0, 0);
208 }
209 // else SDL takes care of it
e81b987f 210 }
e54719ef 211 was_active = event->active.gain;
1f84ba9f 212 break;
213 case SDL_QUIT:
214 if (plat_sdl_quit_cb != NULL)
215 plat_sdl_quit_cb();
216 break;
e81b987f 217 }
218}
219
220int plat_sdl_init(void)
221{
9ba08314 222 static const char *vout_list[] = { NULL, NULL, NULL, NULL, NULL };
e81b987f 223 const SDL_VideoInfo *info;
c668921a 224 const char *env;
e81b987f 225 int overlay_works = 0;
226 int gl_works = 0;
227 int i, ret, h;
528f2522 228 Uint32 flags;
c668921a 229 int try_gl;
e81b987f 230
231 ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
232 if (ret != 0) {
233 fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
234 return -1;
235 }
236
237 info = SDL_GetVideoInfo();
238 if (info != NULL) {
239 fs_w = info->current_w;
240 fs_h = info->current_h;
533726df 241 if (info->wm_available)
242 window_b = WM_DECORATION_H;
63f173a2 243 printf("plat_sdl: using %dx%d as fullscreen resolution\n", fs_w, fs_h);
e81b987f 244 }
245
246 g_menuscreen_w = 640;
247 if (fs_w != 0 && g_menuscreen_w > fs_w)
248 g_menuscreen_w = fs_w;
249 g_menuscreen_h = 480;
250 if (fs_h != 0) {
251 h = fs_h;
533726df 252 if (window_b && h > window_b)
253 h -= window_b;
e81b987f 254 if (g_menuscreen_h > h)
255 g_menuscreen_h = h;
256 }
257
55104482 258 plat_sdl_screen = SDL_SetVideoMode(g_menuscreen_w, g_menuscreen_h, 0,
528f2522 259 (flags = get_screen_flags()));
533726df 260 if (plat_sdl_screen == NULL) {
528f2522 261 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
55104482 262 plat_sdl_screen = SDL_SetVideoMode(0, 0, 0, (flags = get_screen_flags()));
e81b987f 263 if (plat_sdl_screen == NULL) {
528f2522 264 fprintf(stderr, "attempting SDL_SWSURFACE fallback\n");
265 screen_flags = SDL_SWSURFACE;
55104482 266 plat_sdl_screen = SDL_SetVideoMode(0, 0, 0, (flags = get_screen_flags()));
528f2522 267 if (plat_sdl_screen == NULL) {
268 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
269 goto fail;
270 }
e81b987f 271 }
272
273 if (plat_sdl_screen->w < 320 || plat_sdl_screen->h < 240) {
274 fprintf(stderr, "resolution %dx%d is too small, sorry.\n",
275 plat_sdl_screen->w, plat_sdl_screen->h);
276 goto fail;
277 }
278 }
248b5275 279 g_menuscreen_w = plat_sdl_screen->w;
280 g_menuscreen_h = plat_sdl_screen->h;
39639dd1 281 g_menuscreen_pp = g_menuscreen_w;
528f2522 282 screen_flags = flags;
283 window_w = plat_sdl_screen->w;
284 window_h = plat_sdl_screen->h;
da0cc556 285
e81b987f 286 plat_sdl_overlay = SDL_CreateYUVOverlay(plat_sdl_screen->w, plat_sdl_screen->h,
287 SDL_UYVY_OVERLAY, plat_sdl_screen);
288 if (plat_sdl_overlay != NULL) {
63f173a2 289 printf("plat_sdl: overlay: fmt %x, planes: %d, pitch: %d, hw: %d\n",
e81b987f 290 plat_sdl_overlay->format, plat_sdl_overlay->planes, *plat_sdl_overlay->pitches,
291 plat_sdl_overlay->hw_overlay);
292
55104482 293 // some platforms require "native" bpp for this to work
e81b987f 294 if (plat_sdl_overlay->hw_overlay)
295 overlay_works = 1;
296 else
297 fprintf(stderr, "warning: video overlay is not hardware accelerated, "
298 "not going to use it.\n");
299 SDL_FreeYUVOverlay(plat_sdl_overlay);
300 plat_sdl_overlay = NULL;
301 }
302 else
303 fprintf(stderr, "overlay is not available.\n");
304
1f84ba9f 305 SDL_VideoDriverName(vid_drv_name, sizeof(vid_drv_name));
c668921a 306 ret = -1;
307 try_gl = 1;
308 env = getenv("DISPLAY");
309 if (env && env[0] != ':') {
310 fprintf(stderr, "looks like a remote DISPLAY, "
311 "not trying GL (use PICOFE_GL=1 to override)\n");
312 // because some drivers just kill the program with no way to recover
313 try_gl = 0;
314 }
315 env = getenv("PICOFE_GL");
316 if (env)
317 try_gl = atoi(env);
2607df8a 318 if (try_gl) {
4388a645 319 ret = gl_init(display, &gl_quirks);
320 if (ret == 0) {
321 update_wm_display_window();
322 ret = gl_create(window, &gl_quirks, g_menuscreen_w, g_menuscreen_h);
323 }
2607df8a 324 }
e81b987f 325 if (ret == 0) {
c668921a 326 gl_announce();
e81b987f 327 gl_works = 1;
4388a645 328 gl_destroy();
e81b987f 329 }
330
331 i = 0;
332 vout_list[i++] = "SDL Window";
333 if (overlay_works) {
334 plat_target.vout_method = vout_mode_overlay = i;
335 vout_list[i++] = "Video Overlay";
9ba08314 336#ifdef SDL_OVERLAY_2X
ee0dca9e 337 plat_target.vout_method = vout_mode_overlay2x = i;
9ba08314 338 vout_list[i++] = "Video Overlay 2x";
339#endif
e81b987f 340 }
341 if (gl_works) {
342 plat_target.vout_method = vout_mode_gl = i;
343 vout_list[i++] = "OpenGL";
344 }
345 plat_target.vout_methods = vout_list;
346
39b2c61e 347 plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 1);
e81b987f 348 return 0;
349
350fail:
351 SDL_Quit();
352 return -1;
353}
354
355void plat_sdl_finish(void)
356{
357 if (plat_sdl_overlay != NULL) {
358 SDL_FreeYUVOverlay(plat_sdl_overlay);
359 plat_sdl_overlay = NULL;
360 }
361 if (plat_sdl_gl_active) {
4388a645 362 gl_destroy();
e81b987f 363 plat_sdl_gl_active = 0;
364 }
4388a645 365 gl_shutdown();
366
63f173a2 367 // restore back to initial resolution
368 // resolves black screen issue on R-Pi
369 if (strcmp(vid_drv_name, "x11") != 0)
370 SDL_SetVideoMode(fs_w, fs_h, 16, SDL_SWSURFACE);
e81b987f 371 SDL_Quit();
372}
373
374void plat_sdl_overlay_clear(void)
375{
248b5275 376 int pixels = plat_sdl_overlay->pitches[0]/2 * plat_sdl_overlay->h;
e81b987f 377 int *dst = (int *)plat_sdl_overlay->pixels[0];
378 int v = 0x10801080;
379
d07718a0 380 for (; pixels > 7; dst += 4, pixels -= 2 * 4)
e81b987f 381 dst[0] = dst[1] = dst[2] = dst[3] = v;
382
d07718a0 383 for (; pixels > 1; dst++, pixels -= 2)
e81b987f 384 *dst = v;
385}
386
fbbf5e3f 387int plat_sdl_is_windowed(void)
388{
389 return window_b != 0;
390}
f0615849 391
392int plat_sdl_is_fullscreen(void)
393{
394 // consider window title bar and screen menu here
395 return window_w >= fs_w && window_h >= fs_h - 2*window_b;
396}
5411ac21 397
398void plat_sdl_gl_scaling(int type)
399{
400 gl_quirks &= ~GL_QUIRK_SCALING_NEAREST;
401 if (type == 0)
402 gl_quirks |= GL_QUIRK_SCALING_NEAREST;
403}
0b3abad8 404
405void plat_sdl_gl_vsync(int on)
406{
407 gl_quirks &= ~GL_QUIRK_VSYNC_ON;
408 if (on)
409 gl_quirks |= GL_QUIRK_VSYNC_ON;
410}
411
e81b987f 412// vim:shiftwidth=2:expandtab