From 6a2071c20e8a17615439fd69479434cb530f485a Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Thu, 24 Nov 2016 15:40:25 +0100 Subject: [PATCH] Added Idle Detection tab --- advanced-scene-switcher-win.cpp | 22 +++++ advanced-scene-switcher.cpp | 159 ++++++++++++++++++++++++++++---- advanced-scene-switcher.hpp | 75 +++++++++------ switcher-data-structs.hpp | 8 ++ utility.hpp | 5 +- 5 files changed, 223 insertions(+), 46 deletions(-) diff --git a/advanced-scene-switcher-win.cpp b/advanced-scene-switcher-win.cpp index 8f494d8a..a106cf02 100644 --- a/advanced-scene-switcher-win.cpp +++ b/advanced-scene-switcher-win.cpp @@ -142,3 +142,25 @@ bool isInFocus(const QString &exeToCheck) { } //dasoven region_end +int getLastInputTime() +{ + LASTINPUTINFO lastInputInfo; + lastInputInfo.cbSize = sizeof(LASTINPUTINFO); //<------------- crash here + if (GetLastInputInfo(&lastInputInfo)) + return lastInputInfo.dwTime; + return 0; +} + +int getTime() +{ + return GetTickCount(); +} + +bool noInput(DWORD startTime, int time) +{ + PLASTINPUTINFO lastInputInfo; + if (GetLastInputInfo(lastInputInfo)) + if (lastInputInfo->dwTime - startTime > time * 1000) + return true; + return false; +} diff --git a/advanced-scene-switcher.cpp b/advanced-scene-switcher.cpp index d669966d..12b547c3 100644 --- a/advanced-scene-switcher.cpp +++ b/advanced-scene-switcher.cpp @@ -54,6 +54,7 @@ struct SwitcherData vector ignoreWindowsSwitches; vector sceneRoundTripSwitches; + bool autoStopEnable = false; OBSWeakSource autoStopScene; @@ -62,6 +63,8 @@ struct SwitcherData vector executableSwitches; // dasoven FileIOData fileIO; + + IdleData idleData; void Thread(); void Start(); @@ -104,6 +107,12 @@ struct SwitcherData sceneRoundTripSwitches.erase(sceneRoundTripSwitches.begin() + i--); } + if (!WeakSourceValid(autoStopScene)) + { + autoStopScene = NULL; + autoStopEnable = false; + } + for (size_t i = 0; i < sceneTransitions.size(); i++) { SceneTransition& s = sceneTransitions[i]; @@ -119,6 +128,13 @@ struct SwitcherData if (!WeakSourceValid(s.mScene) || !WeakSourceValid(s.mTransition)) switches.erase(switches.begin() + i--); } // dasoven region_end + + if (!WeakSourceValid(idleData.scene) || !WeakSourceValid(idleData.transition)) + { + idleData.scene = NULL; + idleData.transition = NULL; + idleData.idleEnable = false; + } } inline ~SwitcherData() @@ -154,16 +170,13 @@ SceneSwitcher::SceneSwitcher(QWidget* parent) ui->transitionsScene1->addItem(name); ui->transitionsScene2->addItem(name); ui->executableScenes->addItem(name); // dasoven + ui->idleScenes->addItem(name); temp++; } obs_frontend_source_list* transitions = new obs_frontend_source_list(); obs_frontend_get_transitions(transitions); - // ui->transitions->addItem("Default"); - // ui->screenRegionsTransitions->addItem("Default"); - // ui->sceneRoundTripTransitions->addItem("Default"); - for (size_t i = 0; i < transitions->sources.num; i++) { const char* name = obs_source_get_name(transitions->sources.array[i]); @@ -172,12 +185,9 @@ SceneSwitcher::SceneSwitcher(QWidget* parent) ui->sceneRoundTripTransitions->addItem(name); ui->transitionsTransitions->addItem(name); ui->executableTransitions->addItem(name); // dasoven + ui->idleTransitions->addItem(name); } - // ui->transitions->addItem("Random"); - // ui->screenRegionsTransitions->addItem("Random"); - // ui->sceneRoundTripTransitions->addItem("Random"); - obs_frontend_source_list_free(transitions); if (switcher->switchIfNotMatching) @@ -299,6 +309,24 @@ SceneSwitcher::SceneSwitcher(QWidget* parent) item->setData(Qt::UserRole, text); } + ui->idleCheckBox->setChecked(switcher->idleData.idleEnable); + ui->idleScenes->setCurrentText(GetWeakSourceName(switcher->idleData.scene).c_str()); + ui->idleTransitions->setCurrentText(GetWeakSourceName(switcher->idleData.transition).c_str()); + ui->idleSpinBox->setValue(switcher->idleData.time); + + if (ui->idleCheckBox->checkState()) + { + ui->idleScenes->setDisabled(false); + ui->idleSpinBox->setDisabled(false); + ui->idleTransitions->setDisabled(false); + } + else + { + ui->idleScenes->setDisabled(true); + ui->idleSpinBox->setDisabled(true); + ui->idleTransitions->setDisabled(true); + } + ui->readPathLineEdit->setText(QString::fromStdString(switcher->fileIO.readPath.c_str())); ui->readFileCheckBox->setChecked(switcher->fileIO.readEnabled); ui->writePathLineEdit->setText(QString::fromStdString(switcher->fileIO.writePath.c_str())); @@ -1296,6 +1324,74 @@ void SceneSwitcher::on_executableRemove_clicked() // DASOVEN REGION_END +void SceneSwitcher::on_idleCheckBox_stateChanged(int state) +{ + if (loading) + return; + + lock_guard lock(switcher->m); + if (!state) + { + ui->idleScenes->setDisabled(true); + ui->idleSpinBox->setDisabled(true); + ui->idleTransitions->setDisabled(true); + + switcher->idleData.idleEnable = false; + } + else + { + ui->idleScenes->setDisabled(false); + ui->idleSpinBox->setDisabled(false); + ui->idleTransitions->setDisabled(false); + + switcher->idleData.idleEnable = true; + } +} + + +void SceneSwitcher::UpdateIdleDataTransition(const QString& name) +{ + obs_weak_source_t* transition = GetWeakTransitionByQString(name); + switcher->idleData.transition = transition; +} + +void SceneSwitcher::UpdateIdleDataScene(const QString& name) +{ + obs_source_t* scene = obs_get_source_by_name(name.toUtf8().constData()); + obs_weak_source_t* ws = obs_source_get_weak_source(scene); + + switcher->idleData.scene = ws; + + obs_weak_source_release(ws); + obs_source_release(scene); +} + +void SceneSwitcher::on_idleTransitions_currentTextChanged(const QString& text) +{ + if (loading) + return; + + lock_guard lock(switcher->m); + UpdateIdleDataTransition(text); +} + +void SceneSwitcher::on_idleScenes_currentTextChanged(const QString& text) +{ + if (loading) + return; + + lock_guard lock(switcher->m); + UpdateIdleDataScene(text); +} + +void SceneSwitcher::on_idleSpinBox_valueChanged(int i) +{ + if (loading) + return; + lock_guard lock(switcher->m); + switcher->idleData.time = i; +} + void SceneSwitcher::on_browseButton_clicked() { QString directory = QFileDialog::getOpenFileName( @@ -1649,6 +1745,13 @@ static void SaveSceneSwitcher(obs_data_t* save_data, bool saving, void*) obs_data_set_bool(obj, "autoStopEnable", switcher->autoStopEnable); obs_data_set_string(obj, "autoStopSceneName", autoStopSceneName.c_str()); + string idleSceneName = GetWeakSourceName(switcher->idleData.scene); + string idleTransitionName = GetWeakSourceName(switcher->idleData.transition); + obs_data_set_bool(obj, "idleEnable", switcher->idleData.idleEnable); + obs_data_set_string(obj, "idleSceneName", idleSceneName.c_str()); + obs_data_set_string(obj, "idleTransitionName", idleTransitionName.c_str()); + obs_data_set_int(obj, "idleTime", switcher->idleData.time); + obs_data_set_bool(obj, "readEnabled", switcher->fileIO.readEnabled); obs_data_set_string(obj, "readPath", switcher->fileIO.readPath.c_str()); obs_data_set_bool(obj, "writeEnabled", switcher->fileIO.writeEnabled); @@ -1839,6 +1942,13 @@ static void SaveSceneSwitcher(obs_data_t* save_data, bool saving, void*) switcher->autoStopEnable = obs_data_get_bool(obj, "autoStopEnable"); switcher->autoStopScene = GetWeakSourceByName(autoStopScene.c_str()); + string idleSceneName = obs_data_get_string(obj, "idleSceneName"); + string idleTransitionName = obs_data_get_string(obj, "idleTransitionName"); + switcher->idleData.scene = GetWeakSourceByName(idleSceneName.c_str()); + switcher->idleData.transition = GetWeakTransitionByName(idleTransitionName.c_str()); + switcher->idleData.idleEnable = obs_data_get_bool(obj, "idleEnable"); + switcher->idleData.time = obs_data_get_int(obj, "idleTime"); + switcher->fileIO.readEnabled = obs_data_get_bool(obj, "readEnabled"); switcher->fileIO.readPath = obs_data_get_string(obj, "readPath"); switcher->fileIO.writeEnabled = obs_data_get_bool(obj, "writeEnabled"); @@ -2088,19 +2198,36 @@ void SwitcherData::Thread() // End Ignore Windows - // Regular switch - switcher->Prune(); - for (SceneSwitch& s : switches) + // Idle switch + + if (!match && idleData.idleEnable) { - if (s.window == title) + if (getTime() - getLastInputTime() > idleData.time * 1000) { + scene = idleData.scene; + transition = idleData.transition; + fullscreen = false; match = true; - scene = s.scene; - transition = s.transition; - fullscreen = s.fullscreen; - break; + } + } + + // End Idle switch + + // Regular switch + + if (!match){ + for (SceneSwitch& s : switches) + { + if (s.window == title) + { + match = true; + scene = s.scene; + transition = s.transition; + fullscreen = s.fullscreen; + break; + } } } diff --git a/advanced-scene-switcher.hpp b/advanced-scene-switcher.hpp index c7d50b90..813bd0f9 100644 --- a/advanced-scene-switcher.hpp +++ b/advanced-scene-switcher.hpp @@ -32,62 +32,79 @@ public: int IgnoreWindowsFindByData(const QString ®ion); int SceneRoundTripFindByData(const QString &scene1); int SceneTransitionsFindByData(const QString &scene1, const QString &scene2); - int executableFindByData(const QString &exe);//dasoven void UpdateNonMatchingScene(const QString &name); void UpdateAutoStopScene(const QString &name); + void UpdateIdleDataTransition(const QString& name); + void UpdateIdleDataScene(const QString& name); public slots: void on_switches_currentRowChanged(int idx); - void on_screenRegions_currentRowChanged(int idx); - void on_pauseScenes_currentRowChanged(int idx); - void on_pauseWindows_currentRowChanged(int idx); - void on_ignoreWindows_currentRowChanged(int idx); - void on_sceneRoundTrips_currentRowChanged(int idx); - void on_sceneTransitions_currentRowChanged(int idx); - void on_close_clicked(); void on_add_clicked(); void on_remove_clicked(); - void on_screenRegionAdd_clicked(); - void on_screenRegionRemove_clicked(); - void on_pauseScenesAdd_clicked(); - void on_pauseScenesRemove_clicked(); - void on_pauseWindowsAdd_clicked(); - void on_pauseWindowsRemove_clicked(); - void on_ignoreWindowsAdd_clicked(); - void on_ignoreWindowsRemove_clicked(); - void on_sceneRoundTripAdd_clicked(); - void on_sceneRoundTripRemove_clicked(); - void on_autoStopSceneCheckBox_stateChanged(int state); - void on_autoStopScenes_currentTextChanged(const QString &text); - void on_transitionsAdd_clicked(); - void on_transitionsRemove_clicked(); - void on_browseButton_clicked(); - void on_readFileCheckBox_stateChanged(int state); - void on_readPathLineEdit_textChanged(const QString & text); - void on_writePathLineEdit_textChanged(const QString & text); - void on_browseButton_2_clicked(); void on_noMatchDontSwitch_clicked(); void on_noMatchSwitch_clicked(); void on_startAtLaunch_toggled(bool value); void on_noMatchSwitchScene_currentTextChanged(const QString &text); void on_checkInterval_valueChanged(int value); void on_toggleStartButton_clicked(); - void updateScreenRegionCursorPos(); + + void on_screenRegions_currentRowChanged(int idx); + void on_screenRegionAdd_clicked(); + void on_screenRegionRemove_clicked(); + + void on_pauseScenes_currentRowChanged(int idx); + void on_pauseScenesAdd_clicked(); + void on_pauseScenesRemove_clicked(); + + void on_pauseWindows_currentRowChanged(int idx); + void on_pauseWindowsAdd_clicked(); + void on_pauseWindowsRemove_clicked(); + + void on_ignoreWindows_currentRowChanged(int idx); + void on_ignoreWindowsAdd_clicked(); + void on_ignoreWindowsRemove_clicked(); + + void on_sceneRoundTrips_currentRowChanged(int idx); + void on_sceneRoundTripAdd_clicked(); + void on_sceneRoundTripRemove_clicked(); + + void on_autoStopSceneCheckBox_stateChanged(int state); + void on_autoStopScenes_currentTextChanged(const QString &text); + + void on_sceneTransitions_currentRowChanged(int idx); + void on_transitionsAdd_clicked(); + void on_transitionsRemove_clicked(); + + void on_browseButton_clicked(); + void on_readFileCheckBox_stateChanged(int state); + void on_readPathLineEdit_textChanged(const QString & text); + void on_writePathLineEdit_textChanged(const QString & text); + void on_browseButton_2_clicked(); //dasoven region_start void on_executableAdd_clicked(); void on_executableRemove_clicked(); void on_executables_currentRowChanged(int idx); //dasoven region_end + + void on_idleCheckBox_stateChanged(int state); + void on_idleTransitions_currentTextChanged(const QString& text); + void on_idleScenes_currentTextChanged(const QString& text); + void on_idleSpinBox_valueChanged(int i); + + void updateScreenRegionCursorPos(); + + void on_close_clicked(); }; void GetWindowList(std::vector &windows); void GetCurrentWindowTitle(std::string &title); std::pair getCursorPos(); bool isFullscreen(); - +int getLastInputTime(); +int getTime(); //dasoven region_start bool isInFocus(const QString &exeToCheck); diff --git a/switcher-data-structs.hpp b/switcher-data-structs.hpp index f85f89a6..baee82f6 100644 --- a/switcher-data-structs.hpp +++ b/switcher-data-structs.hpp @@ -96,4 +96,12 @@ struct FileIOData string readPath; bool writeEnabled; string writePath; +}; + +struct IdleData +{ + bool idleEnable; + int time; + OBSWeakSource scene; + OBSWeakSource transition; }; \ No newline at end of file diff --git a/utility.hpp b/utility.hpp index a7acda79..711b59e5 100644 --- a/utility.hpp +++ b/utility.hpp @@ -105,18 +105,21 @@ static inline OBSWeakSource GetWeakTransitionByName(const char* transitionName) obs_frontend_source_list* transitions = new obs_frontend_source_list(); obs_frontend_get_transitions(transitions); + bool match = false; for (size_t i = 0; i < transitions->sources.num; i++) { const char* name = obs_source_get_name(transitions->sources.array[i]); if (strcmp(transitionName, name) == 0) { + match = true; source = transitions->sources.array[i]; break; } } - weak = obs_source_get_weak_source(source); + if (match) + weak = obs_source_get_weak_source(source); obs_frontend_source_list_free(transitions); obs_weak_source_release(weak);