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

tdecore

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

tdecore

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

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • knewstuff
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.