kdecore Library API Documentation

kkeynative_x11.cpp

00001 /*
00002     Copyright (C) 2001 Ellis Whitehead <ellis@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017     Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include <qnamespace.h>
00021 #include <qwindowdefs.h>
00022 
00023 #ifdef Q_WS_X11 // Only compile this module if we're compiling for X11
00024 
00025 #include "kkeynative.h"
00026 #include "kkeyserver_x11.h"
00027 
00028 #include <qmap.h>
00029 #include <qstringlist.h>
00030 #include "kckey.h"
00031 #include <kdebug.h>
00032 #include <klocale.h>
00033 
00034 #define XK_MISCELLANY
00035 #define XK_XKB_KEYS
00036 #include <X11/X.h>
00037 #include <X11/Xlib.h>
00038 #include <X11/Xutil.h>
00039 #include <X11/keysymdef.h>
00040 #include <ctype.h>
00041 
00042 //---------------------------------------------------------------------
00043 
00044 static KKeyNative* gx_pkey = 0;
00045 
00046 //---------------------------------------------------------------------
00047 // KKeyNative
00048 //---------------------------------------------------------------------
00049 
00050 KKeyNative::KKeyNative()                           { clear(); }
00051 KKeyNative::KKeyNative( const KKey& key )          { init( key ); }
00052 KKeyNative::KKeyNative( const KKeyNative& key )    { init( key ); }
00053 KKeyNative::KKeyNative( const XEvent* pEvent )     { init( pEvent ); }
00054 
00055 KKeyNative::KKeyNative( uint code, uint mod, uint sym )
00056 {
00057     m_code = code;
00058     m_mod = mod;
00059     m_sym = sym;
00060 }
00061 
00062 KKeyNative::~KKeyNative()
00063     { }
00064 
00065 void KKeyNative::clear()
00066 {
00067     m_code = 0;
00068     m_mod = 0;
00069     m_sym = 0;
00070 }
00071 
00072 bool KKeyNative::init( const XEvent* pEvent )
00073 {
00074     m_code = pEvent->xkey.keycode;
00075     m_mod = pEvent->xkey.state;
00076     XLookupString( (XKeyEvent*) pEvent, 0, 0, (KeySym*) &m_sym, 0 );
00077     return true;
00078 }
00079 
00080 bool KKeyNative::init( const KKey& key )
00081 {
00082     // Get any extra mods required by the sym.
00083     //  E.g., XK_Plus requires SHIFT on the en layout.
00084     m_sym = key.sym();
00085     uint modExtra = KKeyServer::Sym(m_sym).getModsRequired();
00086     // Get the X modifier equivalent.
00087     if( !m_sym || !KKeyServer::modToModX( key.modFlags() | modExtra, m_mod ) ) {
00088         m_sym = m_mod = 0;
00089         m_code = 0;
00090         return false;
00091     }
00092 
00093     // FIXME: Accomadate non-standard layouts
00094     // XKeysymToKeycode returns the wrong keycode for XK_Print and XK_Break.
00095     // Specifically, it returns the code for SysReq instead of Print
00096     if( m_sym == XK_Print && !(m_mod & Mod1Mask) )
00097         m_code = 111; // code for Print
00098     else if( m_sym == XK_Break || (m_sym == XK_Pause && (m_mod & ControlMask)) )
00099         m_code = 114;
00100     else
00101         m_code = XKeysymToKeycode( qt_xdisplay(), m_sym );
00102 
00103     if( !m_code && m_sym )
00104         kdDebug(125) << "Couldn't get code for sym" << endl;
00105     // Now get the true sym formed by the modifiers
00106     //  E.g., Shift+Equal => Plus on the en layout.
00107     if( key.modFlags() )
00108         KKeyServer::codeXToSym( m_code, m_mod, m_sym );
00109 
00110     return true;
00111 }
00112 
00113 bool KKeyNative::init( const KKeyNative& key )
00114 {
00115     m_code = key.m_code;
00116     m_mod = key.m_mod;
00117     m_sym = key.m_sym;
00118     return true;
00119 }
00120 
00121 uint KKeyNative::code() const { return m_code; }
00122 uint KKeyNative::mod() const  { return m_mod; }
00123 uint KKeyNative::sym() const  { return m_sym; }
00124 
00125 bool KKeyNative::isNull() const
00126 {
00127     return m_sym == 0;
00128 }
00129 
00130 int KKeyNative::compare( const KKeyNative& key ) const
00131 {
00132     if( m_sym != key.m_sym )   return m_sym - key.m_sym;
00133     if( m_mod != key.m_mod )   return m_mod - key.m_mod;
00134     if( m_code != key.m_code ) return m_code - key.m_code;
00135     return 0;
00136 }
00137 
00138 KKeyNative& KKeyNative::null()
00139 {
00140     if( !gx_pkey )
00141         gx_pkey = new KKeyNative;
00142     if( !gx_pkey->isNull() )
00143         gx_pkey->clear();
00144     return *gx_pkey;
00145 }
00146 
00147 KKey KKeyNative::key() const
00148 {
00149     uint modSpec;
00150     if( KKeyServer::modXToMod( m_mod, modSpec ) )
00151         return KKey( m_sym, modSpec );
00152     else
00153         return KKey();
00154 }
00155 
00156 int KKeyNative::keyCodeQt() const
00157 {
00158     int keyQt = KKeyServer::Sym(m_sym).qt(), modQt;
00159 
00160     if( keyQt != Qt::Key_unknown && KKeyServer::modXToModQt( m_mod, modQt ) )
00161         return keyQt | modQt;
00162 
00163     return 0;
00164 }
00165 
00166 uint KKeyNative::modX( KKey::ModFlag modFlag ) { return KKeyServer::modX( modFlag ); }
00167 bool KKeyNative::keyboardHasWinKey()           { return KKeyServer::keyboardHasWinKey(); }
00168 uint KKeyNative::accelModMaskX()               { return KKeyServer::accelModMaskX(); }
00169 uint KKeyNative::modXNumLock()                 { return KKeyServer::modXNumLock(); }
00170 uint KKeyNative::modXLock()                    { return KKeyServer::modXLock(); }
00171 uint KKeyNative::modXScrollLock()              { return KKeyServer::modXScrollLock(); }
00172 
00173 #endif // Q_WS_X11
KDE Logo
This file is part of the documentation for kdecore Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Sep 23 17:11:36 2004 by doxygen 1.3.8-20040913 written by Dimitri van Heesch, © 1997-2003