2 * (C) GraÅžvydas "notaz" Ignotas, 2012
4 * This work is licensed under the terms of any of these licenses
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
9 * See the COPYING file in the top-level directory.
15 #include <SDL_syswm.h>
22 // XXX: maybe determine this instead..
23 #define WM_DECORATION_H 32
25 SDL_Surface *plat_sdl_screen;
26 SDL_Overlay *plat_sdl_overlay;
27 int plat_sdl_gl_active;
28 void (*plat_sdl_resize_cb)(int w, int h);
29 void (*plat_sdl_quit_cb)(void);
30 int plat_sdl_no_overlay2x;
32 static char vid_drv_name[32];
33 static int window_w, window_h, window_b;
34 static int fs_w, fs_h;
35 static int old_fullscreen;
36 static int vout_mode_overlay = -1, vout_mode_overlay2x = -1, vout_mode_gl = -1;
37 static void *display, *window;
39 static Uint32 screen_flags =
40 #if defined(SDL_SURFACE_SW)
42 #elif defined(SDL_TRIPLEBUF) && defined(SDL_BUFFER_3X)
43 SDL_HWSURFACE | SDL_TRIPLEBUF;
45 SDL_HWSURFACE | SDL_DOUBLEBUF;
48 static Uint32 get_screen_flags(void)
50 Uint32 flags = screen_flags;
51 flags &= ~(SDL_FULLSCREEN | SDL_RESIZABLE);
52 if (plat_target.vout_fullscreen && fs_w && fs_h)
53 flags |= SDL_FULLSCREEN;
55 flags |= SDL_RESIZABLE;
59 static void update_wm_display_window(void)
61 // get x11 display/window for GL
62 #ifdef SDL_VIDEO_DRIVER_X11
65 if (strcmp(vid_drv_name, "x11") == 0) {
66 SDL_VERSION(&wminfo.version);
67 ret = SDL_GetWMInfo(&wminfo);
69 display = wminfo.info.x11.display;
70 window = (void *)wminfo.info.x11.window;
76 /* w, h is layer resolution */
77 int plat_sdl_change_video_mode(int w, int h, int force)
79 static int prev_w, prev_h;
81 // skip GL recreation if window doesn't change - avoids flicker
82 if (plat_target.vout_method == vout_mode_gl && plat_sdl_gl_active
83 && plat_target.vout_fullscreen == old_fullscreen
84 && w == prev_w && h == prev_h && !force)
98 // invalid method might come from config..
99 if (plat_target.vout_method != 0
100 && plat_target.vout_method != vout_mode_overlay
101 && plat_target.vout_method != vout_mode_overlay2x
102 && plat_target.vout_method != vout_mode_gl)
104 fprintf(stderr, "invalid vout_method: %d\n", plat_target.vout_method);
105 plat_target.vout_method = 0;
108 if (plat_sdl_overlay != NULL) {
109 SDL_FreeYUVOverlay(plat_sdl_overlay);
110 plat_sdl_overlay = NULL;
112 if (plat_sdl_gl_active) {
114 plat_sdl_gl_active = 0;
117 if (plat_target.vout_method == 0 || (force && (window_w != w || window_h != h
118 || plat_target.vout_fullscreen != old_fullscreen
119 || !plat_sdl_screen->format || plat_sdl_screen->format->BitsPerPixel != 16))) {
120 Uint32 flags = get_screen_flags();
124 if (plat_target.vout_fullscreen && fs_w && fs_h) {
129 // XXX: workaround some occasional mysterious deadlock in SDL_SetVideoMode
132 if (!plat_sdl_screen || screen_flags != flags ||
133 plat_sdl_screen->w != win_w || plat_sdl_screen->h != win_h ||
134 (!plat_sdl_screen->format || plat_sdl_screen->format->BitsPerPixel != 16))
135 plat_sdl_screen = SDL_SetVideoMode(win_w, win_h, 16, flags);
136 if (plat_sdl_screen == NULL) {
137 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
140 if (vout_mode_gl != -1)
141 update_wm_display_window();
142 screen_flags = flags;
147 if (plat_target.vout_method == vout_mode_overlay
148 || plat_target.vout_method == vout_mode_overlay2x) {
149 int W = plat_target.vout_method == vout_mode_overlay2x && !plat_sdl_no_overlay2x ? 2*w : w;
150 plat_sdl_overlay = SDL_CreateYUVOverlay(W, h, SDL_UYVY_OVERLAY, plat_sdl_screen);
151 if (plat_sdl_overlay != NULL && SDL_LockYUVOverlay(plat_sdl_overlay) == 0) {
152 if ((uintptr_t)plat_sdl_overlay->pixels[0] & 3)
153 fprintf(stderr, "warning: overlay pointer is unaligned\n");
155 plat_sdl_overlay_clear();
157 SDL_UnlockYUVOverlay(plat_sdl_overlay);
160 fprintf(stderr, "warning: could not create overlay.\n");
161 plat_target.vout_method = 0;
164 else if (plat_target.vout_method == vout_mode_gl) {
165 int sw = plat_sdl_screen->w, sh = plat_sdl_screen->h;
166 plat_sdl_gl_active = (gl_create(window, &gl_quirks, sw, sh) == 0);
167 if (!plat_sdl_gl_active) {
168 fprintf(stderr, "warning: could not init GL.\n");
169 plat_target.vout_method = 0;
173 old_fullscreen = plat_target.vout_fullscreen;
174 if (plat_sdl_resize_cb != NULL)
175 plat_sdl_resize_cb(plat_sdl_screen->w, plat_sdl_screen->h);
180 void plat_sdl_event_handler(void *event_)
182 static int was_active;
183 SDL_Event *event = event_;
185 switch (event->type) {
186 case SDL_VIDEORESIZE:
187 //printf("resize %dx%d\n", event->resize.w, event->resize.h);
188 if ((plat_target.vout_method != 0 || window_b) && !plat_target.vout_fullscreen)
190 int win_w = event->resize.w & ~3;
191 int win_h = event->resize.h & ~3;
192 plat_sdl_change_video_mode(win_w, win_h, 1);
195 case SDL_ACTIVEEVENT:
196 if (event->active.gain && !was_active) {
197 if (plat_sdl_overlay != NULL) {
198 SDL_Rect dstrect = { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
199 SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
201 else if (plat_sdl_gl_active) {
202 if (gl_quirks & GL_QUIRK_ACTIVATE_RECREATE) {
203 int sw = plat_sdl_screen->w, sh = plat_sdl_screen->h;
205 plat_sdl_gl_active = (gl_create(window, &gl_quirks, sw, sh) == 0);
209 // else SDL takes care of it
211 was_active = event->active.gain;
214 if (plat_sdl_quit_cb != NULL)
220 int plat_sdl_init(void)
222 static const char *vout_list[] = { NULL, NULL, NULL, NULL, NULL };
223 const SDL_VideoInfo *info;
225 int overlay_works = 0;
231 ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
233 fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
237 info = SDL_GetVideoInfo();
239 fs_w = info->current_w;
240 fs_h = info->current_h;
241 if (info->wm_available)
242 window_b = WM_DECORATION_H;
243 printf("plat_sdl: using %dx%d as fullscreen resolution\n", fs_w, fs_h);
246 g_menuscreen_w = 640;
247 if (fs_w != 0 && g_menuscreen_w > fs_w)
248 g_menuscreen_w = fs_w;
249 g_menuscreen_h = 480;
252 if (window_b && h > window_b)
254 if (g_menuscreen_h > h)
258 plat_sdl_screen = SDL_SetVideoMode(g_menuscreen_w, g_menuscreen_h, 0,
259 (flags = get_screen_flags()));
260 if (plat_sdl_screen == NULL) {
261 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
262 plat_sdl_screen = SDL_SetVideoMode(0, 0, 0, (flags = get_screen_flags()));
263 if (plat_sdl_screen == NULL) {
264 fprintf(stderr, "attempting SDL_SWSURFACE fallback\n");
265 screen_flags = SDL_SWSURFACE;
266 plat_sdl_screen = SDL_SetVideoMode(0, 0, 0, (flags = get_screen_flags()));
267 if (plat_sdl_screen == NULL) {
268 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
273 if (plat_sdl_screen->w < 320 || plat_sdl_screen->h < 240) {
274 fprintf(stderr, "resolution %dx%d is too small, sorry.\n",
275 plat_sdl_screen->w, plat_sdl_screen->h);
279 g_menuscreen_w = plat_sdl_screen->w;
280 g_menuscreen_h = plat_sdl_screen->h;
281 g_menuscreen_pp = g_menuscreen_w;
282 screen_flags = flags;
283 window_w = plat_sdl_screen->w;
284 window_h = plat_sdl_screen->h;
286 plat_sdl_overlay = SDL_CreateYUVOverlay(plat_sdl_screen->w, plat_sdl_screen->h,
287 SDL_UYVY_OVERLAY, plat_sdl_screen);
288 if (plat_sdl_overlay != NULL) {
289 printf("plat_sdl: overlay: fmt %x, planes: %d, pitch: %d, hw: %d\n",
290 plat_sdl_overlay->format, plat_sdl_overlay->planes, *plat_sdl_overlay->pitches,
291 plat_sdl_overlay->hw_overlay);
293 // some platforms require "native" bpp for this to work
294 if (plat_sdl_overlay->hw_overlay)
297 fprintf(stderr, "warning: video overlay is not hardware accelerated, "
298 "not going to use it.\n");
299 SDL_FreeYUVOverlay(plat_sdl_overlay);
300 plat_sdl_overlay = NULL;
303 fprintf(stderr, "overlay is not available.\n");
305 SDL_VideoDriverName(vid_drv_name, sizeof(vid_drv_name));
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
315 env = getenv("PICOFE_GL");
319 ret = gl_init(display, &gl_quirks);
321 update_wm_display_window();
322 ret = gl_create(window, &gl_quirks, g_menuscreen_w, g_menuscreen_h);
332 vout_list[i++] = "SDL Window";
334 plat_target.vout_method = vout_mode_overlay = i;
335 vout_list[i++] = "Video Overlay";
336 #ifdef SDL_OVERLAY_2X
337 plat_target.vout_method = vout_mode_overlay2x = i;
338 vout_list[i++] = "Video Overlay 2x";
342 plat_target.vout_method = vout_mode_gl = i;
343 vout_list[i++] = "OpenGL";
345 plat_target.vout_methods = vout_list;
347 plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 1);
355 void plat_sdl_finish(void)
357 if (plat_sdl_overlay != NULL) {
358 SDL_FreeYUVOverlay(plat_sdl_overlay);
359 plat_sdl_overlay = NULL;
361 if (plat_sdl_gl_active) {
363 plat_sdl_gl_active = 0;
367 // restore back to initial resolution
368 // resolves black screen issue on R-Pi
369 if (strcmp(vid_drv_name, "x11") != 0)
370 SDL_SetVideoMode(fs_w, fs_h, 16, SDL_SWSURFACE);
374 void plat_sdl_overlay_clear(void)
376 int pixels = plat_sdl_overlay->pitches[0]/2 * plat_sdl_overlay->h;
377 int *dst = (int *)plat_sdl_overlay->pixels[0];
380 for (; pixels > 7; dst += 4, pixels -= 2 * 4)
381 dst[0] = dst[1] = dst[2] = dst[3] = v;
383 for (; pixels > 1; dst++, pixels -= 2)
387 int plat_sdl_is_windowed(void)
389 return window_b != 0;
392 int plat_sdl_is_fullscreen(void)
394 // consider window title bar and screen menu here
395 return window_w >= fs_w && window_h >= fs_h - 2*window_b;
398 void plat_sdl_gl_scaling(int type)
400 gl_quirks &= ~GL_QUIRK_SCALING_NEAREST;
402 gl_quirks |= GL_QUIRK_SCALING_NEAREST;
405 void plat_sdl_gl_vsync(int on)
407 gl_quirks &= ~GL_QUIRK_VSYNC_ON;
409 gl_quirks |= GL_QUIRK_VSYNC_ON;
412 // vim:shiftwidth=2:expandtab