@@ -0,0 +1,968 @@
2+ * Copyright © 2002 Keith Packard
4+ * Permission to use, copy, modify, distribute, and sell this software and its
5+ * documentation for any purpose is hereby granted without fee, provided that
6+ * the above copyright notice appear in all copies and that both that
7+ * copyright notice and this permission notice appear in supporting
8+ * documentation, and that the name of Keith Packard not be used in
9+ * advertising or publicity pertaining to distribution of the software without
10+ * specific, written prior permission. Keith Packard makes no
11+ * representations about the suitability of this software for any purpose. It
12+ * is provided "as is" without express or implied warranty.
14+ * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15+ * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16+ * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18+ * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19+ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20+ * PERFORMANCE OF THIS SOFTWARE.
30+ * From libXcursor/include/X11/extensions/Xcursor.h
34+#define XcursorFalse 0
37+ * Cursor files start with a header. The header
38+ * contains a magic number, a version number and a
39+ * table of contents which has type and offset information
40+ * for the remaining tables in the file.
42+ * File minor versions increment for compatible changes
43+ * File major versions increment for incompatible changes (never, we hope)
45+ * Chunks of the same type are always upward compatible. Incompatible
46+ * changes are made with new chunk types; the old data can remain under
47+ * the old type. Upward compatible changes can add header data as the
48+ * header lengths are specified in the file.
55+ * CARD32 magic magic number
56+ * CARD32 header bytes in file header
57+ * CARD32 version file version
58+ * CARD32 ntoc number of toc entries
59+ * LISTofFileToc toc table of contents
62+ * CARD32 type entry type
63+ * CARD32 subtype entry subtype (size for images)
64+ * CARD32 position absolute file position
67+#define XCURSOR_MAGIC 0x72756358 /* "Xcur" LSBFirst */
70+ * Current Xcursor version number. Will be substituted by configure
71+ * from the version in the libXcursor configure.ac file.
74+#define XCURSOR_LIB_MAJOR 1
75+#define XCURSOR_LIB_MINOR 1
76+#define XCURSOR_LIB_REVISION 13
77+#define XCURSOR_LIB_VERSION ((XCURSOR_LIB_MAJOR * 10000) + \
78+ (XCURSOR_LIB_MINOR * 100) + \
79+ (XCURSOR_LIB_REVISION))
82+ * This version number is stored in cursor files; changes to the
83+ * file format require updating this version number
85+#define XCURSOR_FILE_MAJOR 1
86+#define XCURSOR_FILE_MINOR 0
87+#define XCURSOR_FILE_VERSION ((XCURSOR_FILE_MAJOR << 16) | (XCURSOR_FILE_MINOR))
88+#define XCURSOR_FILE_HEADER_LEN (4 * 4)
89+#define XCURSOR_FILE_TOC_LEN (3 * 4)
91+typedef struct _XcursorFileToc {
92+ XcursorUInt type; /* chunk type */
93+ XcursorUInt subtype; /* subtype (size for images) */
94+ XcursorUInt position; /* absolute position in file */
97+typedef struct _XcursorFileHeader {
98+ XcursorUInt magic; /* magic number */
99+ XcursorUInt header; /* byte length of header */
100+ XcursorUInt version; /* file version number */
101+ XcursorUInt ntoc; /* number of toc entries */
102+ XcursorFileToc *tocs; /* table of contents */
106+ * The rest of the file is a list of chunks, each tagged by type
111+ * <extra type-specific header fields>
112+ * <type-specific data>
115+ * CARD32 header bytes in chunk header + type header
116+ * CARD32 type chunk type
117+ * CARD32 subtype chunk subtype
118+ * CARD32 version chunk type version
121+#define XCURSOR_CHUNK_HEADER_LEN (4 * 4)
123+typedef struct _XcursorChunkHeader {
124+ XcursorUInt header; /* bytes in chunk header */
125+ XcursorUInt type; /* chunk type */
126+ XcursorUInt subtype; /* chunk subtype (size for images) */
127+ XcursorUInt version; /* version of this type */
128+} XcursorChunkHeader;
131+ * Here's a list of the known chunk types
135+ * Comments consist of a 4-byte length field followed by
136+ * UTF-8 encoded text
139+ * ChunkHeader header chunk header
140+ * CARD32 length bytes in text
141+ * LISTofCARD8 text UTF-8 encoded text
144+#define XCURSOR_COMMENT_TYPE 0xfffe0001
145+#define XCURSOR_COMMENT_VERSION 1
146+#define XCURSOR_COMMENT_HEADER_LEN (XCURSOR_CHUNK_HEADER_LEN + (1 *4))
147+#define XCURSOR_COMMENT_COPYRIGHT 1
148+#define XCURSOR_COMMENT_LICENSE 2
149+#define XCURSOR_COMMENT_OTHER 3
150+#define XCURSOR_COMMENT_MAX_LEN 0x100000
152+typedef struct _XcursorComment {
153+ XcursorUInt version;
154+ XcursorUInt comment_type;
159+ * Each cursor image occupies a separate image chunk.
160+ * The length of the image header follows the chunk header
161+ * so that future versions can extend the header without
162+ * breaking older applications
165+ * ChunkHeader header chunk header
166+ * CARD32 width actual width
167+ * CARD32 height actual height
168+ * CARD32 xhot hot spot x
169+ * CARD32 yhot hot spot y
170+ * CARD32 delay animation delay
171+ * LISTofCARD32 pixels ARGB pixels
174+#define XCURSOR_IMAGE_TYPE 0xfffd0002
175+#define XCURSOR_IMAGE_VERSION 1
176+#define XCURSOR_IMAGE_HEADER_LEN (XCURSOR_CHUNK_HEADER_LEN + (5*4))
177+#define XCURSOR_IMAGE_MAX_SIZE 0x7fff /* 32767x32767 max cursor size */
179+typedef struct _XcursorFile XcursorFile;
181+struct _XcursorFile {
183+ int (*read) (XcursorFile *file, unsigned char *buf, int len);
184+ int (*write) (XcursorFile *file, unsigned char *buf, int len);
185+ int (*seek) (XcursorFile *file, long offset, int whence);
188+typedef struct _XcursorComments {
189+ int ncomment; /* number of comments */
190+ XcursorComment **comments; /* array of XcursorComment pointers */
194+ * From libXcursor/src/file.c
197+static XcursorImage *
198+XcursorImageCreate (int width, int height)
200+ XcursorImage *image;
202+ image = malloc (sizeof (XcursorImage) +
203+ width * height * sizeof (XcursorPixel));
206+ image->version = XCURSOR_IMAGE_VERSION;
207+ image->pixels = (XcursorPixel *) (image + 1);
208+ image->size = width > height ? width : height;
209+ image->width = width;
210+ image->height = height;
216+XcursorImageDestroy (XcursorImage *image)
221+static XcursorImages *
222+XcursorImagesCreate (int size)
224+ XcursorImages *images;
226+ images = malloc (sizeof (XcursorImages) +
227+ size * sizeof (XcursorImage *));
231+ images->images = (XcursorImage **) (images + 1);
232+ images->name = NULL;
237+XcursorImagesDestroy (XcursorImages *images)
244+ for (n = 0; n < images->nimage; n++)
245+ XcursorImageDestroy (images->images[n]);
247+ free (images->name);
252+XcursorImagesSetName (XcursorImages *images, const char *name)
256+ if (!images || !name)
259+ new = malloc (strlen (name) + 1);
266+ free (images->name);
271+_XcursorReadUInt (XcursorFile *file, XcursorUInt *u)
273+ unsigned char bytes[4];
276+ return XcursorFalse;
278+ if ((*file->read) (file, bytes, 4) != 4)
279+ return XcursorFalse;
280+ *u = ((bytes[0] << 0) |
288+_XcursorFileHeaderDestroy (XcursorFileHeader *fileHeader)
293+static XcursorFileHeader *
294+_XcursorFileHeaderCreate (int ntoc)
296+ XcursorFileHeader *fileHeader;
300+ fileHeader = malloc (sizeof (XcursorFileHeader) +
301+ ntoc * sizeof (XcursorFileToc));
304+ fileHeader->magic = XCURSOR_MAGIC;
305+ fileHeader->header = XCURSOR_FILE_HEADER_LEN;
306+ fileHeader->version = XCURSOR_FILE_VERSION;
307+ fileHeader->ntoc = ntoc;
308+ fileHeader->tocs = (XcursorFileToc *) (fileHeader + 1);
312+static XcursorFileHeader *
313+_XcursorReadFileHeader (XcursorFile *file)
315+ XcursorFileHeader head, *fileHeader;
322+ if (!_XcursorReadUInt (file, &head.magic))
324+ if (head.magic != XCURSOR_MAGIC)
326+ if (!_XcursorReadUInt (file, &head.header))
328+ if (!_XcursorReadUInt (file, &head.version))
330+ if (!_XcursorReadUInt (file, &head.ntoc))
332+ skip = head.header - XCURSOR_FILE_HEADER_LEN;
334+ if ((*file->seek) (file, skip, SEEK_CUR) == EOF)
336+ fileHeader = _XcursorFileHeaderCreate (head.ntoc);
339+ fileHeader->magic = head.magic;
340+ fileHeader->header = head.header;
341+ fileHeader->version = head.version;
342+ fileHeader->ntoc = head.ntoc;
343+ for (n = 0; n < fileHeader->ntoc; n++)
345+ if (!_XcursorReadUInt (file, &fileHeader->tocs[n].type))
347+ if (!_XcursorReadUInt (file, &fileHeader->tocs[n].subtype))
349+ if (!_XcursorReadUInt (file, &fileHeader->tocs[n].position))
352+ if (n != fileHeader->ntoc)
354+ _XcursorFileHeaderDestroy (fileHeader);
361+_XcursorSeekToToc (XcursorFile *file,
362+ XcursorFileHeader *fileHeader,
365+ if (!file || !fileHeader || \
366+ (*file->seek) (file, fileHeader->tocs[toc].position, SEEK_SET) == EOF)
367+ return XcursorFalse;
372+_XcursorFileReadChunkHeader (XcursorFile *file,
373+ XcursorFileHeader *fileHeader,
375+ XcursorChunkHeader *chunkHeader)
377+ if (!file || !fileHeader || !chunkHeader)
378+ return XcursorFalse;
379+ if (!_XcursorSeekToToc (file, fileHeader, toc))
380+ return XcursorFalse;
381+ if (!_XcursorReadUInt (file, &chunkHeader->header))
382+ return XcursorFalse;
383+ if (!_XcursorReadUInt (file, &chunkHeader->type))
384+ return XcursorFalse;
385+ if (!_XcursorReadUInt (file, &chunkHeader->subtype))
386+ return XcursorFalse;
387+ if (!_XcursorReadUInt (file, &chunkHeader->version))
388+ return XcursorFalse;
390+ if (chunkHeader->type != fileHeader->tocs[toc].type ||
391+ chunkHeader->subtype != fileHeader->tocs[toc].subtype)
392+ return XcursorFalse;
396+#define dist(a,b) ((a) > (b) ? (a) - (b) : (b) - (a))
399+_XcursorFindBestSize (XcursorFileHeader *fileHeader,
405+ XcursorDim bestSize = 0;
406+ XcursorDim thisSize;
408+ if (!fileHeader || !nsizesp)
411+ for (n = 0; n < fileHeader->ntoc; n++)
413+ if (fileHeader->tocs[n].type != XCURSOR_IMAGE_TYPE)
415+ thisSize = fileHeader->tocs[n].subtype;
416+ if (!bestSize || dist (thisSize, size) < dist (bestSize, size))
418+ bestSize = thisSize;
421+ else if (thisSize == bestSize)
429+_XcursorFindImageToc (XcursorFileHeader *fileHeader,
434+ XcursorDim thisSize;
439+ for (toc = 0; toc < fileHeader->ntoc; toc++)
441+ if (fileHeader->tocs[toc].type != XCURSOR_IMAGE_TYPE)
443+ thisSize = fileHeader->tocs[toc].subtype;
444+ if (thisSize != size)
450+ if (toc == fileHeader->ntoc)
455+static XcursorImage *
456+_XcursorReadImage (XcursorFile *file,
457+ XcursorFileHeader *fileHeader,
460+ XcursorChunkHeader chunkHeader;
462+ XcursorImage *image;
466+ if (!file || !fileHeader)
469+ if (!_XcursorFileReadChunkHeader (file, fileHeader, toc, &chunkHeader))
471+ if (!_XcursorReadUInt (file, &head.width))
473+ if (!_XcursorReadUInt (file, &head.height))
475+ if (!_XcursorReadUInt (file, &head.xhot))
477+ if (!_XcursorReadUInt (file, &head.yhot))
479+ if (!_XcursorReadUInt (file, &head.delay))
481+ /* sanity check data */
482+ if (head.width >= 0x10000 || head.height > 0x10000)
484+ if (head.width == 0 || head.height == 0)
486+ if (head.xhot > head.width || head.yhot > head.height)
489+ /* Create the image and initialize it */
490+ image = XcursorImageCreate (head.width, head.height);
493+ if (chunkHeader.version < image->version)
494+ image->version = chunkHeader.version;
495+ image->size = chunkHeader.subtype;
496+ image->xhot = head.xhot;
497+ image->yhot = head.yhot;
498+ image->delay = head.delay;
499+ n = image->width * image->height;
503+ if (!_XcursorReadUInt (file, p))
505+ XcursorImageDestroy (image);
513+static XcursorImages *
514+XcursorXcFileLoadImages (XcursorFile *file, int size)
516+ XcursorFileHeader *fileHeader;
517+ XcursorDim bestSize;
519+ XcursorImages *images;
523+ if (!file || size < 0)
525+ fileHeader = _XcursorReadFileHeader (file);
528+ bestSize = _XcursorFindBestSize (fileHeader, (XcursorDim) size, &nsize);
531+ _XcursorFileHeaderDestroy (fileHeader);
534+ images = XcursorImagesCreate (nsize);
537+ _XcursorFileHeaderDestroy (fileHeader);
540+ for (n = 0; n < nsize; n++)
542+ toc = _XcursorFindImageToc (fileHeader, bestSize, n);
545+ images->images[images->nimage] = _XcursorReadImage (file, fileHeader,
547+ if (!images->images[images->nimage])
551+ _XcursorFileHeaderDestroy (fileHeader);
552+ if (images->nimage != nsize)
554+ XcursorImagesDestroy (images);
561+_XcursorStdioFileRead (XcursorFile *file, unsigned char *buf, int len)
563+ FILE *f = file->closure;
564+ return fread (buf, 1, len, f);
568+_XcursorStdioFileWrite (XcursorFile *file, unsigned char *buf, int len)
570+ FILE *f = file->closure;
571+ return fwrite (buf, 1, len, f);
575+_XcursorStdioFileSeek (XcursorFile *file, long offset, int whence)
577+ FILE *f = file->closure;
578+ return fseek (f, offset, whence);
582+_XcursorStdioFileInitialize (FILE *stdfile, XcursorFile *file)
584+ file->closure = stdfile;
585+ file->read = _XcursorStdioFileRead;
586+ file->write = _XcursorStdioFileWrite;
587+ file->seek = _XcursorStdioFileSeek;
590+static XcursorImages *
591+XcursorFileLoadImages (FILE *file, int size)
598+ _XcursorStdioFileInitialize (file, &f);
599+ return XcursorXcFileLoadImages (&f, size);
603+ * From libXcursor/src/library.c
607+#define ICONDIR "/usr/X11R6/lib/X11/icons"
611+#define XCURSORPATH "~/.icons:/usr/share/icons:/usr/share/pixmaps:~/.cursors:/usr/share/cursors/xorg-x11:"ICONDIR
615+XcursorLibraryPath (void)
617+ static const char *path;
621+ path = getenv ("XCURSOR_PATH");
629+_XcursorAddPathElt (char *path, const char *elt, int len)
631+ int pathlen = strlen (path);
633+ /* append / if the path doesn't currently have one */
634+ if (path[0] == '\0' || path[pathlen - 1] != '/')
641+ /* strip leading slashes */
642+ while (len && elt[0] == '/')
647+ strncpy (path + pathlen, elt, len);
648+ path[pathlen + len] = '\0';
652+_XcursorBuildThemeDir (const char *dir, const char *theme)
666+ colon = strchr (dir, ':');
668+ colon = dir + strlen (dir);
670+ dirlen = colon - dir;
672+ tcolon = strchr (theme, ':');
674+ tcolon = theme + strlen (theme);
676+ themelen = tcolon - theme;
682+ home = getenv ("HOME");
685+ homelen = strlen (home);
691+ * add space for any needed directory separators, one per component,
692+ * and one for the trailing null
694+ len = 1 + homelen + 1 + dirlen + 1 + themelen + 1;
696+ full = malloc (len);
702+ _XcursorAddPathElt (full, home, -1);
703+ _XcursorAddPathElt (full, dir, dirlen);
704+ _XcursorAddPathElt (full, theme, themelen);
709+_XcursorBuildFullname (const char *dir, const char *subdir, const char *file)
713+ if (!dir || !subdir || !file)
716+ full = malloc (strlen (dir) + 1 + strlen (subdir) + 1 + strlen (file) + 1);
720+ _XcursorAddPathElt (full, dir, -1);
721+ _XcursorAddPathElt (full, subdir, -1);
722+ _XcursorAddPathElt (full, file, -1);
727+_XcursorNextPath (const char *path)
729+ char *colon = strchr (path, ':');
736+#define XcursorWhite(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
737+#define XcursorSep(c) ((c) == ';' || (c) == ',')
740+_XcursorThemeInherits (const char *full)
743+ char *result = NULL;
749+ f = fopen (full, "r");
752+ while (fgets (line, sizeof (line), f))
754+ if (!strncmp (line, "Inherits", 8))
758+ while (*l == ' ') l++;
759+ if (*l != '=') continue;
761+ while (*l == ' ') l++;
762+ result = malloc (strlen (l) + 1);
768+ while (XcursorSep(*l) || XcursorWhite (*l)) l++;
773+ while (*l && !XcursorWhite(*l) &&
788+XcursorScanTheme (const char *theme, const char *name)
794+ char *inherits = NULL;
797+ if (!theme || !name)
803+ for (path = XcursorLibraryPath ();
805+ path = _XcursorNextPath (path))
807+ dir = _XcursorBuildThemeDir (path, theme);
810+ full = _XcursorBuildFullname (dir, "cursors", name);
813+ f = fopen (full, "r");
816+ if (!f && !inherits)
818+ full = _XcursorBuildFullname (dir, "", "index.theme");
821+ inherits = _XcursorThemeInherits (full);
829+ * Recurse to scan inherited themes
831+ for (i = inherits; i && f == NULL; i = _XcursorNextPath (i))
832+ f = XcursorScanTheme (i, name);
833+ if (inherits != NULL)
839+XcursorLibraryLoadImages (const char *file, const char *theme, int size)
842+ XcursorImages *images = NULL;
848+ f = XcursorScanTheme (theme, file);
850+ f = XcursorScanTheme ("default", file);
853+ images = XcursorFileLoadImages (f, size);
855+ XcursorImagesSetName (images, file);
862+load_all_cursors_from_dir(const char *path, int size,
863+ void (*load_callback)(XcursorImages *, void *),
867+ DIR *dir = opendir(path);
870+ XcursorImages *images;
875+ for(ent = readdir(dir); ent; ent = readdir(dir)) {
876+#ifdef _DIRENT_HAVE_D_TYPE
877+ if (ent->d_type != DT_UNKNOWN &&
878+ (ent->d_type != DT_REG && ent->d_type != DT_LNK))
882+ full = _XcursorBuildFullname(path, "", ent->d_name);
886+ f = fopen(full, "r");
892+ images = XcursorFileLoadImages(f, size);
895+ XcursorImagesSetName(images, ent->d_name);
896+ load_callback(images, user_data);
906+/** Load all the cursor of a theme
908+ * This function loads all the cursor images of a given theme and its
909+ * inherited themes. Each cursor is loaded into an XcursorImages object
910+ * which is passed to the caller's load callback. If a cursor appears
911+ * more than once across all the inherited themes, the load callback
912+ * will be called multiple times, with possibly different XcursorImages
913+ * object which have the same name. The user is expected to destroy the
914+ * XcursorImages objects passed to the callback with
915+ * XcursorImagesDestroy().
917+ * \param theme The name of theme that should be loaded
918+ * \param size The desired size of the cursor images
919+ * \param load_callback A callback function that will be called
920+ * for each cursor loaded. The first parameter is the XcursorImages
921+ * object representing the loaded cursor and the second is a pointer
922+ * to data provided by the user.
923+ * \param user_data The data that should be passed to the load callback
926+xcursor_load_theme(const char *theme, int size,
927+ void (*load_callback)(XcursorImages *, void *),
931+ char *inherits = NULL;
932+ const char *path, *i;
937+ for (path = XcursorLibraryPath();
939+ path = _XcursorNextPath(path)) {
940+ dir = _XcursorBuildThemeDir(path, theme);
944+ full = _XcursorBuildFullname(dir, "cursors", "");
947+ load_all_cursors_from_dir(full, size, load_callback,
953+ full = _XcursorBuildFullname(dir, "", "index.theme");
955+ inherits = _XcursorThemeInherits(full);
963+ for (i = inherits; i; i = _XcursorNextPath(i))
964+ xcursor_load_theme(i, size, load_callback, user_data);