Added llvm into exceptions as we can't add README.chromium into 3rd party repository
[chromium-blink-merge.git] / ash / virtual_keyboard_controller.cc
blobd043dd32dfe2252fd05b1f50192a3edebad2c6f3
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 "ash_switches.h"
12 #include "base/command_line.h"
13 #include "base/strings/string_util.h"
14 #include "ui/events/devices/device_data_manager.h"
15 #include "ui/events/devices/input_device.h"
16 #include "ui/events/devices/keyboard_device.h"
17 #include "ui/events/devices/touchscreen_device.h"
18 #include "ui/gfx/x/x11_types.h"
19 #include "ui/keyboard/keyboard_switches.h"
20 #include "ui/keyboard/keyboard_util.h"
22 namespace ash {
23 namespace {
25 // Checks whether smart deployment is enabled.
26 bool IsSmartVirtualKeyboardEnabled() {
27 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
28 keyboard::switches::kEnableVirtualKeyboard)) {
29 return false;
31 return !base::CommandLine::ForCurrentProcess()->HasSwitch(
32 keyboard::switches::kDisableSmartVirtualKeyboard);
35 } // namespace
37 VirtualKeyboardController::VirtualKeyboardController()
38 : has_external_keyboard_(false),
39 has_internal_keyboard_(false),
40 has_touchscreen_(false),
41 ignore_external_keyboard_(false) {
42 Shell::GetInstance()->AddShellObserver(this);
43 ui::DeviceDataManager::GetInstance()->AddObserver(this);
44 UpdateDevices();
47 VirtualKeyboardController::~VirtualKeyboardController() {
48 Shell::GetInstance()->RemoveShellObserver(this);
49 ui::DeviceDataManager::GetInstance()->RemoveObserver(this);
52 void VirtualKeyboardController::OnMaximizeModeStarted() {
53 if (!IsSmartVirtualKeyboardEnabled())
54 SetKeyboardEnabled(true);
57 void VirtualKeyboardController::OnMaximizeModeEnded() {
58 if (!IsSmartVirtualKeyboardEnabled())
59 SetKeyboardEnabled(false);
62 void VirtualKeyboardController::OnTouchscreenDeviceConfigurationChanged() {
63 UpdateDevices();
66 void VirtualKeyboardController::OnKeyboardDeviceConfigurationChanged() {
67 UpdateDevices();
70 void VirtualKeyboardController::OnMouseDeviceConfigurationChanged() {
73 void VirtualKeyboardController::OnTouchpadDeviceConfigurationChanged() {
76 void VirtualKeyboardController::ToggleIgnoreExternalKeyboard() {
77 ignore_external_keyboard_ = !ignore_external_keyboard_;
78 UpdateKeyboardEnabled();
81 void VirtualKeyboardController::UpdateDevices() {
82 ui::DeviceDataManager* device_data_manager =
83 ui::DeviceDataManager::GetInstance();
85 // Checks for touchscreens.
86 has_touchscreen_ = device_data_manager->touchscreen_devices().size() > 0;
88 // Checks for keyboards.
89 has_external_keyboard_ = false;
90 has_internal_keyboard_ = false;
91 for (const ui::KeyboardDevice& device :
92 device_data_manager->keyboard_devices()) {
93 if (has_internal_keyboard_ && has_external_keyboard_)
94 break;
95 ui::InputDeviceType type = device.type;
96 if (type == ui::InputDeviceType::INPUT_DEVICE_INTERNAL)
97 has_internal_keyboard_ = true;
98 if (type == ui::InputDeviceType::INPUT_DEVICE_EXTERNAL)
99 has_external_keyboard_ = true;
101 // Update keyboard state.
102 UpdateKeyboardEnabled();
105 void VirtualKeyboardController::UpdateKeyboardEnabled() {
106 if (!IsSmartVirtualKeyboardEnabled()) {
107 SetKeyboardEnabled(Shell::GetInstance()
108 ->maximize_mode_controller()
109 ->IsMaximizeModeWindowManagerEnabled());
110 return;
112 SetKeyboardEnabled(!has_internal_keyboard_ && has_touchscreen_ &&
113 (!has_external_keyboard_ || ignore_external_keyboard_));
114 ash::Shell::GetInstance()
115 ->system_tray_notifier()
116 ->NotifyVirtualKeyboardSuppressionChanged(!has_internal_keyboard_ &&
117 has_touchscreen_ &&
118 has_external_keyboard_);
121 void VirtualKeyboardController::SetKeyboardEnabled(bool enabled) {
122 keyboard::SetTouchKeyboardEnabled(enabled);
123 if (enabled) {
124 Shell::GetInstance()->CreateKeyboard();
125 } else {
126 if (!keyboard::IsKeyboardEnabled())
127 Shell::GetInstance()->DeactivateKeyboard();
131 } // namespace ash