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