This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
forked from tildearrow/kwin-lowlatency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor.h
304 lines (279 loc) · 9.41 KB
/
cursor.h
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2013 Martin Gräßlin <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#ifndef KWIN_CURSOR_H
#define KWIN_CURSOR_H
// kwin
#include <kwinglobals.h>
// Qt
#include <QHash>
#include <QObject>
#include <QPoint>
// xcb
#include <xcb/xcb.h>
class QTimer;
namespace KWin
{
namespace ExtendedCursor {
/**
* Extension of Qt::CursorShape with values not currently present there
*/
enum Shape {
SizeNorthWest = 0x100 + 0,
SizeNorth = 0x100 + 1,
SizeNorthEast = 0x100 + 2,
SizeEast = 0x100 + 3,
SizeWest = 0x100 + 4,
SizeSouthEast = 0x100 + 5,
SizeSouth = 0x100 + 6,
SizeSouthWest = 0x100 + 7
};
}
/**
* @brief Wrapper round Qt::CursorShape with extensions enums into a single entity
*/
class KWIN_EXPORT CursorShape {
public:
CursorShape() = default;
CursorShape(Qt::CursorShape qtShape) {
m_shape = qtShape;
}
CursorShape(KWin::ExtendedCursor::Shape kwinShape) {
m_shape = kwinShape;
}
bool operator==(const CursorShape &o) const {
return m_shape == o.m_shape;
}
operator int() const {
return m_shape;
}
/**
* @brief The name of a cursor shape in the theme.
*/
QByteArray name() const;
private:
int m_shape = Qt::ArrowCursor;
};
/**
* @short Replacement for QCursor.
*
* This class provides a similar API to QCursor and should be preferred inside KWin. It allows to
* get the position and warp the mouse cursor with static methods just like QCursor. It also provides
* the possibility to get an X11 cursor for a Qt::CursorShape - a functionality lost in Qt 5's QCursor
* implementation.
*
* In addition the class provides a mouse polling facility as required by e.g. Effects and ScreenEdges
* and emits signals when the mouse position changes. In opposite to QCursor this class is a QObject
* and cannot be constructed. Instead it provides a singleton getter, though the most important
* methods are wrapped in a static method, just like QCursor.
*
* The actual implementation is split into two parts: a system independent interface and a windowing
* system specific subclass. So far only an X11 backend is implemented which uses query pointer to
* fetch the position and warp pointer to set the position. It uses a timer based mouse polling and
* can provide X11 cursors through the XCursor library.
*/
class KWIN_EXPORT Cursor : public QObject
{
Q_OBJECT
public:
~Cursor() override;
void startMousePolling();
void stopMousePolling();
/**
* @brief Enables tracking changes of cursor images.
*
* After enabling cursor change tracking the signal cursorChanged will be emitted
* whenever a change to the cursor image is recognized.
*
* Use stopCursorTracking to no longer emit this signal. Note: the signal will be
* emitted until each call of this method has been matched with a call to stopCursorTracking.
*
* This tracking is not about pointer position tracking.
* @see stopCursorTracking
* @see cursorChanged
*/
void startCursorTracking();
/**
* @brief Disables tracking changes of cursor images.
*
* Only call after using startCursorTracking.
*
* @see startCursorTracking
*/
void stopCursorTracking();
/**
* @brief The name of the currently used Cursor theme.
*
* @return const QString&
*/
const QString &themeName() const;
/**
* @brief The size of the currently used Cursor theme.
*
* @return int
*/
int themeSize() const;
/**
* @return list of alternative names for the cursor with @p name
*/
QVector<QByteArray> cursorAlternativeNames(const QByteArray &name) const;
/**
* Returns the current cursor position. This method does an update of the mouse position if
* needed. It's save to call it multiple times.
*
* Implementing subclasses should prefer to use currentPos which is not performing a check
* for update.
*/
static QPoint pos();
/**
* Warps the mouse cursor to new @p pos.
*/
static void setPos(const QPoint &pos);
static void setPos(int x, int y);
static xcb_cursor_t x11Cursor(CursorShape shape);
/**
* Notice: if available always use the CursorShape variant to avoid cache duplicates for
* ambiguous cursor names in the non existing cursor name specification
*/
static xcb_cursor_t x11Cursor(const QByteArray &name);
Q_SIGNALS:
void posChanged(QPoint pos);
void mouseChanged(const QPoint& pos, const QPoint& oldpos,
Qt::MouseButtons buttons, Qt::MouseButtons oldbuttons,
Qt::KeyboardModifiers modifiers, Qt::KeyboardModifiers oldmodifiers);
/**
* @brief Signal emitted when the cursor image changes.
*
* To enable these signals use startCursorTracking.
*
* @see startCursorTracking
* @see stopCursorTracking
*/
void cursorChanged();
void themeChanged();
protected:
/**
* Called from x11Cursor to actually retrieve the X11 cursor. Base implementation returns
* a null cursor, an implementing subclass should implement this method if it can provide X11
* mouse cursors.
*/
virtual xcb_cursor_t getX11Cursor(CursorShape shape);
/**
* Called from x11Cursor to actually retrieve the X11 cursor. Base implementation returns
* a null cursor, an implementing subclass should implement this method if it can provide X11
* mouse cursors.
*/
virtual xcb_cursor_t getX11Cursor(const QByteArray &name);
/**
* Performs the actual warping of the cursor.
*/
virtual void doSetPos();
/**
* Called from @ref pos() to allow syncing the internal position with the underlying
* system's cursor position.
*/
virtual void doGetPos();
/**
* Called from startMousePolling when the mouse polling gets activated. Base implementation
* does nothing, inheriting classes can overwrite to e.g. start a timer.
*/
virtual void doStartMousePolling();
/**
* Called from stopMousePolling when the mouse polling gets deactivated. Base implementation
* does nothing, inheriting classes can overwrite to e.g. stop a timer.
*/
virtual void doStopMousePolling();
/**
* Called from startCursorTracking when cursor image tracking gets activated. Inheriting class needs
* to overwrite to enable platform specific code for the tracking.
*/
virtual void doStartCursorTracking();
/**
* Called from stopCursorTracking when cursor image tracking gets deactivated. Inheriting class needs
* to overwrite to disable platform specific code for the tracking.
*/
virtual void doStopCursorTracking();
bool isCursorTracking() const;
/**
* Provides the actual internal cursor position to inheriting classes. If an inheriting class needs
* access to the cursor position this method should be used instead of the static @ref pos, as
* the static method syncs with the underlying system's cursor.
*/
const QPoint ¤tPos() const;
/**
* Updates the internal position to @p pos without warping the pointer as
* setPos does.
*/
void updatePos(const QPoint &pos);
void updatePos(int x, int y);
private Q_SLOTS:
void loadThemeSettings();
void slotKGlobalSettingsNotifyChange(int type, int arg);
private:
void updateTheme(const QString &name, int size);
void loadThemeFromKConfig();
QPoint m_pos;
int m_mousePollingCounter;
int m_cursorTrackingCounter;
QString m_themeName;
int m_themeSize;
KWIN_SINGLETON(Cursor)
};
/**
* @brief Implementation using the InputRedirection framework to get pointer positions.
*
* Does not support warping of cursor.
*/
class InputRedirectionCursor : public Cursor
{
Q_OBJECT
public:
explicit InputRedirectionCursor(QObject *parent);
~InputRedirectionCursor() override;
protected:
void doSetPos() override;
void doStartCursorTracking() override;
void doStopCursorTracking() override;
private Q_SLOTS:
void slotPosChanged(const QPointF &pos);
void slotPointerButtonChanged();
void slotModifiersChanged(Qt::KeyboardModifiers mods, Qt::KeyboardModifiers oldMods);
private:
Qt::MouseButtons m_currentButtons;
friend class Cursor;
};
inline const QPoint &Cursor::currentPos() const
{
return m_pos;
}
inline void Cursor::updatePos(int x, int y)
{
updatePos(QPoint(x, y));
}
inline const QString& Cursor::themeName() const
{
return m_themeName;
}
inline int Cursor::themeSize() const
{
return m_themeSize;
}
inline bool Cursor::isCursorTracking() const
{
return m_cursorTrackingCounter > 0;
}
}
Q_DECLARE_METATYPE(KWin::CursorShape)
#endif // KWIN_CURSOR_H