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(x11drv
);
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 /* inorder 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 BOOL
X11DRV_ImmSetInternalString(DWORD dwIndex
, 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
);
70 TRACE("( %i, %i, %d, %p, %d):\n", dwOffset
, selLength
, dwIndex
, lpComp
, dwCompLen
);
72 if (dwIndex
== GCS_COMPSTR
)
78 if ((dwCompLen
== 0) && (selLength
== 0))
82 /* deletion occurred */
83 else if ((dwCompLen
== 0) && (selLength
!= 0))
85 if (dwCompStringLength
)
87 for (i
= 0; i
< byte_selection
; i
++)
89 if (byte_offset
+byte_selection
+i
<
92 CompositionString
[byte_offset
+ i
] =
93 CompositionString
[byte_offset
+ byte_selection
+ i
];
96 CompositionString
[byte_offset
+ i
] = 0;
98 /* clean up the end */
99 dwCompStringLength
-= byte_selection
;
101 i
= dwCompStringLength
;
102 while (i
< dwCompStringSize
)
104 CompositionString
[i
++] = 0;
110 int byte_expansion
= byte_length
- byte_selection
;
112 if (byte_expansion
+ dwCompStringLength
>= dwCompStringSize
)
114 if (CompositionString
)
116 HeapReAlloc(GetProcessHeap(), 0,
122 HeapAlloc(GetProcessHeap(), 0, dwCompStringSize
+
125 memset(&(CompositionString
[dwCompStringSize
]), 0,
128 dwCompStringSize
+= byte_expansion
;
131 ptr_new
= ((LPBYTE
)lpComp
);
132 ptr_old
= CompositionString
+ byte_offset
+ byte_selection
;
134 dwCompStringLength
+= byte_expansion
;
136 for (j
=0,i
= byte_offset
; i
< dwCompStringSize
; i
++)
140 CompositionString
[i
] = ptr_new
[j
++];
144 if (ptr_old
< CompositionString
+ dwCompStringSize
)
146 CompositionString
[i
] = *ptr_old
;
150 CompositionString
[i
] = 0;
155 rc
= IME_SetCompositionString(SCS_SETSTR
, CompositionString
,
156 dwCompStringLength
, NULL
, 0);
158 else if ((dwIndex
== GCS_RESULTSTR
) && (lpComp
) && (dwCompLen
))
160 rc
= IME_SetCompositionString(SCS_SETSTR
, lpComp
,
161 byte_length
, NULL
, 0);
163 IME_NotifyIME( NI_COMPOSITIONSTR
, CPS_COMPLETE
, 0);
169 void X11DRV_XIMLookupChars( const char *str
, DWORD count
)
175 dwOutput
= MultiByteToWideChar(CP_UNIXCP
, 0, str
, count
, NULL
, 0);
176 wcOutput
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * dwOutput
);
177 if (wcOutput
== NULL
)
179 MultiByteToWideChar(CP_UNIXCP
, 0, str
, count
, wcOutput
, dwOutput
);
181 if ((focus
= GetFocus()))
182 IME_UpdateAssociation(focus
);
184 X11DRV_ImmSetInternalString(GCS_RESULTSTR
,0,0,wcOutput
,dwOutput
);
185 HeapFree(GetProcessHeap(), 0, wcOutput
);
188 static void X11DRV_ImmSetOpenStatus(BOOL fOpen
)
192 if (dwCompStringSize
)
193 HeapFree(GetProcessHeap(),0,CompositionString
);
195 dwCompStringSize
= 0;
196 dwCompStringLength
= 0;
197 CompositionString
= NULL
;
200 IME_SetOpenStatus(fOpen
);
203 static int XIMPreEditStartCallback(XIC ic
, XPointer client_data
, XPointer call_data
)
205 TRACE("PreEditStartCallback %p\n",ic
);
206 X11DRV_ImmSetOpenStatus(TRUE
);
207 ximInComposeMode
= TRUE
;
211 static void XIMPreEditDoneCallback(XIC ic
, XPointer client_data
, XPointer call_data
)
213 TRACE("PreeditDoneCallback %p\n",ic
);
214 ximInComposeMode
= FALSE
;
215 X11DRV_ImmSetOpenStatus(FALSE
);
218 static void XIMPreEditDrawCallback(XIM ic
, XPointer client_data
,
219 XIMPreeditDrawCallbackStruct
*P_DR
)
221 TRACE("PreEditDrawCallback %p\n",ic
);
225 int sel
= P_DR
->chg_first
;
226 int len
= P_DR
->chg_length
;
229 if (! P_DR
->text
->encoding_is_wchar
)
234 TRACE("multibyte\n");
235 dwOutput
= MultiByteToWideChar(CP_UNIXCP
, 0,
236 P_DR
->text
->string
.multi_byte
, -1,
238 wcOutput
= HeapAlloc(GetProcessHeap(), 0, sizeof (WCHAR
) * dwOutput
);
241 dwOutput
= MultiByteToWideChar(CP_UNIXCP
, 0,
242 P_DR
->text
->string
.multi_byte
, -1,
247 X11DRV_ImmSetInternalString (GCS_COMPSTR
, sel
, len
, wcOutput
, dwOutput
);
248 HeapFree(GetProcessHeap(), 0, wcOutput
);
253 FIXME("wchar PROBIBILY WRONG\n");
254 X11DRV_ImmSetInternalString (GCS_COMPSTR
, sel
, len
,
255 (LPWSTR
)P_DR
->text
->string
.wide_char
,
260 X11DRV_ImmSetInternalString (GCS_COMPSTR
, sel
, len
, NULL
, 0);
261 IME_SetCursorPos(P_DR
->caret
);
266 static void XIMPreEditCaretCallback(XIC ic
, XPointer client_data
,
267 XIMPreeditCaretCallbackStruct
*P_C
)
269 TRACE("PreeditCaretCallback %p\n",ic
);
273 int pos
= IME_GetCursorPos();
274 TRACE("pos: %d\n", pos
);
275 switch(P_C
->direction
)
281 case XIMBackwardChar
:
282 case XIMBackwardWord
:
288 case XIMAbsolutePosition
:
296 case XIMPreviousLine
:
299 FIXME("Not implemented\n");
302 IME_SetCursorPos(pos
);
308 void X11DRV_ForceXIMReset(HWND hwnd
)
310 XIC ic
= X11DRV_get_ic(hwnd
);
314 TRACE("Forcing Reset %p\n",ic
);
316 leftover
= XmbResetIC(ic
);
322 /***********************************************************************
325 * Process-wide XIM initialization.
327 BOOL
X11DRV_InitXIM( const char *input_style
)
331 if (!strcasecmp(input_style
, "offthespot"))
332 ximStyleRequest
= STYLE_OFFTHESPOT
;
333 else if (!strcasecmp(input_style
, "overthespot"))
334 ximStyleRequest
= STYLE_OVERTHESPOT
;
335 else if (!strcasecmp(input_style
, "root"))
336 ximStyleRequest
= STYLE_ROOT
;
339 if (!(ret
= XSupportsLocale()))
341 WARN("X does not support locale.\n");
343 else if (XSetLocaleModifiers("") == NULL
)
345 WARN("Could not set locale modifiers.\n");
353 static void open_xim_callback( Display
*display
, XPointer ptr
, XPointer data
);
355 static void X11DRV_DestroyIM(XIM xim
, XPointer p
, XPointer data
)
357 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
359 TRACE("xim = %p, p = %p\n", xim
, p
);
360 thread_data
->xim
= NULL
;
363 XRegisterIMInstantiateCallback( thread_data
->display
, NULL
, NULL
, NULL
, open_xim_callback
, NULL
);
367 /***********************************************************************
368 * X11DRV Ime creation
370 * Should always be called with the x11 lock held
372 static BOOL
open_xim( Display
*display
)
374 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
375 XIMStyle ximStyleCallback
, ximStyleNone
;
376 XIMStyles
*ximStyles
= NULL
;
381 xim
= XOpenIM(display
, NULL
, NULL
, NULL
);
384 WARN("Could not open input method.\n");
388 destroy
.client_data
= NULL
;
389 destroy
.callback
= X11DRV_DestroyIM
;
390 if (XSetIMValues(xim
, XNDestroyCallback
, &destroy
, NULL
))
392 WARN("Could not set destroy callback.\n");
395 TRACE("xim = %p\n", xim
);
396 TRACE("X display of IM = %p\n", XDisplayOfIM(xim
));
397 TRACE("Using %s locale of Input Method\n", XLocaleOfIM(xim
));
399 XGetIMValues(xim
, XNQueryInputStyle
, &ximStyles
, NULL
);
402 WARN("Could not find supported input style.\n");
408 TRACE("ximStyles->count_styles = %d\n", ximStyles
->count_styles
);
412 ximStyleCallback
= 0;
414 for (i
= 0; i
< ximStyles
->count_styles
; ++i
)
416 int style
= ximStyles
->supported_styles
[i
];
417 TRACE("ximStyles[%d] = %s%s%s%s%s\n", i
,
418 (style
&XIMPreeditArea
)?"XIMPreeditArea ":"",
419 (style
&XIMPreeditCallbacks
)?"XIMPreeditCallbacks ":"",
420 (style
&XIMPreeditPosition
)?"XIMPreeditPosition ":"",
421 (style
&XIMPreeditNothing
)?"XIMPreeditNothing ":"",
422 (style
&XIMPreeditNone
)?"XIMPreeditNone ":"");
423 if (!ximStyle
&& (ximStyles
->supported_styles
[i
] ==
426 ximStyle
= ximStyleRequest
;
427 TRACE("Setting Style: ximStyle = ximStyleRequest\n");
429 else if (!ximStyleRoot
&&(ximStyles
->supported_styles
[i
] ==
432 ximStyleRoot
= STYLE_ROOT
;
433 TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
435 else if (!ximStyleCallback
&&(ximStyles
->supported_styles
[i
] ==
438 ximStyleCallback
= STYLE_CALLBACK
;
439 TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
441 else if (!ximStyleNone
&& (ximStyles
->supported_styles
[i
] ==
444 TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
445 ximStyleNone
= STYLE_NONE
;
451 ximStyle
= ximStyleRoot
;
454 ximStyle
= ximStyleNone
;
456 if (ximStyleCallback
== 0)
458 TRACE("No callback style avalable\n");
459 ximStyleCallback
= ximStyle
;
464 thread_data
->xim
= xim
;
466 if ((ximStyle
& (XIMPreeditNothing
| XIMPreeditNone
)) == 0 ||
467 (ximStyle
& (XIMStatusNothing
| XIMStatusNone
)) == 0)
471 thread_data
->font_set
= XCreateFontSet(display
, "fixed",
472 &list
, &count
, NULL
);
473 TRACE("ximFontSet = %p\n", thread_data
->font_set
);
474 TRACE("list = %p, count = %d\n", list
, count
);
478 for (i
= 0; i
< count
; ++i
)
479 TRACE("list[%d] = %s\n", i
, list
[i
]);
480 XFreeStringList(list
);
484 thread_data
->font_set
= NULL
;
487 IME_UpdateAssociation(NULL
);
492 static void open_xim_callback( Display
*display
, XPointer ptr
, XPointer data
)
494 if (open_xim( display
))
495 XUnregisterIMInstantiateCallback( display
, NULL
, NULL
, NULL
, open_xim_callback
, NULL
);
498 void X11DRV_SetupXIM(void)
500 Display
*display
= thread_display();
503 if (!open_xim( display
))
504 XRegisterIMInstantiateCallback( display
, NULL
, NULL
, NULL
, open_xim_callback
, NULL
);
508 static BOOL
X11DRV_DestroyIC(XIC xic
, XPointer p
, XPointer data
)
510 struct x11drv_win_data
*win_data
= (struct x11drv_win_data
*)p
;
511 TRACE("xic = %p, win = %lx\n", xic
, win_data
->whole_window
);
512 win_data
->xic
= NULL
;
517 XIC
X11DRV_CreateIC(XIM xim
, struct x11drv_win_data
*data
)
520 XVaNestedList preedit
= NULL
;
521 XVaNestedList status
= NULL
;
523 XICCallback destroy
= {(XPointer
)data
, (XICProc
)X11DRV_DestroyIC
};
524 XICCallback P_StartCB
, P_DoneCB
, P_DrawCB
, P_CaretCB
;
525 LANGID langid
= PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale()));
526 Window win
= data
->whole_window
;
527 XFontSet fontSet
= x11drv_thread_data()->font_set
;
529 TRACE("xim = %p\n", xim
);
533 /* use complex and slow XIC initialization method only for CJK */
534 if (langid
!= LANG_CHINESE
&&
535 langid
!= LANG_JAPANESE
&&
536 langid
!= LANG_KOREAN
)
539 XNInputStyle
, XIMPreeditNothing
| XIMStatusNothing
,
542 XNDestroyCallback
, &destroy
,
549 /* create callbacks */
550 P_StartCB
.client_data
= NULL
;
551 P_DoneCB
.client_data
= NULL
;
552 P_DrawCB
.client_data
= NULL
;
553 P_CaretCB
.client_data
= NULL
;
554 P_StartCB
.callback
= (XICProc
)XIMPreEditStartCallback
;
555 P_DoneCB
.callback
= (XICProc
)XIMPreEditDoneCallback
;
556 P_DrawCB
.callback
= (XICProc
)XIMPreEditDrawCallback
;
557 P_CaretCB
.callback
= (XICProc
)XIMPreEditCaretCallback
;
559 if ((ximStyle
& (XIMPreeditNothing
| XIMPreeditNone
)) == 0)
561 preedit
= XVaCreateNestedList(0,
563 XNSpotLocation
, &spot
,
564 XNPreeditStartCallback
, &P_StartCB
,
565 XNPreeditDoneCallback
, &P_DoneCB
,
566 XNPreeditDrawCallback
, &P_DrawCB
,
567 XNPreeditCaretCallback
, &P_CaretCB
,
569 TRACE("preedit = %p\n", preedit
);
573 preedit
= XVaCreateNestedList(0,
574 XNPreeditStartCallback
, &P_StartCB
,
575 XNPreeditDoneCallback
, &P_DoneCB
,
576 XNPreeditDrawCallback
, &P_DrawCB
,
577 XNPreeditCaretCallback
, &P_CaretCB
,
580 TRACE("preedit = %p\n", preedit
);
583 if ((ximStyle
& (XIMStatusNothing
| XIMStatusNone
)) == 0)
585 status
= XVaCreateNestedList(0,
588 TRACE("status = %p\n", status
);
591 if (preedit
!= NULL
&& status
!= NULL
)
594 XNInputStyle
, ximStyle
,
595 XNPreeditAttributes
, preedit
,
596 XNStatusAttributes
, status
,
599 XNDestroyCallback
, &destroy
,
602 else if (preedit
!= NULL
)
605 XNInputStyle
, ximStyle
,
606 XNPreeditAttributes
, preedit
,
609 XNDestroyCallback
, &destroy
,
612 else if (status
!= NULL
)
615 XNInputStyle
, ximStyle
,
616 XNStatusAttributes
, status
,
619 XNDestroyCallback
, &destroy
,
625 XNInputStyle
, ximStyle
,
628 XNDestroyCallback
, &destroy
,
632 TRACE("xic = %p\n", xic
);