~josephjamesmills/u2t/trunk

« back to all changes in this revision

Viewing changes to libunity-2d-private/src/gkeysequenceparser.cpp

  • Committer: Michał Sawicz
  • Date: 2012-04-13 11:49:14 UTC
  • Revision ID: michal.sawicz@canonical.com-20120413114914-de64smwjwsq50zu1
Merge lp:unity-2d r1030

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2012 Canonical, Ltd.
 
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; version 3.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 */
 
16
 
 
17
#include "gkeysequenceparser.h"
 
18
 
 
19
#include <QDebug>
 
20
#include <QString>
 
21
#include <QX11Info>
 
22
 
 
23
#include <X11/XKBlib.h>
 
24
 
 
25
bool GKeySequenceParser::parse(const QString &keySequence, int *x11KeyCode, Qt::KeyboardModifiers *modifiers)
 
26
{
 
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
 
31
    bool success = true;
 
32
    *x11KeyCode = 0;
 
33
    *modifiers = Qt::NoModifier;
 
34
    if (keySequence == "Disabled") {
 
35
        return success;
 
36
    }
 
37
 
 
38
    QString aux = keySequence;
 
39
    while (success && aux.startsWith('<')) {
 
40
        const int closing = aux.indexOf('>');
 
41
        if (closing > 0) {
 
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;
 
51
            } else {
 
52
                qWarning() << "Could not parse modifier" << modifier << "in key sequence" << keySequence;
 
53
                success = false;
 
54
            }
 
55
            aux = aux.mid(closing + 1);
 
56
        } else {
 
57
            qWarning() << "Could not find modifier end in key sequence" << keySequence;
 
58
            success = false;
 
59
        }
 
60
    }
 
61
 
 
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;
 
66
            success = false;
 
67
        } else {
 
68
            *x11KeyCode = XKeysymToKeycode(QX11Info::display(), keysym);
 
69
        }
 
70
    }
 
71
 
 
72
    return success;
 
73
}