kglobalaccel_x11.cpp

00001 /* This file is part of the KDE libraries
00002     Copyright (C) 2001,2002 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., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "config.h"
00021 
00022 #include <qwindowdefs.h>
00023 #ifdef Q_WS_X11
00024 
00025 #include "kglobalaccel_x11.h"
00026 #include "kglobalaccel.h"
00027 #include "kkeyserver_x11.h"
00028 
00029 #include <qpopupmenu.h>
00030 #include <qregexp.h>
00031 #include <qwidget.h>
00032 #include <qmetaobject.h>
00033 #include <private/qucomextra_p.h>
00034 #include <kapplication.h>
00035 #include <kdebug.h>
00036 #include <kkeynative.h>
00037 
00038 #ifdef Q_WS_X11
00039 #include <kxerrorhandler.h>
00040 #endif
00041 
00042 #include <X11/X.h>
00043 #include <X11/Xlib.h>
00044 #include <X11/keysym.h>
00045 #include <fixx11h.h>
00046 
00047 extern "C" {
00048   static int XGrabErrorHandler( Display *, XErrorEvent *e ) {
00049     if ( e->error_code != BadAccess ) {
00050         kdWarning() << "grabKey: got X error " << e->type << " instead of BadAccess\n";
00051     }
00052     return 1;
00053   }
00054 }
00055 
00056 // g_keyModMaskXAccel
00057 //  mask of modifiers which can be used in shortcuts
00058 //  (meta, alt, ctrl, shift)
00059 // g_keyModMaskXOnOrOff
00060 //  mask of modifiers where we don't care whether they are on or off
00061 //  (caps lock, num lock, scroll lock)
00062 static uint g_keyModMaskXAccel = 0;
00063 static uint g_keyModMaskXOnOrOff = 0;
00064 
00065 static void calculateGrabMasks()
00066 {
00067     g_keyModMaskXAccel = KKeyServer::accelModMaskX();
00068     g_keyModMaskXOnOrOff =
00069             KKeyServer::modXLock() |
00070             KKeyServer::modXNumLock() |
00071             KKeyServer::modXScrollLock() | 
00072             KKeyServer::modXModeSwitch(); 
00073     //kdDebug() << "g_keyModMaskXAccel = " << g_keyModMaskXAccel
00074     //  << "g_keyModMaskXOnOrOff = " << g_keyModMaskXOnOrOff << endl;
00075 }
00076 
00077 //----------------------------------------------------
00078 
00079 static QValueList< KGlobalAccelPrivate* >* all_accels = 0;
00080 
00081 KGlobalAccelPrivate::KGlobalAccelPrivate()
00082 : KAccelBase( KAccelBase::NATIVE_KEYS )
00083 , m_blocked( false )
00084 , m_blockingDisabled( false )
00085 {
00086         if( all_accels == NULL )
00087             all_accels = new QValueList< KGlobalAccelPrivate* >;
00088         all_accels->append( this );
00089     m_sConfigGroup = "Global Shortcuts";
00090     kapp->installX11EventFilter( this );
00091 }
00092 
00093 KGlobalAccelPrivate::~KGlobalAccelPrivate()
00094 {
00095     // TODO: Need to release all grabbed keys if the main window is not shutting down.
00096     //for( CodeModMap::ConstIterator it = m_rgCodeModToAction.begin(); it != m_rgCodeModToAction.end(); ++it ) {
00097     //  const CodeMod& codemod = it.key();
00098     //}
00099         all_accels->remove( this );
00100         if( all_accels->count() == 0 ) {
00101             delete all_accels;
00102             all_accels = NULL;
00103         }
00104 }
00105 
00106 void KGlobalAccelPrivate::setEnabled( bool bEnable )
00107 {
00108     m_bEnabled = bEnable;
00109     updateConnections();
00110 }
00111 
00112 void KGlobalAccelPrivate::blockShortcuts( bool block )
00113 {
00114         if( all_accels == NULL )
00115             return;
00116         for( QValueList< KGlobalAccelPrivate* >::ConstIterator it = all_accels->begin();
00117              it != all_accels->end();
00118              ++it ) {
00119             if( (*it)->m_blockingDisabled )
00120                 continue;
00121             (*it)->m_blocked = block;
00122             (*it)->updateConnections();
00123         }
00124 }
00125 
00126 void KGlobalAccelPrivate::disableBlocking( bool block )
00127 {
00128         m_blockingDisabled = block;
00129 }
00130 
00131 bool KGlobalAccelPrivate::isEnabledInternal() const
00132 {
00133         return KAccelBase::isEnabled() && !m_blocked;
00134 }
00135 
00136 bool KGlobalAccelPrivate::emitSignal( Signal )
00137 {
00138     return false;
00139 }
00140 
00141 bool KGlobalAccelPrivate::connectKey( KAccelAction& action, const KKeyServer::Key& key )
00142     { return grabKey( key, true, &action ); }
00143 bool KGlobalAccelPrivate::connectKey( const KKeyServer::Key& key )
00144     { return grabKey( key, true, 0 ); }
00145 bool KGlobalAccelPrivate::disconnectKey( KAccelAction& action, const KKeyServer::Key& key )
00146     { return grabKey( key, false, &action ); }
00147 bool KGlobalAccelPrivate::disconnectKey( const KKeyServer::Key& key )
00148     { return grabKey( key, false, 0 ); }
00149 
00150 bool KGlobalAccelPrivate::grabKey( const KKeyServer::Key& key, bool bGrab, KAccelAction* pAction )
00151 {
00152     if( !key.code() ) {
00153         kdWarning(125) << "KGlobalAccelPrivate::grabKey( " << key.key().toStringInternal() << ", " << bGrab << ", \"" << (pAction ? pAction->name().latin1() : "(null)") << "\" ): Tried to grab key with null code." << endl;
00154         return false;
00155     }
00156 
00157     // Make sure that grab masks have been initialized.
00158     if( g_keyModMaskXOnOrOff == 0 )
00159         calculateGrabMasks();
00160 
00161     uchar keyCodeX = key.code();
00162     uint keyModX = key.mod() & g_keyModMaskXAccel; // Get rid of any non-relevant bits in mod
00163     // HACK: make Alt+Print work
00164     // only do this for the Xorg default keyboard keycodes,
00165     // other mappings (e.g. evdev) don't need or want it
00166     if( key.sym() == XK_Sys_Req && XKeycodeToKeysym( qt_xdisplay(), 111, 0 ) == XK_Print ) {
00167         keyModX |= KKeyServer::modXAlt();
00168         keyCodeX = 111;
00169     }
00170 
00171 #ifndef __osf__
00172 // this crashes under Tru64 so .....
00173     kdDebug(125) << QString( "grabKey( key: '%1', bGrab: %2 ): keyCodeX: %3 keyModX: %4\n" )
00174         .arg( key.key().toStringInternal() ).arg( bGrab )
00175         .arg( keyCodeX, 0, 16 ).arg( keyModX, 0, 16 );
00176 #endif
00177     if( !keyCodeX )
00178         return false;
00179 
00180 #ifdef Q_WS_X11
00181         KXErrorHandler handler( XGrabErrorHandler );
00182 #endif
00183     // We'll have to grab 8 key modifier combinations in order to cover all
00184     //  combinations of CapsLock, NumLock, ScrollLock.
00185     // Does anyone with more X-savvy know how to set a mask on qt_xrootwin so that
00186     //  the irrelevant bits are always ignored and we can just make one XGrabKey
00187     //  call per accelerator? -- ellis
00188 #ifndef NDEBUG
00189     QString sDebug = QString("\tcode: 0x%1 state: 0x%2 | ").arg(keyCodeX,0,16).arg(keyModX,0,16);
00190 #endif
00191     uint keyModMaskX = ~g_keyModMaskXOnOrOff;
00192     for( uint irrelevantBitsMask = 0; irrelevantBitsMask <= 0xff; irrelevantBitsMask++ ) {
00193         if( (irrelevantBitsMask & keyModMaskX) == 0 ) {
00194 #ifndef NDEBUG
00195             sDebug += QString("0x%3, ").arg(irrelevantBitsMask, 0, 16);
00196 #endif
00197             if( bGrab )
00198                 XGrabKey( qt_xdisplay(), keyCodeX, keyModX | irrelevantBitsMask,
00199                     qt_xrootwin(), True, GrabModeAsync, GrabModeSync );
00200             else
00201                 XUngrabKey( qt_xdisplay(), keyCodeX, keyModX | irrelevantBitsMask, qt_xrootwin() );
00202         }
00203     }
00204 #ifndef NDEBUG
00205     kdDebug(125) << sDebug << endl;
00206 #endif
00207 
00208         bool failed = false;
00209         if( bGrab ) {
00210 #ifdef Q_WS_X11
00211             failed = handler.error( true ); // sync now
00212 #endif
00213             // If grab failed, then ungrab any grabs that could possibly succeed
00214         if( failed ) {
00215             kdDebug(125) << "grab failed!\n";
00216             for( uint m = 0; m <= 0xff; m++ ) {
00217                 if(( m & keyModMaskX ) == 0 )
00218                     XUngrabKey( qt_xdisplay(), keyCodeX, keyModX | m, qt_xrootwin() );
00219                 }
00220                 }
00221     }
00222         if( !failed )
00223         {
00224         CodeMod codemod;
00225         codemod.code = keyCodeX;
00226         codemod.mod = keyModX;
00227         if( key.mod() & KKeyServer::MODE_SWITCH )
00228             codemod.mod |= KKeyServer::MODE_SWITCH;
00229 
00230         if( bGrab )
00231             m_rgCodeModToAction.insert( codemod, pAction );
00232         else
00233             m_rgCodeModToAction.remove( codemod );
00234     }
00235     return !failed;
00236 }
00237 
00238 bool KGlobalAccelPrivate::x11Event( XEvent* pEvent )
00239 {
00240     //kdDebug(125) << "x11EventFilter( type = " << pEvent->type << " )" << endl;
00241     switch( pEvent->type ) {
00242      case MappingNotify:
00243             XRefreshKeyboardMapping( &pEvent->xmapping );
00244         x11MappingNotify();
00245         return false;
00246      case XKeyPress:
00247         if( x11KeyPress( pEvent ) )
00248             return true;
00249      default:
00250         return QWidget::x11Event( pEvent );
00251     }
00252 }
00253 
00254 void KGlobalAccelPrivate::x11MappingNotify()
00255 {
00256     kdDebug(125) << "KGlobalAccelPrivate::x11MappingNotify()" << endl;
00257     // Maybe the X modifier map has been changed.
00258     KKeyServer::initializeMods();
00259     calculateGrabMasks();
00260     // Do new XGrabKey()s.
00261     updateConnections();
00262 }
00263 
00264 bool KGlobalAccelPrivate::x11KeyPress( const XEvent *pEvent )
00265 {
00266     // do not change this line unless you really really know what you are doing (Matthias)
00267     if ( !QWidget::keyboardGrabber() && !QApplication::activePopupWidget() ) {
00268         XUngrabKeyboard( qt_xdisplay(), pEvent->xkey.time );
00269                 XFlush( qt_xdisplay()); // avoid X(?) bug
00270         }
00271 
00272     if( !isEnabledInternal())
00273         return false;
00274 
00275     CodeMod codemod;
00276     codemod.code = pEvent->xkey.keycode;
00277     codemod.mod = pEvent->xkey.state & (g_keyModMaskXAccel | KKeyServer::MODE_SWITCH);
00278 
00279     // If numlock is active and a keypad key is pressed, XOR the SHIFT state.
00280     //  e.g., KP_4 => Shift+KP_Left, and Shift+KP_4 => KP_Left.
00281     if( pEvent->xkey.state & KKeyServer::modXNumLock() ) {
00282         // TODO: what's the xor operator in c++?
00283         uint sym = XKeycodeToKeysym( qt_xdisplay(), codemod.code, 0 );
00284         // If this is a keypad key,
00285         if( sym >= XK_KP_Space && sym <= XK_KP_9 ) {
00286             switch( sym ) {
00287                 // Leave the following keys unaltered
00288                 // FIXME: The proper solution is to see which keysyms don't change when shifted.
00289                 case XK_KP_Multiply:
00290                 case XK_KP_Add:
00291                 case XK_KP_Subtract:
00292                 case XK_KP_Divide:
00293                     break;
00294                 default:
00295                     if( codemod.mod & KKeyServer::modXShift() )
00296                         codemod.mod &= ~KKeyServer::modXShift();
00297                     else
00298                         codemod.mod |= KKeyServer::modXShift();
00299             }
00300         }
00301     }
00302 
00303     KKeyNative keyNative( pEvent );
00304     KKey key = keyNative;
00305 
00306     kdDebug(125) << "x11KeyPress: seek " << key.toStringInternal()
00307         << QString( " keyCodeX: %1 state: %2 keyModX: %3" )
00308             .arg( codemod.code, 0, 16 ).arg( pEvent->xkey.state, 0, 16 ).arg( codemod.mod, 0, 16 ) << endl;
00309 
00310     // Search for which accelerator activated this event:
00311     if( !m_rgCodeModToAction.contains( codemod ) ) {
00312 #ifndef NDEBUG
00313         for( CodeModMap::ConstIterator it = m_rgCodeModToAction.begin(); it != m_rgCodeModToAction.end(); ++it ) {
00314             KAccelAction* pAction = *it;
00315             kdDebug(125) << "\tcode: " << QString::number(it.key().code, 16) << " mod: " << QString::number(it.key().mod, 16)
00316                 << (pAction ? QString(" name: \"%1\" shortcut: %2").arg(pAction->name()).arg(pAction->shortcut().toStringInternal()) : QString::null)
00317                 << endl;
00318         }
00319 #endif
00320         return false;
00321     }
00322     KAccelAction* pAction = m_rgCodeModToAction[codemod];
00323 
00324     if( !pAction ) {
00325                 static bool recursion_block = false;
00326                 if( !recursion_block ) {
00327                         recursion_block = true;
00328                 QPopupMenu* pMenu = createPopupMenu( 0, KKeySequence(key) );
00329                 connect( pMenu, SIGNAL(activated(int)), this, SLOT(slotActivated(int)) );
00330                 pMenu->exec( QPoint( 0, 0 ) );
00331                 disconnect( pMenu, SIGNAL(activated(int)), this, SLOT(slotActivated(int)));
00332                 delete pMenu;
00333                         recursion_block = false;
00334                 }
00335     } else if( !pAction->objSlotPtr() || !pAction->isEnabled() )
00336         return false;
00337     else
00338         activate( pAction, KKeySequence(key) );
00339 
00340     return true;
00341 }
00342 
00343 void KGlobalAccelPrivate::activate( KAccelAction* pAction, const KKeySequence& seq )
00344 {
00345     kdDebug(125) << "KGlobalAccelPrivate::activate( \"" << pAction->name() << "\" ) " << endl;
00346 
00347     QRegExp rexPassIndex( "([ ]*int[ ]*)" );
00348     QRegExp rexPassInfo( " QString" );
00349     QRegExp rexIndex( " ([0-9]+)$" );
00350 
00351     // If the slot to be called accepts an integer index
00352     //  and an index is present at the end of the action's name,
00353     //  then send the slot the given index #.
00354     if( rexPassIndex.search( pAction->methodSlotPtr() ) >= 0 && rexIndex.search( pAction->name() ) >= 0 ) {
00355         int n = rexIndex.cap(1).toInt();
00356         kdDebug(125) << "Calling " << pAction->methodSlotPtr() << " int = " << n << endl;
00357                 int slot_id = pAction->objSlotPtr()->metaObject()->findSlot( normalizeSignalSlot( pAction->methodSlotPtr() ).data() + 1, true );
00358                 if( slot_id >= 0 ) {
00359                     QUObject o[2];
00360                     static_QUType_int.set(o+1,n);
00361                     const_cast< QObject* >( pAction->objSlotPtr())->qt_invoke( slot_id, o );
00362                 }
00363     } else if( rexPassInfo.search( pAction->methodSlotPtr() ) ) {
00364                 int slot_id = pAction->objSlotPtr()->metaObject()->findSlot( normalizeSignalSlot( pAction->methodSlotPtr() ).data() + 1, true );
00365                 if( slot_id >= 0 ) {
00366                     QUObject o[4];
00367                     static_QUType_QString.set(o+1,pAction->name());
00368                     static_QUType_QString.set(o+2,pAction->label());
00369                     static_QUType_ptr.set(o+3,&seq);
00370                     const_cast< QObject* >( pAction->objSlotPtr())->qt_invoke( slot_id, o );
00371                 }
00372     } else {
00373                 int slot_id = pAction->objSlotPtr()->metaObject()->findSlot( normalizeSignalSlot( pAction->methodSlotPtr() ).data() + 1, true );
00374                 if( slot_id >= 0 )
00375                     const_cast< QObject* >( pAction->objSlotPtr())->qt_invoke( slot_id, 0 );
00376     }
00377 }
00378 
00379 void KGlobalAccelPrivate::slotActivated( int iAction )
00380 {
00381     KAccelAction* pAction = actions().actionPtr( iAction );
00382     if( pAction )
00383         activate( pAction, KKeySequence() );
00384 }
00385 
00386 #include "kglobalaccel_x11.moc"
00387 
00388 #endif // !Q_WS_X11
KDE Home | KDE Accessibility Home | Description of Access Keys