strmbase: Remove break after return (Smatch).
[wine/multimedia.git] / dlls / winex11.drv / xim.c
blob786578eb516f92a06fc1064228a1da34c788a6ce
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(xim);
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 /* 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;
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_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);
129 switch (state)
131 case XIMPreeditEnable:
132 IME_SetOpenStatus(TRUE);
133 break;
134 case XIMPreeditDisable:
135 IME_SetOpenStatus(FALSE);
136 break;
137 default:
138 break;
140 return TRUE;
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;
148 return -1;
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);
168 if (P_DR)
170 int sel = P_DR->chg_first;
171 int len = P_DR->chg_length;
172 if (P_DR->text)
174 if (! P_DR->text->encoding_is_wchar)
176 DWORD dwOutput;
177 WCHAR *wcOutput;
179 TRACE("multibyte\n");
180 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
181 P_DR->text->string.multi_byte, -1,
182 NULL, 0);
183 wcOutput = HeapAlloc(GetProcessHeap(), 0, sizeof (WCHAR) * dwOutput);
184 if (wcOutput)
186 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
187 P_DR->text->string.multi_byte, -1,
188 wcOutput, dwOutput);
190 /* ignore null */
191 dwOutput --;
192 X11DRV_ImmSetInternalString (sel, len, wcOutput, dwOutput);
193 HeapFree(GetProcessHeap(), 0, wcOutput);
196 else
198 FIXME("wchar PROBIBILY WRONG\n");
199 X11DRV_ImmSetInternalString (sel, len,
200 (LPWSTR)P_DR->text->string.wide_char,
201 P_DR->text->length);
204 else
205 X11DRV_ImmSetInternalString (sel, len, NULL, 0);
206 IME_SetCursorPos(P_DR->caret);
208 TRACE("Finished\n");
211 static void XIMPreEditCaretCallback(XIC ic, XPointer client_data,
212 XIMPreeditCaretCallbackStruct *P_C)
214 TRACE("PreeditCaretCallback %p\n",ic);
216 if (P_C)
218 int pos = IME_GetCursorPos();
219 TRACE("pos: %d\n", pos);
220 switch(P_C->direction)
222 case XIMForwardChar:
223 case XIMForwardWord:
224 pos++;
225 break;
226 case XIMBackwardChar:
227 case XIMBackwardWord:
228 pos--;
229 break;
230 case XIMLineStart:
231 pos = 0;
232 break;
233 case XIMAbsolutePosition:
234 pos = P_C->position;
235 break;
236 case XIMDontChange:
237 P_C->position = pos;
238 return;
239 case XIMCaretUp:
240 case XIMCaretDown:
241 case XIMPreviousLine:
242 case XIMNextLine:
243 case XIMLineEnd:
244 FIXME("Not implemented\n");
245 break;
247 IME_SetCursorPos(pos);
248 P_C->position = pos;
250 TRACE("Finished\n");
253 void X11DRV_ForceXIMReset(HWND hwnd)
255 XIC ic = X11DRV_get_ic(hwnd);
256 if (ic)
258 char* leftover;
259 TRACE("Forcing Reset %p\n",ic);
260 wine_tsx11_lock();
261 leftover = XmbResetIC(ic);
262 XFree(leftover);
263 wine_tsx11_unlock();
267 void X11DRV_SetPreeditState(HWND hwnd, BOOL fOpen)
269 XIC ic;
270 XIMPreeditState state;
271 XVaNestedList attr;
273 ic = X11DRV_get_ic(hwnd);
274 if (!ic)
275 return;
277 if (fOpen)
278 state = XIMPreeditEnable;
279 else
280 state = XIMPreeditDisable;
282 wine_tsx11_lock();
284 attr = XVaCreateNestedList(0, XNPreeditState, state, NULL);
285 if (attr != NULL)
287 XSetICValues(ic, XNPreeditAttributes, attr, NULL);
288 XFree(attr);
291 wine_tsx11_unlock();
295 /***********************************************************************
296 * X11DRV_InitXIM
298 * Process-wide XIM initialization.
300 BOOL X11DRV_InitXIM( const char *input_style )
302 BOOL ret;
304 if (!strcasecmp(input_style, "offthespot"))
305 ximStyleRequest = STYLE_OFFTHESPOT;
306 else if (!strcasecmp(input_style, "overthespot"))
307 ximStyleRequest = STYLE_OVERTHESPOT;
308 else if (!strcasecmp(input_style, "root"))
309 ximStyleRequest = STYLE_ROOT;
311 wine_tsx11_lock();
312 if (!(ret = XSupportsLocale()))
314 WARN("X does not support locale.\n");
316 else if (XSetLocaleModifiers("") == NULL)
318 WARN("Could not set locale modifiers.\n");
319 ret = FALSE;
321 wine_tsx11_unlock();
322 return ret;
326 static void open_xim_callback( Display *display, XPointer ptr, XPointer data );
328 static void X11DRV_DestroyIM(XIM xim, XPointer p, XPointer data)
330 struct x11drv_thread_data *thread_data = x11drv_thread_data();
332 TRACE("xim = %p, p = %p\n", xim, p);
333 thread_data->xim = NULL;
334 ximStyle = 0;
335 wine_tsx11_lock();
336 XRegisterIMInstantiateCallback( thread_data->display, NULL, NULL, NULL, open_xim_callback, NULL );
337 wine_tsx11_unlock();
340 /***********************************************************************
341 * X11DRV Ime creation
343 * Should always be called with the x11 lock held
345 static BOOL open_xim( Display *display )
347 struct x11drv_thread_data *thread_data = x11drv_thread_data();
348 XIMStyle ximStyleCallback, ximStyleNone;
349 XIMStyles *ximStyles = NULL;
350 INT i;
351 XIM xim;
352 XIMCallback destroy;
354 xim = XOpenIM(display, NULL, NULL, NULL);
355 if (xim == NULL)
357 WARN("Could not open input method.\n");
358 return FALSE;
361 destroy.client_data = NULL;
362 destroy.callback = X11DRV_DestroyIM;
363 if (XSetIMValues(xim, XNDestroyCallback, &destroy, NULL))
365 WARN("Could not set destroy callback.\n");
368 TRACE("xim = %p\n", xim);
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");
376 XCloseIM(xim);
377 return FALSE;
379 else
381 TRACE("ximStyles->count_styles = %d\n", ximStyles->count_styles);
383 ximStyleRoot = 0;
384 ximStyleNone = 0;
385 ximStyleCallback = 0;
387 for (i = 0; i < ximStyles->count_styles; ++i)
389 int style = ximStyles->supported_styles[i];
390 TRACE("ximStyles[%d] = %s%s%s%s%s\n", i,
391 (style&XIMPreeditArea)?"XIMPreeditArea ":"",
392 (style&XIMPreeditCallbacks)?"XIMPreeditCallbacks ":"",
393 (style&XIMPreeditPosition)?"XIMPreeditPosition ":"",
394 (style&XIMPreeditNothing)?"XIMPreeditNothing ":"",
395 (style&XIMPreeditNone)?"XIMPreeditNone ":"");
396 if (!ximStyle && (ximStyles->supported_styles[i] ==
397 ximStyleRequest))
399 ximStyle = ximStyleRequest;
400 TRACE("Setting Style: ximStyle = ximStyleRequest\n");
402 else if (!ximStyleRoot &&(ximStyles->supported_styles[i] ==
403 STYLE_ROOT))
405 ximStyleRoot = STYLE_ROOT;
406 TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
408 else if (!ximStyleCallback &&(ximStyles->supported_styles[i] ==
409 STYLE_CALLBACK))
411 ximStyleCallback = STYLE_CALLBACK;
412 TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
414 else if (!ximStyleNone && (ximStyles->supported_styles[i] ==
415 STYLE_NONE))
417 TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
418 ximStyleNone = STYLE_NONE;
421 XFree(ximStyles);
423 if (ximStyle == 0)
424 ximStyle = ximStyleRoot;
426 if (ximStyle == 0)
427 ximStyle = ximStyleNone;
429 if (ximStyleCallback == 0)
431 TRACE("No callback style available\n");
432 ximStyleCallback = ximStyle;
437 thread_data->xim = xim;
439 if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0 ||
440 (ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
442 char **list;
443 int count;
444 thread_data->font_set = XCreateFontSet(display, "fixed",
445 &list, &count, NULL);
446 TRACE("ximFontSet = %p\n", thread_data->font_set);
447 TRACE("list = %p, count = %d\n", list, count);
448 if (list != NULL)
450 int i;
451 for (i = 0; i < count; ++i)
452 TRACE("list[%d] = %s\n", i, list[i]);
453 XFreeStringList(list);
456 else
457 thread_data->font_set = NULL;
459 wine_tsx11_unlock();
460 IME_UpdateAssociation(NULL);
461 wine_tsx11_lock();
462 return TRUE;
465 static void open_xim_callback( Display *display, XPointer ptr, XPointer data )
467 if (open_xim( display ))
468 XUnregisterIMInstantiateCallback( display, NULL, NULL, NULL, open_xim_callback, NULL);
471 void X11DRV_SetupXIM(void)
473 Display *display = thread_display();
475 wine_tsx11_lock();
476 if (!open_xim( display ))
477 XRegisterIMInstantiateCallback( display, NULL, NULL, NULL, open_xim_callback, NULL );
478 wine_tsx11_unlock();
481 static BOOL X11DRV_DestroyIC(XIC xic, XPointer p, XPointer data)
483 struct x11drv_win_data *win_data = (struct x11drv_win_data *)p;
484 TRACE("xic = %p, win = %lx\n", xic, win_data->whole_window);
485 win_data->xic = NULL;
486 return TRUE;
490 XIC X11DRV_CreateIC(XIM xim, struct x11drv_win_data *data)
492 XPoint spot = {0};
493 XVaNestedList preedit = NULL;
494 XVaNestedList status = NULL;
495 XIC xic;
496 XICCallback destroy = {(XPointer)data, (XICProc)X11DRV_DestroyIC};
497 XICCallback P_StateNotifyCB, P_StartCB, P_DoneCB, P_DrawCB, P_CaretCB;
498 LANGID langid = PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale()));
499 Window win = data->whole_window;
500 XFontSet fontSet = x11drv_thread_data()->font_set;
502 TRACE("xim = %p\n", xim);
504 wine_tsx11_lock();
506 /* use complex and slow XIC initialization method only for CJK */
507 if (langid != LANG_CHINESE &&
508 langid != LANG_JAPANESE &&
509 langid != LANG_KOREAN)
511 xic = XCreateIC(xim,
512 XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
513 XNClientWindow, win,
514 XNFocusWindow, win,
515 XNDestroyCallback, &destroy,
516 NULL);
517 wine_tsx11_unlock();
518 data->xic = xic;
519 return xic;
522 /* create callbacks */
523 P_StateNotifyCB.client_data = (XPointer)data;
524 P_StartCB.client_data = NULL;
525 P_DoneCB.client_data = NULL;
526 P_DrawCB.client_data = NULL;
527 P_CaretCB.client_data = NULL;
528 P_StateNotifyCB.callback = (XICProc)XIMPreEditStateNotifyCallback;
529 P_StartCB.callback = (XICProc)XIMPreEditStartCallback;
530 P_DoneCB.callback = (XICProc)XIMPreEditDoneCallback;
531 P_DrawCB.callback = (XICProc)XIMPreEditDrawCallback;
532 P_CaretCB.callback = (XICProc)XIMPreEditCaretCallback;
534 if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0)
536 preedit = XVaCreateNestedList(0,
537 XNFontSet, fontSet,
538 XNSpotLocation, &spot,
539 XNPreeditStateNotifyCallback, &P_StateNotifyCB,
540 XNPreeditStartCallback, &P_StartCB,
541 XNPreeditDoneCallback, &P_DoneCB,
542 XNPreeditDrawCallback, &P_DrawCB,
543 XNPreeditCaretCallback, &P_CaretCB,
544 NULL);
545 TRACE("preedit = %p\n", preedit);
547 else
549 preedit = XVaCreateNestedList(0,
550 XNPreeditStateNotifyCallback, &P_StateNotifyCB,
551 XNPreeditStartCallback, &P_StartCB,
552 XNPreeditDoneCallback, &P_DoneCB,
553 XNPreeditDrawCallback, &P_DrawCB,
554 XNPreeditCaretCallback, &P_CaretCB,
555 NULL);
557 TRACE("preedit = %p\n", preedit);
560 if ((ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
562 status = XVaCreateNestedList(0,
563 XNFontSet, fontSet,
564 NULL);
565 TRACE("status = %p\n", status);
568 if (preedit != NULL && status != NULL)
570 xic = XCreateIC(xim,
571 XNInputStyle, ximStyle,
572 XNPreeditAttributes, preedit,
573 XNStatusAttributes, status,
574 XNClientWindow, win,
575 XNFocusWindow, win,
576 XNDestroyCallback, &destroy,
577 NULL);
579 else if (preedit != NULL)
581 xic = XCreateIC(xim,
582 XNInputStyle, ximStyle,
583 XNPreeditAttributes, preedit,
584 XNClientWindow, win,
585 XNFocusWindow, win,
586 XNDestroyCallback, &destroy,
587 NULL);
589 else if (status != NULL)
591 xic = XCreateIC(xim,
592 XNInputStyle, ximStyle,
593 XNStatusAttributes, status,
594 XNClientWindow, win,
595 XNFocusWindow, win,
596 XNDestroyCallback, &destroy,
597 NULL);
599 else
601 xic = XCreateIC(xim,
602 XNInputStyle, ximStyle,
603 XNClientWindow, win,
604 XNFocusWindow, win,
605 XNDestroyCallback, &destroy,
606 NULL);
609 TRACE("xic = %p\n", xic);
610 data->xic = xic;
612 if (preedit != NULL)
613 XFree(preedit);
614 if (status != NULL)
615 XFree(status);
617 wine_tsx11_unlock();
619 return xic;