Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(376)

Side by Side Diff: ui/events/devices/x11/device_data_manager_x11.cc

Issue 1071193002: Change device IDs from unsigned to signed. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: check for negative IDs Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/devices/x11/device_data_manager_x11.h" 5 #include "ui/events/devices/x11/device_data_manager_x11.h"
6 6
7 #include <X11/extensions/XInput.h> 7 #include <X11/extensions/XInput.h>
8 #include <X11/extensions/XInput2.h> 8 #include <X11/extensions/XInput2.h>
9 #include <X11/Xlib.h> 9 #include <X11/Xlib.h>
10 10
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 // types. 104 // types.
105 const int kCMTDataTypeStart = ui::DeviceDataManagerX11::DT_CMT_SCROLL_X; 105 const int kCMTDataTypeStart = ui::DeviceDataManagerX11::DT_CMT_SCROLL_X;
106 const int kCMTDataTypeEnd = ui::DeviceDataManagerX11::DT_CMT_FINGER_COUNT; 106 const int kCMTDataTypeEnd = ui::DeviceDataManagerX11::DT_CMT_FINGER_COUNT;
107 const int kTouchDataTypeStart = ui::DeviceDataManagerX11::DT_TOUCH_MAJOR; 107 const int kTouchDataTypeStart = ui::DeviceDataManagerX11::DT_TOUCH_MAJOR;
108 const int kTouchDataTypeEnd = ui::DeviceDataManagerX11::DT_TOUCH_RAW_TIMESTAMP; 108 const int kTouchDataTypeEnd = ui::DeviceDataManagerX11::DT_TOUCH_RAW_TIMESTAMP;
109 109
110 namespace ui { 110 namespace ui {
111 111
112 namespace { 112 namespace {
113 113
114 bool DeviceHasId(const ui::InputDevice input_device, unsigned int id) { 114 bool DeviceHasId(const ui::InputDevice input_device, int id) {
115 return input_device.id == id; 115 return input_device.id == id;
116 } 116 }
117 117
118 } // namespace 118 } // namespace
119 119
120 bool DeviceDataManagerX11::IsCMTDataType(const int type) { 120 bool DeviceDataManagerX11::IsCMTDataType(const int type) {
121 return (type >= kCMTDataTypeStart) && (type <= kCMTDataTypeEnd); 121 return (type >= kCMTDataTypeStart) && (type <= kCMTDataTypeEnd);
122 } 122 }
123 123
124 bool DeviceDataManagerX11::IsTouchDataType(const int type) { 124 bool DeviceDataManagerX11::IsTouchDataType(const int type) {
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 return true; 294 return true;
295 } 295 }
296 return factory->QuerySlotForTrackingID(xiev->detail, slot); 296 return factory->QuerySlotForTrackingID(xiev->detail, slot);
297 } 297 }
298 298
299 void DeviceDataManagerX11::GetEventRawData(const XEvent& xev, EventData* data) { 299 void DeviceDataManagerX11::GetEventRawData(const XEvent& xev, EventData* data) {
300 if (xev.type != GenericEvent) 300 if (xev.type != GenericEvent)
301 return; 301 return;
302 302
303 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(xev.xcookie.data); 303 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(xev.xcookie.data);
304 CHECK(xiev->sourceid >= 0);
305 CHECK(xiev->deviceid >= 0);
304 if (xiev->sourceid >= kMaxDeviceNum || xiev->deviceid >= kMaxDeviceNum) 306 if (xiev->sourceid >= kMaxDeviceNum || xiev->deviceid >= kMaxDeviceNum)
305 return; 307 return;
306 data->clear(); 308 data->clear();
307 const int sourceid = xiev->sourceid; 309 const int sourceid = xiev->sourceid;
308 double* valuators = xiev->valuators.values; 310 double* valuators = xiev->valuators.values;
309 for (int i = 0; i <= valuator_count_[sourceid]; ++i) { 311 for (int i = 0; i <= valuator_count_[sourceid]; ++i) {
310 if (XIMaskIsSet(xiev->valuators.mask, i)) { 312 if (XIMaskIsSet(xiev->valuators.mask, i)) {
311 int type = data_type_lookup_[sourceid][i]; 313 int type = data_type_lookup_[sourceid][i];
312 if (type != DT_LAST_ENTRY) { 314 if (type != DT_LAST_ENTRY) {
313 (*data)[type] = *valuators; 315 (*data)[type] = *valuators;
314 if (IsTouchDataType(type)) { 316 if (IsTouchDataType(type)) {
315 int slot = -1; 317 int slot = -1;
316 if (GetSlotNumber(xiev, &slot) && slot >= 0 && slot < kMaxSlotNum) 318 if (GetSlotNumber(xiev, &slot) && slot >= 0 && slot < kMaxSlotNum)
317 last_seen_valuator_[sourceid][slot][type] = *valuators; 319 last_seen_valuator_[sourceid][slot][type] = *valuators;
318 } 320 }
319 } 321 }
320 valuators++; 322 valuators++;
321 } 323 }
322 } 324 }
323 } 325 }
324 326
325 bool DeviceDataManagerX11::GetEventData(const XEvent& xev, 327 bool DeviceDataManagerX11::GetEventData(const XEvent& xev,
326 const DataType type, double* value) { 328 const DataType type, double* value) {
327 if (xev.type != GenericEvent) 329 if (xev.type != GenericEvent)
328 return false; 330 return false;
329 331
330 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(xev.xcookie.data); 332 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(xev.xcookie.data);
333 CHECK(xiev->sourceid >= 0);
334 CHECK(xiev->deviceid >= 0);
331 if (xiev->sourceid >= kMaxDeviceNum || xiev->deviceid >= kMaxDeviceNum) 335 if (xiev->sourceid >= kMaxDeviceNum || xiev->deviceid >= kMaxDeviceNum)
332 return false; 336 return false;
333 const int sourceid = xiev->sourceid; 337 const int sourceid = xiev->sourceid;
334 if (valuator_lookup_[sourceid].empty()) 338 if (valuator_lookup_[sourceid].empty())
335 return false; 339 return false;
336 340
337 if (type == DT_TOUCH_TRACKING_ID) { 341 if (type == DT_TOUCH_TRACKING_ID) {
338 // With XInput2 MT, Tracking ID is provided in the detail field for touch 342 // With XInput2 MT, Tracking ID is provided in the detail field for touch
339 // events. 343 // events.
340 if (xiev->evtype == XI_TouchBegin || 344 if (xiev->evtype == XI_TouchBegin ||
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 return xi_device_event_types_[native_event->xcookie.evtype]; 383 return xi_device_event_types_[native_event->xcookie.evtype];
380 } 384 }
381 385
382 bool DeviceDataManagerX11::IsTouchpadXInputEvent( 386 bool DeviceDataManagerX11::IsTouchpadXInputEvent(
383 const base::NativeEvent& native_event) const { 387 const base::NativeEvent& native_event) const {
384 if (native_event->type != GenericEvent) 388 if (native_event->type != GenericEvent)
385 return false; 389 return false;
386 390
387 XIDeviceEvent* xievent = 391 XIDeviceEvent* xievent =
388 static_cast<XIDeviceEvent*>(native_event->xcookie.data); 392 static_cast<XIDeviceEvent*>(native_event->xcookie.data);
393 CHECK(xievent->sourceid >= 0);
389 if (xievent->sourceid >= kMaxDeviceNum) 394 if (xievent->sourceid >= kMaxDeviceNum)
390 return false; 395 return false;
391 return touchpads_[xievent->sourceid]; 396 return touchpads_[xievent->sourceid];
392 } 397 }
393 398
394 bool DeviceDataManagerX11::IsCMTDeviceEvent( 399 bool DeviceDataManagerX11::IsCMTDeviceEvent(
395 const base::NativeEvent& native_event) const { 400 const base::NativeEvent& native_event) const {
396 if (native_event->type != GenericEvent) 401 if (native_event->type != GenericEvent)
397 return false; 402 return false;
398 403
399 XIDeviceEvent* xievent = 404 XIDeviceEvent* xievent =
400 static_cast<XIDeviceEvent*>(native_event->xcookie.data); 405 static_cast<XIDeviceEvent*>(native_event->xcookie.data);
406 CHECK(xievent->sourceid >= 0);
401 if (xievent->sourceid >= kMaxDeviceNum) 407 if (xievent->sourceid >= kMaxDeviceNum)
402 return false; 408 return false;
403 return cmt_devices_[xievent->sourceid]; 409 return cmt_devices_[xievent->sourceid];
404 } 410 }
405 411
406 bool DeviceDataManagerX11::IsCMTGestureEvent( 412 bool DeviceDataManagerX11::IsCMTGestureEvent(
407 const base::NativeEvent& native_event) const { 413 const base::NativeEvent& native_event) const {
408 return (IsScrollEvent(native_event) || 414 return (IsScrollEvent(native_event) ||
409 IsFlingEvent(native_event) || 415 IsFlingEvent(native_event) ||
410 IsCMTMetricsEvent(native_event)); 416 IsCMTMetricsEvent(native_event));
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
563 569
564 EventData data; 570 EventData data;
565 GetEventRawData(*native_event, &data); 571 GetEventRawData(*native_event, &data);
566 572
567 if (data.find(DT_CMT_START_TIME) != data.end()) 573 if (data.find(DT_CMT_START_TIME) != data.end())
568 *start_time = data[DT_CMT_START_TIME]; 574 *start_time = data[DT_CMT_START_TIME];
569 if (data.find(DT_CMT_END_TIME) != data.end()) 575 if (data.find(DT_CMT_END_TIME) != data.end())
570 *end_time = data[DT_CMT_END_TIME]; 576 *end_time = data[DT_CMT_END_TIME];
571 } 577 }
572 578
573 bool DeviceDataManagerX11::NormalizeData(unsigned int deviceid, 579 bool DeviceDataManagerX11::NormalizeData(int deviceid,
574 const DataType type, 580 const DataType type,
575 double* value) { 581 double* value) {
576 double max_value; 582 double max_value;
577 double min_value; 583 double min_value;
578 if (GetDataRange(deviceid, type, &min_value, &max_value)) { 584 if (GetDataRange(deviceid, type, &min_value, &max_value)) {
579 *value = (*value - min_value) / (max_value - min_value); 585 *value = (*value - min_value) / (max_value - min_value);
580 DCHECK(*value >= 0.0 && *value <= 1.0); 586 DCHECK(*value >= 0.0 && *value <= 1.0);
581 return true; 587 return true;
582 } 588 }
583 return false; 589 return false;
584 } 590 }
585 591
586 bool DeviceDataManagerX11::GetDataRange(unsigned int deviceid, 592 bool DeviceDataManagerX11::GetDataRange(int deviceid,
587 const DataType type, 593 const DataType type,
588 double* min, 594 double* min,
589 double* max) { 595 double* max) {
590 if (deviceid >= static_cast<unsigned int>(kMaxDeviceNum)) 596 CHECK(deviceid >= 0);
597 if (deviceid >= kMaxDeviceNum)
591 return false; 598 return false;
592 if (valuator_lookup_[deviceid][type] >= 0) { 599 if (valuator_lookup_[deviceid][type] >= 0) {
593 *min = valuator_min_[deviceid][type]; 600 *min = valuator_min_[deviceid][type];
594 *max = valuator_max_[deviceid][type]; 601 *max = valuator_max_[deviceid][type];
595 return true; 602 return true;
596 } 603 }
597 return false; 604 return false;
598 } 605 }
599 606
600 void DeviceDataManagerX11::SetDeviceListForTest( 607 void DeviceDataManagerX11::SetDeviceListForTest(
601 const std::vector<unsigned int>& touchscreen, 608 const std::vector<int>& touchscreen,
602 const std::vector<unsigned int>& cmt_devices) { 609 const std::vector<int>& cmt_devices) {
603 for (int i = 0; i < kMaxDeviceNum; ++i) { 610 for (int i = 0; i < kMaxDeviceNum; ++i) {
604 valuator_count_[i] = 0; 611 valuator_count_[i] = 0;
605 valuator_lookup_[i].clear(); 612 valuator_lookup_[i].clear();
606 data_type_lookup_[i].clear(); 613 data_type_lookup_[i].clear();
607 valuator_min_[i].clear(); 614 valuator_min_[i].clear();
608 valuator_max_[i].clear(); 615 valuator_max_[i].clear();
609 for (int j = 0; j < kMaxSlotNum; j++) 616 for (int j = 0; j < kMaxSlotNum; j++)
610 last_seen_valuator_[i][j].clear(); 617 last_seen_valuator_[i][j].clear();
611 } 618 }
612 619
613 for (size_t i = 0; i < touchscreen.size(); i++) { 620 for (size_t i = 0; i < touchscreen.size(); i++) {
614 unsigned int deviceid = touchscreen[i]; 621 int deviceid = touchscreen[i];
615 InitializeValuatorsForTest(deviceid, kTouchDataTypeStart, kTouchDataTypeEnd, 622 InitializeValuatorsForTest(deviceid, kTouchDataTypeStart, kTouchDataTypeEnd,
616 0, 1000); 623 0, 1000);
617 } 624 }
618 625
619 cmt_devices_.reset(); 626 cmt_devices_.reset();
620 for (size_t i = 0; i < cmt_devices.size(); ++i) { 627 for (size_t i = 0; i < cmt_devices.size(); ++i) {
621 unsigned int deviceid = cmt_devices[i]; 628 int deviceid = cmt_devices[i];
622 cmt_devices_[deviceid] = true; 629 cmt_devices_[deviceid] = true;
623 touchpads_[deviceid] = true; 630 touchpads_[deviceid] = true;
624 InitializeValuatorsForTest(deviceid, kCMTDataTypeStart, kCMTDataTypeEnd, 631 InitializeValuatorsForTest(deviceid, kCMTDataTypeStart, kCMTDataTypeEnd,
625 -1000, 1000); 632 -1000, 1000);
626 } 633 }
627 } 634 }
628 635
629 void DeviceDataManagerX11::SetValuatorDataForTest(XIDeviceEvent* xievent, 636 void DeviceDataManagerX11::SetValuatorDataForTest(XIDeviceEvent* xievent,
630 DataType type, 637 DataType type,
631 double value) { 638 double value) {
(...skipping 26 matching lines...) Expand all
658 last_seen_valuator_[deviceid][j].resize(DT_LAST_ENTRY, 0); 665 last_seen_valuator_[deviceid][j].resize(DT_LAST_ENTRY, 0);
659 for (int j = start_valuator; j <= end_valuator; ++j) { 666 for (int j = start_valuator; j <= end_valuator; ++j) {
660 valuator_lookup_[deviceid][j] = valuator_count_[deviceid]; 667 valuator_lookup_[deviceid][j] = valuator_count_[deviceid];
661 data_type_lookup_[deviceid][valuator_count_[deviceid]] = j; 668 data_type_lookup_[deviceid][valuator_count_[deviceid]] = j;
662 valuator_min_[deviceid][j] = min_value; 669 valuator_min_[deviceid][j] = min_value;
663 valuator_max_[deviceid][j] = max_value; 670 valuator_max_[deviceid][j] = max_value;
664 valuator_count_[deviceid]++; 671 valuator_count_[deviceid]++;
665 } 672 }
666 } 673 }
667 674
668 bool DeviceDataManagerX11::TouchEventNeedsCalibrate( 675 bool DeviceDataManagerX11::TouchEventNeedsCalibrate(int touch_device_id) const {
669 unsigned int touch_device_id) const {
670 #if defined(OS_CHROMEOS) 676 #if defined(OS_CHROMEOS)
671 if (!base::SysInfo::IsRunningOnChromeOS()) 677 if (!base::SysInfo::IsRunningOnChromeOS())
672 return false; 678 return false;
673 679
674 const std::vector<TouchscreenDevice>& touch_devices = 680 const std::vector<TouchscreenDevice>& touch_devices =
675 ui::DeviceDataManager::GetInstance()->touchscreen_devices(); 681 ui::DeviceDataManager::GetInstance()->touchscreen_devices();
676 std::vector<TouchscreenDevice>::const_iterator it = 682 std::vector<TouchscreenDevice>::const_iterator it =
677 std::find_if(touch_devices.begin(), touch_devices.end(), 683 std::find_if(touch_devices.begin(), touch_devices.end(),
678 std::bind2nd(std::ptr_fun(&DeviceHasId), touch_device_id)); 684 std::bind2nd(std::ptr_fun(&DeviceHasId), touch_device_id));
679 return it != touch_devices.end() && it->type == INPUT_DEVICE_INTERNAL; 685 return it != touch_devices.end() && it->type == INPUT_DEVICE_INTERNAL;
680 #endif // defined(OS_CHROMEOS) 686 #endif // defined(OS_CHROMEOS)
681 return false; 687 return false;
682 } 688 }
683 689
684 void DeviceDataManagerX11::SetDisabledKeyboardAllowedKeys( 690 void DeviceDataManagerX11::SetDisabledKeyboardAllowedKeys(
685 scoped_ptr<std::set<KeyboardCode> > excepted_keys) { 691 scoped_ptr<std::set<KeyboardCode> > excepted_keys) {
686 DCHECK(!excepted_keys.get() || 692 DCHECK(!excepted_keys.get() ||
687 !blocked_keyboard_allowed_keys_.get()); 693 !blocked_keyboard_allowed_keys_.get());
688 blocked_keyboard_allowed_keys_ = excepted_keys.Pass(); 694 blocked_keyboard_allowed_keys_ = excepted_keys.Pass();
689 } 695 }
690 696
691 void DeviceDataManagerX11::DisableDevice(unsigned int deviceid) { 697 void DeviceDataManagerX11::DisableDevice(int deviceid) {
692 blocked_devices_.set(deviceid, true); 698 blocked_devices_.set(deviceid, true);
693 // TODO(rsadam@): Support blocking touchscreen devices. 699 // TODO(rsadam@): Support blocking touchscreen devices.
694 std::vector<KeyboardDevice> keyboards = keyboard_devices(); 700 std::vector<KeyboardDevice> keyboards = keyboard_devices();
695 std::vector<KeyboardDevice>::iterator it = 701 std::vector<KeyboardDevice>::iterator it =
696 std::find_if(keyboards.begin(), 702 std::find_if(keyboards.begin(),
697 keyboards.end(), 703 keyboards.end(),
698 std::bind2nd(std::ptr_fun(&DeviceHasId), deviceid)); 704 std::bind2nd(std::ptr_fun(&DeviceHasId), deviceid));
699 if (it != std::end(keyboards)) { 705 if (it != std::end(keyboards)) {
700 blocked_keyboards_.insert( 706 blocked_keyboards_.insert(
701 std::pair<unsigned int, KeyboardDevice>(deviceid, *it)); 707 std::pair<int, KeyboardDevice>(deviceid, *it));
702 keyboards.erase(it); 708 keyboards.erase(it);
703 DeviceDataManager::OnKeyboardDevicesUpdated(keyboards); 709 DeviceDataManager::OnKeyboardDevicesUpdated(keyboards);
704 } 710 }
705 } 711 }
706 712
707 void DeviceDataManagerX11::EnableDevice(unsigned int deviceid) { 713 void DeviceDataManagerX11::EnableDevice(int deviceid) {
708 blocked_devices_.set(deviceid, false); 714 blocked_devices_.set(deviceid, false);
709 std::map<unsigned int, KeyboardDevice>::iterator it = 715 std::map<int, KeyboardDevice>::iterator it =
710 blocked_keyboards_.find(deviceid); 716 blocked_keyboards_.find(deviceid);
711 if (it != blocked_keyboards_.end()) { 717 if (it != blocked_keyboards_.end()) {
712 std::vector<KeyboardDevice> devices = keyboard_devices(); 718 std::vector<KeyboardDevice> devices = keyboard_devices();
713 // Add device to current list of active devices. 719 // Add device to current list of active devices.
714 devices.push_back((*it).second); 720 devices.push_back((*it).second);
715 blocked_keyboards_.erase(it); 721 blocked_keyboards_.erase(it);
716 DeviceDataManager::OnKeyboardDevicesUpdated(devices); 722 DeviceDataManager::OnKeyboardDevicesUpdated(devices);
717 } 723 }
718 } 724 }
719 725
(...skipping 13 matching lines...) Expand all
733 blocked_keyboard_allowed_keys_->end()) { 739 blocked_keyboard_allowed_keys_->end()) {
734 return false; 740 return false;
735 } 741 }
736 742
737 return blocked_devices_.test(xievent->sourceid); 743 return blocked_devices_.test(xievent->sourceid);
738 } 744 }
739 745
740 void DeviceDataManagerX11::OnKeyboardDevicesUpdated( 746 void DeviceDataManagerX11::OnKeyboardDevicesUpdated(
741 const std::vector<KeyboardDevice>& devices) { 747 const std::vector<KeyboardDevice>& devices) {
742 std::vector<KeyboardDevice> keyboards(devices); 748 std::vector<KeyboardDevice> keyboards(devices);
743 for (std::map<unsigned int, KeyboardDevice>::iterator blocked_iter = 749 for (std::map<int, KeyboardDevice>::iterator blocked_iter =
744 blocked_keyboards_.begin(); 750 blocked_keyboards_.begin();
745 blocked_iter != blocked_keyboards_.end();) { 751 blocked_iter != blocked_keyboards_.end();) {
746 // Check if the blocked device still exists in list of devices. 752 // Check if the blocked device still exists in list of devices.
747 std::vector<KeyboardDevice>::iterator it = std::find_if( 753 std::vector<KeyboardDevice>::iterator it = std::find_if(
748 keyboards.begin(), keyboards.end(), 754 keyboards.begin(), keyboards.end(),
749 std::bind2nd(std::ptr_fun(&DeviceHasId), (*blocked_iter).first)); 755 std::bind2nd(std::ptr_fun(&DeviceHasId), (*blocked_iter).first));
750 // If the device no longer exists, unblock it, else filter it out from our 756 // If the device no longer exists, unblock it, else filter it out from our
751 // active list. 757 // active list.
752 if (it == keyboards.end()) { 758 if (it == keyboards.end()) {
753 blocked_devices_.set((*blocked_iter).first, false); 759 blocked_devices_.set((*blocked_iter).first, false);
754 blocked_keyboards_.erase(blocked_iter++); 760 blocked_keyboards_.erase(blocked_iter++);
755 } else { 761 } else {
756 keyboards.erase(it); 762 keyboards.erase(it);
757 ++blocked_iter; 763 ++blocked_iter;
758 } 764 }
759 } 765 }
760 // Notify base class of updated list. 766 // Notify base class of updated list.
761 DeviceDataManager::OnKeyboardDevicesUpdated(keyboards); 767 DeviceDataManager::OnKeyboardDevicesUpdated(keyboards);
762 } 768 }
763 769
764 } // namespace ui 770 } // namespace ui
OLDNEW
« no previous file with comments | « ui/events/devices/x11/device_data_manager_x11.h ('k') | ui/events/devices/x11/device_data_manager_x11_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698