forked from lc-soft/LCUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathime.c
285 lines (263 loc) · 6.56 KB
/
ime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* ime.c -- Input Method Engine
*
* Copyright (c) 2018, Liu chao <[email protected]> All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of LCUI nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/* FIXME: redesign the input method engine
* The current design used is not robust and needs to be redesigned with
* reference to other complete input method engine frameworks.
*/
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <LCUI_Build.h>
#include <LCUI/LCUI.h>
#include <LCUI/input.h>
#include <LCUI/ime.h>
typedef struct LCUI_IMERec_ {
int id;
char *name;
LCUI_IMEHandlerRec handler;
LinkedListNode node;
} LCUI_IMERec, *LCUI_IME;
static struct LCUI_InputMethodEngine {
int id_count;
LinkedList list;
LCUI_IME ime;
LCUI_BOOL enable_caps_lock;
LCUI_BOOL active;
} self;
static LCUI_IME LCUIIME_Find(int ime_id)
{
LinkedListNode *node;
for (LinkedList_Each(node, &self.list)) {
LCUI_IME ime = node->data;
if (ime->id == ime_id) {
return ime;
}
}
return NULL;
}
static LCUI_IME LCUIIME_FindByName(const char *name)
{
LinkedListNode *node;
for (LinkedList_Each(node, &self.list)) {
LCUI_IME ime = node->data;
if (strcmp(ime->name, name) == 0) {
return ime;
}
}
return NULL;
}
int LCUIIME_Register(const char *name, LCUI_IMEHandler handler)
{
size_t len;
LCUI_IME ime;
if (!self.active) {
return -1;
}
if (LCUIIME_FindByName(name)) {
return -2;
}
ime = NEW(LCUI_IMERec, 1);
if (!ime) {
return -ENOMEM;
}
len = strlen(name) + 1;
ime->name = malloc(len * sizeof(char));
if (!ime->name) {
return -ENOMEM;
}
self.id_count += 1;
ime->id = self.id_count;
ime->node.data = ime;
strncpy(ime->name, name, len);
memcpy(&ime->handler, handler, sizeof(LCUI_IMEHandlerRec));
LinkedList_AppendNode(&self.list, &ime->node);
return ime->id;
}
static LCUI_BOOL LCUIIME_Open(LCUI_IME ime)
{
if (ime && ime->handler.open) {
return ime->handler.open();
}
return FALSE;
}
static LCUI_BOOL LCUIIME_Close(LCUI_IME ime)
{
if (ime && ime->handler.close) {
Logger_Debug("[ime] close engine: %s\n", ime->name);
return ime->handler.close();
}
return FALSE;
}
LCUI_BOOL LCUIIME_Select(int ime_id)
{
LCUI_IME ime = LCUIIME_Find(ime_id);
if (ime) {
LCUIIME_Close(self.ime);
Logger_Debug("[ime] select engine: %s\n", ime->name);
self.ime = ime;
LCUIIME_Open(self.ime);
return TRUE;
}
return FALSE;
}
LCUI_BOOL LCUIIME_SelectByName(const char *name)
{
LCUI_IME ime = LCUIIME_FindByName(name);
if (ime) {
LCUIIME_Close(self.ime);
self.ime = ime;
LCUIIME_Open(self.ime);
return TRUE;
}
return FALSE;
}
void LCUIIME_Switch(void)
{
LCUI_IME ime;
if (self.ime && self.ime->node.next) {
ime = self.ime->node.next->data;
LCUIIME_Close(self.ime);
self.ime = ime;
LCUIIME_Open(self.ime);
}
}
static void LCUIIME_OnDestroy(void *arg)
{
LCUI_IME ime = arg;
if (self.ime == ime) {
self.ime = NULL;
}
free(ime->name);
free(ime);
}
LCUI_BOOL LCUIIME_CheckCharKey(int key)
{
switch (key) {
case LCUI_KEY_TAB:
case LCUI_KEY_ENTER:
case LCUI_KEY_SPACE:
case LCUI_KEY_SEMICOLON:
case LCUI_KEY_MINUS:
case LCUI_KEY_EQUAL:
case LCUI_KEY_COMMA:
case LCUI_KEY_PERIOD:
case LCUI_KEY_SLASH:
case LCUI_KEY_BRACKETLEFT:
case LCUI_KEY_BACKSLASH:
case LCUI_KEY_BRACKETRIGHT:
case LCUI_KEY_APOSTROPHE:
return TRUE;
default:
if (key >= LCUI_KEY_0 && key <= LCUI_KEY_Z) {
return TRUE;
}
break;
}
return FALSE;
}
static void LCUIIME_ToText(LCUI_SysEvent e)
{
self.ime->handler.totext(e->key.code);
}
LCUI_BOOL LCUIIME_ProcessKey(LCUI_SysEvent e)
{
int key_state;
/* 根据事件类型判定按键状态 */
if (e->type == LCUI_KEYUP) {
key_state = LCUI_KSTATE_RELEASE;
/* 如果是 caps lock 按键被释放 */
if (e->key.code == LCUI_KEY_CAPITAL) {
self.enable_caps_lock = !self.enable_caps_lock;
return FALSE;
}
} else {
key_state = LCUI_KSTATE_PRESSED;
/* 如果按下的是 shift 键,但没释放,则直接退出 */
if (e->key.code == LCUI_KEY_SHIFT) {
return FALSE;
}
}
if (self.ime && self.ime->handler.prockey) {
return self.ime->handler.prockey(e->key.code, key_state);
}
return FALSE;
}
int LCUIIME_Commit(const wchar_t *str, size_t len)
{
LCUI_SysEventRec sys_ev;
if (len == 0) {
len = wcslen(str);
}
sys_ev.type = LCUI_TEXTINPUT;
sys_ev.text.length = len;
sys_ev.text.text = NEW(wchar_t, len + 1);
if (!sys_ev.text.text) {
return -ENOMEM;
}
wcsncpy(sys_ev.text.text, str, len + 1);
LCUI_TriggerEvent(&sys_ev, NULL);
free(sys_ev.text.text);
sys_ev.text.text = NULL;
sys_ev.text.length = 0;
return 0;
}
static void LCUIIME_OnKeyDown(LCUI_SysEvent e, void *arg)
{
if (LCUIIME_ProcessKey(e)) {
LCUIIME_ToText(e);
}
}
void LCUI_InitIME(void)
{
LinkedList_Init(&self.list);
self.active = TRUE;
LCUI_BindEvent(LCUI_KEYDOWN, LCUIIME_OnKeyDown, NULL, NULL);
#ifdef WINAPI_FAMILY_APP
return;
#else
#ifdef LCUI_BUILD_IN_WIN32
LCUIIME_Select(LCUI_RegisterWin32IME());
#else
LCUIIME_Select(LCUI_RegisterLinuxIME());
#endif
#endif
}
void LCUIIME_SetCaret(int x, int y)
{
if (self.ime && self.ime->handler.setcaret) {
self.ime->handler.setcaret(x, y);
}
}
void LCUI_FreeIME(void)
{
self.active = FALSE;
LCUIIME_Close(self.ime);
LinkedList_ClearData(&self.list, LCUIIME_OnDestroy);
}