OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/events/ozone/evdev/input_controller_evdev.h" | 5 #include "ui/events/ozone/evdev/input_controller_evdev.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <linux/input.h> | 8 #include <linux/input.h> |
9 #include <vector> | |
10 | 9 |
11 #include "base/bind.h" | |
12 #include "base/strings/stringprintf.h" | |
13 #include "base/thread_task_runner_handle.h" | |
14 #include "ui/events/ozone/evdev/input_device_factory_evdev.h" | 10 #include "ui/events/ozone/evdev/input_device_factory_evdev.h" |
15 #include "ui/events/ozone/evdev/keyboard_evdev.h" | 11 #include "ui/events/ozone/evdev/keyboard_evdev.h" |
16 #include "ui/events/ozone/evdev/mouse_button_map_evdev.h" | 12 #include "ui/events/ozone/evdev/mouse_button_map_evdev.h" |
17 | 13 |
18 #if defined(USE_EVDEV_GESTURES) | |
19 #include "ui/events/ozone/evdev/libgestures_glue/gesture_property_provider.h" | |
20 #endif | |
21 | |
22 namespace ui { | 14 namespace ui { |
23 | 15 |
24 namespace { | 16 InputControllerEvdev::InputControllerEvdev(KeyboardEvdev* keyboard, |
25 | 17 MouseButtonMapEvdev* button_map) |
26 #if defined(USE_EVDEV_GESTURES) | |
27 void SetGestureIntProperty(GesturePropertyProvider* provider, | |
28 int id, | |
29 const std::string& name, | |
30 int value) { | |
31 GesturesProp* property = provider->GetProperty(id, name); | |
32 if (property) { | |
33 std::vector<int> values(1, value); | |
34 property->SetIntValue(values); | |
35 } | |
36 } | |
37 | |
38 void SetGestureBoolProperty(GesturePropertyProvider* provider, | |
39 int id, | |
40 const std::string& name, | |
41 bool value) { | |
42 GesturesProp* property = provider->GetProperty(id, name); | |
43 if (property) { | |
44 std::vector<bool> values(1, value); | |
45 property->SetBoolValue(values); | |
46 } | |
47 } | |
48 | |
49 // Return the values in an array in one string. Used for touch logging. | |
50 template <typename T> | |
51 std::string DumpArrayProperty(const std::vector<T>& value, const char* format) { | |
52 std::string ret; | |
53 for (size_t i = 0; i < value.size(); ++i) { | |
54 if (i > 0) | |
55 ret.append(", "); | |
56 ret.append(base::StringPrintf(format, value[i])); | |
57 } | |
58 return ret; | |
59 } | |
60 | |
61 // Return the values in a gesture property in one string. Used for touch | |
62 // logging. | |
63 std::string DumpGesturePropertyValue(GesturesProp* property) { | |
64 switch (property->type()) { | |
65 case GesturePropertyProvider::PT_INT: | |
66 return DumpArrayProperty(property->GetIntValue(), "%d"); | |
67 break; | |
68 case GesturePropertyProvider::PT_SHORT: | |
69 return DumpArrayProperty(property->GetShortValue(), "%d"); | |
70 break; | |
71 case GesturePropertyProvider::PT_BOOL: | |
72 return DumpArrayProperty(property->GetBoolValue(), "%d"); | |
73 break; | |
74 case GesturePropertyProvider::PT_STRING: | |
75 return "\"" + property->GetStringValue() + "\""; | |
76 break; | |
77 case GesturePropertyProvider::PT_REAL: | |
78 return DumpArrayProperty(property->GetDoubleValue(), "%lf"); | |
79 break; | |
80 default: | |
81 NOTREACHED(); | |
82 break; | |
83 } | |
84 return std::string(); | |
85 } | |
86 | |
87 // Dump touch device property values to a string. | |
88 void DumpTouchDeviceStatus(InputDeviceFactoryEvdev* event_factory, | |
89 GesturePropertyProvider* provider, | |
90 std::string* status) { | |
91 // We use DT_ALL since we want gesture property values for all devices that | |
92 // run with the gesture library, not just mice or touchpads. | |
93 std::vector<int> ids; | |
94 event_factory->GetDeviceIdsByType(DT_ALL, &ids); | |
95 | |
96 // Dump the property names and values for each device. | |
97 for (size_t i = 0; i < ids.size(); ++i) { | |
98 std::vector<std::string> names = provider->GetPropertyNamesById(ids[i]); | |
99 status->append("\n"); | |
100 status->append(base::StringPrintf("ID %d:\n", ids[i])); | |
101 status->append(base::StringPrintf( | |
102 "Device \'%s\':\n", provider->GetDeviceNameById(ids[i]).c_str())); | |
103 | |
104 // Note that, unlike X11, we don't maintain the "atom" concept here. | |
105 // Therefore, the property name indices we output here shouldn't be treated | |
106 // as unique identifiers of the properties. | |
107 std::sort(names.begin(), names.end()); | |
108 for (size_t j = 0; j < names.size(); ++j) { | |
109 status->append(base::StringPrintf("\t%s (%zu):", names[j].c_str(), j)); | |
110 GesturesProp* property = provider->GetProperty(ids[i], names[j]); | |
111 status->append("\t" + DumpGesturePropertyValue(property) + '\n'); | |
112 } | |
113 } | |
114 } | |
115 | |
116 #endif | |
117 | |
118 } // namespace | |
119 | |
120 InputControllerEvdev::InputControllerEvdev( | |
121 KeyboardEvdev* keyboard, | |
122 MouseButtonMapEvdev* button_map | |
123 #if defined(USE_EVDEV_GESTURES) | |
124 , | |
125 GesturePropertyProvider* gesture_property_provider | |
126 #endif | |
127 ) | |
128 : input_device_factory_(nullptr), | 18 : input_device_factory_(nullptr), |
129 keyboard_(keyboard), | 19 keyboard_(keyboard), |
130 button_map_(button_map) | 20 button_map_(button_map) { |
131 #if defined(USE_EVDEV_GESTURES) | |
132 , | |
133 gesture_property_provider_(gesture_property_provider) | |
134 #endif | |
135 { | |
136 } | 21 } |
137 | 22 |
138 InputControllerEvdev::~InputControllerEvdev() { | 23 InputControllerEvdev::~InputControllerEvdev() { |
139 } | 24 } |
140 | 25 |
141 void InputControllerEvdev::SetInputDeviceFactory( | 26 void InputControllerEvdev::SetInputDeviceFactory( |
142 InputDeviceFactoryEvdev* input_device_factory) { | 27 InputDeviceFactoryEvdev* input_device_factory) { |
143 input_device_factory_ = input_device_factory; | 28 input_device_factory_ = input_device_factory; |
144 } | 29 } |
145 | 30 |
146 bool InputControllerEvdev::HasMouse() { | 31 bool InputControllerEvdev::HasMouse() { |
147 if (!input_device_factory_) | 32 if (!input_device_factory_) |
148 return false; | 33 return false; |
149 return input_device_factory_->GetDeviceIdsByType(DT_MOUSE, NULL); | 34 return input_device_factory_->HasMouse(); |
150 } | 35 } |
151 | 36 |
152 bool InputControllerEvdev::HasTouchpad() { | 37 bool InputControllerEvdev::HasTouchpad() { |
153 if (!input_device_factory_) | 38 if (!input_device_factory_) |
154 return false; | 39 return false; |
155 return input_device_factory_->GetDeviceIdsByType(DT_TOUCHPAD, NULL); | 40 return input_device_factory_->HasTouchpad(); |
156 } | 41 } |
157 | 42 |
158 bool InputControllerEvdev::IsCapsLockEnabled() { | 43 bool InputControllerEvdev::IsCapsLockEnabled() { |
159 return keyboard_->IsCapsLockEnabled(); | 44 return keyboard_->IsCapsLockEnabled(); |
160 } | 45 } |
161 | 46 |
162 void InputControllerEvdev::SetCapsLockEnabled(bool enabled) { | 47 void InputControllerEvdev::SetCapsLockEnabled(bool enabled) { |
163 keyboard_->SetCapsLockEnabled(enabled); | 48 keyboard_->SetCapsLockEnabled(enabled); |
164 } | 49 } |
165 | 50 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 input_device_factory_->DisableInternalKeyboardExceptKeys( | 86 input_device_factory_->DisableInternalKeyboardExceptKeys( |
202 excepted_keys.Pass()); | 87 excepted_keys.Pass()); |
203 } | 88 } |
204 } | 89 } |
205 | 90 |
206 void InputControllerEvdev::EnableInternalKeyboard() { | 91 void InputControllerEvdev::EnableInternalKeyboard() { |
207 if (input_device_factory_) | 92 if (input_device_factory_) |
208 input_device_factory_->EnableInternalKeyboard(); | 93 input_device_factory_->EnableInternalKeyboard(); |
209 } | 94 } |
210 | 95 |
211 void InputControllerEvdev::SetIntPropertyForOneType(const EventDeviceType type, | |
212 const std::string& name, | |
213 int value) { | |
214 if (!input_device_factory_) | |
215 return; | |
216 #if defined(USE_EVDEV_GESTURES) | |
217 std::vector<int> ids; | |
218 input_device_factory_->GetDeviceIdsByType(type, &ids); | |
219 for (size_t i = 0; i < ids.size(); ++i) { | |
220 SetGestureIntProperty(gesture_property_provider_, ids[i], name, value); | |
221 } | |
222 #endif | |
223 // In the future, we may add property setting codes for other non-gesture | |
224 // devices. One example would be keyboard settings. | |
225 // TODO(sheckylin): See http://crbug.com/398518 for example. | |
226 } | |
227 | |
228 void InputControllerEvdev::SetBoolPropertyForOneType(const EventDeviceType type, | |
229 const std::string& name, | |
230 bool value) { | |
231 if (!input_device_factory_) | |
232 return; | |
233 #if defined(USE_EVDEV_GESTURES) | |
234 std::vector<int> ids; | |
235 input_device_factory_->GetDeviceIdsByType(type, &ids); | |
236 for (size_t i = 0; i < ids.size(); ++i) { | |
237 SetGestureBoolProperty(gesture_property_provider_, ids[i], name, value); | |
238 } | |
239 #endif | |
240 } | |
241 | |
242 void InputControllerEvdev::SetTouchpadSensitivity(int value) { | 96 void InputControllerEvdev::SetTouchpadSensitivity(int value) { |
243 SetIntPropertyForOneType(DT_TOUCHPAD, "Pointer Sensitivity", value); | 97 if (input_device_factory_) |
244 SetIntPropertyForOneType(DT_TOUCHPAD, "Scroll Sensitivity", value); | 98 input_device_factory_->SetTouchpadSensitivity(value); |
245 } | 99 } |
246 | 100 |
247 void InputControllerEvdev::SetTapToClick(bool enabled) { | 101 void InputControllerEvdev::SetTapToClick(bool enabled) { |
248 SetBoolPropertyForOneType(DT_TOUCHPAD, "Tap Enable", enabled); | 102 if (input_device_factory_) |
| 103 input_device_factory_->SetTapToClick(enabled); |
249 } | 104 } |
250 | 105 |
251 void InputControllerEvdev::SetThreeFingerClick(bool enabled) { | 106 void InputControllerEvdev::SetThreeFingerClick(bool enabled) { |
252 SetBoolPropertyForOneType(DT_TOUCHPAD, "T5R2 Three Finger Click Enable", | 107 if (input_device_factory_) |
253 enabled); | 108 input_device_factory_->SetThreeFingerClick(enabled); |
254 } | 109 } |
255 | 110 |
256 void InputControllerEvdev::SetTapDragging(bool enabled) { | 111 void InputControllerEvdev::SetTapDragging(bool enabled) { |
257 SetBoolPropertyForOneType(DT_TOUCHPAD, "Tap Drag Enable", enabled); | 112 if (input_device_factory_) |
| 113 input_device_factory_->SetTapDragging(enabled); |
258 } | 114 } |
259 | 115 |
260 void InputControllerEvdev::SetNaturalScroll(bool enabled) { | 116 void InputControllerEvdev::SetNaturalScroll(bool enabled) { |
261 SetBoolPropertyForOneType(DT_MULTITOUCH, "Australian Scrolling", enabled); | 117 if (input_device_factory_) |
| 118 input_device_factory_->SetNaturalScroll(enabled); |
262 } | 119 } |
263 | 120 |
264 void InputControllerEvdev::SetMouseSensitivity(int value) { | 121 void InputControllerEvdev::SetMouseSensitivity(int value) { |
265 SetIntPropertyForOneType(DT_MOUSE, "Pointer Sensitivity", value); | 122 if (input_device_factory_) |
266 SetIntPropertyForOneType(DT_MOUSE, "Scroll Sensitivity", value); | 123 input_device_factory_->SetMouseSensitivity(value); |
267 } | 124 } |
268 | 125 |
269 void InputControllerEvdev::SetPrimaryButtonRight(bool right) { | 126 void InputControllerEvdev::SetPrimaryButtonRight(bool right) { |
270 button_map_->UpdateButtonMap(BTN_LEFT, right ? BTN_RIGHT : BTN_LEFT); | 127 button_map_->UpdateButtonMap(BTN_LEFT, right ? BTN_RIGHT : BTN_LEFT); |
271 button_map_->UpdateButtonMap(BTN_RIGHT, right ? BTN_LEFT : BTN_RIGHT); | 128 button_map_->UpdateButtonMap(BTN_RIGHT, right ? BTN_LEFT : BTN_RIGHT); |
272 } | 129 } |
273 | 130 |
274 void InputControllerEvdev::SetTapToClickPaused(bool state) { | 131 void InputControllerEvdev::SetTapToClickPaused(bool state) { |
275 SetBoolPropertyForOneType(DT_TOUCHPAD, "Tap Paused", state); | 132 if (input_device_factory_) |
| 133 input_device_factory_->SetTapToClickPaused(state); |
276 } | 134 } |
277 | 135 |
278 void InputControllerEvdev::GetTouchDeviceStatus( | 136 void InputControllerEvdev::GetTouchDeviceStatus( |
279 const GetTouchDeviceStatusReply& reply) { | 137 const GetTouchDeviceStatusReply& reply) { |
280 scoped_ptr<std::string> status(new std::string); | 138 if (input_device_factory_) |
281 #if defined(USE_EVDEV_GESTURES) | 139 input_device_factory_->GetTouchDeviceStatus(reply); |
282 std::string* status_ptr = status.get(); | 140 else |
283 base::ThreadTaskRunnerHandle::Get()->PostTaskAndReply( | 141 reply.Run(make_scoped_ptr(new std::string)); |
284 FROM_HERE, base::Bind(&DumpTouchDeviceStatus, input_device_factory_, | |
285 gesture_property_provider_, status_ptr), | |
286 base::Bind(reply, base::Passed(&status))); | |
287 #else | |
288 reply.Run(status.Pass()); | |
289 #endif | |
290 } | 142 } |
291 | 143 |
292 } // namespace ui | 144 } // namespace ui |
OLD | NEW |