diff --git a/src/advanced-scene-switcher.cpp b/src/advanced-scene-switcher.cpp index c707836d..0752f985 100644 --- a/src/advanced-scene-switcher.cpp +++ b/src/advanced-scene-switcher.cpp @@ -233,6 +233,7 @@ static void SaveSceneSwitcher(obs_data_t *save_data, bool saving, void *) switcher->saveTimeSwitches(obj); switcher->saveAudioSwitches(obj); switcher->saveGeneralSettings(obj); + switcher->saveHotkeys(obj); obs_data_set_obj(save_data, "advanced-scene-switcher", obj); @@ -259,6 +260,7 @@ static void SaveSceneSwitcher(obs_data_t *save_data, bool saving, void *) switcher->loadTimeSwitches(obj); switcher->loadAudioSwitches(obj); switcher->loadGeneralSettings(obj); + switcher->loadHotkeys(obj); obs_data_release(obj); @@ -541,25 +543,4 @@ extern "C" void InitSceneSwitcher() obs_frontend_add_event_callback(OBSEvent, switcher); action->connect(action, &QAction::triggered, cb); - - char *path = obs_module_config_path(""); - QDir dir(path); - if (!dir.exists()) { - dir.mkpath("."); - } - bfree(path); - - obs_hotkey_id toggleHotkeyId = obs_hotkey_register_frontend( - "startStopToggleSwitcherHotkey", - "Toggle Start/Stop for the Advanced Scene Switcher", - startStopToggleHotkeyFunc, NULL); - loadKeybinding(toggleHotkeyId, toggle_hotkey_path); - obs_hotkey_id startHotkeyId = obs_hotkey_register_frontend( - "startSwitcherHotkey", "Start the Advanced Scene Switcher", - startHotkeyFunc, NULL); - loadKeybinding(startHotkeyId, start_hotkey_path); - obs_hotkey_id stopHotkeyId = obs_hotkey_register_frontend( - "stopSwitcherHotkey", "Stop the Advanced Scene Switcher", - stopHotkeyFunc, NULL); - loadKeybinding(stopHotkeyId, stop_hotkey_path); } diff --git a/src/general.cpp b/src/general.cpp index f1ff5030..08739b36 100644 --- a/src/general.cpp +++ b/src/general.cpp @@ -305,6 +305,7 @@ void SceneSwitcher::on_exportSettings_clicked() switcher->saveTimeSwitches(obj); switcher->saveAudioSwitches(obj); switcher->saveGeneralSettings(obj); + switcher->saveHotkeys(obj); obs_data_save_json(obj, file.fileName().toUtf8().constData()); @@ -350,6 +351,7 @@ void SceneSwitcher::on_importSettings_clicked() switcher->loadTimeSwitches(obj); switcher->loadAudioSwitches(obj); switcher->loadGeneralSettings(obj); + switcher->loadHotkeys(obj); obs_data_release(obj); diff --git a/src/headers/advanced-scene-switcher.hpp b/src/headers/advanced-scene-switcher.hpp index a56e7781..f28ac706 100644 --- a/src/headers/advanced-scene-switcher.hpp +++ b/src/headers/advanced-scene-switcher.hpp @@ -255,22 +255,6 @@ void switchScene(OBSWeakSource &scene, OBSWeakSource &transition, bool &transitionOverrideOverride, std::unique_lock &lock); -/******************************************************************************** - * Hotkey helper - ********************************************************************************/ - -constexpr auto toggle_hotkey_path = "hotkey_toggle.txt"; -constexpr auto stop_hotkey_path = "hotkey_stop.txt"; -constexpr auto start_hotkey_path = "hotkey_start.txt"; - -void stopHotkeyFunc(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, - bool pressed); -void startHotkeyFunc(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, - bool pressed); -void startStopToggleHotkeyFunc(void *data, obs_hotkey_id id, - obs_hotkey_t *hotkey, bool pressed); -void loadKeybinding(obs_hotkey_id hotkeyId, std::string path); - /******************************************************************************** * Main SwitcherData ********************************************************************************/ diff --git a/src/headers/switcher-data-structs.hpp b/src/headers/switcher-data-structs.hpp index 11b7be23..a8dfbdc4 100644 --- a/src/headers/switcher-data-structs.hpp +++ b/src/headers/switcher-data-structs.hpp @@ -134,6 +134,11 @@ struct SwitcherData { std::vector tabOrder; + bool hotkeysRegistered = false; + obs_hotkey_id startHotkey; + obs_hotkey_id stopHotkey; + obs_hotkey_id toggleHotkey; + void Thread(); void Start(); void Stop(); @@ -182,6 +187,7 @@ struct SwitcherData { void saveTimeSwitches(obs_data_t *obj); void saveAudioSwitches(obs_data_t *obj); void saveGeneralSettings(obs_data_t *obj); + void saveHotkeys(obs_data_t *obj); void loadWindowTitleSwitches(obs_data_t *obj); void loadScreenRegionSwitches(obs_data_t *obj); @@ -196,6 +202,7 @@ struct SwitcherData { void loadTimeSwitches(obs_data_t *obj); void loadAudioSwitches(obs_data_t *obj); void loadGeneralSettings(obs_data_t *obj); + void loadHotkeys(obs_data_t *obj); void Prune(); inline ~SwitcherData() { Stop(); } diff --git a/src/hotkey.cpp b/src/hotkey.cpp index 8e9a599a..e293e583 100644 --- a/src/hotkey.cpp +++ b/src/hotkey.cpp @@ -6,6 +6,7 @@ void startHotkeyFunc(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, bool pressed) { + UNUSED_PARAMETER(id); UNUSED_PARAMETER(data); UNUSED_PARAMETER(hotkey); @@ -13,33 +14,12 @@ void startHotkeyFunc(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, if (!(switcher->th && switcher->th->isRunning())) switcher->Start(); } - - obs_data_array *hotkeyData = obs_hotkey_save(id); - - if (hotkeyData != NULL) { - char *path = obs_module_config_path(""); - std::ofstream file; - file.open(std::string(path).append(start_hotkey_path), - std::ofstream::trunc); - if (file.is_open()) { - size_t num = obs_data_array_count(hotkeyData); - for (size_t i = 0; i < num; i++) { - obs_data_t *data = - obs_data_array_item(hotkeyData, i); - std::string temp = obs_data_get_json(data); - obs_data_release(data); - file << temp; - } - file.close(); - } - bfree(path); - } - obs_data_array_release(hotkeyData); } void stopHotkeyFunc(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, bool pressed) { + UNUSED_PARAMETER(id); UNUSED_PARAMETER(data); UNUSED_PARAMETER(hotkey); @@ -47,33 +27,12 @@ void stopHotkeyFunc(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, if (switcher->th && switcher->th->isRunning()) switcher->Stop(); } - - obs_data_array *hotkeyData = obs_hotkey_save(id); - - if (hotkeyData != NULL) { - char *path = obs_module_config_path(""); - std::ofstream file; - file.open(std::string(path).append(stop_hotkey_path), - std::ofstream::trunc); - if (file.is_open()) { - size_t num = obs_data_array_count(hotkeyData); - for (size_t i = 0; i < num; i++) { - obs_data_t *data = - obs_data_array_item(hotkeyData, i); - std::string temp = obs_data_get_json(data); - obs_data_release(data); - file << temp; - } - file.close(); - } - bfree(path); - } - obs_data_array_release(hotkeyData); } void startStopToggleHotkeyFunc(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, bool pressed) { + UNUSED_PARAMETER(id); UNUSED_PARAMETER(data); UNUSED_PARAMETER(hotkey); @@ -83,58 +42,60 @@ void startStopToggleHotkeyFunc(void *data, obs_hotkey_id id, else switcher->Start(); } - - obs_data_array *hotkeyData = obs_hotkey_save(id); - - if (hotkeyData != NULL) { - char *path = obs_module_config_path(""); - std::ofstream file; - file.open(std::string(path).append(toggle_hotkey_path), - std::ofstream::trunc); - if (file.is_open()) { - size_t num = obs_data_array_count(hotkeyData); - for (size_t i = 0; i < num; i++) { - obs_data_t *data = - obs_data_array_item(hotkeyData, i); - std::string temp = obs_data_get_json(data); - obs_data_release(data); - file << temp; - } - file.close(); - } - bfree(path); - } - obs_data_array_release(hotkeyData); } -std::string loadConfigFile(std::string filename) +void registerHotkeys() { - std::ifstream settingsFile; - char *path = obs_module_config_path(""); - std::string value; + switcher->startHotkey = obs_hotkey_register_frontend( + "startSwitcherHotkey", "Start the Advanced Scene Switcher", + startHotkeyFunc, NULL); + switcher->stopHotkey = obs_hotkey_register_frontend( + "stopSwitcherHotkey", "Stop the Advanced Scene Switcher", + stopHotkeyFunc, NULL); + switcher->toggleHotkey = obs_hotkey_register_frontend( + "startStopToggleSwitcherHotkey", + "Toggle Start/Stop for the Advanced Scene Switcher", + startStopToggleHotkeyFunc, NULL); - settingsFile.open(std::string(path).append(filename)); - if (settingsFile.is_open()) { - settingsFile.seekg(0, std::ios::end); - value.reserve(settingsFile.tellg()); - settingsFile.seekg(0, std::ios::beg); - value.assign((std::istreambuf_iterator(settingsFile)), - std::istreambuf_iterator()); - settingsFile.close(); - } - bfree(path); - return value; + switcher->hotkeysRegistered = true; } -void loadKeybinding(obs_hotkey_id hotkeyId, std::string path) +void SwitcherData::saveHotkeys(obs_data_t *obj) { - std::string bindings = loadConfigFile(path); - if (!bindings.empty()) { - obs_data_array_t *hotkeyData = obs_data_array_create(); - obs_data_t *data = obs_data_create_from_json(bindings.c_str()); - obs_data_array_insert(hotkeyData, 0, data); - obs_data_release(data); - obs_hotkey_load(hotkeyId, hotkeyData); - obs_data_array_release(hotkeyData); - } + obs_data_array_t *startHotkeyArrray = + obs_hotkey_save(switcher->startHotkey); + obs_data_set_array(obj, "startHotkey", startHotkeyArrray); + obs_data_array_release(startHotkeyArrray); + + obs_data_array_t *stopHotkeyArrray = + obs_hotkey_save(switcher->stopHotkey); + + obs_data_set_array(obj, "stopHotkey", stopHotkeyArrray); + obs_data_array_release(stopHotkeyArrray); + + obs_data_array_t *toggleHotkeyArrray = + obs_hotkey_save(switcher->toggleHotkey); + obs_data_set_array(obj, "toggleHotkey", toggleHotkeyArrray); + obs_data_array_release(toggleHotkeyArrray); +} + +void SwitcherData::loadHotkeys(obs_data_t *obj) +{ + if (!switcher->hotkeysRegistered) + registerHotkeys(); + + obs_data_array_t *startHotkeyArrray = + obs_data_get_array(obj, "startHotkey"); + obs_hotkey_load(switcher->startHotkey, startHotkeyArrray); + obs_data_array_release(startHotkeyArrray); + + obs_data_array_t *stopHotkeyArrray = + obs_data_get_array(obj, "stopHotkey"); + obs_hotkey_load(switcher->stopHotkey, stopHotkeyArrray); + obs_data_array_release(stopHotkeyArrray); + + obs_data_array_t *toggleHotkeyArrray = + obs_data_get_array(obj, "toggleHotkey"); + obs_hotkey_load(switcher->toggleHotkey, toggleHotkeyArrray); + obs_data_array_release(toggleHotkeyArrray); }