KWin
Loading...
Searching...
No Matches
cursorsource.cpp
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2022 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "cursorsource.h"
8#include "cursor.h"
9#include "wayland/surface.h"
10
11namespace KWin
12{
13
15 : QObject(parent)
16{
17}
18
19QSizeF CursorSource::size() const
20{
21 return m_size;
22}
23
24QPointF CursorSource::hotspot() const
25{
26 return m_hotspot;
27}
28
30 : CursorSource(parent)
31{
32 m_delayTimer.setSingleShot(true);
33 connect(&m_delayTimer, &QTimer::timeout, this, &ShapeCursorSource::selectNextSprite);
34}
35
37{
38 return m_image;
39}
40
41QByteArray ShapeCursorSource::shape() const
42{
43 return m_shape;
44}
45
46void ShapeCursorSource::setShape(const QByteArray &shape)
47{
48 if (m_shape != shape) {
49 m_shape = shape;
50 refresh();
51 }
52}
53
54void ShapeCursorSource::setShape(Qt::CursorShape shape)
55{
56 setShape(CursorShape(shape).name());
57}
58
60{
61 return m_theme;
62}
63
65{
66 if (m_theme != theme) {
67 m_theme = theme;
68 refresh();
69 }
70}
71
72void ShapeCursorSource::refresh()
73{
74 m_currentSprite = -1;
75 m_delayTimer.stop();
76
77 m_sprites = m_theme.shape(m_shape);
78 if (m_sprites.isEmpty()) {
79 const auto alternativeNames = CursorShape::alternatives(m_shape);
80 for (const QByteArray &alternativeName : alternativeNames) {
81 m_sprites = m_theme.shape(alternativeName);
82 if (!m_sprites.isEmpty()) {
83 break;
84 }
85 }
86 }
87
88 if (!m_sprites.isEmpty()) {
89 selectSprite(0);
90 }
91}
92
93void ShapeCursorSource::selectNextSprite()
94{
95 selectSprite((m_currentSprite + 1) % m_sprites.size());
96}
97
98void ShapeCursorSource::selectSprite(int index)
99{
100 if (m_currentSprite == index) {
101 return;
102 }
103 const KXcursorSprite &sprite = m_sprites[index];
104 m_currentSprite = index;
105 m_image = sprite.data();
106 m_size = QSizeF(m_image.size()) / m_image.devicePixelRatio();
107 m_hotspot = sprite.hotspot();
108 if (sprite.delay().count() && m_sprites.size() > 1) {
109 m_delayTimer.start(sprite.delay());
110 }
111 Q_EMIT changed();
112}
113
115 : CursorSource(parent)
116{
117}
118
120{
121 return m_surface;
122}
123
124void SurfaceCursorSource::refresh()
125{
126 m_size = m_surface->size();
127 m_hotspot -= m_surface->offset();
128 Q_EMIT changed();
129}
130
131void SurfaceCursorSource::reset()
132{
133 m_size = QSizeF(0, 0);
134 m_hotspot = QPointF(0, 0);
135 m_surface = nullptr;
136 Q_EMIT changed();
137}
138
139void SurfaceCursorSource::update(SurfaceInterface *surface, const QPointF &hotspot)
140{
141 bool dirty = false;
142
143 if (m_hotspot != hotspot) {
144 dirty = true;
146 }
147
148 if (m_surface != surface) {
149 dirty = true;
150
151 if (m_surface) {
152 disconnect(m_surface, &SurfaceInterface::committed, this, &SurfaceCursorSource::refresh);
153 disconnect(m_surface, &SurfaceInterface::destroyed, this, &SurfaceCursorSource::reset);
154 }
155
156 m_surface = surface;
157
158 if (m_surface) {
159 m_size = surface->size();
160
161 connect(m_surface, &SurfaceInterface::committed, this, &SurfaceCursorSource::refresh);
162 connect(m_surface, &SurfaceInterface::destroyed, this, &SurfaceCursorSource::reset);
163 } else {
164 m_size = QSizeF(0, 0);
165 }
166 }
167
168 if (dirty) {
169 Q_EMIT changed();
170 }
171}
172
173} // namespace KWin
174
175#include "moc_cursorsource.cpp"
Wrapper round Qt::CursorShape with extensions enums into a single entity.
Definition cursor.h:50
static QList< QByteArray > alternatives(const QByteArray &name)
Definition cursor.cpp:343
QSizeF size() const
CursorSource(QObject *parent=nullptr)
QPointF hotspot() const
QList< KXcursorSprite > shape(const QByteArray &name) const
void setTheme(const KXcursorTheme &theme)
KXcursorTheme theme() const
QByteArray shape() const
void setShape(Qt::CursorShape shape)
ShapeCursorSource(QObject *parent=nullptr)
void setShape(const QByteArray &shape)
SurfaceCursorSource(QObject *parent=nullptr)
SurfaceInterface * surface() const
void update(SurfaceInterface *surface, const QPointF &hotspot)
Resource representing a wl_surface.
Definition surface.h:80
QPoint offset() const
Definition surface.cpp:814