diff --git a/SceneSwitcher.cpp b/SceneSwitcher.cpp index c3d3f239..bcf56dde 100644 --- a/SceneSwitcher.cpp +++ b/SceneSwitcher.cpp @@ -6,22 +6,68 @@ #include #include #include +#include using namespace std; //Swicthing done in here Switcher *switcher = new Switcher(); -//Hotkey -obs_hotkey_id hotkeyId; -obs_data_array_t *hotkeyData; +//Hotkeys +const char *PAUSE_HOTKEY_NAME = "pauseHotkey"; +const char *OPTIONS_HOTKEY_NAME = "optionsHotkey"; +obs_hotkey_id pauseHotkeyId; +obs_hotkey_id optionsHotkeyId; +obs_data_array_t *pauseHotkeyData; +obs_data_array_t *optionsHotkeyData; //path to config folder where to save the hotkeybinding (probably later the settings file) string configPath; +void saveKeybinding(string name, obs_data_array_t *hotkeyData) { + name.append(".txt"); + //save settings (the only hotkey for now) + wstring stemp = wstring(configPath.begin(), configPath.end()); + LPCWSTR sw = stemp.c_str(); + //check if config dir exists + if (CreateDirectory(sw, NULL) || + ERROR_ALREADY_EXISTS == GetLastError()) + { + //save hotkey data + fstream file; + file.open(obs_module_config_path(name.c_str()), fstream::out); + //doesnt seem to work in obs_module_unload (hotkey data freed already (<- Jim)) + //hotkeyData = obs_hotkey_save(pauseHotkeyId); + int num = obs_data_array_count(hotkeyData); + for (int i = 0; i < num; i++) { + string temp = obs_data_get_json(obs_data_array_item(hotkeyData, i)); + file << temp; + } + } +} + +void loadKeybinding(string name, obs_data_array_t *hotkeyData, obs_hotkey_id hotkeyId) { + name.append(".txt"); + ifstream file(obs_module_config_path(name.c_str())); + if (file.is_open()) + { + string temp; + file.seekg(0, std::ios::end); + temp.reserve(file.tellg()); + file.seekg(0, std::ios::beg); + temp.assign((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + hotkeyData = obs_data_array_create(); + if (!temp.empty()) { + //fails if hotkey not set + obs_data_array_insert(hotkeyData, 0, obs_data_create_from_json(temp.c_str())); + obs_hotkey_load(hotkeyId, hotkeyData); + } + } +} + OBS_DECLARE_MODULE() -void SceneSwitcherHotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, bool pressed) +void SceneSwitcherPauseHotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, bool pressed) { UNUSED_PARAMETER(id); UNUSED_PARAMETER(hotkey); @@ -39,62 +85,31 @@ void SceneSwitcherHotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, boo } } //save the keybinding here since it is currently not possible in obs_module_unload - hotkeyData = obs_hotkey_save(hotkeyId); + pauseHotkeyData = obs_hotkey_save(pauseHotkeyId); } - bool obs_module_load(void) { //set config path configPath = obs_module_config_path(""); - //register hotkey - hotkeyId = obs_hotkey_register_frontend("Scene Switcher", "Toggle automatic scene switching", SceneSwitcherHotkey, switcher); - //load hotkey binding if set already (ONLY WORKING FOR A SINGLE HOTKEY AT THE MOMENT! CAREFUL!) - ifstream file(obs_module_config_path("hotkey.txt")); - if (file.is_open()) - { - string temp; - file.seekg(0, std::ios::end); - temp.reserve(file.tellg()); - file.seekg(0, std::ios::beg); - temp.assign((std::istreambuf_iterator(file)),std::istreambuf_iterator()); - hotkeyData = obs_data_array_create(); - obs_data_array_insert(hotkeyData, 0,obs_data_create_from_json(temp.c_str())); - obs_hotkey_load(hotkeyId, hotkeyData); - } - + pauseHotkeyId = obs_hotkey_register_frontend(PAUSE_HOTKEY_NAME, "Toggle automatic scene switching", SceneSwitcherPauseHotkey, switcher); + //optionsHotkeyId = obs_hotkey_register_frontend(OPTIONS_HOTKEY_NAME, "Open the Scene Switcher options menu", SceneSwitcherOptionsHotkey, switcher); + //load hotkey binding if set already + loadKeybinding(PAUSE_HOTKEY_NAME, pauseHotkeyData, pauseHotkeyId); + //loadKeybinding(OPTIONS_HOTKEY_NAME, optionsHotkeyData, optionsHotkeyId); //load settings file - switcher->load(); + switcher->firstLoad(); //start the switching thread switcher->start(); return true; - } void obs_module_unload(void) { - - //save settings (the only hotkey for now) - wstring stemp = wstring(configPath.begin(), configPath.end()); - LPCWSTR sw = stemp.c_str(); - //check if config dir exists - if (CreateDirectory(sw, NULL) || - ERROR_ALREADY_EXISTS == GetLastError()) - { - //save hotkey data - fstream file; - file.open(obs_module_config_path("hotkey.txt"), fstream::out); - //doesnt seem to work in obs_module_unload (hotkey data freed already (<- Jim)) - //hotkeyData = obs_hotkey_save(hotkeyId); - int num = obs_data_array_count(hotkeyData); - for (int i = 0; i < num; i++) { - string temp = obs_data_get_json(obs_data_array_item(hotkeyData, i)); - file << temp; - } - - file.close(); - } - + switcher->stop(); + //save settings (only hotkey for now) + saveKeybinding(PAUSE_HOTKEY_NAME, pauseHotkeyData); + //saveKeybinding(OPTIONS_HOTKEY_NAME, optionsHotkeyData); } const char *obs_module_author(void) @@ -111,5 +126,3 @@ const char *obs_module_description(void) { return "Automatic scene switching"; } - - diff --git a/settings.cpp b/settings.cpp index 8463626f..8edebf23 100644 --- a/settings.cpp +++ b/settings.cpp @@ -34,16 +34,17 @@ void Settings::load() { while (infile.good()) { getline(infile, line); - stringstream lineStream = stringstream(line); - while (lineStream.good()) { - getline(lineStream, value, ','); - settingsElements.push_back(value); - numValues++; + if (!line.empty()) { + stringstream lineStream = stringstream(line); + while (lineStream.good()) { + getline(lineStream, value, ','); + settingsElements.push_back(value); + numValues++; + } } - } - //create settings map containgin windowname and desired scene + //create settings map containing windowname and desired scene for (int i = 0; i < numValues; ) { string s2 = settingsElements.back(); settingsElements.pop_back(); @@ -51,7 +52,7 @@ void Settings::load() { string s1 = settingsElements.back(); settingsElements.pop_back(); i++; - //window name , scene + //window name,scene Settings::addToMap(s1, s2); } diff --git a/switcher.cpp b/switcher.cpp index 0ce7a21d..8cdeabdc 100644 --- a/switcher.cpp +++ b/switcher.cpp @@ -1,9 +1,6 @@ #pragma once - - #include "stdafx.h" #include "settings.h" -#include #include #include #include @@ -13,22 +10,15 @@ #include #include "switcher.h" - - using namespace std; - //scene switching is done in here void Switcher::switcherThreadFunc() { - while (isRunning) { - //get active window title string windowname = GetActiveWindowTitle(); - bool match = false; string name = ""; - for (std::map::iterator iter = settingsMap.begin(); iter != settingsMap.end(); ++iter) { try @@ -86,8 +76,7 @@ void Switcher::switcherThreadFunc() { } } -//load the settings needed to start the thread -void Switcher::load() { +void Switcher::firstLoad() { settings.load(); settingsMap = settings.getMap(); if (settingsMap.find("Disable Start Message") == settingsMap.end() || settingsMap.find("Disable Start Message")->second != "Yes") { @@ -100,6 +89,12 @@ void Switcher::load() { } } +//load the settings needed to start the thread +void Switcher::load() { + settings.load(); + settingsMap = settings.getMap(); +} + //start thread void Switcher::start() { diff --git a/switcher.h b/switcher.h index 14bf67fd..77d53e03 100644 --- a/switcher.h +++ b/switcher.h @@ -1,10 +1,7 @@ #pragma once #include "settings.h" -#include #include #include -#include -#include #include using namespace std; @@ -12,6 +9,7 @@ using namespace std; class Switcher { public: bool getIsRunning(); + void firstLoad(); void load(); void start(); void stop();