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