| OLD | NEW |
| 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/ozone/evdev/libgestures_glue/gesture_property_provider.h" | 5 #include "ui/events/ozone/evdev/libgestures_glue/gesture_property_provider.h" |
| 6 | 6 |
| 7 #include <fnmatch.h> | 7 #include <fnmatch.h> |
| 8 #include <gestures/gestures.h> | 8 #include <gestures/gestures.h> |
| 9 #include <libevdev/libevdev.h> | 9 #include <libevdev/libevdev.h> |
| 10 #include <stdint.h> | 10 #include <stdint.h> |
| 11 #include <string.h> | 11 #include <string.h> |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 #include <unordered_map> |
| 14 | 15 |
| 15 #include "base/containers/hash_tables.h" | 16 #include "base/containers/hash_tables.h" |
| 16 #include "base/files/file_enumerator.h" | 17 #include "base/files/file_enumerator.h" |
| 17 #include "base/files/file_util.h" | 18 #include "base/files/file_util.h" |
| 18 #include "base/logging.h" | 19 #include "base/logging.h" |
| 19 #include "base/macros.h" | 20 #include "base/macros.h" |
| 20 #include "base/memory/ptr_util.h" | 21 #include "base/memory/ptr_util.h" |
| 21 #include "base/strings/string_number_conversions.h" | 22 #include "base/strings/string_number_conversions.h" |
| 22 #include "base/strings/string_split.h" | 23 #include "base/strings/string_split.h" |
| 23 #include "base/strings/string_tokenizer.h" | 24 #include "base/strings/string_tokenizer.h" |
| (...skipping 578 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 602 LOG(ERROR) << "Unknown gesture property type: " << property->type(); | 603 LOG(ERROR) << "Unknown gesture property type: " << property->type(); |
| 603 NOTREACHED(); | 604 NOTREACHED(); |
| 604 break; | 605 break; |
| 605 } | 606 } |
| 606 return os; | 607 return os; |
| 607 } | 608 } |
| 608 | 609 |
| 609 namespace ui { | 610 namespace ui { |
| 610 namespace internal { | 611 namespace internal { |
| 611 | 612 |
| 612 // Mapping table from a property name to its corresponding GesturesProp | |
| 613 // object pointer. | |
| 614 typedef base::hash_map<std::string, GesturesProp*> PropertiesMap; | |
| 615 typedef base::ScopedPtrHashMap<std::string, std::unique_ptr<GesturesProp>> | |
| 616 ScopedPropertiesMap; | |
| 617 | |
| 618 // Struct holding properties of a device. | 613 // Struct holding properties of a device. |
| 619 // | 614 // |
| 620 // Note that we can't define it in GesturePropertyProvider as a nested class | 615 // Note that we can't define it in GesturePropertyProvider as a nested class |
| 621 // because ScopedPtrHashMap will require us to expose the GestureProp's | 616 // because ScopedPtrHashMap will require us to expose the GestureProp's |
| 622 // destructor so that it can instantiate the object. This is something we | 617 // destructor so that it can instantiate the object. This is something we |
| 623 // don't want to do. | 618 // don't want to do. |
| 624 struct GestureDevicePropertyData { | 619 struct GestureDevicePropertyData { |
| 625 GestureDevicePropertyData() {} | 620 GestureDevicePropertyData() {} |
| 626 | 621 |
| 627 // Properties owned and being used by the device. | 622 // Properties owned and being used by the device. |
| 628 ScopedPropertiesMap properties; | 623 std::unordered_map<std::string, std::unique_ptr<GesturesProp>> properties; |
| 629 | 624 |
| 630 // Unowned default properties (owned by the configuration file). Their values | 625 // Unowned default properties (owned by the configuration file). Their values |
| 631 // will be applied when a property of the same name is created. These are | 626 // will be applied when a property of the same name is created. These are |
| 632 // usually only a small portion of all properties in use. | 627 // usually only a small portion of all properties in use. |
| 633 PropertiesMap default_properties; | 628 base::hash_map<std::string, GesturesProp*> default_properties; |
| 634 }; | 629 }; |
| 635 | 630 |
| 636 // Base class for device match criterias in conf files. | 631 // Base class for device match criterias in conf files. |
| 637 // Check the xorg-conf spec for more detailed information. | 632 // Check the xorg-conf spec for more detailed information. |
| 638 class MatchCriteria { | 633 class MatchCriteria { |
| 639 public: | 634 public: |
| 640 typedef ui::GesturePropertyProvider::DevicePtr DevicePtr; | 635 typedef ui::GesturePropertyProvider::DevicePtr DevicePtr; |
| 641 explicit MatchCriteria(const std::string& arg); | 636 explicit MatchCriteria(const std::string& arg); |
| 642 virtual ~MatchCriteria() {} | 637 virtual ~MatchCriteria() {} |
| 643 virtual bool Match(const DevicePtr device) = 0; | 638 virtual bool Match(const DevicePtr device) = 0; |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 868 LoadDeviceConfigurations(); | 863 LoadDeviceConfigurations(); |
| 869 } | 864 } |
| 870 | 865 |
| 871 GesturePropertyProvider::~GesturePropertyProvider() { | 866 GesturePropertyProvider::~GesturePropertyProvider() { |
| 872 } | 867 } |
| 873 | 868 |
| 874 bool GesturePropertyProvider::GetDeviceIdsByType( | 869 bool GesturePropertyProvider::GetDeviceIdsByType( |
| 875 const EventDeviceType type, | 870 const EventDeviceType type, |
| 876 std::vector<DeviceId>* device_ids) { | 871 std::vector<DeviceId>* device_ids) { |
| 877 bool exists = false; | 872 bool exists = false; |
| 878 DeviceMap::const_iterator it = device_map_.begin(); | 873 for (auto it = device_map_.begin(); it != device_map_.end(); ++it) { |
| 879 for (; it != device_map_.end(); ++it) { | |
| 880 if (IsDeviceIdOfType(it->first, type)) { | 874 if (IsDeviceIdOfType(it->first, type)) { |
| 881 exists = true; | 875 exists = true; |
| 882 if (device_ids) | 876 if (device_ids) |
| 883 device_ids->push_back(it->first); | 877 device_ids->push_back(it->first); |
| 884 } | 878 } |
| 885 } | 879 } |
| 886 return exists; | 880 return exists; |
| 887 } | 881 } |
| 888 | 882 |
| 889 bool GesturePropertyProvider::IsDeviceIdOfType(const DeviceId device_id, | 883 bool GesturePropertyProvider::IsDeviceIdOfType(const DeviceId device_id, |
| 890 const EventDeviceType type) { | 884 const EventDeviceType type) { |
| 891 DeviceMap::const_iterator it = device_map_.find(device_id); | 885 auto it = device_map_.find(device_id); |
| 892 if (it == device_map_.end()) | 886 if (it == device_map_.end()) |
| 893 return false; | 887 return false; |
| 894 return IsDeviceOfType(it->second, type, | 888 return IsDeviceOfType(it->second, type, |
| 895 GetProperty(device_id, "Device Mouse"), | 889 GetProperty(device_id, "Device Mouse"), |
| 896 GetProperty(device_id, "Device Touchpad")); | 890 GetProperty(device_id, "Device Touchpad")); |
| 897 } | 891 } |
| 898 | 892 |
| 899 GesturesProp* GesturePropertyProvider::GetProperty(const DeviceId device_id, | 893 GesturesProp* GesturePropertyProvider::GetProperty(const DeviceId device_id, |
| 900 const std::string& name) { | 894 const std::string& name) { |
| 901 return FindProperty(device_id, name); | 895 return FindProperty(device_id, name); |
| 902 } | 896 } |
| 903 | 897 |
| 904 std::vector<std::string> GesturePropertyProvider::GetPropertyNamesById( | 898 std::vector<std::string> GesturePropertyProvider::GetPropertyNamesById( |
| 905 const DeviceId device_id) { | 899 const DeviceId device_id) { |
| 906 internal::GestureDevicePropertyData* device_data = | 900 auto it = device_data_map_.find(device_id); |
| 907 device_data_map_.get(device_id); | 901 if (it == device_data_map_.end()) |
| 908 if (!device_data) | |
| 909 return std::vector<std::string>(); | 902 return std::vector<std::string>(); |
| 910 | 903 |
| 904 internal::GestureDevicePropertyData* device_data = it->second.get(); |
| 905 |
| 911 // Dump all property names of the device. | 906 // Dump all property names of the device. |
| 912 std::vector<std::string> names; | 907 std::vector<std::string> names; |
| 913 for (internal::ScopedPropertiesMap::const_iterator it = | 908 for (auto it = device_data->properties.begin(); |
| 914 device_data->properties.begin(); | |
| 915 it != device_data->properties.end(); ++it) | 909 it != device_data->properties.end(); ++it) |
| 916 names.push_back(it->first); | 910 names.push_back(it->first); |
| 917 return names; | 911 return names; |
| 918 } | 912 } |
| 919 | 913 |
| 920 std::string GesturePropertyProvider::GetDeviceNameById( | 914 std::string GesturePropertyProvider::GetDeviceNameById( |
| 921 const DeviceId device_id) { | 915 const DeviceId device_id) { |
| 922 DeviceMap::const_iterator it = device_map_.find(device_id); | 916 auto it = device_map_.find(device_id); |
| 923 if (it == device_map_.end()) | 917 if (it == device_map_.end()) |
| 924 return std::string(); | 918 return std::string(); |
| 925 return std::string(it->second->info.name); | 919 return std::string(it->second->info.name); |
| 926 } | 920 } |
| 927 | 921 |
| 928 void GesturePropertyProvider::RegisterDevice(const DeviceId id, | 922 void GesturePropertyProvider::RegisterDevice(const DeviceId id, |
| 929 const DevicePtr device) { | 923 const DevicePtr device) { |
| 930 DeviceMap::const_iterator it = device_map_.find(id); | 924 auto it = device_map_.find(id); |
| 931 if (it != device_map_.end()) | 925 if (it != device_map_.end()) |
| 932 return; | 926 return; |
| 933 | 927 |
| 934 // Setup data-structures. | 928 // Setup data-structures. |
| 935 device_map_[id] = device; | 929 device_map_[id] = device; |
| 936 device_data_map_.set(id, std::unique_ptr<internal::GestureDevicePropertyData>( | 930 device_data_map_[id] = |
| 937 new internal::GestureDevicePropertyData)); | 931 base::MakeUnique<internal::GestureDevicePropertyData>(); |
| 938 | 932 |
| 939 // Gather default property values for the device from the parsed conf files. | 933 // Gather default property values for the device from the parsed conf files. |
| 940 SetupDefaultProperties(id, device); | 934 SetupDefaultProperties(id, device); |
| 941 return; | 935 return; |
| 942 } | 936 } |
| 943 | 937 |
| 944 void GesturePropertyProvider::UnregisterDevice(const DeviceId id) { | 938 void GesturePropertyProvider::UnregisterDevice(const DeviceId id) { |
| 945 DeviceMap::const_iterator it = device_map_.find(id); | 939 auto it = device_map_.find(id); |
| 946 if (it == device_map_.end()) | 940 if (it == device_map_.end()) |
| 947 return; | 941 return; |
| 948 device_data_map_.erase(id); | 942 device_data_map_.erase(id); |
| 949 device_map_.erase(it); | 943 device_map_.erase(it); |
| 950 } | 944 } |
| 951 | 945 |
| 952 void GesturePropertyProvider::AddProperty( | 946 void GesturePropertyProvider::AddProperty( |
| 953 const DeviceId device_id, | 947 const DeviceId device_id, |
| 954 const std::string& name, | 948 const std::string& name, |
| 955 std::unique_ptr<GesturesProp> property) { | 949 std::unique_ptr<GesturesProp> property) { |
| 956 // The look-up should never fail because ideally a property can only be | 950 // The look-up should never fail because ideally a property can only be |
| 957 // created with GesturesPropCreate* functions from the gesture lib side. | 951 // created with GesturesPropCreate* functions from the gesture lib side. |
| 958 // Therefore, we simply return on failure. | 952 // Therefore, we simply return on failure. |
| 959 internal::GestureDevicePropertyData* device_data = | 953 auto it = device_data_map_.find(device_id); |
| 960 device_data_map_.get(device_id); | 954 if (it != device_data_map_.end()) |
| 961 if (device_data) | 955 it->second->properties[name] = std::move(property); |
| 962 device_data->properties.set(name, std::move(property)); | |
| 963 } | 956 } |
| 964 | 957 |
| 965 void GesturePropertyProvider::DeleteProperty(const DeviceId device_id, | 958 void GesturePropertyProvider::DeleteProperty(const DeviceId device_id, |
| 966 const std::string& name) { | 959 const std::string& name) { |
| 967 internal::GestureDevicePropertyData* device_data = | 960 auto it = device_data_map_.find(device_id); |
| 968 device_data_map_.get(device_id); | 961 if (it != device_data_map_.end()) |
| 969 if (device_data) | 962 it->second->properties.erase(name); |
| 970 device_data->properties.erase(name); | |
| 971 } | 963 } |
| 972 | 964 |
| 973 GesturesProp* GesturePropertyProvider::FindProperty(const DeviceId device_id, | 965 GesturesProp* GesturePropertyProvider::FindProperty(const DeviceId device_id, |
| 974 const std::string& name) { | 966 const std::string& name) { |
| 975 internal::GestureDevicePropertyData* device_data = | 967 auto it = device_data_map_.find(device_id); |
| 976 device_data_map_.get(device_id); | 968 if (it == device_data_map_.end()) |
| 977 if (!device_data) | 969 return nullptr; |
| 978 return NULL; | 970 |
| 979 return device_data->properties.get(name); | 971 auto it2 = it->second->properties.find(name); |
| 972 if (it2 == it->second->properties.end()) |
| 973 return nullptr; |
| 974 |
| 975 return it2->second.get(); |
| 980 } | 976 } |
| 981 | 977 |
| 982 GesturesProp* GesturePropertyProvider::GetDefaultProperty( | 978 GesturesProp* GesturePropertyProvider::GetDefaultProperty( |
| 983 const DeviceId device_id, | 979 const DeviceId device_id, |
| 984 const std::string& name) { | 980 const std::string& name) { |
| 985 internal::GestureDevicePropertyData* device_data = | 981 auto it = device_data_map_.find(device_id); |
| 986 device_data_map_.get(device_id); | 982 if (it == device_data_map_.end()) |
| 987 if (!device_data) | 983 return nullptr; |
| 988 return NULL; | 984 |
| 989 internal::PropertiesMap::const_iterator ib = | 985 auto it2 = it->second->default_properties.find(name); |
| 990 device_data->default_properties.find(name); | 986 if (it2 == it->second->default_properties.end()) |
| 991 if (ib == device_data->default_properties.end()) | 987 return nullptr; |
| 992 return NULL; | 988 |
| 993 return ib->second; | 989 return it2->second; |
| 994 } | 990 } |
| 995 | 991 |
| 996 void GesturePropertyProvider::LoadDeviceConfigurations() { | 992 void GesturePropertyProvider::LoadDeviceConfigurations() { |
| 997 // Enumerate conf files and sort them lexicographically. | 993 // Enumerate conf files and sort them lexicographically. |
| 998 std::set<base::FilePath> files; | 994 std::set<base::FilePath> files; |
| 999 base::FileEnumerator file_enum(base::FilePath(kConfigurationFilePath), | 995 base::FileEnumerator file_enum(base::FilePath(kConfigurationFilePath), |
| 1000 false, | 996 false, |
| 1001 base::FileEnumerator::FILES, | 997 base::FileEnumerator::FILES, |
| 1002 "*.conf"); | 998 "*.conf"); |
| 1003 for (base::FilePath path = file_enum.Next(); !path.empty(); | 999 for (base::FilePath path = file_enum.Next(); !path.empty(); |
| 1004 path = file_enum.Next()) { | 1000 path = file_enum.Next()) { |
| 1005 files.insert(path); | 1001 files.insert(path); |
| 1006 } | 1002 } |
| 1007 DVLOG(2) << files.size() << " conf files were found"; | 1003 DVLOG(2) << files.size() << " conf files were found"; |
| 1008 | 1004 |
| 1009 // Parse conf files one-by-one. | 1005 // Parse conf files one-by-one. |
| 1010 for (std::set<base::FilePath>::iterator file_iter = files.begin(); | 1006 for (auto file_iter = files.begin(); file_iter != files.end(); ++file_iter) { |
| 1011 file_iter != files.end(); | |
| 1012 ++file_iter) { | |
| 1013 DVLOG(2) << "Parsing conf file: " << (*file_iter).value(); | 1007 DVLOG(2) << "Parsing conf file: " << (*file_iter).value(); |
| 1014 std::string content; | 1008 std::string content; |
| 1015 if (!base::ReadFileToString(*file_iter, &content)) { | 1009 if (!base::ReadFileToString(*file_iter, &content)) { |
| 1016 LOG(ERROR) << "Can't loading gestures conf file: " | 1010 LOG(ERROR) << "Can't loading gestures conf file: " |
| 1017 << (*file_iter).value(); | 1011 << (*file_iter).value(); |
| 1018 continue; | 1012 continue; |
| 1019 } | 1013 } |
| 1020 ParseXorgConfFile(content); | 1014 ParseXorgConfFile(content); |
| 1021 } | 1015 } |
| 1022 } | 1016 } |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1272 // specify some name or args as invalid. | 1266 // specify some name or args as invalid. |
| 1273 return property; | 1267 return property; |
| 1274 } | 1268 } |
| 1275 | 1269 |
| 1276 void GesturePropertyProvider::SetupDefaultProperties(const DeviceId device_id, | 1270 void GesturePropertyProvider::SetupDefaultProperties(const DeviceId device_id, |
| 1277 const DevicePtr device) { | 1271 const DevicePtr device) { |
| 1278 DVLOG(2) << "Setting up default properties for (" << device << ", " | 1272 DVLOG(2) << "Setting up default properties for (" << device << ", " |
| 1279 << device_id << ", " << device->info.name << ")"; | 1273 << device_id << ", " << device->info.name << ")"; |
| 1280 | 1274 |
| 1281 // Go through all parsed sections. | 1275 // Go through all parsed sections. |
| 1282 internal::PropertiesMap& property_map = | 1276 auto& property_map = device_data_map_[device_id]->default_properties; |
| 1283 device_data_map_.get(device_id)->default_properties; | |
| 1284 for (const auto& configuration : configurations_) { | 1277 for (const auto& configuration : configurations_) { |
| 1285 if (configuration->Match(device)) { | 1278 if (configuration->Match(device)) { |
| 1286 DVLOG(2) << "Conf section \"" << configuration->identifier | 1279 DVLOG(2) << "Conf section \"" << configuration->identifier |
| 1287 << "\" is matched"; | 1280 << "\" is matched"; |
| 1288 for (const auto& property : configuration->properties) { | 1281 for (const auto& property : configuration->properties) { |
| 1289 // We can't use insert here because a property may be set for several | 1282 // We can't use insert here because a property may be set for several |
| 1290 // times along the way. | 1283 // times along the way. |
| 1291 property_map[property->name()] = property.get(); | 1284 property_map[property->name()] = property.get(); |
| 1292 } | 1285 } |
| 1293 } | 1286 } |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1546 const GesturesPropProvider kGesturePropProvider = { | 1539 const GesturesPropProvider kGesturePropProvider = { |
| 1547 GesturesPropFunctionsWrapper::CreateInt, | 1540 GesturesPropFunctionsWrapper::CreateInt, |
| 1548 GesturesPropFunctionsWrapper::CreateShort, | 1541 GesturesPropFunctionsWrapper::CreateShort, |
| 1549 GesturesPropFunctionsWrapper::CreateBool, | 1542 GesturesPropFunctionsWrapper::CreateBool, |
| 1550 GesturesPropFunctionsWrapper::CreateString, | 1543 GesturesPropFunctionsWrapper::CreateString, |
| 1551 GesturesPropFunctionsWrapper::CreateReal, | 1544 GesturesPropFunctionsWrapper::CreateReal, |
| 1552 GesturesPropFunctionsWrapper::RegisterHandlers, | 1545 GesturesPropFunctionsWrapper::RegisterHandlers, |
| 1553 GesturesPropFunctionsWrapper::Free}; | 1546 GesturesPropFunctionsWrapper::Free}; |
| 1554 | 1547 |
| 1555 } // namespace ui | 1548 } // namespace ui |
| OLD | NEW |