kkeynative_x11.cpp
00001 /* 00002 Copyright (C) 2001 Ellis Whitehead <ellis@kde.org> 00003 00004 Win32 port: 00005 Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl> 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to 00019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00020 Boston, MA 02110-1301, USA. 00021 */ 00022 00023 #include <qnamespace.h> 00024 #include <qwindowdefs.h> 00025 00026 #if defined(Q_WS_X11) || defined(Q_WS_WIN) || defined(Q_WS_MACX) // Only compile this module if we're compiling for X11, mac or win32 00027 00028 #include "kkeynative.h" 00029 #include "kkeyserver_x11.h" 00030 00031 #include <qmap.h> 00032 #include <qstringlist.h> 00033 #include "kckey.h" 00034 #include <kdebug.h> 00035 #include <klocale.h> 00036 00037 #ifdef Q_WS_X11 00038 #define XK_MISCELLANY 00039 #define XK_XKB_KEYS 00040 #include <X11/X.h> 00041 #include <X11/Xlib.h> 00042 #include <X11/Xutil.h> 00043 #include <X11/keysymdef.h> 00044 #include <ctype.h> 00045 #endif 00046 00047 //--------------------------------------------------------------------- 00048 00049 static KKeyNative* gx_pkey = 0; 00050 00051 //--------------------------------------------------------------------- 00052 // KKeyNative 00053 //--------------------------------------------------------------------- 00054 00055 KKeyNative::KKeyNative() { clear(); } 00056 KKeyNative::KKeyNative( const KKey& key ) { init( key ); } 00057 KKeyNative::KKeyNative( const KKeyNative& key ) { init( key ); } 00058 #ifdef Q_WS_X11 00059 KKeyNative::KKeyNative( const XEvent* pEvent ) { init( pEvent ); } 00060 #endif 00061 00062 KKeyNative::KKeyNative( uint code, uint mod, uint sym ) 00063 { 00064 m_code = code; 00065 m_mod = mod; 00066 m_sym = sym; 00067 } 00068 00069 KKeyNative::~KKeyNative() 00070 { } 00071 00072 void KKeyNative::clear() 00073 { 00074 m_code = 0; 00075 m_mod = 0; 00076 m_sym = 0; 00077 } 00078 00079 #ifdef Q_WS_X11 00080 bool KKeyNative::init( const XEvent* pEvent ) 00081 { 00082 KeySym keySym; 00083 m_code = pEvent->xkey.keycode; 00084 m_mod = pEvent->xkey.state; 00085 XLookupString( (XKeyEvent*) pEvent, 0, 0, &keySym, 0 ); 00086 m_sym = (uint) keySym; 00087 return true; 00088 } 00089 #endif 00090 00091 bool KKeyNative::init( const KKey& key ) 00092 { 00093 #ifdef Q_WS_WIN 00094 m_sym = key.sym(); 00095 m_code = m_sym; //key.keyCodeQt(); 00096 m_mod = key.m_mod; 00097 #elif !defined(Q_WS_WIN) && !defined(Q_WS_MACX) 00098 // Get any extra mods required by the sym. 00099 // E.g., XK_Plus requires SHIFT on the en layout. 00100 m_sym = key.sym(); 00101 uint modExtra = KKeyServer::Sym(m_sym).getModsRequired(); 00102 // Get the X modifier equivalent. 00103 if( !m_sym || !KKeyServer::modToModX( key.modFlags() | modExtra, m_mod ) ) { 00104 m_sym = m_mod = 0; 00105 m_code = 0; 00106 return false; 00107 } 00108 00109 // XKeysymToKeycode returns the wrong keycode for XK_Print and XK_Break. 00110 // Specifically, it returns the code for SysReq instead of Print 00111 // Only do this for the default Xorg layout, other keycode mappings 00112 // (e.g. evdev) don't need or want it. 00113 if( m_sym == XK_Print && !(m_mod & Mod1Mask) && 00114 XKeycodeToKeysym( qt_xdisplay(), 111, 0 ) == XK_Print ) 00115 m_code = 111; // code for Print 00116 else if( m_sym == XK_Break || (m_sym == XK_Pause && (m_mod & ControlMask)) && 00117 XKeycodeToKeysym( qt_xdisplay(), 114, 0 ) == XK_Pause ) 00118 m_code = 114; 00119 else 00120 m_code = XKeysymToKeycode( qt_xdisplay(), m_sym ); 00121 00122 if( !m_code && m_sym ) 00123 kdDebug(125) << "Couldn't get code for sym" << endl; 00124 // Now get the true sym formed by the modifiers 00125 // E.g., Shift+Equal => Plus on the en layout. 00126 if( key.modFlags() && ( ( m_sym < XK_Home || m_sym > XK_Begin ) && 00127 m_sym != XK_Insert && m_sym != XK_Delete )) 00128 KKeyServer::codeXToSym( m_code, m_mod, m_sym ); 00129 #endif 00130 return true; 00131 } 00132 00133 bool KKeyNative::init( const KKeyNative& key ) 00134 { 00135 m_code = key.m_code; 00136 m_mod = key.m_mod; 00137 m_sym = key.m_sym; 00138 return true; 00139 } 00140 00141 uint KKeyNative::code() const { return m_code; } 00142 uint KKeyNative::mod() const { return m_mod; } 00143 uint KKeyNative::sym() const { return m_sym; } 00144 00145 bool KKeyNative::isNull() const 00146 { 00147 return m_sym == 0; 00148 } 00149 00150 int KKeyNative::compare( const KKeyNative& key ) const 00151 { 00152 if( m_sym != key.m_sym ) return m_sym - key.m_sym; 00153 if( m_mod != key.m_mod ) return m_mod - key.m_mod; 00154 if( m_code != key.m_code ) return m_code - key.m_code; 00155 return 0; 00156 } 00157 00158 KKeyNative& KKeyNative::null() 00159 { 00160 if( !gx_pkey ) 00161 gx_pkey = new KKeyNative; 00162 if( !gx_pkey->isNull() ) 00163 gx_pkey->clear(); 00164 return *gx_pkey; 00165 } 00166 00167 KKey KKeyNative::key() const 00168 { 00169 #ifdef Q_WS_WIN 00170 return KKey( m_sym, m_mod ); 00171 #else 00172 uint modSpec; 00173 if( KKeyServer::modXToMod( m_mod, modSpec ) ) 00174 return KKey( m_sym, modSpec ); 00175 else 00176 return KKey(); 00177 #endif 00178 } 00179 00180 int KKeyNative::keyCodeQt() const 00181 { 00182 int keyQt = KKeyServer::Sym(m_sym).qt(), modQt; 00183 00184 if( keyQt != Qt::Key_unknown && KKeyServer::modXToModQt( m_mod, modQt ) ) 00185 return keyQt | modQt; 00186 00187 return 0; 00188 } 00189 00190 bool KKeyNative::keyboardHasWinKey() { return KKeyServer::keyboardHasWinKey(); } 00191 00192 #ifdef Q_WS_X11 00193 uint KKeyNative::modX( KKey::ModFlag modFlag ) { return KKeyServer::modX( modFlag ); } 00194 uint KKeyNative::accelModMaskX() { return KKeyServer::accelModMaskX(); } 00195 uint KKeyNative::modXNumLock() { return KKeyServer::modXNumLock(); } 00196 uint KKeyNative::modXLock() { return KKeyServer::modXLock(); } 00197 uint KKeyNative::modXScrollLock() { return KKeyServer::modXScrollLock(); } 00198 uint KKeyNative::modXModeSwitch() { return KKeyServer::modXModeSwitch(); } 00199 #endif 00200 00201 #endif // Q_WS_X11