Linux notify
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m33s

This commit is contained in:
2025-07-14 19:47:09 -04:00
parent edcd5a7fca
commit 2291ee6215
6 changed files with 77 additions and 6 deletions

View File

@@ -46,14 +46,17 @@ file(GLOB_RECURSE HEADERS "include/logger/*.h" "include/logger/*.hpp")
if(UNIX AND NOT APPLE) if(UNIX AND NOT APPLE)
file(GLOB_RECURSE SOURCES "src/types/*.cpp" "src/platform/linux/*.cpp" "src/platform/shared/*.cpp" "src/ReWindow/*.cpp" ) file(GLOB_RECURSE SOURCES "src/types/*.cpp" "src/platform/linux/*.cpp" "src/platform/shared/*.cpp" "src/ReWindow/*.cpp" )
find_package(PkgConfig REQUIRED)
pkg_check_modules(DBUS REQUIRED dbus-1)
include_directories("include" ${DBUS_INCLUDE_DIRS})
endif() endif()
if(WIN32) if(WIN32)
file(GLOB_RECURSE SOURCES "src/types/*.cpp" "src/platform/windows/*.cpp" "src/platform/shared/*.cpp" "src/ReWindow/*.cpp") file(GLOB_RECURSE SOURCES "src/types/*.cpp" "src/platform/windows/*.cpp" "src/platform/shared/*.cpp" "src/ReWindow/*.cpp")
include_directories("include")
endif() endif()
include_directories("include")
if(UNIX) if(UNIX)
add_library(ReWindow SHARED ${SOURCES}) add_library(ReWindow SHARED ${SOURCES})
endif() endif()
@@ -68,7 +71,7 @@ target_include_directories(ReWindow PUBLIC ${Event_SOURCE_DIR}/include)
set_target_properties(ReWindow PROPERTIES LINKER_LANGUAGE CXX) set_target_properties(ReWindow PROPERTIES LINKER_LANGUAGE CXX)
if(UNIX AND NOT APPLE) if(UNIX AND NOT APPLE)
target_link_libraries(ReWindow PUBLIC X11 Event jlog) target_link_libraries(ReWindow PUBLIC X11 Event jlog ${DBUS_LIBRARIES})
target_link_libraries(ReWindow PUBLIC) target_link_libraries(ReWindow PUBLIC)
add_executable(ReWindowDemo main.cpp) add_executable(ReWindowDemo main.cpp)
target_link_libraries(ReWindowDemo PUBLIC ReWindow) target_link_libraries(ReWindowDemo PUBLIC ReWindow)

View File

@@ -25,8 +25,8 @@ A library which allows easily creating and managing a window and it's events acr
Install dependencies Install dependencies
```bash ```bash
Fedora/RHEL: dnf install cmake make gcc-g++ libX11 libX11-devel mesa-libGL-devel vulkan-loader-devel Fedora/RHEL: dnf install cmake make gcc-g++ libX11 libX11-devel mesa-libGL-devel vulkan-loader-devel dbus-devel
Ubuntu/Debian: apt-get install cmake make gcc g++ libx11-6 libx11-dev libgl-dev libvulkan-dev libxrandr-dev Ubuntu/Debian: apt-get install cmake make gcc g++ libx11-6 libx11-dev libgl-dev libvulkan-dev libxrandr-dev libdbus-1-dev
``` ```
Clone the repository Clone the repository

View File

@@ -4,6 +4,7 @@
#include <vector> #include <vector>
#include <queue> #include <queue>
#include <map> #include <map>
#include <filesystem>
#include <Event.h> #include <Event.h>
#include <ReWindow/types/Key.h> #include <ReWindow/types/Key.h>
#include <ReWindow/types/Cursors.h> #include <ReWindow/types/Cursors.h>
@@ -371,6 +372,12 @@ public:
/// @note If the window is already in focus when this is called nothing happens. /// @note If the window is already in focus when this is called nothing happens.
void Flash(); void Flash();
/// Sends a notification to the operating system notification area.
/// @param title The notification title.
/// @param content The actual text of the notification.
/// @param icon The path to the icon to be displayed on the notification, Empty for none.
bool Notify(const std::string& title, const std::string& content, const std::filesystem::path& icon = "");
/// @returns True if we are definitely running on a software renderer. /// @returns True if we are definitely running on a software renderer.
/// @note For some APIs this isn't typically possible. /// @note For some APIs this isn't typically possible.
[[nodiscard]] virtual bool SoftwareRendered() { return false; } [[nodiscard]] virtual bool SoftwareRendered() { return false; }

View File

@@ -48,6 +48,7 @@ int main() {
auto* window = new MyWindow("Test Window", 600, 480); auto* window = new MyWindow("Test Window", 600, 480);
Logger::Debug(std::format("New window '{}' created. width={} height={}", window->GetTitle(), window->GetWidth(), window->GetHeight())); Logger::Debug(std::format("New window '{}' created. width={} height={}", window->GetTitle(), window->GetWidth(), window->GetHeight()));
window->Notify("Hello", "Test notification.");
if (window->Open()) if (window->Open())
Logger::Debug(std::format("Opened window '{}'", window->GetTitle())); Logger::Debug(std::format("Opened window '{}'", window->GetTitle()));

View File

@@ -1 +1 @@
Main:new("Install build dependencies", "apt-get install -yq libx11-6 libx11-dev libgl-dev libvulkan-dev") Main:new("Install build dependencies", "apt-get install -yq libx11-6 libx11-dev libgl-dev libvulkan-dev libdbus-1-dev")

View File

@@ -7,6 +7,7 @@
#include <X11/Xutil.h> #include <X11/Xutil.h>
#include <X11/Xatom.h> #include <X11/Xatom.h>
#include <thread> #include <thread>
#include <dbus/dbus.h>
class ReWindow::RWindow::Platform { class ReWindow::RWindow::Platform {
public: public:
@@ -31,6 +32,65 @@ public:
using namespace ReWindow; using namespace ReWindow;
bool RWindow::Notify(const std::string& title, const std::string& content, const std::filesystem::path& icon) {
DBusError db_error;
dbus_error_init(&db_error);
DBusConnection* db_connection = dbus_bus_get(DBUS_BUS_SESSION, &db_error);
if (!db_connection) {
dbus_error_free(&db_error);
return false;
}
DBusMessage* db_message = dbus_message_new_method_call(
"org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
"org.freedesktop.Notifications",
"Notify"
);
if (!db_message)
return false;
const char* sender = this->title.c_str();
uint32_t replaces_id = 0;
const char* app_icon = icon.c_str();
const char* summary = title.c_str();
const char* body = content.c_str();
const char** actions = nullptr;
DBusMessageIter db_args;
dbus_message_iter_init_append(db_message, &db_args);
dbus_message_iter_append_basic(&db_args, DBUS_TYPE_STRING, &sender);
dbus_message_iter_append_basic(&db_args, DBUS_TYPE_UINT32, &replaces_id);
dbus_message_iter_append_basic(&db_args, DBUS_TYPE_STRING, &app_icon);
dbus_message_iter_append_basic(&db_args, DBUS_TYPE_STRING, &summary);
dbus_message_iter_append_basic(&db_args, DBUS_TYPE_STRING, &body);
DBusMessageIter db_actions_array_iterator;
dbus_message_iter_open_container(&db_args, DBUS_TYPE_ARRAY, "s", &db_actions_array_iterator);
dbus_message_iter_close_container(&db_args, &db_actions_array_iterator);
dbus_message_iter_open_container(&db_args, DBUS_TYPE_ARRAY, "{sv}", &db_actions_array_iterator);
dbus_message_iter_close_container(&db_args, &db_actions_array_iterator);
int32_t notif_timeout = 5000;
dbus_message_iter_append_basic(&db_args, DBUS_TYPE_INT32, &notif_timeout);
DBusMessage* db_reply = dbus_connection_send_with_reply_and_block(db_connection, db_message, -1, &db_error);
dbus_message_unref(db_message);
if (!db_reply) {
dbus_error_free(&db_error);
return false;
}
dbus_message_unref(db_reply);
return true;
}
void RWindow::SetSize(const std::pair<int, int>& size) { void RWindow::SetSize(const std::pair<int, int>& size) {
this->SetSize(size.first, size.second); this->SetSize(size.first, size.second);
} }