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