OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/virtual_keyboard_controller.h" | 5 #include "ash/virtual_keyboard_controller.h" |
6 | 6 |
7 #include <vector> | |
8 | |
7 #include "ash/shell.h" | 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/device_data_manager.h" | |
14 #include "ui/events/input_device.h" | |
15 #include "ui/events/keyboard_device.h" | |
16 #include "ui/events/touchscreen_device.h" | |
17 #include "ui/gfx/x/x11_types.h" | |
18 #include "ui/keyboard/keyboard_switches.h" | |
8 #include "ui/keyboard/keyboard_util.h" | 19 #include "ui/keyboard/keyboard_util.h" |
9 | 20 |
10 namespace ash { | 21 namespace ash { |
11 | 22 |
12 VirtualKeyboardController::VirtualKeyboardController() { | 23 VirtualKeyboardController::VirtualKeyboardController() |
13 Shell::GetInstance()->AddShellObserver(this); | 24 : has_external_keyboard_(false), |
25 has_internal_keyboard_(false), | |
26 has_touchscreen_(false) { | |
27 ui::DeviceDataManager::GetInstance()->AddObserver(this); | |
28 UpdateDevices(); | |
29 // Do not actually create the keyboard object since Ash environment may not be | |
30 // ready yet. | |
31 keyboard::SetTouchKeyboardEnabled(ShouldShowKeyboard()); | |
14 } | 32 } |
15 | 33 |
16 VirtualKeyboardController::~VirtualKeyboardController() { | 34 VirtualKeyboardController::~VirtualKeyboardController() { |
17 Shell::GetInstance()->RemoveShellObserver(this); | 35 ui::DeviceDataManager::GetInstance()->RemoveObserver(this); |
18 } | 36 } |
19 | 37 |
20 void VirtualKeyboardController::OnMaximizeModeStarted() { | 38 // Determines the active input devices. |
21 keyboard::SetTouchKeyboardEnabled(true); | 39 void VirtualKeyboardController::UpdateDevices() { |
22 Shell::GetInstance()->CreateKeyboard(); | 40 ui::DeviceDataManager* device_data_manager = |
41 ui::DeviceDataManager::GetInstance(); | |
42 | |
43 // Checks for internal touchscreens. | |
44 std::vector<ui::TouchscreenDevice> screens = | |
45 device_data_manager->touchscreen_devices(); | |
46 has_touchscreen_ = screens.size() > 0; | |
47 | |
48 // Checks for keyboards. | |
49 has_external_keyboard_ = false; | |
50 has_internal_keyboard_ = false; | |
51 std::vector<ui::KeyboardDevice> keyboards = | |
52 device_data_manager->keyboard_devices(); | |
53 std::vector<ui::KeyboardDevice>::const_iterator iter; | |
54 for (iter = keyboards.begin(); iter != keyboards.end(); ++iter) { | |
55 ui::InputDeviceType type = (*iter).type; | |
56 if (type == ui::InputDeviceType::INPUT_DEVICE_INTERNAL) | |
57 has_internal_keyboard_ = true; | |
58 if (type == ui::InputDeviceType::INPUT_DEVICE_EXTERNAL) | |
59 has_external_keyboard_ = true; | |
60 } | |
23 } | 61 } |
24 | 62 |
25 void VirtualKeyboardController::OnMaximizeModeEnded() { | 63 // Updates the virtual keyboard state. |
26 keyboard::SetTouchKeyboardEnabled(false); | 64 void VirtualKeyboardController::SetKeyboardEnabled(bool enabled) { |
27 if (!keyboard::IsKeyboardEnabled()) | 65 keyboard::SetTouchKeyboardEnabled(enabled); |
28 Shell::GetInstance()->DeactivateKeyboard(); | 66 if (enabled) { |
67 Shell::GetInstance()->CreateKeyboard(); | |
68 } else { | |
69 if (!keyboard::IsKeyboardEnabled()) | |
70 Shell::GetInstance()->DeactivateKeyboard(); | |
71 } | |
72 } | |
73 | |
74 // Whether the keyboard should be shown. | |
75 bool VirtualKeyboardController::ShouldShowKeyboard() { | |
76 if (!CommandLine::ForCurrentProcess()->HasSwitch( | |
77 keyboard::switches::kAutoVirtualKeyboard)) { | |
78 MaximizeModeController* controller = | |
79 Shell::GetInstance()->maximize_mode_controller(); | |
80 // Prevent keyboard from deploying while testing on a desktop. | |
81 if (!controller || !controller->CanEnterMaximizeMode()) | |
flackr
2014/10/24 14:54:23
I think controller should always exist. Also, keep
rsadam
2014/10/24 15:32:32
I was getting segfaults that the controller was no
| |
82 return false; | |
83 // The internal keyboard is disabled when the user enters maximize mode. | |
84 return !has_internal_keyboard_; | |
85 } | |
86 // TODO(rsadam@): Add logic for a repressed keyboard. For now just enable the | |
87 // keyboard. | |
88 return !has_internal_keyboard_ && has_touchscreen_; | |
flackr
2014/10/24 14:54:23
Since this is behind a flag, can we try adding &&
rsadam
2014/10/24 15:32:32
Done.
| |
89 } | |
90 | |
91 void VirtualKeyboardController::OnTouchscreenDeviceConfigurationChanged() { | |
92 UpdateDevices(); | |
93 SetKeyboardEnabled(ShouldShowKeyboard()); | |
94 } | |
95 | |
96 void VirtualKeyboardController::OnKeyboardDeviceConfigurationChanged() { | |
97 UpdateDevices(); | |
98 SetKeyboardEnabled(ShouldShowKeyboard()); | |
29 } | 99 } |
30 | 100 |
31 } // namespace ash | 101 } // namespace ash |
OLD | NEW |