00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00057
00058
00059
00060
00061
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
00074
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 , m_suspended( false )
00086 {
00087 if( all_accels == NULL )
00088 all_accels = new QValueList< KGlobalAccelPrivate* >;
00089 all_accels->append( this );
00090 m_sConfigGroup = "Global Shortcuts";
00091 kapp->installX11EventFilter( this );
00092 }
00093
00094 KGlobalAccelPrivate::~KGlobalAccelPrivate()
00095 {
00096
00097
00098
00099
00100 all_accels->remove( this );
00101 if( all_accels->count() == 0 ) {
00102 delete all_accels;
00103 all_accels = NULL;
00104 }
00105 }
00106
00107 void KGlobalAccelPrivate::setEnabled( bool bEnable )
00108 {
00109 m_bEnabled = bEnable;
00110 updateConnections();
00111 }
00112
00113 void KGlobalAccelPrivate::blockShortcuts( bool block )
00114 {
00115 if( all_accels == NULL )
00116 return;
00117 for( QValueList< KGlobalAccelPrivate* >::ConstIterator it = all_accels->begin();
00118 it != all_accels->end();
00119 ++it ) {
00120 if( (*it)->m_blockingDisabled )
00121 continue;
00122 (*it)->m_blocked = block;
00123 (*it)->updateConnections();
00124 }
00125 }
00126
00127 void KGlobalAccelPrivate::disableBlocking( bool block )
00128 {
00129 m_blockingDisabled = block;
00130 }
00131
00132 bool KGlobalAccelPrivate::isEnabledInternal() const
00133 {
00134 return KAccelBase::isEnabled() && !m_blocked;
00135 }
00136
00137
00138
00139 void KGlobalAccelPrivate::suspend( bool s )
00140 {
00141 m_suspended = s;
00142 }
00143
00144 bool KGlobalAccelPrivate::emitSignal( Signal )
00145 {
00146 return false;
00147 }
00148
00149 bool KGlobalAccelPrivate::connectKey( KAccelAction& action, const KKeyServer::Key& key )
00150 { return grabKey( key, true, &action ); }
00151 bool KGlobalAccelPrivate::connectKey( const KKeyServer::Key& key )
00152 { return grabKey( key, true, 0 ); }
00153 bool KGlobalAccelPrivate::disconnectKey( KAccelAction& action, const KKeyServer::Key& key )
00154 { return grabKey( key, false, &action ); }
00155 bool KGlobalAccelPrivate::disconnectKey( const KKeyServer::Key& key )
00156 { return grabKey( key, false, 0 ); }
00157
00158 bool KGlobalAccelPrivate::grabKey( const KKeyServer::Key& key, bool bGrab, KAccelAction* pAction )
00159 {
00160 if( !key.code() ) {
00161 kdWarning(125) << "KGlobalAccelPrivate::grabKey( " << key.key().toStringInternal() << ", " << bGrab << ", \"" << (pAction ? pAction->name().latin1() : "(null)") << "\" ): Tried to grab key with null code." << endl;
00162 return false;
00163 }
00164
00165
00166 if( g_keyModMaskXOnOrOff == 0 )
00167 calculateGrabMasks();
00168
00169 uchar keyCodeX = key.code();
00170 uint keyModX = key.mod() & g_keyModMaskXAccel;
00171
00172 if( key.sym() == XK_Sys_Req ) {
00173 keyModX |= KKeyServer::modXAlt();
00174 keyCodeX = 111;
00175 }
00176
00177 #ifndef __osf__
00178
00179 kdDebug(125) << QString( "grabKey( key: '%1', bGrab: %2 ): keyCodeX: %3 keyModX: %4\n" )
00180 .arg( key.key().toStringInternal() ).arg( bGrab )
00181 .arg( keyCodeX, 0, 16 ).arg( keyModX, 0, 16 );
00182 #endif
00183 if( !keyCodeX )
00184 return false;
00185
00186 #ifdef Q_WS_X11
00187 KXErrorHandler handler( XGrabErrorHandler );
00188 #endif
00189
00190
00191
00192
00193
00194 #ifndef NDEBUG
00195 QString sDebug = QString("\tcode: 0x%1 state: 0x%2 | ").arg(keyCodeX,0,16).arg(keyModX,0,16);
00196 #endif
00197 uint keyModMaskX = ~g_keyModMaskXOnOrOff;
00198 for( uint irrelevantBitsMask = 0; irrelevantBitsMask <= 0xff; irrelevantBitsMask++ ) {
00199 if( (irrelevantBitsMask & keyModMaskX) == 0 ) {
00200 #ifndef NDEBUG
00201 sDebug += QString("0x%3, ").arg(irrelevantBitsMask, 0, 16);
00202 #endif
00203 if( bGrab )
00204 XGrabKey( qt_xdisplay(), keyCodeX, keyModX | irrelevantBitsMask,
00205 qt_xrootwin(), True, GrabModeAsync, GrabModeSync );
00206 else
00207 XUngrabKey( qt_xdisplay(), keyCodeX, keyModX | irrelevantBitsMask, qt_xrootwin() );
00208 }
00209 }
00210 #ifndef NDEBUG
00211 kdDebug(125) << sDebug << endl;
00212 #endif
00213
00214 bool failed = false;
00215 if( bGrab ) {
00216 #ifdef Q_WS_X11
00217 failed = handler.error( true );
00218 #endif
00219
00220 if( failed ) {
00221 kdDebug(125) << "grab failed!\n";
00222 for( uint m = 0; m <= 0xff; m++ ) {
00223 if(( m & keyModMaskX ) == 0 )
00224 XUngrabKey( qt_xdisplay(), keyCodeX, keyModX | m, qt_xrootwin() );
00225 }
00226 }
00227 }
00228 if( !failed )
00229 {
00230 CodeMod codemod;
00231 codemod.code = keyCodeX;
00232 codemod.mod = keyModX;
00233 if( key.mod() & KKeyServer::MODE_SWITCH )
00234 codemod.mod |= KKeyServer::MODE_SWITCH;
00235
00236 if( bGrab )
00237 m_rgCodeModToAction.insert( codemod, pAction );
00238 else
00239 m_rgCodeModToAction.remove( codemod );
00240 }
00241 return !failed;
00242 }
00243
00244 bool KGlobalAccelPrivate::x11Event( XEvent* pEvent )
00245 {
00246
00247 switch( pEvent->type ) {
00248 case MappingNotify:
00249 XRefreshKeyboardMapping( &pEvent->xmapping );
00250 x11MappingNotify();
00251 return false;
00252 case XKeyPress:
00253 if( x11KeyPress( pEvent ) )
00254 return true;
00255 default:
00256 return QWidget::x11Event( pEvent );
00257 }
00258 }
00259
00260 void KGlobalAccelPrivate::x11MappingNotify()
00261 {
00262 kdDebug(125) << "KGlobalAccelPrivate::x11MappingNotify()" << endl;
00263
00264 KKeyServer::initializeMods();
00265 calculateGrabMasks();
00266
00267 updateConnections();
00268 }
00269
00270 bool KGlobalAccelPrivate::x11KeyPress( const XEvent *pEvent )
00271 {
00272
00273 if ( !QWidget::keyboardGrabber() && !QApplication::activePopupWidget() ) {
00274 XUngrabKeyboard( qt_xdisplay(), pEvent->xkey.time );
00275 XFlush( qt_xdisplay());
00276 }
00277
00278 if( !isEnabledInternal() || m_suspended )
00279 return false;
00280
00281 CodeMod codemod;
00282 codemod.code = pEvent->xkey.keycode;
00283 codemod.mod = pEvent->xkey.state & (g_keyModMaskXAccel | KKeyServer::MODE_SWITCH);
00284
00285
00286
00287 if( pEvent->xkey.state & KKeyServer::modXNumLock() ) {
00288
00289 uint sym = XKeycodeToKeysym( qt_xdisplay(), codemod.code, 0 );
00290
00291 if( sym >= XK_KP_Space && sym <= XK_KP_9 ) {
00292 switch( sym ) {
00293
00294
00295 case XK_KP_Multiply:
00296 case XK_KP_Add:
00297 case XK_KP_Subtract:
00298 case XK_KP_Divide:
00299 break;
00300 default:
00301 if( codemod.mod & KKeyServer::modXShift() )
00302 codemod.mod &= ~KKeyServer::modXShift();
00303 else
00304 codemod.mod |= KKeyServer::modXShift();
00305 }
00306 }
00307 }
00308
00309 KKeyNative keyNative( pEvent );
00310 KKey key = keyNative;
00311
00312 kdDebug(125) << "x11KeyPress: seek " << key.toStringInternal()
00313 << QString( " keyCodeX: %1 state: %2 keyModX: %3" )
00314 .arg( codemod.code, 0, 16 ).arg( pEvent->xkey.state, 0, 16 ).arg( codemod.mod, 0, 16 ) << endl;
00315
00316
00317 if( !m_rgCodeModToAction.contains( codemod ) ) {
00318 #ifndef NDEBUG
00319 for( CodeModMap::ConstIterator it = m_rgCodeModToAction.begin(); it != m_rgCodeModToAction.end(); ++it ) {
00320 KAccelAction* pAction = *it;
00321 kdDebug(125) << "\tcode: " << QString::number(it.key().code, 16) << " mod: " << QString::number(it.key().mod, 16)
00322 << (pAction ? QString(" name: \"%1\" shortcut: %2").arg(pAction->name()).arg(pAction->shortcut().toStringInternal()) : QString::null)
00323 << endl;
00324 }
00325 #endif
00326 return false;
00327 }
00328
00329 KAccelAction* pAction = m_rgCodeModToAction[codemod];
00330
00331 if( !pAction ) {
00332 static bool recursion_block = false;
00333 if( !recursion_block ) {
00334 recursion_block = true;
00335 QPopupMenu* pMenu = createPopupMenu( 0, KKeySequence(key) );
00336 connect( pMenu, SIGNAL(activated(int)), this, SLOT(slotActivated(int)) );
00337 pMenu->exec( QPoint( 0, 0 ) );
00338 disconnect( pMenu, SIGNAL(activated(int)), this, SLOT(slotActivated(int)));
00339 delete pMenu;
00340 recursion_block = false;
00341 }
00342 } else if( !pAction->objSlotPtr() || !pAction->isEnabled() )
00343 return false;
00344 else
00345 activate( pAction, KKeySequence(key) );
00346
00347 return true;
00348 }
00349
00350 void KGlobalAccelPrivate::activate( KAccelAction* pAction, const KKeySequence& seq )
00351 {
00352 kdDebug(125) << "KGlobalAccelPrivate::activate( \"" << pAction->name() << "\" ) " << endl;
00353
00354 QRegExp rexPassIndex( "([ ]*int[ ]*)" );
00355 QRegExp rexPassInfo( " QString" );
00356 QRegExp rexIndex( " ([0-9]+)$" );
00357
00358
00359
00360
00361 if( rexPassIndex.search( pAction->methodSlotPtr() ) >= 0 && rexIndex.search( pAction->name() ) >= 0 ) {
00362 int n = rexIndex.cap(1).toInt();
00363 kdDebug(125) << "Calling " << pAction->methodSlotPtr() << " int = " << n << endl;
00364 int slot_id = pAction->objSlotPtr()->metaObject()->findSlot( normalizeSignalSlot( pAction->methodSlotPtr() ).data() + 1, true );
00365 if( slot_id >= 0 ) {
00366 QUObject o[2];
00367 static_QUType_int.set(o+1,n);
00368 const_cast< QObject* >( pAction->objSlotPtr())->qt_invoke( slot_id, o );
00369 }
00370 } else if( rexPassInfo.search( pAction->methodSlotPtr() ) ) {
00371 int slot_id = pAction->objSlotPtr()->metaObject()->findSlot( normalizeSignalSlot( pAction->methodSlotPtr() ).data() + 1, true );
00372 if( slot_id >= 0 ) {
00373 QUObject o[4];
00374 static_QUType_QString.set(o+1,pAction->name());
00375 static_QUType_QString.set(o+2,pAction->label());
00376 static_QUType_ptr.set(o+3,&seq);
00377 const_cast< QObject* >( pAction->objSlotPtr())->qt_invoke( slot_id, o );
00378 }
00379 } else {
00380 int slot_id = pAction->objSlotPtr()->metaObject()->findSlot( normalizeSignalSlot( pAction->methodSlotPtr() ).data() + 1, true );
00381 if( slot_id >= 0 )
00382 const_cast< QObject* >( pAction->objSlotPtr())->qt_invoke( slot_id, 0 );
00383 }
00384 }
00385
00386 void KGlobalAccelPrivate::slotActivated( int iAction )
00387 {
00388 KAccelAction* pAction = actions().actionPtr( iAction );
00389 if( pAction )
00390 activate( pAction, KKeySequence() );
00391 }
00392
00393 #include "kglobalaccel_x11.moc"
00394
00395 #endif // !Q_WS_X11