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(void* data, |
| 56 wl_input_device* input_device, |
| 57 uint32_t time, |
| 58 int32_t x, |
| 59 int32_t y, |
| 60 int32_t sx, |
| 61 int32_t sy) { |
| 62 WaylandInputDevice* device = static_cast<WaylandInputDevice*>(data); |
| 63 WaylandWindow* window = device->pointer_focus_; |
| 64 |
| 65 device->last_event_time_ = time; |
| 66 device->global_position_.SetPoint(x, y); |
| 67 device->surface_position_.SetPoint(sx, sy); |
| 68 |
| 69 WaylandEvent event; |
| 70 event.type = WAYLAND_MOTION; |
| 71 event.motion.time = time; |
| 72 event.motion.modifiers = device->keyboard_modifiers_; |
| 73 event.motion.x = sx; |
| 74 event.motion.y = sy; |
| 75 |
| 76 window->widget()->OnMotionNotify(event); |
| 77 } |
| 78 |
| 79 void WaylandInputDevice::OnButtonNotify(void* data, |
| 80 wl_input_device* input_device, |
| 81 uint32_t time, |
| 82 uint32_t button, |
| 83 uint32_t state) { |
| 84 WaylandInputDevice* device = static_cast<WaylandInputDevice*>(data); |
| 85 WaylandWindow* window = device->pointer_focus_; |
| 86 |
| 87 device->last_event_time_ = time; |
| 88 |
| 89 WaylandEvent event; |
| 90 event.type = WAYLAND_BUTTON; |
| 91 event.button.time = time; |
| 92 event.button.button = button; |
| 93 event.button.state = state; |
| 94 event.button.modifiers = device->keyboard_modifiers_; |
| 95 event.button.x = device->surface_position_.x(); |
| 96 event.button.y = device->surface_position_.y(); |
| 97 |
| 98 window->widget()->OnButtonNotify(event); |
| 99 } |
| 100 |
| 101 void WaylandInputDevice::OnKeyNotify(void* data, |
| 102 wl_input_device* input_device, |
| 103 uint32_t time, |
| 104 uint32_t key, |
| 105 uint32_t state) { |
| 106 WaylandInputDevice* device = static_cast<WaylandInputDevice*>(data); |
| 107 WaylandWindow* window = device->keyboard_focus_; |
| 108 struct xkb_desc *xkb = device->xkb_; |
| 109 |
| 110 device->last_event_time_ = time; |
| 111 |
| 112 WaylandEvent event; |
| 113 event.type = WAYLAND_KEY; |
| 114 event.key.time = time; |
| 115 event.key.key = key; |
| 116 event.key.state = state; |
| 117 |
| 118 uint32_t code = key + xkb->min_key_code; |
| 119 uint32_t level = 0; |
| 120 if ((device->keyboard_modifiers_ & XKB_COMMON_SHIFT_MASK) && |
| 121 XkbKeyGroupWidth(xkb, code, 0) > 1) { |
| 122 level = 1; |
| 123 } |
| 124 |
| 125 event.key.sym = XkbKeySymEntry(xkb, code, level, 0); |
| 126 if (state) |
| 127 device->keyboard_modifiers_ |= xkb->map->modmap[code]; |
| 128 else |
| 129 device->keyboard_modifiers_ &= ~xkb->map->modmap[code]; |
| 130 |
| 131 event.key.modifiers = device->keyboard_modifiers_; |
| 132 |
| 133 window->widget()->OnKeyNotify(event); |
| 134 } |
| 135 |
| 136 void WaylandInputDevice::OnPointerFocus(void* data, |
| 137 wl_input_device* input_device, |
| 138 uint32_t time, |
| 139 wl_surface* surface, |
| 140 int32_t x, |
| 141 int32_t y, |
| 142 int32_t sx, |
| 143 int32_t sy) { |
| 144 WaylandInputDevice* device = static_cast<WaylandInputDevice*>(data); |
| 145 WaylandWindow* window = device->pointer_focus_; |
| 146 |
| 147 device->last_event_time_ = time; |
| 148 |
| 149 WaylandEvent event; |
| 150 event.type = WAYLAND_POINTER_FOCUS; |
| 151 event.pointer_focus.time = time; |
| 152 event.pointer_focus.x = sx; |
| 153 event.pointer_focus.y = sy; |
| 154 |
| 155 // If we have a window, then this means it loses focus |
| 156 if (window) { |
| 157 event.pointer_focus.state = 0; |
| 158 device->pointer_focus_ = NULL; |
| 159 window->widget()->OnPointerFocus(event); |
| 160 } |
| 161 |
| 162 // If we have a surface, then a new window is in focus |
| 163 if (surface) { |
| 164 event.pointer_focus.state = 1; |
| 165 window = static_cast<WaylandWindow*>(wl_surface_get_user_data(surface)); |
| 166 device->pointer_focus_ = window; |
| 167 window->widget()->OnPointerFocus(event); |
| 168 } |
| 169 } |
| 170 |
| 171 void WaylandInputDevice::OnKeyboardFocus(void* data, |
| 172 wl_input_device* input_device, |
| 173 uint32_t time, |
| 174 wl_surface* surface, |
| 175 wl_array* keys) { |
| 176 WaylandInputDevice* device = static_cast<WaylandInputDevice*>(data); |
| 177 WaylandWindow* window = device->keyboard_focus_; |
| 178 struct xkb_desc* xkb = device->xkb_; |
| 179 |
| 180 device->last_event_time_ = time; |
| 181 |
| 182 WaylandEvent event; |
| 183 event.type = WAYLAND_KEYBOARD_FOCUS; |
| 184 event.keyboard_focus.time = time; |
| 185 device->keyboard_modifiers_ = 0; |
| 186 |
| 187 uint32_t* codes = static_cast<uint32_t*>(keys->data); |
| 188 int codes_size = keys->size / sizeof(uint32_t); |
| 189 for (int i = 0; i < codes_size; i++) { |
| 190 uint32_t code = codes[i] + xkb->min_key_code; |
| 191 device->keyboard_modifiers_ |= xkb->map->modmap[code]; |
| 192 } |
| 193 event.keyboard_focus.modifiers = device->keyboard_modifiers_; |
| 194 |
| 195 // If there is a window, then it loses focus |
| 196 if (window) { |
| 197 event.keyboard_focus.state = 0; |
| 198 device->keyboard_focus_ = NULL; |
| 199 window->widget()->OnKeyboardFocus(event); |
| 200 } |
| 201 |
| 202 // If we have a surface, then a window gains focus |
| 203 if (surface) { |
| 204 event.keyboard_focus.state = 1; |
| 205 window = static_cast<WaylandWindow*>(wl_surface_get_user_data(surface)); |
| 206 device->keyboard_focus_ = window; |
| 207 window->widget()->OnKeyboardFocus(event); |
| 208 } |
| 209 } |
| 210 |
| 211 } // namespace ui |
OLD | NEW |