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
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 {
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)
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);
237 XcursorImagesDestroy (XcursorImages *images)
244 for (n = 0; n < images->nimage; n++)
245 XcursorImageDestroy (images->images[n]);
252 XcursorImagesSetName (XcursorImages *images, const char *name)
256 if (!images || !name)
259 new = malloc (strlen (name) + 1);
271 _XcursorReadUInt (XcursorFile *file, XcursorUInt *u)
273 unsigned char bytes[4];
278 if ((*file->read) (file, bytes, 4) != 4)
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)
372 _XcursorFileReadChunkHeader (XcursorFile *file,
373 XcursorFileHeader *fileHeader,
375 XcursorChunkHeader *chunkHeader)
377 if (!file || !fileHeader || !chunkHeader)
379 if (!_XcursorSeekToToc (file, fileHeader, toc))
381 if (!_XcursorReadUInt (file, &chunkHeader->header))
383 if (!_XcursorReadUInt (file, &chunkHeader->type))
385 if (!_XcursorReadUInt (file, &chunkHeader->subtype))
387 if (!_XcursorReadUInt (file, &chunkHeader->version))
390 if (chunkHeader->type != fileHeader->tocs[toc].type ||
391 chunkHeader->subtype != fileHeader->tocs[toc].subtype)
396 #define dist(a,b) ((a) > (b) ? (a) - (b) : (b) - (a))
399 _XcursorFindBestSize (XcursorFileHeader *fileHeader,
405 XcursorDim bestSize = 0;
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))
421 else if (thisSize == bestSize)
429 _XcursorFindImageToc (XcursorFileHeader *fileHeader,
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;
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);
491 if (chunkHeader.version < image->version)
492 image->version = chunkHeader.version;
493 image->size = chunkHeader.subtype;
494 image->xhot = head.xhot;
495 image->yhot = head.yhot;
496 image->delay = head.delay;
497 n = image->width * image->height;
501 if (!_XcursorReadUInt (file, p))
503 XcursorImageDestroy (image);
511 static XcursorImages *
512 XcursorXcFileLoadImages (XcursorFile *file, int size)
514 XcursorFileHeader *fileHeader;
517 XcursorImages *images;
521 if (!file || size < 0)
523 fileHeader = _XcursorReadFileHeader (file);
526 bestSize = _XcursorFindBestSize (fileHeader, (XcursorDim) size, &nsize);
529 _XcursorFileHeaderDestroy (fileHeader);
532 images = XcursorImagesCreate (nsize);
535 _XcursorFileHeaderDestroy (fileHeader);
538 for (n = 0; n < nsize; n++)
540 toc = _XcursorFindImageToc (fileHeader, bestSize, n);
543 images->images[images->nimage] = _XcursorReadImage (file, fileHeader,
545 if (!images->images[images->nimage])
549 _XcursorFileHeaderDestroy (fileHeader);
550 if (images->nimage != nsize)
552 XcursorImagesDestroy (images);
559 _XcursorStdioFileRead (XcursorFile *file, unsigned char *buf, int len)
561 FILE *f = file->closure;
562 return fread (buf, 1, len, f);
566 _XcursorStdioFileWrite (XcursorFile *file, unsigned char *buf, int len)
568 FILE *f = file->closure;
569 return fwrite (buf, 1, len, f);
573 _XcursorStdioFileSeek (XcursorFile *file, long offset, int whence)
575 FILE *f = file->closure;
576 return fseek (f, offset, whence);
580 _XcursorStdioFileInitialize (FILE *stdfile, XcursorFile *file)
582 file->closure = stdfile;
583 file->read = _XcursorStdioFileRead;
584 file->write = _XcursorStdioFileWrite;
585 file->seek = _XcursorStdioFileSeek;
588 static XcursorImages *
589 XcursorFileLoadImages (FILE *file, int size)
596 _XcursorStdioFileInitialize (file, &f);
597 return XcursorXcFileLoadImages (&f, size);
601 * From libXcursor/src/library.c
605 #define ICONDIR "/usr/X11R6/lib/X11/icons"
609 #define XCURSORPATH "~/.icons:/usr/share/icons:/usr/share/pixmaps:~/.cursors:/usr/share/cursors/xorg-x11:"ICONDIR
613 XcursorLibraryPath (void)
615 static const char *path;
619 path = getenv ("XCURSOR_PATH");
627 _XcursorAddPathElt (char *path, const char *elt, int len)
629 int pathlen = strlen (path);
631 /* append / if the path doesn't currently have one */
632 if (path[0] == '\0' || path[pathlen - 1] != '/')
639 /* strip leading slashes */
640 while (len && elt[0] == '/')
645 strncpy (path + pathlen, elt, len);
646 path[pathlen + len] = '\0';
650 _XcursorBuildThemeDir (const char *dir, const char *theme)
664 colon = strchr (dir, ':');
666 colon = dir + strlen (dir);
668 dirlen = colon - dir;
670 tcolon = strchr (theme, ':');
672 tcolon = theme + strlen (theme);
674 themelen = tcolon - theme;
680 home = getenv ("HOME");
683 homelen = strlen (home);
689 * add space for any needed directory separators, one per component,
690 * and one for the trailing null
692 len = 1 + homelen + 1 + dirlen + 1 + themelen + 1;
700 _XcursorAddPathElt (full, home, -1);
701 _XcursorAddPathElt (full, dir, dirlen);
702 _XcursorAddPathElt (full, theme, themelen);
707 _XcursorBuildFullname (const char *dir, const char *subdir, const char *file)
711 if (!dir || !subdir || !file)
714 full = malloc (strlen (dir) + 1 + strlen (subdir) + 1 + strlen (file) + 1);
718 _XcursorAddPathElt (full, dir, -1);
719 _XcursorAddPathElt (full, subdir, -1);
720 _XcursorAddPathElt (full, file, -1);
725 _XcursorNextPath (const char *path)
727 char *colon = strchr (path, ':');
734 #define XcursorWhite(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
735 #define XcursorSep(c) ((c) == ';' || (c) == ',')
738 _XcursorThemeInherits (const char *full)
747 f = fopen (full, "r");
750 while (fgets (line, sizeof (line), f))
752 if (!strncmp (line, "Inherits", 8))
756 while (*l == ' ') l++;
757 if (*l != '=') continue;
759 while (*l == ' ') l++;
760 result = malloc (strlen (l) + 1);
766 while (XcursorSep(*l) || XcursorWhite (*l)) l++;
771 while (*l && !XcursorWhite(*l) &&
786 XcursorScanTheme (const char *theme, const char *name)
792 char *inherits = NULL;
801 for (path = XcursorLibraryPath ();
803 path = _XcursorNextPath (path))
805 dir = _XcursorBuildThemeDir (path, theme);
808 full = _XcursorBuildFullname (dir, "cursors", name);
811 f = fopen (full, "r");
816 full = _XcursorBuildFullname (dir, "", "index.theme");
819 inherits = _XcursorThemeInherits (full);
827 * Recurse to scan inherited themes
829 for (i = inherits; i && f == NULL; i = _XcursorNextPath (i))
830 f = XcursorScanTheme (i, name);
831 if (inherits != NULL)
837 XcursorLibraryLoadImages (const char *file, const char *theme, int size)
840 XcursorImages *images = NULL;
846 f = XcursorScanTheme (theme, file);
848 f = XcursorScanTheme ("default", file);
851 images = XcursorFileLoadImages (f, size);
853 XcursorImagesSetName (images, file);
860 load_all_cursors_from_dir(const char *path, int size,
861 void (*load_callback)(XcursorImages *, void *),
865 DIR *dir = opendir(path);
868 XcursorImages *images;
873 for(ent = readdir(dir); ent; ent = readdir(dir)) {
874 #ifdef _DIRENT_HAVE_D_TYPE
875 if (ent->d_type != DT_UNKNOWN &&
876 (ent->d_type != DT_REG && ent->d_type != DT_LNK))
880 full = _XcursorBuildFullname(path, "", ent->d_name);
884 f = fopen(full, "r");
890 images = XcursorFileLoadImages(f, size);
893 XcursorImagesSetName(images, ent->d_name);
894 load_callback(images, user_data);
904 /** Load all the cursor of a theme
906 * This function loads all the cursor images of a given theme and its
907 * inherited themes. Each cursor is loaded into an XcursorImages object
908 * which is passed to the caller's load callback. If a cursor appears
909 * more than once across all the inherited themes, the load callback
910 * will be called multiple times, with possibly different XcursorImages
911 * object which have the same name. The user is expected to destroy the
912 * XcursorImages objects passed to the callback with
913 * XcursorImagesDestroy().
915 * \param theme The name of theme that should be loaded
916 * \param size The desired size of the cursor images
917 * \param load_callback A callback function that will be called
918 * for each cursor loaded. The first parameter is the XcursorImages
919 * object representing the loaded cursor and the second is a pointer
920 * to data provided by the user.
921 * \param user_data The data that should be passed to the load callback
924 xcursor_load_theme(const char *theme, int size,
925 void (*load_callback)(XcursorImages *, void *),
929 char *inherits = NULL;
930 const char *path, *i;
935 for (path = XcursorLibraryPath();
937 path = _XcursorNextPath(path)) {
938 dir = _XcursorBuildThemeDir(path, theme);
942 full = _XcursorBuildFullname(dir, "cursors", "");
945 load_all_cursors_from_dir(full, size, load_callback,
951 full = _XcursorBuildFullname(dir, "", "index.theme");
953 inherits = _XcursorThemeInherits(full);
961 for (i = inherits; i; i = _XcursorNextPath(i))
962 xcursor_load_theme(i, size, load_callback, user_data);