OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chromeos/ime/ime_keyboard_x11.h" |
| 6 |
| 7 namespace chromeos { |
| 8 namespace input_method { |
| 9 namespace { |
| 10 |
| 11 // The delay in milliseconds that we'll wait between checking if |
| 12 // setxkbmap command finished. |
| 13 const int kSetLayoutCommandCheckDelayMs = 100; |
| 14 |
| 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) |
| 17 const char kSetxkbmapCommand[] = "/usr/bin/setxkbmap"; |
| 18 |
| 19 // A string for obtaining a mask value for Num Lock. |
| 20 const char kNumLockVirtualModifierString[] = "NumLock"; |
| 21 |
| 22 // Returns false if |layout_name| contains a bad character. |
| 23 bool CheckLayoutName(const std::string& layout_name) { |
| 24 static const char kValidLayoutNameCharacters[] = |
| 25 "abcdefghijklmnopqrstuvwxyz0123456789()-_"; |
| 26 |
| 27 if (layout_name.empty()) { |
| 28 DVLOG(1) << "Invalid layout_name: " << layout_name; |
| 29 return false; |
| 30 } |
| 31 |
| 32 if (layout_name.find_first_not_of(kValidLayoutNameCharacters) != |
| 33 std::string::npos) { |
| 34 DVLOG(1) << "Invalid layout_name: " << layout_name; |
| 35 return false; |
| 36 } |
| 37 |
| 38 return true; |
| 39 } |
| 40 |
| 41 } // namespace |
| 42 |
| 43 ImeKeyboardX11::ImeKeyboardX11() |
| 44 : is_running_on_chrome_os_(base::SysInfo::IsRunningOnChromeOS()), |
| 45 weak_factory_(this) { |
| 46 // X must be already initialized. |
| 47 CHECK(gfx::GetXDisplay()); |
| 48 |
| 49 num_lock_mask_ = GetNumLockMask(); |
| 50 |
| 51 if (is_running_on_chrome_os_) { |
| 52 // Some code seems to assume that Mod2Mask is always assigned to |
| 53 // Num Lock. |
| 54 // |
| 55 // TODO(yusukes): Check the assumption is really okay. If not, |
| 56 // modify the Aura code, and then remove the CHECK below. |
| 57 LOG_IF(ERROR, num_lock_mask_ != Mod2Mask) |
| 58 << "NumLock is not assigned to Mod2Mask. : " << num_lock_mask_; |
| 59 } |
| 60 |
| 61 caps_lock_is_enabled_ = CapsLockIsEnabled(); |
| 62 // Disable Num Lock on X start up for http://crosbug.com/29169. |
| 63 DisableNumLock(); |
| 64 } |
| 65 |
| 66 ImeKeyboardX11::~ImeKeyboardX11() {} |
| 67 |
| 68 unsigned int ImeKeyboardX11::GetNumLockMask() { |
| 69 DCHECK(thread_checker_.CalledOnValidThread()); |
| 70 static const unsigned int kBadMask = 0; |
| 71 |
| 72 unsigned int real_mask = kBadMask; |
| 73 XkbDescPtr xkb_desc = |
| 74 XkbGetKeyboard(gfx::GetXDisplay(), XkbAllComponentsMask, XkbUseCoreKbd); |
| 75 if (!xkb_desc) |
| 76 return kBadMask; |
| 77 |
| 78 if (xkb_desc->dpy && xkb_desc->names) { |
| 79 const std::string string_to_find(kNumLockVirtualModifierString); |
| 80 for (size_t i = 0; i < XkbNumVirtualMods; ++i) { |
| 81 const unsigned int virtual_mod_mask = 1U << i; |
| 82 char* virtual_mod_str_raw_ptr = |
| 83 XGetAtomName(xkb_desc->dpy, xkb_desc->names->vmods[i]); |
| 84 if (!virtual_mod_str_raw_ptr) |
| 85 continue; |
| 86 const std::string virtual_mod_str = virtual_mod_str_raw_ptr; |
| 87 XFree(virtual_mod_str_raw_ptr); |
| 88 |
| 89 if (string_to_find == virtual_mod_str) { |
| 90 if (!XkbVirtualModsToReal(xkb_desc, virtual_mod_mask, &real_mask)) { |
| 91 DVLOG(1) << "XkbVirtualModsToReal failed"; |
| 92 real_mask = kBadMask; // reset the return value, just in case. |
| 93 } |
| 94 break; |
| 95 } |
| 96 } |
| 97 } |
| 98 XkbFreeKeyboard(xkb_desc, 0, True /* free all components */); |
| 99 return real_mask; |
| 100 } |
| 101 |
| 102 void ImeKeyboardX11::SetLockedModifiers() { |
| 103 DCHECK(thread_checker_.CalledOnValidThread()); |
| 104 |
| 105 // Always turn off num lock. |
| 106 unsigned int affect_mask = num_lock_mask_; |
| 107 unsigned int value_mask = 0; |
| 108 |
| 109 affect_mask |= LockMask; |
| 110 value_mask |= (caps_lock_is_enabled_ ? LockMask : 0); |
| 111 |
| 112 XkbLockModifiers(gfx::GetXDisplay(), XkbUseCoreKbd, affect_mask, value_mask); |
| 113 } |
| 114 |
| 115 bool ImeKeyboardX11::SetLayoutInternal(const std::string& layout_name, |
| 116 bool force) { |
| 117 if (!is_running_on_chrome_os_) { |
| 118 // We should not try to change a layout on Linux or inside ui_tests. Just |
| 119 // return true. |
| 120 return true; |
| 121 } |
| 122 |
| 123 if (!CheckLayoutName(layout_name)) |
| 124 return false; |
| 125 |
| 126 if (!force && (last_layout_ == layout_name)) { |
| 127 DVLOG(1) << "The requested layout is already set: " << layout_name; |
| 128 return true; |
| 129 } |
| 130 |
| 131 DVLOG(1) << (force ? "Reapply" : "Set") << " layout: " << layout_name; |
| 132 |
| 133 const bool start_execution = execute_queue_.empty(); |
| 134 // If no setxkbmap command is in flight (i.e. start_execution is true), |
| 135 // start the first one by explicitly calling MaybeExecuteSetLayoutCommand(). |
| 136 // If one or more setxkbmap commands are already in flight, just push the |
| 137 // layout name to the queue. setxkbmap command for the layout will be called |
| 138 // via OnSetLayoutFinish() callback later. |
| 139 execute_queue_.push(layout_name); |
| 140 if (start_execution) |
| 141 MaybeExecuteSetLayoutCommand(); |
| 142 |
| 143 return true; |
| 144 } |
| 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 ImeKeyboardX11::MaybeExecuteSetLayoutCommand() { |
| 150 if (execute_queue_.empty()) |
| 151 return; |
| 152 const std::string layout_to_set = execute_queue_.front(); |
| 153 |
| 154 std::vector<std::string> argv; |
| 155 base::ProcessHandle handle = base::kNullProcessHandle; |
| 156 |
| 157 argv.push_back(kSetxkbmapCommand); |
| 158 argv.push_back("-layout"); |
| 159 argv.push_back(layout_to_set); |
| 160 argv.push_back("-synch"); |
| 161 |
| 162 if (!base::LaunchProcess(argv, base::LaunchOptions(), &handle)) { |
| 163 DVLOG(1) << "Failed to execute setxkbmap: " << layout_to_set; |
| 164 execute_queue_ = std::queue<std::string>(); // clear the queue. |
| 165 return; |
| 166 } |
| 167 |
| 168 PollUntilChildFinish(handle); |
| 169 |
| 170 DVLOG(1) << "ExecuteSetLayoutCommand: " << layout_to_set |
| 171 << ": pid=" << base::GetProcId(handle); |
| 172 } |
| 173 |
| 174 // Delay and loop until child process finishes and call the callback. |
| 175 void ImeKeyboardX11::PollUntilChildFinish(const base::ProcessHandle handle) { |
| 176 int exit_code; |
| 177 DVLOG(1) << "PollUntilChildFinish: poll for pid=" << base::GetProcId(handle); |
| 178 switch (base::GetTerminationStatus(handle, &exit_code)) { |
| 179 case base::TERMINATION_STATUS_STILL_RUNNING: |
| 180 DVLOG(1) << "PollUntilChildFinish: Try waiting again"; |
| 181 base::MessageLoop::current()->PostDelayedTask( |
| 182 FROM_HERE, |
| 183 base::Bind(&ImeKeyboardX11::PollUntilChildFinish, |
| 184 weak_factory_.GetWeakPtr(), |
| 185 handle), |
| 186 base::TimeDelta::FromMilliseconds(kSetLayoutCommandCheckDelayMs)); |
| 187 return; |
| 188 |
| 189 case base::TERMINATION_STATUS_NORMAL_TERMINATION: |
| 190 DVLOG(1) << "PollUntilChildFinish: Child process finished"; |
| 191 OnSetLayoutFinish(); |
| 192 return; |
| 193 |
| 194 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION: |
| 195 DVLOG(1) << "PollUntilChildFinish: Abnormal exit code: " << exit_code; |
| 196 OnSetLayoutFinish(); |
| 197 return; |
| 198 |
| 199 default: |
| 200 NOTIMPLEMENTED(); |
| 201 OnSetLayoutFinish(); |
| 202 return; |
| 203 } |
| 204 } |
| 205 |
| 206 bool ImeKeyboardX11::CapsLockIsEnabled() { |
| 207 DCHECK(thread_checker_.CalledOnValidThread()); |
| 208 XkbStateRec status; |
| 209 XkbGetState(gfx::GetXDisplay(), XkbUseCoreKbd, &status); |
| 210 return (status.locked_mods & LockMask); |
| 211 } |
| 212 |
| 213 bool ImeKeyboardX11::SetAutoRepeatEnabled(bool enabled) { |
| 214 if (enabled) |
| 215 XAutoRepeatOn(gfx::GetXDisplay()); |
| 216 else |
| 217 XAutoRepeatOff(gfx::GetXDisplay()); |
| 218 DVLOG(1) << "Set auto-repeat mode to: " << (enabled ? "on" : "off"); |
| 219 return true; |
| 220 } |
| 221 |
| 222 bool ImeKeyboardX11::SetAutoRepeatRate(const AutoRepeatRate& rate) { |
| 223 DVLOG(1) << "Set auto-repeat rate to: " |
| 224 << rate.initial_delay_in_ms << " ms delay, " |
| 225 << rate.repeat_interval_in_ms << " ms interval"; |
| 226 if (XkbSetAutoRepeatRate(gfx::GetXDisplay(), XkbUseCoreKbd, |
| 227 rate.initial_delay_in_ms, |
| 228 rate.repeat_interval_in_ms) != True) { |
| 229 DVLOG(1) << "Failed to set auto-repeat rate"; |
| 230 return false; |
| 231 } |
| 232 return true; |
| 233 } |
| 234 |
| 235 void ImeKeyboardX11::SetCapsLockEnabled(bool enable_caps_lock) { |
| 236 ImeKeyboard::SetCapsLockEnabled(enable_caps_lock); |
| 237 SetLockedModifiers(); |
| 238 } |
| 239 |
| 240 bool ImeKeyboardX11::SetCurrentKeyboardLayoutByName( |
| 241 const std::string& layout_name) { |
| 242 if (SetLayoutInternal(layout_name, false)) { |
| 243 last_layout_ = layout_name; |
| 244 return true; |
| 245 } |
| 246 return false; |
| 247 } |
| 248 |
| 249 bool ImeKeyboardX11::ReapplyCurrentKeyboardLayout() { |
| 250 if (last_layout_.empty()) { |
| 251 DVLOG(1) << "Can't reapply XKB layout: layout unknown"; |
| 252 return false; |
| 253 } |
| 254 return SetLayoutInternal(last_layout_, true /* force */); |
| 255 } |
| 256 |
| 257 void ImeKeyboardX11::ReapplyCurrentModifierLockStatus() { |
| 258 SetLockedModifiers(); |
| 259 } |
| 260 |
| 261 void ImeKeyboardX11::DisableNumLock() { |
| 262 SetCapsLockEnabled(caps_lock_is_enabled_); |
| 263 } |
| 264 |
| 265 void ImeKeyboardX11::OnSetLayoutFinish() { |
| 266 if (execute_queue_.empty()) { |
| 267 DVLOG(1) << "OnSetLayoutFinish: execute_queue_ is empty. " |
| 268 << "base::LaunchProcess failed?"; |
| 269 return; |
| 270 } |
| 271 execute_queue_.pop(); |
| 272 MaybeExecuteSetLayoutCommand(); |
| 273 } |
| 274 |
| 275 // static |
| 276 bool ImeKeyboard::GetAutoRepeatEnabledForTesting() { |
| 277 XKeyboardState state = {}; |
| 278 XGetKeyboardControl(gfx::GetXDisplay(), &state); |
| 279 return state.global_auto_repeat != AutoRepeatModeOff; |
| 280 } |
| 281 |
| 282 // static |
| 283 bool ImeKeyboard::GetAutoRepeatRateForTesting(AutoRepeatRate* out_rate) { |
| 284 return XkbGetAutoRepeatRate(gfx::GetXDisplay(), |
| 285 XkbUseCoreKbd, |
| 286 &(out_rate->initial_delay_in_ms), |
| 287 &(out_rate->repeat_interval_in_ms)) == True; |
| 288 } |
| 289 |
| 290 // static |
| 291 bool ImeKeyboard::CheckLayoutNameForTesting(const std::string& layout_name) { |
| 292 return CheckLayoutName(layout_name); |
| 293 } |
| 294 |
| 295 // static |
| 296 ImeKeyboard* ImeKeyboard::Create() { return new ImeKeyboardX11(); } |
| 297 |
| 298 } // namespace input_method |
| 299 } // namespace chromeos |
OLD | NEW |