2
This source file is part of Konsole, a terminal emulator.
4
Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23
#include "KeyboardTranslator.h"
33
#include <QTextStream>
34
#include <QKeySequence>
43
//#include <KStandardDirs>
45
using namespace Konsole;
48
const QByteArray KeyboardTranslatorManager::defaultTranslatorText(
49
"keyboard \"Fallback Key Translator\"\n"
53
KeyboardTranslatorManager::KeyboardTranslatorManager()
54
: _haveLoadedAll(false)
57
KeyboardTranslatorManager::~KeyboardTranslatorManager()
59
qDeleteAll(_translators);
61
QString KeyboardTranslatorManager::findTranslatorPath(const QString& name)
63
return QString(get_kb_layout_dir() + name + ".keytab");
64
//return KGlobal::dirs()->findResource("data","konsole/"+name+".keytab");
67
void KeyboardTranslatorManager::findTranslators()
69
QDir dir(get_kb_layout_dir());
71
filters << "*.keytab";
72
dir.setNameFilters(filters);
73
QStringList list = dir.entryList(filters);
74
list = dir.entryList(filters);
75
// QStringList list = KGlobal::dirs()->findAllResources("data",
76
// "konsole/*.keytab",
77
// KStandardDirs::NoDuplicates);
79
// add the name of each translator to the list and associated
80
// the name with a null pointer to indicate that the translator
81
// has not yet been loaded from disk
82
QStringListIterator listIter(list);
83
while (listIter.hasNext())
85
QString translatorPath = listIter.next();
87
QString name = QFileInfo(translatorPath).baseName();
89
if ( !_translators.contains(name) )
90
_translators.insert(name,0);
93
_haveLoadedAll = true;
96
const KeyboardTranslator* KeyboardTranslatorManager::findTranslator(const QString& name)
99
return defaultTranslator();
101
if ( _translators.contains(name) && _translators[name] != 0 )
102
return _translators[name];
104
KeyboardTranslator* translator = loadTranslator(name);
106
if ( translator != 0 )
107
_translators[name] = translator;
108
else if ( !name.isEmpty() )
109
qDebug() << "Unable to load translator" << name;
114
bool KeyboardTranslatorManager::saveTranslator(const KeyboardTranslator* translator)
116
qDebug() << "KeyboardTranslatorManager::saveTranslator" << "unimplemented";
117
Q_UNUSED(translator);
119
const QString path = KGlobal::dirs()->saveLocation("data","konsole/")+translator->name()
122
//kDebug() << "Saving translator to" << path;
124
QFile destination(path);
125
if (!destination.open(QIODevice::WriteOnly | QIODevice::Text))
127
qDebug() << "Unable to save keyboard translation:"
128
<< destination.errorString();
133
KeyboardTranslatorWriter writer(&destination);
134
writer.writeHeader(translator->description());
136
QListIterator<KeyboardTranslator::Entry> iter(translator->entries());
137
while ( iter.hasNext() )
138
writer.writeEntry(iter.next());
146
KeyboardTranslator* KeyboardTranslatorManager::loadTranslator(const QString& name)
148
const QString& path = findTranslatorPath(name);
151
if (name.isEmpty() || !source.open(QIODevice::ReadOnly | QIODevice::Text))
154
return loadTranslator(&source,name);
157
const KeyboardTranslator* KeyboardTranslatorManager::defaultTranslator()
159
// Try to find the default.keytab file if it exists, otherwise
160
// fall back to the hard-coded one
161
const KeyboardTranslator* translator = findTranslator("default");
165
textBuffer.setData(defaultTranslatorText);
166
textBuffer.open(QIODevice::ReadOnly);
167
translator = loadTranslator(&textBuffer,"fallback");
172
KeyboardTranslator* KeyboardTranslatorManager::loadTranslator(QIODevice* source,const QString& name)
174
KeyboardTranslator* translator = new KeyboardTranslator(name);
175
KeyboardTranslatorReader reader(source);
176
translator->setDescription( reader.description() );
177
while ( reader.hasNextEntry() )
178
translator->addEntry(reader.nextEntry());
182
if ( !reader.parseError() )
193
KeyboardTranslatorWriter::KeyboardTranslatorWriter(QIODevice* destination)
194
: _destination(destination)
196
Q_ASSERT( destination && destination->isWritable() );
198
_writer = new QTextStream(_destination);
200
KeyboardTranslatorWriter::~KeyboardTranslatorWriter()
204
void KeyboardTranslatorWriter::writeHeader( const QString& description )
206
*_writer << "keyboard \"" << description << '\"' << '\n';
208
void KeyboardTranslatorWriter::writeEntry( const KeyboardTranslator::Entry& entry )
211
if ( entry.command() != KeyboardTranslator::NoCommand )
212
result = entry.resultToString();
214
result = '\"' + entry.resultToString() + '\"';
216
*_writer << "key " << entry.conditionToString() << " : " << result << '\n';
220
// each line of the keyboard translation file is one of:
223
// - key KeySequence : "characters"
224
// - key KeySequence : CommandName
226
// KeySequence begins with the name of the key ( taken from the Qt::Key enum )
227
// and is followed by the keyboard modifiers and state flags ( with + or - in front
228
// of each modifier or flag to indicate whether it is required ). All keyboard modifiers
229
// and flags are optional, if a particular modifier or state is not specified it is
230
// assumed not to be a part of the sequence. The key sequence may contain whitespace
232
// eg: "key Up+Shift : scrollLineUp"
233
// "key Next-Shift : "\E[6~"
235
// (lines containing only whitespace are ignored, parseLine assumes that comments have
236
// already been removed)
239
KeyboardTranslatorReader::KeyboardTranslatorReader( QIODevice* source )
243
// read input until we find the description
244
while ( _description.isEmpty() && !source->atEnd() )
246
QList<Token> tokens = tokenize( QString(source->readLine()) );
247
if ( !tokens.isEmpty() && tokens.first().type == Token::TitleKeyword )
248
_description = tokens[1].text.toUtf8();
250
// read first entry (if any)
253
void KeyboardTranslatorReader::readNext()
256
while ( !_source->atEnd() )
258
const QList<Token>& tokens = tokenize( QString(_source->readLine()) );
259
if ( !tokens.isEmpty() && tokens.first().type == Token::KeyKeyword )
261
KeyboardTranslator::States flags = KeyboardTranslator::NoState;
262
KeyboardTranslator::States flagMask = KeyboardTranslator::NoState;
263
Qt::KeyboardModifiers modifiers = Qt::NoModifier;
264
Qt::KeyboardModifiers modifierMask = Qt::NoModifier;
266
int keyCode = Qt::Key_unknown;
268
decodeSequence(tokens[1].text.toLower(),
275
KeyboardTranslator::Command command = KeyboardTranslator::NoCommand;
278
// get text or command
279
if ( tokens[2].type == Token::OutputText )
281
text = tokens[2].text.toLocal8Bit();
283
else if ( tokens[2].type == Token::Command )
286
if (!parseAsCommand(tokens[2].text,command))
287
qDebug() << "Command" << tokens[2].text << "not understood.";
290
KeyboardTranslator::Entry newEntry;
291
newEntry.setKeyCode( keyCode );
292
newEntry.setState( flags );
293
newEntry.setStateMask( flagMask );
294
newEntry.setModifiers( modifiers );
295
newEntry.setModifierMask( modifierMask );
296
newEntry.setText( text );
297
newEntry.setCommand( command );
299
_nextEntry = newEntry;
310
bool KeyboardTranslatorReader::parseAsCommand(const QString& text,KeyboardTranslator::Command& command)
312
if ( text.compare("erase",Qt::CaseInsensitive) == 0 )
313
command = KeyboardTranslator::EraseCommand;
314
else if ( text.compare("scrollpageup",Qt::CaseInsensitive) == 0 )
315
command = KeyboardTranslator::ScrollPageUpCommand;
316
else if ( text.compare("scrollpagedown",Qt::CaseInsensitive) == 0 )
317
command = KeyboardTranslator::ScrollPageDownCommand;
318
else if ( text.compare("scrolllineup",Qt::CaseInsensitive) == 0 )
319
command = KeyboardTranslator::ScrollLineUpCommand;
320
else if ( text.compare("scrolllinedown",Qt::CaseInsensitive) == 0 )
321
command = KeyboardTranslator::ScrollLineDownCommand;
322
else if ( text.compare("scrolllock",Qt::CaseInsensitive) == 0 )
323
command = KeyboardTranslator::ScrollLockCommand;
330
bool KeyboardTranslatorReader::decodeSequence(const QString& text,
332
Qt::KeyboardModifiers& modifiers,
333
Qt::KeyboardModifiers& modifierMask,
334
KeyboardTranslator::States& flags,
335
KeyboardTranslator::States& flagMask)
337
bool isWanted = true;
338
bool endOfItem = false;
341
Qt::KeyboardModifiers tempModifiers = modifiers;
342
Qt::KeyboardModifiers tempModifierMask = modifierMask;
343
KeyboardTranslator::States tempFlags = flags;
344
KeyboardTranslator::States tempFlagMask = flagMask;
346
for ( int i = 0 ; i < text.count() ; i++ )
348
const QChar& ch = text[i];
349
bool isFirstLetter = i == 0;
350
bool isLastLetter = ( i == text.count()-1 );
352
if ( ch.isLetterOrNumber() )
356
} else if ( isFirstLetter )
361
if ( (endOfItem || isLastLetter) && !buffer.isEmpty() )
363
Qt::KeyboardModifier itemModifier = Qt::NoModifier;
365
KeyboardTranslator::State itemFlag = KeyboardTranslator::NoState;
367
if ( parseAsModifier(buffer,itemModifier) )
369
tempModifierMask |= itemModifier;
372
tempModifiers |= itemModifier;
374
else if ( parseAsStateFlag(buffer,itemFlag) )
376
tempFlagMask |= itemFlag;
379
tempFlags |= itemFlag;
381
else if ( parseAsKeyCode(buffer,itemKeyCode) )
382
keyCode = itemKeyCode;
384
qDebug() << "Unable to parse key binding item:" << buffer;
389
// check if this is a wanted / not-wanted flag and update the
390
// state ready for the next item
393
else if ( ch == '-' )
397
modifiers = tempModifiers;
398
modifierMask = tempModifierMask;
400
flagMask = tempFlagMask;
405
bool KeyboardTranslatorReader::parseAsModifier(const QString& item , Qt::KeyboardModifier& modifier)
407
if ( item == "shift" )
408
modifier = Qt::ShiftModifier;
409
else if ( item == "ctrl" || item == "control" )
410
modifier = Qt::ControlModifier;
411
else if ( item == "alt" )
412
modifier = Qt::AltModifier;
413
else if ( item == "meta" )
414
modifier = Qt::MetaModifier;
415
else if ( item == "keypad" )
416
modifier = Qt::KeypadModifier;
422
bool KeyboardTranslatorReader::parseAsStateFlag(const QString& item , KeyboardTranslator::State& flag)
424
if ( item == "appcukeys" || item == "appcursorkeys" )
425
flag = KeyboardTranslator::CursorKeysState;
426
else if ( item == "ansi" )
427
flag = KeyboardTranslator::AnsiState;
428
else if ( item == "newline" )
429
flag = KeyboardTranslator::NewLineState;
430
else if ( item == "appscreen" )
431
flag = KeyboardTranslator::AlternateScreenState;
432
else if ( item == "anymod" || item == "anymodifier" )
433
flag = KeyboardTranslator::AnyModifierState;
434
else if ( item == "appkeypad" )
435
flag = KeyboardTranslator::ApplicationKeypadState;
441
bool KeyboardTranslatorReader::parseAsKeyCode(const QString& item , int& keyCode)
443
QKeySequence sequence = QKeySequence::fromString(item);
444
if ( !sequence.isEmpty() )
446
keyCode = sequence[0];
448
if ( sequence.count() > 1 )
450
qDebug() << "Unhandled key codes in sequence: " << item;
453
// additional cases implemented for backwards compatibility with KDE 3
454
else if ( item == "prior" )
455
keyCode = Qt::Key_PageUp;
456
else if ( item == "next" )
457
keyCode = Qt::Key_PageDown;
464
QString KeyboardTranslatorReader::description() const
468
bool KeyboardTranslatorReader::hasNextEntry()
472
KeyboardTranslator::Entry KeyboardTranslatorReader::createEntry( const QString& condition ,
473
const QString& result )
475
QString entryString("keyboard \"temporary\"\nkey ");
476
entryString.append(condition);
477
entryString.append(" : ");
479
// if 'result' is the name of a command then the entry result will be that command,
480
// otherwise the result will be treated as a string to echo when the key sequence
481
// specified by 'condition' is pressed
482
KeyboardTranslator::Command command;
483
if (parseAsCommand(result,command))
484
entryString.append(result);
486
entryString.append('\"' + result + '\"');
488
QByteArray array = entryString.toUtf8();
489
QBuffer buffer(&array);
490
buffer.open(QIODevice::ReadOnly);
491
KeyboardTranslatorReader reader(&buffer);
493
KeyboardTranslator::Entry entry;
494
if ( reader.hasNextEntry() )
495
entry = reader.nextEntry();
500
KeyboardTranslator::Entry KeyboardTranslatorReader::nextEntry()
502
Q_ASSERT( _hasNext );
503
KeyboardTranslator::Entry entry = _nextEntry;
507
bool KeyboardTranslatorReader::parseError()
511
QList<KeyboardTranslatorReader::Token> KeyboardTranslatorReader::tokenize(const QString& line)
516
bool inQuotes = false;
518
for (int i=text.length()-1;i>=0;i--)
522
inQuotes = !inQuotes;
523
else if (ch == '#' && !inQuotes)
526
if (commentPos != -1)
527
text.remove(commentPos,text.length());
529
text = text.simplified();
531
// title line: keyboard "title"
532
static QRegExp title("keyboard\\s+\"(.*)\"");
533
// key line: key KeySequence : "output"
534
// key line: key KeySequence : command
535
static QRegExp key("key\\s+([\\w\\+\\s\\-\\*\\.]+)\\s*:\\s*(\"(.*)\"|\\w+)");
538
if ( text.isEmpty() )
543
if ( title.exactMatch(text) )
545
Token titleToken = { Token::TitleKeyword , QString() };
546
Token textToken = { Token::TitleText , title.capturedTexts()[1] };
548
list << titleToken << textToken;
550
else if ( key.exactMatch(text) )
552
Token keyToken = { Token::KeyKeyword , QString() };
553
Token sequenceToken = { Token::KeySequence , key.capturedTexts()[1].remove(' ') };
555
list << keyToken << sequenceToken;
557
if ( key.capturedTexts()[3].isEmpty() )
559
// capturedTexts()[2] is a command
560
Token commandToken = { Token::Command , key.capturedTexts()[2] };
561
list << commandToken;
565
// capturedTexts()[3] is the output string
566
Token outputToken = { Token::OutputText , key.capturedTexts()[3] };
572
qDebug() << "Line in keyboard translator file could not be understood:" << text;
578
QList<QString> KeyboardTranslatorManager::allTranslators()
580
if ( !_haveLoadedAll )
585
return _translators.keys();
588
KeyboardTranslator::Entry::Entry()
590
, _modifiers(Qt::NoModifier)
591
, _modifierMask(Qt::NoModifier)
593
, _stateMask(NoState)
594
, _command(NoCommand)
598
bool KeyboardTranslator::Entry::operator==(const Entry& rhs) const
600
return _keyCode == rhs._keyCode &&
601
_modifiers == rhs._modifiers &&
602
_modifierMask == rhs._modifierMask &&
603
_state == rhs._state &&
604
_stateMask == rhs._stateMask &&
605
_command == rhs._command &&
609
bool KeyboardTranslator::Entry::matches(int keyCode ,
610
Qt::KeyboardModifiers modifiers,
611
States testState) const
613
if ( _keyCode != keyCode )
616
if ( (modifiers & _modifierMask) != (_modifiers & _modifierMask) )
619
// if modifiers is non-zero, the 'any modifier' state is implicit
620
if ( modifiers != 0 )
621
testState |= AnyModifierState;
623
if ( (testState & _stateMask) != (_state & _stateMask) )
626
// special handling for the 'Any Modifier' state, which checks for the presence of
627
// any or no modifiers. In this context, the 'keypad' modifier does not count.
628
bool anyModifiersSet = modifiers != 0 && modifiers != Qt::KeypadModifier;
629
bool wantAnyModifier = _state & KeyboardTranslator::AnyModifierState;
630
if ( _stateMask & KeyboardTranslator::AnyModifierState )
632
if ( wantAnyModifier != anyModifiersSet )
638
QByteArray KeyboardTranslator::Entry::escapedText(bool expandWildCards,Qt::KeyboardModifiers modifiers) const
640
QByteArray result(text(expandWildCards,modifiers));
642
for ( int i = 0 ; i < result.count() ; i++ )
645
char replacement = 0;
649
case 27 : replacement = 'E'; break;
650
case 8 : replacement = 'b'; break;
651
case 12 : replacement = 'f'; break;
652
case 9 : replacement = 't'; break;
653
case 13 : replacement = 'r'; break;
654
case 10 : replacement = 'n'; break;
656
// any character which is not printable is replaced by an equivalent
657
// \xhh escape sequence (where 'hh' are the corresponding hex digits)
658
if ( !QChar(ch).isPrint() )
662
if ( replacement == 'x' )
664
result.replace(i,1,"\\x"+QByteArray(1,ch).toHex());
665
} else if ( replacement != 0 )
668
result.insert(i,'\\');
669
result.insert(i+1,replacement);
675
QByteArray KeyboardTranslator::Entry::unescape(const QByteArray& input) const
677
QByteArray result(input);
679
for ( int i = 0 ; i < result.count()-1 ; i++ )
682
QByteRef ch = result[i];
685
char replacement[2] = {0,0};
686
int charsToRemove = 2;
687
bool escapedChar = true;
689
switch ( result[i+1] )
691
case 'E' : replacement[0] = 27; break;
692
case 'b' : replacement[0] = 8 ; break;
693
case 'f' : replacement[0] = 12; break;
694
case 't' : replacement[0] = 9 ; break;
695
case 'r' : replacement[0] = 13; break;
696
case 'n' : replacement[0] = 10; break;
699
// format is \xh or \xhh where 'h' is a hexadecimal
700
// digit from 0-9 or A-F which should be replaced
701
// with the corresponding character value
702
char hexDigits[3] = {0};
704
if ( (i < result.count()-2) && isxdigit(result[i+2]) )
705
hexDigits[0] = result[i+2];
706
if ( (i < result.count()-3) && isxdigit(result[i+3]) )
707
hexDigits[1] = result[i+3];
709
unsigned charValue = 0;
710
sscanf(hexDigits,"%x",&charValue);
712
replacement[0] = (char)charValue;
713
charsToRemove = 2 + strlen(hexDigits);
721
result.replace(i,charsToRemove,replacement);
728
void KeyboardTranslator::Entry::insertModifier( QString& item , int modifier ) const
730
if ( !(modifier & _modifierMask) )
733
if ( modifier & _modifiers )
738
if ( modifier == Qt::ShiftModifier )
740
else if ( modifier == Qt::ControlModifier )
742
else if ( modifier == Qt::AltModifier )
744
else if ( modifier == Qt::MetaModifier )
746
else if ( modifier == Qt::KeypadModifier )
749
void KeyboardTranslator::Entry::insertState( QString& item , int state ) const
751
if ( !(state & _stateMask) )
754
if ( state & _state )
759
if ( state == KeyboardTranslator::AlternateScreenState )
761
else if ( state == KeyboardTranslator::NewLineState )
763
else if ( state == KeyboardTranslator::AnsiState )
765
else if ( state == KeyboardTranslator::CursorKeysState )
766
item += "AppCursorKeys";
767
else if ( state == KeyboardTranslator::AnyModifierState )
768
item += "AnyModifier";
769
else if ( state == KeyboardTranslator::ApplicationKeypadState )
772
QString KeyboardTranslator::Entry::resultToString(bool expandWildCards,Qt::KeyboardModifiers modifiers) const
774
if ( !_text.isEmpty() )
775
return escapedText(expandWildCards,modifiers);
776
else if ( _command == EraseCommand )
778
else if ( _command == ScrollPageUpCommand )
779
return "ScrollPageUp";
780
else if ( _command == ScrollPageDownCommand )
781
return "ScrollPageDown";
782
else if ( _command == ScrollLineUpCommand )
783
return "ScrollLineUp";
784
else if ( _command == ScrollLineDownCommand )
785
return "ScrollLineDown";
786
else if ( _command == ScrollLockCommand )
791
QString KeyboardTranslator::Entry::conditionToString() const
793
QString result = QKeySequence(_keyCode).toString();
795
insertModifier( result , Qt::ShiftModifier );
796
insertModifier( result , Qt::ControlModifier );
797
insertModifier( result , Qt::AltModifier );
798
insertModifier( result , Qt::MetaModifier );
799
insertModifier( result , Qt::KeypadModifier );
801
insertState( result , KeyboardTranslator::AlternateScreenState );
802
insertState( result , KeyboardTranslator::NewLineState );
803
insertState( result , KeyboardTranslator::AnsiState );
804
insertState( result , KeyboardTranslator::CursorKeysState );
805
insertState( result , KeyboardTranslator::AnyModifierState );
806
insertState( result , KeyboardTranslator::ApplicationKeypadState );
811
KeyboardTranslator::KeyboardTranslator(const QString& name)
816
void KeyboardTranslator::setDescription(const QString& description)
818
_description = description;
820
QString KeyboardTranslator::description() const
824
void KeyboardTranslator::setName(const QString& name)
828
QString KeyboardTranslator::name() const
833
QList<KeyboardTranslator::Entry> KeyboardTranslator::entries() const
835
return _entries.values();
838
void KeyboardTranslator::addEntry(const Entry& entry)
840
const int keyCode = entry.keyCode();
841
_entries.insert(keyCode,entry);
843
void KeyboardTranslator::replaceEntry(const Entry& existing , const Entry& replacement)
845
if ( !existing.isNull() )
846
_entries.remove(existing.keyCode(),existing);
847
_entries.insert(replacement.keyCode(),replacement);
849
void KeyboardTranslator::removeEntry(const Entry& entry)
851
_entries.remove(entry.keyCode(),entry);
853
KeyboardTranslator::Entry KeyboardTranslator::findEntry(int keyCode, Qt::KeyboardModifiers modifiers, States state) const
855
foreach(const Entry& entry, _entries.values(keyCode))
857
if ( entry.matches(keyCode,modifiers,state) )
860
return Entry(); // entry not found
862
void KeyboardTranslatorManager::addTranslator(KeyboardTranslator* translator)
864
_translators.insert(translator->name(),translator);
866
if ( !saveTranslator(translator) )
867
qDebug() << "Unable to save translator" << translator->name()
870
bool KeyboardTranslatorManager::deleteTranslator(const QString& name)
872
Q_ASSERT( _translators.contains(name) );
875
QString path = findTranslatorPath(name);
876
if ( QFile::remove(path) )
878
_translators.remove(name);
883
qDebug() << "Failed to remove translator - " << path;
887
//K_GLOBAL_STATIC( KeyboardTranslatorManager , theKeyboardTranslatorManager )
888
KeyboardTranslatorManager* KeyboardTranslatorManager::theKeyboardTranslatorManager = 0;
889
KeyboardTranslatorManager* KeyboardTranslatorManager::instance()
891
if (! theKeyboardTranslatorManager)
892
theKeyboardTranslatorManager = new KeyboardTranslatorManager();
893
return theKeyboardTranslatorManager;