OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/chromeos/input_method/xkeyboard.h" | 5 #include "chrome/browser/chromeos/input_method/xkeyboard.h" |
6 | 6 |
7 #include <cstdlib> | 7 #include <cstdlib> |
8 #include <cstring> | 8 #include <cstring> |
9 #include <queue> | 9 #include <queue> |
10 #include <set> | 10 #include <set> |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 | 88 |
89 // Returns true if the XKB layout uses the right Alt key for special purposes | 89 // Returns true if the XKB layout uses the right Alt key for special purposes |
90 // like AltGr. | 90 // like AltGr. |
91 bool KeepRightAlt(const std::string& xkb_layout_name) const; | 91 bool KeepRightAlt(const std::string& xkb_layout_name) const; |
92 | 92 |
93 // Returns true if the XKB layout uses the CapsLock key for special purposes. | 93 // Returns true if the XKB layout uses the CapsLock key for special purposes. |
94 // For example, since US Colemak layout uses the key as back space, | 94 // For example, since US Colemak layout uses the key as back space, |
95 // KeepCapsLock("us(colemak)") would return true. | 95 // KeepCapsLock("us(colemak)") would return true. |
96 bool KeepCapsLock(const std::string& xkb_layout_name) const; | 96 bool KeepCapsLock(const std::string& xkb_layout_name) const; |
97 | 97 |
| 98 // Returns true if the current thread is the UI thread, or the process is |
| 99 // running on Linux. |
| 100 bool CurrentlyOnUIThread() const; |
| 101 |
98 // Converts |key| to a modifier key name which is used in | 102 // Converts |key| to a modifier key name which is used in |
99 // /usr/share/X11/xkb/symbols/chromeos. | 103 // /usr/share/X11/xkb/symbols/chromeos. |
100 static std::string ModifierKeyToString(ModifierKey key); | 104 static std::string ModifierKeyToString(ModifierKey key); |
101 | 105 |
102 // Called when execve'd setxkbmap process exits. | 106 // Called when execve'd setxkbmap process exits. |
103 static void OnSetLayoutFinish(pid_t pid, int status, XKeyboardImpl* self); | 107 static void OnSetLayoutFinish(pid_t pid, int status, XKeyboardImpl* self); |
104 | 108 |
105 const bool is_running_on_chrome_os_; | 109 const bool is_running_on_chrome_os_; |
106 unsigned int num_lock_mask_; | 110 unsigned int num_lock_mask_; |
107 | 111 |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 return num_lock_enabled; | 248 return num_lock_enabled; |
245 } | 249 } |
246 | 250 |
247 bool XKeyboardImpl::CapsLockIsEnabled() { | 251 bool XKeyboardImpl::CapsLockIsEnabled() { |
248 bool caps_lock_enabled = false; | 252 bool caps_lock_enabled = false; |
249 GetLockedModifiers(&caps_lock_enabled, NULL /* Num Lock */); | 253 GetLockedModifiers(&caps_lock_enabled, NULL /* Num Lock */); |
250 return caps_lock_enabled; | 254 return caps_lock_enabled; |
251 } | 255 } |
252 | 256 |
253 unsigned int XKeyboardImpl::GetNumLockMask() { | 257 unsigned int XKeyboardImpl::GetNumLockMask() { |
| 258 CHECK(CurrentlyOnUIThread()); |
254 static const unsigned int kBadMask = 0; | 259 static const unsigned int kBadMask = 0; |
255 | 260 |
256 unsigned int real_mask = kBadMask; | 261 unsigned int real_mask = kBadMask; |
257 XkbDescPtr xkb_desc = | 262 XkbDescPtr xkb_desc = |
258 XkbGetKeyboard(ui::GetXDisplay(), XkbAllComponentsMask, XkbUseCoreKbd); | 263 XkbGetKeyboard(ui::GetXDisplay(), XkbAllComponentsMask, XkbUseCoreKbd); |
259 if (!xkb_desc) { | 264 if (!xkb_desc) { |
260 return kBadMask; | 265 return kBadMask; |
261 } | 266 } |
262 | 267 |
263 if (xkb_desc->dpy && xkb_desc->names && xkb_desc->names->vmods) { | 268 if (xkb_desc->dpy && xkb_desc->names && xkb_desc->names->vmods) { |
(...skipping 12 matching lines...) Expand all Loading... |
276 break; | 281 break; |
277 } | 282 } |
278 } | 283 } |
279 } | 284 } |
280 XkbFreeKeyboard(xkb_desc, 0, True /* free all components */); | 285 XkbFreeKeyboard(xkb_desc, 0, True /* free all components */); |
281 return real_mask; | 286 return real_mask; |
282 } | 287 } |
283 | 288 |
284 void XKeyboardImpl::GetLockedModifiers(bool* out_caps_lock_enabled, | 289 void XKeyboardImpl::GetLockedModifiers(bool* out_caps_lock_enabled, |
285 bool* out_num_lock_enabled) { | 290 bool* out_num_lock_enabled) { |
286 // For now, don't call CHECK() here to make | 291 CHECK(CurrentlyOnUIThread()); |
287 // TabRestoreServiceTest.DontRestorePrintPreviewTab test happy. | |
288 // TODO(yusukes): Fix the test, then fix the if(!BrowserThread...) line below. | |
289 // CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
290 | 292 |
291 if (!BrowserThread::CurrentlyOn(BrowserThread::UI) || | 293 if (out_num_lock_enabled && !num_lock_mask_) { |
292 (out_num_lock_enabled && !num_lock_mask_)) { | |
293 VLOG(1) << "Cannot get locked modifiers. Num Lock mask unknown."; | 294 VLOG(1) << "Cannot get locked modifiers. Num Lock mask unknown."; |
294 if (out_caps_lock_enabled) { | 295 if (out_caps_lock_enabled) { |
295 *out_caps_lock_enabled = false; | 296 *out_caps_lock_enabled = false; |
296 } | 297 } |
297 if (out_num_lock_enabled) { | 298 if (out_num_lock_enabled) { |
298 *out_num_lock_enabled = false; | 299 *out_num_lock_enabled = false; |
299 } | 300 } |
300 return; | 301 return; |
301 } | 302 } |
302 | 303 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 (full_xkb_layout_name.substr(0, 3) != "us(")) { | 384 (full_xkb_layout_name.substr(0, 3) != "us(")) { |
384 full_xkb_layout_name += ",us"; | 385 full_xkb_layout_name += ",us"; |
385 } | 386 } |
386 #endif | 387 #endif |
387 | 388 |
388 return full_xkb_layout_name; | 389 return full_xkb_layout_name; |
389 } | 390 } |
390 | 391 |
391 void XKeyboardImpl::SetLockedModifiers(ModifierLockStatus new_caps_lock_status, | 392 void XKeyboardImpl::SetLockedModifiers(ModifierLockStatus new_caps_lock_status, |
392 ModifierLockStatus new_num_lock_status) { | 393 ModifierLockStatus new_num_lock_status) { |
393 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 394 CHECK(CurrentlyOnUIThread()); |
394 if (!num_lock_mask_) { | 395 if (!num_lock_mask_) { |
395 LOG(ERROR) << "Cannot set locked modifiers. Num Lock mask unknown."; | 396 LOG(ERROR) << "Cannot set locked modifiers. Num Lock mask unknown."; |
396 return; | 397 return; |
397 } | 398 } |
398 | 399 |
399 unsigned int affect_mask = 0; | 400 unsigned int affect_mask = 0; |
400 unsigned int value_mask = 0; | 401 unsigned int value_mask = 0; |
401 if (new_caps_lock_status != kDontChange) { | 402 if (new_caps_lock_status != kDontChange) { |
402 affect_mask |= LockMask; | 403 affect_mask |= LockMask; |
403 value_mask |= ((new_caps_lock_status == kEnableLock) ? LockMask : 0); | 404 value_mask |= ((new_caps_lock_status == kEnableLock) ? LockMask : 0); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 } | 460 } |
460 | 461 |
461 bool XKeyboardImpl::KeepRightAlt(const std::string& xkb_layout_name) const { | 462 bool XKeyboardImpl::KeepRightAlt(const std::string& xkb_layout_name) const { |
462 return keep_right_alt_xkb_layout_names_.count(xkb_layout_name) > 0; | 463 return keep_right_alt_xkb_layout_names_.count(xkb_layout_name) > 0; |
463 } | 464 } |
464 | 465 |
465 bool XKeyboardImpl::KeepCapsLock(const std::string& xkb_layout_name) const { | 466 bool XKeyboardImpl::KeepCapsLock(const std::string& xkb_layout_name) const { |
466 return caps_lock_remapped_xkb_layout_names_.count(xkb_layout_name) > 0; | 467 return caps_lock_remapped_xkb_layout_names_.count(xkb_layout_name) > 0; |
467 } | 468 } |
468 | 469 |
| 470 bool XKeyboardImpl::CurrentlyOnUIThread() const { |
| 471 // It seems that the tot Chrome (as of Mar 7 2012) does not allow browser |
| 472 // tests to call BrowserThread::CurrentlyOn(). It ends up a CHECK failure: |
| 473 // FATAL:sequenced_worker_pool.cc |
| 474 // Check failed: constructor_message_loop_.get(). |
| 475 // For now, just allow unit/browser tests to call any functions in this class. |
| 476 // TODO(yusukes): Stop special-casing browser_tests and remove this function. |
| 477 if (!is_running_on_chrome_os_) { |
| 478 return true; |
| 479 } |
| 480 return BrowserThread::CurrentlyOn(BrowserThread::UI); |
| 481 } |
| 482 |
469 // static | 483 // static |
470 std::string XKeyboardImpl::ModifierKeyToString(ModifierKey key) { | 484 std::string XKeyboardImpl::ModifierKeyToString(ModifierKey key) { |
471 switch (key) { | 485 switch (key) { |
472 case kSearchKey: | 486 case kSearchKey: |
473 return "search"; | 487 return "search"; |
474 case kLeftControlKey: | 488 case kLeftControlKey: |
475 return "leftcontrol"; | 489 return "leftcontrol"; |
476 case kLeftAltKey: | 490 case kLeftAltKey: |
477 return "leftalt"; | 491 return "leftalt"; |
478 case kVoidKey: | 492 case kVoidKey: |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 return false; | 569 return false; |
556 } | 570 } |
557 | 571 |
558 // static | 572 // static |
559 XKeyboard* XKeyboard::Create(const InputMethodUtil& util) { | 573 XKeyboard* XKeyboard::Create(const InputMethodUtil& util) { |
560 return new XKeyboardImpl(util); | 574 return new XKeyboardImpl(util); |
561 } | 575 } |
562 | 576 |
563 } // namespace input_method | 577 } // namespace input_method |
564 } // namespace chromeos | 578 } // namespace chromeos |
OLD | NEW |