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