Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

requesthandler: Add optional context to TriggerHotkeyByName #1187

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
exeldro marked this conversation as resolved.
Show resolved Hide resolved
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
Loading