2022-11-18 18:58:17 +00:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2022 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cursorsource.h"
|
2022-11-18 20:22:03 +00:00
|
|
|
#include "cursor.h"
|
2022-12-19 10:20:21 +00:00
|
|
|
#include "wayland/surface_interface.h"
|
2022-11-18 18:58:17 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
|
|
|
CursorSource::CursorSource(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-02-19 23:35:44 +00:00
|
|
|
QSizeF CursorSource::size() const
|
2022-12-21 18:30:27 +00:00
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
2023-02-19 23:35:44 +00:00
|
|
|
QPointF CursorSource::hotspot() const
|
2022-11-18 18:58:17 +00:00
|
|
|
{
|
|
|
|
return m_hotspot;
|
|
|
|
}
|
|
|
|
|
2022-11-18 20:22:03 +00:00
|
|
|
ShapeCursorSource::ShapeCursorSource(QObject *parent)
|
|
|
|
: CursorSource(parent)
|
|
|
|
{
|
|
|
|
m_delayTimer.setSingleShot(true);
|
|
|
|
connect(&m_delayTimer, &QTimer::timeout, this, &ShapeCursorSource::selectNextSprite);
|
|
|
|
}
|
|
|
|
|
2023-05-10 06:52:41 +00:00
|
|
|
QImage ShapeCursorSource::image() const
|
|
|
|
{
|
|
|
|
return m_image;
|
|
|
|
}
|
|
|
|
|
2022-11-18 20:22:03 +00:00
|
|
|
QByteArray ShapeCursorSource::shape() const
|
|
|
|
{
|
|
|
|
return m_shape;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShapeCursorSource::setShape(const QByteArray &shape)
|
|
|
|
{
|
|
|
|
if (m_shape != shape) {
|
|
|
|
m_shape = shape;
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShapeCursorSource::setShape(Qt::CursorShape shape)
|
|
|
|
{
|
|
|
|
setShape(CursorShape(shape).name());
|
|
|
|
}
|
|
|
|
|
|
|
|
KXcursorTheme ShapeCursorSource::theme() const
|
|
|
|
{
|
|
|
|
return m_theme;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShapeCursorSource::setTheme(const KXcursorTheme &theme)
|
|
|
|
{
|
|
|
|
if (m_theme != theme) {
|
|
|
|
m_theme = theme;
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShapeCursorSource::refresh()
|
|
|
|
{
|
|
|
|
m_currentSprite = -1;
|
|
|
|
m_delayTimer.stop();
|
|
|
|
|
|
|
|
m_sprites = m_theme.shape(m_shape);
|
|
|
|
if (m_sprites.isEmpty()) {
|
|
|
|
const auto alternativeNames = Cursor::cursorAlternativeNames(m_shape);
|
|
|
|
for (const QByteArray &alternativeName : alternativeNames) {
|
|
|
|
m_sprites = m_theme.shape(alternativeName);
|
|
|
|
if (!m_sprites.isEmpty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_sprites.isEmpty()) {
|
|
|
|
selectSprite(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShapeCursorSource::selectNextSprite()
|
|
|
|
{
|
|
|
|
selectSprite((m_currentSprite + 1) % m_sprites.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShapeCursorSource::selectSprite(int index)
|
|
|
|
{
|
|
|
|
if (m_currentSprite == index) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const KXcursorSprite &sprite = m_sprites[index];
|
|
|
|
m_currentSprite = index;
|
|
|
|
m_image = sprite.data();
|
2023-02-19 23:35:44 +00:00
|
|
|
m_size = QSizeF(m_image.size()) / m_image.devicePixelRatio();
|
2022-11-18 20:22:03 +00:00
|
|
|
m_hotspot = sprite.hotspot();
|
|
|
|
if (sprite.delay().count() && m_sprites.size() > 1) {
|
|
|
|
m_delayTimer.start(sprite.delay());
|
|
|
|
}
|
|
|
|
Q_EMIT changed();
|
|
|
|
}
|
|
|
|
|
2022-12-19 10:20:21 +00:00
|
|
|
SurfaceCursorSource::SurfaceCursorSource(QObject *parent)
|
|
|
|
: CursorSource(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
KWaylandServer::SurfaceInterface *SurfaceCursorSource::surface() const
|
|
|
|
{
|
|
|
|
return m_surface;
|
|
|
|
}
|
|
|
|
|
2023-05-07 12:44:24 +00:00
|
|
|
void SurfaceCursorSource::refresh()
|
2022-12-19 10:20:21 +00:00
|
|
|
{
|
2023-05-07 12:44:24 +00:00
|
|
|
m_size = m_surface->size();
|
|
|
|
Q_EMIT changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceCursorSource::update(KWaylandServer::SurfaceInterface *surface, const QPointF &hotspot)
|
|
|
|
{
|
|
|
|
bool dirty = false;
|
|
|
|
|
|
|
|
if (m_hotspot != hotspot) {
|
|
|
|
dirty = true;
|
|
|
|
m_hotspot = hotspot;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_surface != surface) {
|
|
|
|
dirty = true;
|
|
|
|
|
|
|
|
if (m_surface) {
|
|
|
|
disconnect(m_surface, &KWaylandServer::SurfaceInterface::committed, this, &SurfaceCursorSource::refresh);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_surface = surface;
|
|
|
|
|
|
|
|
if (m_surface) {
|
|
|
|
m_size = surface->size();
|
|
|
|
|
|
|
|
connect(m_surface, &KWaylandServer::SurfaceInterface::committed, this, &SurfaceCursorSource::refresh);
|
2022-12-19 10:20:21 +00:00
|
|
|
} else {
|
2023-05-07 12:44:24 +00:00
|
|
|
m_size = QSizeF(0, 0);
|
2022-12-19 10:20:21 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-07 12:44:24 +00:00
|
|
|
|
|
|
|
if (dirty) {
|
|
|
|
Q_EMIT changed();
|
|
|
|
}
|
2022-12-19 10:20:21 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 18:58:17 +00:00
|
|
|
} // namespace KWin
|