1 // Copyright (c) 2014 Noser Engineering AG. 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.
5 #include "ozone/wayland/input/touchscreen.h"
7 #include <linux/input.h>
9 #include "ozone/ui/events/event_factory_ozone_wayland.h"
10 #include "ozone/wayland/input/cursor.h"
11 #include "ozone/wayland/input_device.h"
12 #include "ozone/wayland/window.h"
13 #include "ui/events/event.h"
15 namespace ozonewayland {
17 WaylandTouchscreen::WaylandTouchscreen()
19 pointer_position_(0, 0),
23 WaylandTouchscreen::~WaylandTouchscreen() {
25 wl_touch_destroy(wl_touch_);
28 void WaylandTouchscreen::OnSeatCapabilities(wl_seat *seat, uint32_t caps) {
29 static const struct wl_touch_listener kInputTouchListener = {
30 WaylandTouchscreen::OnTouchDown,
31 WaylandTouchscreen::OnTouchUp,
32 WaylandTouchscreen::OnTouchMotion,
33 WaylandTouchscreen::OnTouchFrame,
34 WaylandTouchscreen::OnTouchCancel,
37 dispatcher_ = ui::EventFactoryOzoneWayland::GetInstance()->EventConverter();
39 if ((caps & WL_SEAT_CAPABILITY_TOUCH)) {
40 wl_touch_ = wl_seat_get_touch(seat);
41 wl_touch_set_user_data(wl_touch_, this);
42 wl_touch_add_listener(wl_touch_, &kInputTouchListener, this);
46 void WaylandTouchscreen::OnTouchDown(void *data,
47 struct wl_touch *wl_touch,
50 struct wl_surface *surface,
54 WaylandTouchscreen* device = static_cast<WaylandTouchscreen*>(data);
55 WaylandDisplay::GetInstance()->SetSerial(serial);
56 WaylandInputDevice* input = WaylandDisplay::GetInstance()->PrimaryInput();
57 if (input->GetFocusWindowHandle() && input->GetGrabButton() == 0)
58 input->SetGrabWindowHandle(input->GetFocusWindowHandle(), id);
60 float sx = wl_fixed_to_double(x);
61 float sy = wl_fixed_to_double(y);
63 device->pointer_position_.SetPoint(sx, sy);
65 device->dispatcher_->TouchNotify(ui::ET_TOUCH_PRESSED, sx, sy, id, time);
68 void WaylandTouchscreen::OnTouchUp(void *data,
69 struct wl_touch *wl_touch,
73 WaylandTouchscreen* device = static_cast<WaylandTouchscreen*>(data);
74 WaylandDisplay::GetInstance()->SetSerial(serial);
75 WaylandInputDevice* input = WaylandDisplay::GetInstance()->PrimaryInput();
77 device->dispatcher_->TouchNotify(ui::ET_TOUCH_RELEASED,
78 device->pointer_position_.x(),
79 device->pointer_position_.y(), id, time);
81 if (input->GetGrabWindowHandle() && input->GetGrabButton() == id)
82 input->SetGrabWindowHandle(0, 0);
85 void WaylandTouchscreen::OnTouchMotion(void *data,
86 struct wl_touch *wl_touch,
91 WaylandTouchscreen* device = static_cast<WaylandTouchscreen*>(data);
92 WaylandInputDevice* input = WaylandDisplay::GetInstance()->PrimaryInput();
93 float sx = wl_fixed_to_double(x);
94 float sy = wl_fixed_to_double(y);
96 device->pointer_position_.SetPoint(sx, sy);
98 if (input->GetGrabWindowHandle() &&
99 input->GetGrabWindowHandle() != input->GetFocusWindowHandle()) {
103 device->dispatcher_->TouchNotify(ui::ET_TOUCH_MOVED, sx, sy, id, time);
106 void WaylandTouchscreen::OnTouchFrame(void *data,
107 struct wl_touch *wl_touch) {
108 // TODO(speedpat): find out what should be done here
111 void WaylandTouchscreen::OnTouchCancel(void *data,
112 struct wl_touch *wl_touch) {
113 WaylandTouchscreen* device = static_cast<WaylandTouchscreen*>(data);
114 WaylandInputDevice* input = WaylandDisplay::GetInstance()->PrimaryInput();
116 device->dispatcher_->TouchNotify(ui::ET_TOUCH_CANCELLED,
117 device->pointer_position_.x(),
118 device->pointer_position_.y(),
119 input->GetGrabButton(),
122 if (input->GetGrabWindowHandle() && input->GetGrabButton() != 0)
123 input->SetGrabWindowHandle(0, 0);
126 } // namespace ozonewayland