From 1262f1400c6f443a1460e168c7adc25997247cde Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 9 Jun 2017 11:28:10 -0400 Subject: Initial pass on libinput backend --- backend/libinput/backend.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 backend/libinput/backend.c (limited to 'backend/libinput/backend.c') diff --git a/backend/libinput/backend.c b/backend/libinput/backend.c new file mode 100644 index 00000000..6142bde3 --- /dev/null +++ b/backend/libinput/backend.c @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include +#include "backend/udev.h" +#include "backend/libinput/backend.h" +#include "common/log.h" + +static int wlr_libinput_open_restricted(const char *path, + int flags, void *_state) { + struct wlr_backend_state *state = _state; + return wlr_session_open_file(state->session, path); +} + +static void wlr_libinput_close_restricted(int fd, void *_state) { + struct wlr_backend_state *state = _state; + wlr_session_close_file(state->session, fd); +} + +static const struct libinput_interface libinput_impl = { + .open_restricted = wlr_libinput_open_restricted, + .close_restricted = wlr_libinput_close_restricted +}; + +static int wlr_libinput_handle_event(int fd, uint32_t mask, void *_state) { + // TODO + return 0; +} + +static void wlr_libinput_log(struct libinput *libinput, + enum libinput_log_priority priority, const char *fmt, va_list args) { + _wlr_vlog(L_ERROR, fmt, args); +} + +static bool wlr_libinput_backend_init(struct wlr_backend_state *state) { + state->handle = libinput_udev_create_context(&libinput_impl, state, + state->udev->udev); + if (!state->handle) { + return false; + } + + // TODO: Let user customize seat used + if (!libinput_udev_assign_seat(state->handle, "seat0")) { + return false; + } + + // TODO: More sophisticated logging + libinput_log_set_handler(state->handle, wlr_libinput_log); + libinput_log_set_priority(state->handle, LIBINPUT_LOG_PRIORITY_ERROR); + + struct wl_event_loop *event_loop = + wl_display_get_event_loop(state->display); + if (state->input_event) { + wl_event_source_remove(state->input_event); + } + state->input_event = wl_event_loop_add_fd(event_loop, + libinput_get_fd(state->handle), WL_EVENT_READABLE, + wlr_libinput_handle_event, state); + if (!state->input_event) { + return false; + } + return true; +} + +static void wlr_libinput_backend_destroy(struct wlr_backend_state *state) { + // TODO +} + +static struct wlr_backend_impl backend_impl = { + .init = wlr_libinput_backend_init, + .destroy = wlr_libinput_backend_destroy +}; + +struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display, + struct wlr_session *session, struct wlr_udev *udev) { + assert(display && session && udev); + + struct wlr_backend_state *state = calloc(1, sizeof(struct wlr_backend_state)); + if (!state) { + wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); + return NULL; + } + + struct wlr_backend *backend = wlr_backend_create(&backend_impl, state); + if (!backend) { + wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); + return NULL; + } + + state->backend = backend; + state->session = session; + state->udev = udev; + state->display = display; + + return backend; +} -- cgit v1.2.3 From af69591e6233c83ed749b2f51922edb45bdaef2e Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 9 Jun 2017 11:38:38 -0400 Subject: Add error handling and init in backend_autocreate --- backend/CMakeLists.txt | 2 ++ backend/backend.c | 8 ++++++-- backend/libinput/backend.c | 5 +++++ example/rotation.c | 1 + include/wlr/backend/libinput.h | 12 ++++++++++++ 5 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 include/wlr/backend/libinput.h (limited to 'backend/libinput/backend.c') diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index bca615fb..c3e7ef54 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -2,6 +2,7 @@ include_directories( ${PROTOCOLS_INCLUDE_DIRS} ${WAYLAND_INCLUDE_DIR} ${DRM_INCLUDE_DIRS} + ${LIBINPUT_INCLUDE_DIRS} ) add_library(wlr-backend @@ -30,5 +31,6 @@ target_link_libraries(wlr-backend ${EGL_LIBRARIES} ${SYSTEMD_LIBRARIES} ${UDEV_LIBRARIES} + ${LIBINPUT_LIBRARIES} ${GBM_LIBRARIES} ) diff --git a/backend/backend.c b/backend/backend.c index 38b65dca..1a0c3295 100644 --- a/backend/backend.c +++ b/backend/backend.c @@ -5,8 +5,10 @@ #include #include #include +#include +#include +#include "backend/udev.h" #include "common/log.h" -#include "backend/drm.h" struct wlr_backend *wlr_backend_create(const struct wlr_backend_impl *impl, struct wlr_backend_state *state) { @@ -46,12 +48,14 @@ struct wlr_backend *wlr_backend_autocreate(struct wl_display *display, wlr_log(L_ERROR, "Failed to start udev"); goto error; } + struct wlr_backend *wlr; + wlr = wlr_libinput_backend_create(display, session, udev); + return wlr; int gpu = wlr_udev_find_gpu(udev, session); if (gpu == -1) { wlr_log(L_ERROR, "Failed to open DRM device"); goto error_udev; } - struct wlr_backend *wlr; wlr = wlr_drm_backend_create(display, session, udev, gpu); if (!wlr) { goto error_gpu; diff --git a/backend/libinput/backend.c b/backend/libinput/backend.c index 6142bde3..b49bf6b4 100644 --- a/backend/libinput/backend.c +++ b/backend/libinput/backend.c @@ -34,14 +34,17 @@ static void wlr_libinput_log(struct libinput *libinput, } static bool wlr_libinput_backend_init(struct wlr_backend_state *state) { + wlr_log(L_DEBUG, "Initializing libinput"); state->handle = libinput_udev_create_context(&libinput_impl, state, state->udev->udev); if (!state->handle) { + wlr_log(L_ERROR, "Failed to create libinput context"); return false; } // TODO: Let user customize seat used if (!libinput_udev_assign_seat(state->handle, "seat0")) { + wlr_log(L_ERROR, "Failed to assign libinput seat"); return false; } @@ -58,8 +61,10 @@ static bool wlr_libinput_backend_init(struct wlr_backend_state *state) { libinput_get_fd(state->handle), WL_EVENT_READABLE, wlr_libinput_handle_event, state); if (!state->input_event) { + wlr_log(L_ERROR, "Failed to create input event on event loop"); return false; } + wlr_log(L_DEBUG, "libinput sucessfully initialized"); return true; } diff --git a/example/rotation.c b/example/rotation.c index 25755f5f..12778f8e 100644 --- a/example/rotation.c +++ b/example/rotation.c @@ -219,6 +219,7 @@ int main(int argc, char *argv[]) { wl_signal_add(&wlr->events.output_remove, &state.output_remove); if (!wlr_backend_init(wlr)) { + printf("Failed to initialize backend, bailing out\n"); return 1; } diff --git a/include/wlr/backend/libinput.h b/include/wlr/backend/libinput.h new file mode 100644 index 00000000..1e8c3555 --- /dev/null +++ b/include/wlr/backend/libinput.h @@ -0,0 +1,12 @@ +#ifndef WLR_BACKEND_LIBINPUT_H +#define WLR_BACKEND_LIBINPUT_H + +#include +#include +#include +#include + +struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display, + struct wlr_session *session, struct wlr_udev *udev); + +#endif -- cgit v1.2.3 From 019fff06be1ad460ac427bea070fbe7a61c09ea8 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 9 Jun 2017 13:47:35 -0400 Subject: Fix issues, flesh out event handling somewhat --- backend/libinput/backend.c | 28 +++++++++++++++++++++------- include/backend/libinput/backend.h | 5 ++++- 2 files changed, 25 insertions(+), 8 deletions(-) (limited to 'backend/libinput/backend.c') diff --git a/backend/libinput/backend.c b/backend/libinput/backend.c index b49bf6b4..f6dce58e 100644 --- a/backend/libinput/backend.c +++ b/backend/libinput/backend.c @@ -24,7 +24,21 @@ static const struct libinput_interface libinput_impl = { }; static int wlr_libinput_handle_event(int fd, uint32_t mask, void *_state) { - // TODO + struct wlr_backend_state *state = _state; + if (libinput_dispatch(state->libinput) != 0) { + wlr_log(L_ERROR, "Failed to dispatch libinput"); + // TODO: some kind of abort? + return 0; + } + 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 + } return 0; } @@ -35,22 +49,22 @@ static void wlr_libinput_log(struct libinput *libinput, static bool wlr_libinput_backend_init(struct wlr_backend_state *state) { wlr_log(L_DEBUG, "Initializing libinput"); - state->handle = libinput_udev_create_context(&libinput_impl, state, + state->libinput = libinput_udev_create_context(&libinput_impl, state, state->udev->udev); - if (!state->handle) { + if (!state->libinput) { wlr_log(L_ERROR, "Failed to create libinput context"); return false; } // TODO: Let user customize seat used - if (!libinput_udev_assign_seat(state->handle, "seat0")) { + if (libinput_udev_assign_seat(state->libinput, "seat0") != 0) { wlr_log(L_ERROR, "Failed to assign libinput seat"); return false; } // TODO: More sophisticated logging - libinput_log_set_handler(state->handle, wlr_libinput_log); - libinput_log_set_priority(state->handle, LIBINPUT_LOG_PRIORITY_ERROR); + libinput_log_set_handler(state->libinput, wlr_libinput_log); + libinput_log_set_priority(state->libinput, LIBINPUT_LOG_PRIORITY_ERROR); struct wl_event_loop *event_loop = wl_display_get_event_loop(state->display); @@ -58,7 +72,7 @@ static bool wlr_libinput_backend_init(struct wlr_backend_state *state) { wl_event_source_remove(state->input_event); } state->input_event = wl_event_loop_add_fd(event_loop, - libinput_get_fd(state->handle), WL_EVENT_READABLE, + libinput_get_fd(state->libinput), WL_EVENT_READABLE, wlr_libinput_handle_event, state); if (!state->input_event) { wlr_log(L_ERROR, "Failed to create input event on event loop"); diff --git a/include/backend/libinput/backend.h b/include/backend/libinput/backend.h index bafe76ed..a23a45ac 100644 --- a/include/backend/libinput/backend.h +++ b/include/backend/libinput/backend.h @@ -12,10 +12,13 @@ struct wlr_backend_state { struct wlr_udev *udev; struct wl_display *display; - struct libinput *handle; + struct libinput *libinput; struct wl_event_source *input_event; list_t *devices; }; +void wlr_libinput_event(struct wlr_backend_state *state, + struct libinput_event *event); + #endif -- cgit v1.2.3 From 0e75d157f52db45a1af350574bd95cccbd09fa57 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 9 Jun 2017 17:31:21 -0400 Subject: Initialize keyboards from libinput --- backend/CMakeLists.txt | 1 + backend/backend.c | 8 +-- backend/libinput/backend.c | 13 ++--- backend/libinput/events.c | 100 +++++++++++++++++++++++++++++++++++++ example/simple.c | 87 +++++++++++++++++++++----------- include/backend/libinput/backend.h | 6 ++- include/types.h | 21 ++++++++ include/wlr/backend.h | 8 +-- include/wlr/types.h | 93 ++++++++++++++++++++++++++++++++++ types/CMakeLists.txt | 2 + types/wlr_input_device.c | 24 +++++++++ types/wlr_keyboard.c | 22 ++++++++ 12 files changed, 336 insertions(+), 49 deletions(-) create mode 100644 backend/libinput/events.c create mode 100644 types/wlr_input_device.c create mode 100644 types/wlr_keyboard.c (limited to 'backend/libinput/backend.c') diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index c3e7ef54..e22032c3 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -15,6 +15,7 @@ add_library(wlr-backend drm/drm.c libinput/backend.c + libinput/events.c backend.c egl.c diff --git a/backend/backend.c b/backend/backend.c index 1a0c3295..83249a8c 100644 --- a/backend/backend.c +++ b/backend/backend.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; } diff --git a/backend/libinput/backend.c b/backend/libinput/backend.c index f6dce58e..3af08875 100644 --- a/backend/libinput/backend.c +++ b/backend/libinput/backend.c @@ -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; } diff --git a/backend/libinput/events.c b/backend/libinput/events.c new file mode 100644 index 00000000..b4816928 --- /dev/null +++ b/backend/libinput/events.c @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include +#include +#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; + } +} diff --git a/example/simple.c b/example/simple.c index 21f333c7..3206b3fa 100644 --- a/example/simple.c +++ b/example/simple.c @@ -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,28 +97,44 @@ void output_remove(struct wl_listener *listener, void *data) { wl_list_remove(&ostate->frame.link); } -int timer_done(void *data) { - *(bool *)data = true; - return 1; +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); } -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); +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; } - return 1; + 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 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); - } +int timer_done(void *data) { + *(bool *)data = true; return 1; } @@ -113,12 +142,18 @@ 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); diff --git a/include/backend/libinput/backend.h b/include/backend/libinput/backend.h index a23a45ac..6f2fa2a0 100644 --- a/include/backend/libinput/backend.h +++ b/include/backend/libinput/backend.h @@ -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 diff --git a/include/types.h b/include/types.h index d2c8d008..564b24ec 100644 --- a/include/types.h +++ b/include/types.h @@ -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 diff --git a/include/wlr/backend.h b/include/wlr/backend.h index 32d96a8d..db85c169 100644 --- a/include/wlr/backend.h +++ b/include/wlr/backend.h @@ -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; }; diff --git a/include/wlr/types.h b/include/wlr/types.h index 51ea45cf..a65b5d9c 100644 --- a/include/wlr/types.h +++ b/include/wlr/types.h @@ -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 diff --git a/types/CMakeLists.txt b/types/CMakeLists.txt index eba92cc0..5ac1baf6 100644 --- a/types/CMakeLists.txt +++ b/types/CMakeLists.txt @@ -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 diff --git a/types/wlr_input_device.c b/types/wlr_input_device.c new file mode 100644 index 00000000..7cbe2f7e --- /dev/null +++ b/types/wlr_input_device.c @@ -0,0 +1,24 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include +#include +#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); +} diff --git a/types/wlr_keyboard.c b/types/wlr_keyboard.c new file mode 100644 index 00000000..d102311b --- /dev/null +++ b/types/wlr_keyboard.c @@ -0,0 +1,22 @@ +#include +#include +#include +#include +#include +#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); +} -- cgit v1.2.3 From 59ceaf507e9a7c80e04460d1ae4946ce7b6f56ac Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 9 Jun 2017 17:52:11 -0400 Subject: Handle key presses --- backend/libinput/backend.c | 14 +++++++++--- backend/libinput/events.c | 56 ++++++++++++++++++++++++++++++++++++++++++---- example/simple.c | 9 ++++++++ include/wlr/types.h | 26 ++++++++++++++++++--- 4 files changed, 95 insertions(+), 10 deletions(-) (limited to 'backend/libinput/backend.c') diff --git a/backend/libinput/backend.c b/backend/libinput/backend.c index 3af08875..dc018b10 100644 --- a/backend/libinput/backend.c +++ b/backend/libinput/backend.c @@ -99,7 +99,12 @@ struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display, struct wlr_backend *backend = wlr_backend_create(&backend_impl, state); if (!backend) { wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); - return NULL; + goto error_state; + } + + if (!(state->keyboards = list_create())) { + wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); + goto error_backend; } state->backend = backend; @@ -107,7 +112,10 @@ struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display, state->udev = udev; state->display = display; - state->keyboards = list_create(); - return backend; +error_state: + free(state); +error_backend: + wlr_backend_destroy(backend); + return NULL; } diff --git a/backend/libinput/events.c b/backend/libinput/events.c index b4816928..c31632b5 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -8,6 +8,22 @@ #include "common/log.h" #include "types.h" +static struct wlr_input_device *get_appropriate_device( + enum wlr_input_device_type desired_type, + struct libinput_device *device) { + list_t *devices = libinput_device_get_user_data(device); + if (!devices) { + return NULL; + } + for (size_t i = 0; i < devices->length; ++i) { + struct wlr_input_device *dev = devices->items[i]; + if (dev->type == desired_type) { + return dev; + } + } + return NULL; +} + static void wlr_libinput_keyboard_destroy(struct wlr_keyboard_state *state) { free(state); } @@ -22,10 +38,39 @@ static struct wlr_keyboard *wlr_libinput_keyboard_create( struct wlr_keyboard_state *kbstate = calloc(1, sizeof(struct wlr_keyboard_state)); kbstate->handle = device; + libinput_device_ref(device); return wlr_keyboard_create(&keyboard_impl, kbstate); } -static void device_added(struct wlr_backend_state *state, +static void handle_keyboard_key(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_KEYBOARD, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a keyboard event for a device with no keyboards?"); + return; + } + struct libinput_event_keyboard *kbevent = + libinput_event_get_keyboard_event(event); + struct wlr_keyboard_key *wlr_event = + calloc(1, sizeof(struct wlr_keyboard_key)); + wlr_event->time_sec = libinput_event_keyboard_get_time(kbevent); + wlr_event->time_usec = libinput_event_keyboard_get_time_usec(kbevent); + wlr_event->keycode = libinput_event_keyboard_get_key(kbevent); + enum libinput_key_state state = + libinput_event_keyboard_get_key_state(kbevent); + switch (state) { + case LIBINPUT_KEY_STATE_RELEASED: + wlr_event->state = WLR_KEY_RELEASED; + break; + case LIBINPUT_KEY_STATE_PRESSED: + wlr_event->state = WLR_KEY_PRESSED; + break; + } + wl_signal_emit(&dev->keyboard->events.key, wlr_event); +} + +static void handle_device_added(struct wlr_backend_state *state, struct libinput_device *device) { assert(state && device); /* @@ -73,7 +118,7 @@ static void device_added(struct wlr_backend_state *state, } } -static void device_removed(struct wlr_backend_state *state, +static void handle_device_removed(struct wlr_backend_state *state, struct libinput_device *device) { wlr_log(L_DEBUG, "libinput device removed"); // TODO @@ -88,10 +133,13 @@ void wlr_libinput_event(struct wlr_backend_state *state, (void)context; switch (event_type) { case LIBINPUT_EVENT_DEVICE_ADDED: - device_added(state, device); + handle_device_added(state, device); break; case LIBINPUT_EVENT_DEVICE_REMOVED: - device_removed(state, device); + handle_device_removed(state, device); + break; + case LIBINPUT_EVENT_KEYBOARD_KEY: + handle_keyboard_key(event, device); break; default: wlr_log(L_DEBUG, "Unknown libinput event %d", event_type); diff --git a/example/simple.c b/example/simple.c index 3206b3fa..660c56a6 100644 --- a/example/simple.c +++ b/example/simple.c @@ -97,6 +97,13 @@ void output_remove(struct wl_listener *listener, void *data) { wl_list_remove(&ostate->frame.link); } +static void keyboard_key(struct wl_listener *listener, void *data) { + struct wlr_keyboard_key *event = data; + struct keyboard_state *kbstate = wl_container_of(listener, kbstate, key); + fprintf(stderr, "Key event: %u %s\n", event->keycode, + event->state == WLR_KEY_PRESSED ? "pressed" : "released"); +} + 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); @@ -109,6 +116,8 @@ void input_add(struct wl_listener *listener, void *data) { kbstate->device = device; wl_list_init(&kbstate->key.link); wl_list_init(&kbstate->mods.link); + kbstate->key.notify = keyboard_key; + wl_signal_add(&device->keyboard->events.key, &kbstate->key); wl_list_insert(&state->keyboards, &kbstate->link); } diff --git a/include/wlr/types.h b/include/wlr/types.h index a65b5d9c..e2f3075f 100644 --- a/include/wlr/types.h +++ b/include/wlr/types.h @@ -65,6 +65,18 @@ struct wlr_keyboard { } events; }; +enum wlr_key_state { + WLR_KEY_RELEASED, + WLR_KEY_PRESSED, +}; + +struct wlr_keyboard_key { + uint32_t time_sec; + uint64_t time_usec; + uint32_t keycode; + enum wlr_key_state state; +}; + struct wlr_pointer_state; struct wlr_pointer_impl; @@ -81,20 +93,26 @@ struct wlr_pointer { }; struct wlr_pointer_motion { + uint32_t time_sec; + uint64_t time_usec; double delta_x, delta_y; }; struct wlr_pointer_motion_absolute { + uint32_t time_sec; + uint64_t time_usec; double x_mm, y_mm; double width_mm, height_mm; }; enum wlr_button_state { - WLR_BUTTON_DEPRESSED, - WLR_BUTTON_RELEASED + WLR_BUTTON_RELEASED, + WLR_BUTTON_PRESSED, }; struct wlr_pointer_button { + uint32_t time_sec; + uint64_t time_usec; uint32_t button; enum wlr_button_state state; }; @@ -108,10 +126,12 @@ enum wlr_axis_source { enum wlr_axis_orientation { WLR_AXIS_ORIENTATION_VERTICAL, - WLR_AXIS_ORIENTATION_HORIZONTAL + WLR_AXIS_ORIENTATION_HORIZONTAL, }; struct wlr_pointer_axis { + uint32_t time_sec; + uint64_t time_usec; enum wlr_axis_source source; enum wlr_axis_orientation orientation; double delta; -- cgit v1.2.3 From f479b7c8c7aa93229c46287f774a30ac8324da1e Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 10 Jun 2017 11:58:25 -0400 Subject: Split keyboard code out into its own file --- backend/CMakeLists.txt | 1 + backend/libinput/backend.c | 2 +- backend/libinput/events.c | 50 ++-------------------------------- backend/libinput/keyboard.c | 56 ++++++++++++++++++++++++++++++++++++++ include/backend/libinput.h | 38 ++++++++++++++++++++++++++ include/backend/libinput/backend.h | 28 ------------------- 6 files changed, 98 insertions(+), 77 deletions(-) create mode 100644 backend/libinput/keyboard.c create mode 100644 include/backend/libinput.h delete mode 100644 include/backend/libinput/backend.h (limited to 'backend/libinput/backend.c') diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index e22032c3..5505e417 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -16,6 +16,7 @@ add_library(wlr-backend libinput/backend.c libinput/events.c + libinput/keyboard.c backend.c egl.c diff --git a/backend/libinput/backend.c b/backend/libinput/backend.c index dc018b10..9b0a6d8c 100644 --- a/backend/libinput/backend.c +++ b/backend/libinput/backend.c @@ -4,7 +4,7 @@ #include #include #include "backend/udev.h" -#include "backend/libinput/backend.h" +#include "backend/libinput.h" #include "common/log.h" static int wlr_libinput_open_restricted(const char *path, diff --git a/backend/libinput/events.c b/backend/libinput/events.c index c31632b5..3c623de4 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -4,11 +4,11 @@ #include #include #include -#include "backend/libinput/backend.h" +#include "backend/libinput.h" #include "common/log.h" #include "types.h" -static struct wlr_input_device *get_appropriate_device( +struct wlr_input_device *get_appropriate_device( enum wlr_input_device_type desired_type, struct libinput_device *device) { list_t *devices = libinput_device_get_user_data(device); @@ -24,52 +24,6 @@ static struct wlr_input_device *get_appropriate_device( return NULL; } -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; - libinput_device_ref(device); - return wlr_keyboard_create(&keyboard_impl, kbstate); -} - -static void handle_keyboard_key(struct libinput_event *event, - struct libinput_device *device) { - struct wlr_input_device *dev = - get_appropriate_device(WLR_INPUT_DEVICE_KEYBOARD, device); - if (!dev) { - wlr_log(L_DEBUG, "Got a keyboard event for a device with no keyboards?"); - return; - } - struct libinput_event_keyboard *kbevent = - libinput_event_get_keyboard_event(event); - struct wlr_keyboard_key *wlr_event = - calloc(1, sizeof(struct wlr_keyboard_key)); - wlr_event->time_sec = libinput_event_keyboard_get_time(kbevent); - wlr_event->time_usec = libinput_event_keyboard_get_time_usec(kbevent); - wlr_event->keycode = libinput_event_keyboard_get_key(kbevent); - enum libinput_key_state state = - libinput_event_keyboard_get_key_state(kbevent); - switch (state) { - case LIBINPUT_KEY_STATE_RELEASED: - wlr_event->state = WLR_KEY_RELEASED; - break; - case LIBINPUT_KEY_STATE_PRESSED: - wlr_event->state = WLR_KEY_PRESSED; - break; - } - wl_signal_emit(&dev->keyboard->events.key, wlr_event); -} - static void handle_device_added(struct wlr_backend_state *state, struct libinput_device *device) { assert(state && device); diff --git a/backend/libinput/keyboard.c b/backend/libinput/keyboard.c new file mode 100644 index 00000000..afae960e --- /dev/null +++ b/backend/libinput/keyboard.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +#include +#include "backend/libinput.h" +#include "common/log.h" +#include "types.h" + +static void wlr_libinput_keyboard_destroy(struct wlr_keyboard_state *state) { + libinput_device_unref(state->handle); + free(state); +} + +static struct wlr_keyboard_impl keyboard_impl = { + .destroy = wlr_libinput_keyboard_destroy +}; + +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; + libinput_device_ref(device); + return wlr_keyboard_create(&keyboard_impl, kbstate); +} + +void handle_keyboard_key(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_KEYBOARD, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a keyboard event for a device with no keyboards?"); + return; + } + struct libinput_event_keyboard *kbevent = + libinput_event_get_keyboard_event(event); + struct wlr_keyboard_key *wlr_event = + calloc(1, sizeof(struct wlr_keyboard_key)); + wlr_event->time_sec = libinput_event_keyboard_get_time(kbevent); + wlr_event->time_usec = libinput_event_keyboard_get_time_usec(kbevent); + wlr_event->keycode = libinput_event_keyboard_get_key(kbevent); + enum libinput_key_state state = + libinput_event_keyboard_get_key_state(kbevent); + switch (state) { + case LIBINPUT_KEY_STATE_RELEASED: + wlr_event->state = WLR_KEY_RELEASED; + break; + case LIBINPUT_KEY_STATE_PRESSED: + wlr_event->state = WLR_KEY_PRESSED; + break; + } + wl_signal_emit(&dev->keyboard->events.key, wlr_event); +} diff --git a/include/backend/libinput.h b/include/backend/libinput.h new file mode 100644 index 00000000..a59ab401 --- /dev/null +++ b/include/backend/libinput.h @@ -0,0 +1,38 @@ +#ifndef _WLR_BACKEND_LIBINPUT_INTERNAL_H +#define _WLR_BACKEND_LIBINPUT_INTERNAL_H +#include +#include +#include +#include +#include "backend/udev.h" +#include "types.h" + +struct wlr_backend_state { + struct wlr_backend *backend; + struct wlr_session *session; + struct wlr_udev *udev; + struct wl_display *display; + + struct libinput *libinput; + struct wl_event_source *input_event; + + list_t *keyboards; +}; + +void wlr_libinput_event(struct wlr_backend_state *state, + struct libinput_event *event); + +struct wlr_input_device *get_appropriate_device( + enum wlr_input_device_type desired_type, + struct libinput_device *device); + +struct wlr_keyboard_state { + struct libinput_device *handle; +}; + +void handle_keyboard_key(struct libinput_event *event, + struct libinput_device *device); +struct wlr_keyboard *wlr_libinput_keyboard_create( + struct libinput_device *device); + +#endif diff --git a/include/backend/libinput/backend.h b/include/backend/libinput/backend.h deleted file mode 100644 index 6f2fa2a0..00000000 --- a/include/backend/libinput/backend.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef _WLR_BACKEND_LIBINPUT_INTERNAL_H -#define _WLR_BACKEND_LIBINPUT_INTERNAL_H -#include -#include -#include -#include -#include "backend/udev.h" - -struct wlr_backend_state { - struct wlr_backend *backend; - struct wlr_session *session; - struct wlr_udev *udev; - struct wl_display *display; - - struct libinput *libinput; - struct wl_event_source *input_event; - - 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 -- cgit v1.2.3 From d6905f86cb9d430e0ba05c6a066ed350761116d1 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 14 Jun 2017 11:40:03 -0400 Subject: Allocate wlr_touch devices --- backend/CMakeLists.txt | 1 + backend/libinput/backend.c | 2 +- backend/libinput/events.c | 17 +++++++++++------ backend/libinput/touch.c | 15 +++++++++++++++ include/backend/libinput.h | 15 ++++++++++++++- include/types.h | 8 ++++++++ include/wlr/types.h | 18 +++++++++++++++++- types/CMakeLists.txt | 1 + types/wlr_keyboard.c | 4 +++- types/wlr_pointer.c | 10 ++++++---- types/wlr_touch.c | 27 +++++++++++++++++++++++++++ 11 files changed, 104 insertions(+), 14 deletions(-) create mode 100644 backend/libinput/touch.c create mode 100644 types/wlr_touch.c (limited to 'backend/libinput/backend.c') diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index b8df9373..8272e039 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -18,6 +18,7 @@ add_library(wlr-backend libinput/events.c libinput/keyboard.c libinput/pointer.c + libinput/touch.c multi/backend.c backend.c diff --git a/backend/libinput/backend.c b/backend/libinput/backend.c index 9b0a6d8c..82ba44a6 100644 --- a/backend/libinput/backend.c +++ b/backend/libinput/backend.c @@ -102,7 +102,7 @@ struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display, goto error_state; } - if (!(state->keyboards = list_create())) { + if (!(state->devices = list_create())) { wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); goto error_backend; } diff --git a/backend/libinput/events.c b/backend/libinput/events.c index 5ef58a3e..4af8082f 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -33,7 +33,8 @@ static struct wlr_input_device_impl input_device_impl = { .destroy = wlr_libinput_device_destroy }; -static struct wlr_input_device *allocate_device(struct libinput_device *device, +static struct wlr_input_device *allocate_device( + struct wlr_backend_state *state, struct libinput_device *device, list_t *devices, enum wlr_input_device_type type) { int vendor = libinput_device_get_id_vendor(device); int product = libinput_device_get_id_product(device); @@ -46,6 +47,7 @@ static struct wlr_input_device *allocate_device(struct libinput_device *device, type, &input_device_impl, devstate, name, vendor, product); list_add(devices, wlr_device); + list_add(state->devices, wlr_device); return wlr_device; } @@ -65,19 +67,22 @@ static void handle_device_added(struct wlr_backend_state *state, 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 = allocate_device(device, devices, - WLR_INPUT_DEVICE_KEYBOARD); + struct wlr_input_device *wlr_device = allocate_device(state, + device, devices, WLR_INPUT_DEVICE_KEYBOARD); wlr_device->keyboard = wlr_libinput_keyboard_create(device); wl_signal_emit(&state->backend->events.input_add, wlr_device); } if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER)) { - struct wlr_input_device *wlr_device = allocate_device(device, devices, - WLR_INPUT_DEVICE_POINTER); + struct wlr_input_device *wlr_device = allocate_device(state, + device, devices, WLR_INPUT_DEVICE_POINTER); wlr_device->pointer = wlr_libinput_pointer_create(device); wl_signal_emit(&state->backend->events.input_add, wlr_device); } if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH)) { - // TODO + struct wlr_input_device *wlr_device = allocate_device(state, + device, devices, WLR_INPUT_DEVICE_TOUCH); + wlr_device->touch = wlr_libinput_touch_create(device); + wl_signal_emit(&state->backend->events.input_add, wlr_device); } if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_TOOL)) { // TODO diff --git a/backend/libinput/touch.c b/backend/libinput/touch.c new file mode 100644 index 00000000..55e8609a --- /dev/null +++ b/backend/libinput/touch.c @@ -0,0 +1,15 @@ +#include +#include +#include +#include +#include +#include +#include "backend/libinput.h" +#include "common/log.h" +#include "types.h" + +struct wlr_touch *wlr_libinput_touch_create( + struct libinput_device *device) { + assert(device); + return wlr_touch_create(NULL, NULL); +} diff --git a/include/backend/libinput.h b/include/backend/libinput.h index 7c318746..dd24fbb0 100644 --- a/include/backend/libinput.h +++ b/include/backend/libinput.h @@ -16,7 +16,7 @@ struct wlr_backend_state { struct libinput *libinput; struct wl_event_source *input_event; - list_t *keyboards; + list_t *devices; }; struct wlr_input_device_state { @@ -46,4 +46,17 @@ void handle_pointer_button(struct libinput_event *event, void handle_pointer_axis(struct libinput_event *event, struct libinput_device *device); +struct wlr_touch *wlr_libinput_touch_create( + struct libinput_device *device); +void handle_touch_down(struct libinput_event *event, + struct libinput_device *device); +void handle_touch_up(struct libinput_event *event, + struct libinput_device *device); +void handle_touch_motion(struct libinput_event *event, + struct libinput_device *device); +void handle_touch_cancel(struct libinput_event *event, + struct libinput_device *device); +void handle_touch_frame(struct libinput_event *event, + struct libinput_device *device); + #endif diff --git a/include/types.h b/include/types.h index 20f5ed13..5b20e2ed 100644 --- a/include/types.h +++ b/include/types.h @@ -34,6 +34,14 @@ 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_touch_impl { + void (*destroy)(struct wlr_touch_state *state); +}; + +struct wlr_touch *wlr_touch_create(struct wlr_touch_impl *impl, + struct wlr_touch_state *state); +void wlr_touch_destroy(struct wlr_touch *touch); + struct wlr_input_device_impl { void (*destroy)(struct wlr_input_device_state *state); }; diff --git a/include/wlr/types.h b/include/wlr/types.h index af23152c..b4d69ccb 100644 --- a/include/wlr/types.h +++ b/include/wlr/types.h @@ -134,7 +134,22 @@ struct wlr_pointer_axis { double delta; }; -// TODO: touch +struct wlr_touch_state; +struct wlr_touch_impl; + +struct wlr_touch { + struct wlr_touch_state *state; + struct wlr_touch_impl *impl; + + struct { + struct wl_signal down; + struct wl_signal up; + struct wl_signal motion; + struct wl_signal cancel; + struct wl_signal frame; + } events; +}; + // TODO: tablet & tablet tool // TODO: gestures // TODO: switch @@ -164,6 +179,7 @@ struct wlr_input_device { void *_device; struct wlr_keyboard *keyboard; struct wlr_pointer *pointer; + struct wlr_touch *touch; }; }; diff --git a/types/CMakeLists.txt b/types/CMakeLists.txt index 91ab9da0..b158db39 100644 --- a/types/CMakeLists.txt +++ b/types/CMakeLists.txt @@ -7,6 +7,7 @@ add_library(wlr-types wlr_output.c wlr_keyboard.c wlr_pointer.c + wlr_touch.c wlr_input_device.c ) diff --git a/types/wlr_keyboard.c b/types/wlr_keyboard.c index db4f7ca7..32728c78 100644 --- a/types/wlr_keyboard.c +++ b/types/wlr_keyboard.c @@ -16,6 +16,8 @@ struct wlr_keyboard *wlr_keyboard_create(struct wlr_keyboard_impl *impl, void wlr_keyboard_destroy(struct wlr_keyboard *kb) { if (!kb) return; - kb->impl->destroy(kb->state); + if (kb->impl) { + kb->impl->destroy(kb->state); + } free(kb); } diff --git a/types/wlr_pointer.c b/types/wlr_pointer.c index b1364c34..9c40fe2f 100644 --- a/types/wlr_pointer.c +++ b/types/wlr_pointer.c @@ -17,8 +17,10 @@ struct wlr_pointer *wlr_pointer_create(struct wlr_pointer_impl *impl, return pointer; } -void wlr_pointer_destroy(struct wlr_pointer *kb) { - if (!kb) return; - kb->impl->destroy(kb->state); - free(kb); +void wlr_pointer_destroy(struct wlr_pointer *pointer) { + if (!pointer) return; + if (pointer->impl) { + pointer->impl->destroy(pointer->state); + } + free(pointer); } diff --git a/types/wlr_touch.c b/types/wlr_touch.c new file mode 100644 index 00000000..9c8b4b94 --- /dev/null +++ b/types/wlr_touch.c @@ -0,0 +1,27 @@ +#include +#include +#include +#include +#include +#include "types.h" + +struct wlr_touch *wlr_touch_create(struct wlr_touch_impl *impl, + struct wlr_touch_state *state) { + struct wlr_touch *touch = calloc(1, sizeof(struct wlr_touch)); + touch->impl = impl; + touch->state = state; + wl_signal_init(&touch->events.down); + wl_signal_init(&touch->events.up); + wl_signal_init(&touch->events.motion); + wl_signal_init(&touch->events.frame); + wl_signal_init(&touch->events.cancel); + return touch; +} + +void wlr_touch_destroy(struct wlr_touch *touch) { + if (!touch) return; + if (touch->impl) { + touch->impl->destroy(touch->state); + } + free(touch); +} -- cgit v1.2.3