OLD | NEW |
(Empty) | |
| 1 // Copyright 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()); |
| 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 weak_factory_.GetWeakPtr(), |
| 65 base::Unretained(key))); |
| 66 |
| 67 bool new_slate_mode = IsSlateModeEnabled(key); |
| 68 if (slate_mode_enabled_ == new_slate_mode) |
| 69 return; |
| 70 |
| 71 NotifyObserversTouchpadDeviceConfigurationChanged(); |
| 72 NotifyObserversKeyboardDeviceConfigurationChanged(); |
| 73 slate_mode_enabled_ = new_slate_mode; |
| 74 } |
| 75 |
| 76 bool InputDeviceObserverWin::IsSlateModeEnabled(base::win::RegKey* key) { |
| 77 DWORD slate_enabled; |
| 78 if (key->ReadValueDW(kRegistryConvertibleSlateModeKey, &slate_enabled) != |
| 79 ERROR_SUCCESS) |
| 80 return false; |
| 81 return slate_enabled == 1; |
| 82 } |
| 83 |
| 84 void InputDeviceObserverWin::AddObserver(InputDeviceEventObserver* observer) { |
| 85 observers_.AddObserver(observer); |
| 86 } |
| 87 |
| 88 void InputDeviceObserverWin::RemoveObserver( |
| 89 InputDeviceEventObserver* observer) { |
| 90 observers_.RemoveObserver(observer); |
| 91 } |
| 92 |
| 93 NOTIFY_OBSERVERS(NotifyObserversKeyboardDeviceConfigurationChanged(), |
| 94 OnKeyboardDeviceConfigurationChanged()); |
| 95 |
| 96 NOTIFY_OBSERVERS(NotifyObserversTouchpadDeviceConfigurationChanged(), |
| 97 OnTouchpadDeviceConfigurationChanged()); |
| 98 |
| 99 } // namespace ui |
OLD | NEW |