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