File: winputmethod.c

package info (click to toggle)
wmaker 0.96.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,416 kB
  • sloc: ansic: 99,483; sh: 7,068; perl: 3,423; makefile: 1,647; lisp: 219; python: 34
file content (244 lines) | stat: -rw-r--r-- 6,125 bytes parent folder | download | duplicates (2)
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

#include <X11/Xlib.h>

#include "wconfig.h"

#include "WINGsP.h"

typedef struct W_IMContext {
	XIM xim;
	XIMStyle ximstyle;
} WMIMContext;

static void instantiateIM_cb(Display * display, XPointer client_data, XPointer call_data)
{
	/* Parameter not used, but tell the compiler that it is ok */
	(void) display;
	(void) call_data;

	W_InitIM((W_Screen *) client_data);
}

static void destroyIM_cb(XIM xim, XPointer client_data, XPointer call_data)
{
	W_Screen *scr = (W_Screen *) client_data;
	W_View *target;

	/* Parameter not used, but tell the compiler that it is ok */
	(void) call_data;

	if (scr->imctx->xim != xim)
		return;

	target = scr->rootView->childrenList;
	while (target != NULL) {
		W_DestroyIC(target);
		target = target->nextSister;
	}

	wfree(scr->imctx);
	scr->imctx = NULL;

	XRegisterIMInstantiateCallback(scr->display, NULL, NULL, NULL, instantiateIM_cb, (XPointer) scr);
}

void W_InitIM(W_Screen * scr)
{
	XIM xim;

	if (scr->imctx)
		return;

	xim = XOpenIM(scr->display, NULL, NULL, NULL);

	if (xim) {
		XIMStyles *im_styles;
		XIMCallback cb;
		int i;

		scr->imctx = wmalloc(sizeof(WMIMContext));
		scr->imctx->xim = xim;

		cb.callback = destroyIM_cb;
		cb.client_data = (XPointer) scr;
		if (XSetIMValues(scr->imctx->xim, XNDestroyCallback, &cb, NULL))
			wwarning(_("could not add destroy callback for XIM input method"));
		XUnregisterIMInstantiateCallback(scr->display, NULL, NULL, NULL, instantiateIM_cb, (XPointer) scr);
		/* Get available input style */
		XGetIMValues(scr->imctx->xim, XNQueryInputStyle, &im_styles, NULL);

		scr->imctx->ximstyle = 0;

		for (i = 0; i < im_styles->count_styles && scr->imctx->ximstyle == 0; i++) {
			if ((im_styles->supported_styles[i] & XIMPreeditPosition) &&
			    (im_styles->supported_styles[i] & XIMStatusNothing)) {
				scr->imctx->ximstyle = XIMPreeditPosition | XIMStatusNothing;
			} else if ((im_styles->supported_styles[i] & XIMPreeditNothing) &&
				   (im_styles->supported_styles[i] & XIMStatusNothing)) {
				scr->imctx->ximstyle = XIMPreeditNothing | XIMStatusNothing;
			}
		}
		XFree(im_styles);
	} else {
		XRegisterIMInstantiateCallback(scr->display, NULL, NULL, NULL, instantiateIM_cb, (XPointer) scr);
	}
}

void W_CreateIC(WMView * view)
{
	WMScreen *scr = W_VIEW_SCREEN(view);
	XVaNestedList preedit_attr = NULL;

	if (view->xic || !view->flags.realized || !scr->imctx)
		return;

	if (scr->imctx->ximstyle & XIMPreeditPosition) {
		XPoint spot;
		XRectangle rect;
		int ofs;

		ofs = (view->size.height - WMFontHeight(scr->normalFont)) / 2;

		rect.x = ofs;
		rect.y = ofs;
		rect.height = WMFontHeight(scr->normalFont);
		rect.width = view->size.width - ofs * 2;
		spot.x = rect.x;
		spot.y = rect.y + rect.height;

		// this really needs to be changed, but I don't know how yet -Dan
		// it used to be like this with fontsets, but no longer applies to xft
		preedit_attr = XVaCreateNestedList(0, XNSpotLocation, &spot,
						   XNArea, &rect, XNFontInfo, scr->normalFont->font, NULL);
	}

	view->xic = XCreateIC(scr->imctx->xim, XNInputStyle, scr->imctx->ximstyle,
			      XNClientWindow, view->window,
			      preedit_attr ? XNPreeditAttributes : NULL, preedit_attr, NULL);

	if (preedit_attr)
		XFree(preedit_attr);

	if (view->xic) {
		unsigned long fevent = 0;
		XGetICValues(view->xic, XNFilterEvents, &fevent, NULL);
		XSelectInput(scr->display, view->window,
			     ButtonPressMask | ButtonReleaseMask | ExposureMask |
			     KeyPressMask | FocusChangeMask | ButtonMotionMask | fevent);
	}
}

void W_DestroyIC(WMView * view)
{
	if (view->xic) {
		XDestroyIC(view->xic);
		view->xic = 0;
	}
}

static void setPreeditArea(W_View * view)
{
	WMScreen *scr = W_VIEW_SCREEN(view);
	XVaNestedList preedit_attr = NULL;

	if (view->xic && (scr->imctx->ximstyle & XIMPreeditPosition)) {
		XRectangle rect;
		int ofs;

		ofs = (view->size.height - WMFontHeight(scr->normalFont)) / 2;
		rect.x = ofs;
		rect.y = ofs;
		rect.height = WMFontHeight(scr->normalFont);
		rect.width = view->size.width - ofs * 2;

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

		if (preedit_attr) {
			XFree(preedit_attr);
		}
	}
}

void W_FocusIC(WMView * view)
{
	WMScreen *scr = W_VIEW_SCREEN(view);

	if (view->xic) {
		XSetICFocus(view->xic);
		XSetICValues(view->xic, XNFocusWindow, view->window, NULL);

		if (scr->imctx->ximstyle & XIMPreeditPosition) {
			setPreeditArea(view);
		}
	}
}

void W_UnFocusIC(WMView * view)
{
	if (view->xic) {
		XUnsetICFocus(view->xic);
	}
}

void W_SetPreeditPositon(W_View * view, int x, int y)
{
	WMScreen *scr = W_VIEW_SCREEN(view);
	XVaNestedList preedit_attr = NULL;

	if (view->xic && (scr->imctx->ximstyle & XIMPreeditPosition)) {
		XPoint spot;
		int ofs;

		ofs = (view->size.height - WMFontHeight(scr->normalFont)) / 2;
		spot.x = x;
		spot.y = y + view->size.height - ofs - 3;
		preedit_attr = XVaCreateNestedList(0, XNSpotLocation, &spot, NULL);
		XSetICValues(view->xic, XNPreeditAttributes, preedit_attr, NULL);
		if (preedit_attr) {
			XFree(preedit_attr);
		}
	}
}

int W_LookupString(W_View *view, XKeyPressedEvent *event, char *buffer, int buflen, KeySym *keysym, Status *status)
{
	WMScreen *scr = W_VIEW_SCREEN(view);

	XSetInputFocus(scr->display, view->window, RevertToParent, CurrentTime);

#ifdef X_HAVE_UTF8_STRING
	if (view->xic)
		return Xutf8LookupString(view->xic, event, buffer, buflen, keysym, status);
#endif
	return XLookupString(event, buffer, buflen, keysym, (XComposeStatus *) status);
}

/*
 * Map a keycode to the corresponding keysym
 * To replace the deprecated X11 function XKeycodeToKeysym
 */
KeySym W_KeycodeToKeysym(Display *display, KeyCode keycode, int index)
{
	static int min_kc = -1;
	static int max_kc;
	int num_syms;
	KeySym *key_syms;
	KeySym ks;

	XDisplayKeycodes(display, &min_kc, &max_kc);
	if (keycode < min_kc || keycode > max_kc || index < 0) {
		return NoSymbol;
	}

	key_syms = XGetKeyboardMapping(display, keycode, 1, &num_syms);
	if (index >= num_syms) {
		XFree(key_syms);
		return NoSymbol;
	}

	ks = key_syms[index];
	XFree(key_syms);

	return ks;
}