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.
6 #include "ozone/wayland/input_device.h"
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"
16 namespace ozonewayland {
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;
64 // Returns Wayland font cursor shape from an Aura cursor.
65 WaylandCursor::CursorType CursorShapeFromNative(unsigned cursor_type) {
66 switch (cursor_type) {
67 case kCursorMiddlePanning:
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;
91 // TODO(kalyan): Choose right cursor types.
94 return WaylandCursor::CURSOR_LEFT_PTR;
96 return WaylandCursor::CURSOR_CROSS;
98 return WaylandCursor::CURSOR_HAND1;
100 return WaylandCursor::CURSOR_IBEAM;
101 case kCursorProgress:
102 return WaylandCursor::CURSOR_WATCH;
104 return WaylandCursor::CURSOR_WAIT;
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;
127 return WaylandCursor::CURSOR_DEFAULT;
129 NOTREACHED() << "Case not handled for " << cursor_type;
130 return WaylandCursor::CURSOR_LEFT_PTR;
133 WaylandInputDevice::WaylandInputDevice(WaylandDisplay* display,
135 : focused_window_handle_(0),
136 grab_window_handle_(0),
139 input_keyboard_(NULL),
140 input_pointer_(NULL),
143 ui::IMEStateChangeHandler::SetInstance(this);
144 static const struct wl_seat_listener kInputSeatListener = {
145 WaylandInputDevice::OnSeatCapabilities,
148 input_seat_ = static_cast<wl_seat*>(
149 wl_registry_bind(display->registry(), id, &wl_seat_interface, 1));
151 wl_seat_add_listener(input_seat_, &kInputSeatListener, this);
152 wl_seat_set_user_data(input_seat_, this);
153 text_input_ = new WaylandTextInput(this);
156 WaylandInputDevice::~WaylandInputDevice() {
157 delete input_keyboard_;
158 delete input_pointer_;
160 if (input_touch_ != NULL) {
163 wl_seat_destroy(input_seat_);
166 void WaylandInputDevice::OnSeatCapabilities(void *data,
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;
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;
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;
191 if (device->input_keyboard_)
192 device->input_keyboard_->OnSeatCapabilities(seat, caps);
194 if (device->input_pointer_)
195 device->input_pointer_->OnSeatCapabilities(seat, caps);
197 if (device->input_touch_)
198 device->input_touch_->OnSeatCapabilities(seat, caps);
201 void WaylandInputDevice::SetFocusWindowHandle(unsigned windowhandle) {
202 focused_window_handle_ = windowhandle;
203 WaylandWindow* window = NULL;
205 window = WaylandDisplay::GetInstance()->GetWindow(windowhandle);
206 text_input_->SetActiveWindow(window);
209 void WaylandInputDevice::SetGrabWindowHandle(unsigned windowhandle,
211 grab_window_handle_ = windowhandle;
212 grab_button_ = button;
215 void WaylandInputDevice::SetCursorType(int cursor_type) {
216 if (!input_pointer_) {
217 LOG(WARNING) << "Tried to change cursor without input configured";
220 input_pointer_->Cursor()->Update(CursorShapeFromNative(cursor_type),
221 WaylandDisplay::GetInstance()->GetSerial());
224 void WaylandInputDevice::ResetIme() {
225 text_input_->ResetIme();
228 void WaylandInputDevice::ImeCaretBoundsChanged(gfx::Rect rect) {
232 void WaylandInputDevice::ShowInputPanel() {
233 text_input_->ShowInputPanel(input_seat_);
236 void WaylandInputDevice::HideInputPanel() {
237 text_input_->HideInputPanel(input_seat_);
240 } // namespace ozonewayland