1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/*
* Copyright (C) 2023 Igalia S.L.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "WPECursorTheme.h"
#include <stdio.h>
#include <wtf/FileSystem.h>
#include <wtf/TZoneMallocInlines.h>
namespace WPE {
static GUniquePtr<char> cursorsPath(const char* basePath, Vector<GUniquePtr<char>>& inherited)
{
auto inheritedThemes = [&]() -> GUniquePtr<char*> {
GUniquePtr<char> index(g_build_filename(basePath, "index.theme", nullptr));
if (!g_file_test(index.get(), G_FILE_TEST_EXISTS))
return nullptr;
GUniquePtr<GKeyFile> keyFile(g_key_file_new());
if (!g_key_file_load_from_file(keyFile.get(), index.get(), G_KEY_FILE_NONE, nullptr))
return nullptr;
return GUniquePtr<char*>(g_key_file_get_string_list(keyFile.get(), "Icon Theme", "Inherits", nullptr, nullptr));
};
String pathOfIndex = FileSystem::pathByAppendingComponent(String::fromUTF8(basePath), "index.theme"_s);
String canonicalPathOfIndex = FileSystem::realPath(pathOfIndex);
GUniquePtr<char> canonicalDirectoryOfIndex(g_path_get_dirname(canonicalPathOfIndex.utf8().data()));
const char* actualBasePath = g_file_test(canonicalDirectoryOfIndex.get(), G_FILE_TEST_IS_DIR) ? canonicalDirectoryOfIndex.get() : basePath;
GUniquePtr<char> baseCursorsPath(g_build_filename(actualBasePath, "cursors", nullptr));
if (auto inherits = inheritedThemes()) {
for (unsigned i = 0; inherits.get()[i]; ++i) {
GUniquePtr<char> parentPath(g_path_get_dirname(actualBasePath));
GUniquePtr<char> inheritedBasePath(g_build_filename(parentPath.get(), inherits.get()[i], nullptr));
auto path = cursorsPath(inheritedBasePath.get(), inherited);
auto exists = path && inherited.containsIf([&](const auto& item) {
return !g_strcmp0(item.get(), path.get());
});
if (path && !exists)
inherited.append(WTFMove(path));
}
}
if (g_file_test(baseCursorsPath.get(), G_FILE_TEST_IS_DIR))
return baseCursorsPath;
return nullptr;
}
WTF_MAKE_TZONE_ALLOCATED_IMPL(CursorTheme);
std::unique_ptr<CursorTheme> CursorTheme::create(const char* name, uint32_t size)
{
auto findThemePath = [](const char* name) -> GUniquePtr<char> {
GUniquePtr<char> path(g_build_filename(g_get_user_data_dir(), "icons", name, nullptr));
if (g_file_test(path.get(), G_FILE_TEST_IS_DIR))
return path;
path.reset(g_build_filename(g_get_home_dir(), ".icons", name, nullptr));
if (g_file_test(path.get(), G_FILE_TEST_IS_DIR))
return path;
auto* dataDirs = g_get_system_data_dirs();
for (unsigned i = 0; dataDirs[i]; ++i) {
path.reset(g_build_filename(dataDirs[i], "icons", name, nullptr));
if (g_file_test(path.get(), G_FILE_TEST_IS_DIR))
return path;
}
return nullptr;
};
if (auto basePath = findThemePath(name)) {
Vector<GUniquePtr<char>> inheritedThemes;
auto path = cursorsPath(basePath.get(), inheritedThemes);
if (path || !inheritedThemes.isEmpty()) {
if (!path) {
// If there's no cursors path, use the first inherited theme.
path = WTFMove(inheritedThemes[0]);
inheritedThemes.remove(0);
}
return makeUnique<CursorTheme>(WTFMove(path), size, WTFMove(inheritedThemes));
}
g_warning("Could not find any cursors (search started from '%s')", basePath.get());
} else
g_warning("Could not find any base paths for cursors search");
return nullptr;
}
std::unique_ptr<CursorTheme> CursorTheme::create()
{
return create("default", 24);
}
CursorTheme::CursorTheme(GUniquePtr<char>&& path, uint32_t size, Vector<GUniquePtr<char>>&& inherited)
: m_path(WTFMove(path))
, m_size(size)
, m_inherited(WTFMove(inherited))
{
}
#define XCURSOR_MAGIC 0x72756358
#define XCURSOR_IMAGE_TYPE 0xfffd0002
#define XCURSOR_IMAGE_MAX_SIZE 0x7fff // 32767x32767 max cursor size.
struct XcusorContent {
uint32_t type { 0 };
uint32_t subtype { 0 };
uint32_t position { 0 };
};
struct XcursorHeader {
uint32_t magic { 0 };
uint32_t length { 0 };
uint32_t version { 0 };
Vector<XcusorContent> contents;
uint32_t imageSize { 0 };
uint32_t imageCount { 0 };
};
struct XcursorChunkHeader {
uint32_t length { 0 };
uint32_t type { 0 };
uint32_t subtype { 0 };
uint32_t version { 0 };
};
static bool readUint32(FILE* file, uint32_t* value)
{
unsigned char bytes[4];
if (fread(&bytes, 1, 4, file) != 4)
return false;
*value = ((bytes[0] << 0) | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24));
return true;
}
#define distance(x, y) (x > y ? x - y : y - x)
static std::optional<XcursorHeader> readHeader(FILE* file, uint32_t size)
{
XcursorHeader header;
if (!readUint32(file, &header.magic) || header.magic != XCURSOR_MAGIC)
return std::nullopt;
if (!readUint32(file, &header.length))
return std::nullopt;
if (!readUint32(file, &header.version))
return std::nullopt;
uint32_t tocLength;
if (!readUint32(file, &tocLength) || tocLength > std::numeric_limits<uint16_t>::max())
return std::nullopt;
if (auto bytesToSkip = header.length - (4 * 4)) {
if (fseek(file, bytesToSkip, SEEK_CUR) == EOF)
return std::nullopt;
}
header.contents.grow(tocLength);
for (auto& entry : header.contents) {
if (!readUint32(file, &entry.type))
return std::nullopt;
if (!readUint32(file, &entry.subtype))
return std::nullopt;
if (!readUint32(file, &entry.position))
return std::nullopt;
if (entry.type != XCURSOR_IMAGE_TYPE)
continue;
if (!header.imageSize || distance(entry.subtype, size) < distance(header.imageSize, size)) {
header.imageSize = entry.subtype;
header.imageCount = 1;
} else if (entry.subtype == header.imageSize)
header.imageCount++;
}
if (!header.imageSize || !header.imageCount)
return std::nullopt;
return header;
}
static std::optional<XcursorChunkHeader> readChunkHeader(FILE* file, const XcusorContent& entry)
{
if (fseek(file, entry.position, SEEK_SET) == EOF)
return std::nullopt;
XcursorChunkHeader chunkHeader;
if (!readUint32(file, &chunkHeader.length))
return std::nullopt;
if (!readUint32(file, &chunkHeader.type) || chunkHeader.type != entry.type)
return std::nullopt;
if (!readUint32(file, &chunkHeader.subtype) || chunkHeader.subtype != entry.subtype)
return std::nullopt;
if (!readUint32(file, &chunkHeader.version))
return std::nullopt;
return chunkHeader;
}
static std::optional<CursorTheme::CursorImage> readImage(FILE* file, const XcusorContent& entry)
{
auto chunkHeader = readChunkHeader(file, entry);
if (!chunkHeader)
return std::nullopt;
CursorTheme::CursorImage image;
if (!readUint32(file, &image.width) || !image.width || image.width > XCURSOR_IMAGE_MAX_SIZE)
return std::nullopt;
if (!readUint32(file, &image.height) || !image.height || image.height > XCURSOR_IMAGE_MAX_SIZE)
return std::nullopt;
if (!readUint32(file, &image.hotspotX) || image.hotspotX > image.width)
return std::nullopt;
if (!readUint32(file, &image.hotspotY) || image.hotspotY > image.height)
return std::nullopt;
if (!readUint32(file, &image.delay))
return std::nullopt;
auto imageSize = image.width * image.height;
image.pixels.grow(imageSize);
auto* pixels = image.pixels.data();
while (imageSize--) {
if (!readUint32(file, pixels))
return std::nullopt;
pixels++;
}
return image;
}
Vector<CursorTheme::CursorImage> CursorTheme::loadCursor(const char* name, uint32_t size, std::optional<uint32_t> maxImages)
{
GUniquePtr<char> path(g_build_filename(m_path.get(), name, nullptr));
if (!g_file_test(path.get(), G_FILE_TEST_EXISTS)) {
path = nullptr;
for (auto& theme : m_inherited) {
path.reset(g_build_filename(theme.get(), name, nullptr));
if (g_file_test(path.get(), G_FILE_TEST_EXISTS))
break;
path = nullptr;
}
}
if (!path)
return { };
FILE* file = fopen(path.get(), "r");
if (!file)
return { };
auto header = readHeader(file, size);
if (!header) {
fclose(file);
return { };
}
header->imageCount = maxImages.value_or(header->imageCount);
Vector<CursorImage> cursorImages;
for (auto& entry : header->contents) {
if (entry.type != XCURSOR_IMAGE_TYPE)
continue;
if (entry.subtype != header->imageSize)
continue;
auto image = readImage(file, entry);
if (!image)
break;
cursorImages.append(WTFMove(*image));
if (cursorImages.size() == header->imageCount)
break;
}
fclose(file);
return cursorImages;
}
} // namespace WPE
|