controller_patcher/source/patcher/ControllerPatcherUtils.hpp

322 lines
17 KiB
C++
Raw Permalink Normal View History

/****************************************************************************
* 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/>.
****************************************************************************/
/**
* @file ControllerPatcherUtil.hpp
* @author Maschell
* @date 25 Aug 2016
* \brief This files contain useful functions for the controller patcher engine
*
* @see https://github.com/Maschell/controller_patcher
*/
#ifndef _CONTROLLER_PATCHER_UTIL_H_
#define _CONTROLLER_PATCHER_UTIL_H_
2018-06-17 20:33:39 +02:00
#include <padscore/kpad.h>
#include <padscore/wpad.h>
2017-05-07 14:44:09 +02:00
2017-10-29 09:34:47 +01:00
#include "../ControllerPatcherIncludes.hpp"
2017-05-07 14:44:09 +02:00
2018-06-19 17:46:37 +02:00
class ControllerPatcherUtils {
//give the other classes access to the private functions.
friend class ControllerPatcher;
friend class ControllerPatcherHID;
friend class ConfigParser;
2018-06-19 17:46:37 +02:00
public:
/**
\brief Returns the device slot for a given HID-Mask.
2018-06-19 17:46:37 +02:00
\param hidmask Given HID-Mask
2018-06-19 17:46:37 +02:00
\return When the functions failed result < 0 is returned. If the result is >= 0 the function was successful. The returned value is the deviceslot of the given HID-Mask
**/
2018-06-20 15:07:15 +02:00
static CONTROLLER_PATCHER_RESULT_OR_ERROR getDeviceSlot(uint32_t hidmask);
2018-06-19 17:46:37 +02:00
/**
\brief Returns the device slot for a given HID-Mask.
2018-06-19 17:46:37 +02:00
\param handle Given HID-handle
\param data Given my_cb_user ** where the result will be stored. Valid pointer when result is >= 0.
2018-06-19 17:46:37 +02:00
\return When the functions failed result < 0 is returned. If the result is >= 0 the function was successful. The actual result will be store in the given my_cb_user **.
**/
2018-06-20 15:07:15 +02:00
static CONTROLLER_PATCHER_RESULT_OR_ERROR getDataByHandle(int32_t handle, my_cb_user ** data);
2018-06-19 17:46:37 +02:00
/**
\brief Returns the VID/PID for the given device slot.
2018-06-19 17:46:37 +02:00
\param deviceslot Given device slot
\param vidpid Pointer to the DeviceVIDPIDInfo struct where the result will be stored.
2018-06-19 17:46:37 +02:00
\return When the functions failed result < 0 is returned. If the result is >= 0 the function was successful. The actual result will be store in the given DeviceVIDPIDInfo *.
**/
2018-06-20 15:07:15 +02:00
static CONTROLLER_PATCHER_RESULT_OR_ERROR getVIDPIDbyDeviceSlot(int32_t deviceslot, DeviceVIDPIDInfo * vidpid);
2018-06-19 17:46:37 +02:00
/** \brief Set the VPAD data for a given KPAD data.
*
* \param vpad_buffer VPADStatus* A pointer to the VPAD Data where the result will be stored.
* \param pro_buffer KPADData* A pointer to the given KPADData data.
2018-06-20 15:07:15 +02:00
* \param lastButtonsPressesPRO uint32_t* A pointer to the button presses of the previous call. Will be updated while calling.
2018-06-19 17:46:37 +02:00
* \return When the functions failed result < 0 is returned. If the result is >= 0 the function was successful.
*
*/
2018-06-20 15:07:15 +02:00
static CONTROLLER_PATCHER_RESULT_OR_ERROR translateToVPAD(VPADStatus * vpad_buffer,KPADStatus * pro_buffer,uint32_t * lastButtonsPressesVPAD);
2018-06-19 17:46:37 +02:00
private:
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
* Analyse inputs
*---------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
2018-06-19 17:46:37 +02:00
/** \brief Checks if a the given @p VPADButton was pressed in the given HID @data. When it was pressed, the result will be set the in given @p buttons_hold
*
* \param data Pointer to the HID_Data from where the input is read.
2018-06-20 15:07:15 +02:00
* \param buttons_hold Pointer to the uint32_t where the result will be written to.
2018-06-19 17:46:37 +02:00
* \param VPADButton The button that will be checked
* \return When the functions failed result < 0 is returned.If the result is >= 0 the function was successful.
*
*/
2018-06-20 15:07:15 +02:00
static CONTROLLER_PATCHER_RESULT_OR_ERROR getButtonPressed(HID_Data * data, int32_t * buttons_hold, int32_t VPADButton);
2018-06-19 17:46:37 +02:00
/** \brief Checks if a given value is set in the HID_DATA given the data in the slot number provided by cur_config.
*
* \param data Pointer to the HID_Data from where the input is read.
* \param cur_config slot of the configuration array which will be checked.
* \return When the functions failed result < 0 is returned. If the value is set, 1 will be returned. Otherwise 0.
*
*/
2018-06-20 15:07:15 +02:00
static CONTROLLER_PATCHER_RESULT_OR_ERROR isValueSet(HID_Data * data,int32_t cur_config);
2018-06-19 17:46:37 +02:00
/** \brief Checks if a given key in the keyboard data is pressed.
*
* \param keyboardData A pointer to the keyboard data.
* \param key A pointer to the keyboard data.
* \return When the functions failed result < 0 is returned. If the key is active pressed, 1 is returned.
*
*/
2018-06-20 15:07:15 +02:00
static CONTROLLER_PATCHER_RESULT_OR_ERROR isInKeyboardData(unsigned char * keyboardData,int32_t key);
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
* Utils for setting the Button data
*---------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
2018-06-19 17:46:37 +02:00
/** \brief Checks if a @p VPADButton (VPAD_BUTTON_XXX) is set in the given @p CONTRPS_SLOT (usually the one for buttons remapping) of the GamePad. When its set it'll be
* set for the corresponding Button (aka button remapping). When the @p CONTRPS_SLOT is not valid, the normal buttons layout will be used.
*
* \param old_buffer A pointer to a VPADStatus struct from which will be read.
* \param new_buffer A pointer to a VPADStatus struct where the result will be written.
* \param VPADButton The buttons that will be may replaced
* \param CONTRPS_SLOT The CONTRPS_SLOT where the VPAD_Buttons we want to use instead of the parameter "VPADButton" could be saved.
* \return When the functions failed result < 0 is returned. If the pad is active/connected, 1 is returned.
*
*/
2018-06-20 15:07:15 +02:00
static CONTROLLER_PATCHER_RESULT_OR_ERROR setButtonRemappingData(VPADStatus * old_buffer, VPADStatus * new_buffer,uint32_t VPADButton, int32_t CONTRPS_SLOT);
2018-06-19 17:46:37 +02:00
/**
\brief Checks if a given button (oldVPADButton) is set in a given VPADStatus struct (old_buffer). If its set, it will set an other
button (newVPADButton) to the second given VPADStatus struct (new_buffer)
\param old_buffer A pointer to a VPADStatus struct from which will be read.
\param new_buffer A pointer to a VPADStatus struct where the result will be written.
\param oldVPADButton The buttons that need to be set in the first VPADStatus
\param newVPADButton The buttons that will be set in the second VPADStatus, when the oldVPADButton is pressed in the first buffer.
\return When the functions failed result < 0 is returned. If the pad is active/connected, 1 is returned.
**/
2018-06-20 15:07:15 +02:00
static CONTROLLER_PATCHER_RESULT_OR_ERROR setButtonData(VPADStatus * old_buffer, VPADStatus * new_buffer,uint32_t oldVPADButton,uint32_t newVPADButton);
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
* Pad Status functions
*---------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
2018-06-19 17:46:37 +02:00
/**
\brief Checks if a controller is attached for the given HID-Mask and pad.
2018-06-19 17:46:37 +02:00
\param hidmask Bit-Mask of the target hid-device.
\param pad Defines for which pad the connection will be checked.
2018-06-19 17:46:37 +02:00
\return When the functions failed result < 0 is returned. If the pad is active/connected, 1 is returned.
**/
2018-06-20 15:07:15 +02:00
static CONTROLLER_PATCHER_RESULT_OR_ERROR checkActivePad(uint32_t hidmask,int32_t pad);
2018-06-19 17:46:37 +02:00
/**
\brief Returns the first active pad of devices with the given HID-Mask. Currently only implemented for the GC-Adapter. Every other pad will always return 0.
2018-06-19 17:46:37 +02:00
\param hidmask Bit-Mask of the target hid-device.
2018-06-19 17:46:37 +02:00
\return When the functions failed result < 0 is returned. If the result is >= 0 the function was successful. The returned value is fist active pad.
**/
2018-06-20 15:07:15 +02:00
static CONTROLLER_PATCHER_RESULT_OR_ERROR getActivePad(uint32_t hidmask);
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
* Stick functions
*---------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
2018-06-19 17:46:37 +02:00
/**
\brief Normalizes the stick to valid values.
2018-06-19 17:46:37 +02:00
\param stick Pointer to the stick that will be normalized
2018-06-19 17:46:37 +02:00
\return When the functions failed result < 0 is returned. If the result is >= 0 the function was successful.
**/
static CONTROLLER_PATCHER_RESULT_OR_ERROR normalizeStickValues(VPADVec2D * stick);
2018-06-19 17:46:37 +02:00
/**
\brief Converts the digital absolute stick data into a float value. It also applies the deadzones, and can invert the result.
2018-06-19 17:46:37 +02:00
\param value Given current value of the stick axis
\param default_val Value in neutral axis-position
\param min Value that represents -1.0f
\param max Value that represents 1.0f
\param invert Set to 1 if the axis needs to be inverted
\param deadzone Deadzone
2018-06-19 17:46:37 +02:00
\return When the functions failed result < 0 is returned. If the result is >= 0 the function was successful.
**/
2018-06-20 15:07:15 +02:00
static float convertAnalogValue(uint8_t value, uint8_t default_val, uint8_t min, uint8_t max, uint8_t invert,uint8_t deadzone);
2018-06-19 17:46:37 +02:00
/**
\brief Calculates a the stick data (VPADVec2D) from given digital direction.
2018-06-19 17:46:37 +02:00
\param stick_values bits need to set for each direction. (STICK_VALUE_UP,STICK_VALUE_DOWN,STICK_VALUE_LEFT,STICK_VALUE_RIGHT)
2018-06-19 17:46:37 +02:00
\return The VPADVec2D with the set values.
**/
2018-06-20 15:07:15 +02:00
static VPADVec2D getAnalogValueByButtons(uint8_t stick_values);
2018-06-19 17:46:37 +02:00
/**
\brief Handles the analog-stick data of HID devices. The result will written in the VPADStatus buffer.
2018-06-19 17:46:37 +02:00
\param data Pointer to the current data of the HID device
\param buffer Pointer to VPADStatus where the analog-stick data will be set.
2018-06-19 17:46:37 +02:00
\return When the functions failed result < 0 is returned. If the result is >= 0 the function was successful.
**/
static CONTROLLER_PATCHER_RESULT_OR_ERROR convertAnalogSticks(HID_Data * data,VPADStatus * buffer);
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
* Mouse functions
*---------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
2018-06-19 17:46:37 +02:00
/**
\brief Set the touch data in the VPADStatus buffer.
Currently its only possible to set the touch data from a Mouse
2018-06-19 17:46:37 +02:00
\param data The current data of the HID device
\param buffer Pointer to VPADStatus where the touch data will be set.
2018-06-19 17:46:37 +02:00
\return When the functions failed result < 0 is returned. If the result is >= 0 the function was successful.
**/
static CONTROLLER_PATCHER_RESULT_OR_ERROR setTouch(HID_Data * data,VPADStatus * buffer);
2018-06-19 17:46:37 +02:00
/** \brief Checks if the mouse mode needs to be changed. Sets it to the new mode if necessary.
* Currently the incoming data needs to be from a keyboard.
*
* \param data HID_Data* Pointer to the current data
* \return When the functions failed result < 0 is returned. If the result is >= 0 the function was successful.
*
*/
static CONTROLLER_PATCHER_RESULT_OR_ERROR checkAndSetMouseMode(HID_Data * data);
2018-06-19 17:46:37 +02:00
/**
\brief Set the emulated sticks for a given VPAD data.
2018-06-19 17:46:37 +02:00
\param buffer: A pointer to the given VPAD Data.
\param last_emulatedSticks: A pointer to the button presses of the previous call. Will be updated while calling.
2018-06-19 17:46:37 +02:00
\return When the functions failed result < 0 is returned. If the result is >= 0 the function was successful.
**/
2018-06-20 15:07:15 +02:00
static CONTROLLER_PATCHER_RESULT_OR_ERROR setEmulatedSticks(VPADStatus * buffer, uint32_t * last_emulatedSticks);
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
* Other functions
*---------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
2018-06-19 17:46:37 +02:00
/** \brief Set the Pro Controller for a given VPAD data.
*
* \param vpad_buffer VPADStatus* A pointer to the given VPAD Data.
* \param pro_buffer KPADData* A pointer to the KPADData where the result will be stored.
2018-06-20 15:07:15 +02:00
* \param lastButtonsPressesPRO uint32_t* A pointer to the button presses of the previous call. Will be updated while calling.
2018-06-19 17:46:37 +02:00
* \return When the functions failed result < 0 is returned. If the result is >= 0 the function was successful.
*
*/
2018-06-20 15:07:15 +02:00
static CONTROLLER_PATCHER_RESULT_OR_ERROR translateToPro(VPADStatus * vpad_buffer, KPADStatus * pro_buffer, uint32_t * lastButtonsPressesPRO);
2018-06-19 17:46:37 +02:00
static CONTROLLER_PATCHER_RESULT_OR_ERROR translateToProWPADRead(VPADStatus * vpad_buffer,WPADStatusProController * pro_buffer);
2018-06-19 17:46:37 +02:00
/**
\brief Checks if the value at the given device + CONTRPS slot equals the expected value.
2018-06-19 17:46:37 +02:00
\param device_slot
\param CONTRPS_slot
\param expectedValue
2018-06-19 17:46:37 +02:00
\return When the functions failed result < 0 is returned. If the result is >= 0 the function was successful.
**/
2018-06-20 15:07:15 +02:00
static CONTROLLER_PATCHER_RESULT_OR_ERROR checkValueinConfigController(int32_t device_slot,int32_t CONTRPS_slot,int32_t expectedValue);
2018-06-19 17:46:37 +02:00
/**
2018-06-20 15:07:15 +02:00
\brief Sets two uint8_t values to the given pointer.
2018-06-19 17:46:37 +02:00
\param dest: pointer to the destination array.
\param first: Value that will be written in @p dest[0]
\param second: Value that will be written in @p dest[1]
**/
2018-06-20 15:07:15 +02:00
static void setConfigValue(uint8_t * dest , uint8_t first, uint8_t second);
2018-06-19 17:46:37 +02:00
/**
\brief Saves a new free device slot and the corresponding HID-Mask in the given @p HIDSlotData pointer
2018-06-19 17:46:37 +02:00
\param slotdata Pointer to the HIDSlotData struct where the result will be saved.
2018-06-19 17:46:37 +02:00
\return When the functions failed result < 0 is returned. If the result is >= 0 the function was successful.
**/
static CONTROLLER_PATCHER_RESULT_OR_ERROR getNextSlotData(HIDSlotData * slotdata);
2018-06-19 17:46:37 +02:00
/**
\brief Fills up a given DeviceInfo, which provides a valid VID/PID, with HIDSlotData.
2018-06-19 17:46:37 +02:00
\param info Pointer the target DeviceInfo. The VID/PID need to be set, the HIDSlotData will be filled with data.
2018-06-19 17:46:37 +02:00
\return When the functions failed result < 0 is returned. If the result is >= 0 the function was successful.
**/
static CONTROLLER_PATCHER_RESULT_OR_ERROR getDeviceInfoFromVidPid(DeviceInfo * info);
2018-06-19 17:46:37 +02:00
/**
\brief returns the internal slot number of the device. Some adapters have multiple slot and send the data for each one
alternating (with an identifier at the beginning). This function searches for the identifier (if it's set) and returns the
slot number relative to this pad.
2018-06-19 17:46:37 +02:00
\param device slot
\param current input data
\return The relative slot in the device
**/
2018-06-20 15:07:15 +02:00
static int32_t getPadSlotInAdapter(int32_t deviceslot, uint8_t * input_data);
2018-06-19 17:46:37 +02:00
/**
\brief returns a pointer to the ControllerMapping to the given controller type
2018-06-19 17:46:37 +02:00
\param type controller type
2018-06-19 17:46:37 +02:00
\return pointer to ControllerMapping data, null is type was invalid
**/
static ControllerMappingPAD * getControllerMappingByType(UController_Type type);
2018-06-20 15:07:15 +02:00
static CONTROLLER_PATCHER_RESULT_OR_ERROR doSampling(uint16_t deviceslot,uint8_t padslot,BOOL ignorePadSlot);
2018-06-20 15:07:15 +02:00
static CONTROLLER_PATCHER_RESULT_OR_ERROR doSamplingSingle(WPADChan chan, uint16_t deviceslot, uint8_t padslot, BOOL ignorePadSlot);
};
#endif /* _CONTROLLER_PATCHER_UTIL_H_ */