mirror of
https://gitlab.freedesktop.org/wlroots/wlroots.git
synced 2024-11-25 16:42:26 +00:00
Fix wlr_seat; add to example compositor
This commit is contained in:
parent
af5db7a44c
commit
ad22b4874d
@ -4,6 +4,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <inttypes.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
#include <wayland-server.h>
|
||||
#include <wlr/backend.h>
|
||||
#include <wlr/backend/session.h>
|
||||
@ -14,16 +16,26 @@
|
||||
#include <wlr/types/wlr_surface.h>
|
||||
#include <wlr/types/wlr_wl_shell.h>
|
||||
#include <wlr/types/wlr_xdg_shell_v6.h>
|
||||
#include <wlr/types/wlr_seat.h>
|
||||
#include <xkbcommon/xkbcommon.h>
|
||||
#include <wlr/util/log.h>
|
||||
#include "shared.h"
|
||||
#include "compositor.h"
|
||||
|
||||
// TODO: move to common header?
|
||||
int os_create_anonymous_file(off_t size);
|
||||
|
||||
struct sample_state {
|
||||
struct wlr_renderer *renderer;
|
||||
struct wl_compositor_state compositor;
|
||||
struct wlr_wl_shell *wl_shell;
|
||||
struct wlr_seat *wl_seat;
|
||||
struct wlr_xdg_shell_v6 *xdg_shell;
|
||||
struct wl_resource *focus;
|
||||
struct wl_listener keyboard_bound;
|
||||
int keymap_fd;
|
||||
size_t keymap_size;
|
||||
uint32_t serial;
|
||||
};
|
||||
|
||||
/*
|
||||
@ -33,7 +45,7 @@ static inline int64_t timespec_to_msec(const struct timespec *a) {
|
||||
return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000;
|
||||
}
|
||||
|
||||
void output_frame_handle_surface(struct sample_state *sample,
|
||||
static void output_frame_handle_surface(struct sample_state *sample,
|
||||
struct wlr_output *wlr_output, struct timespec *ts,
|
||||
struct wl_resource *_res) {
|
||||
struct wlr_surface *surface = wl_resource_get_user_data(_res);
|
||||
@ -53,7 +65,7 @@ void output_frame_handle_surface(struct sample_state *sample,
|
||||
}
|
||||
}
|
||||
}
|
||||
void handle_output_frame(struct output_state *output, struct timespec *ts) {
|
||||
static void handle_output_frame(struct output_state *output, struct timespec *ts) {
|
||||
struct compositor_state *state = output->compositor;
|
||||
struct sample_state *sample = state->data;
|
||||
struct wlr_output *wlr_output = output->output;
|
||||
@ -74,11 +86,48 @@ void handle_output_frame(struct output_state *output, struct timespec *ts) {
|
||||
wlr_output_swap_buffers(wlr_output);
|
||||
}
|
||||
|
||||
static void handle_keyboard_key(struct keyboard_state *keyboard, uint32_t keycode,
|
||||
xkb_keysym_t sym, enum wlr_key_state key_state) {
|
||||
struct compositor_state *state = keyboard->compositor;
|
||||
struct sample_state *sample = state->data;
|
||||
|
||||
struct wl_resource *res = NULL;
|
||||
struct wlr_seat_handle *seat_handle = NULL;
|
||||
wl_list_for_each(res, &sample->compositor.surfaces, link) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (res) {
|
||||
seat_handle = wlr_seat_handle_for_client(sample->wl_seat,
|
||||
wl_resource_get_client(res));
|
||||
}
|
||||
|
||||
if (res != sample->focus && seat_handle && seat_handle->keyboard) {
|
||||
struct wl_array keys;
|
||||
wl_array_init(&keys);
|
||||
wl_keyboard_send_enter(seat_handle->keyboard, ++sample->serial, res, &keys);
|
||||
sample->focus = res;
|
||||
}
|
||||
|
||||
if (seat_handle && seat_handle->keyboard) {
|
||||
wl_keyboard_send_key(seat_handle->keyboard, ++sample->serial, 0, keycode, key_state);
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_keyboard_bound(struct wl_listener *listener, void *data) {
|
||||
struct wlr_seat_handle *handle = data;
|
||||
struct sample_state *state = wl_container_of(listener, state, keyboard_bound);
|
||||
|
||||
wl_keyboard_send_keymap(handle->keyboard, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
|
||||
state->keymap_fd, state->keymap_size);
|
||||
}
|
||||
|
||||
int main() {
|
||||
struct sample_state state = { 0 };
|
||||
struct compositor_state compositor = { 0,
|
||||
.data = &state,
|
||||
.output_frame_cb = handle_output_frame,
|
||||
.keyboard_key_cb = handle_keyboard_key
|
||||
};
|
||||
compositor_init(&compositor);
|
||||
|
||||
@ -92,8 +141,29 @@ int main() {
|
||||
state.wl_shell = wlr_wl_shell_create(compositor.display);
|
||||
state.xdg_shell = wlr_xdg_shell_v6_create(compositor.display);
|
||||
|
||||
state.wl_seat = wlr_seat_create(compositor.display, "seat0");
|
||||
state.keyboard_bound.notify = handle_keyboard_bound;
|
||||
wl_signal_add(&state.wl_seat->events.keyboard_bound, &state.keyboard_bound);
|
||||
wlr_seat_set_capabilities(state.wl_seat, WL_SEAT_CAPABILITY_KEYBOARD
|
||||
| WL_SEAT_CAPABILITY_POINTER | WL_SEAT_CAPABILITY_TOUCH);
|
||||
|
||||
struct keyboard_state *kbstate;
|
||||
wl_list_for_each(kbstate, &compositor.keyboards, link) {
|
||||
char *keymap = xkb_keymap_get_as_string(kbstate->keymap,
|
||||
XKB_KEYMAP_FORMAT_TEXT_V1);
|
||||
state.keymap_size = strlen(keymap);
|
||||
state.keymap_fd = os_create_anonymous_file(state.keymap_size);
|
||||
void *ptr = mmap(NULL, state.keymap_size,
|
||||
PROT_READ | PROT_WRITE,
|
||||
MAP_SHARED, state.keymap_fd, 0);
|
||||
strcpy(ptr, keymap);
|
||||
free(keymap);
|
||||
break;
|
||||
}
|
||||
|
||||
compositor_run(&compositor);
|
||||
|
||||
wlr_wl_shell_destroy(state.wl_shell);
|
||||
wlr_xdg_shell_v6_destroy(state.xdg_shell);
|
||||
close(state.keymap_fd);
|
||||
}
|
||||
|
@ -104,7 +104,7 @@ static void update_velocities(struct compositor_state *state,
|
||||
}
|
||||
|
||||
static void handle_keyboard_key(struct keyboard_state *kbstate,
|
||||
xkb_keysym_t sym, enum wlr_key_state key_state) {
|
||||
uint32_t keycode, xkb_keysym_t sym, enum wlr_key_state key_state) {
|
||||
// NOTE: It may be better to simply refer to our key state during each frame
|
||||
// and make this change in pixels/sec^2
|
||||
// Also, key repeat
|
||||
|
@ -41,7 +41,7 @@ static void keyboard_key_notify(struct wl_listener *listener, void *data) {
|
||||
key_state == WLR_KEY_PRESSED ? "pressed" : "released");
|
||||
}
|
||||
if (kbstate->compositor->keyboard_key_cb) {
|
||||
kbstate->compositor->keyboard_key_cb(kbstate, sym, key_state);
|
||||
kbstate->compositor->keyboard_key_cb(kbstate, event->keycode, sym, key_state);
|
||||
}
|
||||
if (sym == XKB_KEY_Escape) {
|
||||
wl_display_terminate(kbstate->compositor->display);
|
||||
|
@ -79,8 +79,8 @@ struct compositor_state {
|
||||
void (*output_frame_cb)(struct output_state *s, struct timespec *ts);
|
||||
void (*output_remove_cb)(struct output_state *s);
|
||||
void (*keyboard_remove_cb)(struct keyboard_state *s);
|
||||
void (*keyboard_key_cb)(struct keyboard_state *s, xkb_keysym_t sym,
|
||||
enum wlr_key_state key_state);
|
||||
void (*keyboard_key_cb)(struct keyboard_state *s, uint32_t keycode,
|
||||
xkb_keysym_t sym, enum wlr_key_state key_state);
|
||||
void (*pointer_motion_cb)(struct pointer_state *s,
|
||||
double d_x, double d_y);
|
||||
void (*pointer_motion_absolute_cb)(struct pointer_state *s,
|
||||
|
@ -27,6 +27,7 @@ struct wlr_seat {
|
||||
struct {
|
||||
struct wl_signal client_bound;
|
||||
struct wl_signal client_unbound;
|
||||
struct wl_signal keyboard_bound;
|
||||
} events;
|
||||
|
||||
void *data;
|
||||
@ -49,10 +50,12 @@ struct wlr_seat_handle *wlr_seat_handle_for_client(struct wlr_seat *wlr_seat,
|
||||
struct wl_client *client);
|
||||
/**
|
||||
* Updates the capabilities available on this seat.
|
||||
* Will automatically send them to all clients.
|
||||
*/
|
||||
void wlr_seat_set_capabilities(struct wlr_seat *wlr_seat, uint32_t capabilities);
|
||||
/**
|
||||
* Updates the name of this seat.
|
||||
* Will automatically send it to all clients.
|
||||
*/
|
||||
void wlr_seat_set_name(struct wlr_seat *wlr_seat, const char *name);
|
||||
|
||||
|
115
types/wlr_seat.c
115
types/wlr_seat.c
@ -6,6 +6,11 @@
|
||||
#include <wlr/types/wlr_seat.h>
|
||||
#include <wlr/util/log.h>
|
||||
|
||||
static void resource_destroy(struct wl_client *client,
|
||||
struct wl_resource *resource) {
|
||||
wl_resource_destroy(resource);
|
||||
}
|
||||
|
||||
static void wl_pointer_set_cursor(struct wl_client *client,
|
||||
struct wl_resource *resource,
|
||||
uint32_t serial,
|
||||
@ -15,29 +20,22 @@ static void wl_pointer_set_cursor(struct wl_client *client,
|
||||
wlr_log(L_DEBUG, "TODO: wl_pointer_set_cursor");
|
||||
}
|
||||
|
||||
static const struct wl_pointer_interface wl_pointer_impl = {
|
||||
.set_cursor = wl_pointer_set_cursor,
|
||||
.release = resource_destroy
|
||||
};
|
||||
|
||||
static void wl_pointer_destroy(struct wl_resource *resource) {
|
||||
struct wlr_seat_handle *handle = wl_resource_get_user_data(resource);
|
||||
if (handle->pointer) {
|
||||
wl_resource_destroy(handle->pointer);
|
||||
handle->pointer = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void wl_pointer_release(struct wl_client *client,
|
||||
struct wl_resource *resource) {
|
||||
wl_pointer_destroy(resource);
|
||||
}
|
||||
|
||||
struct wl_pointer_interface wl_pointer_impl = {
|
||||
.set_cursor = wl_pointer_set_cursor,
|
||||
.release = wl_pointer_release
|
||||
};
|
||||
|
||||
static void wl_seat_get_pointer(struct wl_client *client,
|
||||
struct wl_resource *_handle, uint32_t id) {
|
||||
struct wlr_seat_handle *handle = wl_resource_get_user_data(_handle);
|
||||
if (!(handle->wlr_seat->capabilities & WL_SEAT_CAPABILITY_KEYBOARD)) {
|
||||
// TODO: Show error?
|
||||
if (!(handle->wlr_seat->capabilities & WL_SEAT_CAPABILITY_POINTER)) {
|
||||
return;
|
||||
}
|
||||
if (handle->pointer) {
|
||||
@ -46,19 +44,69 @@ static void wl_seat_get_pointer(struct wl_client *client,
|
||||
// the same seat
|
||||
wl_resource_destroy(handle->pointer);
|
||||
}
|
||||
handle->pointer = wl_resource_create(client, &wl_pointer_interface, 5, id);
|
||||
handle->pointer = wl_resource_create(client, &wl_pointer_interface,
|
||||
wl_resource_get_version(_handle), id);
|
||||
wl_resource_set_implementation(handle->pointer, &wl_pointer_impl,
|
||||
handle, NULL);
|
||||
handle, &wl_pointer_destroy);
|
||||
}
|
||||
|
||||
static const struct wl_keyboard_interface wl_keyboard_impl = {
|
||||
.release = resource_destroy
|
||||
};
|
||||
|
||||
static void wl_keyboard_destroy(struct wl_resource *resource) {
|
||||
struct wlr_seat_handle *handle = wl_resource_get_user_data(resource);
|
||||
if (handle->keyboard) {
|
||||
handle->keyboard = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void wl_seat_get_keyboard(struct wl_client *client,
|
||||
struct wl_resource *_handle, uint32_t id) {
|
||||
wlr_log(L_DEBUG, "TODO: wl_seat_get_keyboard");
|
||||
struct wlr_seat_handle *handle = wl_resource_get_user_data(_handle);
|
||||
if (!(handle->wlr_seat->capabilities & WL_SEAT_CAPABILITY_KEYBOARD)) {
|
||||
return;
|
||||
}
|
||||
if (handle->keyboard) {
|
||||
// TODO: this is probably a protocol violation but it simplifies our
|
||||
// code and it'd be stupid for clients to create several pointers for
|
||||
// the same seat
|
||||
wl_resource_destroy(handle->keyboard);
|
||||
}
|
||||
handle->keyboard = wl_resource_create(client, &wl_keyboard_interface,
|
||||
wl_resource_get_version(_handle), id);
|
||||
wl_resource_set_implementation(handle->keyboard, &wl_keyboard_impl,
|
||||
handle, &wl_keyboard_destroy);
|
||||
wl_signal_emit(&handle->wlr_seat->events.keyboard_bound, handle);
|
||||
}
|
||||
|
||||
static const struct wl_touch_interface wl_touch_impl = {
|
||||
.release = resource_destroy
|
||||
};
|
||||
|
||||
static void wl_touch_destroy(struct wl_resource *resource) {
|
||||
struct wlr_seat_handle *handle = wl_resource_get_user_data(resource);
|
||||
if (handle->touch) {
|
||||
handle->touch = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void wl_seat_get_touch(struct wl_client *client,
|
||||
struct wl_resource *_handle, uint32_t id) {
|
||||
wlr_log(L_DEBUG, "TODO: wl_seat_get_touch");
|
||||
struct wlr_seat_handle *handle = wl_resource_get_user_data(_handle);
|
||||
if (!(handle->wlr_seat->capabilities & WL_SEAT_CAPABILITY_TOUCH)) {
|
||||
return;
|
||||
}
|
||||
if (handle->touch) {
|
||||
// TODO: this is probably a protocol violation but it simplifies our
|
||||
// code and it'd be stupid for clients to create several pointers for
|
||||
// the same seat
|
||||
wl_resource_destroy(handle->touch);
|
||||
}
|
||||
handle->touch = wl_resource_create(client, &wl_touch_interface,
|
||||
wl_resource_get_version(_handle), id);
|
||||
wl_resource_set_implementation(handle->touch, &wl_touch_impl,
|
||||
handle, &wl_touch_destroy);
|
||||
}
|
||||
|
||||
static void wl_seat_destroy(struct wl_resource *resource) {
|
||||
@ -67,33 +115,28 @@ static void wl_seat_destroy(struct wl_resource *resource) {
|
||||
wl_resource_destroy(handle->pointer);
|
||||
}
|
||||
if (handle->keyboard) {
|
||||
wl_resource_destroy(handle->pointer);
|
||||
wl_resource_destroy(handle->keyboard);
|
||||
}
|
||||
if (handle->touch) {
|
||||
wl_resource_destroy(handle->pointer);
|
||||
wl_resource_destroy(handle->touch);
|
||||
}
|
||||
wl_signal_emit(&handle->wlr_seat->events.client_unbound, handle);
|
||||
wl_list_remove(&handle->link);
|
||||
free(handle);
|
||||
}
|
||||
|
||||
static void wl_seat_release(struct wl_client *client,
|
||||
struct wl_resource *resource) {
|
||||
wl_seat_destroy(resource);
|
||||
}
|
||||
|
||||
struct wl_seat_interface wl_seat_impl = {
|
||||
.get_pointer = wl_seat_get_pointer,
|
||||
.get_keyboard = wl_seat_get_keyboard,
|
||||
.get_touch = wl_seat_get_touch,
|
||||
.release = wl_seat_release
|
||||
.release = resource_destroy
|
||||
};
|
||||
|
||||
static void wl_seat_bind(struct wl_client *wl_client, void *_wlr_seat,
|
||||
uint32_t version, uint32_t id) {
|
||||
struct wlr_seat *wlr_seat = _wlr_seat;
|
||||
assert(wl_client && wlr_seat);
|
||||
if (version > 5) {
|
||||
if (version > 6) {
|
||||
wlr_log(L_ERROR, "Client requested unsupported wl_seat version, disconnecting");
|
||||
wl_client_destroy(wl_client);
|
||||
return;
|
||||
@ -115,7 +158,7 @@ struct wlr_seat *wlr_seat_create(struct wl_display *display, const char *name) {
|
||||
return NULL;
|
||||
}
|
||||
struct wl_global *wl_global = wl_global_create(display,
|
||||
&wl_seat_interface, 5, wlr_seat, wl_seat_bind);
|
||||
&wl_seat_interface, 6, wlr_seat, wl_seat_bind);
|
||||
if (!wl_global) {
|
||||
free(wlr_seat);
|
||||
return NULL;
|
||||
@ -125,11 +168,18 @@ struct wlr_seat *wlr_seat_create(struct wl_display *display, const char *name) {
|
||||
wl_list_init(&wlr_seat->handles);
|
||||
wl_signal_init(&wlr_seat->events.client_bound);
|
||||
wl_signal_init(&wlr_seat->events.client_unbound);
|
||||
wl_signal_init(&wlr_seat->events.keyboard_bound);
|
||||
return wlr_seat;
|
||||
}
|
||||
|
||||
void wlr_seat_destroy(struct wlr_seat *wlr_seat) {
|
||||
// TODO
|
||||
if (!wlr_seat) {
|
||||
return;
|
||||
}
|
||||
|
||||
wl_global_destroy(wlr_seat->wl_global);
|
||||
free(wlr_seat->name);
|
||||
free(wlr_seat);
|
||||
}
|
||||
|
||||
struct wlr_seat_handle *wlr_seat_handle_for_client(struct wlr_seat *wlr_seat,
|
||||
@ -146,11 +196,18 @@ struct wlr_seat_handle *wlr_seat_handle_for_client(struct wlr_seat *wlr_seat,
|
||||
|
||||
void wlr_seat_set_capabilities(struct wlr_seat *wlr_seat,
|
||||
uint32_t capabilities) {
|
||||
// TODO: If e.g. pointer was removed, destroy all client pointer resources
|
||||
wlr_seat->capabilities = capabilities;
|
||||
struct wlr_seat_handle *handle;
|
||||
wl_list_for_each(handle, &wlr_seat->handles, link) {
|
||||
wl_seat_send_capabilities(handle->wl_resource, capabilities);
|
||||
}
|
||||
}
|
||||
|
||||
void wlr_seat_set_name(struct wlr_seat *wlr_seat, const char *name) {
|
||||
free(wlr_seat->name);
|
||||
wlr_seat->name = strdup(name);
|
||||
struct wlr_seat_handle *handle;
|
||||
wl_list_for_each(handle, &wlr_seat->handles, link) {
|
||||
wl_seat_send_name(handle->wl_resource, name);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user