| 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, window_b; |
| 32 | static int fs_w, fs_h; |
| 33 | static int old_fullscreen; |
| 34 | static int screen_flags; |
| 35 | static int vout_mode_overlay = -1, vout_mode_overlay2x = -1, vout_mode_gl = -1; |
| 36 | static void *display, *window; |
| 37 | static int gl_quirks; |
| 38 | |
| 39 | /* w, h is layer resolution */ |
| 40 | int plat_sdl_change_video_mode(int w, int h, int force) |
| 41 | { |
| 42 | static int prev_w, prev_h; |
| 43 | |
| 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 | |
| 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 | |
| 61 | // invalid method might come from config.. |
| 62 | if (plat_target.vout_method != 0 |
| 63 | && plat_target.vout_method != vout_mode_overlay |
| 64 | && plat_target.vout_method != vout_mode_overlay2x |
| 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 | |
| 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 |
| 92 | // (seen on r-pi) |
| 93 | SDL_PumpEvents(); |
| 94 | |
| 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; |
| 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 | |
| 105 | if (plat_target.vout_method == vout_mode_overlay |
| 106 | || plat_target.vout_method == vout_mode_overlay2x) { |
| 107 | int W = plat_target.vout_method == vout_mode_overlay2x && w < 640 ? 2*w : w; |
| 108 | plat_sdl_overlay = SDL_CreateYUVOverlay(W, h, SDL_UYVY_OVERLAY, plat_sdl_screen); |
| 109 | if (plat_sdl_overlay != NULL) { |
| 110 | SDL_LockYUVOverlay(plat_sdl_overlay); |
| 111 | if ((long)plat_sdl_overlay->pixels[0] & 3) |
| 112 | fprintf(stderr, "warning: overlay pointer is unaligned\n"); |
| 113 | |
| 114 | plat_sdl_overlay_clear(); |
| 115 | SDL_UnlockYUVOverlay(plat_sdl_overlay); |
| 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) { |
| 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); |
| 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) { |
| 132 | Uint32 flags; |
| 133 | int win_w = w; |
| 134 | int win_h = h; |
| 135 | |
| 136 | #if defined SDL_SURFACE_SW |
| 137 | flags = SDL_SWSURFACE; |
| 138 | #elif defined(SDL_TRIPLEBUF) && defined(SDL_BUFFER_3X) |
| 139 | flags = SDL_HWSURFACE | SDL_TRIPLEBUF; |
| 140 | #else |
| 141 | flags = SDL_HWSURFACE | SDL_DOUBLEBUF; |
| 142 | #endif |
| 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 | |
| 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; |
| 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; |
| 162 | if (plat_sdl_resize_cb != NULL) |
| 163 | plat_sdl_resize_cb(plat_sdl_screen->w, plat_sdl_screen->h); |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | void plat_sdl_event_handler(void *event_) |
| 169 | { |
| 170 | static int was_active; |
| 171 | SDL_Event *event = event_; |
| 172 | |
| 173 | switch (event->type) { |
| 174 | case SDL_VIDEORESIZE: |
| 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 | { |
| 179 | window_w = event->resize.w & ~3; |
| 180 | window_h = event->resize.h & ~3; |
| 181 | plat_sdl_change_video_mode(0, 0, 1); |
| 182 | } |
| 183 | break; |
| 184 | case SDL_ACTIVEEVENT: |
| 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) { |
| 191 | if (gl_quirks & GL_QUIRK_ACTIVATE_RECREATE) { |
| 192 | int sw = plat_sdl_screen->w, sh = plat_sdl_screen->h; |
| 193 | gl_finish(); |
| 194 | plat_sdl_gl_active = (gl_init(display, window, &gl_quirks, sw, sh) == 0); |
| 195 | } |
| 196 | gl_flip(NULL, 0, 0); |
| 197 | } |
| 198 | // else SDL takes care of it |
| 199 | } |
| 200 | was_active = event->active.gain; |
| 201 | break; |
| 202 | case SDL_QUIT: |
| 203 | if (plat_sdl_quit_cb != NULL) |
| 204 | plat_sdl_quit_cb(); |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | int plat_sdl_init(void) |
| 210 | { |
| 211 | static const char *vout_list[] = { NULL, NULL, NULL, NULL, NULL }; |
| 212 | const SDL_VideoInfo *info; |
| 213 | SDL_SysWMinfo wminfo; |
| 214 | const char *env; |
| 215 | int overlay_works = 0; |
| 216 | int gl_works = 0; |
| 217 | int i, ret, h; |
| 218 | int try_gl; |
| 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; |
| 230 | if (info->wm_available) |
| 231 | window_b = WM_DECORATION_H; |
| 232 | printf("plat_sdl: using %dx%d as fullscreen resolution\n", fs_w, fs_h); |
| 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; |
| 241 | if (window_b && h > window_b) |
| 242 | h -= window_b; |
| 243 | if (g_menuscreen_h > h) |
| 244 | g_menuscreen_h = h; |
| 245 | } |
| 246 | |
| 247 | plat_sdl_screen = SDL_SetVideoMode(g_menuscreen_w, g_menuscreen_h, 16, SDL_HWSURFACE); |
| 248 | if (plat_sdl_screen == NULL) { |
| 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; |
| 263 | g_menuscreen_pp = g_menuscreen_w; |
| 264 | |
| 265 | // overlay/gl require native bpp in some cases.. |
| 266 | plat_sdl_screen = SDL_SetVideoMode(g_menuscreen_w, g_menuscreen_h, |
| 267 | 0, plat_sdl_screen->flags); |
| 268 | if (plat_sdl_screen == NULL) { |
| 269 | fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError()); |
| 270 | goto fail; |
| 271 | } |
| 272 | |
| 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) { |
| 276 | printf("plat_sdl: overlay: fmt %x, planes: %d, pitch: %d, hw: %d\n", |
| 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 | |
| 291 | // get x11 display/window for GL |
| 292 | SDL_VideoDriverName(vid_drv_name, sizeof(vid_drv_name)); |
| 293 | #ifdef SDL_VIDEO_DRIVER_X11 |
| 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 | } |
| 302 | #else |
| 303 | (void)wminfo; |
| 304 | #endif |
| 305 | |
| 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) |
| 319 | ret = gl_init(display, window, &gl_quirks, g_menuscreen_w, g_menuscreen_h); |
| 320 | if (ret == 0) { |
| 321 | gl_announce(); |
| 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"; |
| 331 | #ifdef SDL_OVERLAY_2X |
| 332 | vout_mode_overlay2x = i; |
| 333 | vout_list[i++] = "Video Overlay 2x"; |
| 334 | #endif |
| 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 | |
| 342 | plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 1); |
| 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 | } |
| 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); |
| 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 | |
| 373 | for (; pixels > 7; dst += 4, pixels -= 2 * 4) |
| 374 | dst[0] = dst[1] = dst[2] = dst[3] = v; |
| 375 | |
| 376 | for (; pixels > 1; dst++, pixels -= 2) |
| 377 | *dst = v; |
| 378 | } |
| 379 | |
| 380 | // vim:shiftwidth=2:expandtab |