OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "wayland_input_device.h" |
| 6 |
| 7 #include <X11/extensions/XKBcommon.h> |
| 8 #include <wayland-client.h> |
| 9 |
| 10 #include "events/wayland_event.h" |
| 11 #include "wayland_widget.h" |
| 12 #include "wayland_window.h" |
| 13 |
| 14 WaylandInputDevice::WaylandInputDevice(struct wl_display* display, |
| 15 uint32_t id) : input_device_(wl_input_device_create(display, id, 1)), |
| 16 pointer_focus_(NULL), |
| 17 keyboard_focus_(NULL), |
| 18 keyboard_modifiers_(0) { |
| 19 |
| 20 wl_input_device_add_listener(input_device_, &input_device_listener_, this); |
| 21 wl_input_device_set_user_data(input_device_, this); |
| 22 |
| 23 struct xkb_rule_names names; |
| 24 names.rules = "evdev"; |
| 25 names.model = "pc105"; |
| 26 names.layout = "us"; |
| 27 names.variant = ""; |
| 28 names.options = ""; |
| 29 |
| 30 xkb_ = xkb_compile_keymap_from_rules(&names); |
| 31 } |
| 32 |
| 33 WaylandInputDevice::~WaylandInputDevice() { |
| 34 if (input_device_) { |
| 35 wl_input_device_destroy(input_device_); |
| 36 } |
| 37 } |
| 38 |
| 39 void WaylandInputDevice::Attach(struct wl_buffer* buffer, |
| 40 int32_t x, int32_t y) { |
| 41 wl_input_device_attach(input_device_, last_event_time_, buffer, x, y); |
| 42 } |
| 43 |
| 44 void WaylandInputDevice::OnMotionNotify(void* data, |
| 45 struct wl_input_device *input_device, uint32_t time, |
| 46 int32_t x, int32_t y, int32_t sx, int32_t sy) { |
| 47 |
| 48 WaylandInputDevice *device = static_cast<WaylandInputDevice*>(data); |
| 49 WaylandWindow* window = device->pointer_focus_; |
| 50 |
| 51 device->last_event_time_ = time; |
| 52 device->global_position_ = Point(x, y); |
| 53 device->surface_position_ = Point(sx, sy); |
| 54 |
| 55 WaylandEvent event; |
| 56 event.type = WAYLAND_MOTION; |
| 57 event.motion.time = time; |
| 58 event.motion.modifiers = device->keyboard_modifiers_; |
| 59 event.motion.x = sx; |
| 60 event.motion.y = sy; |
| 61 |
| 62 window->GetWidget()->OnMotionNotify(event); |
| 63 } |
| 64 |
| 65 void WaylandInputDevice::OnButtonNotify(void* data, |
| 66 struct wl_input_device *input_device, uint32_t time, |
| 67 uint32_t button, uint32_t state) { |
| 68 |
| 69 WaylandInputDevice *device = static_cast<WaylandInputDevice*>(data); |
| 70 WaylandWindow* window = device->pointer_focus_; |
| 71 |
| 72 device->last_event_time_ = time; |
| 73 |
| 74 WaylandEvent event; |
| 75 event.type = WAYLAND_BUTTON; |
| 76 event.button.time = time; |
| 77 event.button.button = button; |
| 78 event.button.state = state; |
| 79 event.button.modifiers = device->keyboard_modifiers_; |
| 80 event.button.x = device->surface_position_.x; |
| 81 event.button.y = device->surface_position_.y; |
| 82 |
| 83 window->GetWidget()->OnButtonNotify(event); |
| 84 } |
| 85 |
| 86 void WaylandInputDevice::OnKeyNotify(void* data, |
| 87 struct wl_input_device *input_device, uint32_t time, |
| 88 uint32_t key, uint32_t state) { |
| 89 |
| 90 WaylandInputDevice *device = static_cast<WaylandInputDevice*>(data); |
| 91 WaylandWindow* window = device->keyboard_focus_; |
| 92 struct xkb_desc *xkb = device->xkb_; |
| 93 |
| 94 device->last_event_time_ = time; |
| 95 |
| 96 WaylandEvent event; |
| 97 event.type = WAYLAND_KEY; |
| 98 event.key.time = time; |
| 99 event.key.key = key; |
| 100 event.key.state = state; |
| 101 |
| 102 uint32_t code = key + xkb->min_key_code; |
| 103 uint32_t level = 0; |
| 104 if ((device->keyboard_modifiers_ & XKB_COMMON_SHIFT_MASK) && |
| 105 XkbKeyGroupWidth(xkb, code, 0) > 1) { |
| 106 level = 1; |
| 107 } |
| 108 |
| 109 event.key.sym = XkbKeySymEntry(xkb, code, level, 0); |
| 110 if (state) { |
| 111 device->keyboard_modifiers_ |= xkb->map->modmap[code]; |
| 112 } else { |
| 113 device->keyboard_modifiers_ &= ~xkb->map->modmap[code]; |
| 114 } |
| 115 |
| 116 event.key.modifiers = device->keyboard_modifiers_; |
| 117 |
| 118 window->GetWidget()->OnKeyNotify(event); |
| 119 } |
| 120 |
| 121 void WaylandInputDevice::OnPointerFocus(void* data, |
| 122 struct wl_input_device *input_device, uint32_t time, |
| 123 struct wl_surface *surface, int32_t x, int32_t y, |
| 124 int32_t sx, int32_t sy) { |
| 125 |
| 126 WaylandInputDevice *device = static_cast<WaylandInputDevice*>(data); |
| 127 WaylandWindow* window = device->pointer_focus_; |
| 128 |
| 129 device->last_event_time_ = time; |
| 130 |
| 131 WaylandEvent event; |
| 132 event.type = WAYLAND_POINTER_FOCUS; |
| 133 event.pointer_focus.time = time; |
| 134 event.pointer_focus.x = sx; |
| 135 event.pointer_focus.y = sy; |
| 136 |
| 137 // If we have a window, then this means it loses focus |
| 138 if (window) { |
| 139 event.pointer_focus.state = 0; |
| 140 device->pointer_focus_ = NULL; |
| 141 window->GetWidget()->OnPointerFocus(event); |
| 142 } |
| 143 |
| 144 // If we have a surface, then a new window is in focus |
| 145 if (surface) { |
| 146 event.pointer_focus.state = 1; |
| 147 window = static_cast<WaylandWindow*>(wl_surface_get_user_data(surface)); |
| 148 device->pointer_focus_ = window; |
| 149 window->GetWidget()->OnPointerFocus(event); |
| 150 } |
| 151 } |
| 152 |
| 153 void WaylandInputDevice::OnKeyboardFocus(void* data, |
| 154 struct wl_input_device *input_device, uint32_t time, |
| 155 struct wl_surface *surface, struct wl_array *keys) { |
| 156 |
| 157 WaylandInputDevice *device = static_cast<WaylandInputDevice*>(data); |
| 158 WaylandWindow* window = device->keyboard_focus_; |
| 159 struct xkb_desc *xkb = device->xkb_; |
| 160 |
| 161 device->last_event_time_ = time; |
| 162 |
| 163 WaylandEvent event; |
| 164 event.type = WAYLAND_KEYBOARD_FOCUS; |
| 165 event.keyboard_focus.time = time; |
| 166 device->keyboard_modifiers_ = 0; |
| 167 |
| 168 for (uint32_t* k = static_cast<uint32_t*>(keys->data); |
| 169 k < reinterpret_cast<uint32_t*>( |
| 170 static_cast<char*>(keys->data) + keys->size); |
| 171 k++) { |
| 172 uint32_t code = *k + xkb->min_key_code; |
| 173 device->keyboard_modifiers_ |= xkb->map->modmap[code]; |
| 174 } |
| 175 event.keyboard_focus.modifiers = device->keyboard_modifiers_; |
| 176 |
| 177 // If there is a window, then it loses focus |
| 178 if (window) { |
| 179 event.keyboard_focus.state = 0; |
| 180 device->keyboard_focus_ = NULL; |
| 181 window->GetWidget()->OnKeyboardFocus(event); |
| 182 } |
| 183 |
| 184 // If we have a surface, then a window gains focus |
| 185 if (surface) { |
| 186 event.keyboard_focus.state = 1; |
| 187 window = static_cast<WaylandWindow*>(wl_surface_get_user_data(surface)); |
| 188 device->keyboard_focus_ = window; |
| 189 window->GetWidget()->OnKeyboardFocus(event); |
| 190 } |
| 191 } |
| 192 |
| 193 const struct wl_input_device_listener |
| 194 WaylandInputDevice::input_device_listener_ = { |
| 195 WaylandInputDevice::OnMotionNotify, |
| 196 WaylandInputDevice::OnButtonNotify, |
| 197 WaylandInputDevice::OnKeyNotify, |
| 198 WaylandInputDevice::OnPointerFocus, |
| 199 WaylandInputDevice::OnKeyboardFocus, |
| 200 }; |
OLD | NEW |