From 0d419a0064bf95feec139e6484a00af20df08967 Mon Sep 17 00:00:00 2001 From: Maschell Date: Sat, 20 May 2017 19:22:16 +0200 Subject: [PATCH] Saving the order in which the controllers were connected --- ControllerPatcher.cpp | 145 ++++++++----------------------- ControllerPatcher.hpp | 1 - ControllerPatcherWrapper.cpp | 8 +- ControllerPatcherWrapper.h | 2 +- patcher/ControllerPatcherDefs.h | 34 ++++---- patcher/ControllerPatcherHID.cpp | 19 ++++ utils/CPRetainVars.cpp | 2 + utils/CPRetainVars.hpp | 2 + 8 files changed, 78 insertions(+), 135 deletions(-) diff --git a/ControllerPatcher.cpp b/ControllerPatcher.cpp index 5518957..7f5fb34 100644 --- a/ControllerPatcher.cpp +++ b/ControllerPatcher.cpp @@ -78,8 +78,8 @@ void ControllerPatcher::InitButtonMapping(){ } void ControllerPatcher::ResetConfig(){ - memset(&gControllerMapping,0,sizeof(gControllerMapping)); - disableControllerMapping(); + memset(connectionOrderHelper,0,sizeof(connectionOrderHelper)); + memset(&gControllerMapping,0,sizeof(gControllerMapping)); // Init / Invalid everything memset(config_controller,CONTROLLER_PATCHER_INVALIDVALUE,sizeof(config_controller)); // Init / Invalid everything memset(config_controller_hidmask,0,sizeof(config_controller_hidmask)); // Init / Invalid everything memset(gNetworkController,0,sizeof(gNetworkController)); // Init / Invalid everything @@ -564,6 +564,7 @@ void ControllerPatcher::DeInit(){ memset(config_controller,0,sizeof(config_controller)); memset(config_controller_hidmask,0,sizeof(config_controller_hidmask)); memset(gNetworkController,0,sizeof(gNetworkController)); + memset(connectionOrderHelper,0,sizeof(connectionOrderHelper)); memset(gWPADConnectCallback,0,sizeof(gWPADConnectCallback)); memset(gKPADConnectCallback,0,sizeof(gKPADConnectCallback)); @@ -762,44 +763,40 @@ CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcher::setRumble(UController_Type return CONTROLLER_PATCHER_ERROR_NONE; } -CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcher::gettingInputAllDevicesEx(InputDataEx * output,s32 array_size){ - s32 hid = gHIDCurrentDevice; +CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcher::gettingInputAllDevices(InputData * output,s32 array_size){ HID_Data * data_cur; VPADStatus pad_buffer; VPADStatus * buffer = &pad_buffer; s32 result = CONTROLLER_PATCHER_ERROR_NONE; - for(s32 i = 0;i< gHIDMaxDevices;i++){ - if(result > array_size){ - break; - } - if((hid & (1 << i)) != 0){ - memset(buffer,0,sizeof(*buffer)); + std::map pad_count; + //printf("fill in data\n"); + for(s32 i = 0;i < gHIDMaxDevices;i++){ + u8 * status = &output[result].status; + *status = 0; + if(connectionOrderHelper[i] != NULL){ + *status = 1; - s32 newhid = (1 << i); + my_cb_user * usr = connectionOrderHelper[i]; + pad_count[usr] = pad_count[usr] +1; + s32 hid = usr->slotdata.hidmask; + //printf("result[%d] usr: %08X\n",result,usr); + + s32 realpad = pad_count[usr] - 1; + + output[result].device_info.pad_count = usr->pads_per_device; + output[result].device_info.slotdata = usr->slotdata; + output[result].device_info.vidpid = usr->vidpid; + + InputButtonData * buttondata = &output[result].button_data; + InputStickData * stickdata = &output[result].stick_data; s32 buttons_hold = 0; + buttondata->hold = 0; + buttondata->trigger = 0; + buttondata->release = 0; - for(s32 pad = 0;padhold = 0; - buttondata->trigger = 0; - buttondata->release = 0; - - result++; - if(result > array_size){ - break; - } - - s32 res; - if((res = ControllerPatcherHID::getHIDData(newhid,pad,&data_cur)) < 0){ - *status = 0; - continue; - } - *status = 1; - + s32 res; + if((res = ControllerPatcherHID::getHIDData(hid,realpad,&data_cur)) == 0){ res = ControllerPatcherUtils::getButtonPressed(data_cur,&buttons_hold,VPAD_BUTTON_A); ControllerPatcherUtils::getButtonPressed(data_cur,&buttons_hold,VPAD_BUTTON_B); ControllerPatcherUtils::getButtonPressed(data_cur,&buttons_hold,VPAD_BUTTON_X); @@ -840,89 +837,15 @@ CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcher::gettingInputAllDevicesEx(I stickdata->rightStickX = buffer->rightStick.x; stickdata->rightStickY = buffer->rightStick.y; + //printf("result[%d] buttons %08X\n",result,buttons_hold); + data_cur->last_buttons = buttons_hold; } } - } - return array_size; -} - -CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcher::gettingInputAllDevices(InputData * output,s32 array_size){ - s32 hid = gHIDCurrentDevice; - HID_Data * data_cur; - VPADStatus pad_buffer; - VPADStatus * buffer = &pad_buffer; - s32 result = CONTROLLER_PATCHER_ERROR_NONE; - for(s32 i = 0;i< gHIDMaxDevices;i++){ - if((hid & (1 << i)) != 0){ - memset(buffer,0,sizeof(*buffer)); - - s32 newhid = (1 << i); - s32 deviceslot = ControllerPatcherUtils::getDeviceSlot(newhid); - if(deviceslot < 0) continue; - DeviceInfo * deviceinfo = &(output[result].device_info); - InputButtonData * buttondata = output[result].button_data; - - deviceinfo->slotdata.deviceslot = deviceslot; - deviceinfo->slotdata.hidmask = newhid; - - deviceinfo->vidpid.vid = config_controller[deviceslot][CONTRPS_VID][0] * 0x100 + config_controller[deviceslot][CONTRPS_VID][1]; - deviceinfo->vidpid.pid = config_controller[deviceslot][CONTRPS_PID][0] * 0x100 + config_controller[deviceslot][CONTRPS_PID][1]; - - /* not needed - if(config_controller[deviceslot][CONTRPS_PAD_COUNT][0] != CONTROLLER_PATCHER_INVALIDVALUE){ - deviceinfo->pad_count = config_controller[deviceslot][CONTRPS_PAD_COUNT][1]; - }else{ - deviceinfo->pad_count = HID_MAX_PADS_COUNT; - }*/ - - s32 buttons_hold = 0; - - for(s32 pad = 0;padslotdata.hidmask,pad,&data_cur)) < 0){ - continue; - } - - res = ControllerPatcherUtils::getButtonPressed(data_cur,&buttons_hold,VPAD_BUTTON_A); - ControllerPatcherUtils::getButtonPressed(data_cur,&buttons_hold,VPAD_BUTTON_B); - ControllerPatcherUtils::getButtonPressed(data_cur,&buttons_hold,VPAD_BUTTON_X); - ControllerPatcherUtils::getButtonPressed(data_cur,&buttons_hold,VPAD_BUTTON_Y); - - ControllerPatcherUtils::getButtonPressed(data_cur,&buttons_hold,VPAD_BUTTON_LEFT); - ControllerPatcherUtils::getButtonPressed(data_cur,&buttons_hold,VPAD_BUTTON_RIGHT); - ControllerPatcherUtils::getButtonPressed(data_cur,&buttons_hold,VPAD_BUTTON_DOWN); - ControllerPatcherUtils::getButtonPressed(data_cur,&buttons_hold,VPAD_BUTTON_UP); - - ControllerPatcherUtils::getButtonPressed(data_cur,&buttons_hold,VPAD_BUTTON_MINUS); - ControllerPatcherUtils::getButtonPressed(data_cur,&buttons_hold,VPAD_BUTTON_L); - ControllerPatcherUtils::getButtonPressed(data_cur,&buttons_hold,VPAD_BUTTON_R); - - ControllerPatcherUtils::getButtonPressed(data_cur,&buttons_hold,VPAD_BUTTON_PLUS); - ControllerPatcherUtils::getButtonPressed(data_cur,&buttons_hold,VPAD_BUTTON_ZL); - ControllerPatcherUtils::getButtonPressed(data_cur,&buttons_hold,VPAD_BUTTON_ZR); - - ControllerPatcherUtils::getButtonPressed(data_cur,&buttons_hold,VPAD_BUTTON_HOME); - ControllerPatcherUtils::getButtonPressed(data_cur,&buttons_hold,VPAD_BUTTON_STICK_L); - ControllerPatcherUtils::getButtonPressed(data_cur,&buttons_hold,VPAD_BUTTON_STICK_R); - - buttondata[pad].hold |= buttons_hold; - buttondata[pad].trigger |= (buttons_hold & (~(data_cur->last_buttons))); - buttondata[pad].release |= ((data_cur->last_buttons) & (~buttons_hold)); - - data_cur->last_buttons = buttons_hold; - } - result++; - - if(result >= array_size){ - break; - } + result++; + if(result >= array_size){ + break; } } diff --git a/ControllerPatcher.hpp b/ControllerPatcher.hpp index ecc7bd7..58aaa65 100644 --- a/ControllerPatcher.hpp +++ b/ControllerPatcher.hpp @@ -217,7 +217,6 @@ class ControllerPatcher{ @return When the functions failed result < 0 is returned. If the result is == 0 the function was successful. If the result is > 0 the number of stored sets in the array is returned. **/ static CONTROLLER_PATCHER_RESULT_OR_ERROR gettingInputAllDevices(InputData * output,s32 array_size); - static CONTROLLER_PATCHER_RESULT_OR_ERROR gettingInputAllDevicesEx(InputDataEx * output,s32 array_size); /** Remaps the buttons in the given \p VPADStatus pointer. InitButtonMapping() needs to be called before calling this. The information about the remapping is stored in the config_controller array. diff --git a/ControllerPatcherWrapper.cpp b/ControllerPatcherWrapper.cpp index e0c2fb6..c7b7b8c 100644 --- a/ControllerPatcherWrapper.cpp +++ b/ControllerPatcherWrapper.cpp @@ -15,7 +15,8 @@ * along with this program. If not, see . ****************************************************************************/ #include "controller_patcher/ControllerPatcher.hpp" -#include "controller_patcher/ControllerPatcherWrapper.h" + #include "controller_patcher/ControllerPatcherWrapper.h" + extern "C" void ControllerPatcherInit(void){ ControllerPatcher::Init(); ControllerPatcher::disableControllerMapping(); @@ -23,12 +24,13 @@ extern "C" void ControllerPatcherInit(void){ ControllerPatcher::disableWiiUEnergySetting(); } + extern "C" CONTROLLER_PATCHER_RESULT_OR_ERROR setControllerDataFromHID(VPADStatus * data){ ControllerPatcher::setControllerDataFromHID(data); } -extern "C" CONTROLLER_PATCHER_RESULT_OR_ERROR gettingInputAllDevicesEx(InputDataEx * output,s32 array_size){ - ControllerPatcher::gettingInputAllDevicesEx(output,array_size); +extern "C" CONTROLLER_PATCHER_RESULT_OR_ERROR gettingInputAllDevices(InputData * output,s32 array_size){ + ControllerPatcher::gettingInputAllDevices(output,array_size); } extern "C" void ControllerPatcherDeInit(void){ diff --git a/ControllerPatcherWrapper.h b/ControllerPatcherWrapper.h index 4291b22..106d2aa 100644 --- a/ControllerPatcherWrapper.h +++ b/ControllerPatcherWrapper.h @@ -14,7 +14,7 @@ extern "C" { void ControllerPatcherInit(void); void ControllerPatcherDeInit(void); CONTROLLER_PATCHER_RESULT_OR_ERROR setControllerDataFromHID(VPADStatus * data); -CONTROLLER_PATCHER_RESULT_OR_ERROR gettingInputAllDevicesEx(InputDataEx * output,s32 array_size); +CONTROLLER_PATCHER_RESULT_OR_ERROR gettingInputAllDevices(InputData * output,s32 array_size); #ifdef __cplusplus } diff --git a/patcher/ControllerPatcherDefs.h b/patcher/ControllerPatcherDefs.h index a1c0c6e..f005e39 100644 --- a/patcher/ControllerPatcherDefs.h +++ b/patcher/ControllerPatcherDefs.h @@ -223,6 +223,14 @@ typedef struct _HIDSlotData{ u32 hidmask; /**< Used HID-Mask */ }HIDSlotData; +/** + * @brief Stores a VID and PID + */ +typedef struct _DeviceVIDPIDInfo{ + u16 vid; /**< Vendor ID of this device */ + u16 pid; /**< Product ID of this device */ +}DeviceVIDPIDInfo; + /** * @brief Struct where the data for the callback funtion is stored */ @@ -235,6 +243,7 @@ typedef struct _my_cb_user{ u8 pad_slot; /**< number of the pad that will be used */ u8 rumblestatus[HID_MAX_PADS_COUNT]; /**< Current status of the device rumble */ u8 forceRumbleInTicks[HID_MAX_PADS_COUNT]; + DeviceVIDPIDInfo vidpid; /**< The VID/PID of the device */ }my_cb_user; /** @@ -289,15 +298,6 @@ typedef struct _HID_DEVICE_DATA { HID_Data pad_data[HID_MAX_PADS_COUNT]; } HID_DEVICE_DATA; - -/** - * @brief Stores a VID and PID - */ -typedef struct _DeviceVIDPIDInfo{ - u16 vid; /**< Vendor ID of this device */ - u16 pid; /**< Product ID of this device */ -}DeviceVIDPIDInfo; - /** * @brief Infos of the device */ @@ -353,14 +353,6 @@ typedef struct _InputButtonData{ u32 release; /**< Buttons that were button released */ }InputButtonData; -/** - * @brief Struct where the inputdata of a device for all HID_MAX_PADS_COUNT pads can be stored - */ -typedef struct _InputData{ - DeviceInfo device_info; /**< Infos about the device where the data is coming from */ - InputButtonData button_data[HID_MAX_PADS_COUNT]; -}InputData; - typedef struct _InputStickData{ f32 leftStickX; f32 leftStickY; @@ -368,11 +360,15 @@ typedef struct _InputStickData{ f32 rightStickY; }InputStickData; -typedef struct _InputDataEx{ +/** + * @brief Struct where the inputdata of a device for all HID_MAX_PADS_COUNT pads can be stored + */ +typedef struct _InputData{ + DeviceInfo device_info; /**< Infos about the device where the data is coming from */ u8 status; InputButtonData button_data; InputStickData stick_data; -}InputDataEx; +}InputData; /** * @brief The enumeration of WiiU Controller types diff --git a/patcher/ControllerPatcherHID.cpp b/patcher/ControllerPatcherHID.cpp index 8033a32..7bafcd0 100644 --- a/patcher/ControllerPatcherHID.cpp +++ b/patcher/ControllerPatcherHID.cpp @@ -160,6 +160,7 @@ s32 ControllerPatcherHID::AttachDetachCallback(HIDClient *p_client, HIDDevice *p usr->slotdata = device_info.slotdata; usr->transfersize = p_device->max_packet_size_rx; usr->handle = p_device->handle; + usr->vidpid = device_info.vidpid; gHIDAttached |= slotdata->hidmask; gHIDCurrentDevice |= slotdata->hidmask; s32 pads_per_device = 1; @@ -220,6 +221,15 @@ s32 ControllerPatcherHID::AttachDetachCallback(HIDClient *p_client, HIDDevice *p DCInvalidateRange(&gHID_Devices[slotdata->deviceslot].pad_data[pad_slot+i],sizeof(HID_Data)); } + for(s32 j = 0;j < pads_per_device; j++){ + for(s32 i = 0;i < gHIDMaxDevices; i++){ + if(connectionOrderHelper[i] == NULL){ + connectionOrderHelper[i] = usr; + break; + } + } + } + if(HID_DEBUG){ printf("ControllerPatcherHID::AttachDetachCallback(line %d): Device successfully attached\n",__LINE__); } if(slotdata->hidmask == gHID_LIST_GC){ // GC PAD @@ -299,6 +309,15 @@ s32 ControllerPatcherHID::AttachDetachCallback(HIDClient *p_client, HIDDevice *p } if(user_data){ + for(s32 j = 0;j < user_data->pads_per_device; j++){ + for(s32 i = 0;i < gHIDMaxDevices; i++){ + if(connectionOrderHelper[i] == user_data){ + connectionOrderHelper[i] = NULL; + break; + } + } + } + config_controller[slotdata->deviceslot][CONTRPS_CONNECTED_PADS][1] &= ~ (1 << user_data->pad_slot); DCFlushRange(&config_controller[slotdata->deviceslot][CONTRPS_CONNECTED_PADS][1],sizeof(config_controller[slotdata->deviceslot][CONTRPS_CONNECTED_PADS][1])); DCInvalidateRange(&config_controller[slotdata->deviceslot][CONTRPS_CONNECTED_PADS][1],sizeof(config_controller[slotdata->deviceslot][CONTRPS_CONNECTED_PADS][1])); diff --git a/utils/CPRetainVars.cpp b/utils/CPRetainVars.cpp index 1cb6d1d..1642753 100644 --- a/utils/CPRetainVars.cpp +++ b/utils/CPRetainVars.cpp @@ -64,6 +64,8 @@ u8 gCallbackCooldown __attribute__((section(".data"))) = 0; u8 gGlobalRumbleActivated __attribute__((section(".data"))) = 0; +my_cb_user * connectionOrderHelper[gHIDMaxDevices] __attribute__((section(".data"))); + u32 gUDPClientip __attribute__((section(".data"))) = 0; ControllerMappingPADInfo* gProPadInfo[4] __attribute__((section(".data"))) = {&gControllerMapping.proController[0].pad_infos[0], &gControllerMapping.proController[1].pad_infos[0], diff --git a/utils/CPRetainVars.hpp b/utils/CPRetainVars.hpp index e2ce448..696212a 100644 --- a/utils/CPRetainVars.hpp +++ b/utils/CPRetainVars.hpp @@ -68,6 +68,8 @@ extern wpad_sampling_callback_t gSamplingCallback; extern u8 gCallbackCooldown; extern u8 gGlobalRumbleActivated; +extern my_cb_user * connectionOrderHelper[gHIDMaxDevices]; + extern u32 gUDPClientip; extern ControllerMappingPADInfo* gProPadInfo[4];