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_quit_cb)(void);
29 static char vid_drv_name[32];
30 static int window_w, window_h;
31 static int fs_w, fs_h;
32 static int old_fullscreen;
33 static int vout_mode_overlay = -1, vout_mode_gl = -1;
34 static void *display, *window;
37 /* w, h is layer resolution */
38 int plat_sdl_change_video_mode(int w, int h, int force)
40 static int prev_w, prev_h;
51 // skip GL recreation if window doesn't change - avoids flicker
52 if (plat_target.vout_method == vout_mode_gl && plat_sdl_gl_active
53 && plat_target.vout_fullscreen == old_fullscreen && !force)
58 if (plat_sdl_overlay != NULL) {
59 SDL_FreeYUVOverlay(plat_sdl_overlay);
60 plat_sdl_overlay = NULL;
62 if (plat_sdl_gl_active) {
64 plat_sdl_gl_active = 0;
67 if (plat_target.vout_method != 0) {
68 Uint32 flags = SDL_RESIZABLE | SDL_SWSURFACE;
72 if (plat_target.vout_fullscreen) {
73 flags |= SDL_FULLSCREEN;
78 // XXX: workaround some occasional mysterious deadlock in SDL_SetVideoMode
81 plat_sdl_screen = SDL_SetVideoMode(win_w, win_h, 0, flags);
82 if (plat_sdl_screen == NULL) {
83 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
84 plat_target.vout_method = 0;
88 if (plat_target.vout_method == vout_mode_overlay) {
89 plat_sdl_overlay = SDL_CreateYUVOverlay(w, h, SDL_UYVY_OVERLAY, plat_sdl_screen);
90 if (plat_sdl_overlay != NULL) {
91 if ((long)plat_sdl_overlay->pixels[0] & 3)
92 fprintf(stderr, "warning: overlay pointer is unaligned\n");
94 plat_sdl_overlay_clear();
97 fprintf(stderr, "warning: could not create overlay.\n");
98 plat_target.vout_method = 0;
101 else if (plat_target.vout_method == vout_mode_gl) {
102 plat_sdl_gl_active = (gl_init(display, window, &gl_quirks) == 0);
103 if (!plat_sdl_gl_active) {
104 fprintf(stderr, "warning: could not init GL.\n");
105 plat_target.vout_method = 0;
109 if (plat_target.vout_method == 0) {
112 plat_sdl_screen = SDL_SetVideoMode(w, h, 16, SDL_SWSURFACE);
113 if (plat_sdl_screen == NULL) {
114 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
119 old_fullscreen = plat_target.vout_fullscreen;
123 void plat_sdl_event_handler(void *event_)
125 static int was_active;
126 SDL_Event *event = event_;
128 switch (event->type) {
129 case SDL_VIDEORESIZE:
130 //printf("resize %dx%d\n", event->resize.w, event->resize.h);
131 if (plat_target.vout_method != 0
132 && !plat_target.vout_fullscreen && !old_fullscreen)
134 window_w = event->resize.w;
135 window_h = event->resize.h;
136 plat_sdl_change_video_mode(0, 0, 1);
139 case SDL_ACTIVEEVENT:
140 if (event->active.gain && !was_active) {
141 if (plat_sdl_overlay != NULL) {
142 SDL_Rect dstrect = { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
143 SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
145 else if (plat_sdl_gl_active) {
146 if (gl_quirks & GL_QUIRK_ACTIVATE_RECREATE) {
148 plat_sdl_gl_active = (gl_init(display, window, &gl_quirks) == 0);
152 // else SDL takes care of it
154 was_active = event->active.gain;
157 if (plat_sdl_quit_cb != NULL)
163 int plat_sdl_init(void)
165 static const char *vout_list[] = { NULL, NULL, NULL, NULL };
166 const SDL_VideoInfo *info;
167 SDL_SysWMinfo wminfo;
168 int overlay_works = 0;
172 ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
174 fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
178 info = SDL_GetVideoInfo();
180 fs_w = info->current_w;
181 fs_h = info->current_h;
182 printf("plat_sdl: using %dx%d as fullscreen resolution\n", fs_w, fs_h);
185 g_menuscreen_w = 640;
186 if (fs_w != 0 && g_menuscreen_w > fs_w)
187 g_menuscreen_w = fs_w;
188 g_menuscreen_h = 480;
191 if (info && info->wm_available && h > WM_DECORATION_H)
192 h -= WM_DECORATION_H;
193 if (g_menuscreen_h > h)
197 ret = plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 1);
199 plat_sdl_screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE);
200 if (plat_sdl_screen == NULL) {
201 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
205 if (plat_sdl_screen->w < 320 || plat_sdl_screen->h < 240) {
206 fprintf(stderr, "resolution %dx%d is too small, sorry.\n",
207 plat_sdl_screen->w, plat_sdl_screen->h);
211 g_menuscreen_w = window_w = plat_sdl_screen->w;
212 g_menuscreen_h = window_h = plat_sdl_screen->h;
214 plat_sdl_overlay = SDL_CreateYUVOverlay(plat_sdl_screen->w, plat_sdl_screen->h,
215 SDL_UYVY_OVERLAY, plat_sdl_screen);
216 if (plat_sdl_overlay != NULL) {
217 printf("plat_sdl: overlay: fmt %x, planes: %d, pitch: %d, hw: %d\n",
218 plat_sdl_overlay->format, plat_sdl_overlay->planes, *plat_sdl_overlay->pitches,
219 plat_sdl_overlay->hw_overlay);
221 if (plat_sdl_overlay->hw_overlay)
224 fprintf(stderr, "warning: video overlay is not hardware accelerated, "
225 "not going to use it.\n");
226 SDL_FreeYUVOverlay(plat_sdl_overlay);
227 plat_sdl_overlay = NULL;
230 fprintf(stderr, "overlay is not available.\n");
232 // get x11 display/window for GL
233 SDL_VideoDriverName(vid_drv_name, sizeof(vid_drv_name));
234 #ifdef SDL_VIDEO_DRIVER_X11
235 if (strcmp(vid_drv_name, "x11") == 0) {
236 SDL_VERSION(&wminfo.version);
237 ret = SDL_GetWMInfo(&wminfo);
239 display = wminfo.info.x11.display;
240 window = (void *)wminfo.info.x11.window;
245 ret = gl_init(display, window, &gl_quirks);
252 vout_list[i++] = "SDL Window";
254 plat_target.vout_method = vout_mode_overlay = i;
255 vout_list[i++] = "Video Overlay";
258 plat_target.vout_method = vout_mode_gl = i;
259 vout_list[i++] = "OpenGL";
261 plat_target.vout_methods = vout_list;
270 void plat_sdl_finish(void)
272 if (plat_sdl_overlay != NULL) {
273 SDL_FreeYUVOverlay(plat_sdl_overlay);
274 plat_sdl_overlay = NULL;
276 if (plat_sdl_gl_active) {
278 plat_sdl_gl_active = 0;
280 // restore back to initial resolution
281 // resolves black screen issue on R-Pi
282 if (strcmp(vid_drv_name, "x11") != 0)
283 SDL_SetVideoMode(fs_w, fs_h, 16, SDL_SWSURFACE);
287 void plat_sdl_overlay_clear(void)
289 int pixels = plat_sdl_overlay->w * plat_sdl_overlay->h;
290 int *dst = (int *)plat_sdl_overlay->pixels[0];
293 for (; pixels > 0; dst += 4, pixels -= 2 * 4)
294 dst[0] = dst[1] = dst[2] = dst[3] = v;
296 for (; pixels > 0; dst++, pixels -= 2)
300 // vim:shiftwidth=2:expandtab