File: context.c

package info (click to toggle)
uim 1%3A1.8.1-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 38,956 kB
  • sloc: lisp: 356,804; ansic: 77,833; cpp: 27,846; sh: 12,460; makefile: 2,546; asm: 333; ruby: 288
file content (460 lines) | stat: -rw-r--r-- 9,763 bytes parent folder | download
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
/*
  Copyright (c) 2005-2012 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 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.
*/

#include "context.h"

/* current focused context */
uim_agent_context *current;

/* global foscu */
int focused = 0;

uim_agent_context_list *agent_context_list_head = NULL;
uim_agent_context_list *agent_context_list_tail = NULL;

static void
update_context_im(uim_agent_context *ua)
{
  debug_printf(DEBUG_NOTE, "update_context_im\n");

  uim_switch_im(ua->context, ua->im);

  if (ua->im && strcmp(ua->im, uim_get_current_im_name(ua->context)) != 0) {
	debug_printf(DEBUG_ERROR,
				 "update_context_im: failed to switch IM to %s\n", ua->im);
	free(ua->im);
	ua->im = strdup(uim_get_current_im_name(ua->context));
  }
}

static void
update_context_encoding(uim_agent_context *ua)
{

  debug_printf(DEBUG_NOTE, "update_context_encoding\n");

  /* discard current context */
  clear_candidate(ua->cand);
  clear_preedit(ua->pe);
  
  uim_set_client_encoding(ua->context, ua->encoding);
}


/* search context */
uim_agent_context *
get_uim_agent_context(int id)
{
  uim_agent_context_list *ptr;

  debug_printf(DEBUG_NOTE, "get_uim_agent_context (%d)\n", id);
  
  for (ptr = agent_context_list_head; ptr != NULL; ptr = ptr->next) {
	if (ptr->agent_context->context_id == id)
	  return ptr->agent_context;
  }

  return NULL;
}



uim_agent_context *
switch_context_im(uim_agent_context *ua, const char *im)
{
  const char *encoding;

  debug_printf(DEBUG_NOTE, "switch_context_im\n");

  encoding = get_im_encoding(im);

  /* update IM name */
  free(ua->im);

  if (im)
	ua->im = uim_strdup(im);
  else
	ua->im = NULL;

  if (strcmp(ua->encoding, encoding) == 0) {
	/* encodings are same */

	debug_printf(DEBUG_NOTE, "same encoding %s %s\n", ua->im, im);

	update_context_im(ua);

	uim_prop_list_update(ua->context);

  } else {
	/* encodings are different */

	debug_printf(DEBUG_NOTE, 
				 "different encoding %s %s\n", ua->encoding, encoding);

	free(ua->encoding);
	ua->encoding = uim_strdup(encoding);

	update_context_encoding(ua);
	update_context_im(ua);
  }

  uim_prop_list_update(ua->context);

  return ua;

}


void
switch_context_im_all(const char *im)
{
  char *quot_im_name;
  uim_agent_context_list *ptr;

  /* change default IM  */
  update_default_engine(im);

  /* check focus state when change IM of current application */
  quot_im_name = uim_malloc(strlen(im) + 2);
  quot_im_name[0] = '\'';
  quot_im_name[1] = '\0';
  strcat(quot_im_name, im);

  if (agent_context_list_head)
	/* update default IM name in libuim? should be called only one time? */
	uim_prop_update_custom(agent_context_list_head->agent_context->context,
						   "custom-preserved-default-im-name",
						   quot_im_name);

  for (ptr = agent_context_list_head; ptr != NULL; ptr = ptr->next) {
	switch_context_im(ptr->agent_context, im);
  }

  free(quot_im_name);
}



uim_context
create_context(const char *encoding, uim_agent_context *ptr)
{
  uim_context context;

  context = uim_create_context(ptr,
							   encoding,
							   NULL,
							   NULL,  /*default_engine_name,*/
							   uim_iconv,
							   commit_cb);

  uim_set_preedit_cb(context,
					 preedit_clear_cb,
					 preedit_pushback_cb,
					 preedit_update_cb);

  uim_set_candidate_selector_cb(context,
								candidate_activate_cb,
								candidate_select_cb,
								candidate_shift_page_cb,
								candidate_deactivate_cb);

  uim_set_prop_list_update_cb(context,
							  prop_list_update_cb);


  uim_set_configuration_changed_cb(context,
								   configuration_changed_cb);

  uim_set_im_switch_request_cb(context,
							   switch_app_global_im_cb,
							   switch_system_global_im_cb);

  
  return context;

}


uim_agent_context *
create_uim_agent_context(const char *encoding)
{

  uim_agent_context *ret;
  const char *im;

  debug_printf(DEBUG_NOTE, "create_uim_agent_context\n");

  ret = uim_malloc(sizeof(uim_agent_context));

  if (encoding) {
	ret->encoding = uim_strdup(encoding);
  } else {
	if (debug_level > 0)
	  ret->encoding = uim_strdup("EUC-JP");
	else
	  ret->encoding = uim_strdup("UTF-8");
  }

  ret->context = create_context(ret->encoding, ret);

  if ((im = uim_get_default_im_name(setlocale(LC_CTYPE, NULL))))
	ret->im = uim_strdup(im);
  else
	ret->im = NULL;

  ret->pe = create_preedit();
  ret->cand = create_candidate();
  ret->prop = create_prop();

  ret->comstr = (char *)NULL;

  return ret;
}




/* add context to context list */
uim_agent_context *
new_uim_agent_context(int id, const char *encoding)
{
  uim_agent_context_list *ptr;
  
  debug_printf(DEBUG_NOTE, "add_uim_agent_context(%d)\n", id);

  ptr = uim_malloc(sizeof(uim_agent_context_list));

  ptr->agent_context = create_uim_agent_context(encoding);
  ptr->next = NULL;
  ptr->prev = NULL;

  ptr->agent_context->context_id = id;

  if (agent_context_list_tail != NULL) {
	agent_context_list_tail->next = ptr;
	ptr->prev = agent_context_list_tail;
  }

  agent_context_list_tail = ptr;

  if (agent_context_list_head == NULL)
	agent_context_list_head = ptr;

  return ptr->agent_context;
}


/* release context from context list */
int
release_uim_agent_context(int context_id)
{
  uim_agent_context_list *ptr;
  
  for (ptr = agent_context_list_head; ptr != NULL; ptr = ptr->next) {
	if (ptr->agent_context->context_id == context_id) {

	  uim_agent_context *ua = ptr->agent_context;

	  /* clear current */
	  if (current == ua)
		clear_current_uim_agent_context();
	  
	  /* release */
	  uim_release_context(ua->context);

	  /* clear candidate */
	  clear_candidate(ua->cand);
	  free(ua->cand);

	  /* clear preedit */
	  clear_preedit(ua->pe);
	  free(ua->pe);

	  /* free others */
	  free(ua->encoding);
	  free(ua->im);
	  free(ua->prop->list);
	  free(ua->prop);
	  free(ua->comstr);

	  /* rebuild list */
	  if (ptr->next != NULL)
		ptr->next->prev = ptr->prev;
	  else
		agent_context_list_tail = ptr->prev;

	  if (ptr->prev != NULL)
		ptr->prev->next = ptr->next;
	  else
		agent_context_list_head = ptr->next;

	  free(ua);
	  free(ptr);

	  return context_id;
	}
  }

  return -1;
}





int
set_current_uim_agent_context(uim_agent_context *ua)
{
  debug_printf(DEBUG_NOTE, "set_current_context\n");

  if (ua == NULL || ua->context == NULL) {
	debug_printf(DEBUG_ERROR, "set_current_context: invalid context\n");
	return -1;
  }

  helper_send_message("focus_in\n");

  current = ua;
  focused = 1;

  uim_prop_list_update(ua->context);

  return ua->context_id;
}


int
clear_current_uim_agent_context(void)
{

  debug_printf(DEBUG_NOTE, "unfocused\n");

  if (current == NULL || current->context == NULL) return -1;

  /*focused = 0;*/

  helper_send_message("focus_out\n");

  debug_printf(DEBUG_NOTE, " focused %d\n", focused);
  
  return current->context_id;
}



/* handle configuration change */
void
update_context_configuration(uim_agent_context *ua)
{

  /* configuration of context has changed at uim side */
  debug_printf(DEBUG_NOTE, "update_context_configuration\n");
  
  /* update IM name */
  free(ua->im);
  ua->im = uim_strdup(uim_get_current_im_name(ua->context));

  debug_printf(DEBUG_NOTE, "ua->im %s\n", ua->im);

  free(ua->encoding);
  ua->encoding = uim_strdup(get_im_encoding(ua->im));

  debug_printf(DEBUG_NOTE, "ua->encoding %s\n", ua->encoding);

  /* switch IM again */
  update_context_encoding(ua);
}


int
show_commit_string_uim_agent_context(uim_agent_context *ua)
{
  int ret;
  if (ua == NULL) {
	return -1;
  } else {
	ret = show_commit_string(ua->comstr);
	if (ret > 0) {
	  reset_commit_string(ua->comstr);
	  ua->comstr = NULL;
	}
	return ret;
  }
}

int
show_preedit_uim_agent_context(uim_agent_context *ua)
{
  if (ua && ua->cand->valid)
	return show_preedit_force(ua->pe);
  else if (ua == NULL || !ua->pe->valid) 
	return -1;
  else
	return show_preedit(ua->pe);
}


int
show_candidate_uim_agent_context(uim_agent_context *ua)
{
  if (ua == NULL || !ua->cand->valid)
	return -1;
  else if (focused)
	return show_candidate(ua->cand);
  else
	return 0;
}


int
show_prop_uim_agent_context(uim_agent_context *ua)
{
  if (ua == NULL || !ua->prop->valid)
	return -1;
  else 
	return show_prop(ua->prop);
}


int
show_im_uim_agent_context(uim_agent_context *ua)
{
  if (ua == NULL)
	return -1;
  else
	return show_im(ua->im);
}