support for regex

This commit is contained in:
WarmUpTill 2016-05-30 21:26:34 +02:00
parent e89080574f
commit 9873d62121
5 changed files with 76 additions and 53 deletions

View File

@ -3,7 +3,8 @@
#include "stdafx.h"
#include "switcher.h"
#include <obs-module.h>
#include <obs-ui.h>
//#include <QtWidgets/QApplication>
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<Switcher *>(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();

View File

@ -11,28 +11,23 @@
#include <shlwapi.h>
#pragma comment(lib,"shlwapi.lib")
#include "shlobj.h"
#include "settings.h"
using namespace std;
string Settings::getSettingsFilePath()
{
return settingsFilePath;
}
class Settings {
map<string, string> settings;
public:
void load(string);
map<string, string> getMap();
private :
void addToMap(string, string);
};
void Settings::load(string filepath) {
void Settings::load() {
//read the settings file
std::vector<std::string> settingsElements;
int numValues = 0;
ifstream infile(filepath);
ifstream infile(settingsFilePath);
string value;
string line;

View File

@ -1,19 +1,14 @@
#pragma once
#include <map>
#ifndef SETTINGS_H
#define SETTINGS_H
using namespace std;
class Settings {
map<string, string> settings;
public:
void load(string);
void load();
map<string, string> getMap();
string getSettingsFilePath();
private:
string settingsFilePath = "..\\..\\data\\obs-plugins\\SceneSwitcher\\settings.txt";
void addToMap(string, string);
};
#endif

View File

@ -10,45 +10,48 @@
#include <chrono>
#include <obs.h>
#include <thread>
#include <regex>
#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<string, string> 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<string, string>::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;

View File

@ -15,6 +15,7 @@ public:
void load();
void start();
void stop();
string getSettingsFilePath();
thread switcherThread;
private:
bool isRunning = true;