/
scim.cpp
690 lines (576 loc) · 19.3 KB
/
scim.cpp
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*
Copyright (c) 2004-2010 uim Project http://code.google.com/p/uim/
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. 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.
3. Neither the name of authors 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 HOLDERS 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.
*/
#include <config.h>
#define Uses_SCIM_CONFIG_PATH
#define Uses_SCIM_IMENGINE_MODULE
#define Uses_SCIM_BACKEND
#define Uses_SCIM_PANEL
#define Uses_SCIM_SOCKET
#define Uses_SCIM_SOCKET_TRANSACTION
#include <scim.h>
#include "uim.h"
#include "uim-scm.h"
#include "uim-internal.h"
#include "uim-notify.h"
// FIXME: unconditional fprintf()s
using namespace scim;
typedef struct SCIMInputMethod SCIMInputMethod;
struct SCIMInputMethod {
WideString imname;
String lang;
String uuid;
};
typedef struct SCIMContext SCIMContext;
struct SCIMContext {
IMEngineFactoryPointer factory;
IMEngineInstancePointer instance;
int id;
bool is_on;
WideString preedit_str;
AttributeList preedit_attr;
int preedit_caret;
int candidate_num;
char **candidates;
};
static int initialized = 0;
static std::vector<String> engine_list;
static std::vector<SCIMInputMethod *> im_list;
static std::vector<SCIMContext *> context_list;
static std::vector<String> config_modules;
static std::vector<String> factories;
static ConfigModule *config_module = NULL;
static ConfigPointer config = NULL;
static BackEndPointer be = NULL;
static SocketClient panel;
static int instance_count = 0;
static void cb_commit( IMEngineInstanceBase *instance, const WideString &wstr );
static void cb_preedit_update( IMEngineInstanceBase *instance, const WideString &wstr, const AttributeList &attr );
static void cb_preedit_hide( IMEngineInstanceBase *instance );
static void cb_preedit_caret( IMEngineInstanceBase *instance, int caret );
static void cb_lookup_update( IMEngineInstanceBase *instance, const LookupTable &table );
static void cb_lookup_show( IMEngineInstanceBase *instance );
static void cb_lookup_hide( IMEngineInstanceBase *instance );
static const int UIM_PREEDIT_FLAG_UNDERLINE = 1;
static const int UIM_PREEDIT_FLAG_REVERSE = 2;
static const int UIM_PREEDIT_FLAG_CURSOR = 4;
static void uim_eval_im_commit(int id, const char *str);
static void uim_eval_im_clear_preedit(int id);
static void uim_eval_im_pushback_preedit(int id, int flag, const char *str);
static void uim_eval_im_update_preedit(int id);
static void uim_eval_im_activate_candidate_selector(int id, int nr_candidates, int display_limit);
static void uim_eval_im_select_candidate(int id, int index);
static void uim_eval_im_deactivate_candidate_selector(int id);
static void uim_keysymbol_to_scim_keysymbol( const char *sym, KeyEvent *key );
#define WideStr_to_CStr(widestr) ((char *)utf8_wcstombs(widestr).c_str())
#define WideStr_to_String(widestr) ((String)utf8_wcstombs(widestr))
static bool
check_socket_frontend()
{
SocketAddress address;
SocketClient client;
uint32 magic;
address.set_address (scim_get_default_socket_frontend_address ());
if (!client.connect (address))
return false;
if (!scim_socket_open_connection (magic,
String ("ConnectionTester"),
String ("SocketFrontEnd"),
client,
1000))
{
return false;
}
return true;
}
static void
create_im_list()
{
std::vector<IMEngineFactoryPointer> factories;
int num = be->get_factories_for_language( factories );
std::vector<IMEngineFactoryPointer>::iterator it = factories.begin();
for ( ; it != factories.end(); ++it )
{
SCIMInputMethod *scim_im = new SCIMInputMethod();
// FIXME: IMEngineFactoryBase::get_name() returns the IM name
// for humans (and it is recommended to be 'localized'). So it
// does not fit the uim's symbolic IM name (idname), and
// causes parse errors on Scheme when being read as Scheme
// symbol. For example, an IM name "scim-Probhat(phonetic)"
// cannot be a safe Scheme symbol since it contains
// parenthesis. Some character replacements and
// anti-message-translation (localization) is required to
// provide uim IMs. uim rejects IMs that have invalid IM
// name. -- YamaKen 2008-03-24
scim_im->imname = (*it)->get_name();
scim_im->lang = (*it)->get_language();
scim_im->uuid = (*it)->get_uuid();
im_list.push_back( scim_im );
}
}
static uim_lisp
init_scim()
{
fprintf( stderr, "init_scim()\n" );
if ( !initialized )
{
context_list.clear();
scim_get_imengine_module_list( engine_list );
if ( std::find( engine_list.begin() , engine_list.end() , "socket") == engine_list.end() )
{
fprintf(stderr, "Could not find socket module.\n");
return uim_scm_f();
}
config_module = new ConfigModule( "simple" );
if ( !config_module )
{
uim_notify_fatal("init_scim: Could not create ConfigModule");
return uim_scm_f();
}
#if 0
/* for old versions (1.2 or older) of SCIM */
config = config_module->create_config("scim");
#else
/* for new versions (1.3, 1.4) of SCIM */
config = config_module->create_config();
#endif
if ( config.null() )
{
uim_notify_fatal("init_scim: create_config failed");
return uim_scm_f();
}
be = new CommonBackEnd( config, engine_list );
if ( be.null() )
{
uim_notify_fatal("init_scim: create CommonBackEnd failed");
return uim_scm_f();
}
if ( !check_socket_frontend() )
{
/*
* 2004-03-03 Kazuki Ohta <mover@hct.zaq.ne.jp> @ChinaOSS CodeFest
*
* If no SocketFrontend is runnig, launch a SCIM daemon.
*/
fprintf(stderr, "launch SCIM daemon\n");
char *new_argv [] = { (char *)"--no-stay", 0 };
scim_launch( true,
"simple", //FIX ME
(engine_list.size () ? scim_combine_string_list( engine_list, ',' ) : "all"),
"socket",
new_argv );
return uim_scm_f();
}
create_im_list();
initialized = 1;
}
return uim_scm_t();
}
static uim_lisp
get_nr_input_methods()
{
return uim_scm_make_int( im_list.size() );
}
static uim_lisp
get_input_method_lang(uim_lisp nth_)
{
// FIXME
int nth = uim_scm_c_int( nth_ );
if ( nth < (int)im_list.size() )
{
return uim_scm_make_str( im_list.at( nth )->lang.c_str() );
}
return uim_scm_f();
}
std::string& replace(std::string& str, const std::string sb, const std::string sa)
{
std::string::size_type n, nb = 0;
while ((n = str.find(sb,nb)) != std::string::npos)
{
str.replace(n,sb.size(),sa);
nb = n + sa.size();
}
return str;
}
static uim_lisp
get_input_method_name(uim_lisp nth_)
{
int nth = uim_scm_c_int( nth_ );
if ( nth < (int)im_list.size() )
{
// remove space
String imname = WideStr_to_String( im_list.at( nth )->imname );
replace( imname, " ", "" );
const char *orig_name = imname.c_str();
// add "scim" as prefix
char *name = (char *)alloca( strlen(orig_name) + 20 );
if ( name )
{
sprintf(name, "scim-%s", orig_name);
return uim_scm_make_str( name );
}
return uim_scm_f();
}
return uim_scm_f();
}
static String
search_uuid_by_imname( String imname )
{
fprintf(stderr, "search imname = %s\n", imname.c_str() );
std::vector<SCIMInputMethod *>::iterator it = im_list.begin();
for ( ; it != im_list.end(); ++it )
{
fprintf(stderr, "im = %s\n", WideStr_to_CStr( (*it)->imname));
// remove "scim-" prefix
String name = imname.substr( 5, imname.length() - 5 );
if ( WideStr_to_String((*it)->imname) == name )
{
return (*it)->uuid;
}
}
return "";
}
static uim_lisp
alloc_id( uim_lisp name_ )
{
char *imname = uim_scm_c_str(name_);
fprintf(stderr, "scim.cpp : alloc_id = %s\n", imname);
SCIMContext *context = new SCIMContext();
// create factory by specifying the uuid
String uuid = search_uuid_by_imname( imname );
if ( uuid.empty() )
{
uim_notify_fatal( "uim-scim: failed to search uuid" );
return uim_scm_f();
}
context->factory = be->get_factory( uuid );
fprintf(stderr, "imname = %s\n", WideStr_to_CStr( context->factory->get_name() ));
fprintf(stderr, "lang = %s\n", context->factory->get_language().c_str() );
// initialize context's member variable
context->instance = context->factory->create_instance( "UTF-8", instance_count );
if ( context->instance.null() )
{
uim_notify_fatal("uim-scim: failed to create IMEngineInstance");
return uim_scm_f();
}
context->id = instance_count;
context->is_on = false;
// set callbacks for the created instance
context->instance->signal_connect_commit_string( slot( cb_commit) );
context->instance->signal_connect_update_preedit_string( slot( cb_preedit_update) );
context->instance->signal_connect_hide_preedit_string( slot( cb_preedit_hide) );
context->instance->signal_connect_update_preedit_caret( slot( cb_preedit_caret) );
context->instance->signal_connect_update_lookup_table( slot( cb_lookup_update) );
context->instance->signal_connect_show_lookup_table( slot( cb_lookup_show) );
context->instance->signal_connect_hide_lookup_table( slot( cb_lookup_hide) );
/*
context->instance->signal_connect_register_properties( slot( cb_prop_register) );
context->instance->signal_connect_update_property( slot( cb_prop_update) );
*/
context->instance->set_frontend_data( static_cast <void *> (context) );
// and store the context
context_list.push_back( context );
instance_count++;
free(imname);
return uim_scm_make_int( context->id );
}
static uim_lisp
free_id(uim_lisp id_)
{
return uim_scm_f();
}
static SCIMContext *get_context_from_id(int id)
{
std::vector<SCIMContext *>::iterator it = context_list.begin();
for ( ; it != context_list.end(); ++it )
{
if ( id == (*it)->id )
{
return (*it);
}
}
return NULL;
}
static int
ukey_mod_to_skey_mod(int mod)
{
int rv = 0;
if (mod & UMod_Shift) {
rv |= SCIM_KEY_ShiftMask;
}
if (mod & UMod_Control) {
rv |= SCIM_KEY_ControlMask;
}
if (mod & UMod_Alt) {
rv |= SCIM_KEY_AltMask;
}
if (mod & UMod_Super) { /* assuming mod3 */
rv |= SCIM_KEY_Mod3Mask;
}
if (mod & UMod_Hyper) { /* assuming mod4 */
rv |= SCIM_KEY_Mod3Mask;
}
return rv;
}
static uim_lisp
push_key(uim_lisp id_, uim_lisp key_, uim_lisp mod_)
{
fprintf(stderr, "push_key\n");
int id = uim_scm_c_int( id_ );
int code = uim_scm_c_int( key_ );
int mod = uim_scm_c_int( mod_ );
KeyEvent scim_key;
scim_key.code = code;
scim_key.mask = ukey_mod_to_skey_mod(mod);
SCIMContext *ic = get_context_from_id( id );
if ( ic->instance->process_key_event( scim_key ) )
{
return uim_scm_t();
}
return uim_scm_f();
}
static uim_lisp
push_symbol_key(uim_lisp id_, uim_lisp key_, uim_lisp mod_)
{
int id = uim_scm_c_int(id_);
int mod = uim_scm_c_int(mod_);
const char *sym = uim_scm_refer_c_str(key_);
fprintf(stderr, "push_symbol_key = %s\n", sym);
KeyEvent scim_key;
uim_keysymbol_to_scim_keysymbol(sym, &scim_key);
scim_key.mask = ukey_mod_to_skey_mod(mod);
SCIMContext *ic = get_context_from_id( id );
if (ic->instance->process_key_event( scim_key )) {
return uim_scm_t();
}
return uim_scm_f();
}
static uim_lisp
get_nth_candidate( uim_lisp id_, uim_lisp idx_ )
{
int id = uim_scm_c_int( id_ );
int idx = uim_scm_c_int( idx_ );
SCIMContext *ic = get_context_from_id( id );
if ( !ic )
{
return uim_scm_f();
}
return uim_scm_f();
}
static void cb_commit( IMEngineInstanceBase *instance, const WideString &wstr )
{
fprintf(stderr, "cb_commit\n");
SCIMContext *ic = static_cast<SCIMContext *>(instance->get_frontend_data());
if ( !ic )
{
return;
}
ic->preedit_str = WideString();
ic->preedit_attr.clear();
ic->preedit_caret = 0;
uim_eval_im_clear_preedit( ic->id );
uim_eval_im_update_preedit( ic->id );
uim_eval_im_commit( ic->id, WideStr_to_CStr( wstr ) );
}
static void cb_preedit_update( IMEngineInstanceBase *instance, const WideString &wstr, const AttributeList &attr )
{
fprintf(stderr, "cb_preedit_update : preedit_str = [%s]\n", WideStr_to_CStr(wstr));
SCIMContext *ic = static_cast<SCIMContext *>(instance->get_frontend_data());
if ( !ic )
{
return;
}
ic->preedit_str = wstr;
ic->preedit_attr = attr;
uim_eval_im_clear_preedit( ic->id );
uim_eval_im_pushback_preedit( ic->id, 0, WideStr_to_CStr(wstr) );
uim_eval_im_update_preedit( ic->id );
}
static void cb_preedit_hide( IMEngineInstanceBase *instance )
{
fprintf(stderr, "cb_preedit_hide\n");
SCIMContext *ic = static_cast<SCIMContext *>(instance->get_frontend_data());
if ( !ic )
{
return;
}
ic->preedit_str = WideString();
ic->preedit_attr.clear();
uim_eval_im_update_preedit( ic->id );
}
static void cb_preedit_caret( IMEngineInstanceBase *instance, int caret )
{
fprintf(stderr, "cb_preedit_caret\n");
SCIMContext *ic = static_cast<SCIMContext *>(instance->get_frontend_data());
if ( !ic )
{
return;
}
// FIXME
// makes nothing in present state
ic->preedit_caret = caret;
uim_eval_im_update_preedit( ic->id );
}
static void cb_lookup_update( IMEngineInstanceBase *instance, const LookupTable &table )
{
fprintf(stderr, "cb_lookup_update\n");
SCIMContext *ic = static_cast<SCIMContext *>(instance->get_frontend_data());
if ( !ic )
{
return;
}
}
static void cb_lookup_show( IMEngineInstanceBase *instance )
{
fprintf(stderr, "cb_lookup_show\n");
}
static void cb_lookup_hide( IMEngineInstanceBase *instance )
{
fprintf(stderr, "cb_lookup_hide\n");
}
static void
uim_eval_im_commit(int id, const char *str)
{
uim_lisp form = uim_scm_list3(uim_scm_make_symbol("im-commit"),
uim_scm_make_int(id),
uim_scm_make_str(str));
uim_scm_eval(form);
}
static void
uim_eval_im_clear_preedit(int id)
{
uim_lisp form = uim_scm_list2(uim_scm_make_symbol("im-clear-preedit"),
uim_scm_make_int(id));
uim_scm_eval(form);
}
static void
uim_eval_im_pushback_preedit(int id, int flag, const char *str)
{
uim_lisp form;
/*
* FIXME! : 2004-02-12 Kazuki Ohta <mover@hct.zaq.ne.jp>
* This is very adhoc hack!
* Consider multiple attr!
*/
if (flag & UIM_PREEDIT_FLAG_CURSOR) {
form = uim_scm_list4(uim_scm_make_symbol("im-pushback-preedit"),
uim_scm_make_int(id),
uim_scm_make_symbol("preedit-cursor"),
uim_scm_make_str(""));
uim_scm_eval(form);
} else if (flag & UIM_PREEDIT_FLAG_REVERSE) {
form = uim_scm_list4(uim_scm_make_symbol("im-pushback-preedit"),
uim_scm_make_int(id),
uim_scm_make_symbol("preedit-reverse"),
uim_scm_make_str(str));
uim_scm_eval(form);
} else {
form = uim_scm_list4(uim_scm_make_symbol("im-pushback-preedit"),
uim_scm_make_int(id),
uim_scm_make_symbol("preedit-underline"),
uim_scm_make_str(str));
uim_scm_eval(form);
}
}
static void
uim_eval_im_update_preedit(int id)
{
uim_lisp form = uim_scm_list2(uim_scm_make_symbol("im-update-preedit"),
uim_scm_make_int(id));
uim_scm_eval(form);
}
static void uim_eval_im_activate_candidate_selector(int id, int nr_candidates, int display_limit)
{
uim_lisp form = uim_scm_list4(uim_scm_make_symbol("im-activate-candidate-selector"),
uim_scm_make_int(id),
uim_scm_make_int(nr_candidates),
uim_scm_make_int(display_limit));
uim_scm_eval(form);
}
static void uim_eval_im_select_candidate(int id, int index)
{
uim_lisp form = uim_scm_list3(uim_scm_make_symbol("im-select-candidate"),
uim_scm_make_int(id),
uim_scm_make_int(index));
uim_scm_eval(form);
}
static void uim_eval_im_deactivate_candidate_selector(int id)
{
uim_lisp form = uim_scm_list2(uim_scm_make_symbol("im-deactivate-candidate-selector"),
uim_scm_make_int(id));
uim_scm_eval(form);
}
extern "C" void
uim_plugin_instance_init(void)
{
uim_scm_init_subr_0("scim-lib-init", init_scim);
uim_scm_init_subr_0("scim-lib-nr-input-methods", get_nr_input_methods);
uim_scm_init_subr_1("scim-lib-nth-input-method-lang", get_input_method_lang);
uim_scm_init_subr_1("scim-lib-nth-input-method-name", get_input_method_name);
uim_scm_init_subr_1("scim-lib-alloc-context", alloc_id);
uim_scm_init_subr_1("scim-lib-free-context", free_id);
uim_scm_init_subr_3("scim-lib-push-key", push_key);
uim_scm_init_subr_3("scim-lib-push-symbol-key", push_symbol_key);
uim_scm_init_subr_2("sicm-lib-nth-candidate", get_nth_candidate);
}
extern "C" void
uim_plugin_instance_quit(void)
{
}
static void
uim_keysymbol_to_scim_keysymbol( const char *sym, KeyEvent *key )
{
fprintf(stderr, "uim_keysymbol! : sym = %s\n", sym);
static struct keycode_map_ {
char *symbol;
unsigned int keycode;
} keycode_map[] = {
{(char *)"Escape", SCIM_KEY_Escape},
{(char *)"Tab", SCIM_KEY_Tab},
{(char *)"BackSpace", SCIM_KEY_BackSpace},
{(char *)"Delete", SCIM_KEY_Delete},
{(char *)"Return", SCIM_KEY_Return},
{(char *)"Left", SCIM_KEY_Left},
{(char *)"Up", SCIM_KEY_Up},
{(char *)"Right", SCIM_KEY_Right},
{(char *)"Down", SCIM_KEY_Down},
{(char *)"Prior", SCIM_KEY_Prior},
{(char *)"Next", SCIM_KEY_Next},
{(char *)"Home", SCIM_KEY_Home},
{(char *)"End", SCIM_KEY_End},
{NULL, 0}
};
struct keycode_map_ *l;
for ( l = keycode_map; l->symbol; l++ )
{
if ( strcmp(sym, l->symbol) == 0 )
{
fprintf(stderr, "keysymbol = %s\n", l->symbol);
(*key).code = l->keycode;
return;
}
}
}