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