Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / x11drv / xim.c
blob04824103cf0e46f72ebb4ccf72f177be674800b0
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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()
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("( %li, %li, %ld, %p, %ld):\n", dwOffset, selLength, dwIndex, lpComp,
111 dwCompLen );
113 if (dwIndex == GCS_COMPSTR)
115 unsigned int i,j;
116 LPBYTE ptr_new;
117 LPBYTE ptr_old;
119 if ((dwCompLen == 0) && (selLength == 0))
121 /* DO Nothing */
123 /* deletion occurred */
124 else if ((dwCompLen== 0) && (selLength != 0))
126 if (dwCompStringLength)
128 for (i = 0; i < byte_selection; i++)
130 if (byte_offset+byte_selection+i <
131 dwCompStringLength)
133 CompositionString[byte_offset + i] =
134 CompositionString[byte_offset + byte_selection + i];
136 else
137 CompositionString[byte_offset + i] = 0;
139 /* clean up the end */
140 dwCompStringLength -= byte_selection;
142 i = dwCompStringLength;
143 while (i < dwCompStringSize)
145 CompositionString[i++] = 0;
149 else
151 int byte_expansion = byte_length - byte_selection;
153 if (byte_expansion + dwCompStringLength >= dwCompStringSize)
155 if (CompositionString)
156 CompositionString =
157 HeapReAlloc(GetProcessHeap(), 0,
158 CompositionString,
159 dwCompStringSize +
160 byte_expansion);
161 else
162 CompositionString =
163 HeapAlloc(GetProcessHeap(), 0, dwCompStringSize +
164 byte_expansion);
166 memset(&(CompositionString[dwCompStringSize]), byte_expansion,
169 dwCompStringSize += byte_expansion;
172 ptr_new = ((LPBYTE)lpComp);
173 ptr_old = CompositionString + byte_offset + byte_selection;
175 dwCompStringLength += byte_expansion;
177 for (j=0,i = byte_offset; i < dwCompStringSize; i++)
179 if (j < byte_length)
181 CompositionString[i] = ptr_new[j++];
183 else
185 if (ptr_old < CompositionString + dwCompStringSize)
187 CompositionString[i] = *ptr_old;
188 ptr_old++;
190 else
191 CompositionString[i] = 0;
196 if (pImmSetCompositionString)
197 rc = pImmSetCompositionString((HIMC)FROM_IME, SCS_SETSTR,
198 (LPWSTR)CompositionString, dwCompStringLength,
199 NULL, 0);
201 else if ((dwIndex == GCS_RESULTSTR) && (lpComp) && (dwCompLen))
203 if (dwResultStringSize)
204 HeapFree(GetProcessHeap(),0,ResultString);
205 dwResultStringSize= byte_length;
206 ResultString= HeapAlloc(GetProcessHeap(),0,byte_length);
207 memcpy(ResultString,lpComp,byte_length);
209 if (pImmSetCompositionString)
210 rc = pImmSetCompositionString((HIMC)FROM_IME, SCS_SETSTR,
211 (LPWSTR)ResultString, dwResultStringSize,
212 NULL, 0);
214 if (pImmNotifyIME)
215 pImmNotifyIME((HIMC)FROM_IME, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
218 return rc;
221 void X11DRV_XIMLookupChars( const char *str, DWORD count )
223 DWORD dwOutput;
224 WCHAR wcOutput[64];
225 HWND focus;
227 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0, str, count, wcOutput, sizeof(wcOutput));
229 if (pImmAssociateContext && (focus = GetFocus()))
230 pImmAssociateContext(focus,root_context);
232 X11DRV_ImmSetInternalString(GCS_RESULTSTR,0,0,wcOutput,dwOutput);
235 static void X11DRV_ImmSetOpenStatus(BOOL fOpen)
237 if (fOpen == FALSE)
239 if (dwCompStringSize)
240 HeapFree(GetProcessHeap(),0,CompositionString);
242 dwCompStringSize = 0;
243 dwCompStringLength = 0;
244 CompositionString = NULL;
246 if (dwResultStringSize)
247 HeapFree(GetProcessHeap(),0,ResultString);
249 dwResultStringSize = 0;
250 ResultString = NULL;
253 if (pImmSetOpenStatus)
254 pImmSetOpenStatus((HIMC)FROM_IME,fOpen);
257 static int XIMPreEditStartCallback(XIC ic, XPointer client_data, XPointer call_data)
259 TRACE("PreEditStartCallback %p\n",ic);
260 X11DRV_ImmSetOpenStatus(TRUE);
261 ximInComposeMode = TRUE;
262 return -1;
265 static void XIMPreEditDoneCallback(XIC ic, XPointer client_data, XPointer call_data)
267 TRACE("PreeditDoneCallback %p\n",ic);
268 ximInComposeMode = FALSE;
269 X11DRV_ImmSetOpenStatus(FALSE);
272 static void XIMPreEditDrawCallback(XIM ic, XPointer client_data,
273 XIMPreeditDrawCallbackStruct *P_DR)
275 DWORD dwOutput;
276 WCHAR wcOutput[64];
278 TRACE("PreEditDrawCallback %p\n",ic);
280 if (P_DR)
282 int sel = P_DR->chg_first;
283 int len = P_DR->chg_length;
284 if (P_DR->text)
286 if (! P_DR->text->encoding_is_wchar)
288 TRACE("multibyte\n");
289 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
290 P_DR->text->string.multi_byte, -1,
291 wcOutput, 64);
293 /* ignore null */
294 dwOutput --;
295 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, wcOutput, dwOutput);
297 else
299 FIXME("wchar PROBIBILY WRONG\n");
300 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len,
301 (LPWSTR)P_DR->text->string.wide_char,
302 P_DR->text->length);
305 else
306 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, NULL, 0);
308 TRACE("Finished\n");
311 static void XIMPreEditCaretCallback(XIC ic, XPointer client_data,
312 XIMPreeditCaretCallbackStruct *P_C)
314 FIXME("PreeditCaretCalback %p\n",ic);
317 void X11DRV_ForceXIMReset(HWND hwnd)
319 XIC ic = X11DRV_get_ic(hwnd);
320 if (ic)
322 char* leftover;
323 TRACE("Forcing Reset %p\n",ic);
324 wine_tsx11_lock();
325 leftover = XmbResetIC(ic);
326 XFree(leftover);
327 wine_tsx11_unlock();
331 /***********************************************************************
332 * X11DRV Ime creation
334 XIM X11DRV_SetupXIM(Display *display, const char *input_style)
336 XIMStyle ximStyleRequest, ximStyleCallback, ximStyleNone;
337 XIMStyles *ximStyles = NULL;
338 INT i;
339 XIM xim;
341 ximStyleRequest = STYLE_CALLBACK;
343 if (!strcasecmp(input_style, "offthespot"))
344 ximStyleRequest = STYLE_OFFTHESPOT;
345 else if (!strcasecmp(input_style, "overthespot"))
346 ximStyleRequest = STYLE_OVERTHESPOT;
347 else if (!strcasecmp(input_style, "root"))
348 ximStyleRequest = STYLE_ROOT;
350 wine_tsx11_lock();
352 if(!XSupportsLocale())
354 WARN("X does not support locale.\n");
355 goto err;
357 if(XSetLocaleModifiers("") == NULL)
359 WARN("Could not set locale modifiers.\n");
360 goto err;
363 xim = XOpenIM(display, NULL, NULL, NULL);
364 if (xim == NULL)
366 WARN("Could not open input method.\n");
367 goto err;
370 TRACE("X display of IM = %p\n", XDisplayOfIM(xim));
371 TRACE("Using %s locale of Input Method\n", XLocaleOfIM(xim));
373 XGetIMValues(xim, XNQueryInputStyle, &ximStyles, NULL);
374 if (ximStyles == 0)
376 WARN("Could not find supported input style.\n");
378 else
380 TRACE("ximStyles->count_styles = %d\n", ximStyles->count_styles);
382 ximStyleRoot = 0;
383 ximStyleNone = 0;
384 ximStyleCallback = 0;
386 for (i = 0; i < ximStyles->count_styles; ++i)
388 int style = ximStyles->supported_styles[i];
389 TRACE("ximStyles[%d] = %s%s%s%s%s\n", i,
390 (style&XIMPreeditArea)?"XIMPreeditArea ":"",
391 (style&XIMPreeditCallbacks)?"XIMPreeditCallbacks ":"",
392 (style&XIMPreeditPosition)?"XIMPreeditPosition ":"",
393 (style&XIMPreeditNothing)?"XIMPreeditNothing ":"",
394 (style&XIMPreeditNone)?"XIMPreeditNone ":"");
395 if (!ximStyle && (ximStyles->supported_styles[i] ==
396 ximStyleRequest))
398 ximStyle = ximStyleRequest;
399 TRACE("Setting Style: ximStyle = ximStyleRequest\n");
401 else if (!ximStyleRoot &&(ximStyles->supported_styles[i] ==
402 STYLE_ROOT))
404 ximStyleRoot = STYLE_ROOT;
405 TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
407 else if (!ximStyleCallback &&(ximStyles->supported_styles[i] ==
408 STYLE_CALLBACK))
410 ximStyleCallback = STYLE_CALLBACK;
411 TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
413 else if (!ximStyleNone && (ximStyles->supported_styles[i] ==
414 STYLE_NONE))
416 TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
417 ximStyleNone = STYLE_NONE;
420 XFree(ximStyles);
422 if (ximStyle == 0)
423 ximStyle = ximStyleRoot;
425 if (ximStyle == 0)
426 ximStyle = ximStyleNone;
428 if (ximStyleCallback == 0)
430 TRACE("No callback style avalable\n");
431 ximStyleCallback = ximStyle;
436 wine_tsx11_unlock();
438 LoadImmDll();
440 if (pImmCreateContext)
442 root_context = pImmCreateContext();
443 if (pImmAssociateContext)
444 pImmAssociateContext(0,root_context);
447 return xim;
449 err:
450 wine_tsx11_unlock();
451 return NULL;
455 XIC X11DRV_CreateIC(XIM xim, Display *display, Window win)
457 XFontSet fontSet;
458 char **list;
459 int count;
460 XPoint spot = {0};
461 XVaNestedList preedit = NULL;
462 XVaNestedList status = NULL;
463 XIC xic;
464 XIMCallback P_StartCB;
465 XIMCallback P_DoneCB;
466 XIMCallback P_DrawCB;
467 XIMCallback P_CaretCB;
468 LANGID langid = PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale()));
470 wine_tsx11_lock();
472 /* use complex and slow XIC initialization method only for CJK */
473 if (langid != LANG_CHINESE &&
474 langid != LANG_JAPANESE &&
475 langid != LANG_KOREAN)
477 xic = XCreateIC(xim,
478 XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
479 XNClientWindow, win,
480 XNFocusWindow, win,
481 NULL);
482 wine_tsx11_unlock();
483 return xic;
486 fontSet = XCreateFontSet(display,
487 "*", /*FIXME*/
488 &list, &count, NULL);
490 TRACE("ximFontSet = 0x%x\n", (unsigned int) fontSet);
491 TRACE("list = 0x%x, count = %d\n", (unsigned int) list, count);
493 if (list != NULL)
495 int i;
497 for (i = 0; i < count; ++i)
499 TRACE("list[%d] = %s\n", i, list[i]);
501 XFreeStringList(list);
504 /* create callbacks */
505 P_StartCB.client_data = NULL;
506 P_StartCB.callback = (XIMProc)XIMPreEditStartCallback;
507 P_DoneCB.client_data = NULL;
508 P_DoneCB.callback = (XIMProc)XIMPreEditDoneCallback;
509 P_DrawCB.client_data = NULL;
510 P_DrawCB.callback = (XIMProc)XIMPreEditDrawCallback;
511 P_CaretCB.client_data = NULL;
512 P_CaretCB.callback = (XIMProc)XIMPreEditCaretCallback;
514 if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0)
516 preedit = XVaCreateNestedList(0,
517 XNFontSet, fontSet,
518 XNSpotLocation, &spot,
519 XNPreeditStartCallback, &P_StartCB,
520 XNPreeditDoneCallback, &P_DoneCB,
521 XNPreeditDrawCallback, &P_DrawCB,
522 XNPreeditCaretCallback, &P_CaretCB,
523 NULL);
524 TRACE("preedit = 0x%x\n", (unsigned int) preedit);
526 else
528 preedit = XVaCreateNestedList(0,
529 XNPreeditStartCallback, &P_StartCB,
530 XNPreeditDoneCallback, &P_DoneCB,
531 XNPreeditDrawCallback, &P_DrawCB,
532 XNPreeditCaretCallback, &P_CaretCB,
533 NULL);
535 TRACE("preedit = 0x%x\n", (unsigned int) preedit);
538 if ((ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
540 status = XVaCreateNestedList(0,
541 XNFontSet, fontSet,
542 NULL);
543 TRACE("status = 0x%x\n", (unsigned int) status);
546 if (preedit != NULL && status != NULL)
548 xic = XCreateIC(xim,
549 XNInputStyle, ximStyle,
550 XNPreeditAttributes, preedit,
551 XNStatusAttributes, status,
552 XNClientWindow, win,
553 XNFocusWindow, win,
554 NULL);
556 else if (preedit != NULL)
558 xic = XCreateIC(xim,
559 XNInputStyle, ximStyle,
560 XNPreeditAttributes, preedit,
561 XNClientWindow, win,
562 XNFocusWindow, win,
563 NULL);
565 else if (status != NULL)
567 xic = XCreateIC(xim,
568 XNInputStyle, ximStyle,
569 XNStatusAttributes, status,
570 XNClientWindow, win,
571 XNFocusWindow, win,
572 NULL);
574 else
576 xic = XCreateIC(xim,
577 XNInputStyle, ximStyle,
578 XNClientWindow, win,
579 XNFocusWindow, win,
580 NULL);
583 TRACE("xic = 0x%x\n", (unsigned int) xic);
585 if (preedit != NULL)
586 XFree(preedit);
587 if (status != NULL)
588 XFree(status);
590 wine_tsx11_unlock();
592 return xic;