1 /***************************************************************************
2  *   Copyright (C) 2002~2005 by Yuking                                     *
3  *   yuking_net@sohu.com                                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
19  ***************************************************************************/
20 #ifndef _PY_H
21 #define _PY_H
22 
23 #include "fcitx/ime.h"
24 #include "fcitx/fcitx.h"
25 #include "fcitx-utils/memory.h"
26 #include "fcitx/candidate.h"
27 #include "fcitx/instance.h"
28 #include "pyconfig.h"
29 #include "pyParser.h"
30 
31 #define PY_BASE_FILE    "pybase.mb"
32 #define PY_PHRASE_FILE  "pyphrase.mb"
33 #define PY_USERPHRASE_FILE "pyusrphrase.mb"
34 #define PY_INDEX_FILE   "pyindex.dat"
35 #define PY_FREQ_FILE    "pyfreq.mb"
36 #define PY_SYMBOL_FILE  "pySym.mb"
37 
38 #define AUTOSAVE_PHRASE_COUNT   1024
39 #define AUTOSAVE_ORDER_COUNT    1024
40 #define AUTOSAVE_FREQ_COUNT     32
41 
42 typedef enum {
43     FIND_PHRASE,
44     FIND_BASE,
45     FIND_FREQ,
46 } FINDMODE;
47 
48 typedef enum {
49     PY_CAND_AUTO,
50     PY_CAND_BASE,
51     PY_CAND_SYSPHRASE,
52     PY_CAND_USERPHRASE,
53     PY_CAND_FREQ,
54     PY_CAND_REMIND
55 } PY_CAND_WORD_TYPE;
56 
57 typedef struct _HZ {
58     char            strHZ[MAX_PY_PHRASE_LENGTH * UTF8_MAX_LENGTH + 1];
59     int32_t         iPYFA;
60     uint32_t        iHit;
61     uint32_t        iIndex;
62     struct _HZ     *next;
63 } HZ;
64 
65 typedef struct _PYFREQ {
66     HZ             *HZList;
67     char            strPY[MAX_PY_PHRASE_LENGTH * MAX_PY_LENGTH + 1];
68     uint32_t        iCount;
69     struct _PYFREQ  *next;
70 } PyFreq;
71 
72 typedef struct {
73     char           *strPhrase;
74     char           *strMap;
75     uint32_t       iIndex;
76     uint32_t       iHit;
77 } PyPhrase;
78 
79 typedef struct _PYUSRPHRASE {
80     PyPhrase phrase;
81     struct _PYUSRPHRASE* next;
82 } PyUsrPhrase;
83 
84 #define USER_PHRASE_NEXT(x) (&((PyUsrPhrase*)(x))->next->phrase)
85 
86 typedef struct {
87     char            strHZ[UTF8_MAX_LENGTH + 1];
88     PyPhrase *phrase;
89     int             iPhrase;
90     PyUsrPhrase *userPhrase;
91     int             iUserPhrase;
92     uint32_t        iIndex;
93     uint32_t        iHit;
94 } PyBase;
95 
96 typedef struct {
97     char strMap[3];
98     PyBase *pyBase;
99     int32_t iBase;
100 } PYFA;
101 
102 typedef struct {
103     HZ             *hz;
104     char           *strPY;
105     PyFreq         *pyFreq;
106 } PYFreqCandWord;
107 
108 typedef struct {
109     int32_t iPYFA;
110     int32_t iBase;
111     PyPhrase *phrase;
112 } PYPhraseCandWord;
113 
114 typedef struct {
115     int32_t iPYFA;
116     int32_t iBase;
117 } PYBaseCandWord;
118 
119 typedef struct {
120     PyPhrase *phrase;
121     int      iLength;
122 } PYRemindCandWord;
123 
124 typedef union {
125     PYFreqCandWord  freq;
126     PYBaseCandWord  base;
127     PYPhraseCandWord phrase;
128     PYRemindCandWord remind;
129 } PCand;
130 
131 typedef struct {
132     PCand                 cand;
133     /**
134      * 0->Auto
135      * 1->System single HZ
136      * 2->System phrase
137      * 3->User phrase
138      * 4->frequent HZ
139      **/
140     PY_CAND_WORD_TYPE     iWhich;
141 } PYCandWord;
142 
143 typedef struct {
144     int32_t iPYFA;
145     int32_t iBase;
146     int32_t iPhrase;
147 } PYCandIndex;
148 
149 typedef struct {
150     char            strPY[(MAX_PY_LENGTH + 1) * MAX_PY_PHRASE_LENGTH + 1];
151     char            strHZ[MAX_PY_PHRASE_LENGTH * UTF8_MAX_LENGTH + 1];
152     char            strMap[MAX_PY_PHRASE_LENGTH * 2 + 1];
153 } PY_SELECTED;
154 
155 typedef struct _FcitxPinyinState {
156     FcitxPinyinConfig pyconfig;
157 
158     int32_t iPYFACount;
159     PYFA *PYFAList;
160     uint32_t iCounter;
161     uint32_t iOrigCounter;
162     boolean bPYBaseDictLoaded;
163     boolean bPYOtherDictLoaded;
164 
165     PyFreq *pyFreq;
166     uint32_t iPYFreqCount;
167 
168     char strFindString[MAX_USER_INPUT + 2];
169     ParsePYStruct findMap;
170     int iPYInsertPoint;
171 
172     char strPYRemindSource[MAX_WORDS_USER_INPUT * UTF8_MAX_LENGTH + 1];
173     char strPYRemindMap[MAX_WORDS_USER_INPUT * 2 + 1];
174 
175     PY_SELECTED pySelected[MAX_WORDS_USER_INPUT + 1];
176     unsigned int iPYSelected;
177 
178     char strPYAuto[MAX_WORDS_USER_INPUT * UTF8_MAX_LENGTH + 1];
179     char strPYAutoMap[MAX_WORDS_USER_INPUT * 2 + 1];
180 
181     int iNewPYPhraseCount;
182     int iOrderCount;
183     int iNewFreqCount;
184 
185     boolean bIsPYAddFreq;
186     boolean bIsPYDelFreq;
187     boolean bIsPYDelUserPhr;
188 
189     boolean bSP_UseSemicolon;
190     boolean bSP;
191 
192     FcitxMemoryPool *pool;
193 
194     FcitxInstance *owner;
195 } FcitxPinyinState;
196 
197 void *PYCreate(FcitxInstance *instance);
198 void PYDestroy(void* arg);
199 boolean         PYInit(void* arg);
200 boolean         SPInit(void* arg);
201 boolean         LoadPYBaseDict(FcitxPinyinState* pystate);
202 boolean         LoadPYOtherDict(FcitxPinyinState* pystate);
203 void            ResetPYStatus(void* pystate);
204 int             GetBaseIndex(FcitxPinyinState* pystate, int32_t iPYFA,
205                              char *strBase);
206 INPUT_RETURN_VALUE DoPYInput(void* arg, FcitxKeySym sym, unsigned int state);
207 void            UpdateCodeInputPY(FcitxPinyinState* pystate);
208 void            UpdateFindString(FcitxPinyinState* pystate, int val);
209 void            CalculateCursorPosition(FcitxPinyinState* pystate);
210 
211 void            PYCreateAuto(FcitxPinyinState* pystate);
212 INPUT_RETURN_VALUE PYGetCandWords(void* arg);
213 INPUT_RETURN_VALUE PYGetCandWord(void* arg, FcitxCandidateWord *pycandWord);
214 void            PYGetBaseCandWords(FcitxPinyinState* pystate, PyFreq* pCurFreq);
215 void PYAddBaseCandWord(PYCandIndex pos, PYCandWord* pycandWord);
216 void            PYGetFreqCandWords(FcitxPinyinState* pystate, PyFreq* pyFreq);
217 void            PYAddFreqCandWord(PyFreq* pyFreq, HZ* hz, char* strPY, PYCandWord* pycandWord);
218 void            PYGetPhraseCandWords(FcitxPinyinState* pystate);
219 boolean         PYAddPhraseCandWord(FcitxPinyinState* pystate, PYCandIndex pos, PyPhrase* phrase, boolean b, PYCandWord* pycandWord);
220 boolean         PYAddUserPhrase(FcitxPinyinState* pystate, const char* phrase,
221                                 const char* map, boolean incHit);
222 void            PYDelUserPhrase(FcitxPinyinState* pystate, int32_t iPYFA,
223                                 int iBase, PyUsrPhrase* phrase);
224 int             GetBaseMapIndex(FcitxPinyinState* pystate, char *strMap);
225 void            SavePYUserPhrase(FcitxPinyinState* pystate);
226 void            SavePYFreq(FcitxPinyinState* pystate);
227 void            SavePYIndex(FcitxPinyinState* pystate);
228 void            SavePY(void *arg);
229 
230 void            PYAddFreq(FcitxPinyinState* pystate, PYCandWord* pycandWord);
231 void            PYDelFreq(FcitxPinyinState* pystate, PYCandWord* pycandWord);
232 boolean         PYIsInFreq(PyFreq* pCurFreq, char* strHZ);
233 
234 INPUT_RETURN_VALUE PYGetRemindCandWords(void* arg);
235 void            PYAddRemindCandWord(FcitxPinyinState* pystate, PyPhrase * phrase, PYCandWord* pycandWord);
236 void            PYGetPYByHZ(FcitxPinyinState* pystate, const char *strHZ, char *strPY);
237 
238 #endif
239 
240 // kate: indent-mode cstyle; space-indent on; indent-width 0;
241