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); |
e81b987f | 109 | if (plat_sdl_overlay != NULL) { |
110 | if ((long)plat_sdl_overlay->pixels[0] & 3) | |
111 | fprintf(stderr, "warning: overlay pointer is unaligned\n"); | |
112 | ||
113 | plat_sdl_overlay_clear(); | |
114 | } | |
115 | else { | |
116 | fprintf(stderr, "warning: could not create overlay.\n"); | |
117 | plat_target.vout_method = 0; | |
118 | } | |
119 | } | |
120 | else if (plat_target.vout_method == vout_mode_gl) { | |
c7228611 | 121 | int sw = plat_sdl_screen->w, sh = plat_sdl_screen->h; |
122 | plat_sdl_gl_active = (gl_init(display, window, &gl_quirks, sw, sh) == 0); | |
e81b987f | 123 | if (!plat_sdl_gl_active) { |
124 | fprintf(stderr, "warning: could not init GL.\n"); | |
125 | plat_target.vout_method = 0; | |
126 | } | |
127 | } | |
128 | ||
129 | if (plat_target.vout_method == 0) { | |
74cb846a | 130 | Uint32 flags; |
e3ea3015 | 131 | int win_w = w; |
132 | int win_h = h; | |
e81b987f | 133 | |
2de059ca | 134 | #if defined SDL_SURFACE_SW |
74cb846a | 135 | flags = SDL_SWSURFACE; |
2de059ca | 136 | #elif defined(SDL_TRIPLEBUF) && defined(SDL_BUFFER_3X) |
74cb846a | 137 | flags = SDL_HWSURFACE | SDL_TRIPLEBUF; |
34e55f23 | 138 | #else |
74cb846a | 139 | flags = SDL_HWSURFACE | SDL_DOUBLEBUF; |
34e55f23 | 140 | #endif |
74cb846a | 141 | if (plat_target.vout_fullscreen && fs_w && fs_h) { |
142 | flags |= SDL_FULLSCREEN; | |
143 | win_w = fs_w; | |
144 | win_h = fs_h; | |
145 | } | |
146 | ||
147 | SDL_PumpEvents(); | |
148 | ||
533726df | 149 | if (!plat_sdl_screen || screen_flags != flags || |
150 | plat_sdl_screen->w != win_w || plat_sdl_screen->h != win_h) | |
151 | plat_sdl_screen = SDL_SetVideoMode(win_w, win_h, 16, flags); | |
152 | screen_flags = flags; | |
e81b987f | 153 | if (plat_sdl_screen == NULL) { |
154 | fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError()); | |
155 | return -1; | |
156 | } | |
157 | } | |
158 | ||
159 | old_fullscreen = plat_target.vout_fullscreen; | |
e288d776 | 160 | if (plat_sdl_resize_cb != NULL) |
161 | plat_sdl_resize_cb(plat_sdl_screen->w, plat_sdl_screen->h); | |
162 | ||
e81b987f | 163 | return 0; |
164 | } | |
165 | ||
166 | void plat_sdl_event_handler(void *event_) | |
167 | { | |
e54719ef | 168 | static int was_active; |
e81b987f | 169 | SDL_Event *event = event_; |
170 | ||
1f84ba9f | 171 | switch (event->type) { |
172 | case SDL_VIDEORESIZE: | |
e81b987f | 173 | //printf("resize %dx%d\n", event->resize.w, event->resize.h); |
174 | if (plat_target.vout_method != 0 | |
175 | && !plat_target.vout_fullscreen && !old_fullscreen) | |
176 | { | |
533726df | 177 | window_w = event->resize.w & ~3; |
178 | window_h = event->resize.h & ~3; | |
e54719ef | 179 | plat_sdl_change_video_mode(0, 0, 1); |
180 | } | |
1f84ba9f | 181 | break; |
182 | case SDL_ACTIVEEVENT: | |
e54719ef | 183 | if (event->active.gain && !was_active) { |
184 | if (plat_sdl_overlay != NULL) { | |
185 | SDL_Rect dstrect = { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h }; | |
186 | SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect); | |
187 | } | |
188 | else if (plat_sdl_gl_active) { | |
0d645bc5 | 189 | if (gl_quirks & GL_QUIRK_ACTIVATE_RECREATE) { |
c7228611 | 190 | int sw = plat_sdl_screen->w, sh = plat_sdl_screen->h; |
0d645bc5 | 191 | gl_finish(); |
c7228611 | 192 | plat_sdl_gl_active = (gl_init(display, window, &gl_quirks, sw, sh) == 0); |
0d645bc5 | 193 | } |
e54719ef | 194 | gl_flip(NULL, 0, 0); |
195 | } | |
196 | // else SDL takes care of it | |
e81b987f | 197 | } |
e54719ef | 198 | was_active = event->active.gain; |
1f84ba9f | 199 | break; |
200 | case SDL_QUIT: | |
201 | if (plat_sdl_quit_cb != NULL) | |
202 | plat_sdl_quit_cb(); | |
203 | break; | |
e81b987f | 204 | } |
205 | } | |
206 | ||
207 | int plat_sdl_init(void) | |
208 | { | |
9ba08314 | 209 | static const char *vout_list[] = { NULL, NULL, NULL, NULL, NULL }; |
e81b987f | 210 | const SDL_VideoInfo *info; |
211 | SDL_SysWMinfo wminfo; | |
c668921a | 212 | const char *env; |
e81b987f | 213 | int overlay_works = 0; |
214 | int gl_works = 0; | |
215 | int i, ret, h; | |
c668921a | 216 | int try_gl; |
e81b987f | 217 | |
218 | ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE); | |
219 | if (ret != 0) { | |
220 | fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError()); | |
221 | return -1; | |
222 | } | |
223 | ||
224 | info = SDL_GetVideoInfo(); | |
225 | if (info != NULL) { | |
226 | fs_w = info->current_w; | |
227 | fs_h = info->current_h; | |
533726df | 228 | if (info->wm_available) |
229 | window_b = WM_DECORATION_H; | |
63f173a2 | 230 | printf("plat_sdl: using %dx%d as fullscreen resolution\n", fs_w, fs_h); |
e81b987f | 231 | } |
232 | ||
233 | g_menuscreen_w = 640; | |
234 | if (fs_w != 0 && g_menuscreen_w > fs_w) | |
235 | g_menuscreen_w = fs_w; | |
236 | g_menuscreen_h = 480; | |
237 | if (fs_h != 0) { | |
238 | h = fs_h; | |
533726df | 239 | if (window_b && h > window_b) |
240 | h -= window_b; | |
e81b987f | 241 | if (g_menuscreen_h > h) |
242 | g_menuscreen_h = h; | |
243 | } | |
244 | ||
533726df | 245 | plat_sdl_screen = SDL_SetVideoMode(g_menuscreen_w, g_menuscreen_h, 16, SDL_HWSURFACE); |
246 | if (plat_sdl_screen == NULL) { | |
e81b987f | 247 | plat_sdl_screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE); |
248 | if (plat_sdl_screen == NULL) { | |
249 | fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError()); | |
250 | goto fail; | |
251 | } | |
252 | ||
253 | if (plat_sdl_screen->w < 320 || plat_sdl_screen->h < 240) { | |
254 | fprintf(stderr, "resolution %dx%d is too small, sorry.\n", | |
255 | plat_sdl_screen->w, plat_sdl_screen->h); | |
256 | goto fail; | |
257 | } | |
258 | } | |
259 | g_menuscreen_w = window_w = plat_sdl_screen->w; | |
260 | g_menuscreen_h = window_h = plat_sdl_screen->h; | |
39639dd1 | 261 | g_menuscreen_pp = g_menuscreen_w; |
e81b987f | 262 | |
da0cc556 | 263 | // overlay/gl require native bpp in some cases.. |
264 | plat_sdl_screen = SDL_SetVideoMode(g_menuscreen_w, g_menuscreen_h, | |
17def48f | 265 | 0, plat_sdl_screen->flags); |
da0cc556 | 266 | if (plat_sdl_screen == NULL) { |
267 | fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError()); | |
268 | goto fail; | |
269 | } | |
270 | ||
e81b987f | 271 | plat_sdl_overlay = SDL_CreateYUVOverlay(plat_sdl_screen->w, plat_sdl_screen->h, |
272 | SDL_UYVY_OVERLAY, plat_sdl_screen); | |
273 | if (plat_sdl_overlay != NULL) { | |
63f173a2 | 274 | printf("plat_sdl: overlay: fmt %x, planes: %d, pitch: %d, hw: %d\n", |
e81b987f | 275 | plat_sdl_overlay->format, plat_sdl_overlay->planes, *plat_sdl_overlay->pitches, |
276 | plat_sdl_overlay->hw_overlay); | |
277 | ||
278 | if (plat_sdl_overlay->hw_overlay) | |
279 | overlay_works = 1; | |
280 | else | |
281 | fprintf(stderr, "warning: video overlay is not hardware accelerated, " | |
282 | "not going to use it.\n"); | |
283 | SDL_FreeYUVOverlay(plat_sdl_overlay); | |
284 | plat_sdl_overlay = NULL; | |
285 | } | |
286 | else | |
287 | fprintf(stderr, "overlay is not available.\n"); | |
288 | ||
1f84ba9f | 289 | // get x11 display/window for GL |
290 | SDL_VideoDriverName(vid_drv_name, sizeof(vid_drv_name)); | |
215e7ed2 | 291 | #ifdef SDL_VIDEO_DRIVER_X11 |
1f84ba9f | 292 | if (strcmp(vid_drv_name, "x11") == 0) { |
293 | SDL_VERSION(&wminfo.version); | |
294 | ret = SDL_GetWMInfo(&wminfo); | |
295 | if (ret > 0) { | |
296 | display = wminfo.info.x11.display; | |
297 | window = (void *)wminfo.info.x11.window; | |
298 | } | |
299 | } | |
b57ed12e GI |
300 | #else |
301 | (void)wminfo; | |
215e7ed2 | 302 | #endif |
e81b987f | 303 | |
c668921a | 304 | ret = -1; |
305 | try_gl = 1; | |
306 | env = getenv("DISPLAY"); | |
307 | if (env && env[0] != ':') { | |
308 | fprintf(stderr, "looks like a remote DISPLAY, " | |
309 | "not trying GL (use PICOFE_GL=1 to override)\n"); | |
310 | // because some drivers just kill the program with no way to recover | |
311 | try_gl = 0; | |
312 | } | |
313 | env = getenv("PICOFE_GL"); | |
314 | if (env) | |
315 | try_gl = atoi(env); | |
316 | if (try_gl) | |
c7228611 | 317 | ret = gl_init(display, window, &gl_quirks, g_menuscreen_w, g_menuscreen_h); |
e81b987f | 318 | if (ret == 0) { |
c668921a | 319 | gl_announce(); |
e81b987f | 320 | gl_works = 1; |
321 | gl_finish(); | |
322 | } | |
323 | ||
324 | i = 0; | |
325 | vout_list[i++] = "SDL Window"; | |
326 | if (overlay_works) { | |
327 | plat_target.vout_method = vout_mode_overlay = i; | |
328 | vout_list[i++] = "Video Overlay"; | |
9ba08314 | 329 | #ifdef SDL_OVERLAY_2X |
330 | vout_mode_overlay2x = i; | |
331 | vout_list[i++] = "Video Overlay 2x"; | |
332 | #endif | |
e81b987f | 333 | } |
334 | if (gl_works) { | |
335 | plat_target.vout_method = vout_mode_gl = i; | |
336 | vout_list[i++] = "OpenGL"; | |
337 | } | |
338 | plat_target.vout_methods = vout_list; | |
339 | ||
39b2c61e | 340 | plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 1); |
e81b987f | 341 | return 0; |
342 | ||
343 | fail: | |
344 | SDL_Quit(); | |
345 | return -1; | |
346 | } | |
347 | ||
348 | void plat_sdl_finish(void) | |
349 | { | |
350 | if (plat_sdl_overlay != NULL) { | |
351 | SDL_FreeYUVOverlay(plat_sdl_overlay); | |
352 | plat_sdl_overlay = NULL; | |
353 | } | |
354 | if (plat_sdl_gl_active) { | |
355 | gl_finish(); | |
356 | plat_sdl_gl_active = 0; | |
357 | } | |
63f173a2 | 358 | // restore back to initial resolution |
359 | // resolves black screen issue on R-Pi | |
360 | if (strcmp(vid_drv_name, "x11") != 0) | |
361 | SDL_SetVideoMode(fs_w, fs_h, 16, SDL_SWSURFACE); | |
e81b987f | 362 | SDL_Quit(); |
363 | } | |
364 | ||
365 | void plat_sdl_overlay_clear(void) | |
366 | { | |
367 | int pixels = plat_sdl_overlay->w * plat_sdl_overlay->h; | |
368 | int *dst = (int *)plat_sdl_overlay->pixels[0]; | |
369 | int v = 0x10801080; | |
370 | ||
d07718a0 | 371 | for (; pixels > 7; dst += 4, pixels -= 2 * 4) |
e81b987f | 372 | dst[0] = dst[1] = dst[2] = dst[3] = v; |
373 | ||
d07718a0 | 374 | for (; pixels > 1; dst++, pixels -= 2) |
e81b987f | 375 | *dst = v; |
376 | } | |
377 | ||
378 | // vim:shiftwidth=2:expandtab |