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_INPUT_DEVICE_FACTORY_EVDEV_H_
6 #define UI_EVENTS_OZONE_EVDEV_INPUT_DEVICE_FACTORY_EVDEV_H_
7 
8 #include <map>
9 #include <memory>
10 #include <set>
11 #include <vector>
12 
13 #include "base/callback.h"
14 #include "base/compiler_specific.h"
15 #include "base/component_export.h"
16 #include "base/files/file_path.h"
17 #include "base/macros.h"
18 #include "base/memory/ref_counted.h"
19 #include "base/memory/weak_ptr.h"
20 #include "base/sequenced_task_runner.h"
21 #include "mojo/public/cpp/bindings/pending_receiver.h"
22 #include "ui/events/ozone/evdev/event_converter_evdev.h"
23 #include "ui/events/ozone/evdev/event_device_info.h"
24 #include "ui/events/ozone/evdev/input_device_settings_evdev.h"
25 #include "ui/events/ozone/evdev/touch_filter/shared_palm_detection_filter_state.h"
26 #include "ui/ozone/public/input_controller.h"
27 
28 #if defined(USE_EVDEV_GESTURES)
29 #include "ui/events/ozone/evdev/libgestures_glue/gesture_properties_service.h"
30 #endif
31 
32 namespace ui {
33 
34 class CursorDelegateEvdev;
35 class DeviceEventDispatcherEvdev;
36 
37 #if !defined(USE_EVDEV)
38 #error Missing dependency on ui/events/ozone/evdev
39 #endif
40 
41 #if defined(USE_EVDEV_GESTURES)
42 class GesturePropertyProvider;
43 #endif
44 
45 // Manager for event device objects. All device I/O starts here.
COMPONENT_EXPORT(EVDEV)46 class COMPONENT_EXPORT(EVDEV) InputDeviceFactoryEvdev {
47  public:
48   InputDeviceFactoryEvdev(
49       std::unique_ptr<DeviceEventDispatcherEvdev> dispatcher,
50       CursorDelegateEvdev* cursor);
51   ~InputDeviceFactoryEvdev();
52 
53   // Open & start reading a newly plugged-in input device.
54   void AddInputDevice(int id, const base::FilePath& path);
55 
56   // Stop reading & close an unplugged input device.
57   void RemoveInputDevice(const base::FilePath& path);
58 
59   // Device thread handler for initial scan completion.
60   void OnStartupScanComplete();
61 
62   // LED state.
63   void SetCapsLockLed(bool enabled);
64 
65   // Handle gamepad force feedback effects.
66   void PlayVibrationEffect(int id, uint8_t amplitude, uint16_t duration_millis);
67   void StopVibration(int id);
68 
69   // Bits from InputController that have to be answered on IO.
70   void UpdateInputDeviceSettings(const InputDeviceSettingsEvdev& settings);
71   void GetTouchDeviceStatus(InputController::GetTouchDeviceStatusReply reply);
72   void GetTouchEventLog(const base::FilePath& out_dir,
73                         InputController::GetTouchEventLogReply reply);
74 
75   void GetGesturePropertiesService(
76       mojo::PendingReceiver<ozone::mojom::GesturePropertiesService> receiver);
77 
78   base::WeakPtr<InputDeviceFactoryEvdev> GetWeakPtr();
79 
80  private:
81   // Open device at path & starting processing events.
82   void AttachInputDevice(std::unique_ptr<EventConverterEvdev> converter);
83 
84   // Close device at path.
85   void DetachInputDevice(const base::FilePath& file_path);
86 
87   // Sync input_device_settings_ to attached devices.
88   void ApplyInputDeviceSettings();
89   void ApplyRelativePointingDeviceSettings(EventDeviceType type);
90   void ApplyCapsLockLed();
91 
92   // Policy for device enablement.
93   bool IsDeviceEnabled(const EventConverterEvdev* converter);
94 
95   // Update observers on device changes.
96   void UpdateDirtyFlags(const EventConverterEvdev* converter);
97   void NotifyDevicesUpdated();
98   void NotifyKeyboardsUpdated();
99   void NotifyTouchscreensUpdated();
100   void NotifyMouseDevicesUpdated();
101   void NotifyTouchpadDevicesUpdated();
102   void NotifyGamepadDevicesUpdated();
103   void NotifyUncategorizedDevicesUpdated();
104 
105   void SetIntPropertyForOneType(const EventDeviceType type,
106                                 const std::string& name,
107                                 int value);
108   void SetBoolPropertyForOneType(const EventDeviceType type,
109                                  const std::string& name,
110                                  bool value);
111   void EnablePalmSuppression(bool enabled);
112   void EnableDevices();
113 
114   // Task runner for our thread.
115   const scoped_refptr<base::SequencedTaskRunner> task_runner_;
116 
117   // Cursor movement.
118   CursorDelegateEvdev* const cursor_;
119 
120   // Shared Palm state.
121   const std::unique_ptr<SharedPalmDetectionFilterState> shared_palm_state_;
122 
123 #if defined(USE_EVDEV_GESTURES)
124   // Gesture library property provider (used by touchpads/mice).
125   std::unique_ptr<GesturePropertyProvider> gesture_property_provider_;
126   std::unique_ptr<GesturePropertiesService> gesture_properties_service_;
127 #endif
128 
129   // Dispatcher for events.
130   const std::unique_ptr<DeviceEventDispatcherEvdev> dispatcher_;
131 
132   // Number of pending device additions & device classes.
133   int pending_device_changes_ = 0;
134   bool touchscreen_list_dirty_ = true;
135   bool keyboard_list_dirty_ = true;
136   bool mouse_list_dirty_ = true;
137   bool touchpad_list_dirty_ = true;
138   bool gamepad_list_dirty_ = true;
139   bool uncategorized_list_dirty_ = true;
140 
141   // Whether we have a list of devices that were present at startup.
142   bool startup_devices_enumerated_ = false;
143 
144   // Whether devices that were present at startup are open.
145   bool startup_devices_opened_ = false;
146 
147   // LEDs.
148   bool caps_lock_led_enabled_ = false;
149 
150   // Whether touch palm suppression is enabled.
151   bool palm_suppression_enabled_ = false;
152 
153   // Device settings. These primarily affect libgestures behavior.
154   InputDeviceSettingsEvdev input_device_settings_;
155 
156   // Owned per-device event converters (by path).
157   // NB: This should be destroyed early, before any shared state.
158   std::map<base::FilePath, std::unique_ptr<EventConverterEvdev>> converters_;
159 
160   // Support weak pointers for attach & detach callbacks.
161   base::WeakPtrFactory<InputDeviceFactoryEvdev> weak_ptr_factory_{this};
162 
163   DISALLOW_COPY_AND_ASSIGN(InputDeviceFactoryEvdev);
164 };
165 
166 }  // namespace ui
167 
168 #endif  // UI_EVENTS_OZONE_EVDEV_INPUT_DEVICE_FACTORY_EVDEV_H_
169