OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #ifndef UI_EVENTS_OZONE_EVDEV_LIBGESTURES_GLUE_GESTURE_PROPERTY_PROVIDER_H_ | |
6 #define UI_EVENTS_OZONE_EVDEV_LIBGESTURES_GLUE_GESTURE_PROPERTY_PROVIDER_H_ | |
7 | |
8 #include <gestures/gestures.h> | |
9 #include <libevdev/libevdev.h> | |
10 | |
11 #include <map> | |
12 #include <string> | |
13 #include <vector> | |
14 | |
15 #include "base/basictypes.h" | |
16 #include "base/containers/hash_tables.h" | |
17 #include "base/containers/scoped_ptr_hash_map.h" | |
18 #include "ui/events/ozone/evdev/events_ozone_evdev_export.h" | |
19 | |
20 template <typename T> struct DefaultSingletonTraits; | |
21 | |
22 namespace ui { | |
23 | |
24 class GesturesPropFunctionsWrapper; | |
25 | |
26 // Provide the interface to access the CrOS gesture library properties. | |
27 // It maintains the property data for all input devices that runs with | |
28 // the gesture library. | |
29 // | |
30 // The class also parses the configuration files on the system to | |
31 // initialize the specified property values. The configuration files are | |
32 // currently in the xorg-conf format so that they can be shared with non-Ozone | |
33 // builds. | |
34 class EVENTS_OZONE_EVDEV_EXPORT GesturePropertyProvider { | |
35 friend class GesturesPropFunctionsWrapper; | |
spang
2014/09/18 18:06:16
Put this under private:
Shecky Lin
2014/09/19 09:31:09
Done.
| |
36 public: | |
37 static GesturePropertyProvider* GetInstance(); | |
38 | |
39 // Device types. | |
40 enum DeviceType { | |
41 DT_MOUSE, | |
42 DT_TOUCHPAD, | |
43 DT_TOUCHSCREEN, | |
44 DT_MULTITOUCH, | |
45 DT_MULTITOUCH_MOUSE, | |
46 DT_ALL, | |
47 }; | |
48 | |
49 // Property types. | |
50 enum PropertyType { | |
51 PT_INT, | |
52 PT_SHORT, | |
53 PT_BOOL, | |
54 PT_STRING, | |
55 PT_REAL, | |
56 }; | |
57 | |
58 // A struct holding device properties that are useful when interacting with | |
59 // the gestures lib. | |
60 struct DeviceProperty { | |
spang
2014/09/18 18:06:15
Name makes me think it's only one property.
Maybe
Shecky Lin
2014/09/19 09:31:09
Done.
| |
61 int area_left; | |
62 int area_right; | |
63 int area_top; | |
64 int area_bottom; | |
65 int res_y; | |
66 int res_x; | |
67 int orientation_minimum; | |
68 int orientation_maximum; | |
69 GesturesPropBool raw_passthrough; | |
70 GesturesPropBool dump_debug_log; | |
71 }; | |
72 | |
73 // Pointer that leads to the device info. | |
74 typedef Evdev* DevicePtr; | |
75 | |
76 // Use void* for more freedom. | |
77 typedef void* PropertyValuePtr; | |
78 | |
79 // The device ids are only maintained by GesturePropertyProvider to identify | |
80 // the input devices that uses the gesture lib and are not to be confused with | |
81 // the Evdev input node id, for example. | |
82 typedef int DeviceId; | |
83 | |
84 // Get a list of device ids that matches a device type. | |
85 void GetDeviceIndices(const DeviceType type, | |
spang
2014/09/18 18:06:15
Maybe "GetDeviceIdsByType"
Shecky Lin
2014/09/19 09:31:09
Done.
| |
86 std::vector<DeviceId>* device_ids); | |
87 | |
88 // Property access functions for other classes in Chrome, e.g., the | |
89 // preferences page. Note that the values in preferences are not synced with | |
90 // the ones here in realtime - they are only applied from the preference | |
91 // side in a single way once appropriate (e.g., when the user clicked "OK"). | |
92 | |
93 // Get the property value. | |
94 void GetProperty(const DeviceId device_id, | |
95 const std::string& name, | |
96 DeviceType* type, | |
97 PropertyValuePtr* val, | |
98 size_t* count); | |
spang
2014/09/18 18:06:15
This interface concerns me.. why have we lost all
Shecky Lin
2014/09/19 09:31:09
I agree. Will do it at the next patch. However, mo
| |
99 | |
100 // Set the property value. | |
101 void SetProperty(const DeviceId device_id, | |
102 const std::string& name, | |
103 const DeviceType type, | |
104 const PropertyValuePtr val, | |
105 const size_t count); | |
106 private: | |
107 // Requirement for Singleton | |
108 friend struct DefaultSingletonTraits<GesturePropertyProvider>; | |
109 | |
110 // Base class for device match criterias in conf files. | |
111 // Check the xorg-conf spec for more detailed information. | |
112 class MatchCriteria { | |
113 public: | |
114 MatchCriteria(const std::string& arg); | |
115 virtual ~MatchCriteria() {}; | |
116 virtual bool Match(DevicePtr device) = 0; | |
117 protected: | |
118 std::vector<std::string> args_; | |
119 }; | |
120 | |
121 // Trick for converting strings to class names. | |
122 template <typename T> | |
123 MatchCriteria* AllocateMatchCriteria(const std::string& arg) { | |
124 return new T(arg); | |
125 } | |
126 typedef std::map<std::string, | |
127 MatchCriteria* (ui::GesturePropertyProvider::*)( | |
128 const std::string&)> MatchCriteriaCreatorMap; | |
129 | |
130 // Match a device based on its evdev name string. | |
131 class MatchProduct : public MatchCriteria { | |
132 public: | |
133 MatchProduct(const std::string& arg); | |
134 virtual ~MatchProduct() {}; | |
135 virtual bool Match(DevicePtr device); | |
136 }; | |
137 | |
138 // Math a device based on its device node path. | |
139 class MatchDevicePath : public MatchCriteria { | |
140 public: | |
141 MatchDevicePath(const std::string& arg); | |
142 virtual ~MatchDevicePath() {}; | |
143 virtual bool Match(DevicePtr device); | |
144 }; | |
145 | |
146 // Math a USB device based on its USB vid and pid. | |
147 // Mostly used for external mice and touchpads. | |
148 class MatchUSBID : public MatchCriteria { | |
149 public: | |
150 MatchUSBID(const std::string& arg); | |
151 virtual ~MatchUSBID() {}; | |
152 virtual bool Match(DevicePtr device); | |
153 private: | |
154 bool IsValidPattern(const std::string &pattern); | |
155 std::vector<std::string> vid_patterns_; | |
156 std::vector<std::string> pid_patterns_; | |
157 }; | |
158 | |
159 // Generic base class for device type math criteria. | |
160 class MatchDeviceType : public MatchCriteria { | |
161 public: | |
162 MatchDeviceType(const std::string& arg); | |
163 virtual ~MatchDeviceType() {}; | |
164 virtual bool Match(DevicePtr device) = 0; | |
165 protected: | |
166 bool value_; | |
167 bool is_valid_; | |
168 }; | |
169 | |
170 // Check if a device is a pointer device. | |
171 class MatchIsPointer : public MatchDeviceType { | |
172 public: | |
173 MatchIsPointer(const std::string& arg); | |
174 virtual ~MatchIsPointer() {}; | |
175 virtual bool Match(DevicePtr device); | |
176 }; | |
177 | |
178 // Check if a device is a touchpad. | |
179 class MatchIsTouchpad : public MatchDeviceType { | |
180 public: | |
181 MatchIsTouchpad(const std::string& arg); | |
182 virtual ~MatchIsTouchpad() {}; | |
183 virtual bool Match(DevicePtr device); | |
184 }; | |
185 | |
186 // Check if a device is a touchscreen. | |
187 class MatchIsTouchscreen : public MatchDeviceType { | |
188 public: | |
189 MatchIsTouchscreen(const std::string& arg); | |
190 virtual ~MatchIsTouchscreen() {}; | |
191 virtual bool Match(DevicePtr device); | |
192 }; | |
193 | |
194 // Struct for sections in xorg conf files. | |
195 struct ConfigurationSection { | |
196 bool Match(DevicePtr device); | |
197 std::string identifier; | |
198 std::vector<MatchCriteria*> criterias; | |
199 std::vector<GesturesProp> properties; | |
200 }; | |
201 | |
202 // Mapping table from a dev pointer to its device id. | |
203 typedef std::map<DevicePtr, DeviceId> DeviceIdMap; | |
204 | |
205 // Mapping table from a property name to its corresponding GesturesProp | |
206 // object pointer. | |
207 typedef base::hash_map<std::string, GesturesProp*> PropertyMap; | |
208 | |
209 // Mapping table from a dev id to its property map. | |
210 typedef base::ScopedPtrHashMap<DeviceId, PropertyMap> DevicePropertyMap; | |
211 | |
212 GesturePropertyProvider(); | |
213 ~GesturePropertyProvider(); | |
214 | |
215 // Populate the match criteria creators in the creator map. | |
216 void RegisterMatchCriterias(); | |
217 | |
218 // Get the device id of a device pointer. May create a new one if not | |
219 // currently mapped. | |
220 DeviceId GetDeviceId(DevicePtr dev, const bool do_create = false); | |
221 DeviceId GetDeviceId(void* dev, const bool do_create = false) { | |
222 return GetDeviceId(static_cast<DevicePtr>(dev), do_create); | |
223 } | |
224 | |
225 // Called by functions in GesturesPropFunctionsWrapper to manipulate | |
226 // properties. Note these functions do not new/delete the GesturesProp | |
227 // pointers. It is caller's responsibility to manage them. | |
228 void AddProperty(const DeviceId device_id, | |
229 const std::string& name, | |
230 GesturesProp* prop); | |
231 void DeleteProperty(const DeviceId device_id, const std::string& name); | |
232 | |
233 // Check if a property exists for a device. Return if it is found. | |
234 GesturesProp* FindProperty(const DeviceId device_id, const std::string& name); | |
235 | |
236 // Core function for property finding routines. | |
237 GesturesProp* FindProperty(const DevicePropertyMap& device_property_map, | |
238 const DeviceId device_id, | |
239 const std::string& name); | |
240 | |
241 // Get the default value of a property based on the configuration files. | |
242 GesturesProp* GetDefaultProperty(const DeviceId device_id, | |
243 const std::string& name); | |
244 | |
245 // The device configuration files are parsed and stored in the memory upon | |
246 // Chrome starts. The default property values are then applied to each device | |
247 // when it is attached/detected. | |
248 void LoadDeviceConfigurations(); | |
249 | |
250 // Parse a xorg-conf file. We ignore all sections other than InputClass. | |
251 // Check the xorg-conf spec for more infomation about its format. | |
252 void ParseXorgConfFile(const std::string& content); | |
253 | |
254 // Check if a match criteria is currently implemented. Note that we didn't | |
255 // implemented all of them as some are inapplicable in the non-X world. | |
256 static bool IsMatchTypeSupported(const std::string& match_type); | |
257 | |
258 // Check if a match criteria is a device type one. | |
259 static bool IsDeviceMatchType(const std::string& match_type); | |
260 | |
261 // Create a match criteria. | |
262 MatchCriteria* CreateMatchCriteria(const std::string& match_type, | |
263 const std::string& arg); | |
264 | |
265 // Create a property that comes from the conf files. | |
266 bool CreateDefaultProperty(const std::string& name, | |
267 const std::string& value, | |
268 GesturesProp* prop); | |
269 | |
270 // Parse a boolean value keyword (e.g., on/off, true/false). | |
271 static int ParseBooleanKeyword(const std::string& value); | |
272 | |
273 // Setup default property values for a newly found device. | |
274 void SetupDefaultProperties(const DeviceId device_id, DevicePtr dev); | |
275 | |
276 // Max number of devices that we track. | |
277 static const DeviceId kMaxDeviceNum = 0xffff; | |
278 | |
279 // A map that stores the match criteria creators. | |
280 MatchCriteriaCreatorMap match_criteria_map_; | |
281 | |
282 // An incremental counter of the next device id to be used for mapping. | |
283 DeviceId device_id_counter_; | |
284 | |
285 // Map of device ids. | |
286 DeviceIdMap device_ids_map_; | |
287 | |
288 // Array of property maps indexed by their respective device ids. | |
289 DevicePropertyMap properties_maps_; | |
290 | |
291 // Same as properties_maps_, but loaded with only values specified in the | |
292 // configuration files. Will be applied when a property of the same name is | |
293 // created. | |
294 DevicePropertyMap default_properties_maps_; | |
295 | |
296 // A vector of parsed sections in configuration files. | |
297 std::vector<ConfigurationSection> configurations_; | |
298 | |
299 DISALLOW_COPY_AND_ASSIGN(GesturePropertyProvider); | |
300 }; | |
301 | |
302 // Wrapper of GesturesProp related functions. We group them together so that we | |
303 // can friend them all at once. | |
304 class GesturesPropFunctionsWrapper { | |
305 public: | |
306 // Property provider interface implementation. | |
307 // | |
308 // These functions will create a GesturesProp object that can link back to the | |
309 // memory that holds the real value, which is often declared in the gesture | |
310 // lib. | |
311 static GesturesProp* CreateInt(void*, const char*, int*, size_t, const int*); | |
312 static GesturesProp* CreateShort(void*, | |
313 const char*, | |
314 short*, | |
315 size_t, | |
316 const short*); | |
317 static GesturesProp* CreateBool(void*, | |
318 const char*, | |
319 GesturesPropBool*, | |
320 size_t, | |
321 const GesturesPropBool*); | |
322 | |
323 // String GestureProps needs special care due to the use of const char* in the | |
324 // gesture lib. Its argument list is also different from numeric properties. | |
325 static GesturesProp* CreateString(void*, | |
326 const char*, | |
327 const char**, | |
328 const char*); | |
329 | |
330 static GesturesProp* CreateReal(void*, | |
331 const char*, | |
332 double*, | |
333 size_t, | |
334 const double*); | |
335 | |
336 // Set the handlers to call when a property is accessed. | |
337 static void RegisterHandlers(void*, | |
338 GesturesProp*, | |
339 void*, | |
340 GesturesPropGetHandler, | |
341 GesturesPropSetHandler); | |
342 | |
343 // Free a property. | |
344 static void Free(void*, GesturesProp*); | |
345 | |
346 // Initialize hardware-related device properties which will be used in the | |
347 // gesture lib. | |
348 static bool InitializeDeviceProperties( | |
349 void*, | |
350 GesturePropertyProvider::DeviceProperty* props); | |
351 | |
352 private: | |
353 // Property helper functions. | |
354 // Core function for creating properties in GesturePropertyProvider. | |
355 template <typename T> | |
356 static GesturesProp* Create(void*, | |
357 const char*, | |
358 GesturePropertyProvider::PropertyType, | |
359 T*, | |
360 size_t); | |
361 | |
362 // Template function for creating numeric GestureProps (e.g., int, short, | |
363 // double). String property doesn't use this. | |
364 template <typename T, GesturePropertyProvider::PropertyType type> | |
365 static GesturesProp* CreateProperty(void*, const char*, T*, size_t, const T*); | |
366 | |
367 static GesturesProp* CreateIntSingle(void*, const char*, int*, int); | |
368 static GesturesProp* CreateBoolSingle(void*, | |
369 const char*, | |
370 GesturesPropBool*, | |
371 GesturesPropBool); | |
372 }; | |
373 | |
374 extern const GesturesPropProvider kGesturePropProvider; | |
375 | |
376 } // namspace ui | |
377 | |
378 #endif // UI_EVENTS_OZONE_EVDEV_LIBGESTURES_GLUE_GESTURE_PROPERTY_PROVIDER_H_ | |
OLD | NEW |