Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wayland: Load cursor theme/size based on settings #16683

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions gfx/common/dbus_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ static unsigned int dbus_screensaver_cookie = 0;
#endif

#include "../../verbosity.h"
#include <string.h>

void dbus_ensure_connection(void)
{
Expand Down Expand Up @@ -162,3 +163,144 @@ bool dbus_suspend_screensaver(bool enable)
#endif
return false;
}

static bool
get_cursor_settings_from_env(char **theme, int *size)
{
char *env_xtheme;
char *env_xsize;

env_xtheme = getenv("XCURSOR_THEME");
if (env_xtheme != NULL)
*theme = strdup(env_xtheme);

env_xsize = getenv("XCURSOR_SIZE");
if (env_xsize != NULL)
*size = atoi(env_xsize);

return env_xtheme != NULL && env_xsize != NULL;
}

#ifdef HAVE_DBUS
static DBusMessage *
get_setting_sync(DBusConnection *const connection,
const char *key,
const char *value)
{
DBusError error;
dbus_bool_t success;
DBusMessage *message;
DBusMessage *reply;

message = dbus_message_new_method_call(
"org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
"org.freedesktop.portal.Settings",
"Read");

success = dbus_message_append_args(message,
DBUS_TYPE_STRING, &key,
DBUS_TYPE_STRING, &value,
DBUS_TYPE_INVALID);

if (!success)
return NULL;

dbus_error_init(&error);

reply = dbus_connection_send_with_reply_and_block(
connection,
message,
DBUS_TIMEOUT_USE_DEFAULT,
&error);

dbus_message_unref(message);

if (dbus_error_is_set(&error)) {
dbus_error_free(&error);
return NULL;
}

dbus_error_free(&error);
return reply;
}

static bool
parse_type(DBusMessage *const reply,
const int type,
void *value)
{
DBusMessageIter iter[3];

dbus_message_iter_init(reply, &iter[0]);
if (dbus_message_iter_get_arg_type(&iter[0]) != DBUS_TYPE_VARIANT)
return false;

dbus_message_iter_recurse(&iter[0], &iter[1]);
if (dbus_message_iter_get_arg_type(&iter[1]) != DBUS_TYPE_VARIANT)
return false;

dbus_message_iter_recurse(&iter[1], &iter[2]);
if (dbus_message_iter_get_arg_type(&iter[2]) != type)
return false;

dbus_message_iter_get_basic(&iter[2], value);

return true;
}

bool
dbus_get_cursor_settings(char **theme, int *size)
{
static const char name[] = "org.gnome.desktop.interface";
static const char key_theme[] = "cursor-theme";
static const char key_size[] = "cursor-size";

DBusError error;
DBusConnection *connection;
DBusMessage *reply;
const char *value_theme = NULL;

dbus_error_init(&error);

connection = dbus_bus_get(DBUS_BUS_SESSION, &error);

if (dbus_error_is_set(&error))
goto fallback;

reply = get_setting_sync(connection, name, key_theme);
if (!reply)
goto fallback;

if (!parse_type(reply, DBUS_TYPE_STRING, &value_theme)) {
dbus_message_unref(reply);
goto fallback;
}

*theme = strdup(value_theme);

dbus_message_unref(reply);

reply = get_setting_sync(connection, name, key_size);
if (!reply)
goto fallback;

if (!parse_type(reply, DBUS_TYPE_INT32, size)) {
dbus_message_unref(reply);
goto fallback;
}

dbus_message_unref(reply);

return true;

fallback:
return get_cursor_settings_from_env(theme, size);
}
#else
bool
dbus_get_cursor_settings(char **theme, int *size)
{
return get_cursor_settings_from_env(theme, size);
}
#endif
2 changes: 2 additions & 0 deletions gfx/common/dbus_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ void dbus_screensaver_uninhibit(void);

bool dbus_suspend_screensaver(bool enable);

bool dbus_get_cursor_settings(char **theme, int *size);

#endif
4 changes: 3 additions & 1 deletion gfx/common/wayland_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -805,8 +805,10 @@ bool gfx_ctx_wl_init_common(
wl->input.keyboard_focus = true;
wl->input.mouse.focus = true;

wl->cursor.theme_name = NULL;
wl->cursor.size = 24;
wl->cursor.surface = wl_compositor_create_surface(wl->compositor);
wl->cursor.theme = wl_cursor_theme_load(NULL, 16, wl->shm);
wl->cursor.theme = wl_cursor_theme_load(wl->cursor.theme_name, wl->cursor.size, wl->shm);
wl->cursor.default_cursor = wl_cursor_theme_get_cursor(wl->cursor.theme, "left_ptr");

wl->num_active_touches = 0;
Expand Down
24 changes: 23 additions & 1 deletion input/common/wayland_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "../input_keymaps.h"
#include "../../frontend/frontend_driver.h"
#include "../../verbosity.h"
#include "../../gfx/common/dbus_common.h"

#define DND_ACTION WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE
#define FILE_MIME "text/uri-list"
Expand Down Expand Up @@ -164,12 +165,33 @@ void gfx_ctx_wl_show_mouse(void *data, bool state)

if (state)
{
if (!dbus_get_cursor_settings(&wl->cursor.theme_name, &wl->cursor.size)) {
wl->cursor.theme_name = NULL;
wl->cursor.size = 24;
}
int scale = ceil(FRACTIONAL_SCALE_MULT_DOUBLE(1.0, wl->fractional_scale_num));
struct wl_cursor_theme *theme = wl_cursor_theme_load(
wl->cursor.theme_name,
wl->cursor.size * scale,
wl->shm);
if (theme) {
wl_cursor_theme_destroy(wl->cursor.theme);
wl->cursor.theme = theme;
}

wl->cursor.default_cursor = wl_cursor_theme_get_cursor(wl->cursor.theme, "left_ptr");
struct wl_cursor_image *image = wl->cursor.default_cursor->images[0];
wl_pointer_set_cursor(wl->wl_pointer,
wl->cursor.serial, wl->cursor.surface,
image->hotspot_x, image->hotspot_y);
image->hotspot_x / scale, image->hotspot_y / scale);
wl_surface_attach(wl->cursor.surface,
wl_cursor_image_get_buffer(image), 0, 0);

if (wl_surface_get_version (wl->cursor.surface)
>= WL_SURFACE_SET_BUFFER_SCALE_SINCE_VERSION)
{
wl_surface_set_buffer_scale (wl->cursor.surface, scale);
}
wl_surface_damage(wl->cursor.surface, 0, 0, image->width, image->height);
wl_surface_commit(wl->cursor.surface);
}
Expand Down
4 changes: 4 additions & 0 deletions input/common/wayland_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#define FRACTIONAL_SCALE_V1_DEN 120
#define FRACTIONAL_SCALE_MULT(v, scale_num) \
(((v) * (scale_num) + FRACTIONAL_SCALE_V1_DEN / 2) / FRACTIONAL_SCALE_V1_DEN)
#define FRACTIONAL_SCALE_MULT_DOUBLE(v, scale_num) \
(((double)(v) * (double)(scale_num) + (double)FRACTIONAL_SCALE_V1_DEN / 2.0) / (double)FRACTIONAL_SCALE_V1_DEN)

#define UDEV_KEY_MAX 0x2ff
#define UDEV_MAX_KEYS (UDEV_KEY_MAX + 7) / 8
Expand Down Expand Up @@ -195,6 +197,8 @@ typedef struct gfx_ctx_wayland_data
struct wl_cursor_theme *theme;
struct wl_surface *surface;
uint32_t serial;
char *theme_name;
int size;
bool visible;
} cursor;

Expand Down
Loading