source: trunk/tools/kmap2qmap/main.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

  • Property svn:eol-style set to native
File size: 35.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <cstdio>
43
44#include <QFile>
45#include <QFileInfo>
46#include <QDir>
47#include <QTextCodec>
48#include <QList>
49#include <QVector>
50#include <QByteArray>
51#include <QStringList>
52#include <QTextStream>
53
54#include "qkbd_qws_p.h"
55
56using namespace std;
57
58
59struct modifier_map_t {
60 const char *symbol;
61 quint8 modifier;
62 quint32 qtmodifier;
63};
64
65static const struct modifier_map_t modifier_map[] = {
66 { "plain", QWSKeyboard::ModPlain, Qt::NoModifier },
67 { "shift", QWSKeyboard::ModShift, Qt::ShiftModifier },
68 { "altgr", QWSKeyboard::ModAltGr, Qt::AltModifier },
69 { "control", QWSKeyboard::ModControl, Qt::ControlModifier },
70 { "alt", QWSKeyboard::ModAlt, Qt::AltModifier },
71 { "meta", QWSKeyboard::ModAlt, Qt::AltModifier },
72 { "shiftl", QWSKeyboard::ModShiftL, Qt::ShiftModifier },
73 { "shiftr", QWSKeyboard::ModShiftR, Qt::ShiftModifier },
74 { "ctrll", QWSKeyboard::ModCtrlL, Qt::ControlModifier },
75 { "ctrlr", QWSKeyboard::ModCtrlR, Qt::ControlModifier },
76};
77
78static const int modifier_map_size = sizeof(modifier_map)/sizeof(modifier_map_t);
79
80
81struct symbol_map_t {
82 const char *symbol;
83 quint32 qtcode;
84};
85
86static const struct symbol_map_t symbol_map[] = {
87 { "space", Qt::Key_Space },
88 { "exclam", Qt::Key_Exclam },
89 { "quotedbl", Qt::Key_QuoteDbl },
90 { "numbersign", Qt::Key_NumberSign },
91 { "dollar", Qt::Key_Dollar },
92 { "percent", Qt::Key_Percent },
93 { "ampersand", Qt::Key_Ampersand },
94 { "apostrophe", Qt::Key_Apostrophe },
95 { "parenleft", Qt::Key_ParenLeft },
96 { "parenright", Qt::Key_ParenRight },
97 { "asterisk", Qt::Key_Asterisk },
98 { "plus", Qt::Key_Plus },
99 { "comma", Qt::Key_Comma },
100 { "minus", Qt::Key_Minus },
101 { "period", Qt::Key_Period },
102 { "slash", Qt::Key_Slash },
103 { "zero", Qt::Key_0 },
104 { "one", Qt::Key_1 },
105 { "two", Qt::Key_2 },
106 { "three", Qt::Key_3 },
107 { "four", Qt::Key_4 },
108 { "five", Qt::Key_5 },
109 { "six", Qt::Key_6 },
110 { "seven", Qt::Key_7 },
111 { "eight", Qt::Key_8 },
112 { "nine", Qt::Key_9 },
113 { "colon", Qt::Key_Colon },
114 { "semicolon", Qt::Key_Semicolon },
115 { "less", Qt::Key_Less },
116 { "equal", Qt::Key_Equal },
117 { "greater", Qt::Key_Greater },
118 { "question", Qt::Key_Question },
119 { "at", Qt::Key_At },
120 { "bracketleft", Qt::Key_BracketLeft },
121 { "backslash", Qt::Key_Backslash },
122 { "bracketright", Qt::Key_BracketRight },
123 { "asciicircum", Qt::Key_AsciiCircum },
124 { "underscore", Qt::Key_Underscore },
125 { "grave", Qt::Key_QuoteLeft },
126 { "braceleft", Qt::Key_BraceLeft },
127 { "bar", Qt::Key_Bar },
128 { "braceright", Qt::Key_BraceRight },
129 { "asciitilde", Qt::Key_AsciiTilde },
130 { "nobreakspace", Qt::Key_nobreakspace },
131 { "exclamdown", Qt::Key_exclamdown },
132 { "cent", Qt::Key_cent },
133 { "sterling", Qt::Key_sterling },
134 { "currency", Qt::Key_currency },
135 { "yen", Qt::Key_yen },
136 { "brokenbar", Qt::Key_brokenbar },
137 { "section", Qt::Key_section },
138 { "diaeresis", Qt::Key_diaeresis },
139 { "copyright", Qt::Key_copyright },
140 { "ordfeminine", Qt::Key_ordfeminine },
141 { "guillemotleft", Qt::Key_guillemotleft },
142 { "notsign", Qt::Key_notsign },
143 { "hyphen", Qt::Key_hyphen },
144 { "registered", Qt::Key_registered },
145 { "macron", Qt::Key_macron },
146 { "degree", Qt::Key_degree },
147 { "plusminus", Qt::Key_plusminus },
148 { "twosuperior", Qt::Key_twosuperior },
149 { "threesuperior", Qt::Key_threesuperior },
150 { "acute", Qt::Key_acute },
151 { "mu", Qt::Key_mu },
152 { "paragraph", Qt::Key_paragraph },
153 { "periodcentered", Qt::Key_periodcentered },
154 { "cedilla", Qt::Key_cedilla },
155 { "onesuperior", Qt::Key_onesuperior },
156 { "masculine", Qt::Key_masculine },
157 { "guillemotright", Qt::Key_guillemotright },
158 { "onequarter", Qt::Key_onequarter },
159 { "onehalf", Qt::Key_onehalf },
160 { "threequarters", Qt::Key_threequarters },
161 { "questiondown", Qt::Key_questiondown },
162 { "Agrave", Qt::Key_Agrave },
163 { "Aacute", Qt::Key_Aacute },
164 { "Acircumflex", Qt::Key_Acircumflex },
165 { "Atilde", Qt::Key_Atilde },
166 { "Adiaeresis", Qt::Key_Adiaeresis },
167 { "Aring", Qt::Key_Aring },
168 { "AE", Qt::Key_AE },
169 { "Ccedilla", Qt::Key_Ccedilla },
170 { "Egrave", Qt::Key_Egrave },
171 { "Eacute", Qt::Key_Eacute },
172 { "Ecircumflex", Qt::Key_Ecircumflex },
173 { "Ediaeresis", Qt::Key_Ediaeresis },
174 { "Igrave", Qt::Key_Igrave },
175 { "Iacute", Qt::Key_Iacute },
176 { "Icircumflex", Qt::Key_Icircumflex },
177 { "Idiaeresis", Qt::Key_Idiaeresis },
178 { "ETH", Qt::Key_ETH },
179 { "Ntilde", Qt::Key_Ntilde },
180 { "Ograve", Qt::Key_Ograve },
181 { "Oacute", Qt::Key_Oacute },
182 { "Ocircumflex", Qt::Key_Ocircumflex },
183 { "Otilde", Qt::Key_Otilde },
184 { "Odiaeresis", Qt::Key_Odiaeresis },
185 { "multiply", Qt::Key_multiply },
186 { "Ooblique", Qt::Key_Ooblique },
187 { "Ugrave", Qt::Key_Ugrave },
188 { "Uacute", Qt::Key_Uacute },
189 { "Ucircumflex", Qt::Key_Ucircumflex },
190 { "Udiaeresis", Qt::Key_Udiaeresis },
191 { "Yacute", Qt::Key_Yacute },
192 { "THORN", Qt::Key_THORN },
193 { "ssharp", Qt::Key_ssharp },
194
195 { "agrave", 0xe0 /*Qt::Key_agrave*/ },
196 { "aacute", 0xe1 /*Qt::Key_aacute*/ },
197 { "acircumflex", 0xe2 /*Qt::Key_acircumflex*/ },
198 { "atilde", 0xe3 /*Qt::Key_atilde*/ },
199 { "adiaeresis", 0xe4 /*Qt::Key_adiaeresis*/ },
200 { "aring", 0xe5 /*Qt::Key_aring*/ },
201 { "ae", 0xe6 /*Qt::Key_ae*/ },
202 { "ccedilla", 0xe7 /*Qt::Key_ccedilla*/ },
203 { "egrave", 0xe8 /*Qt::Key_egrave*/ },
204 { "eacute", 0xe9 /*Qt::Key_eacute*/ },
205 { "ecircumflex", 0xea /*Qt::Key_ecircumflex*/ },
206 { "ediaeresis", 0xeb /*Qt::Key_ediaeresis*/ },
207 { "igrave", 0xec /*Qt::Key_igrave*/ },
208 { "iacute", 0xed /*Qt::Key_iacute*/ },
209 { "icircumflex", 0xee /*Qt::Key_icircumflex*/ },
210 { "idiaeresis", 0xef /*Qt::Key_idiaeresis*/ },
211 { "eth", 0xf0 /*Qt::Key_eth*/ },
212 { "ntilde", 0xf1 /*Qt::Key_ntilde*/ },
213 { "ograve", 0xf2 /*Qt::Key_ograve*/ },
214 { "oacute", 0xf3 /*Qt::Key_oacute*/ },
215 { "ocircumflex", 0xf4 /*Qt::Key_ocircumflex*/ },
216 { "otilde", 0xf5 /*Qt::Key_otilde*/ },
217 { "odiaeresis", 0xf6 /*Qt::Key_odiaeresis*/ },
218 { "division", Qt::Key_division },
219 { "oslash", 0xf8 /*Qt::Key_oslash*/ },
220 { "ugrave", 0xf9 /*Qt::Key_ugrave*/ },
221 { "uacute", 0xfa /*Qt::Key_uacute*/ },
222 { "ucircumflex", 0xfb /*Qt::Key_ucircumflex*/ },
223 { "udiaeresis", 0xfc /*Qt::Key_udiaeresis*/ },
224 { "yacute", 0xfd /*Qt::Key_yacute*/ },
225 { "thorn", 0xfe /*Qt::Key_thorn*/ },
226 { "ydiaeresis", Qt::Key_ydiaeresis },
227
228 { "F1", Qt::Key_F1 },
229 { "F2", Qt::Key_F2 },
230 { "F3", Qt::Key_F3 },
231 { "F4", Qt::Key_F4 },
232 { "F5", Qt::Key_F5 },
233 { "F6", Qt::Key_F6 },
234 { "F7", Qt::Key_F7 },
235 { "F8", Qt::Key_F8 },
236 { "F9", Qt::Key_F9 },
237 { "F10", Qt::Key_F10 },
238 { "F11", Qt::Key_F11 },
239 { "F12", Qt::Key_F12 },
240 { "F13", Qt::Key_F13 },
241 { "F14", Qt::Key_F14 },
242 { "F15", Qt::Key_F15 },
243 { "F16", Qt::Key_F16 },
244 { "F17", Qt::Key_F17 },
245 { "F18", Qt::Key_F18 },
246 { "F19", Qt::Key_F19 },
247 { "F20", Qt::Key_F20 },
248 { "F21", Qt::Key_F21 },
249 { "F22", Qt::Key_F22 },
250 { "F23", Qt::Key_F23 },
251 { "F24", Qt::Key_F24 },
252 { "F25", Qt::Key_F25 },
253 { "F26", Qt::Key_F26 },
254 { "F27", Qt::Key_F27 },
255 { "F28", Qt::Key_F28 },
256 { "F29", Qt::Key_F29 },
257 { "F30", Qt::Key_F30 },
258 { "F31", Qt::Key_F31 },
259 { "F32", Qt::Key_F32 },
260 { "F33", Qt::Key_F33 },
261 { "F34", Qt::Key_F34 },
262 { "F35", Qt::Key_F35 },
263
264 { "BackSpace", Qt::Key_Backspace },
265 { "Tab", Qt::Key_Tab },
266 { "Escape", Qt::Key_Escape },
267 { "Delete", Qt::Key_Backspace }, // what's the difference between "Delete" and "BackSpace"??
268 { "Return", Qt::Key_Return },
269 { "Break", Qt::Key_unknown }, //TODO: why doesn't Qt support the 'Break' key?
270 { "Caps_Lock", Qt::Key_CapsLock },
271 { "Num_Lock", Qt::Key_NumLock },
272 { "Scroll_Lock", Qt::Key_ScrollLock },
273 { "Caps_On", Qt::Key_CapsLock },
274 { "Compose", Qt::Key_Multi_key },
275 { "Bare_Num_Lock", Qt::Key_NumLock },
276 { "Find", Qt::Key_Home },
277 { "Insert", Qt::Key_Insert },
278 { "Remove", Qt::Key_Delete },
279 { "Select", Qt::Key_End },
280 { "Prior", Qt::Key_PageUp },
281 { "Next", Qt::Key_PageDown },
282 { "Help", Qt::Key_Help },
283 { "Pause", Qt::Key_Pause },
284
285 { "KP_0", Qt::Key_0 | Qt::KeypadModifier },
286 { "KP_1", Qt::Key_1 | Qt::KeypadModifier },
287 { "KP_2", Qt::Key_2 | Qt::KeypadModifier },
288 { "KP_3", Qt::Key_3 | Qt::KeypadModifier },
289 { "KP_4", Qt::Key_4 | Qt::KeypadModifier },
290 { "KP_5", Qt::Key_5 | Qt::KeypadModifier },
291 { "KP_6", Qt::Key_6 | Qt::KeypadModifier },
292 { "KP_7", Qt::Key_7 | Qt::KeypadModifier },
293 { "KP_8", Qt::Key_8 | Qt::KeypadModifier },
294 { "KP_9", Qt::Key_9 | Qt::KeypadModifier },
295 { "KP_Add", Qt::Key_Plus | Qt::KeypadModifier },
296 { "KP_Subtract", Qt::Key_Minus | Qt::KeypadModifier },
297 { "KP_Multiply", Qt::Key_Asterisk | Qt::KeypadModifier },
298 { "KP_Divide", Qt::Key_Slash | Qt::KeypadModifier },
299 { "KP_Enter", Qt::Key_Enter | Qt::KeypadModifier },
300 { "KP_Comma", Qt::Key_Comma | Qt::KeypadModifier },
301 { "KP_Period", Qt::Key_Period | Qt::KeypadModifier },
302 { "KP_MinPlus", Qt::Key_plusminus | Qt::KeypadModifier },
303
304 { "dead_grave", Qt::Key_Dead_Grave },
305 { "dead_acute", Qt::Key_Dead_Acute },
306 { "dead_circumflex", Qt::Key_Dead_Circumflex },
307 { "dead_tilde", Qt::Key_Dead_Tilde },
308 { "dead_diaeresis", Qt::Key_Dead_Diaeresis },
309 { "dead_cedilla", Qt::Key_Dead_Cedilla },
310
311 { "Down", Qt::Key_Down },
312 { "Left", Qt::Key_Left },
313 { "Right", Qt::Key_Right },
314 { "Up", Qt::Key_Up },
315 { "Shift", Qt::Key_Shift },
316 { "AltGr", Qt::Key_AltGr },
317 { "Control", Qt::Key_Control },
318 { "Alt", Qt::Key_Alt },
319 { "ShiftL", Qt::Key_Shift },
320 { "ShiftR", Qt::Key_Shift },
321 { "CtrlL", Qt::Key_Control },
322 { "CtrlR", Qt::Key_Control },
323};
324
325static const int symbol_map_size = sizeof(symbol_map)/sizeof(symbol_map_t);
326
327
328struct symbol_dead_unicode_t {
329 quint32 dead;
330 quint16 unicode;
331};
332
333static const symbol_dead_unicode_t symbol_dead_unicode[] = {
334 { Qt::Key_Dead_Grave, '`' },
335 { Qt::Key_Dead_Acute, '\'' },
336 { Qt::Key_Dead_Circumflex, '^' },
337 { Qt::Key_Dead_Tilde, '~' },
338 { Qt::Key_Dead_Diaeresis, '"' },
339 { Qt::Key_Dead_Cedilla, ',' },
340};
341
342static const int symbol_dead_unicode_size = sizeof(symbol_dead_unicode)/sizeof(symbol_dead_unicode_t);
343
344
345struct symbol_synonyms_t {
346 const char *from;
347 const char *to;
348};
349
350static const symbol_synonyms_t symbol_synonyms[] = {
351 { "Control_h", "BackSpace" },
352 { "Control_i", "Tab" },
353 { "Control_j", "Linefeed" },
354 { "Home", "Find" },
355 { "End", "Select" },
356 { "PageUp", "Prior" },
357 { "PageDown", "Next" },
358 { "multiplication", "multiply" },
359 { "pound", "sterling" },
360 { "pilcrow", "paragraph" },
361 { "Oslash", "Ooblique" },
362 { "Shift_L", "ShiftL" },
363 { "Shift_R", "ShiftR" },
364 { "Control_L", "CtrlL" },
365 { "Control_R", "CtrlR" },
366 { "AltL", "Alt" },
367 { "AltR", "AltGr" },
368 { "Alt_L", "Alt" },
369 { "Alt_R", "AltGr" },
370 { "AltGr_L", "Alt" },
371 { "AltGr_R", "AltGr" },
372 { "tilde", "asciitilde" },
373 { "circumflex", "asciicircum" },
374 { "dead_ogonek", "dead_cedilla" },
375 { "dead_caron", "dead_circumflex" },
376 { "dead_breve", "dead_tilde" },
377 { "dead_doubleacute", "dead_tilde" },
378 { "no-break_space", "nobreakspace" },
379 { "paragraph_sign", "section" },
380 { "soft_hyphen", "hyphen" },
381 { "rightanglequote", "guillemotright" },
382};
383
384static const int symbol_synonyms_size = sizeof(symbol_synonyms)/sizeof(symbol_synonyms_t);
385
386// makes the generated array in --header mode a bit more human readable
387QT_BEGIN_NAMESPACE
388static bool operator<(const QWSKeyboard::Mapping &m1, const QWSKeyboard::Mapping &m2)
389{
390 return m1.keycode != m2.keycode ? m1.keycode < m2.keycode : m1.modifiers < m2.modifiers;
391}
392QT_END_NAMESPACE
393
394class KeymapParser {
395public:
396 KeymapParser();
397 ~KeymapParser();
398
399 bool parseKmap(QFile *kmap);
400 bool generateQmap(QFile *qmap);
401 bool generateHeader(QFile *qmap);
402
403 int parseWarningCount() const { return m_warning_count; }
404
405private:
406 bool parseSymbol(const QByteArray &str, const QTextCodec *codec, quint16 &unicode, quint32 &qtcode, quint8 &flags, quint16 &special);
407 bool parseCompose(const QByteArray &str, const QTextCodec *codec, quint16 &unicode);
408 bool parseModifier(const QByteArray &str, quint8 &modifier);
409
410 void updateMapping(quint16 keycode = 0, quint8 modifiers = 0, quint16 unicode = 0xffff, quint32 qtcode = Qt::Key_unknown, quint8 flags = 0, quint16 = 0);
411
412 static quint32 toQtModifiers(quint8 modifiers);
413 static QList<QByteArray> tokenize(const QByteArray &line);
414
415
416private:
417 QList<QWSKeyboard::Mapping> m_keymap;
418 QList<QWSKeyboard::Composing> m_keycompose;
419
420 int m_warning_count;
421};
422
423
424
425int main(int argc, char **argv)
426{
427 int header = 0;
428 if (argc >= 2 && !qstrcmp(argv[1], "--header"))
429 header = 1;
430
431 if (argc < (3 + header)) {
432 fprintf(stderr, "Usage: kmap2qmap [--header] <kmap> [<additional kmaps> ...] <qmap>\n");
433 fprintf(stderr, " --header can be used to generate Qt's default compiled in qmap.\n");
434 return 1;
435 }
436
437 QVector<QFile *> kmaps(argc - header - 2);
438 for (int i = 0; i < kmaps.size(); ++i) {
439 kmaps [i] = new QFile(QString::fromLocal8Bit(argv[i + 1 + header]));
440
441 if (!kmaps[i]->open(QIODevice::ReadOnly)) {
442 fprintf(stderr, "Could not read from '%s'.\n", argv[i + 1 + header]);
443 return 2;
444 }
445 }
446 QFile *qmap = new QFile(QString::fromLocal8Bit(argv[argc - 1]));
447
448 if (!qmap->open(QIODevice::WriteOnly)) {
449 fprintf(stderr, "Could not write to '%s'.\n", argv[argc - 1]);
450 return 3;
451 }
452
453 KeymapParser p;
454
455 for (int i = 0; i < kmaps.size(); ++i) {
456 if (!p.parseKmap(kmaps[i])) {
457 fprintf(stderr, "Parsing kmap '%s' failed.\n", qPrintable(kmaps[i]->fileName()));
458 return 4;
459 }
460 }
461
462 if (p.parseWarningCount()) {
463 fprintf(stderr, "\nParsing the specified keymap(s) produced %d warning(s).\n" \
464 "Your generated qmap might not be complete.\n", \
465 p.parseWarningCount());
466 }
467 if (!(header ? p.generateHeader(qmap) : p.generateQmap(qmap))) {
468 fprintf(stderr, "Generating the qmap failed.\n");
469 return 5;
470 }
471
472 qDeleteAll(kmaps);
473 delete qmap;
474
475 return 0;
476}
477
478
479KeymapParser::KeymapParser()
480 : m_warning_count(0)
481{ }
482
483
484KeymapParser::~KeymapParser()
485{ }
486
487
488bool KeymapParser::generateHeader(QFile *f)
489{
490 QTextStream ts(f);
491
492 ts << "#ifndef QWSKEYBOARDHANDLER_DEFAULTMAP_H" << endl;
493 ts << "#define QWSKEYBOARDHANDLER_DEFAULTMAP_H" << endl << endl;
494
495 ts << "const QWSKeyboard::Mapping QWSKbPrivate::s_keymap_default[] = {" << endl;
496
497 for (int i = 0; i < m_keymap.size(); ++i) {
498 const QWSKeyboard::Mapping &m = m_keymap.at(i);
499 QString s;
500 s.sprintf(" { %3d, 0x%04x, 0x%08x, 0x%02x, 0x%02x, 0x%04x },\n", m.keycode, m.unicode, m.qtcode, m.modifiers, m.flags, m.special);
501 ts << s;
502 }
503
504 ts << "};" << endl << endl;
505
506 ts << "const QWSKeyboard::Composing QWSKbPrivate::s_keycompose_default[] = {" << endl;
507
508 for (int i = 0; i < m_keycompose.size(); ++i) {
509 const QWSKeyboard::Composing &c = m_keycompose.at(i);
510 QString s;
511 s.sprintf(" { 0x%04x, 0x%04x, 0x%04x },\n", c.first, c.second, c.result);
512 ts << s;
513 }
514 ts << "};" << endl << endl;
515
516 ts << "#endif" << endl;
517
518 return (ts.status() == QTextStream::Ok);
519}
520
521
522bool KeymapParser::generateQmap(QFile *f)
523{
524 QDataStream ds(f);
525
526 ds << quint32(QWSKeyboard::FileMagic) << quint32(1 /* version */) << quint32(m_keymap.size()) << quint32(m_keycompose.size());
527
528 if (ds.status() != QDataStream::Ok)
529 return false;
530
531 for (int i = 0; i < m_keymap.size(); ++i)
532 ds << m_keymap[i];
533
534 for (int i = 0; i < m_keycompose.size(); ++i)
535 ds << m_keycompose[i];
536
537 return (ds.status() == QDataStream::Ok);
538}
539
540
541QList<QByteArray> KeymapParser::tokenize(const QByteArray &line)
542{
543 bool quoted = false, separator = true;
544 QList<QByteArray> result;
545 QByteArray token;
546
547 for (int i = 0; i < line.length(); ++i) {
548 QChar c = line.at(i);
549
550 if (!quoted && c == '#' && separator)
551 break;
552 else if (!quoted && c == '"' && separator)
553 quoted = true;
554 else if (quoted && c == '"')
555 quoted = false;
556 else if (!quoted && c.isSpace()) {
557 separator = true;
558 if (!token.isEmpty()) {
559 result.append(token);
560 token.truncate(0);
561 }
562 }
563 else {
564 separator = false;
565 token.append(c);
566 }
567 }
568 if (!token.isEmpty())
569 result.append(token);
570 return result;
571}
572
573
574#define parseWarning(s) do { qWarning("Warning: keymap file '%s', line %d: %s", qPrintable(f->fileName()), lineno, s); ++m_warning_count; } while (false)
575
576bool KeymapParser::parseKmap(QFile *f)
577{
578 QByteArray line;
579 int lineno = 0;
580 QList<int> keymaps;
581 QTextCodec *codec = QTextCodec::codecForName("iso8859-1");
582
583 for (int i = 0; i <= 256; ++i)
584 keymaps << i;
585
586 while (!f->atEnd() && !f->error()) {
587 line = f->readLine();
588 lineno++;
589
590 QList<QByteArray> tokens = tokenize(line);
591
592 if (tokens.isEmpty())
593 continue;
594
595 if (tokens[0] == "keymaps") {
596 keymaps.clear();
597
598 if (tokens.count() > 1) {
599 foreach (const QByteArray &section, tokens[1].split(',')) {
600 int dashpos = section.indexOf('-');
601
602 //qWarning("Section %s", section.constData());
603 int end = section.mid(dashpos + 1).toInt();
604 int start = end;
605 if (dashpos > 0)
606 start = section.left(dashpos).toInt();
607
608 if (start <= end && start >=0 && end <= 256) {
609 for (int i = start; i <= end; ++i) {
610 //qWarning("appending keymap %d", i);
611 keymaps.append(i);
612 }
613 }
614 else
615 parseWarning("keymaps has an invalid range");
616 }
617 qSort(keymaps);
618 }
619 else
620 parseWarning("keymaps with more than one argument");
621 }
622 else if (tokens[0] == "alt_is_meta") {
623 // simply ignore it for now
624 }
625 else if (tokens[0] == "include") {
626 if (tokens.count() == 2) {
627 QString incname = QString::fromLocal8Bit(tokens[1]);
628 bool found = false;
629 QList<QDir> searchpath;
630 QFileInfo fi(*f);
631
632 if (!incname.endsWith(QLatin1String(".kmap")) && !incname.endsWith(QLatin1String(".inc")))
633 incname.append(QLatin1String(".inc"));
634
635 QDir d = fi.dir();
636 searchpath << d;
637 if (d.cdUp() && d.cd(QLatin1String("include")))
638 searchpath << d;
639 searchpath << QDir::current();
640
641 foreach (const QDir &path, searchpath) {
642 QFile f2(path.filePath(incname));
643 //qWarning(" -- trying to include %s", qPrintable(f2.fileName()));
644 if (f2.open(QIODevice::ReadOnly)) {
645 if (!parseKmap(&f2))
646 parseWarning("could not parse keymap include");
647 found = true;
648 }
649 }
650
651 if (!found)
652 parseWarning("could not locate keymap include");
653 } else
654 parseWarning("include doesn't have exactly one argument");
655 }
656 else if (tokens[0] == "charset") {
657 if (tokens.count() == 2) {
658 codec = QTextCodec::codecForName(tokens[1]);
659 if (!codec) {
660 parseWarning("could not parse codec definition");
661 codec = QTextCodec::codecForName("iso8859-1");
662 }
663 } else
664 parseWarning("codec doesn't habe exactly one argument");
665 }
666 else if (tokens[0] == "strings") {
667 // simply ignore those - they have no meaning for QWS
668 }
669 else if (tokens[0] == "compose") {
670 if (tokens.count() == 5 && tokens[3] == "to") {
671 QWSKeyboard::Composing c = { 0xffff, 0xffff, 0xffff };
672
673 if (!parseCompose(tokens[1], codec, c.first))
674 parseWarning("could not parse first compose symbol");
675 if (!parseCompose(tokens[2], codec, c.second))
676 parseWarning("could not parse second compose symbol");
677 if (!parseCompose(tokens[4], codec, c.result))
678 parseWarning("could not parse resulting compose symbol");
679
680 if (c.first != 0xffff && c.second != 0xffff && c.result != 0xffff) {
681 m_keycompose << c;
682 }
683 } else
684 parseWarning("non-standard compose line");
685 }
686 else {
687 int kcpos = tokens.indexOf("keycode");
688
689 if (kcpos >= 0 && kcpos < (tokens.count()-3) && tokens[kcpos+2] == "=") {
690 quint16 keycode = tokens[kcpos+1].toInt();
691
692 if (keycode <= 0 || keycode > 0x1ff /* KEY_MAX */) {
693 parseWarning("keycode out of range [0..0x1ff]");
694 break;
695 }
696
697 bool line_modifiers = (kcpos > 0);
698
699 quint8 modifiers = 0; //, modifiers_mask = 0xff;
700 for (int i = 0; i < kcpos; ++i) {
701 quint8 mod;
702 if (!parseModifier(tokens[i], mod)) {
703 parseWarning("unknown modifier prefix for keycode");
704 continue;
705 }
706 modifiers |= mod;
707 }
708
709 int kccount = tokens.count() - kcpos - 3; // 3 : 'keycode' 'X' '='
710
711 if (line_modifiers && kccount > 1) {
712 parseWarning("line has modifiers, but more than one keycode");
713 break;
714 }
715
716 // only process one symbol when a prefix modifer was specified
717 for (int i = 0; i < (line_modifiers ? 1 : kccount); ++i) {
718 if (!line_modifiers)
719 modifiers = keymaps[i];
720
721 quint32 qtcode;
722 quint16 unicode;
723 quint16 special;
724 quint8 flags;
725 if (!parseSymbol(tokens[i + kcpos + 3], codec, unicode, qtcode, flags, special)) {
726 parseWarning((QByteArray("symbol could not be parsed: ") + tokens[i + kcpos + 3]).constData());
727 break;
728 }
729
730 if (qtcode == Qt::Key_unknown && unicode == 0xffff) // VoidSymbol
731 continue;
732
733 if (!line_modifiers && kccount == 1) {
734 if ((unicode >= 'A' && unicode <= 'Z') || (unicode >= 'a' && unicode <= 'z')) {
735 quint16 other_unicode = (unicode >= 'A' && unicode <= 'Z') ? unicode - 'A' + 'a' : unicode - 'a' + 'A';
736 quint16 lower_unicode = (unicode >= 'A' && unicode <= 'Z') ? unicode - 'A' + 'a' : unicode;
737
738 // a single a-z|A-Z value results in a very flags mapping: see below
739
740 updateMapping(keycode, QWSKeyboard::ModPlain, unicode, qtcode, flags, 0);
741
742 updateMapping(keycode, QWSKeyboard::ModShift, other_unicode, qtcode, flags, 0);
743
744 updateMapping(keycode, QWSKeyboard::ModAltGr, unicode, qtcode, flags, 0);
745 updateMapping(keycode, QWSKeyboard::ModAltGr | QWSKeyboard::ModShift, other_unicode, qtcode, flags, 0);
746
747 updateMapping(keycode, QWSKeyboard::ModControl, lower_unicode, qtcode | Qt::ControlModifier, flags, 0);
748 updateMapping(keycode, QWSKeyboard::ModControl | QWSKeyboard::ModShift, lower_unicode, qtcode | Qt::ControlModifier, flags, 0);
749 updateMapping(keycode, QWSKeyboard::ModControl | QWSKeyboard::ModAltGr, lower_unicode, qtcode | Qt::ControlModifier, flags, 0);
750 updateMapping(keycode, QWSKeyboard::ModControl | QWSKeyboard::ModAltGr | QWSKeyboard::ModShift, lower_unicode, qtcode | Qt::ControlModifier, flags, 0);
751
752 updateMapping(keycode, QWSKeyboard::ModAlt, unicode, qtcode | Qt::AltModifier, flags, 0);
753 updateMapping(keycode, QWSKeyboard::ModAlt | QWSKeyboard::ModShift, unicode, qtcode | Qt::AltModifier, flags, 0);
754 updateMapping(keycode, QWSKeyboard::ModAlt | QWSKeyboard::ModAltGr, unicode, qtcode | Qt::AltModifier, flags, 0);
755 updateMapping(keycode, QWSKeyboard::ModAlt | QWSKeyboard::ModAltGr | QWSKeyboard::ModShift, unicode, qtcode | Qt::AltModifier, flags, 0);
756
757 updateMapping(keycode, QWSKeyboard::ModAlt | QWSKeyboard::ModControl, lower_unicode, qtcode | Qt::ControlModifier | Qt::AltModifier, flags, 0);
758 updateMapping(keycode, QWSKeyboard::ModAlt | QWSKeyboard::ModControl | QWSKeyboard::ModShift, lower_unicode, qtcode | Qt::ControlModifier | Qt::AltModifier, flags, 0);
759 updateMapping(keycode, QWSKeyboard::ModAlt | QWSKeyboard::ModControl | QWSKeyboard::ModAltGr, lower_unicode, qtcode | Qt::ControlModifier | Qt::AltModifier, flags, 0);
760 updateMapping(keycode, QWSKeyboard::ModAlt | QWSKeyboard::ModControl | QWSKeyboard::ModAltGr | QWSKeyboard::ModShift, lower_unicode, qtcode | Qt::ControlModifier | Qt::AltModifier, flags, 0);
761 }
762 else {
763 // a single value results in that mapping regardless of the modifier
764 //for (int mod = 0; mod <= 255; ++mod)
765 // updateMapping(keycode, quint8(mod), unicode, qtcode | toQtModifiers(mod), flags, special);
766
767 // we can save a lot of space in the qmap, since we do that anyway in the kbd handler:
768 updateMapping(keycode, QWSKeyboard::ModPlain, unicode, qtcode, flags, special);
769 }
770 }
771 else {
772 // "normal" mapping
773 updateMapping(keycode, modifiers, unicode, qtcode, flags, special);
774 }
775 }
776 }
777 }
778 }
779 qSort(m_keymap);
780 return !m_keymap.isEmpty();
781}
782
783
784
785void KeymapParser::updateMapping(quint16 keycode, quint8 modifiers, quint16 unicode, quint32 qtcode, quint8 flags, quint16 special)
786{
787 for (int i = 0; i < m_keymap.size(); ++i) {
788 QWSKeyboard::Mapping &m = m_keymap[i];
789
790 if (m.keycode == keycode && m.modifiers == modifiers) {
791 m.unicode = unicode;
792 m.qtcode = qtcode;
793 m.flags = flags;
794 m.special = special;
795 return;
796 }
797 }
798 QWSKeyboard::Mapping m = { keycode, unicode, qtcode, modifiers, flags, special };
799 m_keymap << m;
800}
801
802
803quint32 KeymapParser::toQtModifiers(quint8 modifiers)
804{
805 quint32 qtmodifiers = Qt::NoModifier;
806
807 for (int i = 0; i < modifier_map_size; ++i) {
808 if (modifiers & modifier_map[i].modifier)
809 qtmodifiers |= modifier_map[i].qtmodifier;
810 }
811 return qtmodifiers;
812}
813
814
815bool KeymapParser::parseModifier(const QByteArray &str, quint8 &modifier)
816{
817 QByteArray lstr = str.toLower();
818
819 for (int i = 0; i < modifier_map_size; ++i) {
820 if (lstr == modifier_map[i].symbol) {
821 modifier = modifier_map[i].modifier;
822 return true;
823 }
824 }
825 return false;
826}
827
828
829bool KeymapParser::parseCompose(const QByteArray &str, const QTextCodec *codec, quint16 &unicode)
830{
831 if (str == "'\\''") {
832 unicode = '\'';
833 return true;
834 } else if (str.length() == 3 && str.startsWith('\'') && str.endsWith('\'')) {
835 QString temp = codec->toUnicode(str.constData() + 1, str.length() - 2);
836 if (temp.length() != 1)
837 return false;
838 unicode = temp[0].unicode();
839 return true;
840 } else {
841 quint32 code = str.toUInt();
842 if (code > 255)
843 return false;
844 char c[2];
845 c[0] = char(code);
846 c[1] = 0;
847 QString temp = codec->toUnicode(c);
848 if (temp.length() != 1)
849 return false;
850 unicode = temp[0].unicode();
851 return true;
852 }
853}
854
855
856bool KeymapParser::parseSymbol(const QByteArray &str, const QTextCodec * /*codec*/, quint16 &unicode, quint32 &qtcode, quint8 &flags, quint16 &special)
857{
858 flags = (str[0] == '+') ? QWSKeyboard::IsLetter : 0;
859 QByteArray sym = (flags & QWSKeyboard::IsLetter) ? str.right(str.length() - 1) : str;
860
861 special = 0;
862 qtcode = Qt::Key_unknown;
863 unicode = 0xffff;
864
865 if (sym == "VoidSymbol" || sym == "nul")
866 return true;
867
868 bool try_to_find_qtcode = false;
869
870 if (sym[0] >= '0' && sym[0] <= '9') { // kernel internal action number
871 return false;
872 } else if (sym.length() == 6 && sym[1] == '+' && (sym[0] == 'U' || sym[0] == 'u')) { // unicode
873 bool ok;
874 unicode = sym.mid(2).toUInt(&ok, 16);
875 if (!ok)
876 return false;
877 try_to_find_qtcode = true;
878 } else { // symbolic
879 for (int i = 0; i < symbol_synonyms_size; ++i) {
880 if (sym == symbol_synonyms[i].from) {
881 sym = symbol_synonyms[i].to;
882 break;
883 }
884 }
885
886 quint32 qtmod = 0;
887
888 // parse prepended modifiers
889 forever {
890 int underpos = sym.indexOf('_');
891
892 if (underpos <= 0)
893 break;
894 QByteArray modsym = sym.left(underpos);
895 QByteArray nomodsym = sym.mid(underpos + 1);
896 quint8 modifier = 0;
897
898 if (!parseModifier(modsym, modifier))
899 break;
900
901 qtmod |= toQtModifiers(modifier);
902 sym = nomodsym;
903 }
904
905 if (qtcode == Qt::Key_unknown) {
906 quint8 modcode;
907 // check if symbol is a modifier
908 if (parseModifier(sym, modcode)) {
909 special = modcode;
910 flags |= QWSKeyboard::IsModifier;
911 }
912
913 // map symbol to Qt key code
914 for (int i = 0; i < symbol_map_size; ++i) {
915 if (sym == symbol_map[i].symbol) {
916 qtcode = symbol_map[i].qtcode;
917 break;
918 }
919 }
920
921 // a-zA-Z is not in the table to save space
922 if (qtcode == Qt::Key_unknown && sym.length() == 1) {
923 char letter = sym.at(0);
924
925 if (letter >= 'a' && letter <= 'z') {
926 qtcode = Qt::Key_A + letter - 'a';
927 unicode = letter;
928 }
929 else if (letter >= 'A' && letter <= 'Z') {
930 qtcode = Qt::Key_A + letter - 'A';
931 unicode = letter;
932 }
933 }
934 // System keys
935 if (qtcode == Qt::Key_unknown) {
936 quint16 sys = 0;
937
938 if (sym == "Decr_Console") {
939 sys = QWSKeyboard::SystemConsolePrevious;
940 } else if (sym == "Incr_Console") {
941 sys = QWSKeyboard::SystemConsoleNext;
942 } else if (sym.startsWith("Console_")) {
943 int console = sym.mid(8).toInt() - 1;
944 if (console >= 0 && console <= (QWSKeyboard::SystemConsoleLast - QWSKeyboard::SystemConsoleFirst)) {
945 sys = QWSKeyboard::SystemConsoleFirst + console;
946 }
947 } else if (sym == "Boot") {
948 sys = QWSKeyboard::SystemReboot;
949 } else if (sym == "QtZap") {
950 sys = QWSKeyboard::SystemZap;
951 }
952
953 if (sys) {
954 flags |= QWSKeyboard::IsSystem;
955 special = sys;
956 qtcode = Qt::Key_Escape; // just a dummy
957 }
958 }
959
960 // map Qt key codes in the iso-8859-1 range to unicode
961 if (qtcode != Qt::Key_unknown && unicode == 0xffff) {
962 quint32 qtcode_no_mod = qtcode & ~(Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier | Qt::KeypadModifier);
963 if (qtcode_no_mod <= 0x000000ff) // iso-8859-1
964 unicode = quint16(qtcode_no_mod);
965 }
966
967 // flag dead keys
968 if (qtcode >= Qt::Key_Dead_Grave && qtcode <= Qt::Key_Dead_Horn) {
969 flags = QWSKeyboard::IsDead;
970
971 for (int i = 0; i < symbol_dead_unicode_size; ++i) {
972 if (symbol_dead_unicode[i].dead == qtcode) {
973 unicode = symbol_dead_unicode[i].unicode;
974 break;
975 }
976 }
977 }
978 }
979 if ((qtcode == Qt::Key_unknown) && (unicode == 0xffff))
980 return false;
981
982 qtcode |= qtmod;
983 }
984
985 // map unicode in the iso-8859-1 range to Qt key codes
986 if (unicode >= 0x0020 && unicode <= 0x00ff && qtcode == Qt::Key_unknown)
987 qtcode = unicode; // iso-8859-1
988
989 return true;
990}
991
Note: See TracBrowser for help on using the repository browser.