📄 qkeymapper_x11.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qkeymapper_p.h"#include "qdebug.h"#include "qtextcodec.h"#include "qwidget.h"#include "qapplication_p.h"#include "qevent_p.h"#include "qt_x11_p.h"#ifndef QT_NO_XKB# include <X11/XKBlib.h>#endif#define XK_MISCELLANY#define XK_LATIN1#define XK_KOREAN#define XK_XKB_KEYS#include <X11/keysymdef.h>#include <ctype.h>#ifndef QT_NO_XKB// bring in the auto-generated xkbLayoutData#include "qkeymapper_x11_p.cpp"static void getLocaleAndDirection(QLocale *locale, Qt::LayoutDirection *direction, const QByteArray &layoutName, const QByteArray &variantName){ int i = 0; while (xkbLayoutData[i].layout != 0) { if (layoutName == xkbLayoutData[i].layout && variantName == xkbLayoutData[i].variant) { *locale = QLocale(xkbLayoutData[i].language, xkbLayoutData[i].country); *direction = xkbLayoutData[i].direction; return; } ++i; } *locale = QLocale::c(); *direction = Qt::LeftToRight;}#endif // QT_NO_XKB// from qapplication_x11.cppextern uchar qt_alt_mask;extern uchar qt_meta_mask;extern uchar qt_super_mask;extern uchar qt_hyper_mask;extern uchar qt_mode_switch_mask;uchar qt_num_lock_mask = 0;extern bool qt_sendSpontaneousEvent(QObject*, QEvent*);// ### we should really resolve conflicts with other masks by// ### decomposing the Qt::KeyboardModifers in possibleKeys()#define SETMASK(sym, mask) \ do { \ if (qt_alt_mask == 0 \ && qt_meta_mask != mask \ && qt_super_mask != mask \ && qt_hyper_mask != mask \ && (sym == XK_Alt_L || sym == XK_Alt_R)) { \ qt_alt_mask = mask; \ } \ if (qt_meta_mask == 0 \ && qt_alt_mask != mask \ && qt_super_mask != mask \ && qt_hyper_mask != mask \ && (sym == XK_Meta_L || sym == XK_Meta_R)) { \ qt_meta_mask = mask; \ } \ if (qt_super_mask == 0 \ && qt_alt_mask != mask \ && qt_meta_mask != mask \ && qt_hyper_mask != mask \ && (sym == XK_Super_L || sym == XK_Super_R)) { \ qt_super_mask = mask; \ } \ if (qt_hyper_mask == 0 \ && qt_alt_mask != mask \ && qt_meta_mask != mask \ && qt_super_mask != mask \ && (sym == XK_Hyper_L || sym == XK_Hyper_R)) { \ qt_hyper_mask = mask; \ } \ if (qt_mode_switch_mask == 0 \ && qt_alt_mask != mask \ && qt_meta_mask != mask \ && qt_super_mask != mask \ && qt_hyper_mask != mask \ && sym == XK_Mode_switch) { \ qt_mode_switch_mask = mask; \ } \ if (qt_num_lock_mask == 0 \ && sym == XK_Num_Lock) { \ qt_num_lock_mask = mask; \ } \ } while(false)// qt_XTranslateKey() is based on _XTranslateKey() taken from:/* $Xorg: KeyBind.c,v 1.4 2001/02/09 02:03:34 xorgcvs Exp $ *//*Copyright 1985, 1987, 1998 The Open GroupPermission to use, copy, modify, distribute, and sell this software and itsdocumentation for any purpose is hereby granted without fee, provided thatthe above copyright notice appear in all copies and that both thatcopyright notice and this permission notice appear in supportingdocumentation.The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER INAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR INCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.Except as contained in this notice, the name of The Open Group shall not beused in advertising or otherwise to promote the sale, use or other dealingsin this Software without prior written authorization from The Open Group.*/static intqt_XTranslateKey(register QXCoreDesc *dpy, KeyCode keycode, register unsigned int modifiers, unsigned int *modifiers_return, KeySym *keysym_return){ int per; register KeySym *syms; KeySym sym, lsym, usym; if (! dpy->keysyms) return 0; *modifiers_return = ((ShiftMask|LockMask) | dpy->mode_switch | dpy->num_lock); if (((int)keycode < dpy->min_keycode) || ((int)keycode > dpy->max_keycode)) { *keysym_return = NoSymbol; return 1; } per = dpy->keysyms_per_keycode; syms = &dpy->keysyms[(keycode - dpy->min_keycode) * per]; while ((per > 2) && (syms[per - 1] == NoSymbol)) per--; if ((per > 2) && (modifiers & dpy->mode_switch)) { syms += 2; per -= 2; } if ((modifiers & dpy->num_lock) && (per > 1 && (IsKeypadKey(syms[1]) || IsPrivateKeypadKey(syms[1])))) { if ((modifiers & ShiftMask) || ((modifiers & LockMask) && (dpy->lock_meaning == XK_Shift_Lock))) *keysym_return = syms[0]; else *keysym_return = syms[1]; } else if (!(modifiers & ShiftMask) && (!(modifiers & LockMask) || (dpy->lock_meaning == NoSymbol))) { if ((per == 1) || (syms[1] == NoSymbol)) XConvertCase(syms[0], keysym_return, &usym); else *keysym_return = syms[0]; } else if (!(modifiers & LockMask) || (dpy->lock_meaning != XK_Caps_Lock)) { if ((per == 1) || ((usym = syms[1]) == NoSymbol)) XConvertCase(syms[0], &lsym, &usym); *keysym_return = usym; } else { if ((per == 1) || ((sym = syms[1]) == NoSymbol)) sym = syms[0]; XConvertCase(sym, &lsym, &usym); if (!(modifiers & ShiftMask) && (sym != syms[0]) && ((sym != usym) || (lsym == usym))) XConvertCase(syms[0], &lsym, &usym); *keysym_return = usym; } if (*keysym_return == XK_VoidSymbol) *keysym_return = NoSymbol; return 1;}QKeyMapperPrivate::QKeyMapperPrivate() : keyboardInputDirection(Qt::LeftToRight), useXKB(false){ memset(&coreDesc, 0, sizeof(coreDesc));#ifndef QT_NO_XKB int opcode = -1; int xkbEventBase = -1; int xkbErrorBase = -1; int xkblibMajor = XkbMajorVersion; int xkblibMinor = XkbMinorVersion; if (XkbQueryExtension(X11->display, &opcode, &xkbEventBase, &xkbErrorBase, &xkblibMajor, &xkblibMinor)) useXKB = true;#endif#if 0 qDebug() << "useXKB =" << useXKB;#endif}QKeyMapperPrivate::~QKeyMapperPrivate(){ if (coreDesc.keysyms) XFree(coreDesc.keysyms);}QList<int> QKeyMapperPrivate::possibleKeys(QKeyEvent *event){#ifndef QT_NO_XKB if (useXKB) return possibleKeysXKB(event);#endif return possibleKeysCore(event);}enum { MaxBits = sizeof(uint) * 8 };static QString translateKeySym(KeySym keysym, uint xmodifiers, int &code, Qt::KeyboardModifiers &modifiers, QByteArray &chars, int &count);QList<int> QKeyMapperPrivate::possibleKeysXKB(QKeyEvent *event){#ifndef QT_NO_XKB const int xkeycode = event->nativeScanCode(); const uint xmodifiers = event->nativeModifiers(); // first, translate key only using lock modifiers (there are no Qt equivalents for these, so we must // always use them when determining the baseKeySym) KeySym baseKeySym; uint consumedModifiers; if (!XkbLookupKeySym(X11->display, xkeycode, (xmodifiers & (LockMask | qt_num_lock_mask)), &consumedModifiers, &baseKeySym)) return QList<int>(); QList<int> result; // translate sym -> code Qt::KeyboardModifiers baseModifiers = 0; int baseCode = -1; QByteArray chars; int count = 0; QString text = translateKeySym(baseKeySym, xmodifiers, baseCode, baseModifiers, chars, count); if (baseCode == -1) { if (text.isEmpty()) return QList<int>(); baseCode = text.at(0).unicode(); } if (baseCode && baseCode < 0xfffe) baseCode = QChar(baseCode).toUpper().unicode(); result += (baseCode | baseModifiers); int pos1Bits[MaxBits]; int num1Bits = 0; for (int i = 0; i < MaxBits; ++i) { if (consumedModifiers & (1 << i)) pos1Bits[num1Bits++] = i; } const int numPerms = (1 << num1Bits); // translate the key again using each permutation of consumedModifiers for (int i = 1; i < numPerms; ++i) { uint val = 0; for (int j = 0; j < num1Bits; ++j) { if (i & (1 << j)) val |= (1 << pos1Bits[j]); } if ((xmodifiers & val) != val) continue; KeySym sym; uint mods; if (!XkbLookupKeySym(X11->display, xkeycode, val, &mods, &sym)) continue; // translate sym -> code Qt::KeyboardModifiers modifiers = 0; int code = -1; chars.clear(); count = 0; // mask out the modifiers needed to translate keycode text = translateKeySym(sym, xmodifiers & ~val, code, modifiers, chars, count); if (code == -1) { if (text.isEmpty()) continue; code = text.at(0).unicode(); } if (code && code < 0xfffe) code = QChar(code).toUpper().unicode(); if (code == baseCode) continue; result += (code | modifiers); }#if 0 qDebug() << "possibleKeysXKB()" << hex << result;#endif return result;#else Q_UNUSED(event); return QList<int>();#endif // QT_NO_XKB}QList<int> QKeyMapperPrivate::possibleKeysCore(QKeyEvent *event){ const int xkeycode = event->nativeScanCode(); const uint xmodifiers = event->nativeModifiers(); // first, translate key only using lock modifiers (there are no Qt equivalents for these, so we must // always use them when determining the baseKeySym) KeySym baseKeySym; uint consumedModifiers; if (!qt_XTranslateKey(&coreDesc, xkeycode, (xmodifiers & (LockMask | qt_num_lock_mask)), &consumedModifiers, &baseKeySym)) return QList<int>(); QList<int> result; // translate sym -> code Qt::KeyboardModifiers baseModifiers = 0; int baseCode = -1; QByteArray chars; int count = 0; QString text = translateKeySym(baseKeySym, xmodifiers, baseCode, baseModifiers, chars, count); if (baseCode == -1) { if (text.isEmpty()) return QList<int>(); baseCode = text.at(0).unicode(); } if (baseCode && baseCode < 0xfffe) baseCode = QChar(baseCode).toUpper().unicode(); result += (baseCode | baseModifiers); int pos1Bits[MaxBits]; int num1Bits = 0; for (int i = 0; i < MaxBits; ++i) { if (consumedModifiers & (1 << i)) pos1Bits[num1Bits++] = i; } const int numPerms = (1 << num1Bits);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -