OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2017 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 #include "ui/events/devices/input_device_observer_win.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/callback.h" | |
9 #include "base/memory/singleton.h" | |
10 #include "base/strings/string16.h" | |
11 | |
12 // This macro provides the implementation for the observer notification methods. | |
13 #define NOTIFY_OBSERVERS(method_decl, observer_call) \ | |
14 void InputDeviceObserverWin::method_decl { \ | |
15 for (InputDeviceEventObserver & observer : observers_) \ | |
16 observer.observer_call; \ | |
17 } | |
18 | |
19 namespace ui { | |
20 | |
21 namespace { | |
22 | |
23 // The registry subkey that contains information about the state of the | |
24 // detachable/convertible laptop, it tells if the device has an accessible | |
25 // keyboard. | |
26 // OEMs are expected to follow this guidelines to report docked/undocked state | |
27 // https://msdn.microsoft.com/en-us/windows/hardware/commercialize/customize/des ktop/unattend/microsoft-windows-gpiobuttons-convertibleslatemode | |
28 const base::char16 kRegistryPriorityControl[] = | |
29 L"System\\CurrentControlSet\\Control\\PriorityControl"; | |
30 | |
31 const base::char16 kRegistryConvertibleSlateModeKey[] = L"ConvertibleSlateMode"; | |
32 | |
33 } // namespace | |
34 | |
35 InputDeviceObserverWin::InputDeviceObserverWin() : weak_factory_(this) { | |
36 registry_key_.reset(new base::win::RegKey( | |
37 HKEY_LOCAL_MACHINE, kRegistryPriorityControl, KEY_NOTIFY | KEY_READ)); | |
38 | |
39 if (registry_key_->Valid()) { | |
40 slate_mode_enabled_ = IsSlateModeEnabled(registry_key_.get()); | |
41 // Start watching the registry for changes. | |
42 base::win::RegKey::ChangeCallback callback = | |
43 base::Bind(&InputDeviceObserverWin::OnRegistryKeyChanged, | |
44 weak_factory_.GetWeakPtr(), registry_key_.get()); | |
ananta
2017/04/08 00:54:47
Doesn't WM_SETTINGCHANGE fire when this happens?.
| |
45 registry_key_->StartWatching(callback); | |
46 } | |
47 } | |
48 | |
49 InputDeviceObserverWin* InputDeviceObserverWin::GetInstance() { | |
50 return base::Singleton< | |
51 InputDeviceObserverWin, | |
52 base::LeakySingletonTraits<InputDeviceObserverWin>>::get(); | |
53 } | |
54 | |
55 InputDeviceObserverWin::~InputDeviceObserverWin() {} | |
56 | |
57 void InputDeviceObserverWin::OnRegistryKeyChanged(base::win::RegKey* key) { | |
58 if (!key) | |
59 return; | |
60 | |
61 // |OnRegistryKeyChanged| is removed as an observer when the ChangeCallback is | |
62 // called, so we need to re-register. | |
63 key->StartWatching(base::Bind(&InputDeviceObserverWin::OnRegistryKeyChanged, | |
64 base::Unretained(this), base::Unretained(key))); | |
ananta
2017/04/08 00:54:47
Shouldn't we use weakptrs here?
| |
65 | |
66 bool new_slate_mode = IsSlateModeEnabled(key); | |
67 if (slate_mode_enabled_ == new_slate_mode) | |
68 return; | |
69 | |
70 NotifyObserversTouchpadDeviceConfigurationChanged(); | |
71 NotifyObserversKeyboardDeviceConfigurationChanged(); | |
72 slate_mode_enabled_ = new_slate_mode; | |
73 } | |
74 | |
75 bool InputDeviceObserverWin::IsSlateModeEnabled(base::win::RegKey* key) { | |
76 DWORD slate_enabled; | |
77 if (key->ReadValueDW(kRegistryConvertibleSlateModeKey, &slate_enabled) != | |
78 ERROR_SUCCESS) | |
79 return false; | |
80 return slate_enabled == 1; | |
81 } | |
82 | |
83 void InputDeviceObserverWin::AddObserver(InputDeviceEventObserver* observer) { | |
84 observers_.AddObserver(observer); | |
85 } | |
86 | |
87 void InputDeviceObserverWin::RemoveObserver( | |
88 InputDeviceEventObserver* observer) { | |
89 observers_.RemoveObserver(observer); | |
90 } | |
91 | |
92 NOTIFY_OBSERVERS(NotifyObserversKeyboardDeviceConfigurationChanged(), | |
93 OnKeyboardDeviceConfigurationChanged()); | |
94 | |
95 NOTIFY_OBSERVERS(NotifyObserversTouchpadDeviceConfigurationChanged(), | |
96 OnTouchpadDeviceConfigurationChanged()); | |
97 | |
98 } // namespace ui | |
OLD | NEW |