File: x_im.c

package info (click to toggle)
mlterm 3.1.2-1.3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 23,168 kB
  • sloc: ansic: 102,795; sh: 9,676; java: 2,018; perl: 1,601; makefile: 1,595; cpp: 771; sed: 16
file content (353 lines) | stat: -rw-r--r-- 6,293 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
/*
 *	$Id$
 */
#include  <stdio.h>		/* sprintf */
#include  <kiklib/kik_mem.h>	/* malloc/alloca/free */
#include  <kiklib/kik_str.h>	/* kik_str_alloca_dup kik_str_sep */
#include  <kiklib/kik_locale.h>

#include  "x_im.h"
#include  "x_event_source.h"

#ifdef  USE_IM_PLUGIN

#ifndef  LIBDIR
#define  IM_DIR  "/usr/local/lib/mlterm/"
#else
#define  IM_DIR  LIBDIR "/mlterm/"
#endif

typedef  x_im_t * (*x_im_new_func_t)( u_int64_t  magic ,
				      ml_char_encoding_t  term_encoding ,
				      x_im_export_syms_t *  syms ,
				      char *  engine ,
				      u_int  mod_ignore_mask) ;


/* --- static variables --- */

static  x_im_export_syms_t  im_export_syms =
{
	ml_str_init ,
	ml_str_delete ,
	ml_char_combine ,
	ml_char_set ,
	ml_get_char_encoding_name ,
	ml_get_char_encoding ,
	ml_is_msb_set ,
	ml_isciikey_state_new ,
	ml_isciikey_state_delete ,
	ml_convert_ascii_to_iscii ,
	ml_parser_new ,
	ml_conv_new ,
	x_im_candidate_screen_new ,
	x_im_status_screen_new ,
	x_event_source_add_fd ,
	x_event_source_remove_fd

} ;

#if  1
/* restroing locale which was overwritten by SCIM */
#  define  RESTORE_LOCALE  1
#endif

/* --- static functions --- */

static int
dlsym_im_new_func(
	char *  im_name ,
	x_im_new_func_t *  func ,
	kik_dl_handle_t *  handle
	)
{
	char * libname ;
	char * symname ;

	if( ! im_name)
	{
		return  0 ;
	}

	if( ! ( libname = alloca( strlen( im_name) + 4)))
	{
	#ifdef  DEBUG
		kik_debug_printf( KIK_DEBUG_TAG " alloca() failed.\n") ;
	#endif

		return  0 ;
	}

	sprintf( libname , "im-%s" , im_name) ;

	if( ! ( symname = alloca( strlen( im_name) + 8)))
	{
	#ifdef  DEBUG
		kik_debug_printf( KIK_DEBUG_TAG " alloca() failed.\n") ;
	#endif

		return  0 ;
	}

	sprintf( symname , "im_%s_new" , im_name) ;

	if( ! ( *handle = kik_dl_open( IM_DIR , libname)) &&
	    ! ( *handle = kik_dl_open( "" , libname)))
	{
		return  0 ;
	}

	if( ! ( *func = (x_im_new_func_t) kik_dl_func_symbol( *handle , symname)))
	{
		kik_dl_close( *handle) ;

		return  0 ;
	}

	return  1 ;
}


/* --- global functions --- */

x_im_t *
x_im_new(
	ml_char_encoding_t  term_encoding ,
	x_im_event_listener_t *  im_listener ,
	char *  input_method ,
	u_int  mod_ignore_mask
	)
{
	x_im_t *  im ;
	x_im_new_func_t  func ;
	kik_dl_handle_t  handle ;
	char *  im_name ;
	char *  im_attr ;
#ifdef  RESTORE_LOCALE
	char *  cur_locale ;
#endif

	if( input_method == NULL)
	{
		return  NULL ;
	}

	if( strcmp( input_method , "none") == 0)
	{
		return  NULL ;
	}

	if( strchr( input_method , ':'))
	{
		im_attr = kik_str_alloca_dup( input_method) ;

		if( ( im_name = kik_str_sep( &im_attr , ":")) == NULL)
		{
		#ifdef  DEBUG
			kik_error_printf( "%s is illegal input method.\n" , input_method) ;
		#endif

			return  NULL ;
		}
	}
	else
	{
		im_name = kik_str_alloca_dup( input_method) ;
		im_attr = NULL ;
	}

#ifdef  RESTORE_LOCALE
	cur_locale = kik_str_alloca_dup( kik_get_locale()) ;
#endif

	if ( ! dlsym_im_new_func( im_name , &func , &handle))
	{
	#ifdef  RESTORE_LOCALE
		kik_locale_init( cur_locale) ;
	#endif
		kik_error_printf( "%s: Could not load.\n" , im_name) ;
		return  NULL ;
	}

#ifdef  RESTORE_LOCALE
	kik_locale_init( cur_locale) ;
#endif

	if( ! ( im = (*func)( IM_API_COMPAT_CHECK_MAGIC , term_encoding ,
			      &im_export_syms , im_attr , mod_ignore_mask)))
	{
		kik_error_printf( "%s: Could not open.\n" , im_name) ;

		/*
		 * Even if ibus daemon was not found, ibus_init() has been
		 * already called in im_ibus_new().
		 * So if im-ibus module is unloaded here, ibus_init()
		 * will be called again and segfault will happen when
		 * im-ibus module is loaded next time.
		 */
		if( strcmp( im_name , "ibus") != 0)
		{
			kik_dl_close( handle) ;
		}

		return  NULL ;
	}


	/*
	 * initializations for x_im_t
	 */
	im->handle = handle ;
	im->name = strdup( im_name) ;
	im->listener = im_listener ;
	im->cand_screen = NULL ;
	im->stat_screen = NULL ;
	im->preedit.chars = NULL ;
	im->preedit.num_of_chars = 0 ;
	im->preedit.filled_len = 0 ;
	im->preedit.segment_offset = 0 ;
	im->preedit.cursor_offset = X_IM_PREEDIT_NOCURSOR ;

	return  im ;
}

void
x_im_delete(
	x_im_t *  im
	)
{
	kik_dl_handle_t  handle ;
	int  is_ibus ;

	if( strcmp( im->name , "ibus") == 0)
	{
		is_ibus = 1 ;
	}
	else
	{
		is_ibus = 0 ;
	}

	free( im->name) ;

	if( im->cand_screen)
	{
		(*im->cand_screen->delete)( im->cand_screen) ;
	}

	if( im->stat_screen)
	{
		(*im->stat_screen->delete)( im->stat_screen) ;
	}

	if( im->preedit.chars)
	{
		ml_str_delete( im->preedit.chars , im->preedit.num_of_chars) ;
	}

	handle = im->handle ;

	(*im->delete)( im) ;

	/*
	 * Don't unload libim-ibus.so because it depends on glib which works
	 * unexpectedly.
	 */
	if( ! is_ibus)
	{
		kik_dl_close( handle) ;
	}
}

void
x_im_redraw_preedit(
	x_im_t *  im ,
	int  is_focused
	)
{
	(*im->listener->draw_preedit_str)( im->listener->self ,
					   im->preedit.chars ,
					   im->preedit.filled_len ,
					   im->preedit.cursor_offset) ;

	if( ! im->cand_screen && ! im->stat_screen)
	{
		return ;
	}

	if( is_focused)
	{
		int  x ;
		int  y ;

		if( (*im->listener->get_spot)( im->listener->self ,
					       im->preedit.chars ,
					       im->preedit.segment_offset ,
					       &x , &y))
		{
			if( im->stat_screen &&
			    (im->cand_screen && im->preedit.filled_len))
			{
				(*im->stat_screen->hide)( im->stat_screen) ;
				(*im->cand_screen->show)( im->cand_screen) ;
				(*im->cand_screen->set_spot)( im->cand_screen ,
							      x , y) ;
			}
			else if( im->stat_screen)
			{
				(*im->stat_screen->show)( im->stat_screen) ;
				(*im->stat_screen->set_spot)( im->stat_screen ,
							      x , y) ;
			}
			else if( im->cand_screen && im->preedit.filled_len)
			{
				(*im->cand_screen->show)( im->cand_screen) ;
				(*im->cand_screen->set_spot)( im->cand_screen ,
							      x , y) ;
			}
		}
	}
	else
	{
		if( im->cand_screen)
		{
			(*im->cand_screen->hide)( im->cand_screen) ;
		}

		if( im->stat_screen)
		{
			(*im->stat_screen->hide)( im->stat_screen) ;
		}
	}
}

#else  /* ! USE_IM_PLUGIN */

x_im_t *
x_im_new(
	ml_char_encoding_t  term_encoding ,
	x_im_event_listener_t *  im_listener ,
	char *  input_method ,
	u_int  mod_ignore_mask
	)
{
	return  NULL ;
}

void
x_im_delete(
	x_im_t *  im
	)
{
}

void
x_im_redraw_preedit(
	x_im_t *  im ,
	int  is_focused
	)
{
}

#endif