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