mirror of
https://gitlab.freedesktop.org/wayland/wayland.git
synced 2026-03-15 05:34:36 -04:00
cursor: Add function for calculating current frame in cursor animation
Given a pointer to struct wl_cursor and an elapsed time in ms., wl_cursor_frame() will return the index of the cursor image the should be displayed.
This commit is contained in:
parent
b6aa49455e
commit
6b861f7b4d
2 changed files with 45 additions and 7 deletions
|
|
@ -129,6 +129,11 @@ struct cursor_image {
|
||||||
int offset; /* data offset of this image in the shm pool */
|
int offset; /* data offset of this image in the shm pool */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct cursor {
|
||||||
|
struct wl_cursor cursor;
|
||||||
|
uint32_t total_delay; /* length of the animation in ms */
|
||||||
|
};
|
||||||
|
|
||||||
/** Get an shm buffer for a cursor image
|
/** Get an shm buffer for a cursor image
|
||||||
*
|
*
|
||||||
* \param image The cursor image
|
* \param image The cursor image
|
||||||
|
|
@ -180,7 +185,7 @@ static struct wl_cursor *
|
||||||
wl_cursor_create_from_xcursor_images(XcursorImages *images,
|
wl_cursor_create_from_xcursor_images(XcursorImages *images,
|
||||||
struct wl_cursor_theme *theme)
|
struct wl_cursor_theme *theme)
|
||||||
{
|
{
|
||||||
struct wl_cursor *cursor;
|
struct cursor *cursor;
|
||||||
struct cursor_image *image;
|
struct cursor_image *image;
|
||||||
int i, size;
|
int i, size;
|
||||||
|
|
||||||
|
|
@ -188,18 +193,20 @@ wl_cursor_create_from_xcursor_images(XcursorImages *images,
|
||||||
if (!cursor)
|
if (!cursor)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
cursor->image_count = images->nimage;
|
cursor->cursor.image_count = images->nimage;
|
||||||
cursor->images = malloc(images->nimage * sizeof cursor->images[0]);
|
cursor->cursor.images =
|
||||||
if (!cursor->images) {
|
malloc(images->nimage * sizeof cursor->cursor.images[0]);
|
||||||
|
if (!cursor->cursor.images) {
|
||||||
free(cursor);
|
free(cursor);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
cursor->name = strdup(images->name);
|
cursor->cursor.name = strdup(images->name);
|
||||||
|
cursor->total_delay = 0;
|
||||||
|
|
||||||
for (i = 0; i < images->nimage; i++) {
|
for (i = 0; i < images->nimage; i++) {
|
||||||
image = malloc(sizeof *image);
|
image = malloc(sizeof *image);
|
||||||
cursor->images[i] = (struct wl_cursor_image *) image;
|
cursor->cursor.images[i] = (struct wl_cursor_image *) image;
|
||||||
|
|
||||||
image->theme = theme;
|
image->theme = theme;
|
||||||
image->buffer = NULL;
|
image->buffer = NULL;
|
||||||
|
|
@ -209,6 +216,7 @@ wl_cursor_create_from_xcursor_images(XcursorImages *images,
|
||||||
image->image.hotspot_x = images->images[i]->xhot;
|
image->image.hotspot_x = images->images[i]->xhot;
|
||||||
image->image.hotspot_y = images->images[i]->yhot;
|
image->image.hotspot_y = images->images[i]->yhot;
|
||||||
image->image.delay = images->images[i]->delay;
|
image->image.delay = images->images[i]->delay;
|
||||||
|
cursor->total_delay += image->image.delay;
|
||||||
|
|
||||||
/* copy pixels to shm pool */
|
/* copy pixels to shm pool */
|
||||||
size = image->image.width * image->image.height * 4;
|
size = image->image.width * image->image.height * 4;
|
||||||
|
|
@ -217,7 +225,7 @@ wl_cursor_create_from_xcursor_images(XcursorImages *images,
|
||||||
images->images[i]->pixels, size);
|
images->images[i]->pixels, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
return cursor;
|
return &cursor->cursor;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
|
@ -318,3 +326,30 @@ wl_cursor_theme_get_cursor(struct wl_cursor_theme *theme,
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Find the frame for a given elapsed time in a cursor animation
|
||||||
|
*
|
||||||
|
* \param cursor The cursor
|
||||||
|
* \param time Elapsed time since the beginning of the animation
|
||||||
|
*
|
||||||
|
* \return The index of the image that should be displayed for the
|
||||||
|
* given time in the cursor animation.
|
||||||
|
*/
|
||||||
|
WL_EXPORT int
|
||||||
|
wl_cursor_frame(struct wl_cursor *_cursor, uint32_t time)
|
||||||
|
{
|
||||||
|
struct cursor *cursor = (struct cursor *) _cursor;
|
||||||
|
uint32_t t;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (cursor->cursor.image_count == 1)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
t = time % cursor->total_delay;
|
||||||
|
|
||||||
|
while (t - cursor->cursor.images[i]->delay < t)
|
||||||
|
t -= cursor->cursor.images[i++]->delay;
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,6 +60,9 @@ wl_cursor_theme_get_cursor(struct wl_cursor_theme *theme,
|
||||||
struct wl_buffer *
|
struct wl_buffer *
|
||||||
wl_cursor_image_get_buffer(struct wl_cursor_image *image);
|
wl_cursor_image_get_buffer(struct wl_cursor_image *image);
|
||||||
|
|
||||||
|
int
|
||||||
|
wl_cursor_frame(struct wl_cursor *cursor, uint32_t time);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue