From 0d8341f258c58913dba9aa98aac2e6e125edf8b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Andr=C3=A9=20Santoni?= Date: Sun, 3 Oct 2021 13:53:22 +0700 Subject: [PATCH] Improve keyboard support and add input device configuration --- README.md | 19 +++++++++++++++++++ config.c | 13 +++++++++++++ config.h | 4 ++++ core.c | 10 ++++++---- input.c | 5 +++++ 5 files changed, 47 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 252e4ab..2751643 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,25 @@ ROMBundler will read the ini file, load the emulator and the ROM and the game wi You can then rename `rombundler` with the name of your game, change it's icon, and distribute this as a zip. +## Inputs configuration + +You can choose what kind of device is plugged in the console ports in the config.ini: + + port0 = 3 + port1 = 1 + +The identifiers can be found in the libretro.h: + +``` +#define RETRO_DEVICE_NONE 0 +#define RETRO_DEVICE_JOYPAD 1 +#define RETRO_DEVICE_MOUSE 2 +#define RETRO_DEVICE_KEYBOARD 3 +#define RETRO_DEVICE_LIGHTGUN 4 +#define RETRO_DEVICE_ANALOG 5 +#define RETRO_DEVICE_POINTER 6 +``` + ## Emulator configuration If you need special options for the emulator, you can set them by creating a options.ini file like this: diff --git a/config.c b/config.c index e2ac879..a04f071 100644 --- a/config.c +++ b/config.c @@ -3,6 +3,7 @@ #include #include "config.h" #include "ini.h" +#include "libretro.h" void cfg_defaults(config *c) { @@ -16,6 +17,10 @@ void cfg_defaults(config *c) c->swap_interval = 1; c->hide_cursor = false; c->map_analog_to_dpad = true; + c->port0 = RETRO_DEVICE_NONE; + c->port1 = RETRO_DEVICE_NONE; + c->port2 = RETRO_DEVICE_NONE; + c->port3 = RETRO_DEVICE_NONE; } int cfg_handler(void* user, const char* section, const char* name, const char* value) @@ -47,6 +52,14 @@ int cfg_handler(void* user, const char* section, const char* name, const char* v c->hide_cursor = strcmp(value, "true") == 0; else if (MATCH("", "map_analog_to_dpad")) c->map_analog_to_dpad = strcmp(value, "true") == 0; + else if (MATCH("", "port0")) + c->port0 = atoi(value); + else if (MATCH("", "port1")) + c->port1 = atoi(value); + else if (MATCH("", "port2")) + c->port2 = atoi(value); + else if (MATCH("", "port3")) + c->port3 = atoi(value); else return 0; return 1; diff --git a/config.h b/config.h index 6501ed3..59096ce 100644 --- a/config.h +++ b/config.h @@ -14,6 +14,10 @@ typedef struct float aspect_ratio; bool hide_cursor; bool map_analog_to_dpad; + unsigned port0; + unsigned port1; + unsigned port2; + unsigned port3; } config; void cfg_defaults(config* c); diff --git a/core.c b/core.c index 64e10bc..f01479d 100644 --- a/core.c +++ b/core.c @@ -18,6 +18,7 @@ #include "audio.h" #include "input.h" #include "options.h" +#include "config.h" #if defined(_WIN32) #define load_lib(L) LoadLibrary(L); @@ -53,6 +54,7 @@ static struct { static struct retro_frame_time_callback runloop_frame_time; static retro_usec_t runloop_frame_time_last = 0; +extern config g_cfg; static void core_log(enum retro_log_level level, const char *fmt, ...) { @@ -258,10 +260,10 @@ void core_load_game(const char *filename) video_configure(&av.geometry); audio_init(av.timing.sample_rate); - core.retro_set_controller_port_device(0, RETRO_DEVICE_JOYPAD); - core.retro_set_controller_port_device(1, RETRO_DEVICE_JOYPAD); - core.retro_set_controller_port_device(2, RETRO_DEVICE_JOYPAD); - core.retro_set_controller_port_device(3, RETRO_DEVICE_JOYPAD); + if (g_cfg.port0) core.retro_set_controller_port_device(0, g_cfg.port0); + if (g_cfg.port1) core.retro_set_controller_port_device(1, g_cfg.port1); + if (g_cfg.port2) core.retro_set_controller_port_device(2, g_cfg.port2); + if (g_cfg.port3) core.retro_set_controller_port_device(3, g_cfg.port3); return; } diff --git a/input.c b/input.c index a34fcf6..b74ec15 100644 --- a/input.c +++ b/input.c @@ -280,5 +280,10 @@ int16_t input_state(unsigned port, unsigned device, unsigned index, unsigned id) return 1; } + if (device == RETRO_DEVICE_KEYBOARD) + for (int i = 0; kbd_binds[i].k || kbd_binds[i].rk; ++i) + if (id == kbd_binds[i].rk && window && glfwGetKey(window, kbd_binds[i].k) == GLFW_PRESS) + return 1; + return 0; }