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

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, 12 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
« no previous file with comments | « ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/threading/worker_pool.h"
10 #include "ui/events/event_constants.h" 13 #include "ui/events/event_constants.h"
11 #include "ui/events/keycodes/dom3/dom_code.h" 14 #include "ui/events/keycodes/dom3/dom_code.h"
12 #include "ui/events/keycodes/dom3/dom_key.h" 15 #include "ui/events/keycodes/dom3/dom_key.h"
13 #include "ui/events/keycodes/dom4/keycode_converter.h" 16 #include "ui/events/keycodes/dom4/keycode_converter.h"
14 #include "ui/events/ozone/layout/layout_util.h" 17 #include "ui/events/ozone/layout/layout_util.h"
15 #include "ui/events/ozone/layout/xkb/xkb_keyboard_code_conversion.h" 18 #include "ui/events/ozone/layout/xkb/xkb_keyboard_code_conversion.h"
16 19
17 namespace ui { 20 namespace ui {
18 21
19 namespace { 22 namespace {
(...skipping 591 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 {0x016F, VKEY_OEM_1}, // u ring above 614 {0x016F, VKEY_OEM_1}, // u ring above
612 {0x0171, VKEY_OEM_5}, // u double acute 615 {0x0171, VKEY_OEM_5}, // u double acute
613 {0x01A1, VKEY_OEM_6}, // o horn 616 {0x01A1, VKEY_OEM_6}, // o horn
614 {0x01B0, VKEY_OEM_4}, // u horn 617 {0x01B0, VKEY_OEM_4}, // u horn
615 {0x01B6, VKEY_OEM_6}, // z stroke 618 {0x01B6, VKEY_OEM_6}, // z stroke
616 {0x0259, VKEY_OEM_3}, // schwa 619 {0x0259, VKEY_OEM_3}, // schwa
617 }; 620 };
618 621
619 } // anonymous namespace 622 } // anonymous namespace
620 623
624 XkbKeyboardLayoutEngine::XkbKeymapLoader::XkbKeymapLoader(
625 XkbKeyboardLayoutEngine* layout_engine) : layout_engine_(layout_engine) {
626 }
627
628 void XkbKeyboardLayoutEngine::XkbKeymapLoader::loadKeymap(
629 const std::string& layout_name) {
630 size_t dash_index = layout_name.find('-');
631 size_t parentheses_index = layout_name.find('(');
632 std::string layout_id = layout_name;
633 std::string layout_variant = "";
634 if (parentheses_index != std::string::npos) {
635 layout_id = layout_name.substr(0, parentheses_index);
636 size_t close_index = layout_name.find(')', parentheses_index);
637 if (close_index == std::string::npos)
638 close_index = layout_name.size();
639 layout_variant = layout_name.substr(parentheses_index + 1,
640 close_index - parentheses_index - 1);
641 } else if (dash_index != std::string::npos) {
642 layout_id = layout_name.substr(0, dash_index);
643 layout_variant = layout_name.substr(dash_index + 1);
644 }
645 names = new xkb_rule_names {
646 .rules = NULL,
647 .model = "pc101",
648 .layout = layout_id.c_str(),
649 .variant = layout_variant.c_str(),
650 .options = ""
651 };
652 base::WorkerPool::PostTask(
653 FROM_HERE,
654 base::Bind(&XkbKeyboardLayoutEngine::XkbKeymapLoader::startLoadKeymap,
655 base::Unretained(this),
Shu Chen 2014/12/23 08:46:49 base::Owned(this)
FengYuan 2014/12/23 15:24:57 Done.
656 layout_name,
657 base::Unretained(names)),
658 true);
659 }
660
661 void XkbKeyboardLayoutEngine::XkbKeymapLoader::startLoadKeymap(
662 const std::string& layout_name, xkb_rule_names* names) {
Shu Chen 2014/12/23 08:46:49 You need to delete names in this method.
FengYuan 2014/12/23 15:24:57 Done.
663 xkb_keymap* keymap = xkb_keymap_new_from_names(
664 layout_engine_->xkb_context_.get(),
665 names,
666 XKB_KEYMAP_COMPILE_NO_FLAGS);
667
668 if (keymap) {
669 layout_engine_->xkb_keymaps_[layout_name] = keymap;
Shu Chen 2014/12/23 08:46:49 Please make sure these operations are executed in
FengYuan 2014/12/23 15:24:57 These operations are in the worker thread, and rep
670 layout_engine_->SetKeymap(keymap);
671 }
672 }
673
621 XkbKeyCodeConverter::XkbKeyCodeConverter() { 674 XkbKeyCodeConverter::XkbKeyCodeConverter() {
622 } 675 }
623 676
624 XkbKeyCodeConverter::~XkbKeyCodeConverter() { 677 XkbKeyCodeConverter::~XkbKeyCodeConverter() {
625 } 678 }
626 679
627 XkbKeyboardLayoutEngine::XkbKeyboardLayoutEngine( 680 XkbKeyboardLayoutEngine::XkbKeyboardLayoutEngine(
628 const XkbKeyCodeConverter& converter) 681 const XkbKeyCodeConverter& converter)
629 : key_code_converter_(converter) { 682 : key_code_converter_(converter) {
630 // TODO: add XKB_CONTEXT_NO_ENVIRONMENT_NAMES 683 // TODO: add XKB_CONTEXT_NO_ENVIRONMENT_NAMES
631 xkb_context_.reset(xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES)); 684 xkb_context_.reset(xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES));
632 xkb_context_include_path_append(xkb_context_.get(), 685 xkb_context_include_path_append(xkb_context_.get(),
633 "/usr/share/X11/xkb"); 686 "/usr/share/X11/xkb");
634 } 687 }
635 688
636 XkbKeyboardLayoutEngine::~XkbKeyboardLayoutEngine() { 689 XkbKeyboardLayoutEngine::~XkbKeyboardLayoutEngine() {
690 std::map<std::string, xkb_keymap*>::const_iterator it = xkb_keymaps_.begin();
691 for(;it != xkb_keymaps_.end(); ++it) {
692 xkb_keymap_unref(it->second);
693 }
637 } 694 }
638 695
639 bool XkbKeyboardLayoutEngine::CanSetCurrentLayout() const { 696 bool XkbKeyboardLayoutEngine::CanSetCurrentLayout() const {
640 #if defined(OS_CHROMEOS) 697 #if defined(OS_CHROMEOS)
641 return true; 698 return true;
642 #else 699 #else
643 return false; 700 return false;
644 #endif 701 #endif
645 } 702 }
646 703
647 bool XkbKeyboardLayoutEngine::SetCurrentLayoutByName( 704 bool XkbKeyboardLayoutEngine::SetCurrentLayoutByName(
648 const std::string& layout_name) { 705 const std::string& layout_name) {
649 #if defined(OS_CHROMEOS) 706 #if defined(OS_CHROMEOS)
650 size_t dash_index = layout_name.find('-'); 707 xkb_keymap* keymap = xkb_keymaps_[layout_name];
651 size_t parentheses_index = layout_name.find('(');
652 std::string layout_id = layout_name;
653 std::string layout_variant = "";
654 if (parentheses_index != std::string::npos) {
655 layout_id = layout_name.substr(0, parentheses_index);
656 size_t close_index = layout_name.find(')', parentheses_index);
657 if (close_index == std::string::npos)
658 close_index = layout_name.size();
659 layout_variant = layout_name.substr(parentheses_index + 1,
660 close_index - parentheses_index - 1);
661 } else if (dash_index != std::string::npos) {
662 layout_id = layout_name.substr(0, dash_index);
663 layout_variant = layout_name.substr(dash_index + 1);
664 }
665 struct xkb_rule_names names = {
666 .rules = NULL,
667 .model = "pc101",
668 .layout = layout_id.c_str(),
669 .variant = layout_variant.c_str(),
670 .options = ""
671 };
672 xkb_keymap* keymap = xkb_keymap_new_from_names(xkb_context_.get(),
673 &names,
674 XKB_KEYMAP_COMPILE_NO_FLAGS);
675 if (keymap) { 708 if (keymap) {
676 SetKeymap(keymap); 709 SetKeymap(keymap);
677 return true; 710 } else {
711 XkbKeymapLoader* keymap_loader = new XkbKeymapLoader(this);
Shu Chen 2014/12/23 08:46:49 nowhere to delete keymap_loader? Suggest to make i
FengYuan 2014/12/23 15:24:57 Done.
712 keymap_loader->loadKeymap(layout_name);
678 } 713 }
714 return true;
679 #endif // defined(OS_CHROMEOS) 715 #endif // defined(OS_CHROMEOS)
680 return false; 716 return false;
681 } 717 }
682 718
683 bool XkbKeyboardLayoutEngine::UsesISOLevel5Shift() const { 719 bool XkbKeyboardLayoutEngine::UsesISOLevel5Shift() const {
684 // NOTIMPLEMENTED(); 720 // NOTIMPLEMENTED();
685 return false; 721 return false;
686 } 722 }
687 723
688 bool XkbKeyboardLayoutEngine::UsesAltGr() const { 724 bool XkbKeyboardLayoutEngine::UsesAltGr() const {
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
869 if (flags == base_flags) 905 if (flags == base_flags)
870 return base_character; 906 return base_character;
871 xkb_keysym_t keysym; 907 xkb_keysym_t keysym;
872 base::char16 character = 0; 908 base::char16 character = 0;
873 if (!XkbLookup(xkb_keycode, flags, &keysym, &character)) 909 if (!XkbLookup(xkb_keycode, flags, &keysym, &character))
874 character = kNone; 910 character = kNone;
875 return character; 911 return character;
876 } 912 }
877 913
878 } // namespace ui 914 } // namespace ui
OLDNEW
« no previous file with comments | « ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698