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