#include "switcher.h" #include #include #include #include #include #include using namespace std; //Swicthing done in here Switcher *switcher = new Switcher(); //settings source struct obs_source_info sceneSwitcherOptionsSource; //Hotkeys const char *PAUSE_HOTKEY_NAME = "pauseHotkey"; obs_hotkey_id pauseHotkeyId; obs_data_array_t *pauseHotkeyData; //path to config folder where to save the hotkeybinding (probably later the settings file) char* configPath; //save settings (the only hotkey for now) void saveKeybinding(string name, obs_data_array_t *hotkeyData) { if (hotkeyData != NULL) { name.append(".txt"); //save hotkey data ofstream file; file.open(string(configPath).append(name), ofstream::trunc); //hotkeyData = obs_hotkey_save(pauseHotkeyId); //doesnt seem to work in obs_module_unload (hotkey data freed already (<- Jim)) 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); string temp = obs_data_get_json(data); obs_data_release(data); file << temp; } file.close(); } obs_data_array_release(hotkeyData); } } string loadConfigFile(string filename) { ifstream settingsFile; settingsFile.open(string(configPath).append(filename)); string value; if (settingsFile.is_open()) { settingsFile.seekg(0, ios::end); value.reserve(settingsFile.tellg()); settingsFile.seekg(0, ios::beg); value.assign((istreambuf_iterator(settingsFile)), istreambuf_iterator()); settingsFile.close(); } return value; } void loadKeybinding(string name, obs_hotkey_id hotkeyId) { string temp = loadConfigFile(name.append(".txt")); if (!temp.empty()) { obs_data_array_t *hotkeyData = obs_data_array_create(); obs_data_t *data = obs_data_create_from_json(temp.c_str()); obs_data_array_insert(hotkeyData, 0, data); obs_hotkey_load(hotkeyId, hotkeyData); obs_data_release(data); obs_data_array_release(hotkeyData); } } OBS_DECLARE_MODULE() void SceneSwitcherPauseHotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, bool pressed) { UNUSED_PARAMETER(data); UNUSED_PARAMETER(hotkey); if (pressed) { if (switcher->getIsRunning()) { switcher->stop(); } else { switcher->start(); } } //save the keybinding here since it is not possible in obs_module_unload obs_data_array_release(pauseHotkeyData); pauseHotkeyData = obs_hotkey_save(id); } const char *sceneSwitcherOptionsGetName(void *type_data) { UNUSED_PARAMETER(type_data); return "Scene Switcher Options"; } void *sceneSwitcherOptionsCreate(obs_data_t *settings, obs_source_t *source) { UNUSED_PARAMETER(settings); UNUSED_PARAMETER(source); return switcher; } void sceneSwitcherOptionsDestroy(void *data) { UNUSED_PARAMETER(data); } uint32_t sceneSwitcherOptionsGetWidth(void *data) { UNUSED_PARAMETER(data); return 0; } uint32_t sceneSwitcherOptionsGetHeight(void *data) { UNUSED_PARAMETER(data); return 0; } void sceneSwitcherOptionsSourceGetDefaults(obs_data_t *settings) { //load settings file string temp = loadConfigFile("settings.txt"); //set values from file obs_data_t *data = obs_data_create_from_json(temp.c_str()); obs_data_apply(settings, data); obs_data_release(data); } obs_properties_t *sceneSwitcherOptionsSourceGetProperties(void *data) { UNUSED_PARAMETER(data); obs_properties_t *props = obs_properties_create(); obs_properties_add_bool(props, "StartMessageDisable", "Disable Start Message"); obs_properties_add_editable_list(props, "WindowList", "", (enum obs_editable_list_type)0, "", NULL); return props; } void sceneSwitcherOptionsSourceSave(void *data, obs_data_t *settings) { UNUSED_PARAMETER(data); //hang here if multiple instances of Scene Switcher Options are active (why?) ofstream settingsFile; settingsFile.open(string(configPath).append("settings.txt"), ofstream::trunc); if (settingsFile.is_open()) { settingsFile << obs_data_get_json(settings); settingsFile.close(); } if (switcher->getIsRunning()) switcher->stop(); switcher->load(); switcher->start(); } void sceneSwitcherOptionsSourceLoad(void *data, obs_data_t *settings) { UNUSED_PARAMETER(data); //load settings file string temp = loadConfigFile("settings.txt"); //set values from file obs_data_t *loadedSettings = obs_data_create_from_json(temp.c_str()); obs_data_apply(settings, loadedSettings); obs_data_release(loadedSettings); } void sceneSwitcherOptionsSourceSetup() { sceneSwitcherOptionsSource.id = "Scene Switcher Options"; sceneSwitcherOptionsSource.type = OBS_SOURCE_TYPE_INPUT; sceneSwitcherOptionsSource.get_name = sceneSwitcherOptionsGetName; sceneSwitcherOptionsSource.create = sceneSwitcherOptionsCreate; sceneSwitcherOptionsSource.destroy = sceneSwitcherOptionsDestroy; sceneSwitcherOptionsSource.get_width = sceneSwitcherOptionsGetWidth; sceneSwitcherOptionsSource.get_height = sceneSwitcherOptionsGetHeight; sceneSwitcherOptionsSource.get_defaults = sceneSwitcherOptionsSourceGetDefaults; //create settings file if not present sceneSwitcherOptionsSource.get_properties = sceneSwitcherOptionsSourceGetProperties; sceneSwitcherOptionsSource.update = sceneSwitcherOptionsSourceSave; //save new settings to file sceneSwitcherOptionsSource.load = sceneSwitcherOptionsSourceLoad; //read settings file obs_register_source(&sceneSwitcherOptionsSource); } bool obs_module_load(void) { //set config path and check if config dir was set up configPath = obs_module_config_path(""); boost::filesystem::create_directories(configPath);//<- memory leak here somehow //register hotkey pauseHotkeyId = obs_hotkey_register_frontend(PAUSE_HOTKEY_NAME, "Toggle automatic scene switching", SceneSwitcherPauseHotkey, NULL); //load hotkey binding if set already loadKeybinding(PAUSE_HOTKEY_NAME, pauseHotkeyId); //load settings file switcher->setSettingsFilePath(configPath); switcher->firstLoad(); //create options menu sceneSwitcherOptionsSourceSetup(); //start the switching thread switcher->start(); return true; } void obs_module_unload(void) { bfree(configPath); switcher->stop(); //save settings (only hotkey for now) saveKeybinding(PAUSE_HOTKEY_NAME, pauseHotkeyData); obs_data_array_release(pauseHotkeyData); delete switcher; switcher = NULL; } const char *obs_module_author(void) { return "WarmUpTill"; } const char *obs_module_name(void) { return "Scene Switcher"; } const char *obs_module_description(void) { return "Automatic scene switching"; }