push 6fe5edf8439c19d3885814583531c2f2b1495177
[wine/hacks.git] / dlls / winex11.drv / xim.c
blob265f9c806084ed29d7220d9cc3a828984b7f7c06
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 "ddk/imm.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
38 #ifndef HAVE_XICCALLBACK_CALLBACK
39 #define XICCallback XIMCallback
40 #define XICProc XIMProc
41 #endif
43 BOOL ximInComposeMode=FALSE;
45 /* moved here from imm32 for dll separation */
46 static DWORD dwCompStringLength = 0;
47 static LPBYTE CompositionString = NULL;
48 static DWORD dwCompStringSize = 0;
49 static LPBYTE ResultString = NULL;
50 static DWORD dwResultStringSize = 0;
52 #define STYLE_OFFTHESPOT (XIMPreeditArea | XIMStatusArea)
53 #define STYLE_OVERTHESPOT (XIMPreeditPosition | XIMStatusNothing)
54 #define STYLE_ROOT (XIMPreeditNothing | XIMStatusNothing)
55 /* this uses all the callbacks to utilize full IME support */
56 #define STYLE_CALLBACK (XIMPreeditCallbacks | XIMStatusNothing)
57 /* inorder to enable deadkey support */
58 #define STYLE_NONE (XIMPreeditNothing | XIMStatusNothing)
60 static XIMStyle ximStyle = 0;
61 static XIMStyle ximStyleRoot = 0;
62 static XIMStyle ximStyleRequest = STYLE_CALLBACK;
64 static BOOL X11DRV_ImmSetInternalString(DWORD dwIndex, DWORD dwOffset,
65 DWORD selLength, LPWSTR lpComp, DWORD dwCompLen)
67 /* Composition strings are edited in chunks */
68 unsigned int byte_length = dwCompLen * sizeof(WCHAR);
69 unsigned int byte_offset = dwOffset * sizeof(WCHAR);
70 unsigned int byte_selection = selLength * sizeof(WCHAR);
71 BOOL rc = FALSE;
73 TRACE("( %i, %i, %d, %p, %d):\n", dwOffset, selLength, dwIndex, lpComp, dwCompLen );
75 if (dwIndex == GCS_COMPSTR)
77 unsigned int i,j;
78 LPBYTE ptr_new;
79 LPBYTE ptr_old;
81 if ((dwCompLen == 0) && (selLength == 0))
83 /* DO Nothing */
85 /* deletion occurred */
86 else if ((dwCompLen== 0) && (selLength != 0))
88 if (dwCompStringLength)
90 for (i = 0; i < byte_selection; i++)
92 if (byte_offset+byte_selection+i <
93 dwCompStringLength)
95 CompositionString[byte_offset + i] =
96 CompositionString[byte_offset + byte_selection + i];
98 else
99 CompositionString[byte_offset + i] = 0;
101 /* clean up the end */
102 dwCompStringLength -= byte_selection;
104 i = dwCompStringLength;
105 while (i < dwCompStringSize)
107 CompositionString[i++] = 0;
111 else
113 int byte_expansion = byte_length - byte_selection;
115 if (byte_expansion + dwCompStringLength >= dwCompStringSize)
117 if (CompositionString)
118 CompositionString =
119 HeapReAlloc(GetProcessHeap(), 0,
120 CompositionString,
121 dwCompStringSize +
122 byte_expansion);
123 else
124 CompositionString =
125 HeapAlloc(GetProcessHeap(), 0, dwCompStringSize +
126 byte_expansion);
128 memset(&(CompositionString[dwCompStringSize]), 0,
129 byte_expansion);
131 dwCompStringSize += byte_expansion;
134 ptr_new = ((LPBYTE)lpComp);
135 ptr_old = CompositionString + byte_offset + byte_selection;
137 dwCompStringLength += byte_expansion;
139 for (j=0,i = byte_offset; i < dwCompStringSize; i++)
141 if (j < byte_length)
143 CompositionString[i] = ptr_new[j++];
145 else
147 if (ptr_old < CompositionString + dwCompStringSize)
149 CompositionString[i] = *ptr_old;
150 ptr_old++;
152 else
153 CompositionString[i] = 0;
158 rc = IME_SetCompositionString(SCS_SETSTR, (LPWSTR)CompositionString,
159 dwCompStringLength, NULL, 0);
161 else if ((dwIndex == GCS_RESULTSTR) && (lpComp) && (dwCompLen))
163 if (dwResultStringSize)
164 HeapFree(GetProcessHeap(),0,ResultString);
165 dwResultStringSize= byte_length;
166 ResultString= HeapAlloc(GetProcessHeap(),0,byte_length);
167 memcpy(ResultString,lpComp,byte_length);
169 rc = IME_SetCompositionString(SCS_SETSTR, (LPWSTR)ResultString,
170 dwResultStringSize, NULL, 0);
172 IME_NotifyIME( NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
175 return rc;
178 void X11DRV_XIMLookupChars( const char *str, DWORD count )
180 DWORD dwOutput;
181 WCHAR wcOutput[64];
182 HWND focus;
184 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0, str, count, wcOutput, sizeof(wcOutput)/sizeof(WCHAR));
186 if ((focus = GetFocus()))
187 IME_UpdateAssociation(focus);
189 X11DRV_ImmSetInternalString(GCS_RESULTSTR,0,0,wcOutput,dwOutput);
192 static void X11DRV_ImmSetOpenStatus(BOOL fOpen)
194 if (fOpen == FALSE)
196 if (dwCompStringSize)
197 HeapFree(GetProcessHeap(),0,CompositionString);
199 dwCompStringSize = 0;
200 dwCompStringLength = 0;
201 CompositionString = NULL;
203 if (dwResultStringSize)
204 HeapFree(GetProcessHeap(),0,ResultString);
206 dwResultStringSize = 0;
207 ResultString = NULL;
210 IME_SetOpenStatus(fOpen);
213 static int XIMPreEditStartCallback(XIC ic, XPointer client_data, XPointer call_data)
215 TRACE("PreEditStartCallback %p\n",ic);
216 X11DRV_ImmSetOpenStatus(TRUE);
217 ximInComposeMode = TRUE;
218 return -1;
221 static void XIMPreEditDoneCallback(XIC ic, XPointer client_data, XPointer call_data)
223 TRACE("PreeditDoneCallback %p\n",ic);
224 ximInComposeMode = FALSE;
225 X11DRV_ImmSetOpenStatus(FALSE);
228 static void XIMPreEditDrawCallback(XIM ic, XPointer client_data,
229 XIMPreeditDrawCallbackStruct *P_DR)
231 TRACE("PreEditDrawCallback %p\n",ic);
233 if (P_DR)
235 int sel = P_DR->chg_first;
236 int len = P_DR->chg_length;
237 if (P_DR->text)
239 if (! P_DR->text->encoding_is_wchar)
241 DWORD dwOutput;
242 WCHAR *wcOutput;
244 TRACE("multibyte\n");
245 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
246 P_DR->text->string.multi_byte, -1,
247 NULL, 0);
248 wcOutput = HeapAlloc(GetProcessHeap(), 0, sizeof (WCHAR) * dwOutput);
249 if (wcOutput)
251 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
252 P_DR->text->string.multi_byte, -1,
253 wcOutput, dwOutput);
255 /* ignore null */
256 dwOutput --;
257 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, wcOutput, dwOutput);
258 HeapFree(GetProcessHeap(), 0, wcOutput);
261 else
263 FIXME("wchar PROBIBILY WRONG\n");
264 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len,
265 (LPWSTR)P_DR->text->string.wide_char,
266 P_DR->text->length);
269 else
270 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, NULL, 0);
271 IME_SetCursorPos(P_DR->caret);
273 TRACE("Finished\n");
276 static void XIMPreEditCaretCallback(XIC ic, XPointer client_data,
277 XIMPreeditCaretCallbackStruct *P_C)
279 TRACE("PreeditCaretCallback %p\n",ic);
281 if (P_C)
283 int pos = IME_GetCursorPos();
284 TRACE("pos: %d\n", pos);
285 switch(P_C->direction)
287 case XIMForwardChar:
288 case XIMForwardWord:
289 pos++;
290 break;
291 case XIMBackwardChar:
292 case XIMBackwardWord:
293 pos--;
294 break;
295 case XIMLineStart:
296 pos = 0;
297 break;
298 case XIMAbsolutePosition:
299 pos = P_C->position;
300 break;
301 case XIMDontChange:
302 P_C->position = pos;
303 return;
304 case XIMCaretUp:
305 case XIMCaretDown:
306 case XIMPreviousLine:
307 case XIMNextLine:
308 case XIMLineEnd:
309 FIXME("Not implemented\n");
310 break;
312 IME_SetCursorPos(pos);
313 P_C->position = pos;
315 TRACE("Finished\n");
318 void X11DRV_ForceXIMReset(HWND hwnd)
320 XIC ic = X11DRV_get_ic(hwnd);
321 if (ic)
323 char* leftover;
324 TRACE("Forcing Reset %p\n",ic);
325 wine_tsx11_lock();
326 leftover = XmbResetIC(ic);
327 XFree(leftover);
328 wine_tsx11_unlock();
332 /***********************************************************************
333 * X11DRV_InitXIM
335 * Process-wide XIM initialization.
337 BOOL X11DRV_InitXIM( const char *input_style )
339 BOOL ret;
341 if (!strcasecmp(input_style, "offthespot"))
342 ximStyleRequest = STYLE_OFFTHESPOT;
343 else if (!strcasecmp(input_style, "overthespot"))
344 ximStyleRequest = STYLE_OVERTHESPOT;
345 else if (!strcasecmp(input_style, "root"))
346 ximStyleRequest = STYLE_ROOT;
348 wine_tsx11_lock();
349 if (!(ret = XSupportsLocale()))
351 WARN("X does not support locale.\n");
353 else if (XSetLocaleModifiers("") == NULL)
355 WARN("Could not set locale modifiers.\n");
356 ret = FALSE;
358 wine_tsx11_unlock();
359 return ret;
363 static void X11DRV_OpenIM(Display *display, XPointer p, XPointer data);
365 static void X11DRV_DestroyIM(XIM xim, XPointer p, XPointer data)
367 struct x11drv_thread_data *thread_data = x11drv_thread_data();
369 TRACE("xim = %p, p = %p\n", xim, p);
370 thread_data->xim = NULL;
371 ximStyle = 0;
372 wine_tsx11_lock();
373 XRegisterIMInstantiateCallback( thread_data->display, NULL, NULL, NULL, X11DRV_OpenIM, NULL );
374 wine_tsx11_unlock();
377 /***********************************************************************
378 * X11DRV Ime creation
380 static void X11DRV_OpenIM(Display *display, XPointer ptr, XPointer data)
382 struct x11drv_thread_data *thread_data = x11drv_thread_data();
383 XIMStyle ximStyleCallback, ximStyleNone;
384 XIMStyles *ximStyles = NULL;
385 INT i;
386 XIM xim;
387 XIMCallback destroy;
389 wine_tsx11_lock();
391 xim = XOpenIM(display, NULL, NULL, NULL);
392 if (xim == NULL)
394 WARN("Could not open input method.\n");
395 wine_tsx11_unlock();
396 return;
399 destroy.client_data = NULL;
400 destroy.callback = X11DRV_DestroyIM;
401 if (XSetIMValues(xim, XNDestroyCallback, &destroy, NULL))
403 WARN("Could not set destroy callback.\n");
406 TRACE("xim = %p\n", xim);
407 TRACE("X display of IM = %p\n", XDisplayOfIM(xim));
408 TRACE("Using %s locale of Input Method\n", XLocaleOfIM(xim));
410 XGetIMValues(xim, XNQueryInputStyle, &ximStyles, NULL);
411 if (ximStyles == 0)
413 WARN("Could not find supported input style.\n");
414 XCloseIM(xim);
415 wine_tsx11_unlock();
416 return;
418 else
420 TRACE("ximStyles->count_styles = %d\n", ximStyles->count_styles);
422 ximStyleRoot = 0;
423 ximStyleNone = 0;
424 ximStyleCallback = 0;
426 for (i = 0; i < ximStyles->count_styles; ++i)
428 int style = ximStyles->supported_styles[i];
429 TRACE("ximStyles[%d] = %s%s%s%s%s\n", i,
430 (style&XIMPreeditArea)?"XIMPreeditArea ":"",
431 (style&XIMPreeditCallbacks)?"XIMPreeditCallbacks ":"",
432 (style&XIMPreeditPosition)?"XIMPreeditPosition ":"",
433 (style&XIMPreeditNothing)?"XIMPreeditNothing ":"",
434 (style&XIMPreeditNone)?"XIMPreeditNone ":"");
435 if (!ximStyle && (ximStyles->supported_styles[i] ==
436 ximStyleRequest))
438 ximStyle = ximStyleRequest;
439 TRACE("Setting Style: ximStyle = ximStyleRequest\n");
441 else if (!ximStyleRoot &&(ximStyles->supported_styles[i] ==
442 STYLE_ROOT))
444 ximStyleRoot = STYLE_ROOT;
445 TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
447 else if (!ximStyleCallback &&(ximStyles->supported_styles[i] ==
448 STYLE_CALLBACK))
450 ximStyleCallback = STYLE_CALLBACK;
451 TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
453 else if (!ximStyleNone && (ximStyles->supported_styles[i] ==
454 STYLE_NONE))
456 TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
457 ximStyleNone = STYLE_NONE;
460 XFree(ximStyles);
462 if (ximStyle == 0)
463 ximStyle = ximStyleRoot;
465 if (ximStyle == 0)
466 ximStyle = ximStyleNone;
468 if (ximStyleCallback == 0)
470 TRACE("No callback style avalable\n");
471 ximStyleCallback = ximStyle;
476 thread_data->xim = xim;
477 XUnregisterIMInstantiateCallback(display, NULL, NULL, NULL, X11DRV_OpenIM, NULL);
478 wine_tsx11_unlock();
479 IME_XIMPresent(TRUE);
480 IME_UpdateAssociation(NULL);
484 void X11DRV_SetupXIM(void)
486 wine_tsx11_lock();
487 XRegisterIMInstantiateCallback(thread_display(), NULL, NULL, NULL, X11DRV_OpenIM, NULL);
488 wine_tsx11_unlock();
491 static BOOL X11DRV_DestroyIC(XIC xic, XPointer p, XPointer data)
493 struct x11drv_win_data *win_data = (struct x11drv_win_data *)p;
494 TRACE("xic = %p, win = %lx\n", xic, win_data->whole_window);
495 win_data->xic = NULL;
496 return TRUE;
500 XIC X11DRV_CreateIC(XIM xim, struct x11drv_win_data *data)
502 XPoint spot = {0};
503 XVaNestedList preedit = NULL;
504 XVaNestedList status = NULL;
505 XIC xic;
506 XICCallback destroy = {(XPointer)data, (XICProc)X11DRV_DestroyIC};
507 XICCallback P_StartCB, P_DoneCB, P_DrawCB, P_CaretCB;
508 LANGID langid = PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale()));
509 Window win = data->whole_window;
511 TRACE("xim = %p\n", xim);
513 wine_tsx11_lock();
515 /* use complex and slow XIC initialization method only for CJK */
516 if (langid != LANG_CHINESE &&
517 langid != LANG_JAPANESE &&
518 langid != LANG_KOREAN)
520 xic = XCreateIC(xim,
521 XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
522 XNClientWindow, win,
523 XNFocusWindow, win,
524 XNDestroyCallback, &destroy,
525 NULL);
526 wine_tsx11_unlock();
527 data->xic = xic;
528 return xic;
531 /* create callbacks */
532 P_StartCB.client_data = NULL;
533 P_DoneCB.client_data = NULL;
534 P_DrawCB.client_data = NULL;
535 P_CaretCB.client_data = NULL;
536 P_StartCB.callback = (XICProc)XIMPreEditStartCallback;
537 P_DoneCB.callback = (XICProc)XIMPreEditDoneCallback;
538 P_DrawCB.callback = (XICProc)XIMPreEditDrawCallback;
539 P_CaretCB.callback = (XICProc)XIMPreEditCaretCallback;
541 if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0)
543 preedit = XVaCreateNestedList(0,
544 XNSpotLocation, &spot,
545 XNPreeditStartCallback, &P_StartCB,
546 XNPreeditDoneCallback, &P_DoneCB,
547 XNPreeditDrawCallback, &P_DrawCB,
548 XNPreeditCaretCallback, &P_CaretCB,
549 NULL);
550 TRACE("preedit = %p\n", preedit);
552 else
554 preedit = XVaCreateNestedList(0,
555 XNPreeditStartCallback, &P_StartCB,
556 XNPreeditDoneCallback, &P_DoneCB,
557 XNPreeditDrawCallback, &P_DrawCB,
558 XNPreeditCaretCallback, &P_CaretCB,
559 NULL);
561 TRACE("preedit = %p\n", preedit);
564 if ((ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
566 status = XVaCreateNestedList(0,
567 NULL);
568 TRACE("status = %p\n", status);
571 if (preedit != NULL && status != NULL)
573 xic = XCreateIC(xim,
574 XNInputStyle, ximStyle,
575 XNPreeditAttributes, preedit,
576 XNStatusAttributes, status,
577 XNClientWindow, win,
578 XNFocusWindow, win,
579 XNDestroyCallback, &destroy,
580 NULL);
582 else if (preedit != NULL)
584 xic = XCreateIC(xim,
585 XNInputStyle, ximStyle,
586 XNPreeditAttributes, preedit,
587 XNClientWindow, win,
588 XNFocusWindow, win,
589 XNDestroyCallback, &destroy,
590 NULL);
592 else if (status != NULL)
594 xic = XCreateIC(xim,
595 XNInputStyle, ximStyle,
596 XNStatusAttributes, status,
597 XNClientWindow, win,
598 XNFocusWindow, win,
599 XNDestroyCallback, &destroy,
600 NULL);
602 else
604 xic = XCreateIC(xim,
605 XNInputStyle, ximStyle,
606 XNClientWindow, win,
607 XNFocusWindow, win,
608 XNDestroyCallback, &destroy,
609 NULL);
612 TRACE("xic = %p\n", xic);
613 data->xic = xic;
615 if (preedit != NULL)
616 XFree(preedit);
617 if (status != NULL)
618 XFree(status);
620 wine_tsx11_unlock();
622 return xic;