2
* Copyright (C) 2012 Canonical, Ltd.
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; version 3.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
#include "gkeysequenceparser.h"
23
#include <X11/XKBlib.h>
25
bool GKeySequenceParser::parse(const QString &keySequence, int *x11KeyCode, Qt::KeyboardModifiers *modifiers)
27
// Parses a string in the form created by the gtk shortcut dialog into x11 keycode and qt modifiers
28
// The expected format is
29
// <Modifier>*Keyname?
30
// i.e. there can be none or multiple modifiers followed or not by the name of a key
33
*modifiers = Qt::NoModifier;
34
if (keySequence == "Disabled") {
38
QString aux = keySequence;
39
while (success && aux.startsWith('<')) {
40
const int closing = aux.indexOf('>');
42
const QString modifier = aux.mid(1, closing - 1);
43
if (modifier == "Control" || modifier == "Primary") {
44
*modifiers = *modifiers | Qt::ControlModifier;
45
} else if (modifier == "Shift") {
46
*modifiers = *modifiers | Qt::ShiftModifier;
47
} else if (modifier == "Alt") {
48
*modifiers = *modifiers | Qt::AltModifier;
49
} else if (modifier == "Super") {
50
*modifiers = *modifiers | Qt::MetaModifier;
52
qWarning() << "Could not parse modifier" << modifier << "in key sequence" << keySequence;
55
aux = aux.mid(closing + 1);
57
qWarning() << "Could not find modifier end in key sequence" << keySequence;
62
if (success && !aux.isEmpty()) {
63
KeySym keysym = XStringToKeysym(aux.toLatin1().constData());
64
if (keysym == NoSymbol) {
65
qWarning() << "Could not parse key" << aux << "in key sequence" << keySequence;
68
*x11KeyCode = XKeysymToKeycode(QX11Info::display(), keysym);