1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #include "session/key_info_util.h"
31
32 #include <algorithm>
33 #include <memory>
34 #include <sstream>
35 #include <string>
36 #include <vector>
37
38 #include "base/config_file_stream.h"
39 #include "base/logging.h"
40 #include "base/port.h"
41 #include "base/util.h"
42 #include "composer/key_parser.h"
43 #include "config/config_handler.h"
44 #include "protocol/commands.pb.h"
45 #include "protocol/config.pb.h"
46 #include "session/internal/keymap.h"
47
48 namespace mozc {
49 using commands::KeyEvent;
50
51 namespace {
52
53 using config::Config;
54
ExtractSortedDirectModeKeysFromStream(std::istream * ifs)55 std::vector<KeyInformation> ExtractSortedDirectModeKeysFromStream(
56 std::istream *ifs) {
57 const char kModeDirect[] = "Direct";
58 const char kModeDirectInput[] = "DirectInput";
59
60 std::vector<KeyInformation> result;
61
62 string line;
63 getline(*ifs, line); // Skip the first line.
64 while (!ifs->eof()) {
65 getline(*ifs, line);
66 Util::ChopReturns(&line);
67 if (line.empty() || line[0] == '#') {
68 // empty or comment
69 continue;
70 }
71 std::vector<string> rules;
72 Util::SplitStringUsing(line, "\t", &rules);
73 if (rules.size() != 3) {
74 LOG(ERROR) << "Invalid format: " << line;
75 continue;
76 }
77 if (!(rules[0] == kModeDirect || rules[0] == kModeDirectInput)) {
78 continue;
79 }
80 commands::KeyEvent key_event;
81 if (KeyParser::ParseKey(rules[1], &key_event)) {
82 KeyInformation info;
83 if (KeyEventUtil::GetKeyInformation(key_event, &info)) {
84 result.push_back(info);
85 }
86 }
87 }
88
89 std::sort(result.begin(), result.end());
90 return result;
91 }
92
ExtractSortedDirectModeKeysFromFile(const string & filename)93 std::vector<KeyInformation> ExtractSortedDirectModeKeysFromFile(
94 const string &filename) {
95 std::unique_ptr<std::istream> ifs(ConfigFileStream::LegacyOpen(filename));
96 if (ifs.get() == NULL) {
97 DLOG(FATAL) << "could not open file: " << filename;
98 return std::vector<KeyInformation>();
99 }
100 return ExtractSortedDirectModeKeysFromStream(ifs.get());
101 }
102
103 } // namespace
104
ExtractSortedDirectModeKeys(const config::Config & config)105 std::vector<KeyInformation> KeyInfoUtil::ExtractSortedDirectModeKeys(
106 const config::Config &config) {
107 const config::Config::SessionKeymap &keymap = config.session_keymap();
108 if (keymap == Config::CUSTOM) {
109 const string &custom_keymap_table = config.custom_keymap_table();
110 if (custom_keymap_table.empty()) {
111 LOG(WARNING) << "custom_keymap_table is empty. use default setting";
112 const char *default_keymapfile = keymap::KeyMapManager::GetKeyMapFileName(
113 config::ConfigHandler::GetDefaultKeyMap());
114 return ExtractSortedDirectModeKeysFromFile(default_keymapfile);
115 }
116 std::istringstream ifs(custom_keymap_table);
117 return ExtractSortedDirectModeKeysFromStream(&ifs);
118 }
119 const char *keymap_file = keymap::KeyMapManager::GetKeyMapFileName(keymap);
120 return ExtractSortedDirectModeKeysFromFile(keymap_file);
121 }
122
ContainsKey(const std::vector<KeyInformation> & sorted_keys,const commands::KeyEvent & key_event)123 bool KeyInfoUtil::ContainsKey(const std::vector<KeyInformation> &sorted_keys,
124 const commands::KeyEvent &key_event) {
125 KeyInformation key_info;
126 if (!KeyEventUtil::GetKeyInformation(key_event, &key_info)) {
127 return false;
128 }
129 return std::binary_search(sorted_keys.begin(), sorted_keys.end(), key_info);
130 }
131
132 } // namespace mozc
133