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 "chromeos/ime/ime_keyboard.h" | 5 #include "chromeos/ime/ime_keyboard_x11.h" |
6 | |
7 #include <cstdlib> | |
8 #include <cstring> | |
9 #include <queue> | |
10 #include <set> | |
11 #include <utility> | |
12 | |
13 #include "base/bind.h" | |
14 #include "base/logging.h" | |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/message_loop/message_loop.h" | |
17 #include "base/process/kill.h" | |
18 #include "base/process/launch.h" | |
19 #include "base/process/process_handle.h" | |
20 #include "base/strings/string_util.h" | |
21 #include "base/strings/stringprintf.h" | |
22 #include "base/sys_info.h" | |
23 #include "base/threading/thread_checker.h" | |
24 #include "ui/gfx/x/x11_types.h" | |
25 | |
26 // These includes conflict with base/tracked_objects.h so must come last. | |
27 #include <X11/XKBlib.h> | |
28 #include <X11/Xlib.h> | |
29 | 6 |
30 namespace chromeos { | 7 namespace chromeos { |
31 namespace input_method { | 8 namespace input_method { |
32 namespace { | 9 namespace { |
33 | 10 |
34 // The delay in milliseconds that we'll wait between checking if | 11 // The delay in milliseconds that we'll wait between checking if |
35 // setxkbmap command finished. | 12 // setxkbmap command finished. |
36 const int kSetLayoutCommandCheckDelayMs = 100; | 13 const int kSetLayoutCommandCheckDelayMs = 100; |
37 | 14 |
38 // The command we use to set the current XKB layout and modifier key mapping. | 15 // The command we use to set the current XKB layout and modifier key mapping. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 79 |
103 if (layout_name.find_first_not_of(kValidLayoutNameCharacters) != | 80 if (layout_name.find_first_not_of(kValidLayoutNameCharacters) != |
104 std::string::npos) { | 81 std::string::npos) { |
105 DVLOG(1) << "Invalid layout_name: " << layout_name; | 82 DVLOG(1) << "Invalid layout_name: " << layout_name; |
106 return false; | 83 return false; |
107 } | 84 } |
108 | 85 |
109 return true; | 86 return true; |
110 } | 87 } |
111 | 88 |
112 class ImeKeyboardX11 : public ImeKeyboard { | |
113 public: | |
114 ImeKeyboardX11(); | |
115 virtual ~ImeKeyboardX11() {} | |
116 | |
117 // Adds/removes observer. | |
118 virtual void AddObserver(Observer* observer) override; | |
119 virtual void RemoveObserver(Observer* observer) override; | |
120 | |
121 // ImeKeyboard: | |
122 virtual bool SetCurrentKeyboardLayoutByName( | |
123 const std::string& layout_name) override; | |
124 virtual bool ReapplyCurrentKeyboardLayout() override; | |
125 virtual void ReapplyCurrentModifierLockStatus() override; | |
126 virtual void DisableNumLock() override; | |
127 virtual void SetCapsLockEnabled(bool enable_caps_lock) override; | |
128 virtual bool CapsLockIsEnabled() override; | |
129 virtual bool IsISOLevel5ShiftAvailable() const override; | |
130 virtual bool IsAltGrAvailable() const override; | |
131 virtual bool SetAutoRepeatEnabled(bool enabled) override; | |
132 virtual bool SetAutoRepeatRate(const AutoRepeatRate& rate) override; | |
133 | |
134 private: | |
135 // Returns a mask for Num Lock (e.g. 1U << 4). Returns 0 on error. | |
136 unsigned int GetNumLockMask(); | |
137 | |
138 // Sets the caps-lock status. Note that calling this function always disables | |
139 // the num-lock. | |
140 void SetLockedModifiers(bool caps_lock_enabled); | |
141 | |
142 // This function is used by SetLayout() and RemapModifierKeys(). Calls | |
143 // setxkbmap command if needed, and updates the last_full_layout_name_ cache. | |
144 bool SetLayoutInternal(const std::string& layout_name, bool force); | |
145 | |
146 // Executes 'setxkbmap -layout ...' command asynchronously using a layout name | |
147 // in the |execute_queue_|. Do nothing if the queue is empty. | |
148 // TODO(yusukes): Use libxkbfile.so instead of the command (crosbug.com/13105) | |
149 void MaybeExecuteSetLayoutCommand(); | |
150 | |
151 // Polls to see setxkbmap process exits. | |
152 void PollUntilChildFinish(const base::ProcessHandle handle); | |
153 | |
154 // Called when execve'd setxkbmap process exits. | |
155 void OnSetLayoutFinish(); | |
156 | |
157 const bool is_running_on_chrome_os_; | |
158 unsigned int num_lock_mask_; | |
159 | |
160 // The current Caps Lock status. If true, enabled. | |
161 bool current_caps_lock_status_; | |
162 | |
163 // The XKB layout name which we set last time like "us" and "us(dvorak)". | |
164 std::string current_layout_name_; | |
165 | |
166 // A queue for executing setxkbmap one by one. | |
167 std::queue<std::string> execute_queue_; | |
168 | |
169 base::ThreadChecker thread_checker_; | |
170 | |
171 base::WeakPtrFactory<ImeKeyboardX11> weak_factory_; | |
172 | |
173 ObserverList<Observer> observers_; | |
174 | |
175 DISALLOW_COPY_AND_ASSIGN(ImeKeyboardX11); | |
176 }; | |
177 | |
178 ImeKeyboardX11::ImeKeyboardX11() | 89 ImeKeyboardX11::ImeKeyboardX11() |
179 : is_running_on_chrome_os_(base::SysInfo::IsRunningOnChromeOS()), | 90 : is_running_on_chrome_os_(base::SysInfo::IsRunningOnChromeOS()), |
180 weak_factory_(this) { | 91 weak_factory_(this) { |
181 // X must be already initialized. | 92 // X must be already initialized. |
182 CHECK(gfx::GetXDisplay()); | 93 CHECK(gfx::GetXDisplay()); |
183 | 94 |
184 num_lock_mask_ = GetNumLockMask(); | 95 num_lock_mask_ = GetNumLockMask(); |
185 | 96 |
186 if (is_running_on_chrome_os_) { | 97 if (is_running_on_chrome_os_) { |
187 // Some code seems to assume that Mod2Mask is always assigned to | 98 // Some code seems to assume that Mod2Mask is always assigned to |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 // static | 365 // static |
455 bool ImeKeyboard::CheckLayoutNameForTesting(const std::string& layout_name) { | 366 bool ImeKeyboard::CheckLayoutNameForTesting(const std::string& layout_name) { |
456 return CheckLayoutName(layout_name); | 367 return CheckLayoutName(layout_name); |
457 } | 368 } |
458 | 369 |
459 // static | 370 // static |
460 ImeKeyboard* ImeKeyboard::Create() { return new ImeKeyboardX11(); } | 371 ImeKeyboard* ImeKeyboard::Create() { return new ImeKeyboardX11(); } |
461 | 372 |
462 } // namespace input_method | 373 } // namespace input_method |
463 } // namespace chromeos | 374 } // namespace chromeos |
OLD | NEW |