diff --git a/src/requesthandler/RequestHandler_General.cpp b/src/requesthandler/RequestHandler_General.cpp index a8cb59bf..2a15803d 100644 --- a/src/requesthandler/RequestHandler_General.cpp +++ b/src/requesthandler/RequestHandler_General.cpp @@ -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."); diff --git a/src/utils/Obs.h b/src/utils/Obs.h index 0e646c83..9110a6cc 100644 --- a/src/utils/Obs.h +++ b/src/utils/Obs.h @@ -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 diff --git a/src/utils/Obs_SearchHelper.cpp b/src/utils/Obs_SearchHelper.cpp index e76c9645..7b98512a 100644 --- a/src/utils/Obs_SearchHelper.cpp +++ b/src/utils/Obs_SearchHelper.cpp @@ -19,7 +19,7 @@ with this program. If not, see #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;