Chromium Code Reviews| Index: ui/wayland/wayland_input_device.cc |
| diff --git a/ui/wayland/wayland_input_device.cc b/ui/wayland/wayland_input_device.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a76b24a83297fb6677c56e1f8763774b03c3444d |
| --- /dev/null |
| +++ b/ui/wayland/wayland_input_device.cc |
| @@ -0,0 +1,198 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ui/wayland/wayland_input_device.h" |
| + |
| +#include <X11/extensions/XKBcommon.h> |
| +#include <wayland-client.h> |
| + |
| +#include "ui/wayland/events/wayland_event.h" |
| +#include "ui/wayland/wayland_widget.h" |
| +#include "ui/wayland/wayland_window.h" |
| + |
| +WaylandInputDevice::WaylandInputDevice(struct wl_display* display, |
| + uint32_t id) : input_device_(wl_input_device_create(display, id, 1)), |
| + pointer_focus_(NULL), |
| + keyboard_focus_(NULL), |
| + keyboard_modifiers_(0) { |
| + |
| + wl_input_device_add_listener(input_device_, &input_device_listener_, this); |
| + wl_input_device_set_user_data(input_device_, this); |
| + |
| + struct xkb_rule_names names; |
| + names.rules = "evdev"; |
| + names.model = "pc105"; |
| + names.layout = "us"; |
| + names.variant = ""; |
| + names.options = ""; |
| + |
| + xkb_ = xkb_compile_keymap_from_rules(&names); |
| +} |
| + |
| +WaylandInputDevice::~WaylandInputDevice() { |
| + if (input_device_) |
| + wl_input_device_destroy(input_device_); |
| +} |
| + |
| +void WaylandInputDevice::Attach(struct wl_buffer* buffer, |
| + int32_t x, int32_t y) { |
| + wl_input_device_attach(input_device_, last_event_time_, buffer, x, y); |
| +} |
| + |
| +void WaylandInputDevice::OnMotionNotify(void* data, |
| + struct wl_input_device *input_device, uint32_t time, |
| + int32_t x, int32_t y, int32_t sx, int32_t sy) { |
| + |
| + WaylandInputDevice *device = static_cast<WaylandInputDevice*>(data); |
| + WaylandWindow* window = device->pointer_focus_; |
| + |
| + device->last_event_time_ = time; |
| + device->global_position_ = Point(x, y); |
| + device->surface_position_ = Point(sx, sy); |
| + |
| + WaylandEvent event; |
| + event.type = WAYLAND_MOTION; |
| + event.motion.time = time; |
| + event.motion.modifiers = device->keyboard_modifiers_; |
| + event.motion.x = sx; |
| + event.motion.y = sy; |
| + |
| + window->GetWidget()->OnMotionNotify(event); |
| +} |
| + |
| +void WaylandInputDevice::OnButtonNotify(void* data, |
| + struct wl_input_device *input_device, uint32_t time, |
| + uint32_t button, uint32_t state) { |
| + |
| + WaylandInputDevice *device = static_cast<WaylandInputDevice*>(data); |
| + WaylandWindow* window = device->pointer_focus_; |
| + |
| + device->last_event_time_ = time; |
| + |
| + WaylandEvent event; |
| + event.type = WAYLAND_BUTTON; |
| + event.button.time = time; |
| + event.button.button = button; |
| + event.button.state = state; |
| + event.button.modifiers = device->keyboard_modifiers_; |
| + event.button.x = device->surface_position_.x; |
| + event.button.y = device->surface_position_.y; |
| + |
| + window->GetWidget()->OnButtonNotify(event); |
| +} |
| + |
| +void WaylandInputDevice::OnKeyNotify(void* data, |
| + struct wl_input_device *input_device, uint32_t time, |
| + uint32_t key, uint32_t state) { |
| + |
| + WaylandInputDevice *device = static_cast<WaylandInputDevice*>(data); |
| + WaylandWindow* window = device->keyboard_focus_; |
| + struct xkb_desc *xkb = device->xkb_; |
| + |
| + device->last_event_time_ = time; |
| + |
| + WaylandEvent event; |
| + event.type = WAYLAND_KEY; |
| + event.key.time = time; |
| + event.key.key = key; |
| + event.key.state = state; |
| + |
| + uint32_t code = key + xkb->min_key_code; |
| + uint32_t level = 0; |
| + if ((device->keyboard_modifiers_ & XKB_COMMON_SHIFT_MASK) && |
| + XkbKeyGroupWidth(xkb, code, 0) > 1) { |
| + level = 1; |
| + } |
| + |
| + event.key.sym = XkbKeySymEntry(xkb, code, level, 0); |
| + if (state) |
| + device->keyboard_modifiers_ |= xkb->map->modmap[code]; |
| + else |
| + device->keyboard_modifiers_ &= ~xkb->map->modmap[code]; |
| + |
| + event.key.modifiers = device->keyboard_modifiers_; |
| + |
| + window->GetWidget()->OnKeyNotify(event); |
| +} |
| + |
| +void WaylandInputDevice::OnPointerFocus(void* data, |
| + struct wl_input_device *input_device, uint32_t time, |
| + struct wl_surface *surface, int32_t x, int32_t y, |
| + int32_t sx, int32_t sy) { |
| + |
| + WaylandInputDevice *device = static_cast<WaylandInputDevice*>(data); |
| + WaylandWindow* window = device->pointer_focus_; |
| + |
| + device->last_event_time_ = time; |
| + |
| + WaylandEvent event; |
| + event.type = WAYLAND_POINTER_FOCUS; |
| + event.pointer_focus.time = time; |
| + event.pointer_focus.x = sx; |
| + event.pointer_focus.y = sy; |
| + |
| + // If we have a window, then this means it loses focus |
| + if (window) { |
| + event.pointer_focus.state = 0; |
| + device->pointer_focus_ = NULL; |
| + window->GetWidget()->OnPointerFocus(event); |
| + } |
| + |
| + // If we have a surface, then a new window is in focus |
| + if (surface) { |
| + event.pointer_focus.state = 1; |
| + window = static_cast<WaylandWindow*>(wl_surface_get_user_data(surface)); |
| + device->pointer_focus_ = window; |
| + window->GetWidget()->OnPointerFocus(event); |
| + } |
| +} |
| + |
| +void WaylandInputDevice::OnKeyboardFocus(void* data, |
| + struct wl_input_device *input_device, uint32_t time, |
| + struct wl_surface *surface, struct wl_array *keys) { |
| + |
| + WaylandInputDevice *device = static_cast<WaylandInputDevice*>(data); |
| + WaylandWindow* window = device->keyboard_focus_; |
| + struct xkb_desc *xkb = device->xkb_; |
| + |
| + device->last_event_time_ = time; |
| + |
| + WaylandEvent event; |
| + event.type = WAYLAND_KEYBOARD_FOCUS; |
| + event.keyboard_focus.time = time; |
| + device->keyboard_modifiers_ = 0; |
| + |
| + for (uint32_t* k = static_cast<uint32_t*>(keys->data); |
| + k < reinterpret_cast<uint32_t*>( |
| + static_cast<char*>(keys->data) + keys->size); |
| + k++) { |
|
Evan Martin
2011/07/22 21:08:54
I wonder if this loop would be easier to write as
|
| + uint32_t code = *k + xkb->min_key_code; |
| + device->keyboard_modifiers_ |= xkb->map->modmap[code]; |
| + } |
| + event.keyboard_focus.modifiers = device->keyboard_modifiers_; |
| + |
| + // If there is a window, then it loses focus |
| + if (window) { |
| + event.keyboard_focus.state = 0; |
| + device->keyboard_focus_ = NULL; |
| + window->GetWidget()->OnKeyboardFocus(event); |
| + } |
| + |
| + // If we have a surface, then a window gains focus |
| + if (surface) { |
| + event.keyboard_focus.state = 1; |
| + window = static_cast<WaylandWindow*>(wl_surface_get_user_data(surface)); |
| + device->keyboard_focus_ = window; |
| + window->GetWidget()->OnKeyboardFocus(event); |
| + } |
| +} |
| + |
| +const struct wl_input_device_listener |
| +WaylandInputDevice::input_device_listener_ = { |
|
Evan Martin
2011/07/22 21:08:54
Maybe this should just be a const static within th
|
| + WaylandInputDevice::OnMotionNotify, |
| + WaylandInputDevice::OnButtonNotify, |
| + WaylandInputDevice::OnKeyNotify, |
| + WaylandInputDevice::OnPointerFocus, |
| + WaylandInputDevice::OnKeyboardFocus, |
| +}; |