00001 #include <qwindowdefs.h>
00002 #ifdef Q_WS_X11
00003
00004 #include "kglobalaccel_x11.h"
00005 #include "kglobalaccel.h"
00006 #include "kkeyserver_x11.h"
00007
00008 #include <qpopupmenu.h>
00009 #include <qregexp.h>
00010 #include <qwidget.h>
00011 #include <kapplication.h>
00012 #include <kdebug.h>
00013 #include <kkeynative.h>
00014
00015 #include <X11/X.h>
00016 #include <X11/Xlib.h>
00017 #include <X11/keysym.h>
00018
00019 #ifdef KeyPress
00020
00021 const int XKeyPress = KeyPress;
00022 const int XKeyRelease = KeyRelease;
00023 #undef KeyPress
00024 #endif
00025
00026 static bool g_bGrabFailed;
00027
00028 extern "C" {
00029 static int XGrabErrorHandler( Display *, XErrorEvent *e ) {
00030 if ( e->error_code != BadAccess ) {
00031 kdWarning() << "grabKey: got X error " << e->type << " instead of BadAccess\n";
00032 }
00033 g_bGrabFailed = true;
00034 return 0;
00035 }
00036 }
00037
00038
00039
00040
00041
00042
00043
00044 static uint g_keyModMaskXAccel = 0;
00045 static uint g_keyModMaskXOnOrOff = 0;
00046
00047 static void calculateGrabMasks()
00048 {
00049 g_keyModMaskXAccel = KKeyServer::accelModMaskX();
00050 g_keyModMaskXOnOrOff =
00051 KKeyServer::modXLock() |
00052 KKeyServer::modXNumLock() |
00053 KKeyServer::modXScrollLock();
00054
00055
00056 }
00057
00058
00059
00060 KGlobalAccelPrivate::KGlobalAccelPrivate()
00061 : KAccelBase( KAccelBase::NATIVE_KEYS )
00062 {
00063 m_sConfigGroup = "Global Shortcuts";
00064 kapp->installX11EventFilter( this );
00065 }
00066
00067 KGlobalAccelPrivate::~KGlobalAccelPrivate()
00068 {
00069
00070
00071
00072
00073 }
00074
00075 void KGlobalAccelPrivate::setEnabled( bool bEnable )
00076 {
00077 m_bEnabled = bEnable;
00078
00079 };
00080
00081 bool KGlobalAccelPrivate::emitSignal( Signal )
00082 {
00083 return false;
00084 }
00085
00086 bool KGlobalAccelPrivate::connectKey( KAccelAction& action, const KKeyServer::Key& key )
00087 { return grabKey( key, true, &action ); }
00088 bool KGlobalAccelPrivate::connectKey( const KKeyServer::Key& key )
00089 { return grabKey( key, true, 0 ); }
00090 bool KGlobalAccelPrivate::disconnectKey( KAccelAction& action, const KKeyServer::Key& key )
00091 { return grabKey( key, false, &action ); }
00092 bool KGlobalAccelPrivate::disconnectKey( const KKeyServer::Key& key )
00093 { return grabKey( key, false, 0 ); }
00094
00095 bool KGlobalAccelPrivate::grabKey( const KKeyServer::Key& key, bool bGrab, KAccelAction* pAction )
00096 {
00097 if( !key.code() ) {
00098 kdWarning(125) << "KGlobalAccelPrivate::grabKey( " << key.key().toStringInternal() << ", " << bGrab << ", \"" << (pAction ? pAction->name().latin1() : "(null)") << "\" ): Tried to grab key with null code." << endl;
00099 return false;
00100 }
00101
00102
00103 if( g_keyModMaskXOnOrOff == 0 )
00104 calculateGrabMasks();
00105
00106 uchar keyCodeX = key.code();
00107 uint keyModX = key.mod() & g_keyModMaskXAccel;
00108
00109 #ifndef __osf__
00110
00111 kdDebug(125) << QString( "grabKey( key: '%1', bGrab: %2 ): keyCodeX: %3 keyModX: %4\n" )
00112 .arg( key.key().toStringInternal() ).arg( bGrab )
00113 .arg( keyCodeX, 0, 16 ).arg( keyModX, 0, 16 );
00114 #endif
00115 if( !keyCodeX )
00116 return false;
00117
00118
00119 g_bGrabFailed = false;
00120 XSync( qt_xdisplay(), 0 );
00121 XErrorHandler savedErrorHandler = XSetErrorHandler( XGrabErrorHandler );
00122
00123
00124
00125
00126
00127
00128 #ifndef NDEBUG
00129 QString sDebug = QString("\tcode: 0x%1 state: 0x%2 | ").arg(keyCodeX,0,16).arg(keyModX,0,16);
00130 #endif
00131 uint keyModMaskX = ~g_keyModMaskXOnOrOff;
00132 for( uint irrelevantBitsMask = 0; irrelevantBitsMask <= 0xff; irrelevantBitsMask++ ) {
00133 if( (irrelevantBitsMask & keyModMaskX) == 0 ) {
00134 #ifndef NDEBUG
00135 sDebug += QString("0x%3, ").arg(irrelevantBitsMask, 0, 16);
00136 #endif
00137 if( bGrab ) {
00138 XGrabKey( qt_xdisplay(), keyCodeX, keyModX | irrelevantBitsMask,
00139 qt_xrootwin(), True, GrabModeAsync, GrabModeSync );
00140
00141
00142 if( g_bGrabFailed ) {
00143 kdDebug(125) << "grab failed!\n";
00144 for( uint m = 0; m < irrelevantBitsMask; m++ ) {
00145 if( m & keyModMaskX == 0 )
00146 XUngrabKey( qt_xdisplay(), keyCodeX, keyModX | m, qt_xrootwin() );
00147 }
00148 break;
00149 }
00150 } else
00151 XUngrabKey( qt_xdisplay(), keyCodeX, keyModX | irrelevantBitsMask, qt_xrootwin() );
00152 }
00153 }
00154 #ifndef NDEBUG
00155 kdDebug(125) << sDebug << endl;
00156 #endif
00157
00158 XSync( qt_xdisplay(), 0 );
00159 XSetErrorHandler( savedErrorHandler );
00160
00161 if( !g_bGrabFailed ) {
00162 CodeMod codemod;
00163 codemod.code = keyCodeX;
00164 codemod.mod = keyModX;
00165 if( key.mod() & KKeyServer::MODE_SWITCH )
00166 codemod.mod |= KKeyServer::MODE_SWITCH;
00167
00168 if( bGrab )
00169 m_rgCodeModToAction.insert( codemod, pAction );
00170 else
00171 m_rgCodeModToAction.remove( codemod );
00172 }
00173 return !g_bGrabFailed;
00174 }
00175
00176 bool KGlobalAccelPrivate::x11Event( XEvent* pEvent )
00177 {
00178
00179 switch( pEvent->type ) {
00180 case MappingNotify:
00181 x11MappingNotify();
00182 return true;
00183 case XKeyPress:
00184 if( x11KeyPress( pEvent ) )
00185 return true;
00186 default:
00187 return QWidget::x11Event( pEvent );
00188 }
00189 }
00190
00191 void KGlobalAccelPrivate::x11MappingNotify()
00192 {
00193 kdDebug(125) << "KGlobalAccelPrivate::x11MappingNotify()" << endl;
00194 if( m_bEnabled ) {
00195
00196 KKeyServer::initializeMods();
00197 calculateGrabMasks();
00198
00199 updateConnections();
00200 }
00201 }
00202
00203 bool KGlobalAccelPrivate::x11KeyPress( const XEvent *pEvent )
00204 {
00205
00206 if ( !QWidget::keyboardGrabber() && !QApplication::activePopupWidget() )
00207 XUngrabKeyboard( qt_xdisplay(), pEvent->xkey.time );
00208
00209 if( !m_bEnabled )
00210 return false;
00211
00212 CodeMod codemod;
00213 codemod.code = pEvent->xkey.keycode;
00214 codemod.mod = pEvent->xkey.state & (g_keyModMaskXAccel | KKeyServer::MODE_SWITCH);
00215
00216
00217
00218 if( pEvent->xkey.state & KKeyServer::modXNumLock() ) {
00219
00220 uint sym = XKeycodeToKeysym( qt_xdisplay(), codemod.code, 0 );
00221 if( sym >= XK_KP_Space && sym <= XK_KP_9 ) {
00222 if( codemod.mod & KKeyServer::modXShift() )
00223 codemod.mod &= ~KKeyServer::modXShift();
00224 else
00225 codemod.mod |= KKeyServer::modXShift();
00226 }
00227 }
00228
00229 KKeyNative keyNative( pEvent );
00230 KKey key = keyNative;
00231
00232 kdDebug(125) << "x11KeyPress: seek " << key.toStringInternal()
00233 << QString( " keyCodeX: %1 state: %2 keyModX: %3" )
00234 .arg( codemod.code, 0, 16 ).arg( pEvent->xkey.state, 0, 16 ).arg( codemod.mod, 0, 16 ) << endl;
00235
00236
00237 if( !m_rgCodeModToAction.contains( codemod ) ) {
00238 #ifndef NDEBUG
00239 for( CodeModMap::ConstIterator it = m_rgCodeModToAction.begin(); it != m_rgCodeModToAction.end(); ++it ) {
00240 KAccelAction* pAction = *it;
00241 kdDebug(125) << "\tcode: " << QString::number(it.key().code, 16) << " mod: " << QString::number(it.key().mod, 16)
00242 << (pAction ? QString(" name: \"%1\" shortcut: %2").arg(pAction->name()).arg(pAction->shortcut().toStringInternal()) : QString::null)
00243 << endl;
00244 }
00245 #endif
00246 return false;
00247 }
00248 KAccelAction* pAction = m_rgCodeModToAction[codemod];
00249
00250 if( !pAction ) {
00251 QPopupMenu* pMenu = createPopupMenu( 0, KKeySequence(key) );
00252 connect( pMenu, SIGNAL(activated(int)), this, SLOT(slotActivated(int)) );
00253 pMenu->exec( QPoint( 0, 0 ) );
00254 disconnect( pMenu, SIGNAL(activated(int)), this, SLOT(slotActivated(int)));
00255 delete pMenu;
00256 } else if( !pAction->objSlotPtr() || !pAction->isEnabled() )
00257 return false;
00258 else
00259 activate( pAction, KKeySequence(key) );
00260
00261 return true;
00262 }
00263
00264 void KGlobalAccelPrivate::activate( KAccelAction* pAction, const KKeySequence& seq )
00265 {
00266 kdDebug(125) << "KGlobalAccelPrivate::activate( \"" << pAction->name() << "\" ) " << endl;
00267
00268 QRegExp rexPassIndex( "([ ]*int[ ]*)" );
00269 QRegExp rexPassInfo( " QString" );
00270 QRegExp rexIndex( " ([0-9]+)$" );
00271
00272
00273
00274
00275 if( rexPassIndex.search( pAction->methodSlotPtr() ) >= 0 && rexIndex.search( pAction->name() ) >= 0 ) {
00276 int n = rexIndex.cap(1).toInt();
00277 kdDebug(125) << "Calling " << pAction->methodSlotPtr() << " int = " << n << endl;
00278 connect( this, SIGNAL(activated(int)), pAction->objSlotPtr(), pAction->methodSlotPtr() );
00279 emit activated( n );
00280 disconnect( this, SIGNAL(activated(int)), pAction->objSlotPtr(), pAction->methodSlotPtr() );
00281 } else if( rexPassInfo.search( pAction->methodSlotPtr() ) ) {
00282 connect( this, SIGNAL(activated(const QString&, const QString&, const KKeySequence&)), pAction->objSlotPtr(), pAction->methodSlotPtr() );
00283 emit activated( pAction->name(), pAction->label(), seq );
00284 disconnect( this, SIGNAL(activated(const QString&, const QString&, const KKeySequence&)), pAction->objSlotPtr(), pAction->methodSlotPtr() );
00285 } else {
00286 connect( this, SIGNAL(activated()), pAction->objSlotPtr(), pAction->methodSlotPtr() );
00287 emit activated();
00288 disconnect( this, SIGNAL(activated()), pAction->objSlotPtr(), pAction->methodSlotPtr() );
00289 }
00290 }
00291
00292 void KGlobalAccelPrivate::slotActivated( int iAction )
00293 {
00294 KAccelAction* pAction = actions().actionPtr( iAction );
00295 if( pAction )
00296 activate( pAction, KKeySequence() );
00297 }
00298
00299 #include "kglobalaccel_x11.moc"
00300
00301 #endif // !Q_WS_X11