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;
48 static LPBYTE ResultString
= NULL
;
49 static DWORD dwResultStringSize
= 0;
51 #define STYLE_OFFTHESPOT (XIMPreeditArea | XIMStatusArea)
52 #define STYLE_OVERTHESPOT (XIMPreeditPosition | XIMStatusNothing)
53 #define STYLE_ROOT (XIMPreeditNothing | XIMStatusNothing)
54 /* this uses all the callbacks to utilize full IME support */
55 #define STYLE_CALLBACK (XIMPreeditCallbacks | XIMStatusNothing)
56 /* inorder to enable deadkey support */
57 #define STYLE_NONE (XIMPreeditNothing | XIMStatusNothing)
59 static XIMStyle ximStyle
= 0;
60 static XIMStyle ximStyleRoot
= 0;
61 static XIMStyle ximStyleRequest
= STYLE_CALLBACK
;
63 static BOOL
X11DRV_ImmSetInternalString(DWORD dwIndex
, DWORD dwOffset
,
64 DWORD selLength
, LPWSTR lpComp
, DWORD dwCompLen
)
66 /* Composition strings are edited in chunks */
67 unsigned int byte_length
= dwCompLen
* sizeof(WCHAR
);
68 unsigned int byte_offset
= dwOffset
* sizeof(WCHAR
);
69 unsigned int byte_selection
= selLength
* sizeof(WCHAR
);
72 TRACE("( %i, %i, %d, %p, %d):\n", dwOffset
, selLength
, dwIndex
, lpComp
, dwCompLen
);
74 if (dwIndex
== GCS_COMPSTR
)
80 if ((dwCompLen
== 0) && (selLength
== 0))
84 /* deletion occurred */
85 else if ((dwCompLen
== 0) && (selLength
!= 0))
87 if (dwCompStringLength
)
89 for (i
= 0; i
< byte_selection
; i
++)
91 if (byte_offset
+byte_selection
+i
<
94 CompositionString
[byte_offset
+ i
] =
95 CompositionString
[byte_offset
+ byte_selection
+ i
];
98 CompositionString
[byte_offset
+ i
] = 0;
100 /* clean up the end */
101 dwCompStringLength
-= byte_selection
;
103 i
= dwCompStringLength
;
104 while (i
< dwCompStringSize
)
106 CompositionString
[i
++] = 0;
112 int byte_expansion
= byte_length
- byte_selection
;
114 if (byte_expansion
+ dwCompStringLength
>= dwCompStringSize
)
116 if (CompositionString
)
118 HeapReAlloc(GetProcessHeap(), 0,
124 HeapAlloc(GetProcessHeap(), 0, dwCompStringSize
+
127 memset(&(CompositionString
[dwCompStringSize
]), 0,
130 dwCompStringSize
+= byte_expansion
;
133 ptr_new
= ((LPBYTE
)lpComp
);
134 ptr_old
= CompositionString
+ byte_offset
+ byte_selection
;
136 dwCompStringLength
+= byte_expansion
;
138 for (j
=0,i
= byte_offset
; i
< dwCompStringSize
; i
++)
142 CompositionString
[i
] = ptr_new
[j
++];
146 if (ptr_old
< CompositionString
+ dwCompStringSize
)
148 CompositionString
[i
] = *ptr_old
;
152 CompositionString
[i
] = 0;
157 rc
= IME_SetCompositionString(SCS_SETSTR
, (LPWSTR
)CompositionString
,
158 dwCompStringLength
, NULL
, 0);
160 else if ((dwIndex
== GCS_RESULTSTR
) && (lpComp
) && (dwCompLen
))
162 if (dwResultStringSize
)
163 HeapFree(GetProcessHeap(),0,ResultString
);
164 dwResultStringSize
= byte_length
;
165 ResultString
= HeapAlloc(GetProcessHeap(),0,byte_length
);
166 memcpy(ResultString
,lpComp
,byte_length
);
168 rc
= IME_SetCompositionString(SCS_SETSTR
, (LPWSTR
)ResultString
,
169 dwResultStringSize
, NULL
, 0);
171 IME_NotifyIME( NI_COMPOSITIONSTR
, CPS_COMPLETE
, 0);
177 void X11DRV_XIMLookupChars( const char *str
, DWORD count
)
183 dwOutput
= MultiByteToWideChar(CP_UNIXCP
, 0, str
, count
, NULL
, 0);
184 wcOutput
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * dwOutput
);
185 if (wcOutput
== NULL
)
187 MultiByteToWideChar(CP_UNIXCP
, 0, str
, count
, wcOutput
, dwOutput
);
189 if ((focus
= GetFocus()))
190 IME_UpdateAssociation(focus
);
192 X11DRV_ImmSetInternalString(GCS_RESULTSTR
,0,0,wcOutput
,dwOutput
);
193 HeapFree(GetProcessHeap(), 0, wcOutput
);
196 static void X11DRV_ImmSetOpenStatus(BOOL fOpen
)
200 if (dwCompStringSize
)
201 HeapFree(GetProcessHeap(),0,CompositionString
);
203 dwCompStringSize
= 0;
204 dwCompStringLength
= 0;
205 CompositionString
= NULL
;
207 if (dwResultStringSize
)
208 HeapFree(GetProcessHeap(),0,ResultString
);
210 dwResultStringSize
= 0;
214 IME_SetOpenStatus(fOpen
);
217 static int XIMPreEditStartCallback(XIC ic
, XPointer client_data
, XPointer call_data
)
219 TRACE("PreEditStartCallback %p\n",ic
);
220 X11DRV_ImmSetOpenStatus(TRUE
);
221 ximInComposeMode
= TRUE
;
225 static void XIMPreEditDoneCallback(XIC ic
, XPointer client_data
, XPointer call_data
)
227 TRACE("PreeditDoneCallback %p\n",ic
);
228 ximInComposeMode
= FALSE
;
229 X11DRV_ImmSetOpenStatus(FALSE
);
232 static void XIMPreEditDrawCallback(XIM ic
, XPointer client_data
,
233 XIMPreeditDrawCallbackStruct
*P_DR
)
235 TRACE("PreEditDrawCallback %p\n",ic
);
239 int sel
= P_DR
->chg_first
;
240 int len
= P_DR
->chg_length
;
243 if (! P_DR
->text
->encoding_is_wchar
)
248 TRACE("multibyte\n");
249 dwOutput
= MultiByteToWideChar(CP_UNIXCP
, 0,
250 P_DR
->text
->string
.multi_byte
, -1,
252 wcOutput
= HeapAlloc(GetProcessHeap(), 0, sizeof (WCHAR
) * dwOutput
);
255 dwOutput
= MultiByteToWideChar(CP_UNIXCP
, 0,
256 P_DR
->text
->string
.multi_byte
, -1,
261 X11DRV_ImmSetInternalString (GCS_COMPSTR
, sel
, len
, wcOutput
, dwOutput
);
262 HeapFree(GetProcessHeap(), 0, wcOutput
);
267 FIXME("wchar PROBIBILY WRONG\n");
268 X11DRV_ImmSetInternalString (GCS_COMPSTR
, sel
, len
,
269 (LPWSTR
)P_DR
->text
->string
.wide_char
,
274 X11DRV_ImmSetInternalString (GCS_COMPSTR
, sel
, len
, NULL
, 0);
275 IME_SetCursorPos(P_DR
->caret
);
280 static void XIMPreEditCaretCallback(XIC ic
, XPointer client_data
,
281 XIMPreeditCaretCallbackStruct
*P_C
)
283 TRACE("PreeditCaretCallback %p\n",ic
);
287 int pos
= IME_GetCursorPos();
288 TRACE("pos: %d\n", pos
);
289 switch(P_C
->direction
)
295 case XIMBackwardChar
:
296 case XIMBackwardWord
:
302 case XIMAbsolutePosition
:
310 case XIMPreviousLine
:
313 FIXME("Not implemented\n");
316 IME_SetCursorPos(pos
);
322 void X11DRV_ForceXIMReset(HWND hwnd
)
324 XIC ic
= X11DRV_get_ic(hwnd
);
328 TRACE("Forcing Reset %p\n",ic
);
330 leftover
= XmbResetIC(ic
);
336 /***********************************************************************
339 * Process-wide XIM initialization.
341 BOOL
X11DRV_InitXIM( const char *input_style
)
345 if (!strcasecmp(input_style
, "offthespot"))
346 ximStyleRequest
= STYLE_OFFTHESPOT
;
347 else if (!strcasecmp(input_style
, "overthespot"))
348 ximStyleRequest
= STYLE_OVERTHESPOT
;
349 else if (!strcasecmp(input_style
, "root"))
350 ximStyleRequest
= STYLE_ROOT
;
353 if (!(ret
= XSupportsLocale()))
355 WARN("X does not support locale.\n");
357 else if (XSetLocaleModifiers("") == NULL
)
359 WARN("Could not set locale modifiers.\n");
367 static void open_xim_callback( Display
*display
, XPointer ptr
, XPointer data
);
369 static void X11DRV_DestroyIM(XIM xim
, XPointer p
, XPointer data
)
371 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
373 TRACE("xim = %p, p = %p\n", xim
, p
);
374 thread_data
->xim
= NULL
;
377 XRegisterIMInstantiateCallback( thread_data
->display
, NULL
, NULL
, NULL
, open_xim_callback
, NULL
);
381 /***********************************************************************
382 * X11DRV Ime creation
384 * Should always be called with the x11 lock held
386 static BOOL
open_xim( Display
*display
)
388 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
389 XIMStyle ximStyleCallback
, ximStyleNone
;
390 XIMStyles
*ximStyles
= NULL
;
395 xim
= XOpenIM(display
, NULL
, NULL
, NULL
);
398 WARN("Could not open input method.\n");
402 destroy
.client_data
= NULL
;
403 destroy
.callback
= X11DRV_DestroyIM
;
404 if (XSetIMValues(xim
, XNDestroyCallback
, &destroy
, NULL
))
406 WARN("Could not set destroy callback.\n");
409 TRACE("xim = %p\n", xim
);
410 TRACE("X display of IM = %p\n", XDisplayOfIM(xim
));
411 TRACE("Using %s locale of Input Method\n", XLocaleOfIM(xim
));
413 XGetIMValues(xim
, XNQueryInputStyle
, &ximStyles
, NULL
);
416 WARN("Could not find supported input style.\n");
422 TRACE("ximStyles->count_styles = %d\n", ximStyles
->count_styles
);
426 ximStyleCallback
= 0;
428 for (i
= 0; i
< ximStyles
->count_styles
; ++i
)
430 int style
= ximStyles
->supported_styles
[i
];
431 TRACE("ximStyles[%d] = %s%s%s%s%s\n", i
,
432 (style
&XIMPreeditArea
)?"XIMPreeditArea ":"",
433 (style
&XIMPreeditCallbacks
)?"XIMPreeditCallbacks ":"",
434 (style
&XIMPreeditPosition
)?"XIMPreeditPosition ":"",
435 (style
&XIMPreeditNothing
)?"XIMPreeditNothing ":"",
436 (style
&XIMPreeditNone
)?"XIMPreeditNone ":"");
437 if (!ximStyle
&& (ximStyles
->supported_styles
[i
] ==
440 ximStyle
= ximStyleRequest
;
441 TRACE("Setting Style: ximStyle = ximStyleRequest\n");
443 else if (!ximStyleRoot
&&(ximStyles
->supported_styles
[i
] ==
446 ximStyleRoot
= STYLE_ROOT
;
447 TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
449 else if (!ximStyleCallback
&&(ximStyles
->supported_styles
[i
] ==
452 ximStyleCallback
= STYLE_CALLBACK
;
453 TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
455 else if (!ximStyleNone
&& (ximStyles
->supported_styles
[i
] ==
458 TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
459 ximStyleNone
= STYLE_NONE
;
465 ximStyle
= ximStyleRoot
;
468 ximStyle
= ximStyleNone
;
470 if (ximStyleCallback
== 0)
472 TRACE("No callback style avalable\n");
473 ximStyleCallback
= ximStyle
;
478 thread_data
->xim
= xim
;
480 if ((ximStyle
& (XIMPreeditNothing
| XIMPreeditNone
)) == 0 ||
481 (ximStyle
& (XIMStatusNothing
| XIMStatusNone
)) == 0)
485 thread_data
->font_set
= XCreateFontSet(display
, "fixed",
486 &list
, &count
, NULL
);
487 TRACE("ximFontSet = %p\n", thread_data
->font_set
);
488 TRACE("list = %p, count = %d\n", list
, count
);
492 for (i
= 0; i
< count
; ++i
)
493 TRACE("list[%d] = %s\n", i
, list
[i
]);
494 XFreeStringList(list
);
498 thread_data
->font_set
= NULL
;
501 IME_UpdateAssociation(NULL
);
506 static void open_xim_callback( Display
*display
, XPointer ptr
, XPointer data
)
508 if (open_xim( display
))
509 XUnregisterIMInstantiateCallback( display
, NULL
, NULL
, NULL
, open_xim_callback
, NULL
);
512 void X11DRV_SetupXIM(void)
514 Display
*display
= thread_display();
517 if (!open_xim( display
))
518 XRegisterIMInstantiateCallback( display
, NULL
, NULL
, NULL
, open_xim_callback
, NULL
);
522 static BOOL
X11DRV_DestroyIC(XIC xic
, XPointer p
, XPointer data
)
524 struct x11drv_win_data
*win_data
= (struct x11drv_win_data
*)p
;
525 TRACE("xic = %p, win = %lx\n", xic
, win_data
->whole_window
);
526 win_data
->xic
= NULL
;
531 XIC
X11DRV_CreateIC(XIM xim
, struct x11drv_win_data
*data
)
534 XVaNestedList preedit
= NULL
;
535 XVaNestedList status
= NULL
;
537 XICCallback destroy
= {(XPointer
)data
, (XICProc
)X11DRV_DestroyIC
};
538 XICCallback P_StartCB
, P_DoneCB
, P_DrawCB
, P_CaretCB
;
539 LANGID langid
= PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale()));
540 Window win
= data
->whole_window
;
541 XFontSet fontSet
= x11drv_thread_data()->font_set
;
543 TRACE("xim = %p\n", xim
);
547 /* use complex and slow XIC initialization method only for CJK */
548 if (langid
!= LANG_CHINESE
&&
549 langid
!= LANG_JAPANESE
&&
550 langid
!= LANG_KOREAN
)
553 XNInputStyle
, XIMPreeditNothing
| XIMStatusNothing
,
556 XNDestroyCallback
, &destroy
,
563 /* create callbacks */
564 P_StartCB
.client_data
= NULL
;
565 P_DoneCB
.client_data
= NULL
;
566 P_DrawCB
.client_data
= NULL
;
567 P_CaretCB
.client_data
= NULL
;
568 P_StartCB
.callback
= (XICProc
)XIMPreEditStartCallback
;
569 P_DoneCB
.callback
= (XICProc
)XIMPreEditDoneCallback
;
570 P_DrawCB
.callback
= (XICProc
)XIMPreEditDrawCallback
;
571 P_CaretCB
.callback
= (XICProc
)XIMPreEditCaretCallback
;
573 if ((ximStyle
& (XIMPreeditNothing
| XIMPreeditNone
)) == 0)
575 preedit
= XVaCreateNestedList(0,
577 XNSpotLocation
, &spot
,
578 XNPreeditStartCallback
, &P_StartCB
,
579 XNPreeditDoneCallback
, &P_DoneCB
,
580 XNPreeditDrawCallback
, &P_DrawCB
,
581 XNPreeditCaretCallback
, &P_CaretCB
,
583 TRACE("preedit = %p\n", preedit
);
587 preedit
= XVaCreateNestedList(0,
588 XNPreeditStartCallback
, &P_StartCB
,
589 XNPreeditDoneCallback
, &P_DoneCB
,
590 XNPreeditDrawCallback
, &P_DrawCB
,
591 XNPreeditCaretCallback
, &P_CaretCB
,
594 TRACE("preedit = %p\n", preedit
);
597 if ((ximStyle
& (XIMStatusNothing
| XIMStatusNone
)) == 0)
599 status
= XVaCreateNestedList(0,
602 TRACE("status = %p\n", status
);
605 if (preedit
!= NULL
&& status
!= NULL
)
608 XNInputStyle
, ximStyle
,
609 XNPreeditAttributes
, preedit
,
610 XNStatusAttributes
, status
,
613 XNDestroyCallback
, &destroy
,
616 else if (preedit
!= NULL
)
619 XNInputStyle
, ximStyle
,
620 XNPreeditAttributes
, preedit
,
623 XNDestroyCallback
, &destroy
,
626 else if (status
!= NULL
)
629 XNInputStyle
, ximStyle
,
630 XNStatusAttributes
, status
,
633 XNDestroyCallback
, &destroy
,
639 XNInputStyle
, ximStyle
,
642 XNDestroyCallback
, &destroy
,
646 TRACE("xic = %p\n", xic
);