Skip to content

Commit

Permalink
Use a crosshair cursor via wayland-cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
Drew DeVault authored and emersion committed Jun 26, 2018
1 parent 8d97e28 commit 70b6a14
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions include/slurp.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ struct slurp_output {
int32_t width, height;
struct pool_buffer buffers[2];
struct pool_buffer *current_buffer;

struct wl_cursor_theme *cursor_theme;
struct wl_cursor_image *cursor_image;
};

struct slurp_pointer {
Expand All @@ -63,6 +66,8 @@ struct slurp_pointer {
int32_t pressed_x, pressed_y;
enum wl_pointer_button_state button_state;
struct slurp_output *current_output;

struct wl_surface *cursor_surface;
};

void pointer_get_box(struct slurp_pointer *pointer, int *x, int *y,
Expand Down
26 changes: 25 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#define _POSIX_C_SOURCE 2
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wayland-cursor.h>
#ifdef __linux__
#include <linux/input-event-codes.h>
#elif __FreeBSD__
Expand Down Expand Up @@ -37,6 +39,14 @@ static void pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,

// TODO: handle multiple overlapping outputs
pointer->current_output = output;

wl_surface_set_buffer_scale(pointer->cursor_surface, output->scale);
wl_surface_attach(pointer->cursor_surface,
wl_cursor_image_get_buffer(output->cursor_image), 0, 0);
wl_pointer_set_cursor(wl_pointer, serial, pointer->cursor_surface,
output->cursor_image->hotspot_x / output->scale,
output->cursor_image->hotspot_y / output->scale);
wl_surface_commit(pointer->cursor_surface);
}

static void pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
Expand Down Expand Up @@ -435,14 +445,28 @@ int main(int argc, char *argv[]) {
ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM);
zwlr_layer_surface_v1_set_exclusive_zone(output->layer_surface, -1);
wl_surface_commit(output->surface);

output->cursor_theme =
wl_cursor_theme_load(NULL, 24 * output->scale, state.shm);
assert(output->cursor_theme);
struct wl_cursor *cursor =
wl_cursor_theme_get_cursor(output->cursor_theme, "crosshair");
assert(cursor);
output->cursor_image = cursor->images[0];
}

struct slurp_pointer *pointer;
wl_list_for_each(pointer, &state.pointers, link) {
pointer->cursor_surface =
wl_compositor_create_surface(state.compositor);
}

state.running = true;
while (state.running && wl_display_dispatch(state.display) != -1) {
// This space intentionally left blank
}

struct slurp_pointer *pointer, *pointer_tmp;
struct slurp_pointer *pointer_tmp;
wl_list_for_each_safe(pointer, pointer_tmp, &state.pointers, link) {
destroy_pointer(pointer);
}
Expand Down
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ slurp_inc = include_directories('include')

cairo = dependency('cairo')
wayland_client = dependency('wayland-client')
wayland_cursor = dependency('wayland-cursor')
wayland_protos = dependency('wayland-protocols', version: '>=1.14')

subdir('protocol')
Expand All @@ -32,6 +33,7 @@ executable(
cairo,
client_protos,
wayland_client,
wayland_cursor,
],
include_directories: [slurp_inc],
install: true,
Expand Down

0 comments on commit 70b6a14

Please sign in to comment.