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') 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') 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') 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') 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') 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') 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 7a5f35b5bb2a30e46defe9ea1bd610cb06df95e8 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 10 Jun 2017 12:21:54 -0400 Subject: Reassign ownership of libinput handle --- backend/backend.c | 6 ++++++ backend/libinput/events.c | 15 ++++++++++++++- backend/libinput/keyboard.c | 15 +-------------- include/backend/libinput.h | 8 ++++---- include/types.h | 10 ++++++++-- include/wlr/backend/libinput.h | 3 +++ include/wlr/types.h | 7 +++++++ types/wlr_input_device.c | 23 +++++++++++++++++++++-- 8 files changed, 64 insertions(+), 23 deletions(-) (limited to 'backend/libinput') diff --git a/backend/backend.c b/backend/backend.c index 83249a8c..03c662c3 100644 --- a/backend/backend.c +++ b/backend/backend.c @@ -3,10 +3,12 @@ #include #include #include +#include #include #include #include #include +#include "backend/libinput.h" #include "backend/udev.h" #include "common/log.h" @@ -64,3 +66,7 @@ error_udev: error: return NULL; } + +struct libinput_device *wlr_libinput_get_device_handle(struct wlr_input_device *dev) { + return dev->state->handle; +} diff --git a/backend/libinput/events.c b/backend/libinput/events.c index 3c623de4..60a2fd5e 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -24,6 +24,15 @@ struct wlr_input_device *get_appropriate_device( return NULL; } +static void wlr_libinput_device_destroy(struct wlr_input_device_state *state) { + libinput_device_unref(state->handle); + free(state); +} + +static struct wlr_input_device_impl input_device_impl = { + .destroy = wlr_libinput_device_destroy +}; + static void handle_device_added(struct wlr_backend_state *state, struct libinput_device *device) { assert(state && device); @@ -38,10 +47,13 @@ static void handle_device_added(struct wlr_backend_state *state, const char *name = libinput_device_get_name(device); list_t *devices = list_create(); wlr_log(L_DEBUG, "Added %s [%d:%d]", name, vendor, product); + struct wlr_input_device_state *devstate = + calloc(1, sizeof(struct wlr_input_device_state)); 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_INPUT_DEVICE_KEYBOARD, &input_device_impl, devstate, + 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); @@ -68,6 +80,7 @@ static void handle_device_added(struct wlr_backend_state *state, if (devices->length > 0) { libinput_device_set_user_data(device, devices); } else { + wlr_libinput_device_destroy(devstate); list_free(devices); } } diff --git a/backend/libinput/keyboard.c b/backend/libinput/keyboard.c index afae960e..158ded28 100644 --- a/backend/libinput/keyboard.c +++ b/backend/libinput/keyboard.c @@ -8,23 +8,10 @@ #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); + return wlr_keyboard_create(NULL, NULL); } void handle_keyboard_key(struct libinput_event *event, diff --git a/include/backend/libinput.h b/include/backend/libinput.h index a59ab401..f6622695 100644 --- a/include/backend/libinput.h +++ b/include/backend/libinput.h @@ -19,6 +19,10 @@ struct wlr_backend_state { list_t *keyboards; }; +struct wlr_input_device_state { + struct libinput_device *handle; +}; + void wlr_libinput_event(struct wlr_backend_state *state, struct libinput_event *event); @@ -26,10 +30,6 @@ 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( diff --git a/include/types.h b/include/types.h index 564b24ec..20f5ed13 100644 --- a/include/types.h +++ b/include/types.h @@ -34,9 +34,15 @@ 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_impl { + void (*destroy)(struct wlr_input_device_state *state); +}; + struct wlr_input_device *wlr_input_device_create( - enum wlr_input_device_type type, const char *name, - int vendor, int product); + enum wlr_input_device_type type, + struct wlr_input_device_impl *impl, + struct wlr_input_device_state *state, + const char *name, int vendor, int product); void wlr_input_device_destroy(struct wlr_input_device *dev); #endif diff --git a/include/wlr/backend/libinput.h b/include/wlr/backend/libinput.h index 1e8c3555..7108f42c 100644 --- a/include/wlr/backend/libinput.h +++ b/include/wlr/backend/libinput.h @@ -1,12 +1,15 @@ #ifndef WLR_BACKEND_LIBINPUT_H #define WLR_BACKEND_LIBINPUT_H +#include #include #include #include #include +#include struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display, struct wlr_session *session, struct wlr_udev *udev); +struct libinput_device *wlr_libinput_get_device_handle(struct wlr_input_device *dev); #endif diff --git a/include/wlr/types.h b/include/wlr/types.h index e2f3075f..389989e8 100644 --- a/include/wlr/types.h +++ b/include/wlr/types.h @@ -152,10 +152,17 @@ enum wlr_input_device_type { WLR_INPUT_DEVICE_SWITCH, }; +struct wlr_input_device_state; +struct wlr_input_device_impl; + struct wlr_input_device { + struct wlr_input_device_state *state; + struct wlr_input_device_impl *impl; + enum wlr_input_device_type type; int vendor, product; char *name; + union { void *_device; struct wlr_keyboard *keyboard; diff --git a/types/wlr_input_device.c b/types/wlr_input_device.c index 7cbe2f7e..dbb25487 100644 --- a/types/wlr_input_device.c +++ b/types/wlr_input_device.c @@ -4,13 +4,18 @@ #include #include #include +#include "common/log.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) { + enum wlr_input_device_type type, + struct wlr_input_device_impl *impl, + struct wlr_input_device_state *state, + const char *name, int vendor, int product) { struct wlr_input_device *dev = calloc(1, sizeof(struct wlr_input_device)); dev->type = type; + dev->impl = impl; + dev->state = state; dev->name = strdup(name); dev->vendor = vendor; dev->product = product; @@ -19,6 +24,20 @@ struct wlr_input_device *wlr_input_device_create( void wlr_input_device_destroy(struct wlr_input_device *dev) { if (!dev) return; + if (dev->impl && dev->impl->destroy && dev->state) { + dev->impl->destroy(dev->state); + } + if (dev->_device) { + switch (dev->type) { + case WLR_INPUT_DEVICE_KEYBOARD: + wlr_keyboard_destroy(dev->keyboard); + break; + default: + wlr_log(L_DEBUG, "Warning: leaking memory %p %p %d", + dev->_device, dev, dev->type); + break; + } + } free(dev->name); free(dev); } -- cgit v1.2.3 From 508d135de78a483feb947ee4cc5e979743bd7317 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 10 Jun 2017 12:24:35 -0400 Subject: Only allocate device state when it will be used This design also avoids double frees in the future when we're creating several wlr_input_devices from one libinput_device. --- backend/libinput/events.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'backend/libinput') diff --git a/backend/libinput/events.c b/backend/libinput/events.c index 60a2fd5e..2ce368e3 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -47,10 +47,12 @@ static void handle_device_added(struct wlr_backend_state *state, const char *name = libinput_device_get_name(device); list_t *devices = list_create(); wlr_log(L_DEBUG, "Added %s [%d:%d]", name, vendor, product); - struct wlr_input_device_state *devstate = - calloc(1, sizeof(struct wlr_input_device_state)); if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD)) { + struct wlr_input_device_state *devstate = + calloc(1, sizeof(struct wlr_input_device_state)); + devstate->handle = device; + libinput_device_ref(device); struct wlr_input_device *wlr_device = wlr_input_device_create( WLR_INPUT_DEVICE_KEYBOARD, &input_device_impl, devstate, name, vendor, product); @@ -80,7 +82,6 @@ static void handle_device_added(struct wlr_backend_state *state, if (devices->length > 0) { libinput_device_set_user_data(device, devices); } else { - wlr_libinput_device_destroy(devstate); list_free(devices); } } -- cgit v1.2.3 From 5dd96c077238b314162570655ea970ba6c2f8693 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Mon, 12 Jun 2017 21:53:41 -0400 Subject: Incorporate XKBCommon into example --- CMake/FindXKBCommon.cmake | 19 ++++++++++++++ CMakeLists.txt | 1 + backend/libinput/keyboard.c | 1 + example/CMakeLists.txt | 3 +++ example/simple.c | 64 ++++++++++++++++++++++++++++++++++++++++----- include/wlr/types.h | 3 --- types/wlr_keyboard.c | 1 - 7 files changed, 81 insertions(+), 11 deletions(-) create mode 100644 CMake/FindXKBCommon.cmake (limited to 'backend/libinput') diff --git a/CMake/FindXKBCommon.cmake b/CMake/FindXKBCommon.cmake new file mode 100644 index 00000000..30ac503a --- /dev/null +++ b/CMake/FindXKBCommon.cmake @@ -0,0 +1,19 @@ +# - Find XKBCommon +# Once done, this will define +# +# XKBCOMMON_FOUND - System has XKBCommon +# XKBCOMMON_INCLUDE_DIRS - The XKBCommon include directories +# XKBCOMMON_LIBRARIES - The libraries needed to use XKBCommon +# XKBCOMMON_DEFINITIONS - Compiler switches required for using XKBCommon + +find_package(PkgConfig) +pkg_check_modules(PC_XKBCOMMON QUIET xkbcommon) +find_path(XKBCOMMON_INCLUDE_DIRS NAMES xkbcommon/xkbcommon.h HINTS ${PC_XKBCOMMON_INCLUDE_DIRS}) +find_library(XKBCOMMON_LIBRARIES NAMES xkbcommon HINTS ${PC_XKBCOMMON_LIBRARY_DIRS}) + +set(XKBCOMMON_DEFINITIONS ${PC_XKBCOMMON_CFLAGS_OTHER}) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(XKBCOMMON DEFAULT_MSG XKBCOMMON_LIBRARIES XKBCOMMON_INCLUDE_DIRS) +mark_as_advanced(XKBCOMMON_LIBRARIES XKBCOMMON_INCLUDE_DIRS) + diff --git a/CMakeLists.txt b/CMakeLists.txt index ae1dc9a1..353e96fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,7 @@ find_package(GLESv2 REQUIRED) find_package(DRM REQUIRED) find_package(GBM REQUIRED) find_package(LibInput REQUIRED) +find_package(XKBCommon REQUIRED) find_package(Udev REQUIRED) find_package(Systemd) diff --git a/backend/libinput/keyboard.c b/backend/libinput/keyboard.c index 158ded28..9ad41a78 100644 --- a/backend/libinput/keyboard.c +++ b/backend/libinput/keyboard.c @@ -11,6 +11,7 @@ struct wlr_keyboard *wlr_libinput_keyboard_create( struct libinput_device *device) { assert(device); + libinput_device_led_update(device, 0); return wlr_keyboard_create(NULL, NULL); } diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index e8f7ec72..bf9efd64 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -1,6 +1,7 @@ include_directories( ${DRM_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR} + ${XKBCOMMON_INCLUDE_DIRS} ) add_executable(simple @@ -10,6 +11,7 @@ add_executable(simple target_link_libraries(simple wlr-backend wlr-session + ${XKBCOMMON_LIBRARIES} ) add_executable(rotation @@ -21,4 +23,5 @@ target_link_libraries(rotation wlr-backend wlr-session wlr-render + ${XKBCOMMON_LIBRARIES} ) diff --git a/example/simple.c b/example/simple.c index c9561a7c..89f74330 100644 --- a/example/simple.c +++ b/example/simple.c @@ -1,4 +1,5 @@ #define _POSIX_C_SOURCE 199309L +#include #include #include #include @@ -8,12 +9,15 @@ #include #include #include +#include struct state { float color[3]; int dec; bool exit; struct timespec last_frame; + struct xkb_keymap *keymap; + struct xkb_state *xkb_state; struct wl_list keyboards; struct wl_listener input_add; @@ -35,7 +39,6 @@ struct keyboard_state { struct state *state; struct wlr_input_device *device; struct wl_listener key; - struct wl_listener mods; struct wl_list link; }; @@ -98,12 +101,30 @@ void output_remove(struct wl_listener *listener, void *data) { wl_list_remove(&ostate->frame.link); } +static void handle_keysym(struct state *state, xkb_keysym_t sym, + enum wlr_key_state key_state) { + char name[64]; + int l = xkb_keysym_get_name(sym, name, sizeof(name)); + if (l != -1 && l != sizeof(name)) { + fprintf(stderr, "Key event: %s %s\n", name, + key_state == WLR_KEY_PRESSED ? "pressed" : "released"); + } + if (sym == XKB_KEY_Escape) { + state->exit = true; + } +} + 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"); - kbstate->state->exit = true; + uint32_t keycode = event->keycode + 8; + const xkb_keysym_t *syms; + int nsyms = xkb_state_key_get_syms(kbstate->state->xkb_state, keycode, &syms); + for (int i = 0; i < nsyms; ++i) { + handle_keysym(kbstate->state, syms[i], event->state); + } + xkb_state_update_key(kbstate->state->xkb_state, keycode, + event->state == WLR_KEY_PRESSED ? XKB_KEY_DOWN : XKB_KEY_UP); } void input_add(struct wl_listener *listener, void *data) { @@ -116,7 +137,6 @@ void input_add(struct wl_listener *listener, void *data) { kbstate->device = device; kbstate->state = state; 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); @@ -140,7 +160,12 @@ void input_remove(struct wl_listener *listener, void *data) { } wl_list_remove(&kbstate->link); wl_list_remove(&kbstate->key.link); - //wl_list_remove(&kbstate->mods.link); +} + +static int timer_done(void *data) { + struct state *state = data; + state->exit = true; + return 1; } int main() { @@ -151,9 +176,30 @@ int main() { .input_add = { .notify = input_add }, .input_remove = { .notify = input_remove }, .output_add = { .notify = output_add }, - .output_remove = { .notify = output_remove } + .output_remove = { .notify = output_remove }, }; + struct xkb_rule_names rules; + memset(&rules, 0, sizeof(rules)); + rules.rules = getenv("XKB_DEFAULT_RULES"); + rules.model = getenv("XKB_DEFAULT_MODEL"); + rules.layout = getenv("XKB_DEFAULT_LAYOUT"); + rules.variant = getenv("XKB_DEFAULT_VARIANT"); + rules.options = getenv("XKB_DEFAULT_OPTIONS"); + struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + if (!context) { + fprintf(stderr, "Failed to create XKB context\n"); + return 1; + } + state.keymap = + xkb_map_new_from_names(context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS); + if (!state.keymap) { + fprintf(stderr, "Failed to create XKB keymap\n"); + return 1; + } + xkb_context_unref(context); + state.xkb_state = xkb_state_new(state.keymap); + wl_list_init(&state.keyboards); wl_list_init(&state.input_add.link); wl_list_init(&state.input_remove.link); @@ -179,6 +225,10 @@ int main() { if (!wlr || !wlr_backend_init(wlr)) { return 1; } + struct wl_event_source *timer = wl_event_loop_add_timer(event_loop, + timer_done, &state); + + wl_event_source_timer_update(timer, 30000); while (!state.exit) { wl_event_loop_dispatch(event_loop, 0); diff --git a/include/wlr/types.h b/include/wlr/types.h index 389989e8..af23152c 100644 --- a/include/wlr/types.h +++ b/include/wlr/types.h @@ -50,8 +50,6 @@ 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; @@ -61,7 +59,6 @@ struct wlr_keyboard { struct { struct wl_signal key; - struct wl_signal mods; } events; }; diff --git a/types/wlr_keyboard.c b/types/wlr_keyboard.c index d102311b..db4f7ca7 100644 --- a/types/wlr_keyboard.c +++ b/types/wlr_keyboard.c @@ -11,7 +11,6 @@ struct wlr_keyboard *wlr_keyboard_create(struct wlr_keyboard_impl *impl, kb->impl = impl; kb->state = state; wl_signal_init(&kb->events.key); - wl_signal_init(&kb->events.mods); return kb; } -- cgit v1.2.3 From a63230e59cfdaad42c3444b2bc4be5480202e527 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Tue, 13 Jun 2017 10:27:15 -0400 Subject: Implement libinput wlr_pointer --- backend/CMakeLists.txt | 1 + backend/libinput/events.c | 43 +++++++++++---- backend/libinput/pointer.c | 129 +++++++++++++++++++++++++++++++++++++++++++++ example/rotation.c | 1 - include/backend/libinput.h | 13 ++++- types/CMakeLists.txt | 1 + types/wlr_pointer.c | 24 +++++++++ 7 files changed, 201 insertions(+), 11 deletions(-) create mode 100644 backend/libinput/pointer.c create mode 100644 types/wlr_pointer.c (limited to 'backend/libinput') diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 23914240..b8df9373 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -17,6 +17,7 @@ add_library(wlr-backend libinput/backend.c libinput/events.c libinput/keyboard.c + libinput/pointer.c multi/backend.c backend.c diff --git a/backend/libinput/events.c b/backend/libinput/events.c index 2ce368e3..5ef58a3e 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -33,6 +33,22 @@ 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, + 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); + const char *name = libinput_device_get_name(device); + struct wlr_input_device_state *devstate = + calloc(1, sizeof(struct wlr_input_device_state)); + devstate->handle = device; + libinput_device_ref(device); + struct wlr_input_device *wlr_device = wlr_input_device_create( + type, &input_device_impl, devstate, + name, vendor, product); + list_add(devices, wlr_device); + return wlr_device; +} + static void handle_device_added(struct wlr_backend_state *state, struct libinput_device *device) { assert(state && device); @@ -49,19 +65,16 @@ 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_state *devstate = - calloc(1, sizeof(struct wlr_input_device_state)); - devstate->handle = device; - libinput_device_ref(device); - struct wlr_input_device *wlr_device = wlr_input_device_create( - WLR_INPUT_DEVICE_KEYBOARD, &input_device_impl, devstate, - name, vendor, product); + struct wlr_input_device *wlr_device = allocate_device(device, devices, + WLR_INPUT_DEVICE_KEYBOARD); 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 + struct wlr_input_device *wlr_device = allocate_device(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 @@ -109,6 +122,18 @@ void wlr_libinput_event(struct wlr_backend_state *state, case LIBINPUT_EVENT_KEYBOARD_KEY: handle_keyboard_key(event, device); break; + case LIBINPUT_EVENT_POINTER_MOTION: + handle_pointer_motion(event, device); + break; + case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE: + handle_pointer_motion_abs(event, device); + break; + case LIBINPUT_EVENT_POINTER_BUTTON: + handle_pointer_button(event, device); + break; + case LIBINPUT_EVENT_POINTER_AXIS: + handle_pointer_axis(event, device); + break; default: wlr_log(L_DEBUG, "Unknown libinput event %d", event_type); break; diff --git a/backend/libinput/pointer.c b/backend/libinput/pointer.c new file mode 100644 index 00000000..0139fd60 --- /dev/null +++ b/backend/libinput/pointer.c @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include +#include +#include "backend/libinput.h" +#include "common/log.h" +#include "types.h" + +struct wlr_pointer *wlr_libinput_pointer_create( + struct libinput_device *device) { + assert(device); + return wlr_pointer_create(NULL, NULL); +} + +void handle_pointer_motion(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_POINTER, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a pointer event for a device with no pointers?"); + return; + } + struct libinput_event_pointer *pevent = + libinput_event_get_pointer_event(event); + struct wlr_pointer_motion *wlr_event = + calloc(1, sizeof(struct wlr_pointer_motion)); + wlr_event->time_sec = libinput_event_pointer_get_time(pevent); + wlr_event->time_usec = libinput_event_pointer_get_time_usec(pevent); + wlr_event->delta_x = libinput_event_pointer_get_dx(pevent); + wlr_event->delta_y = libinput_event_pointer_get_dy(pevent); + wl_signal_emit(&dev->pointer->events.motion, wlr_event); +} + +void handle_pointer_motion_abs(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_POINTER, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a pointer event for a device with no pointers?"); + return; + } + struct libinput_event_pointer *pevent = + libinput_event_get_pointer_event(event); + struct wlr_pointer_motion_absolute *wlr_event = + calloc(1, sizeof(struct wlr_pointer_motion_absolute)); + wlr_event->time_sec = libinput_event_pointer_get_time(pevent); + wlr_event->time_usec = libinput_event_pointer_get_time_usec(pevent); + wlr_event->x_mm = libinput_event_pointer_get_absolute_x(pevent); + wlr_event->y_mm = libinput_event_pointer_get_absolute_y(pevent); + libinput_device_get_size(device, &wlr_event->width_mm, &wlr_event->height_mm); + wl_signal_emit(&dev->pointer->events.motion_absolute, wlr_event); +} + +void handle_pointer_button(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_POINTER, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a pointer event for a device with no pointers?"); + return; + } + struct libinput_event_pointer *pevent = + libinput_event_get_pointer_event(event); + struct wlr_pointer_button *wlr_event = + calloc(1, sizeof(struct wlr_pointer_button)); + wlr_event->time_sec = libinput_event_pointer_get_time(pevent); + wlr_event->time_usec = libinput_event_pointer_get_time_usec(pevent); + wlr_event->button = libinput_event_pointer_get_button(pevent); + switch (libinput_event_pointer_get_button_state(pevent)) { + case LIBINPUT_BUTTON_STATE_PRESSED: + wlr_event->state = WLR_BUTTON_PRESSED; + break; + case LIBINPUT_BUTTON_STATE_RELEASED: + wlr_event->state = WLR_BUTTON_RELEASED; + break; + } + wl_signal_emit(&dev->pointer->events.button, wlr_event); +} + +void handle_pointer_axis(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_POINTER, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a pointer event for a device with no pointers?"); + return; + } + struct libinput_event_pointer *pevent = + libinput_event_get_pointer_event(event); + struct wlr_pointer_axis *wlr_event = + calloc(1, sizeof(struct wlr_pointer_axis)); + wlr_event->time_sec = libinput_event_pointer_get_time(pevent); + wlr_event->time_usec = libinput_event_pointer_get_time_usec(pevent); + switch (libinput_event_pointer_get_axis_source(pevent)) { + case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL: + wlr_event->source = WLR_AXIS_SOURCE_WHEEL; + break; + case LIBINPUT_POINTER_AXIS_SOURCE_FINGER: + wlr_event->source = WLR_AXIS_SOURCE_FINGER; + break; + case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS: + wlr_event->source = WLR_AXIS_SOURCE_CONTINUOUS; + break; + case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL_TILT: + wlr_event->source = WLR_AXIS_SOURCE_WHEEL_TILT; + break; + } + enum libinput_pointer_axis axies[] = { + LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL, + LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL, + }; + for (size_t i = 0; i < sizeof(axies) / sizeof(axies[0]); ++i) { + if (libinput_event_pointer_has_axis(pevent, axies[i])) { + switch (axies[i]) { + case LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL: + wlr_event->orientation = WLR_AXIS_ORIENTATION_VERTICAL; + break; + case LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL: + wlr_event->orientation = WLR_AXIS_ORIENTATION_HORIZONTAL; + break; + } + wlr_event->delta = libinput_event_pointer_get_axis_value( + pevent, axies[i]); + } + wl_signal_emit(&dev->pointer->events.axis, wlr_event); + } +} diff --git a/example/rotation.c b/example/rotation.c index 2634ea03..892083e6 100644 --- a/example/rotation.c +++ b/example/rotation.c @@ -21,7 +21,6 @@ struct sample_state { struct wl_list config; - // TODO: Move renderer into shared struct wlr_renderer *renderer; struct wlr_surface *cat_texture; }; diff --git a/include/backend/libinput.h b/include/backend/libinput.h index f6622695..7c318746 100644 --- a/include/backend/libinput.h +++ b/include/backend/libinput.h @@ -30,9 +30,20 @@ struct wlr_input_device *get_appropriate_device( enum wlr_input_device_type desired_type, struct libinput_device *device); +struct wlr_keyboard *wlr_libinput_keyboard_create( + struct libinput_device *device); void handle_keyboard_key(struct libinput_event *event, struct libinput_device *device); -struct wlr_keyboard *wlr_libinput_keyboard_create( + +struct wlr_pointer *wlr_libinput_pointer_create( + struct libinput_device *device); +void handle_pointer_motion(struct libinput_event *event, + struct libinput_device *device); +void handle_pointer_motion_abs(struct libinput_event *event, + struct libinput_device *device); +void handle_pointer_button(struct libinput_event *event, + struct libinput_device *device); +void handle_pointer_axis(struct libinput_event *event, struct libinput_device *device); #endif diff --git a/types/CMakeLists.txt b/types/CMakeLists.txt index 5ac1baf6..91ab9da0 100644 --- a/types/CMakeLists.txt +++ b/types/CMakeLists.txt @@ -6,6 +6,7 @@ include_directories( add_library(wlr-types wlr_output.c wlr_keyboard.c + wlr_pointer.c wlr_input_device.c ) diff --git a/types/wlr_pointer.c b/types/wlr_pointer.c new file mode 100644 index 00000000..b1364c34 --- /dev/null +++ b/types/wlr_pointer.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include +#include +#include "types.h" + +struct wlr_pointer *wlr_pointer_create(struct wlr_pointer_impl *impl, + struct wlr_pointer_state *state) { + struct wlr_pointer *pointer = calloc(1, sizeof(struct wlr_pointer)); + pointer->impl = impl; + pointer->state = state; + wl_signal_init(&pointer->events.motion); + wl_signal_init(&pointer->events.motion_absolute); + wl_signal_init(&pointer->events.button); + wl_signal_init(&pointer->events.axis); + return pointer; +} + +void wlr_pointer_destroy(struct wlr_pointer *kb) { + if (!kb) return; + kb->impl->destroy(kb->state); + free(kb); +} -- 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') 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 From 3f24f8a1bee10fb3aadf8c57ca107fe5aaa7cffa Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 14 Jun 2017 14:50:09 -0400 Subject: Flesh out touch events and add demo --- backend/libinput/events.c | 15 ++++++ backend/libinput/touch.c | 78 +++++++++++++++++++++++++++++ example/CMakeLists.txt | 13 +++++ example/shared.c | 86 ++++++++++++++++++++++++++++++-- example/shared.h | 18 +++++++ example/touch.c | 122 +++++++++++++++++++++++++++++++++++++++++++++ include/backend/libinput.h | 2 - include/wlr/types.h | 29 ++++++++++- types/wlr_touch.c | 1 - 9 files changed, 356 insertions(+), 8 deletions(-) create mode 100644 example/touch.c (limited to 'backend/libinput') diff --git a/backend/libinput/events.c b/backend/libinput/events.c index 4af8082f..f8413dae 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -139,6 +139,21 @@ void wlr_libinput_event(struct wlr_backend_state *state, case LIBINPUT_EVENT_POINTER_AXIS: handle_pointer_axis(event, device); break; + case LIBINPUT_EVENT_TOUCH_DOWN: + handle_touch_down(event, device); + break; + case LIBINPUT_EVENT_TOUCH_UP: + handle_touch_up(event, device); + break; + case LIBINPUT_EVENT_TOUCH_MOTION: + handle_touch_motion(event, device); + break; + case LIBINPUT_EVENT_TOUCH_CANCEL: + handle_touch_cancel(event, device); + break; + case LIBINPUT_EVENT_TOUCH_FRAME: + // no-op (at least for now) + break; default: wlr_log(L_DEBUG, "Unknown libinput event %d", event_type); break; diff --git a/backend/libinput/touch.c b/backend/libinput/touch.c index 55e8609a..02c9cfef 100644 --- a/backend/libinput/touch.c +++ b/backend/libinput/touch.c @@ -13,3 +13,81 @@ struct wlr_touch *wlr_libinput_touch_create( assert(device); return wlr_touch_create(NULL, NULL); } + +void handle_touch_down(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a touch event for a device with no touch?"); + return; + } + struct libinput_event_touch *tevent = + libinput_event_get_touch_event(event); + struct wlr_touch_down *wlr_event = + calloc(1, sizeof(struct wlr_touch_down)); + wlr_event->time_sec = libinput_event_touch_get_time(tevent); + wlr_event->time_usec = libinput_event_touch_get_time_usec(tevent); + wlr_event->slot = libinput_event_touch_get_slot(tevent); + wlr_event->x_mm = libinput_event_touch_get_x(tevent); + wlr_event->y_mm = libinput_event_touch_get_y(tevent); + libinput_device_get_size(device, &wlr_event->width_mm, &wlr_event->height_mm); + wl_signal_emit(&dev->touch->events.down, wlr_event); +} + +void handle_touch_up(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a touch event for a device with no touch?"); + return; + } + struct libinput_event_touch *tevent = + libinput_event_get_touch_event(event); + struct wlr_touch_up *wlr_event = + calloc(1, sizeof(struct wlr_touch_up)); + wlr_event->time_sec = libinput_event_touch_get_time(tevent); + wlr_event->time_usec = libinput_event_touch_get_time_usec(tevent); + wlr_event->slot = libinput_event_touch_get_slot(tevent); + wl_signal_emit(&dev->touch->events.up, wlr_event); +} + +void handle_touch_motion(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a touch event for a device with no touch?"); + return; + } + struct libinput_event_touch *tevent = + libinput_event_get_touch_event(event); + struct wlr_touch_motion *wlr_event = + calloc(1, sizeof(struct wlr_touch_motion)); + wlr_event->time_sec = libinput_event_touch_get_time(tevent); + wlr_event->time_usec = libinput_event_touch_get_time_usec(tevent); + wlr_event->slot = libinput_event_touch_get_slot(tevent); + wlr_event->x_mm = libinput_event_touch_get_x(tevent); + wlr_event->y_mm = libinput_event_touch_get_y(tevent); + libinput_device_get_size(device, &wlr_event->width_mm, &wlr_event->height_mm); + wl_signal_emit(&dev->touch->events.motion, wlr_event); +} + +void handle_touch_cancel(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a touch event for a device with no touch?"); + return; + } + struct libinput_event_touch *tevent = + libinput_event_get_touch_event(event); + struct wlr_touch_cancel *wlr_event = + calloc(1, sizeof(struct wlr_touch_cancel)); + wlr_event->time_sec = libinput_event_touch_get_time(tevent); + wlr_event->time_usec = libinput_event_touch_get_time_usec(tevent); + wlr_event->slot = libinput_event_touch_get_slot(tevent); + wl_signal_emit(&dev->touch->events.cancel, wlr_event); +} diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index ef11a822..a6085392 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -40,3 +40,16 @@ target_link_libraries(pointer wlr-render ${XKBCOMMON_LIBRARIES} ) + +add_executable(touch + touch.c + shared.c + cat.c +) + +target_link_libraries(touch + wlr-backend + wlr-session + wlr-render + ${XKBCOMMON_LIBRARIES} +) diff --git a/example/shared.c b/example/shared.c index 6de16074..2592c924 100644 --- a/example/shared.c +++ b/example/shared.c @@ -114,6 +114,59 @@ static void pointer_add(struct wlr_input_device *device, struct compositor_state wl_list_insert(&state->pointers, &pstate->link); } +static void touch_down_notify(struct wl_listener *listener, void *data) { + struct wlr_touch_down *event = data; + struct touch_state *tstate = wl_container_of(listener, tstate, down); + if (tstate->compositor->touch_down_cb) { + tstate->compositor->touch_down_cb(tstate, event->slot, + event->x_mm, event->y_mm, event->width_mm, event->height_mm); + } +} + +static void touch_motion_notify(struct wl_listener *listener, void *data) { + struct wlr_touch_motion *event = data; + struct touch_state *tstate = wl_container_of(listener, tstate, motion); + if (tstate->compositor->touch_motion_cb) { + tstate->compositor->touch_motion_cb(tstate, event->slot, + event->x_mm, event->y_mm, event->width_mm, event->height_mm); + } +} + +static void touch_up_notify(struct wl_listener *listener, void *data) { + struct wlr_touch_up *event = data; + struct touch_state *tstate = wl_container_of(listener, tstate, up); + if (tstate->compositor->touch_up_cb) { + tstate->compositor->touch_up_cb(tstate, event->slot); + } +} + +static void touch_cancel_notify(struct wl_listener *listener, void *data) { + struct wlr_touch_cancel *event = data; + struct touch_state *tstate = wl_container_of(listener, tstate, cancel); + if (tstate->compositor->touch_cancel_cb) { + tstate->compositor->touch_cancel_cb(tstate, event->slot); + } +} + +static void touch_add(struct wlr_input_device *device, struct compositor_state *state) { + struct touch_state *tstate = calloc(sizeof(struct touch_state), 1); + tstate->device = device; + tstate->compositor = state; + wl_list_init(&tstate->down.link); + wl_list_init(&tstate->motion.link); + wl_list_init(&tstate->up.link); + wl_list_init(&tstate->cancel.link); + tstate->down.notify = touch_down_notify; + tstate->motion.notify = touch_motion_notify; + tstate->up.notify = touch_up_notify; + tstate->cancel.notify = touch_cancel_notify; + wl_signal_add(&device->touch->events.down, &tstate->down); + wl_signal_add(&device->touch->events.motion, &tstate->motion); + wl_signal_add(&device->touch->events.up, &tstate->up); + wl_signal_add(&device->touch->events.cancel, &tstate->cancel); + wl_list_insert(&state->touch, &tstate->link); +} + static void input_add_notify(struct wl_listener *listener, void *data) { struct wlr_input_device *device = data; struct compositor_state *state = wl_container_of(listener, state, input_add); @@ -124,6 +177,9 @@ static void input_add_notify(struct wl_listener *listener, void *data) { case WLR_INPUT_DEVICE_POINTER: pointer_add(device, state); break; + case WLR_INPUT_DEVICE_TOUCH: + touch_add(device, state); + break; default: break; } @@ -156,10 +212,28 @@ static void pointer_remove(struct wlr_input_device *device, struct compositor_st return; } wl_list_remove(&pstate->link); - //wl_list_remove(&pstate->motion.link); - wl_list_remove(&pstate->motion_absolute.link); - //wl_list_remove(&pstate->button.link); - //wl_list_remove(&pstate->axis.link); + wl_list_remove(&pstate->motion.link); + //wl_list_remove(&pstate->motion_absolute.link); + wl_list_remove(&pstate->button.link); + wl_list_remove(&pstate->axis.link); +} + +static void touch_remove(struct wlr_input_device *device, struct compositor_state *state) { + struct touch_state *tstate = NULL, *_tstate; + wl_list_for_each(_tstate, &state->touch, link) { + if (_tstate->device == device) { + tstate = _tstate; + break; + } + } + if (!tstate) { + return; + } + wl_list_remove(&tstate->link); + wl_list_remove(&tstate->down.link); + wl_list_remove(&tstate->motion.link); + wl_list_remove(&tstate->up.link); + wl_list_remove(&tstate->cancel.link); } static void input_remove_notify(struct wl_listener *listener, void *data) { @@ -172,6 +246,9 @@ static void input_remove_notify(struct wl_listener *listener, void *data) { case WLR_INPUT_DEVICE_POINTER: pointer_remove(device, state); break; + case WLR_INPUT_DEVICE_TOUCH: + touch_remove(device, state); + break; default: break; } @@ -245,6 +322,7 @@ void compositor_init(struct compositor_state *state) { wl_list_init(&state->keyboards); wl_list_init(&state->pointers); + wl_list_init(&state->touch); wl_list_init(&state->input_add.link); state->input_add.notify = input_add_notify; wl_list_init(&state->input_remove.link); diff --git a/example/shared.h b/example/shared.h index 7f0dade5..d09bcd0e 100644 --- a/example/shared.h +++ b/example/shared.h @@ -39,6 +39,17 @@ struct pointer_state { void *data; }; +struct touch_state { + struct compositor_state *compositor; + struct wlr_input_device *device; + struct wl_listener down; + struct wl_listener up; + struct wl_listener motion; + struct wl_listener cancel; + struct wl_list link; + void *data; +}; + struct compositor_state { void (*output_add_cb)(struct output_state *s); void (*keyboard_add_cb)(struct keyboard_state *s); @@ -55,6 +66,12 @@ struct compositor_state { enum wlr_axis_source source, enum wlr_axis_orientation orientation, double delta); + void (*touch_down_cb)(struct touch_state *s, int32_t slot, + double x, double y, double width, double height); + void (*touch_motion_cb)(struct touch_state *s, int32_t slot, + double x, double y, double width, double height); + void (*touch_up_cb)(struct touch_state *s, int32_t slot); + void (*touch_cancel_cb)(struct touch_state *s, int32_t slot); struct wl_display *display; struct wl_event_loop *event_loop; @@ -63,6 +80,7 @@ struct compositor_state { struct wl_list keyboards; struct wl_list pointers; + struct wl_list touch; struct wl_listener input_add; struct wl_listener input_remove; diff --git a/example/touch.c b/example/touch.c new file mode 100644 index 00000000..7f4f3800 --- /dev/null +++ b/example/touch.c @@ -0,0 +1,122 @@ +#define _POSIX_C_SOURCE 199309L +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "shared.h" +#include "cat.h" + +struct sample_state { + struct wlr_renderer *renderer; + struct wlr_surface *cat_texture; + list_t *touch_points; +}; + +struct touch_point { + int32_t slot; + double x, y; +}; + +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; + + int32_t width, height; + wlr_output_effective_resolution(wlr_output, &width, &height); + wlr_renderer_begin(sample->renderer, wlr_output); + + float matrix[16]; + for (size_t i = 0; i < sample->touch_points->length; ++i) { + struct touch_point *p = sample->touch_points->items[i]; + wlr_surface_get_matrix(sample->cat_texture, &matrix, + &wlr_output->transform_matrix, + (int)(p->x * width) - sample->cat_texture->width / 2, + (int)(p->y * height) - sample->cat_texture->height / 2); + wlr_render_with_matrix(sample->renderer, + sample->cat_texture, &matrix); + } + + wlr_renderer_end(sample->renderer); +} + +static void handle_keyboard_key(struct keyboard_state *kbstate, + xkb_keysym_t sym, enum wlr_key_state key_state) { + if (sym == XKB_KEY_Escape) { + kbstate->compositor->exit = true; + } +} + +static void handle_touch_down(struct touch_state *tstate, int32_t slot, + double x, double y, double width, double height) { + struct sample_state *sample = tstate->compositor->data; + struct touch_point *point = calloc(1, sizeof(struct touch_state)); + point->slot = slot; + point->x = x / width; + point->y = y / height; + list_add(sample->touch_points, point); +} + +static void handle_touch_up(struct touch_state *tstate, int32_t slot) { + struct sample_state *sample = tstate->compositor->data; + for (size_t i = 0; i < sample->touch_points->length; ++i) { + struct touch_point *point = sample->touch_points->items[i]; + if (point->slot == slot) { + list_del(sample->touch_points, i); + break; + } + } +} + +static void handle_touch_motion(struct touch_state *tstate, int32_t slot, + double x, double y, double width, double height) { + struct sample_state *sample = tstate->compositor->data; + for (size_t i = 0; i < sample->touch_points->length; ++i) { + struct touch_point *point = sample->touch_points->items[i]; + if (point->slot == slot) { + point->x = x / width; + point->y = y / height; + break; + } + } +} + +int main(int argc, char *argv[]) { + struct sample_state state = { + .touch_points = list_create() + }; + struct compositor_state compositor; + + compositor_init(&compositor); + compositor.output_frame_cb = handle_output_frame; + compositor.keyboard_key_cb = handle_keyboard_key; + compositor.touch_down_cb = handle_touch_down; + compositor.touch_up_cb = handle_touch_up; + compositor.touch_motion_cb = handle_touch_motion; + + state.renderer = wlr_gles3_renderer_init(); + state.cat_texture = wlr_render_surface_init(state.renderer); + wlr_surface_attach_pixels(state.cat_texture, GL_RGB, + cat_tex.width, cat_tex.height, cat_tex.pixel_data); + + compositor.data = &state; + compositor_run(&compositor); + + wlr_surface_destroy(state.cat_texture); + wlr_renderer_destroy(state.renderer); +} diff --git a/include/backend/libinput.h b/include/backend/libinput.h index dd24fbb0..2427ae5c 100644 --- a/include/backend/libinput.h +++ b/include/backend/libinput.h @@ -56,7 +56,5 @@ 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/wlr/types.h b/include/wlr/types.h index b4d69ccb..4763013a 100644 --- a/include/wlr/types.h +++ b/include/wlr/types.h @@ -146,10 +146,37 @@ struct wlr_touch { struct wl_signal up; struct wl_signal motion; struct wl_signal cancel; - struct wl_signal frame; } events; }; +struct wlr_touch_down { + uint32_t time_sec; + uint64_t time_usec; + int32_t slot; + double x_mm, y_mm; + double width_mm, height_mm; +}; + +struct wlr_touch_up { + uint32_t time_sec; + uint64_t time_usec; + int32_t slot; +}; + +struct wlr_touch_motion { + uint32_t time_sec; + uint64_t time_usec; + int32_t slot; + double x_mm, y_mm; + double width_mm, height_mm; +}; + +struct wlr_touch_cancel { + uint32_t time_sec; + uint64_t time_usec; + int32_t slot; +}; + // TODO: tablet & tablet tool // TODO: gestures // TODO: switch diff --git a/types/wlr_touch.c b/types/wlr_touch.c index 9c8b4b94..0970f70f 100644 --- a/types/wlr_touch.c +++ b/types/wlr_touch.c @@ -13,7 +13,6 @@ struct wlr_touch *wlr_touch_create(struct wlr_touch_impl *impl, 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; } -- cgit v1.2.3 From 4a9966b1a47d497ccc9473a24ba56b3d0eef72d2 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 15 Jun 2017 14:32:28 -0400 Subject: Implement wlr_tablet_tool --- backend/CMakeLists.txt | 1 + backend/libinput/events.c | 17 ++++- backend/libinput/tablet_tool.c | 154 +++++++++++++++++++++++++++++++++++++++++ include/backend/libinput.h | 11 +++ include/types.h | 8 +++ include/wlr/types.h | 80 ++++++++++++++++++++- types/CMakeLists.txt | 3 +- types/wlr_tablet_tool.c | 26 +++++++ 8 files changed, 295 insertions(+), 5 deletions(-) create mode 100644 backend/libinput/tablet_tool.c create mode 100644 types/wlr_tablet_tool.c (limited to 'backend/libinput') diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 8272e039..6ffa2042 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -19,6 +19,7 @@ add_library(wlr-backend libinput/keyboard.c libinput/pointer.c libinput/touch.c + libinput/tablet_tool.c multi/backend.c backend.c diff --git a/backend/libinput/events.c b/backend/libinput/events.c index f8413dae..8ebec63b 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -85,7 +85,10 @@ static void handle_device_added(struct wlr_backend_state *state, wl_signal_emit(&state->backend->events.input_add, wlr_device); } if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_TOOL)) { - // TODO + struct wlr_input_device *wlr_device = allocate_device(state, + device, devices, WLR_INPUT_DEVICE_TABLET_TOOL); + wlr_device->tablet_tool = wlr_libinput_tablet_tool_create(device); + wl_signal_emit(&state->backend->events.input_add, wlr_device); } if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_PAD)) { // TODO @@ -154,6 +157,18 @@ void wlr_libinput_event(struct wlr_backend_state *state, case LIBINPUT_EVENT_TOUCH_FRAME: // no-op (at least for now) break; + case LIBINPUT_EVENT_TABLET_TOOL_AXIS: + handle_tablet_tool_axis(event, device); + break; + case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY: + handle_tablet_tool_proximity(event, device); + break; + case LIBINPUT_EVENT_TABLET_TOOL_TIP: + handle_tablet_tool_tip(event, device); + break; + case LIBINPUT_EVENT_TABLET_TOOL_BUTTON: + handle_tablet_tool_button(event, device); + break; default: wlr_log(L_DEBUG, "Unknown libinput event %d", event_type); break; diff --git a/backend/libinput/tablet_tool.c b/backend/libinput/tablet_tool.c new file mode 100644 index 00000000..276c263c --- /dev/null +++ b/backend/libinput/tablet_tool.c @@ -0,0 +1,154 @@ +#include +#include +#include +#include +#include +#include +#include "backend/libinput.h" +#include "common/log.h" +#include "types.h" + +struct wlr_tablet_tool *wlr_libinput_tablet_tool_create( + struct libinput_device *device) { + assert(device); + return wlr_tablet_tool_create(NULL, NULL); +} + +void handle_tablet_tool_axis(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a tablet tool event for a device with no tablet tools?"); + return; + } + struct libinput_event_tablet_tool *tevent = + libinput_event_get_tablet_tool_event(event); + struct wlr_tablet_tool_axis *wlr_event = + calloc(1, sizeof(struct wlr_tablet_tool_axis)); + wlr_event->time_sec = libinput_event_tablet_tool_get_time(tevent); + wlr_event->time_usec = libinput_event_tablet_tool_get_time_usec(tevent); + libinput_device_get_size(device, &wlr_event->width_mm, &wlr_event->height_mm); + if (libinput_event_tablet_tool_x_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_X; + wlr_event->x_mm = libinput_event_tablet_tool_get_x(tevent); + } + if (libinput_event_tablet_tool_y_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_Y; + wlr_event->y_mm = libinput_event_tablet_tool_get_y(tevent); + } + if (libinput_event_tablet_tool_pressure_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_PRESSURE; + wlr_event->pressure = libinput_event_tablet_tool_get_pressure(tevent); + } + if (libinput_event_tablet_tool_distance_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_DISTANCE; + wlr_event->distance = libinput_event_tablet_tool_get_distance(tevent); + } + if (libinput_event_tablet_tool_tilt_x_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_TILT_X; + wlr_event->tilt_x = libinput_event_tablet_tool_get_tilt_x(tevent); + } + if (libinput_event_tablet_tool_tilt_y_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_TILT_Y; + wlr_event->tilt_y = libinput_event_tablet_tool_get_tilt_y(tevent); + } + if (libinput_event_tablet_tool_rotation_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_ROTATION; + wlr_event->rotation = libinput_event_tablet_tool_get_rotation(tevent); + } + if (libinput_event_tablet_tool_slider_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_SLIDER; + wlr_event->slider = libinput_event_tablet_tool_get_slider_position(tevent); + } + if (libinput_event_tablet_tool_wheel_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_WHEEL; + wlr_event->wheel_delta = libinput_event_tablet_tool_get_wheel_delta(tevent); + } + wl_signal_emit(&dev->tablet_tool->events.axis, wlr_event); +} + +void handle_tablet_tool_proximity(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a tablet tool event for a device with no tablet tools?"); + return; + } + // Proximity events contain axis information. We update this information + // before we send the proximity event + handle_tablet_tool_axis(event, device); + struct libinput_event_tablet_tool *tevent = + libinput_event_get_tablet_tool_event(event); + struct wlr_tablet_tool_proximity *wlr_event = + calloc(1, sizeof(struct wlr_tablet_tool_proximity)); + wlr_event->time_sec = libinput_event_tablet_tool_get_time(tevent); + wlr_event->time_usec = libinput_event_tablet_tool_get_time_usec(tevent); + switch (libinput_event_tablet_tool_get_proximity_state(tevent)) { + case LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT: + wlr_event->state = WLR_TABLET_TOOL_PROXIMITY_OUT; + break; + case LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN: + wlr_event->state = WLR_TABLET_TOOL_PROXIMITY_IN; + break; + } + wl_signal_emit(&dev->tablet_tool->events.proximity, wlr_event); +} + +void handle_tablet_tool_tip(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a tablet tool event for a device with no tablet tools?"); + return; + } + // Tip events contain axis information. We update this information + // before we send the proximity event + handle_tablet_tool_axis(event, device); + struct libinput_event_tablet_tool *tevent = + libinput_event_get_tablet_tool_event(event); + struct wlr_tablet_tool_tip *wlr_event = + calloc(1, sizeof(struct wlr_tablet_tool_tip)); + wlr_event->time_sec = libinput_event_tablet_tool_get_time(tevent); + wlr_event->time_usec = libinput_event_tablet_tool_get_time_usec(tevent); + switch (libinput_event_tablet_tool_get_tip_state(tevent)) { + case LIBINPUT_TABLET_TOOL_TIP_UP: + wlr_event->state = WLR_TABLET_TOOL_TIP_UP; + break; + case LIBINPUT_TABLET_TOOL_TIP_DOWN: + wlr_event->state = WLR_TABLET_TOOL_TIP_DOWN; + break; + } + wl_signal_emit(&dev->tablet_tool->events.tip, wlr_event); +} + +void handle_tablet_tool_button(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a tablet tool event for a device with no tablet tools?"); + return; + } + // Tip events contain axis information. We update this information + // before we send the proximity event + handle_tablet_tool_axis(event, device); + struct libinput_event_tablet_tool *tevent = + libinput_event_get_tablet_tool_event(event); + struct wlr_tablet_tool_button *wlr_event = + calloc(1, sizeof(struct wlr_tablet_tool_button)); + wlr_event->time_sec = libinput_event_tablet_tool_get_time(tevent); + wlr_event->time_usec = libinput_event_tablet_tool_get_time_usec(tevent); + wlr_event->button = libinput_event_tablet_tool_get_button(tevent); + switch (libinput_event_tablet_tool_get_button_state(tevent)) { + case LIBINPUT_BUTTON_STATE_RELEASED: + wlr_event->state = WLR_BUTTON_RELEASED; + break; + case LIBINPUT_BUTTON_STATE_PRESSED: + wlr_event->state = WLR_BUTTON_PRESSED; + break; + } + wl_signal_emit(&dev->tablet_tool->events.button, wlr_event); +} diff --git a/include/backend/libinput.h b/include/backend/libinput.h index 2427ae5c..80f57177 100644 --- a/include/backend/libinput.h +++ b/include/backend/libinput.h @@ -57,4 +57,15 @@ void handle_touch_motion(struct libinput_event *event, void handle_touch_cancel(struct libinput_event *event, struct libinput_device *device); +struct wlr_tablet_tool *wlr_libinput_tablet_tool_create( + struct libinput_device *device); +void handle_tablet_tool_axis(struct libinput_event *event, + struct libinput_device *device); +void handle_tablet_tool_proximity(struct libinput_event *event, + struct libinput_device *device); +void handle_tablet_tool_tip(struct libinput_event *event, + struct libinput_device *device); +void handle_tablet_tool_button(struct libinput_event *event, + struct libinput_device *device); + #endif diff --git a/include/types.h b/include/types.h index 5b20e2ed..efead03c 100644 --- a/include/types.h +++ b/include/types.h @@ -42,6 +42,14 @@ 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_tablet_tool_impl { + void (*destroy)(struct wlr_tablet_tool_state *tool); +}; + +struct wlr_tablet_tool *wlr_tablet_tool_create(struct wlr_tablet_tool_impl *impl, + struct wlr_tablet_tool_state *state); +void wlr_tablet_tool_destroy(struct wlr_tablet_tool *tool); + 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 4763013a..aae76f06 100644 --- a/include/wlr/types.h +++ b/include/wlr/types.h @@ -177,15 +177,88 @@ struct wlr_touch_cancel { int32_t slot; }; -// TODO: tablet & tablet tool -// TODO: gestures +struct wlr_tablet_tool_impl; +struct wlr_tablet_tool_state; + +struct wlr_tablet_tool { + struct wlr_tablet_tool_impl *impl; + struct wlr_tablet_tool_state *state; + + struct { + struct wl_signal axis; + struct wl_signal proximity; + struct wl_signal tip; + struct wl_signal button; + } events; +}; + +enum wlr_tablet_tool_axes { + WLR_TABLET_TOOL_AXIS_X = 1, + WLR_TABLET_TOOL_AXIS_Y = 2, + WLR_TABLET_TOOL_AXIS_DISTANCE = 4, + WLR_TABLET_TOOL_AXIS_PRESSURE = 8, + WLR_TABLET_TOOL_AXIS_TILT_X = 16, + WLR_TABLET_TOOL_AXIS_TILT_Y = 32, + WLR_TABLET_TOOL_AXIS_ROTATION = 64, + WLR_TABLET_TOOL_AXIS_SLIDER = 128, + WLR_TABLET_TOOL_AXIS_WHEEL = 256, +}; + +struct wlr_tablet_tool_axis { + uint32_t time_sec; + uint64_t time_usec; + uint32_t updated_axes; + double x_mm, y_mm; + double width_mm, height_mm; + double pressure; + double distance; + double tilt_x, tilt_y; + double rotation; + double slider; + double wheel_delta; +}; + +enum wlr_tablet_tool_proximity_state { + WLR_TABLET_TOOL_PROXIMITY_OUT, + WLR_TABLET_TOOL_PROXIMITY_IN, +}; + +struct wlr_tablet_tool_proximity { + uint32_t time_sec; + uint64_t time_usec; + double x, y; + double width_mm, height_mm; + enum wlr_tablet_tool_proximity_state state; +}; + +enum wlr_tablet_tool_tip_state { + WLR_TABLET_TOOL_TIP_UP, + WLR_TABLET_TOOL_TIP_DOWN, +}; + +struct wlr_tablet_tool_tip { + uint32_t time_sec; + uint64_t time_usec; + double x, y; + double width_mm, height_mm; + enum wlr_tablet_tool_tip_state state; +}; + +struct wlr_tablet_tool_button { + uint32_t time_sec; + uint64_t time_usec; + uint32_t button; + enum wlr_button_state state; +}; + +// TODO: tablet pad // 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_TOOL, WLR_INPUT_DEVICE_TABLET_PAD, WLR_INPUT_DEVICE_GESTURE, WLR_INPUT_DEVICE_SWITCH, @@ -207,6 +280,7 @@ struct wlr_input_device { struct wlr_keyboard *keyboard; struct wlr_pointer *pointer; struct wlr_touch *touch; + struct wlr_tablet_tool *tablet_tool; }; }; diff --git a/types/CMakeLists.txt b/types/CMakeLists.txt index b158db39..b10966ec 100644 --- a/types/CMakeLists.txt +++ b/types/CMakeLists.txt @@ -5,10 +5,11 @@ include_directories( add_library(wlr-types wlr_output.c + wlr_input_device.c wlr_keyboard.c wlr_pointer.c wlr_touch.c - wlr_input_device.c + wlr_tablet_tool.c ) target_link_libraries(wlr-types diff --git a/types/wlr_tablet_tool.c b/types/wlr_tablet_tool.c new file mode 100644 index 00000000..1c1d21ee --- /dev/null +++ b/types/wlr_tablet_tool.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include +#include "types.h" + +struct wlr_tablet_tool *wlr_tablet_tool_create(struct wlr_tablet_tool_impl *impl, + struct wlr_tablet_tool_state *state) { + struct wlr_tablet_tool *tool = calloc(1, sizeof(struct wlr_tablet_tool)); + tool->impl = impl; + tool->state = state; + wl_signal_init(&tool->events.axis); + wl_signal_init(&tool->events.proximity); + wl_signal_init(&tool->events.tip); + wl_signal_init(&tool->events.button); + return tool; +} + +void wlr_tablet_tool_destroy(struct wlr_tablet_tool *tool) { + if (!tool) return; + if (tool->impl) { + tool->impl->destroy(tool->state); + } + free(tool); +} -- cgit v1.2.3 From def3d7c64f68d59c6daec5974afc3ca84fa4bda9 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 15 Jun 2017 16:15:12 -0400 Subject: Add tablet example --- backend/libinput/tablet_tool.c | 6 +-- example/shared.c | 68 +++++++++++++++++++++++++++++++ example/shared.h | 20 +++++++++ example/tablet.c | 92 +++++++++++++++++++++++++++++++++++++++--- 4 files changed, 175 insertions(+), 11 deletions(-) (limited to 'backend/libinput') diff --git a/backend/libinput/tablet_tool.c b/backend/libinput/tablet_tool.c index 276c263c..e8a3bc7d 100644 --- a/backend/libinput/tablet_tool.c +++ b/backend/libinput/tablet_tool.c @@ -76,9 +76,6 @@ void handle_tablet_tool_proximity(struct libinput_event *event, wlr_log(L_DEBUG, "Got a tablet tool event for a device with no tablet tools?"); return; } - // Proximity events contain axis information. We update this information - // before we send the proximity event - handle_tablet_tool_axis(event, device); struct libinput_event_tablet_tool *tevent = libinput_event_get_tablet_tool_event(event); struct wlr_tablet_tool_proximity *wlr_event = @@ -91,6 +88,7 @@ void handle_tablet_tool_proximity(struct libinput_event *event, break; case LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN: wlr_event->state = WLR_TABLET_TOOL_PROXIMITY_IN; + handle_tablet_tool_axis(event, device); break; } wl_signal_emit(&dev->tablet_tool->events.proximity, wlr_event); @@ -104,8 +102,6 @@ void handle_tablet_tool_tip(struct libinput_event *event, wlr_log(L_DEBUG, "Got a tablet tool event for a device with no tablet tools?"); return; } - // Tip events contain axis information. We update this information - // before we send the proximity event handle_tablet_tool_axis(event, device); struct libinput_event_tablet_tool *tevent = libinput_event_get_tablet_tool_event(event); diff --git a/example/shared.c b/example/shared.c index 2592c924..88f2b05f 100644 --- a/example/shared.c +++ b/example/shared.c @@ -167,6 +167,50 @@ static void touch_add(struct wlr_input_device *device, struct compositor_state * wl_list_insert(&state->touch, &tstate->link); } +static void tablet_tool_axis_notify(struct wl_listener *listener, void *data) { + struct wlr_tablet_tool_axis *event = data; + struct tablet_tool_state *tstate = wl_container_of(listener, tstate, axis); + if (tstate->compositor->tool_axis_cb) { + tstate->compositor->tool_axis_cb(tstate, event); + } +} + +static void tablet_tool_proximity_notify(struct wl_listener *listener, void *data) { + struct wlr_tablet_tool_proximity *event = data; + struct tablet_tool_state *tstate = wl_container_of(listener, tstate, proximity); + if (tstate->compositor->tool_proximity_cb) { + tstate->compositor->tool_proximity_cb(tstate, event->state); + } +} + +static void tablet_tool_button_notify(struct wl_listener *listener, void *data) { + struct wlr_tablet_tool_button *event = data; + struct tablet_tool_state *tstate = wl_container_of(listener, tstate, button); + if (tstate->compositor->tool_button_cb) { + tstate->compositor->tool_button_cb(tstate, event->button, event->state); + } +} + +static void tablet_tool_add(struct wlr_input_device *device, + struct compositor_state *state) { + struct tablet_tool_state *tstate = calloc(sizeof(struct tablet_tool_state), 1); + tstate->device = device; + tstate->compositor = state; + wl_list_init(&tstate->axis.link); + wl_list_init(&tstate->proximity.link); + wl_list_init(&tstate->tip.link); + wl_list_init(&tstate->button.link); + tstate->axis.notify = tablet_tool_axis_notify; + tstate->proximity.notify = tablet_tool_proximity_notify; + //tstate->tip.notify = tablet_tool_tip_notify; + tstate->button.notify = tablet_tool_button_notify; + wl_signal_add(&device->tablet_tool->events.axis, &tstate->axis); + wl_signal_add(&device->tablet_tool->events.proximity, &tstate->proximity); + //wl_signal_add(&device->tablet_tool->events.tip, &tstate->tip); + wl_signal_add(&device->tablet_tool->events.button, &tstate->button); + wl_list_insert(&state->tablet_tools, &tstate->link); +} + static void input_add_notify(struct wl_listener *listener, void *data) { struct wlr_input_device *device = data; struct compositor_state *state = wl_container_of(listener, state, input_add); @@ -180,6 +224,8 @@ static void input_add_notify(struct wl_listener *listener, void *data) { case WLR_INPUT_DEVICE_TOUCH: touch_add(device, state); break; + case WLR_INPUT_DEVICE_TABLET_TOOL: + tablet_tool_add(device, state); default: break; } @@ -236,6 +282,24 @@ static void touch_remove(struct wlr_input_device *device, struct compositor_stat wl_list_remove(&tstate->cancel.link); } +static void tablet_tool_remove(struct wlr_input_device *device, struct compositor_state *state) { + struct tablet_tool_state *tstate = NULL, *_tstate; + wl_list_for_each(_tstate, &state->tablet_tools, link) { + if (_tstate->device == device) { + tstate = _tstate; + break; + } + } + if (!tstate) { + return; + } + wl_list_remove(&tstate->link); + wl_list_remove(&tstate->axis.link); + wl_list_remove(&tstate->proximity.link); + //wl_list_remove(&tstate->tip.link); + wl_list_remove(&tstate->button.link); +} + static void input_remove_notify(struct wl_listener *listener, void *data) { struct wlr_input_device *device = data; struct compositor_state *state = wl_container_of(listener, state, input_add); @@ -249,6 +313,9 @@ static void input_remove_notify(struct wl_listener *listener, void *data) { case WLR_INPUT_DEVICE_TOUCH: touch_remove(device, state); break; + case WLR_INPUT_DEVICE_TABLET_TOOL: + tablet_tool_remove(device, state); + break; default: break; } @@ -323,6 +390,7 @@ void compositor_init(struct compositor_state *state) { wl_list_init(&state->keyboards); wl_list_init(&state->pointers); wl_list_init(&state->touch); + wl_list_init(&state->tablet_tools); wl_list_init(&state->input_add.link); state->input_add.notify = input_add_notify; wl_list_init(&state->input_remove.link); diff --git a/example/shared.h b/example/shared.h index d09bcd0e..df456c91 100644 --- a/example/shared.h +++ b/example/shared.h @@ -50,6 +50,17 @@ struct touch_state { void *data; }; +struct tablet_tool_state { + struct compositor_state *compositor; + struct wlr_input_device *device; + struct wl_listener axis; + struct wl_listener proximity; + struct wl_listener tip; + struct wl_listener button; + struct wl_list link; + void *data; +}; + struct compositor_state { void (*output_add_cb)(struct output_state *s); void (*keyboard_add_cb)(struct keyboard_state *s); @@ -72,6 +83,14 @@ struct compositor_state { double x, double y, double width, double height); void (*touch_up_cb)(struct touch_state *s, int32_t slot); void (*touch_cancel_cb)(struct touch_state *s, int32_t slot); + void (*tool_axis_cb)(struct tablet_tool_state *s, + struct wlr_tablet_tool_axis *event); + void (*tool_proximity_cb)(struct tablet_tool_state *s, + enum wlr_tablet_tool_proximity_state proximity); + void (*tool_tip_cb)(struct tablet_tool_state *s, + enum wlr_tablet_tool_tip_state state); + void (*tool_button_cb)(struct tablet_tool_state *s, + uint32_t button, enum wlr_button_state state); struct wl_display *display; struct wl_event_loop *event_loop; @@ -81,6 +100,7 @@ struct compositor_state { struct wl_list keyboards; struct wl_list pointers; struct wl_list touch; + struct wl_list tablet_tools; struct wl_listener input_add; struct wl_listener input_remove; diff --git a/example/tablet.c b/example/tablet.c index 9a498438..f2497d59 100644 --- a/example/tablet.c +++ b/example/tablet.c @@ -21,6 +21,13 @@ struct sample_state { struct wlr_renderer *renderer; + bool proximity, tap; + double distance; + double pressure; + double x_mm, y_mm; + double width_mm, height_mm; + struct wl_list link; + float tool_color[4]; }; static void handle_output_frame(struct output_state *output, struct timespec *ts) { @@ -28,13 +35,41 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts struct sample_state *sample = state->data; struct wlr_output *wlr_output = output->output; + int32_t width, height; + wlr_output_effective_resolution(wlr_output, &width, &height); + wlr_renderer_begin(sample->renderer, wlr_output); - float matrix[16]; - float color[4] = { 0, 1.0, 0, 1.0 }; - wlr_matrix_scale(&matrix, 128, 128, 1); - wlr_matrix_mul(&wlr_output->transform_matrix, &matrix, &matrix); - wlr_render_colored_ellipse(sample->renderer, &color, &matrix); + float matrix[16], view[16]; + float pad_color[4] = { 0.75, 0.75, 0.75, 1.0 }; + float distance = 0.8f * (1 - sample->distance); + float tool_color[4] = { distance, distance, distance, 1 }; + for (size_t i = 0; i < 4; ++i) { + tool_color[i] *= sample->tool_color[i]; + } + float scale = 4; + + float pad_width = sample->width_mm * scale; + float pad_height = sample->height_mm * scale; + float left = width / 2.0f - pad_width / 2.0f; + float top = height / 2.0f - pad_height / 2.0f; + wlr_matrix_translate(&matrix, left, top, 0); + wlr_matrix_scale(&view, pad_width, pad_height, 1); + wlr_matrix_mul(&matrix, &view, &view); + wlr_matrix_mul(&wlr_output->transform_matrix, &view, &matrix); + wlr_render_colored_quad(sample->renderer, &pad_color, &matrix); + + if (sample->proximity) { + wlr_matrix_translate(&matrix, + sample->x_mm * scale - 8 * (sample->pressure + 1) + left, + sample->y_mm * scale - 8 * (sample->pressure + 1) + top, 0); + wlr_matrix_scale(&view, + 16 * (sample->pressure + 1), + 16 * (sample->pressure + 1), 1); + wlr_matrix_mul(&matrix, &view, &view); + wlr_matrix_mul(&wlr_output->transform_matrix, &view, &matrix); + wlr_render_colored_ellipse(sample->renderer, &tool_color, &matrix); + } wlr_renderer_end(sample->renderer); } @@ -46,13 +81,58 @@ static void handle_keyboard_key(struct keyboard_state *kbstate, } } +static void handle_tool_axis(struct tablet_tool_state *tstate, + struct wlr_tablet_tool_axis *event) { + struct sample_state *sample = tstate->compositor->data; + sample->width_mm = event->width_mm; + sample->height_mm = event->height_mm; + if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_X)) { + sample->x_mm = event->x_mm; + } + if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_Y)) { + sample->y_mm = event->y_mm; + } + if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_DISTANCE)) { + sample->distance = event->distance; + } + if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_PRESSURE)) { + sample->pressure = event->pressure; + } +} + +static void handle_tool_proximity(struct tablet_tool_state *tstate, + enum wlr_tablet_tool_proximity_state state) { + struct sample_state *sample = tstate->compositor->data; + sample->proximity = state == WLR_TABLET_TOOL_PROXIMITY_IN; +} + +static void handle_tool_button(struct tablet_tool_state *tstate, + uint32_t button, enum wlr_button_state state) { + struct sample_state *sample = tstate->compositor->data; + if (state == WLR_BUTTON_RELEASED) { + float default_color[4] = { 1, 1, 1, 1 }; + memcpy(sample->tool_color, default_color, 4); + } else { + for (size_t i = 0; i < 3; ++i) { + if (button % 3 != i) { + sample->tool_color[button % 3] = 0; + } + } + } +} + int main(int argc, char *argv[]) { - struct sample_state state = { 0 }; + struct sample_state state = { + .tool_color = { 1, 1, 1, 1 } + }; struct compositor_state compositor; compositor_init(&compositor); compositor.output_frame_cb = handle_output_frame; compositor.keyboard_key_cb = handle_keyboard_key; + compositor.tool_axis_cb = handle_tool_axis; + compositor.tool_proximity_cb = handle_tool_proximity; + compositor.tool_button_cb = handle_tool_button; state.renderer = wlr_gles3_renderer_init(); -- cgit v1.2.3