2217ce751defb3b3c392b496bd06b8a50dfbb5e9
[platform/framework/web/crosswalk.git] / src / ozone / wayland / input_device.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Copyright 2013 Intel Corporation. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #include "ozone/wayland/input_device.h"
7
8 #include "base/logging.h"
9 #include "ozone/wayland/display.h"
10 #include "ozone/wayland/input/cursor.h"
11 #include "ozone/wayland/input/keyboard.h"
12 #include "ozone/wayland/input/pointer.h"
13 #include "ozone/wayland/input/text_input.h"
14 #include "ozone/wayland/input/touchscreen.h"
15
16 namespace ozonewayland {
17
18 const int kCursorNull = 0;
19 const int kCursorPointer = 1;
20 const int kCursorCross = 2;
21 const int kCursorHand = 3;
22 const int kCursorIBeam = 4;
23 const int kCursorWait = 5;
24 const int kCursorHelp = 6;
25 const int kCursorEastResize = 7;
26 const int kCursorNorthResize = 8;
27 const int kCursorNorthEastResize = 9;
28 const int kCursorNorthWestResize = 10;
29 const int kCursorSouthResize = 11;
30 const int kCursorSouthEastResize = 12;
31 const int kCursorSouthWestResize = 13;
32 const int kCursorWestResize = 14;
33 const int kCursorNorthSouthResize = 15;
34 const int kCursorEastWestResize = 16;
35 const int kCursorNorthEastSouthWestResize = 17;
36 const int kCursorNorthWestSouthEastResize = 18;
37 const int kCursorColumnResize = 19;
38 const int kCursorRowResize = 20;
39 const int kCursorMiddlePanning = 21;
40 const int kCursorEastPanning = 22;
41 const int kCursorNorthPanning = 23;
42 const int kCursorNorthEastPanning = 24;
43 const int kCursorNorthWestPanning = 25;
44 const int kCursorSouthPanning = 26;
45 const int kCursorSouthEastPanning = 27;
46 const int kCursorSouthWestPanning = 28;
47 const int kCursorWestPanning = 29;
48 const int kCursorMove = 30;
49 const int kCursorVerticalText = 31;
50 const int kCursorCell = 32;
51 const int kCursorContextMenu = 33;
52 const int kCursorAlias = 34;
53 const int kCursorProgress = 35;
54 const int kCursorNoDrop = 36;
55 const int kCursorCopy = 37;
56 const int kCursorNone = 38;
57 const int kCursorNotAllowed = 39;
58 const int kCursorZoomIn = 40;
59 const int kCursorZoomOut = 41;
60 const int kCursorGrab = 42;
61 const int kCursorGrabbing = 43;
62 const int kCursorCustom = 44;
63
64 // Returns Wayland font cursor shape from an Aura cursor.
65 WaylandCursor::CursorType CursorShapeFromNative(unsigned cursor_type) {
66   switch (cursor_type) {
67     case kCursorMiddlePanning:
68     case kCursorMove:
69       return WaylandCursor::CURSOR_FLEUR;
70     case kCursorEastPanning:
71       return WaylandCursor::CURSOR_RIGHT;
72     case kCursorNorthPanning:
73       return WaylandCursor::CURSOR_UP_ARROW;
74     case kCursorNorthEastPanning:
75       return WaylandCursor::CURSOR_TOP_RIGHT;
76     case kCursorNorthWestPanning:
77       return WaylandCursor::CURSOR_TOP_LEFT;
78     case kCursorSouthPanning:
79       return WaylandCursor::CURSOR_BOTTOM;
80     case kCursorSouthEastPanning:
81     case kCursorSouthEastResize:
82       return WaylandCursor::CURSOR_BOTTOM_RIGHT;
83     case kCursorSouthWestPanning:
84     case kCursorSouthWestResize:
85       return WaylandCursor::CURSOR_BOTTOM_LEFT;
86     case kCursorWestPanning:
87       return WaylandCursor::CURSOR_LEFT_ARROW;
88     case kCursorNone:
89     case kCursorNull:
90     case kCursorPointer:
91     // TODO(kalyan): Choose right cursor types.
92     case kCursorGrab:
93     case kCursorGrabbing:
94       return WaylandCursor::CURSOR_LEFT_PTR;
95     case kCursorCross:
96       return WaylandCursor::CURSOR_CROSS;
97     case kCursorHand:
98       return WaylandCursor::CURSOR_HAND1;
99     case kCursorIBeam:
100       return WaylandCursor::CURSOR_IBEAM;
101     case kCursorProgress:
102       return WaylandCursor::CURSOR_WATCH;
103     case kCursorWait:
104       return WaylandCursor::CURSOR_WAIT;
105     case kCursorHelp:
106       return WaylandCursor::CURSOR_QUESTION_ARROW;
107     case kCursorEastResize:
108       return WaylandCursor::CURSOR_RIGHT;
109     case kCursorNorthResize:
110       return WaylandCursor::CURSOR_TOP;
111     case kCursorNorthEastResize:
112       return WaylandCursor::CURSOR_TOP_RIGHT;
113     case kCursorNorthWestResize:
114       return WaylandCursor::CURSOR_TOP_LEFT_ARROW;
115     case kCursorSouthResize:
116       return WaylandCursor::CURSOR_BOTTOM;
117     case kCursorWestResize:
118       return WaylandCursor::CURSOR_LEFT;
119     case kCursorNorthSouthResize:
120     case kCursorRowResize:
121       return WaylandCursor::CURSOR_V_DOUBLE_ARROW;
122     case kCursorEastWestResize:
123     case kCursorColumnResize:
124       return WaylandCursor::CURSOR_H_DOUBLE_ARROW;
125     case kCursorCustom:
126       NOTREACHED();
127       return WaylandCursor::CURSOR_DEFAULT;
128   }
129   NOTREACHED() << "Case not handled for " << cursor_type;
130   return WaylandCursor::CURSOR_LEFT_PTR;
131 }
132
133 WaylandInputDevice::WaylandInputDevice(WaylandDisplay* display,
134                                        uint32_t id)
135     : focused_window_handle_(0),
136       grab_window_handle_(0),
137       grab_button_(0),
138       input_seat_(NULL),
139       input_keyboard_(NULL),
140       input_pointer_(NULL),
141       input_touch_(NULL),
142       text_input_(NULL) {
143   ui::IMEStateChangeHandler::SetInstance(this);
144   static const struct wl_seat_listener kInputSeatListener = {
145     WaylandInputDevice::OnSeatCapabilities,
146   };
147
148   input_seat_ = static_cast<wl_seat*>(
149       wl_registry_bind(display->registry(), id, &wl_seat_interface, 1));
150   DCHECK(input_seat_);
151   wl_seat_add_listener(input_seat_, &kInputSeatListener, this);
152   wl_seat_set_user_data(input_seat_, this);
153   text_input_ = new WaylandTextInput(this);
154 }
155
156 WaylandInputDevice::~WaylandInputDevice() {
157   delete input_keyboard_;
158   delete input_pointer_;
159   delete text_input_;
160   if (input_touch_ != NULL) {
161     delete input_touch_;
162   }
163   wl_seat_destroy(input_seat_);
164 }
165
166 void WaylandInputDevice::OnSeatCapabilities(void *data,
167                                             wl_seat *seat,
168                                             uint32_t caps) {
169   WaylandInputDevice* device = static_cast<WaylandInputDevice*>(data);
170   if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !device->input_keyboard_) {
171     device->input_keyboard_ = new WaylandKeyboard();
172   } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && device->input_keyboard_) {
173     delete device->input_keyboard_;
174     device->input_keyboard_ = NULL;
175   }
176
177   if ((caps & WL_SEAT_CAPABILITY_POINTER) && !device->input_pointer_) {
178     device->input_pointer_ = new WaylandPointer();
179   } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && device->input_pointer_) {
180     delete device->input_pointer_;
181     device->input_pointer_ = NULL;
182   }
183
184   if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !device->input_touch_) {
185     device->input_touch_ = new WaylandTouchscreen();
186   } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && device->input_touch_) {
187     delete device->input_touch_;
188     device->input_touch_ = NULL;
189   }
190
191   if (device->input_keyboard_)
192     device->input_keyboard_->OnSeatCapabilities(seat, caps);
193
194   if (device->input_pointer_)
195     device->input_pointer_->OnSeatCapabilities(seat, caps);
196
197   if (device->input_touch_)
198     device->input_touch_->OnSeatCapabilities(seat, caps);
199 }
200
201 void WaylandInputDevice::SetFocusWindowHandle(unsigned windowhandle) {
202   focused_window_handle_ = windowhandle;
203   WaylandWindow* window = NULL;
204   if (windowhandle)
205     window = WaylandDisplay::GetInstance()->GetWindow(windowhandle);
206   text_input_->SetActiveWindow(window);
207 }
208
209 void WaylandInputDevice::SetGrabWindowHandle(unsigned windowhandle,
210                                              uint32_t button) {
211   grab_window_handle_ = windowhandle;
212   grab_button_ = button;
213 }
214
215 void WaylandInputDevice::SetCursorType(int cursor_type) {
216   if (!input_pointer_) {
217     LOG(WARNING) << "Tried to change cursor without input configured";
218     return;
219   }
220   input_pointer_->Cursor()->Update(CursorShapeFromNative(cursor_type),
221                                    WaylandDisplay::GetInstance()->GetSerial());
222 }
223
224 void WaylandInputDevice::ResetIme() {
225   text_input_->ResetIme();
226 }
227
228 void WaylandInputDevice::ImeCaretBoundsChanged(gfx::Rect rect) {
229   NOTIMPLEMENTED();
230 }
231
232 void WaylandInputDevice::ShowInputPanel() {
233   text_input_->ShowInputPanel(input_seat_);
234 }
235
236 void WaylandInputDevice::HideInputPanel() {
237   text_input_->HideInputPanel(input_seat_);
238 }
239
240 }  // namespace ozonewayland