From bac5b1551b229c016916fbfb36900fb4181fb2f1 Mon Sep 17 00:00:00 2001 From: LorenaGdL Date: Tue, 29 Sep 2020 13:38:32 +0000 Subject: [PATCH 1/4] Requests: Add ProcessHotkeyByName Adds a new request called `ProcessHotkeyByName` which calls the routine associated with a given hotkey. The hotkey is identified by the unique name set while registering it. Works with both OBS default hotkeys and hotkeys added by plugins/scripts. --- src/WSRequestHandler.cpp | 2 ++ src/WSRequestHandler.h | 2 ++ src/WSRequestHandler_General.cpp | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/src/WSRequestHandler.cpp b/src/WSRequestHandler.cpp index 56c4cdaa..2c92305a 100644 --- a/src/WSRequestHandler.cpp +++ b/src/WSRequestHandler.cpp @@ -43,6 +43,8 @@ const QHash WSRequestHandler::messageMap { { "BroadcastCustomMessage", &WSRequestHandler::BroadcastCustomMessage }, + { "ProcessHotkeyByName", &WSRequestHandler::ProcessHotkeyByName }, + { "SetCurrentScene", &WSRequestHandler::SetCurrentScene }, { "GetCurrentScene", &WSRequestHandler::GetCurrentScene }, { "GetSceneList", &WSRequestHandler::GetSceneList }, diff --git a/src/WSRequestHandler.h b/src/WSRequestHandler.h index e409db50..7a4e64ff 100644 --- a/src/WSRequestHandler.h +++ b/src/WSRequestHandler.h @@ -61,6 +61,8 @@ class WSRequestHandler { RpcResponse BroadcastCustomMessage(const RpcRequest&); + RpcResponse ProcessHotkeyByName(const RpcRequest&); + RpcResponse SetCurrentScene(const RpcRequest&); RpcResponse GetCurrentScene(const RpcRequest&); RpcResponse GetSceneList(const RpcRequest&); diff --git a/src/WSRequestHandler_General.cpp b/src/WSRequestHandler_General.cpp index ea1c6a63..54675a84 100644 --- a/src/WSRequestHandler_General.cpp +++ b/src/WSRequestHandler_General.cpp @@ -345,3 +345,24 @@ RpcResponse WSRequestHandler::OpenProjector(const RpcRequest& request) { obs_frontend_open_projector(type, monitor, geometry, name); return request.success(); } + +/** +* Executes hotkey routine, identified by hotkey unique name +* +* @param {String} `name` Unique name of the hotkey, as defined when registering the hotkey (e.g. "ReplayBuffer.Save") +* +* @api requests +* @name ProcessHotkeyByName +* @category general +* @since unreleased +*/ +RpcResponse WSRequestHandler::ProcessHotkeyByName(const RpcRequest& request) { + const char* name = obs_data_get_string(request.parameters(), "name"); + + obs_hotkey_t* hk = Utils::FindHotkeyByName(name); + if (!hk) { + return request.failed("Hotkey not found"); + } + obs_hotkey_trigger_routed_callback(obs_hotkey_get_id(hk), true); + return request.success(); +} From 9daae857de24753f1b85461f05acaab23516cee8 Mon Sep 17 00:00:00 2001 From: LorenaGdL Date: Tue, 29 Sep 2020 13:43:00 +0000 Subject: [PATCH 2/4] Requests: Add ProcessHotkeyByCombination Adds a new request called `ProcessHotkeyByCombination` which calls the routine associated with a given hotkey. The hotkey is identified by the combination of keys set by the user to trigger it. Multiple routines are called if the same combination is bound to different hotkeys. --- src/WSRequestHandler.cpp | 1 + src/WSRequestHandler.h | 1 + src/WSRequestHandler_General.cpp | 45 ++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/src/WSRequestHandler.cpp b/src/WSRequestHandler.cpp index 2c92305a..9b62546c 100644 --- a/src/WSRequestHandler.cpp +++ b/src/WSRequestHandler.cpp @@ -44,6 +44,7 @@ const QHash WSRequestHandler::messageMap { { "BroadcastCustomMessage", &WSRequestHandler::BroadcastCustomMessage }, { "ProcessHotkeyByName", &WSRequestHandler::ProcessHotkeyByName }, + { "ProcessHotkeyByCombination", &WSRequestHandler::ProcessHotkeyByCombination }, { "SetCurrentScene", &WSRequestHandler::SetCurrentScene }, { "GetCurrentScene", &WSRequestHandler::GetCurrentScene }, diff --git a/src/WSRequestHandler.h b/src/WSRequestHandler.h index 7a4e64ff..f8849e84 100644 --- a/src/WSRequestHandler.h +++ b/src/WSRequestHandler.h @@ -62,6 +62,7 @@ class WSRequestHandler { RpcResponse BroadcastCustomMessage(const RpcRequest&); RpcResponse ProcessHotkeyByName(const RpcRequest&); + RpcResponse ProcessHotkeyByCombination(const RpcRequest&); RpcResponse SetCurrentScene(const RpcRequest&); RpcResponse GetCurrentScene(const RpcRequest&); diff --git a/src/WSRequestHandler_General.cpp b/src/WSRequestHandler_General.cpp index 54675a84..0fc5d619 100644 --- a/src/WSRequestHandler_General.cpp +++ b/src/WSRequestHandler_General.cpp @@ -366,3 +366,48 @@ RpcResponse WSRequestHandler::ProcessHotkeyByName(const RpcRequest& request) { obs_hotkey_trigger_routed_callback(obs_hotkey_get_id(hk), true); return request.success(); } + +/** +* Executes hotkey routine, identified by bound combination of keys. A single key combination might trigger multiple hotkey routines depending on user settings +* +* @param {String} `key` Main key identifier (e.g. `OBS_KEY_A` for key "A"). Available identifiers [here](https://github.com/obsproject/obs-studio/blob/master/libobs/obs-hotkeys.h) +* @param {Object (Optional)} `modifiers` Optional key modifiers object: `{"shift":true/false, "alt":true/false, "control":true/false, "command":true/false}`. False entries can be ommitted +* +* @api requests +* @name ProcessHotkeyByCombination +* @category general +* @since unreleased +*/ +RpcResponse WSRequestHandler::ProcessHotkeyByCombination(const RpcRequest& request) { + if (!request.hasField("key")) { + return request.failed("Missing request key parameter"); + } + + OBSDataAutoRelease data = obs_data_get_obj(request.parameters(), "modifiers"); + + obs_key_combination_t combo = {0}; + uint32_t modifiers = 0; + if (obs_data_get_bool(data, "shift")) + modifiers |= INTERACT_SHIFT_KEY; + if (obs_data_get_bool(data, "control")) + modifiers |= INTERACT_CONTROL_KEY; + if (obs_data_get_bool(data, "alt")) + modifiers |= INTERACT_ALT_KEY; + if (obs_data_get_bool(data, "command")) + modifiers |= INTERACT_COMMAND_KEY; + + combo.modifiers = modifiers; + combo.key = obs_key_from_name(obs_data_get_string(request.parameters(), "key")); + + if (!modifiers + && (combo.key == OBS_KEY_NONE || combo.key >= OBS_KEY_LAST_VALUE)) { + return request.failed("Invalid key-modifier combination"); + } + + // Inject hotkey press-release sequence + obs_hotkey_inject_event(combo, false); + obs_hotkey_inject_event(combo, true); + obs_hotkey_inject_event(combo, false); + + return request.success(); +} From 449ad6d13c1fcf92a81466939a5186b37a540bf6 Mon Sep 17 00:00:00 2001 From: LorenaGdL Date: Wed, 30 Sep 2020 19:15:23 +0000 Subject: [PATCH 3/4] PR review changes: Modify requests names Changes requests names to "TriggerHotkeyByName" and "TriggerHotkeyBySequence" --- src/WSRequestHandler.cpp | 4 ++-- src/WSRequestHandler.h | 4 ++-- src/WSRequestHandler_General.cpp | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/WSRequestHandler.cpp b/src/WSRequestHandler.cpp index 9b62546c..b4ab8137 100644 --- a/src/WSRequestHandler.cpp +++ b/src/WSRequestHandler.cpp @@ -43,8 +43,8 @@ const QHash WSRequestHandler::messageMap { { "BroadcastCustomMessage", &WSRequestHandler::BroadcastCustomMessage }, - { "ProcessHotkeyByName", &WSRequestHandler::ProcessHotkeyByName }, - { "ProcessHotkeyByCombination", &WSRequestHandler::ProcessHotkeyByCombination }, + { "TriggerHotkeyByName", &WSRequestHandler::TriggerHotkeyByName }, + { "TriggerHotkeyBySequence", &WSRequestHandler::TriggerHotkeyBySequence }, { "SetCurrentScene", &WSRequestHandler::SetCurrentScene }, { "GetCurrentScene", &WSRequestHandler::GetCurrentScene }, diff --git a/src/WSRequestHandler.h b/src/WSRequestHandler.h index f8849e84..c3190668 100644 --- a/src/WSRequestHandler.h +++ b/src/WSRequestHandler.h @@ -61,8 +61,8 @@ class WSRequestHandler { RpcResponse BroadcastCustomMessage(const RpcRequest&); - RpcResponse ProcessHotkeyByName(const RpcRequest&); - RpcResponse ProcessHotkeyByCombination(const RpcRequest&); + RpcResponse TriggerHotkeyByName(const RpcRequest&); + RpcResponse TriggerHotkeyBySequence(const RpcRequest&); RpcResponse SetCurrentScene(const RpcRequest&); RpcResponse GetCurrentScene(const RpcRequest&); diff --git a/src/WSRequestHandler_General.cpp b/src/WSRequestHandler_General.cpp index 0fc5d619..6f5cea2a 100644 --- a/src/WSRequestHandler_General.cpp +++ b/src/WSRequestHandler_General.cpp @@ -356,7 +356,7 @@ RpcResponse WSRequestHandler::OpenProjector(const RpcRequest& request) { * @category general * @since unreleased */ -RpcResponse WSRequestHandler::ProcessHotkeyByName(const RpcRequest& request) { +RpcResponse WSRequestHandler::TriggerHotkeyByName(const RpcRequest& request) { const char* name = obs_data_get_string(request.parameters(), "name"); obs_hotkey_t* hk = Utils::FindHotkeyByName(name); @@ -378,7 +378,7 @@ RpcResponse WSRequestHandler::ProcessHotkeyByName(const RpcRequest& request) { * @category general * @since unreleased */ -RpcResponse WSRequestHandler::ProcessHotkeyByCombination(const RpcRequest& request) { +RpcResponse WSRequestHandler::TriggerHotkeyBySequence(const RpcRequest& request) { if (!request.hasField("key")) { return request.failed("Missing request key parameter"); } From e7c8c1d6b60926e8ba520d524dff4ede876144c8 Mon Sep 17 00:00:00 2001 From: tt2468 Date: Wed, 30 Sep 2020 15:02:59 -0700 Subject: [PATCH 4/4] Requests: Update hotkey docs and rename some variables --- src/WSRequestHandler_General.cpp | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/WSRequestHandler_General.cpp b/src/WSRequestHandler_General.cpp index 6f5cea2a..a5f19143 100644 --- a/src/WSRequestHandler_General.cpp +++ b/src/WSRequestHandler_General.cpp @@ -349,19 +349,19 @@ RpcResponse WSRequestHandler::OpenProjector(const RpcRequest& request) { /** * Executes hotkey routine, identified by hotkey unique name * -* @param {String} `name` Unique name of the hotkey, as defined when registering the hotkey (e.g. "ReplayBuffer.Save") +* @param {String} `hotkeyName` Unique name of the hotkey, as defined when registering the hotkey (e.g. "ReplayBuffer.Save") * * @api requests -* @name ProcessHotkeyByName +* @name TriggerHotkeyByName * @category general * @since unreleased */ RpcResponse WSRequestHandler::TriggerHotkeyByName(const RpcRequest& request) { - const char* name = obs_data_get_string(request.parameters(), "name"); + const char* name = obs_data_get_string(request.parameters(), "hotkeyName"); obs_hotkey_t* hk = Utils::FindHotkeyByName(name); if (!hk) { - return request.failed("Hotkey not found"); + return request.failed("hotkey not found"); } obs_hotkey_trigger_routed_callback(obs_hotkey_get_id(hk), true); return request.success(); @@ -370,20 +370,24 @@ RpcResponse WSRequestHandler::TriggerHotkeyByName(const RpcRequest& request) { /** * Executes hotkey routine, identified by bound combination of keys. A single key combination might trigger multiple hotkey routines depending on user settings * -* @param {String} `key` Main key identifier (e.g. `OBS_KEY_A` for key "A"). Available identifiers [here](https://github.com/obsproject/obs-studio/blob/master/libobs/obs-hotkeys.h) -* @param {Object (Optional)} `modifiers` Optional key modifiers object: `{"shift":true/false, "alt":true/false, "control":true/false, "command":true/false}`. False entries can be ommitted +* @param {String} `keyId` Main key identifier (e.g. `OBS_KEY_A` for key "A"). Available identifiers [here](https://github.com/obsproject/obs-studio/blob/master/libobs/obs-hotkeys.h) +* @param {Object (Optional)} `keyModifiers` Optional key modifiers object. False entries can be ommitted +* @param {boolean} `keyModifiers.shift` Trigger Shift Key +* @param {boolean} `keyModifiers.alt` Trigger Alt Key +* @param {boolean} `keyModifiers.control` Trigger Control (Ctrl) Key +* @param {boolean} `keyModifiers.command` Trigger Command Key (Mac) * * @api requests -* @name ProcessHotkeyByCombination +* @name TriggerHotkeyByCombination * @category general * @since unreleased */ RpcResponse WSRequestHandler::TriggerHotkeyBySequence(const RpcRequest& request) { - if (!request.hasField("key")) { - return request.failed("Missing request key parameter"); + if (!request.hasField("keyId")) { + return request.failed("missing request keyId parameter"); } - OBSDataAutoRelease data = obs_data_get_obj(request.parameters(), "modifiers"); + OBSDataAutoRelease data = obs_data_get_obj(request.parameters(), "keyModifiers"); obs_key_combination_t combo = {0}; uint32_t modifiers = 0; @@ -397,11 +401,11 @@ RpcResponse WSRequestHandler::TriggerHotkeyBySequence(const RpcRequest& request) modifiers |= INTERACT_COMMAND_KEY; combo.modifiers = modifiers; - combo.key = obs_key_from_name(obs_data_get_string(request.parameters(), "key")); + combo.key = obs_key_from_name(obs_data_get_string(request.parameters(), "keyId")); if (!modifiers && (combo.key == OBS_KEY_NONE || combo.key >= OBS_KEY_LAST_VALUE)) { - return request.failed("Invalid key-modifier combination"); + return request.failed("invalid key-modifier combination"); } // Inject hotkey press-release sequence