KeyboardTranslator.cpp 23.1 KB
Newer Older
1 2 3
/*
    This source file is part of Konsole, a terminal emulator.

4
    Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA.
*/

22 23 24
// Own
#include "KeyboardTranslator.h"

25 26
#include "konsoledebug.h"

27
// System
28
#include <ctype.h>
29 30 31
#include <stdio.h>

// Qt
32
#include <QtCore/QBuffer>
Dirk Mueller's avatar
Dirk Mueller committed
33
#include <QtCore/QTextStream>
34
#include <QtCore/QRegularExpression>
35
#include <QtGui/QKeySequence>
36 37

// KDE
38
#include <KLocalizedString>
39

40 41
using namespace Konsole;

42
KeyboardTranslatorWriter::KeyboardTranslatorWriter(QIODevice* destination)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
43
    : _destination(destination)
44
    , _writer(0)
45
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
46
    Q_ASSERT(destination && destination->isWritable());
47 48 49 50 51 52 53

    _writer = new QTextStream(_destination);
}
KeyboardTranslatorWriter::~KeyboardTranslatorWriter()
{
    delete _writer;
}
Kurt Hindenburg's avatar
Kurt Hindenburg committed
54
void KeyboardTranslatorWriter::writeHeader(const QString& description)
55 56 57
{
    *_writer << "keyboard \"" << description << '\"' << '\n';
}
Kurt Hindenburg's avatar
Kurt Hindenburg committed
58
void KeyboardTranslatorWriter::writeEntry(const KeyboardTranslator::Entry& entry)
59 60
{
    QString result;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
61
    if (entry.command() != KeyboardTranslator::NoCommand)
62 63
        result = entry.resultToString();
    else
64
        result = QLatin1Char('\"') + entry.resultToString() + QLatin1Char('\"');
65 66 67 68

    *_writer << "key " << entry.conditionToString() << " : " << result << '\n';
}

69 70 71 72 73 74 75 76 77
// each line of the keyboard translation file is one of:
//
// - keyboard "name"
// - key KeySequence : "characters"
// - key KeySequence : CommandName
//
// KeySequence begins with the name of the key ( taken from the Qt::Key enum )
// and is followed by the keyboard modifiers and state flags ( with + or - in front
// of each modifier or flag to indicate whether it is required ).  All keyboard modifiers
Kurt Hindenburg's avatar
Kurt Hindenburg committed
78
// and flags are optional, if a particular modifier or state is not specified it is
79 80 81
// assumed not to be a part of the sequence.  The key sequence may contain whitespace
//
// eg:  "key Up+Shift : scrollLineUp"
Jekyll Wu's avatar
Jekyll Wu committed
82
//      "key PgDown-Shift : "\E[6~"
83 84 85 86 87
//
// (lines containing only whitespace are ignored, parseLine assumes that comments have
// already been removed)
//

Kurt Hindenburg's avatar
Kurt Hindenburg committed
88
KeyboardTranslatorReader::KeyboardTranslatorReader(QIODevice* source)
89
    : _source(source)
90 91
    , _description(QString())
    , _nextEntry()
92
    , _hasNext(false)
93
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
94 95
    // read input until we find the description
    while (_description.isEmpty() && !source->atEnd()) {
96
        QList<Token> tokens = tokenize(QString::fromLocal8Bit(source->readLine()));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
97
        if (!tokens.isEmpty() && tokens.first().type == Token::TitleKeyword)
Alex Richardson's avatar
Alex Richardson committed
98
            _description = i18n(tokens[1].text.toUtf8().constData());
Kurt Hindenburg's avatar
Kurt Hindenburg committed
99 100 101
    }
    // read first entry (if any)
    readNext();
102
}
103

104
KeyboardTranslatorReader::~KeyboardTranslatorReader() = default;
105

Kurt Hindenburg's avatar
Kurt Hindenburg committed
106
void KeyboardTranslatorReader::readNext()
107 108
{
    // find next entry
Kurt Hindenburg's avatar
Kurt Hindenburg committed
109
    while (!_source->atEnd()) {
110
        const QList<Token>& tokens = tokenize(QString::fromLocal8Bit(_source->readLine()));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
111
        if (!tokens.isEmpty() && tokens.first().type == Token::KeyKeyword) {
112 113 114 115
            KeyboardTranslator::States flags = KeyboardTranslator::NoState;
            KeyboardTranslator::States flagMask = KeyboardTranslator::NoState;
            Qt::KeyboardModifiers modifiers = Qt::NoModifier;
            Qt::KeyboardModifiers modifierMask = Qt::NoModifier;
116 117 118

            int keyCode = Qt::Key_unknown;

119
            decodeSequence(tokens[1].text.toLower(),
120 121 122 123
                           keyCode,
                           modifiers,
                           modifierMask,
                           flags,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
124
                           flagMask);
125

126 127
            KeyboardTranslator::Command command = KeyboardTranslator::NoCommand;
            QByteArray text;
128 129

            // get text or command
Kurt Hindenburg's avatar
Kurt Hindenburg committed
130
            if (tokens[2].type == Token::OutputText) {
131
                text = tokens[2].text.toLocal8Bit();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
132
            } else if (tokens[2].type == Token::Command) {
133
                // identify command
Kurt Hindenburg's avatar
Kurt Hindenburg committed
134
                if (!parseAsCommand(tokens[2].text, command))
135
                    qCDebug(KonsoleDebug) << "Key" << tokens[1].text << ", Command" << tokens[2].text << "not understood. ";
136 137
            }

138
            KeyboardTranslator::Entry newEntry;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
139 140 141 142 143 144 145
            newEntry.setKeyCode(keyCode);
            newEntry.setState(flags);
            newEntry.setStateMask(flagMask);
            newEntry.setModifiers(modifiers);
            newEntry.setModifierMask(modifierMask);
            newEntry.setText(text);
            newEntry.setCommand(command);
146

147 148
            _nextEntry = newEntry;

149 150 151 152
            _hasNext = true;

            return;
        }
Kurt Hindenburg's avatar
Kurt Hindenburg committed
153
    }
154 155 156

    _hasNext = false;
}
157

Kurt Hindenburg's avatar
Kurt Hindenburg committed
158
bool KeyboardTranslatorReader::parseAsCommand(const QString& text, KeyboardTranslator::Command& command)
159
{
160
    if (text.compare(QLatin1String("erase"), Qt::CaseInsensitive) == 0)
161
        command = KeyboardTranslator::EraseCommand;
162
    else if (text.compare(QLatin1String("scrollpageup"), Qt::CaseInsensitive) == 0)
163
        command = KeyboardTranslator::ScrollPageUpCommand;
164
    else if (text.compare(QLatin1String("scrollpagedown"), Qt::CaseInsensitive) == 0)
165
        command = KeyboardTranslator::ScrollPageDownCommand;
166
    else if (text.compare(QLatin1String("scrolllineup"), Qt::CaseInsensitive) == 0)
167
        command = KeyboardTranslator::ScrollLineUpCommand;
168
    else if (text.compare(QLatin1String("scrolllinedown"), Qt::CaseInsensitive) == 0)
169
        command = KeyboardTranslator::ScrollLineDownCommand;
170
    else if (text.compare(QLatin1String("scrolluptotop"), Qt::CaseInsensitive) == 0)
171
        command = KeyboardTranslator::ScrollUpToTopCommand;
172
    else if (text.compare(QLatin1String("scrolldowntobottom"), Qt::CaseInsensitive) == 0)
173
        command = KeyboardTranslator::ScrollDownToBottomCommand;
174
    else
175
        return false;
176

177
    return true;
178 179
}

180
bool KeyboardTranslatorReader::decodeSequence(const QString& text,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
181 182 183 184 185
        int& keyCode,
        Qt::KeyboardModifiers& modifiers,
        Qt::KeyboardModifiers& modifierMask,
        KeyboardTranslator::States& flags,
        KeyboardTranslator::States& flagMask)
186
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
187
    bool isWanted = true;
188 189
    QString buffer;

190 191 192 193
    Qt::KeyboardModifiers tempModifiers = modifiers;
    Qt::KeyboardModifiers tempModifierMask = modifierMask;
    KeyboardTranslator::States tempFlags = flags;
    KeyboardTranslator::States tempFlagMask = flagMask;
194

Kurt Hindenburg's avatar
Kurt Hindenburg committed
195
    for (int i = 0 ; i < text.count() ; i++) {
196
        const QChar& ch = text[i];
Jekyll Wu's avatar
Jekyll Wu committed
197 198
        const bool isFirstLetter = (i == 0);
        const bool isLastLetter = (i == text.count() - 1);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
199
        bool endOfItem = true;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
200
        if (ch.isLetterOrNumber()) {
201 202
            endOfItem = false;
            buffer.append(ch);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
203
        } else if (isFirstLetter) {
204
            buffer.append(ch);
205 206
        }

Kurt Hindenburg's avatar
Kurt Hindenburg committed
207
        if ((endOfItem || isLastLetter) && !buffer.isEmpty()) {
208
            Qt::KeyboardModifier itemModifier = Qt::NoModifier;
209
            int itemKeyCode = 0;
210
            KeyboardTranslator::State itemFlag = KeyboardTranslator::NoState;
211

Kurt Hindenburg's avatar
Kurt Hindenburg committed
212
            if (parseAsModifier(buffer, itemModifier)) {
213 214
                tempModifierMask |= itemModifier;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
215
                if (isWanted)
216
                    tempModifiers |= itemModifier;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
217
            } else if (parseAsStateFlag(buffer, itemFlag)) {
218 219
                tempFlagMask |= itemFlag;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
220
                if (isWanted)
221
                    tempFlags |= itemFlag;
222
            } else if (parseAsKeyCode(buffer, itemKeyCode)) {
223
                keyCode = itemKeyCode;
224
            } else {
225
                qCDebug(KonsoleDebug) << "Unable to parse key binding item:" << buffer;
226
            }
227 228 229

            buffer.clear();
        }
230

Kurt Hindenburg's avatar
Kurt Hindenburg committed
231
        // check if this is a wanted / not-wanted flag and update the
232
        // state ready for the next item
233
        if (ch == QLatin1Char('+'))
Kurt Hindenburg's avatar
Kurt Hindenburg committed
234
            isWanted = true;
235
        else if (ch == QLatin1Char('-'))
Kurt Hindenburg's avatar
Kurt Hindenburg committed
236 237
            isWanted = false;
    }
238

239 240 241 242
    modifiers = tempModifiers;
    modifierMask = tempModifierMask;
    flags = tempFlags;
    flagMask = tempFlagMask;
243 244 245 246

    return true;
}

247
bool KeyboardTranslatorReader::parseAsModifier(const QString& item , Qt::KeyboardModifier& modifier)
248
{
249
    if (item == QLatin1String("shift"))
250
        modifier = Qt::ShiftModifier;
251
    else if (item == QLatin1String("ctrl") || item == QLatin1String("control"))
252
        modifier = Qt::ControlModifier;
253
    else if (item == QLatin1String("alt"))
254
        modifier = Qt::AltModifier;
255
    else if (item == QLatin1String("meta"))
256
        modifier = Qt::MetaModifier;
257
    else if (item == QLatin1String("keypad"))
258
        modifier = Qt::KeypadModifier;
259 260 261 262 263
    else
        return false;

    return true;
}
264
bool KeyboardTranslatorReader::parseAsStateFlag(const QString& item , KeyboardTranslator::State& flag)
265
{
266
    if (item == QLatin1String("appcukeys") || item == QLatin1String("appcursorkeys"))
267
        flag = KeyboardTranslator::CursorKeysState;
268
    else if (item == QLatin1String("ansi"))
269
        flag = KeyboardTranslator::AnsiState;
270
    else if (item == QLatin1String("newline"))
271
        flag = KeyboardTranslator::NewLineState;
272
    else if (item == QLatin1String("appscreen"))
273
        flag = KeyboardTranslator::AlternateScreenState;
274
    else if (item == QLatin1String("anymod") || item == QLatin1String("anymodifier"))
275
        flag = KeyboardTranslator::AnyModifierState;
276
    else if (item == QLatin1String("appkeypad"))
277
        flag = KeyboardTranslator::ApplicationKeypadState;
278 279 280 281 282 283 284 285
    else
        return false;

    return true;
}
bool KeyboardTranslatorReader::parseAsKeyCode(const QString& item , int& keyCode)
{
    QKeySequence sequence = QKeySequence::fromString(item);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
286
    if (!sequence.isEmpty()) {
287 288
        keyCode = sequence[0];

Kurt Hindenburg's avatar
Kurt Hindenburg committed
289
        if (sequence.count() > 1) {
290
            qCDebug(KonsoleDebug) << "Unhandled key codes in sequence: " << item;
291
        }
292
    } else {
293
        return false;
294
    }
295 296 297 298

    return true;
}

299 300 301 302 303 304 305 306
QString KeyboardTranslatorReader::description() const
{
    return _description;
}
bool KeyboardTranslatorReader::hasNextEntry()
{
    return _hasNext;
}
Kurt Hindenburg's avatar
Kurt Hindenburg committed
307 308
KeyboardTranslator::Entry KeyboardTranslatorReader::createEntry(const QString& condition ,
        const QString& result)
309
{
310
    QString entryString(QStringLiteral("keyboard \"temporary\"\nkey "));
311
    entryString.append(condition);
312
    entryString.append(QLatin1String(" : "));
313

314 315 316 317
    // if 'result' is the name of a command then the entry result will be that command,
    // otherwise the result will be treated as a string to echo when the key sequence
    // specified by 'condition' is pressed
    KeyboardTranslator::Command command;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
318
    if (parseAsCommand(result, command))
319 320
        entryString.append(result);
    else
321
        entryString.append(QLatin1Char('\"') + result + QLatin1Char('\"'));
322 323 324 325 326 327

    QByteArray array = entryString.toUtf8();
    QBuffer buffer(&array);
    buffer.open(QIODevice::ReadOnly);
    KeyboardTranslatorReader reader(&buffer);

328
    KeyboardTranslator::Entry entry;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
329
    if (reader.hasNextEntry())
330 331 332 333 334
        entry = reader.nextEntry();

    return entry;
}

Kurt Hindenburg's avatar
Kurt Hindenburg committed
335
KeyboardTranslator::Entry KeyboardTranslatorReader::nextEntry()
336
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
337
    Q_ASSERT(_hasNext);
338 339 340 341 342 343 344 345 346 347
    KeyboardTranslator::Entry entry = _nextEntry;
    readNext();
    return entry;
}
bool KeyboardTranslatorReader::parseError()
{
    return false;
}
QList<KeyboardTranslatorReader::Token> KeyboardTranslatorReader::tokenize(const QString& line)
{
348
    QString text = line;
349

Kurt Hindenburg's avatar
Kurt Hindenburg committed
350
    // remove comments
351 352
    bool inQuotes = false;
    int commentPos = -1;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
353
    for (int i = text.length() - 1; i >= 0; i--) {
354
        QChar ch = text[i];
355
        if (ch == QLatin1Char('\"'))
356
            inQuotes = !inQuotes;
357
        else if (ch == QLatin1Char('#') && !inQuotes)
358 359 360
            commentPos = i;
    }
    if (commentPos != -1)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
361
        text.remove(commentPos, text.length());
362 363

    text = text.simplified();
364

365
    // title line: keyboard "title"
366
    static const QRegularExpression title(QStringLiteral("keyboard\\s+\"(.*)\""), QRegularExpression::OptimizeOnFirstUsageOption);
367 368
    // key line: key KeySequence : "output"
    // key line: key KeySequence : command
369
    static const QRegularExpression key(QStringLiteral("key\\s+([\\w\\+\\s\\-\\*\\.]+)\\s*:\\s*(\"(.*)\"|\\w+)"), QRegularExpression::OptimizeOnFirstUsageOption);
370

371
    QList<Token> list;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
372
    if (text.isEmpty()) {
373 374 375
        return list;
    }

376 377 378
    QRegularExpressionMatch titleMatch(title.match(text));

    if (titleMatch.hasMatch()) {
379
        Token titleToken = { Token::TitleKeyword , QString() };
380
        Token textToken = { Token::TitleText , titleMatch.captured(1) };
381

382
        list << titleToken << textToken;
383 384 385 386 387
        return list;
    }

    QRegularExpressionMatch keyMatch(key.match(text));
    if (!keyMatch.hasMatch()) {
388
        qCDebug(KonsoleDebug) << "Line in keyboard translator file could not be understood:" << text;
389 390 391 392 393
        return list;
    }

    Token keyToken = { Token::KeyKeyword , QString() };
    QString sequenceTokenString = keyMatch.captured(1);
394
    Token sequenceToken = { Token::KeySequence , sequenceTokenString.remove(QLatin1Char(' ')) };
395 396 397 398 399 400 401 402 403 404 405

    list << keyToken << sequenceToken;

    if (keyMatch.capturedRef(3).isEmpty()) {
        // capturedTexts().at(2) is a command
        Token commandToken = { Token::Command , keyMatch.captured(2) };
        list << commandToken;
    } else {
        // capturedTexts().at(3) is the output string
        Token outputToken = { Token::OutputText , keyMatch.captured(3) };
        list << outputToken;
406 407 408 409
    }

    return list;
}
410 411

KeyboardTranslator::Entry::Entry()
Kurt Hindenburg's avatar
Kurt Hindenburg committed
412 413 414 415 416 417
    : _keyCode(0)
    , _modifiers(Qt::NoModifier)
    , _modifierMask(Qt::NoModifier)
    , _state(NoState)
    , _stateMask(NoState)
    , _command(NoCommand)
418
    , _text(QByteArray())
419 420
{
}
421

422
bool KeyboardTranslator::Entry::operator==(const Entry& rhs) const
423 424 425 426 427 428 429 430 431 432
{
    return _keyCode == rhs._keyCode &&
           _modifiers == rhs._modifiers &&
           _modifierMask == rhs._modifierMask &&
           _state == rhs._state &&
           _stateMask == rhs._stateMask &&
           _command == rhs._command &&
           _text == rhs._text;
}

433 434
bool KeyboardTranslator::Entry::matches(int testKeyCode,
                                        Qt::KeyboardModifiers testKeyboardModifiers,
435
                                        States testState) const
436
{
437
    if (_keyCode != testKeyCode)
438 439
        return false;

440
    if ((testKeyboardModifiers & _modifierMask) != (_modifiers & _modifierMask))
441 442
        return false;

443 444
    // if testKeyboardModifiers is non-zero, the 'any modifier' state is implicit
    if (testKeyboardModifiers != 0)
445
        testState |= AnyModifierState;
446

Kurt Hindenburg's avatar
Kurt Hindenburg committed
447
    if ((testState & _stateMask) != (_state & _stateMask))
448 449
        return false;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
450
    // special handling for the 'Any Modifier' state, which checks for the presence of
451
    // any or no modifiers.  In this context, the 'keypad' modifier does not count.
Kurt Hindenburg's avatar
Kurt Hindenburg committed
452 453
    bool anyModifiersSet = (testKeyboardModifiers != 0)
                           && (testKeyboardModifiers != Qt::KeypadModifier);
454 455
    bool wantAnyModifier = (_state & KeyboardTranslator::AnyModifierState) != 0;
    if ((_stateMask & KeyboardTranslator::AnyModifierState) != 0) {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
456 457
        if (wantAnyModifier != anyModifiersSet)
            return false;
458
    }
459

460
    return true;
461
}
462 463
QByteArray KeyboardTranslator::Entry::escapedText(bool expandWildCards,
        Qt::KeyboardModifiers keyboardModifiers) const
464
{
465
    QByteArray result(text(expandWildCards, keyboardModifiers));
466

Kurt Hindenburg's avatar
Kurt Hindenburg committed
467
    for (int i = 0 ; i < result.count() ; i++) {
Jekyll Wu's avatar
Jekyll Wu committed
468
        const char ch = result[i];
469 470
        char replacement = 0;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
471 472 473 474 475 476 477 478 479 480
        switch (ch) {
        case 27 : replacement = 'E'; break;
        case 8  : replacement = 'b'; break;
        case 12 : replacement = 'f'; break;
        case 9  : replacement = 't'; break;
        case 13 : replacement = 'r'; break;
        case 10 : replacement = 'n'; break;
        default:
            // any character which is not printable is replaced by an equivalent
            // \xhh escape sequence (where 'hh' are the corresponding hex digits)
481
            if (!QChar(QLatin1Char(ch)).isPrint())
Kurt Hindenburg's avatar
Kurt Hindenburg committed
482
                replacement = 'x';
483 484
        }

Kurt Hindenburg's avatar
Kurt Hindenburg committed
485 486 487 488 489 490
        if (replacement == 'x') {
            result.replace(i, 1, "\\x" + QByteArray(1, ch).toHex());
        } else if (replacement != 0) {
            result.remove(i, 1);
            result.insert(i, '\\');
            result.insert(i + 1, replacement);
491 492 493 494 495
        }
    }

    return result;
}
496 497 498 499
QByteArray KeyboardTranslator::Entry::unescape(const QByteArray& input) const
{
    QByteArray result(input);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
500
    for (int i = 0 ; i < result.count() - 1 ; i++) {
501
        QByteRef ch = result[i];
Kurt Hindenburg's avatar
Kurt Hindenburg committed
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
        if (ch == '\\') {
            char replacement[2] = {0, 0};
            int charsToRemove = 2;
            bool escapedChar = true;

            switch (result[i + 1]) {
            case 'E' : replacement[0] = 27; break;
            case 'b' : replacement[0] = 8 ; break;
            case 'f' : replacement[0] = 12; break;
            case 't' : replacement[0] = 9 ; break;
            case 'r' : replacement[0] = 13; break;
            case 'n' : replacement[0] = 10; break;
            case 'x' : {
                // format is \xh or \xhh where 'h' is a hexadecimal
                // digit from 0-9 or A-F which should be replaced
                // with the corresponding character value
                char hexDigits[3] = {0};

520
                if ((i < result.count() - 2) && (isxdigit(result[i + 2]) != 0))
Kurt Hindenburg's avatar
Kurt Hindenburg committed
521
                    hexDigits[0] = result[i + 2];
522
                if ((i < result.count() - 3) && (isxdigit(result[i + 3]) != 0))
Kurt Hindenburg's avatar
Kurt Hindenburg committed
523 524 525
                    hexDigits[1] = result[i + 3];

                unsigned charValue = 0;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
526
                sscanf(hexDigits, "%2x", &charValue);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
527

528
                replacement[0] = static_cast<char>(charValue);
529
                charsToRemove = 2 + qstrlen(hexDigits);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
530 531 532 533 534 535 536
            }
            break;
            default:
                escapedChar = false;
            }

            if (escapedChar)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
537
	         result.replace(i, charsToRemove, replacement, 1);
538 539
        }
    }
Dirk Mueller's avatar
Dirk Mueller committed
540

541 542 543
    return result;
}

Kurt Hindenburg's avatar
Kurt Hindenburg committed
544
void KeyboardTranslator::Entry::insertModifier(QString& item , int modifier) const
545
{
546
    if ((modifier & _modifierMask) == 0u)
547 548
        return;

549
    if ((modifier & _modifiers) != 0u)
550
        item += QLatin1Char('+');
551
    else
552
        item += QLatin1Char('-');
553

Kurt Hindenburg's avatar
Kurt Hindenburg committed
554
    if (modifier == Qt::ShiftModifier)
555
        item += QLatin1String("Shift");
Kurt Hindenburg's avatar
Kurt Hindenburg committed
556
    else if (modifier == Qt::ControlModifier)
557
        item += QLatin1String("Ctrl");
Kurt Hindenburg's avatar
Kurt Hindenburg committed
558
    else if (modifier == Qt::AltModifier)
559
        item += QLatin1String("Alt");
Kurt Hindenburg's avatar
Kurt Hindenburg committed
560
    else if (modifier == Qt::MetaModifier)
561
        item += QLatin1String("Meta");
Kurt Hindenburg's avatar
Kurt Hindenburg committed
562
    else if (modifier == Qt::KeypadModifier)
563
        item += QLatin1String("KeyPad");
564
}
565
void KeyboardTranslator::Entry::insertState(QString& item, int aState) const
566
{
567
    if ((aState & _stateMask) == 0)
568 569
        return;

570
    if ((aState & _state) != 0)
571
        item += QLatin1Char('+');
572
    else
573
        item += QLatin1Char('-');
574

575
    if (aState == KeyboardTranslator::AlternateScreenState)
576
        item += QLatin1String("AppScreen");
577
    else if (aState == KeyboardTranslator::NewLineState)
578
        item += QLatin1String("NewLine");
579
    else if (aState == KeyboardTranslator::AnsiState)
580
        item += QLatin1String("Ansi");
581
    else if (aState == KeyboardTranslator::CursorKeysState)
582
        item += QLatin1String("AppCursorKeys");
583
    else if (aState == KeyboardTranslator::AnyModifierState)
584
        item += QLatin1String("AnyModifier");
585
    else if (aState == KeyboardTranslator::ApplicationKeypadState)
586
        item += QLatin1String("AppKeypad");
587
}
588 589
QString KeyboardTranslator::Entry::resultToString(bool expandWildCards,
        Qt::KeyboardModifiers keyboardModifiers) const
590
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
591
    if (!_text.isEmpty())
592
        return QString::fromLatin1(escapedText(expandWildCards, keyboardModifiers));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
593
    else if (_command == EraseCommand)
594
        return QStringLiteral("Erase");
Kurt Hindenburg's avatar
Kurt Hindenburg committed
595
    else if (_command == ScrollPageUpCommand)
596
        return QStringLiteral("ScrollPageUp");
Kurt Hindenburg's avatar
Kurt Hindenburg committed
597
    else if (_command == ScrollPageDownCommand)
598
        return QStringLiteral("ScrollPageDown");
Kurt Hindenburg's avatar
Kurt Hindenburg committed
599
    else if (_command == ScrollLineUpCommand)
600
        return QStringLiteral("ScrollLineUp");
Kurt Hindenburg's avatar
Kurt Hindenburg committed
601
    else if (_command == ScrollLineDownCommand)
602
        return QStringLiteral("ScrollLineDown");
Kurt Hindenburg's avatar
Kurt Hindenburg committed
603
    else if (_command == ScrollUpToTopCommand)
604
        return QStringLiteral("ScrollUpToTop");
Kurt Hindenburg's avatar
Kurt Hindenburg committed
605
    else if (_command == ScrollDownToBottomCommand)
606
        return QStringLiteral("ScrollDownToBottom");
607 608 609

    return QString();
}
610 611 612 613
QString KeyboardTranslator::Entry::conditionToString() const
{
    QString result = QKeySequence(_keyCode).toString();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
614 615 616 617 618
    insertModifier(result , Qt::ShiftModifier);
    insertModifier(result , Qt::ControlModifier);
    insertModifier(result , Qt::AltModifier);
    insertModifier(result , Qt::MetaModifier);
    insertModifier(result , Qt::KeypadModifier);
619

Kurt Hindenburg's avatar
Kurt Hindenburg committed
620 621 622 623 624 625
    insertState(result , KeyboardTranslator::AlternateScreenState);
    insertState(result , KeyboardTranslator::NewLineState);
    insertState(result , KeyboardTranslator::AnsiState);
    insertState(result , KeyboardTranslator::CursorKeysState);
    insertState(result , KeyboardTranslator::AnyModifierState);
    insertState(result , KeyboardTranslator::ApplicationKeypadState);
626 627 628 629

    return result;
}

630
KeyboardTranslator::KeyboardTranslator(const QString& aName)
631 632 633
    : _entries(QMultiHash<int, Entry>())
    , _name(aName)
    , _description(QString())
634 635
{
}
636

637
FallbackKeyboardTranslator::FallbackKeyboardTranslator()
638
    : KeyboardTranslator(QStringLiteral("fallback"))
639
{
640
    setDescription(QStringLiteral("Fallback Keyboard Translator"));
641 642 643 644 645 646 647 648

    // Key "TAB" should send out '\t'
    KeyboardTranslator::Entry entry;
    entry.setKeyCode(Qt::Key_Tab);
    entry.setText("\t");
    addEntry(entry);
}

649
void KeyboardTranslator::setDescription(const QString& aDescription)
650
{
651
    _description = aDescription;
652
}
Jekyll Wu's avatar
Jekyll Wu committed
653

654 655 656 657
QString KeyboardTranslator::description() const
{
    return _description;
}
Jekyll Wu's avatar
Jekyll Wu committed
658

659
void KeyboardTranslator::setName(const QString& aName)
660
{
661
    _name = aName;
662
}
Jekyll Wu's avatar
Jekyll Wu committed
663

664 665 666 667 668
QString KeyboardTranslator::name() const
{
    return _name;
}

669
QList<KeyboardTranslator::Entry> KeyboardTranslator::entries() const
670
{
671
    return _entries.values();
672
}
673 674 675 676

void KeyboardTranslator::addEntry(const Entry& entry)
{
    const int keyCode = entry.keyCode();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
677
    _entries.insert(keyCode, entry);
678
}
Jekyll Wu's avatar
Jekyll Wu committed
679

680 681
void KeyboardTranslator::replaceEntry(const Entry& existing , const Entry& replacement)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
682 683
    if (!existing.isNull())
        _entries.remove(existing.keyCode(), existing);
Jekyll Wu's avatar
Jekyll Wu committed
684

Kurt Hindenburg's avatar
Kurt Hindenburg committed
685
    _entries.insert(replacement.keyCode(), replacement);
686
}
Jekyll Wu's avatar
Jekyll Wu committed
687

688 689
void KeyboardTranslator::removeEntry(const Entry& entry)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
690
    _entries.remove(entry.keyCode(), entry);
691
}
Jekyll Wu's avatar
Jekyll Wu committed
692

693
KeyboardTranslator::Entry KeyboardTranslator::findEntry(int keyCode, Qt::KeyboardModifiers modifiers, States state) const
694
{
695 696 697 698 699 700
    QHash<int, KeyboardTranslator::Entry>::const_iterator i = _entries.find(keyCode);
    while (i != _entries.constEnd() && i.key() == keyCode) {
        if (i.value().matches(keyCode, modifiers, state)) {
            return i.value();
        }
        ++i;
701
    }
Jekyll Wu's avatar
Jekyll Wu committed
702 703

    return Entry(); // No matching entry
704
}