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> |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 xiev->buttons.mask = new unsigned char[xiev->buttons.mask_len]; | 128 xiev->buttons.mask = new unsigned char[xiev->buttons.mask_len]; |
129 memset(xiev->buttons.mask, 0, xiev->buttons.mask_len); | 129 memset(xiev->buttons.mask, 0, xiev->buttons.mask_len); |
130 } | 130 } |
131 return event; | 131 return event; |
132 } | 132 } |
133 | 133 |
134 } // namespace | 134 } // namespace |
135 | 135 |
136 namespace ui { | 136 namespace ui { |
137 | 137 |
138 // XInput2 events contain additional data that need to be explicitly freed (see | 138 ScopedXI2Event::ScopedXI2Event() {} |
139 // |CreateXInput2Event()|. | 139 ScopedXI2Event::~ScopedXI2Event() { |
140 void XEventDeleter::operator()(XEvent* event) { | 140 Cleanup(); |
141 if (event->type != GenericEvent) | |
142 return; | |
143 XIDeviceEvent* xiev = | |
144 static_cast<XIDeviceEvent*>(event->xcookie.data); | |
145 if (xiev) { | |
146 delete[] xiev->valuators.mask; | |
147 delete[] xiev->valuators.values; | |
148 delete[] xiev->buttons.mask; | |
149 delete xiev; | |
150 } | |
151 } | 141 } |
152 | 142 |
153 ScopedXI2Event::ScopedXI2Event() {} | |
154 ScopedXI2Event::~ScopedXI2Event() {} | |
155 | |
156 void ScopedXI2Event::InitKeyEvent(EventType type, | 143 void ScopedXI2Event::InitKeyEvent(EventType type, |
157 KeyboardCode key_code, | 144 KeyboardCode key_code, |
158 int flags) { | 145 int flags) { |
| 146 Cleanup(); |
159 XDisplay* display = gfx::GetXDisplay(); | 147 XDisplay* display = gfx::GetXDisplay(); |
160 event_.reset(new XEvent); | 148 event_.reset(new XEvent); |
161 memset(event_.get(), 0, sizeof(XEvent)); | 149 memset(event_.get(), 0, sizeof(XEvent)); |
162 event_->type = XKeyEventType(type); | 150 event_->type = XKeyEventType(type); |
163 CHECK_NE(0, event_->type); | 151 CHECK_NE(0, event_->type); |
164 event_->xkey.serial = 0; | 152 event_->xkey.serial = 0; |
165 event_->xkey.send_event = 0; | 153 event_->xkey.send_event = 0; |
166 event_->xkey.display = display; | 154 event_->xkey.display = display; |
167 event_->xkey.time = 0; | 155 event_->xkey.time = 0; |
168 event_->xkey.window = 0; | 156 event_->xkey.window = 0; |
169 event_->xkey.root = 0; | 157 event_->xkey.root = 0; |
170 event_->xkey.subwindow = 0; | 158 event_->xkey.subwindow = 0; |
171 event_->xkey.x = 0; | 159 event_->xkey.x = 0; |
172 event_->xkey.y = 0; | 160 event_->xkey.y = 0; |
173 event_->xkey.x_root = 0; | 161 event_->xkey.x_root = 0; |
174 event_->xkey.y_root = 0; | 162 event_->xkey.y_root = 0; |
175 event_->xkey.state = XEventState(flags); | 163 event_->xkey.state = XEventState(flags); |
176 event_->xkey.keycode = XKeyEventKeyCode(key_code, flags, display); | 164 event_->xkey.keycode = XKeyEventKeyCode(key_code, flags, display); |
177 event_->xkey.same_screen = 1; | 165 event_->xkey.same_screen = 1; |
178 } | 166 } |
179 | 167 |
180 void ScopedXI2Event::InitGenericButtonEvent(int deviceid, | 168 void ScopedXI2Event::InitGenericButtonEvent(int deviceid, |
181 EventType type, | 169 EventType type, |
182 int flags) { | 170 int flags) { |
| 171 Cleanup(); |
183 event_.reset(CreateXInput2Event(deviceid, | 172 event_.reset(CreateXInput2Event(deviceid, |
184 XIButtonEventType(type), 0, gfx::Point())); | 173 XIButtonEventType(type), 0, gfx::Point())); |
185 XIDeviceEvent* xievent = static_cast<XIDeviceEvent*>(event_->xcookie.data); | 174 XIDeviceEvent* xievent = static_cast<XIDeviceEvent*>(event_->xcookie.data); |
186 xievent->mods.effective = XEventState(flags); | 175 xievent->mods.effective = XEventState(flags); |
187 xievent->detail = XButtonEventButton(type, flags); | 176 xievent->detail = XButtonEventButton(type, flags); |
188 XISetMask(xievent->buttons.mask, xievent->detail); | 177 XISetMask(xievent->buttons.mask, xievent->detail); |
189 } | 178 } |
190 | 179 |
191 void ScopedXI2Event::InitButtonEvent(EventType type, | 180 void ScopedXI2Event::InitButtonEvent(EventType type, |
192 int flags) { | 181 int flags) { |
| 182 Cleanup(); |
193 event_.reset(new XEvent); | 183 event_.reset(new XEvent); |
194 memset(event_.get(), 0, sizeof(XEvent)); | 184 memset(event_.get(), 0, sizeof(XEvent)); |
195 event_->type = XButtonEventType(type); | 185 event_->type = XButtonEventType(type); |
196 CHECK_NE(0, event_->type); | 186 CHECK_NE(0, event_->type); |
197 event_->xbutton.serial = 0; | 187 event_->xbutton.serial = 0; |
198 event_->xbutton.send_event = 0; | 188 event_->xbutton.send_event = 0; |
199 event_->xbutton.display = gfx::GetXDisplay(); | 189 event_->xbutton.display = gfx::GetXDisplay(); |
200 event_->xbutton.time = 0; | 190 event_->xbutton.time = 0; |
201 event_->xbutton.window = 0; | 191 event_->xbutton.window = 0; |
202 event_->xbutton.root = 0; | 192 event_->xbutton.root = 0; |
(...skipping 14 matching lines...) Expand all Loading... |
217 // at the moment. | 207 // at the moment. |
218 event_->xbutton.button = wheel_delta > 0 ? Button4 : Button5; | 208 event_->xbutton.button = wheel_delta > 0 ? Button4 : Button5; |
219 } | 209 } |
220 | 210 |
221 void ScopedXI2Event::InitScrollEvent(int deviceid, | 211 void ScopedXI2Event::InitScrollEvent(int deviceid, |
222 int x_offset, | 212 int x_offset, |
223 int y_offset, | 213 int y_offset, |
224 int x_offset_ordinal, | 214 int x_offset_ordinal, |
225 int y_offset_ordinal, | 215 int y_offset_ordinal, |
226 int finger_count) { | 216 int finger_count) { |
| 217 Cleanup(); |
227 event_.reset(CreateXInput2Event(deviceid, XI_Motion, 0, gfx::Point())); | 218 event_.reset(CreateXInput2Event(deviceid, XI_Motion, 0, gfx::Point())); |
228 | 219 |
229 Valuator valuators[] = { | 220 Valuator valuators[] = { |
230 Valuator(DeviceDataManager::DT_CMT_SCROLL_X, x_offset), | 221 Valuator(DeviceDataManager::DT_CMT_SCROLL_X, x_offset), |
231 Valuator(DeviceDataManager::DT_CMT_SCROLL_Y, y_offset), | 222 Valuator(DeviceDataManager::DT_CMT_SCROLL_Y, y_offset), |
232 Valuator(DeviceDataManager::DT_CMT_ORDINAL_X, x_offset_ordinal), | 223 Valuator(DeviceDataManager::DT_CMT_ORDINAL_X, x_offset_ordinal), |
233 Valuator(DeviceDataManager::DT_CMT_ORDINAL_Y, y_offset_ordinal), | 224 Valuator(DeviceDataManager::DT_CMT_ORDINAL_Y, y_offset_ordinal), |
234 Valuator(DeviceDataManager::DT_CMT_FINGER_COUNT, finger_count) | 225 Valuator(DeviceDataManager::DT_CMT_FINGER_COUNT, finger_count) |
235 }; | 226 }; |
236 SetUpValuators( | 227 SetUpValuators( |
237 std::vector<Valuator>(valuators, valuators + arraysize(valuators))); | 228 std::vector<Valuator>(valuators, valuators + arraysize(valuators))); |
238 } | 229 } |
239 | 230 |
240 void ScopedXI2Event::InitFlingScrollEvent(int deviceid, | 231 void ScopedXI2Event::InitFlingScrollEvent(int deviceid, |
241 int x_velocity, | 232 int x_velocity, |
242 int y_velocity, | 233 int y_velocity, |
243 int x_velocity_ordinal, | 234 int x_velocity_ordinal, |
244 int y_velocity_ordinal, | 235 int y_velocity_ordinal, |
245 bool is_cancel) { | 236 bool is_cancel) { |
| 237 Cleanup(); |
246 event_.reset(CreateXInput2Event(deviceid, XI_Motion, deviceid, gfx::Point())); | 238 event_.reset(CreateXInput2Event(deviceid, XI_Motion, deviceid, gfx::Point())); |
247 | 239 |
248 Valuator valuators[] = { | 240 Valuator valuators[] = { |
249 Valuator(DeviceDataManager::DT_CMT_FLING_STATE, is_cancel ? 1 : 0), | 241 Valuator(DeviceDataManager::DT_CMT_FLING_STATE, is_cancel ? 1 : 0), |
250 Valuator(DeviceDataManager::DT_CMT_FLING_Y, y_velocity), | 242 Valuator(DeviceDataManager::DT_CMT_FLING_Y, y_velocity), |
251 Valuator(DeviceDataManager::DT_CMT_ORDINAL_Y, y_velocity_ordinal), | 243 Valuator(DeviceDataManager::DT_CMT_ORDINAL_Y, y_velocity_ordinal), |
252 Valuator(DeviceDataManager::DT_CMT_FLING_X, x_velocity), | 244 Valuator(DeviceDataManager::DT_CMT_FLING_X, x_velocity), |
253 Valuator(DeviceDataManager::DT_CMT_ORDINAL_X, x_velocity_ordinal) | 245 Valuator(DeviceDataManager::DT_CMT_ORDINAL_X, x_velocity_ordinal) |
254 }; | 246 }; |
255 | 247 |
256 SetUpValuators( | 248 SetUpValuators( |
257 std::vector<Valuator>(valuators, valuators + arraysize(valuators))); | 249 std::vector<Valuator>(valuators, valuators + arraysize(valuators))); |
258 } | 250 } |
259 | 251 |
260 void ScopedXI2Event::InitTouchEvent(int deviceid, | 252 void ScopedXI2Event::InitTouchEvent(int deviceid, |
261 int evtype, | 253 int evtype, |
262 int tracking_id, | 254 int tracking_id, |
263 const gfx::Point& location, | 255 const gfx::Point& location, |
264 const std::vector<Valuator>& valuators) { | 256 const std::vector<Valuator>& valuators) { |
| 257 Cleanup(); |
265 event_.reset(CreateXInput2Event(deviceid, evtype, tracking_id, location)); | 258 event_.reset(CreateXInput2Event(deviceid, evtype, tracking_id, location)); |
266 SetUpValuators(valuators); | 259 SetUpValuators(valuators); |
267 } | 260 } |
268 | 261 |
| 262 void ScopedXI2Event::Cleanup() { |
| 263 if (event_.get() && event_->type == GenericEvent) { |
| 264 XIDeviceEvent* xiev = |
| 265 static_cast<XIDeviceEvent*>(event_->xcookie.data); |
| 266 if (xiev) { |
| 267 delete[] xiev->valuators.mask; |
| 268 delete[] xiev->valuators.values; |
| 269 delete[] xiev->buttons.mask; |
| 270 delete xiev; |
| 271 } |
| 272 } |
| 273 event_.reset(); |
| 274 } |
| 275 |
269 void ScopedXI2Event::SetUpValuators(const std::vector<Valuator>& valuators) { | 276 void ScopedXI2Event::SetUpValuators(const std::vector<Valuator>& valuators) { |
270 CHECK(event_.get()); | 277 CHECK(event_.get()); |
271 CHECK_EQ(GenericEvent, event_->type); | 278 CHECK_EQ(GenericEvent, event_->type); |
272 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(event_->xcookie.data); | 279 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(event_->xcookie.data); |
273 InitValuatorsForXIDeviceEvent(xiev); | 280 InitValuatorsForXIDeviceEvent(xiev); |
274 ui::DeviceDataManager* manager = ui::DeviceDataManager::GetInstance(); | 281 ui::DeviceDataManager* manager = ui::DeviceDataManager::GetInstance(); |
275 for (size_t i = 0; i < valuators.size(); ++i) { | 282 for (size_t i = 0; i < valuators.size(); ++i) { |
276 manager->SetValuatorDataForTest(xiev, valuators[i].data_type, | 283 manager->SetValuatorDataForTest(xiev, valuators[i].data_type, |
277 valuators[i].value); | 284 valuators[i].value); |
278 } | 285 } |
279 } | 286 } |
280 | 287 |
281 void SetUpScrollDeviceForTest(unsigned int deviceid) { | 288 void SetUpScrollDeviceForTest(unsigned int deviceid) { |
282 std::vector<unsigned int> device_list; | 289 std::vector<unsigned int> device_list; |
283 device_list.push_back(deviceid); | 290 device_list.push_back(deviceid); |
284 | 291 |
285 TouchFactory::GetInstance()->SetPointerDeviceForTest(device_list); | 292 TouchFactory::GetInstance()->SetPointerDeviceForTest(device_list); |
286 ui::DeviceDataManager* manager = ui::DeviceDataManager::GetInstance(); | 293 ui::DeviceDataManager* manager = ui::DeviceDataManager::GetInstance(); |
287 manager->SetDeviceListForTest(std::vector<unsigned int>(), device_list); | 294 manager->SetDeviceListForTest(std::vector<unsigned int>(), device_list); |
288 } | 295 } |
289 | 296 |
290 void SetUpTouchDevicesForTest(const std::vector<unsigned int>& devices) { | 297 void SetUpTouchDevicesForTest(const std::vector<unsigned int>& devices) { |
291 TouchFactory::GetInstance()->SetTouchDeviceForTest(devices); | 298 TouchFactory::GetInstance()->SetTouchDeviceForTest(devices); |
292 ui::DeviceDataManager* manager = ui::DeviceDataManager::GetInstance(); | 299 ui::DeviceDataManager* manager = ui::DeviceDataManager::GetInstance(); |
293 manager->SetDeviceListForTest(devices, std::vector<unsigned int>()); | 300 manager->SetDeviceListForTest(devices, std::vector<unsigned int>()); |
294 } | 301 } |
295 | 302 |
296 } // namespace ui | 303 } // namespace ui |
OLD | NEW |