1
/* Copyright (C) 2010~2010 by CSSlayer
4
This program is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 3 of the License, or
7
(at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
#include <fcitx-config/hotkey.h>
23
#include <ime-core/imi_view.h>
24
#include <ime-core/imi_options.h>
25
#include <ime-core/utils.h>
26
#include <fcitx-config/xdg.h>
39
_("Sunpinyin"), /* Name */
40
"fcitx-sunpinyin", /* IconName */
42
DoInput, /* DoInput */
43
GetCandWords, /* GetCandWords */
66
static ConfigFileDesc* GetSunpinyinConfigDesc();
67
static void LoadConfig(Bool reload = False);
68
static void SaveConfig();
70
static FcitxWindowHandler* instance = NULL;
71
static CIMIView* view = NULL;
72
static ConfigFileDesc* sunpinyinConfigDesc;
73
static FcitxSunpinyinConfig fs;
75
static const char* fuzzyPairs[][2] = {
92
static const char *correctionPairs[][2] = {
102
* @brief Reset the status.
108
GenericConfig *profile = (GenericConfig*) EIM.profile;
109
ConfigValueType corner = ConfigGetBindValue(profile, "Profile", "Corner");
110
ConfigValueType punc = ConfigGetBindValue(profile, "Profile", "ChnPunc");
111
view->setStatusAttrValue(CIMIWinHandler::STATUS_ID_FULLSYMBOL, *corner.boolean);
112
view->setStatusAttrValue(CIMIWinHandler::STATUS_ID_FULLPUNC, *punc.boolean);
117
* @brief Process Key Input and return the status
119
* @param keycode keycode from XKeyEvent
120
* @param state state from XKeyEvent
121
* @param count count from XKeyEvent
122
* @return INPUT_RETURN_VALUE
125
INPUT_RETURN_VALUE DoInput (unsigned int keycode, unsigned int state, int count)
127
if ((keycode <= 0x20 || keycode > 0x7E) && view->getIC()->isEmpty())
128
return IRV_TO_PROCESS;
130
if (keycode == 0x003b && view->getIC()->isEmpty())
131
return IRV_TO_PROCESS;
133
if (keycode == 0xFF8D)
136
instance->commit_flag = false;
137
instance->candidate_flag = false;
138
unsigned int changeMasks = view->onKeyEvent(CKeyEvent(keycode, keycode, state));
140
if (instance->commit_flag)
141
return IRV_GET_CANDWORDS;
142
if (!(changeMasks & CIMIView::KEYEVENT_USED))
143
return IRV_TO_PROCESS;
145
if (view->getIC()->isEmpty())
148
if (instance->candidate_flag)
150
return IRV_DISPLAY_CANDWORDS;
153
return IRV_TO_PROCESS;
157
* @brief function DoInput has done everything for us.
160
* @return INPUT_RETURN_VALUE
163
INPUT_RETURN_VALUE GetCandWords(SEARCH_MODE searchMode)
165
return IRV_DO_NOTHING;
169
* @brief get the candidate word by index
171
* @param iIndex index of candidate word
172
* @return the string of canidate word
175
char *GetCandWord (int iIndex)
177
EIM.CandWordCount = 0;
178
instance->commit_flag = false;
179
instance->candidate_flag = false;
182
unsigned int keycode = '1' + iIndex;
183
unsigned int state = 0;
184
unsigned int changeMasks = view->onKeyEvent(CKeyEvent(keycode, keycode, state));
186
if (instance->commit_flag)
187
return EIM.StringGet;
194
* @brief initialize the extra input method
197
* @return successful or not
202
bindtextdomain("fcitx-sunpinyin", LOCALEDIR);
203
GenericConfig *fc = (GenericConfig*)EIM.fc;
204
ConfigValueType candword;
205
ConfigValueType prevpage;
206
ConfigValueType nextpage;
210
candword = ConfigGetBindValue(fc, "Appearance", "CandidateWordNumber");
211
prevpage = ConfigGetBindValue(fc, "Hotkey", "PrevPageKey");
212
nextpage = ConfigGetBindValue(fc, "Hotkey", "NextPageKey");
214
CSunpinyinSessionFactory& fac = CSunpinyinSessionFactory::getFactory();
216
if (fs.bUseShuangpin)
217
fac.setPinyinScheme(CSunpinyinSessionFactory::SHUANGPIN);
219
fac.setPinyinScheme(CSunpinyinSessionFactory::QUANPIN);
221
AShuangpinSchemePolicy::instance().setShuangpinType(fs.SPScheme);
222
AQuanpinSchemePolicy::instance().setFuzzySegmentation(fs.bFuzzySegmentation);
223
AQuanpinSchemePolicy::instance().setInnerFuzzySegmentation(fs.bFuzzyInnerSegmentation);
224
view = fac.createSession();
226
instance = new FcitxWindowHandler();
227
view->getIC()->setCharsetLevel(1);// GBK
229
view->setCandiWindowSize(*candword.integer);
230
view->attachWinHandler(instance);
232
CHotkeyProfile* prof = view->getHotkeyProfile();
236
for (i = 0 ; i < 2; i++)
238
if (prevpage.hotkey[i].sym)
239
prof->addPageUpKey(CKeyEvent(prevpage.hotkey[i].sym, 0, prevpage.hotkey[i].state));
240
if (nextpage.hotkey[i].sym)
241
prof->addPageDownKey(CKeyEvent(nextpage.hotkey[i].sym, 0, nextpage.hotkey[i].state));
244
string_pairs fuzzy, correction;
245
for (i = 0; i < FUZZY_SIZE; i++)
247
fuzzy.push_back(std::make_pair<std::string, std::string>(fuzzyPairs[i][0], fuzzyPairs[i][1]));
249
for (i = 0; i < CORRECT_SIZE; i++)
250
if (fs.bAutoCorrecting[i])
251
correction.push_back(std::make_pair<std::string, std::string>(correctionPairs[i][0], correctionPairs[i][1]));
253
if (fuzzy.size() != 0)
255
AQuanpinSchemePolicy::instance().setFuzzyForwarding(true);
256
AQuanpinSchemePolicy::instance().setFuzzyPinyinPairs(fuzzy);
260
AQuanpinSchemePolicy::instance().setFuzzyForwarding(false);
261
AQuanpinSchemePolicy::instance().clearFuzzyPinyinPairs();
264
if (correction.size() != 0)
266
AQuanpinSchemePolicy::instance().setAutoCorrecting(true);
267
AQuanpinSchemePolicy::instance().setAutoCorrectionPairs(correction);
270
AQuanpinSchemePolicy::instance().setAutoCorrecting(false);
272
view->setCancelOnBackspace(1);
273
instance->set_eim(&EIM);
280
* @brief Destroy the input method while unload it.
287
CSunpinyinSessionFactory& fac = CSunpinyinSessionFactory::getFactory();
288
fac.destroySession(view);
297
* @brief Get the config description of fcitx-sunpinyin.
299
* @return ConfigFileDesc*
301
ConfigFileDesc* GetSunpinyinConfigDesc()
303
if (!sunpinyinConfigDesc)
306
tmpfp = GetXDGFileData("addon/fcitx-sunpinyin.desc", "r", NULL);
307
sunpinyinConfigDesc = ParseConfigFileDescFp(tmpfp);
311
return sunpinyinConfigDesc;
315
* @brief Load the config file for fcitx-sunpinyin
317
* @param Bool is reload or not
319
void LoadConfig(Bool reload)
321
ConfigFileDesc *configDesc = GetSunpinyinConfigDesc();
323
FILE *fp = GetXDGFileUser( "addon/fcitx-sunpinyin.config", "rt", NULL);
327
if (!reload && errno == ENOENT)
329
char *lastdomain = strdup(textdomain(NULL));
330
textdomain("fcitx-sunpinyin");
332
textdomain(lastdomain);
338
ConfigFile *cfile = ParseConfigFileFp(fp, configDesc);
342
FcitxSunpinyinConfigConfigBind(&fs, cfile, configDesc);
343
ConfigBindSync((GenericConfig*)&fs);
347
fs.bUseShuangpin = False;
348
fs.SPScheme = MS2003;
349
fs.bFuzzySegmentation = False;
350
fs.bFuzzyInnerSegmentation = False;
352
for (i = 0; i < FUZZY_SIZE; i ++)
353
fs.bFuzzy[i] = False;
355
for (i = 0; i < CORRECT_SIZE; i ++)
356
fs.bAutoCorrecting[i] = False;
362
* @brief Save the config
368
ConfigFileDesc *configDesc = GetSunpinyinConfigDesc();
369
FILE *fp = GetXDGFileUser( "addon/fcitx-sunpinyin.config", "wt", NULL);
370
SaveConfigFileFp(fp, fs.gconfig.configFile, configDesc);