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 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
;
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
);
79 ptr_new
= HeapAlloc(GetProcessHeap(), 0,
80 dwCompStringSize
+ byte_expansion
);
84 ERR("Couldn't expand composition string buffer\n");
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
)
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
)
114 MultiByteToWideChar(CP_UNIXCP
, 0, str
, count
, wcOutput
, dwOutput
);
116 if ((focus
= GetFocus()))
117 IME_UpdateAssociation(focus
);
119 IME_SetCompositionString(SCS_SETSTR
, wcOutput
,
120 sizeof (WCHAR
) * dwOutput
, NULL
, 0);
121 IME_NotifyIME(NI_COMPOSITIONSTR
, CPS_COMPLETE
, 0);
122 HeapFree(GetProcessHeap(), 0, wcOutput
);
125 static int XIMPreEditStartCallback(XIC ic
, XPointer client_data
, XPointer call_data
)
127 TRACE("PreEditStartCallback %p\n",ic
);
128 IME_SetOpenStatus(TRUE
);
129 ximInComposeMode
= TRUE
;
133 static void XIMPreEditDoneCallback(XIC ic
, XPointer client_data
, XPointer call_data
)
135 TRACE("PreeditDoneCallback %p\n",ic
);
136 ximInComposeMode
= FALSE
;
137 if (dwCompStringSize
)
138 HeapFree(GetProcessHeap(), 0, CompositionString
);
139 dwCompStringSize
= 0;
140 dwCompStringLength
= 0;
141 CompositionString
= NULL
;
142 IME_SetOpenStatus(FALSE
);
145 static void XIMPreEditDrawCallback(XIM ic
, XPointer client_data
,
146 XIMPreeditDrawCallbackStruct
*P_DR
)
148 TRACE("PreEditDrawCallback %p\n",ic
);
152 int sel
= P_DR
->chg_first
;
153 int len
= P_DR
->chg_length
;
156 if (! P_DR
->text
->encoding_is_wchar
)
161 TRACE("multibyte\n");
162 dwOutput
= MultiByteToWideChar(CP_UNIXCP
, 0,
163 P_DR
->text
->string
.multi_byte
, -1,
165 wcOutput
= HeapAlloc(GetProcessHeap(), 0, sizeof (WCHAR
) * dwOutput
);
168 dwOutput
= MultiByteToWideChar(CP_UNIXCP
, 0,
169 P_DR
->text
->string
.multi_byte
, -1,
174 X11DRV_ImmSetInternalString (sel
, len
, wcOutput
, dwOutput
);
175 HeapFree(GetProcessHeap(), 0, wcOutput
);
180 FIXME("wchar PROBIBILY WRONG\n");
181 X11DRV_ImmSetInternalString (sel
, len
,
182 (LPWSTR
)P_DR
->text
->string
.wide_char
,
187 X11DRV_ImmSetInternalString (sel
, len
, NULL
, 0);
188 IME_SetCursorPos(P_DR
->caret
);
193 static void XIMPreEditCaretCallback(XIC ic
, XPointer client_data
,
194 XIMPreeditCaretCallbackStruct
*P_C
)
196 TRACE("PreeditCaretCallback %p\n",ic
);
200 int pos
= IME_GetCursorPos();
201 TRACE("pos: %d\n", pos
);
202 switch(P_C
->direction
)
208 case XIMBackwardChar
:
209 case XIMBackwardWord
:
215 case XIMAbsolutePosition
:
223 case XIMPreviousLine
:
226 FIXME("Not implemented\n");
229 IME_SetCursorPos(pos
);
235 void X11DRV_ForceXIMReset(HWND hwnd
)
237 XIC ic
= X11DRV_get_ic(hwnd
);
241 TRACE("Forcing Reset %p\n",ic
);
243 leftover
= XmbResetIC(ic
);
249 /***********************************************************************
252 * Process-wide XIM initialization.
254 BOOL
X11DRV_InitXIM( const char *input_style
)
258 if (!strcasecmp(input_style
, "offthespot"))
259 ximStyleRequest
= STYLE_OFFTHESPOT
;
260 else if (!strcasecmp(input_style
, "overthespot"))
261 ximStyleRequest
= STYLE_OVERTHESPOT
;
262 else if (!strcasecmp(input_style
, "root"))
263 ximStyleRequest
= STYLE_ROOT
;
266 if (!(ret
= XSupportsLocale()))
268 WARN("X does not support locale.\n");
270 else if (XSetLocaleModifiers("") == NULL
)
272 WARN("Could not set locale modifiers.\n");
280 static void open_xim_callback( Display
*display
, XPointer ptr
, XPointer data
);
282 static void X11DRV_DestroyIM(XIM xim
, XPointer p
, XPointer data
)
284 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
286 TRACE("xim = %p, p = %p\n", xim
, p
);
287 thread_data
->xim
= NULL
;
290 XRegisterIMInstantiateCallback( thread_data
->display
, NULL
, NULL
, NULL
, open_xim_callback
, NULL
);
294 /***********************************************************************
295 * X11DRV Ime creation
297 * Should always be called with the x11 lock held
299 static BOOL
open_xim( Display
*display
)
301 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
302 XIMStyle ximStyleCallback
, ximStyleNone
;
303 XIMStyles
*ximStyles
= NULL
;
308 xim
= XOpenIM(display
, NULL
, NULL
, NULL
);
311 WARN("Could not open input method.\n");
315 destroy
.client_data
= NULL
;
316 destroy
.callback
= X11DRV_DestroyIM
;
317 if (XSetIMValues(xim
, XNDestroyCallback
, &destroy
, NULL
))
319 WARN("Could not set destroy callback.\n");
322 TRACE("xim = %p\n", xim
);
323 TRACE("X display of IM = %p\n", XDisplayOfIM(xim
));
324 TRACE("Using %s locale of Input Method\n", XLocaleOfIM(xim
));
326 XGetIMValues(xim
, XNQueryInputStyle
, &ximStyles
, NULL
);
329 WARN("Could not find supported input style.\n");
335 TRACE("ximStyles->count_styles = %d\n", ximStyles
->count_styles
);
339 ximStyleCallback
= 0;
341 for (i
= 0; i
< ximStyles
->count_styles
; ++i
)
343 int style
= ximStyles
->supported_styles
[i
];
344 TRACE("ximStyles[%d] = %s%s%s%s%s\n", i
,
345 (style
&XIMPreeditArea
)?"XIMPreeditArea ":"",
346 (style
&XIMPreeditCallbacks
)?"XIMPreeditCallbacks ":"",
347 (style
&XIMPreeditPosition
)?"XIMPreeditPosition ":"",
348 (style
&XIMPreeditNothing
)?"XIMPreeditNothing ":"",
349 (style
&XIMPreeditNone
)?"XIMPreeditNone ":"");
350 if (!ximStyle
&& (ximStyles
->supported_styles
[i
] ==
353 ximStyle
= ximStyleRequest
;
354 TRACE("Setting Style: ximStyle = ximStyleRequest\n");
356 else if (!ximStyleRoot
&&(ximStyles
->supported_styles
[i
] ==
359 ximStyleRoot
= STYLE_ROOT
;
360 TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
362 else if (!ximStyleCallback
&&(ximStyles
->supported_styles
[i
] ==
365 ximStyleCallback
= STYLE_CALLBACK
;
366 TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
368 else if (!ximStyleNone
&& (ximStyles
->supported_styles
[i
] ==
371 TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
372 ximStyleNone
= STYLE_NONE
;
378 ximStyle
= ximStyleRoot
;
381 ximStyle
= ximStyleNone
;
383 if (ximStyleCallback
== 0)
385 TRACE("No callback style avalable\n");
386 ximStyleCallback
= ximStyle
;
391 thread_data
->xim
= xim
;
393 if ((ximStyle
& (XIMPreeditNothing
| XIMPreeditNone
)) == 0 ||
394 (ximStyle
& (XIMStatusNothing
| XIMStatusNone
)) == 0)
398 thread_data
->font_set
= XCreateFontSet(display
, "fixed",
399 &list
, &count
, NULL
);
400 TRACE("ximFontSet = %p\n", thread_data
->font_set
);
401 TRACE("list = %p, count = %d\n", list
, count
);
405 for (i
= 0; i
< count
; ++i
)
406 TRACE("list[%d] = %s\n", i
, list
[i
]);
407 XFreeStringList(list
);
411 thread_data
->font_set
= NULL
;
414 IME_UpdateAssociation(NULL
);
419 static void open_xim_callback( Display
*display
, XPointer ptr
, XPointer data
)
421 if (open_xim( display
))
422 XUnregisterIMInstantiateCallback( display
, NULL
, NULL
, NULL
, open_xim_callback
, NULL
);
425 void X11DRV_SetupXIM(void)
427 Display
*display
= thread_display();
430 if (!open_xim( display
))
431 XRegisterIMInstantiateCallback( display
, NULL
, NULL
, NULL
, open_xim_callback
, NULL
);
435 static BOOL
X11DRV_DestroyIC(XIC xic
, XPointer p
, XPointer data
)
437 struct x11drv_win_data
*win_data
= (struct x11drv_win_data
*)p
;
438 TRACE("xic = %p, win = %lx\n", xic
, win_data
->whole_window
);
439 win_data
->xic
= NULL
;
444 XIC
X11DRV_CreateIC(XIM xim
, struct x11drv_win_data
*data
)
447 XVaNestedList preedit
= NULL
;
448 XVaNestedList status
= NULL
;
450 XICCallback destroy
= {(XPointer
)data
, (XICProc
)X11DRV_DestroyIC
};
451 XICCallback P_StartCB
, P_DoneCB
, P_DrawCB
, P_CaretCB
;
452 LANGID langid
= PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale()));
453 Window win
= data
->whole_window
;
454 XFontSet fontSet
= x11drv_thread_data()->font_set
;
456 TRACE("xim = %p\n", xim
);
460 /* use complex and slow XIC initialization method only for CJK */
461 if (langid
!= LANG_CHINESE
&&
462 langid
!= LANG_JAPANESE
&&
463 langid
!= LANG_KOREAN
)
466 XNInputStyle
, XIMPreeditNothing
| XIMStatusNothing
,
469 XNDestroyCallback
, &destroy
,
476 /* create callbacks */
477 P_StartCB
.client_data
= NULL
;
478 P_DoneCB
.client_data
= NULL
;
479 P_DrawCB
.client_data
= NULL
;
480 P_CaretCB
.client_data
= NULL
;
481 P_StartCB
.callback
= (XICProc
)XIMPreEditStartCallback
;
482 P_DoneCB
.callback
= (XICProc
)XIMPreEditDoneCallback
;
483 P_DrawCB
.callback
= (XICProc
)XIMPreEditDrawCallback
;
484 P_CaretCB
.callback
= (XICProc
)XIMPreEditCaretCallback
;
486 if ((ximStyle
& (XIMPreeditNothing
| XIMPreeditNone
)) == 0)
488 preedit
= XVaCreateNestedList(0,
490 XNSpotLocation
, &spot
,
491 XNPreeditStartCallback
, &P_StartCB
,
492 XNPreeditDoneCallback
, &P_DoneCB
,
493 XNPreeditDrawCallback
, &P_DrawCB
,
494 XNPreeditCaretCallback
, &P_CaretCB
,
496 TRACE("preedit = %p\n", preedit
);
500 preedit
= XVaCreateNestedList(0,
501 XNPreeditStartCallback
, &P_StartCB
,
502 XNPreeditDoneCallback
, &P_DoneCB
,
503 XNPreeditDrawCallback
, &P_DrawCB
,
504 XNPreeditCaretCallback
, &P_CaretCB
,
507 TRACE("preedit = %p\n", preedit
);
510 if ((ximStyle
& (XIMStatusNothing
| XIMStatusNone
)) == 0)
512 status
= XVaCreateNestedList(0,
515 TRACE("status = %p\n", status
);
518 if (preedit
!= NULL
&& status
!= NULL
)
521 XNInputStyle
, ximStyle
,
522 XNPreeditAttributes
, preedit
,
523 XNStatusAttributes
, status
,
526 XNDestroyCallback
, &destroy
,
529 else if (preedit
!= NULL
)
532 XNInputStyle
, ximStyle
,
533 XNPreeditAttributes
, preedit
,
536 XNDestroyCallback
, &destroy
,
539 else if (status
!= NULL
)
542 XNInputStyle
, ximStyle
,
543 XNStatusAttributes
, status
,
546 XNDestroyCallback
, &destroy
,
552 XNInputStyle
, ximStyle
,
555 XNDestroyCallback
, &destroy
,
559 TRACE("xic = %p\n", xic
);