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;
28 static int window_w, window_h;
29 static int fs_w, fs_h;
30 static int old_fullscreen;
31 static int vout_mode_overlay = -1, vout_mode_gl = -1;
32 static void *display, *window;
35 /* w, h is layer resolution */
36 int plat_sdl_change_video_mode(int w, int h, int force)
38 static int prev_w, prev_h;
49 // skip GL recreation if window doesn't change - avoids flicker
50 if (plat_target.vout_method == vout_mode_gl && plat_sdl_gl_active
51 && plat_target.vout_fullscreen == old_fullscreen && !force)
56 if (plat_sdl_overlay != NULL) {
57 SDL_FreeYUVOverlay(plat_sdl_overlay);
58 plat_sdl_overlay = NULL;
60 if (plat_sdl_gl_active) {
62 plat_sdl_gl_active = 0;
65 if (plat_target.vout_method != 0) {
66 Uint32 flags = SDL_RESIZABLE | SDL_SWSURFACE;
70 if (plat_target.vout_fullscreen) {
71 flags |= SDL_FULLSCREEN;
76 // XXX: workaround some occasional mysterious deadlock in SDL_SetVideoMode
79 plat_sdl_screen = SDL_SetVideoMode(win_w, win_h, 0, flags);
80 if (plat_sdl_screen == NULL) {
81 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
82 plat_target.vout_method = 0;
86 if (plat_target.vout_method == vout_mode_overlay) {
87 plat_sdl_overlay = SDL_CreateYUVOverlay(w, h, SDL_UYVY_OVERLAY, plat_sdl_screen);
88 if (plat_sdl_overlay != NULL) {
89 if ((long)plat_sdl_overlay->pixels[0] & 3)
90 fprintf(stderr, "warning: overlay pointer is unaligned\n");
92 plat_sdl_overlay_clear();
95 fprintf(stderr, "warning: could not create overlay.\n");
96 plat_target.vout_method = 0;
99 else if (plat_target.vout_method == vout_mode_gl) {
100 plat_sdl_gl_active = (gl_init(display, window, &gl_quirks) == 0);
101 if (!plat_sdl_gl_active) {
102 fprintf(stderr, "warning: could not init GL.\n");
103 plat_target.vout_method = 0;
107 if (plat_target.vout_method == 0) {
110 plat_sdl_screen = SDL_SetVideoMode(w, h, 16, SDL_SWSURFACE);
111 if (plat_sdl_screen == NULL) {
112 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
117 old_fullscreen = plat_target.vout_fullscreen;
121 void plat_sdl_event_handler(void *event_)
123 static int was_active;
124 SDL_Event *event = event_;
126 if (event->type == SDL_VIDEORESIZE) {
127 //printf("resize %dx%d\n", event->resize.w, event->resize.h);
128 if (plat_target.vout_method != 0
129 && !plat_target.vout_fullscreen && !old_fullscreen)
131 window_w = event->resize.w;
132 window_h = event->resize.h;
133 plat_sdl_change_video_mode(0, 0, 1);
136 else if (event->type == SDL_ACTIVEEVENT) {
137 if (event->active.gain && !was_active) {
138 if (plat_sdl_overlay != NULL) {
139 SDL_Rect dstrect = { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
140 SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
142 else if (plat_sdl_gl_active) {
143 if (gl_quirks & GL_QUIRK_ACTIVATE_RECREATE) {
145 plat_sdl_gl_active = (gl_init(display, window, &gl_quirks) == 0);
149 // else SDL takes care of it
151 was_active = event->active.gain;
155 int plat_sdl_init(void)
157 static const char *vout_list[] = { NULL, NULL, NULL, NULL };
158 const SDL_VideoInfo *info;
159 SDL_SysWMinfo wminfo;
160 int overlay_works = 0;
164 ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
166 fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
170 info = SDL_GetVideoInfo();
172 fs_w = info->current_w;
173 fs_h = info->current_h;
176 g_menuscreen_w = 640;
177 if (fs_w != 0 && g_menuscreen_w > fs_w)
178 g_menuscreen_w = fs_w;
179 g_menuscreen_h = 480;
182 if (info && info->wm_available && h > WM_DECORATION_H)
183 h -= WM_DECORATION_H;
184 if (g_menuscreen_h > h)
188 ret = plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 1);
190 plat_sdl_screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE);
191 if (plat_sdl_screen == NULL) {
192 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
196 if (plat_sdl_screen->w < 320 || plat_sdl_screen->h < 240) {
197 fprintf(stderr, "resolution %dx%d is too small, sorry.\n",
198 plat_sdl_screen->w, plat_sdl_screen->h);
202 g_menuscreen_w = window_w = plat_sdl_screen->w;
203 g_menuscreen_h = window_h = plat_sdl_screen->h;
205 plat_sdl_overlay = SDL_CreateYUVOverlay(plat_sdl_screen->w, plat_sdl_screen->h,
206 SDL_UYVY_OVERLAY, plat_sdl_screen);
207 if (plat_sdl_overlay != NULL) {
208 printf("overlay: fmt %x, planes: %d, pitch: %d, hw: %d\n",
209 plat_sdl_overlay->format, plat_sdl_overlay->planes, *plat_sdl_overlay->pitches,
210 plat_sdl_overlay->hw_overlay);
212 if (plat_sdl_overlay->hw_overlay)
215 fprintf(stderr, "warning: video overlay is not hardware accelerated, "
216 "not going to use it.\n");
217 SDL_FreeYUVOverlay(plat_sdl_overlay);
218 plat_sdl_overlay = NULL;
221 fprintf(stderr, "overlay is not available.\n");
223 SDL_VERSION(&wminfo.version);
224 SDL_GetWMInfo(&wminfo);
225 display = wminfo.info.x11.display;
226 window = (void *)wminfo.info.x11.window;
228 ret = gl_init(display, window, &gl_quirks);
235 vout_list[i++] = "SDL Window";
237 plat_target.vout_method = vout_mode_overlay = i;
238 vout_list[i++] = "Video Overlay";
241 plat_target.vout_method = vout_mode_gl = i;
242 vout_list[i++] = "OpenGL";
244 plat_target.vout_methods = vout_list;
253 void plat_sdl_finish(void)
255 if (plat_sdl_overlay != NULL) {
256 SDL_FreeYUVOverlay(plat_sdl_overlay);
257 plat_sdl_overlay = NULL;
259 if (plat_sdl_gl_active) {
261 plat_sdl_gl_active = 0;
266 void plat_sdl_overlay_clear(void)
268 int pixels = plat_sdl_overlay->w * plat_sdl_overlay->h;
269 int *dst = (int *)plat_sdl_overlay->pixels[0];
272 for (; pixels > 0; dst += 4, pixels -= 2 * 4)
273 dst[0] = dst[1] = dst[2] = dst[3] = v;
275 for (; pixels > 0; dst++, pixels -= 2)
279 // vim:shiftwidth=2:expandtab