From b4df4febac0be6d97d8bcc8159ec22eefd5d300b Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Thu, 19 May 2016 15:58:38 +0200 Subject: [PATCH] Add files via upload --- ObsSceneSwitcher.cpp | 62 ++++++++++++++++++++++++ settings.cpp | 78 ++++++++++++++++++++++++++++++ settings.h | 19 ++++++++ switcher.cpp | 111 +++++++++++++++++++++++++++++++++++++++++++ switcher.h | 23 +++++++++ 5 files changed, 293 insertions(+) create mode 100644 ObsSceneSwitcher.cpp create mode 100644 settings.cpp create mode 100644 settings.h create mode 100644 switcher.cpp create mode 100644 switcher.h diff --git a/ObsSceneSwitcher.cpp b/ObsSceneSwitcher.cpp new file mode 100644 index 00000000..b9fae74f --- /dev/null +++ b/ObsSceneSwitcher.cpp @@ -0,0 +1,62 @@ +// DexterSceneSwitcher.cpp : Defines the exported functions for the DLL application. +// + +#pragma once + +#include "stdafx.h" +#include "switcher.h" +#include +#include + + +using namespace std; + + +OBS_DECLARE_MODULE() + +bool running = false; +Switcher switcher; +thread switcherThread; + +bool obs_module_load(void) +{ + //maybe do some UI stuff? + + //void obs_register_modeless_ui(const struct obs_modeless_ui *info); + + //create thread + running = true; + map s = switcher.load(); + switcherThread = thread(switcherThreadFunc, s); + return true; + +} + + +void obs_module_unload(void) +{ + if(running) +{ + //tell the thread to stop working + switcher.stop(); + switcherThread.join(); +} +return; +} + +const char *obs_module_author(void) +{ + return "WarmUpTill"; +} + +const char *obs_module_name(void) +{ + return "DexterSceneSwitcher"; +} + +const char *obs_module_description(void) +{ + return "SceneSwitching for Dexter"; +} + + diff --git a/settings.cpp b/settings.cpp new file mode 100644 index 00000000..192165a7 --- /dev/null +++ b/settings.cpp @@ -0,0 +1,78 @@ +#pragma once + +#include "stdafx.h" +#include +#include +#include +#include +#include +#include +#include +#include +#pragma comment(lib,"shlwapi.lib") +#include "shlobj.h" + + +using namespace std; + +//possible settings: +// +//file path +//transition delay / type +//... + +class Settings { + map settings; +public: + void load(string); + map getMap(); +private : + void addToMap(string, string); + +}; + +void Settings::load(string filepath) { + + + //read the settings file (csv like, each line is assignment of window -> scene) + std::vector settingsElements; + int numValues = 0; + ifstream infile(filepath); + string value; + string line; + + while (infile.good()) + { + getline(infile, line); + stringstream lineStream = stringstream(line); + while (lineStream.good()) { + getline(lineStream, value, ','); + settingsElements.push_back(value); + } + + + numValues++; + } + + //create settings map containgin windowname and desired scene + for (int i = 0; i < numValues; ) { + string s2 = settingsElements.back(); + settingsElements.pop_back(); + i++; + string s1 = settingsElements.back(); + settingsElements.pop_back(); + i++; + //window name , scene + Settings::addToMap(s1, s2); + } + +} + +void Settings::addToMap(string s1, string s2) { + settings.insert(pair(s1, s2)); +} + +map Settings::getMap() { + return settings; +} + diff --git a/settings.h b/settings.h new file mode 100644 index 00000000..78b61e3b --- /dev/null +++ b/settings.h @@ -0,0 +1,19 @@ +#pragma once +#include + + +#ifndef SETTINGS_H +#define SETTINGS_H + +using namespace std; +class Settings { + map settings; +public: + void load(string); + map getMap(); +private: + void addToMap(string, string); + +}; + +#endif \ No newline at end of file diff --git a/switcher.cpp b/switcher.cpp new file mode 100644 index 00000000..85cc6a1d --- /dev/null +++ b/switcher.cpp @@ -0,0 +1,111 @@ +#pragma once + + +#include "stdafx.h" +#include "settings.h" +#include +#include +#include +#include +#include +#include + + + +using namespace std; + +//is the thread running? see below :D +static bool isRunning = true; +string GetActiveWindowTitle(); + +class Switcher { +public: + map load(); + void stop(); +private: + Settings settings; + map settingsMap; +}; + +//scene switching is done in here +void switcherThreadFunc(map settingsMap) { + + string windowname = ""; + + while (isRunning) { + + //get active window title + windowname = GetActiveWindowTitle(); + + //do we know the window title? + if (!(settingsMap.find(windowname) == settingsMap.end())) { + + //check if current scene is already the desired scene + const char * name = settingsMap.find(windowname)->second.c_str(); + + //remember to release all sources + obs_source_t * transitionUsed = obs_get_output_source(0); + obs_source_t * sceneUsed = obs_transition_get_active_source(transitionUsed); + const char * sceneUsedName = obs_source_get_name(sceneUsed); + + //NOTE: transitions seem to be scenes and will be canceled unless we check if the current scene is a transition + if ((sceneUsedName) && strcmp(sceneUsedName,name) != 0) { + //switch scene + obs_source_t *source = obs_get_source_by_name(name); + if (source == NULL) + { + //warn("Source not found: \"%s\"", name); + ; + } + else if (obs_scene_from_source(source) == NULL) + { + //warn("\"%s\" is not a scene", name); + ; + } + else { + //create transition to new scene (otherwise UI wont work anymore) + //OBS_TRANSITION_MODE_AUTO uses the obs user settings for transitions + obs_transition_start(transitionUsed, OBS_TRANSITION_MODE_AUTO, 300, source); + } + obs_source_release(source); + } + obs_source_release(sceneUsed); + obs_source_release(transitionUsed); + } + //sleep for a bit + this_thread::sleep_for(std::chrono::milliseconds(1000)); + } +} + + +//load the settings needed to start the thread +map Switcher::load() { + //settings file in + string settingsFilePath = "..\\..\\data\\obs-plugins\\DexterSceneSwitcher\\settings.txt"; //"..\\..\\data\\obs-plugins\\DexterSceneSwitcher\\settings.txt"; + settings.load(settingsFilePath); + map settingsMap = settings.getMap(); + isRunning = true; + string message = "The following settings were found for Scene Switcher:\n"; + for (auto it = settingsMap.cbegin(); it != settingsMap.cend(); ++it) + { + message += (it->first) + " -> " + it->second + "\n"; + } + MessageBox(0, message.c_str(), "Scene Switcher", 0); + return settingsMap; + +} + +//reads the title of the currently active window +string GetActiveWindowTitle() +{ + char wnd_title[256]; + HWND hwnd = GetForegroundWindow(); // get handle of currently active window + GetWindowTextA(hwnd, wnd_title, sizeof(wnd_title)); + return wnd_title; +} + +//tell the thread to stop +void Switcher::stop() { + isRunning = false; //isRunning seems to be called randomly and disables the plugin (?) + return; +} diff --git a/switcher.h b/switcher.h new file mode 100644 index 00000000..d00b88d0 --- /dev/null +++ b/switcher.h @@ -0,0 +1,23 @@ +#pragma once +#include "settings.h" +#include +#include +#include +#include +#include +#include + +using namespace std; + +class Switcher { +public: + map load(); + void stop(); +private: + Settings settings; + map settingsMap; +}; + + +string GetActiveWindowTitle(); +void switcherThreadFunc(map settingsMap);