2
Copyright (c) 2005-2006 uim Project http://uim.freedesktop.org/
6
Redistribution and use in source and binary forms, with or
7
without modification, are permitted provided that the
8
following conditions are met:
10
1. Redistributions of source code must retain the above
11
copyright notice, this list of conditions and the
13
2. Redistributions in binary form must reproduce the above
14
copyright notice, this list of conditions and the
15
following disclaimer in the documentation and/or other
16
materials provided with the distribution.
17
3. Neither the name of authors nor the names of its
18
contributors may be used to endorse or promote products
19
derived from this software without specific prior written
22
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
23
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
24
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
/* current focused context */
40
uim_agent_context *current;
45
uim_agent_context_list *agent_context_list_head = NULL;
46
uim_agent_context_list *agent_context_list_tail = NULL;
49
update_context_im(uim_agent_context *ua)
51
debug_printf(DEBUG_NOTE, "update_context_im\n");
53
uim_switch_im(ua->context, ua->im);
55
if (ua->im && strcmp(ua->im, uim_get_current_im_name(ua->context)) != 0) {
56
debug_printf(DEBUG_ERROR,
57
"update_context_im: failed to switch IM to %s\n", ua->im);
59
ua->im = strdup(uim_get_current_im_name(ua->context));
64
update_context_encoding(uim_agent_context *ua)
67
debug_printf(DEBUG_NOTE, "update_context_encoding\n");
69
/* discard current context */
70
clear_candidate(ua->cand);
71
clear_preedit(ua->pe);
72
uim_release_context(ua->context);
74
ua->context = create_context(ua->encoding, ua);
76
update_context_im(ua);
83
get_uim_agent_context(int id)
85
uim_agent_context_list *ptr;
87
debug_printf(DEBUG_NOTE, "get_uim_agent_context (%d)\n", id);
89
for (ptr = agent_context_list_head; ptr != NULL; ptr = ptr->next) {
90
if (ptr->agent_context->context_id == id)
91
return ptr->agent_context;
100
switch_context_im(uim_agent_context *ua, const char *im)
102
const char *encoding;
104
debug_printf(DEBUG_NOTE, "switch_context_im\n");
106
encoding = get_im_encoding(im);
109
if (ua->im) free(ua->im);
116
if (strcmp(ua->encoding, encoding) == 0) {
117
/* encodings are same */
119
debug_printf(DEBUG_NOTE, "same encoding %s %s\n", ua->im, im);
121
update_context_im(ua);
123
uim_prop_list_update(ua->context);
126
/* encodings are different */
128
debug_printf(DEBUG_NOTE,
129
"different encoding %s %s\n", ua->encoding, encoding);
131
if (ua->encoding) free(ua->encoding);
132
ua->encoding = strdup(encoding);
134
update_context_encoding(ua);
138
uim_prop_list_update(ua->context);
146
switch_context_im_all(const char *im)
149
uim_agent_context_list *ptr;
151
/* change default IM */
152
update_default_engine(im);
154
/* check focus state when change IM of current application */
155
quot_im_name = (char *)malloc(strlen(im) + 2);
156
quot_im_name[0] = '\'';
157
quot_im_name[1] = '\0';
158
strcat(quot_im_name, im);
160
if (agent_context_list_head)
161
/* update default IM name in libuim? should be called only one time? */
162
uim_prop_update_custom(agent_context_list_head->agent_context->context,
163
"custom-preserved-default-im-name",
166
for (ptr = agent_context_list_head; ptr != NULL; ptr = ptr->next) {
167
switch_context_im(ptr->agent_context, im);
176
create_context(const char *encoding, uim_agent_context *ptr)
180
context = uim_create_context(ptr,
183
NULL, /*default_engine_name,*/
187
uim_set_preedit_cb(context,
192
uim_set_candidate_selector_cb(context,
193
candidate_activate_cb,
195
candidate_shift_page_cb,
196
candidate_deactivate_cb);
198
uim_set_prop_list_update_cb(context,
199
prop_list_update_cb);
202
uim_set_configuration_changed_cb(context,
203
configuration_changed_cb);
205
uim_set_im_switch_request_cb(context,
206
switch_app_global_im_cb,
207
switch_system_global_im_cb);
216
create_uim_agent_context(const char *encoding)
219
uim_agent_context *ret;
222
debug_printf(DEBUG_NOTE, "create_uim_agent_context\n");
224
ret = (uim_agent_context *)malloc(sizeof(uim_agent_context));
227
ret->encoding = strdup(encoding);
230
ret->encoding = strdup("EUC-JP");
232
ret->encoding = strdup("UTF-8");
235
ret->context = create_context(ret->encoding, ret);
237
if ((im = uim_get_default_im_name(setlocale(LC_ALL, NULL))))
238
ret->im = strdup(im);
242
ret->pe = create_preedit();
243
ret->cand = create_candidate();
244
ret->prop = create_prop();
246
ret->comstr = (char *)NULL;
254
/* add context to context list */
256
new_uim_agent_context(int id, const char *encoding)
258
uim_agent_context_list *ptr;
260
debug_printf(DEBUG_NOTE, "add_uim_agent_context(%d)\n", id);
262
ptr = (uim_agent_context_list *)malloc(sizeof(uim_agent_context_list));
264
ptr->agent_context = create_uim_agent_context(encoding);
268
ptr->agent_context->context_id = id;
270
if (agent_context_list_tail != NULL) {
271
agent_context_list_tail->next = ptr;
272
ptr->prev = agent_context_list_tail;
275
agent_context_list_tail = ptr;
277
if (agent_context_list_head == NULL)
278
agent_context_list_head = ptr;
280
return ptr->agent_context;
284
/* release context from context list */
286
release_uim_agent_context(int context_id)
288
uim_agent_context_list *ptr;
290
for (ptr = agent_context_list_head; ptr != NULL; ptr = ptr->next) {
291
if (ptr->agent_context->context_id == context_id) {
293
uim_agent_context *ua = ptr->agent_context;
297
clear_current_uim_agent_context();
300
uim_release_context(ua->context);
302
/* clear candidate */
303
clear_candidate(ua->cand);
307
clear_preedit(ua->pe);
317
if (ptr->next != NULL)
318
ptr->next->prev = ptr->prev;
320
agent_context_list_tail = ptr->prev;
322
if (ptr->prev != NULL)
323
ptr->prev->next = ptr->next;
325
agent_context_list_head = ptr->next;
341
set_current_uim_agent_context(uim_agent_context *ua)
343
debug_printf(DEBUG_NOTE, "set_current_context\n");
345
if (ua == NULL || ua->context == NULL) {
346
debug_printf(DEBUG_ERROR, "set_current_context: invalid context\n");
350
helper_send_message("focus_in\n");
355
uim_prop_list_update(ua->context);
357
return ua->context_id;
362
clear_current_uim_agent_context(void)
365
debug_printf(DEBUG_NOTE, "unfocused\n");
367
if (current == NULL || current->context == NULL) return -1;
371
helper_send_message("focus_out\n");
373
debug_printf(DEBUG_NOTE, " focused %d\n", focused);
375
return current->context_id;
380
/* handle configuration change */
382
update_context_configuration(uim_agent_context *ua)
385
/* configuration of context has changed at uim side */
386
debug_printf(DEBUG_NOTE, "update_context_configuration\n");
389
if (ua->im) free(ua->im);
390
ua->im = strdup(uim_get_current_im_name(ua->context));
392
debug_printf(DEBUG_NOTE, "ua->im %s\n", ua->im);
394
if (ua->encoding) free(ua->encoding);
395
ua->encoding = strdup(get_im_encoding(ua->im));
397
debug_printf(DEBUG_NOTE, "ua->encoding %s\n", ua->encoding);
399
/* switch IM again */
400
update_context_encoding(ua);
405
show_commit_string_uim_agent_context(uim_agent_context *ua)
411
ret = show_commit_string(ua->comstr);
413
reset_commit_string(ua->comstr);
421
show_preedit_uim_agent_context(uim_agent_context *ua)
423
if (ua && ua->cand->valid)
424
return show_preedit_force(ua->pe);
425
else if (ua == NULL || !ua->pe->valid)
428
return show_preedit(ua->pe);
433
show_candidate_uim_agent_context(uim_agent_context *ua)
435
if (ua == NULL || !ua->cand->valid)
438
return show_candidate(ua->cand);
445
show_prop_uim_agent_context(uim_agent_context *ua)
447
if (ua == NULL || !ua->prop->valid)
450
return show_prop(ua->prop);
455
show_im_uim_agent_context(uim_agent_context *ua)
460
return show_im(ua->im);