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

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

@ -25,10 +25,10 @@
#define _DEFAULT_SOURCE
#include "xcursor.h"
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
/*
* From libXcursor/include/X11/extensions/Xcursor.h
@ -78,9 +78,7 @@
#define XCURSOR_LIB_MAJOR 1
#define XCURSOR_LIB_MINOR 1
#define XCURSOR_LIB_REVISION 13
#define XCURSOR_LIB_VERSION ((XCURSOR_LIB_MAJOR * 10000) + \
(XCURSOR_LIB_MINOR * 100) + \
(XCURSOR_LIB_REVISION))
#define XCURSOR_LIB_VERSION ((XCURSOR_LIB_MAJOR * 10000) + (XCURSOR_LIB_MINOR * 100) + (XCURSOR_LIB_REVISION))
/*
* This version number is stored in cursor files; changes to the
@ -92,13 +90,15 @@
#define XCURSOR_FILE_HEADER_LEN (4 * 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 position; /* absolute position in file */
} XcursorFileToc;
typedef struct _XcursorFileHeader {
typedef struct _XcursorFileHeader
{
XcursorUInt magic; /* magic number */
XcursorUInt header; /* byte length of header */
XcursorUInt version; /* file version number */
@ -124,7 +124,8 @@ typedef struct _XcursorFileHeader {
#define XCURSOR_CHUNK_HEADER_LEN (4 * 4)
typedef struct _XcursorChunkHeader {
typedef struct _XcursorChunkHeader
{
XcursorUInt header; /* bytes in chunk header */
XcursorUInt type; /* chunk type */
XcursorUInt subtype; /* chunk subtype (size for images) */
@ -147,13 +148,14 @@ typedef struct _XcursorChunkHeader {
#define XCURSOR_COMMENT_TYPE 0xfffe0001
#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_LICENSE 2
#define XCURSOR_COMMENT_OTHER 3
#define XCURSOR_COMMENT_MAX_LEN 0x100000
typedef struct _XcursorComment {
typedef struct _XcursorComment
{
XcursorUInt version;
XcursorUInt comment_type;
char *comment;
@ -177,10 +179,11 @@ typedef struct _XcursorComment {
#define XCURSOR_IMAGE_TYPE 0xfffd0002
#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 */
typedef struct _XcursorComments {
typedef struct _XcursorComments
{
int ncomment; /* number of comments */
XcursorComment **comments; /* array of XcursorComment pointers */
} XcursorComments;
@ -190,7 +193,7 @@ typedef struct _XcursorComments {
*/
static XcursorImage *
XcursorImageCreate (int width, int height)
XcursorImageCreate(int width, int height)
{
XcursorImage *image;
@ -199,12 +202,11 @@ XcursorImageCreate (int width, int height)
if (width > XCURSOR_IMAGE_MAX_SIZE || height > XCURSOR_IMAGE_MAX_SIZE)
return NULL;
image = malloc (sizeof (XcursorImage) +
width * height * sizeof (XcursorPixel));
image = malloc(sizeof(XcursorImage) + width * height * sizeof(XcursorPixel));
if (!image)
return NULL;
image->version = XCURSOR_IMAGE_VERSION;
image->pixels = (XcursorPixel *) (image + 1);
image->pixels = (XcursorPixel *)(image + 1);
image->size = width > height ? width : height;
image->width = width;
image->height = height;
@ -213,27 +215,25 @@ XcursorImageCreate (int width, int height)
}
static void
XcursorImageDestroy (XcursorImage *image)
XcursorImageDestroy(XcursorImage *image)
{
free (image);
free(image);
}
static XcursorImages *
XcursorImagesCreate (int size)
XcursorImagesCreate(int size)
{
XcursorImages *images;
images = malloc (sizeof (XcursorImages) +
size * sizeof (XcursorImage *));
images = malloc(sizeof(XcursorImages) + size * sizeof(XcursorImage *));
if (!images)
return NULL;
images->nimage = 0;
images->images = (XcursorImage **) (images + 1);
images->images = (XcursorImage **)(images + 1);
return images;
}
void
XcursorImagesDestroy (XcursorImages *images)
void XcursorImagesDestroy(XcursorImages *images)
{
int n;
@ -241,55 +241,51 @@ XcursorImagesDestroy (XcursorImages *images)
return;
for (n = 0; n < images->nimage; n++)
XcursorImageDestroy (images->images[n]);
free (images);
XcursorImageDestroy(images->images[n]);
free(images);
}
static XcursorBool
_XcursorReadUInt (XcursorFile *file, XcursorUInt *u)
_XcursorReadUInt(XcursorFile *file, XcursorUInt *u)
{
uint8_t bytes[4];
if (!file || !u)
return XcursorFalse;
if ((*file->read) (file, bytes, 4) != 4)
if ((*file->read)(file, bytes, 4) != 4)
return XcursorFalse;
*u = ((XcursorUInt)(bytes[0]) << 0) |
((XcursorUInt)(bytes[1]) << 8) |
((XcursorUInt)(bytes[2]) << 16) |
((XcursorUInt)(bytes[3]) << 24);
*u = ((XcursorUInt)(bytes[0]) << 0) | ((XcursorUInt)(bytes[1]) << 8) | ((XcursorUInt)(bytes[2]) << 16) | ((XcursorUInt)(bytes[3]) << 24);
return XcursorTrue;
}
static void
_XcursorFileHeaderDestroy (XcursorFileHeader *fileHeader)
_XcursorFileHeaderDestroy(XcursorFileHeader *fileHeader)
{
free (fileHeader);
free(fileHeader);
}
static XcursorFileHeader *
_XcursorFileHeaderCreate (XcursorUInt ntoc)
_XcursorFileHeaderCreate(XcursorUInt ntoc)
{
XcursorFileHeader *fileHeader;
if (ntoc > 0x10000)
return NULL;
fileHeader = malloc (sizeof (XcursorFileHeader) +
ntoc * sizeof (XcursorFileToc));
fileHeader = malloc(sizeof(XcursorFileHeader) + ntoc * sizeof(XcursorFileToc));
if (!fileHeader)
return NULL;
fileHeader->magic = XCURSOR_MAGIC;
fileHeader->header = XCURSOR_FILE_HEADER_LEN;
fileHeader->version = XCURSOR_FILE_VERSION;
fileHeader->ntoc = ntoc;
fileHeader->tocs = (XcursorFileToc *) (fileHeader + 1);
fileHeader->tocs = (XcursorFileToc *)(fileHeader + 1);
return fileHeader;
}
static XcursorFileHeader *
_XcursorReadFileHeader (XcursorFile *file)
_XcursorReadFileHeader(XcursorFile *file)
{
XcursorFileHeader head, *fileHeader;
XcursorUInt skip;
@ -298,83 +294,80 @@ _XcursorReadFileHeader (XcursorFile *file)
if (!file)
return NULL;
if (!_XcursorReadUInt (file, &head.magic))
if (!_XcursorReadUInt(file, &head.magic))
return NULL;
if (head.magic != XCURSOR_MAGIC)
return NULL;
if (!_XcursorReadUInt (file, &head.header))
if (!_XcursorReadUInt(file, &head.header))
return NULL;
if (!_XcursorReadUInt (file, &head.version))
if (!_XcursorReadUInt(file, &head.version))
return NULL;
if (!_XcursorReadUInt (file, &head.ntoc))
if (!_XcursorReadUInt(file, &head.ntoc))
return NULL;
skip = head.header - XCURSOR_FILE_HEADER_LEN;
if (skip)
if (!(*file->skip) (file, skip))
if (!(*file->skip)(file, skip))
return NULL;
fileHeader = _XcursorFileHeaderCreate (head.ntoc);
fileHeader = _XcursorFileHeaderCreate(head.ntoc);
if (!fileHeader)
return NULL;
fileHeader->magic = head.magic;
fileHeader->header = head.header;
fileHeader->version = head.version;
fileHeader->ntoc = head.ntoc;
for (n = 0; n < fileHeader->ntoc; n++)
{
if (!_XcursorReadUInt (file, &fileHeader->tocs[n].type))
for (n = 0; n < fileHeader->ntoc; n++) {
if (!_XcursorReadUInt(file, &fileHeader->tocs[n].type))
break;
if (!_XcursorReadUInt (file, &fileHeader->tocs[n].subtype))
if (!_XcursorReadUInt(file, &fileHeader->tocs[n].subtype))
break;
if (!_XcursorReadUInt (file, &fileHeader->tocs[n].position))
if (!_XcursorReadUInt(file, &fileHeader->tocs[n].position))
break;
}
if (n != fileHeader->ntoc)
{
_XcursorFileHeaderDestroy (fileHeader);
if (n != fileHeader->ntoc) {
_XcursorFileHeaderDestroy(fileHeader);
return NULL;
}
return fileHeader;
}
static XcursorBool
_XcursorSeekToToc (XcursorFile *file,
_XcursorSeekToToc(XcursorFile *file,
XcursorFileHeader *fileHeader,
int toc)
{
if (!file || !fileHeader)
return XcursorFalse;
return (*file->seek) (file, fileHeader->tocs[toc].position);
return (*file->seek)(file, fileHeader->tocs[toc].position);
}
static XcursorBool
_XcursorFileReadChunkHeader (XcursorFile *file,
_XcursorFileReadChunkHeader(XcursorFile *file,
XcursorFileHeader *fileHeader,
int toc,
XcursorChunkHeader *chunkHeader)
{
if (!file || !fileHeader || !chunkHeader)
return XcursorFalse;
if (!_XcursorSeekToToc (file, fileHeader, toc))
if (!_XcursorSeekToToc(file, fileHeader, toc))
return XcursorFalse;
if (!_XcursorReadUInt (file, &chunkHeader->header))
if (!_XcursorReadUInt(file, &chunkHeader->header))
return XcursorFalse;
if (!_XcursorReadUInt (file, &chunkHeader->type))
if (!_XcursorReadUInt(file, &chunkHeader->type))
return XcursorFalse;
if (!_XcursorReadUInt (file, &chunkHeader->subtype))
if (!_XcursorReadUInt(file, &chunkHeader->subtype))
return XcursorFalse;
if (!_XcursorReadUInt (file, &chunkHeader->version))
if (!_XcursorReadUInt(file, &chunkHeader->version))
return XcursorFalse;
/* sanity check */
if (chunkHeader->type != fileHeader->tocs[toc].type ||
chunkHeader->subtype != fileHeader->tocs[toc].subtype)
if (chunkHeader->type != fileHeader->tocs[toc].type || chunkHeader->subtype != fileHeader->tocs[toc].subtype)
return XcursorFalse;
return XcursorTrue;
}
#define dist(a,b) ((a) > (b) ? (a) - (b) : (b) - (a))
#define dist(a, b) ((a) > (b) ? (a) - (b) : (b) - (a))
static XcursorDim
_XcursorFindBestSize (XcursorFileHeader *fileHeader,
_XcursorFindBestSize(XcursorFileHeader *fileHeader,
XcursorDim size,
int *nsizesp)
{
@ -386,17 +379,14 @@ _XcursorFindBestSize (XcursorFileHeader *fileHeader,
if (!fileHeader || !nsizesp)
return 0;
for (n = 0; n < fileHeader->ntoc; n++)
{
for (n = 0; n < fileHeader->ntoc; n++) {
if (fileHeader->tocs[n].type != XCURSOR_IMAGE_TYPE)
continue;
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;
}
else if (thisSize == bestSize)
} else if (thisSize == bestSize)
nsizes++;
}
*nsizesp = nsizes;
@ -404,7 +394,7 @@ _XcursorFindBestSize (XcursorFileHeader *fileHeader,
}
static int
_XcursorFindImageToc (XcursorFileHeader *fileHeader,
_XcursorFindImageToc(XcursorFileHeader *fileHeader,
XcursorDim size,
int count)
{
@ -414,8 +404,7 @@ _XcursorFindImageToc (XcursorFileHeader *fileHeader,
if (!fileHeader)
return 0;
for (toc = 0; toc < fileHeader->ntoc; toc++)
{
for (toc = 0; toc < fileHeader->ntoc; toc++) {
if (fileHeader->tocs[toc].type != XCURSOR_IMAGE_TYPE)
continue;
thisSize = fileHeader->tocs[toc].subtype;
@ -431,7 +420,7 @@ _XcursorFindImageToc (XcursorFileHeader *fileHeader,
}
static XcursorImage *
_XcursorReadImage (XcursorFile *file,
_XcursorReadImage(XcursorFile *file,
XcursorFileHeader *fileHeader,
int toc)
{
@ -444,21 +433,20 @@ _XcursorReadImage (XcursorFile *file,
if (!file || !fileHeader)
return NULL;
if (!_XcursorFileReadChunkHeader (file, fileHeader, toc, &chunkHeader))
if (!_XcursorFileReadChunkHeader(file, fileHeader, toc, &chunkHeader))
return NULL;
if (!_XcursorReadUInt (file, &head.width))
if (!_XcursorReadUInt(file, &head.width))
return NULL;
if (!_XcursorReadUInt (file, &head.height))
if (!_XcursorReadUInt(file, &head.height))
return NULL;
if (!_XcursorReadUInt (file, &head.xhot))
if (!_XcursorReadUInt(file, &head.xhot))
return NULL;
if (!_XcursorReadUInt (file, &head.yhot))
if (!_XcursorReadUInt(file, &head.yhot))
return NULL;
if (!_XcursorReadUInt (file, &head.delay))
if (!_XcursorReadUInt(file, &head.delay))
return NULL;
/* sanity check data */
if (head.width > XCURSOR_IMAGE_MAX_SIZE ||
head.height > XCURSOR_IMAGE_MAX_SIZE)
if (head.width > XCURSOR_IMAGE_MAX_SIZE || head.height > XCURSOR_IMAGE_MAX_SIZE)
return NULL;
if (head.width == 0 || head.height == 0)
return NULL;
@ -466,7 +454,7 @@ _XcursorReadImage (XcursorFile *file,
return NULL;
/* Create the image and initialize it */
image = XcursorImageCreate (head.width, head.height);
image = XcursorImageCreate(head.width, head.height);
if (image == NULL)
return NULL;
if (chunkHeader.version < image->version)
@ -477,11 +465,9 @@ _XcursorReadImage (XcursorFile *file,
image->delay = head.delay;
n = image->width * image->height;
p = image->pixels;
while (n--)
{
if (!_XcursorReadUInt (file, p))
{
XcursorImageDestroy (image);
while (n--) {
if (!_XcursorReadUInt(file, p)) {
XcursorImageDestroy(image);
return NULL;
}
p++;
@ -490,7 +476,7 @@ _XcursorReadImage (XcursorFile *file,
}
XcursorImages *
XcursorXcFileLoadImages (XcursorFile *file, int size)
XcursorXcFileLoadImages(XcursorFile *file, int size)
{
XcursorFileHeader *fileHeader;
XcursorDim bestSize;
@ -501,36 +487,32 @@ XcursorXcFileLoadImages (XcursorFile *file, int size)
if (!file || size < 0)
return NULL;
fileHeader = _XcursorReadFileHeader (file);
fileHeader = _XcursorReadFileHeader(file);
if (!fileHeader)
return NULL;
bestSize = _XcursorFindBestSize (fileHeader, (XcursorDim) size, &nsize);
if (!bestSize)
{
_XcursorFileHeaderDestroy (fileHeader);
bestSize = _XcursorFindBestSize(fileHeader, (XcursorDim)size, &nsize);
if (!bestSize) {
_XcursorFileHeaderDestroy(fileHeader);
return NULL;
}
images = XcursorImagesCreate (nsize);
if (!images)
{
_XcursorFileHeaderDestroy (fileHeader);
images = XcursorImagesCreate(nsize);
if (!images) {
_XcursorFileHeaderDestroy(fileHeader);
return NULL;
}
for (n = 0; n < nsize; n++)
{
toc = _XcursorFindImageToc (fileHeader, bestSize, n);
for (n = 0; n < nsize; n++) {
toc = _XcursorFindImageToc(fileHeader, bestSize, n);
if (toc < 0)
break;
images->images[images->nimage] = _XcursorReadImage (file, fileHeader,
images->images[images->nimage] = _XcursorReadImage(file, fileHeader,
toc);
if (!images->images[images->nimage])
break;
images->nimage++;
}
_XcursorFileHeaderDestroy (fileHeader);
if (images->nimage != nsize)
{
XcursorImagesDestroy (images);
_XcursorFileHeaderDestroy(fileHeader);
if (images->nimage != nsize) {
XcursorImagesDestroy(images);
images = NULL;
}
return images;

View file

@ -38,7 +38,8 @@ typedef uint32_t XcursorUInt;
typedef XcursorUInt XcursorDim;
typedef XcursorUInt XcursorPixel;
typedef struct _XcursorImage {
typedef struct _XcursorImage
{
XcursorUInt version; /* version of the image data */
XcursorDim size; /* nominal size for matching */
XcursorDim width; /* actual width */
@ -52,25 +53,26 @@ typedef struct _XcursorImage {
/*
* 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 */
} XcursorImages;
typedef struct _XcursorFile XcursorFile;
struct _XcursorFile {
struct _XcursorFile
{
void *closure;
int (*read) (XcursorFile *file, uint8_t *buf, int len);
XcursorBool (*skip) (XcursorFile *file, long offset);
XcursorBool (*seek) (XcursorFile *file, long offset);
int (*read)(XcursorFile *file, uint8_t *buf, int len);
XcursorBool (*skip)(XcursorFile *file, long offset);
XcursorBool (*seek)(XcursorFile *file, long offset);
};
XcursorImages *
XcursorXcFileLoadImages (XcursorFile *file, int size);
XcursorXcFileLoadImages(XcursorFile *file, int size);
void
XcursorImagesDestroy (XcursorImages *images);
void XcursorImagesDestroy(XcursorImages *images);
#ifdef __cplusplus
}