OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/test/events_test_utils_x11.h" | 5 #include "ui/events/test/events_test_utils_x11.h" |
6 | 6 |
7 #include <X11/extensions/XI2.h> | 7 #include <X11/extensions/XI2.h> |
8 #include <X11/keysym.h> | 8 #include <X11/keysym.h> |
9 #include <X11/X.h> | 9 #include <X11/X.h> |
10 #include <X11/Xlib.h> | 10 #include <X11/Xlib.h> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "ui/events/event_constants.h" | 13 #include "ui/events/event_constants.h" |
14 #include "ui/events/keycodes/keyboard_code_conversion_x.h" | 14 #include "ui/events/keycodes/keyboard_code_conversion_x.h" |
15 #include "ui/events/x/touch_factory_x11.h" | 15 #include "ui/events/x/touch_factory_x11.h" |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 const int kScrollValuatorNum = 5; | |
20 const int kScrollValuatorMap[kScrollValuatorNum][4] = { | |
21 // { valuator_index, valuator_type, min_val, max_val } | |
22 { 0, ui::DeviceDataManager::DT_CMT_SCROLL_X, -100, 100 }, | |
23 { 1, ui::DeviceDataManager::DT_CMT_SCROLL_Y, -100, 100 }, | |
24 { 2, ui::DeviceDataManager::DT_CMT_ORDINAL_X, -100, 100 }, | |
25 { 3, ui::DeviceDataManager::DT_CMT_ORDINAL_Y, -100, 100 }, | |
26 { 4, ui::DeviceDataManager::DT_CMT_FINGER_COUNT, 0, 3}, | |
27 }; | |
28 | |
29 const int kTouchValuatorNum = 3; | |
30 const int kTouchValuatorMap[kTouchValuatorNum][4] = { | |
31 // { valuator_index, valuator_type, min_val, max_val } | |
32 { 0, ui::DeviceDataManager::DT_TOUCH_MAJOR, 0, 1000}, | |
33 { 1, ui::DeviceDataManager::DT_TOUCH_ORIENTATION, 0, 1}, | |
34 { 2, ui::DeviceDataManager::DT_TOUCH_PRESSURE, 0, 1000}, | |
35 }; | |
36 | |
37 // Converts ui::EventType to state for X*Events. | 19 // Converts ui::EventType to state for X*Events. |
38 unsigned int XEventState(int flags) { | 20 unsigned int XEventState(int flags) { |
39 return | 21 return |
40 ((flags & ui::EF_SHIFT_DOWN) ? ShiftMask : 0) | | 22 ((flags & ui::EF_SHIFT_DOWN) ? ShiftMask : 0) | |
41 ((flags & ui::EF_CONTROL_DOWN) ? ControlMask : 0) | | 23 ((flags & ui::EF_CONTROL_DOWN) ? ControlMask : 0) | |
42 ((flags & ui::EF_ALT_DOWN) ? Mod1Mask : 0) | | 24 ((flags & ui::EF_ALT_DOWN) ? Mod1Mask : 0) | |
43 ((flags & ui::EF_CAPS_LOCK_DOWN) ? LockMask : 0) | | 25 ((flags & ui::EF_CAPS_LOCK_DOWN) ? LockMask : 0) | |
44 ((flags & ui::EF_LEFT_MOUSE_BUTTON) ? Button1Mask: 0) | | 26 ((flags & ui::EF_LEFT_MOUSE_BUTTON) ? Button1Mask: 0) | |
45 ((flags & ui::EF_MIDDLE_MOUSE_BUTTON) ? Button2Mask: 0) | | 27 ((flags & ui::EF_MIDDLE_MOUSE_BUTTON) ? Button2Mask: 0) | |
46 ((flags & ui::EF_RIGHT_MOUSE_BUTTON) ? Button3Mask: 0); | 28 ((flags & ui::EF_RIGHT_MOUSE_BUTTON) ? Button3Mask: 0); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 return Button1; | 79 return Button1; |
98 case ui::EF_MIDDLE_MOUSE_BUTTON: | 80 case ui::EF_MIDDLE_MOUSE_BUTTON: |
99 return Button2; | 81 return Button2; |
100 case ui::EF_RIGHT_MOUSE_BUTTON: | 82 case ui::EF_RIGHT_MOUSE_BUTTON: |
101 return Button3; | 83 return Button3; |
102 } | 84 } |
103 | 85 |
104 return 0; | 86 return 0; |
105 } | 87 } |
106 | 88 |
107 void InitValuatorsForXIDeviceEvent(XIDeviceEvent* xiev, int valuator_count) { | 89 void InitValuatorsForXIDeviceEvent(XIDeviceEvent* xiev) { |
| 90 int valuator_count = ui::DeviceDataManager::DT_LAST_ENTRY; |
108 xiev->valuators.mask_len = (valuator_count / 8) + 1; | 91 xiev->valuators.mask_len = (valuator_count / 8) + 1; |
109 xiev->valuators.mask = new unsigned char[xiev->valuators.mask_len]; | 92 xiev->valuators.mask = new unsigned char[xiev->valuators.mask_len]; |
110 memset(xiev->valuators.mask, 0, xiev->valuators.mask_len); | 93 memset(xiev->valuators.mask, 0, xiev->valuators.mask_len); |
111 xiev->valuators.values = new double[valuator_count]; | 94 xiev->valuators.values = new double[valuator_count]; |
112 } | 95 } |
113 | 96 |
114 XEvent* CreateXInput2Event(int deviceid, | 97 XEvent* CreateXInput2Event(int deviceid, |
115 int evtype, | 98 int evtype, |
116 int tracking_id, | 99 int tracking_id, |
117 const gfx::Point& location) { | 100 const gfx::Point& location) { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 event_->xbutton.button = wheel_delta > 0 ? Button4 : Button5; | 180 event_->xbutton.button = wheel_delta > 0 ? Button4 : Button5; |
198 } | 181 } |
199 | 182 |
200 void ScopedXI2Event::InitScrollEvent(int deviceid, | 183 void ScopedXI2Event::InitScrollEvent(int deviceid, |
201 int x_offset, | 184 int x_offset, |
202 int y_offset, | 185 int y_offset, |
203 int x_offset_ordinal, | 186 int x_offset_ordinal, |
204 int y_offset_ordinal, | 187 int y_offset_ordinal, |
205 int finger_count) { | 188 int finger_count) { |
206 Cleanup(); | 189 Cleanup(); |
207 event_.reset(CreateXInput2Event(deviceid, XI_Motion, deviceid, gfx::Point())); | 190 event_.reset(CreateXInput2Event(deviceid, XI_Motion, 0, gfx::Point())); |
208 | 191 |
209 int valuator_data[kScrollValuatorNum] = | 192 Valuator valuators[] = { |
210 { x_offset, y_offset, x_offset_ordinal, y_offset_ordinal, finger_count}; | 193 Valuator(DeviceDataManager::DT_CMT_SCROLL_X, x_offset), |
211 XIDeviceEvent* xiev = | 194 Valuator(DeviceDataManager::DT_CMT_SCROLL_Y, y_offset), |
212 static_cast<XIDeviceEvent*>(event_->xcookie.data); | 195 Valuator(DeviceDataManager::DT_CMT_ORDINAL_X, x_offset_ordinal), |
213 | 196 Valuator(DeviceDataManager::DT_CMT_ORDINAL_Y, y_offset_ordinal), |
214 InitValuatorsForXIDeviceEvent(xiev, kScrollValuatorNum); | 197 Valuator(DeviceDataManager::DT_CMT_FINGER_COUNT, finger_count) |
215 for(int i = 0; i < kScrollValuatorNum; i++) { | 198 }; |
216 XISetMask(xiev->valuators.mask, i); | 199 SetUpValuators( |
217 xiev->valuators.values[i] = valuator_data[i]; | 200 std::vector<Valuator>(valuators, valuators + arraysize(valuators))); |
218 } | |
219 } | 201 } |
220 | 202 |
221 void ScopedXI2Event::InitTouchEvent(int deviceid, | 203 void ScopedXI2Event::InitTouchEvent(int deviceid, |
222 int evtype, | 204 int evtype, |
223 int tracking_id, | 205 int tracking_id, |
224 const gfx::Point& location, | 206 const gfx::Point& location, |
225 const std::vector<Valuator>& valuators) { | 207 const std::vector<Valuator>& valuators) { |
226 Cleanup(); | 208 Cleanup(); |
227 event_.reset(CreateXInput2Event( | 209 event_.reset(CreateXInput2Event(deviceid, evtype, tracking_id, location)); |
228 deviceid, evtype, tracking_id, location)); | 210 SetUpValuators(valuators); |
229 | |
230 XIDeviceEvent* xiev = | |
231 static_cast<XIDeviceEvent*>(event_->xcookie.data); | |
232 InitValuatorsForXIDeviceEvent(xiev, valuators.size()); | |
233 int val_count = 0; | |
234 for (int i = 0; i < kTouchValuatorNum; i++) { | |
235 for (size_t j = 0; j < valuators.size(); j++) { | |
236 if (valuators[j].data_type == kTouchValuatorMap[i][1]) { | |
237 XISetMask(xiev->valuators.mask, kTouchValuatorMap[i][0]); | |
238 xiev->valuators.values[val_count++] = valuators[j].value; | |
239 } | |
240 } | |
241 } | |
242 | |
243 } | 211 } |
244 | 212 |
245 void ScopedXI2Event::Cleanup() { | 213 void ScopedXI2Event::Cleanup() { |
246 if (event_.get() && event_->type == GenericEvent) { | 214 if (event_.get() && event_->type == GenericEvent) { |
247 XIDeviceEvent* xiev = | 215 XIDeviceEvent* xiev = |
248 static_cast<XIDeviceEvent*>(event_->xcookie.data); | 216 static_cast<XIDeviceEvent*>(event_->xcookie.data); |
249 if (xiev) { | 217 if (xiev) { |
250 delete[] xiev->valuators.mask; | 218 delete[] xiev->valuators.mask; |
251 delete[] xiev->valuators.values; | 219 delete[] xiev->valuators.values; |
252 delete xiev; | 220 delete xiev; |
253 } | 221 } |
254 } | 222 } |
255 event_.reset(); | 223 event_.reset(); |
256 } | 224 } |
257 | 225 |
258 void SetUpScrollDeviceForTest(unsigned int deviceid) { | 226 void ScopedXI2Event::SetUpValuators(const std::vector<Valuator>& valuators) { |
| 227 CHECK(event_.get()); |
| 228 CHECK_EQ(GenericEvent, event_->type); |
| 229 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(event_->xcookie.data); |
| 230 InitValuatorsForXIDeviceEvent(xiev); |
| 231 ui::DeviceDataManager* manager = ui::DeviceDataManager::GetInstance(); |
| 232 for (size_t i = 0; i < valuators.size(); ++i) { |
| 233 manager->SetValuatorDataForTest(xiev, valuators[i].data_type, |
| 234 valuators[i].value); |
| 235 } |
| 236 } |
| 237 |
| 238 void SetUpScrollDeviceForTest(unsigned int deviceid) { |
259 std::vector<unsigned int> device_list; | 239 std::vector<unsigned int> device_list; |
260 device_list.push_back(deviceid); | 240 device_list.push_back(deviceid); |
261 | 241 |
262 TouchFactory::GetInstance()->SetPointerDeviceForTest(device_list); | 242 TouchFactory::GetInstance()->SetPointerDeviceForTest(device_list); |
263 ui::DeviceDataManager* manager = ui::DeviceDataManager::GetInstance(); | 243 ui::DeviceDataManager* manager = ui::DeviceDataManager::GetInstance(); |
264 manager->SetDeviceListForTest(device_list, device_list, device_list); | 244 manager->SetDeviceListForTest(std::vector<unsigned int>(), device_list); |
265 | |
266 for (int i = 0; i < kScrollValuatorNum; i++) { | |
267 manager->SetDeviceValuatorForTest( | |
268 deviceid, | |
269 kScrollValuatorMap[i][0], | |
270 static_cast<DeviceDataManager::DataType>(kScrollValuatorMap[i][1]), | |
271 kScrollValuatorMap[i][2], | |
272 kScrollValuatorMap[i][3]); | |
273 } | |
274 } | 245 } |
275 | 246 |
276 void SetupTouchDevicesForTest(const std::vector<unsigned int>& devices) { | 247 void SetUpTouchDevicesForTest(const std::vector<unsigned int>& devices) { |
277 std::vector<unsigned int> empty_list; | |
278 TouchFactory::GetInstance()->SetTouchDeviceForTest(devices); | 248 TouchFactory::GetInstance()->SetTouchDeviceForTest(devices); |
279 ui::DeviceDataManager* manager = ui::DeviceDataManager::GetInstance(); | 249 ui::DeviceDataManager* manager = ui::DeviceDataManager::GetInstance(); |
280 manager->SetDeviceListForTest(devices, empty_list, empty_list); | 250 manager->SetDeviceListForTest(devices, std::vector<unsigned int>()); |
281 for (size_t i = 0; i < devices.size(); i++) { | |
282 for (int j = 0; j < kTouchValuatorNum; j++) { | |
283 manager->SetDeviceValuatorForTest( | |
284 devices[i], | |
285 kTouchValuatorMap[j][0], | |
286 static_cast<DeviceDataManager::DataType>(kTouchValuatorMap[j][1]), | |
287 kTouchValuatorMap[j][2], | |
288 kTouchValuatorMap[j][3]); | |
289 } | |
290 } | |
291 } | 251 } |
292 | 252 |
293 } // namespace ui | 253 } // namespace ui |
OLD | NEW |