Pointer events, keyboard events, frame callbacks

This commit is contained in:
2022-09-03 14:29:42 +02:00
parent 81e2592c42
commit c9265ea20c
10 changed files with 418 additions and 30 deletions

View File

@@ -1,40 +1,33 @@
#include "common/shm.h"
#include "common/state.h"
#include "pointer.h"
#include "keyboard.h"
#include "protocols/xdg-shell-client-protocol.h"
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <wayland/wayland-client.h>
#include <wayland-client.h>
struct client_state
{
/* Globals */
struct wl_display *wl_display;
struct wl_registry *wl_registry;
struct wl_shm *wl_shm;
struct wl_compositor *wl_compositor;
struct xdg_wm_base *xdg_wm_base;
/* Objects */
struct wl_surface *wl_surface;
struct xdg_surface *xdg_surface;
struct xdg_toplevel *xdg_toplevel;
};
static const struct wl_callback_listener wl_surface_frame_listener;
static struct wl_buffer *draw_frame(struct client_state *state);
static void registry_handle_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface,
static void registry_handle_global(void *data, struct wl_registry *wl_registry, uint32_t name, const char *interface,
uint32_t version)
{
struct client_state *state = data;
if (strcmp(interface, wl_compositor_interface.name) == 0) {
state->wl_compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 5);
state->wl_compositor = wl_registry_bind(wl_registry, name, &wl_compositor_interface, 4);
} else if (strcmp(interface, wl_shm_interface.name) == 0) {
state->wl_shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
state->wl_shm = wl_registry_bind(wl_registry, name, &wl_shm_interface, 1);
} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
state->xdg_wm_base = wl_registry_bind(registry, name, &xdg_wm_base_interface, 4);
state->xdg_wm_base = wl_registry_bind(wl_registry, name, &xdg_wm_base_interface, 3);
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
state->wl_seat = wl_registry_bind(wl_registry, name, &wl_seat_interface, 7);
}
}
@@ -52,7 +45,7 @@ static void wm_base_handle_ping(void *data, struct xdg_wm_base *xdg_wm_base, uin
{
struct client_state *state = data;
// TODO !
xdg_wm_base_pong(xdg_wm_base, serial);
}
static void surface_handle_configure(void *data, struct xdg_surface *xdg_surface, uint32_t serial)
@@ -66,6 +59,61 @@ static void surface_handle_configure(void *data, struct xdg_surface *xdg_surface
wl_surface_commit(state->wl_surface);
}
static void seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name)
{
fprintf(stderr, "seat name: %s\n", name);
}
static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, uint32_t capabilities)
{
struct client_state *state = data;
bool have_pointer = capabilities & WL_SEAT_CAPABILITY_POINTER;
if (have_pointer && state->wl_pointer == NULL) {
state->wl_pointer = wl_seat_get_pointer(state->wl_seat);
wl_pointer_add_listener(state->wl_pointer, &wl_pointer_listener, state);
} else if (!have_pointer && state->wl_pointer != NULL) {
wl_pointer_release(state->wl_pointer);
state->wl_pointer = NULL;
}
bool have_keyboard = capabilities & WL_SEAT_CAPABILITY_KEYBOARD;
if (have_keyboard && state->wl_keyboard == NULL) {
state->wl_keyboard = wl_seat_get_keyboard(state->wl_seat);
wl_keyboard_add_listener(state->wl_keyboard, &wl_keyboard_listener, state);
} else if (!have_keyboard && state->wl_keyboard != NULL) {
wl_keyboard_release(state->wl_keyboard);
state->wl_keyboard = NULL;
}
}
static void wl_surface_frame_done(void *data, struct wl_callback *cb, uint32_t time)
{
/* Destroy this callback */
wl_callback_destroy(cb);
/* Request another frame */
struct client_state *state = data;
cb = wl_surface_frame(state->wl_surface);
wl_callback_add_listener(cb, &wl_surface_frame_listener, state);
/* Update scroll amount at 24 pixels per second */
if (state->last_frame != 0) {
int elapsed = time - state->last_frame;
state->offset += elapsed / 1000.0 * 24;
}
/* Submit a frame for this event */
struct wl_buffer *buffer = draw_frame(state);
wl_surface_attach(state->wl_surface, buffer, 0, 0);
wl_surface_damage_buffer(state->wl_surface, 0, 0, INT32_MAX, INT32_MAX);
wl_surface_commit(state->wl_surface);
state->last_frame = time;
}
static const struct wl_registry_listener wl_registry_listener = {
.global = registry_handle_global,
.global_remove = registry_handle_global_remove,
@@ -83,6 +131,15 @@ static const struct xdg_surface_listener xdg_surface_listener = {
.configure = surface_handle_configure,
};
static const struct wl_seat_listener wl_seat_listener = {
.name = seat_handle_name,
.capabilities = seat_handle_capabilities,
};
static const struct wl_callback_listener wl_surface_frame_listener = {
.done = wl_surface_frame_done,
};
static void wl_buffer_release(void *data, struct wl_buffer *wl_buffer)
{
/* Sent by the compositor when it's no longer using this buffer */
@@ -98,7 +155,6 @@ static struct wl_buffer *draw_frame(struct client_state *state)
const int width = 640, height = 480;
int stride = width * 4;
int size = stride * height;
int offset = 0;
int fd = allocate_shm_file(size);
if (fd == -1) {
@@ -121,10 +177,11 @@ static struct wl_buffer *draw_frame(struct client_state *state)
// memset(pixels, 255, width * height * 4);
// Checkerboard pattern
uint32_t *pixels = (uint32_t *)&data[offset];
int offset = (int)state->offset % 8;
uint32_t *pixels = (uint32_t *)data;
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
if ((x + y / 8 * 8) % 16 < 8) {
if (((x + offset) + (y + offset) / 8 * 8) % 16 < 8) {
pixels[y * width + x] = 0xFF666666;
} else {
pixels[y * width + x] = 0xFFEEEEEE;
@@ -142,25 +199,27 @@ int main(int argc, char *argv[])
struct client_state state = {0};
state.wl_display = wl_display_connect(NULL);
state.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
state.wl_registry = wl_display_get_registry(state.wl_display);
wl_registry_add_listener(state.wl_registry, &wl_registry_listener, &state);
wl_display_roundtrip(state.wl_display);
state.wl_surface = wl_compositor_create_surface(state.wl_compositor);
wl_seat_add_listener(state.wl_seat, &wl_seat_listener, &state);
state.wl_surface = wl_compositor_create_surface(state.wl_compositor);
wl_shm_add_listener(state.wl_shm, &wl_shm_listener, &state);
state.xdg_surface = xdg_wm_base_get_xdg_surface(state.xdg_wm_base, state.wl_surface);
xdg_surface_add_listener(state.xdg_surface, &xdg_surface_listener, &state);
state.xdg_toplevel = xdg_surface_get_toplevel(state.xdg_surface);
xdg_toplevel_set_title(state.xdg_toplevel, "Example client");
wl_surface_commit(state.wl_surface);
struct wl_callback *cb = wl_surface_frame(state.wl_surface);
wl_callback_add_listener(cb, &wl_surface_frame_listener, &state);
while (wl_display_dispatch(state.wl_display)) {
/* This space deliberately left blank */
}