Skip to content

Commit

Permalink
requesthandler: Add optional context to TriggerHotkeyByName
Browse files Browse the repository at this point in the history
  • Loading branch information
exeldro authored and tt2468 committed Jan 9, 2024
1 parent 7a1c71b commit f43ef8e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/requesthandler/RequestHandler_General.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.");

Expand Down
2 changes: 1 addition & 1 deletion src/utils/Obs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 41 additions & 2 deletions src/utils/Obs_SearchHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,55 @@ 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;

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;
Expand Down

0 comments on commit f43ef8e

Please sign in to comment.