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 "ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.h" | 5 #include "ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.h" |
6 | 6 |
7 #include <xkbcommon/xkbcommon-names.h> | 7 #include <xkbcommon/xkbcommon-names.h> |
8 | 8 |
9 #include "base/bind.h" | |
10 #include "base/location.h" | |
11 #include "base/logging.h" | 9 #include "base/logging.h" |
12 #include "base/single_thread_task_runner.h" | |
13 #include "base/task_runner.h" | |
14 #include "base/thread_task_runner_handle.h" | |
15 #include "base/threading/worker_pool.h" | |
16 #include "ui/events/event_constants.h" | 10 #include "ui/events/event_constants.h" |
17 #include "ui/events/keycodes/dom3/dom_code.h" | 11 #include "ui/events/keycodes/dom3/dom_code.h" |
18 #include "ui/events/keycodes/dom3/dom_key.h" | 12 #include "ui/events/keycodes/dom3/dom_key.h" |
19 #include "ui/events/keycodes/dom4/keycode_converter.h" | 13 #include "ui/events/keycodes/dom4/keycode_converter.h" |
20 #include "ui/events/ozone/layout/layout_util.h" | 14 #include "ui/events/ozone/layout/layout_util.h" |
21 #include "ui/events/ozone/layout/xkb/xkb_keyboard_code_conversion.h" | 15 #include "ui/events/ozone/layout/xkb/xkb_keyboard_code_conversion.h" |
22 | 16 |
23 namespace ui { | 17 namespace ui { |
24 | 18 |
25 namespace { | 19 namespace { |
26 | 20 |
27 typedef base::Callback<void(const std::string&, | |
28 scoped_ptr<xkb_keymap, XkbKeymapDeleter> keymap)> | |
29 LoadKeymapCallback; | |
30 | |
31 DomKey CharacterToDomKey(base::char16 character) { | 21 DomKey CharacterToDomKey(base::char16 character) { |
32 switch (character) { | 22 switch (character) { |
33 case 0x08: | 23 case 0x08: |
34 return DomKey::BACKSPACE; | 24 return DomKey::BACKSPACE; |
35 case 0x09: | 25 case 0x09: |
36 return DomKey::TAB; | 26 return DomKey::TAB; |
37 case 0x0A: | 27 case 0x0A: |
38 case 0x0D: | 28 case 0x0D: |
39 return DomKey::ENTER; | 29 return DomKey::ENTER; |
40 case 0x1B: | 30 case 0x1B: |
(...skipping 578 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
619 {0x0163, VKEY_OEM_7}, // t cedilla | 609 {0x0163, VKEY_OEM_7}, // t cedilla |
620 {0x0165, VKEY_5}, // t caron | 610 {0x0165, VKEY_5}, // t caron |
621 {0x016F, VKEY_OEM_1}, // u ring above | 611 {0x016F, VKEY_OEM_1}, // u ring above |
622 {0x0171, VKEY_OEM_5}, // u double acute | 612 {0x0171, VKEY_OEM_5}, // u double acute |
623 {0x01A1, VKEY_OEM_6}, // o horn | 613 {0x01A1, VKEY_OEM_6}, // o horn |
624 {0x01B0, VKEY_OEM_4}, // u horn | 614 {0x01B0, VKEY_OEM_4}, // u horn |
625 {0x01B6, VKEY_OEM_6}, // z stroke | 615 {0x01B6, VKEY_OEM_6}, // z stroke |
626 {0x0259, VKEY_OEM_3}, // schwa | 616 {0x0259, VKEY_OEM_3}, // schwa |
627 }; | 617 }; |
628 | 618 |
629 void LoadKeymap(const std::string& layout_name, | |
630 scoped_ptr<xkb_rule_names> names, | |
631 xkb_context* context, | |
632 scoped_refptr<base::SingleThreadTaskRunner> reply_runner, | |
633 const LoadKeymapCallback& reply_callback) { | |
634 scoped_ptr<xkb_keymap, XkbKeymapDeleter> keymap; | |
635 keymap.reset(xkb_keymap_new_from_names(context, names.get(), | |
636 XKB_KEYMAP_COMPILE_NO_FLAGS)); | |
637 reply_runner->PostTask(FROM_HERE, base::Bind(reply_callback, layout_name, | |
638 base::Passed(&keymap))); | |
639 } | |
640 | |
641 } // anonymous namespace | 619 } // anonymous namespace |
642 | 620 |
643 XkbKeyCodeConverter::XkbKeyCodeConverter() { | 621 XkbKeyCodeConverter::XkbKeyCodeConverter() { |
644 } | 622 } |
645 | 623 |
646 XkbKeyCodeConverter::~XkbKeyCodeConverter() { | 624 XkbKeyCodeConverter::~XkbKeyCodeConverter() { |
647 } | 625 } |
648 | 626 |
649 XkbKeyboardLayoutEngine::XkbKeyboardLayoutEngine( | 627 XkbKeyboardLayoutEngine::XkbKeyboardLayoutEngine( |
650 const XkbKeyCodeConverter& converter) | 628 const XkbKeyCodeConverter& converter) |
651 : key_code_converter_(converter), weak_ptr_factory_(this) { | 629 : key_code_converter_(converter) { |
652 // TODO: add XKB_CONTEXT_NO_ENVIRONMENT_NAMES | 630 // TODO: add XKB_CONTEXT_NO_ENVIRONMENT_NAMES |
653 xkb_context_.reset(xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES)); | 631 xkb_context_.reset(xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES)); |
654 xkb_context_include_path_append(xkb_context_.get(), | 632 xkb_context_include_path_append(xkb_context_.get(), |
655 "/usr/share/X11/xkb"); | 633 "/usr/share/X11/xkb"); |
656 } | 634 } |
657 | 635 |
658 XkbKeyboardLayoutEngine::~XkbKeyboardLayoutEngine() { | 636 XkbKeyboardLayoutEngine::~XkbKeyboardLayoutEngine() { |
659 for (const auto& entry : xkb_keymaps_) { | |
660 xkb_keymap_unref(entry.keymap); | |
661 } | |
662 } | 637 } |
663 | 638 |
664 bool XkbKeyboardLayoutEngine::CanSetCurrentLayout() const { | 639 bool XkbKeyboardLayoutEngine::CanSetCurrentLayout() const { |
665 #if defined(OS_CHROMEOS) | 640 #if defined(OS_CHROMEOS) |
666 return true; | 641 return true; |
667 #else | 642 #else |
668 return false; | 643 return false; |
669 #endif | 644 #endif |
670 } | 645 } |
671 | 646 |
672 bool XkbKeyboardLayoutEngine::SetCurrentLayoutByName( | 647 bool XkbKeyboardLayoutEngine::SetCurrentLayoutByName( |
673 const std::string& layout_name) { | 648 const std::string& layout_name) { |
674 #if defined(OS_CHROMEOS) | 649 #if defined(OS_CHROMEOS) |
675 current_layout_name_ = layout_name; | |
676 for (const auto& entry : xkb_keymaps_) { | |
677 if (entry.layout_name == layout_name) { | |
678 SetKeymap(entry.keymap); | |
679 return true; | |
680 } | |
681 } | |
682 LoadKeymapCallback reply_callback = base::Bind( | |
683 &XkbKeyboardLayoutEngine::OnKeymapLoaded, weak_ptr_factory_.GetWeakPtr()); | |
684 scoped_ptr<xkb_rule_names> names = GetXkbRuleNames(layout_name); | |
685 base::WorkerPool::PostTask( | |
686 FROM_HERE, | |
687 base::Bind(&LoadKeymap, layout_name, base::Passed(&names), | |
688 base::Unretained(xkb_context_.get()), | |
689 base::ThreadTaskRunnerHandle::Get(), reply_callback), | |
690 true); | |
691 return true; | |
692 #endif // defined(OS_CHROMEOS) | |
693 return false; | |
694 } | |
695 | |
696 scoped_ptr<xkb_rule_names> XkbKeyboardLayoutEngine::GetXkbRuleNames( | |
697 const std::string& layout_name) { | |
698 size_t dash_index = layout_name.find('-'); | 650 size_t dash_index = layout_name.find('-'); |
699 size_t parentheses_index = layout_name.find('('); | 651 size_t parentheses_index = layout_name.find('('); |
700 std::string layout_id = layout_name; | 652 std::string layout_id = layout_name; |
701 std::string layout_variant = ""; | 653 std::string layout_variant = ""; |
702 if (parentheses_index != std::string::npos) { | 654 if (parentheses_index != std::string::npos) { |
703 layout_id = layout_name.substr(0, parentheses_index); | 655 layout_id = layout_name.substr(0, parentheses_index); |
704 size_t close_index = layout_name.find(')', parentheses_index); | 656 size_t close_index = layout_name.find(')', parentheses_index); |
705 if (close_index == std::string::npos) | 657 if (close_index == std::string::npos) |
706 close_index = layout_name.size(); | 658 close_index = layout_name.size(); |
707 layout_variant = layout_name.substr(parentheses_index + 1, | 659 layout_variant = layout_name.substr(parentheses_index + 1, |
708 close_index - parentheses_index - 1); | 660 close_index - parentheses_index - 1); |
709 } else if (dash_index != std::string::npos) { | 661 } else if (dash_index != std::string::npos) { |
710 layout_id = layout_name.substr(0, dash_index); | 662 layout_id = layout_name.substr(0, dash_index); |
711 layout_variant = layout_name.substr(dash_index + 1); | 663 layout_variant = layout_name.substr(dash_index + 1); |
712 } | 664 } |
713 return make_scoped_ptr<xkb_rule_names>( | 665 struct xkb_rule_names names = { |
714 new xkb_rule_names{.rules = NULL, | 666 .rules = NULL, |
715 .model = "pc101", | 667 .model = "pc101", |
716 .layout = layout_id.c_str(), | 668 .layout = layout_id.c_str(), |
717 .variant = layout_variant.c_str(), | 669 .variant = layout_variant.c_str(), |
718 .options = ""}); | 670 .options = "" |
719 } | 671 }; |
720 | 672 xkb_keymap* keymap = xkb_keymap_new_from_names(xkb_context_.get(), |
721 void XkbKeyboardLayoutEngine::OnKeymapLoaded( | 673 &names, |
722 const std::string& layout_name, | 674 XKB_KEYMAP_COMPILE_NO_FLAGS); |
723 scoped_ptr<xkb_keymap, XkbKeymapDeleter> keymap) { | |
724 if (keymap) { | 675 if (keymap) { |
725 XkbKeymapEntry entry = {layout_name, xkb_keymap_ref(keymap.get())}; | 676 SetKeymap(keymap); |
726 xkb_keymaps_.push_back(entry); | 677 return true; |
727 if (layout_name == current_layout_name_) | |
728 SetKeymap(keymap.get()); | |
729 } else { | |
730 LOG(ERROR) << "Keymap file fail to load: " << layout_name; | |
731 } | 678 } |
| 679 #endif // defined(OS_CHROMEOS) |
| 680 return false; |
732 } | 681 } |
733 | 682 |
734 bool XkbKeyboardLayoutEngine::UsesISOLevel5Shift() const { | 683 bool XkbKeyboardLayoutEngine::UsesISOLevel5Shift() const { |
735 // NOTIMPLEMENTED(); | 684 // NOTIMPLEMENTED(); |
736 return false; | 685 return false; |
737 } | 686 } |
738 | 687 |
739 bool XkbKeyboardLayoutEngine::UsesAltGr() const { | 688 bool XkbKeyboardLayoutEngine::UsesAltGr() const { |
740 // NOTIMPLEMENTED(); | 689 // NOTIMPLEMENTED(); |
741 return false; | 690 return false; |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
920 if (flags == base_flags) | 869 if (flags == base_flags) |
921 return base_character; | 870 return base_character; |
922 xkb_keysym_t keysym; | 871 xkb_keysym_t keysym; |
923 base::char16 character = 0; | 872 base::char16 character = 0; |
924 if (!XkbLookup(xkb_keycode, flags, &keysym, &character)) | 873 if (!XkbLookup(xkb_keycode, flags, &keysym, &character)) |
925 character = kNone; | 874 character = kNone; |
926 return character; | 875 return character; |
927 } | 876 } |
928 | 877 |
929 } // namespace ui | 878 } // namespace ui |
OLD | NEW |