2009-04-22 20:06:31 +00:00
|
|
|
#ifdef __WIN32 // Q_OS_WIN32 isn't available at this point
|
|
|
|
#define _WIN32_WINNT 0x0430
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2009-04-19 21:32:18 +00:00
|
|
|
#include "hotkeywrapper.hh"
|
2013-11-16 18:34:09 +00:00
|
|
|
#include "gddebug.hh"
|
2009-04-19 21:32:18 +00:00
|
|
|
|
2009-04-21 18:27:26 +00:00
|
|
|
#ifdef Q_WS_X11
|
|
|
|
#include <X11/Xlibint.h>
|
|
|
|
#endif
|
|
|
|
|
2014-03-17 14:53:47 +00:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
#include "mainwindow.hh"
|
|
|
|
#endif
|
|
|
|
|
2009-04-19 21:32:18 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-11-20 13:43:55 +00:00
|
|
|
QHotkeyApplication::QHotkeyApplication( int & argc, char ** argv ):
|
2014-03-21 12:46:11 +00:00
|
|
|
QtSingleApplication( argc, argv )
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
, mainWindow( 0 )
|
|
|
|
#endif
|
2010-11-20 13:43:55 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QHotkeyApplication::QHotkeyApplication( QString const & id,
|
|
|
|
int & argc, char ** argv ):
|
|
|
|
QtSingleApplication( id, argc, argv )
|
2009-04-19 21:32:18 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-06-28 15:14:07 +00:00
|
|
|
void QHotkeyApplication::addDataCommiter( DataCommitter & d )
|
|
|
|
{
|
|
|
|
dataCommitters.append( &d );
|
|
|
|
}
|
|
|
|
|
|
|
|
void QHotkeyApplication::removeDataCommiter( DataCommitter & d )
|
|
|
|
{
|
|
|
|
dataCommitters.removeAll( &d );
|
|
|
|
}
|
|
|
|
|
|
|
|
void QHotkeyApplication::commitData( QSessionManager & s )
|
|
|
|
{
|
|
|
|
for( int x = 0; x < dataCommitters.size(); ++x )
|
|
|
|
dataCommitters[ x ]->commitData( s );
|
|
|
|
}
|
|
|
|
|
2009-04-19 21:32:18 +00:00
|
|
|
void QHotkeyApplication::registerWrapper(HotkeyWrapper *wrapper)
|
|
|
|
{
|
|
|
|
if (wrapper && !hotkeyWrappers.contains(wrapper))
|
|
|
|
hotkeyWrappers.append(wrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QHotkeyApplication::unregisterWrapper(HotkeyWrapper *wrapper)
|
|
|
|
{
|
|
|
|
if (wrapper && hotkeyWrappers.contains(wrapper))
|
|
|
|
hotkeyWrappers.removeAll(wrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2009-04-21 18:27:26 +00:00
|
|
|
HotkeyStruct::HotkeyStruct( quint32 key_, quint32 key2_, quint32 modifier_,
|
2009-04-21 21:07:15 +00:00
|
|
|
int handle_, int id_ ):
|
2009-04-21 18:27:26 +00:00
|
|
|
key( key_ ),
|
|
|
|
key2( key2_ ),
|
|
|
|
modifier( modifier_ ),
|
2009-04-21 21:07:15 +00:00
|
|
|
handle( handle_ ),
|
|
|
|
id( id_ )
|
2013-04-05 12:25:34 +00:00
|
|
|
#ifdef Q_OS_MACX
|
|
|
|
, hkRef( 0 )
|
|
|
|
, hkRef2( 0 )
|
|
|
|
#endif
|
2009-04-19 21:32:18 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-12-04 08:21:13 +00:00
|
|
|
#if !defined(Q_OS_MAC) && !defined(Q_WS_QWS)
|
2009-04-21 18:27:26 +00:00
|
|
|
HotkeyWrapper::HotkeyWrapper(QObject *parent) : QThread( parent ),
|
2009-04-19 21:32:18 +00:00
|
|
|
state2(false)
|
|
|
|
{
|
2011-11-17 13:01:42 +00:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
hwnd=(HWND)((static_cast<QMainWindow*>(parent))->winId());
|
|
|
|
#else
|
2009-04-19 21:32:18 +00:00
|
|
|
init();
|
2011-11-17 13:01:42 +00:00
|
|
|
#endif
|
2009-04-19 21:32:18 +00:00
|
|
|
(static_cast<QHotkeyApplication*>(qApp))->registerWrapper(this);
|
|
|
|
}
|
|
|
|
|
2009-04-21 21:07:15 +00:00
|
|
|
HotkeyWrapper::~HotkeyWrapper()
|
|
|
|
{
|
|
|
|
unregister();
|
|
|
|
}
|
|
|
|
|
2009-04-19 21:32:18 +00:00
|
|
|
void HotkeyWrapper::waitKey2()
|
|
|
|
{
|
|
|
|
state2 = false;
|
2009-04-22 15:29:28 +00:00
|
|
|
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
|
|
|
|
if ( keyToUngrab != grabbedKeys.end() )
|
|
|
|
{
|
|
|
|
ungrabKey( keyToUngrab );
|
|
|
|
keyToUngrab = grabbedKeys.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2009-04-19 21:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HotkeyWrapper::checkState(quint32 vk, quint32 mod)
|
|
|
|
{
|
2009-04-22 15:29:28 +00:00
|
|
|
if ( state2 )
|
|
|
|
{ // wait for 2nd key
|
|
|
|
|
|
|
|
waitKey2(); // Cancel the 2nd-key wait stage
|
|
|
|
|
|
|
|
if (state2waiter.key2 == vk && state2waiter.modifier == mod)
|
|
|
|
{
|
|
|
|
emit hotkeyActivated( state2waiter.handle );
|
|
|
|
return true;
|
2009-04-19 21:32:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < hotkeys.count(); i++) {
|
2009-04-21 18:27:26 +00:00
|
|
|
|
2009-04-19 21:32:18 +00:00
|
|
|
const HotkeyStruct &hs = hotkeys.at(i);
|
|
|
|
|
|
|
|
if (hs.key == vk && hs.modifier == mod) {
|
2009-04-22 20:06:31 +00:00
|
|
|
|
|
|
|
#ifdef Q_WS_WIN32
|
2013-06-10 13:49:42 +00:00
|
|
|
|
|
|
|
if( hs.key2 != 0 || ( mod == MOD_CONTROL && ( vk == VK_INSERT || vk == 'c' || vk == 'C' ) ) )
|
2009-04-22 20:06:31 +00:00
|
|
|
{
|
2013-06-10 13:49:42 +00:00
|
|
|
// Pass-through first part of compound hotkey or clipdoard copy command
|
|
|
|
|
|
|
|
INPUT i[ 10 ];
|
2009-04-22 20:06:31 +00:00
|
|
|
memset( i, 0, sizeof( i ) );
|
2013-06-10 13:49:42 +00:00
|
|
|
int nextKeyNom = 0;
|
|
|
|
short emulateModKeys = 0;
|
2013-04-29 19:35:26 +00:00
|
|
|
|
2013-06-10 13:49:42 +00:00
|
|
|
// If modifier keys aren't pressed it looks like emulation
|
|
|
|
// We then also emulate full sequence
|
2013-04-29 19:35:26 +00:00
|
|
|
|
2013-06-10 13:49:42 +00:00
|
|
|
if( ( mod & MOD_ALT ) != 0 && ( GetAsyncKeyState( VK_MENU ) & 0x8000 ) == 0 )
|
2013-04-29 19:35:26 +00:00
|
|
|
{
|
2013-06-10 13:49:42 +00:00
|
|
|
emulateModKeys |= MOD_ALT;
|
|
|
|
i[ nextKeyNom ].type = INPUT_KEYBOARD;
|
|
|
|
i[ nextKeyNom ].ki.wVk = VK_MENU;
|
|
|
|
nextKeyNom += 1;
|
2013-04-29 19:35:26 +00:00
|
|
|
}
|
2013-06-10 13:49:42 +00:00
|
|
|
if( ( mod & MOD_CONTROL ) != 0 && ( GetAsyncKeyState( VK_CONTROL ) & 0x8000 ) == 0 )
|
2013-04-29 19:35:26 +00:00
|
|
|
{
|
2013-06-10 13:49:42 +00:00
|
|
|
emulateModKeys |= MOD_CONTROL;
|
|
|
|
i[ nextKeyNom ].type = INPUT_KEYBOARD;
|
|
|
|
i[ nextKeyNom ].ki.wVk = VK_CONTROL;
|
|
|
|
nextKeyNom += 1;
|
2013-04-29 19:35:26 +00:00
|
|
|
}
|
2013-06-10 13:49:42 +00:00
|
|
|
if( ( mod & MOD_SHIFT ) != 0 && ( GetAsyncKeyState( VK_SHIFT ) & 0x8000 ) == 0 )
|
|
|
|
{
|
|
|
|
emulateModKeys |= MOD_SHIFT;
|
|
|
|
i[ nextKeyNom ].type = INPUT_KEYBOARD;
|
|
|
|
i[ nextKeyNom ].ki.wVk = VK_SHIFT;
|
|
|
|
nextKeyNom += 1;
|
|
|
|
}
|
|
|
|
if( ( mod & MOD_WIN ) != 0 && ( GetAsyncKeyState( VK_LWIN ) & 0x8000 ) == 0
|
|
|
|
&& ( GetAsyncKeyState( VK_RWIN ) & 0x8000 ) == 0 )
|
|
|
|
{
|
|
|
|
emulateModKeys |= MOD_WIN;
|
|
|
|
i[ nextKeyNom ].type = INPUT_KEYBOARD;
|
|
|
|
i[ nextKeyNom ].ki.wVk = VK_LWIN;
|
|
|
|
nextKeyNom += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
i[ nextKeyNom ].type = INPUT_KEYBOARD;
|
|
|
|
i[ nextKeyNom ].ki.wVk = vk;
|
|
|
|
nextKeyNom += 1;
|
|
|
|
i[ nextKeyNom ].type = INPUT_KEYBOARD;
|
|
|
|
i[ nextKeyNom ].ki.wVk = vk;
|
|
|
|
i[ nextKeyNom ].ki.dwFlags = KEYEVENTF_KEYUP;
|
|
|
|
nextKeyNom += 1;
|
|
|
|
|
|
|
|
if( emulateModKeys & MOD_WIN )
|
|
|
|
{
|
|
|
|
i[ nextKeyNom ].type = INPUT_KEYBOARD;
|
|
|
|
i[ nextKeyNom ].ki.wVk = VK_LWIN;
|
|
|
|
i[ nextKeyNom ].ki.dwFlags = KEYEVENTF_KEYUP;
|
|
|
|
nextKeyNom += 1;
|
|
|
|
}
|
|
|
|
if( emulateModKeys & MOD_SHIFT )
|
|
|
|
{
|
|
|
|
i[ nextKeyNom ].type = INPUT_KEYBOARD;
|
|
|
|
i[ nextKeyNom ].ki.wVk = VK_SHIFT;
|
|
|
|
i[ nextKeyNom ].ki.dwFlags = KEYEVENTF_KEYUP;
|
|
|
|
nextKeyNom += 1;
|
|
|
|
}
|
|
|
|
if( emulateModKeys & MOD_CONTROL )
|
|
|
|
{
|
|
|
|
i[ nextKeyNom ].type = INPUT_KEYBOARD;
|
|
|
|
i[ nextKeyNom ].ki.wVk = VK_CONTROL;
|
|
|
|
i[ nextKeyNom ].ki.dwFlags = KEYEVENTF_KEYUP;
|
|
|
|
nextKeyNom += 1;
|
|
|
|
}
|
|
|
|
if( emulateModKeys & MOD_ALT )
|
|
|
|
{
|
|
|
|
i[ nextKeyNom ].type = INPUT_KEYBOARD;
|
|
|
|
i[ nextKeyNom ].ki.wVk = VK_MENU;
|
|
|
|
i[ nextKeyNom ].ki.dwFlags = KEYEVENTF_KEYUP;
|
|
|
|
nextKeyNom += 1;
|
|
|
|
}
|
|
|
|
|
2009-04-22 20:06:31 +00:00
|
|
|
UnregisterHotKey( hwnd, hs.id );
|
2013-06-10 13:49:42 +00:00
|
|
|
SendInput( nextKeyNom, i, sizeof( *i ) );
|
2009-04-22 20:06:31 +00:00
|
|
|
RegisterHotKey(hwnd, hs.id, hs.modifier, hs.key);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-04-19 21:32:18 +00:00
|
|
|
if (hs.key2 == 0) {
|
2009-04-21 18:27:26 +00:00
|
|
|
emit hotkeyActivated( hs.handle );
|
2009-04-19 21:32:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
state2 = true;
|
|
|
|
state2waiter = hs;
|
|
|
|
QTimer::singleShot(500, this, SLOT(waitKey2()));
|
2009-04-22 15:29:28 +00:00
|
|
|
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
|
|
|
|
// Grab the second key, unless it's grabbed already
|
|
|
|
// Note that we only grab the clipboard key only if
|
|
|
|
// the sequence didn't begin with it
|
|
|
|
|
|
|
|
if ( ( isCopyToClipboardKey( hs.key, hs.modifier ) ||
|
|
|
|
!isCopyToClipboardKey( hs.key2, hs.modifier ) ) &&
|
|
|
|
!isKeyGrabbed( hs.key2, hs.modifier ) )
|
|
|
|
keyToUngrab = grabKey( hs.key2, hs.modifier );
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2009-04-22 20:06:31 +00:00
|
|
|
return true;
|
2009-04-19 21:32:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state2 = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifdef Q_WS_WIN
|
|
|
|
|
|
|
|
void HotkeyWrapper::init()
|
|
|
|
{
|
|
|
|
QWidget *root = qApp->topLevelWidgets().value(0);
|
|
|
|
hwnd = (HWND)root->winId();
|
|
|
|
}
|
|
|
|
|
2009-04-21 18:27:26 +00:00
|
|
|
bool HotkeyWrapper::setGlobalKey( int key, int key2,
|
|
|
|
Qt::KeyboardModifiers modifier, int handle )
|
2009-04-19 21:32:18 +00:00
|
|
|
{
|
2009-04-21 18:27:26 +00:00
|
|
|
if ( !key )
|
|
|
|
return false; // We don't monitor empty combinations
|
2009-04-19 21:32:18 +00:00
|
|
|
|
|
|
|
static int id = 0;
|
2011-11-17 13:01:42 +00:00
|
|
|
if( id > 0xBFFF - 1 )
|
|
|
|
id = 0;
|
2009-04-19 21:32:18 +00:00
|
|
|
|
|
|
|
quint32 mod = 0;
|
|
|
|
if (modifier & Qt::CTRL)
|
|
|
|
mod |= MOD_CONTROL;
|
|
|
|
if (modifier & Qt::ALT)
|
|
|
|
mod |= MOD_ALT;
|
|
|
|
if (modifier & Qt::SHIFT)
|
|
|
|
mod |= MOD_SHIFT;
|
2009-10-20 13:04:46 +00:00
|
|
|
if (modifier & Qt::META)
|
|
|
|
mod |= MOD_WIN;
|
2009-04-19 21:32:18 +00:00
|
|
|
|
2009-04-21 18:27:26 +00:00
|
|
|
quint32 vk = nativeKey( key );
|
|
|
|
quint32 vk2 = key2 ? nativeKey( key2 ) : 0;
|
2009-04-19 21:32:18 +00:00
|
|
|
|
2009-04-21 21:07:15 +00:00
|
|
|
hotkeys.append( HotkeyStruct( vk, vk2, mod, handle, id ) );
|
2009-04-19 21:32:18 +00:00
|
|
|
|
|
|
|
if (!RegisterHotKey(hwnd, id++, mod, vk))
|
|
|
|
return false;
|
|
|
|
|
2009-04-21 18:27:26 +00:00
|
|
|
if ( key2 && key2 != key )
|
2009-04-19 21:32:18 +00:00
|
|
|
return RegisterHotKey(hwnd, id++, mod, vk2);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HotkeyWrapper::winEvent ( MSG * message, long * result )
|
|
|
|
{
|
2012-10-31 13:58:35 +00:00
|
|
|
(void) result;
|
2009-04-19 21:32:18 +00:00
|
|
|
if (message->message == WM_HOTKEY)
|
2009-04-22 20:06:31 +00:00
|
|
|
return checkState( (message->lParam >> 16), (message->lParam & 0xffff) );
|
2009-04-19 21:32:18 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
quint32 HotkeyWrapper::nativeKey(int key)
|
|
|
|
{
|
|
|
|
if (key >= Qt::Key_0 && key <= Qt::Key_9)
|
|
|
|
return key;
|
|
|
|
|
|
|
|
if (key >= Qt::Key_A && key <= Qt::Key_Z)
|
|
|
|
return key;
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case Qt::Key_Space: return VK_SPACE;
|
|
|
|
case Qt::Key_Asterisk: return VK_MULTIPLY;
|
|
|
|
case Qt::Key_Plus: return VK_ADD;
|
|
|
|
case Qt::Key_Comma: return VK_SEPARATOR;
|
|
|
|
case Qt::Key_Minus: return VK_SUBTRACT;
|
|
|
|
case Qt::Key_Slash: return VK_DIVIDE;
|
|
|
|
case Qt::Key_Tab:
|
|
|
|
case Qt::Key_Backtab: return VK_TAB;
|
|
|
|
case Qt::Key_Backspace: return VK_BACK;
|
|
|
|
case Qt::Key_Return:
|
|
|
|
case Qt::Key_Escape: return VK_ESCAPE;
|
|
|
|
case Qt::Key_Enter: return VK_RETURN;
|
|
|
|
case Qt::Key_Insert: return VK_INSERT;
|
|
|
|
case Qt::Key_Delete: return VK_DELETE;
|
|
|
|
case Qt::Key_Pause: return VK_PAUSE;
|
|
|
|
case Qt::Key_Print: return VK_PRINT;
|
|
|
|
case Qt::Key_Clear: return VK_CLEAR;
|
|
|
|
case Qt::Key_Home: return VK_HOME;
|
|
|
|
case Qt::Key_End: return VK_END;
|
|
|
|
case Qt::Key_Up: return VK_UP;
|
|
|
|
case Qt::Key_Down: return VK_DOWN;
|
|
|
|
case Qt::Key_Left: return VK_LEFT;
|
|
|
|
case Qt::Key_Right: return VK_RIGHT;
|
|
|
|
case Qt::Key_PageUp: return VK_PRIOR;
|
|
|
|
case Qt::Key_PageDown: return VK_NEXT;
|
|
|
|
case Qt::Key_F1: return VK_F1;
|
|
|
|
case Qt::Key_F2: return VK_F2;
|
|
|
|
case Qt::Key_F3: return VK_F3;
|
|
|
|
case Qt::Key_F4: return VK_F4;
|
|
|
|
case Qt::Key_F5: return VK_F5;
|
|
|
|
case Qt::Key_F6: return VK_F6;
|
|
|
|
case Qt::Key_F7: return VK_F7;
|
|
|
|
case Qt::Key_F8: return VK_F8;
|
|
|
|
case Qt::Key_F9: return VK_F9;
|
|
|
|
case Qt::Key_F10: return VK_F10;
|
|
|
|
case Qt::Key_F11: return VK_F11;
|
|
|
|
case Qt::Key_F12: return VK_F12;
|
|
|
|
case Qt::Key_F13: return VK_F13;
|
|
|
|
case Qt::Key_F14: return VK_F14;
|
|
|
|
case Qt::Key_F15: return VK_F15;
|
|
|
|
case Qt::Key_F16: return VK_F16;
|
|
|
|
case Qt::Key_F17: return VK_F17;
|
|
|
|
case Qt::Key_F18: return VK_F18;
|
|
|
|
case Qt::Key_F19: return VK_F19;
|
|
|
|
case Qt::Key_F20: return VK_F20;
|
|
|
|
case Qt::Key_F21: return VK_F21;
|
|
|
|
case Qt::Key_F22: return VK_F22;
|
|
|
|
case Qt::Key_F23: return VK_F23;
|
|
|
|
case Qt::Key_F24: return VK_F24;
|
|
|
|
default:;
|
|
|
|
}
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2009-04-21 21:07:15 +00:00
|
|
|
void HotkeyWrapper::unregister()
|
2009-04-19 21:32:18 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < hotkeys.count(); i++)
|
2009-04-21 18:27:26 +00:00
|
|
|
{
|
|
|
|
HotkeyStruct const & hk = hotkeys.at( i );
|
|
|
|
|
2009-04-21 21:07:15 +00:00
|
|
|
UnregisterHotKey( hwnd, hk.id );
|
2009-04-21 18:27:26 +00:00
|
|
|
|
|
|
|
if ( hk.key2 && hk.key2 != hk.key )
|
2009-04-21 21:07:15 +00:00
|
|
|
UnregisterHotKey( hwnd, hk.id+1 );
|
2009-04-21 18:27:26 +00:00
|
|
|
}
|
2009-04-19 21:32:18 +00:00
|
|
|
|
|
|
|
(static_cast<QHotkeyApplication*>(qApp))->unregisterWrapper(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool QHotkeyApplication::winEventFilter ( MSG * message, long * result )
|
|
|
|
{
|
|
|
|
if (message->message == WM_HOTKEY)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < hotkeyWrappers.size(); i++)
|
|
|
|
{
|
2009-04-21 18:27:26 +00:00
|
|
|
if ( hotkeyWrappers.at(i)->winEvent( message, result ) )
|
2009-04-19 21:32:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-17 14:53:47 +00:00
|
|
|
if( mainWindow )
|
|
|
|
{
|
|
|
|
if( ( static_cast< MainWindow * >( mainWindow ) )->handleGDMessage( message, result ) )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-04-19 21:32:18 +00:00
|
|
|
return QApplication::winEventFilter( message, result );
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include <X11/keysym.h>
|
|
|
|
|
|
|
|
void HotkeyWrapper::init()
|
|
|
|
{
|
2009-04-22 15:29:28 +00:00
|
|
|
keyToUngrab = grabbedKeys.end();
|
|
|
|
|
2009-04-21 18:27:26 +00:00
|
|
|
// We use RECORD extension instead of XGrabKey. That's because XGrabKey
|
|
|
|
// prevents other clients from getting their input if it's grabbed.
|
|
|
|
|
|
|
|
Display * display = QX11Info::display();
|
|
|
|
|
|
|
|
lShiftCode = XKeysymToKeycode( display, XK_Shift_L );
|
|
|
|
rShiftCode = XKeysymToKeycode( display, XK_Shift_R );
|
|
|
|
|
|
|
|
lCtrlCode = XKeysymToKeycode( display, XK_Control_L );
|
|
|
|
rCtrlCode = XKeysymToKeycode( display, XK_Control_R );
|
|
|
|
|
|
|
|
lAltCode = XKeysymToKeycode( display, XK_Alt_L );
|
|
|
|
rAltCode = XKeysymToKeycode( display, XK_Alt_R );
|
|
|
|
|
2013-09-24 13:55:58 +00:00
|
|
|
lMetaCode = XKeysymToKeycode( display, XK_Super_L );
|
|
|
|
rMetaCode = XKeysymToKeycode( display, XK_Super_R );
|
|
|
|
|
2009-04-22 15:29:28 +00:00
|
|
|
cCode = XKeysymToKeycode( display, XK_c );
|
|
|
|
insertCode = XKeysymToKeycode( display, XK_Insert );
|
2009-04-25 10:45:42 +00:00
|
|
|
kpInsertCode = XKeysymToKeycode( display, XK_KP_Insert );
|
2009-04-22 15:29:28 +00:00
|
|
|
|
2009-04-21 18:27:26 +00:00
|
|
|
currentModifiers = 0;
|
|
|
|
|
|
|
|
// This one will be used to read the recorded content
|
|
|
|
dataDisplay = XOpenDisplay( 0 );
|
|
|
|
|
|
|
|
if ( !dataDisplay )
|
|
|
|
throw exInit();
|
|
|
|
|
|
|
|
recordRange = XRecordAllocRange();
|
|
|
|
|
|
|
|
if ( !recordRange )
|
|
|
|
{
|
|
|
|
XCloseDisplay( dataDisplay );
|
|
|
|
throw exInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
recordRange->device_events.first = KeyPress;
|
|
|
|
recordRange->device_events.last = KeyRelease;
|
|
|
|
recordClientSpec = XRecordAllClients;
|
|
|
|
|
|
|
|
recordContext = XRecordCreateContext( display, 0,
|
|
|
|
&recordClientSpec, 1,
|
|
|
|
&recordRange, 1 );
|
|
|
|
|
|
|
|
if ( !recordContext )
|
|
|
|
{
|
|
|
|
XFree( recordRange );
|
|
|
|
XCloseDisplay( dataDisplay );
|
|
|
|
throw exInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is required to ensure context was indeed created
|
|
|
|
XSync( display, False );
|
|
|
|
|
|
|
|
connect( this, SIGNAL( keyRecorded( quint32, quint32 ) ),
|
|
|
|
this, SLOT( checkState( quint32, quint32 ) ),
|
|
|
|
Qt::QueuedConnection );
|
|
|
|
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HotkeyWrapper::run() // Runs in a separate thread
|
|
|
|
{
|
|
|
|
if ( !XRecordEnableContext( dataDisplay, recordContext,
|
|
|
|
recordEventCallback,
|
|
|
|
(XPointer) this ) )
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "Failed to enable record context\n" );
|
2009-04-21 18:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HotkeyWrapper::recordEventCallback( XPointer ptr, XRecordInterceptData * data )
|
|
|
|
{
|
|
|
|
((HotkeyWrapper * )ptr)->handleRecordEvent( data );
|
2009-04-19 21:32:18 +00:00
|
|
|
}
|
|
|
|
|
2009-04-21 18:27:26 +00:00
|
|
|
void HotkeyWrapper::handleRecordEvent( XRecordInterceptData * data )
|
2009-04-19 21:32:18 +00:00
|
|
|
{
|
2009-04-21 18:27:26 +00:00
|
|
|
if ( data->category == XRecordFromServer )
|
|
|
|
{
|
|
|
|
xEvent * event = ( xEvent * ) data->data;
|
2009-04-19 21:32:18 +00:00
|
|
|
|
2009-04-21 18:27:26 +00:00
|
|
|
if ( event->u.u.type == KeyPress )
|
|
|
|
{
|
|
|
|
KeyCode key = event->u.u.detail;
|
|
|
|
|
|
|
|
if ( key == lShiftCode ||
|
|
|
|
key == rShiftCode )
|
|
|
|
currentModifiers |= ShiftMask;
|
|
|
|
else
|
|
|
|
if ( key == lCtrlCode ||
|
|
|
|
key == rCtrlCode )
|
|
|
|
currentModifiers |= ControlMask;
|
|
|
|
else
|
|
|
|
if ( key == lAltCode ||
|
|
|
|
key == rAltCode )
|
|
|
|
currentModifiers |= Mod1Mask;
|
|
|
|
else
|
2013-09-24 13:55:58 +00:00
|
|
|
if ( key == lMetaCode ||
|
|
|
|
key == rMetaCode )
|
|
|
|
currentModifiers |= Mod4Mask;
|
|
|
|
else
|
2009-04-25 10:45:42 +00:00
|
|
|
{
|
|
|
|
// Here we employ a kind of hack translating KP_Insert key
|
|
|
|
// to just Insert. This allows reacting to both Insert keys.
|
|
|
|
if ( key == kpInsertCode )
|
|
|
|
key = insertCode;
|
|
|
|
|
2009-04-21 18:27:26 +00:00
|
|
|
emit keyRecorded( key, currentModifiers );
|
2009-04-25 10:45:42 +00:00
|
|
|
}
|
2009-04-21 18:27:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if ( event->u.u.type == KeyRelease )
|
|
|
|
{
|
|
|
|
KeyCode key = event->u.u.detail;
|
|
|
|
|
|
|
|
if ( key == lShiftCode ||
|
|
|
|
key == rShiftCode )
|
|
|
|
currentModifiers &= ~ShiftMask;
|
|
|
|
else
|
|
|
|
if ( key == lCtrlCode ||
|
|
|
|
key == rCtrlCode )
|
|
|
|
currentModifiers &= ~ControlMask;
|
|
|
|
else
|
|
|
|
if ( key == lAltCode ||
|
|
|
|
key == rAltCode )
|
|
|
|
currentModifiers &= ~Mod1Mask;
|
2013-09-24 13:55:58 +00:00
|
|
|
else
|
|
|
|
if ( key == lMetaCode ||
|
|
|
|
key == rMetaCode )
|
|
|
|
currentModifiers &= ~Mod4Mask;
|
2009-04-21 18:27:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
XRecordFreeData( data );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HotkeyWrapper::setGlobalKey( int key, int key2,
|
|
|
|
Qt::KeyboardModifiers modifier, int handle )
|
|
|
|
{
|
|
|
|
if ( !key )
|
|
|
|
return false; // We don't monitor empty combinations
|
|
|
|
|
|
|
|
int vk = nativeKey( key );
|
|
|
|
int vk2 = key2 ? nativeKey( key2 ) : 0;
|
2009-04-19 21:32:18 +00:00
|
|
|
|
|
|
|
quint32 mod = 0;
|
|
|
|
if (modifier & Qt::ShiftModifier)
|
|
|
|
mod |= ShiftMask;
|
|
|
|
if (modifier & Qt::ControlModifier)
|
|
|
|
mod |= ControlMask;
|
|
|
|
if (modifier & Qt::AltModifier)
|
|
|
|
mod |= Mod1Mask;
|
2013-09-24 13:55:58 +00:00
|
|
|
if (modifier & Qt::MetaModifier)
|
|
|
|
mod |= Mod4Mask;
|
2009-04-19 21:32:18 +00:00
|
|
|
|
2009-04-22 15:29:28 +00:00
|
|
|
hotkeys.append( HotkeyStruct( vk, vk2, mod, handle, 0 ) );
|
|
|
|
|
|
|
|
if ( !isCopyToClipboardKey( vk, mod ) )
|
|
|
|
grabKey( vk, mod ); // Make sure it doesn't get caught by other apps
|
2009-04-19 21:32:18 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-04-22 15:29:28 +00:00
|
|
|
bool HotkeyWrapper::isCopyToClipboardKey( quint32 keyCode, quint32 modifiers ) const
|
|
|
|
{
|
|
|
|
return modifiers == ControlMask &&
|
2009-04-25 10:45:42 +00:00
|
|
|
( keyCode == cCode || keyCode == insertCode || keyCode == kpInsertCode );
|
2009-04-22 15:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HotkeyWrapper::isKeyGrabbed( quint32 keyCode, quint32 modifiers ) const
|
|
|
|
{
|
|
|
|
GrabbedKeys::const_iterator i = grabbedKeys.find( std::make_pair( keyCode, modifiers ) );
|
|
|
|
|
|
|
|
return i != grabbedKeys.end();
|
|
|
|
}
|
|
|
|
|
2013-07-19 13:22:40 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
typedef int (*X11ErrorHandler) ( Display * display, XErrorEvent * event );
|
|
|
|
|
|
|
|
class X11GrabUngrabErrorHandler {
|
|
|
|
public:
|
|
|
|
static bool error;
|
|
|
|
|
|
|
|
static int grab_ungrab_error_handler( Display *, XErrorEvent * event )
|
|
|
|
{
|
|
|
|
qDebug() << "grab_ungrab_error_handler is invoked";
|
|
|
|
switch ( event->error_code )
|
|
|
|
{
|
|
|
|
case BadAccess:
|
|
|
|
case BadValue:
|
|
|
|
case BadWindow:
|
|
|
|
if ( event->request_code == 33 /* X_GrabKey */ ||
|
|
|
|
event->request_code == 34 /* X_UngrabKey */)
|
|
|
|
{
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
X11GrabUngrabErrorHandler()
|
|
|
|
{
|
|
|
|
error = false;
|
|
|
|
previousErrorHandler_ = XSetErrorHandler( grab_ungrab_error_handler );
|
|
|
|
}
|
|
|
|
|
|
|
|
~X11GrabUngrabErrorHandler()
|
|
|
|
{
|
|
|
|
XFlush( QX11Info::display() );
|
|
|
|
(void) XSetErrorHandler( previousErrorHandler_ );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isError() const
|
|
|
|
{
|
|
|
|
XFlush( QX11Info::display() );
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
X11ErrorHandler previousErrorHandler_;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool X11GrabUngrabErrorHandler::error = false;
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
|
2009-04-22 15:29:28 +00:00
|
|
|
HotkeyWrapper::GrabbedKeys::iterator HotkeyWrapper::grabKey( quint32 keyCode,
|
|
|
|
quint32 modifiers )
|
|
|
|
{
|
2009-04-22 17:29:51 +00:00
|
|
|
std::pair< GrabbedKeys::iterator, bool > result =
|
2009-04-22 15:29:28 +00:00
|
|
|
grabbedKeys.insert( std::make_pair( keyCode, modifiers ) );
|
|
|
|
|
2013-07-19 13:22:40 +00:00
|
|
|
|
2009-04-22 15:29:28 +00:00
|
|
|
if ( result.second )
|
|
|
|
{
|
2013-07-19 13:22:40 +00:00
|
|
|
X11GrabUngrabErrorHandler errorHandler;
|
2009-04-22 15:29:28 +00:00
|
|
|
XGrabKey( QX11Info::display(), keyCode, modifiers, QX11Info::appRootWindow(),
|
|
|
|
True, GrabModeAsync, GrabModeAsync );
|
2013-07-19 13:22:40 +00:00
|
|
|
|
|
|
|
if ( errorHandler.isError() )
|
|
|
|
{
|
|
|
|
qDebug() << "Warning: Possible hotkeys conflict. Check your hotkeys options.";
|
|
|
|
ungrabKey( result.first );
|
|
|
|
}
|
2009-04-22 15:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HotkeyWrapper::ungrabKey( GrabbedKeys::iterator i )
|
|
|
|
{
|
2013-07-19 13:22:40 +00:00
|
|
|
X11GrabUngrabErrorHandler errorHandler;
|
2009-04-22 15:29:28 +00:00
|
|
|
XUngrabKey( QX11Info::display(), i->first, i->second, QX11Info::appRootWindow() );
|
|
|
|
|
|
|
|
grabbedKeys.erase( i );
|
2013-07-19 13:22:40 +00:00
|
|
|
|
|
|
|
if ( errorHandler.isError() )
|
|
|
|
{
|
|
|
|
qDebug() << "Warning: Cannot ungrab the hotkey";
|
|
|
|
}
|
2009-04-22 15:29:28 +00:00
|
|
|
}
|
|
|
|
|
2009-04-19 21:32:18 +00:00
|
|
|
quint32 HotkeyWrapper::nativeKey(int key)
|
|
|
|
{
|
2009-04-21 18:27:26 +00:00
|
|
|
QString keySymName;
|
2009-04-19 21:32:18 +00:00
|
|
|
|
2009-04-21 18:27:26 +00:00
|
|
|
switch( key )
|
|
|
|
{
|
|
|
|
case Qt::Key_Insert:
|
|
|
|
keySymName = "Insert";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
keySymName = QKeySequence( key ).toString();
|
|
|
|
break;
|
|
|
|
}
|
2009-04-19 21:32:18 +00:00
|
|
|
|
2009-04-21 18:27:26 +00:00
|
|
|
Display * display = QX11Info::display();
|
|
|
|
return XKeysymToKeycode( display, XStringToKeysym( keySymName.toLatin1().data() ) );
|
2009-04-19 21:32:18 +00:00
|
|
|
}
|
|
|
|
|
2009-04-21 21:07:15 +00:00
|
|
|
void HotkeyWrapper::unregister()
|
2009-04-19 21:32:18 +00:00
|
|
|
{
|
2009-04-21 18:27:26 +00:00
|
|
|
Display * display = QX11Info::display();
|
2009-04-19 21:32:18 +00:00
|
|
|
|
2009-04-21 18:27:26 +00:00
|
|
|
XRecordDisableContext( display, recordContext );
|
|
|
|
XSync( display, False );
|
2009-04-19 21:32:18 +00:00
|
|
|
|
2009-04-21 18:27:26 +00:00
|
|
|
wait();
|
2009-04-19 21:32:18 +00:00
|
|
|
|
2009-04-21 18:27:26 +00:00
|
|
|
XRecordFreeContext( display, recordContext );
|
|
|
|
XFree( recordRange );
|
|
|
|
XCloseDisplay( dataDisplay );
|
2009-04-19 21:32:18 +00:00
|
|
|
|
2009-04-22 15:29:28 +00:00
|
|
|
while( grabbedKeys.size() )
|
|
|
|
ungrabKey( grabbedKeys.begin() );
|
|
|
|
|
2009-04-21 18:27:26 +00:00
|
|
|
(static_cast<QHotkeyApplication*>(qApp))->unregisterWrapper(this);
|
2009-04-19 21:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2011-08-06 18:39:16 +00:00
|
|
|
#endif
|