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

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: Figured out Michael's comments 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
25 typedef base::Callback<void(const std::string&,
26 scoped_ptr<xkb_keymap, XkbKeymapDeleter> keymap)>
27 LoadKeymapCallback;
Shu Chen 2015/01/04 01:43:16 Please move this to the anonymous namespace below.
FengYuan 2015/01/04 02:25:22 Done.
28
19 namespace { 29 namespace {
20 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:
(...skipping 590 matching lines...) Expand 10 before | Expand all | Expand 10 after
619 } // anonymous namespace 629 } // anonymous namespace
620 630
621 XkbKeyCodeConverter::XkbKeyCodeConverter() { 631 XkbKeyCodeConverter::XkbKeyCodeConverter() {
622 } 632 }
623 633
624 XkbKeyCodeConverter::~XkbKeyCodeConverter() { 634 XkbKeyCodeConverter::~XkbKeyCodeConverter() {
625 } 635 }
626 636
627 XkbKeyboardLayoutEngine::XkbKeyboardLayoutEngine( 637 XkbKeyboardLayoutEngine::XkbKeyboardLayoutEngine(
628 const XkbKeyCodeConverter& converter) 638 const XkbKeyCodeConverter& converter)
629 : key_code_converter_(converter) { 639 : key_code_converter_(converter), weak_ptr_factory_(this) {
630 // TODO: add XKB_CONTEXT_NO_ENVIRONMENT_NAMES 640 // TODO: add XKB_CONTEXT_NO_ENVIRONMENT_NAMES
631 xkb_context_.reset(xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES)); 641 xkb_context_.reset(xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES));
632 xkb_context_include_path_append(xkb_context_.get(), 642 xkb_context_include_path_append(xkb_context_.get(),
633 "/usr/share/X11/xkb"); 643 "/usr/share/X11/xkb");
634 } 644 }
635 645
636 XkbKeyboardLayoutEngine::~XkbKeyboardLayoutEngine() { 646 XkbKeyboardLayoutEngine::~XkbKeyboardLayoutEngine() {
647 for (const auto& entry : xkb_keymaps_) {
648 xkb_keymap_unref(entry.keymap);
649 }
637 } 650 }
638 651
639 bool XkbKeyboardLayoutEngine::CanSetCurrentLayout() const { 652 bool XkbKeyboardLayoutEngine::CanSetCurrentLayout() const {
640 #if defined(OS_CHROMEOS) 653 #if defined(OS_CHROMEOS)
641 return true; 654 return true;
642 #else 655 #else
643 return false; 656 return false;
644 #endif 657 #endif
645 } 658 }
646 659
660 void LoadKeymap(const std::string& layout_name,
661 scoped_ptr<xkb_rule_names> names,
662 xkb_context* context,
663 scoped_refptr<base::SingleThreadTaskRunner> reply_runner,
664 const LoadKeymapCallback& reply_callback) {
665 scoped_ptr<xkb_keymap, XkbKeymapDeleter> keymap;
666 keymap.reset(xkb_keymap_new_from_names(context, names.get(),
667 XKB_KEYMAP_COMPILE_NO_FLAGS));
668 reply_runner->PostTask(FROM_HERE, base::Bind(reply_callback, layout_name,
669 base::Passed(&keymap)));
670 }
Shu Chen 2015/01/04 01:43:17 Please move this global function to the anonymous
FengYuan 2015/01/04 02:25:22 Done.
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_) {
Shu Chen 2015/01/04 01:43:16 Not sure whether the Chromium building systems are
FengYuan 2015/01/04 02:25:22 It is existing in the above codes, so it should be
spang 2015/01/05 18:17:21 Correct. Here's the full reference: https://chrom
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) {
Shu Chen 2015/01/04 01:43:17 keymap.get()
FengYuan 2015/01/04 02:25:22 not necessary, see same usage in XkbLookup
676 SetKeymap(keymap); 725 XkbKeymapEntry entry = {layout_name, keymap.get()};
677 return true; 726 xkb_keymaps_.push_back(entry);
Shu Chen 2015/01/04 01:43:16 There is risk of inserting duplicated entries here
FengYuan 2015/01/04 02:25:22 yep, duplicate entries doesn't do harm to the logi
Shu Chen 2015/01/04 02:30:59 Are you going to create further CLs to solve the d
FengYuan 2015/01/04 02:58:42 Yep, I will solve the duplicate loading in IMF sid
727 if (layout_name == current_layout_name_)
728 SetKeymap(keymap.get());
678 } 729 }
Shu Chen 2015/01/04 01:43:17 Suggest to do an error log here. else { LOG(ERR
FengYuan 2015/01/04 02:25:22 Done.
679 #endif // defined(OS_CHROMEOS)
680 return false;
681 } 730 }
682 731
683 bool XkbKeyboardLayoutEngine::UsesISOLevel5Shift() const { 732 bool XkbKeyboardLayoutEngine::UsesISOLevel5Shift() const {
684 // NOTIMPLEMENTED(); 733 // NOTIMPLEMENTED();
685 return false; 734 return false;
686 } 735 }
687 736
688 bool XkbKeyboardLayoutEngine::UsesAltGr() const { 737 bool XkbKeyboardLayoutEngine::UsesAltGr() const {
689 // NOTIMPLEMENTED(); 738 // NOTIMPLEMENTED();
690 return false; 739 return false;
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
869 if (flags == base_flags) 918 if (flags == base_flags)
870 return base_character; 919 return base_character;
871 xkb_keysym_t keysym; 920 xkb_keysym_t keysym;
872 base::char16 character = 0; 921 base::char16 character = 0;
873 if (!XkbLookup(xkb_keycode, flags, &keysym, &character)) 922 if (!XkbLookup(xkb_keycode, flags, &keysym, &character))
874 character = kNone; 923 character = kNone;
875 return character; 924 return character;
876 } 925 }
877 926
878 } // namespace ui 927 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698