libretro: Fix key passing of update_controller_port_device()
[pcsx_rearmed.git] / frontend / libretro.c
... / ...
CommitLineData
1/*
2 * (C) notaz, 2012,2014,2015
3 *
4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
6 */
7
8#define _GNU_SOURCE 1 // strcasestr
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <strings.h>
13
14#include "../libpcsxcore/misc.h"
15#include "../libpcsxcore/psxcounters.h"
16#include "../libpcsxcore/psxmem_map.h"
17#include "../libpcsxcore/new_dynarec/new_dynarec.h"
18#include "../libpcsxcore/cdrom.h"
19#include "../libpcsxcore/cdriso.h"
20#include "../libpcsxcore/cheat.h"
21#include "../plugins/dfsound/out.h"
22#include "../plugins/dfsound/spu_config.h"
23#include "../plugins/dfinput/externals.h"
24#include "cspace.h"
25#include "main.h"
26#include "plugin.h"
27#include "plugin_lib.h"
28#include "arm_features.h"
29#include "revision.h"
30#include "libretro.h"
31
32#ifdef _3DS
33#include "3ds/3ds_utils.h"
34#endif
35
36#define PORTS_NUMBER 8
37
38static retro_video_refresh_t video_cb;
39static retro_input_poll_t input_poll_cb;
40static retro_input_state_t input_state_cb;
41static retro_environment_t environ_cb;
42static retro_audio_sample_batch_t audio_batch_cb;
43static struct retro_rumble_interface rumble;
44
45static void *vout_buf;
46static int vout_width, vout_height;
47static int vout_doffs_old, vout_fb_dirty;
48static bool vout_can_dupe;
49static bool duping_enable;
50
51static int plugins_opened;
52static int is_pal_mode;
53
54/* memory card data */
55extern char Mcd1Data[MCD_SIZE];
56extern char McdDisable[2];
57
58/* PCSX ReARMed core calls and stuff */
59int in_type[8] = { PSE_PAD_TYPE_STANDARD, PSE_PAD_TYPE_STANDARD,
60 PSE_PAD_TYPE_STANDARD, PSE_PAD_TYPE_STANDARD,
61 PSE_PAD_TYPE_STANDARD, PSE_PAD_TYPE_STANDARD,
62 PSE_PAD_TYPE_STANDARD, PSE_PAD_TYPE_STANDARD };
63int in_analog_left[8][2] = {{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 }};
64int in_analog_right[8][2] = {{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 }};
65unsigned short in_keystate[PORTS_NUMBER];
66int multitap1 = 0;
67int multitap2 = 0;
68int in_enable_vibration = 1;
69
70/* PSX max resolution is 640x512, but with enhancement it's 1024x512 */
71#define VOUT_MAX_WIDTH 1024
72#define VOUT_MAX_HEIGHT 512
73
74static void init_memcard(char *mcd_data)
75{
76 unsigned off = 0;
77 unsigned i;
78
79 memset(mcd_data, 0, MCD_SIZE);
80
81 mcd_data[off++] = 'M';
82 mcd_data[off++] = 'C';
83 off += 0x7d;
84 mcd_data[off++] = 0x0e;
85
86 for (i = 0; i < 15; i++) {
87 mcd_data[off++] = 0xa0;
88 off += 0x07;
89 mcd_data[off++] = 0xff;
90 mcd_data[off++] = 0xff;
91 off += 0x75;
92 mcd_data[off++] = 0xa0;
93 }
94
95 for (i = 0; i < 20; i++) {
96 mcd_data[off++] = 0xff;
97 mcd_data[off++] = 0xff;
98 mcd_data[off++] = 0xff;
99 mcd_data[off++] = 0xff;
100 off += 0x04;
101 mcd_data[off++] = 0xff;
102 mcd_data[off++] = 0xff;
103 off += 0x76;
104 }
105}
106
107static int vout_open(void)
108{
109 return 0;
110}
111
112static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
113{
114 vout_width = w;
115 vout_height = h;
116}
117
118#ifndef FRONTEND_SUPPORTS_RGB565
119static void convert(void *buf, size_t bytes)
120{
121 unsigned int i, v, *p = buf;
122
123 for (i = 0; i < bytes / 4; i++) {
124 v = p[i];
125 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
126 }
127}
128#endif
129
130static void vout_flip(const void *vram, int stride, int bgr24, int w, int h)
131{
132 unsigned short *dest = vout_buf;
133 const unsigned short *src = vram;
134 int dstride = vout_width, h1 = h;
135 int doffs;
136
137 if (vram == NULL) {
138 // blanking
139 memset(vout_buf, 0, dstride * h * 2);
140 goto out;
141 }
142
143 doffs = (vout_height - h) * dstride;
144 doffs += (dstride - w) / 2 & ~1;
145 if (doffs != vout_doffs_old) {
146 // clear borders
147 memset(vout_buf, 0, dstride * h * 2);
148 vout_doffs_old = doffs;
149 }
150 dest += doffs;
151
152 if (bgr24)
153 {
154 // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here?
155 for (; h1-- > 0; dest += dstride, src += stride)
156 {
157 bgr888_to_rgb565(dest, src, w * 3);
158 }
159 }
160 else
161 {
162 for (; h1-- > 0; dest += dstride, src += stride)
163 {
164 bgr555_to_rgb565(dest, src, w * 2);
165 }
166 }
167
168out:
169#ifndef FRONTEND_SUPPORTS_RGB565
170 convert(vout_buf, vout_width * vout_height * 2);
171#endif
172 vout_fb_dirty = 1;
173 pl_rearmed_cbs.flip_cnt++;
174}
175
176static void vout_close(void)
177{
178}
179
180#ifdef _3DS
181typedef struct
182{
183 void* buffer;
184 uint32_t target_map;
185 size_t size;
186 enum psxMapTag tag;
187}psx_map_t;
188
189psx_map_t custom_psx_maps[] = {
190 {NULL, 0x13000000, 0x210000, MAP_TAG_RAM}, // 0x80000000
191 {NULL, 0x12800000, 0x010000, MAP_TAG_OTHER}, // 0x1f800000
192 {NULL, 0x12c00000, 0x080000, MAP_TAG_OTHER}, // 0x1fc00000
193 {NULL, 0x11000000, 0x800000, MAP_TAG_LUTS}, // 0x08000000
194 {NULL, 0x12000000, 0x200000, MAP_TAG_VRAM}, // 0x00000000
195};
196
197void* pl_3ds_mmap(unsigned long addr, size_t size, int is_fixed,
198 enum psxMapTag tag)
199{
200 (void)is_fixed;
201 (void)addr;
202
203 if (__ctr_svchax)
204 {
205 psx_map_t* custom_map = custom_psx_maps;
206
207 for (; custom_map->size; custom_map++)
208 {
209 if ((custom_map->size == size) && (custom_map->tag == tag))
210 {
211 uint32_t ptr_aligned, tmp;
212
213 custom_map->buffer = malloc(size + 0x1000);
214 ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;
215
216 if(svcControlMemory(&tmp, (void*)custom_map->target_map, (void*)ptr_aligned, size, MEMOP_MAP, 0x3) < 0)
217 {
218 SysPrintf("could not map memory @0x%08X\n", custom_map->target_map);
219 exit(1);
220 }
221
222 return (void*)custom_map->target_map;
223 }
224 }
225 }
226
227 return malloc(size);
228}
229
230void pl_3ds_munmap(void *ptr, size_t size, enum psxMapTag tag)
231{
232 (void)tag;
233
234 if (__ctr_svchax)
235 {
236 psx_map_t* custom_map = custom_psx_maps;
237
238 for (; custom_map->size; custom_map++)
239 {
240 if ((custom_map->target_map == (uint32_t)ptr))
241 {
242 uint32_t ptr_aligned, tmp;
243
244 ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;
245
246 svcControlMemory(&tmp, (void*)custom_map->target_map, (void*)ptr_aligned, size, MEMOP_UNMAP, 0x3);
247
248 free(custom_map->buffer);
249 custom_map->buffer = NULL;
250 return;
251 }
252 }
253 }
254
255 free(ptr);
256}
257#endif
258
259static void *pl_mmap(unsigned int size)
260{
261 return psxMap(0, size, 0, MAP_TAG_VRAM);
262}
263
264static void pl_munmap(void *ptr, unsigned int size)
265{
266 psxUnmap(ptr, size, MAP_TAG_VRAM);
267}
268
269struct rearmed_cbs pl_rearmed_cbs = {
270 .pl_vout_open = vout_open,
271 .pl_vout_set_mode = vout_set_mode,
272 .pl_vout_flip = vout_flip,
273 .pl_vout_close = vout_close,
274 .mmap = pl_mmap,
275 .munmap = pl_munmap,
276 /* from psxcounters */
277 .gpu_hcnt = &hSyncCount,
278 .gpu_frame_count = &frame_counter,
279};
280
281void pl_frame_limit(void)
282{
283 /* called once per frame, make psxCpu->Execute() above return */
284 stop = 1;
285}
286
287void pl_timing_prepare(int is_pal)
288{
289 is_pal_mode = is_pal;
290}
291
292void plat_trigger_vibrate(int pad, int low, int high)
293{
294 rumble.set_rumble_state(pad, RETRO_RUMBLE_STRONG, high << 8);
295 rumble.set_rumble_state(pad, RETRO_RUMBLE_WEAK, low ? 0xffff : 0x0);
296}
297
298void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in)
299{
300}
301
302/* sound calls */
303static int snd_init(void)
304{
305 return 0;
306}
307
308static void snd_finish(void)
309{
310}
311
312static int snd_busy(void)
313{
314 return 0;
315}
316
317static void snd_feed(void *buf, int bytes)
318{
319 if (audio_batch_cb != NULL)
320 audio_batch_cb(buf, bytes / 4);
321}
322
323void out_register_libretro(struct out_driver *drv)
324{
325 drv->name = "libretro";
326 drv->init = snd_init;
327 drv->finish = snd_finish;
328 drv->busy = snd_busy;
329 drv->feed = snd_feed;
330}
331
332/* libretro */
333void retro_set_environment(retro_environment_t cb)
334{
335 static const struct retro_variable vars[] = {
336 { "pcsx_rearmed_frameskip", "Frameskip; 0|1|2|3" },
337 { "pcsx_rearmed_region", "Region; Auto|NTSC|PAL" },
338 { "pcsx_rearmed_pad1type", "Pad 1 Type; default|none|standard|analog|negcon" },
339 { "pcsx_rearmed_pad2type", "Pad 2 Type; default|none|standard|analog|negcon" },
340 { "pcsx_rearmed_pad3type", "Pad 3 Type; default|none|standard|analog|negcon" },
341 { "pcsx_rearmed_pad4type", "Pad 4 Type; default|none|standard|analog|negcon" },
342 { "pcsx_rearmed_pad5type", "Pad 5 Type; default|none|standard|analog|negcon" },
343 { "pcsx_rearmed_pad6type", "Pad 6 Type; default|none|standard|analog|negcon" },
344 { "pcsx_rearmed_pad7type", "Pad 7 Type; default|none|standard|analog|negcon" },
345 { "pcsx_rearmed_pad8type", "Pad 8 Type; default|none|standard|analog|negcon" },
346 { "pcsx_rearmed_multitap1", "Multitap 1; auto|disabled|enabled" },
347 { "pcsx_rearmed_multitap2", "Multitap 2; auto|disabled|enabled" },
348#ifndef DRC_DISABLE
349 { "pcsx_rearmed_drc", "Dynamic recompiler; enabled|disabled" },
350#endif
351#ifdef __ARM_NEON__
352 { "pcsx_rearmed_neon_interlace_enable", "Enable interlacing mode(s); disabled|enabled" },
353 { "pcsx_rearmed_neon_enhancement_enable", "Enhanced resolution (slow); disabled|enabled" },
354 { "pcsx_rearmed_neon_enhancement_no_main", "Enhanced resolution speed hack; disabled|enabled" },
355#endif
356 { "pcsx_rearmed_duping_enable", "Frame duping; on|off" },
357 { "pcsx_rearmed_spu_reverb", "Sound: Reverb; on|off" },
358 { "pcsx_rearmed_spu_interpolation", "Sound: Interpolation; simple|gaussian|cubic|off" },
359 { "pcsx_rearmed_pe2_fix", "Parasite Eve 2/Vandal Hearts 1/2 Fix; disabled|enabled" },
360 { "pcsx_rearmed_inuyasha_fix", "InuYasha Sengoku Battle Fix; disabled|enabled" },
361 { NULL, NULL },
362 };
363
364 environ_cb = cb;
365
366 cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void*)vars);
367}
368
369void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
370void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
371void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
372void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
373void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
374
375unsigned retro_api_version(void)
376{
377 return RETRO_API_VERSION;
378}
379
380static void update_controller_port_device(unsigned port, unsigned device)
381{
382 if (port >= PORTS_NUMBER)
383 return;
384
385 struct retro_variable var;
386
387 var.value = NULL;
388 switch (port) {
389 case 0:
390 var.key = "pcsx_rearmed_pad1type";
391 break;
392 case 1:
393 var.key = "pcsx_rearmed_pad2type";
394 break;
395 case 2:
396 var.key = "pcsx_rearmed_pad3type";
397 break;
398 case 3:
399 var.key = "pcsx_rearmed_pad4type";
400 break;
401 case 4:
402 var.key = "pcsx_rearmed_pad5type";
403 break;
404 case 5:
405 var.key = "pcsx_rearmed_pad6type";
406 break;
407 case 6:
408 var.key = "pcsx_rearmed_pad7type";
409 break;
410 case 7:
411 var.key = "pcsx_rearmed_pad8type";
412 break;
413 }
414
415 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
416 {
417 if (strcmp(var.value, "standard") == 0)
418 in_type[0] = PSE_PAD_TYPE_STANDARD;
419 else if (strcmp(var.value, "analog") == 0)
420 in_type[0] = PSE_PAD_TYPE_ANALOGPAD;
421 else if (strcmp(var.value, "negcon") == 0)
422 in_type[0] = PSE_PAD_TYPE_NEGCON;
423 else if (strcmp(var.value, "none") == 0)
424 in_type[0] = PSE_PAD_TYPE_NONE;
425 else // 'default' case
426 {
427 switch (device)
428 {
429 case RETRO_DEVICE_JOYPAD:
430 in_type[port] = PSE_PAD_TYPE_STANDARD;
431 break;
432 case RETRO_DEVICE_ANALOG:
433 in_type[port] = PSE_PAD_TYPE_ANALOGPAD;
434 break;
435 case RETRO_DEVICE_MOUSE:
436 in_type[port] = PSE_PAD_TYPE_MOUSE;
437 break;
438 case RETRO_DEVICE_LIGHTGUN:
439 in_type[port] = PSE_PAD_TYPE_GUN;
440 break;
441 case RETRO_DEVICE_NONE:
442 default:
443 in_type[port] = PSE_PAD_TYPE_NONE;
444 }
445 }
446 }
447}
448
449static void update_multitap()
450{
451 struct retro_variable var;
452
453 var.value = NULL;
454 var.key = "pcsx_rearmed_multitap1";
455 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
456 {
457 if (strcmp(var.value, "enabled") == 0)
458 multitap1 = 1;
459 else if (strcmp(var.value, "disabled") == 0)
460 multitap1 = 0;
461 else // 'auto' case
462 {
463 // If a gamepad is plugged after port 2, we need a first multitap.
464 multitap1 = 0;
465 for (int port = 2; port < PORTS_NUMBER; port++)
466 multitap1 |= in_type[port] != PSE_PAD_TYPE_NONE;
467 }
468 }
469
470 var.value = NULL;
471 var.key = "pcsx_rearmed_multitap2";
472 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
473 {
474 if (strcmp(var.value, "enabled") == 0)
475 multitap2 = 1;
476 else if (strcmp(var.value, "disabled") == 0)
477 multitap2 = 0;
478 else // 'auto' case
479 {
480 // If a gamepad is plugged after port 4, we need a second multitap.
481 multitap2 = 0;
482 for (int port = 4; port < PORTS_NUMBER; port++)
483 multitap2 |= in_type[port] != PSE_PAD_TYPE_NONE;
484 }
485 }
486}
487
488void retro_set_controller_port_device(unsigned port, unsigned device)
489{
490 SysPrintf("port %u device %u",port,device);
491
492 if (port >= PORTS_NUMBER)
493 return;
494
495 update_controller_port_device(port, device);
496 update_multitap();
497}
498
499void retro_get_system_info(struct retro_system_info *info)
500{
501 memset(info, 0, sizeof(*info));
502 info->library_name = "PCSX-ReARMed";
503 info->library_version = "r22";
504 info->valid_extensions = "bin|cue|img|mdf|pbp|toc|cbn|m3u";
505 info->need_fullpath = true;
506}
507
508void retro_get_system_av_info(struct retro_system_av_info *info)
509{
510 memset(info, 0, sizeof(*info));
511 info->timing.fps = is_pal_mode ? 50 : 60;
512 info->timing.sample_rate = 44100;
513 info->geometry.base_width = 320;
514 info->geometry.base_height = 240;
515 info->geometry.max_width = VOUT_MAX_WIDTH;
516 info->geometry.max_height = VOUT_MAX_HEIGHT;
517 info->geometry.aspect_ratio = 4.0 / 3.0;
518}
519
520/* savestates */
521size_t retro_serialize_size(void)
522{
523 // it's currently 4380651-4397047 bytes,
524 // but have some reserved for future
525 return 0x440000;
526}
527
528struct save_fp {
529 char *buf;
530 size_t pos;
531 int is_write;
532};
533
534static void *save_open(const char *name, const char *mode)
535{
536 struct save_fp *fp;
537
538 if (name == NULL || mode == NULL)
539 return NULL;
540
541 fp = malloc(sizeof(*fp));
542 if (fp == NULL)
543 return NULL;
544
545 fp->buf = (char *)name;
546 fp->pos = 0;
547 fp->is_write = (mode[0] == 'w' || mode[1] == 'w');
548
549 return fp;
550}
551
552static int save_read(void *file, void *buf, u32 len)
553{
554 struct save_fp *fp = file;
555 if (fp == NULL || buf == NULL)
556 return -1;
557
558 memcpy(buf, fp->buf + fp->pos, len);
559 fp->pos += len;
560 return len;
561}
562
563static int save_write(void *file, const void *buf, u32 len)
564{
565 struct save_fp *fp = file;
566 if (fp == NULL || buf == NULL)
567 return -1;
568
569 memcpy(fp->buf + fp->pos, buf, len);
570 fp->pos += len;
571 return len;
572}
573
574static long save_seek(void *file, long offs, int whence)
575{
576 struct save_fp *fp = file;
577 if (fp == NULL)
578 return -1;
579
580 switch (whence) {
581 case SEEK_CUR:
582 fp->pos += offs;
583 return fp->pos;
584 case SEEK_SET:
585 fp->pos = offs;
586 return fp->pos;
587 default:
588 return -1;
589 }
590}
591
592static void save_close(void *file)
593{
594 struct save_fp *fp = file;
595 size_t r_size = retro_serialize_size();
596 if (fp == NULL)
597 return;
598
599 if (fp->pos > r_size)
600 SysPrintf("ERROR: save buffer overflow detected\n");
601 else if (fp->is_write && fp->pos < r_size)
602 // make sure we don't save trash in leftover space
603 memset(fp->buf + fp->pos, 0, r_size - fp->pos);
604 free(fp);
605}
606
607bool retro_serialize(void *data, size_t size)
608{
609 int ret = SaveState(data);
610 return ret == 0 ? true : false;
611}
612
613bool retro_unserialize(const void *data, size_t size)
614{
615 int ret = LoadState(data);
616 return ret == 0 ? true : false;
617}
618
619/* cheats */
620void retro_cheat_reset(void)
621{
622 ClearAllCheats();
623}
624
625void retro_cheat_set(unsigned index, bool enabled, const char *code)
626{
627 char buf[256];
628 int ret;
629
630 // cheat funcs are destructive, need a copy..
631 strncpy(buf, code, sizeof(buf));
632 buf[sizeof(buf) - 1] = 0;
633
634 if (index < NumCheats)
635 ret = EditCheat(index, "", buf);
636 else
637 ret = AddCheat("", buf);
638
639 if (ret != 0)
640 SysPrintf("Failed to set cheat %#u\n", index);
641 else if (index < NumCheats)
642 Cheats[index].Enabled = enabled;
643}
644
645/* multidisk support */
646static bool disk_ejected;
647static unsigned int disk_current_index;
648static unsigned int disk_count;
649static struct disks_state {
650 char *fname;
651 int internal_index; // for multidisk eboots
652} disks[8];
653
654static bool disk_set_eject_state(bool ejected)
655{
656 // weird PCSX API..
657 SetCdOpenCaseTime(ejected ? -1 : (time(NULL) + 2));
658 LidInterrupt();
659
660 disk_ejected = ejected;
661 return true;
662}
663
664static bool disk_get_eject_state(void)
665{
666 /* can't be controlled by emulated software */
667 return disk_ejected;
668}
669
670static unsigned int disk_get_image_index(void)
671{
672 return disk_current_index;
673}
674
675static bool disk_set_image_index(unsigned int index)
676{
677 if (index >= sizeof(disks) / sizeof(disks[0]))
678 return false;
679
680 CdromId[0] = '\0';
681 CdromLabel[0] = '\0';
682
683 if (disks[index].fname == NULL) {
684 SysPrintf("missing disk #%u\n", index);
685 CDR_shutdown();
686
687 // RetroArch specifies "no disk" with index == count,
688 // so don't fail here..
689 disk_current_index = index;
690 return true;
691 }
692
693 SysPrintf("switching to disk %u: \"%s\" #%d\n", index,
694 disks[index].fname, disks[index].internal_index);
695
696 cdrIsoMultidiskSelect = disks[index].internal_index;
697 set_cd_image(disks[index].fname);
698 if (ReloadCdromPlugin() < 0) {
699 SysPrintf("failed to load cdr plugin\n");
700 return false;
701 }
702 if (CDR_open() < 0) {
703 SysPrintf("failed to open cdr plugin\n");
704 return false;
705 }
706
707 if (!disk_ejected) {
708 SetCdOpenCaseTime(time(NULL) + 2);
709 LidInterrupt();
710 }
711
712 disk_current_index = index;
713 return true;
714}
715
716static unsigned int disk_get_num_images(void)
717{
718 return disk_count;
719}
720
721static bool disk_replace_image_index(unsigned index,
722 const struct retro_game_info *info)
723{
724 char *old_fname;
725 bool ret = true;
726
727 if (index >= sizeof(disks) / sizeof(disks[0]))
728 return false;
729
730 old_fname = disks[index].fname;
731 disks[index].fname = NULL;
732 disks[index].internal_index = 0;
733
734 if (info != NULL) {
735 disks[index].fname = strdup(info->path);
736 if (index == disk_current_index)
737 ret = disk_set_image_index(index);
738 }
739
740 if (old_fname != NULL)
741 free(old_fname);
742
743 return ret;
744}
745
746static bool disk_add_image_index(void)
747{
748 if (disk_count >= 8)
749 return false;
750
751 disk_count++;
752 return true;
753}
754
755static struct retro_disk_control_callback disk_control = {
756 .set_eject_state = disk_set_eject_state,
757 .get_eject_state = disk_get_eject_state,
758 .get_image_index = disk_get_image_index,
759 .set_image_index = disk_set_image_index,
760 .get_num_images = disk_get_num_images,
761 .replace_image_index = disk_replace_image_index,
762 .add_image_index = disk_add_image_index,
763};
764
765// just in case, maybe a win-rt port in the future?
766#ifdef _WIN32
767#define SLASH '\\'
768#else
769#define SLASH '/'
770#endif
771
772static char base_dir[PATH_MAX];
773
774static bool read_m3u(const char *file)
775{
776 char line[PATH_MAX];
777 char name[PATH_MAX];
778 FILE *f = fopen(file, "r");
779 if (!f)
780 return false;
781
782 while (fgets(line, sizeof(line), f) && disk_count < sizeof(disks) / sizeof(disks[0])) {
783 if (line[0] == '#')
784 continue;
785 char *carrige_return = strchr(line, '\r');
786 if (carrige_return)
787 *carrige_return = '\0';
788 char *newline = strchr(line, '\n');
789 if (newline)
790 *newline = '\0';
791
792 if (line[0] != '\0')
793 {
794 snprintf(name, sizeof(name), "%s%c%s", base_dir, SLASH, line);
795 disks[disk_count++].fname = strdup(name);
796 }
797 }
798
799 fclose(f);
800 return (disk_count != 0);
801}
802
803static void extract_directory(char *buf, const char *path, size_t size)
804{
805 char *base;
806 strncpy(buf, path, size - 1);
807 buf[size - 1] = '\0';
808
809 base = strrchr(buf, '/');
810 if (!base)
811 base = strrchr(buf, '\\');
812
813 if (base)
814 *base = '\0';
815 else
816 {
817 buf[0] = '.';
818 buf[1] = '\0';
819 }
820}
821
822#if defined(__QNX__) || defined(_WIN32)
823/* Blackberry QNX doesn't have strcasestr */
824
825/*
826 * Find the first occurrence of find in s, ignore case.
827 */
828char *
829strcasestr(const char *s, const char*find)
830{
831 char c, sc;
832 size_t len;
833
834 if ((c = *find++) != 0) {
835 c = tolower((unsigned char)c);
836 len = strlen(find);
837 do {
838 do {
839 if ((sc = *s++) == 0)
840 return (NULL);
841 } while ((char)tolower((unsigned char)sc) != c);
842 } while (strncasecmp(s, find, len) != 0);
843 s--;
844 }
845 return ((char *)s);
846}
847#endif
848
849bool retro_load_game(const struct retro_game_info *info)
850{
851 size_t i;
852 bool is_m3u = (strcasestr(info->path, ".m3u") != NULL);
853
854 struct retro_input_descriptor desc[] = {
855 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
856 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
857 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
858 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
859 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
860 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
861 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
862 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
863 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
864 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
865 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
866 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
867 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
868 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
869 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
870 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
871 { 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
872 { 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
873 { 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
874 { 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
875
876
877 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
878 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
879 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
880 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
881 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
882 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
883 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
884 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
885 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
886 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
887 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
888 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
889 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
890 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
891 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
892 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
893 { 1, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
894 { 1, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
895 { 1, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
896 { 1, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
897
898 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
899 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
900 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
901 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
902 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
903 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
904 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
905 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
906 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
907 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
908 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
909 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
910 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
911 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
912 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
913 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
914 { 2, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
915 { 2, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
916 { 2, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
917 { 2, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
918
919 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
920 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
921 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
922 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
923 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
924 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
925 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
926 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
927 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
928 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
929 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
930 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
931 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
932 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
933 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
934 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
935 { 3, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
936 { 3, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
937 { 3, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
938 { 3, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
939
940 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
941 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
942 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
943 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
944 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
945 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
946 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
947 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
948 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
949 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
950 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
951 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
952 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
953 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
954 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
955 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
956 { 4, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
957 { 4, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
958 { 4, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
959 { 4, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
960
961 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
962 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
963 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
964 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
965 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
966 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
967 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
968 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
969 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
970 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
971 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
972 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
973 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
974 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
975 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
976 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
977 { 5, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
978 { 5, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
979 { 5, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
980 { 5, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
981
982 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
983 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
984 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
985 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
986 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
987 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
988 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
989 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
990 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
991 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
992 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
993 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
994 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
995 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
996 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
997 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
998 { 6, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
999 { 6, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1000 { 6, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1001 { 6, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1002
1003 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1004 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1005 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1006 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1007 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1008 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1009 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1010 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1011 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1012 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1013 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1014 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1015 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1016 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1017 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1018 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1019 { 7, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1020 { 7, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1021 { 7, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1022 { 7, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1023
1024 { 0 },
1025 };
1026
1027 environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
1028
1029#ifdef FRONTEND_SUPPORTS_RGB565
1030 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
1031 if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
1032 SysPrintf("RGB565 supported, using it\n");
1033 }
1034#endif
1035
1036 if (info == NULL || info->path == NULL) {
1037 SysPrintf("info->path required\n");
1038 return false;
1039 }
1040
1041 if (plugins_opened) {
1042 ClosePlugins();
1043 plugins_opened = 0;
1044 }
1045
1046 for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++) {
1047 if (disks[i].fname != NULL) {
1048 free(disks[i].fname);
1049 disks[i].fname = NULL;
1050 }
1051 disks[i].internal_index = 0;
1052 }
1053
1054 disk_current_index = 0;
1055 extract_directory(base_dir, info->path, sizeof(base_dir));
1056
1057 if (is_m3u) {
1058 if (!read_m3u(info->path)) {
1059 SysPrintf("failed to read m3u file\n");
1060 return false;
1061 }
1062 } else {
1063 disk_count = 1;
1064 disks[0].fname = strdup(info->path);
1065 }
1066
1067 set_cd_image(disks[0].fname);
1068
1069 /* have to reload after set_cd_image for correct cdr plugin */
1070 if (LoadPlugins() == -1) {
1071 SysPrintf("failed to load plugins\n");
1072 return false;
1073 }
1074
1075 plugins_opened = 1;
1076 NetOpened = 0;
1077
1078 if (OpenPlugins() == -1) {
1079 SysPrintf("failed to open plugins\n");
1080 return false;
1081 }
1082
1083 plugin_call_rearmed_cbs();
1084 dfinput_activate();
1085
1086 Config.PsxAuto = 1;
1087 if (CheckCdrom() == -1) {
1088 SysPrintf("unsupported/invalid CD image: %s\n", info->path);
1089 return false;
1090 }
1091
1092 SysReset();
1093
1094 if (LoadCdrom() == -1) {
1095 SysPrintf("could not load CD-ROM!\n");
1096 return false;
1097 }
1098 emu_on_new_cd(0);
1099
1100 // multidisk images
1101 if (!is_m3u) {
1102 disk_count = cdrIsoMultidiskCount < 8 ? cdrIsoMultidiskCount : 8;
1103 for (i = 1; i < sizeof(disks) / sizeof(disks[0]) && i < cdrIsoMultidiskCount; i++) {
1104 disks[i].fname = strdup(info->path);
1105 disks[i].internal_index = i;
1106 }
1107 }
1108
1109 return true;
1110}
1111
1112bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
1113{
1114 return false;
1115}
1116
1117void retro_unload_game(void)
1118{
1119}
1120
1121unsigned retro_get_region(void)
1122{
1123 return is_pal_mode ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
1124}
1125
1126void *retro_get_memory_data(unsigned id)
1127{
1128 if (id == RETRO_MEMORY_SAVE_RAM)
1129 return Mcd1Data;
1130 else
1131 return NULL;
1132}
1133
1134size_t retro_get_memory_size(unsigned id)
1135{
1136 if (id == RETRO_MEMORY_SAVE_RAM)
1137 return MCD_SIZE;
1138 else
1139 return 0;
1140}
1141
1142void retro_reset(void)
1143{
1144 SysReset();
1145}
1146
1147static const unsigned short retro_psx_map[] = {
1148 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
1149 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
1150 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
1151 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
1152 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
1153 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
1154 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
1155 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
1156 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
1157 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
1158 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
1159 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
1160 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
1161 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
1162 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
1163 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
1164};
1165#define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
1166
1167static void update_variables(bool in_flight)
1168{
1169 struct retro_variable var;
1170
1171 var.value = NULL;
1172 var.key = "pcsx_rearmed_frameskip";
1173 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1174 pl_rearmed_cbs.frameskip = atoi(var.value);
1175
1176 var.value = NULL;
1177 var.key = "pcsx_rearmed_region";
1178 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1179 {
1180 Config.PsxAuto = 0;
1181 if (strcmp(var.value, "Automatic") == 0)
1182 Config.PsxAuto = 1;
1183 else if (strcmp(var.value, "NTSC") == 0)
1184 Config.PsxType = 0;
1185 else if (strcmp(var.value, "PAL") == 0)
1186 Config.PsxType = 1;
1187 }
1188
1189 for (int i = 0; i < PORTS_NUMBER; i++)
1190 update_controller_port_device(i, RETRO_DEVICE_NONE);
1191
1192 update_multitap();
1193
1194#ifdef __ARM_NEON__
1195 var.value = "NULL";
1196 var.key = "pcsx_rearmed_neon_interlace_enable";
1197
1198 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1199 {
1200 if (strcmp(var.value, "disabled") == 0)
1201 pl_rearmed_cbs.gpu_neon.allow_interlace = 0;
1202 else if (strcmp(var.value, "enabled") == 0)
1203 pl_rearmed_cbs.gpu_neon.allow_interlace = 1;
1204 }
1205
1206 var.value = NULL;
1207 var.key = "pcsx_rearmed_neon_enhancement_enable";
1208
1209 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1210 {
1211 if (strcmp(var.value, "disabled") == 0)
1212 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
1213 else if (strcmp(var.value, "enabled") == 0)
1214 pl_rearmed_cbs.gpu_neon.enhancement_enable = 1;
1215 }
1216
1217 var.value = NULL;
1218 var.key = "pcsx_rearmed_neon_enhancement_no_main";
1219
1220 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1221 {
1222 if (strcmp(var.value, "disabled") == 0)
1223 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
1224 else if (strcmp(var.value, "enabled") == 0)
1225 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 1;
1226 }
1227#endif
1228
1229 var.value = "NULL";
1230 var.key = "pcsx_rearmed_duping_enable";
1231
1232 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1233 {
1234 if (strcmp(var.value, "off") == 0)
1235 duping_enable = false;
1236 else if (strcmp(var.value, "on") == 0)
1237 duping_enable = true;
1238 }
1239
1240#ifndef DRC_DISABLE
1241 var.value = NULL;
1242 var.key = "pcsx_rearmed_drc";
1243
1244 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1245 {
1246 R3000Acpu *prev_cpu = psxCpu;
1247
1248#ifdef _3DS
1249 if(!__ctr_svchax)
1250 Config.Cpu = CPU_INTERPRETER;
1251 else
1252#endif
1253 if (strcmp(var.value, "disabled") == 0)
1254 Config.Cpu = CPU_INTERPRETER;
1255 else if (strcmp(var.value, "enabled") == 0)
1256 Config.Cpu = CPU_DYNAREC;
1257
1258 psxCpu = (Config.Cpu == CPU_INTERPRETER) ? &psxInt : &psxRec;
1259 if (psxCpu != prev_cpu) {
1260 prev_cpu->Shutdown();
1261 psxCpu->Init();
1262 psxCpu->Reset(); // not really a reset..
1263 }
1264 }
1265#endif
1266
1267 var.value = "NULL";
1268 var.key = "pcsx_rearmed_spu_reverb";
1269
1270 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1271 {
1272 if (strcmp(var.value, "off") == 0)
1273 spu_config.iUseReverb = false;
1274 else if (strcmp(var.value, "on") == 0)
1275 spu_config.iUseReverb = true;
1276 }
1277
1278 var.value = "NULL";
1279 var.key = "pcsx_rearmed_spu_interpolation";
1280
1281 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1282 {
1283 if (strcmp(var.value, "simple") == 0)
1284 spu_config.iUseInterpolation = 1;
1285 else if (strcmp(var.value, "gaussian") == 0)
1286 spu_config.iUseInterpolation = 2;
1287 else if (strcmp(var.value, "cubic") == 0)
1288 spu_config.iUseInterpolation = 3;
1289 else if (strcmp(var.value, "off") == 0)
1290 spu_config.iUseInterpolation = 0;
1291 }
1292
1293 var.value = "NULL";
1294 var.key = "pcsx_rearmed_pe2_fix";
1295
1296 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1297 {
1298 if (strcmp(var.value, "disabled") == 0)
1299 Config.RCntFix = 0;
1300 else if (strcmp(var.value, "enabled") == 0)
1301 Config.RCntFix = 1;
1302 }
1303
1304 var.value = "NULL";
1305 var.key = "pcsx_rearmed_inuyasha_fix";
1306
1307 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1308 {
1309 if (strcmp(var.value, "disabled") == 0)
1310 Config.VSyncWA = 0;
1311 else if (strcmp(var.value, "enabled") == 0)
1312 Config.VSyncWA = 1;
1313 }
1314
1315 if (in_flight) {
1316 // inform core things about possible config changes
1317 plugin_call_rearmed_cbs();
1318
1319 if (GPU_open != NULL && GPU_close != NULL) {
1320 GPU_close();
1321 GPU_open(&gpuDisp, "PCSX", NULL);
1322 }
1323
1324 dfinput_activate();
1325 }
1326}
1327
1328void retro_run(void)
1329{
1330 int i;
1331
1332 input_poll_cb();
1333
1334 bool updated = false;
1335 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
1336 update_variables(true);
1337
1338 // reset all keystate, query libretro for keystate
1339 int j;
1340 for(i = 0; i < PORTS_NUMBER; i++) {
1341 in_keystate[i] = 0;
1342
1343 if (in_type[i] == PSE_PAD_TYPE_NONE)
1344 continue;
1345
1346 // query libretro for keystate
1347 for (j = 0; j < RETRO_PSX_MAP_LEN; j++)
1348 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, j))
1349 in_keystate[i] |= retro_psx_map[j];
1350
1351 if (in_type[i] == PSE_PAD_TYPE_ANALOGPAD)
1352 {
1353 in_analog_left[i][0] = (input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X) / 256) + 128;
1354 in_analog_left[i][1] = (input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y) / 256) + 128;
1355 in_analog_right[i][0] = (input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X) / 256) + 128;
1356 in_analog_right[i][1] = (input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y) / 256) + 128;
1357 }
1358 }
1359
1360 stop = 0;
1361 psxCpu->Execute();
1362
1363 video_cb((vout_fb_dirty || !vout_can_dupe || !duping_enable) ? vout_buf : NULL,
1364 vout_width, vout_height, vout_width * 2);
1365 vout_fb_dirty = 0;
1366}
1367
1368static bool try_use_bios(const char *path)
1369{
1370 FILE *f;
1371 long size;
1372 const char *name;
1373
1374 f = fopen(path, "rb");
1375 if (f == NULL)
1376 return false;
1377
1378 fseek(f, 0, SEEK_END);
1379 size = ftell(f);
1380 fclose(f);
1381
1382 if (size != 512 * 1024)
1383 return false;
1384
1385 name = strrchr(path, SLASH);
1386 if (name++ == NULL)
1387 name = path;
1388 snprintf(Config.Bios, sizeof(Config.Bios), "%s", name);
1389 return true;
1390}
1391
1392#ifndef VITA
1393#include <sys/types.h>
1394#include <dirent.h>
1395
1396static bool find_any_bios(const char *dirpath, char *path, size_t path_size)
1397{
1398 DIR *dir;
1399 struct dirent *ent;
1400 bool ret = false;
1401
1402 dir = opendir(dirpath);
1403 if (dir == NULL)
1404 return false;
1405
1406 while ((ent = readdir(dir))) {
1407 if (strncasecmp(ent->d_name, "scph", 4) != 0)
1408 continue;
1409
1410 snprintf(path, path_size, "%s/%s", dirpath, ent->d_name);
1411 ret = try_use_bios(path);
1412 if (ret)
1413 break;
1414 }
1415 closedir(dir);
1416 return ret;
1417}
1418#else
1419#define find_any_bios(...) false
1420#endif
1421
1422static void check_system_specs(void)
1423{
1424 unsigned level = 6;
1425 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
1426}
1427
1428void retro_init(void)
1429{
1430 const char *bios[] = { "scph1001", "scph5501", "scph7001" };
1431 const char *dir;
1432 char path[256];
1433 int i, ret;
1434 bool found_bios = false;
1435
1436#ifdef _3DS
1437 psxMapHook = pl_3ds_mmap;
1438 psxUnmapHook = pl_3ds_munmap;
1439#endif
1440 ret = emu_core_preinit();
1441#ifdef _3DS
1442 /* emu_core_preinit sets the cpu to dynarec */
1443 if(!__ctr_svchax)
1444 Config.Cpu = CPU_INTERPRETER;
1445#endif
1446 ret |= emu_core_init();
1447 if (ret != 0) {
1448 SysPrintf("PCSX init failed.\n");
1449 exit(1);
1450 }
1451
1452#ifdef _3DS
1453 vout_buf = linearMemAlign(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2, 0x80);
1454#elif defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && !defined(VITA)
1455 posix_memalign(&vout_buf, 16, VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
1456#else
1457 vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
1458#endif
1459
1460 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
1461 {
1462 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s", dir);
1463
1464 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) {
1465 snprintf(path, sizeof(path), "%s/%s.bin", dir, bios[i]);
1466 found_bios = try_use_bios(path);
1467 if (found_bios)
1468 break;
1469 }
1470
1471 if (!found_bios)
1472 found_bios = find_any_bios(dir, path, sizeof(path));
1473 }
1474 if (found_bios) {
1475 SysPrintf("found BIOS file: %s\n", Config.Bios);
1476 }
1477 else
1478 {
1479 SysPrintf("no BIOS files found.\n");
1480 struct retro_message msg =
1481 {
1482 "no BIOS found, expect bugs!",
1483 180
1484 };
1485 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, (void*)&msg);
1486 }
1487
1488 environ_cb(RETRO_ENVIRONMENT_GET_CAN_DUPE, &vout_can_dupe);
1489 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
1490 environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble);
1491
1492 /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times)
1493 * we have to do this because cache misses and some IO penalties
1494 * are not emulated. Warning: changing this may break compatibility. */
1495 cycle_multiplier = 175;
1496#ifdef HAVE_PRE_ARMV7
1497 cycle_multiplier = 200;
1498#endif
1499 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
1500 spu_config.iUseFixedUpdates = 1;
1501
1502 McdDisable[0] = 0;
1503 McdDisable[1] = 1;
1504 init_memcard(Mcd1Data);
1505
1506 SaveFuncs.open = save_open;
1507 SaveFuncs.read = save_read;
1508 SaveFuncs.write = save_write;
1509 SaveFuncs.seek = save_seek;
1510 SaveFuncs.close = save_close;
1511
1512 update_variables(false);
1513 check_system_specs();
1514}
1515
1516void retro_deinit(void)
1517{
1518 SysClose();
1519#ifdef _3DS
1520 linearFree(vout_buf);
1521#else
1522 free(vout_buf);
1523#endif
1524 vout_buf = NULL;
1525}
1526
1527#ifdef VITA
1528#include <psp2/kernel/threadmgr.h>
1529int usleep (unsigned long us)
1530{
1531 sceKernelDelayThread(us);
1532}
1533#endif