Skip to content

Commit

Permalink
properly sets cursor on webOS 5+
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotaku committed Oct 29, 2024
1 parent 3e93a44 commit 5507268
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 38 deletions.
115 changes: 78 additions & 37 deletions src/video/wayland/SDL_waylandmouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,66 @@ static SDL_bool wayland_get_system_cursor(SDL_VideoData *vdata, Wayland_CursorDa
SDL_WindowData *focusdata;
int i;

#ifdef SDL_VIDEO_DRIVER_WAYLAND_WEBOS
SDL_Mouse *mouse = SDL_GetMouse();
SDL_Surface *surface = NULL;
SDL_Cursor *temp_cursor;
Wayland_CursorData *temp_data;

int hot_pos;

switch (cdata->system_cursor) {
case SDL_SYSTEM_CURSOR_ARROW:
hot_pos = 0;
surface = WaylandWebOS_LoadCursorSurface("A", "N");
break;
case SDL_SYSTEM_CURSOR_IBEAM:
hot_pos = 0;
surface = WaylandWebOS_LoadCursorSurface("TEXT", "N");
break;
case SDL_SYSTEM_CURSOR_HAND:
hot_pos = 0;
surface = WaylandWebOS_LoadCursorSurface("POINT", "N");
break;
case SDL_SYSTEM_CURSOR_NO:
hot_pos = 0;
surface = WaylandWebOS_LoadCursorSurface("Disable", "N");
break;
case SDL_SYSTEM_CURSOR_SIZENWSE:
case SDL_SYSTEM_CURSOR_SIZENESW:
case SDL_SYSTEM_CURSOR_SIZEWE:
case SDL_SYSTEM_CURSOR_SIZENS:
case SDL_SYSTEM_CURSOR_SIZEALL:
hot_pos = 0;
surface = WaylandWebOS_LoadCursorSurface("HOLD", "N");
break;
default:
break;
}
if (surface) {
*scale = 1;
/* Create a temporary cursor with the surface, and move the data to the real cursor */
temp_cursor = mouse->CreateCursor(surface, hot_pos, hot_pos);
temp_data = (Wayland_CursorData *)temp_cursor->driverdata;

cdata->bitmap = surface;
cdata->hot_x = temp_data->hot_x;
cdata->hot_y = temp_data->hot_y;
cdata->w = temp_data->w;
cdata->h = temp_data->h;
cdata->buffer = temp_data->buffer;
cdata->shm_data = temp_data->shm_data;
cdata->shm_data_size = temp_data->shm_data_size;

temp_data->buffer = NULL;
temp_data->shm_data = NULL;
temp_data->shm_data_size = 0;
mouse->FreeCursor(temp_cursor);
return SDL_TRUE;
}

#endif /* SDL_VIDEO_DRIVER_WAYLAND_WEBOS */

/*
* GNOME based desktops expose the cursor size and theme via the
* org.freedesktop.portal.Settings interface of the xdg-desktop portal.
Expand Down Expand Up @@ -484,38 +544,6 @@ static SDL_Cursor *Wayland_CreateCursor(SDL_Surface *surface, int hot_x, int hot

static SDL_Cursor *Wayland_CreateSystemCursor(SDL_SystemCursor id)
{
#ifdef SDL_VIDEO_DRIVER_WAYLAND_WEBOS
SDL_Cursor *cursor;
int hot_pos = 0;
SDL_Surface *surface = NULL;
switch (id) {
case SDL_SYSTEM_CURSOR_ARROW:
hot_pos = 0;
surface = WaylandWebOS_LoadCursorSurface("A", "N");
break;
case SDL_SYSTEM_CURSOR_HAND:
hot_pos = 0;
surface = WaylandWebOS_LoadCursorSurface("POINT", "N");
break;
case SDL_SYSTEM_CURSOR_NO:
hot_pos = 0;
surface = WaylandWebOS_LoadCursorSurface("Disable", "N");
break;
default:
hot_pos = 0xff;
surface = WaylandWebOS_LoadCursorSurface("A", "N");
break;
}
if (!surface) {
return NULL;
}
cursor = Wayland_CreateCursor(surface, hot_pos, hot_pos);
if (cursor) {
Wayland_CursorData *cdata = cursor->driverdata;
cdata->bitmap = surface;
}
return cursor;
#else
SDL_VideoData *data = SDL_GetVideoDevice()->driverdata;
SDL_Cursor *cursor;

Expand All @@ -541,7 +569,6 @@ static SDL_Cursor *Wayland_CreateSystemCursor(SDL_SystemCursor id)
}

return cursor;
#endif
}

static SDL_Cursor *Wayland_CreateDefaultCursor()
Expand Down Expand Up @@ -594,9 +621,23 @@ static int Wayland_ShowCursor(SDL_Cursor *cursor)
SDL_VideoDevice *vd = SDL_GetVideoDevice();
SDL_VideoData *d = vd->driverdata;
struct SDL_WaylandInput *input = d->input;
uint32_t pointer_enter_serial = input->pointer_enter_serial;
struct wl_pointer *pointer = d->pointer;
float scale = 1.0f;


#ifdef SDL_VIDEO_DRIVER_WAYLAND_WEBOS
SDL_Window *focus = SDL_GetMouse()->focus;
struct SDL_WaylandInput *curr = d->input;
while (curr) {
if (curr->pointer_focus && focus == curr->pointer_focus->sdlwindow) {
pointer_enter_serial = curr->pointer_enter_serial;
break;
}
curr = curr->next;
}
#endif

if (!pointer) {
return -1;
}
Expand All @@ -615,7 +656,7 @@ static int Wayland_ShowCursor(SDL_Cursor *cursor)
wl_surface_set_buffer_scale(data->surface, scale);
}
wl_pointer_set_cursor(pointer,
input->pointer_enter_serial,
pointer_enter_serial,
data->surface,
data->hot_x / scale,
data->hot_y / scale);
Expand All @@ -634,15 +675,15 @@ static int Wayland_ShowCursor(SDL_Cursor *cursor)
input->cursor_visible = SDL_FALSE;
#ifdef SDL_VIDEO_DRIVER_WAYLAND_WEBOS
{
SDL_Cursor *hidden_cur = SDL_GetMouse()->hidden_cursor;
SDL_Cursor *hidden_cur = WaylandWebOS_ObtainHiddenCursor();
Wayland_CursorData *data = hidden_cur->driverdata;
wl_pointer_set_cursor(pointer, input->pointer_enter_serial, data->surface, 0, 0);
wl_pointer_set_cursor(pointer, pointer_enter_serial, data->surface, 0, 0);
wl_surface_attach(data->surface, data->buffer, 0, 0);
wl_surface_damage(data->surface, 0, 0, data->w, data->h);
wl_surface_commit(data->surface);
}
#else
wl_pointer_set_cursor(pointer, input->pointer_enter_serial, NULL, 0, 0);
wl_pointer_set_cursor(pointer, pointer_enter_serial, NULL, 0, 0);
#endif
}

Expand Down
13 changes: 13 additions & 0 deletions src/video/wayland/SDL_waylandwebos_cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "../../core/webos/SDL_webos_luna.h"
#include "../../core/webos/SDL_webos_json.h"
#include "../../core/webos/SDL_webos_png.h"
#include "../../events/SDL_mouse_c.h"

char WaylandWebOS_GetCursorSize()
{
Expand Down Expand Up @@ -61,4 +62,16 @@ SDL_Surface *WaylandWebOS_LoadCursorSurface(const char *type, const char *state)
return surface;
}

SDL_Cursor *WaylandWebOS_ObtainHiddenCursor()
{
SDL_Mouse *mouse = SDL_GetMouse();
if (!mouse->hidden_cursor) {
SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormat(0, 1, 1, 32, SDL_PIXELFORMAT_ARGB8888);
SDL_memset(surface->pixels, 0, (size_t)surface->h * surface->pitch);
mouse->hidden_cursor = SDL_CreateColorCursor(surface, 0, 0);
SDL_FreeSurface(surface);
}
return mouse->hidden_cursor;
}

#endif /* SDL_VIDEO_DRIVER_WAYLAND_WEBOS */
5 changes: 4 additions & 1 deletion src/video/wayland/SDL_waylandwebos_cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

#ifdef SDL_VIDEO_DRIVER_WAYLAND_WEBOS
#include "SDL_surface.h"
#include "SDL_mouse.h"

char WaylandWebOS_GetCursorSize();

SDL_Surface *WaylandWebOS_LoadCursorSurface(const char *type, const char *state);

#endif
SDL_Cursor *WaylandWebOS_ObtainHiddenCursor();

#endif /* SDL_VIDEO_DRIVER_WAYLAND_WEBOS */

#endif // SDL_waylandwebos_cursor_h_

0 comments on commit 5507268

Please sign in to comment.