improve hotkey handling (#49)

This commit is contained in:
WarmUpTill 2020-10-12 20:55:51 +02:00 committed by GitHub
parent fb1eac5268
commit 1e6a022787
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 128 deletions

View File

@ -122,6 +122,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);
@ -148,6 +149,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);
@ -435,25 +437,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);
}

View File

@ -267,6 +267,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());
@ -312,6 +313,7 @@ void SceneSwitcher::on_importSettings_clicked()
switcher->loadTimeSwitches(obj);
switcher->loadAudioSwitches(obj);
switcher->loadGeneralSettings(obj);
switcher->loadHotkeys(obj);
obs_data_release(obj);

View File

@ -245,22 +245,6 @@ void switchScene(OBSWeakSource &scene, OBSWeakSource &transition,
bool &transitionOverrideOverride,
std::unique_lock<std::mutex> &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
********************************************************************************/

View File

@ -133,6 +133,11 @@ struct SwitcherData {
std::vector<int> tabOrder;
bool hotkeysRegistered = false;
obs_hotkey_id startHotkey;
obs_hotkey_id stopHotkey;
obs_hotkey_id toggleHotkey;
void Thread();
void Start();
void Stop();
@ -181,6 +186,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);
@ -195,6 +201,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(); }

View File

@ -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<char>(settingsFile)),
std::istreambuf_iterator<char>());
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);
}