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 void ParseLayoutName(const std::string& layout_name, |
| 629 std::string* layout_id, |
| 630 std::string* layout_variant) { |
| 631 size_t dash_index = layout_name.find('-'); |
| 632 size_t parentheses_index = layout_name.find('('); |
| 633 *layout_id = layout_name; |
| 634 *layout_variant = ""; |
| 635 if (parentheses_index != std::string::npos) { |
| 636 *layout_id = layout_name.substr(0, parentheses_index); |
| 637 size_t close_index = layout_name.find(')', parentheses_index); |
| 638 if (close_index == std::string::npos) |
| 639 close_index = layout_name.size(); |
| 640 *layout_variant = layout_name.substr(parentheses_index + 1, |
| 641 close_index - parentheses_index - 1); |
| 642 } else if (dash_index != std::string::npos) { |
| 643 *layout_id = layout_name.substr(0, dash_index); |
| 644 *layout_variant = layout_name.substr(dash_index + 1); |
| 645 } |
| 646 } |
| 647 |
628 void LoadKeymap(const std::string& layout_name, | 648 void LoadKeymap(const std::string& layout_name, |
629 scoped_ptr<xkb_rule_names> names, | |
630 scoped_refptr<base::SingleThreadTaskRunner> reply_runner, | 649 scoped_refptr<base::SingleThreadTaskRunner> reply_runner, |
631 const LoadKeymapCallback& reply_callback) { | 650 const LoadKeymapCallback& reply_callback) { |
| 651 std::string layout_id; |
| 652 std::string layout_variant; |
| 653 ParseLayoutName(layout_name, &layout_id, &layout_variant); |
| 654 xkb_rule_names names = {.rules = NULL, |
| 655 .model = "pc101", |
| 656 .layout = layout_id.c_str(), |
| 657 .variant = layout_variant.c_str(), |
| 658 .options = ""}; |
632 scoped_ptr<xkb_context, XkbContextDeleter> context; | 659 scoped_ptr<xkb_context, XkbContextDeleter> context; |
633 context.reset(xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES)); | 660 context.reset(xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES)); |
634 xkb_context_include_path_append(context.get(), "/usr/share/X11/xkb"); | 661 xkb_context_include_path_append(context.get(), "/usr/share/X11/xkb"); |
635 scoped_ptr<xkb_keymap, XkbKeymapDeleter> keymap; | 662 scoped_ptr<xkb_keymap, XkbKeymapDeleter> keymap; |
636 keymap.reset(xkb_keymap_new_from_names(context.get(), names.get(), | 663 keymap.reset(xkb_keymap_new_from_names(context.get(), &names, |
637 XKB_KEYMAP_COMPILE_NO_FLAGS)); | 664 XKB_KEYMAP_COMPILE_NO_FLAGS)); |
638 char* keymap_str = | 665 if (keymap) { |
639 xkb_keymap_get_as_string(keymap.get(), XKB_KEYMAP_FORMAT_TEXT_V1); | 666 char* keymap_str = |
640 reply_runner->PostTask(FROM_HERE, base::Bind(reply_callback, layout_name, | 667 xkb_keymap_get_as_string(keymap.get(), XKB_KEYMAP_FORMAT_TEXT_V1); |
641 base::Owned(keymap_str))); | 668 reply_runner->PostTask(FROM_HERE, base::Bind(reply_callback, layout_name, |
| 669 base::Owned(keymap_str))); |
| 670 } else { |
| 671 LOG(ERROR) << "Keymap file failed to load: " << layout_name; |
| 672 } |
642 } | 673 } |
643 | 674 |
644 } // anonymous namespace | 675 } // anonymous namespace |
645 | 676 |
646 XkbKeyCodeConverter::XkbKeyCodeConverter() { | 677 XkbKeyCodeConverter::XkbKeyCodeConverter() { |
647 } | 678 } |
648 | 679 |
649 XkbKeyCodeConverter::~XkbKeyCodeConverter() { | 680 XkbKeyCodeConverter::~XkbKeyCodeConverter() { |
650 } | 681 } |
651 | 682 |
(...skipping 27 matching lines...) Expand all Loading... |
679 #if defined(OS_CHROMEOS) | 710 #if defined(OS_CHROMEOS) |
680 current_layout_name_ = layout_name; | 711 current_layout_name_ = layout_name; |
681 for (const auto& entry : xkb_keymaps_) { | 712 for (const auto& entry : xkb_keymaps_) { |
682 if (entry.layout_name == layout_name) { | 713 if (entry.layout_name == layout_name) { |
683 SetKeymap(entry.keymap); | 714 SetKeymap(entry.keymap); |
684 return true; | 715 return true; |
685 } | 716 } |
686 } | 717 } |
687 LoadKeymapCallback reply_callback = base::Bind( | 718 LoadKeymapCallback reply_callback = base::Bind( |
688 &XkbKeyboardLayoutEngine::OnKeymapLoaded, weak_ptr_factory_.GetWeakPtr()); | 719 &XkbKeyboardLayoutEngine::OnKeymapLoaded, weak_ptr_factory_.GetWeakPtr()); |
689 scoped_ptr<xkb_rule_names> names = GetXkbRuleNames(layout_name); | |
690 base::WorkerPool::PostTask( | 720 base::WorkerPool::PostTask( |
691 FROM_HERE, | 721 FROM_HERE, |
692 base::Bind(&LoadKeymap, layout_name, base::Passed(&names), | 722 base::Bind(&LoadKeymap, layout_name, base::ThreadTaskRunnerHandle::Get(), |
693 base::ThreadTaskRunnerHandle::Get(), reply_callback), | 723 reply_callback), |
694 true); | 724 true); |
695 #else | 725 #else |
696 xkb_keymap* keymap = xkb_map_new_from_string( | 726 xkb_keymap* keymap = xkb_map_new_from_string( |
697 xkb_context_.get(), layout_name.c_str(), XKB_KEYMAP_FORMAT_TEXT_V1, | 727 xkb_context_.get(), layout_name.c_str(), XKB_KEYMAP_FORMAT_TEXT_V1, |
698 XKB_KEYMAP_COMPILE_NO_FLAGS); | 728 XKB_KEYMAP_COMPILE_NO_FLAGS); |
699 if (!keymap) | 729 if (!keymap) |
700 return false; | 730 return false; |
701 SetKeymap(keymap); | 731 SetKeymap(keymap); |
702 #endif // defined(OS_CHROMEOS) | 732 #endif // defined(OS_CHROMEOS) |
703 return true; | 733 return true; |
704 } | 734 } |
705 | 735 |
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, | 736 void XkbKeyboardLayoutEngine::OnKeymapLoaded(const std::string& layout_name, |
732 const char* keymap_str) { | 737 const char* keymap_str) { |
733 if (keymap_str) { | 738 if (keymap_str) { |
734 xkb_keymap* keymap = xkb_map_new_from_string(xkb_context_.get(), keymap_str, | 739 xkb_keymap* keymap = xkb_map_new_from_string(xkb_context_.get(), keymap_str, |
735 XKB_KEYMAP_FORMAT_TEXT_V1, | 740 XKB_KEYMAP_FORMAT_TEXT_V1, |
736 XKB_KEYMAP_COMPILE_NO_FLAGS); | 741 XKB_KEYMAP_COMPILE_NO_FLAGS); |
737 XkbKeymapEntry entry = {layout_name, keymap}; | 742 XkbKeymapEntry entry = {layout_name, keymap}; |
738 xkb_keymaps_.push_back(entry); | 743 xkb_keymaps_.push_back(entry); |
739 if (layout_name == current_layout_name_) | 744 if (layout_name == current_layout_name_) |
740 SetKeymap(keymap); | 745 SetKeymap(keymap); |
741 } else { | 746 } else { |
742 LOG(ERROR) << "Keymap file fail to load: " << layout_name; | 747 LOG(ERROR) << "Keymap file failed to load: " << layout_name; |
743 } | 748 } |
744 } | 749 } |
745 | 750 |
746 bool XkbKeyboardLayoutEngine::UsesISOLevel5Shift() const { | 751 bool XkbKeyboardLayoutEngine::UsesISOLevel5Shift() const { |
747 // NOTIMPLEMENTED(); | 752 // NOTIMPLEMENTED(); |
748 return false; | 753 return false; |
749 } | 754 } |
750 | 755 |
751 bool XkbKeyboardLayoutEngine::UsesAltGr() const { | 756 bool XkbKeyboardLayoutEngine::UsesAltGr() const { |
752 // NOTIMPLEMENTED(); | 757 // NOTIMPLEMENTED(); |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
940 if (flags == base_flags) | 945 if (flags == base_flags) |
941 return base_character; | 946 return base_character; |
942 xkb_keysym_t keysym; | 947 xkb_keysym_t keysym; |
943 base::char16 character = 0; | 948 base::char16 character = 0; |
944 if (!XkbLookup(xkb_keycode, flags, &keysym, &character)) | 949 if (!XkbLookup(xkb_keycode, flags, &keysym, &character)) |
945 character = kNone; | 950 character = kNone; |
946 return character; | 951 return character; |
947 } | 952 } |
948 | 953 |
949 } // namespace ui | 954 } // namespace ui |
OLD | NEW |