1 // Copyright 2013 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.
5 #include "chromeos/ime/xkeyboard.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/process/launch.h"
17 #include "base/process/process_handle.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/sys_info.h"
21 #include "base/threading/thread_checker.h"
23 // These includes conflict with base/tracked_objects.h so must come last.
24 #include <X11/XKBlib.h>
29 namespace input_method {
32 Display* GetXDisplay() {
33 return base::MessagePumpForUI::GetDefaultXDisplay();
36 // The command we use to set the current XKB layout and modifier key mapping.
37 // TODO(yusukes): Use libxkbfile.so instead of the command (crosbug.com/13105)
38 const char kSetxkbmapCommand[] = "/usr/bin/setxkbmap";
40 // A string for obtaining a mask value for Num Lock.
41 const char kNumLockVirtualModifierString[] = "NumLock";
43 // Returns false if |layout_name| contains a bad character.
44 bool CheckLayoutName(const std::string& layout_name) {
45 static const char kValidLayoutNameCharacters[] =
46 "abcdefghijklmnopqrstuvwxyz0123456789()-_";
48 if (layout_name.empty()) {
49 DVLOG(1) << "Invalid layout_name: " << layout_name;
53 if (layout_name.find_first_not_of(kValidLayoutNameCharacters) !=
55 DVLOG(1) << "Invalid layout_name: " << layout_name;
62 class XKeyboardImpl : public XKeyboard {
65 virtual ~XKeyboardImpl() {}
67 // Overridden from XKeyboard:
68 virtual bool SetCurrentKeyboardLayoutByName(
69 const std::string& layout_name) OVERRIDE;
70 virtual bool ReapplyCurrentKeyboardLayout() OVERRIDE;
71 virtual void ReapplyCurrentModifierLockStatus() OVERRIDE;
72 virtual void SetLockedModifiers(
73 ModifierLockStatus new_caps_lock_status,
74 ModifierLockStatus new_num_lock_status) OVERRIDE;
75 virtual void SetNumLockEnabled(bool enable_num_lock) OVERRIDE;
76 virtual void SetCapsLockEnabled(bool enable_caps_lock) OVERRIDE;
77 virtual bool NumLockIsEnabled() OVERRIDE;
78 virtual bool CapsLockIsEnabled() OVERRIDE;
79 virtual unsigned int GetNumLockMask() OVERRIDE;
80 virtual void GetLockedModifiers(bool* out_caps_lock_enabled,
81 bool* out_num_lock_enabled) OVERRIDE;
84 // This function is used by SetLayout() and RemapModifierKeys(). Calls
85 // setxkbmap command if needed, and updates the last_full_layout_name_ cache.
86 bool SetLayoutInternal(const std::string& layout_name, bool force);
88 // Executes 'setxkbmap -layout ...' command asynchronously using a layout name
89 // in the |execute_queue_|. Do nothing if the queue is empty.
90 // TODO(yusukes): Use libxkbfile.so instead of the command (crosbug.com/13105)
91 void MaybeExecuteSetLayoutCommand();
93 // Called when execve'd setxkbmap process exits.
94 static void OnSetLayoutFinish(pid_t pid, int status, XKeyboardImpl* self);
96 const bool is_running_on_chrome_os_;
97 unsigned int num_lock_mask_;
99 // The current Num Lock and Caps Lock status. If true, enabled.
100 bool current_num_lock_status_;
101 bool current_caps_lock_status_;
102 // The XKB layout name which we set last time like "us" and "us(dvorak)".
103 std::string current_layout_name_;
105 // A queue for executing setxkbmap one by one.
106 std::queue<std::string> execute_queue_;
108 base::ThreadChecker thread_checker_;
110 DISALLOW_COPY_AND_ASSIGN(XKeyboardImpl);
113 XKeyboardImpl::XKeyboardImpl()
114 : is_running_on_chrome_os_(base::SysInfo::IsRunningOnChromeOS()) {
115 // X must be already initialized.
116 CHECK(GetXDisplay());
118 num_lock_mask_ = GetNumLockMask();
120 if (is_running_on_chrome_os_) {
121 // Some code seems to assume that Mod2Mask is always assigned to
124 // TODO(yusukes): Check the assumption is really okay. If not,
125 // modify the Aura code, and then remove the CHECK below.
126 LOG_IF(ERROR, num_lock_mask_ != Mod2Mask)
127 << "NumLock is not assigned to Mod2Mask. : " << num_lock_mask_;
129 GetLockedModifiers(¤t_caps_lock_status_, ¤t_num_lock_status_);
132 bool XKeyboardImpl::SetLayoutInternal(const std::string& layout_name,
134 if (!is_running_on_chrome_os_) {
135 // We should not try to change a layout on Linux or inside ui_tests. Just
140 if (!CheckLayoutName(layout_name))
143 if (!force && (current_layout_name_ == layout_name)) {
144 DVLOG(1) << "The requested layout is already set: " << layout_name;
148 DVLOG(1) << (force ? "Reapply" : "Set") << " layout: " << layout_name;
150 const bool start_execution = execute_queue_.empty();
151 // If no setxkbmap command is in flight (i.e. start_execution is true),
152 // start the first one by explicitly calling MaybeExecuteSetLayoutCommand().
153 // If one or more setxkbmap commands are already in flight, just push the
154 // layout name to the queue. setxkbmap command for the layout will be called
155 // via OnSetLayoutFinish() callback later.
156 execute_queue_.push(layout_name);
158 MaybeExecuteSetLayoutCommand();
163 // Executes 'setxkbmap -layout ...' command asynchronously using a layout name
164 // in the |execute_queue_|. Do nothing if the queue is empty.
165 // TODO(yusukes): Use libxkbfile.so instead of the command (crosbug.com/13105)
166 void XKeyboardImpl::MaybeExecuteSetLayoutCommand() {
167 if (execute_queue_.empty())
169 const std::string layout_to_set = execute_queue_.front();
171 std::vector<std::string> argv;
172 base::ProcessHandle handle = base::kNullProcessHandle;
174 argv.push_back(kSetxkbmapCommand);
175 argv.push_back("-layout");
176 argv.push_back(layout_to_set);
177 argv.push_back("-synch");
179 if (!base::LaunchProcess(argv, base::LaunchOptions(), &handle)) {
180 DVLOG(1) << "Failed to execute setxkbmap: " << layout_to_set;
181 execute_queue_ = std::queue<std::string>(); // clear the queue.
185 // g_child_watch_add is necessary to prevent the process from becoming a
187 const base::ProcessId pid = base::GetProcId(handle);
188 g_child_watch_add(pid,
189 reinterpret_cast<GChildWatchFunc>(OnSetLayoutFinish),
191 DVLOG(1) << "ExecuteSetLayoutCommand: " << layout_to_set << ": pid=" << pid;
194 bool XKeyboardImpl::NumLockIsEnabled() {
195 bool num_lock_enabled = false;
196 GetLockedModifiers(NULL /* Caps Lock */, &num_lock_enabled);
197 return num_lock_enabled;
200 bool XKeyboardImpl::CapsLockIsEnabled() {
201 bool caps_lock_enabled = false;
202 GetLockedModifiers(&caps_lock_enabled, NULL /* Num Lock */);
203 return caps_lock_enabled;
206 unsigned int XKeyboardImpl::GetNumLockMask() {
207 DCHECK(thread_checker_.CalledOnValidThread());
208 static const unsigned int kBadMask = 0;
210 unsigned int real_mask = kBadMask;
211 XkbDescPtr xkb_desc =
212 XkbGetKeyboard(GetXDisplay(), XkbAllComponentsMask, XkbUseCoreKbd);
216 if (xkb_desc->dpy && xkb_desc->names && xkb_desc->names->vmods) {
217 const std::string string_to_find(kNumLockVirtualModifierString);
218 for (size_t i = 0; i < XkbNumVirtualMods; ++i) {
219 const unsigned int virtual_mod_mask = 1U << i;
220 char* virtual_mod_str_raw_ptr =
221 XGetAtomName(xkb_desc->dpy, xkb_desc->names->vmods[i]);
222 if (!virtual_mod_str_raw_ptr)
224 const std::string virtual_mod_str = virtual_mod_str_raw_ptr;
225 XFree(virtual_mod_str_raw_ptr);
227 if (string_to_find == virtual_mod_str) {
228 if (!XkbVirtualModsToReal(xkb_desc, virtual_mod_mask, &real_mask)) {
229 DVLOG(1) << "XkbVirtualModsToReal failed";
230 real_mask = kBadMask; // reset the return value, just in case.
236 XkbFreeKeyboard(xkb_desc, 0, True /* free all components */);
240 void XKeyboardImpl::GetLockedModifiers(bool* out_caps_lock_enabled,
241 bool* out_num_lock_enabled) {
242 DCHECK(thread_checker_.CalledOnValidThread());
244 if (out_num_lock_enabled && !num_lock_mask_) {
245 DVLOG(1) << "Cannot get locked modifiers. Num Lock mask unknown.";
246 if (out_caps_lock_enabled)
247 *out_caps_lock_enabled = false;
248 if (out_num_lock_enabled)
249 *out_num_lock_enabled = false;
254 XkbGetState(GetXDisplay(), XkbUseCoreKbd, &status);
255 if (out_caps_lock_enabled)
256 *out_caps_lock_enabled = status.locked_mods & LockMask;
257 if (out_num_lock_enabled)
258 *out_num_lock_enabled = status.locked_mods & num_lock_mask_;
261 void XKeyboardImpl::SetLockedModifiers(ModifierLockStatus new_caps_lock_status,
262 ModifierLockStatus new_num_lock_status) {
263 DCHECK(thread_checker_.CalledOnValidThread());
264 if (!num_lock_mask_) {
265 DVLOG(1) << "Cannot set locked modifiers. Num Lock mask unknown.";
269 unsigned int affect_mask = 0;
270 unsigned int value_mask = 0;
271 if (new_caps_lock_status != kDontChange) {
272 affect_mask |= LockMask;
273 value_mask |= ((new_caps_lock_status == kEnableLock) ? LockMask : 0);
274 current_caps_lock_status_ = (new_caps_lock_status == kEnableLock);
276 if (new_num_lock_status != kDontChange) {
277 affect_mask |= num_lock_mask_;
278 value_mask |= ((new_num_lock_status == kEnableLock) ? num_lock_mask_ : 0);
279 current_num_lock_status_ = (new_num_lock_status == kEnableLock);
283 XkbLockModifiers(GetXDisplay(), XkbUseCoreKbd, affect_mask, value_mask);
286 void XKeyboardImpl::SetNumLockEnabled(bool enable_num_lock) {
288 kDontChange, enable_num_lock ? kEnableLock : kDisableLock);
291 void XKeyboardImpl::SetCapsLockEnabled(bool enable_caps_lock) {
293 enable_caps_lock ? kEnableLock : kDisableLock, kDontChange);
296 bool XKeyboardImpl::SetCurrentKeyboardLayoutByName(
297 const std::string& layout_name) {
298 if (SetLayoutInternal(layout_name, false)) {
299 current_layout_name_ = layout_name;
305 bool XKeyboardImpl::ReapplyCurrentKeyboardLayout() {
306 if (current_layout_name_.empty()) {
307 DVLOG(1) << "Can't reapply XKB layout: layout unknown";
310 return SetLayoutInternal(current_layout_name_, true /* force */);
313 void XKeyboardImpl::ReapplyCurrentModifierLockStatus() {
314 SetLockedModifiers(current_caps_lock_status_ ? kEnableLock : kDisableLock,
315 current_num_lock_status_ ? kEnableLock : kDisableLock);
319 void XKeyboardImpl::OnSetLayoutFinish(pid_t pid,
321 XKeyboardImpl* self) {
322 DVLOG(1) << "OnSetLayoutFinish: pid=" << pid;
323 if (self->execute_queue_.empty()) {
324 DVLOG(1) << "OnSetLayoutFinish: execute_queue_ is empty. "
325 << "base::LaunchProcess failed? pid=" << pid;
328 self->execute_queue_.pop();
329 self->MaybeExecuteSetLayoutCommand();
335 bool XKeyboard::SetAutoRepeatEnabled(bool enabled) {
337 XAutoRepeatOn(GetXDisplay());
339 XAutoRepeatOff(GetXDisplay());
340 DVLOG(1) << "Set auto-repeat mode to: " << (enabled ? "on" : "off");
345 bool XKeyboard::SetAutoRepeatRate(const AutoRepeatRate& rate) {
346 DVLOG(1) << "Set auto-repeat rate to: "
347 << rate.initial_delay_in_ms << " ms delay, "
348 << rate.repeat_interval_in_ms << " ms interval";
349 if (XkbSetAutoRepeatRate(GetXDisplay(), XkbUseCoreKbd,
350 rate.initial_delay_in_ms,
351 rate.repeat_interval_in_ms) != True) {
352 DVLOG(1) << "Failed to set auto-repeat rate";
359 bool XKeyboard::GetAutoRepeatEnabledForTesting() {
360 XKeyboardState state = {};
361 XGetKeyboardControl(GetXDisplay(), &state);
362 return state.global_auto_repeat != AutoRepeatModeOff;
366 bool XKeyboard::GetAutoRepeatRateForTesting(AutoRepeatRate* out_rate) {
367 return XkbGetAutoRepeatRate(GetXDisplay(), XkbUseCoreKbd,
368 &(out_rate->initial_delay_in_ms),
369 &(out_rate->repeat_interval_in_ms)) == True;
373 bool XKeyboard::CheckLayoutNameForTesting(const std::string& layout_name) {
374 return CheckLayoutName(layout_name);
378 XKeyboard* XKeyboard::Create() {
379 return new XKeyboardImpl();
382 } // namespace input_method
383 } // namespace chromeos