Debian Codegraph @

unikey/usrkeymap.cpp

raw
1 /*
2 * SPDX-FileCopyrightText: 2000-2005 Pham Kim Long <[email protected]>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "usrkeymap.h"
8 #include <cstdint>
9 #include <cstdio>
10 #include <cstring>
11 #include <fcitx-utils/fdstreambuf.h>
12 #include <fcitx-utils/log.h>
13 #include <fcitx-utils/misc.h>
14 #include <fcitx-utils/stringutils.h>
15 #include <istream>
16 #include <string>
17 #include <vector>
18
19 namespace {
20
21 constexpr char OPT_COMMENT_CHAR = ';';
22
23 struct UkEventLabelPair {
24 char label[32];
25 int ev;
26 };
27
28 const char *UkKeyMapHeader = "; This is UniKey user-defined key mapping file, "
29 "generated from UniKey (Fcitx 5)\n\n";
30
31 constexpr UkEventLabelPair UkEvLabelList[] = {
32 {"Tone0", vneTone0}, {"Tone1", vneTone1},
33 {"Tone2", vneTone2}, {"Tone3", vneTone3},
34 {"Tone4", vneTone4}, {"Tone5", vneTone5},
35 {"Roof-All", vneRoofAll}, {"Roof-A", vneRoof_a},
36 {"Roof-E", vneRoof_e}, {"Roof-O", vneRoof_o},
37 {"Hook-Bowl", vneHookAll}, {"Hook-UO", vneHook_uo},
38 {"Hook-U", vneHook_u}, {"Hook-O", vneHook_o},
39 {"Bowl", vneBowl}, {"D-Mark", vneDd},
40 {"Telex-W", vne_telex_w}, {"Escape", vneEscChar},
41 {"DD", vneCount + vnl_DD}, {"dd", vneCount + vnl_dd},
42 {"A^", vneCount + vnl_Ar}, {"a^", vneCount + vnl_ar},
43 {"A(", vneCount + vnl_Ab}, {"a(", vneCount + vnl_ab},
44 {"E^", vneCount + vnl_Er}, {"e^", vneCount + vnl_er},
45 {"O^", vneCount + vnl_Or}, {"o^", vneCount + vnl_or},
46 {"O+", vneCount + vnl_Oh}, {"o+", vneCount + vnl_oh},
47 {"U+", vneCount + vnl_Uh}, {"u+", vneCount + vnl_uh}};
48
49 constexpr auto UkEvLabelCount = FCITX_ARRAY_SIZE(UkEvLabelList);
50
51 //-------------------------------------------
52 void initKeyMap(int keyMap[256]) {
53 unsigned int c;
54 for (c = 0; c < 256; c++)
55 keyMap[c] = vneNormal;
56 }
57
58 int getLabelIndex(int event) {
59 for (size_t i = 0; i < UkEvLabelCount; i++) {
60 if (UkEvLabelList[i].ev == event)
61 return i;
62 }
63 return -1;
64 }
65
66 } // namespace
67
68 //--------------------------------------------------
69 static bool parseNameValue(std::string_view line, std::string_view *name,
70 std::string_view *value) {
71 if (line.empty()) {
72 return false;
73 }
74
75 // get rid of comment
76 auto pos = line.find(OPT_COMMENT_CHAR);
77 if (pos != std::string::npos) {
78 line = line.substr(0, pos);
79 }
80 if (line.empty()) {
81 return false;
82 }
83
84 pos = line.find('=');
85 if (pos == std::string::npos) {
86 return false;
87 }
88 auto k = fcitx::stringutils::trimView(line.substr(0, pos));
89 auto v = fcitx::stringutils::trimView(line.substr(pos + 1));
90 if (k.empty() || v.empty()) {
91 return false;
92 }
93
94 *name = k;
95 *value = v;
96 return true;
97 }
98
99 //-----------------------------------------------------
100 DllExport void UkLoadKeyMap(int fd, int keyMap[256]) {
101 std::vector<UkKeyMapping> orderMap = UkLoadKeyOrderMap(fd);
102 initKeyMap(keyMap);
103 for (const auto &item : orderMap) {
104 keyMap[item.key] = item.action;
105 if (item.action < vneCount) {
106 keyMap[tolower(item.key)] = item.action;
107 }
108 }
109 }
110
111 //------------------------------------------------------------------
112 DllExport std::vector<UkKeyMapping> UkLoadKeyOrderMap(int fd) {
113 size_t lineCount = 0;
114 int keyMap[256];
115
116 initKeyMap(keyMap);
117
118 std::vector<UkKeyMapping> pMap;
119 fcitx::IFDStreamBuf buf(fd);
120 std::istream in(&buf);
121 std::string line;
122 while (std::getline(in, line)) {
123 lineCount++;
124 auto text = fcitx::stringutils::trimView(line);
125 if (text.empty()) {
126 continue;
127 }
128 std::string_view name, value;
129 if (parseNameValue(text, &name, &value)) {
130 if (name.size() != 1) {
131 FCITX_ERROR() << "Error in user key layout, line " << lineCount
132 << ": key name is not a single character";
133 continue;
134 }
135 size_t i = 0;
136 for (; i < UkEvLabelCount; i++) {
137 if (UkEvLabelList[i].label == value) {
138 break;
139 }
140 }
141 if (i == UkEvLabelCount) {
142 FCITX_ERROR() << "Error in user key layout, line " << lineCount
143 << ": command not found";
144 continue;
145 }
146
147 auto c = static_cast<uint8_t>(name[0]);
148 if (keyMap[c] != vneNormal) {
149 // already assigned, don't accept this map
150 break;
151 }
152 // cout << "key: " << c << " value: " <<
153 // UkEvLabelList[i].ev << endl; //DEBUG
154 keyMap[c] = UkEvLabelList[i].ev;
155 UkKeyMapping newPair;
156 newPair.action = UkEvLabelList[i].ev;
157 if (keyMap[c] < vneCount) {
158 newPair.key = toupper(c);
159 keyMap[toupper(c)] = UkEvLabelList[i].ev;
160 } else {
161 newPair.key = c;
162 }
163 pMap.push_back(newPair);
164 }
165 }
166 return pMap;
167 }
168
169 DllExport void UkStoreKeyOrderMap(FILE *f,
170 const std::vector<UkKeyMapping> &pMap) {
171 int labelIndex;
172
173 fputs(UkKeyMapHeader, f);
174 for (const auto &item : pMap) {
175 labelIndex = getLabelIndex(item.action);
176 if (labelIndex != -1) {
177 fprintf(f, "%c = %s\n", item.key, UkEvLabelList[labelIndex].label);
178 }
179 }
180 }