Changeset View
Changeset View
Standalone View
Standalone View
globalshortcuts.cpp
Show All 16 Lines | |||||
17 | You should have received a copy of the GNU General Public License | 17 | You should have received a copy of the GNU General Public License | ||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
19 | *********************************************************************/ | 19 | *********************************************************************/ | ||
20 | // own | 20 | // own | ||
21 | #include "globalshortcuts.h" | 21 | #include "globalshortcuts.h" | ||
22 | // kwin | 22 | // kwin | ||
23 | #include <config-kwin.h> | 23 | #include <config-kwin.h> | ||
24 | #include "main.h" | 24 | #include "main.h" | ||
25 | #include "gestures.h" | ||||
25 | #include "utils.h" | 26 | #include "utils.h" | ||
26 | // KDE | 27 | // KDE | ||
27 | #include <KGlobalAccel/private/kglobalacceld.h> | 28 | #include <KGlobalAccel/private/kglobalacceld.h> | ||
28 | #include <KGlobalAccel/private/kglobalaccel_interface.h> | 29 | #include <KGlobalAccel/private/kglobalaccel_interface.h> | ||
29 | // Qt | 30 | // Qt | ||
30 | #include <QAction> | 31 | #include <QAction> | ||
31 | 32 | | |||
32 | namespace KWin | 33 | namespace KWin | ||
33 | { | 34 | { | ||
34 | 35 | | |||
36 | uint qHash(SwipeDirection direction) | ||||
davidedmundson: what's this for?
You don't make a QHash of SwipeDirections anywhere, and even if you did, it… | |||||
I do: QHash<Qt::KeyboardModifiers, QHash<SwipeDirection, GlobalShortcut*> > m_swipeShortcuts; and no, it's not implicit. I only added because I got a compile error. graesslin: I do: QHash<Qt::KeyboardModifiers, QHash<SwipeDirection, GlobalShortcut*> > m_swipeShortcuts… | |||||
37 | { | ||||
38 | return uint(direction); | ||||
39 | } | ||||
40 | | ||||
35 | GlobalShortcut::GlobalShortcut(const QKeySequence &shortcut) | 41 | GlobalShortcut::GlobalShortcut(const QKeySequence &shortcut) | ||
36 | : m_shortcut(shortcut) | 42 | : m_shortcut(shortcut) | ||
37 | , m_pointerModifiers(Qt::NoModifier) | 43 | , m_pointerModifiers(Qt::NoModifier) | ||
38 | , m_pointerButtons(Qt::NoButton) | 44 | , m_pointerButtons(Qt::NoButton) | ||
39 | , m_axis(PointerAxisUp) | 45 | , m_axis(PointerAxisUp) | ||
40 | { | 46 | { | ||
41 | } | 47 | } | ||
42 | 48 | | |||
43 | GlobalShortcut::GlobalShortcut(Qt::KeyboardModifiers pointerButtonModifiers, Qt::MouseButtons pointerButtons) | 49 | GlobalShortcut::GlobalShortcut(Qt::KeyboardModifiers pointerButtonModifiers, Qt::MouseButtons pointerButtons) | ||
44 | : m_shortcut(QKeySequence()) | 50 | : m_shortcut(QKeySequence()) | ||
45 | , m_pointerModifiers(pointerButtonModifiers) | 51 | , m_pointerModifiers(pointerButtonModifiers) | ||
46 | , m_pointerButtons(pointerButtons) | 52 | , m_pointerButtons(pointerButtons) | ||
47 | , m_axis(PointerAxisUp) | 53 | , m_axis(PointerAxisUp) | ||
48 | { | 54 | { | ||
49 | } | 55 | } | ||
50 | 56 | | |||
51 | GlobalShortcut::GlobalShortcut(Qt::KeyboardModifiers modifiers, PointerAxisDirection axis) | 57 | GlobalShortcut::GlobalShortcut(Qt::KeyboardModifiers modifiers, PointerAxisDirection axis) | ||
52 | : m_shortcut(QKeySequence()) | 58 | : m_shortcut(QKeySequence()) | ||
53 | , m_pointerModifiers(modifiers) | 59 | , m_pointerModifiers(modifiers) | ||
54 | , m_pointerButtons(Qt::NoButton) | 60 | , m_pointerButtons(Qt::NoButton) | ||
55 | , m_axis(axis) | 61 | , m_axis(axis) | ||
56 | { | 62 | { | ||
57 | } | 63 | } | ||
58 | 64 | | |||
65 | GlobalShortcut::GlobalShortcut(SwipeDirection direction) | ||||
66 | : m_shortcut(QKeySequence()) | ||||
67 | , m_pointerModifiers(Qt::NoModifier) | ||||
68 | , m_pointerButtons(Qt::NoButton) | ||||
69 | , m_axis(PointerAxisUp) | ||||
70 | , m_swipeDirection(direction) | ||||
71 | { | ||||
72 | } | ||||
73 | | ||||
59 | GlobalShortcut::~GlobalShortcut() | 74 | GlobalShortcut::~GlobalShortcut() | ||
60 | { | 75 | { | ||
61 | } | 76 | } | ||
62 | 77 | | |||
63 | InternalGlobalShortcut::InternalGlobalShortcut(Qt::KeyboardModifiers modifiers, const QKeySequence &shortcut, QAction *action) | 78 | InternalGlobalShortcut::InternalGlobalShortcut(Qt::KeyboardModifiers modifiers, const QKeySequence &shortcut, QAction *action) | ||
64 | : GlobalShortcut(shortcut) | 79 | : GlobalShortcut(shortcut) | ||
65 | , m_action(action) | 80 | , m_action(action) | ||
66 | { | 81 | { | ||
67 | Q_UNUSED(modifiers) | 82 | Q_UNUSED(modifiers) | ||
68 | } | 83 | } | ||
69 | 84 | | |||
70 | InternalGlobalShortcut::InternalGlobalShortcut(Qt::KeyboardModifiers pointerButtonModifiers, Qt::MouseButtons pointerButtons, QAction *action) | 85 | InternalGlobalShortcut::InternalGlobalShortcut(Qt::KeyboardModifiers pointerButtonModifiers, Qt::MouseButtons pointerButtons, QAction *action) | ||
71 | : GlobalShortcut(pointerButtonModifiers, pointerButtons) | 86 | : GlobalShortcut(pointerButtonModifiers, pointerButtons) | ||
72 | , m_action(action) | 87 | , m_action(action) | ||
73 | { | 88 | { | ||
74 | } | 89 | } | ||
75 | 90 | | |||
76 | InternalGlobalShortcut::InternalGlobalShortcut(Qt::KeyboardModifiers axisModifiers, PointerAxisDirection axis, QAction *action) | 91 | InternalGlobalShortcut::InternalGlobalShortcut(Qt::KeyboardModifiers axisModifiers, PointerAxisDirection axis, QAction *action) | ||
77 | : GlobalShortcut(axisModifiers, axis) | 92 | : GlobalShortcut(axisModifiers, axis) | ||
78 | , m_action(action) | 93 | , m_action(action) | ||
79 | { | 94 | { | ||
80 | } | 95 | } | ||
81 | 96 | | |||
97 | static SwipeGesture::Direction toSwipeDirection(SwipeDirection direction) | ||||
98 | { | ||||
99 | switch (direction) { | ||||
100 | case SwipeDirection::Up: | ||||
101 | return SwipeGesture::Direction::Up; | ||||
102 | case SwipeDirection::Down: | ||||
103 | return SwipeGesture::Direction::Down; | ||||
104 | case SwipeDirection::Left: | ||||
105 | return SwipeGesture::Direction::Left; | ||||
106 | case SwipeDirection::Right: | ||||
107 | return SwipeGesture::Direction::Right; | ||||
108 | case SwipeDirection::Invalid: | ||||
109 | default: | ||||
110 | Q_UNREACHABLE(); | ||||
111 | } | ||||
112 | } | ||||
113 | | ||||
114 | InternalGlobalShortcut::InternalGlobalShortcut(Qt::KeyboardModifiers swipeModifier, SwipeDirection direction, QAction *action) | ||||
115 | : GlobalShortcut(direction) | ||||
116 | , m_action(action) | ||||
117 | , m_swipe(new SwipeGesture) | ||||
118 | { | ||||
119 | Q_UNUSED(swipeModifier) | ||||
120 | m_swipe->setDirection(toSwipeDirection(direction)); | ||||
121 | m_swipe->setMinimumFingerCount(4); | ||||
122 | m_swipe->setMaximumFingerCount(4); | ||||
123 | QObject::connect(m_swipe.data(), &SwipeGesture::triggered, m_action, &QAction::trigger, Qt::QueuedConnection); | ||||
124 | } | ||||
125 | | ||||
82 | InternalGlobalShortcut::~InternalGlobalShortcut() | 126 | InternalGlobalShortcut::~InternalGlobalShortcut() | ||
83 | { | 127 | { | ||
84 | } | 128 | } | ||
85 | 129 | | |||
86 | void InternalGlobalShortcut::invoke() | 130 | void InternalGlobalShortcut::invoke() | ||
87 | { | 131 | { | ||
88 | // using QueuedConnection so that we finish the even processing first | 132 | // using QueuedConnection so that we finish the even processing first | ||
89 | QMetaObject::invokeMethod(m_action, "trigger", Qt::QueuedConnection); | 133 | QMetaObject::invokeMethod(m_action, "trigger", Qt::QueuedConnection); | ||
90 | } | 134 | } | ||
91 | 135 | | |||
92 | GlobalShortcutsManager::GlobalShortcutsManager(QObject *parent) | 136 | GlobalShortcutsManager::GlobalShortcutsManager(QObject *parent) | ||
93 | : QObject(parent) | 137 | : QObject(parent) | ||
138 | , m_gestureRecognizer(new GestureRecognizer(this)) | ||||
94 | { | 139 | { | ||
95 | } | 140 | } | ||
96 | 141 | | |||
97 | template <typename T> | 142 | template <typename T> | ||
98 | void clearShortcuts(T &shortcuts) | 143 | void clearShortcuts(T &shortcuts) | ||
99 | { | 144 | { | ||
100 | for (auto it = shortcuts.begin(); it != shortcuts.end(); ++it) { | 145 | for (auto it = shortcuts.begin(); it != shortcuts.end(); ++it) { | ||
101 | qDeleteAll((*it)); | 146 | qDeleteAll((*it)); | ||
102 | } | 147 | } | ||
103 | } | 148 | } | ||
104 | 149 | | |||
105 | GlobalShortcutsManager::~GlobalShortcutsManager() | 150 | GlobalShortcutsManager::~GlobalShortcutsManager() | ||
106 | { | 151 | { | ||
107 | clearShortcuts(m_pointerShortcuts); | 152 | clearShortcuts(m_pointerShortcuts); | ||
108 | clearShortcuts(m_axisShortcuts); | 153 | clearShortcuts(m_axisShortcuts); | ||
154 | clearShortcuts(m_swipeShortcuts); | ||||
109 | } | 155 | } | ||
110 | 156 | | |||
111 | void GlobalShortcutsManager::init() | 157 | void GlobalShortcutsManager::init() | ||
112 | { | 158 | { | ||
113 | if (kwinApp()->shouldUseWaylandForCompositing()) { | 159 | if (kwinApp()->shouldUseWaylandForCompositing()) { | ||
114 | qputenv("KGLOBALACCELD_PLATFORM", QByteArrayLiteral("org.kde.kwin")); | 160 | qputenv("KGLOBALACCELD_PLATFORM", QByteArrayLiteral("org.kde.kwin")); | ||
115 | m_kglobalAccel = new KGlobalAccelD(this); | 161 | m_kglobalAccel = new KGlobalAccelD(this); | ||
116 | if (!m_kglobalAccel->init()) { | 162 | if (!m_kglobalAccel->init()) { | ||
Show All 24 Lines | 175 | for (auto it = shortcuts.begin(); it != shortcuts.end(); ++it) { | |||
141 | } | 187 | } | ||
142 | } | 188 | } | ||
143 | } | 189 | } | ||
144 | 190 | | |||
145 | void GlobalShortcutsManager::objectDeleted(QObject *object) | 191 | void GlobalShortcutsManager::objectDeleted(QObject *object) | ||
146 | { | 192 | { | ||
147 | handleDestroyedAction(object, m_pointerShortcuts); | 193 | handleDestroyedAction(object, m_pointerShortcuts); | ||
148 | handleDestroyedAction(object, m_axisShortcuts); | 194 | handleDestroyedAction(object, m_axisShortcuts); | ||
195 | handleDestroyedAction(object, m_swipeShortcuts); | ||||
149 | } | 196 | } | ||
150 | 197 | | |||
151 | template <typename T, typename R> | 198 | template <typename T, typename R> | ||
152 | void addShortcut(T &shortcuts, QAction *action, Qt::KeyboardModifiers modifiers, R value) | 199 | GlobalShortcut *addShortcut(T &shortcuts, QAction *action, Qt::KeyboardModifiers modifiers, R value) | ||
153 | { | 200 | { | ||
154 | GlobalShortcut *cut = new InternalGlobalShortcut(modifiers, value, action); | 201 | GlobalShortcut *cut = new InternalGlobalShortcut(modifiers, value, action); | ||
155 | auto it = shortcuts.find(modifiers); | 202 | auto it = shortcuts.find(modifiers); | ||
156 | if (it != shortcuts.end()) { | 203 | if (it != shortcuts.end()) { | ||
157 | // TODO: check if shortcut already exists | 204 | // TODO: check if shortcut already exists | ||
158 | (*it).insert(value, cut); | 205 | (*it).insert(value, cut); | ||
159 | } else { | 206 | } else { | ||
160 | QHash<R, GlobalShortcut*> s; | 207 | QHash<R, GlobalShortcut*> s; | ||
161 | s.insert(value, cut); | 208 | s.insert(value, cut); | ||
162 | shortcuts.insert(modifiers, s); | 209 | shortcuts.insert(modifiers, s); | ||
163 | } | 210 | } | ||
211 | return cut; | ||||
164 | } | 212 | } | ||
165 | 213 | | |||
166 | void GlobalShortcutsManager::registerPointerShortcut(QAction *action, Qt::KeyboardModifiers modifiers, Qt::MouseButtons pointerButtons) | 214 | void GlobalShortcutsManager::registerPointerShortcut(QAction *action, Qt::KeyboardModifiers modifiers, Qt::MouseButtons pointerButtons) | ||
167 | { | 215 | { | ||
168 | addShortcut(m_pointerShortcuts, action, modifiers, pointerButtons); | 216 | addShortcut(m_pointerShortcuts, action, modifiers, pointerButtons); | ||
169 | connect(action, &QAction::destroyed, this, &GlobalShortcutsManager::objectDeleted); | 217 | connect(action, &QAction::destroyed, this, &GlobalShortcutsManager::objectDeleted); | ||
170 | } | 218 | } | ||
171 | 219 | | |||
172 | void GlobalShortcutsManager::registerAxisShortcut(QAction *action, Qt::KeyboardModifiers modifiers, PointerAxisDirection axis) | 220 | void GlobalShortcutsManager::registerAxisShortcut(QAction *action, Qt::KeyboardModifiers modifiers, PointerAxisDirection axis) | ||
173 | { | 221 | { | ||
174 | addShortcut(m_axisShortcuts, action, modifiers, axis); | 222 | addShortcut(m_axisShortcuts, action, modifiers, axis); | ||
175 | connect(action, &QAction::destroyed, this, &GlobalShortcutsManager::objectDeleted); | 223 | connect(action, &QAction::destroyed, this, &GlobalShortcutsManager::objectDeleted); | ||
176 | } | 224 | } | ||
177 | 225 | | |||
226 | void GlobalShortcutsManager::registerTouchpadSwipe(QAction *action, SwipeDirection direction) | ||||
227 | { | ||||
228 | auto shortcut = addShortcut(m_swipeShortcuts, action, Qt::NoModifier, direction); | ||||
229 | connect(action, &QAction::destroyed, this, &GlobalShortcutsManager::objectDeleted); | ||||
230 | m_gestureRecognizer->registerGesture(static_cast<InternalGlobalShortcut*>(shortcut)->swipeGesture()); | ||||
231 | } | ||||
232 | | ||||
178 | template <typename T, typename U> | 233 | template <typename T, typename U> | ||
179 | bool processShortcut(Qt::KeyboardModifiers mods, T key, U &shortcuts) | 234 | bool processShortcut(Qt::KeyboardModifiers mods, T key, U &shortcuts) | ||
180 | { | 235 | { | ||
181 | auto it = shortcuts.find(mods); | 236 | auto it = shortcuts.find(mods); | ||
182 | if (it == shortcuts.end()) { | 237 | if (it == shortcuts.end()) { | ||
183 | return false; | 238 | return false; | ||
184 | } | 239 | } | ||
185 | auto it2 = (*it).find(key); | 240 | auto it2 = (*it).find(key); | ||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Line(s) | 282 | { | |||
228 | return processShortcut(mods, pointerButtons, m_pointerShortcuts); | 283 | return processShortcut(mods, pointerButtons, m_pointerShortcuts); | ||
229 | } | 284 | } | ||
230 | 285 | | |||
231 | bool GlobalShortcutsManager::processAxis(Qt::KeyboardModifiers mods, PointerAxisDirection axis) | 286 | bool GlobalShortcutsManager::processAxis(Qt::KeyboardModifiers mods, PointerAxisDirection axis) | ||
232 | { | 287 | { | ||
233 | return processShortcut(mods, axis, m_axisShortcuts); | 288 | return processShortcut(mods, axis, m_axisShortcuts); | ||
234 | } | 289 | } | ||
235 | 290 | | |||
291 | void GlobalShortcutsManager::processSwipeStart(uint fingerCount) | ||||
292 | { | ||||
293 | m_gestureRecognizer->startSwipeGesture(fingerCount); | ||||
294 | } | ||||
295 | | ||||
296 | void GlobalShortcutsManager::processSwipeUpdate(const QSizeF &delta) | ||||
297 | { | ||||
298 | m_gestureRecognizer->updateSwipeGesture(delta); | ||||
299 | } | ||||
300 | | ||||
301 | void GlobalShortcutsManager::processSwipeCancel() | ||||
302 | { | ||||
303 | m_gestureRecognizer->cancelSwipeGesture(); | ||||
304 | } | ||||
305 | | ||||
306 | void GlobalShortcutsManager::processSwipeEnd() | ||||
307 | { | ||||
308 | m_gestureRecognizer->endSwipeGesture(); | ||||
309 | // TODO: cancel on Wayland Seat if one triggered | ||||
310 | } | ||||
311 | | ||||
236 | } // namespace | 312 | } // namespace |
what's this for?
You don't make a QHash of SwipeDirections anywhere, and even if you did, it should be implicity done for enums.