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;
34 /* w, h is layer resolution */
35 int plat_sdl_change_video_mode(int w, int h, int force)
37 static int prev_w, prev_h;
48 // skip GL recreation if window doesn't change - avoids flicker
49 if (plat_target.vout_method == vout_mode_gl && plat_sdl_gl_active
50 && plat_target.vout_fullscreen == old_fullscreen && !force)
55 if (plat_sdl_overlay != NULL) {
56 SDL_FreeYUVOverlay(plat_sdl_overlay);
57 plat_sdl_overlay = NULL;
59 if (plat_sdl_gl_active) {
61 plat_sdl_gl_active = 0;
64 if (plat_target.vout_method != 0) {
65 Uint32 flags = SDL_RESIZABLE | SDL_SWSURFACE;
69 if (plat_target.vout_fullscreen) {
70 flags |= SDL_FULLSCREEN;
75 // XXX: workaround some occasional mysterious deadlock in SDL_SetVideoMode
78 plat_sdl_screen = SDL_SetVideoMode(win_w, win_h, 0, flags);
79 if (plat_sdl_screen == NULL) {
80 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
81 plat_target.vout_method = 0;
85 if (plat_target.vout_method == vout_mode_overlay) {
86 plat_sdl_overlay = SDL_CreateYUVOverlay(w, h, SDL_UYVY_OVERLAY, plat_sdl_screen);
87 if (plat_sdl_overlay != NULL) {
88 if ((long)plat_sdl_overlay->pixels[0] & 3)
89 fprintf(stderr, "warning: overlay pointer is unaligned\n");
91 plat_sdl_overlay_clear();
94 fprintf(stderr, "warning: could not create overlay.\n");
95 plat_target.vout_method = 0;
98 else if (plat_target.vout_method == vout_mode_gl) {
99 plat_sdl_gl_active = (gl_init(display, window) == 0);
100 if (!plat_sdl_gl_active) {
101 fprintf(stderr, "warning: could not init GL.\n");
102 plat_target.vout_method = 0;
106 if (plat_target.vout_method == 0) {
109 plat_sdl_screen = SDL_SetVideoMode(w, h, 16, SDL_SWSURFACE);
110 if (plat_sdl_screen == NULL) {
111 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
116 old_fullscreen = plat_target.vout_fullscreen;
120 void plat_sdl_event_handler(void *event_)
122 static int was_active;
123 SDL_Event *event = event_;
125 if (event->type == SDL_VIDEORESIZE) {
126 //printf("resize %dx%d\n", event->resize.w, event->resize.h);
127 if (plat_target.vout_method != 0
128 && !plat_target.vout_fullscreen && !old_fullscreen)
130 window_w = event->resize.w;
131 window_h = event->resize.h;
132 plat_sdl_change_video_mode(0, 0, 1);
135 else if (event->type == SDL_ACTIVEEVENT) {
136 if (event->active.gain && !was_active) {
137 if (plat_sdl_overlay != NULL) {
138 SDL_Rect dstrect = { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
139 SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
141 else if (plat_sdl_gl_active) {
144 // else SDL takes care of it
146 was_active = event->active.gain;
150 int plat_sdl_init(void)
152 static const char *vout_list[] = { NULL, NULL, NULL, NULL };
153 const SDL_VideoInfo *info;
154 SDL_SysWMinfo wminfo;
155 int overlay_works = 0;
159 ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
161 fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
165 info = SDL_GetVideoInfo();
167 fs_w = info->current_w;
168 fs_h = info->current_h;
171 g_menuscreen_w = 640;
172 if (fs_w != 0 && g_menuscreen_w > fs_w)
173 g_menuscreen_w = fs_w;
174 g_menuscreen_h = 480;
177 if (info && info->wm_available && h > WM_DECORATION_H)
178 h -= WM_DECORATION_H;
179 if (g_menuscreen_h > h)
183 ret = plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 1);
185 plat_sdl_screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE);
186 if (plat_sdl_screen == NULL) {
187 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
191 if (plat_sdl_screen->w < 320 || plat_sdl_screen->h < 240) {
192 fprintf(stderr, "resolution %dx%d is too small, sorry.\n",
193 plat_sdl_screen->w, plat_sdl_screen->h);
197 g_menuscreen_w = window_w = plat_sdl_screen->w;
198 g_menuscreen_h = window_h = plat_sdl_screen->h;
200 plat_sdl_overlay = SDL_CreateYUVOverlay(plat_sdl_screen->w, plat_sdl_screen->h,
201 SDL_UYVY_OVERLAY, plat_sdl_screen);
202 if (plat_sdl_overlay != NULL) {
203 printf("overlay: fmt %x, planes: %d, pitch: %d, hw: %d\n",
204 plat_sdl_overlay->format, plat_sdl_overlay->planes, *plat_sdl_overlay->pitches,
205 plat_sdl_overlay->hw_overlay);
207 if (plat_sdl_overlay->hw_overlay)
210 fprintf(stderr, "warning: video overlay is not hardware accelerated, "
211 "not going to use it.\n");
212 SDL_FreeYUVOverlay(plat_sdl_overlay);
213 plat_sdl_overlay = NULL;
216 fprintf(stderr, "overlay is not available.\n");
218 SDL_VERSION(&wminfo.version);
219 SDL_GetWMInfo(&wminfo);
220 display = wminfo.info.x11.display;
221 window = (void *)wminfo.info.x11.window;
223 ret = gl_init(display, window);
230 vout_list[i++] = "SDL Window";
232 plat_target.vout_method = vout_mode_overlay = i;
233 vout_list[i++] = "Video Overlay";
236 plat_target.vout_method = vout_mode_gl = i;
237 vout_list[i++] = "OpenGL";
239 plat_target.vout_methods = vout_list;
248 void plat_sdl_finish(void)
250 if (plat_sdl_overlay != NULL) {
251 SDL_FreeYUVOverlay(plat_sdl_overlay);
252 plat_sdl_overlay = NULL;
254 if (plat_sdl_gl_active) {
256 plat_sdl_gl_active = 0;
261 void plat_sdl_overlay_clear(void)
263 int pixels = plat_sdl_overlay->w * plat_sdl_overlay->h;
264 int *dst = (int *)plat_sdl_overlay->pixels[0];
267 for (; pixels > 0; dst += 4, pixels -= 2 * 4)
268 dst[0] = dst[1] = dst[2] = dst[3] = v;
270 for (; pixels > 0; dst++, pixels -= 2)
274 // vim:shiftwidth=2:expandtab