-
Notifications
You must be signed in to change notification settings - Fork 0
/
wayland_shortcut.cpp
191 lines (151 loc) · 6.24 KB
/
wayland_shortcut.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "wayland_shortcut.h"
#include <private/qgenericunixservices_p.h>
#include <private/qguiapplication_p.h>
#include <qpa/qplatformintegration.h>
#include <QtDBus/QtDBus>
wayland_shortcut::
wayland_shortcut(QWindow* w)
{
protol_window_handler_id = obtain_protol_window_handler(w);
QDBusMessage create_session =
QDBusMessage::createMethodCall("org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
"org.freedesktop.portal.GlobalShortcuts",
"CreateSession");
QList<QVariant> args_create_session;
QMap<QString, QVariant> options_create_session;
options_create_session.insert("handle_token", handle_token);
options_create_session.insert("session_handle_token", session_handle_token);
args_create_session.append(options_create_session);
create_session.setArguments(args_create_session);
QDBusMessage c = QDBusConnection::sessionBus().call(create_session);
this->response_handle = c.arguments().first().value<QDBusObjectPath>();
qDebug() << "Return from create session ->" << c;
QDBusConnection::sessionBus().connect(
"org.freedesktop.portal.Desktop",
response_handle.path(),
QLatin1String("org.freedesktop.portal.Request"),
QLatin1String("Response"),
this,
SLOT(process_create_session_response(uint, QVariantMap)));
}
void
wayland_shortcut::list_shortcuts()
{
QDBusMessage list_shortcut =
QDBusMessage::createMethodCall("org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
"org.freedesktop.portal.GlobalShortcuts",
"ListShortcuts");
QList<QVariant> args_list_shotcuts;
args_list_shotcuts.append(QVariant::fromValue(session_obj_path));
QMap<QString, QVariant> options_list_shortcut;
options_list_shortcut.insert("handle_token", handle_token);
args_list_shotcuts.append(options_list_shortcut);
list_shortcut.setArguments(args_list_shotcuts);
QDBusMessage list_ret = QDBusConnection::sessionBus().call(list_shortcut);
qDebug() << "list_shortcuts message return ->" << list_ret;
QDBusConnection::sessionBus().connect(
"org.freedesktop.portal.Desktop",
list_ret.arguments().first().value<QDBusObjectPath>().path(),
QLatin1String("org.freedesktop.portal.Request"),
QLatin1String("Response"),
this,
SLOT(process_list_shortcuts_response(uint, QVariantMap)));
}
QString
wayland_shortcut::obtain_protol_window_handler(QWindow* w)
{
// TODO: Private API could be gone
if (const auto services = dynamic_cast<QGenericUnixServices*>(
QGuiApplicationPrivate::platformIntegration()->services())) {
return services->portalWindowIdentifier(w).toUtf8().constData();
}
}
void
wayland_shortcut::process_create_session_response(uint i,
const QVariantMap& results)
{
qDebug() << "result ->" << results;
if (results.contains("session_handle")) {
QString session_handle = results["session_handle"].toString();
this->session_obj_path = QDBusObjectPath(session_handle);
};
QDBusConnection::sessionBus().disconnect(
"org.freedesktop.portal.Desktop",
response_handle.path(),
QLatin1String("org.freedesktop.portal.Request"),
QLatin1String("Response"),
this,
SLOT(process_create_session_response(uint, QVariantMap)));
QDBusMessage bind_shortcut =
QDBusMessage::createMethodCall("org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
"org.freedesktop.portal.GlobalShortcuts",
"BindShortcuts");
QList<QVariant> bind_shortcut_args;
bind_shortcut_args.append(session_obj_path);
Shortcuts shortcuts;
qDBusRegisterMetaType<std::pair<QString, QVariantMap>>();
qDBusRegisterMetaType<Shortcuts>();
Q_ASSERT(QLatin1String(QDBusMetaType::typeToSignature(QMetaType(
qMetaTypeId<Shortcuts>()))) == QLatin1String("a(sa{sv})"));
QPair<QString, QVariantMap> a_shortcut;
QVariantMap a_shortcut_options;
a_shortcut.first = "nice";
a_shortcut_options.insert("description", "do sth");
a_shortcut_options.insert("preferred_trigger", "CTRL+m");
a_shortcut.second = a_shortcut_options;
QPair<QString, QVariantMap> b_shortcut;
QVariantMap b_shortcut_options;
b_shortcut.first = "nice 2";
b_shortcut_options.insert("description", "do sth 2");
b_shortcut_options.insert("preferred_trigger", "CTRL+n");
b_shortcut.second = b_shortcut_options;
shortcuts.append(a_shortcut);
shortcuts.append(b_shortcut);
bind_shortcut_args.append(QVariant::fromValue(shortcuts));
bind_shortcut_args.append(
protol_window_handler_id); // Wayland window id, could be empty for now??
// (useless so far)
QMap<QString, QVariant> bind_opts;
bind_opts.insert("handle_token", handle_token);
bind_shortcut_args.append(bind_opts);
bind_shortcut.setArguments(bind_shortcut_args);
qDebug() << "input of bind->" << bind_shortcut.arguments();
QDBusMessage bind_ret = QDBusConnection::sessionBus().call(bind_shortcut);
qDebug() << "bind message return->" << bind_ret;
// process signals
QDBusConnection::sessionBus().connect(
"org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
"org.freedesktop.portal.GlobalShortcuts",
"Activated",
this,
SLOT(process_activated_signal(
QDBusObjectPath, QString, qulonglong, QVariantMap)));
this->list_shortcuts();
}
void
wayland_shortcut::process_list_shortcuts_response(uint,
const QVariantMap& results)
{
const auto arg = results["shortcuts"].value<QDBusArgument>();
Shortcuts s;
arg >> s;
for (auto it = s.cbegin(), itEnd = s.cend(); it != itEnd; ++it) {
qDebug() << "Registered shortcuts: " << it->first
<< it->second["description"].toString()
<< it->second["trigger_description"].toString();
}
}
void
wayland_shortcut::process_activated_signal(
const QDBusObjectPath& session_handle,
const QString& shortcut_id,
qulonglong timestamp,
const QVariantMap& options)
{
qDebug() << "Got Signal ->" << session_handle.path() << shortcut_id
<< timestamp << options;
}