RequestHandler: Add GetInputList

This commit is contained in:
tt2468 2021-06-16 00:49:07 -07:00
parent 9af7216891
commit aac06b101e
4 changed files with 56 additions and 0 deletions

View File

@ -90,6 +90,7 @@ set(obs-websocket_SOURCES
src/requesthandler/RequestHandler_Config.cpp
src/requesthandler/RequestHandler_Sources.cpp
src/requesthandler/RequestHandler_Scenes.cpp
src/requesthandler/RequestHandler_Inputs.cpp
src/requesthandler/rpc/Request.cpp
src/requesthandler/rpc/RequestResult.cpp
src/forms/SettingsDialog.cpp

View File

@ -36,6 +36,9 @@ const std::map<std::string, RequestMethodHandler> RequestHandler::_handlerMap
{"SetSceneName", &RequestHandler::SetSceneName},
{"CreateScene", &RequestHandler::CreateScene},
{"RemoveScene", &RequestHandler::RemoveScene},
// Inputs
{"GetInputList", &RequestHandler::GetInputList},
};
RequestResult RequestHandler::ProcessRequest(const Request& request)

View File

@ -51,5 +51,8 @@ class RequestHandler {
RequestResult CreateScene(const Request&);
RequestResult RemoveScene(const Request&);
// Inputs
RequestResult GetInputList(const Request&);
static const std::map<std::string, RequestMethodHandler> _handlerMap;
};

View File

@ -0,0 +1,49 @@
#include "RequestHandler.h"
#include "../plugin-macros.generated.h"
struct EnumInputInfo {
std::string inputKind;
std::vector<json> inputs;
};
RequestResult RequestHandler::GetInputList(const Request& request)
{
EnumInputInfo inputInfo;
if (request.RequestData.contains("inputKind") && !request.RequestData["inputKind"].is_null()) {
RequestStatus::RequestStatus statusCode;
std::string comment;
if (!request.ValidateString("inputKind", statusCode, comment)) {
return RequestResult::Error(statusCode, comment);
}
inputInfo.inputKind = request.RequestData["inputKind"];
}
auto inputEnumProc = [](void *param, obs_source_t *input) {
// Sanity check in case the API changes
if (obs_source_get_type(input) != OBS_SOURCE_TYPE_INPUT)
return true;
auto inputInfo = reinterpret_cast<EnumInputInfo*>(param);
std::string inputKind = obs_source_get_id(input);
if (!inputInfo->inputKind.empty() && inputInfo->inputKind != inputKind)
return true;
json inputJson;
inputJson["inputName"] = obs_source_get_name(input);
inputJson["inputKind"] = inputKind;
inputInfo->inputs.push_back(inputJson);
return true;
};
// Actually enumerates only public inputs, despite the name
obs_enum_sources(inputEnumProc, &inputInfo);
json responseData;
responseData["inputs"] = inputInfo.inputs;
return RequestResult::Success(responseData);
}