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