[PATCH wayland] cursor: add wl_cursor_frame_and_duration

Derek Foreman derekf at osg.samsung.com
Wed Mar 4 09:27:45 PST 2015


On 04/03/15 01:50 AM, Pekka Paalanen wrote:
> On Tue,  3 Mar 2015 15:24:13 -0600
> Derek Foreman <derekf at osg.samsung.com> wrote:
> 
>> It's useful to know how long the current cursor frame should be displayed
>> so we can wait that long to change it.
>>
>> Signed-off-by: Derek Foreman <derekf at osg.samsung.com>
>> ---
>>
>> In a follow up weston patch I use this to let toytoolkit set the cursor
>> with a timerfd instead of using a frame callback.
>>
>>  cursor/wayland-cursor.c | 29 ++++++++++++++++++++++++++---
>>  cursor/wayland-cursor.h |  4 ++++
>>  2 files changed, 30 insertions(+), 3 deletions(-)
>>
>> diff --git a/cursor/wayland-cursor.c b/cursor/wayland-cursor.c
>> index dba3b51..d5c8221 100644
>> --- a/cursor/wayland-cursor.c
>> +++ b/cursor/wayland-cursor.c
>> @@ -458,22 +458,28 @@ wl_cursor_theme_get_cursor(struct wl_cursor_theme *theme,
>>  }
>>  
>>  /** Find the frame for a given elapsed time in a cursor animation
>> + *  as well as the time left until next cursor change.
>>   *
>>   * \param cursor The cursor
>> - * \param time Elapsed time since the beginning of the animation
>> + * \param time Elapsed time in ms since the beginning of the animation
>> + * \param duration pointer to uint32_t to store time left for this image
> 
> I think you'd want to document the special duration time of zero
> meaning "forever", right?

Yeah, that's probably a good idea.

>>   *
>>   * \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)
>> +wl_cursor_frame_and_duration(struct wl_cursor *_cursor, uint32_t time,
>> +			     uint32_t *duration)
>>  {
>>  	struct cursor *cursor = (struct cursor *) _cursor;
>>  	uint32_t t;
>>  	int i;
>>  
>> -	if (cursor->cursor.image_count == 1)
>> +	if (cursor->cursor.image_count == 1) {
>> +		if (*duration)
>> +			duration = 0;
>>  		return 0;
>> +	}
>>  
>>  	i = 0;
>>  	t = time % cursor->total_delay;
>> @@ -481,5 +487,22 @@ wl_cursor_frame(struct wl_cursor *_cursor, uint32_t time)
>>  	while (t - cursor->cursor.images[i]->delay < t)
>>  		t -= cursor->cursor.images[i++]->delay;
>>  
>> +	if (duration)
>> +		*duration = cursor->cursor.images[i]->delay - t;
> 
> This can never produce zero duration, right?
> 
> I think it can't, and it's certainly logical to never produce zero, but
> I didn't rigorously think it through. So, we don't accidentally hit the
> special zero here.

Actually it can do far worse than that. :)

if there are 0 delays in the cursor set then the while loop breaks.
It'll stop advancing the cursor at the first 0, and will report that
cursor until time % cursor->total_delay wraps again.

And when that happens the duration remaining will become 2^32-smallnumber.

In v2 which I'll post shortly I return a duration of 1 when this
condition exists.

I'm not going to bother trying to "fix" the delay=0 case unless a bunch
of people agree on what a 0 delay actually means and that it's actually
present in cursor files. :)

>> +
>>  	return i;
>>  }
>> +
>> +/** Find the frame for a given elapsed time in a cursor animation
>> + *
>> + * \param cursor The cursor
>> + * \param time Elapsed time in ms 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)
>> +{
>> +	return wl_cursor_frame_and_duration(_cursor, time, NULL);
>> +}
>> diff --git a/cursor/wayland-cursor.h b/cursor/wayland-cursor.h
>> index c7548ae..c3884a0 100644
>> --- a/cursor/wayland-cursor.h
>> +++ b/cursor/wayland-cursor.h
>> @@ -63,6 +63,10 @@ wl_cursor_image_get_buffer(struct wl_cursor_image *image);
>>  int
>>  wl_cursor_frame(struct wl_cursor *cursor, uint32_t time);
>>  
>> +int
>> +wl_cursor_frame_and_duration(struct wl_cursor *cursor, uint32_t time,
>> +			     uint32_t *duration);
>> +
>>  #ifdef  __cplusplus
>>  }
>>  #endif
> 
> With that one documentation bit fixed:
> Reviewed-by: Pekka Paalanen <pekka.paalanen at collabora.co.uk>
> 
> 
> Thanks,
> pq
> 



More information about the wayland-devel mailing list