From 4a00d169e1b8561e1d0e9fb9e449a803c9eb8937 Mon Sep 17 00:00:00 2001 From: tt2468 Date: Mon, 7 Jun 2021 01:14:32 -0700 Subject: [PATCH] RequestHandler: Add some config requests --- CMakeLists.txt | 1 + src/requesthandler/RequestHandler.cpp | 8 ++ src/requesthandler/RequestHandler.h | 9 ++ src/requesthandler/RequestHandler_Config.cpp | 131 +++++++++++++++++++ 4 files changed, 149 insertions(+) create mode 100644 src/requesthandler/RequestHandler_Config.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index accb6a92..adb4cf82 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -87,6 +87,7 @@ set(obs-websocket_SOURCES src/eventhandler/EventHandler_MediaInputs.cpp src/requesthandler/RequestHandler.cpp src/requesthandler/RequestHandler_General.cpp + src/requesthandler/RequestHandler_Config.cpp src/requesthandler/rpc/Request.cpp src/requesthandler/rpc/RequestResult.cpp src/forms/SettingsDialog.cpp diff --git a/src/requesthandler/RequestHandler.cpp b/src/requesthandler/RequestHandler.cpp index 529bd3ff..73bb0586 100644 --- a/src/requesthandler/RequestHandler.cpp +++ b/src/requesthandler/RequestHandler.cpp @@ -13,6 +13,14 @@ const std::map RequestHandler::_handlerMap {"GetStudioModeEnabled", &RequestHandler::GetStudioModeEnabled}, {"SetStudioModeEnabled", &RequestHandler::SetStudioModeEnabled}, {"Sleep", &RequestHandler::Sleep}, + + // Config + {"GetSceneCollectionList", &RequestHandler::GetSceneCollectionList}, + {"SetCurrentSceneCollection", &RequestHandler::SetCurrentSceneCollection}, + {"GetProfileList", &RequestHandler::GetProfileList}, + {"SetCurrentProfile", &RequestHandler::SetCurrentProfile}, + {"GetProfileParameter", &RequestHandler::GetProfileParameter}, + {"SetProfileParameter", &RequestHandler::SetProfileParameter}, }; RequestResult RequestHandler::ProcessRequest(const Request& request) diff --git a/src/requesthandler/RequestHandler.h b/src/requesthandler/RequestHandler.h index 7458fcdc..faa97ed3 100644 --- a/src/requesthandler/RequestHandler.h +++ b/src/requesthandler/RequestHandler.h @@ -17,6 +17,7 @@ class RequestHandler { std::vector GetRequestList(); private: + // General RequestResult GetVersion(const Request&); RequestResult BroadcastCustomEvent(const Request&); RequestResult GetHotkeyList(const Request&); @@ -26,5 +27,13 @@ class RequestHandler { RequestResult SetStudioModeEnabled(const Request&); RequestResult Sleep(const Request&); + // Config + RequestResult GetSceneCollectionList(const Request&); + RequestResult SetCurrentSceneCollection(const Request&); + RequestResult GetProfileList(const Request&); + RequestResult SetCurrentProfile(const Request&); + RequestResult GetProfileParameter(const Request&); + RequestResult SetProfileParameter(const Request&); + static const std::map _handlerMap; }; diff --git a/src/requesthandler/RequestHandler_Config.cpp b/src/requesthandler/RequestHandler_Config.cpp new file mode 100644 index 00000000..0ace56fb --- /dev/null +++ b/src/requesthandler/RequestHandler_Config.cpp @@ -0,0 +1,131 @@ +#include + +#include "RequestHandler.h" + +#include "../plugin-macros.generated.h" + +RequestResult RequestHandler::GetSceneCollectionList(const Request& request) +{ + json responseData; + + responseData["currentSceneCollectionName"] = Utils::Obs::StringHelper::GetCurrentSceneCollection(); + responseData["sceneCollections"] = Utils::Obs::ListHelper::GetSceneCollectionList(); + + return RequestResult::Success(responseData); +} + +RequestResult RequestHandler::SetCurrentSceneCollection(const Request& request) +{ + RequestStatus::RequestStatus statusCode; + std::string comment; + if (!request.ValidateString("sceneCollectionName", statusCode, comment)) { + return RequestResult::Error(statusCode, comment); + } + + std::string currentSceneCollectionName = Utils::Obs::StringHelper::GetCurrentSceneCollection(); + std::string sceneCollectionName = request.RequestData["sceneCollectionName"]; + + auto sceneCollections = Utils::Obs::ListHelper::GetSceneCollectionList(); + if (std::find(sceneCollections.begin(), sceneCollections.end(), sceneCollectionName) == sceneCollections.end()) + return RequestResult::Error(RequestStatus::SceneCollectionNotFound, "Your specified scene collection was not found."); + + // Avoid queueing tasks if nothing will change + if (currentSceneCollectionName != sceneCollectionName) { + obs_queue_task(OBS_TASK_UI, [](void* param) { + obs_frontend_set_current_scene_collection(reinterpret_cast(param)); + }, (void*)sceneCollectionName.c_str(), true); + } + + return RequestResult::Success(); +} + +RequestResult RequestHandler::GetProfileList(const Request& request) +{ + json responseData; + + responseData["currentProfileName"] = Utils::Obs::StringHelper::GetCurrentProfile(); + responseData["profiles"] = Utils::Obs::ListHelper::GetProfileList(); + + return RequestResult::Success(responseData); +} + +RequestResult RequestHandler::SetCurrentProfile(const Request& request) +{ + RequestStatus::RequestStatus statusCode; + std::string comment; + if (!request.ValidateString("profileName", statusCode, comment)) { + return RequestResult::Error(statusCode, comment); + } + + std::string currentProfileName = Utils::Obs::StringHelper::GetCurrentProfile(); + std::string profileName = request.RequestData["profileName"]; + + auto profiles = Utils::Obs::ListHelper::GetProfileList(); + if (std::find(profiles.begin(), profiles.end(), profileName) == profiles.end()) + return RequestResult::Error(RequestStatus::ProfileNotFound, "Your specified profile was not found."); + + // Avoid queueing tasks if nothing will change + if (currentProfileName != profileName) { + obs_queue_task(OBS_TASK_UI, [](void* param) { + obs_frontend_set_current_profile(reinterpret_cast(param)); + }, (void*)profileName.c_str(), true); + } + + return RequestResult::Success(); +} + +RequestResult RequestHandler::GetProfileParameter(const Request& request) +{ + RequestStatus::RequestStatus statusCode; + std::string comment; + if (!request.ValidateString("parameterCategory", statusCode, comment) || !request.ValidateString("parameterName", statusCode, comment)) { + return RequestResult::Error(statusCode, comment); + } + + std::string parameterCategory = request.RequestData["parameterCategory"]; + std::string parameterName = request.RequestData["parameterName"]; + + config_t* profile = obs_frontend_get_profile_config(); + + json responseData; + if (config_has_default_value(profile, parameterCategory.c_str(), parameterName.c_str())) { + responseData["parameterValue"] = + responseData["defaultParameterValue"] = config_get_default_string(profile, parameterCategory.c_str(), parameterName.c_str()); + } else { + if (config_has_user_value(profile, parameterCategory.c_str(), parameterName.c_str())) + responseData["parameterValue"] = config_get_string(profile, parameterCategory.c_str(), parameterName.c_str()); + else + responseData["parameterValue"] = nullptr; + responseData["defaultParameterValue"] = nullptr; + } + + return RequestResult::Success(responseData); +} + +RequestResult RequestHandler::SetProfileParameter(const Request& request) +{ + RequestStatus::RequestStatus statusCode; + std::string comment; + if (!request.ValidateString("parameterCategory", statusCode, comment) || + !request.ValidateString("parameterName", statusCode, comment)) { + return RequestResult::Error(statusCode, comment); + } + + std::string parameterCategory = request.RequestData["parameterCategory"]; + std::string parameterName = request.RequestData["parameterName"]; + + config_t* profile = obs_frontend_get_profile_config(); + + // Using check helpers here would just make the logic more complicated + if (!request.RequestData.contains("parameterValue") || request.RequestData["parameterValue"].is_null()) { + if (!config_remove_value(profile, parameterCategory.c_str(), parameterName.c_str())) + return RequestResult::Error(RequestStatus::ConfigParameterNotFound); + } else if (request.RequestData["parameterValue"].is_string()) { + std::string parameterValue = request.RequestData["parameterValue"]; + config_set_string(profile, parameterCategory.c_str(), parameterName.c_str(), parameterValue.c_str()); + } else { + return RequestResult::Error(RequestStatus::InvalidRequestParameterDataType, "The parameter `parameterValue` must be a string."); + } + + return RequestResult::Success(); +}