a771868b0a65ff65328f2bf3dc84427835b8ec7c
[platform/framework/web/crosswalk.git] / src / ozone / wayland / input / touchscreen.cc
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.
4
5 #include "ozone/wayland/input/touchscreen.h"
6
7 #include <linux/input.h>
8
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"
14
15 namespace ozonewayland {
16
17 WaylandTouchscreen::WaylandTouchscreen()
18   : dispatcher_(NULL),
19     pointer_position_(0, 0),
20     wl_touch_(NULL) {
21 }
22
23 WaylandTouchscreen::~WaylandTouchscreen() {
24   if (wl_touch_)
25     wl_touch_destroy(wl_touch_);
26 }
27
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,
35   };
36
37   dispatcher_ = ui::EventFactoryOzoneWayland::GetInstance()->EventConverter();
38
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);
43   }
44 }
45
46 void WaylandTouchscreen::OnTouchDown(void *data,
47                                      struct wl_touch *wl_touch,
48                                      uint32_t serial,
49                                      uint32_t time,
50                                      struct wl_surface *surface,
51                                      int32_t id,
52                                      wl_fixed_t x,
53                                      wl_fixed_t y) {
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);
59
60   float sx = wl_fixed_to_double(x);
61   float sy = wl_fixed_to_double(y);
62
63   device->pointer_position_.SetPoint(sx, sy);
64
65   device->dispatcher_->TouchNotify(ui::ET_TOUCH_PRESSED, sx, sy, id, time);
66 }
67
68 void WaylandTouchscreen::OnTouchUp(void *data,
69                                    struct wl_touch *wl_touch,
70                                    uint32_t serial,
71                                    uint32_t time,
72                                    int32_t id) {
73   WaylandTouchscreen* device = static_cast<WaylandTouchscreen*>(data);
74   WaylandDisplay::GetInstance()->SetSerial(serial);
75   WaylandInputDevice* input = WaylandDisplay::GetInstance()->PrimaryInput();
76
77   device->dispatcher_->TouchNotify(ui::ET_TOUCH_RELEASED,
78                                    device->pointer_position_.x(),
79                                    device->pointer_position_.y(), id, time);
80
81   if (input->GetGrabWindowHandle() && input->GetGrabButton() == id)
82     input->SetGrabWindowHandle(0, 0);
83 }
84
85 void WaylandTouchscreen::OnTouchMotion(void *data,
86                                       struct wl_touch *wl_touch,
87                                       uint32_t time,
88                                       int32_t id,
89                                       wl_fixed_t x,
90                                       wl_fixed_t y) {
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);
95
96   device->pointer_position_.SetPoint(sx, sy);
97
98   if (input->GetGrabWindowHandle() &&
99     input->GetGrabWindowHandle() != input->GetFocusWindowHandle()) {
100     return;
101   }
102
103   device->dispatcher_->TouchNotify(ui::ET_TOUCH_MOVED, sx, sy, id, time);
104 }
105
106 void WaylandTouchscreen::OnTouchFrame(void *data,
107                                       struct wl_touch *wl_touch) {
108   // TODO(speedpat): find out what should be done here
109 }
110
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();
115
116   device->dispatcher_->TouchNotify(ui::ET_TOUCH_CANCELLED,
117                                    device->pointer_position_.x(),
118                                    device->pointer_position_.y(),
119                                    input->GetGrabButton(),
120                                    0);
121
122   if (input->GetGrabWindowHandle() && input->GetGrabButton() != 0)
123     input->SetGrabWindowHandle(0, 0);
124 }
125
126 }  // namespace ozonewayland