aboutsummaryrefslogtreecommitdiffstats
path: root/cursor/xcursor.c
diff options
context:
space:
mode:
authorAnder Conselvan de Oliveira <ander.conselvan.de.oliveira@intel.com>2012-05-24 16:17:48 +0300
committerKristian Høgsberg <krh@bitplanet.net>2012-05-25 23:06:27 -0400
commitc66f26024b8ddc7b4b00b81e37945ad86b1a1fd8 (patch)
treea28e2857254d48bc1cd6ff9156f978a0d510e134 /cursor/xcursor.c
parentwayland-cursor: remove enum wl_cursor_type (diff)
downloadwayland-c66f26024b8ddc7b4b00b81e37945ad86b1a1fd8.tar
wayland-c66f26024b8ddc7b4b00b81e37945ad86b1a1fd8.tar.gz
wayland-c66f26024b8ddc7b4b00b81e37945ad86b1a1fd8.tar.bz2
wayland-c66f26024b8ddc7b4b00b81e37945ad86b1a1fd8.tar.lz
wayland-c66f26024b8ddc7b4b00b81e37945ad86b1a1fd8.tar.xz
wayland-c66f26024b8ddc7b4b00b81e37945ad86b1a1fd8.tar.zst
wayland-c66f26024b8ddc7b4b00b81e37945ad86b1a1fd8.zip
wayland-cursor: load all cursors from a theme on wl_cursor_theme_load
Diffstat (limited to 'cursor/xcursor.c')
-rw-r--r--cursor/xcursor.c107
1 files changed, 107 insertions, 0 deletions
diff --git a/cursor/xcursor.c b/cursor/xcursor.c
index 8b1199f..b3fa271 100644
--- a/cursor/xcursor.c
+++ b/cursor/xcursor.c
@@ -24,6 +24,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <dirent.h>
/*
* From libXcursor/include/X11/extensions/Xcursor.h
@@ -854,3 +855,109 @@ XcursorLibraryLoadImages (const char *file, const char *theme, int size)
}
return images;
}
+
+static void
+load_all_cursors_from_dir(const char *path, int size,
+ void (*load_callback)(XcursorImages *, void *),
+ void *user_data)
+{
+ FILE *f;
+ DIR *dir = opendir(path);
+ struct dirent *ent;
+ char *full;
+ XcursorImages *images;
+
+ if (!dir)
+ return;
+
+ ent = readdir(dir);
+ for(ent = readdir(dir); ent; ent = readdir(dir)) {
+#ifdef _DIRENT_HAVE_D_TYPE
+ if (ent->d_type != DT_REG && ent->d_type != DT_LNK)
+ continue;
+#endif
+
+ full = _XcursorBuildFullname(path, "", ent->d_name);
+ if (!full)
+ continue;
+
+ f = fopen(full, "r");
+ if (!f)
+ continue;
+
+ images = XcursorFileLoadImages(f, size);
+
+ if (images) {
+ XcursorImagesSetName(images, ent->d_name);
+ load_callback(images, user_data);
+ }
+
+ fclose (f);
+ }
+
+ closedir(dir);
+}
+
+/** Load all the cursor of a theme
+ *
+ * This function loads all the cursor images of a given theme and its
+ * inherited themes. Each cursor is loaded into an XcursorImages object
+ * which is passed to the caller's load callback. If a cursor appears
+ * more than once across all the inherited themes, the load callback
+ * will be called multiple times, with possibly different XcursorImages
+ * object which have the same name. The user is expected to destroy the
+ * XcursorImages objects passed to the callback with
+ * XcursorImagesDestroy().
+ *
+ * \param theme The name of theme that should be loaded
+ * \param size The desired size of the cursor images
+ * \param load_callback A callback function that will be called
+ * for each cursor loaded. The first parameter is the XcursorImages
+ * object representing the loaded cursor and the second is a pointer
+ * to data provided by the user.
+ * \param user_data The data that should be passed to the load callback
+ */
+void
+xcursor_load_theme(const char *theme, int size,
+ void (*load_callback)(XcursorImages *, void *),
+ void *user_data)
+{
+ char *full, *dir;
+ char *inherits = NULL;
+ const char *path, *i;
+
+ if (!theme)
+ theme = "default";
+
+ for (path = XcursorLibraryPath();
+ path;
+ path = _XcursorNextPath(path)) {
+ dir = _XcursorBuildThemeDir(path, theme);
+ if (!dir)
+ continue;
+
+ full = _XcursorBuildFullname(dir, "cursors", "");
+
+ if (full) {
+ load_all_cursors_from_dir(full, size, load_callback,
+ user_data);
+ free(full);
+ }
+
+ if (!inherits) {
+ full = _XcursorBuildFullname(dir, "", "index.theme");
+ if (full) {
+ inherits = _XcursorThemeInherits(full);
+ free(full);
+ }
+ }
+
+ free(dir);
+ }
+
+ for (i = inherits; i; i = _XcursorNextPath(i))
+ xcursor_load_theme(i, size, load_callback, user_data);
+
+ if (inherits)
+ free(inherits);
+}