From 9ca1e85f7d45d1a6cd7ad98063660d3b4774c8a3 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Mon, 22 Jul 2019 01:11:34 -0700 Subject: [PATCH 1/3] UI: Fix hotkeys working even when disabled in focus Fixes a bug where the hotkey inject would trigger hotkeys despite hotkeys being disabled when in focus. --- UI/obs-app.cpp | 6 ++++++ UI/obs-app.hpp | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/UI/obs-app.cpp b/UI/obs-app.cpp index ca5b0e976..b086303dc 100644 --- a/UI/obs-app.cpp +++ b/UI/obs-app.cpp @@ -93,6 +93,9 @@ QObject *CreateShortcutFilter() { return new OBSEventFilter([](QObject *obj, QEvent *event) { auto mouse_event = [](QMouseEvent &event) { + if (!App()->HotkeysEnabledInFocus()) + return true; + obs_key_combination_t hotkey = {0, OBS_KEY_NONE}; bool pressed = event.type() == QEvent::MouseButtonPress; @@ -147,6 +150,9 @@ QObject *CreateShortcutFilter() }; auto key_event = [&](QKeyEvent *event) { + if (!App()->HotkeysEnabledInFocus()) + return true; + QDialog *dialog = qobject_cast(obj); obs_key_combination_t hotkey = {0, OBS_KEY_NONE}; diff --git a/UI/obs-app.hpp b/UI/obs-app.hpp index 94ae84460..cc751ac68 100644 --- a/UI/obs-app.hpp +++ b/UI/obs-app.hpp @@ -107,6 +107,11 @@ public: void EnableInFocusHotkeys(bool enable); + inline bool HotkeysEnabledInFocus() const + { + return enableHotkeysInFocus; + } + inline QMainWindow *GetMainWindow() const { return mainWindow.data(); } inline config_t *GlobalConfig() const { return globalConfig; } From 985772d9158cc52ff5cb756e78a087cf5d8ea3d1 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Mon, 22 Jul 2019 01:17:29 -0700 Subject: [PATCH 2/3] UI: Fix param logic of ResetHotkeyState calls The parameter "inFocus" was being given the opposite of what the name implies: it was being set to false when in focus, and true when not in focus. This fixes that confusion. --- UI/obs-app.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/UI/obs-app.cpp b/UI/obs-app.cpp index b086303dc..234e0d60c 100644 --- a/UI/obs-app.cpp +++ b/UI/obs-app.cpp @@ -1257,13 +1257,13 @@ static bool StartupOBS(const char *locale, profiler_name_store_t *store) inline void OBSApp::ResetHotkeyState(bool inFocus) { - obs_hotkey_enable_background_press(inFocus || enableHotkeysInFocus); + obs_hotkey_enable_background_press(!inFocus || enableHotkeysInFocus); } void OBSApp::EnableInFocusHotkeys(bool enable) { enableHotkeysInFocus = enable; - ResetHotkeyState(applicationState() != Qt::ApplicationActive); + ResetHotkeyState(applicationState() == Qt::ApplicationActive); } Q_DECLARE_METATYPE(VoidFunc) @@ -1313,9 +1313,9 @@ bool OBSApp::OBSInit() connect(this, &QGuiApplication::applicationStateChanged, [this](Qt::ApplicationState state) { - ResetHotkeyState(state != Qt::ApplicationActive); + ResetHotkeyState(state == Qt::ApplicationActive); }); - ResetHotkeyState(applicationState() != Qt::ApplicationActive); + ResetHotkeyState(applicationState() == Qt::ApplicationActive); return true; } From 19464e6552a1cf6d30d944ef9be986f5148b8532 Mon Sep 17 00:00:00 2001 From: Jonathan Baecker Date: Mon, 22 Jul 2019 01:20:12 -0700 Subject: [PATCH 3/3] UI: Add ability to disable hotkeys when not in focus --- UI/data/locale/en-US.ini | 3 +++ UI/forms/OBSBasicSettings.ui | 35 +++++++++++++----------- UI/obs-app.cpp | 52 ++++++++++++++++++++++++++++-------- UI/obs-app.hpp | 4 ++- UI/window-basic-settings.cpp | 33 +++++++++++++++-------- 5 files changed, 89 insertions(+), 38 deletions(-) diff --git a/UI/data/locale/en-US.ini b/UI/data/locale/en-US.ini index 7c997f385..472457fca 100644 --- a/UI/data/locale/en-US.ini +++ b/UI/data/locale/en-US.ini @@ -831,7 +831,10 @@ Basic.Settings.Advanced.Network="Network" Basic.Settings.Advanced.Network.BindToIP="Bind to IP" Basic.Settings.Advanced.Network.EnableNewSocketLoop="Enable new networking code" Basic.Settings.Advanced.Network.EnableLowLatencyMode="Low latency mode" +Basic.Settings.Advanced.Hotkeys.HotkeyFocusBehavior="Hotkey Focus Behavior" +Basic.Settings.Advanced.Hotkeys.NeverDisableHotkeys="Never disable hotkeys" Basic.Settings.Advanced.Hotkeys.DisableHotkeysInFocus="Disable hotkeys when main window is in focus" +Basic.Settings.Advanced.Hotkeys.DisableHotkeysOutOfFocus="Disable hotkeys when main window is not in focus" Basic.Settings.Advanced.AutoRemux="Automatically remux to mp4" Basic.Settings.Advanced.AutoRemux.MP4="(record as mkv)" diff --git a/UI/forms/OBSBasicSettings.ui b/UI/forms/OBSBasicSettings.ui index c93f2f2e6..0c8f94256 100644 --- a/UI/forms/OBSBasicSettings.ui +++ b/UI/forms/OBSBasicSettings.ui @@ -151,8 +151,8 @@ 0 0 - 806 - 1225 + 803 + 954 @@ -1173,8 +1173,8 @@ 0 0 - 813 - 761 + 601 + 631 @@ -3621,8 +3621,8 @@ 0 0 - 767 - 582 + 555 + 469 @@ -4436,9 +4436,9 @@ 0 - 0 - 806 - 933 + -114 + 803 + 761 @@ -5136,17 +5136,23 @@ Basic.Settings.Hotkeys + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + 2 - - + + - Basic.Settings.Advanced.Hotkeys.DisableHotkeysInFocus + Basic.Settings.Advanced.Hotkeys.HotkeyFocusBehavior - + + + + Qt::Horizontal @@ -5154,7 +5160,7 @@ 170 - 20 + 0 @@ -5399,7 +5405,6 @@ enableNewSocketLoop enableLowLatencyMode browserHWAccel - disableFocusHotkeys diff --git a/UI/obs-app.cpp b/UI/obs-app.cpp index 234e0d60c..46967e650 100644 --- a/UI/obs-app.cpp +++ b/UI/obs-app.cpp @@ -432,6 +432,9 @@ bool OBSApp::InitGlobalConfigDefaults() "CurrentTheme", DEFAULT_THEME); } + config_set_default_string(globalConfig, "General", "HotkeyFocusType", + "NeverDisableHotkeys"); + config_set_default_bool(globalConfig, "BasicWindow", "VerticalVolControl", false); @@ -699,9 +702,10 @@ bool OBSApp::InitGlobalConfig() } } + uint32_t lastVersion = + config_get_int(globalConfig, "General", "LastVersion"); + if (!config_has_user_value(globalConfig, "General", "Pre19Defaults")) { - uint32_t lastVersion = - config_get_int(globalConfig, "General", "LastVersion"); bool useOldDefaults = lastVersion && lastVersion < MAKE_SEMANTIC_VERSION(19, 0, 0); @@ -712,8 +716,6 @@ bool OBSApp::InitGlobalConfig() } if (!config_has_user_value(globalConfig, "General", "Pre21Defaults")) { - uint32_t lastVersion = - config_get_int(globalConfig, "General", "LastVersion"); bool useOldDefaults = lastVersion && lastVersion < MAKE_SEMANTIC_VERSION(21, 0, 0); @@ -724,8 +726,6 @@ bool OBSApp::InitGlobalConfig() } if (!config_has_user_value(globalConfig, "General", "Pre23Defaults")) { - uint32_t lastVersion = - config_get_int(globalConfig, "General", "LastVersion"); bool useOldDefaults = lastVersion && lastVersion < MAKE_SEMANTIC_VERSION(23, 0, 0); @@ -742,6 +742,16 @@ bool OBSApp::InitGlobalConfig() changed |= UpdatePre22MultiviewLayout(layout); } + if (lastVersion && lastVersion < MAKE_SEMANTIC_VERSION(24, 0, 0)) { + bool disableHotkeysInFocus = config_get_bool( + globalConfig, "General", "DisableHotkeysInFocus"); + if (disableHotkeysInFocus) + config_set_string(globalConfig, "General", + "HotkeyFocusType", + "DisableHotkeysInFocus"); + changed = true; + } + if (changed) config_save_safe(globalConfig, "tmp", nullptr); @@ -1227,8 +1237,7 @@ void OBSApp::AppInit() EnableOSXVSync(false); #endif - enableHotkeysInFocus = !config_get_bool(globalConfig, "General", - "DisableHotkeysInFocus"); + UpdateHotkeyFocusSetting(false); move_basic_to_profiles(); move_basic_to_scene_collections(); @@ -1257,12 +1266,33 @@ static bool StartupOBS(const char *locale, profiler_name_store_t *store) inline void OBSApp::ResetHotkeyState(bool inFocus) { - obs_hotkey_enable_background_press(!inFocus || enableHotkeysInFocus); + obs_hotkey_enable_background_press( + (inFocus && enableHotkeysInFocus) || + (!inFocus && enableHotkeysOutOfFocus)); } -void OBSApp::EnableInFocusHotkeys(bool enable) +void OBSApp::UpdateHotkeyFocusSetting(bool resetState) { - enableHotkeysInFocus = enable; + enableHotkeysInFocus = true; + enableHotkeysOutOfFocus = true; + + const char *hotkeyFocusType = + config_get_string(globalConfig, "General", "HotkeyFocusType"); + + if (astrcmpi(hotkeyFocusType, "DisableHotkeysInFocus") == 0) { + enableHotkeysInFocus = false; + } else if (astrcmpi(hotkeyFocusType, "DisableHotkeysOutOfFocus") == 0) { + enableHotkeysOutOfFocus = false; + } + + if (resetState) + ResetHotkeyState(applicationState() == Qt::ApplicationActive); +} + +void OBSApp::DisableHotkeys() +{ + enableHotkeysInFocus = false; + enableHotkeysOutOfFocus = false; ResetHotkeyState(applicationState() == Qt::ApplicationActive); } diff --git a/UI/obs-app.hpp b/UI/obs-app.hpp index cc751ac68..b550ea80a 100644 --- a/UI/obs-app.hpp +++ b/UI/obs-app.hpp @@ -80,6 +80,7 @@ private: int sleepInhibitRefs = 0; bool enableHotkeysInFocus = true; + bool enableHotkeysOutOfFocus = true; std::deque translatorHooks; @@ -105,7 +106,8 @@ public: void AppInit(); bool OBSInit(); - void EnableInFocusHotkeys(bool enable); + void UpdateHotkeyFocusSetting(bool reset = true); + void DisableHotkeys(); inline bool HotkeysEnabledInFocus() const { diff --git a/UI/window-basic-settings.cpp b/UI/window-basic-settings.cpp index 95f8cfa25..4df5d77f7 100644 --- a/UI/window-basic-settings.cpp +++ b/UI/window-basic-settings.cpp @@ -463,10 +463,20 @@ OBSBasicSettings::OBSBasicSettings(QWidget *parent) HookWidget(ui->bindToIP, COMBO_CHANGED, ADV_CHANGED); HookWidget(ui->enableNewSocketLoop, CHECK_CHANGED, ADV_CHANGED); HookWidget(ui->enableLowLatencyMode, CHECK_CHANGED, ADV_CHANGED); - HookWidget(ui->disableFocusHotkeys, CHECK_CHANGED, ADV_CHANGED); + HookWidget(ui->hotkeyFocusType, COMBO_CHANGED, ADV_CHANGED); HookWidget(ui->autoRemux, CHECK_CHANGED, ADV_CHANGED); /* clang-format on */ +#define ADD_HOTKEY_FOCUS_TYPE(s) \ + ui->hotkeyFocusType->addItem( \ + QTStr("Basic.Settings.Advanced.Hotkeys." s), s) + + ADD_HOTKEY_FOCUS_TYPE("NeverDisableHotkeys"); + ADD_HOTKEY_FOCUS_TYPE("DisableHotkeysInFocus"); + ADD_HOTKEY_FOCUS_TYPE("DisableHotkeysOutOfFocus"); + +#undef ADD_HOTKEY_FOCUS_TYPE + ui->simpleOutputVBitrate->setSingleStep(50); ui->simpleOutputVBitrate->setSuffix(" Kbps"); ui->advOutFFVBitrate->setSingleStep(50); @@ -741,16 +751,15 @@ OBSBasicSettings::OBSBasicSettings(QWidget *parent) UpdateAutomaticReplayBufferCheckboxes(); - App()->EnableInFocusHotkeys(false); + App()->DisableHotkeys(); } OBSBasicSettings::~OBSBasicSettings() { - bool disableHotkeysInFocus = config_get_bool( - App()->GlobalConfig(), "General", "DisableHotkeysInFocus"); delete ui->filenameFormatting->completer(); main->EnableOutputs(true); - App()->EnableInFocusHotkeys(!disableHotkeysInFocus); + + App()->UpdateHotkeyFocusSetting(); EnableThreadedMessageBoxes(false); } @@ -2243,6 +2252,8 @@ void OBSBasicSettings::LoadAdvancedSettings() int rbTime = config_get_int(main->Config(), "AdvOut", "RecRBTime"); int rbSize = config_get_int(main->Config(), "AdvOut", "RecRBSize"); bool autoRemux = config_get_bool(main->Config(), "Video", "AutoRemux"); + const char *hotkeyFocusType = config_get_string( + App()->GlobalConfig(), "General", "HotkeyFocusType"); loading = true; @@ -2315,9 +2326,7 @@ void OBSBasicSettings::LoadAdvancedSettings() ui->browserHWAccel->setChecked(browserHWAccel); #endif - bool disableFocusHotkeys = config_get_bool( - App()->GlobalConfig(), "General", "DisableHotkeysInFocus"); - ui->disableFocusHotkeys->setChecked(disableFocusHotkeys); + SetComboByValue(ui->hotkeyFocusType, hotkeyFocusType); loading = false; } @@ -2917,9 +2926,11 @@ void OBSBasicSettings::SaveAdvancedSettings() browserHWAccel); #endif - bool disableFocusHotkeys = ui->disableFocusHotkeys->isChecked(); - config_set_bool(App()->GlobalConfig(), "General", - "DisableHotkeysInFocus", disableFocusHotkeys); + if (WidgetChanged(ui->hotkeyFocusType)) { + QString str = GetComboData(ui->hotkeyFocusType); + config_set_string(App()->GlobalConfig(), "General", + "HotkeyFocusType", QT_TO_UTF8(str)); + } #ifdef __APPLE__ if (WidgetChanged(ui->disableOSXVSync)) {