From 34da16b7e626be4d96c3098b5265e2c008fb2d65 Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Mon, 20 Mar 2023 15:23:25 +0000 Subject: [PATCH] plugin api: add addNotificationV2 Allows for issuing fancy notifs via api --- example/examplePlugin/main.cpp | 3 ++ src/SharedDefs.hpp | 11 ++++++ src/debug/HyprNotificationOverlay.hpp | 11 +----- src/plugins/PluginAPI.cpp | 48 +++++++++++++++++++++++++++ src/plugins/PluginAPI.hpp | 16 +++++++++ 5 files changed, 79 insertions(+), 10 deletions(-) create mode 100644 src/SharedDefs.hpp diff --git a/example/examplePlugin/main.cpp b/example/examplePlugin/main.cpp index 49364f59..6007a58b 100644 --- a/example/examplePlugin/main.cpp +++ b/example/examplePlugin/main.cpp @@ -77,6 +77,9 @@ APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) { g_pMouseDownHook = HyprlandAPI::createFunctionHook( PHANDLE, HyprlandAPI::getFunctionAddressFromSignature(PHANDLE, "_ZN13CInputManager22processMouseDownNormalEP24wlr_pointer_button_event"), (void*)&hkProcessMouseDownNormal); + // fancy notifications + HyprlandAPI::addNotificationV2(PHANDLE, {{"text", "Example hint"}, {"time", (uint64_t)10000}, {"color", CColor(0.2, 0.2, 0.9, 1.0)}, {"icon", ICON_HINT}}); + // Enable our hooks g_pFocusHook->hook(); g_pMotionHook->hook(); diff --git a/src/SharedDefs.hpp b/src/SharedDefs.hpp new file mode 100644 index 00000000..8ae1d0a2 --- /dev/null +++ b/src/SharedDefs.hpp @@ -0,0 +1,11 @@ +#pragma once + +enum eIcons +{ + ICON_WARNING = 0, + ICON_INFO, + ICON_HINT, + ICON_ERROR, + ICON_CONFUSED, + ICON_NONE +}; \ No newline at end of file diff --git a/src/debug/HyprNotificationOverlay.hpp b/src/debug/HyprNotificationOverlay.hpp index 9f0a85ef..fb3414da 100644 --- a/src/debug/HyprNotificationOverlay.hpp +++ b/src/debug/HyprNotificationOverlay.hpp @@ -4,6 +4,7 @@ #include "../helpers/Timer.hpp" #include "../helpers/Monitor.hpp" #include "../render/Texture.hpp" +#include "../SharedDefs.hpp" #include @@ -16,16 +17,6 @@ enum eIconBackend ICONS_BACKEND_FA }; -enum eIcons -{ - ICON_WARNING = 0, - ICON_INFO, - ICON_HINT, - ICON_ERROR, - ICON_CONFUSED, - ICON_NONE -}; - static const std::array, 3 /* backends */> ICONS_ARRAY = {std::array{"[!]", "[i]", "[Hint]", "[Err]", "[?]", ""}, std::array{"", "", "", "", "", ""}, std::array{"", "", "", "", ""}}; diff --git a/src/plugins/PluginAPI.cpp b/src/plugins/PluginAPI.cpp index 8c816546..c878516b 100644 --- a/src/plugins/PluginAPI.cpp +++ b/src/plugins/PluginAPI.cpp @@ -193,3 +193,51 @@ APICALL bool HyprlandAPI::removeDispatcher(HANDLE handle, const std::string& nam return true; } + +APICALL bool addNotificationV2(HANDLE handle, const std::unordered_map& data) { + auto* const PLUGIN = g_pPluginSystem->getPluginByHandle(handle); + + if (!PLUGIN) + return false; + + try { + auto iterator = data.find("text"); + if (iterator == data.end()) + return false; + + // mandatory + std::string text; + try { + text = std::any_cast(iterator->second); + } catch (std::exception& e) { + // attempt const char* + text = std::any_cast(iterator->second); + } + + iterator = data.find("time"); + if (iterator == data.end()) + return false; + + const auto TIME = std::any_cast(iterator->second); + + iterator = data.find("color"); + if (iterator == data.end()) + return false; + + const auto COLOR = std::any_cast(iterator->second); + + // optional + eIcons icon = ICON_NONE; + iterator = data.find("icon"); + if (iterator != data.end()) + icon = std::any_cast(iterator->second); + + g_pHyprNotificationOverlay->addNotification(text, COLOR, TIME, icon); + + } catch (std::exception& e) { + // bad any_cast most likely, plugin error + return false; + } + + return true; +} \ No newline at end of file diff --git a/src/plugins/PluginAPI.hpp b/src/plugins/PluginAPI.hpp index 756a5631..80744bc3 100644 --- a/src/plugins/PluginAPI.hpp +++ b/src/plugins/PluginAPI.hpp @@ -19,6 +19,7 @@ See examples/examplePlugin for an example plugin #include "../helpers/Color.hpp" #include "HookSystem.hpp" +#include "../SharedDefs.hpp" #include #include @@ -214,4 +215,19 @@ namespace HyprlandAPI { returns: true on success. False otherwise. */ APICALL bool removeDispatcher(HANDLE handle, const std::string& name); + + /* + Adds a notification. + + data has to contain: + - text: std::string or const char* + - time: uint64_t + - color: CColor + + data may contain: + - icon: eIcons + + returns: true on success. False otherwise. + */ + APICALL bool addNotificationV2(HANDLE handle, const std::unordered_map& data); }; \ No newline at end of file