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

tdecore

  • tdecore
tdeglobalaccel_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 TQ_WS_X11
24
25#include "tdeglobalaccel_x11.h"
26#include "tdeglobalaccel.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 <private/tqucomextra_p.h>
34#include <tdeapplication.h>
35#include <kdebug.h>
36#include <kkeynative.h>
37
38#ifdef TQ_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
48extern "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)
63static uint g_keyModMaskXAccel = 0;
64static uint g_keyModMaskXOnOrOff = 0;
65
66static 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
80static TQValueList< TDEGlobalAccelPrivate* >* all_accels = 0;
81
82TDEGlobalAccelPrivate::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 tdeApp->installX11EventFilter( this );
93 connect(tdeApp, TQ_SIGNAL(coreFakeKeyPress(unsigned int)), this, TQ_SLOT(fakeKeyPressed(unsigned int)));
94}
95
96TDEGlobalAccelPrivate::~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
109void TDEGlobalAccelPrivate::setEnabled( bool bEnable )
110{
111 m_bEnabled = bEnable;
112 updateConnections();
113}
114
115void 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
129void TDEGlobalAccelPrivate::disableBlocking( bool block )
130{
131 m_blockingDisabled = block;
132}
133
134bool 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
141void TDEGlobalAccelPrivate::suspend( bool s )
142{
143 m_suspended = s;
144}
145
146bool TDEGlobalAccelPrivate::emitSignal( Signal )
147{
148 return false;
149}
150
151bool TDEGlobalAccelPrivate::connectKey( TDEAccelAction& action, const KKeyServer::Key& key )
152 { return grabKey( key, true, &action ); }
153bool TDEGlobalAccelPrivate::connectKey( const KKeyServer::Key& key )
154 { return grabKey( key, true, 0 ); }
155bool TDEGlobalAccelPrivate::disconnectKey( TDEAccelAction& action, const KKeyServer::Key& key )
156 { return grabKey( key, false, &action ); }
157bool TDEGlobalAccelPrivate::disconnectKey( const KKeyServer::Key& key )
158 { return grabKey( key, false, 0 ); }
159
160bool 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
172 uchar keyCodeX = key.code();
173 uint keyModX = key.mod() & g_keyModMaskXAccel; // Get rid of any non-relevant bits in mod
174 // HACK: make Alt+Print work
175 // only do this for the Xorg default keyboard keycodes,
176 // other mappings (e.g. evdev) don't need or want it
177 if( key.sym() == XK_Sys_Req && XkbKeycodeToKeysym( tqt_xdisplay(), 111, 0, 0 ) == XK_Print ) {
178 keyModX |= KKeyServer::modXAlt();
179 keyCodeX = 111;
180 }
181 // If the MODE_SWITCH modifier was set in the original key, and was truncated in g_keyModMaskXAccel, XGrabKey will grab the wrong key
182 // See Bug 1676
183 if ((key.mod() & KKeyServer::MODE_SWITCH) && (!(g_keyModMaskXAccel & KKeyServer::MODE_SWITCH))) {
184 // FIXME
185 // Is there any way to make AltGr-based character sequences work with XGrabKey?
186 kdWarning(125) << "TDEGlobalAccelPrivate::grabKey( " << key.key().toStringInternal() << ", " << bGrab << ", \"" << (pAction ? pAction->name().latin1() : "(null)") << "\" ): Tried to grab key requiring ISO_Level3_Shift (AltGr) sequence." << endl;
187 return false;
188 }
189
190 kdDebug(125) << TQString(TQString( "grabKey( key: '%1', bGrab: %2 ): keyCodeX: %3 keyModX: %4\n" )
191 .arg( key.key().toStringInternal() ).arg( bGrab )
192 .arg( keyCodeX, 0, 16 ).arg( keyModX, 0, 16 ));
193 if( !keyCodeX ) {
194 return false;
195 }
196
197#ifdef TQ_WS_X11
198 KXErrorHandler handler( XGrabErrorHandler );
199#endif
200 // We'll have to grab 8 key modifier combinations in order to cover all
201 // combinations of CapsLock, NumLock, ScrollLock.
202 // Does anyone with more X-savvy know how to set a mask on tqt_xrootwin so that
203 // the irrelevant bits are always ignored and we can just make one XGrabKey
204 // call per accelerator? -- ellis
205#ifndef NDEBUG
206 TQString sDebug = TQString("\tcode: 0x%1 state: 0x%2 | ").arg(keyCodeX,0,16).arg(keyModX,0,16);
207#endif
208 uint keyModMaskX = ~g_keyModMaskXOnOrOff;
209 for( uint irrelevantBitsMask = 0; irrelevantBitsMask <= 0xff; irrelevantBitsMask++ ) {
210 if( (irrelevantBitsMask & keyModMaskX) == 0 ) {
211#ifndef NDEBUG
212 sDebug += TQString("0x%3, ").arg(irrelevantBitsMask, 0, 16);
213#endif
214 if( bGrab )
215 XGrabKey( tqt_xdisplay(), keyCodeX, keyModX | irrelevantBitsMask,
216 tqt_xrootwin(), True, GrabModeAsync, GrabModeSync );
217 else
218 XUngrabKey( tqt_xdisplay(), keyCodeX, keyModX | irrelevantBitsMask, tqt_xrootwin() );
219 }
220 }
221#ifndef NDEBUG
222 kdDebug(125) << sDebug << endl;
223#endif
224
225 bool failed = false;
226 if( bGrab ) {
227#ifdef TQ_WS_X11
228 failed = handler.error( true ); // sync now
229#endif
230 // If grab failed, then ungrab any grabs that could possibly succeed
231 if( failed ) {
232 kdDebug(125) << "grab failed!\n";
233 for( uint m = 0; m <= 0xff; m++ ) {
234 if(( m & keyModMaskX ) == 0 )
235 XUngrabKey( tqt_xdisplay(), keyCodeX, keyModX | m, tqt_xrootwin() );
236 }
237 }
238 }
239 if( !failed )
240 {
241 CodeMod codemod;
242 codemod.code = keyCodeX;
243 codemod.mod = keyModX;
244 if( key.mod() & KKeyServer::MODE_SWITCH )
245 codemod.mod |= KKeyServer::MODE_SWITCH;
246
247 if( bGrab )
248 m_rgCodeModToAction.insert( codemod, pAction );
249 else
250 m_rgCodeModToAction.remove( codemod );
251 }
252 return !failed;
253}
254
255bool TDEGlobalAccelPrivate::x11Event( XEvent* pEvent )
256{
257 //kdDebug(125) << "x11EventFilter( type = " << pEvent->type << " )" << endl;
258 switch( pEvent->type ) {
259 case MappingNotify:
260 XRefreshKeyboardMapping( &pEvent->xmapping );
261 x11MappingNotify();
262 return false;
263 case XKeyPress:
264 if( x11KeyPress( pEvent ) ) {
265 return true;
266 }
267 default:
268 return TQWidget::x11Event( pEvent );
269 }
270}
271
272void TDEGlobalAccelPrivate::x11MappingNotify()
273{
274 kdDebug(125) << "TDEGlobalAccelPrivate::x11MappingNotify()" << endl;
275 // Maybe the X modifier map has been changed.
276 KKeyServer::initializeMods();
277 calculateGrabMasks();
278 // Do new XGrabKey()s.
279 updateConnections();
280}
281
282void TDEGlobalAccelPrivate::fakeKeyPressed(unsigned int keyCode) {
283 CodeMod codemod;
284 codemod.code = keyCode;
285 codemod.mod = 0;
286
287 KKey key(keyCode, 0);
288
289 kdDebug(125) << "fakeKeyPressed: seek " << key.toStringInternal()
290 << TQString(TQString( " keyCodeX: %1 keyCode: %2 keyModX: %3" )
291 .arg( codemod.code, 0, 16 ).arg( keyCode, 0, 16 ).arg( codemod.mod, 0, 16 )) << endl;
292
293 // Search for which accelerator activated this event:
294 if( !m_rgCodeModToAction.contains( codemod ) ) {
295#ifndef NDEBUG
296 for( CodeModMap::ConstIterator it = m_rgCodeModToAction.begin(); it != m_rgCodeModToAction.end(); ++it ) {
297 TDEAccelAction* pAction = *it;
298 kdDebug(125) << "\tcode: " << TQString::number(it.key().code, 16) << " mod: " << TQString::number(it.key().mod, 16)
299 << (pAction ? TQString(" name: \"%1\" shortcut: %2").arg(pAction->name()).arg(pAction->shortcut().toStringInternal()) : TQString())
300 << endl;
301 }
302#endif
303 return;
304 }
305
306 TDEAccelAction* pAction = m_rgCodeModToAction[codemod];
307
308 if( !pAction ) {
309 static bool recursion_block = false;
310 if( !recursion_block ) {
311 recursion_block = true;
312 TQPopupMenu* pMenu = createPopupMenu( 0, KKeySequence(key) );
313 connect( pMenu, TQ_SIGNAL(activated(int)), this, TQ_SLOT(slotActivated(int)) );
314 pMenu->exec( TQPoint( 0, 0 ) );
315 disconnect( pMenu, TQ_SIGNAL(activated(int)), this, TQ_SLOT(slotActivated(int)));
316 delete pMenu;
317 recursion_block = false;
318 }
319 } else if( !pAction->objSlotPtr() || !pAction->isEnabled() )
320 return;
321 else
322 activate( pAction, KKeySequence(key) );
323}
324
325bool TDEGlobalAccelPrivate::x11KeyPress( const XEvent *pEvent )
326{
327 // do not change this line unless you really really know what you are doing (Matthias)
328 if ( !TQWidget::keyboardGrabber() && !TQApplication::activePopupWidget() ) {
329 XUngrabKeyboard( tqt_xdisplay(), pEvent->xkey.time );
330 XFlush( tqt_xdisplay()); // avoid X(?) bug
331 }
332
333 if( !isEnabledInternal() || m_suspended ) {
334 return false;
335 }
336
337 CodeMod codemod;
338 codemod.code = pEvent->xkey.keycode;
339 codemod.mod = pEvent->xkey.state & (g_keyModMaskXAccel | KKeyServer::MODE_SWITCH);
340
341 // If numlock is active and a keypad key is pressed, XOR the SHIFT state.
342 // e.g., KP_4 => Shift+KP_Left, and Shift+KP_4 => KP_Left.
343 if( pEvent->xkey.state & KKeyServer::modXNumLock() ) {
344 // TODO: what's the xor operator in c++?
345 uint sym = XkbKeycodeToKeysym( tqt_xdisplay(), codemod.code, 0, 0 );
346 // If this is a keypad key,
347 if( sym >= XK_KP_Space && sym <= XK_KP_9 ) {
348 switch( sym ) {
349 // Leave the following keys unaltered
350 // FIXME: The proper solution is to see which keysyms don't change when shifted.
351 case XK_KP_Multiply:
352 case XK_KP_Add:
353 case XK_KP_Subtract:
354 case XK_KP_Divide:
355 break;
356 default:
357 if( codemod.mod & KKeyServer::modXShift() )
358 codemod.mod &= ~KKeyServer::modXShift();
359 else
360 codemod.mod |= KKeyServer::modXShift();
361 }
362 }
363 }
364
365 KKeyNative keyNative( pEvent );
366 KKey key = keyNative;
367
368 kdDebug(125) << "x11KeyPress: seek " << key.toStringInternal()
369 << TQString(TQString( " keyCodeX: %1 state: %2 keyModX: %3" )
370 .arg( codemod.code, 0, 16 ).arg( pEvent->xkey.state, 0, 16 ).arg( codemod.mod, 0, 16 )) << endl;
371
372 // Search for which accelerator activated this event:
373 if( !m_rgCodeModToAction.contains( codemod ) ) {
374#ifndef NDEBUG
375 for( CodeModMap::ConstIterator it = m_rgCodeModToAction.begin(); it != m_rgCodeModToAction.end(); ++it ) {
376 TDEAccelAction* pAction = *it;
377 kdDebug(125) << "\tcode: " << TQString::number(it.key().code, 16) << " mod: " << TQString::number(it.key().mod, 16)
378 << (pAction ? TQString(" name: \"%1\" shortcut: %2").arg(pAction->name()).arg(pAction->shortcut().toStringInternal()) : TQString())
379 << endl;
380 }
381#endif
382 return false;
383 }
384
385 TDEAccelAction* pAction = m_rgCodeModToAction[codemod];
386
387 if( !pAction ) {
388 static bool recursion_block = false;
389 if( !recursion_block ) {
390 recursion_block = true;
391 TQPopupMenu* pMenu = createPopupMenu( 0, KKeySequence(key) );
392 connect( pMenu, TQ_SIGNAL(activated(int)), this, TQ_SLOT(slotActivated(int)) );
393 pMenu->exec( TQPoint( 0, 0 ) );
394 disconnect( pMenu, TQ_SIGNAL(activated(int)), this, TQ_SLOT(slotActivated(int)));
395 delete pMenu;
396 recursion_block = false;
397 }
398 } else if( !pAction->objSlotPtr() || !pAction->isEnabled() )
399 return false;
400 else
401 activate( pAction, KKeySequence(key) );
402
403 return true;
404}
405
406void TDEGlobalAccelPrivate::activate( TDEAccelAction* pAction, const KKeySequence& seq )
407{
408 kdDebug(125) << "TDEGlobalAccelPrivate::activate( \"" << pAction->name() << "\" ) " << endl;
409
410 TQRegExp rexPassIndex( "([ ]*int[ ]*)" );
411 TQRegExp rexPassInfo( " TQString" );
412 TQRegExp rexIndex( " ([0-9]+)$" );
413
414 // If the slot to be called accepts an integer index
415 // and an index is present at the end of the action's name,
416 // then send the slot the given index #.
417 if( rexPassIndex.search( pAction->methodSlotPtr() ) >= 0 && rexIndex.search( pAction->name() ) >= 0 ) {
418 int n = rexIndex.cap(1).toInt();
419 kdDebug(125) << "Calling " << pAction->methodSlotPtr() << " int = " << n << endl;
420 int slot_id = pAction->objSlotPtr()->metaObject()->findSlot( normalizeSignalSlot( pAction->methodSlotPtr() ).data() + 1, true );
421 if( slot_id >= 0 ) {
422 TQUObject o[2];
423 static_QUType_int.set(o+1,n);
424 const_cast< TQObject* >( pAction->objSlotPtr())->tqt_invoke( slot_id, o );
425 }
426 } else if( rexPassInfo.search( pAction->methodSlotPtr() ) ) {
427 int slot_id = pAction->objSlotPtr()->metaObject()->findSlot( normalizeSignalSlot( pAction->methodSlotPtr() ).data() + 1, true );
428 if( slot_id >= 0 ) {
429 TQUObject o[4];
430 static_QUType_TQString.set(o+1,pAction->name());
431 static_QUType_TQString.set(o+2,pAction->label());
432 static_QUType_ptr.set(o+3,&seq);
433 const_cast< TQObject* >( pAction->objSlotPtr())->tqt_invoke( slot_id, o );
434 }
435 } else {
436 int slot_id = pAction->objSlotPtr()->metaObject()->findSlot( normalizeSignalSlot( pAction->methodSlotPtr() ).data() + 1, true );
437 if( slot_id >= 0 )
438 const_cast< TQObject* >( pAction->objSlotPtr())->tqt_invoke( slot_id, 0 );
439 }
440}
441
442void TDEGlobalAccelPrivate::slotActivated( int iAction )
443{
444 TDEAccelAction* pAction = TDEAccelBase::actions().actionPtr( iAction );
445 if( pAction )
446 activate( pAction, KKeySequence() );
447}
448
449#include "tdeglobalaccel_x11.moc"
450
451#endif // !TQ_WS_X11
KKeyNative
Representation of a key in the format native of the windowing system (eg.
Definition: kkeynative.h:38
KKeySequence
A KKeySequence object holds a sequence of up to 4 keys.
Definition: tdeshortcut.h:289
KKey
A KKey object represents a single key with possible modifiers (Shift, Ctrl, Alt, Win).
Definition: tdeshortcut.h:41
KXErrorHandler
This class simplifies handling of X errors.
Definition: kxerrorhandler.h:58
TDEGlobal::kdWarning
kdbgstream kdWarning(int area=0)
Returns a warning stream.
Definition: kdebug.cpp:376
TDEGlobal::kdDebug
kdbgstream kdDebug(int area=0)
Returns a debug stream.
Definition: kdebug.cpp:371
TDEGlobal::endl
kdbgstream & endl(kdbgstream &s)
Prints an "\n".
Definition: kdebug.h:430
KKeyServer
A collection of functions for the conversion of key presses and their modifiers from the window syste...
Definition: kkeyserver_x11.h:35
KKeyServer::modXModeSwitch
uint modXModeSwitch()
Returns the X11 Mode_switch modifier mask/flag.
KKeyServer::modXShift
uint modXShift()
Returns the X11 Shift modifier mask/flag.
KKeyServer::modXNumLock
uint modXNumLock()
Returns the X11 NumLock modifier mask/flag.
KKeyServer::modXLock
uint modXLock()
Returns the X11 Lock modifier mask/flag.
KKeyServer::accelModMaskX
uint accelModMaskX()
Returns bitwise OR'ed mask containing Shift, Ctrl, Alt, and Win (if available).
KKeyServer::modXAlt
uint modXAlt()
Returns the X11 Alt (Mod1) modifier mask/flag.
KKeyServer::modXScrollLock
uint modXScrollLock()
Returns the X11 ScrollLock modifier mask/flag.
KKeyServer::initializeMods
bool initializeMods()
TODO: please document.
TDEStdAccel::key
int key(StdAccel id)
Definition: tdestdaccel.cpp:383
KKeyServer::Key
Represents a key press.
Definition: kkeyserver_x11.h:138

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
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.4
This website is maintained by Timothy Pearson.