libretro.c 7.43 KB
Newer Older
Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
1
#include "lutro.h"
Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
2
#include "audio.h"
Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
3
4
5
6
7
8
9

#include <stdint.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
Libretro-Admin's avatar
Libretro-Admin committed
10

11
#include <libretro.h>
Libretro-Admin's avatar
Libretro-Admin committed
12
13
#include <streams/file_stream.h>

RobLoach's avatar
RobLoach committed
14
#include "joystick.h"
Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
15

Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
16
17
18
static bool use_audio_cb;
int16_t audio_buffer[2 * AUDIO_FRAMES];

Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
19
static struct retro_log_callback logging;
20
21

static retro_log_printf_t log_cb;
Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
22
23
24
25
26
27
28
29
30
static retro_video_refresh_t video_cb;
static retro_audio_sample_t audio_cb;
static retro_audio_sample_batch_t audio_batch_cb;
static retro_environment_t environ_cb;
static retro_input_poll_t input_poll_cb;
static retro_input_state_t input_state_cb;

double frame_time = 0;

Libretro-Admin's avatar
Nit    
Libretro-Admin committed
31
static void emit_audio(void)
Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
32
33
34
35
36
37
38
39
40
41
{
   mixer_render(audio_buffer);
   audio_batch_cb(audio_buffer, AUDIO_FRAMES);
}

static void audio_set_state(bool enable)
{
   (void)enable;
}

Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
42
43
44
45
46
47
48
49
50
51
52
static void fallback_log(enum retro_log_level level, const char *fmt, ...)
{
   (void)level;
   va_list va;
   va_start(va, fmt);
   vfprintf(stderr, fmt, va);
   va_end(va);
}

void retro_init(void)
{
53
54
   lutro_init();

55
56
57
58
59
   if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging))
      log_cb = logging.log;
   else
      log_cb = fallback_log;

60
61
62
63
   // Always get the perf interface because we need it for the timers
   if (!environ_cb( RETRO_ENVIRONMENT_GET_PERF_INTERFACE, &perf_cb))
   {
      perf_cb.get_time_usec = NULL;
64
      log_cb(RETRO_LOG_WARN, "Could not get the perf interface\n");
65
   }
Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
}

void retro_deinit(void)
{
   lutro_deinit();

   if (settings.framebuffer) {
      free(settings.framebuffer);
      settings.framebuffer = NULL;
   }
}

unsigned retro_api_version(void)
{
   return RETRO_API_VERSION;
}

void retro_set_controller_port_device(unsigned port, unsigned device)
{
   (void)port;
   (void)device;
}

Libretro-Admin's avatar
Libretro-Admin committed
89
90
91
92
93
94
95
96
97
98
99
100
101
/*---------------------------------------------------------------------------*/
/* compatibility functions */

#ifdef __CELLOS_LV2__

char* getenv( const char* name)
{
  (void)name;
  return NULL;
}

#endif

Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
102
103
104
105
void retro_get_system_info(struct retro_system_info *info)
{
   memset(info, 0, sizeof(*info));
   info->library_name     = "lutro";
106
107
108
109
#ifndef GIT_VERSION
#define GIT_VERSION ""
#endif
   info->library_version  = VERSION_STRING GIT_VERSION;
Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
110
   info->need_fullpath    = true;
111
   info->valid_extensions = "lutro|lua";
Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
}

void retro_get_system_av_info(struct retro_system_av_info *info)
{
   info->timing.fps = 60.0;
   info->timing.sample_rate = 44100.0;

   info->geometry.base_width   = settings.width;
   info->geometry.base_height  = settings.height;
   info->geometry.max_width    = settings.width;
   info->geometry.max_height   = settings.height;
   info->geometry.aspect_ratio = (float)settings.width/(float)settings.height;
}

void retro_set_environment(retro_environment_t cb)
{
Libretro-Admin's avatar
Libretro-Admin committed
128
   struct retro_vfs_interface_info vfs_iface_info = { 1, NULL };
Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
129
130
131
132
133
   environ_cb = cb;

   bool no_rom = false;
   cb(RETRO_ENVIRONMENT_SET_SUPPORT_NO_GAME, &no_rom);

Libretro-Admin's avatar
Libretro-Admin committed
134
135
   if (environ_cb(RETRO_ENVIRONMENT_GET_VFS_INTERFACE, &vfs_iface_info))
      filestream_vfs_init(&vfs_iface_info);
Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
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
}

void retro_set_audio_sample(retro_audio_sample_t cb)
{
   audio_cb = cb;
}

void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb)
{
   audio_batch_cb = cb;
}

void retro_set_input_poll(retro_input_poll_t cb)
{
   input_poll_cb = cb;
}

void retro_set_input_state(retro_input_state_t cb)
{
   input_state_cb = cb;
   settings.input_cb = cb;
}

void retro_set_video_refresh(retro_video_refresh_t cb)
{
   video_cb = cb;
}

void retro_reset(void)
{
166
   lutro_reset();
Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
167
168
169
170
171
172
173
174
175
176
177
178
}

static void frame_time_cb(retro_usec_t usec)
{
   frame_time = usec / 1000000.0;
}

void retro_run(void)
{
   input_poll_cb();

   lutro_run(frame_time);
Higor Eurípedes's avatar
Higor Eurípedes committed
179
   video_cb(settings.framebuffer, settings.width, settings.height, settings.pitch);
Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
180
181
182
183
184
185
186
187
188
}

bool retro_load_game(const struct retro_game_info *info)
{
   struct retro_input_descriptor desc[] = {
      { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT,  "Left" },
      { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP,    "Up" },
      { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN,  "Down" },
      { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" },
SapphireDrew's avatar
SapphireDrew committed
189
190
191
192
193
194
195
196
197
198
199
200
      { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
      { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
      { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
      { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" },
      { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "X" },
      { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Y" },
      { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
      { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
      { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
      { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
      { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
      { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
201
202
203
      { 0 },
   };

Libretro-Admin's avatar
Libretro-Admin committed
204
205
206
   if (!info)
      return false;

Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
207
208
209
210
211
   environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);

   enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_XRGB8888;
   if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt))
   {
212
      log_cb(RETRO_LOG_INFO, "XRGB_8888 is not supported.\n");
Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
213
214
215
216
217
218
      return false;
   }

   struct retro_frame_time_callback frame_cb = { frame_time_cb, 1000000 / 60 };
   environ_cb(RETRO_ENVIRONMENT_SET_FRAME_TIME_CALLBACK, &frame_cb);

Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
219
220
221
   struct retro_audio_callback audio_cb = { emit_audio, audio_set_state };
   use_audio_cb = environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_CALLBACK, &audio_cb);

222
223
   if (!perf_cb.get_time_usec)
   {
224
      log_cb(RETRO_LOG_ERROR, "Core needs the perf interface\n");
225
226
227
      return false;
   }

Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
228
229
230
231
232
233
234
   int success = lutro_load(info->path);

   return success;
}

void retro_unload_game(void)
{
235
236
237
	// Workaround a crash on Windows & Android because the callbacks are invoked after the DLL/SO was unloaded 
	struct retro_audio_callback no_audio_callback_definition = { NULL, NULL }; 
	environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_CALLBACK, &no_audio_callback_definition); 
Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
238
239
}

240
void lutro_shutdown_game(void)
RobLoach's avatar
RobLoach committed
241
242
243
244
{
   environ_cb(RETRO_ENVIRONMENT_SHUTDOWN, 0);
}

Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
unsigned retro_get_region(void)
{
   return RETRO_REGION_NTSC;
}

bool retro_load_game_special(unsigned type, const struct retro_game_info *info, size_t num)
{
   (void)type;
   (void)info;
   (void)num;
   return false;
}

size_t retro_serialize_size(void)
{
260
   return lutro_serialize_size();
Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
261
262
263
264
}

bool retro_serialize(void *data_, size_t size)
{
265
   return lutro_serialize(data_, size);
Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
266
267
268
269
}

bool retro_unserialize(const void *data_, size_t size)
{
270
   return lutro_unserialize(data_, size);
Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
271
272
273
274
275
276
277
278
}

void *retro_get_memory_data(unsigned id)
{
//   if (id != RETRO_MEMORY_SAVE_RAM)
//      return NULL;

//   return game_data();
279
   return NULL;
Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
280
281
282
283
284
285
286
287
}

size_t retro_get_memory_size(unsigned id)
{
//   if (id != RETRO_MEMORY_SAVE_RAM)
//      return 0;

//   return game_data_size();
288
   return 0;
Jean-Andre Santoni's avatar
Jean-Andre Santoni committed
289
290
291
292
293
294
295
296
297
298
299
300
}

void retro_cheat_reset(void)
{}

void retro_cheat_set(unsigned index, bool enabled, const char *code)
{
   (void)index;
   (void)enabled;
   (void)code;
}

301
#if defined(__QNX__) || defined(_MSC_VER)
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* QNX doesn't have this */
int vasprintf(char **strp, const char *fmt, va_list ap)
{
   /* measure the string */
   int len = vsnprintf(NULL,0,fmt,ap);
   /* try allocating some memory */
   if((len < 0) || ((*strp = malloc(++len)) == NULL)) return -1;
   /* print the string */
   len = vsnprintf(*strp,len,fmt,ap);
   /* handle failure */
   if(len < 0) free(*strp);
   return len;
}
#endif