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