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     // FIXME: Accomadate non-standard layouts
00110     // XKeysymToKeycode returns the wrong keycode for XK_Print and XK_Break.
00111     // Specifically, it returns the code for SysReq instead of Print
00112     if( m_sym == XK_Print && !(m_mod & Mod1Mask) )
00113         m_code = 111; // code for Print
00114     else if( m_sym == XK_Break || (m_sym == XK_Pause && (m_mod & ControlMask)) )
00115         m_code = 114;
00116     else
00117         m_code = XKeysymToKeycode( qt_xdisplay(), m_sym );
00118 
00119     if( !m_code && m_sym )
00120         kdDebug(125) << "Couldn't get code for sym" << endl;
00121     // Now get the true sym formed by the modifiers
00122     //  E.g., Shift+Equal => Plus on the en layout.
00123     if( key.modFlags() && ( ( m_sym < XK_Home || m_sym > XK_Begin ) && 
00124                   m_sym != XK_Insert && m_sym != XK_Delete ))
00125         KKeyServer::codeXToSym( m_code, m_mod, m_sym );
00126 #endif
00127     return true;
00128 }
00129 
00130 bool KKeyNative::init( const KKeyNative& key )
00131 {
00132     m_code = key.m_code;
00133     m_mod = key.m_mod;
00134     m_sym = key.m_sym;
00135     return true;
00136 }
00137 
00138 uint KKeyNative::code() const { return m_code; }
00139 uint KKeyNative::mod() const  { return m_mod; }
00140 uint KKeyNative::sym() const  { return m_sym; }
00141 
00142 bool KKeyNative::isNull() const
00143 {
00144     return m_sym == 0;
00145 }
00146 
00147 int KKeyNative::compare( const KKeyNative& key ) const
00148 {
00149     if( m_sym != key.m_sym )   return m_sym - key.m_sym;
00150     if( m_mod != key.m_mod )   return m_mod - key.m_mod;
00151     if( m_code != key.m_code ) return m_code - key.m_code;
00152     return 0;
00153 }
00154 
00155 KKeyNative& KKeyNative::null()
00156 {
00157     if( !gx_pkey )
00158         gx_pkey = new KKeyNative;
00159     if( !gx_pkey->isNull() )
00160         gx_pkey->clear();
00161     return *gx_pkey;
00162 }
00163 
00164 KKey KKeyNative::key() const
00165 {
00166 #ifdef Q_WS_WIN
00167     return KKey( m_sym, m_mod );
00168 #else
00169     uint modSpec;
00170     if( KKeyServer::modXToMod( m_mod, modSpec ) )
00171         return KKey( m_sym, modSpec );
00172     else
00173         return KKey();
00174 #endif
00175 }
00176 
00177 int KKeyNative::keyCodeQt() const
00178 {
00179     int keyQt = KKeyServer::Sym(m_sym).qt(), modQt;
00180 
00181     if( keyQt != Qt::Key_unknown && KKeyServer::modXToModQt( m_mod, modQt ) )
00182         return keyQt | modQt;
00183 
00184     return 0;
00185 }
00186 
00187 bool KKeyNative::keyboardHasWinKey()           { return KKeyServer::keyboardHasWinKey(); }
00188 
00189 #ifdef Q_WS_X11
00190 uint KKeyNative::modX( KKey::ModFlag modFlag ) { return KKeyServer::modX( modFlag ); }
00191 uint KKeyNative::accelModMaskX()               { return KKeyServer::accelModMaskX(); }
00192 uint KKeyNative::modXNumLock()                 { return KKeyServer::modXNumLock(); }
00193 uint KKeyNative::modXLock()                    { return KKeyServer::modXLock(); }
00194 uint KKeyNative::modXScrollLock()              { return KKeyServer::modXScrollLock(); }
00195 uint KKeyNative::modXModeSwitch()              { return KKeyServer::modXModeSwitch(); }
00196 #endif
00197 
00198 #endif // Q_WS_X11
KDE Home | KDE Accessibility Home | Description of Access Keys