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 // skip GL recreation if window doesn't change - avoids flicker
53 if (plat_target.vout_method == vout_mode_gl && plat_sdl_gl_active
54 && plat_target.vout_fullscreen == old_fullscreen && !force)
59 if (plat_sdl_overlay != NULL) {
60 SDL_FreeYUVOverlay(plat_sdl_overlay);
61 plat_sdl_overlay = NULL;
63 if (plat_sdl_gl_active) {
65 plat_sdl_gl_active = 0;
68 if (plat_target.vout_method != 0) {
69 Uint32 flags = SDL_RESIZABLE | SDL_SWSURFACE;
73 if (plat_target.vout_fullscreen) {
74 flags |= SDL_FULLSCREEN;
79 // XXX: workaround some occasional mysterious deadlock in SDL_SetVideoMode
82 plat_sdl_screen = SDL_SetVideoMode(win_w, win_h, 0, flags);
83 if (plat_sdl_screen == NULL) {
84 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
85 plat_target.vout_method = 0;
89 if (plat_target.vout_method == vout_mode_overlay) {
90 plat_sdl_overlay = SDL_CreateYUVOverlay(w, h, SDL_UYVY_OVERLAY, plat_sdl_screen);
91 if (plat_sdl_overlay != NULL) {
92 if ((long)plat_sdl_overlay->pixels[0] & 3)
93 fprintf(stderr, "warning: overlay pointer is unaligned\n");
95 plat_sdl_overlay_clear();
98 fprintf(stderr, "warning: could not create overlay.\n");
99 plat_target.vout_method = 0;
102 else if (plat_target.vout_method == vout_mode_gl) {
103 plat_sdl_gl_active = (gl_init(display, window, &gl_quirks) == 0);
104 if (!plat_sdl_gl_active) {
105 fprintf(stderr, "warning: could not init GL.\n");
106 plat_target.vout_method = 0;
110 if (plat_target.vout_method == 0) {
113 plat_sdl_screen = SDL_SetVideoMode(w, h, 16, SDL_SWSURFACE);
114 if (plat_sdl_screen == NULL) {
115 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
120 old_fullscreen = plat_target.vout_fullscreen;
121 if (plat_sdl_resize_cb != NULL)
122 plat_sdl_resize_cb(plat_sdl_screen->w, plat_sdl_screen->h);
127 void plat_sdl_event_handler(void *event_)
129 static int was_active;
130 SDL_Event *event = event_;
132 switch (event->type) {
133 case SDL_VIDEORESIZE:
134 //printf("resize %dx%d\n", event->resize.w, event->resize.h);
135 if (plat_target.vout_method != 0
136 && !plat_target.vout_fullscreen && !old_fullscreen)
138 window_w = event->resize.w;
139 window_h = event->resize.h;
140 plat_sdl_change_video_mode(0, 0, 1);
143 case SDL_ACTIVEEVENT:
144 if (event->active.gain && !was_active) {
145 if (plat_sdl_overlay != NULL) {
146 SDL_Rect dstrect = { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
147 SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
149 else if (plat_sdl_gl_active) {
150 if (gl_quirks & GL_QUIRK_ACTIVATE_RECREATE) {
152 plat_sdl_gl_active = (gl_init(display, window, &gl_quirks) == 0);
156 // else SDL takes care of it
158 was_active = event->active.gain;
161 if (plat_sdl_quit_cb != NULL)
167 int plat_sdl_init(void)
169 static const char *vout_list[] = { NULL, NULL, NULL, NULL };
170 const SDL_VideoInfo *info;
171 SDL_SysWMinfo wminfo;
172 int overlay_works = 0;
176 ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
178 fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
182 info = SDL_GetVideoInfo();
184 fs_w = info->current_w;
185 fs_h = info->current_h;
186 printf("plat_sdl: using %dx%d as fullscreen resolution\n", fs_w, fs_h);
189 g_menuscreen_w = 640;
190 if (fs_w != 0 && g_menuscreen_w > fs_w)
191 g_menuscreen_w = fs_w;
192 g_menuscreen_h = 480;
195 if (info && info->wm_available && h > WM_DECORATION_H)
196 h -= WM_DECORATION_H;
197 if (g_menuscreen_h > h)
201 ret = plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 1);
203 plat_sdl_screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE);
204 if (plat_sdl_screen == NULL) {
205 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
209 if (plat_sdl_screen->w < 320 || plat_sdl_screen->h < 240) {
210 fprintf(stderr, "resolution %dx%d is too small, sorry.\n",
211 plat_sdl_screen->w, plat_sdl_screen->h);
215 g_menuscreen_w = window_w = plat_sdl_screen->w;
216 g_menuscreen_h = window_h = plat_sdl_screen->h;
218 plat_sdl_overlay = SDL_CreateYUVOverlay(plat_sdl_screen->w, plat_sdl_screen->h,
219 SDL_UYVY_OVERLAY, plat_sdl_screen);
220 if (plat_sdl_overlay != NULL) {
221 printf("plat_sdl: overlay: fmt %x, planes: %d, pitch: %d, hw: %d\n",
222 plat_sdl_overlay->format, plat_sdl_overlay->planes, *plat_sdl_overlay->pitches,
223 plat_sdl_overlay->hw_overlay);
225 if (plat_sdl_overlay->hw_overlay)
228 fprintf(stderr, "warning: video overlay is not hardware accelerated, "
229 "not going to use it.\n");
230 SDL_FreeYUVOverlay(plat_sdl_overlay);
231 plat_sdl_overlay = NULL;
234 fprintf(stderr, "overlay is not available.\n");
236 // get x11 display/window for GL
237 SDL_VideoDriverName(vid_drv_name, sizeof(vid_drv_name));
238 #ifdef SDL_VIDEO_DRIVER_X11
239 if (strcmp(vid_drv_name, "x11") == 0) {
240 SDL_VERSION(&wminfo.version);
241 ret = SDL_GetWMInfo(&wminfo);
243 display = wminfo.info.x11.display;
244 window = (void *)wminfo.info.x11.window;
249 ret = gl_init(display, window, &gl_quirks);
256 vout_list[i++] = "SDL Window";
258 plat_target.vout_method = vout_mode_overlay = i;
259 vout_list[i++] = "Video Overlay";
262 plat_target.vout_method = vout_mode_gl = i;
263 vout_list[i++] = "OpenGL";
265 plat_target.vout_methods = vout_list;
274 void plat_sdl_finish(void)
276 if (plat_sdl_overlay != NULL) {
277 SDL_FreeYUVOverlay(plat_sdl_overlay);
278 plat_sdl_overlay = NULL;
280 if (plat_sdl_gl_active) {
282 plat_sdl_gl_active = 0;
284 // restore back to initial resolution
285 // resolves black screen issue on R-Pi
286 if (strcmp(vid_drv_name, "x11") != 0)
287 SDL_SetVideoMode(fs_w, fs_h, 16, SDL_SWSURFACE);
291 void plat_sdl_overlay_clear(void)
293 int pixels = plat_sdl_overlay->w * plat_sdl_overlay->h;
294 int *dst = (int *)plat_sdl_overlay->pixels[0];
297 for (; pixels > 0; dst += 4, pixels -= 2 * 4)
298 dst[0] = dst[1] = dst[2] = dst[3] = v;
300 for (; pixels > 0; dst++, pixels -= 2)
304 // vim:shiftwidth=2:expandtab