requesthandler: Add optional context to TriggerHotkeyByName

This commit is contained in:
Exeldro 2023-12-03 08:43:36 +01:00 committed by tt2468
parent 7a1c71bb96
commit f43ef8e2da
3 changed files with 52 additions and 4 deletions

View File

@ -233,6 +233,7 @@ RequestResult RequestHandler::GetHotkeyList(const Request &)
* Triggers a hotkey using its name. See `GetHotkeyList`
*
* @requestField hotkeyName | String | Name of the hotkey to trigger
* @requestField ?contextName | String | Name of context of the hotkey to trigger
*
* @requestType TriggerHotkeyByName
* @complexity 3
@ -248,7 +249,15 @@ RequestResult RequestHandler::TriggerHotkeyByName(const Request &request)
if (!request.ValidateString("hotkeyName", statusCode, comment))
return RequestResult::Error(statusCode, comment);
obs_hotkey_t *hotkey = Utils::Obs::SearchHelper::GetHotkeyByName(request.RequestData["hotkeyName"]);
std::string contextName;
if (request.Contains("contextName")) {
if (!request.ValidateOptionalString("contextName", statusCode, comment))
return RequestResult::Error(statusCode, comment);
contextName = request.RequestData["contextName"];
}
obs_hotkey_t *hotkey = Utils::Obs::SearchHelper::GetHotkeyByName(request.RequestData["hotkeyName"], contextName);
if (!hotkey)
return RequestResult::Error(RequestStatus::ResourceNotFound, "No hotkeys were found by that name.");

View File

@ -297,7 +297,7 @@ namespace Utils {
}
namespace SearchHelper {
obs_hotkey_t *GetHotkeyByName(std::string name);
obs_hotkey_t *GetHotkeyByName(std::string name, std::string context);
obs_source_t *GetSceneTransitionByName(std::string name); // Increments source ref. Use OBSSourceAutoRelease
obs_sceneitem_t *GetSceneItemByName(obs_scene_t *scene, std::string name,
int offset = 0); // Increments ref. Use OBSSceneItemAutoRelease

View File

@ -19,7 +19,7 @@ with this program. If not, see <https://www.gnu.org/licenses/>
#include "Obs.h"
#include "plugin-macros.generated.h"
obs_hotkey_t *Utils::Obs::SearchHelper::GetHotkeyByName(std::string name)
obs_hotkey_t *Utils::Obs::SearchHelper::GetHotkeyByName(std::string name, std::string context)
{
if (name.empty())
return nullptr;
@ -27,8 +27,47 @@ obs_hotkey_t *Utils::Obs::SearchHelper::GetHotkeyByName(std::string name)
auto hotkeys = ArrayHelper::GetHotkeyList();
for (auto hotkey : hotkeys) {
if (obs_hotkey_get_name(hotkey) == name)
if (obs_hotkey_get_name(hotkey) != name)
continue;
if (context.empty())
return hotkey;
auto type = obs_hotkey_get_registerer_type(hotkey);
if (type == OBS_HOTKEY_REGISTERER_SOURCE) {
OBSSourceAutoRelease source = obs_weak_source_get_source((obs_weak_source_t *)obs_hotkey_get_registerer(hotkey));
if (!source)
continue;
if (context != obs_source_get_name(source))
continue;
} else if (type == OBS_HOTKEY_REGISTERER_OUTPUT) {
OBSOutputAutoRelease output = obs_weak_output_get_output((obs_weak_output_t *)obs_hotkey_get_registerer(hotkey));
if (!output)
continue;
if (context != obs_output_get_name(output))
continue;
} else if (type == OBS_HOTKEY_REGISTERER_ENCODER) {
OBSEncoderAutoRelease encoder = obs_weak_encoder_get_encoder((obs_weak_encoder_t *)obs_hotkey_get_registerer(hotkey));
if (!encoder)
continue;
if (context != obs_encoder_get_name(encoder))
continue;
} else if (type == OBS_HOTKEY_REGISTERER_SERVICE) {
OBSServiceAutoRelease service = obs_weak_service_get_service((obs_weak_service_t *)obs_hotkey_get_registerer(hotkey));
if (!service)
continue;
if (context != obs_service_get_name(service))
continue;
}
return hotkey;
}
return nullptr;