diff --git a/CMakeLists.txt b/CMakeLists.txt index 556f762..e5e4f78 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,7 +65,7 @@ if (OS_LINUX) endif() if (ENABLE_FCITX) - find_package(Fcitx5Core REQUIRED) + find_package(Fcitx5Core 5.0.6 REQUIRED) endif() ## Find zstd ## pkg_check_modules(ZSTD REQUIRED libzstd) diff --git a/src/engine/fcitx/openbangla-addon.conf b/src/engine/fcitx/openbangla-addon.conf index 123221c..1b0ac5a 100644 --- a/src/engine/fcitx/openbangla-addon.conf +++ b/src/engine/fcitx/openbangla-addon.conf @@ -5,3 +5,6 @@ Library=openbangla Type=SharedLibrary OnDemand=True Configurable=True + +[Dependencies] +0=core:5.0.6 diff --git a/src/engine/fcitx/openbangla.cpp b/src/engine/fcitx/openbangla.cpp index 319ddff..65294e7 100644 --- a/src/engine/fcitx/openbangla.cpp +++ b/src/engine/fcitx/openbangla.cpp @@ -25,8 +25,12 @@ #include #include #include +#include #include +FCITX_DEFINE_LOG_CATEGORY(openbangla, "openbangla"); +#define FCITX_OPENBANGLA_DEBUG() FCITX_LOGC(openbangla, Debug) + namespace fcitx { class OpenBanglaState : public InputContextProperty { @@ -77,7 +81,7 @@ class OpenBanglaState : public InputContextProperty { } else { auto txt = riti_suggestion_get_lonely_suggestion(suggestion_.get()); text = txt; - // TODO: free txt + riti_string_free(txt); } Text preedit(std::move(text)); @@ -106,8 +110,8 @@ class OpenBanglaState : public InputContextProperty { } else { char *txt = riti_suggestion_get_lonely_suggestion(suggestion_.get()); text = txt; - // TODO: free txt. ic_->commitString(text); + riti_string_free(txt); riti_context_candidate_committed(ctx_.get(), 0); reset(); } @@ -123,18 +127,17 @@ class OpenBanglaState : public InputContextProperty { if (aux && aux[0]) { ic_->inputPanel().setAuxUp(Text(std::string(aux))); } - // TODO: free aux + riti_string_free(aux); auto candidateList = std::make_unique(); auto len = riti_suggestion_get_length(suggestion_.get()); - char *const *suggestions = - riti_suggestion_get_suggestions(suggestion_.get()); + char **suggestions = riti_suggestion_get_suggestions(suggestion_.get()); for (decltype(len) i = 0; i < len; i++) { candidateList->append(engine_, suggestions[i], i); } - // TODO: free suggestions + riti_string_array_free(suggestions, len); - candidateList->setLayoutHint(*engine_->config().candidateWinHorizontal + candidateList->setLayoutHint(engine_->candidateWinHorizontal() ? CandidateLayoutHint::Horizontal : CandidateLayoutHint::Vertical); auto index = @@ -195,7 +198,7 @@ class OpenBanglaState : public InputContextProperty { case FcitxKey_Return: if (riti_context_ongoing_input_session(ctx)) { commit(); - if (*engine_->config().enterKeyClosesPrevWin) { + if (engine_->enterKeyClosesPrevWin()) { keyEvent.filterAndAccept(); } } @@ -215,7 +218,7 @@ class OpenBanglaState : public InputContextProperty { case FcitxKey_Right: case FcitxKey_Left: if (riti_context_ongoing_input_session(ctx)) { - if (*engine_->config().candidateWinHorizontal && + if (engine_->candidateWinHorizontal() && !riti_suggestion_is_lonely(suggestion_.get()) && candidateList && candidateList->toCursorMovable()) { if (key.sym() == FcitxKey_Right) { @@ -235,7 +238,7 @@ class OpenBanglaState : public InputContextProperty { case FcitxKey_Up: case FcitxKey_Down: if (riti_context_ongoing_input_session(ctx)) { - if (!*engine_->config().candidateWinHorizontal && + if (!engine_->candidateWinHorizontal() && !riti_suggestion_is_lonely(suggestion_.get()) && candidateList && candidateList->toCursorMovable()) { if (key.sym() == FcitxKey_Up) { @@ -346,6 +349,10 @@ OpenBanglaEngine::OpenBanglaEngine(Instance *instance) OpenBanglaEngine::~OpenBanglaEngine() {} +void OpenBanglaEngine::activate(const InputMethodEntry &, InputContextEvent &) { + reloadConfig(); +} + void OpenBanglaEngine::keyEvent(const InputMethodEntry &, KeyEvent &keyEvent) { auto ic = keyEvent.inputContext(); auto state = ic->propertyFor(&factory_); @@ -358,31 +365,74 @@ void OpenBanglaEngine::reset(const InputMethodEntry &, state->reset(); } -void OpenBanglaEngine::populateConfig() { - setenv("RITI_LAYOUT_FILE", config_.layoutPath->data(), 1); - setenv("RITI_PHONETIC_DATABASE_ON", - *config_.showCWPhonetic ? "true" : "false", 1); +bool booleanValue(const RawConfig &config, const std::string &path, + bool defaultValue) { + bool value = defaultValue; + if (auto *option = config.valueByPath(path)) { + value = *option == "true"; + } + return value; +} + +void OpenBanglaEngine::populateConfig(const RawConfig &config) { + // Keep sync with Settings.cpp + std::string layoutPath = + "/usr/share/openbangla-keyboard/layouts/avrophonetic.json"; + if (auto *path = config.valueByPath("layout/path")) { + layoutPath = *path; + } + const bool showCWPhonetic = + booleanValue(config, "settings/CandidateWin\\Phonetic", true); + const bool includeEnglishPrevWin = + booleanValue(config, "settings/PreviewWin\\IncludeEnglishPhonetic", true); + const bool showPrevWinFixed = + booleanValue(config, "settings/FixedLayout\\ShowPrevWin", true); + const bool autoVowelFormFixed = + booleanValue(config, "settings/ixedLayout\\AutoVowelForm", true); + const bool autoChandraPosFixed = + booleanValue(config, "settings/FixedLayout\\AutoChandraPos", true); + const bool traditionalKarFixed = + booleanValue(config, "settings/FixedLayout\\TraditionalKar", false); + const bool oldReph = + booleanValue(config, "settings/FixedLayout\\OldReph", true); + const bool numberPadFixed = + booleanValue(config, "settings/FixedLayout\\NumberPad", true); + setenv("RITI_LAYOUT_FILE", layoutPath.data(), 1); + setenv("RITI_PHONETIC_DATABASE_ON", showCWPhonetic ? "true" : "false", 1); setenv("RITI_PHONETIC_INCLUDE_ENGLISH", - *config_.includeEnglishPrevWin ? "true" : "false", 1); - setenv("RITI_DATABASE_DIR", config_.databasePath->data(), 1); - setenv("RITI_LAYOUT_FIXED_DATABASE_ON", - *config_.showPrevWinFixed ? "true" : "false", 1); - setenv("RITI_LAYOUT_FIXED_VOWEL", - *config_.autoVowelFormFixed ? "true" : "false", 1); - setenv("RITI_LAYOUT_FIXED_CHANDRA", - *config_.autoChandraPosFixed ? "true" : "false", 1); - setenv("RITI_LAYOUT_FIXED_KAR", - *config_.traditionalKarFixed ? "true" : "false", 1); - setenv("RITI_LAYOUT_FIXED_OLD_REPH", *config_.oldReph ? "true" : "false", 1); - setenv("RITI_LAYOUT_FIXED_NUMBERPAD", - *config_.numberPadFixed ? "true" : "false", 1); + includeEnglishPrevWin ? "true" : "false", 1); + setenv("RITI_DATABASE_DIR", "/usr/share/openbangla-keyboard/data", 1); + setenv("RITI_LAYOUT_FIXED_DATABASE_ON", showPrevWinFixed ? "true" : "false", + 1); + setenv("RITI_LAYOUT_FIXED_VOWEL", autoVowelFormFixed ? "true" : "false", 1); + setenv("RITI_LAYOUT_FIXED_CHANDRA", autoChandraPosFixed ? "true" : "false", + 1); + setenv("RITI_LAYOUT_FIXED_KAR", traditionalKarFixed ? "true" : "false", 1); + setenv("RITI_LAYOUT_FIXED_OLD_REPH", oldReph ? "true" : "false", 1); + setenv("RITI_LAYOUT_FIXED_NUMBERPAD", numberPadFixed ? "true" : "false", 1); + candidateWinHorizontal_ = + booleanValue(config, "settings/CandidateWin\\Horizontal", true); + enterKeyClosesPrevWin_ = + booleanValue(config, "settings/EnterKeyClosesPrevWin", false); } void OpenBanglaEngine::reloadConfig() { - readAsIni(config_, "conf/openbangla.conf"); - populateConfig(); + auto time = StandardPath::global().timestamp(StandardPath::Type::Config, + "OpenBangla/Keyboard.conf"); + if (time < lastConfigTimestamp_) { + return; + } + + RawConfig config; + auto configFile = StandardPath::global().open( + StandardPath::Type::Config, "OpenBangla/Keyboard.conf", O_RDONLY); + if (configFile.isValid()) { + FCITX_OPENBANGLA_DEBUG() << "Reload openbangla configuration"; + lastConfigTimestamp_ = time; + readFromIni(config, configFile.fd()); + } + populateConfig(config); } +FCITX_ADDON_FACTORY(OpenBanglaFactory); } // namespace fcitx - -FCITX_ADDON_FACTORY(fcitx::OpenBanglaFactory); diff --git a/src/engine/fcitx/openbangla.h b/src/engine/fcitx/openbangla.h index 322edfb..b3660fd 100644 --- a/src/engine/fcitx/openbangla.h +++ b/src/engine/fcitx/openbangla.h @@ -27,62 +27,44 @@ #include #include #include +#include namespace fcitx { class OpenBanglaState; -FCITX_CONFIGURATION( - OpenBanglaConfig, - Option enterKeyClosesPrevWin{this, "enterKeyClosesPrevWin", - "enterKeyClosesPrevWin", false}; - Option candidateWinHorizontal{this, "CandidateWinHorizontal", - "CandidateWinHorizontal", true}; - Option layoutPath{ - this, "LayoutPath", "LayoutPath", - "/usr/share/openbangla-keyboard/layouts/avrophonetic.json"}; - Option showCWPhonetic{this, "ShowCWPhonetic", "ShowCWPhonetic", true}; - Option includeEnglishPrevWin{this, "IncludeEnglishPrevWin", - "IncludeEnglishPrevWin", true}; - Option databasePath{this, "databasePath", "DatabasePath", - "/usr/share/openbangla-keyboard/data"}; - Option showPrevWinFixed{this, "ShowPrevWinFixed", "ShowPrevWinFixed", - true}; - Option autoVowelFormFixed{this, "AutoVowelFormFixed", - "AutoVowelFormFixed", true}; - Option autoChandraPosFixed{this, "AutoChandraPosFixed", - "AutoChandraPosFixed", true}; - Option traditionalKarFixed{this, "TraditionalKarFixed", - "TraditionalKarFixed", false}; - Option oldReph{this, "OldReph", "OldReph", true}; - Option numberPadFixed{this, "NumberPadFixed", "NumberPadFixed", - true};); +FCITX_CONFIGURATION(OpenBanglaConfig, + ExternalOption config{this, "OpenBanglaKeyboard", + _("OpenBangla Keyboard"), + PROJECT_DATADIR "/openbangla-gui"};); class OpenBanglaEngine final : public InputMethodEngine { public: OpenBanglaEngine(Instance *instance); ~OpenBanglaEngine(); + void activate(const fcitx::InputMethodEntry &, + fcitx::InputContextEvent &) override; void keyEvent(const InputMethodEntry &entry, KeyEvent &keyEvent) override; void reset(const InputMethodEntry &entry, InputContextEvent &event) override; - const auto &config() const { return config_; } auto *factory() { return &factory_; } void reloadConfig() override; const Configuration *getConfig() const override { return &config_; } - void setConfig(const RawConfig &config) override { - config_.load(config, true); - safeSaveAsIni(config_, "conf/openbangla.conf"); - populateConfig(); - } + + auto candidateWinHorizontal() const { return candidateWinHorizontal_; } + auto enterKeyClosesPrevWin() const { return enterKeyClosesPrevWin_; } private: - void populateConfig(); + void populateConfig(const RawConfig &config); Instance *instance_; OpenBanglaConfig config_; FactoryFor factory_; + int64_t lastConfigTimestamp_ = std::numeric_limits::min(); + bool candidateWinHorizontal_ = true; + bool enterKeyClosesPrevWin_ = false; }; class OpenBanglaFactory : public AddonFactory { diff --git a/src/engine/riti b/src/engine/riti index 1d0dd1c..719e4aa 160000 --- a/src/engine/riti +++ b/src/engine/riti @@ -1 +1 @@ -Subproject commit 1d0dd1cc6cb78bda64219116ef71a63329d627a3 +Subproject commit 719e4aa9e7c8c9759189571737d6f438e38b19ef