push 87b6981010d7405c33b14cddcceec21b47729eba
[wine/hacks.git] / dlls / winex11.drv / xim.c
blob38b0a2cbc679b9140088a017ab1c9f1e67af8a88
1 /*
2 * Functions for further XIM control
4 * Copyright 2003 CodeWeavers, Aric Stewart
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "wingdi.h"
30 #include "winnls.h"
31 #include "x11drv.h"
32 #include "imm.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
37 #ifndef HAVE_XICCALLBACK_CALLBACK
38 #define XICCallback XIMCallback
39 #define XICProc XIMProc
40 #endif
42 BOOL ximInComposeMode=FALSE;
44 /* moved here from imm32 for dll separation */
45 static DWORD dwCompStringLength = 0;
46 static LPBYTE CompositionString = NULL;
47 static DWORD dwCompStringSize = 0;
49 #define STYLE_OFFTHESPOT (XIMPreeditArea | XIMStatusArea)
50 #define STYLE_OVERTHESPOT (XIMPreeditPosition | XIMStatusNothing)
51 #define STYLE_ROOT (XIMPreeditNothing | XIMStatusNothing)
52 /* this uses all the callbacks to utilize full IME support */
53 #define STYLE_CALLBACK (XIMPreeditCallbacks | XIMStatusNothing)
54 /* inorder to enable deadkey support */
55 #define STYLE_NONE (XIMPreeditNothing | XIMStatusNothing)
57 static XIMStyle ximStyle = 0;
58 static XIMStyle ximStyleRoot = 0;
59 static XIMStyle ximStyleRequest = STYLE_CALLBACK;
61 static void X11DRV_ImmSetInternalString(DWORD dwOffset,
62 DWORD selLength, LPWSTR lpComp, DWORD dwCompLen)
64 /* Composition strings are edited in chunks */
65 unsigned int byte_length = dwCompLen * sizeof(WCHAR);
66 unsigned int byte_offset = dwOffset * sizeof(WCHAR);
67 unsigned int byte_selection = selLength * sizeof(WCHAR);
68 int byte_expansion = byte_length - byte_selection;
69 LPBYTE ptr_new;
71 TRACE("( %i, %i, %p, %d):\n", dwOffset, selLength, lpComp, dwCompLen );
73 if (byte_expansion + dwCompStringLength >= dwCompStringSize)
75 if (CompositionString)
76 ptr_new = HeapReAlloc(GetProcessHeap(), 0, CompositionString,
77 dwCompStringSize + byte_expansion);
78 else
79 ptr_new = HeapAlloc(GetProcessHeap(), 0,
80 dwCompStringSize + byte_expansion);
82 if (ptr_new == NULL)
84 ERR("Couldn't expand composition string buffer\n");
85 return;
88 CompositionString = ptr_new;
89 dwCompStringSize += byte_expansion;
92 ptr_new = CompositionString + byte_offset;
93 memmove(ptr_new + byte_length, ptr_new + byte_selection,
94 dwCompStringLength - byte_offset - byte_selection);
95 memcpy(ptr_new, lpComp, byte_length);
96 dwCompStringLength += byte_expansion;
98 IME_SetCompositionString(SCS_SETSTR, CompositionString,
99 dwCompStringLength, NULL, 0);
102 void X11DRV_XIMLookupChars( const char *str, DWORD count )
104 DWORD dwOutput;
105 WCHAR *wcOutput;
106 HWND focus;
108 TRACE("%p %u\n", str, count);
110 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0, str, count, NULL, 0);
111 wcOutput = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * dwOutput);
112 if (wcOutput == NULL)
113 return;
114 MultiByteToWideChar(CP_UNIXCP, 0, str, count, wcOutput, dwOutput);
116 if ((focus = GetFocus()))
117 IME_UpdateAssociation(focus);
119 IME_SetCompositionString(SCS_SETSTR, wcOutput,
120 sizeof (WCHAR) * dwOutput, NULL, 0);
121 IME_NotifyIME(NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
122 HeapFree(GetProcessHeap(), 0, wcOutput);
125 static int XIMPreEditStartCallback(XIC ic, XPointer client_data, XPointer call_data)
127 TRACE("PreEditStartCallback %p\n",ic);
128 IME_SetOpenStatus(TRUE);
129 ximInComposeMode = TRUE;
130 return -1;
133 static void XIMPreEditDoneCallback(XIC ic, XPointer client_data, XPointer call_data)
135 TRACE("PreeditDoneCallback %p\n",ic);
136 ximInComposeMode = FALSE;
137 if (dwCompStringSize)
138 HeapFree(GetProcessHeap(), 0, CompositionString);
139 dwCompStringSize = 0;
140 dwCompStringLength = 0;
141 CompositionString = NULL;
142 IME_SetOpenStatus(FALSE);
145 static void XIMPreEditDrawCallback(XIM ic, XPointer client_data,
146 XIMPreeditDrawCallbackStruct *P_DR)
148 TRACE("PreEditDrawCallback %p\n",ic);
150 if (P_DR)
152 int sel = P_DR->chg_first;
153 int len = P_DR->chg_length;
154 if (P_DR->text)
156 if (! P_DR->text->encoding_is_wchar)
158 DWORD dwOutput;
159 WCHAR *wcOutput;
161 TRACE("multibyte\n");
162 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
163 P_DR->text->string.multi_byte, -1,
164 NULL, 0);
165 wcOutput = HeapAlloc(GetProcessHeap(), 0, sizeof (WCHAR) * dwOutput);
166 if (wcOutput)
168 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
169 P_DR->text->string.multi_byte, -1,
170 wcOutput, dwOutput);
172 /* ignore null */
173 dwOutput --;
174 X11DRV_ImmSetInternalString (sel, len, wcOutput, dwOutput);
175 HeapFree(GetProcessHeap(), 0, wcOutput);
178 else
180 FIXME("wchar PROBIBILY WRONG\n");
181 X11DRV_ImmSetInternalString (sel, len,
182 (LPWSTR)P_DR->text->string.wide_char,
183 P_DR->text->length);
186 else
187 X11DRV_ImmSetInternalString (sel, len, NULL, 0);
188 IME_SetCursorPos(P_DR->caret);
190 TRACE("Finished\n");
193 static void XIMPreEditCaretCallback(XIC ic, XPointer client_data,
194 XIMPreeditCaretCallbackStruct *P_C)
196 TRACE("PreeditCaretCallback %p\n",ic);
198 if (P_C)
200 int pos = IME_GetCursorPos();
201 TRACE("pos: %d\n", pos);
202 switch(P_C->direction)
204 case XIMForwardChar:
205 case XIMForwardWord:
206 pos++;
207 break;
208 case XIMBackwardChar:
209 case XIMBackwardWord:
210 pos--;
211 break;
212 case XIMLineStart:
213 pos = 0;
214 break;
215 case XIMAbsolutePosition:
216 pos = P_C->position;
217 break;
218 case XIMDontChange:
219 P_C->position = pos;
220 return;
221 case XIMCaretUp:
222 case XIMCaretDown:
223 case XIMPreviousLine:
224 case XIMNextLine:
225 case XIMLineEnd:
226 FIXME("Not implemented\n");
227 break;
229 IME_SetCursorPos(pos);
230 P_C->position = pos;
232 TRACE("Finished\n");
235 void X11DRV_ForceXIMReset(HWND hwnd)
237 XIC ic = X11DRV_get_ic(hwnd);
238 if (ic)
240 char* leftover;
241 TRACE("Forcing Reset %p\n",ic);
242 wine_tsx11_lock();
243 leftover = XmbResetIC(ic);
244 XFree(leftover);
245 wine_tsx11_unlock();
249 /***********************************************************************
250 * X11DRV_InitXIM
252 * Process-wide XIM initialization.
254 BOOL X11DRV_InitXIM( const char *input_style )
256 BOOL ret;
258 if (!strcasecmp(input_style, "offthespot"))
259 ximStyleRequest = STYLE_OFFTHESPOT;
260 else if (!strcasecmp(input_style, "overthespot"))
261 ximStyleRequest = STYLE_OVERTHESPOT;
262 else if (!strcasecmp(input_style, "root"))
263 ximStyleRequest = STYLE_ROOT;
265 wine_tsx11_lock();
266 if (!(ret = XSupportsLocale()))
268 WARN("X does not support locale.\n");
270 else if (XSetLocaleModifiers("") == NULL)
272 WARN("Could not set locale modifiers.\n");
273 ret = FALSE;
275 wine_tsx11_unlock();
276 return ret;
280 static void open_xim_callback( Display *display, XPointer ptr, XPointer data );
282 static void X11DRV_DestroyIM(XIM xim, XPointer p, XPointer data)
284 struct x11drv_thread_data *thread_data = x11drv_thread_data();
286 TRACE("xim = %p, p = %p\n", xim, p);
287 thread_data->xim = NULL;
288 ximStyle = 0;
289 wine_tsx11_lock();
290 XRegisterIMInstantiateCallback( thread_data->display, NULL, NULL, NULL, open_xim_callback, NULL );
291 wine_tsx11_unlock();
294 /***********************************************************************
295 * X11DRV Ime creation
297 * Should always be called with the x11 lock held
299 static BOOL open_xim( Display *display )
301 struct x11drv_thread_data *thread_data = x11drv_thread_data();
302 XIMStyle ximStyleCallback, ximStyleNone;
303 XIMStyles *ximStyles = NULL;
304 INT i;
305 XIM xim;
306 XIMCallback destroy;
308 xim = XOpenIM(display, NULL, NULL, NULL);
309 if (xim == NULL)
311 WARN("Could not open input method.\n");
312 return FALSE;
315 destroy.client_data = NULL;
316 destroy.callback = X11DRV_DestroyIM;
317 if (XSetIMValues(xim, XNDestroyCallback, &destroy, NULL))
319 WARN("Could not set destroy callback.\n");
322 TRACE("xim = %p\n", xim);
323 TRACE("X display of IM = %p\n", XDisplayOfIM(xim));
324 TRACE("Using %s locale of Input Method\n", XLocaleOfIM(xim));
326 XGetIMValues(xim, XNQueryInputStyle, &ximStyles, NULL);
327 if (ximStyles == 0)
329 WARN("Could not find supported input style.\n");
330 XCloseIM(xim);
331 return FALSE;
333 else
335 TRACE("ximStyles->count_styles = %d\n", ximStyles->count_styles);
337 ximStyleRoot = 0;
338 ximStyleNone = 0;
339 ximStyleCallback = 0;
341 for (i = 0; i < ximStyles->count_styles; ++i)
343 int style = ximStyles->supported_styles[i];
344 TRACE("ximStyles[%d] = %s%s%s%s%s\n", i,
345 (style&XIMPreeditArea)?"XIMPreeditArea ":"",
346 (style&XIMPreeditCallbacks)?"XIMPreeditCallbacks ":"",
347 (style&XIMPreeditPosition)?"XIMPreeditPosition ":"",
348 (style&XIMPreeditNothing)?"XIMPreeditNothing ":"",
349 (style&XIMPreeditNone)?"XIMPreeditNone ":"");
350 if (!ximStyle && (ximStyles->supported_styles[i] ==
351 ximStyleRequest))
353 ximStyle = ximStyleRequest;
354 TRACE("Setting Style: ximStyle = ximStyleRequest\n");
356 else if (!ximStyleRoot &&(ximStyles->supported_styles[i] ==
357 STYLE_ROOT))
359 ximStyleRoot = STYLE_ROOT;
360 TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
362 else if (!ximStyleCallback &&(ximStyles->supported_styles[i] ==
363 STYLE_CALLBACK))
365 ximStyleCallback = STYLE_CALLBACK;
366 TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
368 else if (!ximStyleNone && (ximStyles->supported_styles[i] ==
369 STYLE_NONE))
371 TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
372 ximStyleNone = STYLE_NONE;
375 XFree(ximStyles);
377 if (ximStyle == 0)
378 ximStyle = ximStyleRoot;
380 if (ximStyle == 0)
381 ximStyle = ximStyleNone;
383 if (ximStyleCallback == 0)
385 TRACE("No callback style avalable\n");
386 ximStyleCallback = ximStyle;
391 thread_data->xim = xim;
393 if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0 ||
394 (ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
396 char **list;
397 int count;
398 thread_data->font_set = XCreateFontSet(display, "fixed",
399 &list, &count, NULL);
400 TRACE("ximFontSet = %p\n", thread_data->font_set);
401 TRACE("list = %p, count = %d\n", list, count);
402 if (list != NULL)
404 int i;
405 for (i = 0; i < count; ++i)
406 TRACE("list[%d] = %s\n", i, list[i]);
407 XFreeStringList(list);
410 else
411 thread_data->font_set = NULL;
413 wine_tsx11_unlock();
414 IME_UpdateAssociation(NULL);
415 wine_tsx11_lock();
416 return TRUE;
419 static void open_xim_callback( Display *display, XPointer ptr, XPointer data )
421 if (open_xim( display ))
422 XUnregisterIMInstantiateCallback( display, NULL, NULL, NULL, open_xim_callback, NULL);
425 void X11DRV_SetupXIM(void)
427 Display *display = thread_display();
429 wine_tsx11_lock();
430 if (!open_xim( display ))
431 XRegisterIMInstantiateCallback( display, NULL, NULL, NULL, open_xim_callback, NULL );
432 wine_tsx11_unlock();
435 static BOOL X11DRV_DestroyIC(XIC xic, XPointer p, XPointer data)
437 struct x11drv_win_data *win_data = (struct x11drv_win_data *)p;
438 TRACE("xic = %p, win = %lx\n", xic, win_data->whole_window);
439 win_data->xic = NULL;
440 return TRUE;
444 XIC X11DRV_CreateIC(XIM xim, struct x11drv_win_data *data)
446 XPoint spot = {0};
447 XVaNestedList preedit = NULL;
448 XVaNestedList status = NULL;
449 XIC xic;
450 XICCallback destroy = {(XPointer)data, (XICProc)X11DRV_DestroyIC};
451 XICCallback P_StartCB, P_DoneCB, P_DrawCB, P_CaretCB;
452 LANGID langid = PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale()));
453 Window win = data->whole_window;
454 XFontSet fontSet = x11drv_thread_data()->font_set;
456 TRACE("xim = %p\n", xim);
458 wine_tsx11_lock();
460 /* use complex and slow XIC initialization method only for CJK */
461 if (langid != LANG_CHINESE &&
462 langid != LANG_JAPANESE &&
463 langid != LANG_KOREAN)
465 xic = XCreateIC(xim,
466 XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
467 XNClientWindow, win,
468 XNFocusWindow, win,
469 XNDestroyCallback, &destroy,
470 NULL);
471 wine_tsx11_unlock();
472 data->xic = xic;
473 return xic;
476 /* create callbacks */
477 P_StartCB.client_data = NULL;
478 P_DoneCB.client_data = NULL;
479 P_DrawCB.client_data = NULL;
480 P_CaretCB.client_data = NULL;
481 P_StartCB.callback = (XICProc)XIMPreEditStartCallback;
482 P_DoneCB.callback = (XICProc)XIMPreEditDoneCallback;
483 P_DrawCB.callback = (XICProc)XIMPreEditDrawCallback;
484 P_CaretCB.callback = (XICProc)XIMPreEditCaretCallback;
486 if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0)
488 preedit = XVaCreateNestedList(0,
489 XNFontSet, fontSet,
490 XNSpotLocation, &spot,
491 XNPreeditStartCallback, &P_StartCB,
492 XNPreeditDoneCallback, &P_DoneCB,
493 XNPreeditDrawCallback, &P_DrawCB,
494 XNPreeditCaretCallback, &P_CaretCB,
495 NULL);
496 TRACE("preedit = %p\n", preedit);
498 else
500 preedit = XVaCreateNestedList(0,
501 XNPreeditStartCallback, &P_StartCB,
502 XNPreeditDoneCallback, &P_DoneCB,
503 XNPreeditDrawCallback, &P_DrawCB,
504 XNPreeditCaretCallback, &P_CaretCB,
505 NULL);
507 TRACE("preedit = %p\n", preedit);
510 if ((ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
512 status = XVaCreateNestedList(0,
513 XNFontSet, fontSet,
514 NULL);
515 TRACE("status = %p\n", status);
518 if (preedit != NULL && status != NULL)
520 xic = XCreateIC(xim,
521 XNInputStyle, ximStyle,
522 XNPreeditAttributes, preedit,
523 XNStatusAttributes, status,
524 XNClientWindow, win,
525 XNFocusWindow, win,
526 XNDestroyCallback, &destroy,
527 NULL);
529 else if (preedit != NULL)
531 xic = XCreateIC(xim,
532 XNInputStyle, ximStyle,
533 XNPreeditAttributes, preedit,
534 XNClientWindow, win,
535 XNFocusWindow, win,
536 XNDestroyCallback, &destroy,
537 NULL);
539 else if (status != NULL)
541 xic = XCreateIC(xim,
542 XNInputStyle, ximStyle,
543 XNStatusAttributes, status,
544 XNClientWindow, win,
545 XNFocusWindow, win,
546 XNDestroyCallback, &destroy,
547 NULL);
549 else
551 xic = XCreateIC(xim,
552 XNInputStyle, ximStyle,
553 XNClientWindow, win,
554 XNFocusWindow, win,
555 XNDestroyCallback, &destroy,
556 NULL);
559 TRACE("xic = %p\n", xic);
560 data->xic = xic;
562 if (preedit != NULL)
563 XFree(preedit);
564 if (status != NULL)
565 XFree(status);
567 wine_tsx11_unlock();
569 return xic;