diff --git a/JavascriptApi.h b/JavascriptApi.h index 60b7476..ecfc10a 100644 --- a/JavascriptApi.h +++ b/JavascriptApi.h @@ -38,6 +38,8 @@ class JavascriptApi JS_STOP_WEBSERVER, JS_LAUNCH_OS_BROWSER_URL, JS_GET_AUTH_TOKEN, + JS_SOURCE_FILTER_ADD, + JS_SOURCE_FILTER_REMOVE, JS_CLEAR_AUTH_TOKEN, JS_SET_CURRENT_SCENE, JS_GET_CURRENT_SCENE, @@ -432,6 +434,14 @@ class JavascriptApi // Example arg1 = { "value": true } {"obs_frontend_streaming_active", JS_GET_IS_OBS_STREAMING}, + // .(@function(arg1), @sourceName_target, @sourceName_filter) + // Attaches the filter to a source. + {"obs_source_filter_add", JS_SOURCE_FILTER_ADD}, + + // .(@function(arg1), @sourceName_target, @sourceName_filter) + // Removes a filter from a source. + {"obs_source_filter_remove", JS_SOURCE_FILTER_REMOVE}, + /*** * Web */ diff --git a/PluginJsHandler.cpp b/PluginJsHandler.cpp index 6a53369..b057843 100644 --- a/PluginJsHandler.cpp +++ b/PluginJsHandler.cpp @@ -254,7 +254,9 @@ void PluginJsHandler::executeApiRequest(const std::string &funcName, const std:: case JavascriptApi::JS_GET_IS_OBS_STREAMING: JS_GET_IS_OBS_STREAMING(jsonParams, jsonReturnStr); break; case JavascriptApi::JS_SAVE_SL_BROWSER_DOCKS: JS_SAVE_SL_BROWSER_DOCKS(jsonParams, jsonReturnStr); break; case JavascriptApi::JS_QT_SET_JS_ON_CLICK_STREAM: JS_QT_SET_JS_ON_CLICK_STREAM(jsonParams, jsonReturnStr); break; - case JavascriptApi::JS_QT_INVOKE_CLICK_ON_STREAM_BUTTON: JS_QT_INVOKE_CLICK_ON_STREAM_BUTTON(jsonParams, jsonReturnStr); break; + case JavascriptApi::JS_QT_INVOKE_CLICK_ON_STREAM_BUTTON: JS_QT_INVOKE_CLICK_ON_STREAM_BUTTON(jsonParams, jsonReturnStr); break; + case JavascriptApi::JS_SOURCE_FILTER_ADD: JS_SOURCE_FILTER_ADD(jsonParams, jsonReturnStr); break; + case JavascriptApi::JS_SOURCE_FILTER_REMOVE: JS_SOURCE_FILTER_REMOVE(jsonParams, jsonReturnStr); break; default: jsonReturnStr = Json(Json::object{{"error", "Unknown Javascript Function"}}).dump(); break; } @@ -1188,6 +1190,70 @@ void PluginJsHandler::JS_GET_IS_OBS_STREAMING(const json11::Json ¶ms, std::s out_jsonReturn = Json(Json::object({{"value", obs_frontend_streaming_active()}})).dump(); } +void PluginJsHandler::JS_SOURCE_FILTER_ADD(const json11::Json ¶ms, std::string &out_jsonReturn) +{ + const auto ¶m2Value = params["param2"]; + const auto ¶m3Value = params["param3"]; + std::string sourceName = param2Value.string_value(); + std::string filterName = param3Value.string_value(); + + QMainWindow *mainWindow = (QMainWindow *)obs_frontend_get_main_window(); + + // This code is executed in the context of the QMainWindow's thread. + QMetaObject::invokeMethod(mainWindow, [mainWindow, sourceName, filterName, &out_jsonReturn]() { + OBSSourceAutoRelease source = obs_get_source_by_name(sourceName.c_str()); + + if (!source) + { + out_jsonReturn = Json(Json::object({{"error", "Did not find source named " + sourceName}})).dump(); + return; + } + + OBSSourceAutoRelease filter = obs_get_source_by_name(filterName.c_str()); + + if (!filter) + { + out_jsonReturn = Json(Json::object({{"error", "Did not find filter named " + filterName}})).dump(); + return; + } + + obs_source_filter_add(source, filter); + out_jsonReturn = Json(Json::object({{"success", true}})).dump(); + }); +} + +void PluginJsHandler::JS_SOURCE_FILTER_REMOVE(const json11::Json ¶ms, std::string &out_jsonReturn) +{ + const auto ¶m2Value = params["param2"]; + const auto ¶m3Value = params["param3"]; + std::string sourceName = param2Value.string_value(); + std::string filterName = param3Value.string_value(); + + QMainWindow *mainWindow = (QMainWindow *)obs_frontend_get_main_window(); + + // This code is executed in the context of the QMainWindow's thread. + QMetaObject::invokeMethod(mainWindow, [mainWindow, sourceName, filterName, &out_jsonReturn]() { + OBSSourceAutoRelease source = obs_get_source_by_name(sourceName.c_str()); + + if (!source) + { + out_jsonReturn = Json(Json::object({{"error", "Did not find source named " + sourceName}})).dump(); + return; + } + + OBSSourceAutoRelease filter = obs_get_source_by_name(filterName.c_str()); + + if (!filter) + { + out_jsonReturn = Json(Json::object({{"error", "Did not find filter named " + filterName}})).dump(); + return; + } + + obs_source_filter_remove(source, filter); + out_jsonReturn = Json(Json::object({{"success", true}})).dump(); + }); +} + void PluginJsHandler::JS_OBS_REMOVE_TRANSITION(const json11::Json ¶ms, std::string &out_jsonReturn) { const auto ¶m2Value = params["param2"]; diff --git a/PluginJsHandler.h b/PluginJsHandler.h index 5fb2794..4fbb7f5 100644 --- a/PluginJsHandler.h +++ b/PluginJsHandler.h @@ -110,7 +110,9 @@ class PluginJsHandler void JS_QT_SET_JS_ON_CLICK_STREAM(const json11::Json ¶ms, std::string &out_jsonReturn); void JS_QT_INVOKE_CLICK_ON_STREAM_BUTTON(const json11::Json ¶ms, std::string &out_jsonReturn); void JS_GET_LOGS_REPORT_STRING(const json11::Json ¶ms, std::string &out_jsonReturn); - + void JS_SOURCE_FILTER_ADD(const json11::Json ¶ms, std::string &out_jsonReturn); + void JS_SOURCE_FILTER_REMOVE(const json11::Json ¶ms, std::string &out_jsonReturn); + std::wstring getDownloadsDir() const; std::wstring getFontsDir() const;