| 1 |
#include "varnam_state.h" |
| 2 |
#include "varnam_candidate.h" |
| 3 |
#include "varnam_engine.h" |
| 4 |
#include "varnam_utils.h" |
| 5 |
|
| 6 |
#include <fcitx-utils/capabilityflags.h> |
| 7 |
#include <fcitx-utils/key.h> |
| 8 |
#include <fcitx-utils/keysymgen.h> |
| 9 |
#include <fcitx-utils/stringutils.h> |
| 10 |
#include <fcitx-utils/utf8.h> |
| 11 |
#include <fcitx/inputpanel.h> |
| 12 |
|
| 13 |
#include <iterator> |
| 14 |
#include <limits> |
| 15 |
#include <sstream> |
| 16 |
#include <string> |
| 17 |
#include <thread> |
| 18 |
|
| 19 |
namespace fcitx { |
| 20 |
|
| 21 |
VarnamState::VarnamState(VarnamEngine *engine, InputContext &ic) |
| 22 |
: ic_(&ic), engine_(engine) { |
| 23 |
result_ = nullptr; |
| 24 |
cursor = std::numeric_limits<unsigned int>::max(); |
| 25 |
candidateSelected = 0; |
| 26 |
lastTypedCharIsDigit = false; |
| 27 |
} |
| 28 |
|
| 29 |
VarnamState::~VarnamState() { |
| 30 |
if (result_) { |
| 31 |
varray_free(result_, nullptr); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
std::string VarnamState::bufferToString() { |
| 36 |
std::string str; |
| 37 |
std::stringstream strstream; |
| 38 |
for (auto ch : buffer_) { |
| 39 |
strstream << ch; |
| 40 |
} |
| 41 |
str.assign(strstream.str()); |
| 42 |
return str; |
| 43 |
} |
| 44 |
|
| 45 |
void VarnamState::updatePreeditCursor() { |
| 46 |
if (cursor > preedit_.textLength()) { |
| 47 |
cursor = preedit_.textLength(); |
| 48 |
} |
| 49 |
preedit_.setCursor(cursor); |
| 50 |
if (ic_->capabilityFlags().test(CapabilityFlag::Preedit)) { |
| 51 |
ic_->inputPanel().setClientPreedit(preedit_); |
| 52 |
} else { |
| 53 |
ic_->inputPanel().setPreedit(preedit_); |
| 54 |
} |
| 55 |
ic_->updatePreedit(); |
| 56 |
ic_->updateUserInterface(UserInterfaceComponent::InputPanel); |
| 57 |
} |
| 58 |
|
| 59 |
bool VarnamState::getVarnamResult() { |
| 60 |
std::string preedit = bufferToString(); |
| 61 |
#ifdef DEBUG_MODE |
| 62 |
VARNAM_INFO() << "transliterate preedit:" << preedit; |
| 63 |
#endif |
| 64 |
int rv = VARNAM_SUCCESS; |
| 65 |
rv = varnam_transliterate(engine_->getVarnamHandle(), 1, |
| 66 |
(char *)preedit.c_str(), &result_); |
| 67 |
if (rv != VARNAM_SUCCESS) { |
| 68 |
VARNAM_WARN() << "varnam transliterate failed! err:" << rv; |
| 69 |
return false; |
| 70 |
} |
| 71 |
return true; |
| 72 |
} |
| 73 |
|
| 74 |
void VarnamState::processKeyEvent(KeyEvent &keyEvent) { |
| 75 |
#ifdef DEBUG_MODE |
| 76 |
VARNAM_INFO() << "rcvd key:" |
| 77 |
<< keyEvent.key().toString(KeyStringFormat::Localized); |
| 78 |
#endif |
| 79 |
auto key = keyEvent.key(); |
| 80 |
|
| 81 |
// filter modififer keys |
| 82 |
if (key.checkKeyList(keyListToFilter)) { |
| 83 |
keyEvent.filter(); |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
// handle candidate selection through index key |
| 88 |
if (!buffer_.empty() && key.isDigit() && !lastTypedCharIsDigit) { |
| 89 |
auto idx = key.keyListIndex(selectionKeys); |
| 90 |
selectCandidate(idx); |
| 91 |
commitText(key.sym()); |
| 92 |
updateUI(); |
| 93 |
keyEvent.filterAndAccept(); |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
if (key.states().test(KeyState::Ctrl)) { |
| 98 |
if (buffer_.empty()) { |
| 99 |
keyEvent.filter(); |
| 100 |
return; |
| 101 |
} |
| 102 |
if (key.sym() == FcitxKey_Delete) { |
| 103 |
auto candidates = ic_->inputPanel().candidateList(); |
| 104 |
std::string wordToUnlearn( |
| 105 |
candidates->candidate(candidateSelected) |
| 106 |
.text() |
| 107 |
.toStringForCommit()); // TODO try unique_ptr<char[]> |
| 108 |
if (wordToUnlearn.empty()) { |
| 109 |
keyEvent.filter(); |
| 110 |
return; |
| 111 |
} |
| 112 |
#ifdef DEBUG_MODE |
| 113 |
VARNAM_INFO() << "unlearn word:" << wordToUnlearn; |
| 114 |
#endif |
| 115 |
std::thread unlearnThread(varnam_unlearn_word, engine_->getVarnamHandle(), |
| 116 |
std::move(wordToUnlearn)); |
| 117 |
unlearnThread.detach(); |
| 118 |
reset(); |
| 119 |
updateUI(); |
| 120 |
keyEvent.filterAndAccept(); |
| 121 |
return; |
| 122 |
} |
| 123 |
keyEvent.filter(); |
| 124 |
return; |
| 125 |
} |
| 126 |
if (key.checkKeyList(engine_->getConfig()->nextCandidate.value())) { |
| 127 |
if (buffer_.empty()) { |
| 128 |
keyEvent.filter(); |
| 129 |
return; |
| 130 |
} |
| 131 |
updateLookupTable(NEXT_CANDIDATE); |
| 132 |
keyEvent.filterAndAccept(); |
| 133 |
return; |
| 134 |
} |
| 135 |
if (key.checkKeyList(engine_->getConfig()->prevCandidate.value())) { |
| 136 |
if (buffer_.empty()) { |
| 137 |
keyEvent.filter(); |
| 138 |
return; |
| 139 |
} |
| 140 |
updateLookupTable(PREV_CANDIDATE); |
| 141 |
keyEvent.filterAndAccept(); |
| 142 |
return; |
| 143 |
} |
| 144 |
if (key.checkKeyList(engine_->getConfig()->nextPage.value())) { |
| 145 |
if (buffer_.empty()) { |
| 146 |
keyEvent.filter(); |
| 147 |
return; |
| 148 |
} |
| 149 |
updateLookupTable(NEXT_PAGE); |
| 150 |
keyEvent.filterAndAccept(); |
| 151 |
return; |
| 152 |
} |
| 153 |
if (key.checkKeyList(engine_->getConfig()->prevPage.value())) { |
| 154 |
if (buffer_.empty()) { |
| 155 |
keyEvent.filter(); |
| 156 |
return; |
| 157 |
} |
| 158 |
updateLookupTable(PREV_PAGE); |
| 159 |
keyEvent.filterAndAccept(); |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
auto iterator = buffer_.begin(); |
| 164 |
iterator += cursor; |
| 165 |
|
| 166 |
switch (key.sym()) { |
| 167 |
case FcitxKey_Escape: |
| 168 |
case FcitxKey_space: |
| 169 |
case FcitxKey_Tab: |
| 170 |
case FcitxKey_Return: |
| 171 |
if (buffer_.empty()) { |
| 172 |
keyEvent.filter(); |
| 173 |
return; |
| 174 |
} |
| 175 |
commitText(key.sym()); |
| 176 |
updateUI(); |
| 177 |
keyEvent.filterAndAccept(); |
| 178 |
return; |
| 179 |
case FcitxKey_Left: |
| 180 |
if (preedit_.empty()) { |
| 181 |
keyEvent.filter(); |
| 182 |
return; |
| 183 |
} |
| 184 |
if (cursor > 0) { |
| 185 |
--cursor; |
| 186 |
} |
| 187 |
updatePreeditCursor(); |
| 188 |
keyEvent.filterAndAccept(); |
| 189 |
return; |
| 190 |
case FcitxKey_Right: |
| 191 |
if (preedit_.empty()) { |
| 192 |
keyEvent.filter(); |
| 193 |
return; |
| 194 |
} |
| 195 |
if (cursor < (buffer_.size())) { |
| 196 |
++cursor; |
| 197 |
} |
| 198 |
updatePreeditCursor(); |
| 199 |
keyEvent.filterAndAccept(); |
| 200 |
return; |
| 201 |
case FcitxKey_Up: |
| 202 |
case FcitxKey_Down: |
| 203 |
keyEvent.filter(); |
| 204 |
return; |
| 205 |
case FcitxKey_BackSpace: |
| 206 |
if (buffer_.empty()) { |
| 207 |
keyEvent.filter(); |
| 208 |
return; |
| 209 |
} |
| 210 |
if (cursor > 0) { |
| 211 |
buffer_.erase(--iterator); |
| 212 |
--cursor; |
| 213 |
} |
| 214 |
getVarnamResult(); |
| 215 |
updateUI(); |
| 216 |
keyEvent.filterAndAccept(); |
| 217 |
return; |
| 218 |
case FcitxKey_Delete: |
| 219 |
if (buffer_.empty()) { |
| 220 |
keyEvent.filter(); |
| 221 |
return; |
| 222 |
} |
| 223 |
if (cursor < buffer_.size()) { |
| 224 |
buffer_.erase(iterator); |
| 225 |
} |
| 226 |
getVarnamResult(); |
| 227 |
updateUI(); |
| 228 |
keyEvent.filterAndAccept(); |
| 229 |
return; |
| 230 |
case FcitxKey_Home: |
| 231 |
if (buffer_.empty()) { |
| 232 |
keyEvent.filter(); |
| 233 |
return; |
| 234 |
} |
| 235 |
cursor = 0; |
| 236 |
updatePreeditCursor(); |
| 237 |
keyEvent.filterAndAccept(); |
| 238 |
return; |
| 239 |
case FcitxKey_End: |
| 240 |
if (buffer_.empty()) { |
| 241 |
keyEvent.filter(); |
| 242 |
return; |
| 243 |
} |
| 244 |
cursor = preedit_.textLength(); |
| 245 |
updatePreeditCursor(); |
| 246 |
keyEvent.filterAndAccept(); |
| 247 |
return; |
| 248 |
default: |
| 249 |
break; |
| 250 |
} |
| 251 |
|
| 252 |
if (key.isDigit()) { |
| 253 |
lastTypedCharIsDigit = true; |
| 254 |
} else { |
| 255 |
lastTypedCharIsDigit = false; |
| 256 |
} |
| 257 |
|
| 258 |
if (isWordBreak(keyEvent.key().sym())) { |
| 259 |
commitText(keyEvent.key().sym()); |
| 260 |
updateUI(); |
| 261 |
keyEvent.filterAndAccept(); |
| 262 |
return; |
| 263 |
} |
| 264 |
|
| 265 |
if (key.sym() > 0x80) { |
| 266 |
keyEvent.filter(); |
| 267 |
return; |
| 268 |
} |
| 269 |
|
| 270 |
unsigned char input = |
| 271 |
*(keyEvent.key().toString(KeyStringFormat::Localized).c_str()); |
| 272 |
#ifdef DEBUG_MODE |
| 273 |
VARNAM_INFO() << "cursor at:" << cursor; |
| 274 |
#endif |
| 275 |
if (cursor >= buffer_.size()) { |
| 276 |
buffer_.push_back(input); |
| 277 |
cursor = buffer_.size(); |
| 278 |
} else { |
| 279 |
buffer_.insert(iterator, input); |
| 280 |
++cursor; |
| 281 |
} |
| 282 |
|
| 283 |
if (result_) { |
| 284 |
varray_clear(result_); |
| 285 |
} else { |
| 286 |
result_ = varray_init(); |
| 287 |
} |
| 288 |
|
| 289 |
getVarnamResult(); |
| 290 |
updateUI(); |
| 291 |
keyEvent.filterAndAccept(); |
| 292 |
} |
| 293 |
|
| 294 |
void VarnamState::setLookupTable() { |
| 295 |
if (!result_) { |
| 296 |
return; |
| 297 |
} |
| 298 |
auto candidates = std::make_unique<VarnamCandidateList>(engine_, ic_); |
| 299 |
candidates->setSelectionKey(selectionKeys); |
| 300 |
candidates->setCursorPositionAfterPaging( |
| 301 |
CursorPositionAfterPaging::ResetToFirst); |
| 302 |
candidates->setPageSize(engine_->getConfig()->pageSize.value()); |
| 303 |
|
| 304 |
int count = varray_length(result_); |
| 305 |
char preeditAppended = 0; |
| 306 |
for (int i = 0; i < count; i++) { |
| 307 |
if ((candidates->pageSize() == 10) && |
| 308 |
((i + (preeditAppended ? (1 + preeditAppended) : 1)) % 10 == 0)) { |
| 309 |
// TODO ;} |
| 310 |
candidates->append<VarnamCandidateWord>(engine_, |
| 311 |
preedit_.toString().c_str(), i); |
| 312 |
++preeditAppended; |
| 313 |
} |
| 314 |
vword *word = static_cast<vword *>(varray_get(result_, i)); |
| 315 |
candidates->append<VarnamCandidateWord>(engine_, word->text, |
| 316 |
preeditAppended ? i + 1 : i); |
| 317 |
} |
| 318 |
if (!preeditAppended) { |
| 319 |
candidates->append<VarnamCandidateWord>( |
| 320 |
engine_, preedit_.toString().c_str(), ++count); |
| 321 |
} |
| 322 |
if (count) { |
| 323 |
candidates->setGlobalCursorIndex(0); |
| 324 |
ic_->inputPanel().setCandidateList(std::move(candidates)); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
void VarnamState::updateLookupTable(const PageAction &action) { |
| 329 |
auto candidates = ic_->inputPanel().candidateList(); |
| 330 |
if (candidates == nullptr) { |
| 331 |
return; |
| 332 |
} |
| 333 |
if (candidates->empty()) { |
| 334 |
return; |
| 335 |
} |
| 336 |
switch (action) { |
| 337 |
case PREV_PAGE: |
| 338 |
candidates->toPageable()->prev(); |
| 339 |
break; |
| 340 |
case NEXT_PAGE: |
| 341 |
candidates->toPageable()->next(); |
| 342 |
break; |
| 343 |
case PREV_CANDIDATE: |
| 344 |
candidates->toCursorMovable()->prevCandidate(); |
| 345 |
break; |
| 346 |
case NEXT_CANDIDATE: |
| 347 |
candidates->toCursorMovable()->nextCandidate(); |
| 348 |
break; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
void VarnamState::selectCandidate(int index) { |
| 353 |
auto candidateList_ = ic_->inputPanel().candidateList(); |
| 354 |
#ifdef DEBUG_MODE |
| 355 |
VARNAM_INFO() << "select candidate at :" << index; |
| 356 |
#endif |
| 357 |
if (!candidateList_ || candidateList_->size() <= index) { |
| 358 |
return; |
| 359 |
} |
| 360 |
if (index == 9) { |
| 361 |
candidateSelected = 0; |
| 362 |
} else { |
| 363 |
if (candidateList_->size() <= index) { |
| 364 |
return; |
| 365 |
} |
| 366 |
candidateSelected = index; |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
void VarnamState::commitText(const FcitxKeySym &key) { |
| 371 |
auto candidates = ic_->inputPanel().candidateList(); |
| 372 |
std::string stringToCommit; |
| 373 |
|
| 374 |
if (key == FcitxKey_Escape || key == FcitxKey_0 || candidates == nullptr || |
| 375 |
candidates->size() <= 1 || result_ == nullptr || |
| 376 |
varray_is_empty(result_) || lastTypedCharIsDigit) { |
| 377 |
|
| 378 |
stringToCommit.assign(preedit_.toStringForCommit()); |
| 379 |
candidateSelected = 0; |
| 380 |
} else if ((candidates->cursorIndex() <= 0) && !candidateSelected) { |
| 381 |
vword *first_result = static_cast<vword *>(varray_get(result_, 0)); |
| 382 |
if (first_result != nullptr) { |
| 383 |
stringToCommit.assign(first_result->text); |
| 384 |
candidateSelected = 1; |
| 385 |
} |
| 386 |
} else { |
| 387 |
stringToCommit.assign( |
| 388 |
candidates->candidate(candidateSelected).text().toStringForCommit()); |
| 389 |
} |
| 390 |
|
| 391 |
if (isWordBreak(key)) { |
| 392 |
stringToCommit = stringutils::concat(stringToCommit, getWordBreakChar(key)); |
| 393 |
} |
| 394 |
#ifdef DEBUG_MODE |
| 395 |
VARNAM_INFO() << "string to commit:" << stringToCommit << " " |
| 396 |
<< getWordBreakChar(key); |
| 397 |
#endif |
| 398 |
ic_->commitString(stringToCommit); |
| 399 |
|
| 400 |
if (stringToCommit.empty() || lastTypedCharIsDigit || |
| 401 |
ic_->capabilityFlags().test(CapabilityFlag::PasswordOrSensitive) || |
| 402 |
!engine_->getConfig()->shouldLearnWords.value() || !candidateSelected) { |
| 403 |
reset(); |
| 404 |
return; |
| 405 |
} |
| 406 |
#ifdef DEBUG_MODE |
| 407 |
VARNAM_INFO() << "learn word:" << stringToCommit; |
| 408 |
#endif |
| 409 |
std::string wordToLearn(stringToCommit); // [TODO] try unique_ptr<char[]> |
| 410 |
std::thread learnThread(varnam_learn_word, engine_->getVarnamHandle(), |
| 411 |
std::move(wordToLearn), 0); |
| 412 |
learnThread.detach(); |
| 413 |
reset(); |
| 414 |
} |
| 415 |
|
| 416 |
void VarnamState::updateUI() { |
| 417 |
ic_->inputPanel().reset(); |
| 418 |
if (buffer_.empty()) { |
| 419 |
ic_->updatePreedit(); |
| 420 |
ic_->updateUserInterface(UserInterfaceComponent::InputPanel); |
| 421 |
return; |
| 422 |
} |
| 423 |
if (result_ == nullptr) { |
| 424 |
return; |
| 425 |
} |
| 426 |
if (varray_length(result_) == 0x00) { |
| 427 |
return; |
| 428 |
} |
| 429 |
|
| 430 |
preedit_.clear(); |
| 431 |
preedit_.append(bufferToString(), TextFormatFlag::HighLight); |
| 432 |
if (cursor > preedit_.textLength()) { |
| 433 |
cursor = preedit_.textLength(); |
| 434 |
} |
| 435 |
preedit_.setCursor(cursor); |
| 436 |
|
| 437 |
if (ic_->capabilityFlags().test(CapabilityFlag::Preedit)) { |
| 438 |
ic_->inputPanel().setClientPreedit(preedit_); |
| 439 |
} else { |
| 440 |
ic_->inputPanel().setPreedit(preedit_); |
| 441 |
} |
| 442 |
ic_->updatePreedit(); |
| 443 |
setLookupTable(); |
| 444 |
ic_->updateUserInterface(UserInterfaceComponent::InputPanel); |
| 445 |
} |
| 446 |
|
| 447 |
void VarnamState::reset() { |
| 448 |
cursor = std::numeric_limits<unsigned int>::max(); |
| 449 |
candidateSelected = 0; |
| 450 |
lastTypedCharIsDigit = false; |
| 451 |
buffer_.clear(); |
| 452 |
preedit_.clear(); |
| 453 |
if (result_) { |
| 454 |
varray_clear(result_); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
} // namespace fcitx |