fix off-by-1 in directory listing search
[libpicofe.git] / plat_sdl.c
... / ...
CommitLineData
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;
27void (*plat_sdl_resize_cb)(int w, int h);
28void (*plat_sdl_quit_cb)(void);
29
30static char vid_drv_name[32];
31static int window_w, window_h, window_b;
32static int fs_w, fs_h;
33static int old_fullscreen;
34static int screen_flags;
35static int vout_mode_overlay = -1, vout_mode_overlay2x = -1, vout_mode_gl = -1;
36static void *display, *window;
37static int gl_quirks;
38
39/* w, h is layer resolution */
40int plat_sdl_change_video_mode(int w, int h, int force)
41{
42 static int prev_w, prev_h;
43
44 // skip GL recreation if window doesn't change - avoids flicker
45 if (plat_target.vout_method == vout_mode_gl && plat_sdl_gl_active
46 && plat_target.vout_fullscreen == old_fullscreen
47 && w == prev_w && h == prev_h && !force)
48 {
49 return 0;
50 }
51
52 if (w == 0)
53 w = prev_w;
54 else
55 prev_w = w;
56 if (h == 0)
57 h = prev_h;
58 else
59 prev_h = h;
60
61 // invalid method might come from config..
62 if (plat_target.vout_method != 0
63 && plat_target.vout_method != vout_mode_overlay
64 && plat_target.vout_method != vout_mode_overlay2x
65 && plat_target.vout_method != vout_mode_gl)
66 {
67 fprintf(stderr, "invalid vout_method: %d\n", plat_target.vout_method);
68 plat_target.vout_method = 0;
69 }
70
71 if (plat_sdl_overlay != NULL) {
72 SDL_FreeYUVOverlay(plat_sdl_overlay);
73 plat_sdl_overlay = NULL;
74 }
75 if (plat_sdl_gl_active) {
76 gl_finish();
77 plat_sdl_gl_active = 0;
78 }
79
80 if (plat_target.vout_method != 0) {
81 Uint32 flags = SDL_RESIZABLE | SDL_SWSURFACE;
82 int win_w = window_w;
83 int win_h = window_h;
84
85 if (plat_target.vout_fullscreen) {
86 flags |= SDL_FULLSCREEN;
87 win_w = fs_w;
88 win_h = fs_h;
89 }
90
91 // XXX: workaround some occasional mysterious deadlock in SDL_SetVideoMode
92 // (seen on r-pi)
93 SDL_PumpEvents();
94
95 if (!plat_sdl_screen || screen_flags != flags ||
96 plat_sdl_screen->w != win_w || plat_sdl_screen->h != win_h)
97 plat_sdl_screen = SDL_SetVideoMode(win_w, win_h, 0, flags);
98 screen_flags = flags;
99 if (plat_sdl_screen == NULL) {
100 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
101 plat_target.vout_method = 0;
102 }
103 }
104
105 if (plat_target.vout_method == vout_mode_overlay
106 || plat_target.vout_method == vout_mode_overlay2x) {
107 int W = plat_target.vout_method == vout_mode_overlay2x && w < 640 ? 2*w : w;
108 plat_sdl_overlay = SDL_CreateYUVOverlay(W, h, SDL_UYVY_OVERLAY, plat_sdl_screen);
109 if (plat_sdl_overlay != NULL) {
110 if ((long)plat_sdl_overlay->pixels[0] & 3)
111 fprintf(stderr, "warning: overlay pointer is unaligned\n");
112
113 plat_sdl_overlay_clear();
114 }
115 else {
116 fprintf(stderr, "warning: could not create overlay.\n");
117 plat_target.vout_method = 0;
118 }
119 }
120 else if (plat_target.vout_method == vout_mode_gl) {
121 int sw = plat_sdl_screen->w, sh = plat_sdl_screen->h;
122 plat_sdl_gl_active = (gl_init(display, window, &gl_quirks, sw, sh) == 0);
123 if (!plat_sdl_gl_active) {
124 fprintf(stderr, "warning: could not init GL.\n");
125 plat_target.vout_method = 0;
126 }
127 }
128
129 if (plat_target.vout_method == 0) {
130 Uint32 flags;
131 int win_w = w;
132 int win_h = h;
133
134#if defined SDL_SURFACE_SW
135 flags = SDL_SWSURFACE;
136#elif defined(SDL_TRIPLEBUF) && defined(SDL_BUFFER_3X)
137 flags = SDL_HWSURFACE | SDL_TRIPLEBUF;
138#else
139 flags = SDL_HWSURFACE | SDL_DOUBLEBUF;
140#endif
141 if (plat_target.vout_fullscreen && fs_w && fs_h) {
142 flags |= SDL_FULLSCREEN;
143 win_w = fs_w;
144 win_h = fs_h;
145 }
146
147 SDL_PumpEvents();
148
149 if (!plat_sdl_screen || screen_flags != flags ||
150 plat_sdl_screen->w != win_w || plat_sdl_screen->h != win_h)
151 plat_sdl_screen = SDL_SetVideoMode(win_w, win_h, 16, flags);
152 screen_flags = flags;
153 if (plat_sdl_screen == NULL) {
154 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
155 return -1;
156 }
157 }
158
159 old_fullscreen = plat_target.vout_fullscreen;
160 if (plat_sdl_resize_cb != NULL)
161 plat_sdl_resize_cb(plat_sdl_screen->w, plat_sdl_screen->h);
162
163 return 0;
164}
165
166void plat_sdl_event_handler(void *event_)
167{
168 static int was_active;
169 SDL_Event *event = event_;
170
171 switch (event->type) {
172 case SDL_VIDEORESIZE:
173 //printf("resize %dx%d\n", event->resize.w, event->resize.h);
174 if (plat_target.vout_method != 0
175 && !plat_target.vout_fullscreen && !old_fullscreen)
176 {
177 window_w = event->resize.w & ~3;
178 window_h = event->resize.h & ~3;
179 plat_sdl_change_video_mode(0, 0, 1);
180 }
181 break;
182 case SDL_ACTIVEEVENT:
183 if (event->active.gain && !was_active) {
184 if (plat_sdl_overlay != NULL) {
185 SDL_Rect dstrect = { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
186 SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
187 }
188 else if (plat_sdl_gl_active) {
189 if (gl_quirks & GL_QUIRK_ACTIVATE_RECREATE) {
190 int sw = plat_sdl_screen->w, sh = plat_sdl_screen->h;
191 gl_finish();
192 plat_sdl_gl_active = (gl_init(display, window, &gl_quirks, sw, sh) == 0);
193 }
194 gl_flip(NULL, 0, 0);
195 }
196 // else SDL takes care of it
197 }
198 was_active = event->active.gain;
199 break;
200 case SDL_QUIT:
201 if (plat_sdl_quit_cb != NULL)
202 plat_sdl_quit_cb();
203 break;
204 }
205}
206
207int plat_sdl_init(void)
208{
209 static const char *vout_list[] = { NULL, NULL, NULL, NULL, NULL };
210 const SDL_VideoInfo *info;
211 SDL_SysWMinfo wminfo;
212 const char *env;
213 int overlay_works = 0;
214 int gl_works = 0;
215 int i, ret, h;
216 int try_gl;
217
218 ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
219 if (ret != 0) {
220 fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
221 return -1;
222 }
223
224 info = SDL_GetVideoInfo();
225 if (info != NULL) {
226 fs_w = info->current_w;
227 fs_h = info->current_h;
228 if (info->wm_available)
229 window_b = WM_DECORATION_H;
230 printf("plat_sdl: using %dx%d as fullscreen resolution\n", fs_w, fs_h);
231 }
232
233 g_menuscreen_w = 640;
234 if (fs_w != 0 && g_menuscreen_w > fs_w)
235 g_menuscreen_w = fs_w;
236 g_menuscreen_h = 480;
237 if (fs_h != 0) {
238 h = fs_h;
239 if (window_b && h > window_b)
240 h -= window_b;
241 if (g_menuscreen_h > h)
242 g_menuscreen_h = h;
243 }
244
245 plat_sdl_screen = SDL_SetVideoMode(g_menuscreen_w, g_menuscreen_h, 16, SDL_HWSURFACE);
246 if (plat_sdl_screen == NULL) {
247 plat_sdl_screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE);
248 if (plat_sdl_screen == NULL) {
249 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
250 goto fail;
251 }
252
253 if (plat_sdl_screen->w < 320 || plat_sdl_screen->h < 240) {
254 fprintf(stderr, "resolution %dx%d is too small, sorry.\n",
255 plat_sdl_screen->w, plat_sdl_screen->h);
256 goto fail;
257 }
258 }
259 g_menuscreen_w = window_w = plat_sdl_screen->w;
260 g_menuscreen_h = window_h = plat_sdl_screen->h;
261 g_menuscreen_pp = g_menuscreen_w;
262
263 // overlay/gl require native bpp in some cases..
264 plat_sdl_screen = SDL_SetVideoMode(g_menuscreen_w, g_menuscreen_h,
265 0, plat_sdl_screen->flags);
266 if (plat_sdl_screen == NULL) {
267 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
268 goto fail;
269 }
270
271 plat_sdl_overlay = SDL_CreateYUVOverlay(plat_sdl_screen->w, plat_sdl_screen->h,
272 SDL_UYVY_OVERLAY, plat_sdl_screen);
273 if (plat_sdl_overlay != NULL) {
274 printf("plat_sdl: overlay: fmt %x, planes: %d, pitch: %d, hw: %d\n",
275 plat_sdl_overlay->format, plat_sdl_overlay->planes, *plat_sdl_overlay->pitches,
276 plat_sdl_overlay->hw_overlay);
277
278 if (plat_sdl_overlay->hw_overlay)
279 overlay_works = 1;
280 else
281 fprintf(stderr, "warning: video overlay is not hardware accelerated, "
282 "not going to use it.\n");
283 SDL_FreeYUVOverlay(plat_sdl_overlay);
284 plat_sdl_overlay = NULL;
285 }
286 else
287 fprintf(stderr, "overlay is not available.\n");
288
289 // get x11 display/window for GL
290 SDL_VideoDriverName(vid_drv_name, sizeof(vid_drv_name));
291#ifdef SDL_VIDEO_DRIVER_X11
292 if (strcmp(vid_drv_name, "x11") == 0) {
293 SDL_VERSION(&wminfo.version);
294 ret = SDL_GetWMInfo(&wminfo);
295 if (ret > 0) {
296 display = wminfo.info.x11.display;
297 window = (void *)wminfo.info.x11.window;
298 }
299 }
300#else
301 (void)wminfo;
302#endif
303
304 ret = -1;
305 try_gl = 1;
306 env = getenv("DISPLAY");
307 if (env && env[0] != ':') {
308 fprintf(stderr, "looks like a remote DISPLAY, "
309 "not trying GL (use PICOFE_GL=1 to override)\n");
310 // because some drivers just kill the program with no way to recover
311 try_gl = 0;
312 }
313 env = getenv("PICOFE_GL");
314 if (env)
315 try_gl = atoi(env);
316 if (try_gl)
317 ret = gl_init(display, window, &gl_quirks, g_menuscreen_w, g_menuscreen_h);
318 if (ret == 0) {
319 gl_announce();
320 gl_works = 1;
321 gl_finish();
322 }
323
324 i = 0;
325 vout_list[i++] = "SDL Window";
326 if (overlay_works) {
327 plat_target.vout_method = vout_mode_overlay = i;
328 vout_list[i++] = "Video Overlay";
329#ifdef SDL_OVERLAY_2X
330 vout_mode_overlay2x = i;
331 vout_list[i++] = "Video Overlay 2x";
332#endif
333 }
334 if (gl_works) {
335 plat_target.vout_method = vout_mode_gl = i;
336 vout_list[i++] = "OpenGL";
337 }
338 plat_target.vout_methods = vout_list;
339
340 plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 1);
341 return 0;
342
343fail:
344 SDL_Quit();
345 return -1;
346}
347
348void plat_sdl_finish(void)
349{
350 if (plat_sdl_overlay != NULL) {
351 SDL_FreeYUVOverlay(plat_sdl_overlay);
352 plat_sdl_overlay = NULL;
353 }
354 if (plat_sdl_gl_active) {
355 gl_finish();
356 plat_sdl_gl_active = 0;
357 }
358 // restore back to initial resolution
359 // resolves black screen issue on R-Pi
360 if (strcmp(vid_drv_name, "x11") != 0)
361 SDL_SetVideoMode(fs_w, fs_h, 16, SDL_SWSURFACE);
362 SDL_Quit();
363}
364
365void plat_sdl_overlay_clear(void)
366{
367 int pixels = plat_sdl_overlay->w * plat_sdl_overlay->h;
368 int *dst = (int *)plat_sdl_overlay->pixels[0];
369 int v = 0x10801080;
370
371 for (; pixels > 7; dst += 4, pixels -= 2 * 4)
372 dst[0] = dst[1] = dst[2] = dst[3] = v;
373
374 for (; pixels > 1; dst++, pixels -= 2)
375 *dst = v;
376}
377
378// vim:shiftwidth=2:expandtab