This source file includes following definitions.
- GetXDisplay
- CheckLayoutName
- weak_factory_
- GetNumLockMask
- SetLockedModifiers
- SetLayoutInternal
- MaybeExecuteSetLayoutCommand
- PollUntilChildFinish
- CapsLockIsEnabled
- IsISOLevel5ShiftAvailable
- IsAltGrAvailable
- SetAutoRepeatEnabled
- SetAutoRepeatRate
- SetCapsLockEnabled
- SetCurrentKeyboardLayoutByName
- ReapplyCurrentKeyboardLayout
- ReapplyCurrentModifierLockStatus
- DisableNumLock
- OnSetLayoutFinish
- GetAutoRepeatEnabledForTesting
- GetAutoRepeatRateForTesting
- CheckLayoutNameForTesting
- Create
#include "chromeos/ime/xkeyboard.h"
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
#include <utility>
#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/process/kill.h"
#include "base/process/launch.h"
#include "base/process/process_handle.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/sys_info.h"
#include "base/threading/thread_checker.h"
#include <X11/XKBlib.h>
#include <X11/Xlib.h>
namespace chromeos {
namespace input_method {
namespace {
Display* GetXDisplay() {
return base::MessagePumpForUI::GetDefaultXDisplay();
}
const int kSetLayoutCommandCheckDelayMs = 100;
const char kSetxkbmapCommand[] = "/usr/bin/setxkbmap";
const char kNumLockVirtualModifierString[] = "NumLock";
const char *kISOLevel5ShiftLayoutIds[] = {
"ca(multix)",
"de(neo)",
};
const char *kAltGrLayoutIds[] = {
"be",
"be",
"be",
"bg",
"bg(phonetic)",
"br",
"ca",
"ca(eng)",
"ca(multix)",
"ch",
"ch(fr)",
"cz",
"de",
"de(neo)",
"dk",
"ee",
"es",
"es(cat)",
"fi",
"fr",
"gb(dvorak)",
"gb(extd)",
"gr",
"hr",
"il",
"it",
"latam",
"lt",
"no",
"pl",
"pt",
"ro",
"se",
"si",
"sk",
"tr",
"ua",
"us(altgr-intl)",
"us(intl)",
};
bool CheckLayoutName(const std::string& layout_name) {
static const char kValidLayoutNameCharacters[] =
"abcdefghijklmnopqrstuvwxyz0123456789()-_";
if (layout_name.empty()) {
DVLOG(1) << "Invalid layout_name: " << layout_name;
return false;
}
if (layout_name.find_first_not_of(kValidLayoutNameCharacters) !=
std::string::npos) {
DVLOG(1) << "Invalid layout_name: " << layout_name;
return false;
}
return true;
}
class XKeyboardImpl : public XKeyboard {
public:
XKeyboardImpl();
virtual ~XKeyboardImpl() {}
virtual bool SetCurrentKeyboardLayoutByName(
const std::string& layout_name) OVERRIDE;
virtual bool ReapplyCurrentKeyboardLayout() OVERRIDE;
virtual void ReapplyCurrentModifierLockStatus() OVERRIDE;
virtual void DisableNumLock() OVERRIDE;
virtual void SetCapsLockEnabled(bool enable_caps_lock) OVERRIDE;
virtual bool CapsLockIsEnabled() OVERRIDE;
virtual bool IsISOLevel5ShiftAvailable() const OVERRIDE;
virtual bool IsAltGrAvailable() const OVERRIDE;
virtual bool SetAutoRepeatEnabled(bool enabled) OVERRIDE;
virtual bool SetAutoRepeatRate(const AutoRepeatRate& rate) OVERRIDE;
private:
unsigned int GetNumLockMask();
void SetLockedModifiers(bool caps_lock_enabled);
bool SetLayoutInternal(const std::string& layout_name, bool force);
void MaybeExecuteSetLayoutCommand();
void PollUntilChildFinish(const base::ProcessHandle handle);
void OnSetLayoutFinish();
const bool is_running_on_chrome_os_;
unsigned int num_lock_mask_;
bool current_caps_lock_status_;
std::string current_layout_name_;
std::queue<std::string> execute_queue_;
base::ThreadChecker thread_checker_;
base::WeakPtrFactory<XKeyboardImpl> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(XKeyboardImpl);
};
XKeyboardImpl::XKeyboardImpl()
: is_running_on_chrome_os_(base::SysInfo::IsRunningOnChromeOS()),
weak_factory_(this) {
CHECK(GetXDisplay());
num_lock_mask_ = GetNumLockMask();
if (is_running_on_chrome_os_) {
LOG_IF(ERROR, num_lock_mask_ != Mod2Mask)
<< "NumLock is not assigned to Mod2Mask. : " << num_lock_mask_;
}
current_caps_lock_status_ = CapsLockIsEnabled();
}
unsigned int XKeyboardImpl::GetNumLockMask() {
DCHECK(thread_checker_.CalledOnValidThread());
static const unsigned int kBadMask = 0;
unsigned int real_mask = kBadMask;
XkbDescPtr xkb_desc =
XkbGetKeyboard(GetXDisplay(), XkbAllComponentsMask, XkbUseCoreKbd);
if (!xkb_desc)
return kBadMask;
if (xkb_desc->dpy && xkb_desc->names) {
const std::string string_to_find(kNumLockVirtualModifierString);
for (size_t i = 0; i < XkbNumVirtualMods; ++i) {
const unsigned int virtual_mod_mask = 1U << i;
char* virtual_mod_str_raw_ptr =
XGetAtomName(xkb_desc->dpy, xkb_desc->names->vmods[i]);
if (!virtual_mod_str_raw_ptr)
continue;
const std::string virtual_mod_str = virtual_mod_str_raw_ptr;
XFree(virtual_mod_str_raw_ptr);
if (string_to_find == virtual_mod_str) {
if (!XkbVirtualModsToReal(xkb_desc, virtual_mod_mask, &real_mask)) {
DVLOG(1) << "XkbVirtualModsToReal failed";
real_mask = kBadMask;
}
break;
}
}
}
XkbFreeKeyboard(xkb_desc, 0, True );
return real_mask;
}
void XKeyboardImpl::SetLockedModifiers(bool caps_lock_enabled) {
DCHECK(thread_checker_.CalledOnValidThread());
unsigned int affect_mask = num_lock_mask_;
unsigned int value_mask = 0;
affect_mask |= LockMask;
value_mask |= (caps_lock_enabled ? LockMask : 0);
current_caps_lock_status_ = caps_lock_enabled;
XkbLockModifiers(GetXDisplay(), XkbUseCoreKbd, affect_mask, value_mask);
}
bool XKeyboardImpl::SetLayoutInternal(const std::string& layout_name,
bool force) {
if (!is_running_on_chrome_os_) {
return true;
}
if (!CheckLayoutName(layout_name))
return false;
if (!force && (current_layout_name_ == layout_name)) {
DVLOG(1) << "The requested layout is already set: " << layout_name;
return true;
}
DVLOG(1) << (force ? "Reapply" : "Set") << " layout: " << layout_name;
const bool start_execution = execute_queue_.empty();
execute_queue_.push(layout_name);
if (start_execution)
MaybeExecuteSetLayoutCommand();
return true;
}
void XKeyboardImpl::MaybeExecuteSetLayoutCommand() {
if (execute_queue_.empty())
return;
const std::string layout_to_set = execute_queue_.front();
std::vector<std::string> argv;
base::ProcessHandle handle = base::kNullProcessHandle;
argv.push_back(kSetxkbmapCommand);
argv.push_back("-layout");
argv.push_back(layout_to_set);
argv.push_back("-synch");
if (!base::LaunchProcess(argv, base::LaunchOptions(), &handle)) {
DVLOG(1) << "Failed to execute setxkbmap: " << layout_to_set;
execute_queue_ = std::queue<std::string>();
return;
}
PollUntilChildFinish(handle);
DVLOG(1) << "ExecuteSetLayoutCommand: "
<< layout_to_set << ": pid=" << base::GetProcId(handle);
}
void XKeyboardImpl::PollUntilChildFinish(const base::ProcessHandle handle) {
int exit_code;
DVLOG(1) << "PollUntilChildFinish: poll for pid=" << base::GetProcId(handle);
switch (base::GetTerminationStatus(handle, &exit_code)) {
case base::TERMINATION_STATUS_STILL_RUNNING:
DVLOG(1) << "PollUntilChildFinish: Try waiting again";
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&XKeyboardImpl::PollUntilChildFinish,
weak_factory_.GetWeakPtr(), handle),
base::TimeDelta::FromMilliseconds(kSetLayoutCommandCheckDelayMs));
return;
case base::TERMINATION_STATUS_NORMAL_TERMINATION:
DVLOG(1) << "PollUntilChildFinish: Child process finished";
OnSetLayoutFinish();
return;
case base::TERMINATION_STATUS_ABNORMAL_TERMINATION:
DVLOG(1) << "PollUntilChildFinish: Abnormal exit code: " << exit_code;
OnSetLayoutFinish();
return;
default:
NOTIMPLEMENTED();
OnSetLayoutFinish();
return;
}
}
bool XKeyboardImpl::CapsLockIsEnabled() {
DCHECK(thread_checker_.CalledOnValidThread());
XkbStateRec status;
XkbGetState(GetXDisplay(), XkbUseCoreKbd, &status);
return (status.locked_mods & LockMask);
}
bool XKeyboardImpl::IsISOLevel5ShiftAvailable() const {
for (size_t i = 0; i < arraysize(kISOLevel5ShiftLayoutIds); ++i) {
if (current_layout_name_ == kISOLevel5ShiftLayoutIds[i])
return true;
}
return false;
}
bool XKeyboardImpl::IsAltGrAvailable() const {
for (size_t i = 0; i < arraysize(kAltGrLayoutIds); ++i) {
if (current_layout_name_ == kAltGrLayoutIds[i])
return true;
}
return false;
}
bool XKeyboardImpl::SetAutoRepeatEnabled(bool enabled) {
if (enabled)
XAutoRepeatOn(GetXDisplay());
else
XAutoRepeatOff(GetXDisplay());
DVLOG(1) << "Set auto-repeat mode to: " << (enabled ? "on" : "off");
return true;
}
bool XKeyboardImpl::SetAutoRepeatRate(const AutoRepeatRate& rate) {
DVLOG(1) << "Set auto-repeat rate to: "
<< rate.initial_delay_in_ms << " ms delay, "
<< rate.repeat_interval_in_ms << " ms interval";
if (XkbSetAutoRepeatRate(GetXDisplay(), XkbUseCoreKbd,
rate.initial_delay_in_ms,
rate.repeat_interval_in_ms) != True) {
DVLOG(1) << "Failed to set auto-repeat rate";
return false;
}
return true;
}
void XKeyboardImpl::SetCapsLockEnabled(bool enable_caps_lock) {
SetLockedModifiers(enable_caps_lock);
}
bool XKeyboardImpl::SetCurrentKeyboardLayoutByName(
const std::string& layout_name) {
if (SetLayoutInternal(layout_name, false)) {
current_layout_name_ = layout_name;
return true;
}
return false;
}
bool XKeyboardImpl::ReapplyCurrentKeyboardLayout() {
if (current_layout_name_.empty()) {
DVLOG(1) << "Can't reapply XKB layout: layout unknown";
return false;
}
return SetLayoutInternal(current_layout_name_, true );
}
void XKeyboardImpl::ReapplyCurrentModifierLockStatus() {
SetLockedModifiers(current_caps_lock_status_);
}
void XKeyboardImpl::DisableNumLock() {
SetCapsLockEnabled(current_caps_lock_status_);
}
void XKeyboardImpl::OnSetLayoutFinish() {
if (execute_queue_.empty()) {
DVLOG(1) << "OnSetLayoutFinish: execute_queue_ is empty. "
<< "base::LaunchProcess failed?";
return;
}
execute_queue_.pop();
MaybeExecuteSetLayoutCommand();
}
}
bool XKeyboard::GetAutoRepeatEnabledForTesting() {
XKeyboardState state = {};
XGetKeyboardControl(GetXDisplay(), &state);
return state.global_auto_repeat != AutoRepeatModeOff;
}
bool XKeyboard::GetAutoRepeatRateForTesting(AutoRepeatRate* out_rate) {
return XkbGetAutoRepeatRate(GetXDisplay(), XkbUseCoreKbd,
&(out_rate->initial_delay_in_ms),
&(out_rate->repeat_interval_in_ms)) == True;
}
bool XKeyboard::CheckLayoutNameForTesting(const std::string& layout_name) {
return CheckLayoutName(layout_name);
}
XKeyboard* XKeyboard::Create() {
return new XKeyboardImpl();
}
}
}