File: engine.cpp

package info (click to toggle)
fcitx5-hangul 5.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 236 kB
  • sloc: cpp: 621; sh: 28; ansic: 9; makefile: 5
file content (606 lines) | stat: -rw-r--r-- 19,140 bytes parent folder | download
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*
 * SPDX-FileCopyrightText: 2010~2017 CSSlayer <wengxt@gmail.com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 */

#include "engine.h"
#include <fcitx-utils/standardpath.h>
#include <fcitx-utils/utf8.h>
#include <fcitx/action.h>
#include <fcitx/candidatelist.h>
#include <fcitx/inputcontext.h>
#include <fcitx/inputcontextmanager.h>
#include <fcitx/inputpanel.h>
#include <fcitx/statusarea.h>
#include <fcitx/userinterfacemanager.h>

static const char *keyboardId[] = {"2",  "2y", "39", "3f", "3s",
                                   "3y", "32", "ro", "ahn"};

constexpr auto MAX_LENGTH = 40;

namespace fcitx {

const KeyList &selectionKeys() {
    static const KeyList selectionKeys{
        Key(FcitxKey_1), Key(FcitxKey_2), Key(FcitxKey_3), Key(FcitxKey_4),
        Key(FcitxKey_5), Key(FcitxKey_6), Key(FcitxKey_7), Key(FcitxKey_8),
        Key(FcitxKey_9), Key(FcitxKey_0)};
    return selectionKeys;
}

std::string ustringToUTF8(const UString &ustr) {
    std::string result;
    for (auto c : ustr) {
        result += utf8::UCS4ToUTF8(c);
    }
    return result;
}

static std::string subUTF8String(const std::string &str, int p1, int p2) {
    int limit;
    int pos;
    int n;

    if (str.empty()) {
        return "";
    }

    limit = str.size() + 1;

    p1 = std::max(0, p1);
    p2 = std::max(0, p2);

    pos = std::min(p1, p2);
    n = std::abs(p2 - p1);

    if (pos + n > limit) {
        n = limit - pos;
    }

    auto begin = utf8::nextNChar(str.begin(), pos);
    auto end = utf8::nextNChar(begin, n);

    return std::string(begin, end);
}

class HangulCandidate : public CandidateWord {
public:
    HangulCandidate(HangulEngine *engine, int idx, std::string text)
        : engine_(engine), idx_(idx) {
        setText(Text(std::move(text)));
    }

    void select(InputContext *inputContext) const override;

private:
    HangulEngine *engine_;
    int idx_;
};

class HangulState : public InputContextProperty {
public:
    HangulState(HangulEngine *engine, InputContext *ic)
        : engine_(engine), ic_(ic) {
        configure();
    }

    void configure() {
        context_.reset(hangul_ic_new(
            keyboardId[static_cast<int>(*engine_->config().keyboard)]));
        hangul_ic_connect_callback(
            context_.get(), "transition",
            reinterpret_cast<void *>(&HangulState::onTransitionCallback), this);
    }

    static bool onTransitionCallback(HangulInputContext *, ucschar c,
                                     const ucschar *, void *data) {
        auto that = static_cast<HangulState *>(data);
        return that->onTransition(c);
    }

    bool onTransition(ucschar c) {
        if (!*engine_->config().autoReorder) {
            if (hangul_is_choseong(c)) {
                if (hangul_ic_has_jungseong(context_.get()) ||
                    hangul_ic_has_jongseong(context_.get()))
                    return false;
            }

            if (hangul_is_jungseong(c)) {
                if (hangul_ic_has_jongseong(context_.get()))
                    return false;
            }
        }

        return true;
    }

    void updateLookupTable(bool checkSurrounding) {
        std::string hanjaKey;
        LookupMethod lookupMethod = LOOKUP_METHOD_PREFIX;

        hanjaList_.reset();

        auto hic_preedit = hangul_ic_get_preedit_string(context_.get());
        UString preedit = preedit_;
        preedit.append(UString(hic_preedit));
        if (!preedit.empty()) {
            auto utf8 = ustringToUTF8(preedit);
            if (*engine_->config().wordCommit || *engine_->config().hanjaMode) {
                hanjaKey = utf8;
                lookupMethod = LOOKUP_METHOD_PREFIX;
            } else {
                auto cursorPos = ic_->surroundingText().cursor();
                auto substr =
                    subUTF8String(ic_->surroundingText().text(),
                                  static_cast<int>(cursorPos) - 64, cursorPos);

                if (!substr.empty()) {
                    hanjaKey = substr + utf8;
                } else {
                    hanjaKey = utf8;
                }
                lookupMethod = LOOKUP_METHOD_SUFFIX;
            }
        } else if (checkSurrounding) {

            if (!ic_->capabilityFlags().test(CapabilityFlag::SurroundingText) ||
                !ic_->surroundingText().isValid()) {
                return;
            }
            auto surroundingStr = ic_->surroundingText().text();
            auto cursorPos = ic_->surroundingText().cursor();
            auto anchorPos = ic_->surroundingText().anchor();
            if (cursorPos != anchorPos) {
                // If we have selection in surrounding text, we use that.
                hanjaKey = subUTF8String(surroundingStr, cursorPos, anchorPos);
                lookupMethod = LOOKUP_METHOD_EXACT;
            } else {
                hanjaKey =
                    subUTF8String(surroundingStr,
                                  static_cast<int>(cursorPos) - 64, cursorPos);
                lookupMethod = LOOKUP_METHOD_SUFFIX;
            }
        }

        if (!hanjaKey.empty()) {
            hanjaList_.reset(lookupTable(hanjaKey, lookupMethod));
            lastLookupMethod_ = lookupMethod;
        }
    }

    HanjaList *lookupTable(const std::string key, int method) {
        HanjaList *list = nullptr;

        if (key.empty())
            return nullptr;

        decltype(&hanja_table_match_exact) func = nullptr;

        switch (method) {
        case LOOKUP_METHOD_EXACT:
            func = &hanja_table_match_exact;
            break;
        case LOOKUP_METHOD_PREFIX:
            func = &hanja_table_match_prefix;
            break;
        case LOOKUP_METHOD_SUFFIX:
            func = &hanja_table_match_suffix;
            break;
        }
        if (!func) {
            return nullptr;
        }

        if (auto symbolTable = engine_->symbolTable()) {
            list = func(symbolTable, key.data());
        }

        if (!list) {
            list = func(engine_->table(), key.data());
        }

        return list;
    }

    void keyEvent(KeyEvent &keyEvent) {
        if (keyEvent.isRelease()) {
            return;
        }

        if (keyEvent.key().checkKeyList(
                *engine_->config().hanjaModeToggleKey)) {
            if (!hanjaList_) {
                updateLookupTable(true);
            } else {
                cleanup();
            }
            updateUI();
            return keyEvent.filterAndAccept();
        }

        auto sym = keyEvent.key().sym();

        if (sym == FcitxKey_Shift_L || sym == FcitxKey_Shift_R) {
            return;
        }

        KeyStates s;
        for (auto keyList :
             {&*engine_->config().hanjaModeToggleKey,
              &*engine_->config().prevPageKey, &*engine_->config().nextPageKey,
              &*engine_->config().prevCandidateKey,
              &*engine_->config().nextCandidateKey}) {
            for (auto key : *keyList) {
                s |= key.states();
            }
        }

        struct {
            KeyState state;
            KeySym left, right;
        } modifiers[] = {
            {KeyState::Ctrl, FcitxKey_Control_L, FcitxKey_Control_R},
            {KeyState::Alt, FcitxKey_Alt_L, FcitxKey_Alt_R},
            {KeyState::Shift, FcitxKey_Shift_L, FcitxKey_Shift_R},
            {KeyState::Super, FcitxKey_Super_L, FcitxKey_Super_R},
            {KeyState::Hyper, FcitxKey_Hyper_L, FcitxKey_Hyper_R},
        };

        for (auto &modifier : modifiers) {
            if (s & modifier.state) {
                if (sym == modifier.left || sym == modifier.right) {
                    return;
                }
            }
        }

        // Handle candidate selection.
        auto candList = ic_->inputPanel().candidateList();
        if (candList && candList->size() > 0) {
            if (keyEvent.key().checkKeyList(*engine_->config().prevPageKey)) {
                candList->toPageable()->prev();
                ic_->updateUserInterface(UserInterfaceComponent::InputPanel);
                return keyEvent.filterAndAccept();
            } else if (keyEvent.key().checkKeyList(
                           *engine_->config().nextPageKey)) {
                candList->toPageable()->next();
                ic_->updateUserInterface(UserInterfaceComponent::InputPanel);
                return keyEvent.filterAndAccept();
            }

            if (keyEvent.key().checkKeyList(
                    *engine_->config().prevCandidateKey)) {
                candList->toCursorMovable()->prevCandidate();
                ic_->updateUserInterface(UserInterfaceComponent::InputPanel);
                return keyEvent.filterAndAccept();
            } else if (keyEvent.key().checkKeyList(
                           *engine_->config().nextCandidateKey)) {
                candList->toCursorMovable()->nextCandidate();
                ic_->updateUserInterface(UserInterfaceComponent::InputPanel);
                return keyEvent.filterAndAccept();
            }

            auto idx = keyEvent.key().keyListIndex(selectionKeys());
            if (idx >= 0) {
                if (idx < candList->size()) {
                    candList->candidate(idx).select(ic_);
                }
                return keyEvent.filterAndAccept();
            }

            if (keyEvent.key().check(FcitxKey_Return)) {
                auto idx = candList->cursorIndex();
                if (idx < 0) {
                    idx = 0;
                }

                if (idx < candList->size()) {
                    candList->candidate(idx).select(ic_);
                    return keyEvent.filterAndAccept();
                }
            }

            if (!*engine_->config().hanjaMode) {
                cleanup();
            }
        }

        s = KeyStates{KeyState::Ctrl, KeyState::Alt, KeyState::Shift,
                      KeyState::Super, KeyState::Hyper};
        if (keyEvent.key().states() & s) {
            flush();
            updateUI();
            return;
        }

        bool keyUsed = false;
        if (keyEvent.key().check(FcitxKey_BackSpace)) {
            keyUsed = hangul_ic_backspace(context_.get());
            if (!keyUsed) {
                unsigned int preedit_len = preedit_.size();
                if (preedit_len > 0) {
                    preedit_.pop_back();
                    keyUsed = true;
                }
            }
        } else {
            if (preedit_.size() >= MAX_LENGTH) {
                flush();
            }

            keyUsed = hangul_ic_process(context_.get(), keyEvent.key().sym());
            bool notFlush = false;

            const ucschar *str = hangul_ic_get_commit_string(context_.get());
            if (*engine_->config().wordCommit || *engine_->config().hanjaMode) {
                const ucschar *hic_preedit;

                hic_preedit = hangul_ic_get_preedit_string(context_.get());
                if (hic_preedit != NULL && hic_preedit[0] != 0) {
                    preedit_.append(UString(str));
                } else {
                    preedit_.append(UString(str));
                    if (!preedit_.empty()) {
                        auto commit = ustringToUTF8(preedit_);
                        if (!commit.empty()) {
                            ic_->commitString(commit);
                        }
                    }
                    preedit_.clear();
                }
            } else {
                if (str != NULL && str[0] != 0) {
                    auto commit = ustringToUTF8(str);
                    if (!commit.empty()) {
                        ic_->commitString(commit);
                    }
                }
            }

            if (!keyUsed && !notFlush) {
                flush();
            }
        }

        if (*engine_->config().hanjaMode) {
            updateLookupTable(false);
        } else {
            cleanup();
        }

        updateUI();
        if (keyUsed) {
            keyEvent.filterAndAccept();
        }
    }

    void reset() {
        preedit_.clear();
        hangul_ic_reset(context_.get());
        hanjaList_.reset();
        updateUI();
    }

    void cleanup() { hanjaList_.reset(); }

    void flush() {
        cleanup();

        auto str = hangul_ic_flush(context_.get());

        preedit_ += str;

        if (preedit_.empty())
            return;

        auto utf8 = ustringToUTF8(preedit_);
        if (!utf8.empty()) {
            ic_->commitString(utf8);
        }

        preedit_.clear();
    }

    void updateUI() {
        const ucschar *hic_preedit =
            hangul_ic_get_preedit_string(context_.get());

        ic_->inputPanel().reset();

        auto pre1 = ustringToUTF8(preedit_);
        auto pre2 = hic_preedit ? ustringToUTF8(hic_preedit) : "";

        if (!pre1.empty() || !pre2.empty()) {
            Text text;
            text.append(pre1);
            text.append(pre2, TextFormatFlag::HighLight);
            text.setCursor(pre1.size() + pre2.size());
            if (ic_->capabilityFlags().test(CapabilityFlag::Preedit)) {
                ic_->inputPanel().setClientPreedit(text);
            } else {
                ic_->inputPanel().setPreedit(text);
            }
        }
        ic_->updatePreedit();

        setLookupTable();

        ic_->updateUserInterface(UserInterfaceComponent::InputPanel);
    }

    void setLookupTable() {
        if (!hanjaList_) {
            return;
        }
        HanjaList *list = hanjaList_.get();
        if (list) {
            auto candidate = std::make_unique<CommonCandidateList>();
            candidate->setSelectionKey(selectionKeys());
            candidate->setCursorPositionAfterPaging(
                CursorPositionAfterPaging::ResetToFirst);
            candidate->setPageSize(
                engine_->instance()->globalConfig().defaultPageSize());
            auto n = hanja_list_get_size(list);
            for (auto i = 0; i < n; i++) {
                const char *value = hanja_list_get_nth_value(list, i);
                candidate->append<HangulCandidate>(engine_, i, value);
            }
            if (n) {
                candidate->setGlobalCursorIndex(0);
                ic_->inputPanel().setCandidateList(std::move(candidate));
            }
        }
    }

    void select(int pos) {
        const char *key;
        const char *value;
        const ucschar *hic_preedit;
        int key_len;
        int preedit_len;
        int hic_preedit_len;

        key = hanja_list_get_nth_key(hanjaList_.get(), pos);
        value = hanja_list_get_nth_value(hanjaList_.get(), pos);
        hic_preedit = hangul_ic_get_preedit_string(context_.get());

        if (!key || !value || !hic_preedit) {
            return reset();
        }

        key_len = fcitx::utf8::length(std::string(key));
        preedit_len = preedit_.size();
        hic_preedit_len = UString(hic_preedit).size();

        bool surrounding = false;
        if (lastLookupMethod_ == LOOKUP_METHOD_PREFIX) {
            if (preedit_len == 0 && hic_preedit_len == 0) {
                /* remove surrounding_text */
                if (key_len > 0) {
                    ic_->deleteSurroundingText(-key_len, key_len);
                    surrounding = true;
                }
            } else {
                /* remove preedit text */
                if (key_len > 0) {
                    long n = std::min(key_len, preedit_len);
                    preedit_.erase(0, n);
                    key_len -= preedit_len;
                }

                /* remove hic preedit text */
                if (key_len > 0) {
                    hangul_ic_reset(context_.get());
                    key_len -= hic_preedit_len;
                }
            }
        } else {
            /* remove hic preedit text */
            if (hic_preedit_len > 0) {
                hangul_ic_reset(context_.get());
                key_len -= hic_preedit_len;
            }

            /* remove preedit text */
            if (key_len > preedit_len) {
                preedit_.erase(0, preedit_len);
                key_len -= preedit_len;
            } else if (key_len > 0) {
                preedit_.erase(0, key_len);
                key_len = 0;
            }

            /* remove surrounding_text */
            if (LOOKUP_METHOD_EXACT != lastLookupMethod_ && key_len > 0) {
                ic_->deleteSurroundingText(-key_len, key_len);
                surrounding = true;
            }
        }

        ic_->commitString(value);
        if (surrounding) {
            cleanup();
        }
        updateLookupTable(false);
        updateUI();
    }

private:
    HangulEngine *engine_;
    InputContext *ic_;
    UniqueCPtr<HangulInputContext, &hangul_ic_delete> context_;
    UniqueCPtr<HanjaList, &hanja_list_delete> hanjaList_;
    UString preedit_;
    LookupMethod lastLookupMethod_;
};

HangulEngine::HangulEngine(Instance *instance)
    : instance_(instance),
      factory_([this](InputContext &ic) { return new HangulState(this, &ic); }),
      table_(hanja_table_load(nullptr)) {
    if (!table_) {
        throw std::runtime_error("Failed to load hanja table.");
    }

    auto file = StandardPath::global().locate(StandardPath::Type::PkgData,
                                              "hangul/symbol.txt");
    if (!file.empty()) {
        symbolTable_.reset(hanja_table_load(file.data()));
    }

    reloadConfig();
    action_.connect<SimpleAction::Activated>([this](InputContext *ic) {
        config_.hanjaMode.setValue(!*config_.hanjaMode);
        updateAction(ic);
    });
    instance_->userInterfaceManager().registerAction("hangul", &action_);

    instance_->inputContextManager().registerProperty("hangulState", &factory_);
}

void HangulEngine::activate(const InputMethodEntry &,
                            InputContextEvent &event) {
    event.inputContext()->statusArea().addAction(StatusGroup::InputMethod,
                                                 &action_);
    updateAction(event.inputContext());
}

void HangulEngine::deactivate(const InputMethodEntry &entry,
                              InputContextEvent &event) {
    if (event.type() == EventType::InputContextSwitchInputMethod) {
        auto state = event.inputContext()->propertyFor(&factory_);
        state->flush();
    }
    reset(entry, event);
}

void HangulEngine::keyEvent(const InputMethodEntry &, KeyEvent &keyEvent) {
    if (keyEvent.isRelease()) {
        return;
    }
    auto state = keyEvent.inputContext()->propertyFor(&factory_);
    state->keyEvent(keyEvent);
}

void HangulEngine::reset(const InputMethodEntry &, InputContextEvent &event) {
    auto state = event.inputContext()->propertyFor(&factory_);
    state->reset();
}

void HangulEngine::reloadConfig() { readAsIni(config_, "conf/hangul.conf"); }

HangulState *HangulEngine::state(InputContext *ic) {
    return ic->propertyFor(&factory_);
}

void HangulCandidate::select(InputContext *inputContext) const {
    auto state = engine_->state(inputContext);
    state->select(idx_);
}
} // namespace fcitx

FCITX_ADDON_FACTORY(fcitx::HangulEngineFactory);