summaryrefslogtreecommitdiff
path: root/frontend/plat_linux.c
blob: 09d9a48e71a99a4ca39880d73d84a44a899babce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <SDL/SDL.h>
#include "common.h"

#include "frontend/main.h"
#include "frontend/plat.h"
#include "frontend/scale.h"
#include "frontend/libpicofe/menu.h"
#include "frontend/libpicofe/plat.h"
#include "frontend/libpicofe/input.h"
#include "frontend/libpicofe/in_sdl.h"

static SDL_Surface* screen;

#define BUF_LEN 8192
static short buf[BUF_LEN];
static int buf_w, buf_r;

static char msg[HUD_LEN];

static const struct in_default_bind in_sdl_defbinds[] = {
  { SDLK_UP,        IN_BINDTYPE_PLAYER12, KBIT_UP },
  { SDLK_DOWN,      IN_BINDTYPE_PLAYER12, KBIT_DOWN },
  { SDLK_LEFT,      IN_BINDTYPE_PLAYER12, KBIT_LEFT },
  { SDLK_RIGHT,     IN_BINDTYPE_PLAYER12, KBIT_RIGHT },
  { SDLK_LCTRL,     IN_BINDTYPE_PLAYER12, KBIT_B },
  { SDLK_SPACE,     IN_BINDTYPE_PLAYER12, KBIT_A },
  { SDLK_RETURN,    IN_BINDTYPE_PLAYER12, KBIT_START },
  { SDLK_RCTRL,     IN_BINDTYPE_PLAYER12, KBIT_SELECT },
  { SDLK_TAB,       IN_BINDTYPE_PLAYER12, KBIT_L },
  { SDLK_BACKSPACE, IN_BINDTYPE_PLAYER12, KBIT_R },
  { SDLK_ESCAPE,    IN_BINDTYPE_EMU, EACTION_MENU },
  { 0, 0, 0 }
};

const struct menu_keymap in_sdl_key_map[] =
{
  { SDLK_UP,        PBTN_UP },
  { SDLK_DOWN,      PBTN_DOWN },
  { SDLK_LEFT,      PBTN_LEFT },
  { SDLK_RIGHT,     PBTN_RIGHT },
  { SDLK_SPACE,     PBTN_MOK },
  { SDLK_LCTRL,     PBTN_MBACK },
  { SDLK_LALT,      PBTN_MA2 },
  { SDLK_LSHIFT,    PBTN_MA3 },
  { SDLK_TAB,       PBTN_L },
  { SDLK_BACKSPACE, PBTN_R },
};

const struct menu_keymap in_sdl_joy_map[] =
{
  { SDLK_UP,    PBTN_UP },
  { SDLK_DOWN,  PBTN_DOWN },
  { SDLK_LEFT,  PBTN_LEFT },
  { SDLK_RIGHT, PBTN_RIGHT },
  { SDLK_WORLD_0, PBTN_MOK },
  { SDLK_WORLD_1, PBTN_MBACK },
  { SDLK_WORLD_2, PBTN_MA2 },
  { SDLK_WORLD_3, PBTN_MA3 },
};

static const struct in_pdata in_sdl_platform_data = {
  .defbinds  = in_sdl_defbinds,
  .key_map   = in_sdl_key_map,
  .kmap_size = array_size(in_sdl_key_map),
  .joy_map   = in_sdl_joy_map,
  .jmap_size = array_size(in_sdl_joy_map),
};

static void *fb_flip(void)
{
  SDL_Flip(screen);
  return screen->pixels;
}

void plat_video_menu_enter(int is_rom_loaded)
{
}

void plat_video_menu_begin(void)
{
  g_menuscreen_ptr = fb_flip();
}

void plat_video_menu_end(void)
{
  g_menuscreen_ptr = fb_flip();
}

void plat_video_menu_leave(void)
{
  SDL_LockSurface(screen);
  memset(g_menuscreen_ptr, 0, 320 * 240 * sizeof(uint16_t));
  SDL_UnlockSurface(screen);
  g_menuscreen_ptr = fb_flip();
  SDL_LockSurface(screen);
  memset(g_menuscreen_ptr, 0, 320 * 240 * sizeof(uint16_t));
  SDL_UnlockSurface(screen);
}

void plat_video_open(void)
{
}

void plat_video_set_msg(char *new_msg)
{
  snprintf(msg, HUD_LEN, "%s", new_msg);
}

void plat_video_flip(void)
{
  video_post_process();
  SDL_LockSurface(screen);
  if (msg[0])
    video_clear_msg(screen->pixels, screen->h, screen->pitch / sizeof(uint16_t));

  video_scale(screen->pixels, screen->h, screen->pitch / sizeof(uint16_t));

  if (msg[0])
    video_print_msg(screen->pixels, screen->h, screen->pitch / sizeof(uint16_t), msg);
  SDL_UnlockSurface(screen);

  g_menuscreen_ptr = fb_flip();
  msg[0] = 0;
}

void plat_video_close(void)
{
}

void plat_sound_callback(void *unused, u8 *stream, int len)
{
  short *p = (short *)stream;
  len /= sizeof(short);

  while (buf_r != buf_w && len > 0) {
    *p++ = buf[buf_r++];
    buf_r %= BUF_LEN;
    --len;
  }

  while(len > 0) {
    *p++ = 0;
    --len;
  }
}

void plat_sound_finish(void)
{
  SDL_PauseAudio(1);
  SDL_CloseAudio();
}

int plat_sound_init(void)
{

  if (SDL_InitSubSystem(SDL_INIT_AUDIO)) {
    return -1;
  }

  SDL_AudioSpec spec;

  spec.freq = sound_frequency;
  spec.format = AUDIO_S16;
  spec.channels = 2;
  spec.samples = 512;
  spec.callback = plat_sound_callback;

  if (SDL_OpenAudio(&spec, NULL) < 0) {
    plat_sound_finish();
    return -1;
  }

  SDL_PauseAudio(0);
  return 0;
}

float plat_sound_capacity(void)
{
  int buffered = 0;
  if (buf_w != buf_r) {
    buffered = buf_w > buf_r ? buf_w - buf_r : (buf_w + BUF_LEN) - buf_r;
  }

  return 1.0 - (float)buffered / BUF_LEN;
}

void plat_sound_write(void *data, int bytes)
{
  short *sound_data = (short *)data;
  SDL_LockAudio();

  while (bytes > 0) {
    while (((buf_w + 1) % BUF_LEN) == buf_r) {
      SDL_UnlockAudio();

      if (!limit_frames)
        return;

      plat_sleep_ms(1);
      SDL_LockAudio();
    }

    buf[buf_w] = *sound_data++;

    ++buf_w;
    buf_w %= BUF_LEN;
    bytes -= sizeof(short);
  }
  SDL_UnlockAudio();
}

void plat_sdl_event_handler(void *event_)
{
}

int plat_init(void)
{
  SDL_Init(SDL_INIT_VIDEO);
  screen = SDL_SetVideoMode(320, 240, 16, SDL_SWSURFACE);

  if (screen == NULL) {
    printf("%s, failed to set video mode\n", __func__);
    return -1;
  }

  SDL_ShowCursor(0);

  g_menuscreen_w = 320;
  g_menuscreen_h = 240;
  g_menuscreen_pp = 320;
  g_menuscreen_ptr = fb_flip();

  if (in_sdl_init(&in_sdl_platform_data, plat_sdl_event_handler)) {
    fprintf(stderr, "SDL input failed to init: %s\n", SDL_GetError());
    return -1;
  }
  in_probe();

  if (plat_sound_init()) {
    fprintf(stderr, "SDL sound failed to init: %s\n", SDL_GetError());
    return -1;
  }
  return 0;
}

void plat_pre_finish(void)
{
}

void plat_finish(void)
{
  plat_sound_finish();
  SDL_Quit();
}

void plat_trigger_vibrate(int pad, int low, int high)
{
}