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;
32 static int fs_w, fs_h;
33 static int old_fullscreen;
34 static int vout_mode_overlay = -1, vout_mode_gl = -1;
35 static void *display, *window;
38 /* w, h is layer resolution */
39 int plat_sdl_change_video_mode(int w, int h, int force)
41 static int prev_w, prev_h;
52 // invalid method might come from config..
53 if (plat_target.vout_method != 0
54 && plat_target.vout_method != vout_mode_overlay
55 && plat_target.vout_method != vout_mode_gl)
57 fprintf(stderr, "invalid vout_method: %d\n", plat_target.vout_method);
58 plat_target.vout_method = 0;
61 // skip GL recreation if window doesn't change - avoids flicker
62 if (plat_target.vout_method == vout_mode_gl && plat_sdl_gl_active
63 && plat_target.vout_fullscreen == old_fullscreen && !force)
68 if (plat_sdl_overlay != NULL) {
69 SDL_FreeYUVOverlay(plat_sdl_overlay);
70 plat_sdl_overlay = NULL;
72 if (plat_sdl_gl_active) {
74 plat_sdl_gl_active = 0;
77 if (plat_target.vout_method != 0) {
78 Uint32 flags = SDL_RESIZABLE | SDL_SWSURFACE;
82 if (plat_target.vout_fullscreen) {
83 flags |= SDL_FULLSCREEN;
88 // XXX: workaround some occasional mysterious deadlock in SDL_SetVideoMode
92 plat_sdl_screen = SDL_SetVideoMode(win_w, win_h, 0, flags);
93 if (plat_sdl_screen == NULL) {
94 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
95 plat_target.vout_method = 0;
99 if (plat_target.vout_method == vout_mode_overlay) {
100 plat_sdl_overlay = SDL_CreateYUVOverlay(w, h, SDL_UYVY_OVERLAY, plat_sdl_screen);
101 if (plat_sdl_overlay != NULL) {
102 if ((long)plat_sdl_overlay->pixels[0] & 3)
103 fprintf(stderr, "warning: overlay pointer is unaligned\n");
105 plat_sdl_overlay_clear();
108 fprintf(stderr, "warning: could not create overlay.\n");
109 plat_target.vout_method = 0;
112 else if (plat_target.vout_method == vout_mode_gl) {
113 plat_sdl_gl_active = (gl_init(display, window, &gl_quirks) == 0);
114 if (!plat_sdl_gl_active) {
115 fprintf(stderr, "warning: could not init GL.\n");
116 plat_target.vout_method = 0;
120 if (plat_target.vout_method == 0) {
123 plat_sdl_screen = SDL_SetVideoMode(w, h, 16, SDL_SWSURFACE);
124 if (plat_sdl_screen == NULL) {
125 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
130 old_fullscreen = plat_target.vout_fullscreen;
131 if (plat_sdl_resize_cb != NULL)
132 plat_sdl_resize_cb(plat_sdl_screen->w, plat_sdl_screen->h);
137 void plat_sdl_event_handler(void *event_)
139 static int was_active;
140 SDL_Event *event = event_;
142 switch (event->type) {
143 case SDL_VIDEORESIZE:
144 //printf("resize %dx%d\n", event->resize.w, event->resize.h);
145 if (plat_target.vout_method != 0
146 && !plat_target.vout_fullscreen && !old_fullscreen)
148 window_w = event->resize.w;
149 window_h = event->resize.h;
150 plat_sdl_change_video_mode(0, 0, 1);
153 case SDL_ACTIVEEVENT:
154 if (event->active.gain && !was_active) {
155 if (plat_sdl_overlay != NULL) {
156 SDL_Rect dstrect = { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
157 SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
159 else if (plat_sdl_gl_active) {
160 if (gl_quirks & GL_QUIRK_ACTIVATE_RECREATE) {
162 plat_sdl_gl_active = (gl_init(display, window, &gl_quirks) == 0);
166 // else SDL takes care of it
168 was_active = event->active.gain;
171 if (plat_sdl_quit_cb != NULL)
177 int plat_sdl_init(void)
179 static const char *vout_list[] = { NULL, NULL, NULL, NULL };
180 const SDL_VideoInfo *info;
181 SDL_SysWMinfo wminfo;
182 int overlay_works = 0;
186 ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
188 fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
192 info = SDL_GetVideoInfo();
194 fs_w = info->current_w;
195 fs_h = info->current_h;
196 printf("plat_sdl: using %dx%d as fullscreen resolution\n", fs_w, fs_h);
199 g_menuscreen_w = 640;
200 if (fs_w != 0 && g_menuscreen_w > fs_w)
201 g_menuscreen_w = fs_w;
202 g_menuscreen_h = 480;
205 if (info && info->wm_available && h > WM_DECORATION_H)
206 h -= WM_DECORATION_H;
207 if (g_menuscreen_h > h)
211 ret = plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 1);
213 plat_sdl_screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE);
214 if (plat_sdl_screen == NULL) {
215 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
219 if (plat_sdl_screen->w < 320 || plat_sdl_screen->h < 240) {
220 fprintf(stderr, "resolution %dx%d is too small, sorry.\n",
221 plat_sdl_screen->w, plat_sdl_screen->h);
225 g_menuscreen_w = window_w = plat_sdl_screen->w;
226 g_menuscreen_h = window_h = plat_sdl_screen->h;
228 // overlay/gl require native bpp in some cases..
229 plat_sdl_screen = SDL_SetVideoMode(g_menuscreen_w, g_menuscreen_h,
231 if (plat_sdl_screen == NULL) {
232 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
236 plat_sdl_overlay = SDL_CreateYUVOverlay(plat_sdl_screen->w, plat_sdl_screen->h,
237 SDL_UYVY_OVERLAY, plat_sdl_screen);
238 if (plat_sdl_overlay != NULL) {
239 printf("plat_sdl: overlay: fmt %x, planes: %d, pitch: %d, hw: %d\n",
240 plat_sdl_overlay->format, plat_sdl_overlay->planes, *plat_sdl_overlay->pitches,
241 plat_sdl_overlay->hw_overlay);
243 if (plat_sdl_overlay->hw_overlay)
246 fprintf(stderr, "warning: video overlay is not hardware accelerated, "
247 "not going to use it.\n");
248 SDL_FreeYUVOverlay(plat_sdl_overlay);
249 plat_sdl_overlay = NULL;
252 fprintf(stderr, "overlay is not available.\n");
254 // get x11 display/window for GL
255 SDL_VideoDriverName(vid_drv_name, sizeof(vid_drv_name));
256 #ifdef SDL_VIDEO_DRIVER_X11
257 if (strcmp(vid_drv_name, "x11") == 0) {
258 SDL_VERSION(&wminfo.version);
259 ret = SDL_GetWMInfo(&wminfo);
261 display = wminfo.info.x11.display;
262 window = (void *)wminfo.info.x11.window;
269 ret = gl_init(display, window, &gl_quirks);
276 vout_list[i++] = "SDL Window";
278 plat_target.vout_method = vout_mode_overlay = i;
279 vout_list[i++] = "Video Overlay";
282 plat_target.vout_method = vout_mode_gl = i;
283 vout_list[i++] = "OpenGL";
285 plat_target.vout_methods = vout_list;
294 void plat_sdl_finish(void)
296 if (plat_sdl_overlay != NULL) {
297 SDL_FreeYUVOverlay(plat_sdl_overlay);
298 plat_sdl_overlay = NULL;
300 if (plat_sdl_gl_active) {
302 plat_sdl_gl_active = 0;
304 // restore back to initial resolution
305 // resolves black screen issue on R-Pi
306 if (strcmp(vid_drv_name, "x11") != 0)
307 SDL_SetVideoMode(fs_w, fs_h, 16, SDL_SWSURFACE);
311 void plat_sdl_overlay_clear(void)
313 int pixels = plat_sdl_overlay->w * plat_sdl_overlay->h;
314 int *dst = (int *)plat_sdl_overlay->pixels[0];
317 for (; pixels > 0; dst += 4, pixels -= 2 * 4)
318 dst[0] = dst[1] = dst[2] = dst[3] = v;
320 for (; pixels > 0; dst++, pixels -= 2)
324 // vim:shiftwidth=2:expandtab