Grab alt+space hotkey directly using xcb instead of separate grabbing process

This commit is contained in:
Jerzy Kozera 2013-01-28 00:10:44 +00:00
parent 11a1eb5dd0
commit 90e1589e56
4 changed files with 146 additions and 93 deletions

View File

@ -4,85 +4,21 @@
#include <string>
#include <iostream>
#ifndef WIN32
#include <X11/Xlib.h>
#include <X11/keysym.h>
#endif // WIN32
using namespace std;
void run_grabkey();
int main(int argc, char *argv[])
{
if(argc > 1 && argv[1] == string("--grabkey")) {
run_grabkey();
return -1; // infinite loop - shouldn't terminate
} else {
// detect already running instance
QLocalSocket socket;
socket.connectToServer(serverName);
if (socket.waitForConnected(500)) {
cerr << "Already running. Terminating." << endl;
return -1; // Exit already a process running
}
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
// detect already running instance
QLocalSocket socket;
socket.connectToServer(serverName);
if (socket.waitForConnected(500)) {
cerr << "Already running. Terminating." << endl;
return -1; // Exit already a process running
}
}
void run_grabkey()
{
#ifndef WIN32
Display *display = XOpenDisplay(NULL);
unsigned int AltMask = 0;
XModifierKeymap *xmk = XGetModifierMapping(display);
if(xmk) {
KeyCode altKeyCode = XKeysymToKeycode(display, XK_Alt_L);
KeyCode *c = xmk->modifiermap;
int m, k;
for(m = 0; m < 8; m++) {
for(k = 0; k < xmk->max_keypermod; k++, c++)
{
if(*c == NoSymbol) {
continue;
}
if(*c == altKeyCode) {
AltMask = (1 << m);
}
}
}
XFreeModifiermap (xmk);
}
if(AltMask == 0) {
cout << "failed" << endl;
} else {
cout << "ok" << endl;
}
KeyCode hotKey = XKeysymToKeycode(display, XStringToKeysym("space"));
XGrabKey(display, hotKey, AnyModifier, DefaultRootWindow(display), True, GrabModeSync, GrabModeSync);
XEvent e;
for(;;)
{
XNextEvent(display, &e);
if(e.type == KeyPress){
if(e.xkey.keycode == hotKey && e.xkey.state & AltMask)
{
cout << "1" << endl;
XAllowEvents(display, AsyncKeyboard, e.xkey.time);
} else {
XAllowEvents(display, ReplayKeyboard, e.xkey.time);
XFlush(display);
}
}
}
#endif // WIN32
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

View File

@ -15,6 +15,11 @@
#ifdef WIN32
#include <windows.h>
#else
#include <QtGui/5.0.0/QtGui/qpa/qplatformnativeinterface.h>
#include <xcb/xcb.h>
#include <xcb/xcb_keysyms.h>
#include <X11/keysym.h>
#endif
const QString serverName = "zeal_process_running";
@ -41,7 +46,6 @@ MainWindow::MainWindow(QWidget *parent) :
createTrayIcon();
// initialise key grabber
#ifdef WIN32
auto filter = new ZealNativeEventFilter();
connect(filter, &ZealNativeEventFilter::gotHotKey, [&]() {
if(isVisible()) hide();
@ -50,25 +54,29 @@ MainWindow::MainWindow(QWidget *parent) :
}
});
qApp->eventDispatcher()->installNativeEventFilter(filter);
// platform-specific code for global key grabbing
#ifdef WIN32
RegisterHotKey(NULL, 10, MOD_ALT, VK_SPACE);
#else
keyGrabber.setParent(this);
keyGrabber.start(qApp->applicationFilePath(), {"--grabkey"});
connect(&keyGrabber, &QProcess::readyRead, [&]() {
char buf[100];
keyGrabber.readLine(buf, sizeof(buf));
if(QString(buf) == "failed\n") {
QMessageBox::warning(this, "grabkey process init failed",
QString("Failed to grab keyboard - Alt+Space will not work."));
auto platform = qApp->platformNativeInterface();
xcb_connection_t *c = static_cast<xcb_connection_t*>(platform->nativeResourceForWindow("connection", 0));
xcb_key_symbols_t *keysyms = xcb_key_symbols_alloc(c);
xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, XK_space), keycode;
// add bindings for all screens
xcb_screen_iterator_t iter;
iter = xcb_setup_roots_iterator (xcb_get_setup (c));
for (; iter.rem; xcb_screen_next (&iter)) {
int i = 0;
while(keycodes[i] != XCB_NO_SYMBOL) {
keycode = keycodes[i];
xcb_grab_key(c, true, iter.data->root, XCB_MOD_MASK_ANY, keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
i += 1;
}
//first = false;
if(QString(buf) == "1\n") {
if(isVisible()) hide();
else {
bringToFront();
}
}
});
}
#endif // WIN32
// initialise docsets
auto dataLocation = QStandardPaths::writableLocation(QStandardPaths::DataLocation);

View File

@ -38,3 +38,5 @@ FORMS += mainwindow.ui
QMAKE_CXXFLAGS += -std=c++11
win32:DEFINES += WIN32
unix:!macx: LIBS += -lxcb-keysyms

View File

@ -2,10 +2,87 @@
#ifdef WIN32
#include <windows.h>
#else
#include <xcb/xcb.h>
#include <xcb/xcb_keysyms.h>
#include <X11/keysym.h>
#include <QGuiApplication>
#include <QtGui/5.0.0/QtGui/qpa/qplatformnativeinterface.h>
#include <QDebug>
// http://svn.tribler.org/vlc/trunk/modules/control/globalhotkeys/xcb.c
// Copyright (C) 2009 the VideoLAN team
static unsigned GetModifier( xcb_connection_t *p_connection, xcb_key_symbols_t *p_symbols, xcb_keysym_t sym )
{
static const unsigned pi_mask[8] = {
XCB_MOD_MASK_SHIFT, XCB_MOD_MASK_LOCK, XCB_MOD_MASK_CONTROL,
XCB_MOD_MASK_1, XCB_MOD_MASK_2, XCB_MOD_MASK_3,
XCB_MOD_MASK_4, XCB_MOD_MASK_5
};
if( sym == 0 )
return 0; /* no modifier */
#ifdef XCB_KEYSYM_OLD_API /* as seen in Debian Lenny */
const xcb_keycode_t key = xcb_key_symbols_get_keycode( p_symbols, sym );
if( key == 0 )
return 0;
#else
const xcb_keycode_t *p_keys = xcb_key_symbols_get_keycode( p_symbols, sym );
if( !p_keys )
return 0;
int i = 0;
bool no_modifier = true;
while( p_keys[i] != XCB_NO_SYMBOL )
{
if( p_keys[i] != 0 )
{
no_modifier = false;
break;
}
i++;
}
if( no_modifier )
return 0;
#endif
xcb_get_modifier_mapping_cookie_t r =
xcb_get_modifier_mapping( p_connection );
xcb_get_modifier_mapping_reply_t *p_map =
xcb_get_modifier_mapping_reply( p_connection, r, NULL );
if( !p_map )
return 0;
xcb_keycode_t *p_keycode = xcb_get_modifier_mapping_keycodes( p_map );
if( !p_keycode )
return 0;
for( int i = 0; i < 8; i++ )
for( int j = 0; j < p_map->keycodes_per_modifier; j++ )
#ifdef XCB_KEYSYM_OLD_API /* as seen in Debian Lenny */
if( p_keycode[i * p_map->keycodes_per_modifier + j] == key )
{
free( p_map );
return pi_mask[i];
}
#else
for( int k = 0; p_keys[k] != XCB_NO_SYMBOL; k++ )
if( p_keycode[i*p_map->keycodes_per_modifier + j] == p_keys[k])
{
free( p_map );
return pi_mask[i];
}
#endif
free( p_map ); // FIXME to check
return 0;
}
#endif // WIN32
ZealNativeEventFilter::ZealNativeEventFilter(QObject *parent) :
QAbstractNativeEventFilter(), QObject(parent)
QObject(parent), QAbstractNativeEventFilter()
{
}
@ -18,6 +95,36 @@ bool ZealNativeEventFilter::nativeEventFilter(const QByteArray &eventType, void
emit gotHotKey();
return true;
}
#else // WIN32
xcb_generic_event_t* ev = static_cast<xcb_generic_event_t*>(message);
if((ev->response_type&127) == XCB_KEY_PRESS) {
xcb_connection_t *c = static_cast<xcb_connection_t*>(
((QGuiApplication*)QGuiApplication::instance())->
platformNativeInterface()->nativeResourceForWindow("connection", 0));
xcb_key_press_event_t *event = (xcb_key_press_event_t *)ev;
xcb_key_symbols_t *keysyms = xcb_key_symbols_alloc(c);
xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, XK_space);
int i = 0;
bool found = false;
while(keycodes[i] != XCB_NO_SYMBOL) {
if(event->detail == keycodes[i]) {
if(event->state & GetModifier(c, keysyms, XK_Alt_L) || event->state & GetModifier(c, keysyms, XK_Alt_R)) {
xcb_allow_events(c, XCB_ALLOW_ASYNC_KEYBOARD, event->time);
emit gotHotKey();
found = true;
} else {
xcb_allow_events(c, XCB_ALLOW_REPLAY_KEYBOARD, event->time);
}
break;
}
i += 1;
}
free(keysyms);
free(keycodes);
if(found) return true;
}
#endif // WIN32
return false;
}