|
@@ -63,18 +63,14 @@ typedef struct
|
|
typedef struct
|
|
typedef struct
|
|
{
|
|
{
|
|
struct wl_buffer *wl_buffer;
|
|
struct wl_buffer *wl_buffer;
|
|
- Uint32 duration;
|
|
|
|
|
|
+ Uint64 duration_ns;
|
|
} Wayland_SystemCursorFrame;
|
|
} Wayland_SystemCursorFrame;
|
|
|
|
|
|
typedef struct
|
|
typedef struct
|
|
{
|
|
{
|
|
Wayland_SystemCursorFrame *frames;
|
|
Wayland_SystemCursorFrame *frames;
|
|
- struct wl_callback *frame_callback;
|
|
|
|
- Uint64 last_frame_callback_time_ms;
|
|
|
|
- Uint64 current_frame_time_ms;
|
|
|
|
- Uint32 total_duration;
|
|
|
|
|
|
+ Uint64 total_duration_ns;
|
|
int num_frames;
|
|
int num_frames;
|
|
- int current_frame;
|
|
|
|
SDL_SystemCursor id;
|
|
SDL_SystemCursor id;
|
|
} Wayland_SystemCursor;
|
|
} Wayland_SystemCursor;
|
|
|
|
|
|
@@ -86,9 +82,6 @@ struct SDL_CursorData
|
|
Wayland_SystemCursor system;
|
|
Wayland_SystemCursor system;
|
|
} cursor_data;
|
|
} cursor_data;
|
|
|
|
|
|
- struct wl_surface *surface;
|
|
|
|
- struct wp_viewport *viewport;
|
|
|
|
-
|
|
|
|
bool is_system_cursor;
|
|
bool is_system_cursor;
|
|
};
|
|
};
|
|
|
|
|
|
@@ -298,40 +291,41 @@ struct wl_callback_listener cursor_frame_listener = {
|
|
|
|
|
|
static void cursor_frame_done(void *data, struct wl_callback *cb, uint32_t time)
|
|
static void cursor_frame_done(void *data, struct wl_callback *cb, uint32_t time)
|
|
{
|
|
{
|
|
- SDL_CursorData *c = (SDL_CursorData *)data;
|
|
|
|
|
|
+ SDL_WaylandSeat *seat = (SDL_WaylandSeat *)data;
|
|
|
|
+ SDL_CursorData *c = (struct SDL_CursorData *)seat->pointer.current_cursor;
|
|
|
|
|
|
- const Uint64 now = SDL_GetTicks();
|
|
|
|
- const Uint64 elapsed = (now - c->cursor_data.system.last_frame_callback_time_ms) % c->cursor_data.system.total_duration;
|
|
|
|
|
|
+ const Uint64 now = SDL_GetTicksNS();
|
|
|
|
+ const Uint64 elapsed = (now - seat->pointer.cursor_state.last_frame_callback_time_ns) % c->cursor_data.system.total_duration_ns;
|
|
Uint64 advance = 0;
|
|
Uint64 advance = 0;
|
|
- int next = c->cursor_data.system.current_frame;
|
|
|
|
|
|
+ int next = seat->pointer.cursor_state.current_frame;
|
|
|
|
|
|
wl_callback_destroy(cb);
|
|
wl_callback_destroy(cb);
|
|
- c->cursor_data.system.frame_callback = wl_surface_frame(c->surface);
|
|
|
|
- wl_callback_add_listener(c->cursor_data.system.frame_callback, &cursor_frame_listener, data);
|
|
|
|
|
|
+ seat->pointer.cursor_state.frame_callback = wl_surface_frame(seat->pointer.cursor_state.surface);
|
|
|
|
+ wl_callback_add_listener(seat->pointer.cursor_state.frame_callback, &cursor_frame_listener, data);
|
|
|
|
|
|
- c->cursor_data.system.current_frame_time_ms += elapsed;
|
|
|
|
|
|
+ seat->pointer.cursor_state.current_frame_time_ns += elapsed;
|
|
|
|
|
|
// Calculate the next frame based on the elapsed duration.
|
|
// Calculate the next frame based on the elapsed duration.
|
|
- for (Uint64 t = c->cursor_data.system.frames[next].duration; t <= c->cursor_data.system.current_frame_time_ms; t += c->cursor_data.system.frames[next].duration) {
|
|
|
|
|
|
+ for (Uint64 t = c->cursor_data.system.frames[next].duration_ns; t <= seat->pointer.cursor_state.current_frame_time_ns; t += c->cursor_data.system.frames[next].duration_ns) {
|
|
next = (next + 1) % c->cursor_data.system.num_frames;
|
|
next = (next + 1) % c->cursor_data.system.num_frames;
|
|
advance = t;
|
|
advance = t;
|
|
|
|
|
|
// Make sure we don't end up in an infinite loop if a cursor has frame durations of 0.
|
|
// Make sure we don't end up in an infinite loop if a cursor has frame durations of 0.
|
|
- if (!c->cursor_data.system.frames[next].duration) {
|
|
|
|
|
|
+ if (!c->cursor_data.system.frames[next].duration_ns) {
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- c->cursor_data.system.current_frame_time_ms -= advance;
|
|
|
|
- c->cursor_data.system.last_frame_callback_time_ms = now;
|
|
|
|
- c->cursor_data.system.current_frame = next;
|
|
|
|
- wl_surface_attach(c->surface, c->cursor_data.system.frames[next].wl_buffer, 0, 0);
|
|
|
|
- if (wl_surface_get_version(c->surface) >= WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION) {
|
|
|
|
- wl_surface_damage_buffer(c->surface, 0, 0, SDL_MAX_SINT32, SDL_MAX_SINT32);
|
|
|
|
|
|
+ seat->pointer.cursor_state.current_frame_time_ns -= advance;
|
|
|
|
+ seat->pointer.cursor_state.last_frame_callback_time_ns = now;
|
|
|
|
+ seat->pointer.cursor_state.current_frame = next;
|
|
|
|
+ wl_surface_attach(seat->pointer.cursor_state.surface, c->cursor_data.system.frames[next].wl_buffer, 0, 0);
|
|
|
|
+ if (wl_surface_get_version(seat->pointer.cursor_state.surface) >= WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION) {
|
|
|
|
+ wl_surface_damage_buffer(seat->pointer.cursor_state.surface, 0, 0, SDL_MAX_SINT32, SDL_MAX_SINT32);
|
|
} else {
|
|
} else {
|
|
- wl_surface_damage(c->surface, 0, 0, SDL_MAX_SINT32, SDL_MAX_SINT32);
|
|
|
|
|
|
+ wl_surface_damage(seat->pointer.cursor_state.surface, 0, 0, SDL_MAX_SINT32, SDL_MAX_SINT32);
|
|
}
|
|
}
|
|
- wl_surface_commit(c->surface);
|
|
|
|
|
|
+ wl_surface_commit(seat->pointer.cursor_state.surface);
|
|
}
|
|
}
|
|
|
|
|
|
static bool Wayland_GetSystemCursor(SDL_VideoData *vdata, SDL_CursorData *cdata, int *scale, int *dst_size, int *hot_x, int *hot_y)
|
|
static bool Wayland_GetSystemCursor(SDL_VideoData *vdata, SDL_CursorData *cdata, int *scale, int *dst_size, int *hot_x, int *hot_y)
|
|
@@ -415,11 +409,11 @@ static bool Wayland_GetSystemCursor(SDL_VideoData *vdata, SDL_CursorData *cdata,
|
|
|
|
|
|
// ... Set the cursor data, finally.
|
|
// ... Set the cursor data, finally.
|
|
cdata->cursor_data.system.num_frames = cursor->image_count;
|
|
cdata->cursor_data.system.num_frames = cursor->image_count;
|
|
- cdata->cursor_data.system.total_duration = 0;
|
|
|
|
|
|
+ cdata->cursor_data.system.total_duration_ns = 0;
|
|
for (int i = 0; i < cursor->image_count; ++i) {
|
|
for (int i = 0; i < cursor->image_count; ++i) {
|
|
cdata->cursor_data.system.frames[i].wl_buffer = WAYLAND_wl_cursor_image_get_buffer(cursor->images[i]);
|
|
cdata->cursor_data.system.frames[i].wl_buffer = WAYLAND_wl_cursor_image_get_buffer(cursor->images[i]);
|
|
- cdata->cursor_data.system.frames[i].duration = cursor->images[i]->delay;
|
|
|
|
- cdata->cursor_data.system.total_duration += cursor->images[i]->delay;
|
|
|
|
|
|
+ cdata->cursor_data.system.frames[i].duration_ns = SDL_MS_TO_NS((Uint64)cursor->images[i]->delay);
|
|
|
|
+ cdata->cursor_data.system.total_duration_ns += cdata->cursor_data.system.frames[i].duration_ns;
|
|
}
|
|
}
|
|
|
|
|
|
*scale = SDL_ceil(scale_factor) == scale_factor ? (int)scale_factor : 0;
|
|
*scale = SDL_ceil(scale_factor) == scale_factor ? (int)scale_factor : 0;
|
|
@@ -533,10 +527,8 @@ static bool Wayland_GetCustomCursor(SDL_Cursor *cursor, struct wl_buffer **buffe
|
|
|
|
|
|
static SDL_Cursor *Wayland_CreateCursor(SDL_Surface *surface, int hot_x, int hot_y)
|
|
static SDL_Cursor *Wayland_CreateCursor(SDL_Surface *surface, int hot_x, int hot_y)
|
|
{
|
|
{
|
|
- SDL_VideoDevice *vd = SDL_GetVideoDevice();
|
|
|
|
- SDL_VideoData *wd = vd->internal;
|
|
|
|
-
|
|
|
|
SDL_Cursor *cursor = SDL_calloc(1, sizeof(*cursor));
|
|
SDL_Cursor *cursor = SDL_calloc(1, sizeof(*cursor));
|
|
|
|
+
|
|
if (cursor) {
|
|
if (cursor) {
|
|
SDL_CursorData *data = SDL_calloc(1, sizeof(*data));
|
|
SDL_CursorData *data = SDL_calloc(1, sizeof(*data));
|
|
if (!data) {
|
|
if (!data) {
|
|
@@ -547,7 +539,6 @@ static SDL_Cursor *Wayland_CreateCursor(SDL_Surface *surface, int hot_x, int hot
|
|
WAYLAND_wl_list_init(&data->cursor_data.custom.scaled_cursor_cache);
|
|
WAYLAND_wl_list_init(&data->cursor_data.custom.scaled_cursor_cache);
|
|
data->cursor_data.custom.hot_x = hot_x;
|
|
data->cursor_data.custom.hot_x = hot_x;
|
|
data->cursor_data.custom.hot_y = hot_y;
|
|
data->cursor_data.custom.hot_y = hot_y;
|
|
- data->surface = wl_compositor_create_surface(wd->compositor);
|
|
|
|
|
|
|
|
data->cursor_data.custom.sdl_cursor_surface = surface;
|
|
data->cursor_data.custom.sdl_cursor_surface = surface;
|
|
++surface->refcount;
|
|
++surface->refcount;
|
|
@@ -563,8 +554,8 @@ static SDL_Cursor *Wayland_CreateCursor(SDL_Surface *surface, int hot_x, int hot
|
|
|
|
|
|
static SDL_Cursor *Wayland_CreateSystemCursor(SDL_SystemCursor id)
|
|
static SDL_Cursor *Wayland_CreateSystemCursor(SDL_SystemCursor id)
|
|
{
|
|
{
|
|
- SDL_VideoData *data = SDL_GetVideoDevice()->internal;
|
|
|
|
SDL_Cursor *cursor = SDL_calloc(1, sizeof(*cursor));
|
|
SDL_Cursor *cursor = SDL_calloc(1, sizeof(*cursor));
|
|
|
|
+
|
|
if (cursor) {
|
|
if (cursor) {
|
|
SDL_CursorData *cdata = SDL_calloc(1, sizeof(*cdata));
|
|
SDL_CursorData *cdata = SDL_calloc(1, sizeof(*cdata));
|
|
if (!cdata) {
|
|
if (!cdata) {
|
|
@@ -573,16 +564,6 @@ static SDL_Cursor *Wayland_CreateSystemCursor(SDL_SystemCursor id)
|
|
}
|
|
}
|
|
cursor->internal = cdata;
|
|
cursor->internal = cdata;
|
|
|
|
|
|
- /* The surface is only necessary if the cursor shape manager is not present.
|
|
|
|
- *
|
|
|
|
- * Note that we can't actually set any other cursor properties, as this
|
|
|
|
- * is window-specific. See Wayland_GetSystemCursor for the rest!
|
|
|
|
- */
|
|
|
|
- if (!data->cursor_shape_manager) {
|
|
|
|
- cdata->surface = wl_compositor_create_surface(data->compositor);
|
|
|
|
- wl_surface_set_user_data(cdata->surface, NULL);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
cdata->cursor_data.system.id = id;
|
|
cdata->cursor_data.system.id = id;
|
|
cdata->is_system_cursor = true;
|
|
cdata->is_system_cursor = true;
|
|
}
|
|
}
|
|
@@ -598,18 +579,28 @@ static SDL_Cursor *Wayland_CreateDefaultCursor(void)
|
|
|
|
|
|
static void Wayland_FreeCursorData(SDL_CursorData *d)
|
|
static void Wayland_FreeCursorData(SDL_CursorData *d)
|
|
{
|
|
{
|
|
- SDL_VideoDevice *vd = SDL_GetVideoDevice();
|
|
|
|
- struct SDL_WaylandInput *input = vd->internal->input;
|
|
|
|
|
|
+ SDL_VideoDevice *video_device = SDL_GetVideoDevice();
|
|
|
|
+ SDL_VideoData *video_data = video_device->internal;
|
|
|
|
+ SDL_WaylandSeat *seat;
|
|
|
|
+
|
|
|
|
+ // Stop any frame callbacks and detach buffers associated with the cursor being destroyed.
|
|
|
|
+ wl_list_for_each (seat, &video_data->seat_list, link)
|
|
|
|
+ {
|
|
|
|
+ if (seat->pointer.current_cursor == d) {
|
|
|
|
+ if (seat->pointer.cursor_state.frame_callback) {
|
|
|
|
+ wl_callback_destroy(seat->pointer.cursor_state.frame_callback);
|
|
|
|
+ seat->pointer.cursor_state.frame_callback = NULL;
|
|
|
|
+ }
|
|
|
|
+ if (seat->pointer.cursor_state.surface) {
|
|
|
|
+ wl_surface_attach(seat->pointer.cursor_state.surface, NULL, 0, 0);
|
|
|
|
+ }
|
|
|
|
|
|
- if (input->current_cursor == d) {
|
|
|
|
- input->current_cursor = NULL;
|
|
|
|
|
|
+ seat->pointer.current_cursor = NULL;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
// Buffers for system cursors must not be destroyed.
|
|
// Buffers for system cursors must not be destroyed.
|
|
if (d->is_system_cursor) {
|
|
if (d->is_system_cursor) {
|
|
- if (d->cursor_data.system.frame_callback) {
|
|
|
|
- wl_callback_destroy(d->cursor_data.system.frame_callback);
|
|
|
|
- }
|
|
|
|
SDL_free(d->cursor_data.system.frames);
|
|
SDL_free(d->cursor_data.system.frames);
|
|
} else {
|
|
} else {
|
|
Wayland_ScaledCustomCursor *c, *temp;
|
|
Wayland_ScaledCustomCursor *c, *temp;
|
|
@@ -620,16 +611,6 @@ static void Wayland_FreeCursorData(SDL_CursorData *d)
|
|
|
|
|
|
SDL_DestroySurface(d->cursor_data.custom.sdl_cursor_surface);
|
|
SDL_DestroySurface(d->cursor_data.custom.sdl_cursor_surface);
|
|
}
|
|
}
|
|
-
|
|
|
|
- if (d->viewport) {
|
|
|
|
- wp_viewport_destroy(d->viewport);
|
|
|
|
- d->viewport = NULL;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (d->surface) {
|
|
|
|
- wl_surface_destroy(d->surface);
|
|
|
|
- d->surface = NULL;
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
static void Wayland_FreeCursor(SDL_Cursor *cursor)
|
|
static void Wayland_FreeCursor(SDL_Cursor *cursor)
|
|
@@ -649,7 +630,7 @@ static void Wayland_FreeCursor(SDL_Cursor *cursor)
|
|
SDL_free(cursor);
|
|
SDL_free(cursor);
|
|
}
|
|
}
|
|
|
|
|
|
-static void Wayland_SetSystemCursorShape(struct SDL_WaylandInput *input, SDL_SystemCursor id)
|
|
|
|
|
|
+static void Wayland_SetSystemCursorShape(SDL_WaylandSeat *seat, SDL_SystemCursor id)
|
|
{
|
|
{
|
|
Uint32 shape;
|
|
Uint32 shape;
|
|
|
|
|
|
@@ -719,110 +700,140 @@ static void Wayland_SetSystemCursorShape(struct SDL_WaylandInput *input, SDL_Sys
|
|
shape = WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT;
|
|
shape = WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT;
|
|
}
|
|
}
|
|
|
|
|
|
- wp_cursor_shape_device_v1_set_shape(input->cursor_shape, input->pointer_enter_serial, shape);
|
|
|
|
|
|
+ wp_cursor_shape_device_v1_set_shape(seat->pointer.cursor_shape, seat->pointer.enter_serial, shape);
|
|
}
|
|
}
|
|
|
|
|
|
-static bool Wayland_ShowCursor(SDL_Cursor *cursor)
|
|
|
|
|
|
+static void Wayland_SeatSetCursor(SDL_WaylandSeat *seat, SDL_Cursor *cursor)
|
|
{
|
|
{
|
|
- SDL_VideoDevice *vd = SDL_GetVideoDevice();
|
|
|
|
- SDL_VideoData *d = vd->internal;
|
|
|
|
- struct SDL_WaylandInput *input = d->input;
|
|
|
|
- struct wl_pointer *pointer = d->pointer;
|
|
|
|
- struct wl_buffer *buffer = NULL;
|
|
|
|
- int scale = 1;
|
|
|
|
- int dst_width = 0;
|
|
|
|
- int dst_height = 0;
|
|
|
|
- int hot_x;
|
|
|
|
- int hot_y;
|
|
|
|
|
|
+ if (seat->pointer.wl_pointer) {
|
|
|
|
+ struct wl_buffer *buffer = NULL;
|
|
|
|
+ int scale = 1;
|
|
|
|
+ int dst_width = 0;
|
|
|
|
+ int dst_height = 0;
|
|
|
|
+ int hot_x;
|
|
|
|
+ int hot_y;
|
|
|
|
+ SDL_CursorData *cursor_data = cursor ? cursor->internal : NULL;
|
|
|
|
+
|
|
|
|
+ // Stop the frame callback for old animated cursors.
|
|
|
|
+ if (seat->pointer.cursor_state.frame_callback && cursor_data != seat->pointer.current_cursor) {
|
|
|
|
+ wl_callback_destroy(seat->pointer.cursor_state.frame_callback);
|
|
|
|
+ seat->pointer.cursor_state.frame_callback = NULL;
|
|
|
|
+ }
|
|
|
|
|
|
- if (!pointer) {
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
|
|
+ if (cursor) {
|
|
|
|
+ if (cursor_data == seat->pointer.current_cursor) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
|
|
- // Stop the frame callback for old animated cursors.
|
|
|
|
- if (input->current_cursor && input->current_cursor->is_system_cursor &&
|
|
|
|
- input->current_cursor->cursor_data.system.frame_callback) {
|
|
|
|
- wl_callback_destroy(input->current_cursor->cursor_data.system.frame_callback);
|
|
|
|
- input->current_cursor->cursor_data.system.frame_callback = NULL;
|
|
|
|
- }
|
|
|
|
|
|
+ if (cursor_data->is_system_cursor) {
|
|
|
|
+ // If the cursor shape protocol is supported, the compositor will draw nicely scaled cursors for us, so nothing more to do.
|
|
|
|
+ if (seat->pointer.cursor_shape) {
|
|
|
|
+ // Don't need the surface or viewport if using the cursor shape protocol.
|
|
|
|
+ if (seat->pointer.cursor_state.surface) {
|
|
|
|
+ wl_pointer_set_cursor(seat->pointer.wl_pointer, seat->pointer.enter_serial, NULL, 0, 0);
|
|
|
|
+ wl_surface_destroy(seat->pointer.cursor_state.surface);
|
|
|
|
+ seat->pointer.cursor_state.surface = NULL;
|
|
|
|
+ }
|
|
|
|
+ if (seat->pointer.cursor_state.viewport) {
|
|
|
|
+ wp_viewport_destroy(seat->pointer.cursor_state.viewport);
|
|
|
|
+ seat->pointer.cursor_state.viewport = NULL;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Wayland_SetSystemCursorShape(seat, cursor_data->cursor_data.system.id);
|
|
|
|
+ seat->pointer.current_cursor = cursor_data;
|
|
|
|
+
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
|
|
- if (cursor) {
|
|
|
|
- SDL_CursorData *data = cursor->internal;
|
|
|
|
-
|
|
|
|
- if (data->is_system_cursor) {
|
|
|
|
- // If the cursor shape protocol is supported, the compositor will draw nicely scaled cursors for us, so nothing more to do.
|
|
|
|
- if (input->cursor_shape) {
|
|
|
|
- Wayland_SetSystemCursorShape(input, data->cursor_data.system.id);
|
|
|
|
- input->current_cursor = data;
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
|
|
+ if (!Wayland_GetSystemCursor(seat->display, cursor_data, &scale, &dst_width, &hot_x, &hot_y)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
|
|
- if (!Wayland_GetSystemCursor(d, data, &scale, &dst_width, &hot_x, &hot_y)) {
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
|
|
+ dst_height = dst_width;
|
|
|
|
|
|
- dst_height = dst_width;
|
|
|
|
- wl_surface_attach(data->surface, data->cursor_data.system.frames[0].wl_buffer, 0, 0);
|
|
|
|
|
|
+ if (!seat->pointer.cursor_state.surface) {
|
|
|
|
+ seat->pointer.cursor_state.surface = wl_compositor_create_surface(seat->display->compositor);
|
|
|
|
+ }
|
|
|
|
+ wl_surface_attach(seat->pointer.cursor_state.surface, cursor_data->cursor_data.system.frames[0].wl_buffer, 0, 0);
|
|
|
|
+
|
|
|
|
+ // If more than one frame is available, create a frame callback to run the animation.
|
|
|
|
+ if (cursor_data->cursor_data.system.num_frames > 1) {
|
|
|
|
+ seat->pointer.cursor_state.last_frame_callback_time_ns = SDL_GetTicks();
|
|
|
|
+ seat->pointer.cursor_state.current_frame_time_ns = 0;
|
|
|
|
+ seat->pointer.cursor_state.current_frame = 0;
|
|
|
|
+ seat->pointer.cursor_state.frame_callback = wl_surface_frame(seat->pointer.cursor_state.surface);
|
|
|
|
+ wl_callback_add_listener(seat->pointer.cursor_state.frame_callback, &cursor_frame_listener, seat);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ if (!Wayland_GetCustomCursor(cursor, &buffer, &scale, &dst_width, &dst_height, &hot_x, &hot_y)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
|
|
- // If more than one frame is available, create a frame callback to run the animation.
|
|
|
|
- if (data->cursor_data.system.num_frames > 1) {
|
|
|
|
- data->cursor_data.system.last_frame_callback_time_ms = SDL_GetTicks();
|
|
|
|
- data->cursor_data.system.current_frame_time_ms = 0;
|
|
|
|
- data->cursor_data.system.current_frame = 0;
|
|
|
|
- data->cursor_data.system.frame_callback = wl_surface_frame(data->surface);
|
|
|
|
- wl_callback_add_listener(data->cursor_data.system.frame_callback, &cursor_frame_listener, data);
|
|
|
|
|
|
+ if (!seat->pointer.cursor_state.surface) {
|
|
|
|
+ seat->pointer.cursor_state.surface = wl_compositor_create_surface(seat->display->compositor);
|
|
|
|
+ }
|
|
|
|
+ wl_surface_attach(seat->pointer.cursor_state.surface, buffer, 0, 0);
|
|
}
|
|
}
|
|
- } else {
|
|
|
|
- if (!Wayland_GetCustomCursor(cursor, &buffer, &scale, &dst_width, &dst_height, &hot_x, &hot_y)) {
|
|
|
|
- return false;
|
|
|
|
|
|
+
|
|
|
|
+ // A scale value of 0 indicates that a viewport with the returned destination size should be used.
|
|
|
|
+ if (!scale) {
|
|
|
|
+ if (!seat->pointer.cursor_state.viewport) {
|
|
|
|
+ seat->pointer.cursor_state.viewport = wp_viewporter_get_viewport(seat->display->viewporter, seat->pointer.cursor_state.surface);
|
|
|
|
+ }
|
|
|
|
+ wl_surface_set_buffer_scale(seat->pointer.cursor_state.surface, 1);
|
|
|
|
+ wp_viewport_set_source(seat->pointer.cursor_state.viewport, wl_fixed_from_int(-1), wl_fixed_from_int(-1), wl_fixed_from_int(-1), wl_fixed_from_int(-1));
|
|
|
|
+ wp_viewport_set_destination(seat->pointer.cursor_state.viewport, dst_width, dst_height);
|
|
|
|
+ } else {
|
|
|
|
+ if (seat->pointer.cursor_state.viewport) {
|
|
|
|
+ wp_viewport_destroy(seat->pointer.cursor_state.viewport);
|
|
|
|
+ seat->pointer.cursor_state.viewport = NULL;
|
|
|
|
+ }
|
|
|
|
+ wl_surface_set_buffer_scale(seat->pointer.cursor_state.surface, scale);
|
|
}
|
|
}
|
|
|
|
|
|
- wl_surface_attach(data->surface, buffer, 0, 0);
|
|
|
|
- }
|
|
|
|
|
|
+ wl_pointer_set_cursor(seat->pointer.wl_pointer, seat->pointer.enter_serial, seat->pointer.cursor_state.surface, hot_x, hot_y);
|
|
|
|
|
|
- // A scale value of 0 indicates that a viewport with the returned destination size should be used.
|
|
|
|
- if (!scale) {
|
|
|
|
- if (!data->viewport) {
|
|
|
|
- data->viewport = wp_viewporter_get_viewport(d->viewporter, data->surface);
|
|
|
|
|
|
+ if (wl_surface_get_version(seat->pointer.cursor_state.surface) >= WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION) {
|
|
|
|
+ wl_surface_damage_buffer(seat->pointer.cursor_state.surface, 0, 0, SDL_MAX_SINT32, SDL_MAX_SINT32);
|
|
|
|
+ } else {
|
|
|
|
+ wl_surface_damage(seat->pointer.cursor_state.surface, 0, 0, SDL_MAX_SINT32, SDL_MAX_SINT32);
|
|
}
|
|
}
|
|
- wl_surface_set_buffer_scale(data->surface, 1);
|
|
|
|
- wp_viewport_set_source(data->viewport, wl_fixed_from_int(-1), wl_fixed_from_int(-1), wl_fixed_from_int(-1), wl_fixed_from_int(-1));
|
|
|
|
- wp_viewport_set_destination(data->viewport, dst_width, dst_height);
|
|
|
|
|
|
+
|
|
|
|
+ seat->pointer.current_cursor = cursor_data;
|
|
|
|
+ wl_surface_commit(seat->pointer.cursor_state.surface);
|
|
} else {
|
|
} else {
|
|
- if (data->viewport) {
|
|
|
|
- wp_viewport_destroy(data->viewport);
|
|
|
|
- data->viewport = NULL;
|
|
|
|
- }
|
|
|
|
- wl_surface_set_buffer_scale(data->surface, scale);
|
|
|
|
|
|
+ seat->pointer.current_cursor = NULL;
|
|
|
|
+ wl_pointer_set_cursor(seat->pointer.wl_pointer, seat->pointer.enter_serial, NULL, 0, 0);
|
|
}
|
|
}
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
|
|
- wl_pointer_set_cursor(pointer, input->pointer_enter_serial, data->surface, hot_x, hot_y);
|
|
|
|
|
|
+static bool Wayland_ShowCursor(SDL_Cursor *cursor)
|
|
|
|
+{
|
|
|
|
+ SDL_VideoDevice *vd = SDL_GetVideoDevice();
|
|
|
|
+ SDL_VideoData *d = vd->internal;
|
|
|
|
+ SDL_Mouse *mouse = SDL_GetMouse();
|
|
|
|
+ SDL_WaylandSeat *seat;
|
|
|
|
|
|
- if (wl_surface_get_version(data->surface) >= WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION) {
|
|
|
|
- wl_surface_damage_buffer(data->surface, 0, 0, SDL_MAX_SINT32, SDL_MAX_SINT32);
|
|
|
|
- } else {
|
|
|
|
- wl_surface_damage(data->surface, 0, 0, SDL_MAX_SINT32, SDL_MAX_SINT32);
|
|
|
|
|
|
+ wl_list_for_each (seat, &d->seat_list, link) {
|
|
|
|
+ if (mouse->focus && mouse->focus->internal == seat->pointer.focus) {
|
|
|
|
+ Wayland_SeatSetCursor(seat, cursor);
|
|
|
|
+ } else if (!seat->pointer.focus) {
|
|
|
|
+ Wayland_SeatSetCursor(seat, NULL);
|
|
}
|
|
}
|
|
-
|
|
|
|
- wl_surface_commit(data->surface);
|
|
|
|
- input->current_cursor = data;
|
|
|
|
- } else {
|
|
|
|
- input->current_cursor = NULL;
|
|
|
|
- wl_pointer_set_cursor(pointer, input->pointer_enter_serial, NULL, 0, 0);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
-static bool Wayland_WarpMouse(SDL_Window *window, float x, float y)
|
|
|
|
|
|
+void Wayland_SeatWarpMouse(SDL_WaylandSeat *seat, SDL_WindowData *window, float x, float y)
|
|
{
|
|
{
|
|
SDL_VideoDevice *vd = SDL_GetVideoDevice();
|
|
SDL_VideoDevice *vd = SDL_GetVideoDevice();
|
|
SDL_VideoData *d = vd->internal;
|
|
SDL_VideoData *d = vd->internal;
|
|
- SDL_WindowData *wind = window->internal;
|
|
|
|
- struct SDL_WaylandInput *input = d->input;
|
|
|
|
|
|
|
|
- if (d->pointer_constraints) {
|
|
|
|
- const bool toggle_lock = !wind->locked_pointer;
|
|
|
|
|
|
+ if (seat->pointer.wl_pointer) {
|
|
|
|
+ bool toggle_lock = !seat->pointer.locked_pointer;
|
|
|
|
+ bool update_grabs = false;
|
|
|
|
|
|
/* The pointer confinement protocol allows setting a hint to warp the pointer,
|
|
/* The pointer confinement protocol allows setting a hint to warp the pointer,
|
|
* but only when the pointer is locked.
|
|
* but only when the pointer is locked.
|
|
@@ -830,22 +841,51 @@ static bool Wayland_WarpMouse(SDL_Window *window, float x, float y)
|
|
* Lock the pointer, set the position hint, unlock, and hope for the best.
|
|
* Lock the pointer, set the position hint, unlock, and hope for the best.
|
|
*/
|
|
*/
|
|
if (toggle_lock) {
|
|
if (toggle_lock) {
|
|
- Wayland_input_lock_pointer(input, window);
|
|
|
|
- }
|
|
|
|
- if (wind->locked_pointer) {
|
|
|
|
- const wl_fixed_t f_x = wl_fixed_from_double(x / wind->pointer_scale.x);
|
|
|
|
- const wl_fixed_t f_y = wl_fixed_from_double(y / wind->pointer_scale.y);
|
|
|
|
- zwp_locked_pointer_v1_set_cursor_position_hint(wind->locked_pointer, f_x, f_y);
|
|
|
|
- wl_surface_commit(wind->surface);
|
|
|
|
|
|
+ if (seat->pointer.confined_pointer) {
|
|
|
|
+ zwp_confined_pointer_v1_destroy(seat->pointer.confined_pointer);
|
|
|
|
+ seat->pointer.confined_pointer = NULL;
|
|
|
|
+ update_grabs = true;
|
|
|
|
+ }
|
|
|
|
+ seat->pointer.locked_pointer = zwp_pointer_constraints_v1_lock_pointer(d->pointer_constraints, window->surface,
|
|
|
|
+ seat->pointer.wl_pointer, NULL,
|
|
|
|
+ ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ const wl_fixed_t f_x = wl_fixed_from_double(x / window->pointer_scale.x);
|
|
|
|
+ const wl_fixed_t f_y = wl_fixed_from_double(y / window->pointer_scale.y);
|
|
|
|
+ zwp_locked_pointer_v1_set_cursor_position_hint(seat->pointer.locked_pointer, f_x, f_y);
|
|
|
|
+ wl_surface_commit(window->surface);
|
|
|
|
+
|
|
if (toggle_lock) {
|
|
if (toggle_lock) {
|
|
- Wayland_input_unlock_pointer(input, window);
|
|
|
|
|
|
+ zwp_locked_pointer_v1_destroy(seat->pointer.locked_pointer);
|
|
|
|
+ seat->pointer.locked_pointer = NULL;
|
|
|
|
+
|
|
|
|
+ if (update_grabs) {
|
|
|
|
+ Wayland_SeatUpdatePointerGrab(seat);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
/* NOTE: There is a pending warp event under discussion that should replace this when available.
|
|
/* NOTE: There is a pending warp event under discussion that should replace this when available.
|
|
* https://gitlab.freedesktop.org/wayland/wayland/-/merge_requests/340
|
|
* https://gitlab.freedesktop.org/wayland/wayland/-/merge_requests/340
|
|
*/
|
|
*/
|
|
- SDL_SendMouseMotion(0, window, SDL_GLOBAL_MOUSE_ID, false, x, y);
|
|
|
|
|
|
+ SDL_SendMouseMotion(0, window->sdlwindow, seat->pointer.sdl_id, false, x, y);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static bool Wayland_WarpMouseRelative(SDL_Window *window, float x, float y)
|
|
|
|
+{
|
|
|
|
+ SDL_VideoDevice *vd = SDL_GetVideoDevice();
|
|
|
|
+ SDL_VideoData *d = vd->internal;
|
|
|
|
+ SDL_WindowData *wind = window->internal;
|
|
|
|
+ SDL_WaylandSeat *seat;
|
|
|
|
+
|
|
|
|
+ if (d->pointer_constraints) {
|
|
|
|
+ wl_list_for_each (seat, &d->seat_list, link) {
|
|
|
|
+ if (wind == seat->pointer.focus ||
|
|
|
|
+ (!seat->pointer.focus && wind == seat->keyboard.focus)) {
|
|
|
|
+ Wayland_SeatWarpMouse(seat, wind, x, y);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
} else {
|
|
} else {
|
|
return SDL_SetError("wayland: mouse warp failed; compositor lacks support for the required zwp_pointer_confinement_v1 protocol");
|
|
return SDL_SetError("wayland: mouse warp failed; compositor lacks support for the required zwp_pointer_confinement_v1 protocol");
|
|
}
|
|
}
|
|
@@ -857,16 +897,33 @@ static bool Wayland_WarpMouseGlobal(float x, float y)
|
|
{
|
|
{
|
|
SDL_VideoDevice *vd = SDL_GetVideoDevice();
|
|
SDL_VideoDevice *vd = SDL_GetVideoDevice();
|
|
SDL_VideoData *d = vd->internal;
|
|
SDL_VideoData *d = vd->internal;
|
|
- struct SDL_WaylandInput *input = d->input;
|
|
|
|
- SDL_WindowData *wind = input->pointer_focus;
|
|
|
|
|
|
+ SDL_WaylandSeat *seat;
|
|
|
|
+
|
|
|
|
+ if (d->pointer_constraints) {
|
|
|
|
+ wl_list_for_each (seat, &d->seat_list, link) {
|
|
|
|
+ SDL_WindowData *wind = seat->pointer.focus ? seat->pointer.focus : seat->keyboard.focus;
|
|
|
|
|
|
- // If the client wants the coordinates warped to within the focused window, just convert the coordinates to relative.
|
|
|
|
- if (wind) {
|
|
|
|
- SDL_Window *window = wind->sdlwindow;
|
|
|
|
- return Wayland_WarpMouse(window, x - (float)window->x, y - (float)window->y);
|
|
|
|
|
|
+ // If the client wants the coordinates warped to within a focused window, just convert the coordinates to relative.
|
|
|
|
+ if (wind) {
|
|
|
|
+ SDL_Window *window = wind->sdlwindow;
|
|
|
|
+
|
|
|
|
+ int abs_x, abs_y;
|
|
|
|
+ SDL_RelativeToGlobalForWindow(window, window->x, window->y, &abs_x, &abs_y);
|
|
|
|
+
|
|
|
|
+ const SDL_FPoint p = { x, y };
|
|
|
|
+ const SDL_FRect r = { abs_x, abs_y, window->w, window->h };
|
|
|
|
+
|
|
|
|
+ // Try to warp the cursor if the point is within the seat's focused window.
|
|
|
|
+ if (SDL_PointInRectFloat(&p, &r)) {
|
|
|
|
+ Wayland_SeatWarpMouse(seat, wind, p.x - abs_x, p.y - abs_y);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ return SDL_SetError("wayland: mouse warp failed; compositor lacks support for the required zwp_pointer_confinement_v1 protocol");
|
|
}
|
|
}
|
|
|
|
|
|
- return SDL_SetError("wayland: can't warp the mouse when a window does not have focus");
|
|
|
|
|
|
+ return true;
|
|
}
|
|
}
|
|
|
|
|
|
static bool Wayland_SetRelativeMouseMode(bool enabled)
|
|
static bool Wayland_SetRelativeMouseMode(bool enabled)
|
|
@@ -874,11 +931,17 @@ static bool Wayland_SetRelativeMouseMode(bool enabled)
|
|
SDL_VideoDevice *vd = SDL_GetVideoDevice();
|
|
SDL_VideoDevice *vd = SDL_GetVideoDevice();
|
|
SDL_VideoData *data = vd->internal;
|
|
SDL_VideoData *data = vd->internal;
|
|
|
|
|
|
- if (enabled) {
|
|
|
|
- return Wayland_input_enable_relative_pointer(data->input);
|
|
|
|
- } else {
|
|
|
|
- return Wayland_input_disable_relative_pointer(data->input);
|
|
|
|
|
|
+ // Relative mode requires both the relative motion and pointer confinement protocols.
|
|
|
|
+ if (!data->relative_pointer_manager) {
|
|
|
|
+ return SDL_SetError("Failed to enable relative mode: compositor lacks support for the required zwp_relative_pointer_manager_v1 protocol");
|
|
}
|
|
}
|
|
|
|
+ if (!data->pointer_constraints) {
|
|
|
|
+ return SDL_SetError("Failed to enable relative mode: compositor lacks support for the required zwp_pointer_constraints_v1 protocol");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ data->relative_mode_enabled = enabled;
|
|
|
|
+ Wayland_DisplayUpdatePointerGrabs(data, NULL);
|
|
|
|
+ return true;
|
|
}
|
|
}
|
|
|
|
|
|
/* Wayland doesn't support getting the true global cursor position, but it can
|
|
/* Wayland doesn't support getting the true global cursor position, but it can
|
|
@@ -895,18 +958,19 @@ static bool Wayland_SetRelativeMouseMode(bool enabled)
|
|
*/
|
|
*/
|
|
static SDL_MouseButtonFlags SDLCALL Wayland_GetGlobalMouseState(float *x, float *y)
|
|
static SDL_MouseButtonFlags SDLCALL Wayland_GetGlobalMouseState(float *x, float *y)
|
|
{
|
|
{
|
|
- SDL_Window *focus = SDL_GetMouseFocus();
|
|
|
|
|
|
+ SDL_Mouse *mouse = SDL_GetMouse();
|
|
SDL_MouseButtonFlags result = 0;
|
|
SDL_MouseButtonFlags result = 0;
|
|
|
|
|
|
- if (focus) {
|
|
|
|
- SDL_VideoData *viddata = SDL_GetVideoDevice()->internal;
|
|
|
|
|
|
+ // If there is no window with mouse focus, we have no idea what the actual position or button state is.
|
|
|
|
+ if (mouse->focus) {
|
|
int off_x, off_y;
|
|
int off_x, off_y;
|
|
-
|
|
|
|
- result = viddata->input->buttons_pressed;
|
|
|
|
- SDL_GetMouseState(x, y);
|
|
|
|
- SDL_RelativeToGlobalForWindow(focus, focus->x, focus->y, &off_x, &off_y);
|
|
|
|
- *x += off_x;
|
|
|
|
- *y += off_y;
|
|
|
|
|
|
+ SDL_RelativeToGlobalForWindow(mouse->focus, mouse->focus->x, mouse->focus->y, &off_x, &off_y);
|
|
|
|
+ result = SDL_GetMouseState(x, y);
|
|
|
|
+ *x = mouse->x + off_x;
|
|
|
|
+ *y = mouse->y + off_y;
|
|
|
|
+ } else {
|
|
|
|
+ *x = 0.f;
|
|
|
|
+ *y = 0.f;
|
|
}
|
|
}
|
|
|
|
|
|
return result;
|
|
return result;
|
|
@@ -978,7 +1042,7 @@ void Wayland_InitMouse(void)
|
|
mouse->CreateSystemCursor = Wayland_CreateSystemCursor;
|
|
mouse->CreateSystemCursor = Wayland_CreateSystemCursor;
|
|
mouse->ShowCursor = Wayland_ShowCursor;
|
|
mouse->ShowCursor = Wayland_ShowCursor;
|
|
mouse->FreeCursor = Wayland_FreeCursor;
|
|
mouse->FreeCursor = Wayland_FreeCursor;
|
|
- mouse->WarpMouse = Wayland_WarpMouse;
|
|
|
|
|
|
+ mouse->WarpMouse = Wayland_WarpMouseRelative;
|
|
mouse->WarpMouseGlobal = Wayland_WarpMouseGlobal;
|
|
mouse->WarpMouseGlobal = Wayland_WarpMouseGlobal;
|
|
mouse->SetRelativeMouseMode = Wayland_SetRelativeMouseMode;
|
|
mouse->SetRelativeMouseMode = Wayland_SetRelativeMouseMode;
|
|
mouse->GetGlobalMouseState = Wayland_GetGlobalMouseState;
|
|
mouse->GetGlobalMouseState = Wayland_GetGlobalMouseState;
|
|
@@ -1046,12 +1110,32 @@ void Wayland_FiniMouse(SDL_VideoData *data)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-void Wayland_SetHitTestCursor(SDL_HitTestResult rc)
|
|
|
|
|
|
+void Wayland_SeatUpdateCursor(SDL_WaylandSeat *seat)
|
|
{
|
|
{
|
|
- if (rc == SDL_HITTEST_NORMAL || rc == SDL_HITTEST_DRAGGABLE) {
|
|
|
|
- SDL_SetCursor(NULL);
|
|
|
|
|
|
+ SDL_Mouse *mouse = SDL_GetMouse();
|
|
|
|
+ SDL_WindowData *pointer_focus = seat->pointer.focus;
|
|
|
|
+
|
|
|
|
+ if (pointer_focus) {
|
|
|
|
+ const bool has_relative_focus = Wayland_SeatHasRelativePointerFocus(seat);
|
|
|
|
+
|
|
|
|
+ if (!seat->display->relative_mode_enabled || !has_relative_focus || mouse->relative_mode_cursor_visible) {
|
|
|
|
+ const SDL_HitTestResult rc = pointer_focus->hit_test_result;
|
|
|
|
+
|
|
|
|
+ if ((seat->display->relative_mode_enabled && has_relative_focus) ||
|
|
|
|
+ rc == SDL_HITTEST_NORMAL || rc == SDL_HITTEST_DRAGGABLE) {
|
|
|
|
+ Wayland_SeatSetCursor(seat, mouse->cur_cursor);
|
|
|
|
+ } else {
|
|
|
|
+ Wayland_SeatSetCursor(seat, sys_cursors[rc]);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ // Hide the cursor in relative mode, unless requested otherwise by the hint.
|
|
|
|
+ Wayland_SeatSetCursor(seat, NULL);
|
|
|
|
+ }
|
|
} else {
|
|
} else {
|
|
- Wayland_ShowCursor(sys_cursors[rc]);
|
|
|
|
|
|
+ /* The spec states "The cursor actually changes only if the input device focus is one of the
|
|
|
|
+ * requesting client's surfaces", so just clear the cursor if the seat has no pointer focus.
|
|
|
|
+ */
|
|
|
|
+ Wayland_SeatSetCursor(seat, NULL);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|