Debian Codegraph @

src/gui/globalHotKeys/KeySequenceToX11KeyCodeTranslator.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2019 Damir Porobic <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "KeySequenceToX11KeyCodeTranslator.h"
21
22 // Can't include <QtGlobal> for QT_VERSION_CHECK because it includes too much,
23 // and symbols conflict with X11. Can't include <QtVersionChecks> because it
24 // doesn't exist in Qt 5.
25 #include "BuildConfig.h"
26 #if KSNIP_QT6
27 #include <QtGui/private/qtx11extras_p.h>
28 #else
29 #include <QX11Info>
30 #endif
31
32 #include <X11/Xutil.h>
33
34 KeySequenceToX11KeyCodeTranslator::KeySequenceToX11KeyCodeTranslator()
35 {
36 mHotKeyMap = HotKeyMap::instance();
37 }
38
39 KeyCodeCombo KeySequenceToX11KeyCodeTranslator::map(const QKeySequence &keySequence) const
40 {
41 auto sequenceString = keySequence.toString().toUpper();
42 auto modifierString = sequenceString.section(QLatin1String("+"), 0, -2);
43 auto keyString = sequenceString.section(QLatin1String("+"), -1, -1);
44
45 auto modifier = getModifier(modifierString);
46 auto key = getKey(keyString);
47
48 return KeyCodeCombo(modifier, key);
49 }
50
51 unsigned int KeySequenceToX11KeyCodeTranslator::getModifier(const QString &modifierString) const
52 {
53 unsigned int modifier = 0;
54 if (modifierString.contains(QLatin1String("SHIFT"))) {
55 modifier |= ShiftMask;
56 }
57 if (modifierString.contains(QLatin1String("ALT"))) {
58 modifier |= Mod1Mask;
59 }
60 if (modifierString.contains(QLatin1String("CTRL"))) {
61 modifier |= ControlMask;
62 }
63 if (modifierString.contains(QLatin1String("META"))) {
64 modifier |= Mod4Mask;
65 }
66
67 return modifier;
68 }
69
70 unsigned int KeySequenceToX11KeyCodeTranslator::getKey(const QString &keyString) const
71 {
72 auto display = QX11Info::display();
73 auto keyCode = getKeyCode(keyString);
74 return XKeysymToKeycode(display, keyCode);
75 }
76
77 unsigned int KeySequenceToX11KeyCodeTranslator::getKeyCode(const QString &keyString) const
78 {
79 auto key = mHotKeyMap->getKeyForString(keyString);
80 switch (key) {
81 case Qt::Key_F1:
82 return XK_F1;
83 case Qt::Key_F2:
84 return XK_F2;
85 case Qt::Key_F3:
86 return XK_F3;
87 case Qt::Key_F4:
88 return XK_F4;
89 case Qt::Key_F5:
90 return XK_F5;
91 case Qt::Key_F6:
92 return XK_F6;
93 case Qt::Key_F7:
94 return XK_F7;
95 case Qt::Key_F8:
96 return XK_F8;
97 case Qt::Key_F9:
98 return XK_F9;
99 case Qt::Key_F10:
100 return XK_F10;
101 case Qt::Key_F11:
102 return XK_F11;
103 case Qt::Key_F12:
104 return XK_F12;
105 case Qt::Key_Delete:
106 return XK_Delete;
107 case Qt::Key_Insert:
108 return XK_Insert;
109 case Qt::Key_Print:
110 return XK_Print;
111 case Qt::Key_Left:
112 return XK_Left;
113 case Qt::Key_Up:
114 return XK_Up;
115 case Qt::Key_Right:
116 return XK_Right;
117 case Qt::Key_Down:
118 return XK_Down;
119 case Qt::Key_PageDown:
120 return XK_Page_Down;
121 case Qt::Key_PageUp:
122 return XK_Page_Up;
123 default:
124 return key;
125 }
126 }