winex11.drv: Only create a fontSet if we are going to be using it in the XIC.
[wine/multimedia.git] / dlls / winex11.drv / xim.c
blob2be9dd29e755a2c8e12247975fb909218c8f9469
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 /* this must match with imm32/imm.c */
38 #define FROM_IME 0xcafe1337
40 BOOL ximInComposeMode=FALSE;
42 static HIMC root_context;
43 static XIMStyle ximStyle = 0;
44 static XIMStyle ximStyleRoot = 0;
46 /* moved here from imm32 for dll separation */
47 static DWORD dwCompStringLength = 0;
48 static LPBYTE CompositionString = NULL;
49 static DWORD dwCompStringSize = 0;
50 static LPBYTE ResultString = NULL;
51 static DWORD dwResultStringSize = 0;
53 static HMODULE hImmDll = NULL;
54 static HIMC (WINAPI *pImmAssociateContext)(HWND,HIMC);
55 static HIMC (WINAPI *pImmCreateContext)(void);
56 static VOID (WINAPI *pImmSetOpenStatus)(HIMC,BOOL);
57 static BOOL (WINAPI *pImmSetCompositionString)(HIMC, DWORD, LPWSTR,
58 DWORD, LPWSTR, DWORD);
59 static VOID (WINAPI *pImmNotifyIME)(HIMC, DWORD, DWORD, DWORD);
61 /* WINE specific messages from the xim in x11drv level */
63 #define STYLE_OFFTHESPOT (XIMPreeditArea | XIMStatusArea)
64 #define STYLE_OVERTHESPOT (XIMPreeditPosition | XIMStatusNothing)
65 #define STYLE_ROOT (XIMPreeditNothing | XIMStatusNothing)
66 /* this uses all the callbacks to utilize full IME support */
67 #define STYLE_CALLBACK (XIMPreeditCallbacks | XIMStatusNothing)
68 /* inorder to enable deadkey support */
69 #define STYLE_NONE (XIMPreeditNothing | XIMStatusNothing)
72 * here are the functions that sort of marshall calls into IMM32.DLL
74 static void LoadImmDll(void)
76 hImmDll = LoadLibraryA("imm32.dll");
78 pImmAssociateContext = (void *)GetProcAddress(hImmDll, "ImmAssociateContext");
79 if (!pImmAssociateContext)
80 WARN("IMM: pImmAssociateContext not found in DLL\n");
82 pImmCreateContext = (void *)GetProcAddress(hImmDll, "ImmCreateContext");
83 if (!pImmCreateContext)
84 WARN("IMM: pImmCreateContext not found in DLL\n");
86 pImmSetOpenStatus = (void *)GetProcAddress( hImmDll, "ImmSetOpenStatus");
87 if (!pImmSetOpenStatus)
88 WARN("IMM: pImmSetOpenStatus not found in DLL\n");
90 pImmSetCompositionString =(void *)GetProcAddress(hImmDll, "ImmSetCompositionStringW");
92 if (!pImmSetCompositionString)
93 WARN("IMM: pImmSetCompositionStringW not found in DLL\n");
95 pImmNotifyIME = (void *)GetProcAddress( hImmDll, "ImmNotifyIME");
97 if (!pImmNotifyIME)
98 WARN("IMM: pImmNotifyIME not found in DLL\n");
101 static BOOL X11DRV_ImmSetInternalString(DWORD dwIndex, DWORD dwOffset,
102 DWORD selLength, LPWSTR lpComp, DWORD dwCompLen)
104 /* Composition strings are edited in chunks */
105 unsigned int byte_length = dwCompLen * sizeof(WCHAR);
106 unsigned int byte_offset = dwOffset * sizeof(WCHAR);
107 unsigned int byte_selection = selLength * sizeof(WCHAR);
108 BOOL rc = FALSE;
110 TRACE("( %i, %i, %d, %p, %d):\n", dwOffset, selLength, dwIndex, lpComp, dwCompLen );
112 if (dwIndex == GCS_COMPSTR)
114 unsigned int i,j;
115 LPBYTE ptr_new;
116 LPBYTE ptr_old;
118 if ((dwCompLen == 0) && (selLength == 0))
120 /* DO Nothing */
122 /* deletion occurred */
123 else if ((dwCompLen== 0) && (selLength != 0))
125 if (dwCompStringLength)
127 for (i = 0; i < byte_selection; i++)
129 if (byte_offset+byte_selection+i <
130 dwCompStringLength)
132 CompositionString[byte_offset + i] =
133 CompositionString[byte_offset + byte_selection + i];
135 else
136 CompositionString[byte_offset + i] = 0;
138 /* clean up the end */
139 dwCompStringLength -= byte_selection;
141 i = dwCompStringLength;
142 while (i < dwCompStringSize)
144 CompositionString[i++] = 0;
148 else
150 int byte_expansion = byte_length - byte_selection;
152 if (byte_expansion + dwCompStringLength >= dwCompStringSize)
154 if (CompositionString)
155 CompositionString =
156 HeapReAlloc(GetProcessHeap(), 0,
157 CompositionString,
158 dwCompStringSize +
159 byte_expansion);
160 else
161 CompositionString =
162 HeapAlloc(GetProcessHeap(), 0, dwCompStringSize +
163 byte_expansion);
165 memset(&(CompositionString[dwCompStringSize]), 0,
166 byte_expansion);
168 dwCompStringSize += byte_expansion;
171 ptr_new = ((LPBYTE)lpComp);
172 ptr_old = CompositionString + byte_offset + byte_selection;
174 dwCompStringLength += byte_expansion;
176 for (j=0,i = byte_offset; i < dwCompStringSize; i++)
178 if (j < byte_length)
180 CompositionString[i] = ptr_new[j++];
182 else
184 if (ptr_old < CompositionString + dwCompStringSize)
186 CompositionString[i] = *ptr_old;
187 ptr_old++;
189 else
190 CompositionString[i] = 0;
195 if (pImmSetCompositionString)
196 rc = pImmSetCompositionString((HIMC)FROM_IME, SCS_SETSTR,
197 (LPWSTR)CompositionString, dwCompStringLength,
198 NULL, 0);
200 else if ((dwIndex == GCS_RESULTSTR) && (lpComp) && (dwCompLen))
202 if (dwResultStringSize)
203 HeapFree(GetProcessHeap(),0,ResultString);
204 dwResultStringSize= byte_length;
205 ResultString= HeapAlloc(GetProcessHeap(),0,byte_length);
206 memcpy(ResultString,lpComp,byte_length);
208 if (pImmSetCompositionString)
209 rc = pImmSetCompositionString((HIMC)FROM_IME, SCS_SETSTR,
210 (LPWSTR)ResultString, dwResultStringSize,
211 NULL, 0);
213 if (pImmNotifyIME)
214 pImmNotifyIME((HIMC)FROM_IME, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
217 return rc;
220 void X11DRV_XIMLookupChars( const char *str, DWORD count )
222 DWORD dwOutput;
223 WCHAR wcOutput[64];
224 HWND focus;
226 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0, str, count, wcOutput, sizeof(wcOutput)/sizeof(WCHAR));
228 if (pImmAssociateContext && (focus = GetFocus()))
229 pImmAssociateContext(focus,root_context);
231 X11DRV_ImmSetInternalString(GCS_RESULTSTR,0,0,wcOutput,dwOutput);
234 static void X11DRV_ImmSetOpenStatus(BOOL fOpen)
236 if (fOpen == FALSE)
238 if (dwCompStringSize)
239 HeapFree(GetProcessHeap(),0,CompositionString);
241 dwCompStringSize = 0;
242 dwCompStringLength = 0;
243 CompositionString = NULL;
245 if (dwResultStringSize)
246 HeapFree(GetProcessHeap(),0,ResultString);
248 dwResultStringSize = 0;
249 ResultString = NULL;
252 if (pImmSetOpenStatus)
253 pImmSetOpenStatus((HIMC)FROM_IME,fOpen);
256 static int XIMPreEditStartCallback(XIC ic, XPointer client_data, XPointer call_data)
258 TRACE("PreEditStartCallback %p\n",ic);
259 X11DRV_ImmSetOpenStatus(TRUE);
260 ximInComposeMode = TRUE;
261 return -1;
264 static void XIMPreEditDoneCallback(XIC ic, XPointer client_data, XPointer call_data)
266 TRACE("PreeditDoneCallback %p\n",ic);
267 ximInComposeMode = FALSE;
268 X11DRV_ImmSetOpenStatus(FALSE);
271 static void XIMPreEditDrawCallback(XIM ic, XPointer client_data,
272 XIMPreeditDrawCallbackStruct *P_DR)
274 DWORD dwOutput;
275 WCHAR wcOutput[64];
277 TRACE("PreEditDrawCallback %p\n",ic);
279 if (P_DR)
281 int sel = P_DR->chg_first;
282 int len = P_DR->chg_length;
283 if (P_DR->text)
285 if (! P_DR->text->encoding_is_wchar)
287 TRACE("multibyte\n");
288 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
289 P_DR->text->string.multi_byte, -1,
290 wcOutput, 64);
292 /* ignore null */
293 dwOutput --;
294 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, wcOutput, dwOutput);
296 else
298 FIXME("wchar PROBIBILY WRONG\n");
299 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len,
300 (LPWSTR)P_DR->text->string.wide_char,
301 P_DR->text->length);
304 else
305 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, NULL, 0);
307 TRACE("Finished\n");
310 static void XIMPreEditCaretCallback(XIC ic, XPointer client_data,
311 XIMPreeditCaretCallbackStruct *P_C)
313 FIXME("PreeditCaretCalback %p\n",ic);
316 void X11DRV_ForceXIMReset(HWND hwnd)
318 XIC ic = X11DRV_get_ic(hwnd);
319 if (ic)
321 char* leftover;
322 TRACE("Forcing Reset %p\n",ic);
323 wine_tsx11_lock();
324 leftover = XmbResetIC(ic);
325 XFree(leftover);
326 wine_tsx11_unlock();
330 /***********************************************************************
331 * X11DRV Ime creation
333 XIM X11DRV_SetupXIM(Display *display, const char *input_style)
335 XIMStyle ximStyleRequest, ximStyleCallback, ximStyleNone;
336 XIMStyles *ximStyles = NULL;
337 INT i;
338 XIM xim;
340 ximStyleRequest = STYLE_CALLBACK;
342 if (!strcasecmp(input_style, "offthespot"))
343 ximStyleRequest = STYLE_OFFTHESPOT;
344 else if (!strcasecmp(input_style, "overthespot"))
345 ximStyleRequest = STYLE_OVERTHESPOT;
346 else if (!strcasecmp(input_style, "root"))
347 ximStyleRequest = STYLE_ROOT;
349 wine_tsx11_lock();
351 if(!XSupportsLocale())
353 WARN("X does not support locale.\n");
354 goto err;
356 if(XSetLocaleModifiers("") == NULL)
358 WARN("Could not set locale modifiers.\n");
359 goto err;
362 xim = XOpenIM(display, NULL, NULL, NULL);
363 if (xim == NULL)
365 WARN("Could not open input method.\n");
366 goto err;
369 TRACE("X display of IM = %p\n", XDisplayOfIM(xim));
370 TRACE("Using %s locale of Input Method\n", XLocaleOfIM(xim));
372 XGetIMValues(xim, XNQueryInputStyle, &ximStyles, NULL);
373 if (ximStyles == 0)
375 WARN("Could not find supported input style.\n");
377 else
379 TRACE("ximStyles->count_styles = %d\n", ximStyles->count_styles);
381 ximStyleRoot = 0;
382 ximStyleNone = 0;
383 ximStyleCallback = 0;
385 for (i = 0; i < ximStyles->count_styles; ++i)
387 int style = ximStyles->supported_styles[i];
388 TRACE("ximStyles[%d] = %s%s%s%s%s\n", i,
389 (style&XIMPreeditArea)?"XIMPreeditArea ":"",
390 (style&XIMPreeditCallbacks)?"XIMPreeditCallbacks ":"",
391 (style&XIMPreeditPosition)?"XIMPreeditPosition ":"",
392 (style&XIMPreeditNothing)?"XIMPreeditNothing ":"",
393 (style&XIMPreeditNone)?"XIMPreeditNone ":"");
394 if (!ximStyle && (ximStyles->supported_styles[i] ==
395 ximStyleRequest))
397 ximStyle = ximStyleRequest;
398 TRACE("Setting Style: ximStyle = ximStyleRequest\n");
400 else if (!ximStyleRoot &&(ximStyles->supported_styles[i] ==
401 STYLE_ROOT))
403 ximStyleRoot = STYLE_ROOT;
404 TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
406 else if (!ximStyleCallback &&(ximStyles->supported_styles[i] ==
407 STYLE_CALLBACK))
409 ximStyleCallback = STYLE_CALLBACK;
410 TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
412 else if (!ximStyleNone && (ximStyles->supported_styles[i] ==
413 STYLE_NONE))
415 TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
416 ximStyleNone = STYLE_NONE;
419 XFree(ximStyles);
421 if (ximStyle == 0)
422 ximStyle = ximStyleRoot;
424 if (ximStyle == 0)
425 ximStyle = ximStyleNone;
427 if (ximStyleCallback == 0)
429 TRACE("No callback style avalable\n");
430 ximStyleCallback = ximStyle;
435 wine_tsx11_unlock();
437 if(!hImmDll)
439 LoadImmDll();
441 if (pImmCreateContext)
443 root_context = pImmCreateContext();
444 if (pImmAssociateContext)
445 pImmAssociateContext(0,root_context);
449 return xim;
451 err:
452 wine_tsx11_unlock();
453 return NULL;
457 XIC X11DRV_CreateIC(XIM xim, Display *display, Window win)
459 XFontSet fontSet = NULL;
460 char **list;
461 int count;
462 XPoint spot = {0};
463 XVaNestedList preedit = NULL;
464 XVaNestedList status = NULL;
465 XIC xic;
466 XIMCallback P_StartCB;
467 XIMCallback P_DoneCB;
468 XIMCallback P_DrawCB;
469 XIMCallback P_CaretCB;
470 LANGID langid = PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale()));
472 wine_tsx11_lock();
474 /* use complex and slow XIC initialization method only for CJK */
475 if (langid != LANG_CHINESE &&
476 langid != LANG_JAPANESE &&
477 langid != LANG_KOREAN)
479 xic = XCreateIC(xim,
480 XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
481 XNClientWindow, win,
482 XNFocusWindow, win,
483 NULL);
484 wine_tsx11_unlock();
485 return xic;
489 if (((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0) ||
490 ((ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0))
492 fontSet = XCreateFontSet(display,
493 "*", /*FIXME*/
494 &list, &count, NULL);
496 TRACE("ximFontSet = %p\n", fontSet);
497 TRACE("list = %p, count = %d\n", list, count);
499 if (list != NULL)
501 int i;
503 for (i = 0; i < count; ++i)
505 TRACE("list[%d] = %s\n", i, list[i]);
507 XFreeStringList(list);
511 /* create callbacks */
512 P_StartCB.client_data = NULL;
513 P_StartCB.callback = (XIMProc)XIMPreEditStartCallback;
514 P_DoneCB.client_data = NULL;
515 P_DoneCB.callback = (XIMProc)XIMPreEditDoneCallback;
516 P_DrawCB.client_data = NULL;
517 P_DrawCB.callback = (XIMProc)XIMPreEditDrawCallback;
518 P_CaretCB.client_data = NULL;
519 P_CaretCB.callback = (XIMProc)XIMPreEditCaretCallback;
521 if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0)
523 preedit = XVaCreateNestedList(0,
524 XNFontSet, fontSet,
525 XNSpotLocation, &spot,
526 XNPreeditStartCallback, &P_StartCB,
527 XNPreeditDoneCallback, &P_DoneCB,
528 XNPreeditDrawCallback, &P_DrawCB,
529 XNPreeditCaretCallback, &P_CaretCB,
530 NULL);
531 TRACE("preedit = %p\n", preedit);
533 else
535 preedit = XVaCreateNestedList(0,
536 XNPreeditStartCallback, &P_StartCB,
537 XNPreeditDoneCallback, &P_DoneCB,
538 XNPreeditDrawCallback, &P_DrawCB,
539 XNPreeditCaretCallback, &P_CaretCB,
540 NULL);
542 TRACE("preedit = %p\n", preedit);
545 if ((ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
547 status = XVaCreateNestedList(0,
548 XNFontSet, fontSet,
549 NULL);
550 TRACE("status = %p\n", status);
553 if (preedit != NULL && status != NULL)
555 xic = XCreateIC(xim,
556 XNInputStyle, ximStyle,
557 XNPreeditAttributes, preedit,
558 XNStatusAttributes, status,
559 XNClientWindow, win,
560 XNFocusWindow, win,
561 NULL);
563 else if (preedit != NULL)
565 xic = XCreateIC(xim,
566 XNInputStyle, ximStyle,
567 XNPreeditAttributes, preedit,
568 XNClientWindow, win,
569 XNFocusWindow, win,
570 NULL);
572 else if (status != NULL)
574 xic = XCreateIC(xim,
575 XNInputStyle, ximStyle,
576 XNStatusAttributes, status,
577 XNClientWindow, win,
578 XNFocusWindow, win,
579 NULL);
581 else
583 xic = XCreateIC(xim,
584 XNInputStyle, ximStyle,
585 XNClientWindow, win,
586 XNFocusWindow, win,
587 NULL);
590 TRACE("xic = %p\n", xic);
592 if (preedit != NULL)
593 XFree(preedit);
594 if (status != NULL)
595 XFree(status);
597 wine_tsx11_unlock();
599 return xic;