diff --git a/controller_patcher.c b/controller_patcher.c new file mode 100644 index 0000000..3c89119 --- /dev/null +++ b/controller_patcher.c @@ -0,0 +1,992 @@ +#include +#include +#include +#include +#include "controller_patcher.h" +#include "dynamic_libs/os_functions.h" +#include "dynamic_libs/sys_functions.h" +#include "cp_retain_vars.h" +#include "utils/logger.h" + +void setConfigValue(u8 * dest, u8 first, u8 second){ + dest[0] = first; + dest[1] = second; +} + +void init_config_controller(){ + if(!config_done){ + config_done = 1; + memset(config_controller,CONTROLLER_PATCHER_INVALIDVALUE,sizeof(config_controller)); // Init / Invalid everything + memset(config_controller_list,-1,sizeof(config_controller_list)); // Init / Invalid everything + memset(config_controller_data_ptr,0,sizeof(config_controller_data_ptr)); // Init / Invalid everything + + config_controller_list[CONTRPD_GC] = HID_LIST_GC; + config_controller_list[CONTRPD_DS3] = HID_LIST_DS3; + config_controller_list[CONTRPD_DS4] = HID_LIST_DS4; + config_controller_list[CONTRPD_SP2600] = HID_LIST_SP2600; + config_controller_list[CONTRPD_KEYBOARD] = HID_LIST_KEYBOARD; + config_controller_list[CONTRPD_PS2] = HID_LIST_PS2; + //config_controller_list[CONTRPD_YOUR_DEVICE] = HID_LIST_YOUR_DEVICE; + + //Set data for each pad. Currenty 4 Pads for each device support. May need other extra treatment + config_controller_data_ptr[CONTRPD_GC][0] = (u32)&(gHID_Devices[CONTRPD_GC]).pad_data[0]; //GC Pad Port 1 + config_controller_data_ptr[CONTRPD_GC][1] = (u32)&(gHID_Devices[CONTRPD_GC]).pad_data[1]; //GC Pad Port 2 + config_controller_data_ptr[CONTRPD_GC][2] = (u32)&(gHID_Devices[CONTRPD_GC]).pad_data[2]; //GC Pad Port 3 + config_controller_data_ptr[CONTRPD_GC][3] = (u32)&(gHID_Devices[CONTRPD_GC]).pad_data[3]; //GC Pad Port 4 + config_controller_data_ptr[CONTRPD_DS3][0] = (u32)&(gHID_Devices[CONTRPD_DS3]).pad_data[0]; + config_controller_data_ptr[CONTRPD_DS4][0] = (u32)&(gHID_Devices[CONTRPD_DS4]).pad_data[0]; + config_controller_data_ptr[CONTRPD_SP2600][0] = (u32)&(gHID_Devices[CONTRPD_SP2600]).pad_data[0]; + config_controller_data_ptr[CONTRPD_KEYBOARD][0] = (u32)&(gHID_Devices[CONTRPD_KEYBOARD]).pad_data[0]; + config_controller_data_ptr[CONTRPD_PS2][0] = (u32)&(gHID_Devices[CONTRPD_PS2]).pad_data[0]; + + + //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + //! GC-Adapter + //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VID], 0x05,0x7e); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_PID], 0x03,0x37); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_A], 0x01,HID_GC_BUTTON_A); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_A], 0x01,HID_GC_BUTTON_A); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_B], 0x01,HID_GC_BUTTON_B); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_X], 0x01,HID_GC_BUTTON_X); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_Y], 0x01,HID_GC_BUTTON_Y); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_DPAD_MODE], CONTROLLER_PATCHER_VALUE_SET,CONTRPDM_Normal); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_LEFT], 0x01,HID_GC_BUTTON_LEFT); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_RIGHT], 0x01,HID_GC_BUTTON_RIGHT); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_DOWN], 0x01,HID_GC_BUTTON_DOWN); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_UP], 0x01,HID_GC_BUTTON_UP); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_MINUS], 0x02,HID_GC_BUTTON_START); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_L], 0x07,HID_GC_BUTTON_L); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_R], 0x08,HID_GC_BUTTON_R); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_PLUS], 0x02,HID_GC_BUTTON_START); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_ZL], 0x07,HID_GC_BUTTON_L); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_ZR], 0x08,HID_GC_BUTTON_R); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_DOUBLE_USE], CONTROLLER_PATCHER_VALUE_SET,CONTROLLER_PATCHER_GC_DOUBLE_USE); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_DOUBLE_USE_BUTTON], 0x02,HID_GC_BUTTON_Z); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_PAD_COUNT], CONTROLLER_PATCHER_VALUE_SET,HID_GC_PAD_COUNT); + + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_L_STICK_X], 0x03,0x80); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_L_STICK_X_MINMAX], 0x1D,0xE6); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_L_STICK_Y], 0x04,0x80); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_L_STICK_Y_MINMAX], 0x17,0xE5); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_R_STICK_X], 0x05,0x83); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_R_STICK_X_MINMAX], 0x23,0xE0); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_R_STICK_Y], 0x06,0x80); + setConfigValue((u8*)&config_controller[CONTRPD_GC][CONTRPS_VPAD_BUTTON_R_STICK_Y_MINMAX], 0x19,0xDD); + + //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + //! DS3 + //!--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VID], 0x05,0x4c); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_PID], 0x02,0x68); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_BUF_SIZE], CONTROLLER_PATCHER_VALUE_SET,128); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_A], 0x03,HID_DS3_BUTTON_CIRCLE); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_B], 0x03,HID_DS3_BUTTON_CROSS); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_X], 0x03,HID_DS3_BUTTON_TRIANGLE); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_Y], 0x03,HID_DS3_BUTTON_SQUARE); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_DPAD_MODE], CONTROLLER_PATCHER_VALUE_SET,CONTRPDM_Normal); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_LEFT], 0x02,HID_DS3_BUTTON_LEFT); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_RIGHT], 0x02,HID_DS3_BUTTON_RIGHT); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_DOWN], 0x02,HID_DS3_BUTTON_DOWN); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_UP], 0x02,HID_DS3_BUTTON_UP); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_PLUS], 0x02,HID_DS3_BUTTON_START); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_MINUS], 0x02,HID_DS3_BUTTON_SELECT); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_L], 0x03,HID_DS3_BUTTON_L1); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_R], 0x03,HID_DS3_BUTTON_R1); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_ZL], 0x03,HID_DS3_BUTTON_L2); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_ZR], 0x03,HID_DS3_BUTTON_R2); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_STICK_L], 0x02,HID_DS3_BUTTON_L3); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_STICK_R], 0x02,HID_DS3_BUTTON_R3); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_HOME], 0x04,HID_DS3_BUTTON_GUIDE); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_PAD_COUNT], CONTROLLER_PATCHER_VALUE_SET,HID_DS3_PAD_COUNT); + + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_L_STICK_X], 0x06,0x80); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_L_STICK_X_MINMAX], 0x00,0xFF); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_L_STICK_Y], 0x07,0x80); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_L_STICK_Y_INVERT], CONTROLLER_PATCHER_VALUE_SET,0x01); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_L_STICK_Y_MINMAX], 0x00,0xFF); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_R_STICK_X], 0x08,0x83); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_R_STICK_X_MINMAX], 0x00,0xFF); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_R_STICK_Y], 0x09,0x80); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_R_STICK_Y_INVERT], CONTROLLER_PATCHER_VALUE_SET,0x01); + setConfigValue((u8*)&config_controller[CONTRPD_DS3][CONTRPS_VPAD_BUTTON_R_STICK_Y_MINMAX], 0x00,0xFF); + + //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + //! DS4 + //!--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VID], 0x05,0x4c); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_PID], 0x05,0xc4); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_BUF_SIZE], CONTROLLER_PATCHER_VALUE_SET,128); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_A], 0x05,HID_DS4_BUTTON_CIRCLE); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_B], 0x05,HID_DS4_BUTTON_CROSS); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_X], 0x05,HID_DS4_BUTTON_TRIANGLE); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_Y], 0x05,HID_DS4_BUTTON_SQUARE); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_DPAD_MODE], CONTROLLER_PATCHER_VALUE_SET,CONTRPDM_Hat); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_DPAD_N], 0x05,HID_DS4_BUTTON_DPAD_N); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_DPAD_NE], 0x05,HID_DS4_BUTTON_DPAD_NE); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_DPAD_E], 0x05,HID_DS4_BUTTON_DPAD_E); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_DPAD_SE], 0x05,HID_DS4_BUTTON_DPAD_SE); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_DPAD_S], 0x05,HID_DS4_BUTTON_DPAD_S); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_DPAD_SW], 0x05,HID_DS4_BUTTON_DPAD_SW); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_DPAD_W], 0x05,HID_DS4_BUTTON_DPAD_W); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_DPAD_NW], 0x05,HID_DS4_BUTTON_DPAD_NW); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_DPAD_NEUTRAL], 0x05,HID_DS4_BUTTON_DPAD_NEUTRAL); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_PLUS], 0x06,HID_DS4_BUTTON_OPTIONS); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_MINUS], 0x06,HID_DS4_BUTTON_SHARE); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_L], 0x06,HID_DS4_BUTTON_L1); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_R], 0x06,HID_DS4_BUTTON_R1); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_ZL], 0x06,HID_DS4_BUTTON_L2); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_ZR], 0x06,HID_DS4_BUTTON_R2); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_STICK_L], 0x06,HID_DS4_BUTTON_L3); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_STICK_R], 0x06,HID_DS4_BUTTON_R3); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_HOME], 0x07,HID_DS4_BUTTON_GUIDE); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_PAD_COUNT], CONTROLLER_PATCHER_VALUE_SET,HID_DS4_PAD_COUNT); + + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_L_STICK_X], 0x01,0x80); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_L_STICK_X_MINMAX], 0x00,0xFF); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_L_STICK_Y], 0x02,0x80); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_L_STICK_Y_INVERT], CONTROLLER_PATCHER_VALUE_SET,0x01); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_L_STICK_Y_MINMAX], 0x00,0xFF); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_R_STICK_X], 0x03,0x80); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_R_STICK_X_MINMAX], 0x00,0xFF); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_R_STICK_Y], 0x04,0x80); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_R_STICK_Y_INVERT], CONTROLLER_PATCHER_VALUE_SET,0x01); + setConfigValue((u8*)&config_controller[CONTRPD_DS4][CONTRPS_VPAD_BUTTON_R_STICK_Y_MINMAX], 0x00,0xFF); + + //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + //! Saitek P2600 + //!--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VID], 0xa3,0x06); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_PID], 0x0d,0x5f); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_A], 0x05,HID_SP2600_BUTTON_1); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_B], 0x05,HID_SP2600_BUTTON_2); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_X], 0x05,HID_SP2600_BUTTON_3); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_Y], 0x05,HID_SP2600_BUTTON_4); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_DPAD_MODE], CONTROLLER_PATCHER_VALUE_SET,CONTRPDM_Normal); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_DPAD_MODE], CONTROLLER_PATCHER_VALUE_SET,CONTRPDM_Hat); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_DPAD_N], 0x06,HID_SP2600_BUTTON_DPAD_N); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_DPAD_NE], 0x06,HID_SP2600_BUTTON_DPAD_NE); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_DPAD_E], 0x06,HID_SP2600_BUTTON_DPAD_E); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_DPAD_SE], 0x06,HID_SP2600_BUTTON_DPAD_SE); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_DPAD_S], 0x06,HID_SP2600_BUTTON_DPAD_S); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_DPAD_SW], 0x06,HID_SP2600_BUTTON_DPAD_SW); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_DPAD_W], 0x06,HID_SP2600_BUTTON_DPAD_W); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_DPAD_NW], 0x06,HID_SP2600_BUTTON_DPAD_NW); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_DPAD_NEUTRAL], 0x06,HID_SP2600_BUTTON_DPAD_NEUTRAL); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_PLUS], 0x06,HID_SP2600_BUTTON_FPS); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_MINUS], 0x06,HID_SP2600_BUTTON_Analog); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_L], 0x05,HID_SP2600_BUTTON_7); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_R], 0x05,HID_SP2600_BUTTON_6); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_ZL], 0x05,HID_SP2600_BUTTON_5); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_ZR], 0x05,HID_SP2600_BUTTON_8); + + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_PAD_COUNT], CONTROLLER_PATCHER_VALUE_SET,HID_SP2600_PAD_COUNT); + + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_L_STICK_X], 0x01,0x80); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_L_STICK_X_MINMAX], 0x00,0xFF); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_L_STICK_Y], 0x02,0x80); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_L_STICK_Y_MINMAX], 0x00,0xFF); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_R_STICK_X], 0x03,0x83); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_R_STICK_X_MINMAX], 0x00,0xFF); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_R_STICK_Y], 0x04,0x80); + setConfigValue((u8*)&config_controller[CONTRPD_SP2600][CONTRPS_VPAD_BUTTON_R_STICK_Y_MINMAX], 0x00,0xFF); + + //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + //! PS2 + //!--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VID], 0x0e,0x8f); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_PID], 0x00,0x03); + + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_A], 0x05,HID_PS2_BUTTON_CIRCLE); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_B], 0x05,HID_PS2_BUTTON_CROSS); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_X], 0x05,HID_PS2_BUTTON_TRIANGLE); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_Y], 0x05,HID_PS2_BUTTON_SQUARE); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_PLUS], 0x06,HID_PS2_BUTTON_START); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_MINUS], 0x06,HID_PS2_BUTTON_SELECT); + + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_L], 0x06,HID_PS2_BUTTON_L1); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_R], 0x06,HID_PS2_BUTTON_R1); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_ZL], 0x06,HID_PS2_BUTTON_L2); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_ZR], 0x06,HID_PS2_BUTTON_R2); + + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_DPAD_MODE], CONTROLLER_PATCHER_VALUE_SET,CONTRPDM_Hat); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_DPAD_N], 0x05,HID_PS2_BUTTON_DPAD_N); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_DPAD_NE], 0x05,HID_PS2_BUTTON_DPAD_NE); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_DPAD_E], 0x05,HID_PS2_BUTTON_DPAD_E); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_DPAD_SE], 0x05,HID_PS2_BUTTON_DPAD_SE); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_DPAD_S], 0x05,HID_PS2_BUTTON_DPAD_S); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_DPAD_SW], 0x05,HID_PS2_BUTTON_DPAD_SW); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_DPAD_W], 0x05,HID_PS2_BUTTON_DPAD_W); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_DPAD_NW], 0x05,HID_PS2_BUTTON_DPAD_NW); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_DPAD_NEUTRAL], 0x05,HID_PS2_BUTTON_DPAD_NEUTRAL); + + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_PAD_COUNT], CONTROLLER_PATCHER_VALUE_SET,HID_PS2_PAD_COUNT); + + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_L_STICK_X], 0x02,0x7B); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_L_STICK_X_MINMAX], 0x00,0xFF); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_L_STICK_Y], 0x03,0x84); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_L_STICK_Y_MINMAX], 0x00,0xFF); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_R_STICK_X], 0x00,0x7B); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_R_STICK_X_MINMAX], 0x00,0xFF); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_R_STICK_Y], 0x01,0x84); + setConfigValue((u8*)&config_controller[CONTRPD_PS2][CONTRPS_VPAD_BUTTON_R_STICK_Y_MINMAX], 0x00,0xFF); + + //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + //! Keyboard + //!--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + + setConfigValue((u8*)&config_controller[CONTRPD_KEYBOARD][CONTRPS_VPAD_BUTTON_A], 0x00,HID_KEYBOARD_BUTTON_A); + setConfigValue((u8*)&config_controller[CONTRPD_KEYBOARD][CONTRPS_VPAD_BUTTON_B], 0x00,HID_KEYBOARD_BUTTON_B); + setConfigValue((u8*)&config_controller[CONTRPD_KEYBOARD][CONTRPS_VPAD_BUTTON_X], 0x00,HID_KEYBOARD_BUTTON_X); + setConfigValue((u8*)&config_controller[CONTRPD_KEYBOARD][CONTRPS_VPAD_BUTTON_Y], 0x00,HID_KEYBOARD_BUTTON_Y); + setConfigValue((u8*)&config_controller[CONTRPD_KEYBOARD][CONTRPS_DPAD_MODE], CONTROLLER_PATCHER_VALUE_SET,CONTRPDM_Normal); + setConfigValue((u8*)&config_controller[CONTRPD_KEYBOARD][CONTRPS_VPAD_BUTTON_LEFT], 0x00,HID_KEYBOARD_BUTTON_LEFT); + setConfigValue((u8*)&config_controller[CONTRPD_KEYBOARD][CONTRPS_VPAD_BUTTON_RIGHT], 0x00,HID_KEYBOARD_BUTTON_RIGHT); + setConfigValue((u8*)&config_controller[CONTRPD_KEYBOARD][CONTRPS_VPAD_BUTTON_DOWN], 0x00,HID_KEYBOARD_BUTTON_DOWN); + setConfigValue((u8*)&config_controller[CONTRPD_KEYBOARD][CONTRPS_VPAD_BUTTON_UP], 0x00,HID_KEYBOARD_BUTTON_UP); + setConfigValue((u8*)&config_controller[CONTRPD_KEYBOARD][CONTRPS_VPAD_BUTTON_PLUS], 0x00,HID_KEYBOARD_BUTTON_PLUS); + setConfigValue((u8*)&config_controller[CONTRPD_KEYBOARD][CONTRPS_VPAD_BUTTON_MINUS], 0x00,HID_KEYBOARD_BUTTON_MINUS); + setConfigValue((u8*)&config_controller[CONTRPD_KEYBOARD][CONTRPS_VPAD_BUTTON_L], 0x00,HID_KEYBOARD_BUTTON_L); + setConfigValue((u8*)&config_controller[CONTRPD_KEYBOARD][CONTRPS_VPAD_BUTTON_R], 0x00,HID_KEYBOARD_BUTTON_R); + setConfigValue((u8*)&config_controller[CONTRPD_KEYBOARD][CONTRPS_VPAD_BUTTON_ZL], 0x00,HID_KEYBOARD_BUTTON_U); + setConfigValue((u8*)&config_controller[CONTRPD_KEYBOARD][CONTRPS_VPAD_BUTTON_ZR], 0x00,HID_KEYBOARD_BUTTON_I); + setConfigValue((u8*)&config_controller[CONTRPD_KEYBOARD][CONTRPS_PAD_COUNT], CONTROLLER_PATCHER_VALUE_SET,HID_KEYBOARD_PAD_COUNT); + }else{ + log_print("Config already done!\n"); + } +} + +void deinit_config_controller(){ + if(gHIDSetupDone){ + if(gHIDAttached) HIDDelClient(&gHIDClient); + HIDTeardown(); + } +} + + +#define SWAP16(x) ((x>>8) | ((x&0xFF)<<8)) +#define SWAP8(x) ((x>>4) | ((x&0xF)<<4)) +int my_attach_cb(HIDClient *p_client, HIDDevice *p_device, unsigned int attach) +{ + if(attach){ + log_printf(" handle %08x\n", p_device->handle); + log_printf(" physical device inst %08x\n", p_device->physical_device_inst); + log_printf(" vid %04x\n", SWAP16(p_device->vid)); + log_printf(" pid %04x\n", SWAP16(p_device->pid)); + log_printf(" interface index %02x\n", p_device->interface_index); + log_printf(" sub class %02x\n", p_device->sub_class); + log_printf(" protocol %02x\n", p_device->protocol); + log_printf(" max packet in %02x\n", p_device->max_packet_size_rx); + log_printf(" max packet out %02x\n", p_device->max_packet_size_tx); + } + + int device = 0; + int deviceSlot = 0; + for(int i = 0;i< CONTRPD_MAX_VALUE;i++){ + u16 vid = config_controller[i][CONTRPS_VID][0] * 0x100 + config_controller[i][CONTRPS_VID][1]; + u16 pid = config_controller[i][CONTRPS_PID][0] * 0x100 + config_controller[i][CONTRPS_PID][1]; + if(SWAP16(p_device->vid) == vid && SWAP16(p_device->pid) == pid){ + device = config_controller_list[i]; + deviceSlot = i; + break; + } + } + if ((p_device->sub_class == 1) && (p_device->protocol == 1)) { //Keyboard + device = HID_LIST_KEYBOARD; + deviceSlot = CONTRPD_KEYBOARD; + }else if ((p_device->sub_class == 1) && (p_device->protocol == 2)){ // MOUSE + device = HID_LIST_MOUSE; + } + + if(device){ + if(attach){ + int bufSize = 64; + if(device != HID_LIST_MOUSE && config_controller[deviceSlot][CONTRPS_BUF_SIZE][0] == CONTROLLER_PATCHER_VALUE_SET){ + bufSize = config_controller[deviceSlot][CONTRPS_BUF_SIZE][1]; + } + unsigned char *buf = memalign(64,bufSize); + memset(buf,0,bufSize); + my_cb_user *usr = memalign(64,sizeof(my_cb_user)); + usr->buf = buf; + usr->hid = device; + + usr->transfersize = p_device->max_packet_size_rx; + usr->handle = p_device->handle; + if(device != HID_LIST_MOUSE ){ + usr->deviceSlot = deviceSlot; + memset(&gHID_Devices[deviceSlot],0,sizeof(HID_DEVICE_DATA)); + gHID_Devices[deviceSlot].user_data = usr; + }else{ + memset(&gHID_Mouse,0,sizeof(HID_Mouse)); + gHID_Mouse.user_data = usr; + } + + gHIDAttached |= device; + gHIDCurrentDevice |= device; + + log_printf("%08X(%d) attached!\n",device,deviceSlot); + + if(device == HID_LIST_GC){ // GC PAD + buf[0] = 0x13; + HIDWrite(p_device->handle, usr->buf, 1, my_gc_write_cb, usr); + }else if (device == HID_LIST_MOUSE){ + gHID_Mouse_Mode = HID_MOUSE_MODE_TOUCH; + HIDRead(p_device->handle, buf, p_device->max_packet_size_rx, my_ms_read_cb, usr); + }else if (device == HID_LIST_KEYBOARD){ + HIDSetProtocol(p_device->handle, p_device->interface_index, 1, 0, 0); + HIDSetIdle(p_device->handle, p_device->interface_index, 0, 0, 0); + HIDRead(p_device->handle, buf, p_device->max_packet_size_rx, my_read_cb, usr); + }else if (device == HID_LIST_DS3){ + HIDSetProtocol(p_device->handle, p_device->interface_index, 1, 0, 0); + buf[0] = 0x42; buf[1] = 0x0c; buf[2] = 0x00; buf[3] = 0x00; + HIDSetReport(p_device->handle, HID_REPORT_FEATURE, PS3_F4_REPORT_ID, buf, PS3_F4_REPORT_LEN, NULL, NULL); + HIDRead(p_device->handle, usr->buf, p_device->max_packet_size_rx, my_read_cb, usr); + }else{ + HIDRead(p_device->handle, usr->buf, p_device->max_packet_size_rx, my_read_cb, usr); + } + return HID_DEVICE_ATTACH; + + }else{ + my_cb_user * user_data = NULL; + if(device == HID_LIST_MOUSE){ + user_data = (my_cb_user *) gHID_Mouse.user_data; + }else{ + user_data = (my_cb_user *) gHID_Devices[deviceSlot].user_data; + } + if(user_data){ + if(user_data->buf){ + free(user_data->buf); + user_data->buf = NULL; + } + free(user_data); + user_data = NULL; + } + gHIDAttached &= ~device; + gHIDCurrentDevice &= ~device; + if (device == HID_LIST_MOUSE){ + gHID_Mouse_Mode = HID_MOUSE_MODE_AIM; + } + log_printf("%d(%d) detached!\n",device,deviceSlot); + } + }else{ + log_print("HID-Device currently not supported!\n"); + } + return HID_DEVICE_DETACH; +} + +void setControllerReleasePressData(HID_Data_Struct data_cur, HID_Data_Struct data_last,VPADData * buffer,int VPADButton){ + int press = getButtonPressed(data_cur,VPADButton); + if(press) buffer->btns_h |= VPADButton; + if(press != getButtonPressed(data_last,VPADButton)){ + if(!press) buffer->btns_r |= VPADButton; + if(press) buffer->btns_d |= VPADButton; + } +} + +void setControllerDataFromHID(VPADData * buffer,int hid){ + int size = 0; + HID_Data_Struct * data = getHIDDataAll(hid,&size); + if(!data || !size || size < 0){ + if(data)free(data); + return; + } + + HID_Data_Struct data_cur; + HID_Data_Struct data_last; + for(int i = 0;irumbleActive = rumble; +} + +HID_Data_Struct * getHIDDataAll(int hid,int * size){ + *size = 0; + int bit = 1; + for (int i = 0;i < 16; i++){ + *size += (hid & bit)>>i; + bit <<= 1; + } + + HID_Data_Struct * data = malloc(sizeof(HID_Data_Struct)*(*size)); + if(!data) return NULL; + int i = 0; + + //!Mouse always need extra treatment + if(hid & HID_LIST_MOUSE){ + unsigned char * src = (unsigned char *) &(gHID_Mouse.pad_data[getActivePad(HID_LIST_MOUSE)].data[0]); + data[i].src = src; + data[i].hid = HID_LIST_MOUSE; + i++; + } + + for(int j = 0;j < CONTRPD_MAX_VALUE;j++){ + if(hid & config_controller_list[j]){ + data[i].hid = config_controller_list[j]; + data[i].src = (u8*)&(((HID_Data *)config_controller_data_ptr[j][getActivePad(data[i].hid)])->hid_data); + if(!data[i].src){ + (*size)--; + continue; + } + i++; + } + } + return data; +} + +int getPadCount(int hid){ + int device = getDevice(hid); + if(device == -1) return 0; + if(config_controller[device][CONTRPS_PAD_COUNT][0] == CONTROLLER_PATCHER_VALUE_SET){ + return config_controller[device][CONTRPS_PAD_COUNT][1]; + } + + return 0; +} + + +void checkMouseMode(HID_Data_Struct data,HID_Data_Struct data_last){ + unsigned char * src = data.src; + unsigned char * src_last = data_last.src; + int hid = data.hid; + + if(hid & HID_LIST_KEYBOARD){ + if(wasInKeyboard(src,HID_KEYBOARD_BUTTON_F1) && (wasInKeyboard(src,HID_KEYBOARD_BUTTON_F1) != wasInKeyboard(src_last,HID_KEYBOARD_BUTTON_F1))){ + if(gHID_Mouse_Mode == HID_MOUSE_MODE_AIM){ + gHID_Mouse_Mode = HID_MOUSE_MODE_TOUCH; + log_print("Mouse mode changed! to touch \n"); + }else if(gHID_Mouse_Mode == HID_MOUSE_MODE_TOUCH){ + log_print("Mouse mode changed! to aim \n"); + gHID_Mouse_Mode = HID_MOUSE_MODE_AIM; + } + } + } +} + +void setLastData(HID_Data_Struct data,HID_Data_Struct data_last){ + unsigned char * src = data.src; + unsigned char * src_last = data_last.src; + int hid = data.hid; + + if(!src || !src_last) return; + + if(hid & HID_LIST_MOUSE){ + memcpy(src_last,src,sizeof(HID_Mouse_Data)); + } + + memcpy(src_last,src,HID_MAX_DATA_LENGTH_PER_PAD); +} + + +HID_Data_Struct * getLastData(HID_Data_Struct data){ + int hid = data.hid; + unsigned char * src = data.src; + HID_Data_Struct * last_data = malloc(sizeof(HID_Data_Struct)); + if(!last_data) return NULL; + + if(hid & HID_LIST_MOUSE){ + last_data->hid = HID_LIST_KEYBOARD; + last_data->src = (unsigned char *) &(gHID_Mouse.pad_data[0].data[1]);// further testing + return last_data; + } + + int device = getDevice(hid); + if(device == -1) return NULL; + + last_data->hid = config_controller_list[device]; + last_data->src = src + HID_MAX_DATA_LENGTH_PER_PAD; + return last_data; +} + +int getButtonPressed(HID_Data_Struct data, int VPADButton){ + unsigned char * src = data.src; + int hid = data.hid; + + + if(hid & HID_LIST_MOUSE){ + if(gHID_Mouse_Mode == HID_MOUSE_MODE_TOUCH){ + HID_Mouse_Data * ms_data = (HID_Mouse_Data *) src; + if(VPADButton == VPAD_BUTTON_TOUCH){ + if(ms_data->left_click & 0x01){ + return 1; + } + } + }else if(gHID_Mouse_Mode == HID_MOUSE_MODE_AIM){ + HID_Mouse_Data * ms_data = (HID_Mouse_Data *) src; + if(VPADButton == VPAD_BUTTON_ZR){ + if(ms_data->left_click & 0x01){ + return 1; + } + }else if(VPADButton == VPAD_BUTTON_ZL){ + if(ms_data->right_click & 0x01){ + return 1; + } + } + } + return 0; + } + + int device = getDevice(hid); + if(device == -1) return 0; + + int cur_config = 0; + + if(VPADButton == VPAD_BUTTON_A){ + cur_config = CONTRPS_VPAD_BUTTON_A; + }else if(VPADButton == VPAD_BUTTON_B){ + cur_config = CONTRPS_VPAD_BUTTON_B; + }else if(VPADButton == VPAD_BUTTON_X){ + cur_config = CONTRPS_VPAD_BUTTON_X; + }else if(VPADButton == VPAD_BUTTON_Y){ + cur_config = CONTRPS_VPAD_BUTTON_Y; + }else if(VPADButton == VPAD_BUTTON_L){ + cur_config = CONTRPS_VPAD_BUTTON_L; + }else if(VPADButton == VPAD_BUTTON_R){ + cur_config = CONTRPS_VPAD_BUTTON_R; + }else if(VPADButton == VPAD_BUTTON_ZL){ + cur_config = CONTRPS_VPAD_BUTTON_ZL; + }else if(VPADButton == VPAD_BUTTON_ZR){ + cur_config = CONTRPS_VPAD_BUTTON_ZR; + }else if(VPADButton == VPAD_BUTTON_STICK_L){ + cur_config = CONTRPS_VPAD_BUTTON_STICK_L; + }else if(VPADButton == VPAD_BUTTON_STICK_R){ + cur_config = CONTRPS_VPAD_BUTTON_STICK_R; + }else if(VPADButton == VPAD_BUTTON_PLUS){ + cur_config = CONTRPS_VPAD_BUTTON_PLUS; + }else if(VPADButton == VPAD_BUTTON_MINUS){ + cur_config = CONTRPS_VPAD_BUTTON_MINUS; + }else if(VPADButton == VPAD_BUTTON_HOME){ + cur_config = CONTRPS_VPAD_BUTTON_HOME; + } + + //! Special DPAD treatment. + if(config_controller[device][CONTRPS_DPAD_MODE][0] == CONTROLLER_PATCHER_VALUE_SET){ + if(config_controller[device][CONTRPS_DPAD_MODE][1] == CONTRPDM_Hat){ + if(src[config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_NEUTRAL][0]] != config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_NEUTRAL][1]){ // Not neutral + u8 dir1_0 = 0,dir1_1 = 0; + u8 dir2_0 = 0,dir2_1 = 0; + u8 dir3_0 = 0,dir3_1 = 0; + if(VPADButton == VPAD_BUTTON_LEFT){ + dir1_0 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_W][0]; + dir2_0 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_NW][0]; + dir3_0 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_SW][0]; + dir1_1 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_W][1]; + dir2_1 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_NW][1]; + dir3_1 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_SW][1]; + }else if(VPADButton == VPAD_BUTTON_RIGHT){ + dir1_0 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_E][0]; + dir2_0 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_SE][0]; + dir3_0 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_NE][0]; + dir1_1 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_E][1]; + dir2_1 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_SE][1]; + dir3_1 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_NE][1]; + }else if(VPADButton == VPAD_BUTTON_DOWN){ + dir1_0 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_S][0]; + dir2_0 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_SE][0]; + dir3_0 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_SW][0]; + dir1_1 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_S][1]; + dir2_1 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_SE][1]; + dir3_1 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_SW][1]; + }else if(VPADButton == VPAD_BUTTON_UP){ + dir1_0 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_N][0]; + dir2_0 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_NW][0]; + dir3_0 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_NE][0]; + dir1_1 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_N][1]; + dir2_1 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_NW][1]; + dir3_1 = config_controller[device][CONTRPS_VPAD_BUTTON_DPAD_NE][1]; + } + if( (src[dir1_0] == dir1_1) || + (src[dir2_0] == dir2_1) || + (src[dir3_0] == dir3_1)) return 1; + } + + } + } + + //! Normal DPAD treatment. + if(VPADButton == VPAD_BUTTON_LEFT){ + cur_config = CONTRPS_VPAD_BUTTON_LEFT; + }else if(VPADButton == VPAD_BUTTON_RIGHT){ + cur_config = CONTRPS_VPAD_BUTTON_RIGHT; + }else if(VPADButton == VPAD_BUTTON_DOWN){ + cur_config = CONTRPS_VPAD_BUTTON_DOWN; + }else if(VPADButton == VPAD_BUTTON_UP){ + cur_config = CONTRPS_VPAD_BUTTON_UP; + } + + if(config_controller[device][CONTRPS_DOUBLE_USE][0] == CONTROLLER_PATCHER_VALUE_SET){ + if(config_controller[device][CONTRPS_DOUBLE_USE][1] == CONTROLLER_PATCHER_GC_DOUBLE_USE){ + if(src[config_controller[device][CONTRPS_DOUBLE_USE_BUTTON][0]] & config_controller[device][CONTRPS_DOUBLE_USE_BUTTON][1]){ + if(cur_config == CONTRPS_VPAD_BUTTON_PLUS || cur_config == CONTRPS_VPAD_BUTTON_ZL || cur_config == CONTRPS_VPAD_BUTTON_ZR){ + return 0; + } + }else{ + if(cur_config == CONTRPS_VPAD_BUTTON_MINUS || cur_config == CONTRPS_VPAD_BUTTON_L || cur_config == CONTRPS_VPAD_BUTTON_R){ + return 0; + } + } + } + } + //log_printf("Check button %08X \n",VPADButton); + if(config_controller[device][cur_config][0] != 0xFF){ //Invalid data + if(hid & HID_LIST_KEYBOARD){ + if(wasInKeyboard(src,config_controller[device][cur_config][1])) return 1; + }else{ + //log_printf("src[%02X] = %02X & %02X\n",config_controller[device][cur_config][0],src[config_controller[device][cur_config][0]],config_controller[device][cur_config][1]); + //log_printf("%02X %02X %02X %02X %02X %02X %02X %02X | %d = %08X? \n",src[0],src[1],src[2],src[3],src[4],src[5],src[6],src[7],config_controller[device][cur_config][0],config_controller[device][cur_config][1]); + if(src[config_controller[device][cur_config][0]] & config_controller[device][cur_config][1]){ + return 1; + } + + } + } + return 0; +} + +void setTouch(HID_Data_Struct data,VPADData * buffer){ + unsigned char * src = data.src; + int hid = data.hid; + if(hid & HID_LIST_MOUSE && gHID_Mouse_Mode == HID_MOUSE_MODE_TOUCH){ + if(getButtonPressed(data,VPAD_BUTTON_TOUCH)){ + HID_Mouse_Data * ms_data = (HID_Mouse_Data *) src; + int x_mouse = 80 + ((int)(((ms_data->X)*1.0/1280.0)*3890.0)); + int y_mouse = 3910 - ((int)(((ms_data->Y)*1.0/720.0)*3760.0)); + buffer->tpdata.x = x_mouse; + buffer->tpdata.y = y_mouse; + buffer->tpdata.touched = 1; + buffer->tpdata.invalid = 0; + buffer->tpdata1.x = x_mouse; + buffer->tpdata1.y = y_mouse; + buffer->tpdata1.touched = 1; + buffer->tpdata1.invalid = 0; + buffer->tpdata2.x = x_mouse; + buffer->tpdata2.y = y_mouse; + buffer->tpdata2.touched = 1; + buffer->tpdata2.invalid = 0; + } + } +} + + +extern void DoAnalogConv(void *dst, void *src); + +void convertAnalogSticks(HID_Data_Struct data, VPADData * buffer){ + unsigned char * src = data.src; + int hid = data.hid; + int changed = 0; + if (hid & HID_LIST_MOUSE){ + if(gHID_Mouse_Mode == HID_MOUSE_MODE_AIM){ // TODO: tweak values + HID_Mouse_Data * ms_data = (HID_Mouse_Data *) src; + buffer->rstick.x += ms_data->deltaX/10.0; + buffer->rstick.y += -1*(ms_data->deltaY/10.0); + changed = 1; + } + }else{ + int device = getDevice(hid); + if(device == -1) return; + + if (hid & HID_LIST_KEYBOARD){ + + int w,s,a,d; + w = wasInKeyboard(src,HID_KEYBOARD_BUTTON_W); + s = wasInKeyboard(src,HID_KEYBOARD_BUTTON_S); + a = wasInKeyboard(src,HID_KEYBOARD_BUTTON_A); + d = wasInKeyboard(src,HID_KEYBOARD_BUTTON_D); + + if(w){ + if(!s){ + buffer->lstick.y = 1; + } + if(a || d){ + buffer->lstick.y = 0.75; + if(a) buffer->lstick.x = -0.75; + if(d) buffer->lstick.x = 0.75; + } + changed = 1; + }else if(s){ + if(!w){ + buffer->lstick.y = -1; + } + if(a || d){ + buffer->lstick.y = -0.75; + if(a) buffer->lstick.x = -0.75; + if(d) buffer->lstick.x = 0.75; + } + changed = 1; + }else{ + if(a){ + if(!d){ + buffer->lstick.x = -1; + changed = 1; + } + + }else if(d){ + if(!s){ + buffer->lstick.x = 1; + changed = 1; + } + } + } + }else { + s8 value = (s8)(src[config_controller[device][CONTRPS_VPAD_BUTTON_L_STICK_X][0]] - config_controller[device][CONTRPS_VPAD_BUTTON_L_STICK_X][1]); + u8 range = (config_controller[device][CONTRPS_VPAD_BUTTON_L_STICK_X_MINMAX][1] - config_controller[device][CONTRPS_VPAD_BUTTON_L_STICK_X_MINMAX][0])>>1; + if(config_controller[device][CONTRPS_VPAD_BUTTON_L_STICK_X_INVERT][1] != 0x01){ + buffer->lstick.x += (value / (1.0*range)); + }else{ + buffer->lstick.x -= (value / (1.0*range)); + } + + value = (s8)(src[config_controller[device][CONTRPS_VPAD_BUTTON_L_STICK_Y][0]] - config_controller[device][CONTRPS_VPAD_BUTTON_L_STICK_Y][1]); + range = (config_controller[device][CONTRPS_VPAD_BUTTON_L_STICK_Y_MINMAX][1] - config_controller[device][CONTRPS_VPAD_BUTTON_L_STICK_Y_MINMAX][0])>>1; + if(config_controller[device][CONTRPS_VPAD_BUTTON_L_STICK_Y_INVERT][1] != 0x01){ + buffer->lstick.y += (value / (1.0*range)); + }else{ + buffer->lstick.y -= (value / (1.0*range)); + } + + value = (s8)(src[config_controller[device][CONTRPS_VPAD_BUTTON_R_STICK_X][0]] - config_controller[device][CONTRPS_VPAD_BUTTON_R_STICK_X][1]); + range = (config_controller[device][CONTRPS_VPAD_BUTTON_R_STICK_X_MINMAX][1] - config_controller[device][CONTRPS_VPAD_BUTTON_R_STICK_X_MINMAX][0])>>1; + if(config_controller[device][CONTRPS_VPAD_BUTTON_R_STICK_X_INVERT][1] != 0x01){ + buffer->rstick.x += (value / (1.0*range)); + }else{ + buffer->rstick.x -= (value / (1.0*range)); + } + + value = (s8)(src[config_controller[device][CONTRPS_VPAD_BUTTON_R_STICK_Y][0]] - config_controller[device][CONTRPS_VPAD_BUTTON_R_STICK_Y][1]); + range = (config_controller[device][CONTRPS_VPAD_BUTTON_R_STICK_Y_MINMAX][1] - config_controller[device][CONTRPS_VPAD_BUTTON_R_STICK_Y_MINMAX][0])>>1; + if(config_controller[device][CONTRPS_VPAD_BUTTON_R_STICK_Y_INVERT][1] != 0x01){ + buffer->rstick.y += (value / (1.0*range)); + }else{ + buffer->rstick.y -= (value / (1.0*range)); + } + + changed = 1; + + } + } + if(changed){ + if(buffer->rstick.x > 1.0) buffer->rstick.x = 1.0; + if(buffer->rstick.y > 1.0) buffer->rstick.y = 1.0; + if(buffer->rstick.x < -1.0) buffer->rstick.x = -1.0; + if(buffer->rstick.y < -1.0) buffer->rstick.y = -1.0; + } + //log_printf("LX %f LY %f RX %f RY %f\n",buffer->lstick.x,buffer->lstick.y,buffer->rstick.x,buffer->rstick.y); +} + +int wasInKeyboard(unsigned char * src,int key){ + for(int i = 0;i 1){ + break; + }else if (src[i] == key){ + return 1; + } + } + return 0; +} + + +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//! Mouse +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +#define MOUSE_XY_TYPE_A 0x01 //1 byte x, 1 byte y +#define MOUSE_XY_TYPE_B 0x02 //1 byte x, 2 byte y + +void my_ms_read_cb(unsigned int handle, int error, unsigned char *buf, unsigned int bytes_transfered, void *p_user) +{ + if(error == 0) + { + + int type = MOUSE_XY_TYPE_A; + int click_offset = 0; + int xy_offset = 1; + + if(bytes_transfered == 5){ + type = MOUSE_XY_TYPE_B; + click_offset = 0; + xy_offset = 1; + }else if(bytes_transfered == 6){ + type = MOUSE_XY_TYPE_A; + xy_offset = 2; + click_offset = 1; + }else if(bytes_transfered == 7){ + type = MOUSE_XY_TYPE_B; + xy_offset = 2; + click_offset = 1; + }else if(bytes_transfered == 8){ + type = MOUSE_XY_TYPE_B; + xy_offset = 3; + click_offset = 1; + } + + my_cb_user *usr = (my_cb_user*)p_user; + + gHID_Mouse.pad_data[0].data[0].left_click = buf[click_offset]; + gHID_Mouse.pad_data[0].data[0].right_click = buf[click_offset]>>1; + + s8 x_value = 0; + s8 y_value = 0; + + if(type == MOUSE_XY_TYPE_A){ + x_value = buf[xy_offset]; + y_value = buf[xy_offset+1]; + }else if(type == MOUSE_XY_TYPE_B){ + x_value = buf[xy_offset]; + + y_value = ((buf[xy_offset+1] & 0xF0) >>4) | ((buf[xy_offset+2] & 0x0F) <<4); + } + + gHID_Mouse.pad_data[0].data[0].X += x_value; + gHID_Mouse.pad_data[0].data[0].deltaX = x_value; + + gHID_Mouse.pad_data[0].data[0].Y += y_value; + gHID_Mouse.pad_data[0].data[0].deltaY = y_value; + + + if(gHID_Mouse.pad_data[0].data[0].X < 0) gHID_Mouse.pad_data[0].data[0].X = 0; + if(gHID_Mouse.pad_data[0].data[0].X > 1280) gHID_Mouse.pad_data[0].data[0].X = 1280; + + if(gHID_Mouse.pad_data[0].data[0].Y < 0) gHID_Mouse.pad_data[0].data[0].Y = 0; + if(gHID_Mouse.pad_data[0].data[0].Y > 720) gHID_Mouse.pad_data[0].data[0].Y = 720; + + log_printf("%02X %02X %02X %02X %02X %02X %02X %02X %d = X: %d Y: %d \n",buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7],bytes_transfered,x_value,y_value); + + + HIDRead(handle, usr->buf, bytes_transfered, my_ms_read_cb, usr); + } +} + + +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//! GC-Adapter +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +void my_read_cb(unsigned int handle, int error, unsigned char *buf, unsigned int bytes_transfered, void *p_user) +{ + if(error == 0 && p_user != NULL) + { + my_cb_user *usr = (my_cb_user*)p_user; + + if(usr->hid == HID_LIST_GC){ + //log_printf("GC: %02X %02X %02X %02X %02X %02X %02X %02X \n",buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7]); + + memcpy(&(((HID_Data *)config_controller_data_ptr[CONTRPD_GC][0])->hid_data),&buf[1],9); + memcpy(&(((HID_Data *)config_controller_data_ptr[CONTRPD_GC][1])->hid_data),&buf[10],9); + memcpy(&(((HID_Data *)config_controller_data_ptr[CONTRPD_GC][2])->hid_data),&buf[19],9); + memcpy(&(((HID_Data *)config_controller_data_ptr[CONTRPD_GC][3])->hid_data),&buf[29],9); + + HIDRead(handle, usr->buf, bytes_transfered, my_read_cb, usr); + int pad = getActivePad(HID_LIST_GC); + if(((HID_Data *)config_controller_data_ptr[CONTRPD_GC][pad])->rumbleActive != usr->rumblestatus[pad]) + { + usr->rumblestatus[pad] = ((HID_Data *)config_controller_data_ptr[CONTRPD_GC][pad])->rumbleActive; + HIDGCRumble(handle,usr,usr->rumblestatus[pad],pad); + } + else + { + HIDRead(handle, usr->buf, bytes_transfered, my_read_cb, usr); + } + }else if(usr->hid != 0){ + int size = (HID_MAX_DATA_LENGTH_PER_PAD > bytes_transfered)? bytes_transfered : HID_MAX_DATA_LENGTH_PER_PAD; + memcpy(&(((HID_Data *)config_controller_data_ptr[usr->deviceSlot][0])->hid_data),&buf[0],size); + usleep(5000); //DS4 is way tooo fast + log_printf("HID %02X %02X %02X %02X %02X %02X %02X %02X \n",buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7]); + HIDRead(handle, usr->buf, bytes_transfered, my_read_cb, usr); + } + } +} + +void HIDGCRumble(unsigned int handle,my_cb_user *usr,int rumble, u32 pad) +{ + usr->buf[0] = 0x11; + usr->buf[1] = (pad+1) & rumble; usr->buf[2] = ((pad+1) >> 1) & rumble; + usr->buf[3] = ((pad+1) >> 2) & rumble; + usr->buf[4] = ((pad+1) >> 3) & rumble; + HIDWrite(handle, usr->buf, 5, my_gc_write_cb, usr); +} + +void my_gc_write_cb(unsigned int handle, int error, unsigned char *buf, unsigned int bytes_transfered, void *p_user) +{ + if(error == 0) + { + my_cb_user *usr = (my_cb_user*)p_user; + HIDRead(handle, usr->buf, usr->transfersize, my_read_cb, usr); + } +} diff --git a/controller_patcher.h b/controller_patcher.h new file mode 100644 index 0000000..1895a7c --- /dev/null +++ b/controller_patcher.h @@ -0,0 +1,408 @@ +#ifndef _CONTROLLER_PATCHER_H_ +#define _CONTROLLER_PATCHER_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "common/types.h" +#include "common/common.h" +#include "dynamic_libs/os_functions.h" +#include "dynamic_libs/syshid_functions.h" +#include "dynamic_libs/vpad_functions.h" + + +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//! Don't forget to change this data! +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +enum Controller_Patcher_Devices +{ + CONTRPD_GC, + CONTRPD_DS3, + CONTRPD_DS4, + CONTRPD_SP2600, + CONTRPD_KEYBOARD, + CONTRPD_PS2, + CONTRPD_MAX_VALUE +}; + +#define HID_LIST_GC 0x01 +#define HID_LIST_KEYBOARD 0x02 +#define HID_LIST_MOUSE 0x04 +#define HID_LIST_DS3 0x08 +#define HID_LIST_DS4 0x10 +#define HID_LIST_SP2600 0x20 +#define HID_LIST_PS2 0x40 + +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//! End +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +#define HID_MAX_DATA_LENGTH_PER_PAD 10 +#define HID_MAX_PADS_COUNT 4 + +typedef struct _HID_Data { + u8 rumbleActive; + u8 hid_data[HID_MAX_DATA_LENGTH_PER_PAD*2]; +} HID_Data; + +//! most data has the format: byte,value (byte starting at 0) +enum Controller_Patcher_Settings +{ + CONTRPS_VID, //! pid: 0x451d would be 0x45,0x1d + CONTRPS_PID, //! vid: 0x488d would be 0x48,0x8d + CONTRPS_BUF_SIZE, //! To invert: CONTROLLER_PATCHER_VALUE_SET, BUF_SIZE (default is 64) + CONTRPS_VPAD_BUTTON_A, + CONTRPS_VPAD_BUTTON_B, + CONTRPS_VPAD_BUTTON_X, + CONTRPS_VPAD_BUTTON_Y, + CONTRPS_DPAD_MODE, //! To set mode: CONTROLLER_PATCHER_VALUE_SET, Controller_Patcher_DPAD_MODE (default is normal mode) + /* Normal DPAD */ + CONTRPS_VPAD_BUTTON_LEFT, + CONTRPS_VPAD_BUTTON_RIGHT, + CONTRPS_VPAD_BUTTON_UP, + CONTRPS_VPAD_BUTTON_DOWN, + /* DPAD hat mode */ + CONTRPS_VPAD_BUTTON_DPAD_N, + CONTRPS_VPAD_BUTTON_DPAD_NE, + CONTRPS_VPAD_BUTTON_DPAD_E, + CONTRPS_VPAD_BUTTON_DPAD_SE, + CONTRPS_VPAD_BUTTON_DPAD_S, + CONTRPS_VPAD_BUTTON_DPAD_SW, + CONTRPS_VPAD_BUTTON_DPAD_W, + CONTRPS_VPAD_BUTTON_DPAD_NW, + CONTRPS_VPAD_BUTTON_DPAD_NEUTRAL, + /* */ + CONTRPS_VPAD_BUTTON_ZL, + CONTRPS_VPAD_BUTTON_ZR, + CONTRPS_VPAD_BUTTON_L, + CONTRPS_VPAD_BUTTON_R, + CONTRPS_VPAD_BUTTON_PLUS, + CONTRPS_VPAD_BUTTON_MINUS, + CONTRPS_VPAD_BUTTON_HOME, + CONTRPS_VPAD_BUTTON_SYNC, + CONTRPS_VPAD_BUTTON_STICK_R, + CONTRPS_VPAD_BUTTON_STICK_L, + CONTRPS_VPAD_BUTTON_L_STICK_X, //! byte, default value + CONTRPS_VPAD_BUTTON_L_STICK_X_INVERT, //! To invert: CONTROLLER_PATCHER_VALUE_SET, 0x01 + CONTRPS_VPAD_BUTTON_L_STICK_X_MINMAX, //! min,max + CONTRPS_VPAD_BUTTON_L_STICK_Y, //! byte, default value + CONTRPS_VPAD_BUTTON_L_STICK_Y_INVERT, //! To invert: CONTROLLER_PATCHER_VALUE_SET, 0x01 + CONTRPS_VPAD_BUTTON_L_STICK_Y_MINMAX, //! min,max + CONTRPS_VPAD_BUTTON_R_STICK_X, //! byte, default value + CONTRPS_VPAD_BUTTON_R_STICK_X_INVERT, //! To invert: CONTROLLER_PATCHER_VALUE_SET, 0x01 + CONTRPS_VPAD_BUTTON_R_STICK_X_MINMAX, //! min,max + CONTRPS_VPAD_BUTTON_R_STICK_Y, //! byte, default value + CONTRPS_VPAD_BUTTON_R_STICK_Y_INVERT, //! To invert: CONTROLLER_PATCHER_VALUE_SET, 0x01 + CONTRPS_VPAD_BUTTON_R_STICK_Y_MINMAX, //! min,max + CONTRPS_VPAD_BUTTON_TV, + CONTRPS_DOUBLE_USE, //!When used: e.g. CONTROLLER_PATCHER_VALUE_SET, CONTROLLER_PATCHER_GC_DOUBLE_USE + CONTRPS_DOUBLE_USE_BUTTON, + CONTRPS_PAD_COUNT, //! + CONTRPS_MAX_VALUE +}; + +enum Controller_Patcher_DPAD_MODE +{ + CONTRPDM_Normal, + CONTRPDM_Hat +}; + +#define CONTROLLER_PATCHER_VALUE_SET 0x01 +#define CONTROLLER_PATCHER_GC_DOUBLE_USE 0x01 +#define CONTROLLER_PATCHER_INVALIDVALUE 0xFF + +typedef struct _my_cb_user{ + unsigned char *buf; + unsigned int transfersize; + unsigned int rumblestatus[4]; + unsigned int handle; + unsigned int hid; + unsigned int deviceSlot; +}my_cb_user; + +typedef struct _HID_DEVICE_DATA { + my_cb_user * user_data; + HID_Data pad_data[HID_MAX_PADS_COUNT]; +} HID_DEVICE_DATA; + + + +typedef struct _HID_Data_Struct{ + unsigned char *src; + int hid; +}HID_Data_Struct; + +void setConfigValue(u8 * dest , u8 first, u8 second); +void init_config_controller(); +void deinit_config_controller(); + +void setControllerReleasePressData(HID_Data_Struct data_cur, HID_Data_Struct data_last,VPADData * buffer,int VPADButton); +void setControllerDataFromHID(VPADData * buffer,int hid); +void checkMouseMode(HID_Data_Struct data, HID_Data_Struct data_last); + +void my_read_cb(unsigned int handle, int error, unsigned char *buf, unsigned int bytes_transfered, void *p_user); + +int wasInKeyboard(unsigned char * src,int key); + +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//! GC-Adapter +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +//A config would be cleaner, but no motivation atm :P +#define HID_GC_BUTTON_A 0x01 +#define HID_GC_BUTTON_B 0x02 +#define HID_GC_BUTTON_X 0x04 +#define HID_GC_BUTTON_Y 0x08 +#define HID_GC_BUTTON_LEFT 0x10 +#define HID_GC_BUTTON_RIGHT 0x20 +#define HID_GC_BUTTON_DOWN 0x40 +#define HID_GC_BUTTON_UP 0x80 +#define HID_GC_BUTTON_START 0x01 +#define HID_GC_BUTTON_L 0x80 +#define HID_GC_BUTTON_R 0x80 +#define HID_GC_BUTTON_Z 0x02 + +#define HID_GC_PAD_COUNT 4 + +void HIDGCRumble(unsigned int handle,my_cb_user *usr,int rumble, u32 pad); +void my_gc_write_cb(unsigned int handle, int error, unsigned char *buf, unsigned int bytes_transfered, void *p_user); + +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//! DS3 +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +#define PS3_F4_REPORT_LEN 4 +#define PS3_F5_REPORT_LEN 8 +#define PS3_01_REPORT_LEN 48 +#define HID_REPORT_FEATURE 3 +#define HID_REPORT_OUTPUT 2 +#define PS3_F4_REPORT_ID 0xF4 +#define PS3_01_REPORT_ID 0x01 +#define PS3_F5_REPORT_ID 0xF5 + +#define HID_DS3_BUTTON_CROSS 0x40 // 3 +#define HID_DS3_BUTTON_CIRCLE 0x20 // 3 +#define HID_DS3_BUTTON_SQUARE 0x80 // 3 +#define HID_DS3_BUTTON_TRIANGLE 0x10 // 3 +#define HID_DS3_BUTTON_L1 0x04 // 3 +#define HID_DS3_BUTTON_L2 0x01 // 3 +#define HID_DS3_BUTTON_R1 0x08 // 3 +#define HID_DS3_BUTTON_R2 0x02 // 3 + +#define HID_DS3_BUTTON_L3 0x02 // 2 +#define HID_DS3_BUTTON_R3 0x04 // 2 +#define HID_DS3_BUTTON_SELECT 0x01 // 2 +#define HID_DS3_BUTTON_START 0x08 // 2 +#define HID_DS3_BUTTON_LEFT 0x80 // 2 +#define HID_DS3_BUTTON_RIGHT 0x20 // 2 +#define HID_DS3_BUTTON_UP 0x10 // 2 +#define HID_DS3_BUTTON_DOWN 0x40 // 2 +#define HID_DS3_BUTTON_GUIDE 0x01 // 4 + +#define HID_DS3_PAD_COUNT 1 + +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//! DS4 +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +#define HID_DS4_BUTTON_CROSS 0x20 // 5 +#define HID_DS4_BUTTON_CIRCLE 0x40 // 5 +#define HID_DS4_BUTTON_SQUARE 0x10 // 5 +#define HID_DS4_BUTTON_TRIANGLE 0x80 // 5 +#define HID_DS4_BUTTON_L1 0x01 // 6 +#define HID_DS4_BUTTON_L2 0x04 // 6 +#define HID_DS4_BUTTON_L3 0x40 // 6 +#define HID_DS4_BUTTON_R1 0x02 // 6 +#define HID_DS4_BUTTON_R2 0x08 // 6 +#define HID_DS4_BUTTON_R3 0x80 // 6 +#define HID_DS4_BUTTON_SHARE 0x10 // 6 +#define HID_DS4_BUTTON_OPTIONS 0x20 // 6 +#define HID_DS4_BUTTON_DPAD_N 0x00 // 5 +#define HID_DS4_BUTTON_DPAD_NE 0x01 // 5 +#define HID_DS4_BUTTON_DPAD_E 0x02 // 5 +#define HID_DS4_BUTTON_DPAD_SE 0x03 // 5 +#define HID_DS4_BUTTON_DPAD_S 0x04 // 5 +#define HID_DS4_BUTTON_DPAD_SW 0x05 // 5 +#define HID_DS4_BUTTON_DPAD_W 0x06 // 5 +#define HID_DS4_BUTTON_DPAD_NW 0x07 // 5 +#define HID_DS4_BUTTON_DPAD_NEUTRAL 0x08 // 5 +#define HID_DS4_BUTTON_GUIDE 0x01 // 7 +#define HID_DS4_BUTTON_T_PAD_CLICK 0x02 // 7 + +#define HID_DS4_PAD_COUNT 1 + + +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//! PS2 +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +#define HID_PS2_BUTTON_CROSS 0x40 // 5 +#define HID_PS2_BUTTON_CIRCLE 0x20 // 5 +#define HID_PS2_BUTTON_SQUARE 0x80 // 5 +#define HID_PS2_BUTTON_TRIANGLE 0x10 // 5 + +#define HID_PS2_BUTTON_DPAD_N 0x00 // 5 +#define HID_PS2_BUTTON_DPAD_NE 0x01 // 5 +#define HID_PS2_BUTTON_DPAD_E 0x02 // 5 +#define HID_PS2_BUTTON_DPAD_SE 0x03 // 5 +#define HID_PS2_BUTTON_DPAD_S 0x04 // 5 +#define HID_PS2_BUTTON_DPAD_SW 0x05 // 5 +#define HID_PS2_BUTTON_DPAD_W 0x06 // 5 +#define HID_PS2_BUTTON_DPAD_NW 0x07 // 5 +#define HID_PS2_BUTTON_DPAD_NEUTRAL 0x0F // 5 + +#define HID_PS2_BUTTON_L1 0x04 // 6 +#define HID_PS2_BUTTON_L2 0x01 // 6 +#define HID_PS2_BUTTON_R1 0x08 // 6 +#define HID_PS2_BUTTON_R2 0x02 // 6 +#define HID_PS2_BUTTON_START 0x20 // 6 +#define HID_PS2_BUTTON_SELECT 0x10 // 6 + +#define HID_PS2_PAD_COUNT 1 + +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//! Saitek P2600 +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +#define HID_SP2600_BUTTON_1 0x01 // 5 +#define HID_SP2600_BUTTON_2 0x02 // 5 +#define HID_SP2600_BUTTON_3 0x04 // 5 +#define HID_SP2600_BUTTON_4 0x08 // 5 + +#define HID_SP2600_BUTTON_DPAD_N 0x00 // 5 +#define HID_SP2600_BUTTON_DPAD_NE 0x10 // 5 +#define HID_SP2600_BUTTON_DPAD_E 0x20 // 5 +#define HID_SP2600_BUTTON_DPAD_SE 0x30 // 5 +#define HID_SP2600_BUTTON_DPAD_S 0x40 // 5 +#define HID_SP2600_BUTTON_DPAD_SW 0x50 // 5 +#define HID_SP2600_BUTTON_DPAD_W 0x60 // 5 +#define HID_SP2600_BUTTON_DPAD_NW 0x70 // 5 +#define HID_SP2600_BUTTON_DPAD_NEUTRAL 0xF0 // 5 + +#define HID_SP2600_BUTTON_5 0x10 // 5 //L upper +#define HID_SP2600_BUTTON_6 0x20 // 5 //R upper +#define HID_SP2600_BUTTON_7 0x40 // 5 //L lower +#define HID_SP2600_BUTTON_8 0x80 // 5 //R lower +#define HID_SP2600_BUTTON_WHITE 0x01 // 6 +#define HID_SP2600_BUTTON_BLACK 0x02 // 6 +#define HID_SP2600_BUTTON_FPS 0x04 // 6 +#define HID_SP2600_BUTTON_Analog 0x08 // 6 + +#define HID_SP2600_PAD_COUNT 1 +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//! Keyboard +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +#define HID_KEYBOARD_BUTTON_A 0x04 +#define HID_KEYBOARD_BUTTON_B 0x05 +#define HID_KEYBOARD_BUTTON_C 0x06 +#define HID_KEYBOARD_BUTTON_D 0x07 +#define HID_KEYBOARD_BUTTON_E 0x08 +#define HID_KEYBOARD_BUTTON_F 0x09 +#define HID_KEYBOARD_BUTTON_G 0x0A +#define HID_KEYBOARD_BUTTON_H 0x0B +#define HID_KEYBOARD_BUTTON_I 0x0C +#define HID_KEYBOARD_BUTTON_J 0x0D +#define HID_KEYBOARD_BUTTON_K 0x0E +#define HID_KEYBOARD_BUTTON_L 0x0F +#define HID_KEYBOARD_BUTTON_M 0x10 +#define HID_KEYBOARD_BUTTON_N 0x11 +#define HID_KEYBOARD_BUTTON_O 0x12 +#define HID_KEYBOARD_BUTTON_P 0x13 +#define HID_KEYBOARD_BUTTON_Q 0x14 +#define HID_KEYBOARD_BUTTON_R 0x15 +#define HID_KEYBOARD_BUTTON_S 0x16 +#define HID_KEYBOARD_BUTTON_T 0x17 +#define HID_KEYBOARD_BUTTON_U 0x18 +#define HID_KEYBOARD_BUTTON_V 0x19 +#define HID_KEYBOARD_BUTTON_W 0x1A +#define HID_KEYBOARD_BUTTON_X 0x1B +#define HID_KEYBOARD_BUTTON_Y 0x1C +#define HID_KEYBOARD_BUTTON_Z 0x1D +#define HID_KEYBOARD_BUTTON_F1 0x3A +#define HID_KEYBOARD_BUTTON_F2 0x3B +#define HID_KEYBOARD_BUTTON_F3 0x3C +#define HID_KEYBOARD_BUTTON_F4 0x3D +#define HID_KEYBOARD_BUTTON_F5 0x3E +#define HID_KEYBOARD_BUTTON_F6 0x3F +#define HID_KEYBOARD_BUTTON_F7 0x40 +#define HID_KEYBOARD_BUTTON_F8 0x41 +#define HID_KEYBOARD_BUTTON_F9 0x42 +#define HID_KEYBOARD_BUTTON_F10 0x43 +#define HID_KEYBOARD_BUTTON_F11 0x44 +#define HID_KEYBOARD_BUTTON_F12 0x45 +#define HID_KEYBOARD_BUTTON_LEFT 0x50 +#define HID_KEYBOARD_BUTTON_RIGHT 0x4f +#define HID_KEYBOARD_BUTTON_DOWN 0x51 +#define HID_KEYBOARD_BUTTON_UP 0x52 +#define HID_KEYBOARD_BUTTON_ENTER 0x28 +#define HID_KEYBOARD_BUTTON_MINUS 0x56 +#define HID_KEYBOARD_BUTTON_PLUS 0x57 +#define HID_KEYBOARD_BUTTON_SPACE 0x2C +#define HID_KEYBOARD_BUTTON_SHIFT 0x02 + +#define HID_KEYBOARD_PAD_COUNT 1 +#define HID_KEYBOARD_DATA_LENGTH 8 + +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//! Mouse +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +#define HID_MOUSE_BUTTON_LEFTCLICK 0x04 +#define HID_MOUSE_BUTTON_RIGHTCLICK 0x05 + + +#define HID_MOUSE_PAD_COUNT 1 + +#define HID_MOUSE_MODE_AIM 0x01 +#define HID_MOUSE_MODE_TOUCH 0x02 + +struct _HID_Mouse_Data { + u8 left_click; + u8 right_click; + s16 X; + s16 Y; + s16 deltaX; + s16 deltaY; +} __attribute__ ((packed)); + +typedef struct _HID_Mouse_Data HID_Mouse_Data; + +typedef struct _HID_Mouse_Data_c { + HID_Mouse_Data data[2]; +} HID_Mouse_Data_c; + +typedef struct _HID_Mouse { + my_cb_user * user_data; + HID_Mouse_Data_c pad_data[HID_MOUSE_PAD_COUNT]; +} HID_Mouse; + +void my_ms_read_cb(unsigned int handle, int error, unsigned char *buf, unsigned int bytes_transfered, void *p_user); +void my_ms_write_cb(unsigned int handle, int error, unsigned char *buf, unsigned int bytes_transfered, void *p_user); +void my_ms_report_cb(unsigned int handle, int error, unsigned char *buf, unsigned int bytes_transfered, void *p_user); + +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//! Shared +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + + +int my_attach_cb(HIDClient *p_client, HIDDevice *p_device, unsigned int attach); + +int getPadCount(int hid); +HID_Data_Struct * getLastData(HID_Data_Struct data); +int getButtonPressed(HID_Data_Struct data, int VPADButton); +void setLastData(HID_Data_Struct data,HID_Data_Struct data_last); +int getActivePad(int hid); +void setRumble(int hid,int rumble); +void convertAnalogSticks(HID_Data_Struct data,VPADData * buffer); +void setTouch(HID_Data_Struct data,VPADData * buffer); + +HID_Data_Struct * getHIDDataAll(int hid,int * size); + +#ifdef __cplusplus +} +#endif + +#endif /* _CONTROLLER_PATCHER_H_ */ diff --git a/cp_retain_vars.c b/cp_retain_vars.c new file mode 100644 index 0000000..1665b58 --- /dev/null +++ b/cp_retain_vars.c @@ -0,0 +1,21 @@ +#include +#include "dynamic_libs/syshid_functions.h" +#include "controller_patcher/controller_patcher.h" + +u8 config_done __attribute__((section(".data"))) = 0; + +u8 gHIDSetupDone __attribute__((section(".data"))) = 0; +u8 gHIDAttached __attribute__((section(".data"))) = 0; +u8 gHIDCurrentDevice __attribute__((section(".data"))) = 0; + +HIDClient gHIDClient __attribute__((section(".data"))); + +HID_DEVICE_DATA gHID_Devices[CONTRPD_MAX_VALUE] __attribute__((section(".data"))); + +HID_Mouse gHID_Mouse __attribute__((section(".data"))); + +u8 gHID_Mouse_Mode __attribute__((section(".data"))) = HID_MOUSE_MODE_TOUCH; + +u8 config_controller[CONTRPD_MAX_VALUE][CONTRPS_MAX_VALUE][2] __attribute__((section(".data"))); +s8 config_controller_list[CONTRPD_MAX_VALUE] __attribute__((section(".data"))); +u32 config_controller_data_ptr[CONTRPD_MAX_VALUE][4] __attribute__((section(".data"))); diff --git a/cp_retain_vars.h b/cp_retain_vars.h new file mode 100644 index 0000000..f737016 --- /dev/null +++ b/cp_retain_vars.h @@ -0,0 +1,24 @@ +#ifndef CP_RETAINS_VARS_H_ +#define CP_RETAINS_VARS_H_ + +#include "dynamic_libs/syshid_functions.h" +#include "controller_patcher/controller_patcher.h" + +extern u8 config_done; + +extern u8 gHIDSetupDone; +extern u8 gHIDAttached; +extern u8 gHIDCurrentDevice; +extern HIDClient gHIDClient; + +extern HID_DEVICE_DATA gHID_Devices[CONTRPD_MAX_VALUE]; + +extern HID_Mouse gHID_Mouse; + +extern u8 gHID_Mouse_Mode; + +extern u8 config_controller[CONTRPD_MAX_VALUE][CONTRPS_MAX_VALUE][2]; +extern s8 config_controller_list[CONTRPD_MAX_VALUE]; +extern u32 config_controller_data_ptr[CONTRPD_MAX_VALUE][4]; //currently max per device + +#endif // CP_RETAINS_VARS_H_