OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef DEVICE_HID_INPUT_SERVICE_LINUX_H_ | 5 #ifndef DEVICE_HID_INPUT_SERVICE_LINUX_H_ |
6 #define DEVICE_HID_INPUT_SERVICE_LINUX_H_ | 6 #define DEVICE_HID_INPUT_SERVICE_LINUX_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/containers/hash_tables.h" | 13 #include "base/containers/hash_tables.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/message_loop/message_loop.h" | |
16 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
17 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
18 #include "device/hid/device_monitor_linux.h" | |
19 | 17 |
20 namespace device { | 18 namespace device { |
21 | 19 |
22 // This class provides information and notifications about | 20 // This class provides information and notifications about |
23 // connected/disconnected input/HID devices. This class is *NOT* | 21 // connected/disconnected input/HID devices. This class is *NOT* |
24 // thread-safe and all methods must be called from the FILE thread. | 22 // thread-safe and all methods must be called from the FILE thread. |
25 class InputServiceLinux : public base::MessageLoop::DestructionObserver { | 23 class InputServiceLinux { |
26 public: | 24 public: |
27 struct InputDeviceInfo { | 25 struct InputDeviceInfo { |
28 enum Subsystem { SUBSYSTEM_HID, SUBSYSTEM_INPUT, SUBSYSTEM_UNKNOWN }; | 26 enum Subsystem { SUBSYSTEM_HID, SUBSYSTEM_INPUT, SUBSYSTEM_UNKNOWN }; |
29 enum Type { TYPE_BLUETOOTH, TYPE_USB, TYPE_SERIO, TYPE_UNKNOWN }; | 27 enum Type { TYPE_BLUETOOTH, TYPE_USB, TYPE_SERIO, TYPE_UNKNOWN }; |
30 | 28 |
31 InputDeviceInfo(); | 29 InputDeviceInfo(); |
32 InputDeviceInfo(const InputDeviceInfo& other); | 30 InputDeviceInfo(const InputDeviceInfo& other); |
33 | 31 |
34 std::string id; | 32 std::string id; |
35 std::string name; | 33 std::string name; |
36 Subsystem subsystem; | 34 Subsystem subsystem; |
37 Type type; | 35 Type type; |
38 | 36 |
39 bool is_accelerometer : 1; | 37 bool is_accelerometer : 1; |
40 bool is_joystick : 1; | 38 bool is_joystick : 1; |
41 bool is_key : 1; | 39 bool is_key : 1; |
42 bool is_keyboard : 1; | 40 bool is_keyboard : 1; |
43 bool is_mouse : 1; | 41 bool is_mouse : 1; |
44 bool is_tablet : 1; | 42 bool is_tablet : 1; |
45 bool is_touchpad : 1; | 43 bool is_touchpad : 1; |
46 bool is_touchscreen : 1; | 44 bool is_touchscreen : 1; |
47 }; | 45 }; |
48 | 46 |
49 | |
50 using DeviceMap = base::hash_map<std::string, InputDeviceInfo>; | 47 using DeviceMap = base::hash_map<std::string, InputDeviceInfo>; |
51 | 48 |
52 class Observer { | 49 class Observer { |
53 public: | 50 public: |
54 virtual ~Observer() {} | 51 virtual ~Observer() {} |
55 virtual void OnInputDeviceAdded(const InputDeviceInfo& info) = 0; | 52 virtual void OnInputDeviceAdded(const InputDeviceInfo& info) = 0; |
56 virtual void OnInputDeviceRemoved(const std::string& id) = 0; | 53 virtual void OnInputDeviceRemoved(const std::string& id) = 0; |
57 }; | 54 }; |
58 | 55 |
59 InputServiceLinux(); | 56 InputServiceLinux(); |
60 ~InputServiceLinux() override; | 57 virtual ~InputServiceLinux(); |
61 | 58 |
62 static InputServiceLinux* GetInstance(); | 59 static InputServiceLinux* GetInstance(); |
63 static bool HasInstance(); | 60 static bool HasInstance(); |
64 static void SetForTesting(InputServiceLinux* service); | 61 static void SetForTesting(InputServiceLinux* service); |
65 | 62 |
66 void AddObserver(Observer* observer); | 63 void AddObserver(Observer* observer); |
67 void RemoveObserver(Observer* observer); | 64 void RemoveObserver(Observer* observer); |
68 | 65 |
69 // Returns list of all currently connected input/hid devices. | 66 // Returns list of all currently connected input/hid devices. |
70 virtual void GetDevices(std::vector<InputDeviceInfo>* devices); | 67 virtual void GetDevices(std::vector<InputDeviceInfo>* devices); |
71 | 68 |
72 // Returns an info about input device identified by |id|. When there're | 69 // Returns an info about input device identified by |id|. When there're |
73 // no input or hid device with such id, returns false and doesn't | 70 // no input or hid device with such id, returns false and doesn't |
74 // modify |info|. | 71 // modify |info|. |
75 bool GetDeviceInfo(const std::string& id, InputDeviceInfo* info) const; | 72 bool GetDeviceInfo(const std::string& id, InputDeviceInfo* info) const; |
76 | 73 |
77 // Implements base::MessageLoop::DestructionObserver | |
78 void WillDestroyCurrentMessageLoop() override; | |
79 | |
80 protected: | 74 protected: |
81 | |
82 void AddDevice(const InputDeviceInfo& info); | 75 void AddDevice(const InputDeviceInfo& info); |
83 void RemoveDevice(const std::string& id); | 76 void RemoveDevice(const std::string& id); |
84 | 77 |
85 bool CalledOnValidThread() const; | 78 bool CalledOnValidThread() const; |
86 | 79 |
87 DeviceMap devices_; | 80 DeviceMap devices_; |
88 base::ObserverList<Observer> observers_; | 81 base::ObserverList<Observer> observers_; |
89 | 82 |
90 private: | 83 private: |
91 friend std::default_delete<InputServiceLinux>; | 84 friend std::default_delete<InputServiceLinux>; |
92 | 85 |
93 base::ThreadChecker thread_checker_; | 86 base::ThreadChecker thread_checker_; |
94 | 87 |
95 DISALLOW_COPY_AND_ASSIGN(InputServiceLinux); | 88 DISALLOW_COPY_AND_ASSIGN(InputServiceLinux); |
96 }; | 89 }; |
97 | 90 |
98 } // namespace device | 91 } // namespace device |
99 | 92 |
100 #endif // DEVICE_HID_INPUT_SERVICE_LINUX_H_ | 93 #endif // DEVICE_HID_INPUT_SERVICE_LINUX_H_ |
OLD | NEW |