KWin
Loading...
Searching...
No Matches
cursor.h
Go to the documentation of this file.
1/*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2013 Martin Gräßlin <mgraesslin@kde.org>
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
9#pragma once
10// kwin
11#include "effect/globals.h"
12// Qt
13#include <QHash>
14#include <QObject>
15#include <QPoint>
16// KF
17#include <KSharedConfig>
18// xcb
19#include <xcb/xcb.h>
20
21class QTimer;
22
23namespace KWin
24{
25
26class CursorSource;
27class Output;
28
29namespace ExtendedCursor
30{
34enum Shape {
35 SizeNorthWest = 0x100 + 0,
36 SizeNorth = 0x100 + 1,
37 SizeNorthEast = 0x100 + 2,
38 SizeEast = 0x100 + 3,
39 SizeWest = 0x100 + 4,
40 SizeSouthEast = 0x100 + 5,
41 SizeSouth = 0x100 + 6,
42 SizeSouthWest = 0x100 + 7
43};
44}
45
49class KWIN_EXPORT CursorShape
50{
51public:
52 CursorShape() = default;
53 CursorShape(Qt::CursorShape qtShape)
54 {
55 m_shape = qtShape;
56 }
58 {
59 m_shape = kwinShape;
60 }
61 bool operator==(const CursorShape &o) const
62 {
63 return m_shape == o.m_shape;
64 }
65 operator int() const
66 {
67 return m_shape;
68 }
72 QByteArray name() const;
73
77 static QList<QByteArray> alternatives(const QByteArray &name);
78
79private:
80 int m_shape = Qt::ArrowCursor;
81};
82
101class KWIN_EXPORT Cursor : public QObject
102{
103 Q_OBJECT
104public:
105 Cursor();
106 ~Cursor() override;
107 void startMousePolling();
108 void stopMousePolling();
122 void startCursorTracking();
130 void stopCursorTracking();
131
137 const QString &themeName() const;
143 int themeSize() const;
147 static QString defaultThemeName();
151 static int defaultThemeSize();
152
160 QPointF pos();
164 void setPos(const QPointF &pos);
165 xcb_cursor_t x11Cursor(CursorShape shape);
170 xcb_cursor_t x11Cursor(const QByteArray &name);
171
172 QPointF hotspot() const;
173 QRectF geometry() const;
174 QRectF rect() const;
175
176 CursorSource *source() const;
177 void setSource(CursorSource *source);
178
182 bool isOnOutput(Output *output) const;
183
184 void markAsRendered(std::chrono::milliseconds timestamp);
185
186Q_SIGNALS:
187 void posChanged(const QPointF &pos);
188 void mouseChanged(const QPointF &pos, const QPointF &oldpos,
189 Qt::MouseButtons buttons, Qt::MouseButtons oldbuttons,
190 Qt::KeyboardModifiers modifiers, Qt::KeyboardModifiers oldmodifiers);
201 void rendered(std::chrono::milliseconds timestamp);
202
203protected:
207 virtual void doSetPos();
212 virtual void doGetPos();
217 virtual void doStartMousePolling();
222 virtual void doStopMousePolling();
227 virtual void doStartCursorTracking();
232 virtual void doStopCursorTracking();
233 bool isCursorTracking() const;
239 const QPointF &currentPos() const;
244 void updatePos(const QPointF &pos);
245
246private Q_SLOTS:
247 void loadThemeSettings();
248 void slotKGlobalSettingsNotifyChange(int type, int arg);
249
250private:
251 void updateTheme(const QString &name, int size);
252 void loadThemeFromKConfig();
253 CursorSource *m_source = nullptr;
254 QHash<QByteArray, xcb_cursor_t> m_cursors;
255 QPointF m_pos;
256 int m_mousePollingCounter;
257 int m_cursorTrackingCounter;
258 QString m_themeName;
259 int m_themeSize;
260};
261
262class KWIN_EXPORT Cursors : public QObject
263{
264 Q_OBJECT
265public:
266 Cursor *mouse() const
267 {
268 return m_mouse;
269 }
270
271 void setMouse(Cursor *mouse)
272 {
273 if (m_mouse != mouse) {
274 m_mouse = mouse;
275
276 addCursor(m_mouse);
277 setCurrentCursor(m_mouse);
278 }
279 }
280
281 void addCursor(Cursor *cursor);
282 void removeCursor(Cursor *cursor);
283
286 {
287 return m_currentCursor;
288 }
289
290 void hideCursor();
291 void showCursor();
292 bool isCursorHidden() const;
293
294 static Cursors *self();
295
296Q_SIGNALS:
299 void positionChanged(Cursor *cursor, const QPointF &position);
300
301private:
302 void emitCurrentCursorChanged();
303 void setCurrentCursor(Cursor *cursor);
304
305 static Cursors *s_self;
306 Cursor *m_currentCursor = nullptr;
307 Cursor *m_mouse = nullptr;
308 QList<Cursor *> m_cursors;
309 int m_cursorHideCounter = 0;
310};
311
312inline const QPointF &Cursor::currentPos() const
313{
314 return m_pos;
315}
316
317inline const QString &Cursor::themeName() const
318{
319 return m_themeName;
320}
321
322inline int Cursor::themeSize() const
323{
324 return m_themeSize;
325}
326
327inline bool Cursor::isCursorTracking() const
328{
329 return m_cursorTrackingCounter > 0;
330}
331
332}
333
Replacement for QCursor.
Definition cursor.h:102
void mouseChanged(const QPointF &pos, const QPointF &oldpos, Qt::MouseButtons buttons, Qt::MouseButtons oldbuttons, Qt::KeyboardModifiers modifiers, Qt::KeyboardModifiers oldmodifiers)
bool isCursorTracking() const
Definition cursor.h:327
int themeSize() const
The size of the currently used Cursor theme.
Definition cursor.h:322
void posChanged(const QPointF &pos)
const QString & themeName() const
The name of the currently used Cursor theme.
Definition cursor.h:317
void themeChanged()
const QPointF & currentPos() const
Definition cursor.h:312
void cursorChanged()
Signal emitted when the cursor image changes.
void rendered(std::chrono::milliseconds timestamp)
Wrapper round Qt::CursorShape with extensions enums into a single entity.
Definition cursor.h:50
CursorShape()=default
CursorShape(KWin::ExtendedCursor::Shape kwinShape)
Definition cursor.h:57
bool operator==(const CursorShape &o) const
Definition cursor.h:61
CursorShape(Qt::CursorShape qtShape)
Definition cursor.h:53
void positionChanged(Cursor *cursor, const QPointF &position)
void currentCursorChanged(Cursor *cursor)
Cursor * currentCursor() const
Definition cursor.h:285
void setMouse(Cursor *mouse)
Definition cursor.h:271
Cursor * mouse() const
Definition cursor.h:266
void hiddenChanged()
Q_DECLARE_METATYPE(KWin::SwitchEvent::State)