-
Notifications
You must be signed in to change notification settings - Fork 3
/
quwei.cpp
276 lines (243 loc) · 8.51 KB
/
quwei.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* SPDX-FileCopyrightText: 2021~2021 CSSlayer <wengxt@gmail.com>
*
* SPDX-License-Identifier: BSD-3-Clause
*
*/
#include "quwei.h"
#include <fcitx-utils/utf8.h>
#include <fcitx/candidatelist.h>
#include <fcitx/inputpanel.h>
#include <fcitx/instance.h>
#include <fcitx/userinterfacemanager.h>
#include <utility>
namespace {
// Template to help resolve iconv parameter issue on BSD.
template <class T>
struct function_traits;
// partial specialization for function pointer
template <class R, class... Args>
struct function_traits<R (*)(Args...)> {
using result_type = R;
using argument_types = std::tuple<Args...>;
};
template <class T>
using second_argument_type = typename std::tuple_element<
1, typename function_traits<T>::argument_types>::type;
static const std::array<fcitx::Key, 10> selectionKeys = {
fcitx::Key{FcitxKey_1}, fcitx::Key{FcitxKey_2}, fcitx::Key{FcitxKey_3},
fcitx::Key{FcitxKey_4}, fcitx::Key{FcitxKey_5}, fcitx::Key{FcitxKey_6},
fcitx::Key{FcitxKey_7}, fcitx::Key{FcitxKey_8}, fcitx::Key{FcitxKey_9},
fcitx::Key{FcitxKey_0},
};
class QuweiCandidateWord : public fcitx::CandidateWord {
public:
QuweiCandidateWord(QuweiEngine *engine, std::string text)
: engine_(engine) {
setText(fcitx::Text(std::move(text)));
}
void select(fcitx::InputContext *inputContext) const override {
inputContext->commitString(text().toString());
auto state = inputContext->propertyFor(engine_->factory());
state->reset();
}
private:
QuweiEngine *engine_;
};
class QuweiCandidateList : public fcitx::CandidateList,
public fcitx::PageableCandidateList,
public fcitx::CursorMovableCandidateList {
public:
QuweiCandidateList(QuweiEngine *engine, fcitx::InputContext *ic,
const std::string &code)
: engine_(engine), ic_(ic), code_(std::stoi(code)) {
setPageable(this);
setCursorMovable(this);
for (int i = 0; i < 10; i++) {
const char label[2] = {static_cast<char>('0' + (i + 1) % 10), '\0'};
labels_[i].append(label);
labels_[i].append(". ");
}
generate();
}
const fcitx::Text &label(int idx) const override { return labels_[idx]; }
const fcitx::CandidateWord &candidate(int idx) const override {
return *candidates_[idx];
}
int size() const override { return 10; }
fcitx::CandidateLayoutHint layoutHint() const override {
return fcitx::CandidateLayoutHint::NotSet;
}
bool usedNextBefore() const override { return false; }
void prev() override {
if (!hasPrev()) {
return;
}
--code_;
auto state = ic_->propertyFor(engine_->factory());
state->setCode(code_);
}
void next() override {
if (!hasNext()) {
return;
}
code_++;
auto state = ic_->propertyFor(engine_->factory());
state->setCode(code_);
}
bool hasPrev() const override { return code_ > 0; }
bool hasNext() const override { return code_ < 999; }
void prevCandidate() override { cursor_ = (cursor_ + 9) % 10; }
void nextCandidate() override { cursor_ = (cursor_ + 1) % 10; }
int cursorIndex() const override { return cursor_; }
private:
void generate() {
for (int i = 0; i < 10; i++) {
auto code = code_ * 10 + (i + 1);
auto qu = code / 100;
auto wei = code % 100;
// Quwei to GB2312 (0xA0 + qu, 0xA0 + wei)
char in[3];
if (qu >= 95) { /* Process extend Qu 95 and 96 */
in[0] = qu - 95 + 0xA8;
in[1] = wei + 0x40;
/* skip 0xa87f and 0xa97f */
if (in[1] >= 0x7f) {
in[1]++;
}
} else {
in[0] = qu + 0xa0;
in[1] = wei + 0xa0;
}
size_t insize = 2, avail = FCITX_UTF8_MAX_LENGTH + 1;
std::remove_pointer_t<second_argument_type<decltype(&::iconv)>>
inbuf = in;
char out[FCITX_UTF8_MAX_LENGTH + 1];
char *outbuf = out;
iconv(engine_->conv(), &inbuf, &insize, &outbuf, &avail);
*outbuf = '\0';
candidates_[i] = std::make_unique<QuweiCandidateWord>(engine_, out);
}
}
QuweiEngine *engine_;
fcitx::InputContext *ic_;
fcitx::Text labels_[10];
std::unique_ptr<QuweiCandidateWord> candidates_[10];
int code_;
int cursor_ = 0;
};
} // namespace
void QuweiState::keyEvent(fcitx::KeyEvent &event) {
if (auto candidateList = ic_->inputPanel().candidateList()) {
int idx = event.key().keyListIndex(selectionKeys);
if (idx >= 0 && idx < candidateList->size()) {
event.accept();
candidateList->candidate(idx).select(ic_);
return;
}
if (event.key().checkKeyList(
engine_->instance()->globalConfig().defaultPrevPage())) {
if (auto *pageable = candidateList->toPageable();
pageable && pageable->hasPrev()) {
event.accept();
pageable->prev();
ic_->updateUserInterface(
fcitx::UserInterfaceComponent::InputPanel);
}
return event.filterAndAccept();
}
if (event.key().checkKeyList(
engine_->instance()->globalConfig().defaultNextPage())) {
if (auto *pageable = candidateList->toPageable();
pageable && pageable->hasNext()) {
pageable->next();
ic_->updateUserInterface(
fcitx::UserInterfaceComponent::InputPanel);
}
return event.filterAndAccept();
}
}
if (buffer_.empty()) {
if (!event.key().isDigit()) {
return;
}
} else {
if (event.key().check(FcitxKey_BackSpace)) {
buffer_.backspace();
updateUI();
return event.filterAndAccept();
}
if (event.key().check(FcitxKey_Return)) {
ic_->commitString(buffer_.userInput());
reset();
return event.filterAndAccept();
}
if (event.key().check(FcitxKey_Escape)) {
reset();
return event.filterAndAccept();
}
if (!event.key().isDigit()) {
return event.filterAndAccept();
}
}
buffer_.type(event.key().sym());
updateUI();
return event.filterAndAccept();
}
void QuweiState::setCode(int code) {
if (code < 0 || code > 999) {
return;
}
buffer_.clear();
auto codeStr = std::to_string(code);
while (codeStr.size() < 3) {
codeStr = "0" + codeStr;
}
buffer_.type(std::to_string(code));
updateUI();
}
void QuweiState::updateUI() {
auto &inputPanel = ic_->inputPanel();
inputPanel.reset();
if (buffer_.size() == 3) {
inputPanel.setCandidateList(std::make_unique<QuweiCandidateList>(
engine_, ic_, buffer_.userInput()));
}
if (ic_->capabilityFlags().test(fcitx::CapabilityFlag::Preedit)) {
fcitx::Text preedit(buffer_.userInput(),
fcitx::TextFormatFlag::HighLight);
inputPanel.setClientPreedit(preedit);
} else {
fcitx::Text preedit(buffer_.userInput());
inputPanel.setPreedit(preedit);
}
ic_->updateUserInterface(fcitx::UserInterfaceComponent::InputPanel);
ic_->updatePreedit();
}
QuweiEngine::QuweiEngine(fcitx::Instance *instance)
: instance_(instance), factory_([this](fcitx::InputContext &ic) {
return new QuweiState(this, &ic);
}) {
conv_ = iconv_open("UTF-8", "GB18030");
if (conv_ == reinterpret_cast<iconv_t>(-1)) {
throw std::runtime_error("Failed to create converter");
}
instance->inputContextManager().registerProperty("quweiState", &factory_);
}
void QuweiEngine::keyEvent(const fcitx::InputMethodEntry &entry,
fcitx::KeyEvent &keyEvent) {
FCITX_UNUSED(entry);
if (keyEvent.isRelease() || keyEvent.key().states()) {
return;
}
// FCITX_INFO() << keyEvent.key() << " isRelease=" << keyEvent.isRelease();
auto ic = keyEvent.inputContext();
auto *state = ic->propertyFor(&factory_);
state->keyEvent(keyEvent);
}
void QuweiEngine::reset(const fcitx::InputMethodEntry &,
fcitx::InputContextEvent &event) {
auto *state = event.inputContext()->propertyFor(&factory_);
state->reset();
}
FCITX_ADDON_FACTORY(QuweiEngineFactory);