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 int window_w, window_h;
30 static int fs_w, fs_h;
31 static int old_fullscreen;
32 static int vout_mode_overlay = -1, vout_mode_gl = -1;
33 static void *display, *window;
36 /* w, h is layer resolution */
37 int plat_sdl_change_video_mode(int w, int h, int force)
39 static int prev_w, prev_h;
50 // skip GL recreation if window doesn't change - avoids flicker
51 if (plat_target.vout_method == vout_mode_gl && plat_sdl_gl_active
52 && plat_target.vout_fullscreen == old_fullscreen && !force)
57 if (plat_sdl_overlay != NULL) {
58 SDL_FreeYUVOverlay(plat_sdl_overlay);
59 plat_sdl_overlay = NULL;
61 if (plat_sdl_gl_active) {
63 plat_sdl_gl_active = 0;
66 if (plat_target.vout_method != 0) {
67 Uint32 flags = SDL_RESIZABLE | SDL_SWSURFACE;
71 if (plat_target.vout_fullscreen) {
72 flags |= SDL_FULLSCREEN;
77 // XXX: workaround some occasional mysterious deadlock in SDL_SetVideoMode
80 plat_sdl_screen = SDL_SetVideoMode(win_w, win_h, 0, flags);
81 if (plat_sdl_screen == NULL) {
82 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
83 plat_target.vout_method = 0;
87 if (plat_target.vout_method == vout_mode_overlay) {
88 plat_sdl_overlay = SDL_CreateYUVOverlay(w, h, SDL_UYVY_OVERLAY, plat_sdl_screen);
89 if (plat_sdl_overlay != NULL) {
90 if ((long)plat_sdl_overlay->pixels[0] & 3)
91 fprintf(stderr, "warning: overlay pointer is unaligned\n");
93 plat_sdl_overlay_clear();
96 fprintf(stderr, "warning: could not create overlay.\n");
97 plat_target.vout_method = 0;
100 else if (plat_target.vout_method == vout_mode_gl) {
101 plat_sdl_gl_active = (gl_init(display, window, &gl_quirks) == 0);
102 if (!plat_sdl_gl_active) {
103 fprintf(stderr, "warning: could not init GL.\n");
104 plat_target.vout_method = 0;
108 if (plat_target.vout_method == 0) {
111 plat_sdl_screen = SDL_SetVideoMode(w, h, 16, SDL_SWSURFACE);
112 if (plat_sdl_screen == NULL) {
113 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
118 old_fullscreen = plat_target.vout_fullscreen;
122 void plat_sdl_event_handler(void *event_)
124 static int was_active;
125 SDL_Event *event = event_;
127 switch (event->type) {
128 case SDL_VIDEORESIZE:
129 //printf("resize %dx%d\n", event->resize.w, event->resize.h);
130 if (plat_target.vout_method != 0
131 && !plat_target.vout_fullscreen && !old_fullscreen)
133 window_w = event->resize.w;
134 window_h = event->resize.h;
135 plat_sdl_change_video_mode(0, 0, 1);
138 case SDL_ACTIVEEVENT:
139 if (event->active.gain && !was_active) {
140 if (plat_sdl_overlay != NULL) {
141 SDL_Rect dstrect = { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
142 SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
144 else if (plat_sdl_gl_active) {
145 if (gl_quirks & GL_QUIRK_ACTIVATE_RECREATE) {
147 plat_sdl_gl_active = (gl_init(display, window, &gl_quirks) == 0);
151 // else SDL takes care of it
153 was_active = event->active.gain;
156 if (plat_sdl_quit_cb != NULL)
162 int plat_sdl_init(void)
164 static const char *vout_list[] = { NULL, NULL, NULL, NULL };
165 const SDL_VideoInfo *info;
166 SDL_SysWMinfo wminfo;
167 char vid_drv_name[64];
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;
184 g_menuscreen_w = 640;
185 if (fs_w != 0 && g_menuscreen_w > fs_w)
186 g_menuscreen_w = fs_w;
187 g_menuscreen_h = 480;
190 if (info && info->wm_available && h > WM_DECORATION_H)
191 h -= WM_DECORATION_H;
192 if (g_menuscreen_h > h)
196 ret = plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 1);
198 plat_sdl_screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE);
199 if (plat_sdl_screen == NULL) {
200 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
204 if (plat_sdl_screen->w < 320 || plat_sdl_screen->h < 240) {
205 fprintf(stderr, "resolution %dx%d is too small, sorry.\n",
206 plat_sdl_screen->w, plat_sdl_screen->h);
210 g_menuscreen_w = window_w = plat_sdl_screen->w;
211 g_menuscreen_h = window_h = plat_sdl_screen->h;
213 plat_sdl_overlay = SDL_CreateYUVOverlay(plat_sdl_screen->w, plat_sdl_screen->h,
214 SDL_UYVY_OVERLAY, plat_sdl_screen);
215 if (plat_sdl_overlay != NULL) {
216 printf("overlay: fmt %x, planes: %d, pitch: %d, hw: %d\n",
217 plat_sdl_overlay->format, plat_sdl_overlay->planes, *plat_sdl_overlay->pitches,
218 plat_sdl_overlay->hw_overlay);
220 if (plat_sdl_overlay->hw_overlay)
223 fprintf(stderr, "warning: video overlay is not hardware accelerated, "
224 "not going to use it.\n");
225 SDL_FreeYUVOverlay(plat_sdl_overlay);
226 plat_sdl_overlay = NULL;
229 fprintf(stderr, "overlay is not available.\n");
231 // get x11 display/window for GL
232 SDL_VideoDriverName(vid_drv_name, sizeof(vid_drv_name));
233 #ifdef SDL_VIDEO_DRIVER_X11
234 if (strcmp(vid_drv_name, "x11") == 0) {
235 SDL_VERSION(&wminfo.version);
236 ret = SDL_GetWMInfo(&wminfo);
238 display = wminfo.info.x11.display;
239 window = (void *)wminfo.info.x11.window;
244 ret = gl_init(display, window, &gl_quirks);
251 vout_list[i++] = "SDL Window";
253 plat_target.vout_method = vout_mode_overlay = i;
254 vout_list[i++] = "Video Overlay";
257 plat_target.vout_method = vout_mode_gl = i;
258 vout_list[i++] = "OpenGL";
260 plat_target.vout_methods = vout_list;
269 void plat_sdl_finish(void)
271 if (plat_sdl_overlay != NULL) {
272 SDL_FreeYUVOverlay(plat_sdl_overlay);
273 plat_sdl_overlay = NULL;
275 if (plat_sdl_gl_active) {
277 plat_sdl_gl_active = 0;
282 void plat_sdl_overlay_clear(void)
284 int pixels = plat_sdl_overlay->w * plat_sdl_overlay->h;
285 int *dst = (int *)plat_sdl_overlay->pixels[0];
288 for (; pixels > 0; dst += 4, pixels -= 2 * 4)
289 dst[0] = dst[1] = dst[2] = dst[3] = v;
291 for (; pixels > 0; dst++, pixels -= 2)
295 // vim:shiftwidth=2:expandtab