3rdparty: Reformat xcursor.{h,c}

This commit is contained in:
Vlad Zahorodnii 2024-05-21 15:29:39 +03:00
parent 586a775d5b
commit e678ebef29
2 changed files with 248 additions and 264 deletions

462
src/3rdparty/xcursor.c vendored
View file

@ -25,17 +25,17 @@
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#include "xcursor.h" #include "xcursor.h"
#include <dirent.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <dirent.h>
/* /*
* From libXcursor/include/X11/extensions/Xcursor.h * From libXcursor/include/X11/extensions/Xcursor.h
*/ */
#define XcursorTrue 1 #define XcursorTrue 1
#define XcursorFalse 0 #define XcursorFalse 0
/* /*
* Cursor files start with a header. The header * Cursor files start with a header. The header
@ -68,7 +68,7 @@
* CARD32 position absolute file position * CARD32 position absolute file position
*/ */
#define XCURSOR_MAGIC 0x72756358 /* "Xcur" LSBFirst */ #define XCURSOR_MAGIC 0x72756358 /* "Xcur" LSBFirst */
/* /*
* Current Xcursor version number. Will be substituted by configure * Current Xcursor version number. Will be substituted by configure
@ -78,32 +78,32 @@
#define XCURSOR_LIB_MAJOR 1 #define XCURSOR_LIB_MAJOR 1
#define XCURSOR_LIB_MINOR 1 #define XCURSOR_LIB_MINOR 1
#define XCURSOR_LIB_REVISION 13 #define XCURSOR_LIB_REVISION 13
#define XCURSOR_LIB_VERSION ((XCURSOR_LIB_MAJOR * 10000) + \ #define XCURSOR_LIB_VERSION ((XCURSOR_LIB_MAJOR * 10000) + (XCURSOR_LIB_MINOR * 100) + (XCURSOR_LIB_REVISION))
(XCURSOR_LIB_MINOR * 100) + \
(XCURSOR_LIB_REVISION))
/* /*
* This version number is stored in cursor files; changes to the * This version number is stored in cursor files; changes to the
* file format require updating this version number * file format require updating this version number
*/ */
#define XCURSOR_FILE_MAJOR 1 #define XCURSOR_FILE_MAJOR 1
#define XCURSOR_FILE_MINOR 0 #define XCURSOR_FILE_MINOR 0
#define XCURSOR_FILE_VERSION ((XCURSOR_FILE_MAJOR << 16) | (XCURSOR_FILE_MINOR)) #define XCURSOR_FILE_VERSION ((XCURSOR_FILE_MAJOR << 16) | (XCURSOR_FILE_MINOR))
#define XCURSOR_FILE_HEADER_LEN (4 * 4) #define XCURSOR_FILE_HEADER_LEN (4 * 4)
#define XCURSOR_FILE_TOC_LEN (3 * 4) #define XCURSOR_FILE_TOC_LEN (3 * 4)
typedef struct _XcursorFileToc { typedef struct _XcursorFileToc
XcursorUInt type; /* chunk type */ {
XcursorUInt subtype; /* subtype (size for images) */ XcursorUInt type; /* chunk type */
XcursorUInt position; /* absolute position in file */ XcursorUInt subtype; /* subtype (size for images) */
XcursorUInt position; /* absolute position in file */
} XcursorFileToc; } XcursorFileToc;
typedef struct _XcursorFileHeader { typedef struct _XcursorFileHeader
XcursorUInt magic; /* magic number */ {
XcursorUInt header; /* byte length of header */ XcursorUInt magic; /* magic number */
XcursorUInt version; /* file version number */ XcursorUInt header; /* byte length of header */
XcursorUInt ntoc; /* number of toc entries */ XcursorUInt version; /* file version number */
XcursorFileToc *tocs; /* table of contents */ XcursorUInt ntoc; /* number of toc entries */
XcursorFileToc *tocs; /* table of contents */
} XcursorFileHeader; } XcursorFileHeader;
/* /*
@ -122,13 +122,14 @@ typedef struct _XcursorFileHeader {
* CARD32 version chunk type version * CARD32 version chunk type version
*/ */
#define XCURSOR_CHUNK_HEADER_LEN (4 * 4) #define XCURSOR_CHUNK_HEADER_LEN (4 * 4)
typedef struct _XcursorChunkHeader { typedef struct _XcursorChunkHeader
XcursorUInt header; /* bytes in chunk header */ {
XcursorUInt type; /* chunk type */ XcursorUInt header; /* bytes in chunk header */
XcursorUInt subtype; /* chunk subtype (size for images) */ XcursorUInt type; /* chunk type */
XcursorUInt version; /* version of this type */ XcursorUInt subtype; /* chunk subtype (size for images) */
XcursorUInt version; /* version of this type */
} XcursorChunkHeader; } XcursorChunkHeader;
/* /*
@ -145,18 +146,19 @@ typedef struct _XcursorChunkHeader {
* LISTofCARD8 text UTF-8 encoded text * LISTofCARD8 text UTF-8 encoded text
*/ */
#define XCURSOR_COMMENT_TYPE 0xfffe0001 #define XCURSOR_COMMENT_TYPE 0xfffe0001
#define XCURSOR_COMMENT_VERSION 1 #define XCURSOR_COMMENT_VERSION 1
#define XCURSOR_COMMENT_HEADER_LEN (XCURSOR_CHUNK_HEADER_LEN + (1 *4)) #define XCURSOR_COMMENT_HEADER_LEN (XCURSOR_CHUNK_HEADER_LEN + (1 * 4))
#define XCURSOR_COMMENT_COPYRIGHT 1 #define XCURSOR_COMMENT_COPYRIGHT 1
#define XCURSOR_COMMENT_LICENSE 2 #define XCURSOR_COMMENT_LICENSE 2
#define XCURSOR_COMMENT_OTHER 3 #define XCURSOR_COMMENT_OTHER 3
#define XCURSOR_COMMENT_MAX_LEN 0x100000 #define XCURSOR_COMMENT_MAX_LEN 0x100000
typedef struct _XcursorComment { typedef struct _XcursorComment
XcursorUInt version; {
XcursorUInt comment_type; XcursorUInt version;
char *comment; XcursorUInt comment_type;
char *comment;
} XcursorComment; } XcursorComment;
/* /*
@ -175,14 +177,15 @@ typedef struct _XcursorComment {
* LISTofCARD32 pixels ARGB pixels * LISTofCARD32 pixels ARGB pixels
*/ */
#define XCURSOR_IMAGE_TYPE 0xfffd0002 #define XCURSOR_IMAGE_TYPE 0xfffd0002
#define XCURSOR_IMAGE_VERSION 1 #define XCURSOR_IMAGE_VERSION 1
#define XCURSOR_IMAGE_HEADER_LEN (XCURSOR_CHUNK_HEADER_LEN + (5*4)) #define XCURSOR_IMAGE_HEADER_LEN (XCURSOR_CHUNK_HEADER_LEN + (5 * 4))
#define XCURSOR_IMAGE_MAX_SIZE 0x7fff /* 32767x32767 max cursor size */ #define XCURSOR_IMAGE_MAX_SIZE 0x7fff /* 32767x32767 max cursor size */
typedef struct _XcursorComments { typedef struct _XcursorComments
int ncomment; /* number of comments */ {
XcursorComment **comments; /* array of XcursorComment pointers */ int ncomment; /* number of comments */
XcursorComment **comments; /* array of XcursorComment pointers */
} XcursorComments; } XcursorComments;
/* /*
@ -190,21 +193,20 @@ typedef struct _XcursorComments {
*/ */
static XcursorImage * static XcursorImage *
XcursorImageCreate (int width, int height) XcursorImageCreate(int width, int height)
{ {
XcursorImage *image; XcursorImage *image;
if (width < 0 || height < 0) if (width < 0 || height < 0)
return NULL; return NULL;
if (width > XCURSOR_IMAGE_MAX_SIZE || height > XCURSOR_IMAGE_MAX_SIZE) if (width > XCURSOR_IMAGE_MAX_SIZE || height > XCURSOR_IMAGE_MAX_SIZE)
return NULL; return NULL;
image = malloc (sizeof (XcursorImage) + image = malloc(sizeof(XcursorImage) + width * height * sizeof(XcursorPixel));
width * height * sizeof (XcursorPixel));
if (!image) if (!image)
return NULL; return NULL;
image->version = XCURSOR_IMAGE_VERSION; image->version = XCURSOR_IMAGE_VERSION;
image->pixels = (XcursorPixel *) (image + 1); image->pixels = (XcursorPixel *)(image + 1);
image->size = width > height ? width : height; image->size = width > height ? width : height;
image->width = width; image->width = width;
image->height = height; image->height = height;
@ -213,325 +215,305 @@ XcursorImageCreate (int width, int height)
} }
static void static void
XcursorImageDestroy (XcursorImage *image) XcursorImageDestroy(XcursorImage *image)
{ {
free (image); free(image);
} }
static XcursorImages * static XcursorImages *
XcursorImagesCreate (int size) XcursorImagesCreate(int size)
{ {
XcursorImages *images; XcursorImages *images;
images = malloc (sizeof (XcursorImages) + images = malloc(sizeof(XcursorImages) + size * sizeof(XcursorImage *));
size * sizeof (XcursorImage *));
if (!images) if (!images)
return NULL; return NULL;
images->nimage = 0; images->nimage = 0;
images->images = (XcursorImage **) (images + 1); images->images = (XcursorImage **)(images + 1);
return images; return images;
} }
void void XcursorImagesDestroy(XcursorImages *images)
XcursorImagesDestroy (XcursorImages *images)
{ {
int n; int n;
if (!images) if (!images)
return; return;
for (n = 0; n < images->nimage; n++) for (n = 0; n < images->nimage; n++)
XcursorImageDestroy (images->images[n]); XcursorImageDestroy(images->images[n]);
free (images); free(images);
} }
static XcursorBool static XcursorBool
_XcursorReadUInt (XcursorFile *file, XcursorUInt *u) _XcursorReadUInt(XcursorFile *file, XcursorUInt *u)
{ {
uint8_t bytes[4]; uint8_t bytes[4];
if (!file || !u) if (!file || !u)
return XcursorFalse; return XcursorFalse;
if ((*file->read) (file, bytes, 4) != 4) if ((*file->read)(file, bytes, 4) != 4)
return XcursorFalse; return XcursorFalse;
*u = ((XcursorUInt)(bytes[0]) << 0) | *u = ((XcursorUInt)(bytes[0]) << 0) | ((XcursorUInt)(bytes[1]) << 8) | ((XcursorUInt)(bytes[2]) << 16) | ((XcursorUInt)(bytes[3]) << 24);
((XcursorUInt)(bytes[1]) << 8) |
((XcursorUInt)(bytes[2]) << 16) |
((XcursorUInt)(bytes[3]) << 24);
return XcursorTrue; return XcursorTrue;
} }
static void static void
_XcursorFileHeaderDestroy (XcursorFileHeader *fileHeader) _XcursorFileHeaderDestroy(XcursorFileHeader *fileHeader)
{ {
free (fileHeader); free(fileHeader);
} }
static XcursorFileHeader * static XcursorFileHeader *
_XcursorFileHeaderCreate (XcursorUInt ntoc) _XcursorFileHeaderCreate(XcursorUInt ntoc)
{ {
XcursorFileHeader *fileHeader; XcursorFileHeader *fileHeader;
if (ntoc > 0x10000) if (ntoc > 0x10000)
return NULL; return NULL;
fileHeader = malloc (sizeof (XcursorFileHeader) + fileHeader = malloc(sizeof(XcursorFileHeader) + ntoc * sizeof(XcursorFileToc));
ntoc * sizeof (XcursorFileToc));
if (!fileHeader) if (!fileHeader)
return NULL; return NULL;
fileHeader->magic = XCURSOR_MAGIC; fileHeader->magic = XCURSOR_MAGIC;
fileHeader->header = XCURSOR_FILE_HEADER_LEN; fileHeader->header = XCURSOR_FILE_HEADER_LEN;
fileHeader->version = XCURSOR_FILE_VERSION; fileHeader->version = XCURSOR_FILE_VERSION;
fileHeader->ntoc = ntoc; fileHeader->ntoc = ntoc;
fileHeader->tocs = (XcursorFileToc *) (fileHeader + 1); fileHeader->tocs = (XcursorFileToc *)(fileHeader + 1);
return fileHeader; return fileHeader;
} }
static XcursorFileHeader * static XcursorFileHeader *
_XcursorReadFileHeader (XcursorFile *file) _XcursorReadFileHeader(XcursorFile *file)
{ {
XcursorFileHeader head, *fileHeader; XcursorFileHeader head, *fileHeader;
XcursorUInt skip; XcursorUInt skip;
unsigned int n; unsigned int n;
if (!file) if (!file)
return NULL; return NULL;
if (!_XcursorReadUInt (file, &head.magic)) if (!_XcursorReadUInt(file, &head.magic))
return NULL; return NULL;
if (head.magic != XCURSOR_MAGIC) if (head.magic != XCURSOR_MAGIC)
return NULL; return NULL;
if (!_XcursorReadUInt (file, &head.header)) if (!_XcursorReadUInt(file, &head.header))
return NULL; return NULL;
if (!_XcursorReadUInt (file, &head.version)) if (!_XcursorReadUInt(file, &head.version))
return NULL; return NULL;
if (!_XcursorReadUInt (file, &head.ntoc)) if (!_XcursorReadUInt(file, &head.ntoc))
return NULL; return NULL;
skip = head.header - XCURSOR_FILE_HEADER_LEN; skip = head.header - XCURSOR_FILE_HEADER_LEN;
if (skip) if (skip)
if (!(*file->skip) (file, skip)) if (!(*file->skip)(file, skip))
return NULL; return NULL;
fileHeader = _XcursorFileHeaderCreate (head.ntoc); fileHeader = _XcursorFileHeaderCreate(head.ntoc);
if (!fileHeader) if (!fileHeader)
return NULL; return NULL;
fileHeader->magic = head.magic; fileHeader->magic = head.magic;
fileHeader->header = head.header; fileHeader->header = head.header;
fileHeader->version = head.version; fileHeader->version = head.version;
fileHeader->ntoc = head.ntoc; fileHeader->ntoc = head.ntoc;
for (n = 0; n < fileHeader->ntoc; n++) for (n = 0; n < fileHeader->ntoc; n++) {
{ if (!_XcursorReadUInt(file, &fileHeader->tocs[n].type))
if (!_XcursorReadUInt (file, &fileHeader->tocs[n].type)) break;
break; if (!_XcursorReadUInt(file, &fileHeader->tocs[n].subtype))
if (!_XcursorReadUInt (file, &fileHeader->tocs[n].subtype)) break;
break; if (!_XcursorReadUInt(file, &fileHeader->tocs[n].position))
if (!_XcursorReadUInt (file, &fileHeader->tocs[n].position)) break;
break;
} }
if (n != fileHeader->ntoc) if (n != fileHeader->ntoc) {
{ _XcursorFileHeaderDestroy(fileHeader);
_XcursorFileHeaderDestroy (fileHeader); return NULL;
return NULL;
} }
return fileHeader; return fileHeader;
} }
static XcursorBool static XcursorBool
_XcursorSeekToToc (XcursorFile *file, _XcursorSeekToToc(XcursorFile *file,
XcursorFileHeader *fileHeader, XcursorFileHeader *fileHeader,
int toc) int toc)
{ {
if (!file || !fileHeader) if (!file || !fileHeader)
return XcursorFalse; return XcursorFalse;
return (*file->seek) (file, fileHeader->tocs[toc].position); return (*file->seek)(file, fileHeader->tocs[toc].position);
} }
static XcursorBool static XcursorBool
_XcursorFileReadChunkHeader (XcursorFile *file, _XcursorFileReadChunkHeader(XcursorFile *file,
XcursorFileHeader *fileHeader, XcursorFileHeader *fileHeader,
int toc, int toc,
XcursorChunkHeader *chunkHeader) XcursorChunkHeader *chunkHeader)
{ {
if (!file || !fileHeader || !chunkHeader) if (!file || !fileHeader || !chunkHeader)
return XcursorFalse; return XcursorFalse;
if (!_XcursorSeekToToc (file, fileHeader, toc)) if (!_XcursorSeekToToc(file, fileHeader, toc))
return XcursorFalse; return XcursorFalse;
if (!_XcursorReadUInt (file, &chunkHeader->header)) if (!_XcursorReadUInt(file, &chunkHeader->header))
return XcursorFalse; return XcursorFalse;
if (!_XcursorReadUInt (file, &chunkHeader->type)) if (!_XcursorReadUInt(file, &chunkHeader->type))
return XcursorFalse; return XcursorFalse;
if (!_XcursorReadUInt (file, &chunkHeader->subtype)) if (!_XcursorReadUInt(file, &chunkHeader->subtype))
return XcursorFalse; return XcursorFalse;
if (!_XcursorReadUInt (file, &chunkHeader->version)) if (!_XcursorReadUInt(file, &chunkHeader->version))
return XcursorFalse; return XcursorFalse;
/* sanity check */ /* sanity check */
if (chunkHeader->type != fileHeader->tocs[toc].type || if (chunkHeader->type != fileHeader->tocs[toc].type || chunkHeader->subtype != fileHeader->tocs[toc].subtype)
chunkHeader->subtype != fileHeader->tocs[toc].subtype) return XcursorFalse;
return XcursorFalse;
return XcursorTrue; return XcursorTrue;
} }
#define dist(a,b) ((a) > (b) ? (a) - (b) : (b) - (a)) #define dist(a, b) ((a) > (b) ? (a) - (b) : (b) - (a))
static XcursorDim static XcursorDim
_XcursorFindBestSize (XcursorFileHeader *fileHeader, _XcursorFindBestSize(XcursorFileHeader *fileHeader,
XcursorDim size, XcursorDim size,
int *nsizesp) int *nsizesp)
{ {
unsigned int n; unsigned int n;
int nsizes = 0; int nsizes = 0;
XcursorDim bestSize = 0; XcursorDim bestSize = 0;
XcursorDim thisSize; XcursorDim thisSize;
if (!fileHeader || !nsizesp) if (!fileHeader || !nsizesp)
return 0; return 0;
for (n = 0; n < fileHeader->ntoc; n++) for (n = 0; n < fileHeader->ntoc; n++) {
{ if (fileHeader->tocs[n].type != XCURSOR_IMAGE_TYPE)
if (fileHeader->tocs[n].type != XCURSOR_IMAGE_TYPE) continue;
continue; thisSize = fileHeader->tocs[n].subtype;
thisSize = fileHeader->tocs[n].subtype; if (!bestSize || dist(thisSize, size) < dist(bestSize, size)) {
if (!bestSize || dist (thisSize, size) < dist (bestSize, size)) bestSize = thisSize;
{ nsizes = 1;
bestSize = thisSize; } else if (thisSize == bestSize)
nsizes = 1; nsizes++;
}
else if (thisSize == bestSize)
nsizes++;
} }
*nsizesp = nsizes; *nsizesp = nsizes;
return bestSize; return bestSize;
} }
static int static int
_XcursorFindImageToc (XcursorFileHeader *fileHeader, _XcursorFindImageToc(XcursorFileHeader *fileHeader,
XcursorDim size, XcursorDim size,
int count) int count)
{ {
unsigned int toc; unsigned int toc;
XcursorDim thisSize; XcursorDim thisSize;
if (!fileHeader) if (!fileHeader)
return 0; return 0;
for (toc = 0; toc < fileHeader->ntoc; toc++) for (toc = 0; toc < fileHeader->ntoc; toc++) {
{ if (fileHeader->tocs[toc].type != XCURSOR_IMAGE_TYPE)
if (fileHeader->tocs[toc].type != XCURSOR_IMAGE_TYPE) continue;
continue; thisSize = fileHeader->tocs[toc].subtype;
thisSize = fileHeader->tocs[toc].subtype; if (thisSize != size)
if (thisSize != size) continue;
continue; if (!count)
if (!count) break;
break; count--;
count--;
} }
if (toc == fileHeader->ntoc) if (toc == fileHeader->ntoc)
return -1; return -1;
return toc; return toc;
} }
static XcursorImage * static XcursorImage *
_XcursorReadImage (XcursorFile *file, _XcursorReadImage(XcursorFile *file,
XcursorFileHeader *fileHeader, XcursorFileHeader *fileHeader,
int toc) int toc)
{ {
XcursorChunkHeader chunkHeader; XcursorChunkHeader chunkHeader;
XcursorImage head; XcursorImage head;
XcursorImage *image; XcursorImage *image;
int n; int n;
XcursorPixel *p; XcursorPixel *p;
if (!file || !fileHeader) if (!file || !fileHeader)
return NULL; return NULL;
if (!_XcursorFileReadChunkHeader (file, fileHeader, toc, &chunkHeader)) if (!_XcursorFileReadChunkHeader(file, fileHeader, toc, &chunkHeader))
return NULL; return NULL;
if (!_XcursorReadUInt (file, &head.width)) if (!_XcursorReadUInt(file, &head.width))
return NULL; return NULL;
if (!_XcursorReadUInt (file, &head.height)) if (!_XcursorReadUInt(file, &head.height))
return NULL; return NULL;
if (!_XcursorReadUInt (file, &head.xhot)) if (!_XcursorReadUInt(file, &head.xhot))
return NULL; return NULL;
if (!_XcursorReadUInt (file, &head.yhot)) if (!_XcursorReadUInt(file, &head.yhot))
return NULL; return NULL;
if (!_XcursorReadUInt (file, &head.delay)) if (!_XcursorReadUInt(file, &head.delay))
return NULL; return NULL;
/* sanity check data */ /* sanity check data */
if (head.width > XCURSOR_IMAGE_MAX_SIZE || if (head.width > XCURSOR_IMAGE_MAX_SIZE || head.height > XCURSOR_IMAGE_MAX_SIZE)
head.height > XCURSOR_IMAGE_MAX_SIZE) return NULL;
return NULL;
if (head.width == 0 || head.height == 0) if (head.width == 0 || head.height == 0)
return NULL; return NULL;
if (head.xhot > head.width || head.yhot > head.height) if (head.xhot > head.width || head.yhot > head.height)
return NULL; return NULL;
/* Create the image and initialize it */ /* Create the image and initialize it */
image = XcursorImageCreate (head.width, head.height); image = XcursorImageCreate(head.width, head.height);
if (image == NULL) if (image == NULL)
return NULL; return NULL;
if (chunkHeader.version < image->version) if (chunkHeader.version < image->version)
image->version = chunkHeader.version; image->version = chunkHeader.version;
image->size = chunkHeader.subtype; image->size = chunkHeader.subtype;
image->xhot = head.xhot; image->xhot = head.xhot;
image->yhot = head.yhot; image->yhot = head.yhot;
image->delay = head.delay; image->delay = head.delay;
n = image->width * image->height; n = image->width * image->height;
p = image->pixels; p = image->pixels;
while (n--) while (n--) {
{ if (!_XcursorReadUInt(file, p)) {
if (!_XcursorReadUInt (file, p)) XcursorImageDestroy(image);
{ return NULL;
XcursorImageDestroy (image); }
return NULL; p++;
}
p++;
} }
return image; return image;
} }
XcursorImages * XcursorImages *
XcursorXcFileLoadImages (XcursorFile *file, int size) XcursorXcFileLoadImages(XcursorFile *file, int size)
{ {
XcursorFileHeader *fileHeader; XcursorFileHeader *fileHeader;
XcursorDim bestSize; XcursorDim bestSize;
int nsize; int nsize;
XcursorImages *images; XcursorImages *images;
int n; int n;
int toc; int toc;
if (!file || size < 0) if (!file || size < 0)
return NULL; return NULL;
fileHeader = _XcursorReadFileHeader (file); fileHeader = _XcursorReadFileHeader(file);
if (!fileHeader) if (!fileHeader)
return NULL; return NULL;
bestSize = _XcursorFindBestSize (fileHeader, (XcursorDim) size, &nsize); bestSize = _XcursorFindBestSize(fileHeader, (XcursorDim)size, &nsize);
if (!bestSize) if (!bestSize) {
{ _XcursorFileHeaderDestroy(fileHeader);
_XcursorFileHeaderDestroy (fileHeader); return NULL;
return NULL;
} }
images = XcursorImagesCreate (nsize); images = XcursorImagesCreate(nsize);
if (!images) if (!images) {
{ _XcursorFileHeaderDestroy(fileHeader);
_XcursorFileHeaderDestroy (fileHeader); return NULL;
return NULL;
} }
for (n = 0; n < nsize; n++) for (n = 0; n < nsize; n++) {
{ toc = _XcursorFindImageToc(fileHeader, bestSize, n);
toc = _XcursorFindImageToc (fileHeader, bestSize, n); if (toc < 0)
if (toc < 0) break;
break; images->images[images->nimage] = _XcursorReadImage(file, fileHeader,
images->images[images->nimage] = _XcursorReadImage (file, fileHeader, toc);
toc); if (!images->images[images->nimage])
if (!images->images[images->nimage]) break;
break; images->nimage++;
images->nimage++;
} }
_XcursorFileHeaderDestroy (fileHeader); _XcursorFileHeaderDestroy(fileHeader);
if (images->nimage != nsize) if (images->nimage != nsize) {
{ XcursorImagesDestroy(images);
XcursorImagesDestroy (images); images = NULL;
images = NULL;
} }
return images; return images;
} }

View file

@ -32,45 +32,47 @@ extern "C" {
#include <stdint.h> #include <stdint.h>
typedef int XcursorBool; typedef int XcursorBool;
typedef uint32_t XcursorUInt; typedef uint32_t XcursorUInt;
typedef XcursorUInt XcursorDim; typedef XcursorUInt XcursorDim;
typedef XcursorUInt XcursorPixel; typedef XcursorUInt XcursorPixel;
typedef struct _XcursorImage { typedef struct _XcursorImage
XcursorUInt version; /* version of the image data */ {
XcursorDim size; /* nominal size for matching */ XcursorUInt version; /* version of the image data */
XcursorDim width; /* actual width */ XcursorDim size; /* nominal size for matching */
XcursorDim height; /* actual height */ XcursorDim width; /* actual width */
XcursorDim xhot; /* hot spot x (must be inside image) */ XcursorDim height; /* actual height */
XcursorDim yhot; /* hot spot y (must be inside image) */ XcursorDim xhot; /* hot spot x (must be inside image) */
XcursorUInt delay; /* animation delay to next frame (ms) */ XcursorDim yhot; /* hot spot y (must be inside image) */
XcursorPixel *pixels; /* pointer to pixels */ XcursorUInt delay; /* animation delay to next frame (ms) */
XcursorPixel *pixels; /* pointer to pixels */
} XcursorImage; } XcursorImage;
/* /*
* Other data structures exposed by the library API * Other data structures exposed by the library API
*/ */
typedef struct _XcursorImages { typedef struct _XcursorImages
int nimage; /* number of images */ {
XcursorImage **images; /* array of XcursorImage pointers */ int nimage; /* number of images */
XcursorImage **images; /* array of XcursorImage pointers */
} XcursorImages; } XcursorImages;
typedef struct _XcursorFile XcursorFile; typedef struct _XcursorFile XcursorFile;
struct _XcursorFile { struct _XcursorFile
void *closure; {
int (*read) (XcursorFile *file, uint8_t *buf, int len); void *closure;
XcursorBool (*skip) (XcursorFile *file, long offset); int (*read)(XcursorFile *file, uint8_t *buf, int len);
XcursorBool (*seek) (XcursorFile *file, long offset); XcursorBool (*skip)(XcursorFile *file, long offset);
XcursorBool (*seek)(XcursorFile *file, long offset);
}; };
XcursorImages * XcursorImages *
XcursorXcFileLoadImages (XcursorFile *file, int size); XcursorXcFileLoadImages(XcursorFile *file, int size);
void void XcursorImagesDestroy(XcursorImages *images);
XcursorImagesDestroy (XcursorImages *images);
#ifdef __cplusplus #ifdef __cplusplus
} }