#include "ui/events/devices/input_device_observer_win.h"
#include <string>
#include <utility>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/singleton.h"
#include <windows.h>
#define WIN_NOTIFY_OBSERVERS(method_decl, input_device_types) \
void InputDeviceObserverWin::method_decl { \
for (InputDeviceEventObserver & observer : observers_) { \
observer.OnInputDeviceConfigurationChanged( \
InputDeviceEventObserver::input_device_types); \
} \
}
namespace ui {
InputDeviceObserverWin::InputDeviceObserverWin()
: registry_key_(HKEY_LOCAL_MACHINE,
L"System\\CurrentControlSet\\Control\\PriorityControl",
KEY_NOTIFY | KEY_READ) {
if (registry_key_.Valid()) {
slate_mode_enabled_ = IsSlateModeEnabled();
registry_key_.StartWatching(base::BindOnce(
&InputDeviceObserverWin::OnRegistryKeyChanged, base::Unretained(this)));
}
}
InputDeviceObserverWin* InputDeviceObserverWin::GetInstance() {
return base::Singleton<
InputDeviceObserverWin,
base::LeakySingletonTraits<InputDeviceObserverWin>>::get();
}
InputDeviceObserverWin::~InputDeviceObserverWin() {}
void InputDeviceObserverWin::OnRegistryKeyChanged() {
registry_key_.StartWatching(base::BindOnce(
&InputDeviceObserverWin::OnRegistryKeyChanged, base::Unretained(this)));
bool new_slate_mode = IsSlateModeEnabled();
if (slate_mode_enabled_ == new_slate_mode)
return;
NotifyObserversTouchpadDeviceConfigurationChanged();
NotifyObserversKeyboardDeviceConfigurationChanged();
slate_mode_enabled_ = new_slate_mode;
}
bool InputDeviceObserverWin::IsSlateModeEnabled() {
DCHECK(registry_key_.Valid());
DWORD slate_enabled = 0;
return registry_key_.ReadValueDW(L"ConvertibleSlateMode", &slate_enabled) ==
ERROR_SUCCESS &&
slate_enabled == 1;
}
void InputDeviceObserverWin::AddObserver(InputDeviceEventObserver* observer) {
observers_.AddObserver(observer);
}
void InputDeviceObserverWin::RemoveObserver(
InputDeviceEventObserver* observer) {
observers_.RemoveObserver(observer);
}
WIN_NOTIFY_OBSERVERS(NotifyObserversKeyboardDeviceConfigurationChanged(),
kKeyboard)
WIN_NOTIFY_OBSERVERS(NotifyObserversTouchpadDeviceConfigurationChanged(),
kTouchpad)
}