2020-05-18 19:37:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "xcursortheme.h"
|
|
|
|
#include "3rdparty/xcursor.h"
|
|
|
|
|
2020-07-13 16:02:45 +00:00
|
|
|
#include <QMap>
|
|
|
|
#include <QSharedData>
|
|
|
|
|
2020-05-18 19:37:41 +00:00
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2020-07-13 16:02:45 +00:00
|
|
|
class KXcursorSpritePrivate : public QSharedData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
QImage data;
|
|
|
|
QPoint hotspot;
|
|
|
|
std::chrono::milliseconds delay;
|
|
|
|
};
|
|
|
|
|
|
|
|
class KXcursorThemePrivate : public QSharedData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
QMap<QByteArray, QVector<KXcursorSprite>> registry;
|
|
|
|
qreal devicePixelRatio = 1;
|
|
|
|
};
|
|
|
|
|
2020-05-18 19:37:41 +00:00
|
|
|
KXcursorSprite::KXcursorSprite()
|
2020-07-13 16:02:45 +00:00
|
|
|
: d(new KXcursorSpritePrivate)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
KXcursorSprite::KXcursorSprite(const KXcursorSprite &other)
|
|
|
|
: d(other.d)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
KXcursorSprite::~KXcursorSprite()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
KXcursorSprite &KXcursorSprite::operator=(const KXcursorSprite &other)
|
2020-05-18 19:37:41 +00:00
|
|
|
{
|
2020-07-13 16:02:45 +00:00
|
|
|
d = other.d;
|
|
|
|
return *this;
|
2020-05-18 19:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
KXcursorSprite::KXcursorSprite(const QImage &data, const QPoint &hotspot,
|
|
|
|
const std::chrono::milliseconds &delay)
|
2020-07-13 16:02:45 +00:00
|
|
|
: d(new KXcursorSpritePrivate)
|
2020-05-18 19:37:41 +00:00
|
|
|
{
|
2020-07-13 16:02:45 +00:00
|
|
|
d->data = data;
|
|
|
|
d->hotspot = hotspot;
|
|
|
|
d->delay = delay;
|
2020-05-18 19:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QImage KXcursorSprite::data() const
|
|
|
|
{
|
2020-07-13 16:02:45 +00:00
|
|
|
return d->data;
|
2020-05-18 19:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPoint KXcursorSprite::hotspot() const
|
|
|
|
{
|
2020-07-13 16:02:45 +00:00
|
|
|
return d->hotspot;
|
2020-05-18 19:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::chrono::milliseconds KXcursorSprite::delay() const
|
|
|
|
{
|
2020-07-13 16:02:45 +00:00
|
|
|
return d->delay;
|
2020-05-18 19:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void load_callback(XcursorImages *images, void *data)
|
|
|
|
{
|
2020-07-13 16:02:45 +00:00
|
|
|
KXcursorThemePrivate *themePrivate = static_cast<KXcursorThemePrivate *>(data);
|
2020-05-18 19:37:41 +00:00
|
|
|
QVector<KXcursorSprite> sprites;
|
|
|
|
|
|
|
|
for (int i = 0; i < images->nimage; ++i) {
|
|
|
|
const XcursorImage *nativeCursorImage = images->images[i];
|
|
|
|
const QPoint hotspot(nativeCursorImage->xhot, nativeCursorImage->yhot);
|
|
|
|
const std::chrono::milliseconds delay(nativeCursorImage->delay);
|
|
|
|
|
|
|
|
QImage data(nativeCursorImage->width, nativeCursorImage->height, QImage::Format_ARGB32);
|
|
|
|
memcpy(data.bits(), nativeCursorImage->pixels, data.sizeInBytes());
|
|
|
|
|
2020-07-13 16:05:34 +00:00
|
|
|
sprites.append(KXcursorSprite(data, hotspot / themePrivate->devicePixelRatio, delay));
|
2020-05-18 19:37:41 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 16:02:45 +00:00
|
|
|
themePrivate->registry.insert(images->name, sprites);
|
2020-05-18 19:37:41 +00:00
|
|
|
XcursorImagesDestroy(images);
|
|
|
|
}
|
|
|
|
|
2020-07-13 16:02:45 +00:00
|
|
|
KXcursorTheme::KXcursorTheme()
|
|
|
|
: d(new KXcursorThemePrivate)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
KXcursorTheme::KXcursorTheme(const KXcursorTheme &other)
|
|
|
|
: d(other.d)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
KXcursorTheme::~KXcursorTheme()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
KXcursorTheme &KXcursorTheme::operator=(const KXcursorTheme &other)
|
|
|
|
{
|
|
|
|
d = other.d;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-05-18 19:37:41 +00:00
|
|
|
qreal KXcursorTheme::devicePixelRatio() const
|
|
|
|
{
|
2020-07-13 16:02:45 +00:00
|
|
|
return d->devicePixelRatio;
|
2020-05-18 19:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool KXcursorTheme::isEmpty() const
|
|
|
|
{
|
2020-07-13 16:02:45 +00:00
|
|
|
return d->registry.isEmpty();
|
2020-05-18 19:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QVector<KXcursorSprite> KXcursorTheme::shape(const QByteArray &name) const
|
|
|
|
{
|
2020-07-13 16:02:45 +00:00
|
|
|
return d->registry.value(name);
|
2020-05-18 19:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
KXcursorTheme KXcursorTheme::fromTheme(const QString &themeName, int size, qreal dpr)
|
|
|
|
{
|
|
|
|
KXcursorTheme theme;
|
2020-07-13 16:02:45 +00:00
|
|
|
KXcursorThemePrivate *themePrivate = theme.d;
|
|
|
|
themePrivate->devicePixelRatio = dpr;
|
2020-05-18 19:37:41 +00:00
|
|
|
|
|
|
|
const QByteArray nativeThemeName = themeName.toUtf8();
|
2020-07-13 16:02:45 +00:00
|
|
|
xcursor_load_theme(nativeThemeName, size * dpr, load_callback, themePrivate);
|
2020-05-18 19:37:41 +00:00
|
|
|
|
|
|
|
return theme;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace KWin
|