~fcitx-team/fcitx/debian-sunpinyin

« back to all changes in this revision

Viewing changes to src/eim.cpp

  • Committer: Aron Xu
  • Date: 2012-01-24 19:46:25 UTC
  • Revision ID: git-v1:aee2237cc8a5aa9fec8671e1aa8b69bd77010a87
Tags: upstream/0.2.1
ImportedĀ UpstreamĀ versionĀ 0.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  Copyright (C) 2010~2010 by CSSlayer
 
2
    wengxt@gmail.com 
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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/>.  */
 
16
 
 
17
#include <stdlib.h>
 
18
#include <fcitx/im.h>
 
19
#include <stdio.h>
 
20
#include <string.h>
 
21
#include <errno.h>
 
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>
 
27
#include <string>
 
28
#include <libintl.h>
 
29
 
 
30
#include "handler.h"
 
31
#include "eim.h"
 
32
 
 
33
#define _(x) (x)
 
34
 
 
35
#ifdef __cplusplus
 
36
extern "C" {
 
37
#endif
 
38
    EXTRA_IM EIM = {
 
39
        _("Sunpinyin"), /* Name */
 
40
        "fcitx-sunpinyin", /* IconName */
 
41
        Reset, /* Reset */
 
42
        DoInput, /* DoInput */
 
43
        GetCandWords, /* GetCandWords */
 
44
        GetCandWord,
 
45
        Init,
 
46
        Destroy,
 
47
        NULL,
 
48
        NULL,
 
49
        NULL,
 
50
        NULL,
 
51
        NULL,
 
52
        0,
 
53
        0,
 
54
        0,
 
55
        0,
 
56
        0,
 
57
        0,
 
58
        0,
 
59
        NULL,
 
60
        NULL
 
61
    };
 
62
#ifdef __cplusplus
 
63
}
 
64
#endif
 
65
 
 
66
static ConfigFileDesc* GetSunpinyinConfigDesc();
 
67
static void LoadConfig(Bool reload = False);
 
68
static void SaveConfig();
 
69
 
 
70
static FcitxWindowHandler* instance = NULL;
 
71
static CIMIView* view = NULL;
 
72
static ConfigFileDesc* sunpinyinConfigDesc;
 
73
static FcitxSunpinyinConfig fs;
 
74
 
 
75
static const char* fuzzyPairs[][2] = {
 
76
    {"sh", "s"},
 
77
    {"zh", "z"},
 
78
    {"ch", "c"},
 
79
    {"an", "ang"},
 
80
    {"on", "ong"},
 
81
    {"en", "eng"},
 
82
    {"in", "ing"},
 
83
    {"eng", "ong"},
 
84
    {"ian", "iang"},
 
85
    {"uan", "uang"},
 
86
    {"n", "l"},
 
87
    {"f", "h"},
 
88
    {"l", "r"},
 
89
    {"k", "g"}
 
90
};
 
91
 
 
92
static const char *correctionPairs[][2] = {
 
93
    {"ign", "ing"},
 
94
    {"ogn", "ong"},
 
95
    {"uen", "un"},
 
96
    {"img", "ing"},
 
97
    {"iou", "iu"},
 
98
    {"uei", "ui"}
 
99
};
 
100
 
 
101
/**
 
102
 * @brief Reset the status.
 
103
 *
 
104
 **/
 
105
__EXPORT_API
 
106
void Reset (void)
 
107
{
 
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);
 
113
    view->clearIC();
 
114
}
 
115
 
 
116
/**
 
117
 * @brief Process Key Input and return the status
 
118
 *
 
119
 * @param keycode keycode from XKeyEvent
 
120
 * @param state state from XKeyEvent
 
121
 * @param count count from XKeyEvent
 
122
 * @return INPUT_RETURN_VALUE
 
123
 **/
 
124
__EXPORT_API
 
125
INPUT_RETURN_VALUE DoInput (unsigned int keycode, unsigned int state, int count)
 
126
{
 
127
    if ((keycode <= 0x20 || keycode > 0x7E) && view->getIC()->isEmpty())
 
128
        return IRV_TO_PROCESS;
 
129
    
 
130
    if (keycode == 0x003b && view->getIC()->isEmpty())
 
131
        return IRV_TO_PROCESS;         
 
132
 
 
133
    if (keycode == 0xFF8D)
 
134
        keycode = 0xFF0D;
 
135
 
 
136
    instance->commit_flag = false;
 
137
    instance->candidate_flag = false;
 
138
    unsigned int changeMasks = view->onKeyEvent(CKeyEvent(keycode, keycode, state));
 
139
 
 
140
    if (instance->commit_flag)
 
141
        return IRV_GET_CANDWORDS;
 
142
    if (!(changeMasks & CIMIView::KEYEVENT_USED))
 
143
        return IRV_TO_PROCESS;
 
144
    
 
145
    if (view->getIC()->isEmpty())
 
146
        return IRV_CLEAN;
 
147
 
 
148
    if (instance->candidate_flag)
 
149
    {
 
150
        return IRV_DISPLAY_CANDWORDS;
 
151
    }
 
152
 
 
153
    return IRV_TO_PROCESS;
 
154
}
 
155
 
 
156
/**
 
157
 * @brief function DoInput has done everything for us.
 
158
 *
 
159
 * @param searchMode
 
160
 * @return INPUT_RETURN_VALUE
 
161
 **/
 
162
__EXPORT_API
 
163
INPUT_RETURN_VALUE GetCandWords(SEARCH_MODE searchMode)
 
164
{
 
165
    return IRV_DO_NOTHING;
 
166
}
 
167
 
 
168
/**
 
169
 * @brief get the candidate word by index
 
170
 *
 
171
 * @param iIndex index of candidate word
 
172
 * @return the string of canidate word
 
173
 **/
 
174
__EXPORT_API
 
175
char *GetCandWord (int iIndex)
 
176
{
 
177
    EIM.CandWordCount = 0;
 
178
    instance->commit_flag = false;
 
179
    instance->candidate_flag = false;
 
180
    if (iIndex <= 8)
 
181
    {
 
182
        unsigned int keycode = '1' + iIndex;
 
183
        unsigned int state = 0;
 
184
        unsigned int changeMasks = view->onKeyEvent(CKeyEvent(keycode, keycode, state));
 
185
 
 
186
        if (instance->commit_flag)
 
187
            return EIM.StringGet;
 
188
    }
 
189
    
 
190
    return NULL;
 
191
}
 
192
 
 
193
/**
 
194
 * @brief initialize the extra input method
 
195
 *
 
196
 * @param arg
 
197
 * @return successful or not
 
198
 **/
 
199
__EXPORT_API
 
200
int Init (char *arg)
 
201
{
 
202
    bindtextdomain("fcitx-sunpinyin", LOCALEDIR);
 
203
    GenericConfig *fc = (GenericConfig*)EIM.fc;
 
204
    ConfigValueType candword;
 
205
    ConfigValueType prevpage;
 
206
    ConfigValueType nextpage;
 
207
 
 
208
    LoadConfig();
 
209
 
 
210
    candword = ConfigGetBindValue(fc, "Appearance", "CandidateWordNumber");
 
211
    prevpage = ConfigGetBindValue(fc, "Hotkey", "PrevPageKey");
 
212
    nextpage = ConfigGetBindValue(fc, "Hotkey", "NextPageKey");
 
213
 
 
214
    CSunpinyinSessionFactory& fac = CSunpinyinSessionFactory::getFactory();
 
215
 
 
216
    if (fs.bUseShuangpin)
 
217
        fac.setPinyinScheme(CSunpinyinSessionFactory::SHUANGPIN);
 
218
    else
 
219
        fac.setPinyinScheme(CSunpinyinSessionFactory::QUANPIN);
 
220
 
 
221
    AShuangpinSchemePolicy::instance().setShuangpinType(fs.SPScheme);
 
222
    AQuanpinSchemePolicy::instance().setFuzzySegmentation(fs.bFuzzySegmentation);
 
223
    AQuanpinSchemePolicy::instance().setInnerFuzzySegmentation(fs.bFuzzyInnerSegmentation);
 
224
    view = fac.createSession();
 
225
 
 
226
    instance = new FcitxWindowHandler();
 
227
    view->getIC()->setCharsetLevel(1);// GBK
 
228
 
 
229
    view->setCandiWindowSize(*candword.integer);
 
230
    view->attachWinHandler(instance);
 
231
    // page up/down key
 
232
    CHotkeyProfile* prof = view->getHotkeyProfile();
 
233
    prof->clear();
 
234
 
 
235
    int i = 0;
 
236
    for (i = 0 ; i < 2; i++)
 
237
    {
 
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));
 
242
    }
 
243
    
 
244
    string_pairs fuzzy, correction;
 
245
    for (i = 0; i < FUZZY_SIZE; i++)
 
246
        if (fs.bFuzzy[i])
 
247
            fuzzy.push_back(std::make_pair<std::string, std::string>(fuzzyPairs[i][0], fuzzyPairs[i][1]));
 
248
    
 
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]));
 
252
    
 
253
    if (fuzzy.size() != 0)
 
254
    {
 
255
        AQuanpinSchemePolicy::instance().setFuzzyForwarding(true);
 
256
        AQuanpinSchemePolicy::instance().setFuzzyPinyinPairs(fuzzy);
 
257
    }
 
258
    else
 
259
    {
 
260
        AQuanpinSchemePolicy::instance().setFuzzyForwarding(false);
 
261
        AQuanpinSchemePolicy::instance().clearFuzzyPinyinPairs();
 
262
    }
 
263
    
 
264
    if (correction.size() != 0)
 
265
    {
 
266
        AQuanpinSchemePolicy::instance().setAutoCorrecting(true);
 
267
        AQuanpinSchemePolicy::instance().setAutoCorrectionPairs(correction);
 
268
    }
 
269
    else
 
270
        AQuanpinSchemePolicy::instance().setAutoCorrecting(false);
 
271
        
 
272
    view->setCancelOnBackspace(1);
 
273
    instance->set_eim(&EIM);
 
274
 
 
275
    return 0;
 
276
}
 
277
 
 
278
 
 
279
/**
 
280
 * @brief Destroy the input method while unload it.
 
281
 *
 
282
 * @return int
 
283
 **/
 
284
__EXPORT_API
 
285
int Destroy (void)
 
286
{
 
287
    CSunpinyinSessionFactory& fac = CSunpinyinSessionFactory::getFactory();
 
288
    fac.destroySession(view);
 
289
 
 
290
    if (instance)
 
291
        delete instance;
 
292
 
 
293
    return 0;
 
294
}
 
295
 
 
296
/**
 
297
 * @brief Get the config description of fcitx-sunpinyin.
 
298
 *
 
299
 * @return ConfigFileDesc*
 
300
 **/
 
301
ConfigFileDesc* GetSunpinyinConfigDesc()
 
302
{
 
303
    if (!sunpinyinConfigDesc)
 
304
    {
 
305
        FILE *tmpfp;
 
306
        tmpfp = GetXDGFileData("addon/fcitx-sunpinyin.desc", "r", NULL);
 
307
        sunpinyinConfigDesc = ParseConfigFileDescFp(tmpfp);
 
308
        fclose(tmpfp);
 
309
    }
 
310
 
 
311
    return sunpinyinConfigDesc;
 
312
}
 
313
 
 
314
/**
 
315
 * @brief Load the config file for fcitx-sunpinyin
 
316
 *
 
317
 * @param Bool is reload or not
 
318
 **/
 
319
void LoadConfig(Bool reload)
 
320
{
 
321
    ConfigFileDesc *configDesc = GetSunpinyinConfigDesc();
 
322
 
 
323
    FILE *fp = GetXDGFileUser( "addon/fcitx-sunpinyin.config", "rt", NULL);
 
324
 
 
325
    if (!fp)
 
326
    {
 
327
        if (!reload && errno == ENOENT)
 
328
        {
 
329
            char *lastdomain = strdup(textdomain(NULL));
 
330
            textdomain("fcitx-sunpinyin");
 
331
            SaveConfig();
 
332
            textdomain(lastdomain);
 
333
            free(lastdomain);
 
334
            LoadConfig(True);
 
335
        }
 
336
        return;
 
337
    }
 
338
    ConfigFile *cfile = ParseConfigFileFp(fp, configDesc);
 
339
 
 
340
    if (cfile)
 
341
    {
 
342
        FcitxSunpinyinConfigConfigBind(&fs, cfile, configDesc);
 
343
        ConfigBindSync((GenericConfig*)&fs);
 
344
    }
 
345
    else
 
346
    {
 
347
        fs.bUseShuangpin = False;
 
348
        fs.SPScheme = MS2003;
 
349
        fs.bFuzzySegmentation = False;
 
350
        fs.bFuzzyInnerSegmentation = False;
 
351
        int i = 0;
 
352
        for (i = 0; i < FUZZY_SIZE; i ++)
 
353
            fs.bFuzzy[i] = False;
 
354
        
 
355
        for (i = 0; i < CORRECT_SIZE; i ++)
 
356
            fs.bAutoCorrecting[i] = False;
 
357
    }
 
358
 
 
359
}
 
360
 
 
361
/**
 
362
 * @brief Save the config
 
363
 *
 
364
 * @return void
 
365
 **/
 
366
void SaveConfig()
 
367
{
 
368
    ConfigFileDesc *configDesc = GetSunpinyinConfigDesc();
 
369
    FILE *fp = GetXDGFileUser( "addon/fcitx-sunpinyin.config", "wt", NULL);
 
370
    SaveConfigFileFp(fp, fs.gconfig.configFile, configDesc);
 
371
    fclose(fp);
 
372
}