2017-03-30 17:53:36 +02:00
/****************************************************************************
* Copyright ( C ) 2016 , 2017 Maschell
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2017-05-07 14:44:09 +02:00
# include "ControllerPatcherHID.hpp"
2017-03-30 17:53:36 +02:00
# include <malloc.h>
# include <stdio.h>
# include <string.h>
2017-05-07 14:44:09 +02:00
2017-10-29 09:34:47 +01:00
# include <utils/logger.h>
2017-03-30 17:53:36 +02:00
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
* public implementation for the network controller
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2018-06-20 15:07:15 +02:00
int32_t ControllerPatcherHID : : externAttachDetachCallback ( HIDDevice * p_device , HIDAttachEvent attach ) {
2017-03-30 17:53:36 +02:00
HIDClient client ;
memset ( & client , 0 , sizeof ( client ) ) ;
return AttachDetachCallback ( & client , p_device , attach ) ;
}
2018-06-20 15:07:15 +02:00
void ControllerPatcherHID : : externHIDReadCallback ( uint32_t handle , unsigned char * buf , uint32_t bytes_transfered , my_cb_user * usr ) {
2017-03-30 17:53:36 +02:00
HIDReadCallback ( handle , buf , bytes_transfered , usr ) ;
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
* private implementation for the HID Api .
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2018-06-20 15:07:15 +02:00
int32_t ControllerPatcherHID : : myAttachDetachCallback ( HIDClient * p_client , HIDDevice * p_device , HIDAttachEvent attach ) {
2017-03-30 17:53:36 +02:00
return AttachDetachCallback ( p_client , p_device , attach ) ;
}
2018-06-20 15:07:15 +02:00
void ControllerPatcherHID : : myHIDMouseReadCallback ( uint32_t handle , int32_t error , unsigned char * buf , uint32_t bytes_transfered , void * p_user ) {
2018-06-19 17:46:37 +02:00
if ( error = = 0 ) {
2017-03-30 17:53:36 +02:00
my_cb_user * usr = ( my_cb_user * ) p_user ;
2018-06-20 15:07:15 +02:00
uint32_t slot = 0 ;
2018-06-19 17:46:37 +02:00
if ( usr - > pad_slot < HID_MAX_PADS_COUNT ) {
2017-03-30 17:53:36 +02:00
slot = usr - > pad_slot ;
}
HID_Data * data_ptr = & ( gHID_Devices [ usr - > slotdata . deviceslot ] . pad_data [ slot ] ) ;
HID_Mouse_Data * cur_mouse_data = & data_ptr - > data_union . mouse . cur_mouse_data ;
data_ptr - > type = DEVICE_TYPE_MOUSE ;
2017-10-14 13:22:47 +02:00
//DEBUG_FUNCTION_LINE("%02X %02X %02X %02X %02X bytes_transfered: %d\n",buf[0],buf[1],buf[2],buf[3],buf[4],bytes_transfered);
2017-03-30 17:53:36 +02:00
2018-06-19 17:46:37 +02:00
if ( buf [ 0 ] = = 2 & & bytes_transfered > 3 ) { // using the other mouse mode
2017-03-30 17:53:36 +02:00
buf + = 1 ;
}
2018-06-20 15:07:15 +02:00
int8_t x_value = 0 ;
int8_t y_value = 0 ;
2017-03-30 17:53:36 +02:00
x_value = buf [ 1 ] ;
y_value = buf [ 2 ] ;
cur_mouse_data - > X + = x_value ;
cur_mouse_data - > deltaX = x_value ;
cur_mouse_data - > Y + = y_value ;
cur_mouse_data - > deltaY = y_value ;
cur_mouse_data - > left_click = buf [ 0 ] ;
2018-06-19 17:46:37 +02:00
cur_mouse_data - > right_click = buf [ 0 ] > > 1 ;
2017-03-30 17:53:36 +02:00
if ( cur_mouse_data - > X < 0 ) cur_mouse_data - > X = 0 ;
if ( cur_mouse_data - > X > 1280 ) cur_mouse_data - > X = 1280 ;
if ( cur_mouse_data - > Y < 0 ) cur_mouse_data - > Y = 0 ;
if ( cur_mouse_data - > Y > 720 ) cur_mouse_data - > Y = 720 ;
cur_mouse_data - > valuedChanged = 1 ;
2017-10-14 13:22:47 +02:00
//DEBUG_FUNCTION_LINE("%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);
2017-03-30 17:53:36 +02:00
HIDRead ( handle , usr - > buf , bytes_transfered , myHIDMouseReadCallback , usr ) ;
2018-06-19 17:46:37 +02:00
}
2017-03-30 17:53:36 +02:00
}
2018-06-20 15:07:15 +02:00
void ControllerPatcherHID : : myHIDReadCallback ( uint32_t handle , int32_t error , unsigned char * buf , uint32_t bytes_transfered , void * p_user ) {
2018-06-19 17:46:37 +02:00
if ( error = = 0 & & p_user ! = NULL & & gHIDAttached ) {
my_cb_user * usr = ( my_cb_user * ) p_user ;
2017-03-30 17:53:36 +02:00
2018-06-19 17:46:37 +02:00
HIDReadCallback ( handle , buf , bytes_transfered , usr ) ;
2017-04-23 15:19:21 +02:00
2018-06-19 17:46:37 +02:00
if ( usr - > slotdata . hidmask = = gHID_LIST_DS4 ) {
OSSleepTicks ( OSMicrosecondsToTicks ( 2000 ) ) ; //DS4 is way tooo fast. sleeping to reduce lag. (need to check the other pads)
}
2017-04-23 15:19:21 +02:00
HIDRead ( handle , usr - > buf , bytes_transfered , myHIDReadCallback , usr ) ;
2018-06-19 17:46:37 +02:00
}
2017-03-30 17:53:36 +02:00
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
* Intern Callback actions
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2018-06-20 15:07:15 +02:00
int32_t ControllerPatcherHID : : AttachDetachCallback ( HIDClient * p_client , HIDDevice * p_device , HIDAttachEvent attach ) {
2018-06-19 17:46:37 +02:00
if ( attach ) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE ( " vid %04x pid %04x connected " , SWAP16 ( p_device - > vid ) , SWAP16 ( p_device - > pid ) ) ;
2018-06-19 17:46:37 +02:00
if ( HID_DEBUG ) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE ( " interface index %02x " , p_device - > interfaceIndex ) ;
DEBUG_FUNCTION_LINE ( " sub class %02x " , p_device - > subClass ) ;
DEBUG_FUNCTION_LINE ( " protocol %02x " , p_device - > protocol ) ;
DEBUG_FUNCTION_LINE ( " max packet in %02x " , p_device - > maxPacketSizeRx ) ;
DEBUG_FUNCTION_LINE ( " max packet out %02x " , p_device - > maxPacketSizeRx ) ;
2018-06-19 17:46:37 +02:00
}
2017-03-30 17:53:36 +02:00
}
2018-06-19 17:46:37 +02:00
if ( ! attach ) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE ( " vid %04x pid %04x disconnected " , SWAP16 ( p_device - > vid ) , SWAP16 ( p_device - > pid ) ) ;
2017-03-30 17:53:36 +02:00
}
DeviceInfo device_info ;
memset ( & device_info , 0 , sizeof ( DeviceInfo ) ) ;
device_info . slotdata . deviceslot = - 1 ;
device_info . vidpid . vid = SWAP16 ( p_device - > vid ) ;
device_info . vidpid . pid = SWAP16 ( p_device - > pid ) ;
HIDSlotData * slotdata = & ( device_info . slotdata ) ;
2018-06-19 17:40:02 +02:00
if ( ( p_device - > subClass = = 1 ) & & ( p_device - > protocol = = 1 ) ) { //Keyboard
2017-03-30 17:53:36 +02:00
slotdata - > hidmask = gHID_LIST_KEYBOARD ;
slotdata - > deviceslot = gHID_SLOT_KEYBOARD ;
2020-12-16 02:04:31 +01:00
//DEBUG_FUNCTION_LINE("Found Keyboard: device: %s slot: %d",byte_to_binary(device_info.slotdata.hidmask),device_info.slotdata.deviceslot);
2018-06-19 17:46:37 +02:00
} else if ( ( p_device - > subClass = = 1 ) & & ( p_device - > protocol = = 2 ) ) { // MOUSE
2017-03-30 17:53:36 +02:00
slotdata - > hidmask = gHID_LIST_MOUSE ;
slotdata - > deviceslot = gMouseSlot ;
2020-12-16 02:04:31 +01:00
//DEBUG_FUNCTION_LINE("Found Mouse: device: %s slot: %d",byte_to_binary(device_info.hid),device_info.slot);
2018-06-19 17:46:37 +02:00
} else {
2018-06-20 15:07:15 +02:00
int32_t ret ;
2018-06-19 17:46:37 +02:00
if ( ( ret = ControllerPatcherUtils : : getDeviceInfoFromVidPid ( & device_info ) ) < 0 ) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE ( " ControllerPatcherUtils::getDeviceInfoFromVidPid(&device_info) failed %d " , ret ) ;
2017-03-30 17:53:36 +02:00
return HID_DEVICE_DETACH ;
2018-06-19 17:46:37 +02:00
} else {
2020-12-16 02:04:31 +01:00
//DEBUG_FUNCTION_LINE("ControllerPatcherUtils::getDeviceInfoFromVidPid(&device_info) success %d ",ret);
2017-03-30 17:53:36 +02:00
}
}
2018-06-19 17:46:37 +02:00
if ( slotdata - > hidmask ) {
if ( attach ) {
2018-06-20 15:07:15 +02:00
int32_t bufSize = 64 ;
2018-06-19 17:46:37 +02:00
if ( slotdata - > hidmask ! = gHID_LIST_MOUSE & & config_controller [ slotdata - > deviceslot ] [ CONTRPS_BUF_SIZE ] [ 0 ] = = CONTROLLER_PATCHER_VALUE_SET ) {
2017-03-30 17:53:36 +02:00
bufSize = config_controller [ slotdata - > deviceslot ] [ CONTRPS_BUF_SIZE ] [ 1 ] ;
}
unsigned char * buf = ( unsigned char * ) memalign ( 64 , bufSize ) ;
memset ( buf , 0 , bufSize ) ;
my_cb_user * usr = ( my_cb_user * ) memalign ( 64 , sizeof ( my_cb_user ) ) ;
usr - > buf = buf ;
usr - > slotdata = device_info . slotdata ;
2018-06-19 17:40:02 +02:00
usr - > transfersize = p_device - > maxPacketSizeRx ;
2017-03-30 17:53:36 +02:00
usr - > handle = p_device - > handle ;
gHIDAttached | = slotdata - > hidmask ;
gHIDCurrentDevice | = slotdata - > hidmask ;
2018-06-20 15:07:15 +02:00
int32_t pads_per_device = 1 ;
2018-06-19 17:46:37 +02:00
if ( config_controller [ slotdata - > deviceslot ] [ CONTRPS_PAD_COUNT ] [ 0 ] ! = CONTROLLER_PATCHER_INVALIDVALUE ) {
2017-03-30 17:53:36 +02:00
pads_per_device = config_controller [ slotdata - > deviceslot ] [ CONTRPS_PAD_COUNT ] [ 1 ] ;
2018-06-19 17:46:37 +02:00
if ( pads_per_device > HID_MAX_PADS_COUNT ) { //maximum of HID_MAX_PADS_COUNT
2017-03-30 17:53:36 +02:00
pads_per_device = HID_MAX_PADS_COUNT ;
}
}
2018-06-20 15:07:15 +02:00
int32_t pad_count = config_controller [ slotdata - > deviceslot ] [ CONTRPS_CONNECTED_PADS ] [ 1 ] ;
2017-04-23 15:19:21 +02:00
if ( pad_count > 0x0F ) pad_count = 0 ; //???
2017-03-30 17:53:36 +02:00
2018-06-20 15:07:15 +02:00
int32_t pad_slot = 0 ;
2017-03-30 17:53:36 +02:00
2018-06-20 15:07:15 +02:00
int32_t failed = 1 ;
2017-03-30 17:53:36 +02:00
2018-06-20 15:07:15 +02:00
for ( int32_t i = 0 ; i < HID_MAX_PADS_COUNT ; i + = pads_per_device ) {
2018-06-19 17:46:37 +02:00
if ( ! ( pad_count & ( 1 < < i ) ) ) {
2017-03-30 17:53:36 +02:00
failed = 0 ;
pad_count | = ( 1 < < i ) ;
pad_slot = i ;
break ;
}
}
2018-06-19 17:46:37 +02:00
if ( failed ) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE ( " error: I can only handle %d devices of the same type. Sorry " , HID_MAX_PADS_COUNT ) ;
2018-06-19 17:46:37 +02:00
if ( buf ) {
2017-03-30 17:53:36 +02:00
free ( buf ) ;
buf = NULL ;
}
2018-06-19 17:46:37 +02:00
if ( usr ) {
2017-03-30 17:53:36 +02:00
free ( usr ) ;
usr = NULL ;
}
return 0 ;
}
config_controller [ slotdata - > deviceslot ] [ CONTRPS_CONNECTED_PADS ] [ 1 ] = pad_count ;
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 ] ) ) ;
usr - > pads_per_device = pads_per_device ;
usr - > pad_slot = pad_slot ;
2018-06-20 15:07:15 +02:00
for ( int32_t i = 0 ; i < pads_per_device ; i + + ) {
2017-03-30 17:53:36 +02:00
memset ( & gHID_Devices [ slotdata - > deviceslot ] . pad_data [ pad_slot + i ] , 0 , sizeof ( HID_Data ) ) ;
gHID_Devices [ slotdata - > deviceslot ] . pad_data [ pad_slot + i ] . handle = p_device - > handle ;
2020-12-16 02:04:31 +01:00
//DEBUG_FUNCTION_LINE("saved handle %d to slot %d and pad %d",p_device->handle,slotdata->deviceslot,pad_slot);
2017-03-30 17:53:36 +02:00
gHID_Devices [ slotdata - > deviceslot ] . pad_data [ pad_slot + i ] . user_data = usr ;
gHID_Devices [ slotdata - > deviceslot ] . pad_data [ pad_slot + i ] . slotdata = device_info . slotdata ;
DCFlushRange ( & gHID_Devices [ slotdata - > deviceslot ] . pad_data [ pad_slot + i ] , sizeof ( HID_Data ) ) ;
DCInvalidateRange ( & gHID_Devices [ slotdata - > deviceslot ] . pad_data [ pad_slot + i ] , sizeof ( HID_Data ) ) ;
}
2018-06-19 17:46:37 +02:00
if ( HID_DEBUG ) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE ( " Device successfully attached " ) ;
2018-06-19 17:46:37 +02:00
}
2017-03-30 17:53:36 +02:00
2018-06-19 17:46:37 +02:00
if ( slotdata - > hidmask = = gHID_LIST_GC ) { // GC PAD
2017-03-30 17:53:36 +02:00
//The GC Adapter has all ports in one device. Set them all.
gHID_Devices [ slotdata - > deviceslot ] . pad_data [ 0 ] . slotdata = device_info . slotdata ;
gHID_Devices [ slotdata - > deviceslot ] . pad_data [ 1 ] . slotdata = device_info . slotdata ;
gHID_Devices [ slotdata - > deviceslot ] . pad_data [ 2 ] . slotdata = device_info . slotdata ;
gHID_Devices [ slotdata - > deviceslot ] . pad_data [ 3 ] . slotdata = device_info . slotdata ;
buf [ 0 ] = 0x13 ;
HIDWrite ( p_device - > handle , usr - > buf , 1 , NULL , NULL ) ;
HIDRead ( p_device - > handle , usr - > buf , usr - > transfersize , myHIDReadCallback , usr ) ;
2018-06-19 17:46:37 +02:00
} else if ( slotdata - > hidmask = = gHID_LIST_MOUSE ) {
2018-06-19 17:40:02 +02:00
HIDSetProtocol ( p_device - > handle , p_device - > interfaceIndex , 0 , 0 , 0 ) ;
2017-03-30 17:53:36 +02:00
//HIDGetDescriptor(p_device->handle,0x22,0x00,0,my_buf,512,my_foo_cb,NULL);
2018-06-19 17:40:02 +02:00
HIDSetIdle ( p_device - > handle , p_device - > interfaceIndex , 1 , NULL , NULL ) ;
2017-03-30 17:53:36 +02:00
gHID_Mouse_Mode = HID_MOUSE_MODE_AIM ;
2018-06-19 17:40:02 +02:00
HIDRead ( p_device - > handle , buf , p_device - > maxPacketSizeRx , myHIDMouseReadCallback , usr ) ;
2018-06-19 17:46:37 +02:00
} else if ( slotdata - > hidmask = = gHID_LIST_SWITCH_PRO ) {
2018-06-20 15:07:15 +02:00
int32_t read_result = HIDRead ( p_device - > handle , usr - > buf , usr - > transfersize , NULL , NULL ) ;
2018-06-19 17:46:37 +02:00
if ( read_result = = 64 ) {
if ( usr - > buf [ 01 ] = = 0x01 ) { //We need to do the handshake
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE ( " Switch Pro Controller handshake needed " ) ;
2017-04-23 12:18:36 +02:00
/**
Thanks to ShinyQuagsire23 for the values ( https : //github.com/shinyquagsire23/HID-Joy-Con-Whispering)
* */
//Get MAC
buf [ 0 ] = 0x80 ;
buf [ 1 ] = 0x01 ;
HIDWrite ( p_device - > handle , usr - > buf , 2 , NULL , NULL ) ;
HIDRead ( p_device - > handle , usr - > buf , usr - > transfersize , NULL , NULL ) ;
//Do handshake
buf [ 0 ] = 0x80 ;
buf [ 1 ] = 0x02 ;
HIDWrite ( p_device - > handle , usr - > buf , 2 , NULL , NULL ) ;
HIDRead ( p_device - > handle , usr - > buf , usr - > transfersize , NULL , NULL ) ;
//Talk over HID only.
buf [ 0 ] = 0x80 ;
buf [ 1 ] = 0x04 ;
HIDWrite ( p_device - > handle , usr - > buf , 2 , NULL , NULL ) ;
HIDRead ( p_device - > handle , usr - > buf , usr - > transfersize , NULL , NULL ) ;
2018-06-19 17:46:37 +02:00
} else {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE ( " Switch Pro Controller handshake already done " ) ;
2017-04-23 12:18:36 +02:00
}
HIDRead ( p_device - > handle , usr - > buf , usr - > transfersize , myHIDReadCallback , usr ) ;
}
2018-06-19 17:46:37 +02:00
} else if ( slotdata - > hidmask = = gHID_LIST_KEYBOARD ) {
2018-06-19 17:40:02 +02:00
HIDSetProtocol ( p_device - > handle , p_device - > interfaceIndex , 1 , 0 , 0 ) ;
HIDSetIdle ( p_device - > handle , p_device - > interfaceIndex , 0 , 0 , 0 ) ;
HIDRead ( p_device - > handle , buf , p_device - > maxPacketSizeRx , myHIDReadCallback , usr ) ;
2018-06-19 17:46:37 +02:00
} else if ( slotdata - > hidmask = = gHID_LIST_DS3 ) {
2018-06-19 17:40:02 +02:00
HIDSetProtocol ( p_device - > handle , p_device - > interfaceIndex , 1 , 0 , 0 ) ;
2017-03-30 17:53:36 +02:00
HIDDS3Rumble ( p_device - > handle , usr , 0 ) ;
2018-06-19 17:46:37 +02:00
buf [ 0 ] = 0x42 ;
buf [ 1 ] = 0x0c ;
buf [ 2 ] = 0x00 ;
buf [ 3 ] = 0x00 ;
2017-03-30 17:53:36 +02:00
HIDSetReport ( p_device - > handle , HID_REPORT_FEATURE , PS3_F4_REPORT_ID , buf , PS3_F4_REPORT_LEN , NULL , NULL ) ;
2018-06-19 17:40:02 +02:00
HIDRead ( p_device - > handle , usr - > buf , p_device - > maxPacketSizeRx , myHIDReadCallback , usr ) ;
2018-06-19 17:46:37 +02:00
} else {
2018-06-19 17:40:02 +02:00
HIDRead ( p_device - > handle , usr - > buf , p_device - > maxPacketSizeRx , myHIDReadCallback , usr ) ;
2017-03-30 17:53:36 +02:00
}
return HID_DEVICE_ATTACH ;
2018-06-19 17:46:37 +02:00
} else {
2017-03-30 17:53:36 +02:00
my_cb_user * user_data = NULL ;
2018-06-20 15:07:15 +02:00
int32_t founddata = 0 ;
for ( int32_t i = 0 ; i < HID_MAX_PADS_COUNT ; i + + ) {
2018-06-19 17:46:37 +02:00
if ( gHID_Devices [ slotdata - > deviceslot ] . pad_data [ i ] . handle = = p_device - > handle ) {
2017-03-30 17:53:36 +02:00
gHID_Devices [ slotdata - > deviceslot ] . pad_data [ i ] . handle = 0 ;
DCFlushRange ( & gHID_Devices [ slotdata - > deviceslot ] . pad_data [ i ] . handle , sizeof ( gHID_Devices [ slotdata - > deviceslot ] . pad_data [ i ] . handle ) ) ;
DCInvalidateRange ( & gHID_Devices [ slotdata - > deviceslot ] . pad_data [ i ] . handle , sizeof ( gHID_Devices [ slotdata - > deviceslot ] . pad_data [ i ] . handle ) ) ;
user_data = ( my_cb_user * ) gHID_Devices [ slotdata - > deviceslot ] . pad_data [ i ] . user_data ;
founddata = 1 ;
break ;
}
}
2018-06-19 17:46:37 +02:00
if ( user_data ) {
2017-03-30 17:53:36 +02:00
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 ] ) ) ;
2018-06-19 17:46:37 +02:00
if ( user_data - > buf ) {
2017-03-30 17:53:36 +02:00
free ( user_data - > buf ) ;
user_data - > buf = NULL ;
}
free ( user_data ) ;
user_data = NULL ;
2018-06-19 17:46:37 +02:00
} else {
if ( founddata ) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE ( " user_data null. You may have a memory leak. " ) ;
2018-06-19 17:46:37 +02:00
}
2017-03-30 17:53:36 +02:00
return HID_DEVICE_DETACH ;
}
2018-06-19 17:46:37 +02:00
if ( config_controller [ slotdata - > deviceslot ] [ CONTRPS_CONNECTED_PADS ] [ 1 ] = = 0 ) {
2017-03-30 17:53:36 +02:00
gHIDAttached & = ~ slotdata - > hidmask ;
gHIDCurrentDevice & = ~ slotdata - > hidmask ;
DCFlushRange ( & gHIDAttached , sizeof ( gHIDAttached ) ) ;
DCInvalidateRange ( & gHIDAttached , sizeof ( gHIDAttached ) ) ;
DCFlushRange ( & gHIDCurrentDevice , sizeof ( gHIDCurrentDevice ) ) ;
DCInvalidateRange ( & gHIDCurrentDevice , sizeof ( gHIDCurrentDevice ) ) ;
2018-06-19 17:46:37 +02:00
if ( slotdata - > hidmask = = gHID_LIST_MOUSE ) {
2017-03-30 17:53:36 +02:00
gHID_Mouse_Mode = HID_MOUSE_MODE_AIM ;
}
2018-06-19 17:46:37 +02:00
} else {
if ( HID_DEBUG ) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE ( " We still have pad for deviceslot %d connected. " , slotdata - > deviceslot ) ;
2018-06-19 17:46:37 +02:00
}
}
if ( HID_DEBUG ) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE ( " Device successfully detached " ) ;
2017-03-30 17:53:36 +02:00
}
}
2018-06-19 17:46:37 +02:00
} else {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE ( " HID-Device currently not supported! You can add support through config files " ) ;
2018-06-19 17:46:37 +02:00
}
return HID_DEVICE_DETACH ;
2017-03-30 17:53:36 +02:00
}
2018-06-20 15:07:15 +02:00
void ControllerPatcherHID : : HIDReadCallback ( uint32_t handle , unsigned char * buf , uint32_t bytes_transfered , my_cb_user * usr ) {
2017-04-23 15:19:21 +02:00
ControllerPatcherUtils : : doSampling ( usr - > slotdata . deviceslot , usr - > pad_slot , false ) ;
2020-12-16 02:04:31 +01:00
//DEBUG_FUNCTION_LINE("my_read_cbInternal: %d %08X %d",bytes_transfered,usr->slotdata.hidmask,usr->slotdata.deviceslot);
2018-06-19 17:46:37 +02:00
if ( usr - > slotdata . hidmask = = gHID_LIST_GC ) {
2017-03-30 17:53:36 +02:00
2017-04-23 12:18:36 +02:00
HID_Data * data_ptr = NULL ;
//Copy the data for all 4 pads
2018-06-20 15:07:15 +02:00
for ( int32_t i = 0 ; i < 4 ; i + + ) {
2017-04-23 12:18:36 +02:00
data_ptr = & ( gHID_Devices [ gHID_SLOT_GC ] . pad_data [ i ] ) ;
memcpy ( & ( data_ptr - > data_union . controller . last_hid_data [ 0 ] ) , & ( data_ptr - > data_union . controller . cur_hid_data [ 0 ] ) , 10 ) ; //save last data.
memcpy ( & ( data_ptr - > data_union . controller . cur_hid_data [ 0 ] ) , & buf [ ( i * 9 ) + 1 ] , 9 ) ; //save new data.
2017-03-30 17:53:36 +02:00
2017-04-23 12:18:36 +02:00
}
2017-03-30 17:53:36 +02:00
2017-04-23 12:18:36 +02:00
/*
2018-06-20 15:07:15 +02:00
int32_t i = 0 ;
2017-10-14 13:22:47 +02:00
DEBUG_FUNCTION_LINE ( " GC1 %08X: %02X %02X %02X %02X %02X %02X %02X %02X %02X " , buf [ i * 9 + 0 ] , buf [ i * 9 + 1 ] , buf [ i * 9 + 2 ] , buf [ i * 9 + 3 ] , buf [ i * 9 + 4 ] , buf [ i * 9 + 5 ] , buf [ i * 9 + 6 ] , buf [ i * 9 + 7 ] , buf [ i * 9 + 8 ] ) ; i + + ;
DEBUG_FUNCTION_LINE ( " GC2 %08X: %02X %02X %02X %02X %02X %02X %02X %02X %02X " , buf [ i * 9 + 0 ] , buf [ i * 9 + 1 ] , buf [ i * 9 + 2 ] , buf [ i * 9 + 3 ] , buf [ i * 9 + 4 ] , buf [ i * 9 + 5 ] , buf [ i * 9 + 6 ] , buf [ i * 9 + 7 ] , buf [ i * 9 + 8 ] ) ; i + + ;
DEBUG_FUNCTION_LINE ( " GC3 %08X: %02X %02X %02X %02X %02X %02X %02X %02X %02X " , buf [ i * 9 + 0 ] , buf [ i * 9 + 1 ] , buf [ i * 9 + 2 ] , buf [ i * 9 + 3 ] , buf [ i * 9 + 4 ] , buf [ i * 9 + 5 ] , buf [ i * 9 + 6 ] , buf [ i * 9 + 7 ] , buf [ i * 9 + 8 ] ) ; i + + ;
DEBUG_FUNCTION_LINE ( " GC4 %08X: %02X %02X %02X %02X %02X %02X %02X %02X %02X \n " , buf [ i * 9 + 0 ] , buf [ i * 9 + 1 ] , buf [ i * 9 + 2 ] , buf [ i * 9 + 3 ] , buf [ i * 9 + 4 ] , buf [ i * 9 + 5 ] , buf [ i * 9 + 6 ] , buf [ i * 9 + 7 ] , buf [ i * 9 + 8 ] ) ; */
2017-04-23 12:18:36 +02:00
HIDGCRumble ( handle , usr ) ;
2018-06-19 17:46:37 +02:00
} else if ( usr - > slotdata . hidmask ! = 0 ) {
2017-04-23 12:18:36 +02:00
//Depending on how the switch pro controller is connected, it has a different data format. At first we had the Bluetooth version, so we need to convert
//the USB one into it now. (When it's connected via USB). The network client always sends the BT version, even if connected via USB to the PC.
2018-06-19 17:46:37 +02:00
if ( usr - > slotdata . hidmask = = gHID_LIST_SWITCH_PRO & & buf ! = NULL & & bytes_transfered > = 0x20 ) {
2018-06-20 15:07:15 +02:00
uint8_t buffer [ 0x13 ] ;
2017-04-23 12:18:36 +02:00
memcpy ( buffer , buf + 0x0D , 0x013 ) ;
/**
Thanks to ShinyQuagsire23 for the values ( https : //github.com/shinyquagsire23/HID-Joy-Con-Whispering)
* */
buf [ 0 ] = 0x80 ;
buf [ 1 ] = 0x92 ;
buf [ 2 ] = 0x00 ;
buf [ 3 ] = 0x01 ;
buf [ 4 ] = 0x00 ;
buf [ 5 ] = 0x00 ;
buf [ 6 ] = 0x00 ;
buf [ 7 ] = 0x00 ;
buf [ 8 ] = 0x1F ;
//We want to get the next input!
2018-06-20 15:07:15 +02:00
int32_t res = HIDWrite ( handle , buf , 9 , NULL , NULL ) ;
2017-04-23 12:18:36 +02:00
2018-06-19 17:46:37 +02:00
if ( res = = 9 ) { //Check if it's the USB data format.
2017-04-23 12:18:36 +02:00
if ( buffer [ 1 ] = = 0 ) return ;
//Converting the buttons
2018-06-20 15:07:15 +02:00
uint32_t buttons = ( ( ( uint32_t * ) ( buffer ) ) [ 0 ] ) & 0xFFFFFF00 ;
uint32_t newButtons = 0 ;
2017-04-23 12:18:36 +02:00
if ( ( buttons & HID_SWITCH_PRO_USB_BUTTON_A_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_A_VALUE ) newButtons | = HID_SWITCH_PRO_BT_BUTTON_A_VALUE ;
if ( ( buttons & HID_SWITCH_PRO_USB_BUTTON_B_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_B_VALUE ) newButtons | = HID_SWITCH_PRO_BT_BUTTON_B_VALUE ;
if ( ( buttons & HID_SWITCH_PRO_USB_BUTTON_X_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_X_VALUE ) newButtons | = HID_SWITCH_PRO_BT_BUTTON_X_VALUE ;
if ( ( buttons & HID_SWITCH_PRO_USB_BUTTON_Y_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_Y_VALUE ) newButtons | = HID_SWITCH_PRO_BT_BUTTON_Y_VALUE ;
if ( ( buttons & HID_SWITCH_PRO_USB_BUTTON_PLUS_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_PLUS_VALUE ) newButtons | = HID_SWITCH_PRO_BT_BUTTON_PLUS_VALUE ;
if ( ( buttons & HID_SWITCH_PRO_USB_BUTTON_MINUS_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_MINUS_VALUE ) newButtons | = HID_SWITCH_PRO_BT_BUTTON_MINUS_VALUE ;
if ( ( buttons & HID_SWITCH_PRO_USB_BUTTON_HOME_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_HOME_VALUE ) newButtons | = HID_SWITCH_PRO_BT_BUTTON_HOME_VALUE ;
//if((buttons & SWITCH_PRO_USB_BUTTON_SCREENSHOT) == HID_SWITCH_PRO_USB_BUTTON_SCREENSHOT) newButtons |= HID_SWITCH_PRO_BT_BUTTON_SCREENSHOT;
if ( ( buttons & HID_SWITCH_PRO_USB_BUTTON_R_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_R_VALUE ) newButtons | = HID_SWITCH_PRO_BT_BUTTON_R_VALUE ;
if ( ( buttons & HID_SWITCH_PRO_USB_BUTTON_ZR_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_ZR_VALUE ) newButtons | = HID_SWITCH_PRO_BT_BUTTON_ZR_VALUE ;
if ( ( buttons & HID_SWITCH_PRO_USB_BUTTON_STICK_R_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_STICK_R_VALUE ) newButtons | = HID_SWITCH_PRO_BT_BUTTON_STICK_R_VALUE ;
if ( ( buttons & HID_SWITCH_PRO_USB_BUTTON_L_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_L_VALUE ) newButtons | = HID_SWITCH_PRO_BT_BUTTON_L_VALUE ;
if ( ( buttons & HID_SWITCH_PRO_USB_BUTTON_ZL_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_ZL_VALUE ) newButtons | = HID_SWITCH_PRO_BT_BUTTON_ZL_VALUE ;
if ( ( buttons & HID_SWITCH_PRO_USB_BUTTON_STICK_L_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_STICK_L_VALUE ) newButtons | = HID_SWITCH_PRO_BT_BUTTON_STICK_L_VALUE ;
2018-06-20 15:07:15 +02:00
uint8_t dpad = buffer [ 2 ] ;
uint8_t dpadResult = HID_SWITCH_PRO_BT_BUTTON_DPAD_NEUTRAL_VALUE ;
2017-04-23 12:18:36 +02:00
//Converting the DPAD
if ( ( ( dpad & HID_SWITCH_PRO_USB_BUTTON_UP_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_UP_VALUE ) & &
2018-06-19 17:46:37 +02:00
( ( dpad & HID_SWITCH_PRO_USB_BUTTON_RIGHT_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_RIGHT_VALUE ) ) {
2017-04-23 12:18:36 +02:00
dpadResult = HID_SWITCH_PRO_BT_BUTTON_DPAD_NE_VALUE ;
2018-06-19 17:46:37 +02:00
} else if ( ( ( dpad & HID_SWITCH_PRO_USB_BUTTON_DOWN_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_DOWN_VALUE ) & &
( ( dpad & HID_SWITCH_PRO_USB_BUTTON_RIGHT_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_RIGHT_VALUE ) ) {
2017-04-23 12:18:36 +02:00
dpadResult = HID_SWITCH_PRO_BT_BUTTON_DPAD_SE_VALUE ;
2018-06-19 17:46:37 +02:00
} else if ( ( ( dpad & HID_SWITCH_PRO_USB_BUTTON_DOWN_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_DOWN_VALUE ) & &
( ( dpad & HID_SWITCH_PRO_USB_BUTTON_LEFT_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_LEFT_VALUE ) ) {
2017-04-23 12:18:36 +02:00
dpadResult = HID_SWITCH_PRO_BT_BUTTON_DPAD_SW_VALUE ;
2018-06-19 17:46:37 +02:00
} else if ( ( ( dpad & HID_SWITCH_PRO_USB_BUTTON_UP_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_UP_VALUE ) & &
( ( dpad & HID_SWITCH_PRO_USB_BUTTON_LEFT_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_LEFT_VALUE ) ) {
2017-04-23 12:18:36 +02:00
dpadResult = HID_SWITCH_PRO_BT_BUTTON_DPAD_NW_VALUE ;
2018-06-19 17:46:37 +02:00
} else if ( ( dpad & HID_SWITCH_PRO_USB_BUTTON_UP_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_UP_VALUE ) {
2017-04-23 12:18:36 +02:00
dpadResult = HID_SWITCH_PRO_BT_BUTTON_DPAD_N_VALUE ;
2018-06-19 17:46:37 +02:00
} else if ( ( dpad & HID_SWITCH_PRO_USB_BUTTON_RIGHT_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_RIGHT_VALUE ) {
2017-04-23 12:18:36 +02:00
dpadResult = HID_SWITCH_PRO_BT_BUTTON_DPAD_E_VALUE ;
2018-06-19 17:46:37 +02:00
} else if ( ( dpad & HID_SWITCH_PRO_USB_BUTTON_DOWN_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_DOWN_VALUE ) {
2017-04-23 12:18:36 +02:00
dpadResult = HID_SWITCH_PRO_BT_BUTTON_DPAD_S_VALUE ;
2018-06-19 17:46:37 +02:00
} else if ( ( dpad & HID_SWITCH_PRO_USB_BUTTON_LEFT_VALUE ) = = HID_SWITCH_PRO_USB_BUTTON_LEFT_VALUE ) {
2017-04-23 12:18:36 +02:00
dpadResult = HID_SWITCH_PRO_BT_BUTTON_DPAD_W_VALUE ;
2017-03-30 17:53:36 +02:00
}
2017-04-23 12:18:36 +02:00
//Converting the stick data
2018-06-20 15:07:15 +02:00
uint8_t LX = ( uint8_t ) ( ( uint16_t ) ( ( buffer [ 0x04 ] < < 8 & 0xFF00 ) | ( ( ( uint16_t ) buffer [ 0x03 ] ) & 0xFF ) ) > > 0x04 ) ;
uint8_t LY = ( uint8_t ) ( ( buffer [ 0x05 ] * - 1 ) ) ;
uint8_t RX = ( uint8_t ) ( ( uint16_t ) ( ( buffer [ 0x07 ] < < 8 & 0xFF00 ) | ( ( ( uint16_t ) buffer [ 0x06 ] ) & 0xFF ) ) > > 0x04 ) ;
uint8_t RY = ( uint8_t ) ( ( buffer [ 0x08 ] * - 1 ) ) ;
2017-04-23 12:18:36 +02:00
buf [ 0 ] = ( newButtons > > 24 ) & 0xFF ;
buf [ 1 ] = ( newButtons > > 16 ) & 0xFF ;
buf [ 2 ] | = dpadResult ;
buf [ 4 ] = LX ;
buf [ 6 ] = LY ;
buf [ 8 ] = RX ;
buf [ 10 ] = RY ;
2017-03-30 17:53:36 +02:00
}
2017-04-23 12:18:36 +02:00
}
2017-03-30 17:53:36 +02:00
2018-06-20 15:07:15 +02:00
int32_t dsize = ( HID_MAX_DATA_LENGTH_PER_PAD > bytes_transfered ) ? bytes_transfered : HID_MAX_DATA_LENGTH_PER_PAD ;
int32_t skip = 0 ;
2017-04-23 12:18:36 +02:00
//Input filter
2018-06-19 17:46:37 +02:00
if ( config_controller [ usr - > slotdata . deviceslot ] [ CONTRPS_INPUT_FILTER ] [ 0 ] ! = CONTROLLER_PATCHER_INVALIDVALUE ) {
if ( buf [ config_controller [ usr - > slotdata . deviceslot ] [ CONTRPS_INPUT_FILTER ] [ 0 ] ] ! = config_controller [ usr - > slotdata . deviceslot ] [ CONTRPS_INPUT_FILTER ] [ 1 ] ) {
2017-04-23 12:18:36 +02:00
skip = 1 ;
}
}
2017-03-30 17:53:36 +02:00
2018-06-19 17:46:37 +02:00
if ( ! skip ) {
2018-06-20 15:07:15 +02:00
uint32_t slot = 0 ;
2018-06-19 17:46:37 +02:00
if ( usr - > pad_slot < HID_MAX_PADS_COUNT ) {
2017-04-23 12:18:36 +02:00
slot = usr - > pad_slot ;
}
slot + = ControllerPatcherUtils : : getPadSlotInAdapter ( usr - > slotdata . deviceslot , buf ) ; // If the controller has multiple slots, we need to use the right one.
2017-03-30 17:53:36 +02:00
2017-04-23 12:18:36 +02:00
HID_Data * data_ptr = & ( gHID_Devices [ usr - > slotdata . deviceslot ] . pad_data [ slot ] ) ;
2017-03-30 17:53:36 +02:00
2017-04-23 12:18:36 +02:00
memcpy ( & ( data_ptr - > data_union . controller . last_hid_data [ 0 ] ) , & ( data_ptr - > data_union . controller . cur_hid_data [ 0 ] ) , dsize ) ; // save the last data.
memcpy ( & ( data_ptr - > data_union . controller . cur_hid_data [ 0 ] ) , & buf [ 0 ] , dsize ) ; // save the new data.
2017-03-30 17:53:36 +02:00
2017-04-23 12:18:36 +02:00
DCFlushRange ( & gHID_Devices [ usr - > slotdata . deviceslot ] . pad_data [ slot ] , sizeof ( HID_Data ) ) ;
2017-03-30 17:53:36 +02:00
2017-04-23 12:18:36 +02:00
data_ptr = & ( gHID_Devices [ usr - > slotdata . deviceslot ] . pad_data [ slot ] ) ;
HIDRumble ( handle , usr , slot ) ;
}
}
2017-03-30 17:53:36 +02:00
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
* Other functions
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2018-06-19 17:46:37 +02:00
CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcherHID : : setVPADControllerData ( VPADStatus * buffer , std : : vector < HID_Data * > & data ) {
2017-03-30 17:53:36 +02:00
if ( buffer = = NULL ) return CONTROLLER_PATCHER_ERROR_NULL_POINTER ;
HID_Data * data_cur ;
2018-06-20 15:07:15 +02:00
int32_t buttons_hold ;
for ( uint32_t i = 0 ; i < data . size ( ) ; i + + ) {
2017-03-30 17:53:36 +02:00
data_cur = data [ i ] ;
2018-06-19 17:46:37 +02:00
if ( data_cur - > slotdata . hidmask & gHID_LIST_MOUSE ) { //Reset the input when we have no new inputs
2017-03-30 17:53:36 +02:00
HID_Mouse_Data * mouse_data = & data_cur - > data_union . mouse . cur_mouse_data ;
2018-06-19 17:46:37 +02:00
if ( mouse_data - > valuedChanged = = 1 ) { //Fix for the mouse cursor
2017-03-30 17:53:36 +02:00
mouse_data - > valuedChanged = 0 ;
2018-06-19 17:46:37 +02:00
} else {
2017-03-30 17:53:36 +02:00
mouse_data - > deltaX = 0 ;
mouse_data - > deltaY = 0 ;
}
}
buttons_hold = 0 ;
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 ) ;
2018-06-20 15:07:15 +02:00
uint32_t last_emulate_stick = ( data_cur - > last_buttons ) & VPAD_MASK_EMULATED_STICKS ; // We should only need the emulated stick data.
int32_t last_realbuttons = ( data_cur - > last_buttons ) & VPAD_MASK_BUTTONS ;
2017-03-30 17:53:36 +02:00
2018-06-17 20:33:39 +02:00
buffer - > hold | = buttons_hold ;
buffer - > trigger | = ( buttons_hold & ( ~ last_realbuttons ) ) ;
buffer - > release | = ( last_realbuttons & ( ~ buttons_hold ) ) ;
2017-03-30 17:53:36 +02:00
ControllerPatcherUtils : : convertAnalogSticks ( data_cur , buffer ) ;
ControllerPatcherUtils : : setEmulatedSticks ( buffer , & last_emulate_stick ) ;
ControllerPatcherUtils : : checkAndSetMouseMode ( data_cur ) ;
ControllerPatcherUtils : : setTouch ( data_cur , buffer ) ;
data_cur - > last_buttons = buttons_hold & VPAD_MASK_BUTTONS ;
data_cur - > last_buttons | = last_emulate_stick ;
}
// Caculates a valid stick position
2018-06-19 17:46:37 +02:00
if ( data . size ( ) > 0 ) {
2018-06-17 20:33:39 +02:00
ControllerPatcherUtils : : normalizeStickValues ( & buffer - > leftStick ) ;
ControllerPatcherUtils : : normalizeStickValues ( & buffer - > rightStick ) ;
2017-03-30 17:53:36 +02:00
}
return CONTROLLER_PATCHER_ERROR_NONE ;
}
2018-06-19 17:46:37 +02:00
std : : vector < HID_Data * > ControllerPatcherHID : : getHIDDataAll ( ) {
2018-06-20 15:07:15 +02:00
uint32_t hid = gHIDCurrentDevice ;
2017-03-30 17:53:36 +02:00
std : : vector < HID_Data * > data_list ;
2018-06-20 15:07:15 +02:00
for ( int32_t i = 0 ; i < gHIDMaxDevices ; i + + ) {
2018-06-19 17:46:37 +02:00
if ( ( hid & ( 1 < < i ) ) ! = 0 ) {
2018-06-20 15:07:15 +02:00
uint32_t cur_hidmask = config_controller_hidmask [ i ] ;
for ( int32_t pad = 0 ; pad < HID_MAX_PADS_COUNT ; pad + + ) {
int32_t res ;
2017-03-30 17:53:36 +02:00
HID_Data * new_data = NULL ;
2018-06-19 17:46:37 +02:00
if ( ( res = ControllerPatcherHID : : getHIDData ( cur_hidmask , pad , & new_data ) ) < 0 ) { // Checks if the pad is invalid.
2018-02-14 22:24:20 +01:00
//DEBUG_FUNCTION_LINE("error: Error getting the HID data from HID(%s) CHAN(). Error %d\n",StringTools::byte_to_binary(cur_hidmask),pad,res);
2017-03-30 17:53:36 +02:00
continue ;
}
if ( new_data ! = NULL ) data_list . push_back ( new_data ) ;
}
}
}
return data_list ;
}
/*
The slotdata in the HID_Data pointer is empty . We need to provide the hidmask via the parameter
*/
2018-06-20 15:07:15 +02:00
CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcherHID : : getHIDData ( uint32_t hidmask , int32_t pad , HID_Data * * data ) {
2017-03-30 17:53:36 +02:00
if ( data = = NULL ) return CONTROLLER_PATCHER_ERROR_INVALID_BUFFER ;
if ( ! ( hidmask & gHIDCurrentDevice ) ) return CONTROLLER_PATCHER_ERROR_HID_NOT_CONNECTED ;
if ( pad < 0 & & pad > 3 ) return CONTROLLER_PATCHER_ERROR_INVALID_CHAN ;
2018-06-20 15:07:15 +02:00
int32_t device_slot = ControllerPatcherUtils : : getDeviceSlot ( hidmask ) ;
2018-06-19 17:46:37 +02:00
if ( device_slot < 0 ) {
2017-03-30 17:53:36 +02:00
return CONTROLLER_PATCHER_ERROR_DEVICE_SLOT_NOT_FOUND ;
}
2017-04-02 19:05:31 +02:00
2018-06-20 15:07:15 +02:00
int32_t real_pad = pad ;
2018-06-19 17:46:37 +02:00
if ( ( device_slot ! = gHID_SLOT_GC ) & & config_controller [ device_slot ] [ CONTRPS_PAD_COUNT ] [ 0 ] ! = CONTROLLER_PATCHER_INVALIDVALUE ) {
2018-06-20 15:07:15 +02:00
int32_t pad_count = config_controller [ device_slot ] [ CONTRPS_PAD_COUNT ] [ 1 ] ;
2017-03-30 17:53:36 +02:00
if ( pad_count > HID_MAX_PADS_COUNT ) pad_count = HID_MAX_PADS_COUNT ;
pad = ( pad / ( pad_count ) ) * pad_count ;
}
2018-06-20 15:07:15 +02:00
int32_t result = ControllerPatcherUtils : : checkActivePad ( hidmask , pad ) ;
2017-03-30 17:53:36 +02:00
2018-06-19 17:46:37 +02:00
if ( result < 0 ) { //Not pad connected to adapter
2017-03-30 17:53:36 +02:00
return CONTROLLER_PATCHER_ERROR_NO_PAD_CONNECTED ;
}
* data = & gHID_Devices [ device_slot ] . pad_data [ real_pad ] ;
return CONTROLLER_PATCHER_ERROR_NONE ;
}
2018-06-20 15:07:15 +02:00
void ControllerPatcherHID : : HIDGCRumble ( uint32_t handle , my_cb_user * usr ) {
2017-04-14 16:49:21 +02:00
if ( usr = = NULL ) return ;
2017-05-07 14:29:19 +02:00
if ( ! ControllerPatcher : : isRumbleActivated ( ) ) return ;
2018-06-20 15:07:15 +02:00
int32_t rumblechanged = 0 ;
2017-03-30 17:53:36 +02:00
2018-06-20 15:07:15 +02:00
for ( int32_t i = 0 ; i < HID_GC_PAD_COUNT ; i + + ) {
2017-03-30 17:53:36 +02:00
HID_Data * data_ptr = & ( gHID_Devices [ usr - > slotdata . deviceslot ] . pad_data [ i ] ) ;
2018-06-19 17:46:37 +02:00
if ( data_ptr - > rumbleActive ! = usr - > rumblestatus [ i ] ) {
2017-03-30 17:53:36 +02:00
rumblechanged = 1 ;
}
2017-04-14 16:49:21 +02:00
usr - > rumblestatus [ i ] = data_ptr - > rumbleActive ;
usr - > buf [ i + 1 ] = usr - > rumblestatus [ i ] ;
2017-03-30 17:53:36 +02:00
}
2017-04-23 15:19:21 +02:00
usr - > forceRumbleInTicks [ 0 ] - - ;
2018-06-19 17:46:37 +02:00
if ( rumblechanged | | usr - > forceRumbleInTicks [ 0 ] < = 0 ) {
2017-04-14 16:49:21 +02:00
usr - > buf [ 0 ] = 0x11 ;
HIDWrite ( handle , usr - > buf , 5 , NULL , NULL ) ;
2017-04-23 15:19:21 +02:00
usr - > forceRumbleInTicks [ 0 ] = 10 ;
2017-03-30 17:53:36 +02:00
}
}
2018-06-20 15:07:15 +02:00
void ControllerPatcherHID : : HIDRumble ( uint32_t handle , my_cb_user * usr , uint32_t pad ) {
2017-04-13 15:30:40 +02:00
if ( usr = = NULL | | pad > HID_MAX_PADS_COUNT ) return ;
2017-05-07 14:29:19 +02:00
if ( ! ControllerPatcher : : isRumbleActivated ( ) ) return ;
2017-04-13 15:30:40 +02:00
2018-06-20 15:07:15 +02:00
int32_t rumblechanged = 0 ;
2017-03-30 17:53:36 +02:00
HID_Data * data_ptr = & ( gHID_Devices [ usr - > slotdata . deviceslot ] . pad_data [ pad ] ) ;
2018-06-19 17:46:37 +02:00
if ( data_ptr - > rumbleActive ! = usr - > rumblestatus [ pad ] ) {
2017-03-30 17:53:36 +02:00
usr - > rumblestatus [ pad ] = data_ptr - > rumbleActive ;
rumblechanged = 1 ;
}
2017-04-23 15:19:21 +02:00
usr - > forceRumbleInTicks [ pad ] - - ;
2018-06-19 17:46:37 +02:00
if ( rumblechanged | | usr - > forceRumbleInTicks [ pad ] < = 0 ) {
2020-12-16 02:04:31 +01:00
//DEBUG_FUNCTION_LINE("Rumble: %d %d",usr->rumblestatus[pad],usr->rumbleForce[pad]);
2017-04-13 15:30:40 +02:00
//Seding to the network client!
char bytes [ 6 ] ;
2018-06-20 15:07:15 +02:00
int32_t i = 0 ;
2017-04-13 15:30:40 +02:00
bytes [ i + + ] = 0x01 ;
bytes [ i + + ] = ( handle > > 24 ) & 0xFF ;
bytes [ i + + ] = ( handle > > 16 ) & 0xFF ;
bytes [ i + + ] = ( handle > > 8 ) & 0xFF ;
bytes [ i + + ] = handle & 0xFF ;
bytes [ i + + ] = usr - > rumblestatus [ pad ] ;
UDPClient * instance = UDPClient : : getInstance ( ) ;
2018-06-19 17:46:37 +02:00
if ( instance ! = NULL ) {
2017-04-13 15:30:40 +02:00
instance - > sendData ( bytes , 6 ) ;
}
2018-06-19 17:46:37 +02:00
if ( usr - > slotdata . hidmask = = gHID_LIST_DS3 ) {
2017-03-30 17:53:36 +02:00
HIDDS3Rumble ( handle , usr , usr - > rumblestatus [ pad ] ) ;
2018-06-19 17:46:37 +02:00
} else {
2017-03-30 17:53:36 +02:00
// Not implemented for other devices =(
}
2017-04-23 15:19:21 +02:00
usr - > forceRumbleInTicks [ pad ] = 10 ;
2017-03-30 17:53:36 +02:00
}
}
2018-06-20 15:07:15 +02:00
static uint8_t ds3_rumble_Report [ 48 ] = {
2017-03-30 17:53:36 +02:00
0x00 , 0xFF , 0x00 , 0xFF , 0x00 ,
0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
0xFF , 0x27 , 0x10 , 0x00 , 0x32 ,
0xFF , 0x27 , 0x10 , 0x00 , 0x32 ,
0xFF , 0x27 , 0x10 , 0x00 , 0x32 ,
0xFF , 0x27 , 0x10 , 0x00 , 0x32 ,
0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
0x00 , 0x00 , 0x00 ,
} ;
2018-06-20 15:07:15 +02:00
void ControllerPatcherHID : : HIDDS3Rumble ( uint32_t handle , my_cb_user * usr , int32_t rumble ) {
2017-03-30 17:53:36 +02:00
memcpy ( usr - > buf , ds3_rumble_Report , 48 ) ;
if ( rumble ) {
usr - > buf [ 2 ] = 0x01 ;
usr - > buf [ 4 ] = 0xff ;
}
HIDSetReport ( handle , HID_REPORT_OUTPUT , PS3_01_REPORT_ID , usr - > buf , 48 , NULL , NULL ) ;
}