OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/chromeos/input_method/xkeyboard.h" | 5 #include "chrome/browser/chromeos/input_method/xkeyboard.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include <X11/XKBlib.h> | 9 #include <X11/XKBlib.h> |
10 #include <X11/Xlib.h> | 10 #include <X11/Xlib.h> |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 public: | 130 public: |
131 // Returns the singleton instance of the class. Use LeakySingletonTraits. | 131 // Returns the singleton instance of the class. Use LeakySingletonTraits. |
132 // We don't delete the instance at exit. | 132 // We don't delete the instance at exit. |
133 static XKeyboard* GetInstance() { | 133 static XKeyboard* GetInstance() { |
134 return Singleton<XKeyboard, LeakySingletonTraits<XKeyboard> >::get(); | 134 return Singleton<XKeyboard, LeakySingletonTraits<XKeyboard> >::get(); |
135 } | 135 } |
136 | 136 |
137 // Sets the current keyboard layout to |layout_name|. This function does not | 137 // Sets the current keyboard layout to |layout_name|. This function does not |
138 // change the current mapping of the modifier keys. Returns true on success. | 138 // change the current mapping of the modifier keys. Returns true on success. |
139 bool SetLayout(const std::string& layout_name) { | 139 bool SetLayout(const std::string& layout_name) { |
140 if (SetLayoutInternal(layout_name, current_modifier_map_)) { | 140 if (SetLayoutInternal(layout_name, current_modifier_map_, false)) { |
141 current_layout_name_ = layout_name; | 141 current_layout_name_ = layout_name; |
142 return true; | 142 return true; |
143 } | 143 } |
144 return false; | 144 return false; |
145 } | 145 } |
146 | 146 |
| 147 // Sets the current keyboard layout to |current_layout_name_| again. |
| 148 // See crosbug.com/15851 for details. |
| 149 bool ReapplyLayout() { |
| 150 if (current_layout_name_.empty()) { |
| 151 LOG(ERROR) << "Can't reapply XKB layout: layout unknown"; |
| 152 return false; |
| 153 } |
| 154 VLOG(1) << "ReapplyLayout: setting to " << current_layout_name_; |
| 155 return SetLayoutInternal( |
| 156 current_layout_name_, current_modifier_map_, true /* force */); |
| 157 } |
| 158 |
147 // Remaps modifier keys. This function does not change the current keyboard | 159 // Remaps modifier keys. This function does not change the current keyboard |
148 // layout. Returns true on success. | 160 // layout. Returns true on success. |
149 bool RemapModifierKeys(const ModifierMap& modifier_map) { | 161 bool RemapModifierKeys(const ModifierMap& modifier_map) { |
150 const std::string layout_name = current_layout_name_.empty() ? | 162 const std::string layout_name = current_layout_name_.empty() ? |
151 kDefaultLayoutName : current_layout_name_; | 163 kDefaultLayoutName : current_layout_name_; |
152 if (SetLayoutInternal(layout_name, modifier_map)) { | 164 if (SetLayoutInternal(layout_name, modifier_map, false)) { |
153 current_layout_name_ = layout_name; | 165 current_layout_name_ = layout_name; |
154 current_modifier_map_ = modifier_map; | 166 current_modifier_map_ = modifier_map; |
155 return true; | 167 return true; |
156 } | 168 } |
157 return false; | 169 return false; |
158 } | 170 } |
159 | 171 |
160 // Turns on and off the auto-repeat of the keyboard. Returns true on success. | 172 // Turns on and off the auto-repeat of the keyboard. Returns true on success. |
161 // TODO(yusukes): Remove this function. | 173 // TODO(yusukes): Remove this function. |
162 bool SetAutoRepeatEnabled(bool enabled) { | 174 bool SetAutoRepeatEnabled(bool enabled) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 ModifierKey key = kCustomizableKeys[i]; | 215 ModifierKey key = kCustomizableKeys[i]; |
204 current_modifier_map_.push_back(ModifierKeyPair(key, key)); | 216 current_modifier_map_.push_back(ModifierKeyPair(key, key)); |
205 } | 217 } |
206 } | 218 } |
207 ~XKeyboard() { | 219 ~XKeyboard() { |
208 } | 220 } |
209 | 221 |
210 // This function is used by SetLayout() and RemapModifierKeys(). Calls | 222 // This function is used by SetLayout() and RemapModifierKeys(). Calls |
211 // setxkbmap command if needed, and updates the last_full_layout_name_ cache. | 223 // setxkbmap command if needed, and updates the last_full_layout_name_ cache. |
212 bool SetLayoutInternal(const std::string& layout_name, | 224 bool SetLayoutInternal(const std::string& layout_name, |
213 const ModifierMap& modifier_map) { | 225 const ModifierMap& modifier_map, |
| 226 bool force) { |
214 if (!CrosLibrary::Get()->EnsureLoaded()) { | 227 if (!CrosLibrary::Get()->EnsureLoaded()) { |
215 // We should not try to change a layout inside ui_tests. | 228 // We should not try to change a layout inside ui_tests. |
216 return false; | 229 return false; |
217 } | 230 } |
218 | 231 |
219 const std::string layouts_to_set = CreateFullXkbLayoutName( | 232 const std::string layouts_to_set = CreateFullXkbLayoutName( |
220 layout_name, modifier_map); | 233 layout_name, modifier_map); |
221 if (layouts_to_set.empty()) { | 234 if (layouts_to_set.empty()) { |
222 return false; | 235 return false; |
223 } | 236 } |
224 | 237 |
225 if (!current_layout_name_.empty()) { | 238 if (!current_layout_name_.empty()) { |
226 const std::string current_layout = CreateFullXkbLayoutName( | 239 const std::string current_layout = CreateFullXkbLayoutName( |
227 current_layout_name_, current_modifier_map_); | 240 current_layout_name_, current_modifier_map_); |
228 if (current_layout == layouts_to_set) { | 241 if (!force && (current_layout == layouts_to_set)) { |
229 DLOG(INFO) << "The requested layout is already set: " << layouts_to_set; | 242 DLOG(INFO) << "The requested layout is already set: " << layouts_to_set; |
230 return true; | 243 return true; |
231 } | 244 } |
232 } | 245 } |
233 | 246 |
234 // Turn off caps lock if there is no kCapsLockKey in the remapped keys. | 247 // Turn off caps lock if there is no kCapsLockKey in the remapped keys. |
235 if (!ContainsModifierKeyAsReplacement( | 248 if (!ContainsModifierKeyAsReplacement( |
236 modifier_map, kCapsLockKey)) { | 249 modifier_map, kCapsLockKey)) { |
237 SetCapsLockEnabled(false); | 250 SetCapsLockEnabled(false); |
238 } | 251 } |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 return true; | 397 return true; |
385 } | 398 } |
386 } | 399 } |
387 return false; | 400 return false; |
388 } | 401 } |
389 | 402 |
390 bool SetCurrentKeyboardLayoutByName(const std::string& layout_name) { | 403 bool SetCurrentKeyboardLayoutByName(const std::string& layout_name) { |
391 return XKeyboard::GetInstance()->SetLayout(layout_name); | 404 return XKeyboard::GetInstance()->SetLayout(layout_name); |
392 } | 405 } |
393 | 406 |
| 407 bool ReapplyCurrentKeyboardLayout() { |
| 408 return XKeyboard::GetInstance()->ReapplyLayout(); |
| 409 } |
| 410 |
394 bool RemapModifierKeys(const ModifierMap& modifier_map) { | 411 bool RemapModifierKeys(const ModifierMap& modifier_map) { |
395 return XKeyboard::GetInstance()->RemapModifierKeys(modifier_map); | 412 return XKeyboard::GetInstance()->RemapModifierKeys(modifier_map); |
396 } | 413 } |
397 | 414 |
398 bool SetAutoRepeatEnabled(bool enabled) { | 415 bool SetAutoRepeatEnabled(bool enabled) { |
399 return XKeyboard::GetInstance()->SetAutoRepeatEnabled(enabled); | 416 return XKeyboard::GetInstance()->SetAutoRepeatEnabled(enabled); |
400 } | 417 } |
401 | 418 |
402 bool SetAutoRepeatRate(const AutoRepeatRate& rate) { | 419 bool SetAutoRepeatRate(const AutoRepeatRate& rate) { |
403 return XKeyboard::GetInstance()->SetAutoRepeatRate(rate); | 420 return XKeyboard::GetInstance()->SetAutoRepeatRate(rate); |
404 } | 421 } |
405 | 422 |
406 } // namespace input_method | 423 } // namespace input_method |
407 } // namespace chromeos | 424 } // namespace chromeos |
OLD | NEW |