Index: ui/events/ozone/evdev/libgestures_glue/gesture_property_provider.h |
diff --git a/ui/events/ozone/evdev/libgestures_glue/gesture_property_provider.h b/ui/events/ozone/evdev/libgestures_glue/gesture_property_provider.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9ce846dc0321a75daeb9dfb942476276630d525e |
--- /dev/null |
+++ b/ui/events/ozone/evdev/libgestures_glue/gesture_property_provider.h |
@@ -0,0 +1,378 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef UI_EVENTS_OZONE_EVDEV_LIBGESTURES_GLUE_GESTURE_PROPERTY_PROVIDER_H_ |
+#define UI_EVENTS_OZONE_EVDEV_LIBGESTURES_GLUE_GESTURE_PROPERTY_PROVIDER_H_ |
+ |
+#include <gestures/gestures.h> |
+#include <libevdev/libevdev.h> |
+ |
+#include <map> |
+#include <string> |
+#include <vector> |
+ |
+#include "base/basictypes.h" |
+#include "base/containers/hash_tables.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "ui/events/ozone/evdev/events_ozone_evdev_export.h" |
+ |
+template <typename T> struct DefaultSingletonTraits; |
+ |
+namespace ui { |
+ |
+class GesturesPropFunctionsWrapper; |
+ |
+// Provide the interface to access the CrOS gesture library properties. |
+// It maintains the property data for all input devices that runs with |
+// the gesture library. |
+// |
+// The class also parses the configuration files on the system to |
+// initialize the specified property values. The configuration files are |
+// currently in the xorg-conf format so that they can be shared with non-Ozone |
+// builds. |
+class EVENTS_OZONE_EVDEV_EXPORT GesturePropertyProvider { |
+ friend class GesturesPropFunctionsWrapper; |
+ public: |
+ static GesturePropertyProvider* GetInstance(); |
+ |
+ // Device types. |
+ enum DeviceType { |
+ DT_MOUSE, |
+ DT_TOUCHPAD, |
+ DT_TOUCHSCREEN, |
+ DT_MULTITOUCH, |
+ DT_MULTITOUCH_MOUSE, |
+ DT_ALL, |
+ }; |
+ |
+ // Property types. |
+ enum PropertyType { |
+ PT_INT, |
+ PT_SHORT, |
+ PT_BOOL, |
+ PT_STRING, |
+ PT_REAL, |
+ }; |
+ |
+ // A struct holding device properties that are useful when interacting with |
+ // the gestures lib. |
+ struct DeviceProperty { |
+ int area_left; |
+ int area_right; |
+ int area_top; |
+ int area_bottom; |
+ int res_y; |
+ int res_x; |
+ int orientation_minimum; |
+ int orientation_maximum; |
+ GesturesPropBool raw_passthrough; |
+ GesturesPropBool dump_debug_log; |
+ }; |
+ |
+ // Pointer that leads to the device info. |
+ typedef Evdev* DevicePtr; |
+ |
+ // Use void* for more freedom. |
+ typedef void* PropertyValuePtr; |
+ |
+ // The device ids are only maintained by GesturePropertyProvider to identify |
+ // the input devices that uses the gesture lib and are not to be confused with |
+ // the Evdev input node id, for example. |
+ typedef int DeviceId; |
+ |
+ // Get a list of device ids that matches a device type. |
+ void GetDeviceIndices(const DeviceType type, |
+ std::vector<DeviceId>* device_ids); |
+ |
+ // Property access functions for other classes in Chrome, e.g., the |
+ // preferences page. Note that the values in preferences are not synced with |
+ // the ones here in realtime - they are only applied from the preference |
+ // side in a single way once appropriate (e.g., when the user clicked "OK"). |
+ |
+ // Get the property value. |
+ void GetProperty(const DeviceId device_id, |
+ const std::string& name, |
+ DeviceType* type, |
+ PropertyValuePtr* val, |
+ size_t* count); |
+ |
+ // Set the property value. |
+ void SetProperty(const DeviceId device_id, |
+ const std::string& name, |
+ const DeviceType type, |
+ const PropertyValuePtr val, |
+ const size_t count); |
+ private: |
+ // Requirement for Singleton |
+ friend struct DefaultSingletonTraits<GesturePropertyProvider>; |
+ |
+ // Base class for device match criterias in conf files. |
+ // Check the xorg-conf spec for more detailed information. |
+ class MatchCriteria { |
+ public: |
+ MatchCriteria(const std::string& arg); |
+ virtual ~MatchCriteria() {}; |
+ virtual bool Match(DevicePtr device) = 0; |
+ protected: |
+ std::vector<std::string> args_; |
+ }; |
+ |
+ // Trick for converting strings to class names. |
+ template <typename T> |
+ MatchCriteria* AllocateMatchCriteria(const std::string& arg) { |
+ return new T(arg); |
+ } |
+ typedef std::map<std::string, |
+ MatchCriteria* (ui::GesturePropertyProvider::*)( |
+ const std::string&)> MatchCriteriaCreatorMap; |
+ |
+ // Match a device based on its evdev name string. |
+ class MatchProduct : public MatchCriteria { |
+ public: |
+ MatchProduct(const std::string& arg); |
+ virtual ~MatchProduct() {}; |
+ virtual bool Match(DevicePtr device); |
+ }; |
+ |
+ // Math a device based on its device node path. |
+ class MatchDevicePath : public MatchCriteria { |
+ public: |
+ MatchDevicePath(const std::string& arg); |
+ virtual ~MatchDevicePath() {}; |
+ virtual bool Match(DevicePtr device); |
+ }; |
+ |
+ // Math a USB device based on its USB vid and pid. |
+ // Mostly used for external mice and touchpads. |
+ class MatchUSBID : public MatchCriteria { |
+ public: |
+ MatchUSBID(const std::string& arg); |
+ virtual ~MatchUSBID() {}; |
+ virtual bool Match(DevicePtr device); |
+ private: |
+ bool IsValidPattern(const std::string &pattern); |
+ std::vector<std::string> vid_patterns_; |
+ std::vector<std::string> pid_patterns_; |
+ }; |
+ |
+ // Generic base class for device type math criteria. |
+ class MatchDeviceType : public MatchCriteria { |
+ public: |
+ MatchDeviceType(const std::string& arg); |
+ virtual ~MatchDeviceType() {}; |
+ virtual bool Match(DevicePtr device) = 0; |
+ protected: |
+ bool value_; |
+ bool is_valid_; |
+ }; |
+ |
+ // Check if a device is a pointer device. |
+ class MatchIsPointer : public MatchDeviceType { |
+ public: |
+ MatchIsPointer(const std::string& arg); |
+ virtual ~MatchIsPointer() {}; |
+ virtual bool Match(DevicePtr device); |
+ }; |
+ |
+ // Check if a device is a touchpad. |
+ class MatchIsTouchpad : public MatchDeviceType { |
+ public: |
+ MatchIsTouchpad(const std::string& arg); |
+ virtual ~MatchIsTouchpad() {}; |
+ virtual bool Match(DevicePtr device); |
+ }; |
+ |
+ // Check if a device is a touchscreen. |
+ class MatchIsTouchscreen : public MatchDeviceType { |
+ public: |
+ MatchIsTouchscreen(const std::string& arg); |
+ virtual ~MatchIsTouchscreen() {}; |
+ virtual bool Match(DevicePtr device); |
+ }; |
+ |
+ // Struct for sections in xorg conf files. |
+ struct ConfigurationSection { |
+ bool Match(DevicePtr device); |
+ std::string identifier; |
+ std::vector<MatchCriteria*> criterias; |
+ std::vector<GesturesProp> properties; |
+ }; |
+ |
+ // Mapping table from a dev pointer to its device id. |
+ typedef std::map<DevicePtr, DeviceId> DeviceIdMap; |
+ |
+ // Mapping table from a property name to its corresponding GesturesProp |
+ // object pointer. |
+ typedef base::hash_map<std::string, GesturesProp*> PropertyMap; |
+ |
+ // Mapping table from a dev id to its property map. |
+ typedef std::map<DeviceId, PropertyMap*> DevicePropertyMap; |
alexst (slow to review)
2014/09/17 14:39:53
Just a drive-by comment, could you use scoped_ptr_
|
+ |
+ GesturePropertyProvider(); |
+ ~GesturePropertyProvider(); |
+ |
+ // Populate the match criteria creators in the creator map. |
+ void RegisterMatchCriterias(); |
+ |
+ // Get the device id of a device pointer. May create a new one if not |
+ // currently mapped. |
+ DeviceId GetDeviceId(DevicePtr dev, const bool do_create = false); |
+ DeviceId GetDeviceId(void* dev, const bool do_create = false) { |
+ return GetDeviceId(static_cast<DevicePtr>(dev), do_create); |
+ } |
+ |
+ // Called by functions in GesturesPropFunctionsWrapper to manipulate |
+ // properties. Note these functions do not new/delete the GesturesProp |
+ // pointers. It is caller's responsibility to manage them. |
+ void AddProperty(const DeviceId device_id, |
+ const std::string& name, |
+ GesturesProp* prop); |
+ void DeleteProperty(const DeviceId device_id, const std::string& name); |
+ |
+ // Check if a property exists for a device. Return if it is found. |
+ GesturesProp* FindProperty(const DeviceId device_id, const std::string& name); |
+ |
+ // Core function for property finding routines. |
+ GesturesProp* FindProperty(const DevicePropertyMap& device_property_map, |
+ const DeviceId device_id, |
+ const std::string& name); |
+ |
+ // Get the default value of a property based on the configuration files. |
+ GesturesProp* GetDefaultProperty(const DeviceId device_id, |
+ const std::string& name); |
+ |
+ // The device configuration files are parsed and stored in the memory upon |
+ // Chrome starts. The default property values are then applied to each device |
+ // when it is attached/detected. |
+ void LoadDeviceConfigurations(); |
+ |
+ // Parse a xorg-conf file. We ignore all sections other than InputClass. |
+ // Check the xorg-conf spec for more infomation about its format. |
+ void ParseXorgConfFile(const std::string& content); |
+ |
+ // Check if a match criteria is currently implemented. Note that we didn't |
+ // implemented all of them as some are inapplicable in the non-X world. |
+ static bool IsMatchTypeSupported(const std::string& match_type); |
+ |
+ // Check if a match criteria is a device type one. |
+ static bool IsDeviceMatchType(const std::string& match_type); |
+ |
+ // Create a match criteria. |
+ MatchCriteria* CreateMatchCriteria(const std::string& match_type, |
+ const std::string& arg); |
+ |
+ // Create a property that comes from the conf files. |
+ bool CreateDefaultProperty(const std::string& name, |
+ const std::string& value, |
+ GesturesProp* prop); |
+ |
+ // Parse a boolean value keyword (e.g., on/off, true/false). |
+ static int ParseBooleanKeyword(const std::string& value); |
+ |
+ // Setup default property values for a newly found device. |
+ void SetupDefaultProperties(const DeviceId device_id, DevicePtr dev); |
+ |
+ // Max number of devices that we track. |
+ static const DeviceId kMaxDeviceNum = 0xffff; |
+ |
+ // A map that stores the match criteria creators. |
+ MatchCriteriaCreatorMap match_criteria_map_; |
+ |
+ // An incremental counter of the next device id to be used for mapping. |
+ DeviceId device_id_counter_; |
+ |
+ // Map of device ids. |
+ DeviceIdMap device_ids_map_; |
+ |
+ // Array of property maps indexed by their respective device ids. |
+ DevicePropertyMap properties_maps_; |
+ |
+ // Same as properties_maps_, but loaded with only values specified in the |
+ // configuration files. Will be applied when a property of the same name is |
+ // created. |
+ DevicePropertyMap default_properties_maps_; |
+ |
+ // A vector of parsed sections in configuration files. |
+ std::vector<ConfigurationSection> configurations_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(GesturePropertyProvider); |
+}; |
+ |
+// Wrapper of GesturesProp related functions. We group them together so that we |
+// can friend them all at once. |
+class GesturesPropFunctionsWrapper { |
+ public: |
+ // Property provider interface implementation. |
+ // |
+ // These functions will create a GesturesProp object that can link back to the |
+ // memory that holds the real value, which is often declared in the gesture |
+ // lib. |
+ static GesturesProp* CreateInt(void*, const char*, int*, size_t, const int*); |
+ static GesturesProp* CreateShort(void*, |
+ const char*, |
+ short*, |
+ size_t, |
+ const short*); |
+ static GesturesProp* CreateBool(void*, |
+ const char*, |
+ GesturesPropBool*, |
+ size_t, |
+ const GesturesPropBool*); |
+ |
+ // String GestureProps needs special care due to the use of const char* in the |
+ // gesture lib. Its argument list is also different from numeric properties. |
+ static GesturesProp* CreateString(void*, |
+ const char*, |
+ const char**, |
+ const char*); |
+ |
+ static GesturesProp* CreateReal(void*, |
+ const char*, |
+ double*, |
+ size_t, |
+ const double*); |
+ |
+ // Set the handlers to call when a property is accessed. |
+ static void RegisterHandlers(void*, |
+ GesturesProp*, |
+ void*, |
+ GesturesPropGetHandler, |
+ GesturesPropSetHandler); |
+ |
+ // Free a property. |
+ static void Free(void*, GesturesProp*); |
+ |
+ // Initialize hardware-related device properties which will be used in the |
+ // gesture lib. |
+ static bool InitializeDeviceProperties( |
+ void*, |
+ GesturePropertyProvider::DeviceProperty* props); |
+ |
+ private: |
+ // Property helper functions. |
+ // Core function for creating properties in GesturePropertyProvider. |
+ template <typename T> |
+ static GesturesProp* Create(void*, |
+ const char*, |
+ GesturePropertyProvider::PropertyType, |
+ T*, |
+ size_t); |
+ |
+ // Template function for creating numeric GestureProps (e.g., int, short, |
+ // double). String property doesn't use this. |
+ template <typename T, GesturePropertyProvider::PropertyType type> |
+ static GesturesProp* CreateProperty(void*, const char*, T*, size_t, const T*); |
+ |
+ static GesturesProp* CreateIntSingle(void*, const char*, int*, int); |
+ static GesturesProp* CreateBoolSingle(void*, |
+ const char*, |
+ GesturesPropBool*, |
+ GesturesPropBool); |
+}; |
+ |
+extern const GesturesPropProvider kGesturePropProvider; |
+ |
+} // namspace ui |
+ |
+#endif // UI_EVENTS_OZONE_EVDEV_LIBGESTURES_GLUE_GESTURE_PROPERTY_PROVIDER_H_ |