From d4f7ced6e260961f701f73f672fffa87ee333c3d Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 30 Mar 2018 21:40:43 -0400 Subject: backend/x11: refactor, prepare support for multiple outputs --- backend/x11/input_device.c | 122 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 backend/x11/input_device.c (limited to 'backend/x11/input_device.c') diff --git a/backend/x11/input_device.c b/backend/x11/input_device.c new file mode 100644 index 00000000..fb7f8b85 --- /dev/null +++ b/backend/x11/input_device.c @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include +#ifdef __linux__ +#include +#elif __FreeBSD__ +#include +#endif +#include "backend/x11.h" +#include "util/signal.h" + +static uint32_t xcb_button_to_wl(uint32_t button) { + switch (button) { + case XCB_BUTTON_INDEX_1: return BTN_LEFT; + case XCB_BUTTON_INDEX_2: return BTN_MIDDLE; + case XCB_BUTTON_INDEX_3: return BTN_RIGHT; + // XXX: I'm not sure the scroll-wheel direction is right + case XCB_BUTTON_INDEX_4: return BTN_GEAR_UP; + case XCB_BUTTON_INDEX_5: return BTN_GEAR_DOWN; + default: return 0; + } +} + +bool x11_handle_input_event(struct wlr_x11_backend *x11, + xcb_generic_event_t *event) { + switch (event->response_type & XCB_EVENT_RESPONSE_TYPE_MASK) { + case XCB_KEY_PRESS: + case XCB_KEY_RELEASE: { + xcb_key_press_event_t *ev = (xcb_key_press_event_t *)event; + struct wlr_event_keyboard_key key = { + .time_msec = ev->time, + .keycode = ev->detail - 8, + .state = event->response_type == XCB_KEY_PRESS ? + WLR_KEY_PRESSED : WLR_KEY_RELEASED, + .update_state = true, + }; + + // TODO use xcb-xkb for more precise modifiers state? + wlr_keyboard_notify_key(&x11->keyboard, &key); + x11->time = ev->time; + return true; + } + case XCB_BUTTON_PRESS: { + xcb_button_press_event_t *ev = (xcb_button_press_event_t *)event; + + if (ev->detail == XCB_BUTTON_INDEX_4 || + ev->detail == XCB_BUTTON_INDEX_5) { + double delta = (ev->detail == XCB_BUTTON_INDEX_4 ? -15 : 15); + struct wlr_event_pointer_axis axis = { + .device = &x11->pointer_dev, + .time_msec = ev->time, + .source = WLR_AXIS_SOURCE_WHEEL, + .orientation = WLR_AXIS_ORIENTATION_VERTICAL, + .delta = delta, + }; + wlr_signal_emit_safe(&x11->pointer.events.axis, &axis); + x11->time = ev->time; + break; + } + } + /* fallthrough */ + case XCB_BUTTON_RELEASE: { + xcb_button_press_event_t *ev = (xcb_button_press_event_t *)event; + + if (ev->detail != XCB_BUTTON_INDEX_4 && + ev->detail != XCB_BUTTON_INDEX_5) { + struct wlr_event_pointer_button button = { + .device = &x11->pointer_dev, + .time_msec = ev->time, + .button = xcb_button_to_wl(ev->detail), + .state = event->response_type == XCB_BUTTON_PRESS ? + WLR_BUTTON_PRESSED : WLR_BUTTON_RELEASED, + }; + + wlr_signal_emit_safe(&x11->pointer.events.button, &button); + } + x11->time = ev->time; + return true; + } + case XCB_MOTION_NOTIFY: { + xcb_motion_notify_event_t *ev = (xcb_motion_notify_event_t *)event; + + struct wlr_x11_output *output = + x11_output_from_window_id(x11, ev->event); + if (output == NULL) { + return false; + } + + struct wlr_event_pointer_motion_absolute abs = { + .device = &x11->pointer_dev, + .time_msec = ev->time, + .x = (double)ev->event_x / output->wlr_output.width, + .y = (double)ev->event_y / output->wlr_output.height, + }; + + wlr_signal_emit_safe(&x11->pointer.events.motion_absolute, &abs); + x11->time = ev->time; + return true; + } + default: +#ifdef WLR_HAS_XCB_XKB + if (x11->xkb_supported && event->response_type == x11->xkb_base_event) { + xcb_xkb_state_notify_event_t *ev = + (xcb_xkb_state_notify_event_t *)event; + wlr_keyboard_notify_modifiers(&x11->keyboard, ev->baseMods, + ev->latchedMods, ev->lockedMods, ev->lockedGroup); + return true; + } +#endif + break; + } + + return false; +} + +const struct wlr_input_device_impl input_device_impl = { 0 }; + +bool wlr_input_device_is_x11(struct wlr_input_device *wlr_dev) { + return wlr_dev->impl == &input_device_impl; +} -- cgit v1.2.3 From aa6ae710f70a1b57a71c8a900b0ef4268f60bb4b Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 30 Mar 2018 22:36:04 -0400 Subject: backend/x11: fix input events --- backend/x11/backend.c | 33 +++++++++++++++++++++++++++++++++ backend/x11/input_device.c | 22 ++++++++++++++++++---- include/backend/x11.h | 2 ++ 3 files changed, 53 insertions(+), 4 deletions(-) (limited to 'backend/x11/input_device.c') diff --git a/backend/x11/backend.c b/backend/x11/backend.c index 80998f3f..82004058 100644 --- a/backend/x11/backend.c +++ b/backend/x11/backend.c @@ -1,4 +1,5 @@ #define _POSIX_C_SOURCE 200112L +#include #include #include #include @@ -32,6 +33,38 @@ struct wlr_x11_output *x11_output_from_window_id(struct wlr_x11_backend *x11, return NULL; } +void x11_output_layout_get_box(struct wlr_x11_backend *backend, + struct wlr_box *box) { + int min_x = INT_MAX, min_y = INT_MAX; + int max_x = INT_MIN, max_y = INT_MIN; + + struct wlr_x11_output *output; + wl_list_for_each(output, &backend->outputs, link) { + struct wlr_output *wlr_output = &output->wlr_output; + + int width, height; + wlr_output_effective_resolution(wlr_output, &width, &height); + + if (wlr_output->lx < min_x) { + min_x = wlr_output->lx; + } + if (wlr_output->ly < min_y) { + min_y = wlr_output->ly; + } + if (wlr_output->lx + width > max_x) { + max_x = wlr_output->lx + width; + } + if (wlr_output->ly + height > max_y) { + max_y = wlr_output->ly + height; + } + } + + box->x = min_x; + box->y = min_y; + box->width = max_x - min_x; + box->height = max_y - min_y; +} + static bool handle_x11_event(struct wlr_x11_backend *x11, xcb_generic_event_t *event) { if (x11_handle_input_event(x11, event)) { diff --git a/backend/x11/input_device.c b/backend/x11/input_device.c index fb7f8b85..32b1a1ac 100644 --- a/backend/x11/input_device.c +++ b/backend/x11/input_device.c @@ -87,15 +87,29 @@ bool x11_handle_input_event(struct wlr_x11_backend *x11, if (output == NULL) { return false; } + struct wlr_output *wlr_output = &output->wlr_output; - struct wlr_event_pointer_motion_absolute abs = { + struct wlr_box box = { .x = ev->event_x, .y = ev->event_y }; + wlr_box_transform(&box, wlr_output->transform, wlr_output->width, + wlr_output->height, &box); + box.x /= wlr_output->scale; + box.y /= wlr_output->scale; + + struct wlr_box layout_box; + x11_output_layout_get_box(x11, &layout_box); + + double ox = wlr_output->lx / (double)layout_box.width; + double oy = wlr_output->ly / (double)layout_box.height; + + struct wlr_event_pointer_motion_absolute wlr_event = { .device = &x11->pointer_dev, .time_msec = ev->time, - .x = (double)ev->event_x / output->wlr_output.width, - .y = (double)ev->event_y / output->wlr_output.height, + .x = box.x / (double)layout_box.width + ox, + .y = box.y / (double)layout_box.height + oy, }; - wlr_signal_emit_safe(&x11->pointer.events.motion_absolute, &abs); + wlr_signal_emit_safe(&x11->pointer.events.motion_absolute, &wlr_event); + x11->time = ev->time; return true; } diff --git a/include/backend/x11.h b/include/backend/x11.h index aa058882..0426e481 100644 --- a/include/backend/x11.h +++ b/include/backend/x11.h @@ -69,6 +69,8 @@ struct wlr_x11_backend { struct wlr_x11_output *x11_output_from_window_id(struct wlr_x11_backend *x11, xcb_window_t window); +void x11_output_layout_get_box(struct wlr_x11_backend *backend, + struct wlr_box *box); const struct wlr_input_device_impl input_device_impl; -- cgit v1.2.3