| 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_x11.h" | 5 #include "chromeos/ime/ime_keyboard_x11.h" |
| 6 | 6 |
| 7 namespace chromeos { | 7 namespace chromeos { |
| 8 namespace input_method { | 8 namespace input_method { |
| 9 namespace { | 9 namespace { |
| 10 | 10 |
| 11 // The delay in milliseconds that we'll wait between checking if | 11 // The delay in milliseconds that we'll wait between checking if |
| 12 // setxkbmap command finished. | 12 // setxkbmap command finished. |
| 13 const int kSetLayoutCommandCheckDelayMs = 100; | 13 const int kSetLayoutCommandCheckDelayMs = 100; |
| 14 | 14 |
| 15 // 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. |
| 16 // TODO(yusukes): Use libxkbfile.so instead of the command (crosbug.com/13105) | 16 // TODO(yusukes): Use libxkbfile.so instead of the command (crosbug.com/13105) |
| 17 const char kSetxkbmapCommand[] = "/usr/bin/setxkbmap"; | 17 const char kSetxkbmapCommand[] = "/usr/bin/setxkbmap"; |
| 18 | 18 |
| 19 // A string for obtaining a mask value for Num Lock. | 19 // A string for obtaining a mask value for Num Lock. |
| 20 const char kNumLockVirtualModifierString[] = "NumLock"; | 20 const char kNumLockVirtualModifierString[] = "NumLock"; |
| 21 | 21 |
| 22 const char *kISOLevel5ShiftLayoutIds[] = { | |
| 23 "ca(multix)", | |
| 24 "de(neo)", | |
| 25 }; | |
| 26 | |
| 27 const char *kAltGrLayoutIds[] = { | |
| 28 "be", | |
| 29 "be", | |
| 30 "be", | |
| 31 "bg", | |
| 32 "bg(phonetic)", | |
| 33 "br", | |
| 34 "ca", | |
| 35 "ca(eng)", | |
| 36 "ca(multix)", | |
| 37 "ch", | |
| 38 "ch(fr)", | |
| 39 "cz", | |
| 40 "de", | |
| 41 "de(neo)", | |
| 42 "dk", | |
| 43 "ee", | |
| 44 "es", | |
| 45 "es(cat)", | |
| 46 "fi", | |
| 47 "fr", | |
| 48 "gb(dvorak)", | |
| 49 "gb(extd)", | |
| 50 "gr", | |
| 51 "hr", | |
| 52 "il", | |
| 53 "it", | |
| 54 "latam", | |
| 55 "lt", | |
| 56 "no", | |
| 57 "pl", | |
| 58 "pt", | |
| 59 "ro", | |
| 60 "se", | |
| 61 "si", | |
| 62 "sk", | |
| 63 "tr", | |
| 64 "ua", | |
| 65 "us(altgr-intl)", | |
| 66 "us(intl)", | |
| 67 }; | |
| 68 | |
| 69 | |
| 70 // Returns false if |layout_name| contains a bad character. | 22 // Returns false if |layout_name| contains a bad character. |
| 71 bool CheckLayoutName(const std::string& layout_name) { | 23 bool CheckLayoutName(const std::string& layout_name) { |
| 72 static const char kValidLayoutNameCharacters[] = | 24 static const char kValidLayoutNameCharacters[] = |
| 73 "abcdefghijklmnopqrstuvwxyz0123456789()-_"; | 25 "abcdefghijklmnopqrstuvwxyz0123456789()-_"; |
| 74 | 26 |
| 75 if (layout_name.empty()) { | 27 if (layout_name.empty()) { |
| 76 DVLOG(1) << "Invalid layout_name: " << layout_name; | 28 DVLOG(1) << "Invalid layout_name: " << layout_name; |
| 77 return false; | 29 return false; |
| 78 } | 30 } |
| 79 | 31 |
| 80 if (layout_name.find_first_not_of(kValidLayoutNameCharacters) != | 32 if (layout_name.find_first_not_of(kValidLayoutNameCharacters) != |
| 81 std::string::npos) { | 33 std::string::npos) { |
| 82 DVLOG(1) << "Invalid layout_name: " << layout_name; | 34 DVLOG(1) << "Invalid layout_name: " << layout_name; |
| 83 return false; | 35 return false; |
| 84 } | 36 } |
| 85 | 37 |
| 86 return true; | 38 return true; |
| 87 } | 39 } |
| 88 | 40 |
| 41 } // namespace |
| 42 |
| 89 ImeKeyboardX11::ImeKeyboardX11() | 43 ImeKeyboardX11::ImeKeyboardX11() |
| 90 : is_running_on_chrome_os_(base::SysInfo::IsRunningOnChromeOS()), | 44 : is_running_on_chrome_os_(base::SysInfo::IsRunningOnChromeOS()), |
| 91 weak_factory_(this) { | 45 weak_factory_(this) { |
| 92 // X must be already initialized. | 46 // X must be already initialized. |
| 93 CHECK(gfx::GetXDisplay()); | 47 CHECK(gfx::GetXDisplay()); |
| 94 | 48 |
| 95 num_lock_mask_ = GetNumLockMask(); | 49 num_lock_mask_ = GetNumLockMask(); |
| 96 | 50 |
| 97 if (is_running_on_chrome_os_) { | 51 if (is_running_on_chrome_os_) { |
| 98 // Some code seems to assume that Mod2Mask is always assigned to | 52 // Some code seems to assume that Mod2Mask is always assigned to |
| 99 // Num Lock. | 53 // Num Lock. |
| 100 // | 54 // |
| 101 // TODO(yusukes): Check the assumption is really okay. If not, | 55 // TODO(yusukes): Check the assumption is really okay. If not, |
| 102 // modify the Aura code, and then remove the CHECK below. | 56 // modify the Aura code, and then remove the CHECK below. |
| 103 LOG_IF(ERROR, num_lock_mask_ != Mod2Mask) | 57 LOG_IF(ERROR, num_lock_mask_ != Mod2Mask) |
| 104 << "NumLock is not assigned to Mod2Mask. : " << num_lock_mask_; | 58 << "NumLock is not assigned to Mod2Mask. : " << num_lock_mask_; |
| 105 } | 59 } |
| 106 | 60 |
| 107 current_caps_lock_status_ = CapsLockIsEnabled(); | 61 caps_lock_is_enabled_ = CapsLockIsEnabled(); |
| 108 // Disable Num Lock on X start up for http://crosbug.com/29169. | 62 // Disable Num Lock on X start up for http://crosbug.com/29169. |
| 109 DisableNumLock(); | 63 DisableNumLock(); |
| 110 } | 64 } |
| 111 | 65 |
| 112 ImeKeyboardX11::~ImeKeyboardX11() {}; | 66 ImeKeyboardX11::~ImeKeyboardX11() {} |
| 113 | |
| 114 void ImeKeyboardX11::AddObserver(Observer* observer) { | |
| 115 observers_.AddObserver(observer); | |
| 116 } | |
| 117 | |
| 118 void ImeKeyboardX11::RemoveObserver(Observer* observer) { | |
| 119 observers_.RemoveObserver(observer); | |
| 120 } | |
| 121 | 67 |
| 122 unsigned int ImeKeyboardX11::GetNumLockMask() { | 68 unsigned int ImeKeyboardX11::GetNumLockMask() { |
| 123 DCHECK(thread_checker_.CalledOnValidThread()); | 69 DCHECK(thread_checker_.CalledOnValidThread()); |
| 124 static const unsigned int kBadMask = 0; | 70 static const unsigned int kBadMask = 0; |
| 125 | 71 |
| 126 unsigned int real_mask = kBadMask; | 72 unsigned int real_mask = kBadMask; |
| 127 XkbDescPtr xkb_desc = | 73 XkbDescPtr xkb_desc = |
| 128 XkbGetKeyboard(gfx::GetXDisplay(), XkbAllComponentsMask, XkbUseCoreKbd); | 74 XkbGetKeyboard(gfx::GetXDisplay(), XkbAllComponentsMask, XkbUseCoreKbd); |
| 129 if (!xkb_desc) | 75 if (!xkb_desc) |
| 130 return kBadMask; | 76 return kBadMask; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 155 | 101 |
| 156 void ImeKeyboardX11::SetLockedModifiers(bool caps_lock_enabled) { | 102 void ImeKeyboardX11::SetLockedModifiers(bool caps_lock_enabled) { |
| 157 DCHECK(thread_checker_.CalledOnValidThread()); | 103 DCHECK(thread_checker_.CalledOnValidThread()); |
| 158 | 104 |
| 159 // Always turn off num lock. | 105 // Always turn off num lock. |
| 160 unsigned int affect_mask = num_lock_mask_; | 106 unsigned int affect_mask = num_lock_mask_; |
| 161 unsigned int value_mask = 0; | 107 unsigned int value_mask = 0; |
| 162 | 108 |
| 163 affect_mask |= LockMask; | 109 affect_mask |= LockMask; |
| 164 value_mask |= (caps_lock_enabled ? LockMask : 0); | 110 value_mask |= (caps_lock_enabled ? LockMask : 0); |
| 165 current_caps_lock_status_ = caps_lock_enabled; | 111 caps_lock_is_enabled_ = caps_lock_enabled; |
| 166 | 112 |
| 167 XkbLockModifiers(gfx::GetXDisplay(), XkbUseCoreKbd, affect_mask, value_mask); | 113 XkbLockModifiers(gfx::GetXDisplay(), XkbUseCoreKbd, affect_mask, value_mask); |
| 168 } | 114 } |
| 169 | 115 |
| 170 bool ImeKeyboardX11::SetLayoutInternal(const std::string& layout_name, | 116 bool ImeKeyboardX11::SetLayoutInternal(const std::string& layout_name, |
| 171 bool force) { | 117 bool force) { |
| 172 if (!is_running_on_chrome_os_) { | 118 if (!is_running_on_chrome_os_) { |
| 173 // We should not try to change a layout on Linux or inside ui_tests. Just | 119 // We should not try to change a layout on Linux or inside ui_tests. Just |
| 174 // return true. | 120 // return true. |
| 175 return true; | 121 return true; |
| 176 } | 122 } |
| 177 | 123 |
| 178 if (!CheckLayoutName(layout_name)) | 124 if (!CheckLayoutName(layout_name)) |
| 179 return false; | 125 return false; |
| 180 | 126 |
| 181 if (!force && (current_layout_name_ == layout_name)) { | 127 if (!force && (last_layout_ == layout_name)) { |
| 182 DVLOG(1) << "The requested layout is already set: " << layout_name; | 128 DVLOG(1) << "The requested layout is already set: " << layout_name; |
| 183 return true; | 129 return true; |
| 184 } | 130 } |
| 185 | 131 |
| 186 DVLOG(1) << (force ? "Reapply" : "Set") << " layout: " << layout_name; | 132 DVLOG(1) << (force ? "Reapply" : "Set") << " layout: " << layout_name; |
| 187 | 133 |
| 188 const bool start_execution = execute_queue_.empty(); | 134 const bool start_execution = execute_queue_.empty(); |
| 189 // If no setxkbmap command is in flight (i.e. start_execution is true), | 135 // If no setxkbmap command is in flight (i.e. start_execution is true), |
| 190 // start the first one by explicitly calling MaybeExecuteSetLayoutCommand(). | 136 // start the first one by explicitly calling MaybeExecuteSetLayoutCommand(). |
| 191 // If one or more setxkbmap commands are already in flight, just push the | 137 // If one or more setxkbmap commands are already in flight, just push the |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 } | 204 } |
| 259 } | 205 } |
| 260 | 206 |
| 261 bool ImeKeyboardX11::CapsLockIsEnabled() { | 207 bool ImeKeyboardX11::CapsLockIsEnabled() { |
| 262 DCHECK(thread_checker_.CalledOnValidThread()); | 208 DCHECK(thread_checker_.CalledOnValidThread()); |
| 263 XkbStateRec status; | 209 XkbStateRec status; |
| 264 XkbGetState(gfx::GetXDisplay(), XkbUseCoreKbd, &status); | 210 XkbGetState(gfx::GetXDisplay(), XkbUseCoreKbd, &status); |
| 265 return (status.locked_mods & LockMask); | 211 return (status.locked_mods & LockMask); |
| 266 } | 212 } |
| 267 | 213 |
| 268 bool ImeKeyboardX11::IsISOLevel5ShiftAvailable() const { | |
| 269 for (size_t i = 0; i < arraysize(kISOLevel5ShiftLayoutIds); ++i) { | |
| 270 if (current_layout_name_ == kISOLevel5ShiftLayoutIds[i]) | |
| 271 return true; | |
| 272 } | |
| 273 return false; | |
| 274 } | |
| 275 | |
| 276 bool ImeKeyboardX11::IsAltGrAvailable() const { | |
| 277 for (size_t i = 0; i < arraysize(kAltGrLayoutIds); ++i) { | |
| 278 if (current_layout_name_ == kAltGrLayoutIds[i]) | |
| 279 return true; | |
| 280 } | |
| 281 return false; | |
| 282 } | |
| 283 | |
| 284 bool ImeKeyboardX11::SetAutoRepeatEnabled(bool enabled) { | 214 bool ImeKeyboardX11::SetAutoRepeatEnabled(bool enabled) { |
| 285 if (enabled) | 215 if (enabled) |
| 286 XAutoRepeatOn(gfx::GetXDisplay()); | 216 XAutoRepeatOn(gfx::GetXDisplay()); |
| 287 else | 217 else |
| 288 XAutoRepeatOff(gfx::GetXDisplay()); | 218 XAutoRepeatOff(gfx::GetXDisplay()); |
| 289 DVLOG(1) << "Set auto-repeat mode to: " << (enabled ? "on" : "off"); | 219 DVLOG(1) << "Set auto-repeat mode to: " << (enabled ? "on" : "off"); |
| 290 return true; | 220 return true; |
| 291 } | 221 } |
| 292 | 222 |
| 293 bool ImeKeyboardX11::SetAutoRepeatRate(const AutoRepeatRate& rate) { | 223 bool ImeKeyboardX11::SetAutoRepeatRate(const AutoRepeatRate& rate) { |
| 294 DVLOG(1) << "Set auto-repeat rate to: " | 224 DVLOG(1) << "Set auto-repeat rate to: " |
| 295 << rate.initial_delay_in_ms << " ms delay, " | 225 << rate.initial_delay_in_ms << " ms delay, " |
| 296 << rate.repeat_interval_in_ms << " ms interval"; | 226 << rate.repeat_interval_in_ms << " ms interval"; |
| 297 if (XkbSetAutoRepeatRate(gfx::GetXDisplay(), XkbUseCoreKbd, | 227 if (XkbSetAutoRepeatRate(gfx::GetXDisplay(), XkbUseCoreKbd, |
| 298 rate.initial_delay_in_ms, | 228 rate.initial_delay_in_ms, |
| 299 rate.repeat_interval_in_ms) != True) { | 229 rate.repeat_interval_in_ms) != True) { |
| 300 DVLOG(1) << "Failed to set auto-repeat rate"; | 230 DVLOG(1) << "Failed to set auto-repeat rate"; |
| 301 return false; | 231 return false; |
| 302 } | 232 } |
| 303 return true; | 233 return true; |
| 304 } | 234 } |
| 305 | 235 |
| 306 void ImeKeyboardX11::SetCapsLockEnabled(bool enable_caps_lock) { | 236 void ImeKeyboardX11::SetCapsLockEnabled(bool enable_caps_lock) { |
| 307 bool old_state = current_caps_lock_status_; | 237 bool old_state = caps_lock_is_enabled_; |
| 308 SetLockedModifiers(enable_caps_lock); | 238 SetLockedModifiers(enable_caps_lock); |
| 309 if (old_state != enable_caps_lock) { | 239 if (old_state != enable_caps_lock) { |
| 310 FOR_EACH_OBSERVER(ImeKeyboard::Observer, observers_, | 240 FOR_EACH_OBSERVER(ImeKeyboard::Observer, observers_, |
| 311 OnCapsLockChanged(enable_caps_lock)); | 241 OnCapsLockChanged(enable_caps_lock)); |
| 312 } | 242 } |
| 313 } | 243 } |
| 314 | 244 |
| 315 bool ImeKeyboardX11::SetCurrentKeyboardLayoutByName( | 245 bool ImeKeyboardX11::SetCurrentKeyboardLayoutByName( |
| 316 const std::string& layout_name) { | 246 const std::string& layout_name) { |
| 317 if (SetLayoutInternal(layout_name, false)) { | 247 if (SetLayoutInternal(layout_name, false)) { |
| 318 current_layout_name_ = layout_name; | 248 last_layout_ = layout_name; |
| 319 return true; | 249 return true; |
| 320 } | 250 } |
| 321 return false; | 251 return false; |
| 322 } | 252 } |
| 323 | 253 |
| 324 bool ImeKeyboardX11::ReapplyCurrentKeyboardLayout() { | 254 bool ImeKeyboardX11::ReapplyCurrentKeyboardLayout() { |
| 325 if (current_layout_name_.empty()) { | 255 if (last_layout_.empty()) { |
| 326 DVLOG(1) << "Can't reapply XKB layout: layout unknown"; | 256 DVLOG(1) << "Can't reapply XKB layout: layout unknown"; |
| 327 return false; | 257 return false; |
| 328 } | 258 } |
| 329 return SetLayoutInternal(current_layout_name_, true /* force */); | 259 return SetLayoutInternal(last_layout_, true /* force */); |
| 330 } | 260 } |
| 331 | 261 |
| 332 void ImeKeyboardX11::ReapplyCurrentModifierLockStatus() { | 262 void ImeKeyboardX11::ReapplyCurrentModifierLockStatus() { |
| 333 SetLockedModifiers(current_caps_lock_status_); | 263 SetLockedModifiers(caps_lock_is_enabled_); |
| 334 } | 264 } |
| 335 | 265 |
| 336 void ImeKeyboardX11::DisableNumLock() { | 266 void ImeKeyboardX11::DisableNumLock() { |
| 337 SetCapsLockEnabled(current_caps_lock_status_); | 267 SetCapsLockEnabled(caps_lock_is_enabled_); |
| 338 } | 268 } |
| 339 | 269 |
| 340 void ImeKeyboardX11::OnSetLayoutFinish() { | 270 void ImeKeyboardX11::OnSetLayoutFinish() { |
| 341 if (execute_queue_.empty()) { | 271 if (execute_queue_.empty()) { |
| 342 DVLOG(1) << "OnSetLayoutFinish: execute_queue_ is empty. " | 272 DVLOG(1) << "OnSetLayoutFinish: execute_queue_ is empty. " |
| 343 << "base::LaunchProcess failed?"; | 273 << "base::LaunchProcess failed?"; |
| 344 return; | 274 return; |
| 345 } | 275 } |
| 346 execute_queue_.pop(); | 276 execute_queue_.pop(); |
| 347 MaybeExecuteSetLayoutCommand(); | 277 MaybeExecuteSetLayoutCommand(); |
| 348 } | 278 } |
| 349 | 279 |
| 350 } // namespace | |
| 351 | |
| 352 // static | 280 // static |
| 353 bool ImeKeyboard::GetAutoRepeatEnabledForTesting() { | 281 bool ImeKeyboard::GetAutoRepeatEnabledForTesting() { |
| 354 XKeyboardState state = {}; | 282 XKeyboardState state = {}; |
| 355 XGetKeyboardControl(gfx::GetXDisplay(), &state); | 283 XGetKeyboardControl(gfx::GetXDisplay(), &state); |
| 356 return state.global_auto_repeat != AutoRepeatModeOff; | 284 return state.global_auto_repeat != AutoRepeatModeOff; |
| 357 } | 285 } |
| 358 | 286 |
| 359 // static | 287 // static |
| 360 bool ImeKeyboard::GetAutoRepeatRateForTesting(AutoRepeatRate* out_rate) { | 288 bool ImeKeyboard::GetAutoRepeatRateForTesting(AutoRepeatRate* out_rate) { |
| 361 return XkbGetAutoRepeatRate(gfx::GetXDisplay(), | 289 return XkbGetAutoRepeatRate(gfx::GetXDisplay(), |
| 362 XkbUseCoreKbd, | 290 XkbUseCoreKbd, |
| 363 &(out_rate->initial_delay_in_ms), | 291 &(out_rate->initial_delay_in_ms), |
| 364 &(out_rate->repeat_interval_in_ms)) == True; | 292 &(out_rate->repeat_interval_in_ms)) == True; |
| 365 } | 293 } |
| 366 | 294 |
| 367 // static | 295 // static |
| 368 bool ImeKeyboard::CheckLayoutNameForTesting(const std::string& layout_name) { | 296 bool ImeKeyboard::CheckLayoutNameForTesting(const std::string& layout_name) { |
| 369 return CheckLayoutName(layout_name); | 297 return CheckLayoutName(layout_name); |
| 370 } | 298 } |
| 371 | 299 |
| 372 // static | |
| 373 ImeKeyboard* ImeKeyboard::Create() { return new ImeKeyboardX11(); } | |
| 374 | |
| 375 } // namespace input_method | 300 } // namespace input_method |
| 376 } // namespace chromeos | 301 } // namespace chromeos |
| OLD | NEW |