sdl: restore video modeo n exit
[libpicofe.git] / plat_sdl.c
CommitLineData
e81b987f 1/*
2 * (C) GraÅžvydas "notaz" Ignotas, 2012
3 *
4 * This work is licensed under the terms of any of these licenses
5 * (at your option):
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
8 * - MAME license.
9 * See the COPYING file in the top-level directory.
10 */
11
12#include <stdio.h>
13#include <SDL.h>
14#include <SDL_syswm.h>
15
16#include "menu.h"
17#include "plat.h"
18#include "gl.h"
19#include "plat_sdl.h"
20
21// XXX: maybe determine this instead..
22#define WM_DECORATION_H 32
23
24SDL_Surface *plat_sdl_screen;
25SDL_Overlay *plat_sdl_overlay;
26int plat_sdl_gl_active;
1f84ba9f 27void (*plat_sdl_quit_cb)(void);
e81b987f 28
63f173a2 29static char vid_drv_name[32];
e81b987f 30static int window_w, window_h;
31static int fs_w, fs_h;
32static int old_fullscreen;
33static int vout_mode_overlay = -1, vout_mode_gl = -1;
34static void *display, *window;
0d645bc5 35static int gl_quirks;
e81b987f 36
e54719ef 37/* w, h is layer resolution */
38int plat_sdl_change_video_mode(int w, int h, int force)
e81b987f 39{
40 static int prev_w, prev_h;
41
42 if (w == 0)
43 w = prev_w;
44 else
45 prev_w = w;
46 if (h == 0)
47 h = prev_h;
48 else
49 prev_h = h;
50
e54719ef 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)
54 {
55 return 0;
56 }
57
e81b987f 58 if (plat_sdl_overlay != NULL) {
59 SDL_FreeYUVOverlay(plat_sdl_overlay);
60 plat_sdl_overlay = NULL;
61 }
62 if (plat_sdl_gl_active) {
63 gl_finish();
64 plat_sdl_gl_active = 0;
65 }
66
67 if (plat_target.vout_method != 0) {
68 Uint32 flags = SDL_RESIZABLE | SDL_SWSURFACE;
69 int win_w = window_w;
70 int win_h = window_h;
71
72 if (plat_target.vout_fullscreen) {
73 flags |= SDL_FULLSCREEN;
74 win_w = fs_w;
75 win_h = fs_h;
76 }
77
78 // XXX: workaround some occasional mysterious deadlock in SDL_SetVideoMode
79 SDL_PumpEvents();
80
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;
85 }
86 }
87
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");
93
94 plat_sdl_overlay_clear();
95 }
96 else {
97 fprintf(stderr, "warning: could not create overlay.\n");
98 plat_target.vout_method = 0;
99 }
100 }
101 else if (plat_target.vout_method == vout_mode_gl) {
0d645bc5 102 plat_sdl_gl_active = (gl_init(display, window, &gl_quirks) == 0);
e81b987f 103 if (!plat_sdl_gl_active) {
104 fprintf(stderr, "warning: could not init GL.\n");
105 plat_target.vout_method = 0;
106 }
107 }
108
109 if (plat_target.vout_method == 0) {
110 SDL_PumpEvents();
111
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());
115 return -1;
116 }
117 }
118
119 old_fullscreen = plat_target.vout_fullscreen;
120 return 0;
121}
122
123void plat_sdl_event_handler(void *event_)
124{
e54719ef 125 static int was_active;
e81b987f 126 SDL_Event *event = event_;
127
1f84ba9f 128 switch (event->type) {
129 case SDL_VIDEORESIZE:
e81b987f 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)
133 {
134 window_w = event->resize.w;
135 window_h = event->resize.h;
e54719ef 136 plat_sdl_change_video_mode(0, 0, 1);
137 }
1f84ba9f 138 break;
139 case SDL_ACTIVEEVENT:
e54719ef 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);
144 }
145 else if (plat_sdl_gl_active) {
0d645bc5 146 if (gl_quirks & GL_QUIRK_ACTIVATE_RECREATE) {
147 gl_finish();
148 plat_sdl_gl_active = (gl_init(display, window, &gl_quirks) == 0);
149 }
e54719ef 150 gl_flip(NULL, 0, 0);
151 }
152 // else SDL takes care of it
e81b987f 153 }
e54719ef 154 was_active = event->active.gain;
1f84ba9f 155 break;
156 case SDL_QUIT:
157 if (plat_sdl_quit_cb != NULL)
158 plat_sdl_quit_cb();
159 break;
e81b987f 160 }
161}
162
163int plat_sdl_init(void)
164{
165 static const char *vout_list[] = { NULL, NULL, NULL, NULL };
166 const SDL_VideoInfo *info;
167 SDL_SysWMinfo wminfo;
168 int overlay_works = 0;
169 int gl_works = 0;
170 int i, ret, h;
171
172 ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
173 if (ret != 0) {
174 fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
175 return -1;
176 }
177
178 info = SDL_GetVideoInfo();
179 if (info != NULL) {
180 fs_w = info->current_w;
181 fs_h = info->current_h;
63f173a2 182 printf("plat_sdl: using %dx%d as fullscreen resolution\n", fs_w, fs_h);
e81b987f 183 }
184
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;
189 if (fs_h != 0) {
190 h = fs_h;
191 if (info && info->wm_available && h > WM_DECORATION_H)
192 h -= WM_DECORATION_H;
193 if (g_menuscreen_h > h)
194 g_menuscreen_h = h;
195 }
196
e54719ef 197 ret = plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 1);
e81b987f 198 if (ret != 0) {
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());
202 goto fail;
203 }
204
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);
208 goto fail;
209 }
210 }
211 g_menuscreen_w = window_w = plat_sdl_screen->w;
212 g_menuscreen_h = window_h = plat_sdl_screen->h;
213
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) {
63f173a2 217 printf("plat_sdl: overlay: fmt %x, planes: %d, pitch: %d, hw: %d\n",
e81b987f 218 plat_sdl_overlay->format, plat_sdl_overlay->planes, *plat_sdl_overlay->pitches,
219 plat_sdl_overlay->hw_overlay);
220
221 if (plat_sdl_overlay->hw_overlay)
222 overlay_works = 1;
223 else
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;
228 }
229 else
230 fprintf(stderr, "overlay is not available.\n");
231
1f84ba9f 232 // get x11 display/window for GL
233 SDL_VideoDriverName(vid_drv_name, sizeof(vid_drv_name));
215e7ed2 234#ifdef SDL_VIDEO_DRIVER_X11
1f84ba9f 235 if (strcmp(vid_drv_name, "x11") == 0) {
236 SDL_VERSION(&wminfo.version);
237 ret = SDL_GetWMInfo(&wminfo);
238 if (ret > 0) {
239 display = wminfo.info.x11.display;
240 window = (void *)wminfo.info.x11.window;
241 }
242 }
215e7ed2 243#endif
e81b987f 244
0d645bc5 245 ret = gl_init(display, window, &gl_quirks);
e81b987f 246 if (ret == 0) {
247 gl_works = 1;
248 gl_finish();
249 }
250
251 i = 0;
252 vout_list[i++] = "SDL Window";
253 if (overlay_works) {
254 plat_target.vout_method = vout_mode_overlay = i;
255 vout_list[i++] = "Video Overlay";
256 }
257 if (gl_works) {
258 plat_target.vout_method = vout_mode_gl = i;
259 vout_list[i++] = "OpenGL";
260 }
261 plat_target.vout_methods = vout_list;
262
263 return 0;
264
265fail:
266 SDL_Quit();
267 return -1;
268}
269
270void plat_sdl_finish(void)
271{
272 if (plat_sdl_overlay != NULL) {
273 SDL_FreeYUVOverlay(plat_sdl_overlay);
274 plat_sdl_overlay = NULL;
275 }
276 if (plat_sdl_gl_active) {
277 gl_finish();
278 plat_sdl_gl_active = 0;
279 }
63f173a2 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);
e81b987f 284 SDL_Quit();
285}
286
287void plat_sdl_overlay_clear(void)
288{
289 int pixels = plat_sdl_overlay->w * plat_sdl_overlay->h;
290 int *dst = (int *)plat_sdl_overlay->pixels[0];
291 int v = 0x10801080;
292
293 for (; pixels > 0; dst += 4, pixels -= 2 * 4)
294 dst[0] = dst[1] = dst[2] = dst[3] = v;
295
296 for (; pixels > 0; dst++, pixels -= 2)
297 *dst = v;
298}
299
300// vim:shiftwidth=2:expandtab