forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhotkeywrapper.hh
236 lines (168 loc) · 5.43 KB
/
hotkeywrapper.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#ifndef HOTKEYWRAPPER_H
#define HOTKEYWRAPPER_H
#include <QtGui>
#ifdef HAVE_X11
#include <set>
#include <X11/Xlib.h>
#include <X11/extensions/record.h>
#include <QX11Info>
#include <X11/Xlibint.h>
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#undef Bool
#endif
#endif
#ifdef Q_OS_MAC
#define __SECURITYHI__
#include <Carbon/Carbon.h>
#endif
#include "ex.hh"
#include "qtsingleapplication.h"
#include "qt4x5.hh"
//////////////////////////////////////////////////////////////////////////
struct HotkeyStruct
{
HotkeyStruct() {}
HotkeyStruct( quint32 key, quint32 key2, quint32 modifier, int handle, int id );
quint32 key, key2;
quint32 modifier;
int handle;
int id;
#ifdef Q_OS_MAC
EventHotKeyRef hkRef, hkRef2;
#endif
};
//////////////////////////////////////////////////////////////////////////
#if !defined(Q_WS_QWS)
class HotkeyWrapper : public QThread // Thread is actually only used on X11
{
Q_OBJECT
friend class QHotkeyApplication;
public:
DEF_EX( exInit, "Hotkey wrapper failed to init", std::exception )
HotkeyWrapper(QObject *parent);
virtual ~HotkeyWrapper();
/// The handle is passed back in hotkeyActivated() to inform which hotkey
/// was activated.
bool setGlobalKey( int key, int key2, Qt::KeyboardModifiers modifier,
int handle );
/// Unregisters everything
void unregister();
signals:
void hotkeyActivated( int );
protected slots:
void waitKey2();
#ifndef Q_OS_MAC
private slots:
bool checkState( quint32 vk, quint32 mod );
#endif
private:
void init();
quint32 nativeKey(int key);
QList<HotkeyStruct> hotkeys;
bool state2;
HotkeyStruct state2waiter;
#ifdef Q_OS_WIN32
virtual bool winEvent ( MSG * message, long * result );
HWND hwnd;
#elif defined(Q_OS_MAC)
public:
void activated( int hkId );
private:
void sendCmdC();
static EventHandlerUPP hotKeyFunction;
quint32 keyC;
EventHandlerRef handlerRef;
#else
static void recordEventCallback( XPointer, XRecordInterceptData * );
/// Called by recordEventCallback()
void handleRecordEvent( XRecordInterceptData * );
void run(); // QThread
// We do one-time init of those, translating keysyms to keycodes
KeyCode lShiftCode, rShiftCode, lCtrlCode, rCtrlCode, lAltCode, rAltCode,
cCode, insertCode, kpInsertCode, lMetaCode, rMetaCode;
quint32 currentModifiers;
Display * dataDisplay;
XRecordRange * recordRange;
XRecordContext recordContext;
XRecordClientSpec recordClientSpec;
/// Holds all the keys currently grabbed.
/// The first value is keycode, the second is modifiers
typedef std::set< std::pair< quint32, quint32 > > GrabbedKeys;
GrabbedKeys grabbedKeys;
GrabbedKeys::iterator keyToUngrab; // Used for second stage grabs
/// Returns true if the given key is usually used to copy from clipboard,
/// false otherwise.
bool isCopyToClipboardKey( quint32 keyCode, quint32 modifiers ) const;
/// Returns true if the given key is grabbed, false otherwise
bool isKeyGrabbed( quint32 keyCode, quint32 modifiers ) const;
/// Grabs the given key, recording the fact in grabbedKeys. If the key's
/// already grabbed, does nothing.
/// Returns the key's iterator in grabbedKeys.
GrabbedKeys::iterator grabKey( quint32 keyCode, quint32 modifiers );
/// Ungrabs the given key. erasing it from grabbedKeys. The key's provided
/// as an interator inside the grabbedKeys set.
void ungrabKey( GrabbedKeys::iterator );
signals:
/// Emitted from the thread
void keyRecorded( quint32 vk, quint32 mod );
#endif
};
#else
class HotkeyWrapper : public QObject
{
Q_OBJECT
friend class QHotkeyApplication;
public:
DEF_EX( exInit, "Hotkey wrapper failed to init", std::exception )
HotkeyWrapper(QObject *parent): QObject( parent )
{}
bool setGlobalKey( int key, int key2, Qt::KeyboardModifiers modifier,
int handle )
{ return true; }
void unregister()
{}
signals:
void hotkeyActivated( int );
};
#endif
//////////////////////////////////////////////////////////////////////////
class DataCommitter
{
public:
virtual void commitData( QSessionManager & )=0;
virtual ~DataCommitter()
{}
};
class QHotkeyApplication : public QtSingleApplication
#if defined( Q_OS_WIN ) && IS_QT_5
, public QAbstractNativeEventFilter
#endif
{
friend class HotkeyWrapper;
QList< DataCommitter * > dataCommitters;
public:
QHotkeyApplication( int & argc, char ** argv );
QHotkeyApplication( QString const & id, int & argc, char ** argv );
void addDataCommiter( DataCommitter & );
void removeDataCommiter( DataCommitter & );
/// This calls all data committers.
virtual void commitData( QSessionManager & );
protected:
void registerWrapper(HotkeyWrapper *wrapper);
void unregisterWrapper(HotkeyWrapper *wrapper);
#ifdef Q_OS_WIN32
#if IS_QT_5
virtual bool nativeEventFilter( const QByteArray & eventType, void * message, long * result );
#else // IS_QT_5
virtual bool winEventFilter ( MSG * message, long * result );
#endif // IS_QT_5
QWidget * mainWindow;
public:
void setMainWindow( QWidget * widget )
{ mainWindow = widget; }
protected:
#endif // Q_OS_WIN32
QList<HotkeyWrapper*> hotkeyWrappers;
};
//////////////////////////////////////////////////////////////////////////
#endif // HOTKEYWRAPPER_H