2 * (C) GraÅžvydas "notaz" Ignotas, 2011,2012
4 * This work is licensed under the terms of any of these licenses
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
8 * See the COPYING file in the top-level directory.
14 #include "libpicofe/input.h"
15 #include "libpicofe/in_sdl.h"
16 #include "libpicofe/menu.h"
17 #include "libpicofe/fonts.h"
18 #include "../plugins/gpulib/cspace.h"
19 #include "plugin_lib.h"
25 static const struct in_default_bind in_sdl_defbinds[] = {
26 { SDLK_UP, IN_BINDTYPE_PLAYER12, DKEY_UP },
27 { SDLK_DOWN, IN_BINDTYPE_PLAYER12, DKEY_DOWN },
28 { SDLK_LEFT, IN_BINDTYPE_PLAYER12, DKEY_LEFT },
29 { SDLK_RIGHT, IN_BINDTYPE_PLAYER12, DKEY_RIGHT },
30 { SDLK_d, IN_BINDTYPE_PLAYER12, DKEY_TRIANGLE },
31 { SDLK_z, IN_BINDTYPE_PLAYER12, DKEY_CROSS },
32 { SDLK_x, IN_BINDTYPE_PLAYER12, DKEY_CIRCLE },
33 { SDLK_s, IN_BINDTYPE_PLAYER12, DKEY_SQUARE },
34 { SDLK_v, IN_BINDTYPE_PLAYER12, DKEY_START },
35 { SDLK_c, IN_BINDTYPE_PLAYER12, DKEY_SELECT },
36 { SDLK_w, IN_BINDTYPE_PLAYER12, DKEY_L1 },
37 { SDLK_r, IN_BINDTYPE_PLAYER12, DKEY_R1 },
38 { SDLK_e, IN_BINDTYPE_PLAYER12, DKEY_L2 },
39 { SDLK_t, IN_BINDTYPE_PLAYER12, DKEY_R2 },
40 { SDLK_ESCAPE, IN_BINDTYPE_EMU, SACTION_ENTER_MENU },
41 { SDLK_F1, IN_BINDTYPE_EMU, SACTION_SAVE_STATE },
42 { SDLK_F2, IN_BINDTYPE_EMU, SACTION_LOAD_STATE },
43 { SDLK_F3, IN_BINDTYPE_EMU, SACTION_PREV_SSLOT },
44 { SDLK_F4, IN_BINDTYPE_EMU, SACTION_NEXT_SSLOT },
45 { SDLK_F5, IN_BINDTYPE_EMU, SACTION_TOGGLE_FSKIP },
46 { SDLK_F6, IN_BINDTYPE_EMU, SACTION_SCREENSHOT },
47 { SDLK_F7, IN_BINDTYPE_EMU, SACTION_TOGGLE_FPS },
48 { SDLK_F8, IN_BINDTYPE_EMU, SACTION_SWITCH_DISPMODE },
49 { SDLK_F11, IN_BINDTYPE_EMU, SACTION_TOGGLE_FULLSCREEN },
50 { SDLK_BACKSPACE, IN_BINDTYPE_EMU, SACTION_FAST_FORWARD },
54 // XXX: maybe determine this instead..
55 #define WM_DECORATION_H 32
57 static SDL_Surface *screen;
58 static SDL_Overlay *overlay;
59 static int window_w, window_h;
60 static int fs_w, fs_h;
61 static int psx_w, psx_h;
62 static void *menubg_img;
63 static int in_menu, old_fullscreen;
65 static void overlay_clear(void);
67 static int change_video_mode(int w, int h)
72 if (overlay != NULL) {
73 SDL_FreeYUVOverlay(overlay);
77 if (g_use_overlay && !in_menu) {
78 Uint32 flags = SDL_RESIZABLE;
83 flags |= SDL_FULLSCREEN;
88 screen = SDL_SetVideoMode(win_w, win_h, 0, flags);
90 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
94 overlay = SDL_CreateYUVOverlay(w, h, SDL_UYVY_OVERLAY, screen);
95 if (overlay != NULL) {
96 /*printf("overlay: fmt %x, planes: %d, pitch: %d, hw: %d\n",
97 overlay->format, overlay->planes, *overlay->pitches,
98 overlay->hw_overlay);*/
100 if ((long)overlay->pixels[0] & 3)
101 fprintf(stderr, "warning: overlay pointer is unaligned\n");
103 if (!overlay->hw_overlay) {
104 fprintf(stderr, "warning: video overlay is not hardware accelerated, "
107 SDL_FreeYUVOverlay(overlay);
114 fprintf(stderr, "warning: could not create overlay.\n");
118 if (overlay == NULL) {
119 screen = SDL_SetVideoMode(w, h, 16, 0);
120 if (screen == NULL) {
121 fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
126 window_w = screen->w;
127 window_h = screen->h;
131 old_fullscreen = g_fullscreen;
135 static void event_handler(void *event_)
137 SDL_Event *event = event_;
139 if (event->type == SDL_VIDEORESIZE) {
140 //printf("%dx%d\n", event->resize.w, event->resize.h);
141 if (overlay != NULL && !g_fullscreen && !old_fullscreen) {
142 window_w = event->resize.w;
143 window_h = event->resize.h;
144 change_video_mode(psx_w, psx_h);
151 const SDL_VideoInfo *info;
154 ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
156 fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
160 info = SDL_GetVideoInfo();
162 fs_w = info->current_w;
163 fs_h = info->current_h;
167 g_menuscreen_w = 640;
168 if (fs_w != 0 && g_menuscreen_w > fs_w)
169 g_menuscreen_w = fs_w;
170 g_menuscreen_h = 480;
173 if (info && info->wm_available && h > WM_DECORATION_H)
174 h -= WM_DECORATION_H;
175 if (g_menuscreen_h > h)
179 ret = change_video_mode(g_menuscreen_w, g_menuscreen_h);
181 ret = change_video_mode(0, 0);
185 if (screen->w < 320 || screen->h < 240) {
186 fprintf(stderr, "resolution %dx%d is too small, sorry.\n",
187 screen->w, screen->h);
191 g_menuscreen_w = window_w = screen->w;
192 g_menuscreen_h = window_h = screen->h;
194 SDL_WM_SetCaption("PCSX-ReARMed " REV, NULL);
196 menubg_img = malloc(640 * 512 * 2);
197 if (menubg_img == NULL)
200 in_sdl_init(in_sdl_defbinds, event_handler);
202 pl_rearmed_cbs.only_16bpp = 1;
212 void plat_finish(void)
219 void plat_gvideo_open(int is_pal)
223 static void uyvy_to_rgb565(void *d, const void *s, int pixels)
225 unsigned short *dst = d;
226 const unsigned int *src = s;
229 // no colors, for now
230 for (; pixels > 0; src++, dst += 2, pixels -= 2) {
231 v = (*src >> 8) & 0xff;
232 v = (v - 16) * 255 / 219 / 8;
233 dst[0] = (v << 11) | (v << 6) | v;
235 v = (*src >> 24) & 0xff;
236 v = (v - 16) * 255 / 219 / 8;
237 dst[1] = (v << 11) | (v << 6) | v;
241 static void overlay_clear(void)
243 int pixels = overlay->w * overlay->h;
244 int *dst = (int *)overlay->pixels[0];
247 for (; pixels > 0; dst += 4, pixels -= 2 * 4)
248 dst[0] = dst[1] = dst[2] = dst[3] = v;
250 for (; pixels > 0; dst++, pixels -= 2)
254 static void overlay_blit(int doffs, const void *src_, int w, int h,
255 int sstride, int bgr24)
257 const unsigned short *src = src_;
259 int dstride = overlay->w;
261 SDL_LockYUVOverlay(overlay);
262 dst = (void *)overlay->pixels[0];
266 for (; h > 0; dst += dstride, src += sstride, h--)
267 bgr888_to_uyvy(dst, src, w);
270 for (; h > 0; dst += dstride, src += sstride, h--)
271 bgr555_to_uyvy(dst, src, w);
274 SDL_UnlockYUVOverlay(overlay);
277 static void overlay_hud_print(int x, int y, const char *str, int bpp)
279 SDL_LockYUVOverlay(overlay);
280 basic_text_out_uyvy_nf(overlay->pixels[0], overlay->w, x, y, str);
281 SDL_UnlockYUVOverlay(overlay);
284 void *plat_gvideo_set_mode(int *w, int *h, int *bpp)
286 change_video_mode(*w, *h);
287 if (overlay != NULL) {
288 pl_plat_clear = overlay_clear;
289 pl_plat_blit = overlay_blit;
290 pl_plat_hud_print = overlay_hud_print;
294 pl_plat_clear = NULL;
296 pl_plat_hud_print = NULL;
297 return screen->pixels;
301 void *plat_gvideo_flip(void)
303 if (!in_menu && overlay != NULL) {
304 SDL_Rect dstrect = { 0, 0, screen->w, screen->h };
305 SDL_DisplayYUVOverlay(overlay, &dstrect);
309 // XXX: missing SDL_LockSurface()
311 return screen->pixels;
315 void plat_gvideo_close(void)
319 void plat_video_menu_enter(int is_rom_loaded)
323 /* surface will be lost, must adjust pl_vout_buf for menu bg */
325 uyvy_to_rgb565(menubg_img, overlay->pixels[0], psx_w * psx_h);
327 memcpy(menubg_img, screen->pixels, psx_w * psx_h * 2);
328 pl_vout_buf = menubg_img;
330 change_video_mode(g_menuscreen_w, g_menuscreen_h);
333 void plat_video_menu_begin(void)
335 SDL_LockSurface(screen);
336 g_menuscreen_ptr = screen->pixels;
339 void plat_video_menu_end(void)
341 SDL_UnlockSurface(screen);
343 g_menuscreen_ptr = NULL;
346 void plat_video_menu_leave(void)
352 void *plat_prepare_screenshot(int *w, int *h, int *bpp)
357 void plat_trigger_vibrate(int is_strong)
361 void plat_minimize(void)
365 // vim:shiftwidth=2:expandtab