only take wminfo from appropriate sdl driver
[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;
27
28static int window_w, window_h;
29static int fs_w, fs_h;
30static int old_fullscreen;
31static int vout_mode_overlay = -1, vout_mode_gl = -1;
32static void *display, *window;
0d645bc5 33static int gl_quirks;
e81b987f 34
e54719ef 35/* w, h is layer resolution */
36int plat_sdl_change_video_mode(int w, int h, int force)
e81b987f 37{
38 static int prev_w, prev_h;
39
40 if (w == 0)
41 w = prev_w;
42 else
43 prev_w = w;
44 if (h == 0)
45 h = prev_h;
46 else
47 prev_h = h;
48
e54719ef 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)
52 {
53 return 0;
54 }
55
e81b987f 56 if (plat_sdl_overlay != NULL) {
57 SDL_FreeYUVOverlay(plat_sdl_overlay);
58 plat_sdl_overlay = NULL;
59 }
60 if (plat_sdl_gl_active) {
61 gl_finish();
62 plat_sdl_gl_active = 0;
63 }
64
65 if (plat_target.vout_method != 0) {
66 Uint32 flags = SDL_RESIZABLE | SDL_SWSURFACE;
67 int win_w = window_w;
68 int win_h = window_h;
69
70 if (plat_target.vout_fullscreen) {
71 flags |= SDL_FULLSCREEN;
72 win_w = fs_w;
73 win_h = fs_h;
74 }
75
76 // XXX: workaround some occasional mysterious deadlock in SDL_SetVideoMode
77 SDL_PumpEvents();
78
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;
83 }
84 }
85
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");
91
92 plat_sdl_overlay_clear();
93 }
94 else {
95 fprintf(stderr, "warning: could not create overlay.\n");
96 plat_target.vout_method = 0;
97 }
98 }
99 else if (plat_target.vout_method == vout_mode_gl) {
0d645bc5 100 plat_sdl_gl_active = (gl_init(display, window, &gl_quirks) == 0);
e81b987f 101 if (!plat_sdl_gl_active) {
102 fprintf(stderr, "warning: could not init GL.\n");
103 plat_target.vout_method = 0;
104 }
105 }
106
107 if (plat_target.vout_method == 0) {
108 SDL_PumpEvents();
109
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());
113 return -1;
114 }
115 }
116
117 old_fullscreen = plat_target.vout_fullscreen;
118 return 0;
119}
120
121void plat_sdl_event_handler(void *event_)
122{
e54719ef 123 static int was_active;
e81b987f 124 SDL_Event *event = event_;
125
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)
130 {
131 window_w = event->resize.w;
132 window_h = event->resize.h;
e54719ef 133 plat_sdl_change_video_mode(0, 0, 1);
134 }
135 }
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);
141 }
142 else if (plat_sdl_gl_active) {
0d645bc5 143 if (gl_quirks & GL_QUIRK_ACTIVATE_RECREATE) {
144 gl_finish();
145 plat_sdl_gl_active = (gl_init(display, window, &gl_quirks) == 0);
146 }
e54719ef 147 gl_flip(NULL, 0, 0);
148 }
149 // else SDL takes care of it
e81b987f 150 }
e54719ef 151 was_active = event->active.gain;
e81b987f 152 }
153}
154
155int plat_sdl_init(void)
156{
157 static const char *vout_list[] = { NULL, NULL, NULL, NULL };
158 const SDL_VideoInfo *info;
159 SDL_SysWMinfo wminfo;
160 int overlay_works = 0;
161 int gl_works = 0;
162 int i, ret, h;
163
164 ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
165 if (ret != 0) {
166 fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
167 return -1;
168 }
169
170 info = SDL_GetVideoInfo();
171 if (info != NULL) {
172 fs_w = info->current_w;
173 fs_h = info->current_h;
174 }
175
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;
180 if (fs_h != 0) {
181 h = fs_h;
182 if (info && info->wm_available && h > WM_DECORATION_H)
183 h -= WM_DECORATION_H;
184 if (g_menuscreen_h > h)
185 g_menuscreen_h = h;
186 }
187
e54719ef 188 ret = plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 1);
e81b987f 189 if (ret != 0) {
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());
193 goto fail;
194 }
195
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);
199 goto fail;
200 }
201 }
202 g_menuscreen_w = window_w = plat_sdl_screen->w;
203 g_menuscreen_h = window_h = plat_sdl_screen->h;
204
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);
211
212 if (plat_sdl_overlay->hw_overlay)
213 overlay_works = 1;
214 else
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;
219 }
220 else
221 fprintf(stderr, "overlay is not available.\n");
222
223 SDL_VERSION(&wminfo.version);
224 SDL_GetWMInfo(&wminfo);
215e7ed2 225#ifdef SDL_VIDEO_DRIVER_X11
e81b987f 226 display = wminfo.info.x11.display;
227 window = (void *)wminfo.info.x11.window;
215e7ed2 228#endif
e81b987f 229
0d645bc5 230 ret = gl_init(display, window, &gl_quirks);
e81b987f 231 if (ret == 0) {
232 gl_works = 1;
233 gl_finish();
234 }
235
236 i = 0;
237 vout_list[i++] = "SDL Window";
238 if (overlay_works) {
239 plat_target.vout_method = vout_mode_overlay = i;
240 vout_list[i++] = "Video Overlay";
241 }
242 if (gl_works) {
243 plat_target.vout_method = vout_mode_gl = i;
244 vout_list[i++] = "OpenGL";
245 }
246 plat_target.vout_methods = vout_list;
247
248 return 0;
249
250fail:
251 SDL_Quit();
252 return -1;
253}
254
255void plat_sdl_finish(void)
256{
257 if (plat_sdl_overlay != NULL) {
258 SDL_FreeYUVOverlay(plat_sdl_overlay);
259 plat_sdl_overlay = NULL;
260 }
261 if (plat_sdl_gl_active) {
262 gl_finish();
263 plat_sdl_gl_active = 0;
264 }
265 SDL_Quit();
266}
267
268void plat_sdl_overlay_clear(void)
269{
270 int pixels = plat_sdl_overlay->w * plat_sdl_overlay->h;
271 int *dst = (int *)plat_sdl_overlay->pixels[0];
272 int v = 0x10801080;
273
274 for (; pixels > 0; dst += 4, pixels -= 2 * 4)
275 dst[0] = dst[1] = dst[2] = dst[3] = v;
276
277 for (; pixels > 0; dst++, pixels -= 2)
278 *dst = v;
279}
280
281// vim:shiftwidth=2:expandtab