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_overlay2x = -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_overlay2x
56 && plat_target.vout_method != vout_mode_gl)
58 fprintf(stderr, "invalid vout_method: %d\n", plat_target.vout_method);
59 plat_target.vout_method = 0;
62 // skip GL recreation if window doesn't change - avoids flicker
63 if (plat_target.vout_method == vout_mode_gl && plat_sdl_gl_active
64 && plat_target.vout_fullscreen == old_fullscreen && !force)
69 if (plat_sdl_overlay != NULL) {
70 SDL_FreeYUVOverlay(plat_sdl_overlay);
71 plat_sdl_overlay = NULL;
73 if (plat_sdl_gl_active) {
75 plat_sdl_gl_active = 0;
78 if (plat_target.vout_method != 0) {
79 Uint32 flags = SDL_RESIZABLE | SDL_SWSURFACE;
83 if (plat_target.vout_fullscreen) {
84 flags |= SDL_FULLSCREEN;
89 // XXX: workaround some occasional mysterious deadlock in SDL_SetVideoMode
93 plat_sdl_screen = SDL_SetVideoMode(win_w, win_h, 0, flags);
94 if (plat_sdl_screen == NULL) {
95 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
96 plat_target.vout_method = 0;
100 if (plat_target.vout_method == vout_mode_overlay
101 || plat_target.vout_method == vout_mode_overlay2x) {
102 int W = plat_target.vout_method == vout_mode_overlay2x && w == 320 ? 2*w : w;
103 plat_sdl_overlay = SDL_CreateYUVOverlay(W, h, SDL_UYVY_OVERLAY, plat_sdl_screen);
104 if (plat_sdl_overlay != NULL) {
105 if ((long)plat_sdl_overlay->pixels[0] & 3)
106 fprintf(stderr, "warning: overlay pointer is unaligned\n");
108 plat_sdl_overlay_clear();
111 fprintf(stderr, "warning: could not create overlay.\n");
112 plat_target.vout_method = 0;
115 else if (plat_target.vout_method == vout_mode_gl) {
116 plat_sdl_gl_active = (gl_init(display, window, &gl_quirks) == 0);
117 if (!plat_sdl_gl_active) {
118 fprintf(stderr, "warning: could not init GL.\n");
119 plat_target.vout_method = 0;
123 if (plat_target.vout_method == 0) {
126 #if defined(SDL_TRIPLEBUF) && defined(SDL_BUFFER_3X)
127 plat_sdl_screen = SDL_SetVideoMode(w, h, 16, SDL_HWSURFACE | SDL_TRIPLEBUF);
129 plat_sdl_screen = SDL_SetVideoMode(w, h, 16, SDL_HWSURFACE | SDL_DOUBLEBUF);
131 if (plat_sdl_screen == NULL) {
132 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
137 old_fullscreen = plat_target.vout_fullscreen;
138 if (plat_sdl_resize_cb != NULL)
139 plat_sdl_resize_cb(plat_sdl_screen->w, plat_sdl_screen->h);
144 void plat_sdl_event_handler(void *event_)
146 static int was_active;
147 SDL_Event *event = event_;
149 switch (event->type) {
150 case SDL_VIDEORESIZE:
151 //printf("resize %dx%d\n", event->resize.w, event->resize.h);
152 if (plat_target.vout_method != 0
153 && !plat_target.vout_fullscreen && !old_fullscreen)
155 window_w = event->resize.w;
156 window_h = event->resize.h;
157 plat_sdl_change_video_mode(0, 0, 1);
160 case SDL_ACTIVEEVENT:
161 if (event->active.gain && !was_active) {
162 if (plat_sdl_overlay != NULL) {
163 SDL_Rect dstrect = { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
164 SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
166 else if (plat_sdl_gl_active) {
167 if (gl_quirks & GL_QUIRK_ACTIVATE_RECREATE) {
169 plat_sdl_gl_active = (gl_init(display, window, &gl_quirks) == 0);
173 // else SDL takes care of it
175 was_active = event->active.gain;
178 if (plat_sdl_quit_cb != NULL)
184 int plat_sdl_init(void)
186 static const char *vout_list[] = { NULL, NULL, NULL, NULL, NULL };
187 const SDL_VideoInfo *info;
188 SDL_SysWMinfo wminfo;
189 int overlay_works = 0;
193 ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
195 fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
199 info = SDL_GetVideoInfo();
201 fs_w = info->current_w;
202 fs_h = info->current_h;
203 printf("plat_sdl: using %dx%d as fullscreen resolution\n", fs_w, fs_h);
206 g_menuscreen_w = 640;
207 if (fs_w != 0 && g_menuscreen_w > fs_w)
208 g_menuscreen_w = fs_w;
209 g_menuscreen_h = 480;
212 if (info && info->wm_available && h > WM_DECORATION_H)
213 h -= WM_DECORATION_H;
214 if (g_menuscreen_h > h)
218 ret = plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 1);
220 plat_sdl_screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE);
221 if (plat_sdl_screen == NULL) {
222 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
226 if (plat_sdl_screen->w < 320 || plat_sdl_screen->h < 240) {
227 fprintf(stderr, "resolution %dx%d is too small, sorry.\n",
228 plat_sdl_screen->w, plat_sdl_screen->h);
232 g_menuscreen_w = window_w = plat_sdl_screen->w;
233 g_menuscreen_h = window_h = plat_sdl_screen->h;
235 // overlay/gl require native bpp in some cases..
236 plat_sdl_screen = SDL_SetVideoMode(g_menuscreen_w, g_menuscreen_h,
237 0, plat_sdl_screen->flags);
238 if (plat_sdl_screen == NULL) {
239 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
243 plat_sdl_overlay = SDL_CreateYUVOverlay(plat_sdl_screen->w, plat_sdl_screen->h,
244 SDL_UYVY_OVERLAY, plat_sdl_screen);
245 if (plat_sdl_overlay != NULL) {
246 printf("plat_sdl: overlay: fmt %x, planes: %d, pitch: %d, hw: %d\n",
247 plat_sdl_overlay->format, plat_sdl_overlay->planes, *plat_sdl_overlay->pitches,
248 plat_sdl_overlay->hw_overlay);
250 if (plat_sdl_overlay->hw_overlay)
253 fprintf(stderr, "warning: video overlay is not hardware accelerated, "
254 "not going to use it.\n");
255 SDL_FreeYUVOverlay(plat_sdl_overlay);
256 plat_sdl_overlay = NULL;
259 fprintf(stderr, "overlay is not available.\n");
261 // get x11 display/window for GL
262 SDL_VideoDriverName(vid_drv_name, sizeof(vid_drv_name));
263 #ifdef SDL_VIDEO_DRIVER_X11
264 if (strcmp(vid_drv_name, "x11") == 0) {
265 SDL_VERSION(&wminfo.version);
266 ret = SDL_GetWMInfo(&wminfo);
268 display = wminfo.info.x11.display;
269 window = (void *)wminfo.info.x11.window;
276 ret = gl_init(display, window, &gl_quirks);
283 vout_list[i++] = "SDL Window";
285 plat_target.vout_method = vout_mode_overlay = i;
286 vout_list[i++] = "Video Overlay";
287 #ifdef SDL_OVERLAY_2X
288 vout_mode_overlay2x = i;
289 vout_list[i++] = "Video Overlay 2x";
293 plat_target.vout_method = vout_mode_gl = i;
294 vout_list[i++] = "OpenGL";
296 plat_target.vout_methods = vout_list;
305 void plat_sdl_finish(void)
307 if (plat_sdl_overlay != NULL) {
308 SDL_FreeYUVOverlay(plat_sdl_overlay);
309 plat_sdl_overlay = NULL;
311 if (plat_sdl_gl_active) {
313 plat_sdl_gl_active = 0;
315 // restore back to initial resolution
316 // resolves black screen issue on R-Pi
317 if (strcmp(vid_drv_name, "x11") != 0)
318 SDL_SetVideoMode(fs_w, fs_h, 16, SDL_SWSURFACE);
322 void plat_sdl_overlay_clear(void)
324 int pixels = plat_sdl_overlay->w * plat_sdl_overlay->h;
325 int *dst = (int *)plat_sdl_overlay->pixels[0];
328 for (; pixels > 0; dst += 4, pixels -= 2 * 4)
329 dst[0] = dst[1] = dst[2] = dst[3] = v;
331 for (; pixels > 0; dst++, pixels -= 2)
335 // vim:shiftwidth=2:expandtab