Skip to content

Commit

Permalink
Add pointer input
Browse files Browse the repository at this point in the history
  • Loading branch information
emersion committed Apr 23, 2018
1 parent b8bd27b commit ab49835
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <linux/input-event-codes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -16,10 +17,16 @@ static const int height = 128;
static bool running = true;

static struct wl_shm *shm = NULL;
static struct wl_seat *seat = NULL;
static struct wl_compositor *compositor = NULL;
static struct xdg_wm_base *xdg_wm_base = NULL;

static void *shm_data = NULL;
static struct xdg_toplevel *xdg_toplevel = NULL;

static void noop() {
// This space intentionally left blank
}

static void xdg_surface_handle_configure(void *data,
struct xdg_surface *xdg_surface, uint32_t serial) {
Expand All @@ -30,26 +37,53 @@ static const struct xdg_surface_listener xdg_surface_listener = {
.configure = xdg_surface_handle_configure,
};

static void xdg_toplevel_handle_configure(void *data,
struct xdg_toplevel *xdg_toplevel, int32_t w, int32_t h,
struct wl_array *states) {
// This space intentionally left blank
}

static void xdg_toplevel_handle_close(void *data,
struct xdg_toplevel *xdg_toplevel) {
running = false;
}

static const struct xdg_toplevel_listener xdg_toplevel_listener = {
.configure = xdg_toplevel_handle_configure,
.configure = noop,
.close = xdg_toplevel_handle_close,
};

static void pointer_handle_button(void *data, struct wl_pointer *pointer,
uint32_t serial, uint32_t time, uint32_t button, uint32_t state) {
struct wl_seat *seat = data;

if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
xdg_toplevel_move(xdg_toplevel, seat, serial);
}
}

static const struct wl_pointer_listener pointer_listener = {
.enter = noop,
.leave = noop,
.motion = noop,
.button = pointer_handle_button,
.axis = noop,
};

static void seat_handle_capabilities(void *data, struct wl_seat *seat,
uint32_t capabilities) {
if (capabilities & WL_SEAT_CAPABILITY_POINTER) {
struct wl_pointer *pointer = wl_seat_get_pointer(seat);
wl_pointer_add_listener(pointer, &pointer_listener, seat);
}
}

static const struct wl_seat_listener seat_listener = {
.capabilities = seat_handle_capabilities,
};

static void handle_global(void *data, struct wl_registry *registry,
uint32_t name, const char *interface, uint32_t version) {
if (strcmp(interface, wl_shm_interface.name) == 0) {
shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
struct wl_seat *seat =
wl_registry_bind(registry, name, &wl_seat_interface, 1);
wl_seat_add_listener(seat, &seat_listener, NULL);
} else if (strcmp(interface, wl_compositor_interface.name) == 0) {
compositor = wl_registry_bind(registry, name,
&wl_compositor_interface, 1);
Expand Down Expand Up @@ -119,7 +153,7 @@ int main(int argc, char *argv[]) {
struct wl_surface *surface = wl_compositor_create_surface(compositor);
struct xdg_surface *xdg_surface =
xdg_wm_base_get_xdg_surface(xdg_wm_base, surface);
struct xdg_toplevel *xdg_toplevel = xdg_surface_get_toplevel(xdg_surface);
xdg_toplevel = xdg_surface_get_toplevel(xdg_surface);

xdg_surface_add_listener(xdg_surface, &xdg_surface_listener, NULL);
xdg_toplevel_add_listener(xdg_toplevel, &xdg_toplevel_listener, NULL);
Expand Down

0 comments on commit ab49835

Please sign in to comment.