File: xim.c

package info (click to toggle)
kterm 6.2.0-14
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,212 kB
  • ctags: 2,314
  • sloc: ansic: 23,939; makefile: 981; sh: 17
file content (479 lines) | stat: -rw-r--r-- 10,733 bytes parent folder | download | duplicates (8)
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
/*
 *	$Id: xim.c,v 6.0 1996/07/12 05:01:39 kagotani Rel $
 */

/*
 * Copyright (c) 1996
 * XXI working group in Japan Unix Society (XXI).
 *
 * The X Consortium, and any party obtaining a copy of these files from
 * the X Consortium, directly or indirectly, is granted, free of charge, a
 * full and unrestricted irrevocable, world-wide, paid up, royalty-free,
 * nonexclusive right and license to deal in this software and
 * documentation files (the "Software"), including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons who receive
 * copies from any such party to do so.  This license includes without
 * limitation a license to do the foregoing actions under any patents of
 * the party supplying this software to the X Consortium.
 */ 

#include <stdio.h>
#include <X11/Xos.h>
#include <X11/Xlocale.h>
#include "ptyx.h"
#include "menu.h"
#include "data.h"

extern char ** ParseList();

static void
setColor(screen, fg_p, bg_p)
TScreen *screen;
unsigned long *fg_p;
unsigned long *bg_p;
{
	extern XtermWidget term;
	*fg_p = screen->foreground;
	*bg_p = term->core.background_pixel;
}

void
IMSendColor(screen)
TScreen *screen;
{
	unsigned long	fg, bg;
	XVaNestedList	preedit_attr;

	if (!screen->xic || !(screen->xicstyle & XIMPreeditPosition)) return;

	setColor(screen, &fg, &bg);

	preedit_attr = XVaCreateNestedList(0,
					XNForeground, fg,
					XNBackground, bg,
					NULL);
	XSetICValues(screen->xic, XNPreeditAttributes, preedit_attr, NULL);
	XFree(preedit_attr);
}

static void
setSpot(screen, spot_p)
TScreen *screen;
XPoint *spot_p;
{
	spot_p->x = CursorX(screen, screen->cur_col);
	spot_p->y = CursorY(screen, screen->cur_row) + screen->max_ascent;
}

void
IMSendSpot(screen)
TScreen *screen;
{
	XPoint		spot;
	XVaNestedList	preedit_attr;

	if (!screen->xic || !(screen->xicstyle & XIMPreeditPosition)) return;

	setSpot(screen, &spot);

	preedit_attr = XVaCreateNestedList(0, XNSpotLocation, &spot, NULL);
	XSetICValues(screen->xic, XNPreeditAttributes, preedit_attr, NULL);
	XFree(preedit_attr);
}

static void
setFonts(screen, fontset_p)
TScreen *screen;
XFontSet *fontset_p;
{
	char		**missing_list, *def_string;
	int		missing_count;
	int		i, fnum, len;
	int		fontnum = screen->menu_font_number;
	char		*fn;
	static char	*fontnamelist;
	static int	fontnamelistlen;

	len = 0;
	for (fnum = F_ISO8859_1; fnum < FCNT; fnum++)
		if (fn = screen->_menu_font_names[fnum][fontnum])
			len += strlen(fn) + 1;
	if (fn = screen->menu_font_list[fontnum])
		len += strlen(fn) + 1;
	if (len > fontnamelistlen) {
		if (fontnamelist) XtFree(fontnamelist);
		fontnamelist = XtMalloc(fontnamelistlen = len);
	}
	fontnamelist[0] = '\0';

	for (fnum = F_ISO8859_1; fnum < FCNT; fnum++)
		if (fn = screen->_menu_font_names[fnum][fontnum]) {
			strcat(fontnamelist, fn);
			strcat(fontnamelist, ",");
		}
	if (fn = screen->menu_font_list[fontnum]) {
		strcat(fontnamelist, fn);
	}

	*fontset_p = XCreateFontSet(screen->display, fontnamelist,
				&missing_list, &missing_count, &def_string);
	for(i = 0; i < missing_count; i++)
		fprintf(stderr, "Warning: missing font: %s\n", missing_list[i]);
	XFreeStringList(missing_list);
}

void
IMSendFonts(screen)
TScreen *screen;
{
	XFontSet	fontset;
	XVaNestedList	preedit_attr;

	if (!screen->xic || !(screen->xicstyle & XIMPreeditPosition)) return;

	setFonts(screen, &fontset);

	if (fontset) {
		preedit_attr = XVaCreateNestedList(0, XNFontSet, fontset, NULL);
		XSetICValues(screen->xic, XNPreeditAttributes, preedit_attr, NULL);
		XFree(preedit_attr);
		if (screen->xicfontset)
			XFreeFontSet(screen->display, screen->xicfontset);
		screen->xicfontset = fontset;
	}
}

static void
setSize(screen, rect_p)
TScreen *screen;
XRectangle *rect_p;
{
	rect_p->x = screen->scrollbar;
	rect_p->y = 0;
	rect_p->width  = FullWidth(screen)  - screen->scrollbar;
	rect_p->height = FullHeight(screen);
}

void
IMSendSize(screen)
TScreen *screen;
{
	XRectangle	rect;
	XVaNestedList	preedit_attr;

	if (!screen->xic || !(screen->xicstyle & XIMPreeditPosition)) return;

	setSize(screen, &rect);

	preedit_attr = XVaCreateNestedList(0, XNArea, &rect, NULL);
	XSetICValues(screen->xic, XNPreeditAttributes, preedit_attr, NULL);
	XFree(preedit_attr);
}

void
SetLocale(localelist)
char	*localelist;
{
	char **locales;

	for (locales = ParseList(localelist); *locales; locales++) {
		if (setlocale(LC_CTYPE, *locales) && XSupportsLocale()) return;
	}

	fprintf(stderr, "Couldn't set locale: %s\n", localelist);
}

static XIMStyle
BestStyle(im_styles, style_name)
XIMStyles *im_styles;
char *style_name;
{
	static XIMStyle OverTheSpot_styles[] = {
		XIMPreeditPosition | XIMStatusNothing,
	/*
	 *	*** Where can I display the status ***
	 *	XIMPreeditPosition | XIMStatusArea,
	 */
		0
	};

	static XIMStyle Root_styles[] = {
		XIMPreeditNothing  | XIMStatusNothing,
		XIMPreeditNone     | XIMStatusNone,
		0
	};

	XIMStyle *styles;
	unsigned short i;

	if (!strcmp(style_name, "OverTheSpot")) {
		styles = OverTheSpot_styles;
	} else if (!strcmp(style_name, "Root")) {
		styles = Root_styles;
	} else {
		return 0;
	}

	for (; *styles; styles++)
		for (i = 0; i < im_styles->count_styles; i++)
			if (*styles == im_styles->supported_styles[i])
				return *styles;

	return 0;
}

/* ARGSUSED */
static void
IMDestroyCallback(im, client_data, call_data)
XIM	im;
XPointer	client_data;
XPointer	call_data; /* unused */
{
	extern XtermWidget term;
	TScreen	*screen = &term->screen;
#ifdef KTERM_DEBUG
	printf("IMDestroyCallback()\n");
#endif
	screen->xic = NULL;
}

/* ARGSUSED */
static void
IMInstantiateCallback(display, client_data, call_data)
Display	*display;
XPointer	client_data; /* unused */
XPointer	call_data; /* unused */
{
	extern XtermWidget term;
	TScreen	*screen = &term->screen;
	char **types;
	XIM im = NULL;
	XIMCallback ximcallback;
	XIMStyles *im_styles;
	XIMStyle best_style = 0;
	XPoint spot;
	XRectangle rect;
	XFontSet fontset;
	unsigned long fg, bg;
	XVaNestedList preedit_attr = NULL;
#ifdef KTERM_DEBUG
	printf("IMInstantiateCallback()\n");
#endif
	
	if (screen->xic) return;

	im = XOpenIM(display, NULL, NULL, NULL);

	if (!im) return;
	
	ximcallback.callback = IMDestroyCallback;
	ximcallback.client_data = NULL;
	XSetIMValues(im, XNDestroyCallback, &ximcallback, NULL);

	XGetIMValues(im, XNQueryInputStyle, &im_styles, NULL);
#ifdef KTERM_DEBUG
	{
	    int i;
	    printf("IM supported styles =\n");
	    for (i = 0; i < im_styles->count_styles; i++) {
		printf("    ");
		if(im_styles->supported_styles[i] & XIMPreeditArea)
			printf("XIMPreeditArea");
		else if(im_styles->supported_styles[i] & XIMPreeditCallbacks)
			printf("XIMPreeditCallbacks");
		else if(im_styles->supported_styles[i] & XIMPreeditPosition)
			printf("XIMPreeditPosition");
		else if(im_styles->supported_styles[i] & XIMPreeditNothing)
			printf("XIMPreeditNothing");
		else if(im_styles->supported_styles[i] & XIMPreeditNone)
			printf("XIMPreeditNone");
		printf(" | ");
		if(im_styles->supported_styles[i] & XIMStatusArea)
			printf("XIMStatusArea");
		else if(im_styles->supported_styles[i] & XIMStatusCallbacks)
			printf("XIMStatusCallbacks");
		else if(im_styles->supported_styles[i] & XIMStatusNothing)
			printf("XIMStatusNothing");
		else if(im_styles->supported_styles[i] & XIMStatusNone)
			printf("XIMStatusNone");
		printf("\n");
	    }
	}
#endif
	for (types = ParseList(term->misc.preedit_type); *types; types++) {
		best_style = BestStyle(im_styles, *types);
		if (best_style) break;
	}
	XFree(im_styles);

	if (!best_style) {
		fprintf(stderr, "input method doesn't support my preedit type: %s\n", term->misc.preedit_type);
		XCloseIM(im);
		return;
	}

	if (best_style & XIMPreeditPosition) {
		setSize(screen, &rect);
		setSpot(screen, &spot);
		setFonts(screen, &fontset);
		setColor(screen, &fg, &bg);

		preedit_attr = XVaCreateNestedList(0,
					XNArea, &rect,
					XNSpotLocation, &spot,
					XNFontSet, fontset,
					XNForeground, fg,
					XNBackground, bg,
					XNLineSpace, FontHeight(screen),
					NULL);
	}

	screen->xic = XCreateIC(im,
			XNInputStyle, best_style,
			XNClientWindow, XtWindow(term),
			XNFocusWindow, XtWindow(term),
			preedit_attr ? XNPreeditAttributes : NULL, preedit_attr,
			NULL);

	XFree(preedit_attr);

	if (!screen->xic) {
		fprintf(stderr, "Couldn't create input context.\n");
		XCloseIM(im);
	}
	screen->xicstyle = best_style;
	if (preedit_attr) screen->xicfontset = fontset;
}

void
CloseIM(screen)
TScreen	*screen;
{
	extern XtermWidget term; /* for update_openim() */
	char *p;
	char **mp;

	if (!screen->imregistered) return;

#ifdef KTERM_DEBUG
	{
	    Bool ret;
	    ret =
#endif
		XUnregisterIMInstantiateCallback(screen->display,
			NULL, NULL, NULL, IMInstantiateCallback, NULL);
#ifdef KTERM_DEBUG
	    if (ret)
		printf("Unregister succeeded.\n");
	    else
		printf("Unregister failed.\n");
	}
#endif
	screen->imregistered = False;

	if (screen->xic) {
		XCloseIM(XIMOfIC(screen->xic));
		screen->xic = NULL;
	}

	update_openim();
}

/* ARGSUSED */
void
HandleCloseIM(gw, event, params, nparams)
Widget	gw;
XEvent	*event;
String	*params;
Cardinal	*nparams;
{
	XtermWidget w = (XtermWidget) gw;
	TScreen	*screen = &w->screen;
	CloseIM(screen);
}

static char *
methodtomodifier(method)
char	*method;
{
	char *mod;

	if (!strcmp(method, "XMODIFIERS")) {
		mod = XtMalloc(1);
		*mod = '\0'; /* "" */
	} else {
		mod = XtMalloc(strlen(method) + 5);
		strcpy(mod, "@im=");
		strcat(mod, method);
	}

	return mod;
}

void
SetLocaleModifiers(params, nparams)
String	*params;		/* array of comma-separated strings */
Cardinal	nparams;	/* number of the strings */
{
	char *p;
	char *mod;

	if (nparams) {
		mod = methodtomodifier(params[0]);	/* "@im=..." */
	} else {
		mod = methodtomodifier("XMODIFIERS");	/* "" */
	}

	p = XSetLocaleModifiers(mod);
#ifdef KTERM_DEBUG
	printf("XSetLocaleModifiers(\"%s\") = \"%s\"\n", mod, p);
#endif

	XtFree(mod);

	if (!p) {
		fprintf(stderr, "Couldn't set locale modifiers: %s\n", mod);
		return;
	}
}

void
OpenIM(screen)
TScreen	*screen;
{
	extern XtermWidget term; /* for update_openim() */
#ifdef KTERM_KINPUT2
	extern Boolean Kinput2UnderConversion();

	if (Kinput2UnderConversion()) {
		Bell(XkbBI_MinorError,0);
		return;
	}
#endif /* KTERM_KINPUT2 */

	XRegisterIMInstantiateCallback(screen->display,
		NULL, NULL, NULL, IMInstantiateCallback, NULL);
	screen->imregistered = True;

	update_openim();
}

/* ARGSUSED */
void
HandleOpenIM(gw, event, params, nparams)
Widget	gw;
XEvent	*event;	/* unused */
String	*params;
Cardinal	*nparams;
{
	TScreen	*screen = &((XtermWidget)gw)->screen;

	CloseIM(screen);

	if (*nparams) {
		SetLocaleModifiers(params, *nparams);
	}

	OpenIM(screen);
}