2020-05-18 19:37:41 +00:00
|
|
|
/*
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-FileCopyrightText: 2020 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2020-05-18 19:37:41 +00:00
|
|
|
|
|
|
|
#include "xcursortheme.h"
|
|
|
|
#include "3rdparty/xcursor.h"
|
|
|
|
|
wayland: Rework Xcursor theme loading
Xcursor loading code has hardcoded search paths, in order to take into
account distros installing app data in a different location,
libwayland-cursor sets the ICONDIR to the icon directory computed based
on the install prefix.
However, that won't work with gitlab CI because it relocates binaries. A
more robust way to find cursors would be to use QStandardPaths to find
all the icon directories on the system.
Another advantage of using own cursor loading code is that it allows us
to reuse cursor images that are symlinks. For example, with
breeze_cursors, almost half of the files in the cursors directory are
symlinks.
The main disadvantage of this approach is that we would have to keep the
search paths up to date. However, on the hand, there are not that many
of them, e.g. ~/.icons, ~/.local/share/icons, /usr/share/icons,
/usr/local/share/icons. The last three are implicitly handled by the
QStandardPaths.
2022-01-27 20:47:11 +00:00
|
|
|
#include <KConfig>
|
|
|
|
#include <KConfigGroup>
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
2020-07-13 16:02:45 +00:00
|
|
|
#include <QSharedData>
|
wayland: Rework Xcursor theme loading
Xcursor loading code has hardcoded search paths, in order to take into
account distros installing app data in a different location,
libwayland-cursor sets the ICONDIR to the icon directory computed based
on the install prefix.
However, that won't work with gitlab CI because it relocates binaries. A
more robust way to find cursors would be to use QStandardPaths to find
all the icon directories on the system.
Another advantage of using own cursor loading code is that it allows us
to reuse cursor images that are symlinks. For example, with
breeze_cursors, almost half of the files in the cursors directory are
symlinks.
The main disadvantage of this approach is that we would have to keep the
search paths up to date. However, on the hand, there are not that many
of them, e.g. ~/.icons, ~/.local/share/icons, /usr/share/icons,
/usr/local/share/icons. The last three are implicitly handled by the
QStandardPaths.
2022-01-27 20:47:11 +00:00
|
|
|
#include <QStandardPaths>
|
2020-07-13 16:02:45 +00:00
|
|
|
|
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:
|
wayland: Rework Xcursor theme loading
Xcursor loading code has hardcoded search paths, in order to take into
account distros installing app data in a different location,
libwayland-cursor sets the ICONDIR to the icon directory computed based
on the install prefix.
However, that won't work with gitlab CI because it relocates binaries. A
more robust way to find cursors would be to use QStandardPaths to find
all the icon directories on the system.
Another advantage of using own cursor loading code is that it allows us
to reuse cursor images that are symlinks. For example, with
breeze_cursors, almost half of the files in the cursors directory are
symlinks.
The main disadvantage of this approach is that we would have to keep the
search paths up to date. However, on the hand, there are not that many
of them, e.g. ~/.icons, ~/.local/share/icons, /usr/share/icons,
/usr/local/share/icons. The last three are implicitly handled by the
QStandardPaths.
2022-01-27 20:47:11 +00:00
|
|
|
void load(const QString &themeName, int size, qreal devicePixelRatio);
|
|
|
|
void loadCursors(const QString &packagePath, int size, qreal devicePixelRatio);
|
|
|
|
|
|
|
|
QHash<QByteArray, QVector<KXcursorSprite>> registry;
|
2020-07-13 16:02:45 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
wayland: Rework Xcursor theme loading
Xcursor loading code has hardcoded search paths, in order to take into
account distros installing app data in a different location,
libwayland-cursor sets the ICONDIR to the icon directory computed based
on the install prefix.
However, that won't work with gitlab CI because it relocates binaries. A
more robust way to find cursors would be to use QStandardPaths to find
all the icon directories on the system.
Another advantage of using own cursor loading code is that it allows us
to reuse cursor images that are symlinks. For example, with
breeze_cursors, almost half of the files in the cursors directory are
symlinks.
The main disadvantage of this approach is that we would have to keep the
search paths up to date. However, on the hand, there are not that many
of them, e.g. ~/.icons, ~/.local/share/icons, /usr/share/icons,
/usr/local/share/icons. The last three are implicitly handled by the
QStandardPaths.
2022-01-27 20:47:11 +00:00
|
|
|
static QVector<KXcursorSprite> loadCursor(const QString &filePath, int desiredSize, qreal devicePixelRatio)
|
2021-05-02 10:37:45 +00:00
|
|
|
{
|
wayland: Rework Xcursor theme loading
Xcursor loading code has hardcoded search paths, in order to take into
account distros installing app data in a different location,
libwayland-cursor sets the ICONDIR to the icon directory computed based
on the install prefix.
However, that won't work with gitlab CI because it relocates binaries. A
more robust way to find cursors would be to use QStandardPaths to find
all the icon directories on the system.
Another advantage of using own cursor loading code is that it allows us
to reuse cursor images that are symlinks. For example, with
breeze_cursors, almost half of the files in the cursors directory are
symlinks.
The main disadvantage of this approach is that we would have to keep the
search paths up to date. However, on the hand, there are not that many
of them, e.g. ~/.icons, ~/.local/share/icons, /usr/share/icons,
/usr/local/share/icons. The last three are implicitly handled by the
QStandardPaths.
2022-01-27 20:47:11 +00:00
|
|
|
XcursorImages *images = XcursorFileLoadImages(QFile::encodeName(filePath), desiredSize * devicePixelRatio);
|
|
|
|
if (!images) {
|
|
|
|
return {};
|
|
|
|
}
|
2021-05-02 10:37:45 +00:00
|
|
|
|
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];
|
wayland: Rework Xcursor theme loading
Xcursor loading code has hardcoded search paths, in order to take into
account distros installing app data in a different location,
libwayland-cursor sets the ICONDIR to the icon directory computed based
on the install prefix.
However, that won't work with gitlab CI because it relocates binaries. A
more robust way to find cursors would be to use QStandardPaths to find
all the icon directories on the system.
Another advantage of using own cursor loading code is that it allows us
to reuse cursor images that are symlinks. For example, with
breeze_cursors, almost half of the files in the cursors directory are
symlinks.
The main disadvantage of this approach is that we would have to keep the
search paths up to date. However, on the hand, there are not that many
of them, e.g. ~/.icons, ~/.local/share/icons, /usr/share/icons,
/usr/local/share/icons. The last three are implicitly handled by the
QStandardPaths.
2022-01-27 20:47:11 +00:00
|
|
|
const qreal scale = std::max(qreal(1), qreal(nativeCursorImage->size) / desiredSize);
|
2020-05-18 19:37:41 +00:00
|
|
|
const QPoint hotspot(nativeCursorImage->xhot, nativeCursorImage->yhot);
|
|
|
|
const std::chrono::milliseconds delay(nativeCursorImage->delay);
|
|
|
|
|
2021-02-22 13:28:50 +00:00
|
|
|
QImage data(nativeCursorImage->width, nativeCursorImage->height, QImage::Format_ARGB32_Premultiplied);
|
2021-05-02 10:37:45 +00:00
|
|
|
data.setDevicePixelRatio(scale);
|
2020-05-18 19:37:41 +00:00
|
|
|
memcpy(data.bits(), nativeCursorImage->pixels, data.sizeInBytes());
|
|
|
|
|
2021-05-02 10:37:45 +00:00
|
|
|
sprites.append(KXcursorSprite(data, hotspot / scale, delay));
|
2020-05-18 19:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
XcursorImagesDestroy(images);
|
wayland: Rework Xcursor theme loading
Xcursor loading code has hardcoded search paths, in order to take into
account distros installing app data in a different location,
libwayland-cursor sets the ICONDIR to the icon directory computed based
on the install prefix.
However, that won't work with gitlab CI because it relocates binaries. A
more robust way to find cursors would be to use QStandardPaths to find
all the icon directories on the system.
Another advantage of using own cursor loading code is that it allows us
to reuse cursor images that are symlinks. For example, with
breeze_cursors, almost half of the files in the cursors directory are
symlinks.
The main disadvantage of this approach is that we would have to keep the
search paths up to date. However, on the hand, there are not that many
of them, e.g. ~/.icons, ~/.local/share/icons, /usr/share/icons,
/usr/local/share/icons. The last three are implicitly handled by the
QStandardPaths.
2022-01-27 20:47:11 +00:00
|
|
|
return sprites;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KXcursorThemePrivate::loadCursors(const QString &packagePath, int size, qreal devicePixelRatio)
|
|
|
|
{
|
|
|
|
const QDir dir(packagePath);
|
|
|
|
QFileInfoList entries = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
|
|
|
|
std::partition(entries.begin(), entries.end(), [](const QFileInfo &fileInfo) {
|
|
|
|
return !fileInfo.isSymLink();
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const QFileInfo &entry : std::as_const(entries)) {
|
|
|
|
const QByteArray shape = QFile::encodeName(entry.fileName());
|
|
|
|
if (registry.contains(shape)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (entry.isSymLink()) {
|
|
|
|
const QFileInfo symLinkInfo(entry.symLinkTarget());
|
|
|
|
if (symLinkInfo.absolutePath() == entry.absolutePath()) {
|
|
|
|
const auto sprites = registry.value(QFile::encodeName(symLinkInfo.fileName()));
|
|
|
|
if (!sprites.isEmpty()) {
|
|
|
|
registry.insert(shape, sprites);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const QVector<KXcursorSprite> sprites = loadCursor(entry.absoluteFilePath(), size, devicePixelRatio);
|
|
|
|
if (!sprites.isEmpty()) {
|
|
|
|
registry.insert(shape, sprites);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static QStringList searchPaths()
|
|
|
|
{
|
|
|
|
static QStringList paths;
|
|
|
|
if (paths.isEmpty()) {
|
|
|
|
if (const QString env = qEnvironmentVariable("XCURSOR_PATH"); !env.isEmpty()) {
|
|
|
|
paths.append(env.split(':', Qt::SkipEmptyParts));
|
|
|
|
} else {
|
|
|
|
const QString home = QDir::homePath();
|
|
|
|
if (!home.isEmpty()) {
|
|
|
|
paths.append(home + QLatin1String("/.icons"));
|
|
|
|
}
|
|
|
|
const QStringList dataDirs = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
|
|
|
|
for (const QString &dataDir : dataDirs) {
|
|
|
|
paths.append(dataDir + QLatin1String("/icons"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KXcursorThemePrivate::load(const QString &themeName, int size, qreal devicePixelRatio)
|
|
|
|
{
|
|
|
|
const QStringList paths = searchPaths();
|
|
|
|
QStringList inherits;
|
|
|
|
|
|
|
|
for (const QString &path : paths) {
|
|
|
|
const QDir dir(path + QLatin1Char('/') + themeName);
|
|
|
|
if (!dir.exists()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
loadCursors(dir.filePath(QStringLiteral("cursors")), size, devicePixelRatio);
|
|
|
|
if (inherits.isEmpty()) {
|
|
|
|
const KConfig config(dir.filePath(QStringLiteral("index.theme")), KConfig::NoGlobals);
|
|
|
|
inherits << KConfigGroup(&config, "Icon Theme").readEntry("Inherits", QStringList());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const QString &inherit : inherits) {
|
|
|
|
load(inherit, size, devicePixelRatio);
|
|
|
|
}
|
2020-05-18 19:37:41 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 16:02:45 +00:00
|
|
|
KXcursorTheme::KXcursorTheme()
|
|
|
|
: d(new KXcursorThemePrivate)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
wayland: Rework Xcursor theme loading
Xcursor loading code has hardcoded search paths, in order to take into
account distros installing app data in a different location,
libwayland-cursor sets the ICONDIR to the icon directory computed based
on the install prefix.
However, that won't work with gitlab CI because it relocates binaries. A
more robust way to find cursors would be to use QStandardPaths to find
all the icon directories on the system.
Another advantage of using own cursor loading code is that it allows us
to reuse cursor images that are symlinks. For example, with
breeze_cursors, almost half of the files in the cursors directory are
symlinks.
The main disadvantage of this approach is that we would have to keep the
search paths up to date. However, on the hand, there are not that many
of them, e.g. ~/.icons, ~/.local/share/icons, /usr/share/icons,
/usr/local/share/icons. The last three are implicitly handled by the
QStandardPaths.
2022-01-27 20:47:11 +00:00
|
|
|
KXcursorTheme::KXcursorTheme(const QString &themeName, int size, qreal devicePixelRatio)
|
|
|
|
: d(new KXcursorThemePrivate)
|
2021-05-02 10:37:45 +00:00
|
|
|
{
|
wayland: Rework Xcursor theme loading
Xcursor loading code has hardcoded search paths, in order to take into
account distros installing app data in a different location,
libwayland-cursor sets the ICONDIR to the icon directory computed based
on the install prefix.
However, that won't work with gitlab CI because it relocates binaries. A
more robust way to find cursors would be to use QStandardPaths to find
all the icon directories on the system.
Another advantage of using own cursor loading code is that it allows us
to reuse cursor images that are symlinks. For example, with
breeze_cursors, almost half of the files in the cursors directory are
symlinks.
The main disadvantage of this approach is that we would have to keep the
search paths up to date. However, on the hand, there are not that many
of them, e.g. ~/.icons, ~/.local/share/icons, /usr/share/icons,
/usr/local/share/icons. The last three are implicitly handled by the
QStandardPaths.
2022-01-27 20:47:11 +00:00
|
|
|
d->load(themeName, size, devicePixelRatio);
|
2021-05-02 10:37:45 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 16:02:45 +00:00
|
|
|
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
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace KWin
|