From a9a67fdaa5b7794b21d55b1d1db7665d5ebc46c1 Mon Sep 17 00:00:00 2001 From: "Christoph \"baka0815\" Schwerdtfeger" Date: Fri, 31 Aug 2018 16:47:55 +0200 Subject: [PATCH 1/3] Move the evdev specific code out of main.cpp --- core/linux-dist/evdev.cpp | 126 +++++++++++++++++++++++++++++++++++++- core/linux-dist/evdev.h | 6 +- core/linux-dist/main.cpp | 115 ++-------------------------------- 3 files changed, 132 insertions(+), 115 deletions(-) mode change 100755 => 100644 core/linux-dist/main.cpp diff --git a/core/linux-dist/evdev.cpp b/core/linux-dist/evdev.cpp index ae7bd54fc..25ff4c315 100644 --- a/core/linux-dist/evdev.cpp +++ b/core/linux-dist/evdev.cpp @@ -3,6 +3,9 @@ #include #include "linux-dist/evdev.h" #include "linux-dist/main.h" +#include "hw/maple/maple_devs.h" +#include "hw/maple/maple_cfg.h" +#include "cfg/cfg.h" #include "cfg/ini.h" #include #include @@ -16,6 +19,14 @@ libevdev_func1_t libevdev_event_code_from_name; libevdev_func2_t libevdev_event_code_get_name; + /* evdev input */ + static EvdevController evdev_controllers[4] = { + { -1, NULL }, + { -1, NULL }, + { -1, NULL }, + { -1, NULL } + }; + void dc_stop(void); void load_libevdev() @@ -107,6 +118,102 @@ this->rumble_effect_id = -1; } + MapleDeviceType GetMapleDeviceType(int value, int port) + { + switch (value) + { + case 0: + #if defined(_DEBUG) || defined(DEBUG) + printf("Maple Device: None\n"); + #endif + return MDT_None; + case 1: + #if defined(_DEBUG) || defined(DEBUG) + printf("Maple Device: VMU\n"); + #endif + return MDT_SegaVMU; + case 2: + #if defined(_DEBUG) || defined(DEBUG) + printf("Maple Device: Microphone\n"); + #endif + return MDT_Microphone; + case 3: + #if defined(_DEBUG) || defined(DEBUG) + printf("Maple Device: PuruPuruPack\n"); + #endif + return MDT_PurupuruPack; + default: + MapleDeviceType result = MDT_None; + string result_type = "None"; + + // Controller in port 0 (player1) defaults to VMU for Maple device, all other to None + if (port == 0) + { + result_type = "VMU"; + result = MDT_SegaVMU; + } + + printf("Unsupported configuration (%d) for Maple Device, using %s\n", value, result_type.c_str()); + return result; + } + } + + void input_evdev_init() + { + int evdev_device_id[4] = { -1, -1, -1, -1 }; + size_t size_needed; + int port, i; + + char* evdev_device; + + for (port = 0; port < 4; port++) + { + size_needed = snprintf(NULL, 0, EVDEV_DEVICE_CONFIG_KEY, port+1) + 1; + char* evdev_config_key = (char*)malloc(size_needed); + sprintf(evdev_config_key, EVDEV_DEVICE_CONFIG_KEY, port+1); + evdev_device_id[port] = cfgLoadInt("input", evdev_config_key, EVDEV_DEFAULT_DEVICE_ID(port+1)); + free(evdev_config_key); + + // Check if the same device is already in use on another port + if (evdev_device_id[port] < 0) + { + printf("evdev: Controller %d disabled by config.\n", port + 1); + } + else + { + size_needed = snprintf(NULL, 0, EVDEV_DEVICE_STRING, evdev_device_id[port]) + 1; + evdev_device = (char*)malloc(size_needed); + sprintf(evdev_device, EVDEV_DEVICE_STRING, evdev_device_id[port]); + + size_needed = snprintf(NULL, 0, EVDEV_MAPPING_CONFIG_KEY, port+1) + 1; + evdev_config_key = (char*)malloc(size_needed); + sprintf(evdev_config_key, EVDEV_MAPPING_CONFIG_KEY, port+1); + + string tmp; + const char* mapping = (cfgExists("input", evdev_config_key) == 2 ? (tmp = cfgLoadStr("input", evdev_config_key, "")).c_str() : NULL); + free(evdev_config_key); + + input_evdev_init(&evdev_controllers[port], evdev_device, mapping); + + free(evdev_device); + + for (i = 0; i < port; i++) + { + if (evdev_device_id[port] == evdev_device_id[i]) + { + // Multiple controllers with the same device, check for multiple button assignments + if (input_evdev_button_duplicate_button(evdev_controllers[i].mapping, evdev_controllers[port].mapping)) + { + printf("WARNING: One or more button(s) of this device is also used in the configuration of input device %d (mapping: %s)\n", i, evdev_controllers[i].mapping->name.c_str()); + } + } + } + + mcfg_CreateController(port, GetMapleDeviceType(evdev_controllers[port].mapping->Maple_Device1, port), GetMapleDeviceType(evdev_controllers[port].mapping->Maple_Device2, port)); + } + } + } + std::map loaded_mappings; int load_keycode(ConfigFile* cfg, string section, string dc_key) @@ -383,8 +490,21 @@ } } - bool input_evdev_handle(EvdevController* controller, u32 port) + void input_evdev_close() { + for (int port = 0; port < 4 ; port++) + { + if (evdev_controllers[port].fd >= 0) + { + close(evdev_controllers[port].fd); + } + } + } + + bool input_evdev_handle(u32 port) + { + EvdevController* controller = &evdev_controllers[port]; + #define SET_FLAG(field, mask, expr) field =((expr) ? (field & ~mask) : (field | mask)) if (controller->fd < 0 || controller->mapping == NULL) { @@ -533,8 +653,10 @@ return true; } - void input_evdev_rumble(EvdevController* controller, u16 pow_strong, u16 pow_weak) + void input_evdev_rumble(u32 port, u16 pow_strong, u16 pow_weak) { + EvdevController* controller = &evdev_controllers[port]; + if (controller->fd < 0 || controller->rumble_effect_id == -2) { // Either the controller is not used or previous rumble effect failed diff --git a/core/linux-dist/evdev.h b/core/linux-dist/evdev.h index 464f13a00..348fbc4e8 100644 --- a/core/linux-dist/evdev.h +++ b/core/linux-dist/evdev.h @@ -73,7 +73,9 @@ struct EvdevController #define EVDEV_DEFAULT_DEVICE_ID(port) (port == 1 ? EVDEV_DEFAULT_DEVICE_ID_1 : -1) +extern void input_evdev_init(); +extern void input_evdev_close(); extern int input_evdev_init(EvdevController* controller, const char* device, const char* mapping_fname); -extern bool input_evdev_handle(EvdevController* controller, u32 port); -extern void input_evdev_rumble(EvdevController* controller, u16 pow_strong, u16 pow_weak); +extern bool input_evdev_handle(u32 port); +extern void input_evdev_rumble(u32 port, u16 pow_strong, u16 pow_weak); extern bool input_evdev_button_duplicate_button(EvdevControllerMapping* mapping1, EvdevControllerMapping* mapping2); diff --git a/core/linux-dist/main.cpp b/core/linux-dist/main.cpp old mode 100755 new mode 100644 index b25001411..e79928d7c --- a/core/linux-dist/main.cpp +++ b/core/linux-dist/main.cpp @@ -91,116 +91,15 @@ s8 joyx[4], joyy[4]; void emit_WriteCodeCache(); -#if defined(USE_EVDEV) - /* evdev input */ - static EvdevController evdev_controllers[4] = { - { -1, NULL }, - { -1, NULL }, - { -1, NULL }, - { -1, NULL } - }; -#endif - #if defined(USE_JOYSTICK) /* legacy joystick input */ static int joystick_fd = -1; // Joystick file descriptor #endif -MapleDeviceType GetMapleDeviceType(int value, int port) -{ - switch (value) - { - case 0: - #if defined(_DEBUG) || defined(DEBUG) - printf("Maple Device: None\n"); - #endif - return MDT_None; - case 1: - #if defined(_DEBUG) || defined(DEBUG) - printf("Maple Device: VMU\n"); - #endif - return MDT_SegaVMU; - case 2: - #if defined(_DEBUG) || defined(DEBUG) - printf("Maple Device: Microphone\n"); - #endif - return MDT_Microphone; - case 3: - #if defined(_DEBUG) || defined(DEBUG) - printf("Maple Device: PuruPuruPack\n"); - #endif - return MDT_PurupuruPack; - default: - MapleDeviceType result = MDT_None; - string result_type = "None"; - - // Controller in port 0 (player1) defaults to VMU for Maple device, all other to None - if (port == 0) - { - result_type = "VMU"; - result = MDT_SegaVMU; - } - - printf("Unsupported configuration (%d) for Maple Device, using %s\n", value, result_type.c_str()); - return result; - } -} - void os_SetupInput() { #if defined(USE_EVDEV) - int evdev_device_id[4] = { -1, -1, -1, -1 }; - size_t size_needed; - int port, i; - - char* evdev_device; - - for (port = 0; port < 4; port++) - { - size_needed = snprintf(NULL, 0, EVDEV_DEVICE_CONFIG_KEY, port+1) + 1; - char* evdev_config_key = (char*)malloc(size_needed); - sprintf(evdev_config_key, EVDEV_DEVICE_CONFIG_KEY, port+1); - evdev_device_id[port] = cfgLoadInt("input", evdev_config_key, EVDEV_DEFAULT_DEVICE_ID(port+1)); - free(evdev_config_key); - - // Check if the same device is already in use on another port - if (evdev_device_id[port] < 0) - { - printf("evdev: Controller %d disabled by config.\n", port + 1); - } - else - { - size_needed = snprintf(NULL, 0, EVDEV_DEVICE_STRING, evdev_device_id[port]) + 1; - evdev_device = (char*)malloc(size_needed); - sprintf(evdev_device, EVDEV_DEVICE_STRING, evdev_device_id[port]); - - size_needed = snprintf(NULL, 0, EVDEV_MAPPING_CONFIG_KEY, port+1) + 1; - evdev_config_key = (char*)malloc(size_needed); - sprintf(evdev_config_key, EVDEV_MAPPING_CONFIG_KEY, port+1); - - string tmp; - const char* mapping = (cfgExists("input", evdev_config_key) == 2 ? (tmp = cfgLoadStr("input", evdev_config_key, "")).c_str() : NULL); - free(evdev_config_key); - - input_evdev_init(&evdev_controllers[port], evdev_device, mapping); - - free(evdev_device); - - for (i = 0; i < port; i++) - { - if (evdev_device_id[port] == evdev_device_id[i]) - { - // Multiple controllers with the same device, check for multiple button assignments - if (input_evdev_button_duplicate_button(evdev_controllers[i].mapping, evdev_controllers[port].mapping)) - { - printf("WARNING: One or more button(s) of this device is also used in the configuration of input device %d (mapping: %s)\n", i, evdev_controllers[i].mapping->name.c_str()); - } - } - } - - mcfg_CreateController(port, GetMapleDeviceType(evdev_controllers[port].mapping->Maple_Device1, port), GetMapleDeviceType(evdev_controllers[port].mapping->Maple_Device2, port)); - } - } + input_evdev_init(); #endif #if defined(USE_JOYSTICK) @@ -238,7 +137,7 @@ void UpdateInputState(u32 port) #endif #if defined(USE_EVDEV) - input_evdev_handle(&evdev_controllers[port], port); + input_evdev_handle(port); #endif #if defined(USE_SDL) @@ -263,7 +162,7 @@ void UpdateVibration(u32 port, u32 value) u16 pow_strong = (u16)(65535 * pow_l); u16 pow_weak = (u16)(65535 * pow_r); - input_evdev_rumble(&evdev_controllers[port], pow_strong, pow_weak); + input_evdev_rumble(port, pow_strong, pow_weak); #endif } @@ -531,13 +430,7 @@ int main(int argc, wchar* argv[]) dc_term(); #if defined(USE_EVDEV) - for (int port = 0; port < 4 ; port++) - { - if(evdev_controllers[port].fd >= 0) - { - close(evdev_controllers[port].fd); - } - } + input_evdev_close(); #endif #if defined(SUPPORT_X11) From 8cbc342b06ae5b592e833ce1f2e132cc7ba08fcc Mon Sep 17 00:00:00 2001 From: "Christoph \"baka0815\" Schwerdtfeger" Date: Fri, 31 Aug 2018 16:52:08 +0200 Subject: [PATCH 2/3] Maple specific header no longer necessary --- core/linux-dist/main.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/core/linux-dist/main.cpp b/core/linux-dist/main.cpp index e79928d7c..698f7d2fd 100644 --- a/core/linux-dist/main.cpp +++ b/core/linux-dist/main.cpp @@ -37,11 +37,8 @@ #if defined(USE_EVDEV) #include "linux-dist/evdev.h" - #include "hw/maple/maple_devs.h" #endif -#include "hw/maple/maple_cfg.h" - #if defined(USE_JOYSTICK) #include "linux-dist/joystick.h" #endif From 555e1402169361c9c4ba4db467bda556098c12e3 Mon Sep 17 00:00:00 2001 From: "Christoph \"baka0815\" Schwerdtfeger" Date: Fri, 31 Aug 2018 16:59:37 +0200 Subject: [PATCH 3/3] evdev: Remove unnecessary exports in header ``input_evdev_init(EvdevController* controller, const char* device, const char* mapping_fname)`` and ``input_evdev_button_duplicate_button(EvdevControllerMapping* mapping1, EvdevControllerMapping* mapping2)`` are no longer used outside evdev.cpp Needed to move ``input_evdev_init()`` around a bit. --- core/linux-dist/evdev.cpp | 112 +++++++++++++++++++------------------- core/linux-dist/evdev.h | 2 - 2 files changed, 56 insertions(+), 58 deletions(-) diff --git a/core/linux-dist/evdev.cpp b/core/linux-dist/evdev.cpp index 25ff4c315..6eb60624e 100644 --- a/core/linux-dist/evdev.cpp +++ b/core/linux-dist/evdev.cpp @@ -158,62 +158,6 @@ } } - void input_evdev_init() - { - int evdev_device_id[4] = { -1, -1, -1, -1 }; - size_t size_needed; - int port, i; - - char* evdev_device; - - for (port = 0; port < 4; port++) - { - size_needed = snprintf(NULL, 0, EVDEV_DEVICE_CONFIG_KEY, port+1) + 1; - char* evdev_config_key = (char*)malloc(size_needed); - sprintf(evdev_config_key, EVDEV_DEVICE_CONFIG_KEY, port+1); - evdev_device_id[port] = cfgLoadInt("input", evdev_config_key, EVDEV_DEFAULT_DEVICE_ID(port+1)); - free(evdev_config_key); - - // Check if the same device is already in use on another port - if (evdev_device_id[port] < 0) - { - printf("evdev: Controller %d disabled by config.\n", port + 1); - } - else - { - size_needed = snprintf(NULL, 0, EVDEV_DEVICE_STRING, evdev_device_id[port]) + 1; - evdev_device = (char*)malloc(size_needed); - sprintf(evdev_device, EVDEV_DEVICE_STRING, evdev_device_id[port]); - - size_needed = snprintf(NULL, 0, EVDEV_MAPPING_CONFIG_KEY, port+1) + 1; - evdev_config_key = (char*)malloc(size_needed); - sprintf(evdev_config_key, EVDEV_MAPPING_CONFIG_KEY, port+1); - - string tmp; - const char* mapping = (cfgExists("input", evdev_config_key) == 2 ? (tmp = cfgLoadStr("input", evdev_config_key, "")).c_str() : NULL); - free(evdev_config_key); - - input_evdev_init(&evdev_controllers[port], evdev_device, mapping); - - free(evdev_device); - - for (i = 0; i < port; i++) - { - if (evdev_device_id[port] == evdev_device_id[i]) - { - // Multiple controllers with the same device, check for multiple button assignments - if (input_evdev_button_duplicate_button(evdev_controllers[i].mapping, evdev_controllers[port].mapping)) - { - printf("WARNING: One or more button(s) of this device is also used in the configuration of input device %d (mapping: %s)\n", i, evdev_controllers[i].mapping->name.c_str()); - } - } - } - - mcfg_CreateController(port, GetMapleDeviceType(evdev_controllers[port].mapping->Maple_Device1, port), GetMapleDeviceType(evdev_controllers[port].mapping->Maple_Device2, port)); - } - } - } - std::map loaded_mappings; int load_keycode(ConfigFile* cfg, string section, string dc_key) @@ -490,6 +434,62 @@ } } + void input_evdev_init() + { + int evdev_device_id[4] = { -1, -1, -1, -1 }; + size_t size_needed; + int port, i; + + char* evdev_device; + + for (port = 0; port < 4; port++) + { + size_needed = snprintf(NULL, 0, EVDEV_DEVICE_CONFIG_KEY, port+1) + 1; + char* evdev_config_key = (char*)malloc(size_needed); + sprintf(evdev_config_key, EVDEV_DEVICE_CONFIG_KEY, port+1); + evdev_device_id[port] = cfgLoadInt("input", evdev_config_key, EVDEV_DEFAULT_DEVICE_ID(port+1)); + free(evdev_config_key); + + // Check if the same device is already in use on another port + if (evdev_device_id[port] < 0) + { + printf("evdev: Controller %d disabled by config.\n", port + 1); + } + else + { + size_needed = snprintf(NULL, 0, EVDEV_DEVICE_STRING, evdev_device_id[port]) + 1; + evdev_device = (char*)malloc(size_needed); + sprintf(evdev_device, EVDEV_DEVICE_STRING, evdev_device_id[port]); + + size_needed = snprintf(NULL, 0, EVDEV_MAPPING_CONFIG_KEY, port+1) + 1; + evdev_config_key = (char*)malloc(size_needed); + sprintf(evdev_config_key, EVDEV_MAPPING_CONFIG_KEY, port+1); + + string tmp; + const char* mapping = (cfgExists("input", evdev_config_key) == 2 ? (tmp = cfgLoadStr("input", evdev_config_key, "")).c_str() : NULL); + free(evdev_config_key); + + input_evdev_init(&evdev_controllers[port], evdev_device, mapping); + + free(evdev_device); + + for (i = 0; i < port; i++) + { + if (evdev_device_id[port] == evdev_device_id[i]) + { + // Multiple controllers with the same device, check for multiple button assignments + if (input_evdev_button_duplicate_button(evdev_controllers[i].mapping, evdev_controllers[port].mapping)) + { + printf("WARNING: One or more button(s) of this device is also used in the configuration of input device %d (mapping: %s)\n", i, evdev_controllers[i].mapping->name.c_str()); + } + } + } + + mcfg_CreateController(port, GetMapleDeviceType(evdev_controllers[port].mapping->Maple_Device1, port), GetMapleDeviceType(evdev_controllers[port].mapping->Maple_Device2, port)); + } + } + } + void input_evdev_close() { for (int port = 0; port < 4 ; port++) diff --git a/core/linux-dist/evdev.h b/core/linux-dist/evdev.h index 348fbc4e8..10d298819 100644 --- a/core/linux-dist/evdev.h +++ b/core/linux-dist/evdev.h @@ -75,7 +75,5 @@ struct EvdevController extern void input_evdev_init(); extern void input_evdev_close(); -extern int input_evdev_init(EvdevController* controller, const char* device, const char* mapping_fname); extern bool input_evdev_handle(u32 port); extern void input_evdev_rumble(u32 port, u16 pow_strong, u16 pow_weak); -extern bool input_evdev_button_duplicate_button(EvdevControllerMapping* mapping1, EvdevControllerMapping* mapping2);