13
13
*/
14
14
#include " cskk.h"
15
15
#include " cskkcandidatelist.h"
16
+ #include " cskkconfig.h"
16
17
#include " log.h"
18
+ #include < cstdint>
17
19
#include < cstdlib>
18
20
#include < cstring>
19
21
#include < fcitx-config/iniparser.h>
22
+ #include < fcitx-config/rawconfig.h>
23
+ #include < fcitx-utils/capabilityflags.h>
20
24
#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>
21
30
#include < fcitx-utils/stringutils.h>
31
+ #include < fcitx-utils/textformatflags.h>
32
+ #include < fcitx/addoninstance.h>
22
33
#include < fcitx/addonmanager.h>
34
+ #include < fcitx/event.h>
35
+ #include < fcitx/inputcontextproperty.h>
36
+ #include < fcitx/inputmethodentry.h>
23
37
#include < fcitx/inputpanel.h>
24
- #include < filesystem>
38
+ #include < fcitx/text.h>
39
+ #include < fcitx/userinterface.h>
25
40
#include < istream>
41
+ #include < memory>
26
42
#include < string>
43
+ #include < string_view>
44
+ #include < tuple>
27
45
#include < vector>
28
46
29
47
extern " C" {
30
48
#include < fcntl.h>
49
+ #include < libcskk.h>
31
50
}
32
- using std::getenv;
33
- using std::string;
34
51
35
52
FCITX_DEFINE_LOG_CATEGORY (cskk_log, " cskk" );
36
53
@@ -39,11 +56,11 @@ namespace fcitx {
39
56
/* ******************************************************************************
40
57
* FcitxCskkEngine
41
58
******************************************************************************/
42
- const string FcitxCskkEngine:: config_file_path = string{ " conf/fcitx5-cskk" } ;
59
+ constexpr std::string_view config_file_path = " conf/fcitx5-cskk" ;
43
60
44
61
FcitxCskkEngine::FcitxCskkEngine (Instance *instance)
45
62
: instance_{instance}, factory_([this ](InputContext &ic) {
46
- auto newCskkContext = new FcitxCskkContext (this , &ic);
63
+ auto * newCskkContext = new FcitxCskkContext (this , &ic);
47
64
newCskkContext->applyConfig ();
48
65
return newCskkContext;
49
66
}) {
@@ -53,54 +70,56 @@ FcitxCskkEngine::FcitxCskkEngine(Instance *instance)
53
70
54
71
FcitxCskkEngine::~FcitxCskkEngine () = default ;
55
72
56
- void FcitxCskkEngine::keyEvent (const InputMethodEntry &, KeyEvent &keyEvent) {
73
+ void FcitxCskkEngine::keyEvent (const InputMethodEntry & /* entry*/ ,
74
+ KeyEvent &keyEvent) {
57
75
CSKK_DEBUG () << " Engine keyEvent start: " << keyEvent.rawKey ();
58
76
// 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_);
61
79
context->keyEvent (keyEvent);
62
80
CSKK_DEBUG () << " Engine keyEvent end" ;
63
81
}
64
82
65
83
void FcitxCskkEngine::save () {
66
84
if (factory_.registered ()) {
67
85
instance_->inputContextManager ().foreach ([this ](InputContext *ic) {
68
- auto context = ic->propertyFor (&factory_);
86
+ auto * context = ic->propertyFor (&factory_);
69
87
return context->saveDictionary ();
70
88
});
71
89
}
72
90
}
73
91
74
- void FcitxCskkEngine::activate (const InputMethodEntry &, InputContextEvent &) {}
92
+ void FcitxCskkEngine::activate (const InputMethodEntry & /* entry*/ ,
93
+ InputContextEvent & /* event*/ ) {}
75
94
76
95
void FcitxCskkEngine::deactivate (const InputMethodEntry &entry,
77
96
InputContextEvent &event) {
78
97
reset (entry, event);
79
98
}
80
99
81
- void FcitxCskkEngine::reset (const InputMethodEntry &,
100
+ void FcitxCskkEngine::reset (const InputMethodEntry & /* entry */ ,
82
101
InputContextEvent &event) {
83
102
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_);
86
105
context->reset ();
87
106
}
88
107
89
108
void FcitxCskkEngine::setConfig (const RawConfig &config) {
90
109
CSKK_DEBUG () << " Cskk setconfig" ;
91
110
config_.load (config, true );
92
- safeSaveAsIni (config_, FcitxCskkEngine:: config_file_path);
111
+ safeSaveAsIni (config_, std::string ( config_file_path) );
93
112
reloadConfig ();
94
113
}
95
114
96
115
void FcitxCskkEngine::reloadConfig () {
97
116
CSKK_DEBUG () << " Cskkengine reload config" ;
98
- readAsIni (config_, FcitxCskkEngine:: config_file_path);
117
+ readAsIni (config_, std::string ( config_file_path) );
99
118
100
119
loadDictionary ();
101
120
if (factory_.registered ()) {
102
121
instance_->inputContextManager ().foreach ([this ](InputContext *ic) {
103
- auto context = ic->propertyFor (&factory_);
122
+ auto * context = ic->propertyFor (&factory_);
104
123
context->applyConfig ();
105
124
return true ;
106
125
});
@@ -139,7 +158,7 @@ void FcitxCskkEngine::loadDictionary() {
139
158
std::string encoding;
140
159
bool complete = false ;
141
160
142
- for (auto &token : tokens) {
161
+ for (const auto &token : tokens) {
143
162
auto equal = token.find (' =' );
144
163
if (equal == std::string::npos) {
145
164
continue ;
@@ -177,48 +196,46 @@ void FcitxCskkEngine::loadDictionary() {
177
196
if (type == FSDT_Invalid) {
178
197
CSKK_WARN () << " Dictionary entry has invalid type. Ignored." ;
179
198
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
+ }
180
214
} 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));
184
223
}
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);
196
229
} 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 ;
214
231
}
215
232
}
216
233
}
217
234
}
218
235
219
236
void FcitxCskkEngine::freeDictionaries () {
220
237
CSKK_DEBUG () << " Cskk free dict" ;
221
- for (auto dictionary : dictionaries_) {
238
+ for (auto * dictionary : dictionaries_) {
222
239
skk_free_dictionary (dictionary);
223
240
}
224
241
dictionaries_.clear ();
@@ -247,9 +264,10 @@ KeyList FcitxCskkEngine::getSelectionKeys(
247
264
}
248
265
}
249
266
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_);
253
271
auto current_input_mode = context->getInputMode ();
254
272
switch (current_input_mode) {
255
273
case InputMode::Ascii:
@@ -371,12 +389,12 @@ void FcitxCskkContext::updateUI() {
371
389
CSKK_WARN () << " No context setup" ;
372
390
return ;
373
391
}
374
- auto &config = engine_->config ();
392
+ const auto &config = engine_->config ();
375
393
auto &inputPanel = ic_->inputPanel ();
376
394
inputPanel.reset ();
377
395
378
396
// Output
379
- if (auto output = skk_context_poll_output (context_)) {
397
+ if (auto * output = skk_context_poll_output (context_)) {
380
398
CSKK_DEBUG () << " output: " << output;
381
399
if (strlen (output) > 0 ) {
382
400
ic_->commitString (output);
@@ -386,7 +404,7 @@ void FcitxCskkContext::updateUI() {
386
404
387
405
// Preedit
388
406
uint32_t stateStackLen;
389
- auto preeditDetail = skk_context_get_preedit_detail (context_, &stateStackLen);
407
+ auto * preeditDetail = skk_context_get_preedit_detail (context_, &stateStackLen);
390
408
auto [mainPreedit, supplementPreedit] =
391
409
FcitxCskkContext::formatPreedit (preeditDetail, stateStackLen);
392
410
skk_free_preedit_detail (preeditDetail, stateStackLen);
@@ -410,7 +428,7 @@ void FcitxCskkContext::updateUI() {
410
428
} else {
411
429
// Sync UI with actual data
412
430
currentCandidateList->setCursorPosition (
413
- static_cast < int >( currentCursorPosition) );
431
+ currentCursorPosition);
414
432
}
415
433
416
434
} else {
@@ -442,7 +460,7 @@ void FcitxCskkContext::applyConfig() {
442
460
CSKK_WARN () << " No context setup. Ignoring config." ;
443
461
return ;
444
462
}
445
- auto &config = engine_->config ();
463
+ const auto &config = engine_->config ();
446
464
447
465
skk_context_set_rule (context_, config.cskkRule ->c_str ());
448
466
skk_context_set_input_mode (context_, *config.inputMode );
@@ -452,7 +470,7 @@ void FcitxCskkContext::applyConfig() {
452
470
skk_context_set_comma_style (context_, *config.commaStyle );
453
471
}
454
472
455
- void FcitxCskkContext::copyTo (InputContextProperty *) {
473
+ void FcitxCskkContext::copyTo (InputContextProperty * /* unused */ ) {
456
474
// auto otherContext = dynamic_cast<FcitxCskkContext *>(context);
457
475
// Ignored.
458
476
// Even if fcitx5 global option is set to share input state、it only shares
@@ -491,9 +509,8 @@ FcitxCskkContext::formatPreedit(CskkStateInfoFfi *cskkStateInfoArray,
491
509
std::string precomposition_marker = " ▽" ;
492
510
std::string selection_marker = " ▼" ;
493
511
std::string completion_marker = " ■" ;
494
- Text mainContent = Text (" " ), supplementContent = Text (" " );
495
- mainContent.clear ();
496
- supplementContent.clear ();
512
+ Text mainContent;
513
+ Text supplementContent;
497
514
size_t mainCursorIdx = 0 ;
498
515
for (uint32_t i = 0 ; i < stateLen; i++) {
499
516
auto cskkStateInfo = cskkStateInfoArray[i];
@@ -649,14 +666,13 @@ AddonInstance *FcitxCskkFactory::create(AddonManager *manager) {
649
666
{
650
667
CSKK_DEBUG () << " **** CSKK FcitxCskkFactory Create ****" ;
651
668
registerDomain (" fcitx5-cskk" , FCITX_INSTALL_LOCALEDIR);
652
- auto engine = new FcitxCskkEngine (manager->instance ());
669
+ auto * engine = new FcitxCskkEngine (manager->instance ());
653
670
if (engine->isEngineReady ()) {
654
671
return engine;
655
- } else {
656
- return nullptr ;
657
672
}
673
+ return nullptr ;
658
674
}
659
675
}
660
676
} // namespace fcitx
661
677
662
- FCITX_ADDON_FACTORY ( fcitx::FcitxCskkFactory);
678
+ FCITX_ADDON_FACTORY_V2 (cskk, fcitx::FcitxCskkFactory);
0 commit comments