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; |
27 | |
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; |
0d645bc5 |
33 | static int gl_quirks; |
e81b987f |
34 | |
e54719ef |
35 | /* w, h is layer resolution */ |
36 | int 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 | |
121 | void 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 | |
155 | int 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); |
225 | display = wminfo.info.x11.display; |
226 | window = (void *)wminfo.info.x11.window; |
227 | |
0d645bc5 |
228 | ret = gl_init(display, window, &gl_quirks); |
e81b987f |
229 | if (ret == 0) { |
230 | gl_works = 1; |
231 | gl_finish(); |
232 | } |
233 | |
234 | i = 0; |
235 | vout_list[i++] = "SDL Window"; |
236 | if (overlay_works) { |
237 | plat_target.vout_method = vout_mode_overlay = i; |
238 | vout_list[i++] = "Video Overlay"; |
239 | } |
240 | if (gl_works) { |
241 | plat_target.vout_method = vout_mode_gl = i; |
242 | vout_list[i++] = "OpenGL"; |
243 | } |
244 | plat_target.vout_methods = vout_list; |
245 | |
246 | return 0; |
247 | |
248 | fail: |
249 | SDL_Quit(); |
250 | return -1; |
251 | } |
252 | |
253 | void plat_sdl_finish(void) |
254 | { |
255 | if (plat_sdl_overlay != NULL) { |
256 | SDL_FreeYUVOverlay(plat_sdl_overlay); |
257 | plat_sdl_overlay = NULL; |
258 | } |
259 | if (plat_sdl_gl_active) { |
260 | gl_finish(); |
261 | plat_sdl_gl_active = 0; |
262 | } |
263 | SDL_Quit(); |
264 | } |
265 | |
266 | void plat_sdl_overlay_clear(void) |
267 | { |
268 | int pixels = plat_sdl_overlay->w * plat_sdl_overlay->h; |
269 | int *dst = (int *)plat_sdl_overlay->pixels[0]; |
270 | int v = 0x10801080; |
271 | |
272 | for (; pixels > 0; dst += 4, pixels -= 2 * 4) |
273 | dst[0] = dst[1] = dst[2] = dst[3] = v; |
274 | |
275 | for (; pixels > 0; dst++, pixels -= 2) |
276 | *dst = v; |
277 | } |
278 | |
279 | // vim:shiftwidth=2:expandtab |