• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kdecore
 

kdecore

  • kdecore
kglobalaccel_x11.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2001,2002 Ellis Whitehead <ellis@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "config.h"
21 
22 #include <tqwindowdefs.h>
23 #ifdef Q_WS_X11
24 
25 #include "kglobalaccel_x11.h"
26 #include "kglobalaccel.h"
27 #include "kkeyserver_x11.h"
28 
29 #include <tqpopupmenu.h>
30 #include <tqregexp.h>
31 #include <tqwidget.h>
32 #include <tqmetaobject.h>
33 #include <tqucomextra_p.h>
34 #include <kapplication.h>
35 #include <kdebug.h>
36 #include <kkeynative.h>
37 
38 #ifdef Q_WS_X11
39 #include <kxerrorhandler.h>
40 #endif
41 
42 #include <X11/X.h>
43 #include <X11/Xlib.h>
44 #include <X11/keysym.h>
45 #include <fixx11h.h>
46 
47 extern "C" {
48  static int XGrabErrorHandler( Display *, XErrorEvent *e ) {
49  if ( e->error_code != BadAccess ) {
50  kdWarning() << "grabKey: got X error " << e->type << " instead of BadAccess\n";
51  }
52  return 1;
53  }
54 }
55 
56 // g_keyModMaskXAccel
57 // mask of modifiers which can be used in shortcuts
58 // (meta, alt, ctrl, shift)
59 // g_keyModMaskXOnOrOff
60 // mask of modifiers where we don't care whether they are on or off
61 // (caps lock, num lock, scroll lock)
62 static uint g_keyModMaskXAccel = 0;
63 static uint g_keyModMaskXOnOrOff = 0;
64 
65 static void calculateGrabMasks()
66 {
67  g_keyModMaskXAccel = KKeyServer::accelModMaskX();
68  g_keyModMaskXOnOrOff =
69  KKeyServer::modXLock() |
70  KKeyServer::modXNumLock() |
71  KKeyServer::modXScrollLock() |
72  KKeyServer::modXModeSwitch();
73  //kdDebug() << "g_keyModMaskXAccel = " << g_keyModMaskXAccel
74  // << "g_keyModMaskXOnOrOff = " << g_keyModMaskXOnOrOff << endl;
75 }
76 
77 //----------------------------------------------------
78 
79 static TQValueList< KGlobalAccelPrivate* >* all_accels = 0;
80 
81 KGlobalAccelPrivate::KGlobalAccelPrivate()
82 : KAccelBase( KAccelBase::NATIVE_KEYS )
83 , m_blocked( false )
84 , m_blockingDisabled( false )
85 , m_suspended( false )
86 {
87  if( all_accels == NULL )
88  all_accels = new TQValueList< KGlobalAccelPrivate* >;
89  all_accels->append( this );
90  m_sConfigGroup = "Global Shortcuts";
91  kapp->installX11EventFilter( this );
92  connect(kapp, TQT_SIGNAL(coreFakeKeyPress(unsigned int)), this, TQT_SLOT(fakeKeyPressed(unsigned int)));
93 }
94 
95 KGlobalAccelPrivate::~KGlobalAccelPrivate()
96 {
97  // TODO: Need to release all grabbed keys if the main window is not shutting down.
98  //for( CodeModMap::ConstIterator it = m_rgCodeModToAction.begin(); it != m_rgCodeModToAction.end(); ++it ) {
99  // const CodeMod& codemod = it.key();
100  //}
101  all_accels->remove( this );
102  if( all_accels->count() == 0 ) {
103  delete all_accels;
104  all_accels = NULL;
105  }
106 }
107 
108 void KGlobalAccelPrivate::setEnabled( bool bEnable )
109 {
110  m_bEnabled = bEnable;
111  updateConnections();
112 }
113 
114 void KGlobalAccelPrivate::blockShortcuts( bool block )
115 {
116  if( all_accels == NULL )
117  return;
118  for( TQValueList< KGlobalAccelPrivate* >::ConstIterator it = all_accels->begin();
119  it != all_accels->end();
120  ++it ) {
121  if( (*it)->m_blockingDisabled )
122  continue;
123  (*it)->m_blocked = block;
124  (*it)->updateConnections();
125  }
126 }
127 
128 void KGlobalAccelPrivate::disableBlocking( bool block )
129 {
130  m_blockingDisabled = block;
131 }
132 
133 bool KGlobalAccelPrivate::isEnabledInternal() const
134 {
135  return KAccelBase::isEnabled() && !m_blocked;
136 }
137 
138 // see #117169 - the bug is hard to reproduce, probably somewhere in X, testcase would be probably
139 // difficult to make, and so on - just don't release the grabs and only ignore the events instead
140 void KGlobalAccelPrivate::suspend( bool s )
141 {
142  m_suspended = s;
143 }
144 
145 bool KGlobalAccelPrivate::emitSignal( Signal )
146 {
147  return false;
148 }
149 
150 bool KGlobalAccelPrivate::connectKey( KAccelAction& action, const KKeyServer::Key& key )
151  { return grabKey( key, true, &action ); }
152 bool KGlobalAccelPrivate::connectKey( const KKeyServer::Key& key )
153  { return grabKey( key, true, 0 ); }
154 bool KGlobalAccelPrivate::disconnectKey( KAccelAction& action, const KKeyServer::Key& key )
155  { return grabKey( key, false, &action ); }
156 bool KGlobalAccelPrivate::disconnectKey( const KKeyServer::Key& key )
157  { return grabKey( key, false, 0 ); }
158 
159 bool KGlobalAccelPrivate::grabKey( const KKeyServer::Key& key, bool bGrab, KAccelAction* pAction )
160 {
161  if( !key.code() ) {
162  kdWarning(125) << "KGlobalAccelPrivate::grabKey( " << key.key().toStringInternal() << ", " << bGrab << ", \"" << (pAction ? pAction->name().latin1() : "(null)") << "\" ): Tried to grab key with null code." << endl;
163  return false;
164  }
165 
166  // Make sure that grab masks have been initialized.
167  if( g_keyModMaskXOnOrOff == 0 )
168  calculateGrabMasks();
169 
170  uchar keyCodeX = key.code();
171  uint keyModX = key.mod() & g_keyModMaskXAccel; // Get rid of any non-relevant bits in mod
172  // HACK: make Alt+Print work
173  // only do this for the Xorg default keyboard keycodes,
174  // other mappings (e.g. evdev) don't need or want it
175  if( key.sym() == XK_Sys_Req && XKeycodeToKeysym( qt_xdisplay(), 111, 0 ) == XK_Print ) {
176  keyModX |= KKeyServer::modXAlt();
177  keyCodeX = 111;
178  }
179 
180 #ifndef __osf__
181 // this crashes under Tru64 so .....
182  kdDebug(125) << TQString(TQString( "grabKey( key: '%1', bGrab: %2 ): keyCodeX: %3 keyModX: %4\n" )
183  .arg( key.key().toStringInternal() ).arg( bGrab )
184  .arg( keyCodeX, 0, 16 ).arg( keyModX, 0, 16 ));
185 #endif
186  if( !keyCodeX )
187  return false;
188 
189 #ifdef Q_WS_X11
190  KXErrorHandler handler( XGrabErrorHandler );
191 #endif
192  // We'll have to grab 8 key modifier combinations in order to cover all
193  // combinations of CapsLock, NumLock, ScrollLock.
194  // Does anyone with more X-savvy know how to set a mask on qt_xrootwin so that
195  // the irrelevant bits are always ignored and we can just make one XGrabKey
196  // call per accelerator? -- ellis
197 #ifndef NDEBUG
198  TQString sDebug = TQString("\tcode: 0x%1 state: 0x%2 | ").arg(keyCodeX,0,16).arg(keyModX,0,16);
199 #endif
200  uint keyModMaskX = ~g_keyModMaskXOnOrOff;
201  for( uint irrelevantBitsMask = 0; irrelevantBitsMask <= 0xff; irrelevantBitsMask++ ) {
202  if( (irrelevantBitsMask & keyModMaskX) == 0 ) {
203 #ifndef NDEBUG
204  sDebug += TQString("0x%3, ").arg(irrelevantBitsMask, 0, 16);
205 #endif
206  if( bGrab )
207  XGrabKey( qt_xdisplay(), keyCodeX, keyModX | irrelevantBitsMask,
208  qt_xrootwin(), True, GrabModeAsync, GrabModeSync );
209  else
210  XUngrabKey( qt_xdisplay(), keyCodeX, keyModX | irrelevantBitsMask, qt_xrootwin() );
211  }
212  }
213 #ifndef NDEBUG
214  kdDebug(125) << sDebug << endl;
215 #endif
216 
217  bool failed = false;
218  if( bGrab ) {
219 #ifdef Q_WS_X11
220  failed = handler.error( true ); // sync now
221 #endif
222  // If grab failed, then ungrab any grabs that could possibly succeed
223  if( failed ) {
224  kdDebug(125) << "grab failed!\n";
225  for( uint m = 0; m <= 0xff; m++ ) {
226  if(( m & keyModMaskX ) == 0 )
227  XUngrabKey( qt_xdisplay(), keyCodeX, keyModX | m, qt_xrootwin() );
228  }
229  }
230  }
231  if( !failed )
232  {
233  CodeMod codemod;
234  codemod.code = keyCodeX;
235  codemod.mod = keyModX;
236  if( key.mod() & KKeyServer::MODE_SWITCH )
237  codemod.mod |= KKeyServer::MODE_SWITCH;
238 
239  if( bGrab )
240  m_rgCodeModToAction.insert( codemod, pAction );
241  else
242  m_rgCodeModToAction.remove( codemod );
243  }
244  return !failed;
245 }
246 
247 bool KGlobalAccelPrivate::x11Event( XEvent* pEvent )
248 {
249  //kdDebug(125) << "x11EventFilter( type = " << pEvent->type << " )" << endl;
250  switch( pEvent->type ) {
251  case MappingNotify:
252  XRefreshKeyboardMapping( &pEvent->xmapping );
253  x11MappingNotify();
254  return false;
255  case XKeyPress:
256  if( x11KeyPress( pEvent ) )
257  return true;
258  default:
259  return TQWidget::x11Event( pEvent );
260  }
261 }
262 
263 void KGlobalAccelPrivate::x11MappingNotify()
264 {
265  kdDebug(125) << "KGlobalAccelPrivate::x11MappingNotify()" << endl;
266  // Maybe the X modifier map has been changed.
267  KKeyServer::initializeMods();
268  calculateGrabMasks();
269  // Do new XGrabKey()s.
270  updateConnections();
271 }
272 
273 void KGlobalAccelPrivate::fakeKeyPressed(unsigned int keyCode) {
274  CodeMod codemod;
275  codemod.code = keyCode;
276  codemod.mod = 0;
277 
278  KKey key = (keyCode, 0);
279 
280  kdDebug(125) << "fakeKeyPressed: seek " << key.toStringInternal()
281  << TQString(TQString( " keyCodeX: %1 keyCode: %2 keyModX: %3" )
282  .arg( codemod.code, 0, 16 ).arg( keyCode, 0, 16 ).arg( codemod.mod, 0, 16 )) << endl;
283 
284  // Search for which accelerator activated this event:
285  if( !m_rgCodeModToAction.contains( codemod ) ) {
286 #ifndef NDEBUG
287  for( CodeModMap::ConstIterator it = m_rgCodeModToAction.begin(); it != m_rgCodeModToAction.end(); ++it ) {
288  KAccelAction* pAction = *it;
289  kdDebug(125) << "\tcode: " << TQString::number(it.key().code, 16) << " mod: " << TQString::number(it.key().mod, 16)
290  << (pAction ? TQString(" name: \"%1\" shortcut: %2").arg(pAction->name()).arg(pAction->shortcut().toStringInternal()) : TQString())
291  << endl;
292  }
293 #endif
294  return;
295  }
296 
297  KAccelAction* pAction = m_rgCodeModToAction[codemod];
298 
299  if( !pAction ) {
300  static bool recursion_block = false;
301  if( !recursion_block ) {
302  recursion_block = true;
303  TQPopupMenu* pMenu = createPopupMenu( 0, KKeySequence(key) );
304  connect( pMenu, TQT_SIGNAL(activated(int)), this, TQT_SLOT(slotActivated(int)) );
305  pMenu->exec( TQPoint( 0, 0 ) );
306  disconnect( pMenu, TQT_SIGNAL(activated(int)), this, TQT_SLOT(slotActivated(int)));
307  delete pMenu;
308  recursion_block = false;
309  }
310  } else if( !pAction->objSlotPtr() || !pAction->isEnabled() )
311  return;
312  else
313  activate( pAction, KKeySequence(key) );
314 }
315 
316 bool KGlobalAccelPrivate::x11KeyPress( const XEvent *pEvent )
317 {
318  // do not change this line unless you really really know what you are doing (Matthias)
319  if ( !TQWidget::keyboardGrabber() && !TQApplication::activePopupWidget() ) {
320  XUngrabKeyboard( qt_xdisplay(), pEvent->xkey.time );
321  XFlush( qt_xdisplay()); // avoid X(?) bug
322  }
323 
324  if( !isEnabledInternal() || m_suspended )
325  return false;
326 
327  CodeMod codemod;
328  codemod.code = pEvent->xkey.keycode;
329  codemod.mod = pEvent->xkey.state & (g_keyModMaskXAccel | KKeyServer::MODE_SWITCH);
330 
331  // If numlock is active and a keypad key is pressed, XOR the SHIFT state.
332  // e.g., KP_4 => Shift+KP_Left, and Shift+KP_4 => KP_Left.
333  if( pEvent->xkey.state & KKeyServer::modXNumLock() ) {
334  // TODO: what's the xor operator in c++?
335  uint sym = XKeycodeToKeysym( qt_xdisplay(), codemod.code, 0 );
336  // If this is a keypad key,
337  if( sym >= XK_KP_Space && sym <= XK_KP_9 ) {
338  switch( sym ) {
339  // Leave the following keys unaltered
340  // FIXME: The proper solution is to see which keysyms don't change when shifted.
341  case XK_KP_Multiply:
342  case XK_KP_Add:
343  case XK_KP_Subtract:
344  case XK_KP_Divide:
345  break;
346  default:
347  if( codemod.mod & KKeyServer::modXShift() )
348  codemod.mod &= ~KKeyServer::modXShift();
349  else
350  codemod.mod |= KKeyServer::modXShift();
351  }
352  }
353  }
354 
355  KKeyNative keyNative( pEvent );
356  KKey key = keyNative;
357 
358  kdDebug(125) << "x11KeyPress: seek " << key.toStringInternal()
359  << TQString(TQString( " keyCodeX: %1 state: %2 keyModX: %3" )
360  .arg( codemod.code, 0, 16 ).arg( pEvent->xkey.state, 0, 16 ).arg( codemod.mod, 0, 16 )) << endl;
361 
362  // Search for which accelerator activated this event:
363  if( !m_rgCodeModToAction.contains( codemod ) ) {
364 #ifndef NDEBUG
365  for( CodeModMap::ConstIterator it = m_rgCodeModToAction.begin(); it != m_rgCodeModToAction.end(); ++it ) {
366  KAccelAction* pAction = *it;
367  kdDebug(125) << "\tcode: " << TQString::number(it.key().code, 16) << " mod: " << TQString::number(it.key().mod, 16)
368  << (pAction ? TQString(" name: \"%1\" shortcut: %2").arg(pAction->name()).arg(pAction->shortcut().toStringInternal()) : TQString())
369  << endl;
370  }
371 #endif
372  return false;
373  }
374 
375  KAccelAction* pAction = m_rgCodeModToAction[codemod];
376 
377  if( !pAction ) {
378  static bool recursion_block = false;
379  if( !recursion_block ) {
380  recursion_block = true;
381  TQPopupMenu* pMenu = createPopupMenu( 0, KKeySequence(key) );
382  connect( pMenu, TQT_SIGNAL(activated(int)), this, TQT_SLOT(slotActivated(int)) );
383  pMenu->exec( TQPoint( 0, 0 ) );
384  disconnect( pMenu, TQT_SIGNAL(activated(int)), this, TQT_SLOT(slotActivated(int)));
385  delete pMenu;
386  recursion_block = false;
387  }
388  } else if( !pAction->objSlotPtr() || !pAction->isEnabled() )
389  return false;
390  else
391  activate( pAction, KKeySequence(key) );
392 
393  return true;
394 }
395 
396 void KGlobalAccelPrivate::activate( KAccelAction* pAction, const KKeySequence& seq )
397 {
398  kdDebug(125) << "KGlobalAccelPrivate::activate( \"" << pAction->name() << "\" ) " << endl;
399 
400  TQRegExp rexPassIndex( "([ ]*int[ ]*)" );
401  TQRegExp rexPassInfo( " TQString" );
402  TQRegExp rexIndex( " ([0-9]+)$" );
403 
404  // If the slot to be called accepts an integer index
405  // and an index is present at the end of the action's name,
406  // then send the slot the given index #.
407  if( rexPassIndex.search( pAction->methodSlotPtr() ) >= 0 && rexIndex.search( pAction->name() ) >= 0 ) {
408  int n = rexIndex.cap(1).toInt();
409  kdDebug(125) << "Calling " << pAction->methodSlotPtr() << " int = " << n << endl;
410  int slot_id = pAction->objSlotPtr()->metaObject()->findSlot( normalizeSignalSlot( pAction->methodSlotPtr() ).data() + 1, true );
411  if( slot_id >= 0 ) {
412  TQUObject o[2];
413  static_TQUType_int.set(o+1,n);
414  const_cast< TQObject* >( pAction->objSlotPtr())->qt_invoke( slot_id, o );
415  }
416  } else if( rexPassInfo.search( pAction->methodSlotPtr() ) ) {
417  int slot_id = pAction->objSlotPtr()->metaObject()->findSlot( normalizeSignalSlot( pAction->methodSlotPtr() ).data() + 1, true );
418  if( slot_id >= 0 ) {
419  TQUObject o[4];
420  static_TQUType_TQString.set(o+1,pAction->name());
421  static_TQUType_TQString.set(o+2,pAction->label());
422  static_TQUType_ptr.set(o+3,&seq);
423  const_cast< TQObject* >( pAction->objSlotPtr())->qt_invoke( slot_id, o );
424  }
425  } else {
426  int slot_id = pAction->objSlotPtr()->metaObject()->findSlot( normalizeSignalSlot( pAction->methodSlotPtr() ).data() + 1, true );
427  if( slot_id >= 0 )
428  const_cast< TQObject* >( pAction->objSlotPtr())->qt_invoke( slot_id, 0 );
429  }
430 }
431 
432 void KGlobalAccelPrivate::slotActivated( int iAction )
433 {
434  KAccelAction* pAction = KAccelBase::actions().actionPtr( iAction );
435  if( pAction )
436  activate( pAction, KKeySequence() );
437 }
438 
439 #include "kglobalaccel_x11.moc"
440 
441 #endif // !Q_WS_X11

kdecore

Skip menu "kdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdecore

Skip menu "kdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kdecore by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |