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.
14 #include <SDL_syswm.h>
21 // XXX: maybe determine this instead..
22 #define WM_DECORATION_H 32
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);
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;
39 /* w, h is layer resolution */
40 int plat_sdl_change_video_mode(int w, int h, int force)
42 static int prev_w, prev_h;
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)
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)
67 fprintf(stderr, "invalid vout_method: %d\n", plat_target.vout_method);
68 plat_target.vout_method = 0;
71 if (plat_sdl_overlay != NULL) {
72 SDL_FreeYUVOverlay(plat_sdl_overlay);
73 plat_sdl_overlay = NULL;
75 if (plat_sdl_gl_active) {
77 plat_sdl_gl_active = 0;
80 if (plat_target.vout_method != 0) {
81 Uint32 flags = SDL_RESIZABLE | SDL_SWSURFACE;
85 if (plat_target.vout_fullscreen) {
86 flags |= SDL_FULLSCREEN;
91 // XXX: workaround some occasional mysterious deadlock in SDL_SetVideoMode
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);
99 if (plat_sdl_screen == NULL) {
100 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
101 plat_target.vout_method = 0;
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 if ((long)plat_sdl_overlay->pixels[0] & 3)
111 fprintf(stderr, "warning: overlay pointer is unaligned\n");
113 plat_sdl_overlay_clear();
116 fprintf(stderr, "warning: could not create overlay.\n");
117 plat_target.vout_method = 0;
120 else if (plat_target.vout_method == vout_mode_gl) {
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);
123 if (!plat_sdl_gl_active) {
124 fprintf(stderr, "warning: could not init GL.\n");
125 plat_target.vout_method = 0;
129 if (plat_target.vout_method == 0) {
134 #if defined SDL_SURFACE_SW
135 flags = SDL_SWSURFACE;
136 #elif defined(SDL_TRIPLEBUF) && defined(SDL_BUFFER_3X)
137 flags = SDL_HWSURFACE | SDL_TRIPLEBUF;
139 flags = SDL_HWSURFACE | SDL_DOUBLEBUF;
141 if (plat_target.vout_fullscreen && fs_w && fs_h) {
142 flags |= SDL_FULLSCREEN;
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;
153 if (plat_sdl_screen == NULL) {
154 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
159 old_fullscreen = plat_target.vout_fullscreen;
160 if (plat_sdl_resize_cb != NULL)
161 plat_sdl_resize_cb(plat_sdl_screen->w, plat_sdl_screen->h);
166 void plat_sdl_event_handler(void *event_)
168 static int was_active;
169 SDL_Event *event = event_;
171 switch (event->type) {
172 case SDL_VIDEORESIZE:
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)
177 window_w = event->resize.w & ~3;
178 window_h = event->resize.h & ~3;
179 plat_sdl_change_video_mode(0, 0, 1);
182 case SDL_ACTIVEEVENT:
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);
188 else if (plat_sdl_gl_active) {
189 if (gl_quirks & GL_QUIRK_ACTIVATE_RECREATE) {
190 int sw = plat_sdl_screen->w, sh = plat_sdl_screen->h;
192 plat_sdl_gl_active = (gl_init(display, window, &gl_quirks, sw, sh) == 0);
196 // else SDL takes care of it
198 was_active = event->active.gain;
201 if (plat_sdl_quit_cb != NULL)
207 int plat_sdl_init(void)
209 static const char *vout_list[] = { NULL, NULL, NULL, NULL, NULL };
210 const SDL_VideoInfo *info;
211 SDL_SysWMinfo wminfo;
213 int overlay_works = 0;
218 ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
220 fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
224 info = SDL_GetVideoInfo();
226 fs_w = info->current_w;
227 fs_h = info->current_h;
228 if (info->wm_available)
229 window_b = WM_DECORATION_H;
230 printf("plat_sdl: using %dx%d as fullscreen resolution\n", fs_w, fs_h);
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;
239 if (window_b && h > window_b)
241 if (g_menuscreen_h > h)
245 plat_sdl_screen = SDL_SetVideoMode(g_menuscreen_w, g_menuscreen_h, 16, SDL_HWSURFACE);
246 if (plat_sdl_screen == NULL) {
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());
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);
259 g_menuscreen_w = window_w = plat_sdl_screen->w;
260 g_menuscreen_h = window_h = plat_sdl_screen->h;
261 g_menuscreen_pp = g_menuscreen_w;
263 // overlay/gl require native bpp in some cases..
264 plat_sdl_screen = SDL_SetVideoMode(g_menuscreen_w, g_menuscreen_h,
265 0, plat_sdl_screen->flags);
266 if (plat_sdl_screen == NULL) {
267 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
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) {
274 printf("plat_sdl: overlay: fmt %x, planes: %d, pitch: %d, hw: %d\n",
275 plat_sdl_overlay->format, plat_sdl_overlay->planes, *plat_sdl_overlay->pitches,
276 plat_sdl_overlay->hw_overlay);
278 if (plat_sdl_overlay->hw_overlay)
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;
287 fprintf(stderr, "overlay is not available.\n");
289 // get x11 display/window for GL
290 SDL_VideoDriverName(vid_drv_name, sizeof(vid_drv_name));
291 #ifdef SDL_VIDEO_DRIVER_X11
292 if (strcmp(vid_drv_name, "x11") == 0) {
293 SDL_VERSION(&wminfo.version);
294 ret = SDL_GetWMInfo(&wminfo);
296 display = wminfo.info.x11.display;
297 window = (void *)wminfo.info.x11.window;
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
313 env = getenv("PICOFE_GL");
317 ret = gl_init(display, window, &gl_quirks, g_menuscreen_w, g_menuscreen_h);
325 vout_list[i++] = "SDL Window";
327 plat_target.vout_method = vout_mode_overlay = i;
328 vout_list[i++] = "Video Overlay";
329 #ifdef SDL_OVERLAY_2X
330 vout_mode_overlay2x = i;
331 vout_list[i++] = "Video Overlay 2x";
335 plat_target.vout_method = vout_mode_gl = i;
336 vout_list[i++] = "OpenGL";
338 plat_target.vout_methods = vout_list;
340 plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 1);
348 void plat_sdl_finish(void)
350 if (plat_sdl_overlay != NULL) {
351 SDL_FreeYUVOverlay(plat_sdl_overlay);
352 plat_sdl_overlay = NULL;
354 if (plat_sdl_gl_active) {
356 plat_sdl_gl_active = 0;
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);
365 void plat_sdl_overlay_clear(void)
367 int pixels = plat_sdl_overlay->w * plat_sdl_overlay->h;
368 int *dst = (int *)plat_sdl_overlay->pixels[0];
371 for (; pixels > 7; dst += 4, pixels -= 2 * 4)
372 dst[0] = dst[1] = dst[2] = dst[3] = v;
374 for (; pixels > 1; dst++, pixels -= 2)
378 // vim:shiftwidth=2:expandtab