We now have a current svn snapshot of libqxt in our contrib dir, and
[quassel.git] / src / contrib / libqxt-2007-10-24 / src / gui / qxtapplication_x11.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
4 **
5 ** This file is part of the QxtGui module of the Qt eXTension library
6 **
7 ** This library is free software; you can redistribute it and/or modify it
8 ** under the terms of th Common Public License, version 1.0, as published by
9 ** IBM.
10 **
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14 ** FITNESS FOR A PARTICULAR PURPOSE.
15 **
16 ** You should have received a copy of the CPL along with this file.
17 ** See the LICENSE file and the cpl1.0.txt file included with the source
18 ** distribution for more information. If you did not receive a copy of the
19 ** license, contact the Qxt Foundation.
20 **
21 ** <http://libqxt.sourceforge.net>  <foundation@libqxt.org>
22 **
23 ****************************************************************************/
24 #include "qxtapplication.h"
25 #include "qxtapplication_p.h"
26 #include <QKeySequence>
27 #include <QX11Info>
28 #include <X11/Xlib.h>
29
30 QxtApplication::QxtApplication(Display* display, Qt::HANDLE visual, Qt::HANDLE colormap)
31         : QApplication(display, visual, colormap)
32 {}
33
34 QxtApplication::QxtApplication(Display* display, int& argc, char** argv, Qt::HANDLE visual, Qt::HANDLE colormap)
35         : QApplication(display, argc, argv, visual, colormap)
36 {}
37
38 bool QxtApplication::x11EventFilter(XEvent* event)
39 {
40     foreach (QxtNativeEventFilter* filter, qxt_d().nativeFilters)
41     {
42         if (filter && filter->x11EventFilter(event))
43             return true;
44     }
45
46     if (event->type == KeyPress)
47     {
48         XKeyEvent* key = (XKeyEvent*) event;
49         qxt_d().activateHotKey(key->state, key->keycode);
50     }
51     return QApplication::x11EventFilter(event);
52 }
53
54 uint QxtApplicationPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers) const
55 {
56     // ShiftMask, LockMask, ControlMask, Mod1Mask, Mod2Mask, Mod3Mask, Mod4Mask, and Mod5Mask
57     uint native = 0;
58     if (modifiers & Qt::ShiftModifier)
59         native |= ShiftMask;
60     if (modifiers & Qt::ControlModifier)
61         native |= ControlMask;
62     if (modifiers & Qt::AltModifier)
63         native |= Mod1Mask;
64     // TODO: resolve these?
65     //if (modifiers & Qt::MetaModifier)
66     //if (modifiers & Qt::KeypadModifier)
67     //if (modifiers & Qt::GroupSwitchModifier)
68     return native;
69 }
70
71 uint QxtApplicationPrivate::nativeKeycode(Qt::Key key) const
72 {
73     Display* display = QX11Info::display();
74     return XKeysymToKeycode(display, XStringToKeysym(QKeySequence(key).toString().toLatin1().data()));
75 }
76
77 bool QxtApplicationPrivate::registerHotKey(uint modifiers, uint keycode, QWidget* receiver)
78 {
79     Q_UNUSED(receiver);
80     Display* display = QX11Info::display();
81     Window window = QX11Info::appRootWindow();
82     Bool owner = True;
83     int pointer = GrabModeAsync;
84     int keyboard = GrabModeAsync;
85     return XGrabKey(display, keycode, modifiers, window, owner, pointer, keyboard);
86 }
87
88 bool QxtApplicationPrivate::unregisterHotKey(uint modifiers, uint keycode, QWidget* receiver)
89 {
90     Q_UNUSED(receiver);
91     Display* display = QX11Info::display();
92     Window window = QX11Info::appRootWindow();
93     return XUngrabKey(display, keycode, modifiers, window);
94 }