Debian Codegraph @

src/nicola.cpp

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Copyright (C) 2004 Hiroyuki Ikezoe
4 * Copyright (C) 2004 Takuro Ashie
5 * Copyright (C) 2012 CSSlayer
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22 #include "nicola.h"
23 #include "factory.h"
24 #include "imengine.h"
25 #include "utils.h"
26 #include <fcitx-utils/log.h>
27
28 void NicolaTimeoutFunc(void* arg)
29 {
30 NicolaConvertor* convertor = (NicolaConvertor*) arg;
31 convertor->process_timeout();
32 }
33
34 NicolaConvertor::NicolaConvertor (AnthyInstance &anthy,
35 Key2KanaTableSet &tables)
36 : m_tables (tables),
37 m_anthy (anthy),
38 m_timer_id (0),
39 m_processing_timeout(false)
40 {
41 }
42
43 NicolaConvertor::~NicolaConvertor ()
44 {
45 FcitxInstanceRemoveTimeoutByFunc(m_anthy.get_owner(), NicolaTimeoutFunc);
46 }
47
48 bool
49 NicolaConvertor::can_append (const KeyEvent & key,
50 bool ignore_space)
51 {
52 if (key == m_through_key_event) {
53 m_through_key_event = KeyEvent ();
54 return false;
55 }
56
57 if (m_processing_timeout &&
58 m_prev_char_key.empty () && !m_prev_thumb_key.empty())
59 {
60 emit_key_event (m_prev_thumb_key);
61 m_prev_thumb_key = KeyEvent ();
62 return false;
63 }
64
65 if (key.is_release &&
66 (key.sym != m_prev_char_key.sym &&
67 key.sym != m_repeat_char_key.sym &&
68 key.sym != m_prev_thumb_key.sym &&
69 key.sym != m_repeat_thumb_key.sym
70 ))
71 {
72 return false;
73 }
74
75 // ignore short cut keys of apllication.
76 if ((key.state & FcitxKeyState_Ctrl) ||
77 (key.state & FcitxKeyState_Alt) ||
78 (key.state & FcitxKeyState_Super))
79 {
80 return false;
81 }
82
83 if (isprint (key.get_ascii_code ()) &&
84 (ignore_space || !isspace (key.get_ascii_code ())))
85 {
86 return true;
87 }
88
89 if (is_thumb_key (key))
90 return true;
91
92 return false;
93 }
94
95 void
96 NicolaConvertor::search (const KeyEvent& key,
97 NicolaShiftType shift_type,
98 std::string &result,
99 std::string &raw)
100 {
101 raw = key.get_ascii_code ();
102
103 std::string str1;
104 if (get_case_sensitive ())
105 str1 = raw;
106 else
107 str1 = tolower (key.get_ascii_code ());
108
109 std::vector<Key2KanaTable*> &tables = m_tables.get_tables();
110 for (unsigned int j = 0; j < tables.size (); j++) {
111 if (!tables[j])
112 continue;
113
114 Key2KanaRules &rules = tables[j]->get_table ();
115
116 for (unsigned int i = 0; i < rules.size (); i++) {
117 std::string str2 = rules[i].get_sequence ();
118
119 for (unsigned int k = 0;
120 !get_case_sensitive () && k < str2.length ();
121 k++)
122 {
123 str2[k] = tolower (str2[k]);
124 }
125
126 if (str1 == str2) {
127 switch (shift_type) {
128 case FCITX_ANTHY_NICOLA_SHIFT_RIGHT:
129 result = rules[i].get_result (2);
130 break;
131 case FCITX_ANTHY_NICOLA_SHIFT_LEFT:
132 result = rules[i].get_result (1);
133 break;
134 default:
135 result = rules[i].get_result (0);
136 break;
137 }
138 break;
139 }
140 }
141 }
142
143 if (result.empty ()) {
144 result = raw;
145 }
146 }
147
148 bool
149 NicolaConvertor::handle_voiced_consonant (std::string & result,
150 std::string & pending)
151 {
152 VoicedConsonantRule *table = fcitx_anthy_voiced_consonant_table;
153
154 if (result.empty ())
155 return false;
156
157 if (m_pending.empty ()) {
158 for (unsigned int i = 0; table[i].string; i++) {
159 if (result == table[i].string) {
160 pending = m_pending = result;
161 result = std::string ();
162 return false;
163 }
164 }
165
166 } else if (result == "\xE3\x82\x9B") {
167 // voiced consonant
168 for (unsigned int i = 0; table[i].string; i++) {
169 if (m_pending == table[i].string) {
170 result = table[i].voiced;
171 m_pending = std::string ();
172 return false;
173 }
174 }
175 return true;
176
177 } else if (result == "\xE3\x82\x9C") {
178 // half voiced consonant
179 for (unsigned int i = 0; table[i].string; i++) {
180 if (m_pending == table[i].string) {
181 result = table[i].half_voiced;
182 m_pending = std::string ();
183 return false;
184 }
185 }
186 return true;
187
188 } else {
189 m_pending = std::string ();
190 for (unsigned int i = 0; table[i].string; i++) {
191 if (result == table[i].string) {
192 pending = m_pending = result;
193 result = std::string ();
194 return true;
195 }
196 }
197 return true;
198 }
199
200 return false;
201 }
202
203 bool
204 NicolaConvertor::is_char_key (const KeyEvent& key)
205 {
206 if (!is_thumb_key (key) && isprint (key.get_ascii_code ()))
207 return true;
208 else
209 return false;
210 }
211
212 bool
213 NicolaConvertor::is_thumb_key (const KeyEvent& key)
214 {
215 if (is_left_thumb_key (key) || is_right_thumb_key (key))
216 return true;
217
218 return false;
219 }
220
221 bool
222 NicolaConvertor::is_left_thumb_key (const KeyEvent& key)
223 {
224 return util_match_key_event (m_anthy.get_config()->m_left_thumb_keys,
225 key,
226 0xFFFF);
227 }
228
229 bool
230 NicolaConvertor::is_right_thumb_key (const KeyEvent& key)
231 {
232 return util_match_key_event (m_anthy.get_config()->m_right_thumb_keys,
233 key,
234 0xFFFF);
235 }
236
237 NicolaShiftType
238 NicolaConvertor::get_thumb_key_type (const KeyEvent& key)
239 {
240 if (is_left_thumb_key (key))
241 return FCITX_ANTHY_NICOLA_SHIFT_LEFT;
242 else if (is_right_thumb_key (key))
243 return FCITX_ANTHY_NICOLA_SHIFT_RIGHT;
244 else
245 return FCITX_ANTHY_NICOLA_SHIFT_NONE;
246 }
247
248 bool
249 NicolaConvertor::emit_key_event (const KeyEvent & key)
250 {
251 m_through_key_event = key;
252
253 //m_anthy.forward_key_event (key);
254 return m_anthy.process_key_event (key);
255 }
256
257 void
258 NicolaConvertor::process_timeout (void)
259 {
260 m_processing_timeout = true;
261 if (!m_prev_char_key.empty ())
262 m_anthy.process_key_event (m_prev_char_key);
263 else if (!m_prev_thumb_key.empty())
264 m_anthy.process_key_event (m_prev_thumb_key);
265 m_processing_timeout = false;
266 }
267
268 void
269 NicolaConvertor::set_alarm (int time_msec)
270 {
271 if (time_msec < 5)
272 time_msec = 5;
273 if (time_msec > 1000)
274 time_msec = 1000;
275
276 FcitxInstanceAddTimeout (m_anthy.get_owner(),
277 time_msec,
278 NicolaTimeoutFunc,
279 (void *) this);
280 }
281
282 bool
283 NicolaConvertor::stop()
284 {
285 return FcitxInstanceRemoveTimeoutByFunc(m_anthy.get_owner(), NicolaTimeoutFunc);
286 }
287
288 int
289 NicolaConvertor::thumb_key(const KeyEvent& key)
290 {
291 return is_left_thumb_key(key) ? 1 : 2;
292 }
293
294 bool
295 NicolaConvertor::append (const KeyEvent & key,
296 std::string & result,
297 std::string & pending,
298 std::string &raw)
299 {
300 if (m_processing_timeout) {
301 search (m_prev_char_key,
302 get_thumb_key_type (m_prev_thumb_key),
303 result, raw);
304 if (m_prev_thumb_key.empty ()) {
305 m_prev_char_key = KeyEvent ();
306 m_prev_thumb_key = KeyEvent ();
307 } else {
308 m_repeat_char_key = m_prev_char_key;
309 m_repeat_thumb_key = m_prev_thumb_key;
310 }
311 return handle_voiced_consonant (result, pending);
312 }
313
314 if (!key.is_release && util_key_is_keypad (key)) {
315 util_keypad_to_string (raw, key);
316
317 // convert key pad string to wide
318 std::string wide;
319 TenKeyType ten_key_type = m_anthy.get_config()->m_ten_key_type;
320 if ((ten_key_type == FCITX_ANTHY_TEN_KEY_TYPE_FOLLOWMODE &&
321 (m_anthy.get_input_mode () == FCITX_ANTHY_MODE_LATIN ||
322 m_anthy.get_input_mode () == FCITX_ANTHY_MODE_HALF_KATAKANA)) ||
323 ten_key_type == FCITX_ANTHY_TEN_KEY_TYPE_HALF)
324 {
325 wide = raw;
326 } else {
327 util_convert_to_wide (wide, raw);
328 }
329
330 result = wide;
331
332 m_prev_char_key = m_repeat_char_key = KeyEvent ();
333 m_prev_thumb_key = m_repeat_thumb_key = KeyEvent ();
334
335 return handle_voiced_consonant (result, pending);
336 }
337
338 if (key.is_release) {
339 if (key == m_prev_char_key) {
340 if (stop()) {
341 search (m_prev_char_key, get_thumb_key_type(m_prev_thumb_key), result, raw);
342 }
343 m_prev_char_key = KeyEvent();
344 }
345 else if (get_thumb_key_type(key) == get_thumb_key_type(m_prev_thumb_key)) {
346 if (stop())
347 emit_key_event(m_prev_thumb_key);
348 m_prev_thumb_key = KeyEvent();
349 }
350 if (is_thumb_key(key))
351 m_repeat_thumb_key = KeyEvent();
352 else if (key == m_repeat_char_key) {
353 m_repeat_char_key = KeyEvent();
354 }
355 }
356 else if (is_thumb_key(key)) {
357 if (!m_repeat_thumb_key.empty() && !(key == m_repeat_thumb_key)) {
358 stop();
359 m_prev_char_key = m_prev_thumb_key = m_repeat_char_key = m_repeat_thumb_key = KeyEvent();
360 }
361 if (!m_prev_thumb_key.empty()) {
362 stop();
363 emit_key_event(m_prev_thumb_key);
364 m_prev_thumb_key = key;
365 set_alarm(m_anthy.get_config()->m_nicola_time);
366 }
367 else if (!m_prev_char_key.empty()) {
368 stop();
369 m_repeat_char_key = m_prev_char_key;
370 m_repeat_thumb_key = key;
371 search (m_prev_char_key, get_thumb_key_type(m_repeat_thumb_key), result, raw);
372 }
373 else {
374 if (get_thumb_key_type(m_repeat_thumb_key) == get_thumb_key_type(key)) {
375 if (!m_repeat_char_key.empty()) {
376 search (m_repeat_char_key, get_thumb_key_type(m_repeat_thumb_key), result, raw);
377 }
378 }
379 else {
380 m_prev_thumb_key = key;
381 set_alarm(m_anthy.get_config()->m_nicola_time);
382 }
383 }
384 }
385 else if (is_char_key (key)) {
386 if (!m_repeat_char_key.empty() && !(key == m_repeat_char_key)) {
387 stop();
388 m_prev_char_key = m_prev_thumb_key = m_repeat_char_key = m_repeat_thumb_key = KeyEvent();
389 }
390 if (!m_prev_char_key.empty()) {
391 stop();
392 search (m_prev_char_key, get_thumb_key_type(m_prev_thumb_key), result, raw);
393 set_alarm(m_anthy.get_config()->m_nicola_time);
394 m_prev_char_key = key;
395 }
396 else if (is_thumb_key(m_prev_thumb_key)) {
397 stop();
398 m_repeat_char_key = key;
399 m_repeat_thumb_key = m_prev_thumb_key;
400 search (key, get_thumb_key_type(m_prev_thumb_key), result, raw);
401 }
402 else {
403 if (key == m_repeat_char_key) {
404 if (!m_repeat_thumb_key.empty()) {
405 search (m_repeat_char_key, get_thumb_key_type(m_repeat_thumb_key), result, raw);
406 }
407 }
408 else {
409 set_alarm(m_anthy.get_config()->m_nicola_time);
410 m_prev_char_key = key;
411 }
412 }
413 }
414 else {
415 if (!m_prev_char_key.empty()) {
416 stop();
417 search (m_prev_char_key, get_thumb_key_type(m_prev_thumb_key), result, raw);
418 }
419 else if (!m_prev_thumb_key.empty()) {
420 stop();
421 emit_key_event(m_prev_thumb_key);
422 }
423 if (emit_key_event(key))
424 return true;
425 }
426
427 FcitxLog(DEBUG, "prev:%s %d %d %d", __func__, m_prev_char_key.sym, m_prev_char_key.state, m_prev_char_key.is_release);
428
429 return handle_voiced_consonant (result, pending);
430 }
431
432 bool
433 NicolaConvertor::append (const std::string & str,
434 std::string & result,
435 std::string & pending)
436 {
437 result = str;
438 m_pending = std::string ();
439
440 return false;
441 }
442
443 void
444 NicolaConvertor::clear (void)
445 {
446 m_pending = std::string ();
447 m_prev_char_key = KeyEvent();
448 m_prev_thumb_key = KeyEvent();
449 m_repeat_char_key = KeyEvent();
450 m_repeat_thumb_key = KeyEvent();
451 }
452
453 bool
454 NicolaConvertor::is_pending (void)
455 {
456 return !m_pending.empty ();
457 }
458
459 std::string
460 NicolaConvertor::get_pending (void)
461 {
462 return m_pending;
463 }
464
465 std::string
466 NicolaConvertor::flush_pending (void)
467 {
468 return std::string ();
469 }
470
471 void
472 NicolaConvertor::reset_pending (const std::string & result, const std::string & raw)
473 {
474 VoicedConsonantRule *table = fcitx_anthy_voiced_consonant_table;
475
476 m_pending = std::string ();
477
478 for (unsigned int i = 0; table[i].string; i++) {
479 if (result == table[i].string) {
480 m_pending = result;
481 return;
482 }
483 }
484 }
485 /*
486 vi:ts=4:nowrap:ai:expandtab
487 */