kdecore Library API Documentation

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., 59 Temple Place - Suite 330,
00020     Boston, MA 02111-1307, 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     m_code = pEvent->xkey.keycode;
00083     m_mod = pEvent->xkey.state;
00084     XLookupString( (XKeyEvent*) pEvent, 0, 0, (KeySym*) &m_sym, 0 );
00085     return true;
00086 }
00087 #endif
00088 
00089 bool KKeyNative::init( const KKey& key )
00090 {
00091 #ifdef Q_WS_WIN
00092     m_sym = key.sym();
00093     m_code = m_sym; //key.keyCodeQt();
00094     m_mod = key.m_mod;
00095 #elif !defined(Q_WS_WIN)
00096     // Get any extra mods required by the sym.
00097     //  E.g., XK_Plus requires SHIFT on the en layout.
00098     m_sym = key.sym();
00099     uint modExtra = KKeyServer::Sym(m_sym).getModsRequired();
00100     // Get the X modifier equivalent.
00101     if( !m_sym || !KKeyServer::modToModX( key.modFlags() | modExtra, m_mod ) ) {
00102         m_sym = m_mod = 0;
00103         m_code = 0;
00104         return false;
00105     }
00106 
00107     // FIXME: Accomadate non-standard layouts
00108     // XKeysymToKeycode returns the wrong keycode for XK_Print and XK_Break.
00109     // Specifically, it returns the code for SysReq instead of Print
00110     if( m_sym == XK_Print && !(m_mod & Mod1Mask) )
00111         m_code = 111; // code for Print
00112     else if( m_sym == XK_Break || (m_sym == XK_Pause && (m_mod & ControlMask)) )
00113         m_code = 114;
00114     else
00115         m_code = XKeysymToKeycode( qt_xdisplay(), m_sym );
00116 
00117     if( !m_code && m_sym )
00118         kdDebug(125) << "Couldn't get code for sym" << endl;
00119     // Now get the true sym formed by the modifiers
00120     //  E.g., Shift+Equal => Plus on the en layout.
00121     if( key.modFlags() && ( ( m_sym < XK_Home || m_sym > XK_Begin ) && 
00122                   m_sym != XK_Insert && m_sym != XK_Delete ))
00123         KKeyServer::codeXToSym( m_code, m_mod, m_sym );
00124 #endif
00125     return true;
00126 }
00127 
00128 bool KKeyNative::init( const KKeyNative& key )
00129 {
00130     m_code = key.m_code;
00131     m_mod = key.m_mod;
00132     m_sym = key.m_sym;
00133     return true;
00134 }
00135 
00136 uint KKeyNative::code() const { return m_code; }
00137 uint KKeyNative::mod() const  { return m_mod; }
00138 uint KKeyNative::sym() const  { return m_sym; }
00139 
00140 bool KKeyNative::isNull() const
00141 {
00142     return m_sym == 0;
00143 }
00144 
00145 int KKeyNative::compare( const KKeyNative& key ) const
00146 {
00147     if( m_sym != key.m_sym )   return m_sym - key.m_sym;
00148     if( m_mod != key.m_mod )   return m_mod - key.m_mod;
00149     if( m_code != key.m_code ) return m_code - key.m_code;
00150     return 0;
00151 }
00152 
00153 KKeyNative& KKeyNative::null()
00154 {
00155     if( !gx_pkey )
00156         gx_pkey = new KKeyNative;
00157     if( !gx_pkey->isNull() )
00158         gx_pkey->clear();
00159     return *gx_pkey;
00160 }
00161 
00162 KKey KKeyNative::key() const
00163 {
00164 #ifdef Q_WS_WIN
00165     return KKey( m_sym, m_mod );
00166 #else
00167     uint modSpec;
00168     if( KKeyServer::modXToMod( m_mod, modSpec ) )
00169         return KKey( m_sym, modSpec );
00170     else
00171         return KKey();
00172 #endif
00173 }
00174 
00175 int KKeyNative::keyCodeQt() const
00176 {
00177     int keyQt = KKeyServer::Sym(m_sym).qt(), modQt;
00178 
00179     if( keyQt != Qt::Key_unknown && KKeyServer::modXToModQt( m_mod, modQt ) )
00180         return keyQt | modQt;
00181 
00182     return 0;
00183 }
00184 
00185 bool KKeyNative::keyboardHasWinKey()           { return KKeyServer::keyboardHasWinKey(); }
00186 
00187 #ifdef Q_WS_X11
00188 uint KKeyNative::modX( KKey::ModFlag modFlag ) { return KKeyServer::modX( modFlag ); }
00189 uint KKeyNative::accelModMaskX()               { return KKeyServer::accelModMaskX(); }
00190 uint KKeyNative::modXNumLock()                 { return KKeyServer::modXNumLock(); }
00191 uint KKeyNative::modXLock()                    { return KKeyServer::modXLock(); }
00192 uint KKeyNative::modXScrollLock()              { return KKeyServer::modXScrollLock(); }
00193 #endif
00194 
00195 #endif // Q_WS_X11
KDE Logo
This file is part of the documentation for kdecore Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Jul 21 13:13:44 2006 by doxygen 1.4.0 written by Dimitri van Heesch, © 1997-2003