bshe | eaae09a | 2014-09-22 23:16:52 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "ash/virtual_keyboard_controller.h" |
| 6 | |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
yhanada | 7e20c9b | 2016-11-25 00:03:19 | [diff] [blame^] | 9 | #include "ash/common/keyboard/keyboard_ui.h" |
msw | 5c804fb2 | 2016-06-25 00:09:15 | [diff] [blame] | 10 | #include "ash/common/system/tray/system_tray_notifier.h" |
sky | e2bde217 | 2016-07-01 18:27:02 | [diff] [blame] | 11 | #include "ash/common/wm/maximize_mode/maximize_mode_controller.h" |
sky | 88bd4be6 | 2016-06-09 17:34:41 | [diff] [blame] | 12 | #include "ash/common/wm_shell.h" |
yhanada | 7e20c9b | 2016-11-25 00:03:19 | [diff] [blame^] | 13 | #include "ash/common/wm_window.h" |
| 14 | #include "ash/root_window_controller.h" |
bshe | eaae09a | 2014-09-22 23:16:52 | [diff] [blame] | 15 | #include "ash/shell.h" |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 16 | #include "base/command_line.h" |
| 17 | #include "base/strings/string_util.h" |
yhanada | 7e20c9b | 2016-11-25 00:03:19 | [diff] [blame^] | 18 | #include "ui/display/display.h" |
| 19 | #include "ui/display/screen.h" |
dnicoara | 78a73470 | 2014-11-04 19:54:38 | [diff] [blame] | 20 | #include "ui/events/devices/input_device.h" |
kylechar | e223f03 | 2016-06-03 15:02:53 | [diff] [blame] | 21 | #include "ui/events/devices/input_device_manager.h" |
dnicoara | 78a73470 | 2014-11-04 19:54:38 | [diff] [blame] | 22 | #include "ui/events/devices/touchscreen_device.h" |
yhanada | 7e20c9b | 2016-11-25 00:03:19 | [diff] [blame^] | 23 | #include "ui/keyboard/keyboard_controller.h" |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 24 | #include "ui/keyboard/keyboard_switches.h" |
bshe | eaae09a | 2014-09-22 23:16:52 | [diff] [blame] | 25 | #include "ui/keyboard/keyboard_util.h" |
| 26 | |
| 27 | namespace ash { |
rsadam | 47e23d2 | 2014-12-15 18:11:37 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | // Checks whether smart deployment is enabled. |
| 31 | bool IsSmartVirtualKeyboardEnabled() { |
rsadam | f478ba76 | 2015-01-16 01:08:11 | [diff] [blame] | 32 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 33 | keyboard::switches::kEnableVirtualKeyboard)) { |
| 34 | return false; |
| 35 | } |
rsadam | 318530d | 2015-06-05 15:57:03 | [diff] [blame] | 36 | return keyboard::IsSmartDeployEnabled(); |
rsadam | 47e23d2 | 2014-12-15 18:11:37 | [diff] [blame] | 37 | } |
| 38 | |
yhanada | 7e20c9b | 2016-11-25 00:03:19 | [diff] [blame^] | 39 | void MoveKeyboardToDisplayInternal(const int64_t display_id) { |
| 40 | // Remove the keyboard from curent root window controller |
| 41 | WmShell::Get()->keyboard_ui()->Hide(); |
| 42 | RootWindowController::ForWindow( |
| 43 | keyboard::KeyboardController::GetInstance()->GetContainerWindow()) |
| 44 | ->DeactivateKeyboard(keyboard::KeyboardController::GetInstance()); |
| 45 | |
| 46 | for (RootWindowController* controller : |
| 47 | Shell::GetInstance()->GetAllRootWindowControllers()) { |
| 48 | if (display::Screen::GetScreen() |
| 49 | ->GetDisplayNearestWindow(controller->GetRootWindow()) |
| 50 | .id() == display_id) { |
| 51 | controller->ActivateKeyboard(keyboard::KeyboardController::GetInstance()); |
| 52 | break; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void MoveKeyboardToFirstTouchableDisplay() { |
| 58 | // Move the keyboard to the first display with touch capability. |
| 59 | for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) { |
| 60 | if (display.touch_support() == |
| 61 | display::Display::TouchSupport::TOUCH_SUPPORT_AVAILABLE) { |
| 62 | MoveKeyboardToDisplayInternal(display.id()); |
| 63 | return; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
rsadam | 47e23d2 | 2014-12-15 18:11:37 | [diff] [blame] | 68 | } // namespace |
bshe | eaae09a | 2014-09-22 23:16:52 | [diff] [blame] | 69 | |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 70 | VirtualKeyboardController::VirtualKeyboardController() |
| 71 | : has_external_keyboard_(false), |
| 72 | has_internal_keyboard_(false), |
rsadam | c727b6a | 2014-10-31 18:59:33 | [diff] [blame] | 73 | has_touchscreen_(false), |
| 74 | ignore_external_keyboard_(false) { |
sky | 88bd4be6 | 2016-06-09 17:34:41 | [diff] [blame] | 75 | WmShell::Get()->AddShellObserver(this); |
kylechar | e223f03 | 2016-06-03 15:02:53 | [diff] [blame] | 76 | ui::InputDeviceManager::GetInstance()->AddObserver(this); |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 77 | UpdateDevices(); |
bshe | eaae09a | 2014-09-22 23:16:52 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | VirtualKeyboardController::~VirtualKeyboardController() { |
sky | 88bd4be6 | 2016-06-09 17:34:41 | [diff] [blame] | 81 | WmShell::Get()->RemoveShellObserver(this); |
kylechar | e223f03 | 2016-06-03 15:02:53 | [diff] [blame] | 82 | ui::InputDeviceManager::GetInstance()->RemoveObserver(this); |
bshe | eaae09a | 2014-09-22 23:16:52 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void VirtualKeyboardController::OnMaximizeModeStarted() { |
rsadam | d4f6a0b | 2015-03-02 23:50:08 | [diff] [blame] | 86 | if (!IsSmartVirtualKeyboardEnabled()) { |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 87 | SetKeyboardEnabled(true); |
rsadam | d4f6a0b | 2015-03-02 23:50:08 | [diff] [blame] | 88 | } else { |
| 89 | UpdateKeyboardEnabled(); |
| 90 | } |
bshe | eaae09a | 2014-09-22 23:16:52 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void VirtualKeyboardController::OnMaximizeModeEnded() { |
rsadam | d4f6a0b | 2015-03-02 23:50:08 | [diff] [blame] | 94 | if (!IsSmartVirtualKeyboardEnabled()) { |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 95 | SetKeyboardEnabled(false); |
rsadam | d4f6a0b | 2015-03-02 23:50:08 | [diff] [blame] | 96 | } else { |
| 97 | UpdateKeyboardEnabled(); |
| 98 | } |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 99 | } |
| 100 | |
rsadam | c727b6a | 2014-10-31 18:59:33 | [diff] [blame] | 101 | void VirtualKeyboardController::OnTouchscreenDeviceConfigurationChanged() { |
| 102 | UpdateDevices(); |
| 103 | } |
| 104 | |
| 105 | void VirtualKeyboardController::OnKeyboardDeviceConfigurationChanged() { |
| 106 | UpdateDevices(); |
| 107 | } |
| 108 | |
| 109 | void VirtualKeyboardController::ToggleIgnoreExternalKeyboard() { |
| 110 | ignore_external_keyboard_ = !ignore_external_keyboard_; |
| 111 | UpdateKeyboardEnabled(); |
| 112 | } |
| 113 | |
yhanada | 7e20c9b | 2016-11-25 00:03:19 | [diff] [blame^] | 114 | void VirtualKeyboardController::MoveKeyboardToDisplay(int64_t display_id) { |
| 115 | DCHECK(keyboard::KeyboardController::GetInstance() != nullptr); |
| 116 | DCHECK(display_id != display::kInvalidDisplayId); |
| 117 | |
| 118 | aura::Window* container = |
| 119 | keyboard::KeyboardController::GetInstance()->GetContainerWindow(); |
| 120 | DCHECK(container != nullptr); |
| 121 | const display::Screen* screen = display::Screen::GetScreen(); |
| 122 | const display::Display current_display = |
| 123 | screen->GetDisplayNearestWindow(container); |
| 124 | |
| 125 | if (display_id != current_display.id()) |
| 126 | MoveKeyboardToDisplayInternal(display_id); |
| 127 | } |
| 128 | |
| 129 | void VirtualKeyboardController::MoveKeyboardToTouchableDisplay() { |
| 130 | DCHECK(keyboard::KeyboardController::GetInstance() != nullptr); |
| 131 | |
| 132 | aura::Window* container = |
| 133 | keyboard::KeyboardController::GetInstance()->GetContainerWindow(); |
| 134 | DCHECK(container != nullptr); |
| 135 | |
| 136 | const display::Screen* screen = display::Screen::GetScreen(); |
| 137 | const display::Display current_display = |
| 138 | screen->GetDisplayNearestWindow(container); |
| 139 | |
| 140 | if (WmShell::Get()->GetFocusedWindow() != nullptr) { |
| 141 | // Move the virtual keyboard to the focused display if that display has |
| 142 | // touch capability or keyboard is locked |
| 143 | const display::Display focused_display = |
| 144 | WmShell::Get()->GetFocusedWindow()->GetDisplayNearestWindow(); |
| 145 | if (current_display.id() != focused_display.id() && |
| 146 | focused_display.id() != display::kInvalidDisplayId && |
| 147 | focused_display.touch_support() == |
| 148 | display::Display::TouchSupport::TOUCH_SUPPORT_AVAILABLE) { |
| 149 | MoveKeyboardToDisplayInternal(focused_display.id()); |
| 150 | return; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if (current_display.touch_support() != |
| 155 | display::Display::TouchSupport::TOUCH_SUPPORT_AVAILABLE) { |
| 156 | // The keyboard is currently on the display without touch capability. |
| 157 | MoveKeyboardToFirstTouchableDisplay(); |
| 158 | } |
| 159 | } |
| 160 | |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 161 | void VirtualKeyboardController::UpdateDevices() { |
kylechar | e223f03 | 2016-06-03 15:02:53 | [diff] [blame] | 162 | ui::InputDeviceManager* device_data_manager = |
| 163 | ui::InputDeviceManager::GetInstance(); |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 164 | |
| 165 | // Checks for touchscreens. |
kylechar | e223f03 | 2016-06-03 15:02:53 | [diff] [blame] | 166 | has_touchscreen_ = device_data_manager->GetTouchscreenDevices().size() > 0; |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 167 | |
| 168 | // Checks for keyboards. |
| 169 | has_external_keyboard_ = false; |
| 170 | has_internal_keyboard_ = false; |
kylechar | 65cb32e4 | 2016-05-31 22:19:48 | [diff] [blame] | 171 | for (const ui::InputDevice& device : |
kylechar | e223f03 | 2016-06-03 15:02:53 | [diff] [blame] | 172 | device_data_manager->GetKeyboardDevices()) { |
rsadam | f971997 | 2014-11-05 02:57:59 | [diff] [blame] | 173 | if (has_internal_keyboard_ && has_external_keyboard_) |
| 174 | break; |
| 175 | ui::InputDeviceType type = device.type; |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 176 | if (type == ui::InputDeviceType::INPUT_DEVICE_INTERNAL) |
| 177 | has_internal_keyboard_ = true; |
| 178 | if (type == ui::InputDeviceType::INPUT_DEVICE_EXTERNAL) |
| 179 | has_external_keyboard_ = true; |
| 180 | } |
| 181 | // Update keyboard state. |
| 182 | UpdateKeyboardEnabled(); |
| 183 | } |
| 184 | |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 185 | void VirtualKeyboardController::UpdateKeyboardEnabled() { |
rsadam | 47e23d2 | 2014-12-15 18:11:37 | [diff] [blame] | 186 | if (!IsSmartVirtualKeyboardEnabled()) { |
sky | c68696c | 2016-07-01 21:06:02 | [diff] [blame] | 187 | SetKeyboardEnabled(WmShell::Get() |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 188 | ->maximize_mode_controller() |
| 189 | ->IsMaximizeModeWindowManagerEnabled()); |
| 190 | return; |
| 191 | } |
sky | c68696c | 2016-07-01 21:06:02 | [diff] [blame] | 192 | bool ignore_internal_keyboard = WmShell::Get() |
rsadam | d4f6a0b | 2015-03-02 23:50:08 | [diff] [blame] | 193 | ->maximize_mode_controller() |
| 194 | ->IsMaximizeModeWindowManagerEnabled(); |
| 195 | bool is_internal_keyboard_active = |
| 196 | has_internal_keyboard_ && !ignore_internal_keyboard; |
| 197 | SetKeyboardEnabled(!is_internal_keyboard_active && has_touchscreen_ && |
rsadam | c727b6a | 2014-10-31 18:59:33 | [diff] [blame] | 198 | (!has_external_keyboard_ || ignore_external_keyboard_)); |
jamescook | dcfe3d6 | 2016-06-21 18:58:42 | [diff] [blame] | 199 | WmShell::Get() |
rsadam | c727b6a | 2014-10-31 18:59:33 | [diff] [blame] | 200 | ->system_tray_notifier() |
rsadam | d4f6a0b | 2015-03-02 23:50:08 | [diff] [blame] | 201 | ->NotifyVirtualKeyboardSuppressionChanged(!is_internal_keyboard_active && |
rsadam | c727b6a | 2014-10-31 18:59:33 | [diff] [blame] | 202 | has_touchscreen_ && |
| 203 | has_external_keyboard_); |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 204 | } |
| 205 | |
rsadam | c727b6a | 2014-10-31 18:59:33 | [diff] [blame] | 206 | void VirtualKeyboardController::SetKeyboardEnabled(bool enabled) { |
rsadam | f796de60 | 2015-08-24 16:09:05 | [diff] [blame] | 207 | bool was_enabled = keyboard::IsKeyboardEnabled(); |
rsadam | c727b6a | 2014-10-31 18:59:33 | [diff] [blame] | 208 | keyboard::SetTouchKeyboardEnabled(enabled); |
rsadam | f796de60 | 2015-08-24 16:09:05 | [diff] [blame] | 209 | bool is_enabled = keyboard::IsKeyboardEnabled(); |
| 210 | if (is_enabled == was_enabled) |
| 211 | return; |
| 212 | if (is_enabled) { |
rsadam | c727b6a | 2014-10-31 18:59:33 | [diff] [blame] | 213 | Shell::GetInstance()->CreateKeyboard(); |
| 214 | } else { |
rsadam | f796de60 | 2015-08-24 16:09:05 | [diff] [blame] | 215 | Shell::GetInstance()->DeactivateKeyboard(); |
rsadam | c727b6a | 2014-10-31 18:59:33 | [diff] [blame] | 216 | } |
bshe | eaae09a | 2014-09-22 23:16:52 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | } // namespace ash |