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