1a1ceecc137b0f775dcbd94af1a3912270638c1f
[pcsx_rearmed.git] / frontend / plat_sdl.c
1 /*
2  * (C) GraÅžvydas "notaz" Ignotas, 2011,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  * See the COPYING file in the top-level directory.
9  */
10
11 #include <stdio.h>
12 #include <SDL.h>
13
14 #include "libpicofe/input.h"
15 #include "libpicofe/in_sdl.h"
16 #include "libpicofe/menu.h"
17 #include "plugin_lib.h"
18 #include "main.h"
19 #include "plat.h"
20 #include "revision.h"
21
22 static const struct in_default_bind in_sdl_defbinds[] = {
23   { SDLK_UP,     IN_BINDTYPE_PLAYER12, DKEY_UP },
24   { SDLK_DOWN,   IN_BINDTYPE_PLAYER12, DKEY_DOWN },
25   { SDLK_LEFT,   IN_BINDTYPE_PLAYER12, DKEY_LEFT },
26   { SDLK_RIGHT,  IN_BINDTYPE_PLAYER12, DKEY_RIGHT },
27   { SDLK_d,      IN_BINDTYPE_PLAYER12, DKEY_TRIANGLE },
28   { SDLK_z,      IN_BINDTYPE_PLAYER12, DKEY_CROSS },
29   { SDLK_x,      IN_BINDTYPE_PLAYER12, DKEY_CIRCLE },
30   { SDLK_s,      IN_BINDTYPE_PLAYER12, DKEY_SQUARE },
31   { SDLK_v,      IN_BINDTYPE_PLAYER12, DKEY_START },
32   { SDLK_c,      IN_BINDTYPE_PLAYER12, DKEY_SELECT },
33   { SDLK_w,      IN_BINDTYPE_PLAYER12, DKEY_L1 },
34   { SDLK_r,      IN_BINDTYPE_PLAYER12, DKEY_R1 },
35   { SDLK_e,      IN_BINDTYPE_PLAYER12, DKEY_L2 },
36   { SDLK_t,      IN_BINDTYPE_PLAYER12, DKEY_R2 },
37   { SDLK_ESCAPE, IN_BINDTYPE_EMU, SACTION_ENTER_MENU },
38   { SDLK_F1,     IN_BINDTYPE_EMU, SACTION_SAVE_STATE },
39   { SDLK_F2,     IN_BINDTYPE_EMU, SACTION_LOAD_STATE },
40   { SDLK_F3,     IN_BINDTYPE_EMU, SACTION_PREV_SSLOT },
41   { SDLK_F4,     IN_BINDTYPE_EMU, SACTION_NEXT_SSLOT },
42   { SDLK_F5,     IN_BINDTYPE_EMU, SACTION_TOGGLE_FSKIP },
43   { SDLK_F6,     IN_BINDTYPE_EMU, SACTION_SCREENSHOT },
44   { SDLK_F7,     IN_BINDTYPE_EMU, SACTION_TOGGLE_FPS },
45   { SDLK_F8,     IN_BINDTYPE_EMU, SACTION_SWITCH_DISPMODE },
46   { SDLK_BACKSPACE, IN_BINDTYPE_EMU, SACTION_FAST_FORWARD },
47   { 0, 0, 0 }
48 };
49
50 static SDL_Surface *screen;
51 static void *menubg_img;
52
53 static int change_video_mode(int w, int h)
54 {
55   screen = SDL_SetVideoMode(w, h, 16, 0);
56   if (screen == NULL) {
57     fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
58     return -1;
59   }
60
61   return 0;
62 }
63
64 void plat_init(void)
65 {
66   int ret;
67
68   ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
69   if (ret != 0) {
70     fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
71     exit(1);
72   }
73
74   g_menuscreen_w = 640;
75   g_menuscreen_h = 480;
76   ret = change_video_mode(g_menuscreen_w, g_menuscreen_h);
77   if (ret != 0) {
78     ret = change_video_mode(0, 0);
79     if (ret != 0)
80       goto fail;
81
82     if (screen->w < 320 || screen->h < 240) {
83       fprintf(stderr, "resolution %dx%d is too small, sorry.\n",
84               screen->w, screen->h);
85       goto fail;
86     }
87     g_menuscreen_w = screen->w;
88     g_menuscreen_h = screen->h;
89   }
90   SDL_WM_SetCaption("PCSX-ReARMed " REV, NULL);
91
92   menubg_img = malloc(640 * 512 * 2);
93   if (menubg_img == NULL)
94     goto fail;
95
96   in_sdl_init(in_sdl_defbinds);
97   in_probe();
98   pl_rearmed_cbs.only_16bpp = 1;
99   return;
100
101 fail:
102   SDL_Quit();
103   exit(1);
104 }
105
106 void plat_finish(void)
107 {
108   free(menubg_img);
109   menubg_img = NULL;
110   SDL_Quit();
111 }
112
113 void plat_gvideo_open(int is_pal)
114 {
115 }
116
117 void *plat_gvideo_set_mode(int *w, int *h, int *bpp)
118 {
119   change_video_mode(*w, *h);
120   return screen->pixels;
121 }
122
123 /* XXX: missing SDL_LockSurface() */
124 void *plat_gvideo_flip(void)
125 {
126   SDL_Flip(screen);
127   return screen->pixels;
128 }
129
130 void plat_gvideo_close(void)
131 {
132 }
133
134 void plat_video_menu_enter(int is_rom_loaded)
135 {
136   /* surface will be lost, must adjust pl_vout_buf for menu bg */
137   memcpy(menubg_img, screen->pixels, screen->w * screen->h * 2);
138   pl_vout_buf = menubg_img;
139
140   change_video_mode(g_menuscreen_w, g_menuscreen_h);
141 }
142
143 void plat_video_menu_begin(void)
144 {
145   SDL_LockSurface(screen);
146   g_menuscreen_ptr = screen->pixels;
147 }
148
149 void plat_video_menu_end(void)
150 {
151   SDL_UnlockSurface(screen);
152   SDL_Flip(screen);
153   g_menuscreen_ptr = NULL;
154 }
155
156 void plat_video_menu_leave(void)
157 {
158 }
159
160 /* unused stuff */
161 void *plat_prepare_screenshot(int *w, int *h, int *bpp)
162 {
163   return 0;
164 }
165
166 int plat_cpu_clock_get(void)
167 {
168   return -1;
169 }
170
171 int plat_cpu_clock_apply(int cpu_clock)
172 {
173   return -1;
174 }
175
176 int plat_get_bat_capacity(void)
177 {
178   return -1;
179 }
180
181 void plat_step_volume(int is_up)
182 {
183 }
184
185 void plat_trigger_vibrate(int is_strong)
186 {
187 }
188
189 void plat_minimize(void)
190 {
191 }
192
193 // vim:shiftwidth=2:expandtab