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" | 9 #include "base/bind.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
618 {0x0163, VKEY_OEM_7}, // t cedilla | 618 {0x0163, VKEY_OEM_7}, // t cedilla |
619 {0x0165, VKEY_5}, // t caron | 619 {0x0165, VKEY_5}, // t caron |
620 {0x016F, VKEY_OEM_1}, // u ring above | 620 {0x016F, VKEY_OEM_1}, // u ring above |
621 {0x0171, VKEY_OEM_5}, // u double acute | 621 {0x0171, VKEY_OEM_5}, // u double acute |
622 {0x01A1, VKEY_OEM_6}, // o horn | 622 {0x01A1, VKEY_OEM_6}, // o horn |
623 {0x01B0, VKEY_OEM_4}, // u horn | 623 {0x01B0, VKEY_OEM_4}, // u horn |
624 {0x01B6, VKEY_OEM_6}, // z stroke | 624 {0x01B6, VKEY_OEM_6}, // z stroke |
625 {0x0259, VKEY_OEM_3}, // schwa | 625 {0x0259, VKEY_OEM_3}, // schwa |
626 }; | 626 }; |
627 | 627 |
628 scoped_ptr<xkb_rule_names> GetXkbRuleNames(const std::string& layout_name) { | |
629 size_t dash_index = layout_name.find('-'); | |
630 size_t parentheses_index = layout_name.find('('); | |
631 std::string layout_id = layout_name; | |
632 std::string layout_variant = ""; | |
633 if (parentheses_index != std::string::npos) { | |
634 layout_id = layout_name.substr(0, parentheses_index); | |
635 size_t close_index = layout_name.find(')', parentheses_index); | |
636 if (close_index == std::string::npos) | |
637 close_index = layout_name.size(); | |
638 layout_variant = layout_name.substr(parentheses_index + 1, | |
639 close_index - parentheses_index - 1); | |
640 } else if (dash_index != std::string::npos) { | |
641 layout_id = layout_name.substr(0, dash_index); | |
642 layout_variant = layout_name.substr(dash_index + 1); | |
643 } | |
644 return make_scoped_ptr<xkb_rule_names>( | |
645 new xkb_rule_names{.rules = NULL, | |
646 .model = "pc101", | |
647 .layout = layout_id.c_str(), | |
Shu Chen
2015/01/13 12:49:14
this will cause .layout carry wild pointer, same f
FengYuan
2015/01/13 14:10:25
Done.
| |
648 .variant = layout_variant.c_str(), | |
649 .options = ""}); | |
650 } | |
651 | |
628 void LoadKeymap(const std::string& layout_name, | 652 void LoadKeymap(const std::string& layout_name, |
629 scoped_ptr<xkb_rule_names> names, | |
630 scoped_refptr<base::SingleThreadTaskRunner> reply_runner, | 653 scoped_refptr<base::SingleThreadTaskRunner> reply_runner, |
631 const LoadKeymapCallback& reply_callback) { | 654 const LoadKeymapCallback& reply_callback) { |
655 scoped_ptr<xkb_rule_names> names = GetXkbRuleNames(layout_name); | |
632 scoped_ptr<xkb_context, XkbContextDeleter> context; | 656 scoped_ptr<xkb_context, XkbContextDeleter> context; |
633 context.reset(xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES)); | 657 context.reset(xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES)); |
634 xkb_context_include_path_append(context.get(), "/usr/share/X11/xkb"); | 658 xkb_context_include_path_append(context.get(), "/usr/share/X11/xkb"); |
635 scoped_ptr<xkb_keymap, XkbKeymapDeleter> keymap; | 659 scoped_ptr<xkb_keymap, XkbKeymapDeleter> keymap; |
636 keymap.reset(xkb_keymap_new_from_names(context.get(), names.get(), | 660 keymap.reset(xkb_keymap_new_from_names(context.get(), names.get(), |
637 XKB_KEYMAP_COMPILE_NO_FLAGS)); | 661 XKB_KEYMAP_COMPILE_NO_FLAGS)); |
638 char* keymap_str = | 662 if (keymap) { |
639 xkb_keymap_get_as_string(keymap.get(), XKB_KEYMAP_FORMAT_TEXT_V1); | 663 char* keymap_str = |
640 reply_runner->PostTask(FROM_HERE, base::Bind(reply_callback, layout_name, | 664 xkb_keymap_get_as_string(keymap.get(), XKB_KEYMAP_FORMAT_TEXT_V1); |
641 base::Owned(keymap_str))); | 665 reply_runner->PostTask(FROM_HERE, base::Bind(reply_callback, layout_name, |
666 base::Owned(keymap_str))); | |
667 } else { | |
668 LOG(ERROR) << "Keymap file fail to load: " << layout_name; | |
669 } | |
642 } | 670 } |
643 | 671 |
644 } // anonymous namespace | 672 } // anonymous namespace |
645 | 673 |
646 XkbKeyCodeConverter::XkbKeyCodeConverter() { | 674 XkbKeyCodeConverter::XkbKeyCodeConverter() { |
647 } | 675 } |
648 | 676 |
649 XkbKeyCodeConverter::~XkbKeyCodeConverter() { | 677 XkbKeyCodeConverter::~XkbKeyCodeConverter() { |
650 } | 678 } |
651 | 679 |
(...skipping 27 matching lines...) Expand all Loading... | |
679 #if defined(OS_CHROMEOS) | 707 #if defined(OS_CHROMEOS) |
680 current_layout_name_ = layout_name; | 708 current_layout_name_ = layout_name; |
681 for (const auto& entry : xkb_keymaps_) { | 709 for (const auto& entry : xkb_keymaps_) { |
682 if (entry.layout_name == layout_name) { | 710 if (entry.layout_name == layout_name) { |
683 SetKeymap(entry.keymap); | 711 SetKeymap(entry.keymap); |
684 return true; | 712 return true; |
685 } | 713 } |
686 } | 714 } |
687 LoadKeymapCallback reply_callback = base::Bind( | 715 LoadKeymapCallback reply_callback = base::Bind( |
688 &XkbKeyboardLayoutEngine::OnKeymapLoaded, weak_ptr_factory_.GetWeakPtr()); | 716 &XkbKeyboardLayoutEngine::OnKeymapLoaded, weak_ptr_factory_.GetWeakPtr()); |
689 scoped_ptr<xkb_rule_names> names = GetXkbRuleNames(layout_name); | |
690 base::WorkerPool::PostTask( | 717 base::WorkerPool::PostTask( |
691 FROM_HERE, | 718 FROM_HERE, |
692 base::Bind(&LoadKeymap, layout_name, base::Passed(&names), | 719 base::Bind(&LoadKeymap, layout_name, base::ThreadTaskRunnerHandle::Get(), |
693 base::ThreadTaskRunnerHandle::Get(), reply_callback), | 720 reply_callback), |
694 true); | 721 true); |
695 #else | 722 #else |
696 xkb_keymap* keymap = xkb_map_new_from_string( | 723 xkb_keymap* keymap = xkb_map_new_from_string( |
697 xkb_context_.get(), layout_name.c_str(), XKB_KEYMAP_FORMAT_TEXT_V1, | 724 xkb_context_.get(), layout_name.c_str(), XKB_KEYMAP_FORMAT_TEXT_V1, |
698 XKB_KEYMAP_COMPILE_NO_FLAGS); | 725 XKB_KEYMAP_COMPILE_NO_FLAGS); |
699 if (!keymap) | 726 if (!keymap) |
700 return false; | 727 return false; |
701 SetKeymap(keymap); | 728 SetKeymap(keymap); |
702 #endif // defined(OS_CHROMEOS) | 729 #endif // defined(OS_CHROMEOS) |
703 return true; | 730 return true; |
704 } | 731 } |
705 | 732 |
706 scoped_ptr<xkb_rule_names> XkbKeyboardLayoutEngine::GetXkbRuleNames( | |
707 const std::string& layout_name) { | |
708 size_t dash_index = layout_name.find('-'); | |
709 size_t parentheses_index = layout_name.find('('); | |
710 std::string layout_id = layout_name; | |
711 std::string layout_variant = ""; | |
712 if (parentheses_index != std::string::npos) { | |
713 layout_id = layout_name.substr(0, parentheses_index); | |
714 size_t close_index = layout_name.find(')', parentheses_index); | |
715 if (close_index == std::string::npos) | |
716 close_index = layout_name.size(); | |
717 layout_variant = layout_name.substr(parentheses_index + 1, | |
718 close_index - parentheses_index - 1); | |
719 } else if (dash_index != std::string::npos) { | |
720 layout_id = layout_name.substr(0, dash_index); | |
721 layout_variant = layout_name.substr(dash_index + 1); | |
722 } | |
723 return make_scoped_ptr<xkb_rule_names>( | |
724 new xkb_rule_names{.rules = NULL, | |
725 .model = "pc101", | |
726 .layout = layout_id.c_str(), | |
727 .variant = layout_variant.c_str(), | |
728 .options = ""}); | |
729 } | |
730 | |
731 void XkbKeyboardLayoutEngine::OnKeymapLoaded(const std::string& layout_name, | 733 void XkbKeyboardLayoutEngine::OnKeymapLoaded(const std::string& layout_name, |
732 const char* keymap_str) { | 734 const char* keymap_str) { |
733 if (keymap_str) { | 735 if (keymap_str) { |
734 xkb_keymap* keymap = xkb_map_new_from_string(xkb_context_.get(), keymap_str, | 736 xkb_keymap* keymap = xkb_map_new_from_string(xkb_context_.get(), keymap_str, |
735 XKB_KEYMAP_FORMAT_TEXT_V1, | 737 XKB_KEYMAP_FORMAT_TEXT_V1, |
736 XKB_KEYMAP_COMPILE_NO_FLAGS); | 738 XKB_KEYMAP_COMPILE_NO_FLAGS); |
737 XkbKeymapEntry entry = {layout_name, keymap}; | 739 XkbKeymapEntry entry = {layout_name, keymap}; |
738 xkb_keymaps_.push_back(entry); | 740 xkb_keymaps_.push_back(entry); |
739 if (layout_name == current_layout_name_) | 741 if (layout_name == current_layout_name_) |
740 SetKeymap(keymap); | 742 SetKeymap(keymap); |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
940 if (flags == base_flags) | 942 if (flags == base_flags) |
941 return base_character; | 943 return base_character; |
942 xkb_keysym_t keysym; | 944 xkb_keysym_t keysym; |
943 base::char16 character = 0; | 945 base::char16 character = 0; |
944 if (!XkbLookup(xkb_keycode, flags, &keysym, &character)) | 946 if (!XkbLookup(xkb_keycode, flags, &keysym, &character)) |
945 character = kNone; | 947 character = kNone; |
946 return character; | 948 return character; |
947 } | 949 } |
948 | 950 |
949 } // namespace ui | 951 } // namespace ui |
OLD | NEW |