Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / events / ozone / evdev / libgestures_glue / gesture_property_provider.h
blobbd7b471d361818f4d839059c2cd0cf15166b7ea8
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
3 // found in the LICENSE file.
5 #ifndef UI_EVENTS_OZONE_EVDEV_LIBGESTURES_GLUE_GESTURE_PROPERTY_PROVIDER_H_
6 #define UI_EVENTS_OZONE_EVDEV_LIBGESTURES_GLUE_GESTURE_PROPERTY_PROVIDER_H_
8 #include <gestures/gestures.h>
9 #include <libevdev/libevdev.h>
11 #include <map>
12 #include <ostream>
13 #include <string>
14 #include <vector>
16 #include "base/basictypes.h"
17 #include "base/containers/scoped_ptr_hash_map.h"
18 #include "base/memory/scoped_vector.h"
19 #include "ui/events/ozone/evdev/event_device_info.h"
20 #include "ui/events/ozone/evdev/events_ozone_evdev_export.h"
22 namespace ui {
24 class GesturesPropFunctionsWrapper;
25 class GestureInterpreterLibevdevCros;
27 // Not for public consumption, so we wrap it in namespace internal.
28 namespace internal {
29 struct GestureDevicePropertyData;
30 class MatchCriteria;
31 struct ConfigurationSection;
34 // A struct holding device properties that are useful when interacting with
35 // the gestures lib.
36 struct GestureDeviceProperties {
37 int area_left;
38 int area_right;
39 int area_top;
40 int area_bottom;
41 int res_y;
42 int res_x;
43 int orientation_minimum;
44 int orientation_maximum;
45 GesturesPropBool raw_passthrough;
46 GesturesPropBool dump_debug_log;
49 // Provide the interface to access the CrOS gesture library properties.
50 // It maintains the property data for all input devices that runs with
51 // the gesture library.
53 // The class also parses the configuration files on the system to
54 // initialize the specified property values. The configuration files are
55 // currently in the xorg-conf format so that they can be shared with non-Ozone
56 // builds.
57 class EVENTS_OZONE_EVDEV_EXPORT GesturePropertyProvider {
58 public:
59 // Property types.
60 enum PropertyType {
61 PT_INT,
62 PT_SHORT,
63 PT_BOOL,
64 PT_STRING,
65 PT_REAL,
68 // Pointer that leads to the device info.
69 typedef Evdev* DevicePtr;
71 // The device ids are only maintained by EventFactoryEvdev to identify the
72 // input devices and are not to be confused with the Evdev input node id, for
73 // example.
74 typedef int DeviceId;
76 GesturePropertyProvider();
77 ~GesturePropertyProvider();
79 // Get a list of device ids that matches a device type. Return true if the
80 // list is not empty. |device_ids| can be NULL. Existing data in |device_ids|
81 // won't be deleted.
82 bool GetDeviceIdsByType(const EventDeviceType type,
83 std::vector<DeviceId>* device_ids);
85 // Check if a device id matches a device type. Return true if it matches.
86 // Return false if it doesn't match or if it doesn't use
87 // the GesturePropertyProvider.
88 bool IsDeviceIdOfType(const DeviceId device_id, const EventDeviceType type);
90 // Get the GesturesProp object. Returns NULL if not found.
92 // The user may use the object returned to set/get the property value in the
93 // gesture library's memory. Note that the values in preferences are not
94 // synced with the ones here in realtime - they are only applied from the
95 // preference side in a single way once appropriate (e.g., when the user
96 // clicked "OK").
97 GesturesProp* GetProperty(const DeviceId device_id, const std::string& name);
99 // Get the names of all properties of one device. Mostly used for the logging
100 // purpose.
101 std::vector<std::string> GetPropertyNamesById(const DeviceId device_id);
103 // Get the (Evdev) device name. Mostly used for the logging purpose.
104 std::string GetDeviceNameById(const DeviceId device_id);
106 private:
107 friend class GesturesPropFunctionsWrapper;
109 // Mapping table from a device id to its device pointer.
110 typedef std::map<DeviceId, DevicePtr> DeviceMap;
112 // Mapping table from a device id to its property data.
113 // GestureDevicePropertyData contains both properties in use and default
114 // properties whose values will be applied upon the device attachment.
115 typedef base::ScopedPtrHashMap<
116 DeviceId,
117 scoped_ptr<internal::GestureDevicePropertyData>> ScopedDeviceDataMap;
119 // Register a device. Setup data-structures and the device's default
120 // properties.
121 void RegisterDevice(const DeviceId id, const DevicePtr device);
123 // Unregister a device. Remove all of its properties being tracked.
124 void UnregisterDevice(const DeviceId id);
126 // Called by functions in GesturesPropFunctionsWrapper to manipulate
127 // properties. Note these functions do not new/delete the GesturesProp
128 // pointers. It is caller's responsibility to manage them.
129 void AddProperty(const DeviceId device_id,
130 const std::string& name,
131 GesturesProp* property);
132 void DeleteProperty(const DeviceId device_id, const std::string& name);
134 // Check if a property exists for a device. Return if it is found.
135 GesturesProp* FindProperty(const DeviceId device_id, const std::string& name);
137 // Get the default value of a property based on the configuration files.
138 GesturesProp* GetDefaultProperty(const DeviceId device_id,
139 const std::string& name);
141 // The device configuration files are parsed and stored in the memory upon
142 // Chrome starts. The default property values are then applied to each device
143 // when it is attached/detected.
144 void LoadDeviceConfigurations();
146 // Parse a xorg-conf file. We ignore all sections other than InputClass.
147 // Check the xorg-conf spec for more infomation about its format.
148 void ParseXorgConfFile(const std::string& content);
150 // Create a match criteria.
151 internal::MatchCriteria* CreateMatchCriteria(const std::string& match_type,
152 const std::string& arg);
154 // Create a property that comes from the conf files.
155 GesturesProp* CreateDefaultProperty(const std::string& name,
156 const std::string& value);
158 // Setup default property values for a newly found device.
159 void SetupDefaultProperties(const DeviceId device_id, const DevicePtr device);
161 // Map from device ids to device pointers.
162 DeviceMap device_map_;
164 // GestureDevicePropertyData indexed by their respective device ids. Owns the
165 // objects.
166 ScopedDeviceDataMap device_data_map_;
168 // A vector of parsed sections in configuration files. Owns MatchCriterias,
169 // GesturesProps and ConfigurationSections in it.
170 ScopedVector<internal::ConfigurationSection> configurations_;
172 DISALLOW_COPY_AND_ASSIGN(GesturePropertyProvider);
175 // Wrapper of GesturesProp related functions. We group them together so that we
176 // can friend them all at once.
177 class GesturesPropFunctionsWrapper {
178 public:
179 // Property provider interface implementation.
181 // These functions will create a GesturesProp object that can link back to the
182 // memory that holds the real value, which is often declared in the gesture
183 // lib.
184 static GesturesProp* CreateInt(void* device_data,
185 const char* name,
186 int* value,
187 size_t count,
188 const int* init);
189 static GesturesProp* CreateShort(void* device_data,
190 const char* name,
191 short* value,
192 size_t count,
193 const short* init);
194 static GesturesProp* CreateBool(void* device_data,
195 const char* name,
196 GesturesPropBool* value,
197 size_t count,
198 const GesturesPropBool* init);
200 // String GestureProps needs special care due to the use of const char* in the
201 // gesture lib. Its argument list is also different from numeric properties'.
202 static GesturesProp* CreateString(void* device_data,
203 const char* name,
204 const char** value,
205 const char* init);
207 static GesturesProp* CreateReal(void* device_data,
208 const char* name,
209 double* value,
210 size_t count,
211 const double* init);
213 // Set the handlers to call when a property is accessed.
214 static void RegisterHandlers(void* device_data,
215 GesturesProp* property,
216 void* handler_data,
217 GesturesPropGetHandler get,
218 GesturesPropSetHandler set);
220 // Free a property.
221 static void Free(void* device_data, GesturesProp* property);
223 // Initialize hardware-related device properties which will be used in the
224 // gesture lib.
225 static bool InitializeDeviceProperties(void* device_data,
226 GestureDeviceProperties* properties);
228 // Unregister device from the gesture property provider.
229 static void UnregisterDevice(void* device_data);
231 private:
232 // Property helper functions.
233 // Core template function for creating GestureProps. Used by numerical types.
234 template <typename T, class PROPTYPE>
235 static GesturesProp* CreateProperty(void* device_data,
236 const char* name,
237 T* value,
238 size_t count,
239 const T* init);
241 // Do things that should happen BEFORE we create the property.
242 static bool PreCreateProperty(void* device_data,
243 const char* name,
244 GesturesProp** default_property);
246 // Do things that should happen AFTER we create the property.
247 static void PostCreateProperty(void* device_data,
248 const char* name,
249 GesturesProp* property);
251 // Some other utility functions used in InitializeDeviceProperties.
252 static GesturesProp* CreateIntSingle(void* device_data,
253 const char* name,
254 int* value,
255 int init);
256 static GesturesProp* CreateBoolSingle(void* device_data,
257 const char* name,
258 GesturesPropBool* value,
259 GesturesPropBool init);
261 // Routines to extract information from the GestureInterpreterLibevdevCros
262 // pointer.
263 static GesturePropertyProvider* GetPropertyProvider(void* device_data);
264 static GesturePropertyProvider::DevicePtr GetDevicePointer(void* device_data);
265 static GesturePropertyProvider::DeviceId GetDeviceId(void* device_data);
268 extern const GesturesPropProvider kGesturePropProvider;
270 } // namspace ui
272 // GesturesProp logging function.
273 std::ostream& operator<<(std::ostream& os, const GesturesProp& prop);
275 // Implementation of GesturesProp declared in gestures.h
277 // libgestures requires that this be in the top level namespace. We have also
278 // to put it in the header file so that other classes will be able to use the
279 // gesture property objects.
280 struct GesturesProp {
281 public:
282 typedef ui::GesturePropertyProvider::PropertyType PropertyType;
284 GesturesProp(const std::string& name,
285 const PropertyType type,
286 const size_t count);
287 virtual ~GesturesProp() {}
289 // Variant-ish interfaces for accessing the property value. Each type of
290 // property should override the corresponding interfaces for it.
291 virtual std::vector<int> GetIntValue() const;
292 virtual bool SetIntValue(const std::vector<int>& value);
293 virtual std::vector<int16_t> GetShortValue() const;
294 virtual bool SetShortValue(const std::vector<int16_t>& value);
295 virtual std::vector<bool> GetBoolValue() const;
296 virtual bool SetBoolValue(const std::vector<bool>& value);
297 virtual std::string GetStringValue() const;
298 virtual bool SetStringValue(const std::string& value);
299 virtual std::vector<double> GetDoubleValue() const;
300 virtual bool SetDoubleValue(const std::vector<double>& value);
302 // Set property access handlers.
303 void SetHandlers(GesturesPropGetHandler get,
304 GesturesPropSetHandler set,
305 void* data);
307 // Accessors.
308 const std::string& name() const { return name_; }
309 PropertyType type() const { return type_; }
310 size_t count() const { return count_; }
311 virtual bool IsReadOnly() const = 0;
313 protected:
314 // Functions to be called when the property value was accessed.
315 void OnGet() const;
316 void OnSet() const;
318 private:
319 // For logging purpose.
320 friend std::ostream& operator<<(std::ostream& os,
321 const GesturesProp& property);
323 // Interfaces for getting internal pointers and stuff.
324 virtual const char** GetStringWritebackPtr() const;
325 virtual bool IsAllocated() const;
327 // Property name, type and number of elements.
328 std::string name_;
329 PropertyType type_;
330 size_t count_;
332 // Handler function pointers and the data to be passed to them when the
333 // property is accessed.
334 GesturesPropGetHandler get_;
335 GesturesPropSetHandler set_;
336 void* handler_data_;
338 DISALLOW_COPY_AND_ASSIGN(GesturesProp);
341 #endif // UI_EVENTS_OZONE_EVDEV_LIBGESTURES_GLUE_GESTURE_PROPERTY_PROVIDER_H_