OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/x/events_x_utils.h" | 5 #include "ui/events/x/events_x_utils.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; | 19 const int kScrollValuatorNum = 8; |
20 const int kScrollValuatorMap[kScrollValuatorNum][4] = { | 20 const int kScrollValuatorMap[kScrollValuatorNum][4] = { |
21 // { valuator_index, valuator_type, min_val, max_val } | 21 // { valuator_index, valuator_type, min_val, max_val } |
22 { 0, ui::DeviceDataManager::DT_CMT_SCROLL_X, -100, 100 }, | 22 { 0, ui::DeviceDataManager::DT_CMT_SCROLL_X, -100, 100 }, |
23 { 1, ui::DeviceDataManager::DT_CMT_SCROLL_Y, -100, 100 }, | 23 { 1, ui::DeviceDataManager::DT_CMT_SCROLL_Y, -100, 100 }, |
24 { 2, ui::DeviceDataManager::DT_CMT_ORDINAL_X, -100, 100 }, | 24 { 2, ui::DeviceDataManager::DT_CMT_FLING_X, -100, 100 }, |
25 { 3, ui::DeviceDataManager::DT_CMT_ORDINAL_Y, -100, 100 }, | 25 { 3, ui::DeviceDataManager::DT_CMT_FLING_Y, -100, 100 }, |
26 { 4, ui::DeviceDataManager::DT_CMT_FINGER_COUNT, 0, 3}, | 26 { 4, ui::DeviceDataManager::DT_CMT_ORDINAL_X, -100, 100 }, |
| 27 { 5, ui::DeviceDataManager::DT_CMT_ORDINAL_Y, -100, 100 }, |
| 28 { 6, ui::DeviceDataManager::DT_CMT_FINGER_COUNT, 0, 3}, |
| 29 { 7, ui::DeviceDataManager::DT_CMT_FLING_STATE, 0, 1}, |
27 }; | 30 }; |
28 | 31 |
29 #if defined(USE_XI2_MT) | 32 #if defined(USE_XI2_MT) |
30 const int kTouchValuatorNum = 3; | 33 const int kTouchValuatorNum = 3; |
31 const int kTouchValuatorMap[kTouchValuatorNum][4] = { | 34 const int kTouchValuatorMap[kTouchValuatorNum][4] = { |
32 // { valuator_index, valuator_type, min_val, max_val } | 35 // { valuator_index, valuator_type, min_val, max_val } |
33 { 0, ui::DeviceDataManager::DT_TOUCH_MAJOR, 0, 1000}, | 36 { 0, ui::DeviceDataManager::DT_TOUCH_MAJOR, 0, 1000}, |
34 { 1, ui::DeviceDataManager::DT_TOUCH_ORIENTATION, 0, 1}, | 37 { 1, ui::DeviceDataManager::DT_TOUCH_ORIENTATION, 0, 1}, |
35 { 2, ui::DeviceDataManager::DT_TOUCH_PRESSURE, 0, 1000}, | 38 { 2, ui::DeviceDataManager::DT_TOUCH_PRESSURE, 0, 1000}, |
36 }; | 39 }; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 return Button1; | 102 return Button1; |
100 case ui::EF_MIDDLE_MOUSE_BUTTON: | 103 case ui::EF_MIDDLE_MOUSE_BUTTON: |
101 return Button2; | 104 return Button2; |
102 case ui::EF_RIGHT_MOUSE_BUTTON: | 105 case ui::EF_RIGHT_MOUSE_BUTTON: |
103 return Button3; | 106 return Button3; |
104 } | 107 } |
105 | 108 |
106 return 0; | 109 return 0; |
107 } | 110 } |
108 | 111 |
109 void InitValuatorsForXIDeviceEvent(XIDeviceEvent* xiev, int valuator_count) { | 112 void ClearValuatorsForXIDeviceEvent(XIDeviceEvent* xiev, int valuator_count) { |
110 xiev->valuators.mask_len = (valuator_count / 8) + 1; | 113 xiev->valuators.mask_len = (valuator_count / 8) + 1; |
111 xiev->valuators.mask = new unsigned char[xiev->valuators.mask_len]; | 114 xiev->valuators.mask = new unsigned char[xiev->valuators.mask_len]; |
112 memset(xiev->valuators.mask, 0, xiev->valuators.mask_len); | 115 memset(xiev->valuators.mask, 0, xiev->valuators.mask_len); |
113 xiev->valuators.values = new double[valuator_count]; | 116 xiev->valuators.values = new double[valuator_count]; |
114 } | 117 } |
115 | 118 |
| 119 void SetValuatorsForXIDeviceEvent(XIDeviceEvent* xiev, |
| 120 const int* valuator_data, |
| 121 int valuator_data_count, |
| 122 int total_valuator_count, |
| 123 const unsigned char* valuator_mask) { |
| 124 ClearValuatorsForXIDeviceEvent(xiev, total_valuator_count); |
| 125 memcpy(xiev->valuators.mask, valuator_mask, xiev->valuators.mask_len); |
| 126 for(int i = 0; i < valuator_data_count; ++i) { |
| 127 xiev->valuators.values[i] = valuator_data[i]; |
| 128 } |
| 129 } |
| 130 |
116 XEvent* CreateXInput2Event(int deviceid, | 131 XEvent* CreateXInput2Event(int deviceid, |
117 int evtype, | 132 int evtype, |
118 int tracking_id, | 133 int tracking_id, |
119 const gfx::Point& location) { | 134 const gfx::Point& location) { |
120 XEvent* event = new XEvent; | 135 XEvent* event = new XEvent; |
121 memset(event, 0, sizeof(*event)); | 136 memset(event, 0, sizeof(*event)); |
122 event->type = GenericEvent; | 137 event->type = GenericEvent; |
123 event->xcookie.data = new XIDeviceEvent; | 138 event->xcookie.data = new XIDeviceEvent; |
124 XIDeviceEvent* xiev = | 139 XIDeviceEvent* xiev = |
125 static_cast<XIDeviceEvent*>(event->xcookie.data); | 140 static_cast<XIDeviceEvent*>(event->xcookie.data); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 button_event.y = 0; | 199 button_event.y = 0; |
185 button_event.x_root = 0; | 200 button_event.x_root = 0; |
186 button_event.y_root = 0; | 201 button_event.y_root = 0; |
187 button_event.state = XEventState(flags); | 202 button_event.state = XEventState(flags); |
188 button_event.button = XButtonEventButton(type, flags); | 203 button_event.button = XButtonEventButton(type, flags); |
189 button_event.same_screen = 1; | 204 button_event.same_screen = 1; |
190 event->type = button_event.type; | 205 event->type = button_event.type; |
191 event->xbutton = button_event; | 206 event->xbutton = button_event; |
192 } | 207 } |
193 | 208 |
194 EVENTS_EXPORT void InitXMouseWheelEventForTesting(int wheel_delta, | 209 void InitXMouseWheelEventForTesting(int wheel_delta, |
195 int flags, | 210 int flags, |
196 XEvent* event) { | 211 XEvent* event) { |
197 InitXButtonEventForTesting(ui::ET_MOUSEWHEEL, flags, event); | 212 InitXButtonEventForTesting(ui::ET_MOUSEWHEEL, flags, event); |
198 // MouseWheelEvents are not taking horizontal scrolls into account | 213 // MouseWheelEvents are not taking horizontal scrolls into account |
199 // at the moment. | 214 // at the moment. |
200 event->xbutton.button = wheel_delta > 0 ? Button4 : Button5; | 215 event->xbutton.button = wheel_delta > 0 ? Button4 : Button5; |
201 } | 216 } |
202 | 217 |
203 ScopedXI2Event::ScopedXI2Event(XEvent* event) : event_(event) { | 218 ScopedXI2Event::ScopedXI2Event(XEvent* event) : event_(event) { |
204 } | 219 } |
205 | 220 |
206 ScopedXI2Event::~ScopedXI2Event() { | 221 ScopedXI2Event::~ScopedXI2Event() { |
207 XIDeviceEvent* xiev = | 222 XIDeviceEvent* xiev = |
208 static_cast<XIDeviceEvent*>(event_->xcookie.data); | 223 static_cast<XIDeviceEvent*>(event_->xcookie.data); |
209 if (xiev) { | 224 if (xiev) { |
210 delete[] xiev->valuators.mask; | 225 delete[] xiev->valuators.mask; |
211 delete[] xiev->valuators.values; | 226 delete[] xiev->valuators.values; |
212 delete xiev; | 227 delete xiev; |
213 } | 228 } |
214 } | 229 } |
215 | 230 |
216 EVENTS_EXPORT XEvent* CreateScrollEventForTest( | 231 XEvent* CreateScrollEventForTest( |
217 int deviceid, | 232 int deviceid, |
218 int x_offset, | 233 int x_offset, |
219 int y_offset, | 234 int y_offset, |
220 int x_offset_ordinal, | 235 int x_offset_ordinal, |
221 int y_offset_ordinal, | 236 int y_offset_ordinal, |
222 int finger_count) { | 237 int finger_count) { |
223 XEvent* event = CreateXInput2Event( | 238 XEvent* event = CreateXInput2Event( |
224 deviceid, XI_Motion, deviceid, gfx::Point(0, 0)); | 239 deviceid, XI_Motion, deviceid, gfx::Point(0, 0)); |
225 | 240 |
| 241 // Set the valuator data for the event. Please ensure that |valuator_mask| |
| 242 // is the same size as the valuators mask length. |
| 243 unsigned char valuator_mask = 0x73; // 01110011 |
226 int valuator_data[kScrollValuatorNum] = | 244 int valuator_data[kScrollValuatorNum] = |
227 { x_offset, y_offset, x_offset_ordinal, y_offset_ordinal, finger_count }; | 245 { x_offset, y_offset, x_offset_ordinal, y_offset_ordinal, finger_count}; |
228 XIDeviceEvent* xiev = | |
229 static_cast<XIDeviceEvent*>(event->xcookie.data); | |
230 InitValuatorsForXIDeviceEvent(xiev, kScrollValuatorNum); | |
231 for(int i = 0; i < kScrollValuatorNum; i++) { | |
232 XISetMask(xiev->valuators.mask, i); | |
233 xiev->valuators.values[i] = valuator_data[i]; | |
234 } | |
235 | 246 |
| 247 SetValuatorsForXIDeviceEvent( |
| 248 static_cast<XIDeviceEvent*>(event->xcookie.data), |
| 249 valuator_data, |
| 250 5, |
| 251 kScrollValuatorNum, |
| 252 &valuator_mask); |
236 return event; | 253 return event; |
237 } | 254 } |
238 | 255 |
239 EVENTS_EXPORT void SetUpScrollDeviceForTest(unsigned int deviceid) { | 256 XEvent* CreateFlingEventForTest( |
| 257 int deviceid, |
| 258 int x_velocity, |
| 259 int y_velocity, |
| 260 int x_velocity_ordinal, |
| 261 int y_velocity_ordinal, |
| 262 bool is_cancel) { |
| 263 XEvent* event = CreateXInput2Event( |
| 264 deviceid, XI_Motion, deviceid, gfx::Point(0, 0)); |
| 265 |
| 266 // Set the valuator data for the event. Please ensure that |valuator_mask| |
| 267 // is the same size as the valuators mask length. |
| 268 unsigned char valuator_mask = 0xBC; // 10111100 |
| 269 int valuator_data[kScrollValuatorNum] = |
| 270 { x_velocity, y_velocity, x_velocity_ordinal, y_velocity_ordinal, |
| 271 is_cancel ? 1 : 0 }; |
| 272 |
| 273 SetValuatorsForXIDeviceEvent( |
| 274 static_cast<XIDeviceEvent*>(event->xcookie.data), |
| 275 valuator_data, |
| 276 5, |
| 277 kScrollValuatorNum, |
| 278 &valuator_mask); |
| 279 return event; |
| 280 } |
| 281 |
| 282 void SetUpScrollDeviceForTest(unsigned int deviceid) { |
240 std::vector<unsigned int> device_list; | 283 std::vector<unsigned int> device_list; |
241 device_list.push_back(deviceid); | 284 device_list.push_back(deviceid); |
242 | 285 |
243 TouchFactory::GetInstance()->SetPointerDeviceForTest(device_list); | 286 TouchFactory::GetInstance()->SetPointerDeviceForTest(device_list); |
244 ui::DeviceDataManager* manager = ui::DeviceDataManager::GetInstance(); | 287 ui::DeviceDataManager* manager = ui::DeviceDataManager::GetInstance(); |
245 manager->SetDeviceListForTest(device_list, device_list, device_list); | 288 manager->SetDeviceListForTest(device_list, device_list, device_list); |
246 | 289 |
247 for (int i = 0; i < kScrollValuatorNum; i++) { | 290 for (int i = 0; i < kScrollValuatorNum; i++) { |
248 manager->SetDeviceValuatorForTest( | 291 manager->SetDeviceValuatorForTest( |
249 deviceid, | 292 deviceid, |
250 kScrollValuatorMap[i][0], | 293 kScrollValuatorMap[i][0], |
251 static_cast<DeviceDataManager::DataType>(kScrollValuatorMap[i][1]), | 294 static_cast<DeviceDataManager::DataType>(kScrollValuatorMap[i][1]), |
252 kScrollValuatorMap[i][2], | 295 kScrollValuatorMap[i][2], |
253 kScrollValuatorMap[i][3]); | 296 kScrollValuatorMap[i][3]); |
254 } | 297 } |
255 } | 298 } |
256 | 299 |
257 #if defined(USE_XI2_MT) | 300 #if defined(USE_XI2_MT) |
258 | 301 |
259 XEvent* CreateTouchEventForTest(int deviceid, | 302 XEvent* CreateTouchEventForTest(int deviceid, |
260 int evtype, | 303 int evtype, |
261 int tracking_id, | 304 int tracking_id, |
262 const gfx::Point& location, | 305 const gfx::Point& location, |
263 const std::vector<Valuator>& valuators) { | 306 const std::vector<Valuator>& valuators) { |
264 XEvent* event = CreateXInput2Event( | 307 XEvent* event = CreateXInput2Event( |
265 deviceid, evtype, tracking_id, location); | 308 deviceid, evtype, tracking_id, location); |
266 | 309 |
267 XIDeviceEvent* xiev = | 310 XIDeviceEvent* xiev = |
268 static_cast<XIDeviceEvent*>(event->xcookie.data); | 311 static_cast<XIDeviceEvent*>(event->xcookie.data); |
269 InitValuatorsForXIDeviceEvent(xiev, valuators.size()); | 312 ClearValuatorsForXIDeviceEvent(xiev, valuators.size()); |
270 int val_count = 0; | 313 int val_count = 0; |
271 for (int i = 0; i < kTouchValuatorNum; i++) { | 314 for (int i = 0; i < kTouchValuatorNum; i++) { |
272 for(size_t j = 0; j < valuators.size(); j++) { | 315 for(size_t j = 0; j < valuators.size(); j++) { |
273 if (valuators[j].data_type == kTouchValuatorMap[i][1]) { | 316 if (valuators[j].data_type == kTouchValuatorMap[i][1]) { |
274 XISetMask(xiev->valuators.mask, kTouchValuatorMap[i][0]); | 317 XISetMask(xiev->valuators.mask, kTouchValuatorMap[i][0]); |
275 xiev->valuators.values[val_count++] = valuators[j].value; | 318 xiev->valuators.values[val_count++] = valuators[j].value; |
276 } | 319 } |
277 } | 320 } |
278 } | 321 } |
279 | 322 |
(...skipping 13 matching lines...) Expand all Loading... |
293 static_cast<DeviceDataManager::DataType>(kTouchValuatorMap[j][1]), | 336 static_cast<DeviceDataManager::DataType>(kTouchValuatorMap[j][1]), |
294 kTouchValuatorMap[j][2], | 337 kTouchValuatorMap[j][2], |
295 kTouchValuatorMap[j][3]); | 338 kTouchValuatorMap[j][3]); |
296 } | 339 } |
297 } | 340 } |
298 } | 341 } |
299 | 342 |
300 #endif // defined(USE_XI2_MT) | 343 #endif // defined(USE_XI2_MT) |
301 | 344 |
302 } // namespace ui | 345 } // namespace ui |
OLD | NEW |