Fix compilation error from crrev.com/33c6f11de20
[chromium-blink-merge.git] / ash / virtual_keyboard_controller.cc
blob188a10f1e03657e2509f3285e8a366cd8601769b
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.
5 #include "ash/virtual_keyboard_controller.h"
7 #include <vector>
9 #include "ash/shell.h"
10 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
11 #include "base/command_line.h"
12 #include "base/strings/string_util.h"
13 #include "ui/events/devices/device_data_manager.h"
14 #include "ui/events/devices/input_device.h"
15 #include "ui/events/devices/keyboard_device.h"
16 #include "ui/events/devices/touchscreen_device.h"
17 #include "ui/gfx/x/x11_types.h"
18 #include "ui/keyboard/keyboard_switches.h"
19 #include "ui/keyboard/keyboard_util.h"
21 namespace ash {
22 namespace {
24 // Checks whether smart deployment is enabled.
25 bool IsSmartVirtualKeyboardEnabled() {
26 return !base::CommandLine::ForCurrentProcess()->HasSwitch(
27 keyboard::switches::kDisableSmartVirtualKeyboard);
30 } // namespace
32 VirtualKeyboardController::VirtualKeyboardController()
33 : has_external_keyboard_(false),
34 has_internal_keyboard_(false),
35 has_touchscreen_(false),
36 ignore_external_keyboard_(false) {
37 Shell::GetInstance()->AddShellObserver(this);
38 ui::DeviceDataManager::GetInstance()->AddObserver(this);
39 UpdateDevices();
42 VirtualKeyboardController::~VirtualKeyboardController() {
43 Shell::GetInstance()->RemoveShellObserver(this);
44 ui::DeviceDataManager::GetInstance()->RemoveObserver(this);
47 void VirtualKeyboardController::OnMaximizeModeStarted() {
48 if (!IsSmartVirtualKeyboardEnabled())
49 SetKeyboardEnabled(true);
52 void VirtualKeyboardController::OnMaximizeModeEnded() {
53 if (!IsSmartVirtualKeyboardEnabled())
54 SetKeyboardEnabled(false);
57 void VirtualKeyboardController::OnTouchscreenDeviceConfigurationChanged() {
58 UpdateDevices();
61 void VirtualKeyboardController::OnKeyboardDeviceConfigurationChanged() {
62 UpdateDevices();
65 void VirtualKeyboardController::ToggleIgnoreExternalKeyboard() {
66 ignore_external_keyboard_ = !ignore_external_keyboard_;
67 UpdateKeyboardEnabled();
70 void VirtualKeyboardController::UpdateDevices() {
71 ui::DeviceDataManager* device_data_manager =
72 ui::DeviceDataManager::GetInstance();
74 // Checks for touchscreens.
75 has_touchscreen_ = device_data_manager->touchscreen_devices().size() > 0;
77 // Checks for keyboards.
78 has_external_keyboard_ = false;
79 has_internal_keyboard_ = false;
80 for (const ui::KeyboardDevice& device :
81 device_data_manager->keyboard_devices()) {
82 if (has_internal_keyboard_ && has_external_keyboard_)
83 break;
84 ui::InputDeviceType type = device.type;
85 if (type == ui::InputDeviceType::INPUT_DEVICE_INTERNAL)
86 has_internal_keyboard_ = true;
87 if (type == ui::InputDeviceType::INPUT_DEVICE_EXTERNAL)
88 has_external_keyboard_ = true;
90 // Update keyboard state.
91 UpdateKeyboardEnabled();
94 void VirtualKeyboardController::UpdateKeyboardEnabled() {
95 if (!IsSmartVirtualKeyboardEnabled()) {
96 SetKeyboardEnabled(Shell::GetInstance()
97 ->maximize_mode_controller()
98 ->IsMaximizeModeWindowManagerEnabled());
99 return;
101 SetKeyboardEnabled(!has_internal_keyboard_ && has_touchscreen_ &&
102 (!has_external_keyboard_ || ignore_external_keyboard_));
103 ash::Shell::GetInstance()
104 ->system_tray_notifier()
105 ->NotifyVirtualKeyboardSuppressionChanged(!has_internal_keyboard_ &&
106 has_touchscreen_ &&
107 has_external_keyboard_);
110 void VirtualKeyboardController::SetKeyboardEnabled(bool enabled) {
111 keyboard::SetTouchKeyboardEnabled(enabled);
112 if (enabled) {
113 Shell::GetInstance()->CreateKeyboard();
114 } else {
115 if (!keyboard::IsKeyboardEnabled())
116 Shell::GetInstance()->DeactivateKeyboard();
120 } // namespace ash