• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2002 Keith Packard
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 #include "xcursor.h"
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <dirent.h>
31 
32 /*
33  * From libXcursor/include/X11/extensions/Xcursor.h
34  */
35 
36 #define XcursorTrue	1
37 #define XcursorFalse	0
38 
39 /*
40  * Cursor files start with a header.  The header
41  * contains a magic number, a version number and a
42  * table of contents which has type and offset information
43  * for the remaining tables in the file.
44  *
45  * File minor versions increment for compatible changes
46  * File major versions increment for incompatible changes (never, we hope)
47  *
48  * Chunks of the same type are always upward compatible.  Incompatible
49  * changes are made with new chunk types; the old data can remain under
50  * the old type.  Upward compatible changes can add header data as the
51  * header lengths are specified in the file.
52  *
53  *  File:
54  *	FileHeader
55  *	LISTofChunk
56  *
57  *  FileHeader:
58  *	CARD32		magic	    magic number
59  *	CARD32		header	    bytes in file header
60  *	CARD32		version	    file version
61  *	CARD32		ntoc	    number of toc entries
62  *	LISTofFileToc   toc	    table of contents
63  *
64  *  FileToc:
65  *	CARD32		type	    entry type
66  *	CARD32		subtype	    entry subtype (size for images)
67  *	CARD32		position    absolute file position
68  */
69 
70 #define XCURSOR_MAGIC	0x72756358  /* "Xcur" LSBFirst */
71 
72 /*
73  * Current Xcursor version number.  Will be substituted by configure
74  * from the version in the libXcursor configure.ac file.
75  */
76 
77 #define XCURSOR_LIB_MAJOR 1
78 #define XCURSOR_LIB_MINOR 1
79 #define XCURSOR_LIB_REVISION 13
80 #define XCURSOR_LIB_VERSION	((XCURSOR_LIB_MAJOR * 10000) + \
81 				 (XCURSOR_LIB_MINOR * 100) + \
82 				 (XCURSOR_LIB_REVISION))
83 
84 /*
85  * This version number is stored in cursor files; changes to the
86  * file format require updating this version number
87  */
88 #define XCURSOR_FILE_MAJOR	1
89 #define XCURSOR_FILE_MINOR	0
90 #define XCURSOR_FILE_VERSION	((XCURSOR_FILE_MAJOR << 16) | (XCURSOR_FILE_MINOR))
91 #define XCURSOR_FILE_HEADER_LEN	(4 * 4)
92 #define XCURSOR_FILE_TOC_LEN	(3 * 4)
93 
94 typedef struct _XcursorFileToc {
95     XcursorUInt	    type;	/* chunk type */
96     XcursorUInt	    subtype;	/* subtype (size for images) */
97     XcursorUInt	    position;	/* absolute position in file */
98 } XcursorFileToc;
99 
100 typedef struct _XcursorFileHeader {
101     XcursorUInt	    magic;	/* magic number */
102     XcursorUInt	    header;	/* byte length of header */
103     XcursorUInt	    version;	/* file version number */
104     XcursorUInt	    ntoc;	/* number of toc entries */
105     XcursorFileToc  *tocs;	/* table of contents */
106 } XcursorFileHeader;
107 
108 /*
109  * The rest of the file is a list of chunks, each tagged by type
110  * and version.
111  *
112  *  Chunk:
113  *	ChunkHeader
114  *	<extra type-specific header fields>
115  *	<type-specific data>
116  *
117  *  ChunkHeader:
118  *	CARD32	    header	bytes in chunk header + type header
119  *	CARD32	    type	chunk type
120  *	CARD32	    subtype	chunk subtype
121  *	CARD32	    version	chunk type version
122  */
123 
124 #define XCURSOR_CHUNK_HEADER_LEN    (4 * 4)
125 
126 typedef struct _XcursorChunkHeader {
127     XcursorUInt	    header;	/* bytes in chunk header */
128     XcursorUInt	    type;	/* chunk type */
129     XcursorUInt	    subtype;	/* chunk subtype (size for images) */
130     XcursorUInt	    version;	/* version of this type */
131 } XcursorChunkHeader;
132 
133 /*
134  * Here's a list of the known chunk types
135  */
136 
137 /*
138  * Comments consist of a 4-byte length field followed by
139  * UTF-8 encoded text
140  *
141  *  Comment:
142  *	ChunkHeader header	chunk header
143  *	CARD32	    length	bytes in text
144  *	LISTofCARD8 text	UTF-8 encoded text
145  */
146 
147 #define XCURSOR_COMMENT_TYPE	    0xfffe0001
148 #define XCURSOR_COMMENT_VERSION	    1
149 #define XCURSOR_COMMENT_HEADER_LEN  (XCURSOR_CHUNK_HEADER_LEN + (1 *4))
150 #define XCURSOR_COMMENT_COPYRIGHT   1
151 #define XCURSOR_COMMENT_LICENSE	    2
152 #define XCURSOR_COMMENT_OTHER	    3
153 #define XCURSOR_COMMENT_MAX_LEN	    0x100000
154 
155 typedef struct _XcursorComment {
156     XcursorUInt	    version;
157     XcursorUInt	    comment_type;
158     char	    *comment;
159 } XcursorComment;
160 
161 /*
162  * Each cursor image occupies a separate image chunk.
163  * The length of the image header follows the chunk header
164  * so that future versions can extend the header without
165  * breaking older applications
166  *
167  *  Image:
168  *	ChunkHeader	header	chunk header
169  *	CARD32		width	actual width
170  *	CARD32		height	actual height
171  *	CARD32		xhot	hot spot x
172  *	CARD32		yhot	hot spot y
173  *	CARD32		delay	animation delay
174  *	LISTofCARD32	pixels	ARGB pixels
175  */
176 
177 #define XCURSOR_IMAGE_TYPE    	    0xfffd0002
178 #define XCURSOR_IMAGE_VERSION	    1
179 #define XCURSOR_IMAGE_HEADER_LEN    (XCURSOR_CHUNK_HEADER_LEN + (5*4))
180 #define XCURSOR_IMAGE_MAX_SIZE	    0x7fff	/* 32767x32767 max cursor size */
181 
182 typedef struct _XcursorFile XcursorFile;
183 
184 struct _XcursorFile {
185     void    *closure;
186     int	    (*read)  (XcursorFile *file, unsigned char *buf, int len);
187     int	    (*write) (XcursorFile *file, unsigned char *buf, int len);
188     int	    (*seek)  (XcursorFile *file, long offset, int whence);
189 };
190 
191 typedef struct _XcursorComments {
192     int		    ncomment;	/* number of comments */
193     XcursorComment  **comments;	/* array of XcursorComment pointers */
194 } XcursorComments;
195 
196 /*
197  * From libXcursor/src/file.c
198  */
199 
200 static XcursorImage *
XcursorImageCreate(int width,int height)201 XcursorImageCreate (int width, int height)
202 {
203     XcursorImage    *image;
204 
205     if (width < 0 || height < 0)
206        return NULL;
207     if (width > XCURSOR_IMAGE_MAX_SIZE || height > XCURSOR_IMAGE_MAX_SIZE)
208        return NULL;
209 
210     image = malloc (sizeof (XcursorImage) +
211 		    width * height * sizeof (XcursorPixel));
212     if (!image)
213 	return NULL;
214     image->version = XCURSOR_IMAGE_VERSION;
215     image->pixels = (XcursorPixel *) (image + 1);
216     image->size = width > height ? width : height;
217     image->width = width;
218     image->height = height;
219     image->delay = 0;
220     return image;
221 }
222 
223 static void
XcursorImageDestroy(XcursorImage * image)224 XcursorImageDestroy (XcursorImage *image)
225 {
226     free (image);
227 }
228 
229 static XcursorImages *
XcursorImagesCreate(int size)230 XcursorImagesCreate (int size)
231 {
232     XcursorImages   *images;
233 
234     images = malloc (sizeof (XcursorImages) +
235 		     size * sizeof (XcursorImage *));
236     if (!images)
237 	return NULL;
238     images->nimage = 0;
239     images->images = (XcursorImage **) (images + 1);
240     images->name = NULL;
241     return images;
242 }
243 
244 void
XcursorImagesDestroy(XcursorImages * images)245 XcursorImagesDestroy (XcursorImages *images)
246 {
247     int	n;
248 
249     if (!images)
250         return;
251 
252     for (n = 0; n < images->nimage; n++)
253 	XcursorImageDestroy (images->images[n]);
254     if (images->name)
255 	free (images->name);
256     free (images);
257 }
258 
259 static void
XcursorImagesSetName(XcursorImages * images,const char * name)260 XcursorImagesSetName (XcursorImages *images, const char *name)
261 {
262     char    *new;
263 
264     if (!images || !name)
265         return;
266 
267     new = malloc (strlen (name) + 1);
268 
269     if (!new)
270 	return;
271 
272     strcpy (new, name);
273     if (images->name)
274 	free (images->name);
275     images->name = new;
276 }
277 
278 static XcursorBool
_XcursorReadUInt(XcursorFile * file,XcursorUInt * u)279 _XcursorReadUInt (XcursorFile *file, XcursorUInt *u)
280 {
281     unsigned char   bytes[4];
282 
283     if (!file || !u)
284         return XcursorFalse;
285 
286     if ((*file->read) (file, bytes, 4) != 4)
287 	return XcursorFalse;
288 
289     *u = ((XcursorUInt)(bytes[0]) << 0) |
290          ((XcursorUInt)(bytes[1]) << 8) |
291          ((XcursorUInt)(bytes[2]) << 16) |
292          ((XcursorUInt)(bytes[3]) << 24);
293     return XcursorTrue;
294 }
295 
296 static void
_XcursorFileHeaderDestroy(XcursorFileHeader * fileHeader)297 _XcursorFileHeaderDestroy (XcursorFileHeader *fileHeader)
298 {
299     free (fileHeader);
300 }
301 
302 static XcursorFileHeader *
_XcursorFileHeaderCreate(int ntoc)303 _XcursorFileHeaderCreate (int ntoc)
304 {
305     XcursorFileHeader	*fileHeader;
306 
307     if (ntoc > 0x10000)
308 	return NULL;
309     fileHeader = malloc (sizeof (XcursorFileHeader) +
310 			 ntoc * sizeof (XcursorFileToc));
311     if (!fileHeader)
312 	return NULL;
313     fileHeader->magic = XCURSOR_MAGIC;
314     fileHeader->header = XCURSOR_FILE_HEADER_LEN;
315     fileHeader->version = XCURSOR_FILE_VERSION;
316     fileHeader->ntoc = ntoc;
317     fileHeader->tocs = (XcursorFileToc *) (fileHeader + 1);
318     return fileHeader;
319 }
320 
321 static XcursorFileHeader *
_XcursorReadFileHeader(XcursorFile * file)322 _XcursorReadFileHeader (XcursorFile *file)
323 {
324     XcursorFileHeader	head, *fileHeader;
325     XcursorUInt		skip;
326     unsigned int	n;
327 
328     if (!file)
329         return NULL;
330 
331     if (!_XcursorReadUInt (file, &head.magic))
332 	return NULL;
333     if (head.magic != XCURSOR_MAGIC)
334 	return NULL;
335     if (!_XcursorReadUInt (file, &head.header))
336 	return NULL;
337     if (!_XcursorReadUInt (file, &head.version))
338 	return NULL;
339     if (!_XcursorReadUInt (file, &head.ntoc))
340 	return NULL;
341     skip = head.header - XCURSOR_FILE_HEADER_LEN;
342     if (skip)
343 	if ((*file->seek) (file, skip, SEEK_CUR) == EOF)
344 	    return NULL;
345     fileHeader = _XcursorFileHeaderCreate (head.ntoc);
346     if (!fileHeader)
347 	return NULL;
348     fileHeader->magic = head.magic;
349     fileHeader->header = head.header;
350     fileHeader->version = head.version;
351     fileHeader->ntoc = head.ntoc;
352     for (n = 0; n < fileHeader->ntoc; n++)
353     {
354 	if (!_XcursorReadUInt (file, &fileHeader->tocs[n].type))
355 	    break;
356 	if (!_XcursorReadUInt (file, &fileHeader->tocs[n].subtype))
357 	    break;
358 	if (!_XcursorReadUInt (file, &fileHeader->tocs[n].position))
359 	    break;
360     }
361     if (n != fileHeader->ntoc)
362     {
363 	_XcursorFileHeaderDestroy (fileHeader);
364 	return NULL;
365     }
366     return fileHeader;
367 }
368 
369 static XcursorBool
_XcursorSeekToToc(XcursorFile * file,XcursorFileHeader * fileHeader,int toc)370 _XcursorSeekToToc (XcursorFile		*file,
371 		   XcursorFileHeader	*fileHeader,
372 		   int			toc)
373 {
374     if (!file || !fileHeader || \
375         (*file->seek) (file, fileHeader->tocs[toc].position, SEEK_SET) == EOF)
376 	return XcursorFalse;
377     return XcursorTrue;
378 }
379 
380 static XcursorBool
_XcursorFileReadChunkHeader(XcursorFile * file,XcursorFileHeader * fileHeader,int toc,XcursorChunkHeader * chunkHeader)381 _XcursorFileReadChunkHeader (XcursorFile	*file,
382 			     XcursorFileHeader	*fileHeader,
383 			     int		toc,
384 			     XcursorChunkHeader	*chunkHeader)
385 {
386     if (!file || !fileHeader || !chunkHeader)
387         return XcursorFalse;
388     if (!_XcursorSeekToToc (file, fileHeader, toc))
389 	return XcursorFalse;
390     if (!_XcursorReadUInt (file, &chunkHeader->header))
391 	return XcursorFalse;
392     if (!_XcursorReadUInt (file, &chunkHeader->type))
393 	return XcursorFalse;
394     if (!_XcursorReadUInt (file, &chunkHeader->subtype))
395 	return XcursorFalse;
396     if (!_XcursorReadUInt (file, &chunkHeader->version))
397 	return XcursorFalse;
398     /* sanity check */
399     if (chunkHeader->type != fileHeader->tocs[toc].type ||
400 	chunkHeader->subtype != fileHeader->tocs[toc].subtype)
401 	return XcursorFalse;
402     return XcursorTrue;
403 }
404 
405 #define dist(a,b)   ((a) > (b) ? (a) - (b) : (b) - (a))
406 
407 static XcursorDim
_XcursorFindBestSize(XcursorFileHeader * fileHeader,XcursorDim size,int * nsizesp)408 _XcursorFindBestSize (XcursorFileHeader *fileHeader,
409 		      XcursorDim	size,
410 		      int		*nsizesp)
411 {
412     unsigned int n;
413     int		nsizes = 0;
414     XcursorDim	bestSize = 0;
415     XcursorDim	thisSize;
416 
417     if (!fileHeader || !nsizesp)
418         return 0;
419 
420     for (n = 0; n < fileHeader->ntoc; n++)
421     {
422 	if (fileHeader->tocs[n].type != XCURSOR_IMAGE_TYPE)
423 	    continue;
424 	thisSize = fileHeader->tocs[n].subtype;
425 	if (!bestSize || dist (thisSize, size) < dist (bestSize, size))
426 	{
427 	    bestSize = thisSize;
428 	    nsizes = 1;
429 	}
430 	else if (thisSize == bestSize)
431 	    nsizes++;
432     }
433     *nsizesp = nsizes;
434     return bestSize;
435 }
436 
437 static int
_XcursorFindImageToc(XcursorFileHeader * fileHeader,XcursorDim size,int count)438 _XcursorFindImageToc (XcursorFileHeader	*fileHeader,
439 		      XcursorDim	size,
440 		      int		count)
441 {
442     unsigned int	toc;
443     XcursorDim		thisSize;
444 
445     if (!fileHeader)
446         return 0;
447 
448     for (toc = 0; toc < fileHeader->ntoc; toc++)
449     {
450 	if (fileHeader->tocs[toc].type != XCURSOR_IMAGE_TYPE)
451 	    continue;
452 	thisSize = fileHeader->tocs[toc].subtype;
453 	if (thisSize != size)
454 	    continue;
455 	if (!count)
456 	    break;
457 	count--;
458     }
459     if (toc == fileHeader->ntoc)
460 	return -1;
461     return toc;
462 }
463 
464 static XcursorImage *
_XcursorReadImage(XcursorFile * file,XcursorFileHeader * fileHeader,int toc)465 _XcursorReadImage (XcursorFile		*file,
466 		   XcursorFileHeader	*fileHeader,
467 		   int			toc)
468 {
469     XcursorChunkHeader	chunkHeader;
470     XcursorImage	head;
471     XcursorImage	*image;
472     int			n;
473     XcursorPixel	*p;
474 
475     if (!file || !fileHeader)
476         return NULL;
477 
478     if (!_XcursorFileReadChunkHeader (file, fileHeader, toc, &chunkHeader))
479 	return NULL;
480     if (!_XcursorReadUInt (file, &head.width))
481 	return NULL;
482     if (!_XcursorReadUInt (file, &head.height))
483 	return NULL;
484     if (!_XcursorReadUInt (file, &head.xhot))
485 	return NULL;
486     if (!_XcursorReadUInt (file, &head.yhot))
487 	return NULL;
488     if (!_XcursorReadUInt (file, &head.delay))
489 	return NULL;
490     /* sanity check data */
491     if (head.width > XCURSOR_IMAGE_MAX_SIZE  ||
492 	head.height > XCURSOR_IMAGE_MAX_SIZE)
493 	return NULL;
494     if (head.width == 0 || head.height == 0)
495 	return NULL;
496     if (head.xhot > head.width || head.yhot > head.height)
497 	return NULL;
498 
499     /* Create the image and initialize it */
500     image = XcursorImageCreate (head.width, head.height);
501     if (image == NULL)
502 	    return NULL;
503     if (chunkHeader.version < image->version)
504 	image->version = chunkHeader.version;
505     image->size = chunkHeader.subtype;
506     image->xhot = head.xhot;
507     image->yhot = head.yhot;
508     image->delay = head.delay;
509     n = image->width * image->height;
510     p = image->pixels;
511     while (n--)
512     {
513 	if (!_XcursorReadUInt (file, p))
514 	{
515 	    XcursorImageDestroy (image);
516 	    return NULL;
517 	}
518 	p++;
519     }
520     return image;
521 }
522 
523 static XcursorImages *
XcursorXcFileLoadImages(XcursorFile * file,int size)524 XcursorXcFileLoadImages (XcursorFile *file, int size)
525 {
526     XcursorFileHeader	*fileHeader;
527     XcursorDim		bestSize;
528     int			nsize;
529     XcursorImages	*images;
530     int			n;
531     int			toc;
532 
533     if (!file || size < 0)
534 	return NULL;
535     fileHeader = _XcursorReadFileHeader (file);
536     if (!fileHeader)
537 	return NULL;
538     bestSize = _XcursorFindBestSize (fileHeader, (XcursorDim) size, &nsize);
539     if (!bestSize)
540     {
541         _XcursorFileHeaderDestroy (fileHeader);
542 	return NULL;
543     }
544     images = XcursorImagesCreate (nsize);
545     if (!images)
546     {
547         _XcursorFileHeaderDestroy (fileHeader);
548 	return NULL;
549     }
550     for (n = 0; n < nsize; n++)
551     {
552 	toc = _XcursorFindImageToc (fileHeader, bestSize, n);
553 	if (toc < 0)
554 	    break;
555 	images->images[images->nimage] = _XcursorReadImage (file, fileHeader,
556 							    toc);
557 	if (!images->images[images->nimage])
558 	    break;
559 	images->nimage++;
560     }
561     _XcursorFileHeaderDestroy (fileHeader);
562     if (images->nimage != nsize)
563     {
564 	XcursorImagesDestroy (images);
565 	images = NULL;
566     }
567     return images;
568 }
569 
570 static int
_XcursorStdioFileRead(XcursorFile * file,unsigned char * buf,int len)571 _XcursorStdioFileRead (XcursorFile *file, unsigned char *buf, int len)
572 {
573     FILE    *f = file->closure;
574     return fread (buf, 1, len, f);
575 }
576 
577 static int
_XcursorStdioFileWrite(XcursorFile * file,unsigned char * buf,int len)578 _XcursorStdioFileWrite (XcursorFile *file, unsigned char *buf, int len)
579 {
580     FILE    *f = file->closure;
581     return fwrite (buf, 1, len, f);
582 }
583 
584 static int
_XcursorStdioFileSeek(XcursorFile * file,long offset,int whence)585 _XcursorStdioFileSeek (XcursorFile *file, long offset, int whence)
586 {
587     FILE    *f = file->closure;
588     return fseek (f, offset, whence);
589 }
590 
591 static void
_XcursorStdioFileInitialize(FILE * stdfile,XcursorFile * file)592 _XcursorStdioFileInitialize (FILE *stdfile, XcursorFile *file)
593 {
594     file->closure = stdfile;
595     file->read = _XcursorStdioFileRead;
596     file->write = _XcursorStdioFileWrite;
597     file->seek = _XcursorStdioFileSeek;
598 }
599 
600 static XcursorImages *
XcursorFileLoadImages(FILE * file,int size)601 XcursorFileLoadImages (FILE *file, int size)
602 {
603     XcursorFile	f;
604 
605     if (!file)
606         return NULL;
607 
608     _XcursorStdioFileInitialize (file, &f);
609     return XcursorXcFileLoadImages (&f, size);
610 }
611 
612 /*
613  * From libXcursor/src/library.c
614  */
615 
616 #ifndef ICONDIR
617 #define ICONDIR "/usr/X11R6/lib/X11/icons"
618 #endif
619 
620 #ifndef XCURSORPATH
621 #define XCURSORPATH "~/.icons:/usr/share/icons:/usr/share/pixmaps:~/.cursors:/usr/share/cursors/xorg-x11:"ICONDIR
622 #endif
623 
624 static const char *
XcursorLibraryPath(void)625 XcursorLibraryPath (void)
626 {
627     static const char	*path;
628 
629     if (!path)
630     {
631 	path = getenv ("XCURSOR_PATH");
632 	if (!path)
633 	    path = XCURSORPATH;
634     }
635     return path;
636 }
637 
638 static  void
_XcursorAddPathElt(char * path,const char * elt,int len)639 _XcursorAddPathElt (char *path, const char *elt, int len)
640 {
641     int	    pathlen = strlen (path);
642 
643     /* append / if the path doesn't currently have one */
644     if (path[0] == '\0' || path[pathlen - 1] != '/')
645     {
646 	strcat (path, "/");
647 	pathlen++;
648     }
649     if (len == -1)
650 	len = strlen (elt);
651     /* strip leading slashes */
652     while (len && elt[0] == '/')
653     {
654 	elt++;
655 	len--;
656     }
657     strncpy (path + pathlen, elt, len);
658     path[pathlen + len] = '\0';
659 }
660 
661 static char *
_XcursorBuildThemeDir(const char * dir,const char * theme)662 _XcursorBuildThemeDir (const char *dir, const char *theme)
663 {
664     const char	    *colon;
665     const char	    *tcolon;
666     char	    *full;
667     char	    *home;
668     int		    dirlen;
669     int		    homelen;
670     int		    themelen;
671     int		    len;
672 
673     if (!dir || !theme)
674         return NULL;
675 
676     colon = strchr (dir, ':');
677     if (!colon)
678 	colon = dir + strlen (dir);
679 
680     dirlen = colon - dir;
681 
682     tcolon = strchr (theme, ':');
683     if (!tcolon)
684 	tcolon = theme + strlen (theme);
685 
686     themelen = tcolon - theme;
687 
688     home = NULL;
689     homelen = 0;
690     if (*dir == '~')
691     {
692 	home = getenv ("HOME");
693 	if (!home)
694 	    return NULL;
695 	homelen = strlen (home);
696 	dir++;
697 	dirlen--;
698     }
699 
700     /*
701      * add space for any needed directory separators, one per component,
702      * and one for the trailing null
703      */
704     len = 1 + homelen + 1 + dirlen + 1 + themelen + 1;
705 
706     full = malloc (len);
707     if (!full)
708 	return NULL;
709     full[0] = '\0';
710 
711     if (home)
712 	_XcursorAddPathElt (full, home, -1);
713     _XcursorAddPathElt (full, dir, dirlen);
714     _XcursorAddPathElt (full, theme, themelen);
715     return full;
716 }
717 
718 static char *
_XcursorBuildFullname(const char * dir,const char * subdir,const char * file)719 _XcursorBuildFullname (const char *dir, const char *subdir, const char *file)
720 {
721     char    *full;
722 
723     if (!dir || !subdir || !file)
724         return NULL;
725 
726     full = malloc (strlen (dir) + 1 + strlen (subdir) + 1 + strlen (file) + 1);
727     if (!full)
728 	return NULL;
729     full[0] = '\0';
730     _XcursorAddPathElt (full, dir, -1);
731     _XcursorAddPathElt (full, subdir, -1);
732     _XcursorAddPathElt (full, file, -1);
733     return full;
734 }
735 
736 static const char *
_XcursorNextPath(const char * path)737 _XcursorNextPath (const char *path)
738 {
739     char    *colon = strchr (path, ':');
740 
741     if (!colon)
742 	return NULL;
743     return colon + 1;
744 }
745 
746 #define XcursorWhite(c)	((c) == ' ' || (c) == '\t' || (c) == '\n')
747 #define XcursorSep(c) ((c) == ';' || (c) == ',')
748 
749 static char *
_XcursorThemeInherits(const char * full)750 _XcursorThemeInherits (const char *full)
751 {
752     char    line[8192];
753     char    *result = NULL;
754     FILE    *f;
755 
756     if (!full)
757         return NULL;
758 
759     f = fopen (full, "r");
760     if (f)
761     {
762 	while (fgets (line, sizeof (line), f))
763 	{
764 	    if (!strncmp (line, "Inherits", 8))
765 	    {
766 		char    *l = line + 8;
767 		char    *r;
768 		while (*l == ' ') l++;
769 		if (*l != '=') continue;
770 		l++;
771 		while (*l == ' ') l++;
772 		result = malloc (strlen (l) + 1);
773 		if (result)
774 		{
775 		    r = result;
776 		    while (*l)
777 		    {
778 			while (XcursorSep(*l) || XcursorWhite (*l)) l++;
779 			if (!*l)
780 			    break;
781 			if (r != result)
782 			    *r++ = ':';
783 			while (*l && !XcursorWhite(*l) &&
784 			       !XcursorSep(*l))
785 			    *r++ = *l++;
786 		    }
787 		    *r++ = '\0';
788 		}
789 		break;
790 	    }
791 	}
792 	fclose (f);
793     }
794     return result;
795 }
796 
797 static FILE *
XcursorScanTheme(const char * theme,const char * name)798 XcursorScanTheme (const char *theme, const char *name)
799 {
800     FILE	*f = NULL;
801     char	*full;
802     char	*dir;
803     const char  *path;
804     char	*inherits = NULL;
805     const char	*i;
806 
807     if (!theme || !name)
808         return NULL;
809 
810     /*
811      * Scan this theme
812      */
813     for (path = XcursorLibraryPath ();
814 	 path && f == NULL;
815 	 path = _XcursorNextPath (path))
816     {
817 	dir = _XcursorBuildThemeDir (path, theme);
818 	if (dir)
819 	{
820 	    full = _XcursorBuildFullname (dir, "cursors", name);
821 	    if (full)
822 	    {
823 		f = fopen (full, "r");
824 		free (full);
825 	    }
826 	    if (!f && !inherits)
827 	    {
828 		full = _XcursorBuildFullname (dir, "", "index.theme");
829 		if (full)
830 		{
831 		    inherits = _XcursorThemeInherits (full);
832 		    free (full);
833 		}
834 	    }
835 	    free (dir);
836 	}
837     }
838     /*
839      * Recurse to scan inherited themes
840      */
841     for (i = inherits; i && f == NULL; i = _XcursorNextPath (i))
842 	f = XcursorScanTheme (i, name);
843     if (inherits != NULL)
844 	free (inherits);
845     return f;
846 }
847 
848 XcursorImages *
XcursorLibraryLoadImages(const char * file,const char * theme,int size)849 XcursorLibraryLoadImages (const char *file, const char *theme, int size)
850 {
851     FILE	    *f = NULL;
852     XcursorImages   *images = NULL;
853 
854     if (!file)
855         return NULL;
856 
857     if (theme)
858 	f = XcursorScanTheme (theme, file);
859     if (!f)
860 	f = XcursorScanTheme ("default", file);
861     if (f)
862     {
863 	images = XcursorFileLoadImages (f, size);
864 	if (images)
865 	    XcursorImagesSetName (images, file);
866 	fclose (f);
867     }
868     return images;
869 }
870 
871 static void
load_all_cursors_from_dir(const char * path,int size,void (* load_callback)(XcursorImages *,void *),void * user_data)872 load_all_cursors_from_dir(const char *path, int size,
873 			  void (*load_callback)(XcursorImages *, void *),
874 			  void *user_data)
875 {
876 	FILE *f;
877 	DIR *dir = opendir(path);
878 	struct dirent *ent;
879 	char *full;
880 	XcursorImages *images;
881 
882 	if (!dir)
883 		return;
884 
885 	for(ent = readdir(dir); ent; ent = readdir(dir)) {
886 #ifdef _DIRENT_HAVE_D_TYPE
887 		if (ent->d_type != DT_UNKNOWN &&
888 		    (ent->d_type != DT_REG && ent->d_type != DT_LNK))
889 			continue;
890 #endif
891 
892 		full = _XcursorBuildFullname(path, "", ent->d_name);
893 		if (!full)
894 			continue;
895 
896 		f = fopen(full, "r");
897 		if (!f) {
898 			free(full);
899 			continue;
900 		}
901 
902 		images = XcursorFileLoadImages(f, size);
903 
904 		if (images) {
905 			XcursorImagesSetName(images, ent->d_name);
906 			load_callback(images, user_data);
907 		}
908 
909 		fclose (f);
910 		free(full);
911 	}
912 
913 	closedir(dir);
914 }
915 
916 /** Load all the cursor of a theme
917  *
918  * This function loads all the cursor images of a given theme and its
919  * inherited themes. Each cursor is loaded into an XcursorImages object
920  * which is passed to the caller's load callback. If a cursor appears
921  * more than once across all the inherited themes, the load callback
922  * will be called multiple times, with possibly different XcursorImages
923  * object which have the same name. The user is expected to destroy the
924  * XcursorImages objects passed to the callback with
925  * XcursorImagesDestroy().
926  *
927  * \param theme The name of theme that should be loaded
928  * \param size The desired size of the cursor images
929  * \param load_callback A callback function that will be called
930  * for each cursor loaded. The first parameter is the XcursorImages
931  * object representing the loaded cursor and the second is a pointer
932  * to data provided by the user.
933  * \param user_data The data that should be passed to the load callback
934  */
935 void
xcursor_load_theme(const char * theme,int size,void (* load_callback)(XcursorImages *,void *),void * user_data)936 xcursor_load_theme(const char *theme, int size,
937 		    void (*load_callback)(XcursorImages *, void *),
938 		    void *user_data)
939 {
940 	char *full, *dir;
941 	char *inherits = NULL;
942 	const char *path, *i;
943 
944 	if (!theme)
945 		theme = "default";
946 
947 	for (path = XcursorLibraryPath();
948 	     path;
949 	     path = _XcursorNextPath(path)) {
950 		dir = _XcursorBuildThemeDir(path, theme);
951 		if (!dir)
952 			continue;
953 
954 		full = _XcursorBuildFullname(dir, "cursors", "");
955 
956 		if (full) {
957 			load_all_cursors_from_dir(full, size, load_callback,
958 						  user_data);
959 			free(full);
960 		}
961 
962 		if (!inherits) {
963 			full = _XcursorBuildFullname(dir, "", "index.theme");
964 			if (full) {
965 				inherits = _XcursorThemeInherits(full);
966 				free(full);
967 			}
968 		}
969 
970 		free(dir);
971 	}
972 
973 	for (i = inherits; i; i = _XcursorNextPath(i))
974 		xcursor_load_theme(i, size, load_callback, user_data);
975 
976 	if (inherits)
977 		free(inherits);
978 }
979