Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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/ozone/platform/wayland/wayland_pointer.h" | |
| 6 | |
| 7 #include <linux/input.h> | |
| 8 #include <wayland-client.h> | |
| 9 | |
| 10 #include "ui/events/event.h" | |
| 11 #include "ui/ozone/platform/wayland/wayland_window.h" | |
| 12 | |
| 13 // TODO(forney): Handle version 5 of wl_pointer. | |
| 14 | |
| 15 namespace ui { | |
| 16 | |
| 17 WaylandPointer::WaylandPointer(wl_pointer* pointer, | |
| 18 const EventDispatchCallback& callback) | |
| 19 : obj_(pointer), callback_(callback) { | |
| 20 static const wl_pointer_listener listener = { | |
| 21 &WaylandPointer::Enter, &WaylandPointer::Leave, &WaylandPointer::Motion, | |
| 22 &WaylandPointer::Button, &WaylandPointer::Axis, | |
| 23 }; | |
| 24 | |
| 25 wl_pointer_add_listener(obj_.get(), &listener, this); | |
| 26 } | |
| 27 | |
| 28 WaylandPointer::~WaylandPointer() {} | |
| 29 | |
| 30 // static | |
| 31 void WaylandPointer::Enter(void* data, | |
| 32 wl_pointer* obj, | |
| 33 uint32_t serial, | |
| 34 wl_surface* surface, | |
| 35 wl_fixed_t surface_x, | |
| 36 wl_fixed_t surface_y) { | |
| 37 WaylandPointer* pointer = static_cast<WaylandPointer*>(data); | |
| 38 pointer->location_.SetPoint(wl_fixed_to_double(surface_x), | |
| 39 wl_fixed_to_double(surface_y)); | |
| 40 WaylandWindow::FromSurface(surface)->set_pointer_focus(true); | |
| 41 } | |
| 42 | |
| 43 // static | |
| 44 void WaylandPointer::Leave(void* data, | |
| 45 wl_pointer* obj, | |
| 46 uint32_t serial, | |
| 47 wl_surface* surface) { | |
| 48 WaylandWindow::FromSurface(surface)->set_pointer_focus(false); | |
| 49 } | |
| 50 | |
| 51 // static | |
| 52 void WaylandPointer::Motion(void* data, | |
| 53 wl_pointer* obj, | |
| 54 uint32_t time, | |
| 55 wl_fixed_t surface_x, | |
| 56 wl_fixed_t surface_y) { | |
| 57 WaylandPointer* pointer = static_cast<WaylandPointer*>(data); | |
| 58 pointer->location_.SetPoint(wl_fixed_to_double(surface_x), | |
| 59 wl_fixed_to_double(surface_y)); | |
| 60 MouseEvent event(ET_MOUSE_MOVED, gfx::Point(), gfx::Point(), | |
| 61 base::TimeDelta::FromMilliseconds(time), pointer->flags_, 0); | |
|
spang
2016/02/24 16:42:21
Which clock does wayland use for events?
Michael Forney
2016/02/24 19:32:21
The spec says
"The time argument is a timestamp w
spang
2016/02/25 23:14:32
Ok good, we require CLOCK_MONOTONIC. Compositors t
| |
| 62 event.set_location_f(pointer->location_); | |
| 63 event.set_root_location_f(pointer->location_); | |
| 64 pointer->callback_.Run(&event); | |
| 65 } | |
| 66 | |
| 67 // static | |
| 68 void WaylandPointer::Button(void* data, | |
| 69 wl_pointer* obj, | |
| 70 uint32_t serial, | |
| 71 uint32_t time, | |
| 72 uint32_t button, | |
| 73 uint32_t state) { | |
| 74 WaylandPointer* pointer = static_cast<WaylandPointer*>(data); | |
| 75 int flag; | |
| 76 switch (button) { | |
| 77 case BTN_LEFT: | |
| 78 flag = EF_LEFT_MOUSE_BUTTON; | |
| 79 break; | |
| 80 case BTN_MIDDLE: | |
| 81 flag = EF_MIDDLE_MOUSE_BUTTON; | |
| 82 break; | |
| 83 case BTN_RIGHT: | |
| 84 flag = EF_RIGHT_MOUSE_BUTTON; | |
| 85 break; | |
| 86 case BTN_BACK: | |
| 87 flag = EF_BACK_MOUSE_BUTTON; | |
| 88 break; | |
| 89 case BTN_FORWARD: | |
| 90 flag = EF_FORWARD_MOUSE_BUTTON; | |
| 91 break; | |
| 92 default: | |
| 93 return; | |
| 94 } | |
| 95 int flags = pointer->flags_ | flag; | |
| 96 EventType type; | |
| 97 if (state == WL_POINTER_BUTTON_STATE_PRESSED) { | |
| 98 type = ET_MOUSE_PRESSED; | |
| 99 pointer->flags_ |= flag; | |
| 100 } else { | |
| 101 type = ET_MOUSE_RELEASED; | |
| 102 pointer->flags_ &= ~flag; | |
| 103 } | |
| 104 MouseEvent event(type, gfx::Point(), gfx::Point(), | |
| 105 base::TimeDelta::FromMilliseconds(time), flags, flag); | |
| 106 event.set_location_f(pointer->location_); | |
| 107 event.set_root_location_f(pointer->location_); | |
| 108 pointer->callback_.Run(&event); | |
| 109 } | |
| 110 | |
| 111 // static | |
| 112 void WaylandPointer::Axis(void* data, | |
| 113 wl_pointer* obj, | |
| 114 uint32_t time, | |
| 115 uint32_t axis, | |
| 116 wl_fixed_t value) { | |
| 117 static const double kAxisValueScale = 10.0; | |
| 118 WaylandPointer* pointer = static_cast<WaylandPointer*>(data); | |
| 119 gfx::Vector2d offset; | |
| 120 // Wayland compositors send axis events with values in the surface coordinate | |
| 121 // space. They send a value of 10 per mouse wheel click by convention, so | |
| 122 // clients (e.g. GTK+) typically scale down by this amount to convert to | |
| 123 // discrete step coordinates. wl_pointer version 5 improves the situation by | |
| 124 // adding axis sources and discrete axis events. | |
| 125 if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) | |
| 126 offset.set_y(-wl_fixed_to_double(value) / kAxisValueScale * | |
| 127 MouseWheelEvent::kWheelDelta); | |
| 128 else | |
| 129 offset.set_x(wl_fixed_to_double(value) / kAxisValueScale * | |
|
spang
2016/02/24 16:42:21
Why under "else"?
Surely |axis| is set for horizo
Michael Forney
2016/02/24 19:32:21
Done.
| |
| 130 MouseWheelEvent::kWheelDelta); | |
| 131 MouseWheelEvent event(offset, gfx::Point(), gfx::Point(), | |
| 132 base::TimeDelta::FromMilliseconds(time), | |
| 133 pointer->flags_, 0); | |
| 134 event.set_location_f(pointer->location_); | |
| 135 event.set_root_location_f(pointer->location_); | |
| 136 pointer->callback_.Run(&event); | |
| 137 } | |
| 138 | |
| 139 } // namespace ui | |
| OLD | NEW |