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     if( key.sym() == XK_Sys_Req ) {
00165         keyModX |= KKeyServer::modXAlt();
00166         keyCodeX = 111;
00167     }
00168 
00169 #ifndef __osf__
00170 // this crashes under Tru64 so .....
00171     kdDebug(125) << QString( "grabKey( key: '%1', bGrab: %2 ): keyCodeX: %3 keyModX: %4\n" )
00172         .arg( key.key().toStringInternal() ).arg( bGrab )
00173         .arg( keyCodeX, 0, 16 ).arg( keyModX, 0, 16 );
00174 #endif
00175     if( !keyCodeX )
00176         return false;
00177 
00178 #ifdef Q_WS_X11
00179         KXErrorHandler handler( XGrabErrorHandler );
00180 #endif
00181     // We'll have to grab 8 key modifier combinations in order to cover all
00182     //  combinations of CapsLock, NumLock, ScrollLock.
00183     // Does anyone with more X-savvy know how to set a mask on qt_xrootwin so that
00184     //  the irrelevant bits are always ignored and we can just make one XGrabKey
00185     //  call per accelerator? -- ellis
00186 #ifndef NDEBUG
00187     QString sDebug = QString("\tcode: 0x%1 state: 0x%2 | ").arg(keyCodeX,0,16).arg(keyModX,0,16);
00188 #endif
00189     uint keyModMaskX = ~g_keyModMaskXOnOrOff;
00190     for( uint irrelevantBitsMask = 0; irrelevantBitsMask <= 0xff; irrelevantBitsMask++ ) {
00191         if( (irrelevantBitsMask & keyModMaskX) == 0 ) {
00192 #ifndef NDEBUG
00193             sDebug += QString("0x%3, ").arg(irrelevantBitsMask, 0, 16);
00194 #endif
00195             if( bGrab )
00196                 XGrabKey( qt_xdisplay(), keyCodeX, keyModX | irrelevantBitsMask,
00197                     qt_xrootwin(), True, GrabModeAsync, GrabModeSync );
00198             else
00199                 XUngrabKey( qt_xdisplay(), keyCodeX, keyModX | irrelevantBitsMask, qt_xrootwin() );
00200         }
00201     }
00202 #ifndef NDEBUG
00203     kdDebug(125) << sDebug << endl;
00204 #endif
00205 
00206         bool failed = false;
00207         if( bGrab ) {
00208 #ifdef Q_WS_X11
00209             failed = handler.error( true ); // sync now
00210 #endif
00211             // If grab failed, then ungrab any grabs that could possibly succeed
00212         if( failed ) {
00213             kdDebug(125) << "grab failed!\n";
00214             for( uint m = 0; m <= 0xff; m++ ) {
00215                 if(( m & keyModMaskX ) == 0 )
00216                     XUngrabKey( qt_xdisplay(), keyCodeX, keyModX | m, qt_xrootwin() );
00217                 }
00218                 }
00219     }
00220         if( !failed )
00221         {
00222         CodeMod codemod;
00223         codemod.code = keyCodeX;
00224         codemod.mod = keyModX;
00225         if( key.mod() & KKeyServer::MODE_SWITCH )
00226             codemod.mod |= KKeyServer::MODE_SWITCH;
00227 
00228         if( bGrab )
00229             m_rgCodeModToAction.insert( codemod, pAction );
00230         else
00231             m_rgCodeModToAction.remove( codemod );
00232     }
00233     return !failed;
00234 }
00235 
00236 bool KGlobalAccelPrivate::x11Event( XEvent* pEvent )
00237 {
00238     //kdDebug(125) << "x11EventFilter( type = " << pEvent->type << " )" << endl;
00239     switch( pEvent->type ) {
00240      case MappingNotify:
00241             XRefreshKeyboardMapping( &pEvent->xmapping );
00242         x11MappingNotify();
00243         return false;
00244      case XKeyPress:
00245         if( x11KeyPress( pEvent ) )
00246             return true;
00247      default:
00248         return QWidget::x11Event( pEvent );
00249     }
00250 }
00251 
00252 void KGlobalAccelPrivate::x11MappingNotify()
00253 {
00254     kdDebug(125) << "KGlobalAccelPrivate::x11MappingNotify()" << endl;
00255     // Maybe the X modifier map has been changed.
00256     KKeyServer::initializeMods();
00257     calculateGrabMasks();
00258     // Do new XGrabKey()s.
00259     updateConnections();
00260 }
00261 
00262 bool KGlobalAccelPrivate::x11KeyPress( const XEvent *pEvent )
00263 {
00264     // do not change this line unless you really really know what you are doing (Matthias)
00265     if ( !QWidget::keyboardGrabber() && !QApplication::activePopupWidget() ) {
00266         XUngrabKeyboard( qt_xdisplay(), pEvent->xkey.time );
00267                 XFlush( qt_xdisplay()); // avoid X(?) bug
00268         }
00269 
00270     if( !isEnabledInternal())
00271         return false;
00272 
00273     CodeMod codemod;
00274     codemod.code = pEvent->xkey.keycode;
00275     codemod.mod = pEvent->xkey.state & (g_keyModMaskXAccel | KKeyServer::MODE_SWITCH);
00276 
00277     // If numlock is active and a keypad key is pressed, XOR the SHIFT state.
00278     //  e.g., KP_4 => Shift+KP_Left, and Shift+KP_4 => KP_Left.
00279     if( pEvent->xkey.state & KKeyServer::modXNumLock() ) {
00280         // TODO: what's the xor operator in c++?
00281         uint sym = XKeycodeToKeysym( qt_xdisplay(), codemod.code, 0 );
00282         // If this is a keypad key,
00283         if( sym >= XK_KP_Space && sym <= XK_KP_9 ) {
00284             switch( sym ) {
00285                 // Leave the following keys unaltered
00286                 // FIXME: The proper solution is to see which keysyms don't change when shifted.
00287                 case XK_KP_Multiply:
00288                 case XK_KP_Add:
00289                 case XK_KP_Subtract:
00290                 case XK_KP_Divide:
00291                     break;
00292                 default:
00293                     if( codemod.mod & KKeyServer::modXShift() )
00294                         codemod.mod &= ~KKeyServer::modXShift();
00295                     else
00296                         codemod.mod |= KKeyServer::modXShift();
00297             }
00298         }
00299     }
00300 
00301     KKeyNative keyNative( pEvent );
00302     KKey key = keyNative;
00303 
00304     kdDebug(125) << "x11KeyPress: seek " << key.toStringInternal()
00305         << QString( " keyCodeX: %1 state: %2 keyModX: %3" )
00306             .arg( codemod.code, 0, 16 ).arg( pEvent->xkey.state, 0, 16 ).arg( codemod.mod, 0, 16 ) << endl;
00307 
00308     // Search for which accelerator activated this event:
00309     if( !m_rgCodeModToAction.contains( codemod ) ) {
00310 #ifndef NDEBUG
00311         for( CodeModMap::ConstIterator it = m_rgCodeModToAction.begin(); it != m_rgCodeModToAction.end(); ++it ) {
00312             KAccelAction* pAction = *it;
00313             kdDebug(125) << "\tcode: " << QString::number(it.key().code, 16) << " mod: " << QString::number(it.key().mod, 16)
00314                 << (pAction ? QString(" name: \"%1\" shortcut: %2").arg(pAction->name()).arg(pAction->shortcut().toStringInternal()) : QString::null)
00315                 << endl;
00316         }
00317 #endif
00318         return false;
00319     }
00320     KAccelAction* pAction = m_rgCodeModToAction[codemod];
00321 
00322     if( !pAction ) {
00323                 static bool recursion_block = false;
00324                 if( !recursion_block ) {
00325                         recursion_block = true;
00326                 QPopupMenu* pMenu = createPopupMenu( 0, KKeySequence(key) );
00327                 connect( pMenu, SIGNAL(activated(int)), this, SLOT(slotActivated(int)) );
00328                 pMenu->exec( QPoint( 0, 0 ) );
00329                 disconnect( pMenu, SIGNAL(activated(int)), this, SLOT(slotActivated(int)));
00330                 delete pMenu;
00331                         recursion_block = false;
00332                 }
00333     } else if( !pAction->objSlotPtr() || !pAction->isEnabled() )
00334         return false;
00335     else
00336         activate( pAction, KKeySequence(key) );
00337 
00338     return true;
00339 }
00340 
00341 void KGlobalAccelPrivate::activate( KAccelAction* pAction, const KKeySequence& seq )
00342 {
00343     kdDebug(125) << "KGlobalAccelPrivate::activate( \"" << pAction->name() << "\" ) " << endl;
00344 
00345     QRegExp rexPassIndex( "([ ]*int[ ]*)" );
00346     QRegExp rexPassInfo( " QString" );
00347     QRegExp rexIndex( " ([0-9]+)$" );
00348 
00349     // If the slot to be called accepts an integer index
00350     //  and an index is present at the end of the action's name,
00351     //  then send the slot the given index #.
00352     if( rexPassIndex.search( pAction->methodSlotPtr() ) >= 0 && rexIndex.search( pAction->name() ) >= 0 ) {
00353         int n = rexIndex.cap(1).toInt();
00354         kdDebug(125) << "Calling " << pAction->methodSlotPtr() << " int = " << n << endl;
00355                 int slot_id = pAction->objSlotPtr()->metaObject()->findSlot( normalizeSignalSlot( pAction->methodSlotPtr() ).data() + 1, true );
00356                 if( slot_id >= 0 ) {
00357                     QUObject o[2];
00358                     static_QUType_int.set(o+1,n);
00359                     const_cast< QObject* >( pAction->objSlotPtr())->qt_invoke( slot_id, o );
00360                 }
00361     } else if( rexPassInfo.search( pAction->methodSlotPtr() ) ) {
00362                 int slot_id = pAction->objSlotPtr()->metaObject()->findSlot( normalizeSignalSlot( pAction->methodSlotPtr() ).data() + 1, true );
00363                 if( slot_id >= 0 ) {
00364                     QUObject o[4];
00365                     static_QUType_QString.set(o+1,pAction->name());
00366                     static_QUType_QString.set(o+2,pAction->label());
00367                     static_QUType_ptr.set(o+3,&seq);
00368                     const_cast< QObject* >( pAction->objSlotPtr())->qt_invoke( slot_id, o );
00369                 }
00370     } else {
00371                 int slot_id = pAction->objSlotPtr()->metaObject()->findSlot( normalizeSignalSlot( pAction->methodSlotPtr() ).data() + 1, true );
00372                 if( slot_id >= 0 )
00373                     const_cast< QObject* >( pAction->objSlotPtr())->qt_invoke( slot_id, 0 );
00374     }
00375 }
00376 
00377 void KGlobalAccelPrivate::slotActivated( int iAction )
00378 {
00379     KAccelAction* pAction = actions().actionPtr( iAction );
00380     if( pAction )
00381         activate( pAction, KKeySequence() );
00382 }
00383 
00384 #include "kglobalaccel_x11.moc"
00385 
00386 #endif // !Q_WS_X11
KDE Home | KDE Accessibility Home | Description of Access Keys