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