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 int plat_sdl_change_video_mode(int w, int h)
36 static int prev_w, prev_h;
47 if (plat_sdl_overlay != NULL) {
48 SDL_FreeYUVOverlay(plat_sdl_overlay);
49 plat_sdl_overlay = NULL;
51 if (plat_sdl_gl_active) {
53 plat_sdl_gl_active = 0;
56 if (plat_target.vout_method != 0) {
57 Uint32 flags = SDL_RESIZABLE | SDL_SWSURFACE;
61 if (plat_target.vout_fullscreen) {
62 flags |= SDL_FULLSCREEN;
67 // XXX: workaround some occasional mysterious deadlock in SDL_SetVideoMode
70 plat_sdl_screen = SDL_SetVideoMode(win_w, win_h, 0, flags);
71 if (plat_sdl_screen == NULL) {
72 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
73 plat_target.vout_method = 0;
77 if (plat_target.vout_method == vout_mode_overlay) {
78 plat_sdl_overlay = SDL_CreateYUVOverlay(w, h, SDL_UYVY_OVERLAY, plat_sdl_screen);
79 if (plat_sdl_overlay != NULL) {
80 if ((long)plat_sdl_overlay->pixels[0] & 3)
81 fprintf(stderr, "warning: overlay pointer is unaligned\n");
83 plat_sdl_overlay_clear();
86 fprintf(stderr, "warning: could not create overlay.\n");
87 plat_target.vout_method = 0;
90 else if (plat_target.vout_method == vout_mode_gl) {
91 plat_sdl_gl_active = (gl_init(display, window) == 0);
92 if (!plat_sdl_gl_active) {
93 fprintf(stderr, "warning: could not init GL.\n");
94 plat_target.vout_method = 0;
98 if (plat_target.vout_method == 0) {
101 plat_sdl_screen = SDL_SetVideoMode(w, h, 16, SDL_SWSURFACE);
102 if (plat_sdl_screen == NULL) {
103 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
108 old_fullscreen = plat_target.vout_fullscreen;
112 void plat_sdl_event_handler(void *event_)
114 SDL_Event *event = event_;
116 if (event->type == SDL_VIDEORESIZE) {
117 //printf("resize %dx%d\n", event->resize.w, event->resize.h);
118 if (plat_target.vout_method != 0
119 && !plat_target.vout_fullscreen && !old_fullscreen)
121 window_w = event->resize.w;
122 window_h = event->resize.h;
123 plat_sdl_change_video_mode(0, 0);
128 int plat_sdl_init(void)
130 static const char *vout_list[] = { NULL, NULL, NULL, NULL };
131 const SDL_VideoInfo *info;
132 SDL_SysWMinfo wminfo;
133 int overlay_works = 0;
137 ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
139 fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
143 info = SDL_GetVideoInfo();
145 fs_w = info->current_w;
146 fs_h = info->current_h;
149 g_menuscreen_w = 640;
150 if (fs_w != 0 && g_menuscreen_w > fs_w)
151 g_menuscreen_w = fs_w;
152 g_menuscreen_h = 480;
155 if (info && info->wm_available && h > WM_DECORATION_H)
156 h -= WM_DECORATION_H;
157 if (g_menuscreen_h > h)
161 ret = plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h);
163 plat_sdl_screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE);
164 if (plat_sdl_screen == NULL) {
165 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
169 if (plat_sdl_screen->w < 320 || plat_sdl_screen->h < 240) {
170 fprintf(stderr, "resolution %dx%d is too small, sorry.\n",
171 plat_sdl_screen->w, plat_sdl_screen->h);
175 g_menuscreen_w = window_w = plat_sdl_screen->w;
176 g_menuscreen_h = window_h = plat_sdl_screen->h;
178 plat_sdl_overlay = SDL_CreateYUVOverlay(plat_sdl_screen->w, plat_sdl_screen->h,
179 SDL_UYVY_OVERLAY, plat_sdl_screen);
180 if (plat_sdl_overlay != NULL) {
181 printf("overlay: fmt %x, planes: %d, pitch: %d, hw: %d\n",
182 plat_sdl_overlay->format, plat_sdl_overlay->planes, *plat_sdl_overlay->pitches,
183 plat_sdl_overlay->hw_overlay);
185 if (plat_sdl_overlay->hw_overlay)
188 fprintf(stderr, "warning: video overlay is not hardware accelerated, "
189 "not going to use it.\n");
190 SDL_FreeYUVOverlay(plat_sdl_overlay);
191 plat_sdl_overlay = NULL;
194 fprintf(stderr, "overlay is not available.\n");
196 SDL_VERSION(&wminfo.version);
197 SDL_GetWMInfo(&wminfo);
198 display = wminfo.info.x11.display;
199 window = (void *)wminfo.info.x11.window;
201 ret = gl_init(display, window);
208 vout_list[i++] = "SDL Window";
210 plat_target.vout_method = vout_mode_overlay = i;
211 vout_list[i++] = "Video Overlay";
214 plat_target.vout_method = vout_mode_gl = i;
215 vout_list[i++] = "OpenGL";
217 plat_target.vout_methods = vout_list;
226 void plat_sdl_finish(void)
228 if (plat_sdl_overlay != NULL) {
229 SDL_FreeYUVOverlay(plat_sdl_overlay);
230 plat_sdl_overlay = NULL;
232 if (plat_sdl_gl_active) {
234 plat_sdl_gl_active = 0;
239 void plat_sdl_overlay_clear(void)
241 int pixels = plat_sdl_overlay->w * plat_sdl_overlay->h;
242 int *dst = (int *)plat_sdl_overlay->pixels[0];
245 for (; pixels > 0; dst += 4, pixels -= 2 * 4)
246 dst[0] = dst[1] = dst[2] = dst[3] = v;
248 for (; pixels > 0; dst++, pixels -= 2)
252 // vim:shiftwidth=2:expandtab