1313 */
1414#include " cskk.h"
1515#include " cskkcandidatelist.h"
16+ #include " cskkconfig.h"
1617#include " log.h"
18+ #include < cstdint>
1719#include < cstdlib>
1820#include < cstring>
1921#include < fcitx-config/iniparser.h>
22+ #include < fcitx-config/rawconfig.h>
23+ #include < fcitx-utils/capabilityflags.h>
2024#include < fcitx-utils/fdstreambuf.h>
25+ #include < fcitx-utils/i18n.h>
26+ #include < fcitx-utils/key.h>
27+ #include < fcitx-utils/keysym.h>
28+ #include < fcitx-utils/log.h>
29+ #include < fcitx-utils/standardpath.h>
2130#include < fcitx-utils/stringutils.h>
31+ #include < fcitx-utils/textformatflags.h>
32+ #include < fcitx/addoninstance.h>
2233#include < fcitx/addonmanager.h>
34+ #include < fcitx/event.h>
35+ #include < fcitx/inputcontextproperty.h>
36+ #include < fcitx/inputmethodentry.h>
2337#include < fcitx/inputpanel.h>
24- #include < filesystem>
38+ #include < fcitx/text.h>
39+ #include < fcitx/userinterface.h>
2540#include < istream>
41+ #include < memory>
2642#include < string>
43+ #include < string_view>
44+ #include < tuple>
2745#include < vector>
2846
2947extern " C" {
3048#include < fcntl.h>
49+ #include < libcskk.h>
3150}
32- using std::getenv;
33- using std::string;
3451
3552FCITX_DEFINE_LOG_CATEGORY (cskk_log, " cskk" );
3653
@@ -39,11 +56,11 @@ namespace fcitx {
3956/* ******************************************************************************
4057 * FcitxCskkEngine
4158 ******************************************************************************/
42- const string FcitxCskkEngine:: config_file_path = string{ " conf/fcitx5-cskk" } ;
59+ constexpr std::string_view config_file_path = " conf/fcitx5-cskk" ;
4360
4461FcitxCskkEngine::FcitxCskkEngine (Instance *instance)
4562 : instance_{instance}, factory_([this ](InputContext &ic) {
46- auto newCskkContext = new FcitxCskkContext (this , &ic);
63+ auto * newCskkContext = new FcitxCskkContext (this , &ic);
4764 newCskkContext->applyConfig ();
4865 return newCskkContext;
4966 }) {
@@ -53,54 +70,56 @@ FcitxCskkEngine::FcitxCskkEngine(Instance *instance)
5370
5471FcitxCskkEngine::~FcitxCskkEngine () = default ;
5572
56- void FcitxCskkEngine::keyEvent (const InputMethodEntry &, KeyEvent &keyEvent) {
73+ void FcitxCskkEngine::keyEvent (const InputMethodEntry & /* entry*/ ,
74+ KeyEvent &keyEvent) {
5775 CSKK_DEBUG () << " Engine keyEvent start: " << keyEvent.rawKey ();
5876 // delegate to context
59- auto ic = keyEvent.inputContext ();
60- auto context = ic->propertyFor (&factory_);
77+ auto * ic = keyEvent.inputContext ();
78+ auto * context = ic->propertyFor (&factory_);
6179 context->keyEvent (keyEvent);
6280 CSKK_DEBUG () << " Engine keyEvent end" ;
6381}
6482
6583void FcitxCskkEngine::save () {
6684 if (factory_.registered ()) {
6785 instance_->inputContextManager ().foreach ([this ](InputContext *ic) {
68- auto context = ic->propertyFor (&factory_);
86+ auto * context = ic->propertyFor (&factory_);
6987 return context->saveDictionary ();
7088 });
7189 }
7290}
7391
74- void FcitxCskkEngine::activate (const InputMethodEntry &, InputContextEvent &) {}
92+ void FcitxCskkEngine::activate (const InputMethodEntry & /* entry*/ ,
93+ InputContextEvent & /* event*/ ) {}
7594
7695void FcitxCskkEngine::deactivate (const InputMethodEntry &entry,
7796 InputContextEvent &event) {
7897 reset (entry, event);
7998}
8099
81- void FcitxCskkEngine::reset (const InputMethodEntry &,
100+ void FcitxCskkEngine::reset (const InputMethodEntry & /* entry */ ,
82101 InputContextEvent &event) {
83102 CSKK_DEBUG () << " Reset" ;
84- auto ic = event.inputContext ();
85- auto context = ic->propertyFor (&factory_);
103+ auto * ic = event.inputContext ();
104+ auto * context = ic->propertyFor (&factory_);
86105 context->reset ();
87106}
88107
89108void FcitxCskkEngine::setConfig (const RawConfig &config) {
90109 CSKK_DEBUG () << " Cskk setconfig" ;
91110 config_.load (config, true );
92- safeSaveAsIni (config_, FcitxCskkEngine:: config_file_path);
111+ safeSaveAsIni (config_, std::string ( config_file_path) );
93112 reloadConfig ();
94113}
95114
96115void FcitxCskkEngine::reloadConfig () {
97116 CSKK_DEBUG () << " Cskkengine reload config" ;
98- readAsIni (config_, FcitxCskkEngine:: config_file_path);
117+ readAsIni (config_, std::string ( config_file_path) );
99118
100119 loadDictionary ();
101120 if (factory_.registered ()) {
102121 instance_->inputContextManager ().foreach ([this ](InputContext *ic) {
103- auto context = ic->propertyFor (&factory_);
122+ auto * context = ic->propertyFor (&factory_);
104123 context->applyConfig ();
105124 return true ;
106125 });
@@ -139,7 +158,7 @@ void FcitxCskkEngine::loadDictionary() {
139158 std::string encoding;
140159 bool complete = false ;
141160
142- for (auto &token : tokens) {
161+ for (const auto &token : tokens) {
143162 auto equal = token.find (' =' );
144163 if (equal == std::string::npos) {
145164 continue ;
@@ -177,48 +196,46 @@ void FcitxCskkEngine::loadDictionary() {
177196 if (type == FSDT_Invalid) {
178197 CSKK_WARN () << " Dictionary entry has invalid type. Ignored." ;
179198 continue ;
199+ }
200+ if (path.empty () || mode == 0 ) {
201+ CSKK_WARN () << " Invalid dictionary path or mode. Ignored" ;
202+ continue ;
203+ }
204+ if (mode == 1 ) {
205+ // readonly mode
206+ auto *dict = skk_file_dict_new (path.c_str (), encoding.c_str (), complete);
207+ if (dict) {
208+ CSKK_DEBUG () << " Adding file dict: " << path
209+ << " complete:" << complete;
210+ dictionaries_.emplace_back (dict);
211+ } else {
212+ CSKK_WARN () << " Static dictionary load error. Ignored: " << path;
213+ }
180214 } else {
181- if (path.empty () || mode == 0 ) {
182- CSKK_WARN () << " Invalid dictionary path or mode. Ignored" ;
183- continue ;
215+ // read/write mode
216+ constexpr char configDir[] = " $FCITX_CONFIG_DIR/" ;
217+ constexpr auto var_len = sizeof (configDir) - 1 ;
218+ std::string realpath = path;
219+ if (stringutils::startsWith (path, configDir)) {
220+ realpath = stringutils::joinPath (
221+ StandardPath::global ().userDirectory (StandardPath::Type::PkgData),
222+ path.substr (var_len));
184223 }
185- if (mode == 1 ) {
186- // readonly mode
187- auto *dict =
188- skk_file_dict_new (path.c_str (), encoding.c_str (), complete);
189- if (dict) {
190- CSKK_DEBUG () << " Adding file dict: " << path
191- << " complete:" << complete;
192- dictionaries_.emplace_back (dict);
193- } else {
194- CSKK_WARN () << " Static dictionary load error. Ignored: " << path;
195- }
224+ auto *userdict =
225+ skk_user_dict_new (realpath.c_str (), encoding.c_str (), complete);
226+ if (userdict) {
227+ CSKK_DEBUG () << " Adding user dict: " << realpath;
228+ dictionaries_.emplace_back (userdict);
196229 } else {
197- // read/write mode
198- constexpr char configDir[] = " $FCITX_CONFIG_DIR/" ;
199- constexpr auto var_len = sizeof (configDir) - 1 ;
200- std::string realpath = path;
201- if (stringutils::startsWith (path, configDir)) {
202- realpath = stringutils::joinPath (
203- StandardPath::global ().userDirectory (StandardPath::Type::PkgData),
204- path.substr (var_len));
205- }
206- auto *userdict =
207- skk_user_dict_new (realpath.c_str (), encoding.c_str (), complete);
208- if (userdict) {
209- CSKK_DEBUG () << " Adding user dict: " << realpath;
210- dictionaries_.emplace_back (userdict);
211- } else {
212- CSKK_WARN () << " User dictionary load error. Ignored: " << realpath;
213- }
230+ CSKK_WARN () << " User dictionary load error. Ignored: " << realpath;
214231 }
215232 }
216233 }
217234}
218235
219236void FcitxCskkEngine::freeDictionaries () {
220237 CSKK_DEBUG () << " Cskk free dict" ;
221- for (auto dictionary : dictionaries_) {
238+ for (auto * dictionary : dictionaries_) {
222239 skk_free_dictionary (dictionary);
223240 }
224241 dictionaries_.clear ();
@@ -247,9 +264,10 @@ KeyList FcitxCskkEngine::getSelectionKeys(
247264 }
248265}
249266
250- std::string FcitxCskkEngine::subModeIconImpl (const InputMethodEntry &,
251- InputContext &ic) {
252- auto context = ic.propertyFor (&factory_);
267+ std::string
268+ FcitxCskkEngine::subModeIconImpl (const InputMethodEntry & /* unused*/ ,
269+ InputContext &ic) {
270+ auto *context = ic.propertyFor (&factory_);
253271 auto current_input_mode = context->getInputMode ();
254272 switch (current_input_mode) {
255273 case InputMode::Ascii:
@@ -371,12 +389,12 @@ void FcitxCskkContext::updateUI() {
371389 CSKK_WARN () << " No context setup" ;
372390 return ;
373391 }
374- auto &config = engine_->config ();
392+ const auto &config = engine_->config ();
375393 auto &inputPanel = ic_->inputPanel ();
376394 inputPanel.reset ();
377395
378396 // Output
379- if (auto output = skk_context_poll_output (context_)) {
397+ if (auto * output = skk_context_poll_output (context_)) {
380398 CSKK_DEBUG () << " output: " << output;
381399 if (strlen (output) > 0 ) {
382400 ic_->commitString (output);
@@ -386,7 +404,7 @@ void FcitxCskkContext::updateUI() {
386404
387405 // Preedit
388406 uint32_t stateStackLen;
389- auto preeditDetail = skk_context_get_preedit_detail (context_, &stateStackLen);
407+ auto * preeditDetail = skk_context_get_preedit_detail (context_, &stateStackLen);
390408 auto [mainPreedit, supplementPreedit] =
391409 FcitxCskkContext::formatPreedit (preeditDetail, stateStackLen);
392410 skk_free_preedit_detail (preeditDetail, stateStackLen);
@@ -410,7 +428,7 @@ void FcitxCskkContext::updateUI() {
410428 } else {
411429 // Sync UI with actual data
412430 currentCandidateList->setCursorPosition (
413- static_cast < int >( currentCursorPosition) );
431+ currentCursorPosition);
414432 }
415433
416434 } else {
@@ -442,7 +460,7 @@ void FcitxCskkContext::applyConfig() {
442460 CSKK_WARN () << " No context setup. Ignoring config." ;
443461 return ;
444462 }
445- auto &config = engine_->config ();
463+ const auto &config = engine_->config ();
446464
447465 skk_context_set_rule (context_, config.cskkRule ->c_str ());
448466 skk_context_set_input_mode (context_, *config.inputMode );
@@ -452,7 +470,7 @@ void FcitxCskkContext::applyConfig() {
452470 skk_context_set_comma_style (context_, *config.commaStyle );
453471}
454472
455- void FcitxCskkContext::copyTo (InputContextProperty *) {
473+ void FcitxCskkContext::copyTo (InputContextProperty * /* unused */ ) {
456474 // auto otherContext = dynamic_cast<FcitxCskkContext *>(context);
457475 // Ignored.
458476 // Even if fcitx5 global option is set to share input state、it only shares
@@ -491,9 +509,8 @@ FcitxCskkContext::formatPreedit(CskkStateInfoFfi *cskkStateInfoArray,
491509 std::string precomposition_marker = " ▽" ;
492510 std::string selection_marker = " ▼" ;
493511 std::string completion_marker = " ■" ;
494- Text mainContent = Text (" " ), supplementContent = Text (" " );
495- mainContent.clear ();
496- supplementContent.clear ();
512+ Text mainContent;
513+ Text supplementContent;
497514 size_t mainCursorIdx = 0 ;
498515 for (uint32_t i = 0 ; i < stateLen; i++) {
499516 auto cskkStateInfo = cskkStateInfoArray[i];
@@ -649,14 +666,13 @@ AddonInstance *FcitxCskkFactory::create(AddonManager *manager) {
649666 {
650667 CSKK_DEBUG () << " **** CSKK FcitxCskkFactory Create ****" ;
651668 registerDomain (" fcitx5-cskk" , FCITX_INSTALL_LOCALEDIR);
652- auto engine = new FcitxCskkEngine (manager->instance ());
669+ auto * engine = new FcitxCskkEngine (manager->instance ());
653670 if (engine->isEngineReady ()) {
654671 return engine;
655- } else {
656- return nullptr ;
657672 }
673+ return nullptr ;
658674 }
659675}
660676} // namespace fcitx
661677
662- FCITX_ADDON_FACTORY ( fcitx::FcitxCskkFactory);
678+ FCITX_ADDON_FACTORY_V2 (cskk, fcitx::FcitxCskkFactory);
0 commit comments