Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: move to sdbus-cpp2 #278

Merged
merged 4 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pkg_check_modules(
hyprwayland-scanner>=0.4.2)

# check whether we can find sdbus-c++ through pkg-config
pkg_check_modules(SDBUS IMPORTED_TARGET sdbus-c++)
pkg_check_modules(SDBUS IMPORTED_TARGET sdbus-c++>=2.0.0)
if(NOT SDBUS_FOUND)
include_directories("subprojects/sdbus-cpp/include/")
add_subdirectory(subprojects/sdbus-cpp EXCLUDE_FROM_ALL)
Expand Down
14 changes: 14 additions & 0 deletions nix/overlays.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ in {
inputs.hyprland-protocols.overlays.default
inputs.hyprutils.overlays.default
inputs.hyprwayland-scanner.overlays.default
self.overlays.sdbuscpp
]);
xdg-desktop-portal-hyprland = lib.composeManyExtensions [
(final: prev: {
Expand All @@ -32,4 +33,17 @@ in {
};
})
];

sdbuscpp = final: prev: {
sdbus-cpp = prev.sdbus-cpp.overrideAttrs (self: super: {
version = "2.0.0";

src = final.fetchFromGitHub {
owner = "Kistler-group";
repo = "sdbus-cpp";
rev = "refs/tags/v${self.version}";
hash = "sha256-W8V5FRhV3jtERMFrZ4gf30OpIQLYoj2yYGpnYOmH2+g=";
};
});
};
}
4 changes: 2 additions & 2 deletions src/core/PortalManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ void CPortalManager::init() {
m_iPID = getpid();

try {
m_pConnection = sdbus::createSessionBusConnection("org.freedesktop.impl.portal.desktop.hyprland");
m_pConnection = sdbus::createSessionBusConnection(sdbus::ServiceName{"org.freedesktop.impl.portal.desktop.hyprland"});
} catch (std::exception& e) {
Debug::log(CRIT, "Couldn't create the dbus connection ({})", e.what());
exit(1);
Expand Down Expand Up @@ -367,7 +367,7 @@ void CPortalManager::startEventLoop() {
m_mEventLock.lock();

if (pollfds[0].revents & POLLIN /* dbus */) {
while (m_pConnection->processPendingRequest()) {
while (m_pConnection->processPendingEvent()) {
;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/core/PortalManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "wlr-screencopy-unstable-v1.hpp"

#include "../includes.hpp"
#include "../dbusDefines.hpp"

#include <mutex>

Expand Down
5 changes: 5 additions & 0 deletions src/dbusDefines.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <sdbus-c++/sdbus-c++.h>

typedef std::tuple<uint32_t, std::unordered_map<std::string, sdbus::Variant>> dbUasv;
6 changes: 0 additions & 6 deletions src/helpers/MiscFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,3 @@ bool inShellPath(const std::string& exec) {

return std::ranges::any_of(paths, [&exec](std::string& path) { return access((path + "/" + exec).c_str(), X_OK) == 0; });
}

void sendEmptyDbusMethodReply(sdbus::MethodCall& call, u_int32_t responseCode) {
auto reply = call.createReply();
reply << (uint32_t)responseCode;
reply.send();
}
103 changes: 33 additions & 70 deletions src/portals/GlobalShortcuts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,8 @@ SKeybind* CGlobalShortcutsPortal::registerShortcut(SSession* session, const DBus
return PSHORTCUT;
}

void CGlobalShortcutsPortal::onCreateSession(sdbus::MethodCall& call) {
sdbus::ObjectPath requestHandle, sessionHandle;

call >> requestHandle;
call >> sessionHandle;

std::string appID;
call >> appID;

dbUasv CGlobalShortcutsPortal::onCreateSession(sdbus::ObjectPath requestHandle, sdbus::ObjectPath sessionHandle, std::string appID,
std::unordered_map<std::string, sdbus::Variant> opts) {
Debug::log(LOG, "[globalshortcuts] New session:");
Debug::log(LOG, "[globalshortcuts] | {}", requestHandle.c_str());
Debug::log(LOG, "[globalshortcuts] | {}", sessionHandle.c_str());
Expand All @@ -87,9 +80,6 @@ void CGlobalShortcutsPortal::onCreateSession(sdbus::MethodCall& call) {
PSESSION->request = createDBusRequest(requestHandle);
PSESSION->request->onDestroy = [PSESSION]() { PSESSION->request.release(); };

std::unordered_map<std::string, sdbus::Variant> opts;
call >> opts;

for (auto& [k, v] : opts) {
if (k == "shortcuts") {
PSESSION->registered = true;
Expand All @@ -104,101 +94,86 @@ void CGlobalShortcutsPortal::onCreateSession(sdbus::MethodCall& call) {
}
}

auto reply = call.createReply();
reply << (uint32_t)0;
reply << std::unordered_map<std::string, sdbus::Variant>{};
reply.send();
return {0, {}};
}

void CGlobalShortcutsPortal::onBindShortcuts(sdbus::MethodCall& call) {
sdbus::ObjectPath sessionHandle, requestHandle;
call >> requestHandle;
call >> sessionHandle;

dbUasv CGlobalShortcutsPortal::onBindShortcuts(sdbus::ObjectPath requestHandle, sdbus::ObjectPath sessionHandle, std::vector<DBusShortcut> shortcuts, std::string appID,
std::unordered_map<std::string, sdbus::Variant> opts) {
Debug::log(LOG, "[globalshortcuts] Bind keys:");
Debug::log(LOG, "[globalshortcuts] | {}", sessionHandle.c_str());

std::vector<DBusShortcut> shortcuts;
std::vector<DBusShortcut> shortcutsToReturn;
call >> shortcuts;

const auto PSESSION = getSession(sessionHandle);

if (!PSESSION) {
Debug::log(ERR, "[globalshortcuts] No session?");
return;
return {1, {}};
}

std::vector<DBusShortcut> shortcutsToReturn;

PSESSION->registered = true;

for (auto& s : shortcuts) {
const auto* PSHORTCUT = registerShortcut(PSESSION, s);

std::unordered_map<std::string, sdbus::Variant> shortcutData;
shortcutData["description"] = PSHORTCUT->description;
shortcutData["trigger_description"] = "";
shortcutData["description"] = sdbus::Variant{PSHORTCUT->description};
shortcutData["trigger_description"] = sdbus::Variant{""};
shortcutsToReturn.push_back({PSHORTCUT->id, shortcutData});
}

Debug::log(LOG, "[globalshortcuts] registered {} shortcuts", shortcuts.size());

auto reply = call.createReply();

std::unordered_map<std::string, sdbus::Variant> data;
data["shortcuts"] = shortcutsToReturn;
data["shortcuts"] = sdbus::Variant{shortcutsToReturn};

reply << (uint32_t)0;
reply << data;
reply.send();
return {0, data};
}

void CGlobalShortcutsPortal::onListShortcuts(sdbus::MethodCall& call) {
sdbus::ObjectPath sessionHandle, requestHandle;
call >> requestHandle;
call >> sessionHandle;

dbUasv CGlobalShortcutsPortal::onListShortcuts(sdbus::ObjectPath sessionHandle, sdbus::ObjectPath requestHandle) {
Debug::log(LOG, "[globalshortcuts] List keys:");
Debug::log(LOG, "[globalshortcuts] | {}", sessionHandle.c_str());

const auto PSESSION = getSession(sessionHandle);

if (!PSESSION) {
Debug::log(ERR, "[globalshortcuts] No session?");
return;
return {1, {}};
}

std::vector<DBusShortcut> shortcuts;

for (auto& s : PSESSION->keybinds) {
std::unordered_map<std::string, sdbus::Variant> opts;
opts["description"] = s->description;
opts["trigger_description"] = "";
opts["description"] = sdbus::Variant{s->description};
opts["trigger_description"] = sdbus::Variant{""};
shortcuts.push_back({s->id, opts});
}

auto reply = call.createReply();

std::unordered_map<std::string, sdbus::Variant> data;
data["shortcuts"] = shortcuts;
data["shortcuts"] = sdbus::Variant{shortcuts};

reply << (uint32_t)0;
reply << data;
reply.send();
return {0, data};
}

CGlobalShortcutsPortal::CGlobalShortcutsPortal(SP<CCHyprlandGlobalShortcutsManagerV1> mgr) {
m_sState.manager = mgr;

m_pObject = sdbus::createObject(*g_pPortalManager->getConnection(), OBJECT_PATH);

m_pObject->registerMethod(INTERFACE_NAME, "CreateSession", "oosa{sv}", "ua{sv}", [&](sdbus::MethodCall c) { onCreateSession(c); });
m_pObject->registerMethod(INTERFACE_NAME, "BindShortcuts", "ooa(sa{sv})sa{sv}", "ua{sv}", [&](sdbus::MethodCall c) { onBindShortcuts(c); });
m_pObject->registerMethod(INTERFACE_NAME, "ListShortcuts", "oo", "ua{sv}", [&](sdbus::MethodCall c) { onListShortcuts(c); });
m_pObject->registerSignal(INTERFACE_NAME, "Activated", "osta{sv}");
m_pObject->registerSignal(INTERFACE_NAME, "Deactivated", "osta{sv}");
m_pObject->registerSignal(INTERFACE_NAME, "ShortcutsChanged", "oa(sa{sv})");

m_pObject->finishRegistration();
m_pObject
->addVTable(sdbus::registerMethod("CreateSession")
.implementedAs([this](sdbus::ObjectPath o1, sdbus::ObjectPath o2, std::string s, std::unordered_map<std::string, sdbus::Variant> m) {
return onCreateSession(o1, o2, s, m);
}),
sdbus::registerMethod("BindShortcuts")
.implementedAs([this](sdbus::ObjectPath o1, sdbus::ObjectPath o2, std::vector<DBusShortcut> v1, std::string s1,
std::unordered_map<std::string, sdbus::Variant> m2) { return onBindShortcuts(o1, o2, v1, s1, m2); }),
sdbus::registerMethod("ListShortcuts").implementedAs([this](sdbus::ObjectPath o1, sdbus::ObjectPath o2) { return onListShortcuts(o1, o2); }),
sdbus::registerSignal("Activated").withParameters<sdbus::ObjectPath, std::string, uint64_t, std::unordered_map<std::string, sdbus::Variant>>(),
sdbus::registerSignal("Deactivated").withParameters<sdbus::ObjectPath, std::string, uint64_t, std::unordered_map<std::string, sdbus::Variant>>(),
sdbus::registerSignal("ShortcutsChanged").withParameters<sdbus::ObjectPath, std::unordered_map<std::string, std::unordered_map<std::string, sdbus::Variant>>>())
.forInterface(INTERFACE_NAME);

Debug::log(LOG, "[globalshortcuts] registered");
}
Expand All @@ -208,25 +183,13 @@ void CGlobalShortcutsPortal::onActivated(SKeybind* pKeybind, uint64_t time) {

Debug::log(TRACE, "[gs] Session {} called activated on {}", PSESSION->sessionHandle.c_str(), pKeybind->id);

auto signal = m_pObject->createSignal(INTERFACE_NAME, "Activated");
signal << PSESSION->sessionHandle;
signal << pKeybind->id;
signal << time;
signal << std::unordered_map<std::string, sdbus::Variant>{};

m_pObject->emitSignal(signal);
m_pObject->emitSignal("Activated").onInterface(INTERFACE_NAME).withArguments(PSESSION->sessionHandle, pKeybind->id, time, std::unordered_map<std::string, sdbus::Variant>{});
}

void CGlobalShortcutsPortal::onDeactivated(SKeybind* pKeybind, uint64_t time) {
const auto PSESSION = (CGlobalShortcutsPortal::SSession*)pKeybind->session;

Debug::log(TRACE, "[gs] Session {} called deactivated on {}", PSESSION->sessionHandle.c_str(), pKeybind->id);

auto signal = m_pObject->createSignal(INTERFACE_NAME, "Deactivated");
signal << PSESSION->sessionHandle;
signal << pKeybind->id;
signal << time;
signal << std::unordered_map<std::string, sdbus::Variant>{};

m_pObject->emitSignal(signal);
m_pObject->emitSignal("Deactivated").onInterface(INTERFACE_NAME).withArguments(PSESSION->sessionHandle, pKeybind->id, time, std::unordered_map<std::string, sdbus::Variant>{});
}
26 changes: 14 additions & 12 deletions src/portals/GlobalShortcuts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <sdbus-c++/sdbus-c++.h>
#include "hyprland-global-shortcuts-v1.hpp"
#include "../shared/Session.hpp"
#include "../dbusDefines.hpp"

struct SKeybind {
SKeybind(SP<CCHyprlandGlobalShortcutV1> shortcut);
Expand All @@ -15,12 +16,15 @@ class CGlobalShortcutsPortal {
public:
CGlobalShortcutsPortal(SP<CCHyprlandGlobalShortcutsManagerV1> mgr);

void onCreateSession(sdbus::MethodCall& call);
void onBindShortcuts(sdbus::MethodCall& call);
void onListShortcuts(sdbus::MethodCall& call);
using DBusShortcut = sdbus::Struct<std::string, std::unordered_map<std::string, sdbus::Variant>>;

dbUasv onCreateSession(sdbus::ObjectPath requestHandle, sdbus::ObjectPath sessionHandle, std::string appID, std::unordered_map<std::string, sdbus::Variant> opts);
dbUasv onBindShortcuts(sdbus::ObjectPath requestHandle, sdbus::ObjectPath sessionHandle, std::vector<DBusShortcut> shortcuts, std::string appID,
std::unordered_map<std::string, sdbus::Variant> opts);
dbUasv onListShortcuts(sdbus::ObjectPath sessionHandle, sdbus::ObjectPath requestHandle);

void onActivated(SKeybind* pKeybind, uint64_t time);
void onDeactivated(SKeybind* pKeybind, uint64_t time);
void onActivated(SKeybind* pKeybind, uint64_t time);
void onDeactivated(SKeybind* pKeybind, uint64_t time);

struct SSession {
std::string appid;
Expand All @@ -42,12 +46,10 @@ class CGlobalShortcutsPortal {

std::unique_ptr<sdbus::IObject> m_pObject;

using DBusShortcut = sdbus::Struct<std::string, std::unordered_map<std::string, sdbus::Variant>>;

SSession* getSession(sdbus::ObjectPath& path);
SKeybind* getShortcutById(const std::string& appID, const std::string& shortcutId);
SKeybind* registerShortcut(SSession* session, const DBusShortcut& shortcut);
SSession* getSession(sdbus::ObjectPath& path);
SKeybind* getShortcutById(const std::string& appID, const std::string& shortcutId);
SKeybind* registerShortcut(SSession* session, const DBusShortcut& shortcut);

const std::string INTERFACE_NAME = "org.freedesktop.impl.portal.GlobalShortcuts";
const std::string OBJECT_PATH = "/org/freedesktop/portal/desktop";
const sdbus::InterfaceName INTERFACE_NAME = sdbus::InterfaceName{"org.freedesktop.impl.portal.GlobalShortcuts"};
const sdbus::ObjectPath OBJECT_PATH = sdbus::ObjectPath{"/org/freedesktop/portal/desktop"};
};
Loading
Loading