From 8d1425bde9e7f17a5a9e6bce73dffcf296dad6a1 Mon Sep 17 00:00:00 2001
From: Drew DeVault <sir@cmpwn.com>
Date: Fri, 30 Mar 2018 21:38:28 -0400
Subject: [PATCH] Initialize seat pointer in swaybar

---
 include/swaybar/bar.h |  10 ++++
 meson.build           |   1 +
 swaybar/bar.c         | 105 ++++++++++++++++++++++++++++++++++++++++++
 swaybar/meson.build   |   4 +-
 4 files changed, 118 insertions(+), 2 deletions(-)

diff --git a/include/swaybar/bar.h b/include/swaybar/bar.h
index 1bf2ea2d..0768a683 100644
--- a/include/swaybar/bar.h
+++ b/include/swaybar/bar.h
@@ -8,14 +8,24 @@ struct swaybar_config;
 struct swaybar_output;
 struct swaybar_workspace;
 
+struct swaybar_pointer {
+	struct wl_pointer *pointer;
+	struct wl_cursor_theme *cursor_theme;
+	struct wl_cursor_image *cursor_image;
+	struct wl_surface *cursor_surface;
+	struct swaybar_output *current;
+};
+
 struct swaybar {
 	struct wl_display *display;
 	struct wl_compositor *compositor;
 	struct zwlr_layer_shell_v1 *layer_shell;
 	struct wl_shm *shm;
+	struct wl_seat *seat;
 
 	struct swaybar_config *config;
 	struct swaybar_output *focused_output;
+	struct swaybar_pointer pointer;
 	struct status_line *status;
 
 	int ipc_event_socketfd;
diff --git a/meson.build b/meson.build
index 49824b30..01788fd9 100644
--- a/meson.build
+++ b/meson.build
@@ -24,6 +24,7 @@ pcre           = dependency('libpcre')
 wlroots        = dependency('wlroots', fallback: ['wlroots', 'wlroots'])
 wayland_server = dependency('wayland-server')
 wayland_client = dependency('wayland-client')
+wayland_cursor = dependency('wayland-cursor')
 wayland_egl    = dependency('wayland-egl')
 wayland_protos = dependency('wayland-protocols')
 xkbcommon      = dependency('xkbcommon')
diff --git a/swaybar/bar.c b/swaybar/bar.c
index 0fc41517..e7b8b2ca 100644
--- a/swaybar/bar.c
+++ b/swaybar/bar.c
@@ -9,7 +9,13 @@
 #include <sys/wait.h>
 #include <unistd.h>
 #include <wayland-client.h>
+#include <wayland-cursor.h>
 #include <wlr/util/log.h>
+#ifdef __FreeBSD__
+#include <dev/evdev/input-event-codes.h>
+#else
+#include <linux/input-event-codes.h>
+#endif
 #include "swaybar/render.h"
 #include "swaybar/config.h"
 #include "swaybar/event_loop.h"
@@ -56,12 +62,102 @@ struct zwlr_layer_surface_v1_listener layer_surface_listener = {
 	.closed = layer_surface_closed,
 };
 
+static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer,
+		uint32_t serial, struct wl_surface *surface,
+		wl_fixed_t surface_x, wl_fixed_t surface_y) {
+	struct swaybar *bar = data;
+	struct swaybar_pointer *pointer = &bar->pointer;
+	wl_surface_attach(pointer->cursor_surface,
+			wl_cursor_image_get_buffer(pointer->cursor_image), 0, 0);
+	wl_pointer_set_cursor(wl_pointer, serial, pointer->cursor_surface,
+			pointer->cursor_image->hotspot_x,
+			pointer->cursor_image->hotspot_y);
+	wl_surface_commit(pointer->cursor_surface);
+}
+
+static void wl_pointer_leave(void *data, struct wl_pointer *wl_pointer,
+		uint32_t serial, struct wl_surface *surface) {
+	// Who cares
+}
+
+static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer,
+		uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
+	wlr_log(L_DEBUG, "motion");
+	// TODO
+}
+
+static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
+		uint32_t serial, uint32_t time, uint32_t button, uint32_t state) {
+	wlr_log(L_DEBUG, "button");
+	// TODO
+}
+
+static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer,
+		uint32_t time, uint32_t axis, wl_fixed_t value) {
+	wlr_log(L_DEBUG, "axis");
+	// TODO
+}
+
+static void wl_pointer_frame(void *data, struct wl_pointer *wl_pointer) {
+	// Who cares
+}
+
+static void wl_pointer_axis_source(void *data, struct wl_pointer *wl_pointer,
+		uint32_t axis_source) {
+	// Who cares
+}
+
+static void wl_pointer_axis_stop(void *data, struct wl_pointer *wl_pointer,
+		uint32_t time, uint32_t axis) {
+	// Who cares
+}
+
+static void wl_pointer_axis_discrete(void *data, struct wl_pointer *wl_pointer,
+		uint32_t axis, int32_t discrete) {
+	// Who cares
+}
+
+struct wl_pointer_listener pointer_listener = {
+	.enter = wl_pointer_enter,
+	.leave = wl_pointer_leave,
+	.motion = wl_pointer_motion,
+	.button = wl_pointer_button,
+	.axis = wl_pointer_axis,
+	.frame = wl_pointer_frame,
+	.axis_source = wl_pointer_axis_source,
+	.axis_stop = wl_pointer_axis_stop,
+	.axis_discrete = wl_pointer_axis_discrete,
+};
+
+static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
+		enum wl_seat_capability caps) {
+	struct swaybar *bar = data;
+	if ((caps & WL_SEAT_CAPABILITY_POINTER)) {
+		bar->pointer.pointer = wl_seat_get_pointer(wl_seat);
+		wl_pointer_add_listener(bar->pointer.pointer, &pointer_listener, bar);
+	}
+}
+
+static void seat_handle_name(void *data, struct wl_seat *wl_seat,
+		const char *name) {
+	// Who cares
+}
+
+const struct wl_seat_listener seat_listener = {
+	.capabilities = seat_handle_capabilities,
+	.name = seat_handle_name,
+};
+
 static void handle_global(void *data, struct wl_registry *registry,
 		uint32_t name, const char *interface, uint32_t version) {
 	struct swaybar *bar = data;
 	if (strcmp(interface, wl_compositor_interface.name) == 0) {
 		bar->compositor = wl_registry_bind(registry, name,
 				&wl_compositor_interface, 1);
+	} else if (strcmp(interface, wl_seat_interface.name) == 0) {
+		bar->seat = wl_registry_bind(registry, name,
+				&wl_seat_interface, 1);
+		wl_seat_add_listener(bar->seat, &seat_listener, bar);
 	} else if (strcmp(interface, wl_shm_interface.name) == 0) {
 		bar->shm = wl_registry_bind(registry, name,
 				&wl_shm_interface, 1);
@@ -116,6 +212,15 @@ void bar_setup(struct swaybar *bar,
 	wl_registry_add_listener(registry, &registry_listener, bar);
 	wl_display_roundtrip(bar->display);
 	assert(bar->compositor && bar->layer_shell && bar->shm);
+	struct swaybar_pointer *pointer = &bar->pointer;
+
+	assert(pointer->cursor_theme = wl_cursor_theme_load(NULL, 16, bar->shm));
+	struct wl_cursor *cursor;
+	assert(cursor = wl_cursor_theme_get_cursor(
+				pointer->cursor_theme, "left_ptr"));
+	pointer->cursor_image = cursor->images[0];
+	assert(pointer->cursor_surface =
+			wl_compositor_create_surface(bar->compositor));
 
 	// TODO: we might not necessarily be meant to do all of the outputs
 	struct swaybar_output *output;
diff --git a/swaybar/meson.build b/swaybar/meson.build
index d15e8b5c..bf6f6d7a 100644
--- a/swaybar/meson.build
+++ b/swaybar/meson.build
@@ -1,6 +1,5 @@
 executable(
-	'swaybar',
-	[
+	'swaybar', [
 		'bar.c',
 		'config.c',
 		'event_loop.c',
@@ -20,6 +19,7 @@ executable(
 		pangocairo,
 		rt,
 		wayland_client,
+		wayland_cursor,
 		wlroots,
 	],
 	link_with: [lib_sway_common, lib_sway_client],