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