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 | |
24 | SDL_Surface *plat_sdl_screen; |
25 | SDL_Overlay *plat_sdl_overlay; |
26 | int plat_sdl_gl_active; |
1f84ba9f |
27 | void (*plat_sdl_quit_cb)(void); |
e81b987f |
28 | |
29 | static int window_w, window_h; |
30 | static int fs_w, fs_h; |
31 | static int old_fullscreen; |
32 | static int vout_mode_overlay = -1, vout_mode_gl = -1; |
33 | static void *display, *window; |
0d645bc5 |
34 | static int gl_quirks; |
e81b987f |
35 | |
e54719ef |
36 | /* w, h is layer resolution */ |
37 | int plat_sdl_change_video_mode(int w, int h, int force) |
e81b987f |
38 | { |
39 | static int prev_w, prev_h; |
40 | |
41 | if (w == 0) |
42 | w = prev_w; |
43 | else |
44 | prev_w = w; |
45 | if (h == 0) |
46 | h = prev_h; |
47 | else |
48 | prev_h = h; |
49 | |
e54719ef |
50 | // skip GL recreation if window doesn't change - avoids flicker |
51 | if (plat_target.vout_method == vout_mode_gl && plat_sdl_gl_active |
52 | && plat_target.vout_fullscreen == old_fullscreen && !force) |
53 | { |
54 | return 0; |
55 | } |
56 | |
e81b987f |
57 | if (plat_sdl_overlay != NULL) { |
58 | SDL_FreeYUVOverlay(plat_sdl_overlay); |
59 | plat_sdl_overlay = NULL; |
60 | } |
61 | if (plat_sdl_gl_active) { |
62 | gl_finish(); |
63 | plat_sdl_gl_active = 0; |
64 | } |
65 | |
66 | if (plat_target.vout_method != 0) { |
67 | Uint32 flags = SDL_RESIZABLE | SDL_SWSURFACE; |
68 | int win_w = window_w; |
69 | int win_h = window_h; |
70 | |
71 | if (plat_target.vout_fullscreen) { |
72 | flags |= SDL_FULLSCREEN; |
73 | win_w = fs_w; |
74 | win_h = fs_h; |
75 | } |
76 | |
77 | // XXX: workaround some occasional mysterious deadlock in SDL_SetVideoMode |
78 | SDL_PumpEvents(); |
79 | |
80 | plat_sdl_screen = SDL_SetVideoMode(win_w, win_h, 0, flags); |
81 | if (plat_sdl_screen == NULL) { |
82 | fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError()); |
83 | plat_target.vout_method = 0; |
84 | } |
85 | } |
86 | |
87 | if (plat_target.vout_method == vout_mode_overlay) { |
88 | plat_sdl_overlay = SDL_CreateYUVOverlay(w, h, SDL_UYVY_OVERLAY, plat_sdl_screen); |
89 | if (plat_sdl_overlay != NULL) { |
90 | if ((long)plat_sdl_overlay->pixels[0] & 3) |
91 | fprintf(stderr, "warning: overlay pointer is unaligned\n"); |
92 | |
93 | plat_sdl_overlay_clear(); |
94 | } |
95 | else { |
96 | fprintf(stderr, "warning: could not create overlay.\n"); |
97 | plat_target.vout_method = 0; |
98 | } |
99 | } |
100 | else if (plat_target.vout_method == vout_mode_gl) { |
0d645bc5 |
101 | plat_sdl_gl_active = (gl_init(display, window, &gl_quirks) == 0); |
e81b987f |
102 | if (!plat_sdl_gl_active) { |
103 | fprintf(stderr, "warning: could not init GL.\n"); |
104 | plat_target.vout_method = 0; |
105 | } |
106 | } |
107 | |
108 | if (plat_target.vout_method == 0) { |
109 | SDL_PumpEvents(); |
110 | |
111 | plat_sdl_screen = SDL_SetVideoMode(w, h, 16, SDL_SWSURFACE); |
112 | if (plat_sdl_screen == NULL) { |
113 | fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError()); |
114 | return -1; |
115 | } |
116 | } |
117 | |
118 | old_fullscreen = plat_target.vout_fullscreen; |
119 | return 0; |
120 | } |
121 | |
122 | void plat_sdl_event_handler(void *event_) |
123 | { |
e54719ef |
124 | static int was_active; |
e81b987f |
125 | SDL_Event *event = event_; |
126 | |
1f84ba9f |
127 | switch (event->type) { |
128 | case SDL_VIDEORESIZE: |
e81b987f |
129 | //printf("resize %dx%d\n", event->resize.w, event->resize.h); |
130 | if (plat_target.vout_method != 0 |
131 | && !plat_target.vout_fullscreen && !old_fullscreen) |
132 | { |
133 | window_w = event->resize.w; |
134 | window_h = event->resize.h; |
e54719ef |
135 | plat_sdl_change_video_mode(0, 0, 1); |
136 | } |
1f84ba9f |
137 | break; |
138 | case SDL_ACTIVEEVENT: |
e54719ef |
139 | if (event->active.gain && !was_active) { |
140 | if (plat_sdl_overlay != NULL) { |
141 | SDL_Rect dstrect = { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h }; |
142 | SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect); |
143 | } |
144 | else if (plat_sdl_gl_active) { |
0d645bc5 |
145 | if (gl_quirks & GL_QUIRK_ACTIVATE_RECREATE) { |
146 | gl_finish(); |
147 | plat_sdl_gl_active = (gl_init(display, window, &gl_quirks) == 0); |
148 | } |
e54719ef |
149 | gl_flip(NULL, 0, 0); |
150 | } |
151 | // else SDL takes care of it |
e81b987f |
152 | } |
e54719ef |
153 | was_active = event->active.gain; |
1f84ba9f |
154 | break; |
155 | case SDL_QUIT: |
156 | if (plat_sdl_quit_cb != NULL) |
157 | plat_sdl_quit_cb(); |
158 | break; |
e81b987f |
159 | } |
160 | } |
161 | |
162 | int plat_sdl_init(void) |
163 | { |
164 | static const char *vout_list[] = { NULL, NULL, NULL, NULL }; |
165 | const SDL_VideoInfo *info; |
166 | SDL_SysWMinfo wminfo; |
1f84ba9f |
167 | char vid_drv_name[64]; |
e81b987f |
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; |
182 | } |
183 | |
184 | g_menuscreen_w = 640; |
185 | if (fs_w != 0 && g_menuscreen_w > fs_w) |
186 | g_menuscreen_w = fs_w; |
187 | g_menuscreen_h = 480; |
188 | if (fs_h != 0) { |
189 | h = fs_h; |
190 | if (info && info->wm_available && h > WM_DECORATION_H) |
191 | h -= WM_DECORATION_H; |
192 | if (g_menuscreen_h > h) |
193 | g_menuscreen_h = h; |
194 | } |
195 | |
e54719ef |
196 | ret = plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 1); |
e81b987f |
197 | if (ret != 0) { |
198 | plat_sdl_screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE); |
199 | if (plat_sdl_screen == NULL) { |
200 | fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError()); |
201 | goto fail; |
202 | } |
203 | |
204 | if (plat_sdl_screen->w < 320 || plat_sdl_screen->h < 240) { |
205 | fprintf(stderr, "resolution %dx%d is too small, sorry.\n", |
206 | plat_sdl_screen->w, plat_sdl_screen->h); |
207 | goto fail; |
208 | } |
209 | } |
210 | g_menuscreen_w = window_w = plat_sdl_screen->w; |
211 | g_menuscreen_h = window_h = plat_sdl_screen->h; |
212 | |
213 | plat_sdl_overlay = SDL_CreateYUVOverlay(plat_sdl_screen->w, plat_sdl_screen->h, |
214 | SDL_UYVY_OVERLAY, plat_sdl_screen); |
215 | if (plat_sdl_overlay != NULL) { |
216 | printf("overlay: fmt %x, planes: %d, pitch: %d, hw: %d\n", |
217 | plat_sdl_overlay->format, plat_sdl_overlay->planes, *plat_sdl_overlay->pitches, |
218 | plat_sdl_overlay->hw_overlay); |
219 | |
220 | if (plat_sdl_overlay->hw_overlay) |
221 | overlay_works = 1; |
222 | else |
223 | fprintf(stderr, "warning: video overlay is not hardware accelerated, " |
224 | "not going to use it.\n"); |
225 | SDL_FreeYUVOverlay(plat_sdl_overlay); |
226 | plat_sdl_overlay = NULL; |
227 | } |
228 | else |
229 | fprintf(stderr, "overlay is not available.\n"); |
230 | |
1f84ba9f |
231 | // get x11 display/window for GL |
232 | SDL_VideoDriverName(vid_drv_name, sizeof(vid_drv_name)); |
215e7ed2 |
233 | #ifdef SDL_VIDEO_DRIVER_X11 |
1f84ba9f |
234 | if (strcmp(vid_drv_name, "x11") == 0) { |
235 | SDL_VERSION(&wminfo.version); |
236 | ret = SDL_GetWMInfo(&wminfo); |
237 | if (ret > 0) { |
238 | display = wminfo.info.x11.display; |
239 | window = (void *)wminfo.info.x11.window; |
240 | } |
241 | } |
215e7ed2 |
242 | #endif |
e81b987f |
243 | |
0d645bc5 |
244 | ret = gl_init(display, window, &gl_quirks); |
e81b987f |
245 | if (ret == 0) { |
246 | gl_works = 1; |
247 | gl_finish(); |
248 | } |
249 | |
250 | i = 0; |
251 | vout_list[i++] = "SDL Window"; |
252 | if (overlay_works) { |
253 | plat_target.vout_method = vout_mode_overlay = i; |
254 | vout_list[i++] = "Video Overlay"; |
255 | } |
256 | if (gl_works) { |
257 | plat_target.vout_method = vout_mode_gl = i; |
258 | vout_list[i++] = "OpenGL"; |
259 | } |
260 | plat_target.vout_methods = vout_list; |
261 | |
262 | return 0; |
263 | |
264 | fail: |
265 | SDL_Quit(); |
266 | return -1; |
267 | } |
268 | |
269 | void plat_sdl_finish(void) |
270 | { |
271 | if (plat_sdl_overlay != NULL) { |
272 | SDL_FreeYUVOverlay(plat_sdl_overlay); |
273 | plat_sdl_overlay = NULL; |
274 | } |
275 | if (plat_sdl_gl_active) { |
276 | gl_finish(); |
277 | plat_sdl_gl_active = 0; |
278 | } |
279 | SDL_Quit(); |
280 | } |
281 | |
282 | void plat_sdl_overlay_clear(void) |
283 | { |
284 | int pixels = plat_sdl_overlay->w * plat_sdl_overlay->h; |
285 | int *dst = (int *)plat_sdl_overlay->pixels[0]; |
286 | int v = 0x10801080; |
287 | |
288 | for (; pixels > 0; dst += 4, pixels -= 2 * 4) |
289 | dst[0] = dst[1] = dst[2] = dst[3] = v; |
290 | |
291 | for (; pixels > 0; dst++, pixels -= 2) |
292 | *dst = v; |
293 | } |
294 | |
295 | // vim:shiftwidth=2:expandtab |