Landing Recent QUIC Changes.
[chromium-blink-merge.git] / ui / events / ozone / evdev / libgestures_glue / gesture_property_provider.h
blob1b21e4180e1b2eac36e5e96ede65a01bcb38db2c
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<DeviceId, internal::GestureDevicePropertyData>
116 ScopedDeviceDataMap;
118 // Register a device. Setup data-structures and the device's default
119 // properties.
120 void RegisterDevice(const DeviceId id, const DevicePtr device);
122 // Unregister a device. Remove all of its properties being tracked.
123 void UnregisterDevice(const DeviceId id);
125 // Called by functions in GesturesPropFunctionsWrapper to manipulate
126 // properties. Note these functions do not new/delete the GesturesProp
127 // pointers. It is caller's responsibility to manage them.
128 void AddProperty(const DeviceId device_id,
129 const std::string& name,
130 GesturesProp* property);
131 void DeleteProperty(const DeviceId device_id, const std::string& name);
133 // Check if a property exists for a device. Return if it is found.
134 GesturesProp* FindProperty(const DeviceId device_id, const std::string& name);
136 // Get the default value of a property based on the configuration files.
137 GesturesProp* GetDefaultProperty(const DeviceId device_id,
138 const std::string& name);
140 // The device configuration files are parsed and stored in the memory upon
141 // Chrome starts. The default property values are then applied to each device
142 // when it is attached/detected.
143 void LoadDeviceConfigurations();
145 // Parse a xorg-conf file. We ignore all sections other than InputClass.
146 // Check the xorg-conf spec for more infomation about its format.
147 void ParseXorgConfFile(const std::string& content);
149 // Create a match criteria.
150 internal::MatchCriteria* CreateMatchCriteria(const std::string& match_type,
151 const std::string& arg);
153 // Create a property that comes from the conf files.
154 GesturesProp* CreateDefaultProperty(const std::string& name,
155 const std::string& value);
157 // Setup default property values for a newly found device.
158 void SetupDefaultProperties(const DeviceId device_id, const DevicePtr device);
160 // Map from device ids to device pointers.
161 DeviceMap device_map_;
163 // GestureDevicePropertyData indexed by their respective device ids. Owns the
164 // objects.
165 ScopedDeviceDataMap device_data_map_;
167 // A vector of parsed sections in configuration files. Owns MatchCriterias,
168 // GesturesProps and ConfigurationSections in it.
169 ScopedVector<internal::ConfigurationSection> configurations_;
171 DISALLOW_COPY_AND_ASSIGN(GesturePropertyProvider);
174 // Wrapper of GesturesProp related functions. We group them together so that we
175 // can friend them all at once.
176 class GesturesPropFunctionsWrapper {
177 public:
178 // Property provider interface implementation.
180 // These functions will create a GesturesProp object that can link back to the
181 // memory that holds the real value, which is often declared in the gesture
182 // lib.
183 static GesturesProp* CreateInt(void* device_data,
184 const char* name,
185 int* value,
186 size_t count,
187 const int* init);
188 static GesturesProp* CreateShort(void* device_data,
189 const char* name,
190 short* value,
191 size_t count,
192 const short* init);
193 static GesturesProp* CreateBool(void* device_data,
194 const char* name,
195 GesturesPropBool* value,
196 size_t count,
197 const GesturesPropBool* init);
199 // String GestureProps needs special care due to the use of const char* in the
200 // gesture lib. Its argument list is also different from numeric properties'.
201 static GesturesProp* CreateString(void* device_data,
202 const char* name,
203 const char** value,
204 const char* init);
206 static GesturesProp* CreateReal(void* device_data,
207 const char* name,
208 double* value,
209 size_t count,
210 const double* init);
212 // Set the handlers to call when a property is accessed.
213 static void RegisterHandlers(void* device_data,
214 GesturesProp* property,
215 void* handler_data,
216 GesturesPropGetHandler get,
217 GesturesPropSetHandler set);
219 // Free a property.
220 static void Free(void* device_data, GesturesProp* property);
222 // Initialize hardware-related device properties which will be used in the
223 // gesture lib.
224 static bool InitializeDeviceProperties(void* device_data,
225 GestureDeviceProperties* properties);
227 // Unregister device from the gesture property provider.
228 static void UnregisterDevice(void* device_data);
230 private:
231 // Property helper functions.
232 // Core template function for creating GestureProps. Used by numerical types.
233 template <typename T, class PROPTYPE>
234 static GesturesProp* CreateProperty(void* device_data,
235 const char* name,
236 T* value,
237 size_t count,
238 const T* init);
240 // Do things that should happen BEFORE we create the property.
241 static bool PreCreateProperty(void* device_data,
242 const char* name,
243 GesturesProp** default_property);
245 // Do things that should happen AFTER we create the property.
246 static void PostCreateProperty(void* device_data,
247 const char* name,
248 GesturesProp* property);
250 // Some other utility functions used in InitializeDeviceProperties.
251 static GesturesProp* CreateIntSingle(void* device_data,
252 const char* name,
253 int* value,
254 int init);
255 static GesturesProp* CreateBoolSingle(void* device_data,
256 const char* name,
257 GesturesPropBool* value,
258 GesturesPropBool init);
260 // Routines to extract information from the GestureInterpreterLibevdevCros
261 // pointer.
262 static GesturePropertyProvider* GetPropertyProvider(void* device_data);
263 static GesturePropertyProvider::DevicePtr GetDevicePointer(void* device_data);
264 static GesturePropertyProvider::DeviceId GetDeviceId(void* device_data);
267 extern const GesturesPropProvider kGesturePropProvider;
269 } // namspace ui
271 // GesturesProp logging function.
272 std::ostream& operator<<(std::ostream& os, const GesturesProp& prop);
274 // Implementation of GesturesProp declared in gestures.h
276 // libgestures requires that this be in the top level namespace. We have also
277 // to put it in the header file so that other classes will be able to use the
278 // gesture property objects.
279 struct GesturesProp {
280 public:
281 typedef ui::GesturePropertyProvider::PropertyType PropertyType;
283 GesturesProp(const std::string& name,
284 const PropertyType type,
285 const size_t count);
286 virtual ~GesturesProp() {}
288 // Variant-ish interfaces for accessing the property value. Each type of
289 // property should override the corresponding interfaces for it.
290 virtual std::vector<int> GetIntValue() const;
291 virtual bool SetIntValue(const std::vector<int>& value);
292 virtual std::vector<int16_t> GetShortValue() const;
293 virtual bool SetShortValue(const std::vector<int16_t>& value);
294 virtual std::vector<bool> GetBoolValue() const;
295 virtual bool SetBoolValue(const std::vector<bool>& value);
296 virtual std::string GetStringValue() const;
297 virtual bool SetStringValue(const std::string& value);
298 virtual std::vector<double> GetDoubleValue() const;
299 virtual bool SetDoubleValue(const std::vector<double>& value);
301 // Set property access handlers.
302 void SetHandlers(GesturesPropGetHandler get,
303 GesturesPropSetHandler set,
304 void* data);
306 // Accessors.
307 const std::string& name() const { return name_; }
308 PropertyType type() const { return type_; }
309 size_t count() const { return count_; }
310 virtual bool IsReadOnly() const = 0;
312 protected:
313 // Functions to be called when the property value was accessed.
314 void OnGet() const;
315 void OnSet() const;
317 private:
318 // For logging purpose.
319 friend std::ostream& operator<<(std::ostream& os,
320 const GesturesProp& property);
322 // Interfaces for getting internal pointers and stuff.
323 virtual const char** GetStringWritebackPtr() const;
324 virtual bool IsAllocated() const;
326 // Property name, type and number of elements.
327 std::string name_;
328 PropertyType type_;
329 size_t count_;
331 // Handler function pointers and the data to be passed to them when the
332 // property is accessed.
333 GesturesPropGetHandler get_;
334 GesturesPropSetHandler set_;
335 void* handler_data_;
337 DISALLOW_COPY_AND_ASSIGN(GesturesProp);
340 #endif // UI_EVENTS_OZONE_EVDEV_LIBGESTURES_GLUE_GESTURE_PROPERTY_PROVIDER_H_