libretro: Split 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_NONE, PSE_PAD_TYPE_NONE,
60 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
61 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
62 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE };
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 int controller_port_variable(unsigned port, struct retro_variable *var)
381{
382 if (port >= PORTS_NUMBER)
383 return 0;
384
385 if (!environ_cb)
386 return 0;
387
388 var->value = NULL;
389 switch (port) {
390 case 0:
391 var->key = "pcsx_rearmed_pad1type";
392 break;
393 case 1:
394 var->key = "pcsx_rearmed_pad2type";
395 break;
396 case 2:
397 var->key = "pcsx_rearmed_pad3type";
398 break;
399 case 3:
400 var->key = "pcsx_rearmed_pad4type";
401 break;
402 case 4:
403 var->key = "pcsx_rearmed_pad5type";
404 break;
405 case 5:
406 var->key = "pcsx_rearmed_pad6type";
407 break;
408 case 6:
409 var->key = "pcsx_rearmed_pad7type";
410 break;
411 case 7:
412 var->key = "pcsx_rearmed_pad8type";
413 break;
414 }
415
416 return environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, var) || var->value;
417}
418
419static void update_controller_port_variable(unsigned port)
420{
421 if (port >= PORTS_NUMBER)
422 return;
423
424 struct retro_variable var;
425
426 if (controller_port_variable(port, &var))
427 {
428 if (strcmp(var.value, "standard") == 0)
429 in_type[0] = PSE_PAD_TYPE_STANDARD;
430 else if (strcmp(var.value, "analog") == 0)
431 in_type[0] = PSE_PAD_TYPE_ANALOGPAD;
432 else if (strcmp(var.value, "negcon") == 0)
433 in_type[0] = PSE_PAD_TYPE_NEGCON;
434 else if (strcmp(var.value, "none") == 0)
435 in_type[0] = PSE_PAD_TYPE_NONE;
436 // else 'default' case, do nothing
437 }
438}
439
440static void update_controller_port_device(unsigned port, unsigned device)
441{
442 if (port >= PORTS_NUMBER)
443 return;
444
445 struct retro_variable var;
446
447 if (!controller_port_variable(port, &var))
448 return;
449
450 if (strcmp(var.value, "default") != 0)
451 return;
452
453 switch (device)
454 {
455 case RETRO_DEVICE_JOYPAD:
456 in_type[port] = PSE_PAD_TYPE_STANDARD;
457 break;
458 case RETRO_DEVICE_ANALOG:
459 in_type[port] = PSE_PAD_TYPE_ANALOGPAD;
460 break;
461 case RETRO_DEVICE_MOUSE:
462 in_type[port] = PSE_PAD_TYPE_MOUSE;
463 break;
464 case RETRO_DEVICE_LIGHTGUN:
465 in_type[port] = PSE_PAD_TYPE_GUN;
466 break;
467 case RETRO_DEVICE_NONE:
468 default:
469 in_type[port] = PSE_PAD_TYPE_NONE;
470 }
471}
472
473static void update_multitap()
474{
475 struct retro_variable var;
476 int auto_case;
477
478 var.value = NULL;
479 var.key = "pcsx_rearmed_multitap1";
480 auto_case = 0;
481 if (environ_cb && (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value))
482 {
483 if (strcmp(var.value, "enabled") == 0)
484 multitap1 = 1;
485 else if (strcmp(var.value, "disabled") == 0)
486 multitap1 = 0;
487 else // 'auto' case
488 auto_case = 1;
489 }
490 else
491 auto_case = 1;
492
493 if (auto_case)
494 {
495 // If a gamepad is plugged after port 2, we need a first multitap.
496 multitap1 = 0;
497 for (int port = 2; port < PORTS_NUMBER; port++)
498 multitap1 |= in_type[port] != PSE_PAD_TYPE_NONE;
499 }
500
501 var.value = NULL;
502 var.key = "pcsx_rearmed_multitap2";
503 auto_case = 0;
504 if (environ_cb && (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value))
505 {
506 if (strcmp(var.value, "enabled") == 0)
507 multitap2 = 1;
508 else if (strcmp(var.value, "disabled") == 0)
509 multitap2 = 0;
510 else // 'auto' case
511 auto_case = 1;
512 }
513 else
514 auto_case = 1;
515
516 if (auto_case)
517 {
518 // If a gamepad is plugged after port 4, we need a second multitap.
519 multitap2 = 0;
520 for (int port = 4; port < PORTS_NUMBER; port++)
521 multitap2 |= in_type[port] != PSE_PAD_TYPE_NONE;
522 }
523}
524
525void retro_set_controller_port_device(unsigned port, unsigned device)
526{
527 SysPrintf("port %u device %u",port,device);
528
529 if (port >= PORTS_NUMBER)
530 return;
531
532 update_controller_port_device(port, device);
533 update_multitap();
534}
535
536void retro_get_system_info(struct retro_system_info *info)
537{
538 memset(info, 0, sizeof(*info));
539 info->library_name = "PCSX-ReARMed";
540 info->library_version = "r22";
541 info->valid_extensions = "bin|cue|img|mdf|pbp|toc|cbn|m3u";
542 info->need_fullpath = true;
543}
544
545void retro_get_system_av_info(struct retro_system_av_info *info)
546{
547 memset(info, 0, sizeof(*info));
548 info->timing.fps = is_pal_mode ? 50 : 60;
549 info->timing.sample_rate = 44100;
550 info->geometry.base_width = 320;
551 info->geometry.base_height = 240;
552 info->geometry.max_width = VOUT_MAX_WIDTH;
553 info->geometry.max_height = VOUT_MAX_HEIGHT;
554 info->geometry.aspect_ratio = 4.0 / 3.0;
555}
556
557/* savestates */
558size_t retro_serialize_size(void)
559{
560 // it's currently 4380651-4397047 bytes,
561 // but have some reserved for future
562 return 0x440000;
563}
564
565struct save_fp {
566 char *buf;
567 size_t pos;
568 int is_write;
569};
570
571static void *save_open(const char *name, const char *mode)
572{
573 struct save_fp *fp;
574
575 if (name == NULL || mode == NULL)
576 return NULL;
577
578 fp = malloc(sizeof(*fp));
579 if (fp == NULL)
580 return NULL;
581
582 fp->buf = (char *)name;
583 fp->pos = 0;
584 fp->is_write = (mode[0] == 'w' || mode[1] == 'w');
585
586 return fp;
587}
588
589static int save_read(void *file, void *buf, u32 len)
590{
591 struct save_fp *fp = file;
592 if (fp == NULL || buf == NULL)
593 return -1;
594
595 memcpy(buf, fp->buf + fp->pos, len);
596 fp->pos += len;
597 return len;
598}
599
600static int save_write(void *file, const void *buf, u32 len)
601{
602 struct save_fp *fp = file;
603 if (fp == NULL || buf == NULL)
604 return -1;
605
606 memcpy(fp->buf + fp->pos, buf, len);
607 fp->pos += len;
608 return len;
609}
610
611static long save_seek(void *file, long offs, int whence)
612{
613 struct save_fp *fp = file;
614 if (fp == NULL)
615 return -1;
616
617 switch (whence) {
618 case SEEK_CUR:
619 fp->pos += offs;
620 return fp->pos;
621 case SEEK_SET:
622 fp->pos = offs;
623 return fp->pos;
624 default:
625 return -1;
626 }
627}
628
629static void save_close(void *file)
630{
631 struct save_fp *fp = file;
632 size_t r_size = retro_serialize_size();
633 if (fp == NULL)
634 return;
635
636 if (fp->pos > r_size)
637 SysPrintf("ERROR: save buffer overflow detected\n");
638 else if (fp->is_write && fp->pos < r_size)
639 // make sure we don't save trash in leftover space
640 memset(fp->buf + fp->pos, 0, r_size - fp->pos);
641 free(fp);
642}
643
644bool retro_serialize(void *data, size_t size)
645{
646 int ret = SaveState(data);
647 return ret == 0 ? true : false;
648}
649
650bool retro_unserialize(const void *data, size_t size)
651{
652 int ret = LoadState(data);
653 return ret == 0 ? true : false;
654}
655
656/* cheats */
657void retro_cheat_reset(void)
658{
659 ClearAllCheats();
660}
661
662void retro_cheat_set(unsigned index, bool enabled, const char *code)
663{
664 char buf[256];
665 int ret;
666
667 // cheat funcs are destructive, need a copy..
668 strncpy(buf, code, sizeof(buf));
669 buf[sizeof(buf) - 1] = 0;
670
671 if (index < NumCheats)
672 ret = EditCheat(index, "", buf);
673 else
674 ret = AddCheat("", buf);
675
676 if (ret != 0)
677 SysPrintf("Failed to set cheat %#u\n", index);
678 else if (index < NumCheats)
679 Cheats[index].Enabled = enabled;
680}
681
682/* multidisk support */
683static bool disk_ejected;
684static unsigned int disk_current_index;
685static unsigned int disk_count;
686static struct disks_state {
687 char *fname;
688 int internal_index; // for multidisk eboots
689} disks[8];
690
691static bool disk_set_eject_state(bool ejected)
692{
693 // weird PCSX API..
694 SetCdOpenCaseTime(ejected ? -1 : (time(NULL) + 2));
695 LidInterrupt();
696
697 disk_ejected = ejected;
698 return true;
699}
700
701static bool disk_get_eject_state(void)
702{
703 /* can't be controlled by emulated software */
704 return disk_ejected;
705}
706
707static unsigned int disk_get_image_index(void)
708{
709 return disk_current_index;
710}
711
712static bool disk_set_image_index(unsigned int index)
713{
714 if (index >= sizeof(disks) / sizeof(disks[0]))
715 return false;
716
717 CdromId[0] = '\0';
718 CdromLabel[0] = '\0';
719
720 if (disks[index].fname == NULL) {
721 SysPrintf("missing disk #%u\n", index);
722 CDR_shutdown();
723
724 // RetroArch specifies "no disk" with index == count,
725 // so don't fail here..
726 disk_current_index = index;
727 return true;
728 }
729
730 SysPrintf("switching to disk %u: \"%s\" #%d\n", index,
731 disks[index].fname, disks[index].internal_index);
732
733 cdrIsoMultidiskSelect = disks[index].internal_index;
734 set_cd_image(disks[index].fname);
735 if (ReloadCdromPlugin() < 0) {
736 SysPrintf("failed to load cdr plugin\n");
737 return false;
738 }
739 if (CDR_open() < 0) {
740 SysPrintf("failed to open cdr plugin\n");
741 return false;
742 }
743
744 if (!disk_ejected) {
745 SetCdOpenCaseTime(time(NULL) + 2);
746 LidInterrupt();
747 }
748
749 disk_current_index = index;
750 return true;
751}
752
753static unsigned int disk_get_num_images(void)
754{
755 return disk_count;
756}
757
758static bool disk_replace_image_index(unsigned index,
759 const struct retro_game_info *info)
760{
761 char *old_fname;
762 bool ret = true;
763
764 if (index >= sizeof(disks) / sizeof(disks[0]))
765 return false;
766
767 old_fname = disks[index].fname;
768 disks[index].fname = NULL;
769 disks[index].internal_index = 0;
770
771 if (info != NULL) {
772 disks[index].fname = strdup(info->path);
773 if (index == disk_current_index)
774 ret = disk_set_image_index(index);
775 }
776
777 if (old_fname != NULL)
778 free(old_fname);
779
780 return ret;
781}
782
783static bool disk_add_image_index(void)
784{
785 if (disk_count >= 8)
786 return false;
787
788 disk_count++;
789 return true;
790}
791
792static struct retro_disk_control_callback disk_control = {
793 .set_eject_state = disk_set_eject_state,
794 .get_eject_state = disk_get_eject_state,
795 .get_image_index = disk_get_image_index,
796 .set_image_index = disk_set_image_index,
797 .get_num_images = disk_get_num_images,
798 .replace_image_index = disk_replace_image_index,
799 .add_image_index = disk_add_image_index,
800};
801
802// just in case, maybe a win-rt port in the future?
803#ifdef _WIN32
804#define SLASH '\\'
805#else
806#define SLASH '/'
807#endif
808
809static char base_dir[PATH_MAX];
810
811static bool read_m3u(const char *file)
812{
813 char line[PATH_MAX];
814 char name[PATH_MAX];
815 FILE *f = fopen(file, "r");
816 if (!f)
817 return false;
818
819 while (fgets(line, sizeof(line), f) && disk_count < sizeof(disks) / sizeof(disks[0])) {
820 if (line[0] == '#')
821 continue;
822 char *carrige_return = strchr(line, '\r');
823 if (carrige_return)
824 *carrige_return = '\0';
825 char *newline = strchr(line, '\n');
826 if (newline)
827 *newline = '\0';
828
829 if (line[0] != '\0')
830 {
831 snprintf(name, sizeof(name), "%s%c%s", base_dir, SLASH, line);
832 disks[disk_count++].fname = strdup(name);
833 }
834 }
835
836 fclose(f);
837 return (disk_count != 0);
838}
839
840static void extract_directory(char *buf, const char *path, size_t size)
841{
842 char *base;
843 strncpy(buf, path, size - 1);
844 buf[size - 1] = '\0';
845
846 base = strrchr(buf, '/');
847 if (!base)
848 base = strrchr(buf, '\\');
849
850 if (base)
851 *base = '\0';
852 else
853 {
854 buf[0] = '.';
855 buf[1] = '\0';
856 }
857}
858
859#if defined(__QNX__) || defined(_WIN32)
860/* Blackberry QNX doesn't have strcasestr */
861
862/*
863 * Find the first occurrence of find in s, ignore case.
864 */
865char *
866strcasestr(const char *s, const char*find)
867{
868 char c, sc;
869 size_t len;
870
871 if ((c = *find++) != 0) {
872 c = tolower((unsigned char)c);
873 len = strlen(find);
874 do {
875 do {
876 if ((sc = *s++) == 0)
877 return (NULL);
878 } while ((char)tolower((unsigned char)sc) != c);
879 } while (strncasecmp(s, find, len) != 0);
880 s--;
881 }
882 return ((char *)s);
883}
884#endif
885
886bool retro_load_game(const struct retro_game_info *info)
887{
888 size_t i;
889 bool is_m3u = (strcasestr(info->path, ".m3u") != NULL);
890
891 struct retro_input_descriptor desc[] = {
892 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
893 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
894 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
895 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
896 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
897 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
898 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
899 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
900 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
901 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
902 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
903 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
904 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
905 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
906 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
907 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
908 { 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
909 { 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
910 { 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
911 { 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
912
913
914 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
915 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
916 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
917 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
918 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
919 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
920 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
921 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
922 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
923 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
924 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
925 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
926 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
927 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
928 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
929 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
930 { 1, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
931 { 1, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
932 { 1, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
933 { 1, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
934
935 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
936 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
937 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
938 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
939 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
940 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
941 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
942 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
943 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
944 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
945 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
946 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
947 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
948 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
949 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
950 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
951 { 2, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
952 { 2, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
953 { 2, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
954 { 2, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
955
956 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
957 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
958 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
959 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
960 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
961 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
962 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
963 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
964 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
965 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
966 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
967 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
968 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
969 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
970 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
971 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
972 { 3, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
973 { 3, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
974 { 3, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
975 { 3, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
976
977 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
978 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
979 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
980 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
981 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
982 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
983 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
984 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
985 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
986 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
987 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
988 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
989 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
990 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
991 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
992 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
993 { 4, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
994 { 4, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
995 { 4, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
996 { 4, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
997
998 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
999 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1000 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1001 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1002 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1003 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1004 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1005 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1006 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1007 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1008 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1009 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1010 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1011 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1012 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1013 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1014 { 5, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1015 { 5, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1016 { 5, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1017 { 5, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1018
1019 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1020 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1021 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1022 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1023 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1024 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1025 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1026 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1027 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1028 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1029 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1030 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1031 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1032 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1033 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1034 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1035 { 6, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1036 { 6, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1037 { 6, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1038 { 6, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1039
1040 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1041 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1042 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1043 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1044 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1045 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1046 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1047 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1048 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1049 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1050 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1051 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1052 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1053 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1054 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1055 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1056 { 7, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1057 { 7, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1058 { 7, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1059 { 7, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1060
1061 { 0 },
1062 };
1063
1064 environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
1065
1066#ifdef FRONTEND_SUPPORTS_RGB565
1067 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
1068 if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
1069 SysPrintf("RGB565 supported, using it\n");
1070 }
1071#endif
1072
1073 if (info == NULL || info->path == NULL) {
1074 SysPrintf("info->path required\n");
1075 return false;
1076 }
1077
1078 if (plugins_opened) {
1079 ClosePlugins();
1080 plugins_opened = 0;
1081 }
1082
1083 for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++) {
1084 if (disks[i].fname != NULL) {
1085 free(disks[i].fname);
1086 disks[i].fname = NULL;
1087 }
1088 disks[i].internal_index = 0;
1089 }
1090
1091 disk_current_index = 0;
1092 extract_directory(base_dir, info->path, sizeof(base_dir));
1093
1094 if (is_m3u) {
1095 if (!read_m3u(info->path)) {
1096 SysPrintf("failed to read m3u file\n");
1097 return false;
1098 }
1099 } else {
1100 disk_count = 1;
1101 disks[0].fname = strdup(info->path);
1102 }
1103
1104 set_cd_image(disks[0].fname);
1105
1106 /* have to reload after set_cd_image for correct cdr plugin */
1107 if (LoadPlugins() == -1) {
1108 SysPrintf("failed to load plugins\n");
1109 return false;
1110 }
1111
1112 plugins_opened = 1;
1113 NetOpened = 0;
1114
1115 if (OpenPlugins() == -1) {
1116 SysPrintf("failed to open plugins\n");
1117 return false;
1118 }
1119
1120 plugin_call_rearmed_cbs();
1121 dfinput_activate();
1122
1123 Config.PsxAuto = 1;
1124 if (CheckCdrom() == -1) {
1125 SysPrintf("unsupported/invalid CD image: %s\n", info->path);
1126 return false;
1127 }
1128
1129 SysReset();
1130
1131 if (LoadCdrom() == -1) {
1132 SysPrintf("could not load CD-ROM!\n");
1133 return false;
1134 }
1135 emu_on_new_cd(0);
1136
1137 // multidisk images
1138 if (!is_m3u) {
1139 disk_count = cdrIsoMultidiskCount < 8 ? cdrIsoMultidiskCount : 8;
1140 for (i = 1; i < sizeof(disks) / sizeof(disks[0]) && i < cdrIsoMultidiskCount; i++) {
1141 disks[i].fname = strdup(info->path);
1142 disks[i].internal_index = i;
1143 }
1144 }
1145
1146 return true;
1147}
1148
1149bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
1150{
1151 return false;
1152}
1153
1154void retro_unload_game(void)
1155{
1156}
1157
1158unsigned retro_get_region(void)
1159{
1160 return is_pal_mode ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
1161}
1162
1163void *retro_get_memory_data(unsigned id)
1164{
1165 if (id == RETRO_MEMORY_SAVE_RAM)
1166 return Mcd1Data;
1167 else
1168 return NULL;
1169}
1170
1171size_t retro_get_memory_size(unsigned id)
1172{
1173 if (id == RETRO_MEMORY_SAVE_RAM)
1174 return MCD_SIZE;
1175 else
1176 return 0;
1177}
1178
1179void retro_reset(void)
1180{
1181 SysReset();
1182}
1183
1184static const unsigned short retro_psx_map[] = {
1185 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
1186 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
1187 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
1188 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
1189 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
1190 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
1191 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
1192 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
1193 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
1194 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
1195 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
1196 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
1197 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
1198 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
1199 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
1200 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
1201};
1202#define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
1203
1204static void update_variables(bool in_flight)
1205{
1206 struct retro_variable var;
1207
1208 var.value = NULL;
1209 var.key = "pcsx_rearmed_frameskip";
1210 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1211 pl_rearmed_cbs.frameskip = atoi(var.value);
1212
1213 var.value = NULL;
1214 var.key = "pcsx_rearmed_region";
1215 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1216 {
1217 Config.PsxAuto = 0;
1218 if (strcmp(var.value, "Automatic") == 0)
1219 Config.PsxAuto = 1;
1220 else if (strcmp(var.value, "NTSC") == 0)
1221 Config.PsxType = 0;
1222 else if (strcmp(var.value, "PAL") == 0)
1223 Config.PsxType = 1;
1224 }
1225
1226 for (int i = 0; i < PORTS_NUMBER; i++)
1227 update_controller_port_variable(i);
1228
1229 update_multitap();
1230
1231#ifdef __ARM_NEON__
1232 var.value = "NULL";
1233 var.key = "pcsx_rearmed_neon_interlace_enable";
1234
1235 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1236 {
1237 if (strcmp(var.value, "disabled") == 0)
1238 pl_rearmed_cbs.gpu_neon.allow_interlace = 0;
1239 else if (strcmp(var.value, "enabled") == 0)
1240 pl_rearmed_cbs.gpu_neon.allow_interlace = 1;
1241 }
1242
1243 var.value = NULL;
1244 var.key = "pcsx_rearmed_neon_enhancement_enable";
1245
1246 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1247 {
1248 if (strcmp(var.value, "disabled") == 0)
1249 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
1250 else if (strcmp(var.value, "enabled") == 0)
1251 pl_rearmed_cbs.gpu_neon.enhancement_enable = 1;
1252 }
1253
1254 var.value = NULL;
1255 var.key = "pcsx_rearmed_neon_enhancement_no_main";
1256
1257 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1258 {
1259 if (strcmp(var.value, "disabled") == 0)
1260 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
1261 else if (strcmp(var.value, "enabled") == 0)
1262 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 1;
1263 }
1264#endif
1265
1266 var.value = "NULL";
1267 var.key = "pcsx_rearmed_duping_enable";
1268
1269 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1270 {
1271 if (strcmp(var.value, "off") == 0)
1272 duping_enable = false;
1273 else if (strcmp(var.value, "on") == 0)
1274 duping_enable = true;
1275 }
1276
1277#ifndef DRC_DISABLE
1278 var.value = NULL;
1279 var.key = "pcsx_rearmed_drc";
1280
1281 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1282 {
1283 R3000Acpu *prev_cpu = psxCpu;
1284
1285#ifdef _3DS
1286 if(!__ctr_svchax)
1287 Config.Cpu = CPU_INTERPRETER;
1288 else
1289#endif
1290 if (strcmp(var.value, "disabled") == 0)
1291 Config.Cpu = CPU_INTERPRETER;
1292 else if (strcmp(var.value, "enabled") == 0)
1293 Config.Cpu = CPU_DYNAREC;
1294
1295 psxCpu = (Config.Cpu == CPU_INTERPRETER) ? &psxInt : &psxRec;
1296 if (psxCpu != prev_cpu) {
1297 prev_cpu->Shutdown();
1298 psxCpu->Init();
1299 psxCpu->Reset(); // not really a reset..
1300 }
1301 }
1302#endif
1303
1304 var.value = "NULL";
1305 var.key = "pcsx_rearmed_spu_reverb";
1306
1307 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1308 {
1309 if (strcmp(var.value, "off") == 0)
1310 spu_config.iUseReverb = false;
1311 else if (strcmp(var.value, "on") == 0)
1312 spu_config.iUseReverb = true;
1313 }
1314
1315 var.value = "NULL";
1316 var.key = "pcsx_rearmed_spu_interpolation";
1317
1318 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1319 {
1320 if (strcmp(var.value, "simple") == 0)
1321 spu_config.iUseInterpolation = 1;
1322 else if (strcmp(var.value, "gaussian") == 0)
1323 spu_config.iUseInterpolation = 2;
1324 else if (strcmp(var.value, "cubic") == 0)
1325 spu_config.iUseInterpolation = 3;
1326 else if (strcmp(var.value, "off") == 0)
1327 spu_config.iUseInterpolation = 0;
1328 }
1329
1330 var.value = "NULL";
1331 var.key = "pcsx_rearmed_pe2_fix";
1332
1333 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1334 {
1335 if (strcmp(var.value, "disabled") == 0)
1336 Config.RCntFix = 0;
1337 else if (strcmp(var.value, "enabled") == 0)
1338 Config.RCntFix = 1;
1339 }
1340
1341 var.value = "NULL";
1342 var.key = "pcsx_rearmed_inuyasha_fix";
1343
1344 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1345 {
1346 if (strcmp(var.value, "disabled") == 0)
1347 Config.VSyncWA = 0;
1348 else if (strcmp(var.value, "enabled") == 0)
1349 Config.VSyncWA = 1;
1350 }
1351
1352 if (in_flight) {
1353 // inform core things about possible config changes
1354 plugin_call_rearmed_cbs();
1355
1356 if (GPU_open != NULL && GPU_close != NULL) {
1357 GPU_close();
1358 GPU_open(&gpuDisp, "PCSX", NULL);
1359 }
1360
1361 dfinput_activate();
1362 }
1363}
1364
1365void retro_run(void)
1366{
1367 int i;
1368
1369 input_poll_cb();
1370
1371 bool updated = false;
1372 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
1373 update_variables(true);
1374
1375 // reset all keystate, query libretro for keystate
1376 int j;
1377 for(i = 0; i < PORTS_NUMBER; i++) {
1378 in_keystate[i] = 0;
1379
1380 if (in_type[i] == PSE_PAD_TYPE_NONE)
1381 continue;
1382
1383 // query libretro for keystate
1384 for (j = 0; j < RETRO_PSX_MAP_LEN; j++)
1385 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, j))
1386 in_keystate[i] |= retro_psx_map[j];
1387
1388 if (in_type[i] == PSE_PAD_TYPE_ANALOGPAD)
1389 {
1390 in_analog_left[i][0] = (input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X) / 256) + 128;
1391 in_analog_left[i][1] = (input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y) / 256) + 128;
1392 in_analog_right[i][0] = (input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X) / 256) + 128;
1393 in_analog_right[i][1] = (input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y) / 256) + 128;
1394 }
1395 }
1396
1397 stop = 0;
1398 psxCpu->Execute();
1399
1400 video_cb((vout_fb_dirty || !vout_can_dupe || !duping_enable) ? vout_buf : NULL,
1401 vout_width, vout_height, vout_width * 2);
1402 vout_fb_dirty = 0;
1403}
1404
1405static bool try_use_bios(const char *path)
1406{
1407 FILE *f;
1408 long size;
1409 const char *name;
1410
1411 f = fopen(path, "rb");
1412 if (f == NULL)
1413 return false;
1414
1415 fseek(f, 0, SEEK_END);
1416 size = ftell(f);
1417 fclose(f);
1418
1419 if (size != 512 * 1024)
1420 return false;
1421
1422 name = strrchr(path, SLASH);
1423 if (name++ == NULL)
1424 name = path;
1425 snprintf(Config.Bios, sizeof(Config.Bios), "%s", name);
1426 return true;
1427}
1428
1429#ifndef VITA
1430#include <sys/types.h>
1431#include <dirent.h>
1432
1433static bool find_any_bios(const char *dirpath, char *path, size_t path_size)
1434{
1435 DIR *dir;
1436 struct dirent *ent;
1437 bool ret = false;
1438
1439 dir = opendir(dirpath);
1440 if (dir == NULL)
1441 return false;
1442
1443 while ((ent = readdir(dir))) {
1444 if (strncasecmp(ent->d_name, "scph", 4) != 0)
1445 continue;
1446
1447 snprintf(path, path_size, "%s/%s", dirpath, ent->d_name);
1448 ret = try_use_bios(path);
1449 if (ret)
1450 break;
1451 }
1452 closedir(dir);
1453 return ret;
1454}
1455#else
1456#define find_any_bios(...) false
1457#endif
1458
1459static void check_system_specs(void)
1460{
1461 unsigned level = 6;
1462 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
1463}
1464
1465void retro_init(void)
1466{
1467 const char *bios[] = { "scph1001", "scph5501", "scph7001" };
1468 const char *dir;
1469 char path[256];
1470 int i, ret;
1471 bool found_bios = false;
1472
1473#ifdef _3DS
1474 psxMapHook = pl_3ds_mmap;
1475 psxUnmapHook = pl_3ds_munmap;
1476#endif
1477 ret = emu_core_preinit();
1478#ifdef _3DS
1479 /* emu_core_preinit sets the cpu to dynarec */
1480 if(!__ctr_svchax)
1481 Config.Cpu = CPU_INTERPRETER;
1482#endif
1483 ret |= emu_core_init();
1484 if (ret != 0) {
1485 SysPrintf("PCSX init failed.\n");
1486 exit(1);
1487 }
1488
1489#ifdef _3DS
1490 vout_buf = linearMemAlign(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2, 0x80);
1491#elif defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && !defined(VITA)
1492 posix_memalign(&vout_buf, 16, VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
1493#else
1494 vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
1495#endif
1496
1497 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
1498 {
1499 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s", dir);
1500
1501 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) {
1502 snprintf(path, sizeof(path), "%s/%s.bin", dir, bios[i]);
1503 found_bios = try_use_bios(path);
1504 if (found_bios)
1505 break;
1506 }
1507
1508 if (!found_bios)
1509 found_bios = find_any_bios(dir, path, sizeof(path));
1510 }
1511 if (found_bios) {
1512 SysPrintf("found BIOS file: %s\n", Config.Bios);
1513 }
1514 else
1515 {
1516 SysPrintf("no BIOS files found.\n");
1517 struct retro_message msg =
1518 {
1519 "no BIOS found, expect bugs!",
1520 180
1521 };
1522 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, (void*)&msg);
1523 }
1524
1525 environ_cb(RETRO_ENVIRONMENT_GET_CAN_DUPE, &vout_can_dupe);
1526 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
1527 environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble);
1528
1529 /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times)
1530 * we have to do this because cache misses and some IO penalties
1531 * are not emulated. Warning: changing this may break compatibility. */
1532 cycle_multiplier = 175;
1533#ifdef HAVE_PRE_ARMV7
1534 cycle_multiplier = 200;
1535#endif
1536 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
1537 spu_config.iUseFixedUpdates = 1;
1538
1539 McdDisable[0] = 0;
1540 McdDisable[1] = 1;
1541 init_memcard(Mcd1Data);
1542
1543 SaveFuncs.open = save_open;
1544 SaveFuncs.read = save_read;
1545 SaveFuncs.write = save_write;
1546 SaveFuncs.seek = save_seek;
1547 SaveFuncs.close = save_close;
1548
1549 update_variables(false);
1550 check_system_specs();
1551}
1552
1553void retro_deinit(void)
1554{
1555 SysClose();
1556#ifdef _3DS
1557 linearFree(vout_buf);
1558#else
1559 free(vout_buf);
1560#endif
1561 vout_buf = NULL;
1562}
1563
1564#ifdef VITA
1565#include <psp2/kernel/threadmgr.h>
1566int usleep (unsigned long us)
1567{
1568 sceKernelDelayThread(us);
1569}
1570#endif