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
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(xim);
37 #ifndef HAVE_XICCALLBACK_CALLBACK
38 #define XICCallback XIMCallback
39 #define XICProc XIMProc
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 /* in order 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;
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);
79 ptr_new = HeapAlloc(GetProcessHeap(), 0,
80 dwCompStringSize + byte_expansion);
84 ERR("Couldn't expand composition string buffer\n");
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 )
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)
114 MultiByteToWideChar(CP_UNIXCP, 0, str, count, wcOutput, dwOutput);
116 if ((focus = GetFocus()))
117 IME_UpdateAssociation(focus);
119 IME_SetResultString(wcOutput, dwOutput);
120 HeapFree(GetProcessHeap(), 0, wcOutput);
123 static BOOL XIMPreEditStateNotifyCallback(XIC xic, XPointer p, XPointer data)
125 const struct x11drv_win_data * const win_data = (struct x11drv_win_data *)p;
126 const XIMPreeditState state = ((XIMPreeditStateNotifyCallbackStruct *)data)->state;
128 TRACE("xic = %p, win = %lx, state = %lu\n", xic, win_data->whole_window, state);
131 case XIMPreeditEnable:
132 IME_SetOpenStatus(TRUE);
134 case XIMPreeditDisable:
135 IME_SetOpenStatus(FALSE);
143 static int XIMPreEditStartCallback(XIC ic, XPointer client_data, XPointer call_data)
145 TRACE("PreEditStartCallback %p\n",ic);
146 IME_SetCompositionStatus(TRUE);
147 ximInComposeMode = TRUE;
151 static void XIMPreEditDoneCallback(XIC ic, XPointer client_data, XPointer call_data)
153 TRACE("PreeditDoneCallback %p\n",ic);
154 ximInComposeMode = FALSE;
155 if (dwCompStringSize)
156 HeapFree(GetProcessHeap(), 0, CompositionString);
157 dwCompStringSize = 0;
158 dwCompStringLength = 0;
159 CompositionString = NULL;
160 IME_SetCompositionStatus(FALSE);
163 static void XIMPreEditDrawCallback(XIM ic, XPointer client_data,
164 XIMPreeditDrawCallbackStruct *P_DR)
166 TRACE("PreEditDrawCallback %p\n",ic);
170 int sel = P_DR->chg_first;
171 int len = P_DR->chg_length;
174 if (! P_DR->text->encoding_is_wchar)
179 TRACE("multibyte\n");
180 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
181 P_DR->text->string.multi_byte, -1,
183 wcOutput = HeapAlloc(GetProcessHeap(), 0, sizeof (WCHAR) * dwOutput);
186 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
187 P_DR->text->string.multi_byte, -1,
192 X11DRV_ImmSetInternalString (sel, len, wcOutput, dwOutput);
193 HeapFree(GetProcessHeap(), 0, wcOutput);
198 FIXME("wchar PROBIBILY WRONG\n");
199 X11DRV_ImmSetInternalString (sel, len,
200 (LPWSTR)P_DR->text->string.wide_char,
205 X11DRV_ImmSetInternalString (sel, len, NULL, 0);
206 IME_SetCursorPos(P_DR->caret);
211 static void XIMPreEditCaretCallback(XIC ic, XPointer client_data,
212 XIMPreeditCaretCallbackStruct *P_C)
214 TRACE("PreeditCaretCallback %p\n",ic);
218 int pos = IME_GetCursorPos();
219 TRACE("pos: %d\n", pos);
220 switch(P_C->direction)
226 case XIMBackwardChar:
227 case XIMBackwardWord:
233 case XIMAbsolutePosition:
241 case XIMPreviousLine:
244 FIXME("Not implemented\n");
247 IME_SetCursorPos(pos);
253 void X11DRV_ForceXIMReset(HWND hwnd)
255 XIC ic = X11DRV_get_ic(hwnd);
259 TRACE("Forcing Reset %p\n",ic);
260 leftover = XmbResetIC(ic);
265 void X11DRV_SetPreeditState(HWND hwnd, BOOL fOpen)
268 XIMPreeditState state;
271 ic = X11DRV_get_ic(hwnd);
276 state = XIMPreeditEnable;
278 state = XIMPreeditDisable;
280 attr = XVaCreateNestedList(0, XNPreeditState, state, NULL);
283 XSetICValues(ic, XNPreeditAttributes, attr, NULL);
289 /***********************************************************************
292 * Process-wide XIM initialization.
294 BOOL X11DRV_InitXIM( const char *input_style )
296 if (!strcasecmp(input_style, "offthespot"))
297 ximStyleRequest = STYLE_OFFTHESPOT;
298 else if (!strcasecmp(input_style, "overthespot"))
299 ximStyleRequest = STYLE_OVERTHESPOT;
300 else if (!strcasecmp(input_style, "root"))
301 ximStyleRequest = STYLE_ROOT;
303 if (!XSupportsLocale())
305 WARN("X does not support locale.\n");
308 if (XSetLocaleModifiers("") == NULL)
310 WARN("Could not set locale modifiers.\n");
317 static void open_xim_callback( Display *display, XPointer ptr, XPointer data );
319 static void X11DRV_DestroyIM(XIM xim, XPointer p, XPointer data)
321 struct x11drv_thread_data *thread_data = x11drv_thread_data();
323 TRACE("xim = %p, p = %p\n", xim, p);
324 thread_data->xim = NULL;
326 XRegisterIMInstantiateCallback( thread_data->display, NULL, NULL, NULL, open_xim_callback, NULL );
329 /***********************************************************************
330 * X11DRV Ime creation
332 * Should always be called with the x11 lock held
334 static BOOL open_xim( Display *display )
336 struct x11drv_thread_data *thread_data = x11drv_thread_data();
337 XIMStyle ximStyleCallback, ximStyleNone;
338 XIMStyles *ximStyles = NULL;
343 xim = XOpenIM(display, NULL, NULL, NULL);
346 WARN("Could not open input method.\n");
350 destroy.client_data = NULL;
351 destroy.callback = X11DRV_DestroyIM;
352 if (XSetIMValues(xim, XNDestroyCallback, &destroy, NULL))
354 WARN("Could not set destroy callback.\n");
357 TRACE("xim = %p\n", xim);
358 TRACE("X display of IM = %p\n", XDisplayOfIM(xim));
359 TRACE("Using %s locale of Input Method\n", XLocaleOfIM(xim));
361 XGetIMValues(xim, XNQueryInputStyle, &ximStyles, NULL);
364 WARN("Could not find supported input style.\n");
370 TRACE("ximStyles->count_styles = %d\n", ximStyles->count_styles);
374 ximStyleCallback = 0;
376 for (i = 0; i < ximStyles->count_styles; ++i)
378 int style = ximStyles->supported_styles[i];
379 TRACE("ximStyles[%d] = %s%s%s%s%s\n", i,
380 (style&XIMPreeditArea)?"XIMPreeditArea ":"",
381 (style&XIMPreeditCallbacks)?"XIMPreeditCallbacks ":"",
382 (style&XIMPreeditPosition)?"XIMPreeditPosition ":"",
383 (style&XIMPreeditNothing)?"XIMPreeditNothing ":"",
384 (style&XIMPreeditNone)?"XIMPreeditNone ":"");
385 if (!ximStyle && (ximStyles->supported_styles[i] ==
388 ximStyle = ximStyleRequest;
389 TRACE("Setting Style: ximStyle = ximStyleRequest\n");
391 else if (!ximStyleRoot &&(ximStyles->supported_styles[i] ==
394 ximStyleRoot = STYLE_ROOT;
395 TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
397 else if (!ximStyleCallback &&(ximStyles->supported_styles[i] ==
400 ximStyleCallback = STYLE_CALLBACK;
401 TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
403 else if (!ximStyleNone && (ximStyles->supported_styles[i] ==
406 TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
407 ximStyleNone = STYLE_NONE;
413 ximStyle = ximStyleRoot;
416 ximStyle = ximStyleNone;
418 if (ximStyleCallback == 0)
420 TRACE("No callback style available\n");
421 ximStyleCallback = ximStyle;
426 thread_data->xim = xim;
428 if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0 ||
429 (ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
433 thread_data->font_set = XCreateFontSet(display, "fixed",
434 &list, &count, NULL);
435 TRACE("ximFontSet = %p\n", thread_data->font_set);
436 TRACE("list = %p, count = %d\n", list, count);
440 for (i = 0; i < count; ++i)
441 TRACE("list[%d] = %s\n", i, list[i]);
442 XFreeStringList(list);
446 thread_data->font_set = NULL;
448 IME_UpdateAssociation(NULL);
452 static void open_xim_callback( Display *display, XPointer ptr, XPointer data )
454 if (open_xim( display ))
455 XUnregisterIMInstantiateCallback( display, NULL, NULL, NULL, open_xim_callback, NULL);
458 void X11DRV_SetupXIM(void)
460 Display *display = thread_display();
462 if (!open_xim( display ))
463 XRegisterIMInstantiateCallback( display, NULL, NULL, NULL, open_xim_callback, NULL );
466 static BOOL X11DRV_DestroyIC(XIC xic, XPointer p, XPointer data)
468 struct x11drv_win_data *win_data = (struct x11drv_win_data *)p;
469 TRACE("xic = %p, win = %lx\n", xic, win_data->whole_window);
470 win_data->xic = NULL;
475 XIC X11DRV_CreateIC(XIM xim, struct x11drv_win_data *data)
478 XVaNestedList preedit = NULL;
479 XVaNestedList status = NULL;
481 XICCallback destroy = {(XPointer)data, (XICProc)X11DRV_DestroyIC};
482 XICCallback P_StateNotifyCB, P_StartCB, P_DoneCB, P_DrawCB, P_CaretCB;
483 LANGID langid = PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale()));
484 Window win = data->whole_window;
485 XFontSet fontSet = x11drv_thread_data()->font_set;
487 TRACE("xim = %p\n", xim);
489 /* use complex and slow XIC initialization method only for CJK */
490 if (langid != LANG_CHINESE &&
491 langid != LANG_JAPANESE &&
492 langid != LANG_KOREAN)
495 XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
498 XNDestroyCallback, &destroy,
504 /* create callbacks */
505 P_StateNotifyCB.client_data = (XPointer)data;
506 P_StartCB.client_data = NULL;
507 P_DoneCB.client_data = NULL;
508 P_DrawCB.client_data = NULL;
509 P_CaretCB.client_data = NULL;
510 P_StateNotifyCB.callback = (XICProc)XIMPreEditStateNotifyCallback;
511 P_StartCB.callback = (XICProc)XIMPreEditStartCallback;
512 P_DoneCB.callback = (XICProc)XIMPreEditDoneCallback;
513 P_DrawCB.callback = (XICProc)XIMPreEditDrawCallback;
514 P_CaretCB.callback = (XICProc)XIMPreEditCaretCallback;
516 if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0)
518 preedit = XVaCreateNestedList(0,
520 XNSpotLocation, &spot,
521 XNPreeditStateNotifyCallback, &P_StateNotifyCB,
522 XNPreeditStartCallback, &P_StartCB,
523 XNPreeditDoneCallback, &P_DoneCB,
524 XNPreeditDrawCallback, &P_DrawCB,
525 XNPreeditCaretCallback, &P_CaretCB,
527 TRACE("preedit = %p\n", preedit);
531 preedit = XVaCreateNestedList(0,
532 XNPreeditStateNotifyCallback, &P_StateNotifyCB,
533 XNPreeditStartCallback, &P_StartCB,
534 XNPreeditDoneCallback, &P_DoneCB,
535 XNPreeditDrawCallback, &P_DrawCB,
536 XNPreeditCaretCallback, &P_CaretCB,
539 TRACE("preedit = %p\n", preedit);
542 if ((ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
544 status = XVaCreateNestedList(0,
547 TRACE("status = %p\n", status);
550 if (preedit != NULL && status != NULL)
553 XNInputStyle, ximStyle,
554 XNPreeditAttributes, preedit,
555 XNStatusAttributes, status,
558 XNDestroyCallback, &destroy,
561 else if (preedit != NULL)
564 XNInputStyle, ximStyle,
565 XNPreeditAttributes, preedit,
568 XNDestroyCallback, &destroy,
571 else if (status != NULL)
574 XNInputStyle, ximStyle,
575 XNStatusAttributes, status,
578 XNDestroyCallback, &destroy,
584 XNInputStyle, ximStyle,
587 XNDestroyCallback, &destroy,
591 TRACE("xic = %p\n", xic);