2017-08-08 03:13:04 +02:00
|
|
|
/*
|
|
|
|
* Copyright © 2002 Keith Packard
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the
|
|
|
|
* next paragraph) shall be included in all copies or substantial
|
|
|
|
* portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2024-02-15 15:34:49 +01:00
|
|
|
#undef _POSIX_C_SOURCE
|
|
|
|
#define _DEFAULT_SOURCE // for d_type in struct dirent
|
2023-04-14 16:13:33 +02:00
|
|
|
#include <stdbool.h>
|
2017-08-08 03:13:04 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2023-04-14 16:13:33 +02:00
|
|
|
#include <dirent.h>
|
2022-11-25 18:07:09 +01:00
|
|
|
#include "config.h"
|
2018-02-12 21:29:23 +01:00
|
|
|
#include "xcursor/xcursor.h"
|
2017-08-08 03:13:04 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Cursor files start with a header. The header
|
|
|
|
* contains a magic number, a version number and a
|
|
|
|
* table of contents which has type and offset information
|
|
|
|
* for the remaining tables in the file.
|
|
|
|
*
|
|
|
|
* File minor versions increment for compatible changes
|
|
|
|
* File major versions increment for incompatible changes (never, we hope)
|
|
|
|
*
|
|
|
|
* Chunks of the same type are always upward compatible. Incompatible
|
|
|
|
* changes are made with new chunk types; the old data can remain under
|
|
|
|
* the old type. Upward compatible changes can add header data as the
|
|
|
|
* header lengths are specified in the file.
|
|
|
|
*
|
|
|
|
* File:
|
|
|
|
* FileHeader
|
|
|
|
* LISTofChunk
|
|
|
|
*
|
|
|
|
* FileHeader:
|
|
|
|
* CARD32 magic magic number
|
|
|
|
* CARD32 header bytes in file header
|
|
|
|
* CARD32 version file version
|
|
|
|
* CARD32 ntoc number of toc entries
|
|
|
|
* LISTofFileToc toc table of contents
|
|
|
|
*
|
|
|
|
* FileToc:
|
|
|
|
* CARD32 type entry type
|
|
|
|
* CARD32 subtype entry subtype (size for images)
|
|
|
|
* CARD32 position absolute file position
|
|
|
|
*/
|
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
#define XCURSOR_MAGIC 0x72756358 /* "Xcur" LSBFirst */
|
2017-08-08 03:13:04 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This version number is stored in cursor files; changes to the
|
|
|
|
* file format require updating this version number
|
|
|
|
*/
|
2023-04-14 16:13:33 +02:00
|
|
|
#define XCURSOR_FILE_MAJOR 1
|
|
|
|
#define XCURSOR_FILE_MINOR 0
|
|
|
|
#define XCURSOR_FILE_VERSION ((XCURSOR_FILE_MAJOR << 16) | (XCURSOR_FILE_MINOR))
|
|
|
|
#define XCURSOR_FILE_HEADER_LEN (4 * 4)
|
|
|
|
#define XCURSOR_FILE_TOC_LEN (3 * 4)
|
|
|
|
|
|
|
|
struct xcursor_file_toc {
|
|
|
|
uint32_t type; /* chunk type */
|
|
|
|
uint32_t subtype; /* subtype (size for images) */
|
|
|
|
uint32_t position; /* absolute position in file */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct xcursor_file_header {
|
|
|
|
uint32_t magic; /* magic number */
|
|
|
|
uint32_t header; /* byte length of header */
|
|
|
|
uint32_t version; /* file version number */
|
|
|
|
uint32_t ntoc; /* number of toc entries */
|
|
|
|
struct xcursor_file_toc *tocs; /* table of contents */
|
|
|
|
};
|
2017-08-08 03:13:04 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The rest of the file is a list of chunks, each tagged by type
|
|
|
|
* and version.
|
|
|
|
*
|
|
|
|
* Chunk:
|
|
|
|
* ChunkHeader
|
|
|
|
* <extra type-specific header fields>
|
|
|
|
* <type-specific data>
|
|
|
|
*
|
|
|
|
* ChunkHeader:
|
|
|
|
* CARD32 header bytes in chunk header + type header
|
|
|
|
* CARD32 type chunk type
|
|
|
|
* CARD32 subtype chunk subtype
|
|
|
|
* CARD32 version chunk type version
|
|
|
|
*/
|
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
#define XCURSOR_CHUNK_HEADER_LEN (4 * 4)
|
2017-08-08 03:13:04 +02:00
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
struct xcursor_chunk_header {
|
|
|
|
uint32_t header; /* bytes in chunk header */
|
|
|
|
uint32_t type; /* chunk type */
|
|
|
|
uint32_t subtype; /* chunk subtype (size for images) */
|
|
|
|
uint32_t version; /* version of this type */
|
|
|
|
};
|
2017-08-08 03:13:04 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Each cursor image occupies a separate image chunk.
|
|
|
|
* The length of the image header follows the chunk header
|
|
|
|
* so that future versions can extend the header without
|
|
|
|
* breaking older applications
|
|
|
|
*
|
|
|
|
* Image:
|
|
|
|
* ChunkHeader header chunk header
|
|
|
|
* CARD32 width actual width
|
|
|
|
* CARD32 height actual height
|
|
|
|
* CARD32 xhot hot spot x
|
|
|
|
* CARD32 yhot hot spot y
|
|
|
|
* CARD32 delay animation delay
|
|
|
|
* LISTofCARD32 pixels ARGB pixels
|
|
|
|
*/
|
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
#define XCURSOR_IMAGE_TYPE 0xfffd0002
|
|
|
|
#define XCURSOR_IMAGE_VERSION 1
|
|
|
|
#define XCURSOR_IMAGE_HEADER_LEN (XCURSOR_CHUNK_HEADER_LEN + (5*4))
|
|
|
|
#define XCURSOR_IMAGE_MAX_SIZE 0x7fff /* 32767x32767 max cursor size */
|
2017-08-08 03:13:04 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* From libXcursor/src/file.c
|
|
|
|
*/
|
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
static struct xcursor_image *
|
|
|
|
xcursor_image_create(int width, int height)
|
2017-08-08 03:13:04 +02:00
|
|
|
{
|
2023-04-14 16:13:33 +02:00
|
|
|
struct xcursor_image *image;
|
|
|
|
|
|
|
|
if (width < 0 || height < 0)
|
|
|
|
return NULL;
|
|
|
|
if (width > XCURSOR_IMAGE_MAX_SIZE || height > XCURSOR_IMAGE_MAX_SIZE)
|
|
|
|
return NULL;
|
|
|
|
|
2023-10-03 07:51:07 +02:00
|
|
|
image = malloc(sizeof(*image) +
|
2023-04-14 16:13:33 +02:00
|
|
|
width * height * sizeof(uint32_t));
|
|
|
|
if (!image)
|
|
|
|
return NULL;
|
|
|
|
image->version = XCURSOR_IMAGE_VERSION;
|
|
|
|
image->pixels = (uint32_t *) (image + 1);
|
|
|
|
image->size = width > height ? width : height;
|
|
|
|
image->width = width;
|
|
|
|
image->height = height;
|
|
|
|
image->delay = 0;
|
|
|
|
return image;
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-04-14 16:13:33 +02:00
|
|
|
xcursor_image_destroy(struct xcursor_image *image)
|
2017-08-08 03:13:04 +02:00
|
|
|
{
|
2023-04-14 16:13:33 +02:00
|
|
|
free(image);
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
static struct xcursor_images *
|
|
|
|
xcursor_images_create(int size)
|
2017-08-08 03:13:04 +02:00
|
|
|
{
|
2023-04-14 16:13:33 +02:00
|
|
|
struct xcursor_images *images;
|
|
|
|
|
2023-10-03 07:51:07 +02:00
|
|
|
images = malloc(sizeof(*images) +
|
2023-04-14 16:13:33 +02:00
|
|
|
size * sizeof(struct xcursor_image *));
|
|
|
|
if (!images)
|
|
|
|
return NULL;
|
|
|
|
images->nimage = 0;
|
|
|
|
images->images = (struct xcursor_image **) (images + 1);
|
|
|
|
images->name = NULL;
|
|
|
|
return images;
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-04-14 16:13:33 +02:00
|
|
|
xcursor_images_destroy(struct xcursor_images *images)
|
2017-08-08 03:13:04 +02:00
|
|
|
{
|
2023-04-14 16:13:33 +02:00
|
|
|
int n;
|
2017-08-08 03:13:04 +02:00
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
if (!images)
|
|
|
|
return;
|
2017-08-08 03:13:04 +02:00
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
for (n = 0; n < images->nimage; n++)
|
|
|
|
xcursor_image_destroy(images->images[n]);
|
|
|
|
free(images->name);
|
|
|
|
free(images);
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
static bool
|
|
|
|
xcursor_read_uint(FILE *file, uint32_t *u)
|
2017-08-08 03:13:04 +02:00
|
|
|
{
|
2023-04-14 16:13:33 +02:00
|
|
|
unsigned char bytes[4];
|
2017-08-08 03:13:04 +02:00
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
if (!file || !u)
|
|
|
|
return false;
|
2017-08-08 03:13:04 +02:00
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
if (fread(bytes, 1, 4, file) != 4)
|
|
|
|
return false;
|
2020-08-30 11:13:19 +02:00
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
*u = ((uint32_t)(bytes[0]) << 0) |
|
|
|
|
((uint32_t)(bytes[1]) << 8) |
|
|
|
|
((uint32_t)(bytes[2]) << 16) |
|
|
|
|
((uint32_t)(bytes[3]) << 24);
|
|
|
|
return true;
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-04-14 16:13:33 +02:00
|
|
|
xcursor_file_header_destroy(struct xcursor_file_header *file_header)
|
2017-08-08 03:13:04 +02:00
|
|
|
{
|
2023-04-14 16:13:33 +02:00
|
|
|
free(file_header);
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
static struct xcursor_file_header *
|
|
|
|
xcursor_file_header_create(uint32_t ntoc)
|
2017-08-08 03:13:04 +02:00
|
|
|
{
|
2023-04-14 16:13:33 +02:00
|
|
|
struct xcursor_file_header *file_header;
|
|
|
|
|
|
|
|
if (ntoc > 0x10000)
|
|
|
|
return NULL;
|
2023-10-03 07:51:07 +02:00
|
|
|
file_header = malloc(sizeof(*file_header) +
|
2023-04-14 16:13:33 +02:00
|
|
|
ntoc * sizeof(struct xcursor_file_toc));
|
|
|
|
if (!file_header)
|
|
|
|
return NULL;
|
|
|
|
file_header->magic = XCURSOR_MAGIC;
|
|
|
|
file_header->header = XCURSOR_FILE_HEADER_LEN;
|
|
|
|
file_header->version = XCURSOR_FILE_VERSION;
|
|
|
|
file_header->ntoc = ntoc;
|
|
|
|
file_header->tocs = (struct xcursor_file_toc *) (file_header + 1);
|
|
|
|
return file_header;
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
static struct xcursor_file_header *
|
|
|
|
xcursor_read_file_header(FILE *file)
|
2017-08-08 03:13:04 +02:00
|
|
|
{
|
2023-04-14 16:13:33 +02:00
|
|
|
struct xcursor_file_header head, *file_header;
|
|
|
|
uint32_t skip;
|
|
|
|
unsigned int n;
|
|
|
|
|
|
|
|
if (!file)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!xcursor_read_uint(file, &head.magic))
|
|
|
|
return NULL;
|
|
|
|
if (head.magic != XCURSOR_MAGIC)
|
|
|
|
return NULL;
|
|
|
|
if (!xcursor_read_uint(file, &head.header))
|
|
|
|
return NULL;
|
|
|
|
if (!xcursor_read_uint(file, &head.version))
|
|
|
|
return NULL;
|
|
|
|
if (!xcursor_read_uint(file, &head.ntoc))
|
|
|
|
return NULL;
|
|
|
|
skip = head.header - XCURSOR_FILE_HEADER_LEN;
|
|
|
|
if (skip)
|
|
|
|
if (fseek(file, skip, SEEK_CUR) == EOF)
|
|
|
|
return NULL;
|
|
|
|
file_header = xcursor_file_header_create(head.ntoc);
|
|
|
|
if (!file_header)
|
|
|
|
return NULL;
|
|
|
|
file_header->magic = head.magic;
|
|
|
|
file_header->header = head.header;
|
|
|
|
file_header->version = head.version;
|
|
|
|
file_header->ntoc = head.ntoc;
|
|
|
|
for (n = 0; n < file_header->ntoc; n++) {
|
|
|
|
if (!xcursor_read_uint(file, &file_header->tocs[n].type))
|
|
|
|
break;
|
|
|
|
if (!xcursor_read_uint(file, &file_header->tocs[n].subtype))
|
|
|
|
break;
|
|
|
|
if (!xcursor_read_uint(file, &file_header->tocs[n].position))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (n != file_header->ntoc) {
|
|
|
|
xcursor_file_header_destroy(file_header);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return file_header;
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
static bool
|
|
|
|
xcursor_seek_to_toc(FILE *file,
|
|
|
|
struct xcursor_file_header *file_header,
|
|
|
|
int toc)
|
2017-08-08 03:13:04 +02:00
|
|
|
{
|
2023-04-14 16:13:33 +02:00
|
|
|
if (!file || !file_header ||
|
|
|
|
fseek(file, file_header->tocs[toc].position, SEEK_SET) == EOF)
|
|
|
|
return false;
|
|
|
|
return true;
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
static bool
|
|
|
|
xcursor_file_read_chunk_header(FILE *file,
|
|
|
|
struct xcursor_file_header *file_header,
|
|
|
|
int toc,
|
|
|
|
struct xcursor_chunk_header *chunk_header)
|
2017-08-08 03:13:04 +02:00
|
|
|
{
|
2023-04-14 16:13:33 +02:00
|
|
|
if (!file || !file_header || !chunk_header)
|
|
|
|
return false;
|
|
|
|
if (!xcursor_seek_to_toc(file, file_header, toc))
|
|
|
|
return false;
|
|
|
|
if (!xcursor_read_uint(file, &chunk_header->header))
|
|
|
|
return false;
|
|
|
|
if (!xcursor_read_uint(file, &chunk_header->type))
|
|
|
|
return false;
|
|
|
|
if (!xcursor_read_uint(file, &chunk_header->subtype))
|
|
|
|
return false;
|
|
|
|
if (!xcursor_read_uint(file, &chunk_header->version))
|
|
|
|
return false;
|
|
|
|
/* sanity check */
|
|
|
|
if (chunk_header->type != file_header->tocs[toc].type ||
|
|
|
|
chunk_header->subtype != file_header->tocs[toc].subtype)
|
|
|
|
return false;
|
|
|
|
return true;
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
static uint32_t
|
|
|
|
dist(uint32_t a, uint32_t b)
|
2017-08-08 03:13:04 +02:00
|
|
|
{
|
2023-04-14 16:13:33 +02:00
|
|
|
return a > b ? a - b : b - a;
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
static uint32_t
|
|
|
|
xcursor_file_best_size(struct xcursor_file_header *file_header,
|
|
|
|
uint32_t size, int *nsizesp)
|
2017-08-08 03:13:04 +02:00
|
|
|
{
|
2023-04-14 16:13:33 +02:00
|
|
|
unsigned int n;
|
|
|
|
int nsizes = 0;
|
|
|
|
uint32_t best_size = 0;
|
|
|
|
uint32_t this_size;
|
2017-08-08 03:13:04 +02:00
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
if (!file_header || !nsizesp)
|
|
|
|
return 0;
|
2017-08-08 03:13:04 +02:00
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
for (n = 0; n < file_header->ntoc; n++) {
|
|
|
|
if (file_header->tocs[n].type != XCURSOR_IMAGE_TYPE)
|
|
|
|
continue;
|
|
|
|
this_size = file_header->tocs[n].subtype;
|
|
|
|
if (!best_size || dist(this_size, size) < dist(best_size, size)) {
|
|
|
|
best_size = this_size;
|
|
|
|
nsizes = 1;
|
|
|
|
} else if (this_size == best_size) {
|
|
|
|
nsizes++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*nsizesp = nsizes;
|
|
|
|
return best_size;
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2023-04-14 16:13:33 +02:00
|
|
|
xcursor_find_image_toc(struct xcursor_file_header *file_header,
|
|
|
|
uint32_t size, int count)
|
2017-08-08 03:13:04 +02:00
|
|
|
{
|
2023-04-14 16:13:33 +02:00
|
|
|
unsigned int toc;
|
|
|
|
uint32_t this_size;
|
2017-08-08 03:13:04 +02:00
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
if (!file_header)
|
|
|
|
return 0;
|
2017-08-08 03:13:04 +02:00
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
for (toc = 0; toc < file_header->ntoc; toc++) {
|
|
|
|
if (file_header->tocs[toc].type != XCURSOR_IMAGE_TYPE)
|
|
|
|
continue;
|
|
|
|
this_size = file_header->tocs[toc].subtype;
|
|
|
|
if (this_size != size)
|
|
|
|
continue;
|
|
|
|
if (!count)
|
|
|
|
break;
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
if (toc == file_header->ntoc)
|
|
|
|
return -1;
|
|
|
|
return toc;
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
static struct xcursor_image *
|
|
|
|
xcursor_read_image(FILE *file,
|
|
|
|
struct xcursor_file_header *file_header,
|
|
|
|
int toc)
|
2017-08-08 03:13:04 +02:00
|
|
|
{
|
2023-04-14 16:13:33 +02:00
|
|
|
struct xcursor_chunk_header chunk_header;
|
|
|
|
struct xcursor_image head;
|
|
|
|
struct xcursor_image *image;
|
|
|
|
int n;
|
|
|
|
uint32_t *p;
|
|
|
|
|
|
|
|
if (!file || !file_header)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!xcursor_file_read_chunk_header(file, file_header, toc, &chunk_header))
|
|
|
|
return NULL;
|
|
|
|
if (!xcursor_read_uint(file, &head.width))
|
|
|
|
return NULL;
|
|
|
|
if (!xcursor_read_uint(file, &head.height))
|
|
|
|
return NULL;
|
|
|
|
if (!xcursor_read_uint(file, &head.xhot))
|
|
|
|
return NULL;
|
|
|
|
if (!xcursor_read_uint(file, &head.yhot))
|
|
|
|
return NULL;
|
|
|
|
if (!xcursor_read_uint(file, &head.delay))
|
|
|
|
return NULL;
|
|
|
|
/* sanity check data */
|
|
|
|
if (head.width > XCURSOR_IMAGE_MAX_SIZE ||
|
|
|
|
head.height > XCURSOR_IMAGE_MAX_SIZE)
|
|
|
|
return NULL;
|
|
|
|
if (head.width == 0 || head.height == 0)
|
|
|
|
return NULL;
|
|
|
|
if (head.xhot > head.width || head.yhot > head.height)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Create the image and initialize it */
|
|
|
|
image = xcursor_image_create(head.width, head.height);
|
|
|
|
if (image == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (chunk_header.version < image->version)
|
|
|
|
image->version = chunk_header.version;
|
|
|
|
image->size = chunk_header.subtype;
|
|
|
|
image->xhot = head.xhot;
|
|
|
|
image->yhot = head.yhot;
|
|
|
|
image->delay = head.delay;
|
|
|
|
n = image->width * image->height;
|
|
|
|
p = image->pixels;
|
|
|
|
while (n--) {
|
|
|
|
if (!xcursor_read_uint(file, p)) {
|
|
|
|
xcursor_image_destroy(image);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return image;
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
static struct xcursor_images *
|
|
|
|
xcursor_xc_file_load_images(FILE *file, int size)
|
2017-08-08 03:13:04 +02:00
|
|
|
{
|
2023-04-14 16:13:33 +02:00
|
|
|
struct xcursor_file_header *file_header;
|
|
|
|
uint32_t best_size;
|
|
|
|
int nsize;
|
|
|
|
struct xcursor_images *images;
|
|
|
|
int n;
|
|
|
|
int toc;
|
|
|
|
|
|
|
|
if (!file || size < 0)
|
|
|
|
return NULL;
|
|
|
|
file_header = xcursor_read_file_header(file);
|
|
|
|
if (!file_header)
|
|
|
|
return NULL;
|
|
|
|
best_size = xcursor_file_best_size(file_header, (uint32_t) size, &nsize);
|
|
|
|
if (!best_size) {
|
|
|
|
xcursor_file_header_destroy(file_header);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
images = xcursor_images_create(nsize);
|
|
|
|
if (!images) {
|
|
|
|
xcursor_file_header_destroy(file_header);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (n = 0; n < nsize; n++) {
|
|
|
|
toc = xcursor_find_image_toc(file_header, best_size, n);
|
|
|
|
if (toc < 0)
|
|
|
|
break;
|
|
|
|
images->images[images->nimage] = xcursor_read_image(file, file_header,
|
|
|
|
toc);
|
|
|
|
if (!images->images[images->nimage])
|
|
|
|
break;
|
|
|
|
images->nimage++;
|
|
|
|
}
|
|
|
|
xcursor_file_header_destroy(file_header);
|
|
|
|
if (images->nimage != nsize) {
|
|
|
|
xcursor_images_destroy(images);
|
|
|
|
images = NULL;
|
|
|
|
}
|
|
|
|
return images;
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* From libXcursor/src/library.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ICONDIR
|
|
|
|
#define ICONDIR "/usr/X11R6/lib/X11/icons"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef XCURSORPATH
|
2023-04-14 16:13:33 +02:00
|
|
|
#define XCURSORPATH "~/.icons:/usr/share/icons:/usr/share/pixmaps:~/.cursors:/usr/share/cursors/xorg-x11:"ICONDIR
|
2017-08-08 03:13:04 +02:00
|
|
|
#endif
|
|
|
|
|
2022-03-05 16:33:28 +01:00
|
|
|
#define XDG_DATA_HOME_FALLBACK "~/.local/share"
|
|
|
|
#define CURSORDIR "/icons"
|
|
|
|
|
|
|
|
/** Get search path for cursor themes
|
|
|
|
*
|
|
|
|
* This function builds the list of directories to look for cursor
|
|
|
|
* themes in. The format is PATH-like: directories are separated by
|
|
|
|
* colons.
|
|
|
|
*
|
|
|
|
* The memory block returned by this function is allocated on the heap
|
|
|
|
* and must be freed by the caller.
|
|
|
|
*/
|
|
|
|
static char *
|
2023-04-14 16:13:33 +02:00
|
|
|
xcursor_library_path(void)
|
2017-08-08 03:13:04 +02:00
|
|
|
{
|
2023-04-14 16:13:33 +02:00
|
|
|
const char *env_var, *suffix;
|
|
|
|
char *path;
|
|
|
|
size_t path_size;
|
2017-08-08 03:13:04 +02:00
|
|
|
|
2022-03-05 16:33:28 +01:00
|
|
|
env_var = getenv("XCURSOR_PATH");
|
2023-04-14 16:13:33 +02:00
|
|
|
if (env_var)
|
|
|
|
return strdup(env_var);
|
|
|
|
|
|
|
|
env_var = getenv("XDG_DATA_HOME");
|
|
|
|
if (!env_var || env_var[0] != '/')
|
|
|
|
env_var = XDG_DATA_HOME_FALLBACK;
|
|
|
|
|
|
|
|
suffix = CURSORDIR ":" XCURSORPATH;
|
|
|
|
path_size = strlen(env_var) + strlen(suffix) + 1;
|
|
|
|
path = malloc(path_size);
|
|
|
|
if (!path)
|
|
|
|
return NULL;
|
|
|
|
snprintf(path, path_size, "%s%s", env_var, suffix);
|
2022-03-05 16:33:28 +01:00
|
|
|
return path;
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2023-04-14 16:13:33 +02:00
|
|
|
xcursor_build_theme_dir(const char *dir, const char *theme)
|
2017-08-08 03:13:04 +02:00
|
|
|
{
|
2023-04-14 16:13:33 +02:00
|
|
|
const char *colon;
|
|
|
|
const char *tcolon;
|
|
|
|
char *full;
|
|
|
|
const char *home, *homesep;
|
2023-04-22 15:30:59 +02:00
|
|
|
size_t dirlen;
|
|
|
|
size_t homelen;
|
|
|
|
size_t themelen;
|
2023-04-14 16:13:33 +02:00
|
|
|
size_t full_size;
|
|
|
|
|
|
|
|
if (!dir || !theme)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
colon = strchr(dir, ':');
|
|
|
|
if (!colon)
|
|
|
|
colon = dir + strlen(dir);
|
|
|
|
|
|
|
|
dirlen = colon - dir;
|
|
|
|
|
|
|
|
tcolon = strchr(theme, ':');
|
|
|
|
if (!tcolon)
|
|
|
|
tcolon = theme + strlen(theme);
|
|
|
|
|
|
|
|
themelen = tcolon - theme;
|
|
|
|
|
|
|
|
home = "";
|
|
|
|
homelen = 0;
|
|
|
|
homesep = "";
|
|
|
|
if (*dir == '~') {
|
|
|
|
home = getenv("HOME");
|
|
|
|
if (!home)
|
|
|
|
return NULL;
|
|
|
|
homelen = strlen(home);
|
|
|
|
homesep = "/";
|
|
|
|
dir++;
|
|
|
|
dirlen--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* add space for any needed directory separators, one per component,
|
|
|
|
* and one for the trailing null
|
|
|
|
*/
|
|
|
|
full_size = 1 + homelen + 1 + dirlen + 1 + themelen + 1;
|
|
|
|
full = malloc(full_size);
|
|
|
|
if (!full)
|
|
|
|
return NULL;
|
|
|
|
snprintf(full, full_size, "%s%s%.*s/%.*s", home, homesep,
|
2023-04-22 15:30:59 +02:00
|
|
|
(int)dirlen, dir, (int)themelen, theme);
|
2023-04-14 16:13:33 +02:00
|
|
|
return full;
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2023-04-14 16:13:33 +02:00
|
|
|
xcursor_build_fullname(const char *dir, const char *subdir, const char *file)
|
2017-08-08 03:13:04 +02:00
|
|
|
{
|
2023-04-14 16:13:33 +02:00
|
|
|
char *full;
|
|
|
|
size_t full_size;
|
|
|
|
|
|
|
|
if (!dir || !subdir || !file)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
full_size = strlen(dir) + 1 + strlen(subdir) + 1 + strlen(file) + 1;
|
|
|
|
full = malloc(full_size);
|
|
|
|
if (!full)
|
|
|
|
return NULL;
|
|
|
|
snprintf(full, full_size, "%s/%s/%s", dir, subdir, file);
|
|
|
|
return full;
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
2023-04-14 16:13:33 +02:00
|
|
|
xcursor_next_path(const char *path)
|
2017-08-08 03:13:04 +02:00
|
|
|
{
|
2023-04-14 16:13:33 +02:00
|
|
|
char *colon = strchr(path, ':');
|
2017-08-08 03:13:04 +02:00
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
if (!colon)
|
|
|
|
return NULL;
|
|
|
|
return colon + 1;
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
static bool
|
|
|
|
xcursor_white(char c)
|
|
|
|
{
|
|
|
|
return c == ' ' || c == '\t' || c == '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
xcursor_sep(char c)
|
|
|
|
{
|
|
|
|
return c == ';' || c == ',';
|
|
|
|
}
|
2017-08-08 03:13:04 +02:00
|
|
|
|
|
|
|
static char *
|
2023-04-14 16:13:33 +02:00
|
|
|
xcursor_theme_inherits(const char *full)
|
2017-08-08 03:13:04 +02:00
|
|
|
{
|
2023-04-14 16:13:33 +02:00
|
|
|
char *line = NULL;
|
|
|
|
size_t line_size = 0;
|
|
|
|
char *result = NULL;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if (!full)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
f = fopen(full, "r");
|
|
|
|
if (!f)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (getline(&line, &line_size, f) >= 0) {
|
|
|
|
const char *l;
|
|
|
|
char *r;
|
|
|
|
|
|
|
|
if (strncmp(line, "Inherits", 8))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
l = line + 8;
|
|
|
|
while (*l == ' ')
|
|
|
|
l++;
|
|
|
|
if (*l != '=')
|
|
|
|
continue;
|
2017-08-08 03:13:04 +02:00
|
|
|
l++;
|
2023-04-14 16:13:33 +02:00
|
|
|
while (*l == ' ')
|
|
|
|
l++;
|
|
|
|
result = malloc(strlen(l) + 1);
|
|
|
|
if (!result)
|
|
|
|
break;
|
|
|
|
|
|
|
|
r = result;
|
|
|
|
while (*l) {
|
|
|
|
while (xcursor_sep(*l) || xcursor_white(*l))
|
|
|
|
l++;
|
2017-08-08 03:13:04 +02:00
|
|
|
if (!*l)
|
2023-04-14 16:13:33 +02:00
|
|
|
break;
|
2017-08-08 03:13:04 +02:00
|
|
|
if (r != result)
|
2023-04-14 16:13:33 +02:00
|
|
|
*r++ = ':';
|
|
|
|
while (*l && !xcursor_white(*l) && !xcursor_sep(*l))
|
|
|
|
*r++ = *l++;
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
2023-04-14 16:13:33 +02:00
|
|
|
*r++ = '\0';
|
|
|
|
|
2017-08-08 03:13:04 +02:00
|
|
|
break;
|
|
|
|
}
|
2023-04-14 16:13:33 +02:00
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
free(line);
|
|
|
|
|
|
|
|
return result;
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
load_all_cursors_from_dir(const char *path, int size,
|
2023-04-14 16:13:33 +02:00
|
|
|
void (*load_callback)(struct xcursor_images *, void *),
|
2017-08-08 03:13:04 +02:00
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
DIR *dir = opendir(path);
|
|
|
|
struct dirent *ent;
|
|
|
|
char *full;
|
2023-04-14 16:13:33 +02:00
|
|
|
struct xcursor_images *images;
|
2017-08-08 03:13:04 +02:00
|
|
|
|
|
|
|
if (!dir)
|
|
|
|
return;
|
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
for (ent = readdir(dir); ent; ent = readdir(dir)) {
|
2018-11-06 14:40:41 +01:00
|
|
|
#ifdef _DIRENT_HAVE_D_TYPE
|
2017-08-08 03:13:04 +02:00
|
|
|
if (ent->d_type != DT_UNKNOWN &&
|
2023-04-14 16:13:33 +02:00
|
|
|
ent->d_type != DT_REG &&
|
|
|
|
ent->d_type != DT_LNK)
|
2017-08-08 03:13:04 +02:00
|
|
|
continue;
|
2018-11-06 14:40:41 +01:00
|
|
|
#endif
|
2017-08-08 03:13:04 +02:00
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
full = xcursor_build_fullname(path, "", ent->d_name);
|
2017-08-08 03:13:04 +02:00
|
|
|
if (!full)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
f = fopen(full, "r");
|
|
|
|
if (!f) {
|
|
|
|
free(full);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
images = xcursor_xc_file_load_images(f, size);
|
2017-08-08 03:13:04 +02:00
|
|
|
|
|
|
|
if (images) {
|
2023-04-14 16:13:33 +02:00
|
|
|
images->name = strdup(ent->d_name);
|
2017-08-08 03:13:04 +02:00
|
|
|
load_callback(images, user_data);
|
|
|
|
}
|
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
fclose(f);
|
2017-08-08 03:13:04 +02:00
|
|
|
free(full);
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Load all the cursor of a theme
|
|
|
|
*
|
|
|
|
* This function loads all the cursor images of a given theme and its
|
2023-04-14 16:13:33 +02:00
|
|
|
* inherited themes. Each cursor is loaded into an struct xcursor_images object
|
2017-08-08 03:13:04 +02:00
|
|
|
* which is passed to the caller's load callback. If a cursor appears
|
|
|
|
* more than once across all the inherited themes, the load callback
|
2023-04-14 16:13:33 +02:00
|
|
|
* will be called multiple times, with possibly different struct xcursor_images
|
2017-08-08 03:13:04 +02:00
|
|
|
* object which have the same name. The user is expected to destroy the
|
2023-04-14 16:13:33 +02:00
|
|
|
* struct xcursor_images objects passed to the callback with
|
|
|
|
* xcursor_images_destroy().
|
2017-08-08 03:13:04 +02:00
|
|
|
*
|
|
|
|
* \param theme The name of theme that should be loaded
|
|
|
|
* \param size The desired size of the cursor images
|
|
|
|
* \param load_callback A callback function that will be called
|
2023-04-14 16:13:33 +02:00
|
|
|
* for each cursor loaded. The first parameter is the struct xcursor_images
|
2017-08-08 03:13:04 +02:00
|
|
|
* object representing the loaded cursor and the second is a pointer
|
|
|
|
* to data provided by the user.
|
|
|
|
* \param user_data The data that should be passed to the load callback
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
xcursor_load_theme(const char *theme, int size,
|
2023-04-14 16:13:33 +02:00
|
|
|
void (*load_callback)(struct xcursor_images *, void *),
|
|
|
|
void *user_data)
|
2017-08-08 03:13:04 +02:00
|
|
|
{
|
|
|
|
char *full, *dir;
|
|
|
|
char *inherits = NULL;
|
|
|
|
const char *path, *i;
|
2023-04-14 16:13:33 +02:00
|
|
|
char *xcursor_path;
|
2017-08-08 03:13:04 +02:00
|
|
|
|
|
|
|
if (!theme)
|
|
|
|
theme = "default";
|
2023-04-14 16:13:33 +02:00
|
|
|
|
|
|
|
xcursor_path = xcursor_library_path();
|
|
|
|
for (path = xcursor_path;
|
|
|
|
path;
|
|
|
|
path = xcursor_next_path(path)) {
|
|
|
|
dir = xcursor_build_theme_dir(path, theme);
|
2017-08-08 03:13:04 +02:00
|
|
|
if (!dir)
|
|
|
|
continue;
|
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
full = xcursor_build_fullname(dir, "cursors", "");
|
2023-11-19 11:45:05 +01:00
|
|
|
if (full) {
|
|
|
|
load_all_cursors_from_dir(full, size, load_callback,
|
|
|
|
user_data);
|
|
|
|
free(full);
|
|
|
|
}
|
2017-08-08 03:13:04 +02:00
|
|
|
|
|
|
|
if (!inherits) {
|
2023-04-14 16:13:33 +02:00
|
|
|
full = xcursor_build_fullname(dir, "", "index.theme");
|
|
|
|
inherits = xcursor_theme_inherits(full);
|
|
|
|
free(full);
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
free(dir);
|
|
|
|
}
|
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
for (i = inherits; i; i = xcursor_next_path(i))
|
2017-08-08 03:13:04 +02:00
|
|
|
xcursor_load_theme(i, size, load_callback, user_data);
|
|
|
|
|
2023-04-14 16:13:33 +02:00
|
|
|
free(inherits);
|
2022-03-05 16:33:28 +01:00
|
|
|
free(xcursor_path);
|
2017-08-08 03:13:04 +02:00
|
|
|
}
|