Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(803)

Side by Side Diff: ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.cc

Issue 817983002: ozone: xkb: Load keymaps on worker thread & cache them (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
9 #include "base/logging.h" 11 #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"
10 #include "ui/events/event_constants.h" 16 #include "ui/events/event_constants.h"
11 #include "ui/events/keycodes/dom3/dom_code.h" 17 #include "ui/events/keycodes/dom3/dom_code.h"
12 #include "ui/events/keycodes/dom3/dom_key.h" 18 #include "ui/events/keycodes/dom3/dom_key.h"
13 #include "ui/events/keycodes/dom4/keycode_converter.h" 19 #include "ui/events/keycodes/dom4/keycode_converter.h"
14 #include "ui/events/ozone/layout/layout_util.h" 20 #include "ui/events/ozone/layout/layout_util.h"
15 #include "ui/events/ozone/layout/xkb/xkb_keyboard_code_conversion.h" 21 #include "ui/events/ozone/layout/xkb/xkb_keyboard_code_conversion.h"
16 22
17 namespace ui { 23 namespace ui {
18 24
19 namespace { 25 namespace {
20 26
27 typedef base::Callback<void(const std::string&,
28 scoped_ptr<xkb_keymap, XkbKeymapDeleter> keymap)>
29 LoadKeymapCallback;
30
21 DomKey CharacterToDomKey(base::char16 character) { 31 DomKey CharacterToDomKey(base::char16 character) {
22 switch (character) { 32 switch (character) {
23 case 0x08: 33 case 0x08:
24 return DomKey::BACKSPACE; 34 return DomKey::BACKSPACE;
25 case 0x09: 35 case 0x09:
26 return DomKey::TAB; 36 return DomKey::TAB;
27 case 0x0A: 37 case 0x0A:
28 case 0x0D: 38 case 0x0D:
29 return DomKey::ENTER; 39 return DomKey::ENTER;
30 case 0x1B: 40 case 0x1B:
(...skipping 578 matching lines...) Expand 10 before | Expand all | Expand 10 after
609 {0x0163, VKEY_OEM_7}, // t cedilla 619 {0x0163, VKEY_OEM_7}, // t cedilla
610 {0x0165, VKEY_5}, // t caron 620 {0x0165, VKEY_5}, // t caron
611 {0x016F, VKEY_OEM_1}, // u ring above 621 {0x016F, VKEY_OEM_1}, // u ring above
612 {0x0171, VKEY_OEM_5}, // u double acute 622 {0x0171, VKEY_OEM_5}, // u double acute
613 {0x01A1, VKEY_OEM_6}, // o horn 623 {0x01A1, VKEY_OEM_6}, // o horn
614 {0x01B0, VKEY_OEM_4}, // u horn 624 {0x01B0, VKEY_OEM_4}, // u horn
615 {0x01B6, VKEY_OEM_6}, // z stroke 625 {0x01B6, VKEY_OEM_6}, // z stroke
616 {0x0259, VKEY_OEM_3}, // schwa 626 {0x0259, VKEY_OEM_3}, // schwa
617 }; 627 };
618 628
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
619 } // anonymous namespace 641 } // anonymous namespace
620 642
621 XkbKeyCodeConverter::XkbKeyCodeConverter() { 643 XkbKeyCodeConverter::XkbKeyCodeConverter() {
622 } 644 }
623 645
624 XkbKeyCodeConverter::~XkbKeyCodeConverter() { 646 XkbKeyCodeConverter::~XkbKeyCodeConverter() {
625 } 647 }
626 648
627 XkbKeyboardLayoutEngine::XkbKeyboardLayoutEngine( 649 XkbKeyboardLayoutEngine::XkbKeyboardLayoutEngine(
628 const XkbKeyCodeConverter& converter) 650 const XkbKeyCodeConverter& converter)
629 : key_code_converter_(converter) { 651 : key_code_converter_(converter), weak_ptr_factory_(this) {
630 // TODO: add XKB_CONTEXT_NO_ENVIRONMENT_NAMES 652 // TODO: add XKB_CONTEXT_NO_ENVIRONMENT_NAMES
631 xkb_context_.reset(xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES)); 653 xkb_context_.reset(xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES));
632 xkb_context_include_path_append(xkb_context_.get(), 654 xkb_context_include_path_append(xkb_context_.get(),
633 "/usr/share/X11/xkb"); 655 "/usr/share/X11/xkb");
634 } 656 }
635 657
636 XkbKeyboardLayoutEngine::~XkbKeyboardLayoutEngine() { 658 XkbKeyboardLayoutEngine::~XkbKeyboardLayoutEngine() {
659 for (const auto& entry : xkb_keymaps_) {
660 xkb_keymap_unref(entry.keymap);
661 }
637 } 662 }
638 663
639 bool XkbKeyboardLayoutEngine::CanSetCurrentLayout() const { 664 bool XkbKeyboardLayoutEngine::CanSetCurrentLayout() const {
640 #if defined(OS_CHROMEOS) 665 #if defined(OS_CHROMEOS)
641 return true; 666 return true;
642 #else 667 #else
643 return false; 668 return false;
644 #endif 669 #endif
645 } 670 }
646 671
647 bool XkbKeyboardLayoutEngine::SetCurrentLayoutByName( 672 bool XkbKeyboardLayoutEngine::SetCurrentLayoutByName(
648 const std::string& layout_name) { 673 const std::string& layout_name) {
649 #if defined(OS_CHROMEOS) 674 #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) {
650 size_t dash_index = layout_name.find('-'); 698 size_t dash_index = layout_name.find('-');
651 size_t parentheses_index = layout_name.find('('); 699 size_t parentheses_index = layout_name.find('(');
652 std::string layout_id = layout_name; 700 std::string layout_id = layout_name;
653 std::string layout_variant = ""; 701 std::string layout_variant = "";
654 if (parentheses_index != std::string::npos) { 702 if (parentheses_index != std::string::npos) {
655 layout_id = layout_name.substr(0, parentheses_index); 703 layout_id = layout_name.substr(0, parentheses_index);
656 size_t close_index = layout_name.find(')', parentheses_index); 704 size_t close_index = layout_name.find(')', parentheses_index);
657 if (close_index == std::string::npos) 705 if (close_index == std::string::npos)
658 close_index = layout_name.size(); 706 close_index = layout_name.size();
659 layout_variant = layout_name.substr(parentheses_index + 1, 707 layout_variant = layout_name.substr(parentheses_index + 1,
660 close_index - parentheses_index - 1); 708 close_index - parentheses_index - 1);
661 } else if (dash_index != std::string::npos) { 709 } else if (dash_index != std::string::npos) {
662 layout_id = layout_name.substr(0, dash_index); 710 layout_id = layout_name.substr(0, dash_index);
663 layout_variant = layout_name.substr(dash_index + 1); 711 layout_variant = layout_name.substr(dash_index + 1);
664 } 712 }
665 struct xkb_rule_names names = { 713 return make_scoped_ptr<xkb_rule_names>(
666 .rules = NULL, 714 new xkb_rule_names{.rules = NULL,
667 .model = "pc101", 715 .model = "pc101",
668 .layout = layout_id.c_str(), 716 .layout = layout_id.c_str(),
669 .variant = layout_variant.c_str(), 717 .variant = layout_variant.c_str(),
670 .options = "" 718 .options = ""});
671 }; 719 }
672 xkb_keymap* keymap = xkb_keymap_new_from_names(xkb_context_.get(), 720
673 &names, 721 void XkbKeyboardLayoutEngine::OnKeymapLoaded(
674 XKB_KEYMAP_COMPILE_NO_FLAGS); 722 const std::string& layout_name,
723 scoped_ptr<xkb_keymap, XkbKeymapDeleter> keymap) {
675 if (keymap) { 724 if (keymap) {
676 SetKeymap(keymap); 725 XkbKeymapEntry entry = {layout_name, keymap.get()};
Shu Chen 2015/01/04 02:43:39 This is potentially a crash issue, because the add
FengYuan 2015/01/04 02:58:42 Done.
677 return true; 726 xkb_keymaps_.push_back(entry);
727 if (layout_name == current_layout_name_)
728 SetKeymap(keymap.get());
729 } else {
730 LOG(ERROR) << "Keymap file fail to load: " << layout_name;
678 } 731 }
679 #endif // defined(OS_CHROMEOS)
680 return false;
681 } 732 }
682 733
683 bool XkbKeyboardLayoutEngine::UsesISOLevel5Shift() const { 734 bool XkbKeyboardLayoutEngine::UsesISOLevel5Shift() const {
684 // NOTIMPLEMENTED(); 735 // NOTIMPLEMENTED();
685 return false; 736 return false;
686 } 737 }
687 738
688 bool XkbKeyboardLayoutEngine::UsesAltGr() const { 739 bool XkbKeyboardLayoutEngine::UsesAltGr() const {
689 // NOTIMPLEMENTED(); 740 // NOTIMPLEMENTED();
690 return false; 741 return false;
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
869 if (flags == base_flags) 920 if (flags == base_flags)
870 return base_character; 921 return base_character;
871 xkb_keysym_t keysym; 922 xkb_keysym_t keysym;
872 base::char16 character = 0; 923 base::char16 character = 0;
873 if (!XkbLookup(xkb_keycode, flags, &keysym, &character)) 924 if (!XkbLookup(xkb_keycode, flags, &keysym, &character))
874 character = kNone; 925 character = kNone;
875 return character; 926 return character;
876 } 927 }
877 928
878 } // namespace ui 929 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698