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