Changeset View
Changeset View
Standalone View
Standalone View
globalshortcuts.cpp
Show All 18 Lines | |||||
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 "utils.h" | 25 | #include "utils.h" | ||
26 | // KDE | 26 | // KDE | ||
27 | #include <kkeyserver.h> | | |||
28 | #include <KConfigGroup> | | |||
29 | #include <KGlobalAccel/private/kglobalacceld.h> | 27 | #include <KGlobalAccel/private/kglobalacceld.h> | ||
30 | #include <KGlobalAccel/private/kglobalaccel_interface.h> | 28 | #include <KGlobalAccel/private/kglobalaccel_interface.h> | ||
31 | // Qt | 29 | // Qt | ||
32 | #include <QAction> | 30 | #include <QAction> | ||
33 | 31 | | |||
34 | namespace KWin | 32 | namespace KWin | ||
35 | { | 33 | { | ||
36 | 34 | | |||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Line(s) | |||||
88 | void InternalGlobalShortcut::invoke() | 86 | void InternalGlobalShortcut::invoke() | ||
89 | { | 87 | { | ||
90 | // using QueuedConnection so that we finish the even processing first | 88 | // using QueuedConnection so that we finish the even processing first | ||
91 | QMetaObject::invokeMethod(m_action, "trigger", Qt::QueuedConnection); | 89 | QMetaObject::invokeMethod(m_action, "trigger", Qt::QueuedConnection); | ||
92 | } | 90 | } | ||
93 | 91 | | |||
94 | GlobalShortcutsManager::GlobalShortcutsManager(QObject *parent) | 92 | GlobalShortcutsManager::GlobalShortcutsManager(QObject *parent) | ||
95 | : QObject(parent) | 93 | : QObject(parent) | ||
96 | , m_config(KSharedConfig::openConfig(QStringLiteral("kglobalshortcutsrc"), KConfig::SimpleConfig)) | | |||
97 | { | 94 | { | ||
98 | } | 95 | } | ||
99 | 96 | | |||
100 | template <typename T> | 97 | template <typename T> | ||
101 | void clearShortcuts(T &shortcuts) | 98 | void clearShortcuts(T &shortcuts) | ||
102 | { | 99 | { | ||
103 | for (auto it = shortcuts.begin(); it != shortcuts.end(); ++it) { | 100 | for (auto it = shortcuts.begin(); it != shortcuts.end(); ++it) { | ||
104 | qDeleteAll((*it)); | 101 | qDeleteAll((*it)); | ||
105 | } | 102 | } | ||
106 | } | 103 | } | ||
107 | 104 | | |||
108 | GlobalShortcutsManager::~GlobalShortcutsManager() | 105 | GlobalShortcutsManager::~GlobalShortcutsManager() | ||
109 | { | 106 | { | ||
110 | clearShortcuts(m_shortcuts); | | |||
111 | clearShortcuts(m_pointerShortcuts); | 107 | clearShortcuts(m_pointerShortcuts); | ||
112 | clearShortcuts(m_axisShortcuts); | 108 | clearShortcuts(m_axisShortcuts); | ||
113 | } | 109 | } | ||
114 | 110 | | |||
115 | void GlobalShortcutsManager::init() | 111 | void GlobalShortcutsManager::init() | ||
116 | { | 112 | { | ||
117 | if (kwinApp()->shouldUseWaylandForCompositing()) { | 113 | if (kwinApp()->shouldUseWaylandForCompositing()) { | ||
118 | qputenv("KGLOBALACCELD_PLATFORM", QByteArrayLiteral("org.kde.kwin")); | 114 | qputenv("KGLOBALACCELD_PLATFORM", QByteArrayLiteral("org.kde.kwin")); | ||
Show All 24 Lines | 132 | while (it2 != list.end()) { | |||
143 | } | 139 | } | ||
144 | ++it2; | 140 | ++it2; | ||
145 | } | 141 | } | ||
146 | } | 142 | } | ||
147 | } | 143 | } | ||
148 | 144 | | |||
149 | void GlobalShortcutsManager::objectDeleted(QObject *object) | 145 | void GlobalShortcutsManager::objectDeleted(QObject *object) | ||
150 | { | 146 | { | ||
151 | handleDestroyedAction(object, m_shortcuts); | | |||
152 | handleDestroyedAction(object, m_pointerShortcuts); | 147 | handleDestroyedAction(object, m_pointerShortcuts); | ||
153 | handleDestroyedAction(object, m_axisShortcuts); | 148 | handleDestroyedAction(object, m_axisShortcuts); | ||
154 | } | 149 | } | ||
155 | 150 | | |||
156 | template <typename T, typename R> | 151 | template <typename T, typename R> | ||
157 | void addShortcut(T &shortcuts, QAction *action, Qt::KeyboardModifiers modifiers, R value) | 152 | void addShortcut(T &shortcuts, QAction *action, Qt::KeyboardModifiers modifiers, R value) | ||
158 | { | 153 | { | ||
159 | GlobalShortcut *cut = new InternalGlobalShortcut(modifiers, value, action); | 154 | GlobalShortcut *cut = new InternalGlobalShortcut(modifiers, value, action); | ||
160 | auto it = shortcuts.find(modifiers); | 155 | auto it = shortcuts.find(modifiers); | ||
161 | if (it != shortcuts.end()) { | 156 | if (it != shortcuts.end()) { | ||
162 | // TODO: check if shortcut already exists | 157 | // TODO: check if shortcut already exists | ||
163 | (*it).insert(value, cut); | 158 | (*it).insert(value, cut); | ||
164 | } else { | 159 | } else { | ||
165 | QHash<R, GlobalShortcut*> s; | 160 | QHash<R, GlobalShortcut*> s; | ||
166 | s.insert(value, cut); | 161 | s.insert(value, cut); | ||
167 | shortcuts.insert(modifiers, s); | 162 | shortcuts.insert(modifiers, s); | ||
168 | } | 163 | } | ||
169 | } | 164 | } | ||
170 | 165 | | |||
171 | void GlobalShortcutsManager::registerShortcut(QAction *action, const QKeySequence &shortcut) | | |||
172 | { | | |||
173 | QKeySequence s = getShortcutForAction(KWIN_NAME, action->objectName(), shortcut); | | |||
romangg: `getShortcutForAction` was only called here, so you could remove this function aswell? | |||||
174 | if (s.isEmpty()) { | | |||
175 | // TODO: insert into a list of empty shortcuts to react on changes | | |||
176 | return; | | |||
177 | } | | |||
178 | int keys = s[0]; | | |||
179 | Qt::KeyboardModifiers mods = Qt::NoModifier; | | |||
180 | if (keys & Qt::ShiftModifier) { | | |||
181 | mods |= Qt::ShiftModifier; | | |||
182 | } | | |||
183 | if (keys & Qt::ControlModifier) { | | |||
184 | mods |= Qt::ControlModifier; | | |||
185 | } | | |||
186 | if (keys & Qt::AltModifier) { | | |||
187 | mods |= Qt::AltModifier; | | |||
188 | } | | |||
189 | if (keys & Qt::MetaModifier) { | | |||
190 | mods |= Qt::MetaModifier; | | |||
191 | } | | |||
192 | int keysym = 0; | | |||
193 | if (!KKeyServer::keyQtToSymX(keys, &keysym)) { | | |||
194 | return; | | |||
195 | } | | |||
196 | addShortcut(m_shortcuts, action, mods, static_cast<uint32_t>(keysym)); | | |||
197 | connect(action, &QAction::destroyed, this, &GlobalShortcutsManager::objectDeleted); | | |||
198 | } | | |||
199 | | ||||
200 | void GlobalShortcutsManager::registerPointerShortcut(QAction *action, Qt::KeyboardModifiers modifiers, Qt::MouseButtons pointerButtons) | 166 | void GlobalShortcutsManager::registerPointerShortcut(QAction *action, Qt::KeyboardModifiers modifiers, Qt::MouseButtons pointerButtons) | ||
201 | { | 167 | { | ||
202 | addShortcut(m_pointerShortcuts, action, modifiers, pointerButtons); | 168 | addShortcut(m_pointerShortcuts, action, modifiers, pointerButtons); | ||
203 | connect(action, &QAction::destroyed, this, &GlobalShortcutsManager::objectDeleted); | 169 | connect(action, &QAction::destroyed, this, &GlobalShortcutsManager::objectDeleted); | ||
204 | } | 170 | } | ||
205 | 171 | | |||
206 | void GlobalShortcutsManager::registerAxisShortcut(QAction *action, Qt::KeyboardModifiers modifiers, PointerAxisDirection axis) | 172 | void GlobalShortcutsManager::registerAxisShortcut(QAction *action, Qt::KeyboardModifiers modifiers, PointerAxisDirection axis) | ||
207 | { | 173 | { | ||
208 | addShortcut(m_axisShortcuts, action, modifiers, axis); | 174 | addShortcut(m_axisShortcuts, action, modifiers, axis); | ||
209 | connect(action, &QAction::destroyed, this, &GlobalShortcutsManager::objectDeleted); | 175 | connect(action, &QAction::destroyed, this, &GlobalShortcutsManager::objectDeleted); | ||
210 | } | 176 | } | ||
211 | 177 | | |||
212 | QKeySequence GlobalShortcutsManager::getShortcutForAction(const QString &componentName, const QString &actionName, const QKeySequence &defaultShortcut) | | |||
213 | { | | |||
214 | if (!m_config->hasGroup(componentName)) { | | |||
215 | return defaultShortcut; | | |||
216 | } | | |||
217 | KConfigGroup group = m_config->group(componentName); | | |||
218 | if (!group.hasKey(actionName)) { | | |||
219 | return defaultShortcut; | | |||
220 | } | | |||
221 | QStringList parts = group.readEntry(actionName, QStringList()); | | |||
222 | // must consist of three parts | | |||
223 | if (parts.size() != 3) { | | |||
224 | return defaultShortcut; | | |||
225 | } | | |||
226 | if (parts.first() == "none") { | | |||
227 | return defaultShortcut; | | |||
228 | } | | |||
229 | return QKeySequence(parts.first()); | | |||
230 | } | | |||
231 | | ||||
232 | template <typename T, typename U> | 178 | template <typename T, typename U> | ||
233 | bool processShortcut(Qt::KeyboardModifiers mods, T key, U &shortcuts) | 179 | bool processShortcut(Qt::KeyboardModifiers mods, T key, U &shortcuts) | ||
234 | { | 180 | { | ||
235 | auto it = shortcuts.find(mods); | 181 | auto it = shortcuts.find(mods); | ||
236 | if (it == shortcuts.end()) { | 182 | if (it == shortcuts.end()) { | ||
237 | return false; | 183 | return false; | ||
238 | } | 184 | } | ||
239 | auto it2 = (*it).find(key); | 185 | auto it2 = (*it).find(key); | ||
Show All 29 Lines | 207 | } else if (keyQt == Qt::Key_Backtab) { | |||
269 | if (check(mods | Qt::ShiftModifier, keyQt)) { | 215 | if (check(mods | Qt::ShiftModifier, keyQt)) { | ||
270 | return true; | 216 | return true; | ||
271 | } | 217 | } | ||
272 | if (check(mods | Qt::ShiftModifier, Qt::Key_Tab)) { | 218 | if (check(mods | Qt::ShiftModifier, Qt::Key_Tab)) { | ||
273 | return true; | 219 | return true; | ||
274 | } | 220 | } | ||
275 | } | 221 | } | ||
276 | } | 222 | } | ||
277 | if (processShortcut(mods, key, m_shortcuts)) { | | |||
278 | return true; | | |||
279 | } | | |||
280 | return false; | 223 | return false; | ||
281 | } | 224 | } | ||
282 | 225 | | |||
283 | bool GlobalShortcutsManager::processPointerPressed(Qt::KeyboardModifiers mods, Qt::MouseButtons pointerButtons) | 226 | bool GlobalShortcutsManager::processPointerPressed(Qt::KeyboardModifiers mods, Qt::MouseButtons pointerButtons) | ||
284 | { | 227 | { | ||
285 | return processShortcut(mods, pointerButtons, m_pointerShortcuts); | 228 | return processShortcut(mods, pointerButtons, m_pointerShortcuts); | ||
286 | } | 229 | } | ||
287 | 230 | | |||
288 | bool GlobalShortcutsManager::processAxis(Qt::KeyboardModifiers mods, PointerAxisDirection axis) | 231 | bool GlobalShortcutsManager::processAxis(Qt::KeyboardModifiers mods, PointerAxisDirection axis) | ||
289 | { | 232 | { | ||
290 | return processShortcut(mods, axis, m_axisShortcuts); | 233 | return processShortcut(mods, axis, m_axisShortcuts); | ||
291 | } | 234 | } | ||
292 | 235 | | |||
293 | } // namespace | 236 | } // namespace |
getShortcutForAction was only called here, so you could remove this function aswell?