hid_to_vpad/src/ConfigHooks.cpp

158 lines
6.6 KiB
C++
Raw Normal View History

/****************************************************************************
* Copyright (C) 2018 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/>.
****************************************************************************/
2023-04-10 12:52:56 +02:00
#include "WUPSConfigItemPadMapping.h"
#include "utils/StringTools.h"
#include "utils/logger.h"
#include <controller_patcher/ControllerPatcher.hpp>
2024-10-13 10:35:27 +02:00
#include <coreinit/debug.h>
#include <wups.h>
#include <wups/config/WUPSConfigItemBoolean.h>
2023-04-10 12:52:05 +02:00
bool runNetworkClient = true;
2021-09-19 21:15:58 +02:00
2024-10-13 10:35:27 +02:00
void loadMapping(const std::string &persistedValue, UController_Type type);
2021-09-19 21:15:58 +02:00
void ConfigLoad() {
WUPS_OpenStorage();
2024-10-13 10:35:27 +02:00
bool rumble = false;
2021-09-19 21:15:58 +02:00
2023-04-10 12:52:56 +02:00
if (WUPS_GetBool(nullptr, "rumble", &rumble) == WUPS_STORAGE_ERROR_SUCCESS) {
2024-10-13 10:35:27 +02:00
DEBUG_FUNCTION_LINE("Set rumble to %d", rumble);
2021-09-19 21:15:58 +02:00
ControllerPatcher::setRumbleActivated(rumble);
2023-04-10 12:52:56 +02:00
} else {
2023-04-10 12:52:05 +02:00
WUPS_StoreBool(nullptr, "rumble", ControllerPatcher::isRumbleActivated());
2021-09-19 21:15:58 +02:00
}
2023-04-10 12:52:56 +02:00
if (WUPS_GetBool(nullptr, "networkclient", &runNetworkClient) != WUPS_STORAGE_ERROR_SUCCESS) {
2023-04-10 12:52:05 +02:00
WUPS_StoreBool(nullptr, "networkclient", runNetworkClient);
2021-09-19 21:15:58 +02:00
}
char buffer[512];
2023-04-10 12:52:56 +02:00
if (WUPS_GetString(nullptr, "gamepadmapping", buffer, sizeof(buffer)) == WUPS_STORAGE_ERROR_SUCCESS) {
2023-04-10 12:52:05 +02:00
std::string stringWrapper = buffer;
loadMapping(stringWrapper, UController_Type_Gamepad);
2021-09-19 21:15:58 +02:00
}
2024-10-13 10:35:27 +02:00
if (WUPS_GetString(nullptr, "pro1", buffer, sizeof(buffer)) == WUPS_STORAGE_ERROR_SUCCESS) {
std::string stringWrapper = buffer;
loadMapping(stringWrapper, UController_Type_Pro1);
}
if (WUPS_GetString(nullptr, "pro2", buffer, sizeof(buffer)) == WUPS_STORAGE_ERROR_SUCCESS) {
std::string stringWrapper = buffer;
loadMapping(stringWrapper, UController_Type_Pro2);
}
if (WUPS_GetString(nullptr, "pro3", buffer, sizeof(buffer)) == WUPS_STORAGE_ERROR_SUCCESS) {
std::string stringWrapper = buffer;
loadMapping(stringWrapper, UController_Type_Pro3);
}
if (WUPS_GetString(nullptr, "pro4", buffer, sizeof(buffer)) == WUPS_STORAGE_ERROR_SUCCESS) {
std::string stringWrapper = buffer;
loadMapping(stringWrapper, UController_Type_Pro4);
}
2021-09-19 21:15:58 +02:00
WUPS_CloseStorage();
}
2024-10-13 10:35:27 +02:00
void loadMapping(const std::string &persistedValue, UController_Type controllerType) {
2023-04-10 12:52:56 +02:00
if (persistedValue.empty()) {
2021-09-19 21:15:58 +02:00
// No device mapped.
return;
}
std::vector<std::string> result = StringTools::stringSplit(persistedValue, ",");
2023-04-10 12:52:56 +02:00
if (result.size() != 4) {
2021-09-19 21:15:58 +02:00
return;
}
ControllerMappingPADInfo mappedPadInfo;
mappedPadInfo.vidpid.vid = atoi(result.at(0).c_str());
mappedPadInfo.vidpid.pid = atoi(result.at(1).c_str());
2023-04-10 12:52:56 +02:00
mappedPadInfo.pad = atoi(result.at(2).c_str());
mappedPadInfo.type = CM_Type_Controller; //atoi(result.at(3).c_str());
2021-09-19 21:15:58 +02:00
2023-04-10 12:52:56 +02:00
ControllerPatcher::addControllerMapping(controllerType, mappedPadInfo);
2021-09-19 21:15:58 +02:00
}
2023-04-10 12:52:56 +02:00
void rumbleChanged(ConfigItemBoolean *item, bool newValue) {
DEBUG_FUNCTION_LINE("rumbleChanged %d ", newValue);
ControllerPatcher::setRumbleActivated(newValue);
2021-09-19 21:15:58 +02:00
WUPS_StoreInt(nullptr, "rumble", newValue);
}
2023-04-10 12:52:56 +02:00
void networkClientChanged(ConfigItemBoolean *item, bool newValue) {
DEBUG_FUNCTION_LINE("Trigger network %d", newValue);
ControllerPatcher::setNetworkControllerActivated(newValue);
2023-04-10 12:52:56 +02:00
if (newValue) {
ControllerPatcher::startNetworkServer();
} else {
ControllerPatcher::stopNetworkServer();
}
2021-09-19 21:15:58 +02:00
WUPS_StoreInt(nullptr, "networkclient", newValue);
}
2023-04-10 12:52:56 +02:00
void PadMappingUpdated(ConfigItemPadMapping *item) {
2024-10-13 10:35:27 +02:00
ControllerPatcher::resetControllerMapping(item->controllerType);
2023-04-10 12:52:56 +02:00
if (item->mappedPadInfo.active && item->mappedPadInfo.type == CM_Type_Controller) {
auto res = StringTools::strfmt("%d,%d,%d,%d", item->mappedPadInfo.vidpid.vid, item->mappedPadInfo.vidpid.pid, item->mappedPadInfo.pad, item->mappedPadInfo.type);
2021-09-19 21:15:58 +02:00
WUPS_StoreString(nullptr, item->configId, res.c_str());
2024-10-13 10:35:27 +02:00
loadMapping(res, item->controllerType);
return;
2021-09-19 21:15:58 +02:00
}
2024-10-13 10:35:27 +02:00
WUPS_StoreString(nullptr, item->configId, "");
2021-09-19 21:15:58 +02:00
}
2024-10-13 10:35:27 +02:00
bool gConfigMenuOpen = false;
2023-04-10 12:52:56 +02:00
WUPS_CONFIG_CLOSED() {
2024-10-13 10:35:27 +02:00
gConfigMenuOpen = false;
// Save all changes
if (WUPS_CloseStorage() != WUPS_STORAGE_ERROR_SUCCESS) {
OSReport("Failed to close storage\n");
2023-04-10 12:52:56 +02:00
}
2024-10-13 10:35:27 +02:00
}
2023-04-10 12:52:05 +02:00
2024-10-13 10:35:27 +02:00
#define CONFIG_PadMapping_AddToCategory(__config__, category, config_id, display_name, controller_type, callback) \
if (!WUPSConfigItemPadMapping_AddToCategory(category, config_id, display_name, controller_type, callback)) { \
WUPSConfig_Destroy(__config__); \
return 0; \
2023-04-10 12:52:05 +02:00
}
WUPS_GET_CONFIG() {
2021-09-19 21:15:58 +02:00
WUPS_OpenStorage();
2024-10-13 10:35:27 +02:00
gConfigMenuOpen = true;
2021-09-19 21:15:58 +02:00
WUPSConfigHandle config;
2024-10-13 10:35:27 +02:00
WUPSConfig_CreateHandled(&config, "HID to VPAD");
2023-04-10 12:52:56 +02:00
2023-04-10 12:52:05 +02:00
WUPSConfigCategoryHandle catMapping;
2021-09-19 21:15:58 +02:00
WUPSConfigCategoryHandle catOther;
2023-04-10 12:52:56 +02:00
2024-10-13 10:35:27 +02:00
WUPSConfig_AddCategoryByNameHandled(config, "Mapping", &catMapping);
WUPSConfig_AddCategoryByNameHandled(config, "Other", &catOther);
WUPSConfigItemBoolean_AddToCategoryHandledEx(config, catOther, "rumble", "Rumble", ControllerPatcher::isRumbleActivated(), &rumbleChanged, "On", "Off");
WUPSConfigItemBoolean_AddToCategoryHandledEx(config, catOther, "networkclient", "Network Client", runNetworkClient, &networkClientChanged, "On", "Off");
2023-04-10 12:52:05 +02:00
2024-10-13 10:35:27 +02:00
CONFIG_PadMapping_AddToCategory(config, catMapping, "gamepadmapping", "Gamepad", UController_Type_Gamepad, &PadMappingUpdated);
CONFIG_PadMapping_AddToCategory(config, catMapping, "pro1", "Pro Controller 1", UController_Type_Pro1, &PadMappingUpdated);
CONFIG_PadMapping_AddToCategory(config, catMapping, "pro2", "Pro Controller 2", UController_Type_Pro2, &PadMappingUpdated);
CONFIG_PadMapping_AddToCategory(config, catMapping, "pro3", "Pro Controller 3", UController_Type_Pro3, &PadMappingUpdated);
CONFIG_PadMapping_AddToCategory(config, catMapping, "pro4", "Pro Controller 4", UController_Type_Pro4, &PadMappingUpdated);
return config;
}