mirror of
https://gitlab.freedesktop.org/wlroots/wlroots.git
synced 2024-11-25 08:32:30 +00:00
Initialize keyboards from libinput
This commit is contained in:
parent
019fff06be
commit
0e75d157f5
@ -15,6 +15,7 @@ add_library(wlr-backend
|
||||
drm/drm.c
|
||||
|
||||
libinput/backend.c
|
||||
libinput/events.c
|
||||
|
||||
backend.c
|
||||
egl.c
|
||||
|
@ -19,14 +19,10 @@ struct wlr_backend *wlr_backend_create(const struct wlr_backend_impl *impl,
|
||||
}
|
||||
backend->state = state;
|
||||
backend->impl = impl;
|
||||
wl_signal_init(&backend->events.input_add);
|
||||
wl_signal_init(&backend->events.input_remove);
|
||||
wl_signal_init(&backend->events.output_add);
|
||||
wl_signal_init(&backend->events.output_remove);
|
||||
wl_signal_init(&backend->events.keyboard_add);
|
||||
wl_signal_init(&backend->events.keyboard_remove);
|
||||
wl_signal_init(&backend->events.pointer_add);
|
||||
wl_signal_init(&backend->events.pointer_remove);
|
||||
wl_signal_init(&backend->events.touch_add);
|
||||
wl_signal_init(&backend->events.touch_remove);
|
||||
return backend;
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,7 @@ static const struct libinput_interface libinput_impl = {
|
||||
.close_restricted = wlr_libinput_close_restricted
|
||||
};
|
||||
|
||||
static int wlr_libinput_handle_event(int fd, uint32_t mask, void *_state) {
|
||||
static int wlr_libinput_readable(int fd, uint32_t mask, void *_state) {
|
||||
struct wlr_backend_state *state = _state;
|
||||
if (libinput_dispatch(state->libinput) != 0) {
|
||||
wlr_log(L_ERROR, "Failed to dispatch libinput");
|
||||
@ -32,12 +32,7 @@ static int wlr_libinput_handle_event(int fd, uint32_t mask, void *_state) {
|
||||
}
|
||||
struct libinput_event *event;
|
||||
while ((event = libinput_get_event(state->libinput))) {
|
||||
struct libinput *context = libinput_event_get_context(event);
|
||||
struct libinput_device *device = libinput_event_get_device(event);
|
||||
enum libinput_event_type event_type = libinput_event_get_type(event);
|
||||
wlr_log(L_DEBUG, "libinput event: %d", event_type);
|
||||
(void)device; (void)context;
|
||||
// TODO: dispatch event
|
||||
wlr_libinput_event(state, event);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -73,7 +68,7 @@ static bool wlr_libinput_backend_init(struct wlr_backend_state *state) {
|
||||
}
|
||||
state->input_event = wl_event_loop_add_fd(event_loop,
|
||||
libinput_get_fd(state->libinput), WL_EVENT_READABLE,
|
||||
wlr_libinput_handle_event, state);
|
||||
wlr_libinput_readable, state);
|
||||
if (!state->input_event) {
|
||||
wlr_log(L_ERROR, "Failed to create input event on event loop");
|
||||
return false;
|
||||
@ -112,5 +107,7 @@ struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display,
|
||||
state->udev = udev;
|
||||
state->display = display;
|
||||
|
||||
state->keyboards = list_create();
|
||||
|
||||
return backend;
|
||||
}
|
||||
|
100
backend/libinput/events.c
Normal file
100
backend/libinput/events.c
Normal file
@ -0,0 +1,100 @@
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <libinput.h>
|
||||
#include <wlr/session.h>
|
||||
#include <wlr/types.h>
|
||||
#include <wlr/common/list.h>
|
||||
#include "backend/libinput/backend.h"
|
||||
#include "common/log.h"
|
||||
#include "types.h"
|
||||
|
||||
static void wlr_libinput_keyboard_destroy(struct wlr_keyboard_state *state) {
|
||||
free(state);
|
||||
}
|
||||
|
||||
static struct wlr_keyboard_impl keyboard_impl = {
|
||||
.destroy = wlr_libinput_keyboard_destroy
|
||||
};
|
||||
|
||||
static struct wlr_keyboard *wlr_libinput_keyboard_create(
|
||||
struct libinput_device *device) {
|
||||
assert(device);
|
||||
struct wlr_keyboard_state *kbstate =
|
||||
calloc(1, sizeof(struct wlr_keyboard_state));
|
||||
kbstate->handle = device;
|
||||
return wlr_keyboard_create(&keyboard_impl, kbstate);
|
||||
}
|
||||
|
||||
static void device_added(struct wlr_backend_state *state,
|
||||
struct libinput_device *device) {
|
||||
assert(state && device);
|
||||
/*
|
||||
* Note: the wlr API exposes only devices with a single capability, because
|
||||
* that meshes better with how Wayland does things and is a bit simpler.
|
||||
* However, libinput devices often have multiple capabilities - in such
|
||||
* cases we have to create several devices.
|
||||
*/
|
||||
int vendor = libinput_device_get_id_vendor(device);
|
||||
int product = libinput_device_get_id_product(device);
|
||||
const char *name = libinput_device_get_name(device);
|
||||
list_t *devices = list_create();
|
||||
wlr_log(L_DEBUG, "Added %s [%d:%d]", name, vendor, product);
|
||||
|
||||
if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD)) {
|
||||
struct wlr_input_device *wlr_device = wlr_input_device_create(
|
||||
WLR_INPUT_DEVICE_KEYBOARD, name, vendor, product);
|
||||
wlr_device->keyboard = wlr_libinput_keyboard_create(device);
|
||||
wl_signal_emit(&state->backend->events.input_add, wlr_device);
|
||||
list_add(devices, wlr_device);
|
||||
}
|
||||
if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER)) {
|
||||
// TODO
|
||||
}
|
||||
if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH)) {
|
||||
// TODO
|
||||
}
|
||||
if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_TOOL)) {
|
||||
// TODO
|
||||
}
|
||||
if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_PAD)) {
|
||||
// TODO
|
||||
}
|
||||
if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_GESTURE)) {
|
||||
// TODO
|
||||
}
|
||||
if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_SWITCH)) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
if (devices->length > 0) {
|
||||
libinput_device_set_user_data(device, devices);
|
||||
} else {
|
||||
list_free(devices);
|
||||
}
|
||||
}
|
||||
|
||||
static void device_removed(struct wlr_backend_state *state,
|
||||
struct libinput_device *device) {
|
||||
wlr_log(L_DEBUG, "libinput device removed");
|
||||
// TODO
|
||||
}
|
||||
|
||||
void wlr_libinput_event(struct wlr_backend_state *state,
|
||||
struct libinput_event *event) {
|
||||
assert(state && event);
|
||||
struct libinput *context = libinput_event_get_context(event);
|
||||
struct libinput_device *device = libinput_event_get_device(event);
|
||||
enum libinput_event_type event_type = libinput_event_get_type(event);
|
||||
(void)context;
|
||||
switch (event_type) {
|
||||
case LIBINPUT_EVENT_DEVICE_ADDED:
|
||||
device_added(state, device);
|
||||
break;
|
||||
case LIBINPUT_EVENT_DEVICE_REMOVED:
|
||||
device_removed(state, device);
|
||||
break;
|
||||
default:
|
||||
wlr_log(L_DEBUG, "Unknown libinput event %d", event_type);
|
||||
break;
|
||||
}
|
||||
}
|
@ -13,16 +13,29 @@ struct state {
|
||||
float color[3];
|
||||
int dec;
|
||||
struct timespec last_frame;
|
||||
|
||||
struct wl_list keyboards;
|
||||
struct wl_listener input_add;
|
||||
struct wl_listener input_remove;
|
||||
|
||||
struct wl_listener output_add;
|
||||
struct wl_listener output_remove;
|
||||
struct wl_list outputs;
|
||||
};
|
||||
|
||||
struct output_state {
|
||||
struct wl_list link;
|
||||
struct wlr_output *output;
|
||||
struct state *state;
|
||||
struct wlr_output *output;
|
||||
struct wl_listener frame;
|
||||
struct wl_list link;
|
||||
};
|
||||
|
||||
struct keyboard_state {
|
||||
struct state *state;
|
||||
struct wlr_input_device *device;
|
||||
struct wl_listener key;
|
||||
struct wl_listener mods;
|
||||
struct wl_list link;
|
||||
};
|
||||
|
||||
void output_frame(struct wl_listener *listener, void *data) {
|
||||
@ -84,41 +97,63 @@ void output_remove(struct wl_listener *listener, void *data) {
|
||||
wl_list_remove(&ostate->frame.link);
|
||||
}
|
||||
|
||||
void input_add(struct wl_listener *listener, void *data) {
|
||||
struct wlr_input_device *device = data;
|
||||
struct state *state = wl_container_of(listener, state, input_add);
|
||||
if (device->type != WLR_INPUT_DEVICE_KEYBOARD) {
|
||||
return;
|
||||
}
|
||||
fprintf(stderr, "Keyboard '%s' (%d:%d) added\n", device->name,
|
||||
device->vendor, device->product);
|
||||
struct keyboard_state *kbstate = calloc(sizeof(struct keyboard_state), 1);
|
||||
kbstate->device = device;
|
||||
wl_list_init(&kbstate->key.link);
|
||||
wl_list_init(&kbstate->mods.link);
|
||||
wl_list_insert(&state->keyboards, &kbstate->link);
|
||||
}
|
||||
|
||||
void input_remove(struct wl_listener *listener, void *data) {
|
||||
struct wlr_input_device *device = data;
|
||||
struct state *state = wl_container_of(listener, state, input_add);
|
||||
if (device->type != WLR_INPUT_DEVICE_KEYBOARD) {
|
||||
return;
|
||||
}
|
||||
struct keyboard_state *kbstate = NULL, *_kbstate;
|
||||
wl_list_for_each(_kbstate, &state->keyboards, link) {
|
||||
if (_kbstate->device == device) {
|
||||
kbstate = kbstate;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!kbstate) {
|
||||
return; // We are unfamiliar with this keyboard
|
||||
}
|
||||
wl_list_remove(&kbstate->link);
|
||||
//wl_list_remove(&kbstate->key.link);
|
||||
//wl_list_remove(&kbstate->mods.link);
|
||||
}
|
||||
|
||||
int timer_done(void *data) {
|
||||
*(bool *)data = true;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int enable_outputs(void *data) {
|
||||
struct state *state = data;
|
||||
struct output_state *ostate;
|
||||
wl_list_for_each(ostate, &state->outputs, link) {
|
||||
struct wlr_output *output = ostate->output;
|
||||
wlr_output_enable(output, true);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int disable_outputs(void *data) {
|
||||
struct state *state = data;
|
||||
struct output_state *ostate;
|
||||
wl_list_for_each(ostate, &state->outputs, link) {
|
||||
struct wlr_output *output = ostate->output;
|
||||
wlr_output_enable(output, false);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main() {
|
||||
struct state state = {
|
||||
.color = { 1.0, 0.0, 0.0 },
|
||||
.dec = 0,
|
||||
.input_add = { .notify = input_add },
|
||||
.input_remove = { .notify = input_remove },
|
||||
.output_add = { .notify = output_add },
|
||||
.output_remove = { .notify = output_remove }
|
||||
};
|
||||
|
||||
wl_list_init(&state.keyboards);
|
||||
wl_list_init(&state.input_add.link);
|
||||
wl_list_init(&state.input_remove.link);
|
||||
|
||||
wl_list_init(&state.outputs);
|
||||
wl_list_init(&state.output_add.link);
|
||||
wl_list_init(&state.output_remove.link);
|
||||
wl_list_init(&state.output_remove.link);
|
||||
clock_gettime(CLOCK_MONOTONIC, &state.last_frame);
|
||||
|
||||
@ -131,6 +166,8 @@ int main() {
|
||||
}
|
||||
|
||||
struct wlr_backend *wlr = wlr_backend_autocreate(display, session);
|
||||
wl_signal_add(&wlr->events.input_add, &state.input_add);
|
||||
wl_signal_add(&wlr->events.input_remove, &state.input_remove);
|
||||
wl_signal_add(&wlr->events.output_add, &state.output_add);
|
||||
wl_signal_add(&wlr->events.output_remove, &state.output_remove);
|
||||
if (!wlr || !wlr_backend_init(wlr)) {
|
||||
@ -140,14 +177,8 @@ int main() {
|
||||
bool done = false;
|
||||
struct wl_event_source *timer = wl_event_loop_add_timer(event_loop,
|
||||
timer_done, &done);
|
||||
struct wl_event_source *timer_disable_outputs =
|
||||
wl_event_loop_add_timer(event_loop, disable_outputs, &state);
|
||||
struct wl_event_source *timer_enable_outputs =
|
||||
wl_event_loop_add_timer(event_loop, enable_outputs, &state);
|
||||
|
||||
wl_event_source_timer_update(timer, 20000);
|
||||
wl_event_source_timer_update(timer_disable_outputs, 5000);
|
||||
wl_event_source_timer_update(timer_enable_outputs, 10000);
|
||||
wl_event_source_timer_update(timer, 10000);
|
||||
|
||||
while (!done) {
|
||||
wl_event_loop_dispatch(event_loop, 0);
|
||||
|
@ -15,10 +15,14 @@ struct wlr_backend_state {
|
||||
struct libinput *libinput;
|
||||
struct wl_event_source *input_event;
|
||||
|
||||
list_t *devices;
|
||||
list_t *keyboards;
|
||||
};
|
||||
|
||||
void wlr_libinput_event(struct wlr_backend_state *state,
|
||||
struct libinput_event *event);
|
||||
|
||||
struct wlr_keyboard_state {
|
||||
struct libinput_device *handle;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -18,4 +18,25 @@ struct wlr_output *wlr_output_create(struct wlr_output_impl *impl,
|
||||
struct wlr_output_state *state);
|
||||
void wlr_output_free(struct wlr_output *output);
|
||||
|
||||
struct wlr_keyboard_impl {
|
||||
void (*destroy)(struct wlr_keyboard_state *state);
|
||||
};
|
||||
|
||||
struct wlr_keyboard *wlr_keyboard_create(struct wlr_keyboard_impl *impl,
|
||||
struct wlr_keyboard_state *state);
|
||||
void wlr_keyboard_destroy(struct wlr_keyboard *keyboard);
|
||||
|
||||
struct wlr_pointer_impl {
|
||||
void (*destroy)(struct wlr_pointer_state *state);
|
||||
};
|
||||
|
||||
struct wlr_pointer *wlr_pointer_create(struct wlr_pointer_impl *impl,
|
||||
struct wlr_pointer_state *state);
|
||||
void wlr_pointer_destroy(struct wlr_pointer *pointer);
|
||||
|
||||
struct wlr_input_device *wlr_input_device_create(
|
||||
enum wlr_input_device_type type, const char *name,
|
||||
int vendor, int product);
|
||||
void wlr_input_device_destroy(struct wlr_input_device *dev);
|
||||
|
||||
#endif
|
||||
|
@ -12,14 +12,10 @@ struct wlr_backend {
|
||||
struct wlr_backend_state *state;
|
||||
|
||||
struct {
|
||||
struct wl_signal input_add;
|
||||
struct wl_signal input_remove;
|
||||
struct wl_signal output_add;
|
||||
struct wl_signal output_remove;
|
||||
struct wl_signal keyboard_add;
|
||||
struct wl_signal keyboard_remove;
|
||||
struct wl_signal pointer_add;
|
||||
struct wl_signal pointer_remove;
|
||||
struct wl_signal touch_add;
|
||||
struct wl_signal touch_remove;
|
||||
} events;
|
||||
};
|
||||
|
||||
|
@ -50,4 +50,97 @@ void wlr_output_destroy(struct wlr_output *output);
|
||||
void wlr_output_effective_resolution(struct wlr_output *output,
|
||||
int *width, int *height);
|
||||
|
||||
// TODO: keymaps
|
||||
|
||||
struct wlr_keyboard_state;
|
||||
struct wlr_keyboard_impl;
|
||||
|
||||
struct wlr_keyboard {
|
||||
struct wlr_keyboard_state *state;
|
||||
struct wlr_keyboard_impl *impl;
|
||||
|
||||
struct {
|
||||
struct wl_signal key;
|
||||
struct wl_signal mods;
|
||||
} events;
|
||||
};
|
||||
|
||||
struct wlr_pointer_state;
|
||||
struct wlr_pointer_impl;
|
||||
|
||||
struct wlr_pointer {
|
||||
struct wlr_pointer_state *state;
|
||||
struct wlr_pointer_impl *impl;
|
||||
|
||||
struct {
|
||||
struct wl_signal motion;
|
||||
struct wl_signal motion_absolute;
|
||||
struct wl_signal button;
|
||||
struct wl_signal axis;
|
||||
} events;
|
||||
};
|
||||
|
||||
struct wlr_pointer_motion {
|
||||
double delta_x, delta_y;
|
||||
};
|
||||
|
||||
struct wlr_pointer_motion_absolute {
|
||||
double x_mm, y_mm;
|
||||
double width_mm, height_mm;
|
||||
};
|
||||
|
||||
enum wlr_button_state {
|
||||
WLR_BUTTON_DEPRESSED,
|
||||
WLR_BUTTON_RELEASED
|
||||
};
|
||||
|
||||
struct wlr_pointer_button {
|
||||
uint32_t button;
|
||||
enum wlr_button_state state;
|
||||
};
|
||||
|
||||
enum wlr_axis_source {
|
||||
WLR_AXIS_SOURCE_WHEEL,
|
||||
WLR_AXIS_SOURCE_FINGER,
|
||||
WLR_AXIS_SOURCE_CONTINUOUS,
|
||||
WLR_AXIS_SOURCE_WHEEL_TILT,
|
||||
};
|
||||
|
||||
enum wlr_axis_orientation {
|
||||
WLR_AXIS_ORIENTATION_VERTICAL,
|
||||
WLR_AXIS_ORIENTATION_HORIZONTAL
|
||||
};
|
||||
|
||||
struct wlr_pointer_axis {
|
||||
enum wlr_axis_source source;
|
||||
enum wlr_axis_orientation orientation;
|
||||
double delta;
|
||||
};
|
||||
|
||||
// TODO: touch
|
||||
// TODO: tablet & tablet tool
|
||||
// TODO: gestures
|
||||
// TODO: switch
|
||||
|
||||
enum wlr_input_device_type {
|
||||
WLR_INPUT_DEVICE_KEYBOARD,
|
||||
WLR_INPUT_DEVICE_POINTER,
|
||||
WLR_INPUT_DEVICE_TOUCH,
|
||||
WLR_INPUT_DEVICE_TABLET_PEN,
|
||||
WLR_INPUT_DEVICE_TABLET_PAD,
|
||||
WLR_INPUT_DEVICE_GESTURE,
|
||||
WLR_INPUT_DEVICE_SWITCH,
|
||||
};
|
||||
|
||||
struct wlr_input_device {
|
||||
enum wlr_input_device_type type;
|
||||
int vendor, product;
|
||||
char *name;
|
||||
union {
|
||||
void *_device;
|
||||
struct wlr_keyboard *keyboard;
|
||||
struct wlr_pointer *pointer;
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -5,6 +5,8 @@ include_directories(
|
||||
|
||||
add_library(wlr-types
|
||||
wlr_output.c
|
||||
wlr_keyboard.c
|
||||
wlr_input_device.c
|
||||
)
|
||||
|
||||
target_link_libraries(wlr-types
|
||||
|
24
types/wlr_input_device.c
Normal file
24
types/wlr_input_device.c
Normal file
@ -0,0 +1,24 @@
|
||||
#define _XOPEN_SOURCE 500
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <wayland-server.h>
|
||||
#include <wlr/types.h>
|
||||
#include <wlr/common/list.h>
|
||||
#include "types.h"
|
||||
|
||||
struct wlr_input_device *wlr_input_device_create(
|
||||
enum wlr_input_device_type type, const char *name,
|
||||
int vendor, int product) {
|
||||
struct wlr_input_device *dev = calloc(1, sizeof(struct wlr_input_device));
|
||||
dev->type = type;
|
||||
dev->name = strdup(name);
|
||||
dev->vendor = vendor;
|
||||
dev->product = product;
|
||||
return dev;
|
||||
}
|
||||
|
||||
void wlr_input_device_destroy(struct wlr_input_device *dev) {
|
||||
if (!dev) return;
|
||||
free(dev->name);
|
||||
free(dev);
|
||||
}
|
22
types/wlr_keyboard.c
Normal file
22
types/wlr_keyboard.c
Normal file
@ -0,0 +1,22 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <wayland-server.h>
|
||||
#include <wlr/types.h>
|
||||
#include <wlr/common/list.h>
|
||||
#include "types.h"
|
||||
|
||||
struct wlr_keyboard *wlr_keyboard_create(struct wlr_keyboard_impl *impl,
|
||||
struct wlr_keyboard_state *state) {
|
||||
struct wlr_keyboard *kb = calloc(1, sizeof(struct wlr_keyboard));
|
||||
kb->impl = impl;
|
||||
kb->state = state;
|
||||
wl_signal_init(&kb->events.key);
|
||||
wl_signal_init(&kb->events.mods);
|
||||
return kb;
|
||||
}
|
||||
|
||||
void wlr_keyboard_destroy(struct wlr_keyboard *kb) {
|
||||
if (!kb) return;
|
||||
kb->impl->destroy(kb->state);
|
||||
free(kb);
|
||||
}
|
Loading…
Reference in New Issue
Block a user