From 9873d62121c3791736c2ca4afe00655ef304effc Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Mon, 30 May 2016 21:26:34 +0200 Subject: [PATCH] support for regex --- SceneSwitcher.cpp | 20 ++++++++---- settings.cpp | 19 +++++------- settings.h | 11 ++----- switcher.cpp | 78 +++++++++++++++++++++++++++++++---------------- switcher.h | 1 + 5 files changed, 76 insertions(+), 53 deletions(-) diff --git a/SceneSwitcher.cpp b/SceneSwitcher.cpp index 98ce98c1..dfd5bfab 100644 --- a/SceneSwitcher.cpp +++ b/SceneSwitcher.cpp @@ -3,7 +3,8 @@ #include "stdafx.h" #include "switcher.h" #include - +#include +//#include Switcher *switcher = new Switcher(); using namespace std; @@ -12,8 +13,6 @@ using namespace std; OBS_DECLARE_MODULE() - - void SceneSwitcherHotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, bool pressed) { UNUSED_PARAMETER(id); @@ -21,7 +20,6 @@ void SceneSwitcherHotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, boo if (pressed) { - Switcher *switcher = static_cast(data); if (switcher->getIsRunning()) { @@ -34,11 +32,21 @@ void SceneSwitcherHotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, boo } } + bool obs_module_load(void) { - //maybe do some UI stuff? + //modul UI not implemented yet + //obs_modal_ui *uiInfo = new obs_modal_ui; + //uiInfo->id = "SceneSwitcherUI"; + //uiInfo->task = "Modify settings for SceneSwitcher"; + //uiInfo->target = "qt"; + //uiInfo->exec = execute; + //uiInfo->type_data = NULL; //<- sceneswitcher?? pause it? then reload settings? + //uiInfo->free_type_data = NULL; - //void obs_register_modeless_ui(const struct obs_modeless_ui *info); + //obs_register_modal_ui(uiInfo); + + //Hotkey obs_hotkey_register_frontend("Scene Switcher", "Toggle automatic scene switching", SceneSwitcherHotkey, switcher); switcher->load(); switcher->start(); diff --git a/settings.cpp b/settings.cpp index 4d6cd6da..8463626f 100644 --- a/settings.cpp +++ b/settings.cpp @@ -11,28 +11,23 @@ #include #pragma comment(lib,"shlwapi.lib") #include "shlobj.h" +#include "settings.h" using namespace std; +string Settings::getSettingsFilePath() +{ + return settingsFilePath; +} -class Settings { - map settings; -public: - void load(string); - map getMap(); -private : - void addToMap(string, string); - -}; - -void Settings::load(string filepath) { +void Settings::load() { //read the settings file std::vector settingsElements; int numValues = 0; - ifstream infile(filepath); + ifstream infile(settingsFilePath); string value; string line; diff --git a/settings.h b/settings.h index 78b61e3b..19261832 100644 --- a/settings.h +++ b/settings.h @@ -1,19 +1,14 @@ #pragma once #include - -#ifndef SETTINGS_H -#define SETTINGS_H - using namespace std; class Settings { map settings; public: - void load(string); + void load(); map getMap(); + string getSettingsFilePath(); private: + string settingsFilePath = "..\\..\\data\\obs-plugins\\SceneSwitcher\\settings.txt"; void addToMap(string, string); - }; - -#endif \ No newline at end of file diff --git a/switcher.cpp b/switcher.cpp index ef3cb3d4..2b1532fd 100644 --- a/switcher.cpp +++ b/switcher.cpp @@ -10,45 +10,48 @@ #include #include #include +#include +#include "switcher.h" using namespace std; -//is the thread running? see below :D -//static bool isRunning = true; - - -class Switcher { -public: - bool getIsRunning(); - void load(); - void start(); - void stop(); - thread switcherThread; -private: - bool isRunning = true; - Settings settings; - map settingsMap; - void switcherThreadFunc(); - bool isWindowFullscreen(); - string GetActiveWindowTitle(); -}; //scene switching is done in here void Switcher::switcherThreadFunc() { - string windowname = ""; + while (isRunning) { //get active window title - windowname = GetActiveWindowTitle(); + string windowname = GetActiveWindowTitle(); + + bool match = false; + string name = ""; + + for (std::map::iterator iter = settingsMap.begin(); iter != settingsMap.end(); ++iter) + { + try + { + regex e = regex(iter->first); + match = regex_match(windowname, e); + if (match) { + name = iter->second; + break; + } + } + catch(exception const & ex) + { + + } + + } //do we know the window title or is a fullscreen/backup Scene set? - if (!(settingsMap.find("Backup Scene Name") == settingsMap.end())||!(settingsMap.find(windowname) == settingsMap.end())){ + if (!(settingsMap.find("Backup Scene Name") == settingsMap.end())||match){ - string name = ""; if (!(settingsMap.find(windowname) == settingsMap.end())) { name = settingsMap.find(windowname)->second; } @@ -93,17 +96,33 @@ void Switcher::switcherThreadFunc() { } +// function that checks if two given strings match. The first string may contain wildcard characters +bool wildcardMatch(char *first, char * second) +{ + if (*first == '\0' && *second == '\0') + return true; + + if (*first == '*' && *(first + 1) != '\0' && *second == '\0') + return false; + + if (*first == '?' || *first == *second) + return wildcardMatch(first + 1, second + 1); + + if (*first == '*') + return wildcardMatch(first + 1, second) || wildcardMatch(first, second + 1); + return false; +} + //load the settings needed to start the thread void Switcher::load() { - string settingsFilePath = "..\\..\\data\\obs-plugins\\SceneSwitcher\\settings.txt"; - settings.load(settingsFilePath); + settings.load(); settingsMap = settings.getMap(); 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); + MessageBoxA(0, message.c_str(), "Scene Switcher", 0); } void Switcher::start() @@ -119,7 +138,6 @@ bool Switcher::isWindowFullscreen() { RECT rc; GetWindowRect(GetDesktopWindow(), &rc); HWND hwnd = GetForegroundWindow(); - //return (GetWindowLong(hwnd, GWL_STYLE) & WS_POPUP != 0); //Check we haven't picked up the desktop or the shell if (hwnd != GetDesktopWindow() || hwnd != GetShellWindow()) { @@ -149,6 +167,12 @@ void Switcher::stop() { return; } + +string Switcher::getSettingsFilePath() +{ + return settings.getSettingsFilePath(); +} + bool Switcher::getIsRunning() { return isRunning; diff --git a/switcher.h b/switcher.h index f0c2bca8..14bf67fd 100644 --- a/switcher.h +++ b/switcher.h @@ -15,6 +15,7 @@ public: void load(); void start(); void stop(); + string getSettingsFilePath(); thread switcherThread; private: bool isRunning = true;