Skip to content

Commit 09801ed

Browse files
committed
core: move to sdbus-cpp2
1 parent fb9c8d6 commit 09801ed

File tree

12 files changed

+146
-250
lines changed

12 files changed

+146
-250
lines changed

src/core/PortalManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ void CPortalManager::init() {
201201
m_iPID = getpid();
202202

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

369369
if (pollfds[0].revents & POLLIN /* dbus */) {
370-
while (m_pConnection->processPendingRequest()) {
370+
while (m_pConnection->processPendingEvent()) {
371371
;
372372
}
373373
}

src/core/PortalManager.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "wlr-screencopy-unstable-v1.hpp"
2121

2222
#include "../includes.hpp"
23+
#include "../dbusDefines.hpp"
2324

2425
#include <mutex>
2526

src/dbusDefines.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
3+
#include <sdbus-c++/sdbus-c++.h>
4+
5+
typedef sdbus::Struct<uint32_t, std::unordered_map<std::string, sdbus::Variant>> dbUasv;

src/helpers/MiscFunctions.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,3 @@ bool inShellPath(const std::string& exec) {
5858

5959
return std::ranges::any_of(paths, [&exec](std::string& path) { return access((path + "/" + exec).c_str(), X_OK) == 0; });
6060
}
61-
62-
void sendEmptyDbusMethodReply(sdbus::MethodCall& call, u_int32_t responseCode) {
63-
auto reply = call.createReply();
64-
reply << (uint32_t)responseCode;
65-
reply.send();
66-
}

src/portals/GlobalShortcuts.cpp

Lines changed: 33 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,8 @@ SKeybind* CGlobalShortcutsPortal::registerShortcut(SSession* session, const DBus
6565
return PSHORTCUT;
6666
}
6767

68-
void CGlobalShortcutsPortal::onCreateSession(sdbus::MethodCall& call) {
69-
sdbus::ObjectPath requestHandle, sessionHandle;
70-
71-
call >> requestHandle;
72-
call >> sessionHandle;
73-
74-
std::string appID;
75-
call >> appID;
76-
68+
dbUasv CGlobalShortcutsPortal::onCreateSession(sdbus::ObjectPath requestHandle, sdbus::ObjectPath sessionHandle, std::string appID,
69+
std::unordered_map<std::string, sdbus::Variant> opts) {
7770
Debug::log(LOG, "[globalshortcuts] New session:");
7871
Debug::log(LOG, "[globalshortcuts] | {}", requestHandle.c_str());
7972
Debug::log(LOG, "[globalshortcuts] | {}", sessionHandle.c_str());
@@ -87,9 +80,6 @@ void CGlobalShortcutsPortal::onCreateSession(sdbus::MethodCall& call) {
8780
PSESSION->request = createDBusRequest(requestHandle);
8881
PSESSION->request->onDestroy = [PSESSION]() { PSESSION->request.release(); };
8982

90-
std::unordered_map<std::string, sdbus::Variant> opts;
91-
call >> opts;
92-
9383
for (auto& [k, v] : opts) {
9484
if (k == "shortcuts") {
9585
PSESSION->registered = true;
@@ -104,101 +94,86 @@ void CGlobalShortcutsPortal::onCreateSession(sdbus::MethodCall& call) {
10494
}
10595
}
10696

107-
auto reply = call.createReply();
108-
reply << (uint32_t)0;
109-
reply << std::unordered_map<std::string, sdbus::Variant>{};
110-
reply.send();
97+
return {0, {}};
11198
}
11299

113-
void CGlobalShortcutsPortal::onBindShortcuts(sdbus::MethodCall& call) {
114-
sdbus::ObjectPath sessionHandle, requestHandle;
115-
call >> requestHandle;
116-
call >> sessionHandle;
117-
100+
dbUasv CGlobalShortcutsPortal::onBindShortcuts(sdbus::ObjectPath requestHandle, sdbus::ObjectPath sessionHandle, std::vector<DBusShortcut> shortcuts, std::string appID,
101+
std::unordered_map<std::string, sdbus::Variant> opts) {
118102
Debug::log(LOG, "[globalshortcuts] Bind keys:");
119103
Debug::log(LOG, "[globalshortcuts] | {}", sessionHandle.c_str());
120104

121-
std::vector<DBusShortcut> shortcuts;
122-
std::vector<DBusShortcut> shortcutsToReturn;
123-
call >> shortcuts;
124-
125105
const auto PSESSION = getSession(sessionHandle);
126106

127107
if (!PSESSION) {
128108
Debug::log(ERR, "[globalshortcuts] No session?");
129-
return;
109+
return {1, {}};
130110
}
131111

112+
std::vector<DBusShortcut> shortcutsToReturn;
113+
132114
PSESSION->registered = true;
133115

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

137119
std::unordered_map<std::string, sdbus::Variant> shortcutData;
138-
shortcutData["description"] = PSHORTCUT->description;
139-
shortcutData["trigger_description"] = "";
120+
shortcutData["description"] = sdbus::Variant{PSHORTCUT->description};
121+
shortcutData["trigger_description"] = sdbus::Variant{""};
140122
shortcutsToReturn.push_back({PSHORTCUT->id, shortcutData});
141123
}
142124

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

145-
auto reply = call.createReply();
146-
147127
std::unordered_map<std::string, sdbus::Variant> data;
148-
data["shortcuts"] = shortcutsToReturn;
128+
data["shortcuts"] = sdbus::Variant{shortcutsToReturn};
149129

150-
reply << (uint32_t)0;
151-
reply << data;
152-
reply.send();
130+
return {0, data};
153131
}
154132

155-
void CGlobalShortcutsPortal::onListShortcuts(sdbus::MethodCall& call) {
156-
sdbus::ObjectPath sessionHandle, requestHandle;
157-
call >> requestHandle;
158-
call >> sessionHandle;
159-
133+
dbUasv CGlobalShortcutsPortal::onListShortcuts(sdbus::ObjectPath sessionHandle, sdbus::ObjectPath requestHandle) {
160134
Debug::log(LOG, "[globalshortcuts] List keys:");
161135
Debug::log(LOG, "[globalshortcuts] | {}", sessionHandle.c_str());
162136

163137
const auto PSESSION = getSession(sessionHandle);
164138

165139
if (!PSESSION) {
166140
Debug::log(ERR, "[globalshortcuts] No session?");
167-
return;
141+
return {1, {}};
168142
}
169143

170144
std::vector<DBusShortcut> shortcuts;
171145

172146
for (auto& s : PSESSION->keybinds) {
173147
std::unordered_map<std::string, sdbus::Variant> opts;
174-
opts["description"] = s->description;
175-
opts["trigger_description"] = "";
148+
opts["description"] = sdbus::Variant{s->description};
149+
opts["trigger_description"] = sdbus::Variant{""};
176150
shortcuts.push_back({s->id, opts});
177151
}
178152

179-
auto reply = call.createReply();
180-
181153
std::unordered_map<std::string, sdbus::Variant> data;
182-
data["shortcuts"] = shortcuts;
154+
data["shortcuts"] = sdbus::Variant{shortcuts};
183155

184-
reply << (uint32_t)0;
185-
reply << data;
186-
reply.send();
156+
return {0, data};
187157
}
188158

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

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

194-
m_pObject->registerMethod(INTERFACE_NAME, "CreateSession", "oosa{sv}", "ua{sv}", [&](sdbus::MethodCall c) { onCreateSession(c); });
195-
m_pObject->registerMethod(INTERFACE_NAME, "BindShortcuts", "ooa(sa{sv})sa{sv}", "ua{sv}", [&](sdbus::MethodCall c) { onBindShortcuts(c); });
196-
m_pObject->registerMethod(INTERFACE_NAME, "ListShortcuts", "oo", "ua{sv}", [&](sdbus::MethodCall c) { onListShortcuts(c); });
197-
m_pObject->registerSignal(INTERFACE_NAME, "Activated", "osta{sv}");
198-
m_pObject->registerSignal(INTERFACE_NAME, "Deactivated", "osta{sv}");
199-
m_pObject->registerSignal(INTERFACE_NAME, "ShortcutsChanged", "oa(sa{sv})");
200-
201-
m_pObject->finishRegistration();
164+
m_pObject
165+
->addVTable(sdbus::registerMethod("CreateSession")
166+
.implementedAs([this](sdbus::ObjectPath o1, sdbus::ObjectPath o2, std::string s, std::unordered_map<std::string, sdbus::Variant> m) {
167+
return onCreateSession(o1, o2, s, m);
168+
}),
169+
sdbus::registerMethod("BindShortcuts")
170+
.implementedAs([this](sdbus::ObjectPath o1, sdbus::ObjectPath o2, std::vector<DBusShortcut> v1, std::string s1,
171+
std::unordered_map<std::string, sdbus::Variant> m2) { return onBindShortcuts(o1, o2, v1, s1, m2); }),
172+
sdbus::registerMethod("ListShortcuts").implementedAs([this](sdbus::ObjectPath o1, sdbus::ObjectPath o2) { return onListShortcuts(o1, o2); }),
173+
sdbus::registerSignal("Activated").withParameters<sdbus::ObjectPath, std::string, uint64_t, std::unordered_map<std::string, sdbus::Variant>>(),
174+
sdbus::registerSignal("Deactivated").withParameters<sdbus::ObjectPath, std::string, uint64_t, std::unordered_map<std::string, sdbus::Variant>>(),
175+
sdbus::registerSignal("ShortcutsChanged").withParameters<sdbus::ObjectPath, std::unordered_map<std::string, std::unordered_map<std::string, sdbus::Variant>>>())
176+
.forInterface(INTERFACE_NAME);
202177

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

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

211-
auto signal = m_pObject->createSignal(INTERFACE_NAME, "Activated");
212-
signal << PSESSION->sessionHandle;
213-
signal << pKeybind->id;
214-
signal << time;
215-
signal << std::unordered_map<std::string, sdbus::Variant>{};
216-
217-
m_pObject->emitSignal(signal);
186+
m_pObject->emitSignal("Activated").onInterface(INTERFACE_NAME).withArguments(PSESSION->sessionHandle, pKeybind->id, time, std::unordered_map<std::string, sdbus::Variant>{});
218187
}
219188

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

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

225-
auto signal = m_pObject->createSignal(INTERFACE_NAME, "Deactivated");
226-
signal << PSESSION->sessionHandle;
227-
signal << pKeybind->id;
228-
signal << time;
229-
signal << std::unordered_map<std::string, sdbus::Variant>{};
230-
231-
m_pObject->emitSignal(signal);
194+
m_pObject->emitSignal("Deactivated").onInterface(INTERFACE_NAME).withArguments(PSESSION->sessionHandle, pKeybind->id, time, std::unordered_map<std::string, sdbus::Variant>{});
232195
}

src/portals/GlobalShortcuts.hpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <sdbus-c++/sdbus-c++.h>
44
#include "hyprland-global-shortcuts-v1.hpp"
55
#include "../shared/Session.hpp"
6+
#include "../dbusDefines.hpp"
67

78
struct SKeybind {
89
SKeybind(SP<CCHyprlandGlobalShortcutV1> shortcut);
@@ -15,12 +16,15 @@ class CGlobalShortcutsPortal {
1516
public:
1617
CGlobalShortcutsPortal(SP<CCHyprlandGlobalShortcutsManagerV1> mgr);
1718

18-
void onCreateSession(sdbus::MethodCall& call);
19-
void onBindShortcuts(sdbus::MethodCall& call);
20-
void onListShortcuts(sdbus::MethodCall& call);
19+
using DBusShortcut = sdbus::Struct<std::string, std::unordered_map<std::string, sdbus::Variant>>;
20+
21+
dbUasv onCreateSession(sdbus::ObjectPath requestHandle, sdbus::ObjectPath sessionHandle, std::string appID, std::unordered_map<std::string, sdbus::Variant> opts);
22+
dbUasv onBindShortcuts(sdbus::ObjectPath requestHandle, sdbus::ObjectPath sessionHandle, std::vector<DBusShortcut> shortcuts, std::string appID,
23+
std::unordered_map<std::string, sdbus::Variant> opts);
24+
dbUasv onListShortcuts(sdbus::ObjectPath sessionHandle, sdbus::ObjectPath requestHandle);
2125

22-
void onActivated(SKeybind* pKeybind, uint64_t time);
23-
void onDeactivated(SKeybind* pKeybind, uint64_t time);
26+
void onActivated(SKeybind* pKeybind, uint64_t time);
27+
void onDeactivated(SKeybind* pKeybind, uint64_t time);
2428

2529
struct SSession {
2630
std::string appid;
@@ -42,12 +46,10 @@ class CGlobalShortcutsPortal {
4246

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

45-
using DBusShortcut = sdbus::Struct<std::string, std::unordered_map<std::string, sdbus::Variant>>;
46-
47-
SSession* getSession(sdbus::ObjectPath& path);
48-
SKeybind* getShortcutById(const std::string& appID, const std::string& shortcutId);
49-
SKeybind* registerShortcut(SSession* session, const DBusShortcut& shortcut);
49+
SSession* getSession(sdbus::ObjectPath& path);
50+
SKeybind* getShortcutById(const std::string& appID, const std::string& shortcutId);
51+
SKeybind* registerShortcut(SSession* session, const DBusShortcut& shortcut);
5052

51-
const std::string INTERFACE_NAME = "org.freedesktop.impl.portal.GlobalShortcuts";
52-
const std::string OBJECT_PATH = "/org/freedesktop/portal/desktop";
53+
const sdbus::InterfaceName INTERFACE_NAME = sdbus::InterfaceName{"org.freedesktop.impl.portal.GlobalShortcuts"};
54+
const sdbus::ObjectPath OBJECT_PATH = sdbus::ObjectPath{"/org/freedesktop/portal/desktop"};
5355
};

0 commit comments

Comments
 (0)