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