Deskflow 1.26.0.443
Keyboard and mouse sharing utility
Loading...
Searching...
No Matches
PortalGlobalShortcuts.h
Go to the documentation of this file.
1/*
2 * Deskflow -- mouse and keyboard sharing utility
3 * SPDX-FileCopyrightText: (C) 2026 Deskflow Developers
4 * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
5 */
6
7#pragma once
8
9#include "base/IEventQueue.h"
10#include "mt/Thread.h"
11
12#include <glib.h>
13#include <libportal/globalshortcuts.h>
14#include <optional>
15#include <xkbcommon/xkbcommon.h>
16
17#include <map>
18#include <mutex>
19#include <string>
20#include <vector>
21
22#include "deskflow/KeyTypes.h"
23
24namespace deskflow {
25
26class EiScreen;
27
29{
30public:
33 struct HotKey
34 {
35 std::uint32_t id = 0;
36 KeyID key = kKeyNone;
38 };
39
40 void setHotKeys(std::vector<HotKey> hotkeys);
41
42private:
43 void glibThread(const void *);
44 void setupSession(XdpGlobalShortcutsSession *session);
45
46 void createSessionDone(GObject *object, GAsyncResult *res);
47 void bindShortcutsDone(GObject *object, GAsyncResult *res);
48
49 void handleSessionClosed(XdpSession *session);
50 void handleActivated(
51 XdpGlobalShortcutsSession *session, const char *shortcutId, const guint64 timestamp, const GVariant *options
52 );
53 void handleDeactivated(
54 XdpGlobalShortcutsSession *session, const char *shortcutId, const guint64 timestamp, const GVariant *options
55 );
56 void handleShortcutsChanged(XdpGlobalShortcutsSession *session, GPtrArray *shortcuts);
57
59 static void sessionClosed(XdpSession *session, const gpointer data)
60 {
61 static_cast<PortalGlobalShortcuts *>(data)->handleSessionClosed(session);
62 }
63
64 static void activated(
65 XdpGlobalShortcutsSession *session, const char *shortcutId, const guint64 timestamp, const GVariant *options,
66 const gpointer data
67 )
68 {
69 static_cast<PortalGlobalShortcuts *>(data)->handleActivated(session, shortcutId, timestamp, options);
70 }
71
72 static void deactivated(
73 XdpGlobalShortcutsSession *session, const char *shortcutId, const guint64 timestamp, const GVariant *options,
74 const gpointer data
75 )
76 {
77 static_cast<PortalGlobalShortcuts *>(data)->handleDeactivated(session, shortcutId, timestamp, options);
78 }
79
80 static void shortcutsChanged(XdpGlobalShortcutsSession *session, GPtrArray *shortcuts, const gpointer data)
81 {
82 static_cast<PortalGlobalShortcuts *>(data)->handleShortcutsChanged(session, shortcuts);
83 }
84
85 enum class Signal : uint8_t
86 {
87 SessionClosed,
88 Activated,
89 Deactivated,
90 ShortcutsChanged,
91 };
92
93 struct Binding
94 {
95 std::uint32_t id = 0;
96 KeyID key = kKeyNone;
97 KeyModifierMask mask = 0;
98 std::string shortcutId;
99 std::string description;
100 std::string preferredTrigger;
101 };
102
103 static std::optional<xkb_keysym_t> deskflowKeyToXkbKeysym(KeyID key);
104 static std::string keyNameForPreferredTrigger(KeyID key);
105 static std::string makePreferredTrigger(KeyID key, KeyModifierMask mask);
106 static bool isValidPreferredTriggerIdentifier(const std::string &name);
107
108 gboolean createSession();
109 void closeSession();
110 void bindShortcuts();
111
112 std::uint32_t hotKeyIdForShortcutId(const char *shortcutId) const;
113 static Binding makeBinding(const HotKey &hotkey);
114 std::vector<Binding> bindingsSnapshot() const;
115 static std::string makeShortcutId(KeyID key, KeyModifierMask mask);
116
117 EiScreen *m_screen = nullptr;
118 IEventQueue *m_events = nullptr;
119
120 Thread *m_glibThread = nullptr;
121 GMainLoop *m_glibMainLoop = nullptr;
122
123 XdpPortal *m_portal = nullptr;
124 XdpGlobalShortcutsSession *m_session = nullptr;
125 GCancellable *m_cancellable = nullptr;
126
127 std::map<Signal, gulong> m_signals = {
128 {Signal::SessionClosed, 0},
129 {Signal::Activated, 0},
130 {Signal::Deactivated, 0},
131 {Signal::ShortcutsChanged, 0},
132 };
133
134 mutable std::mutex m_bindingsMutex;
135 guint m_pendingIdleSource = 0; // guarded by m_bindingsMutex
136 // Snapshot derived from EiScreen::m_hotkeys. This is portal-facing state
137 std::vector<Binding> m_bindings;
138};
139
140} // namespace deskflow
static const struct sockaddr FAR * name
Definition ArchNetworkWinsock.cpp:29
static int void FAR * data
Definition ArchNetworkWinsock.cpp:36
uint32_t KeyID
Key ID.
Definition KeyTypes.h:21
uint32_t KeyModifierMask
Modifier key mask.
Definition KeyTypes.h:44
Event queue interface.
Definition IEventQueue.h:29
Implementation of IPlatformScreen for X11.
Definition EiScreen.h:43
Definition PortalGlobalShortcuts.h:29
void setHotKeys(std::vector< HotKey > hotkeys)
Definition PortalGlobalShortcuts.cpp:73
PortalGlobalShortcuts(EiScreen *screen, IEventQueue *events)
Definition PortalGlobalShortcuts.cpp:19
Definition DaemonApp.h:18
Definition PortalGlobalShortcuts.h:34
KeyID key
Definition PortalGlobalShortcuts.h:36
KeyModifierMask mask
Definition PortalGlobalShortcuts.h:37