msvcrt: Added _swscanf_l implementation.
[wine] / dlls / winex11.drv / xim.c
1 /*
2  * Functions for further XIM control
3  *
4  * Copyright 2003 CodeWeavers, Aric Stewart
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25
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"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(xim);
36
37 #ifndef HAVE_XICCALLBACK_CALLBACK
38 #define XICCallback XIMCallback
39 #define XICProc XIMProc
40 #endif
41
42 BOOL ximInComposeMode=FALSE;
43
44 /* moved here from imm32 for dll separation */
45 static DWORD dwCompStringLength = 0;
46 static LPBYTE CompositionString = NULL;
47 static DWORD dwCompStringSize = 0;
48
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)
56
57 static XIMStyle ximStyle = 0;
58 static XIMStyle ximStyleRoot = 0;
59 static XIMStyle ximStyleRequest = STYLE_CALLBACK;
60
61 static void X11DRV_ImmSetInternalString(DWORD dwOffset,
62                                         DWORD selLength, LPWSTR lpComp, DWORD dwCompLen)
63 {
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;
70
71     TRACE("( %i, %i, %p, %d):\n", dwOffset, selLength, lpComp, dwCompLen );
72
73     if (byte_expansion + dwCompStringLength >= dwCompStringSize)
74     {
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);
81
82         if (ptr_new == NULL)
83         {
84             ERR("Couldn't expand composition string buffer\n");
85             return;
86         }
87
88         CompositionString = ptr_new;
89         dwCompStringSize += byte_expansion;
90     }
91
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;
97
98     IME_SetCompositionString(SCS_SETSTR, CompositionString,
99                              dwCompStringLength, NULL, 0);
100 }
101
102 void X11DRV_XIMLookupChars( const char *str, DWORD count )
103 {
104     DWORD dwOutput;
105     WCHAR *wcOutput;
106     HWND focus;
107
108     TRACE("%p %u\n", str, count);
109
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);
115
116     if ((focus = GetFocus()))
117         IME_UpdateAssociation(focus);
118
119     IME_SetResultString(wcOutput, dwOutput);
120     HeapFree(GetProcessHeap(), 0, wcOutput);
121 }
122
123 static BOOL XIMPreEditStateNotifyCallback(XIC xic, XPointer p, XPointer data)
124 {
125     const struct x11drv_win_data * const win_data = (struct x11drv_win_data *)p;
126     const XIMPreeditState state = ((XIMPreeditStateNotifyCallbackStruct *)data)->state;
127
128     TRACE("xic = %p, win = %lx, state = %lu\n", xic, win_data->whole_window, state);
129     switch (state)
130     {
131     case XIMPreeditEnable:
132         IME_SetOpenStatus(TRUE, TRUE);
133         break;
134     case XIMPreeditDisable:
135         IME_SetOpenStatus(FALSE, TRUE);
136         break;
137     default:
138         break;
139     }
140     return TRUE;
141 }
142
143 static int XIMPreEditStartCallback(XIC ic, XPointer client_data, XPointer call_data)
144 {
145     TRACE("PreEditStartCallback %p\n",ic);
146     IME_SetOpenStatus(TRUE, FALSE);
147     ximInComposeMode = TRUE;
148     return -1;
149 }
150
151 static void XIMPreEditDoneCallback(XIC ic, XPointer client_data, XPointer call_data)
152 {
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_SetOpenStatus(FALSE, FALSE);
161 }
162
163 static void XIMPreEditDrawCallback(XIM ic, XPointer client_data,
164                                    XIMPreeditDrawCallbackStruct *P_DR)
165 {
166     TRACE("PreEditDrawCallback %p\n",ic);
167
168     if (P_DR)
169     {
170         int sel = P_DR->chg_first;
171         int len = P_DR->chg_length;
172         if (P_DR->text)
173         {
174             if (! P_DR->text->encoding_is_wchar)
175             {
176                 DWORD dwOutput;
177                 WCHAR *wcOutput;
178
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)
185                 {
186                     dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
187                                P_DR->text->string.multi_byte, -1,
188                                wcOutput, dwOutput);
189
190                     /* ignore null */
191                     dwOutput --;
192                     X11DRV_ImmSetInternalString (sel, len, wcOutput, dwOutput);
193                     HeapFree(GetProcessHeap(), 0, wcOutput);
194                 }
195             }
196             else
197             {
198                 FIXME("wchar PROBIBILY WRONG\n");
199                 X11DRV_ImmSetInternalString (sel, len,
200                                              (LPWSTR)P_DR->text->string.wide_char,
201                                              P_DR->text->length);
202             }
203         }
204         else
205             X11DRV_ImmSetInternalString (sel, len, NULL, 0);
206         IME_SetCursorPos(P_DR->caret);
207     }
208     TRACE("Finished\n");
209 }
210
211 static void XIMPreEditCaretCallback(XIC ic, XPointer client_data,
212                                     XIMPreeditCaretCallbackStruct *P_C)
213 {
214     TRACE("PreeditCaretCallback %p\n",ic);
215
216     if (P_C)
217     {
218         int pos = IME_GetCursorPos();
219         TRACE("pos: %d\n", pos);
220         switch(P_C->direction)
221         {
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;
246         }
247         IME_SetCursorPos(pos);
248         P_C->position = pos;
249     }
250     TRACE("Finished\n");
251 }
252
253 void X11DRV_ForceXIMReset(HWND hwnd)
254 {
255     XIC ic = X11DRV_get_ic(hwnd);
256     if (ic)
257     {
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();
264     }
265 }
266
267 BOOL X11DRV_SetPreeditState(HWND hwnd, BOOL fOpen)
268 {
269     XIC ic;
270     XIMPreeditState state;
271     XVaNestedList attr_set, attr_get;
272     BOOL ret;
273
274     ic = X11DRV_get_ic(hwnd);
275     if (!ic)
276         return FALSE;
277
278     if (fOpen)
279         state = XIMPreeditEnable;
280     else
281         state = XIMPreeditDisable;
282
283     ret = FALSE;
284     wine_tsx11_lock();
285
286     attr_set = XVaCreateNestedList(0, XNPreeditState, state, NULL);
287     if (attr_set == NULL)
288         goto error1;
289
290     attr_get = XVaCreateNestedList(0, XNPreeditState, &state, NULL);
291     if (attr_get == NULL)
292         goto error2;
293
294     if (XSetICValues(ic, XNPreeditAttributes, attr_set, NULL) != NULL)
295         goto error3;
296
297     /* SCIM claims it supports XNPreeditState, but seems to ignore */
298     state = XIMPreeditUnKnown;
299     ret = XGetICValues(ic, XNPreeditAttributes, attr_get, NULL) == NULL &&
300           ((fOpen && state == XIMPreeditEnable) ||
301            (!fOpen && state == XIMPreeditDisable));
302 error3:
303     XFree(attr_get);
304 error2:
305     XFree(attr_set);
306 error1:
307     wine_tsx11_unlock();
308     return ret;
309 }
310
311
312 /***********************************************************************
313  *           X11DRV_InitXIM
314  *
315  * Process-wide XIM initialization.
316  */
317 BOOL X11DRV_InitXIM( const char *input_style )
318 {
319     BOOL ret;
320
321     if (!strcasecmp(input_style, "offthespot"))
322         ximStyleRequest = STYLE_OFFTHESPOT;
323     else if (!strcasecmp(input_style, "overthespot"))
324         ximStyleRequest = STYLE_OVERTHESPOT;
325     else if (!strcasecmp(input_style, "root"))
326         ximStyleRequest = STYLE_ROOT;
327
328     wine_tsx11_lock();
329     if (!(ret = XSupportsLocale()))
330     {
331         WARN("X does not support locale.\n");
332     }
333     else if (XSetLocaleModifiers("") == NULL)
334     {
335         WARN("Could not set locale modifiers.\n");
336         ret = FALSE;
337     }
338     wine_tsx11_unlock();
339     return ret;
340 }
341
342
343 static void open_xim_callback( Display *display, XPointer ptr, XPointer data );
344
345 static void X11DRV_DestroyIM(XIM xim, XPointer p, XPointer data)
346 {
347     struct x11drv_thread_data *thread_data = x11drv_thread_data();
348
349     TRACE("xim = %p, p = %p\n", xim, p);
350     thread_data->xim = NULL;
351     ximStyle = 0;
352     wine_tsx11_lock();
353     XRegisterIMInstantiateCallback( thread_data->display, NULL, NULL, NULL, open_xim_callback, NULL );
354     wine_tsx11_unlock();
355 }
356
357 /***********************************************************************
358  *           X11DRV Ime creation
359  *
360  * Should always be called with the x11 lock held
361  */
362 static BOOL open_xim( Display *display )
363 {
364     struct x11drv_thread_data *thread_data = x11drv_thread_data();
365     XIMStyle ximStyleCallback, ximStyleNone;
366     XIMStyles *ximStyles = NULL;
367     INT i;
368     XIM xim;
369     XIMCallback destroy;
370
371     xim = XOpenIM(display, NULL, NULL, NULL);
372     if (xim == NULL)
373     {
374         WARN("Could not open input method.\n");
375         return FALSE;
376     }
377
378     destroy.client_data = NULL;
379     destroy.callback = X11DRV_DestroyIM;
380     if (XSetIMValues(xim, XNDestroyCallback, &destroy, NULL))
381     {
382         WARN("Could not set destroy callback.\n");
383     }
384
385     TRACE("xim = %p\n", xim);
386     TRACE("X display of IM = %p\n", XDisplayOfIM(xim));
387     TRACE("Using %s locale of Input Method\n", XLocaleOfIM(xim));
388
389     XGetIMValues(xim, XNQueryInputStyle, &ximStyles, NULL);
390     if (ximStyles == 0)
391     {
392         WARN("Could not find supported input style.\n");
393         XCloseIM(xim);
394         return FALSE;
395     }
396     else
397     {
398         TRACE("ximStyles->count_styles = %d\n", ximStyles->count_styles);
399
400         ximStyleRoot = 0;
401         ximStyleNone = 0;
402         ximStyleCallback = 0;
403
404         for (i = 0; i < ximStyles->count_styles; ++i)
405         {
406             int style = ximStyles->supported_styles[i];
407             TRACE("ximStyles[%d] = %s%s%s%s%s\n", i,
408                         (style&XIMPreeditArea)?"XIMPreeditArea ":"",
409                         (style&XIMPreeditCallbacks)?"XIMPreeditCallbacks ":"",
410                         (style&XIMPreeditPosition)?"XIMPreeditPosition ":"",
411                         (style&XIMPreeditNothing)?"XIMPreeditNothing ":"",
412                         (style&XIMPreeditNone)?"XIMPreeditNone ":"");
413             if (!ximStyle && (ximStyles->supported_styles[i] ==
414                                 ximStyleRequest))
415             {
416                 ximStyle = ximStyleRequest;
417                 TRACE("Setting Style: ximStyle = ximStyleRequest\n");
418             }
419             else if (!ximStyleRoot &&(ximStyles->supported_styles[i] ==
420                      STYLE_ROOT))
421             {
422                 ximStyleRoot = STYLE_ROOT;
423                 TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
424             }
425             else if (!ximStyleCallback &&(ximStyles->supported_styles[i] ==
426                      STYLE_CALLBACK))
427             {
428                 ximStyleCallback = STYLE_CALLBACK;
429                 TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
430             }
431             else if (!ximStyleNone && (ximStyles->supported_styles[i] ==
432                      STYLE_NONE))
433             {
434                 TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
435                 ximStyleNone = STYLE_NONE;
436             }
437         }
438         XFree(ximStyles);
439
440         if (ximStyle == 0)
441             ximStyle = ximStyleRoot;
442
443         if (ximStyle == 0)
444             ximStyle = ximStyleNone;
445
446         if (ximStyleCallback == 0)
447         {
448             TRACE("No callback style avalable\n");
449             ximStyleCallback = ximStyle;
450         }
451
452     }
453
454     thread_data->xim = xim;
455
456     if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0 ||
457         (ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
458     {
459         char **list;
460         int count;
461         thread_data->font_set = XCreateFontSet(display, "fixed",
462                           &list, &count, NULL);
463         TRACE("ximFontSet = %p\n", thread_data->font_set);
464         TRACE("list = %p, count = %d\n", list, count);
465         if (list != NULL)
466         {
467             int i;
468             for (i = 0; i < count; ++i)
469                 TRACE("list[%d] = %s\n", i, list[i]);
470             XFreeStringList(list);
471         }
472     }
473     else
474         thread_data->font_set = NULL;
475
476     wine_tsx11_unlock();
477     IME_UpdateAssociation(NULL);
478     wine_tsx11_lock();
479     return TRUE;
480 }
481
482 static void open_xim_callback( Display *display, XPointer ptr, XPointer data )
483 {
484     if (open_xim( display ))
485         XUnregisterIMInstantiateCallback( display, NULL, NULL, NULL, open_xim_callback, NULL);
486 }
487
488 void X11DRV_SetupXIM(void)
489 {
490     Display *display = thread_display();
491
492     wine_tsx11_lock();
493     if (!open_xim( display ))
494         XRegisterIMInstantiateCallback( display, NULL, NULL, NULL, open_xim_callback, NULL );
495     wine_tsx11_unlock();
496 }
497
498 static BOOL X11DRV_DestroyIC(XIC xic, XPointer p, XPointer data)
499 {
500     struct x11drv_win_data *win_data = (struct x11drv_win_data *)p;
501     TRACE("xic = %p, win = %lx\n", xic, win_data->whole_window);
502     win_data->xic = NULL;
503     return TRUE;
504 }
505
506
507 XIC X11DRV_CreateIC(XIM xim, struct x11drv_win_data *data)
508 {
509     XPoint spot = {0};
510     XVaNestedList preedit = NULL;
511     XVaNestedList status = NULL;
512     XIC xic;
513     XICCallback destroy = {(XPointer)data, (XICProc)X11DRV_DestroyIC};
514     XICCallback P_StateNotifyCB, P_StartCB, P_DoneCB, P_DrawCB, P_CaretCB;
515     LANGID langid = PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale()));
516     Window win = data->whole_window;
517     XFontSet fontSet = x11drv_thread_data()->font_set;
518
519     TRACE("xim = %p\n", xim);
520
521     wine_tsx11_lock();
522
523     /* use complex and slow XIC initialization method only for CJK */
524     if (langid != LANG_CHINESE &&
525         langid != LANG_JAPANESE &&
526         langid != LANG_KOREAN)
527     {
528         xic = XCreateIC(xim,
529                         XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
530                         XNClientWindow, win,
531                         XNFocusWindow, win,
532                         XNDestroyCallback, &destroy,
533                         NULL);
534         wine_tsx11_unlock();
535         data->xic = xic;
536         return xic;
537     }
538
539     /* create callbacks */
540     P_StateNotifyCB.client_data = (XPointer)data;
541     P_StartCB.client_data = NULL;
542     P_DoneCB.client_data = NULL;
543     P_DrawCB.client_data = NULL;
544     P_CaretCB.client_data = NULL;
545     P_StateNotifyCB.callback = (XICProc)XIMPreEditStateNotifyCallback;
546     P_StartCB.callback = (XICProc)XIMPreEditStartCallback;
547     P_DoneCB.callback = (XICProc)XIMPreEditDoneCallback;
548     P_DrawCB.callback = (XICProc)XIMPreEditDrawCallback;
549     P_CaretCB.callback = (XICProc)XIMPreEditCaretCallback;
550
551     if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0)
552     {
553         preedit = XVaCreateNestedList(0,
554                         XNFontSet, fontSet,
555                         XNSpotLocation, &spot,
556                         XNPreeditStateNotifyCallback, &P_StateNotifyCB,
557                         XNPreeditStartCallback, &P_StartCB,
558                         XNPreeditDoneCallback, &P_DoneCB,
559                         XNPreeditDrawCallback, &P_DrawCB,
560                         XNPreeditCaretCallback, &P_CaretCB,
561                         NULL);
562         TRACE("preedit = %p\n", preedit);
563     }
564     else
565     {
566         preedit = XVaCreateNestedList(0,
567                         XNPreeditStateNotifyCallback, &P_StateNotifyCB,
568                         XNPreeditStartCallback, &P_StartCB,
569                         XNPreeditDoneCallback, &P_DoneCB,
570                         XNPreeditDrawCallback, &P_DrawCB,
571                         XNPreeditCaretCallback, &P_CaretCB,
572                         NULL);
573
574         TRACE("preedit = %p\n", preedit);
575     }
576
577     if ((ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
578     {
579         status = XVaCreateNestedList(0,
580             XNFontSet, fontSet,
581             NULL);
582         TRACE("status = %p\n", status);
583      }
584
585     if (preedit != NULL && status != NULL)
586     {
587         xic = XCreateIC(xim,
588               XNInputStyle, ximStyle,
589               XNPreeditAttributes, preedit,
590               XNStatusAttributes, status,
591               XNClientWindow, win,
592               XNFocusWindow, win,
593               XNDestroyCallback, &destroy,
594               NULL);
595      }
596     else if (preedit != NULL)
597     {
598         xic = XCreateIC(xim,
599               XNInputStyle, ximStyle,
600               XNPreeditAttributes, preedit,
601               XNClientWindow, win,
602               XNFocusWindow, win,
603               XNDestroyCallback, &destroy,
604               NULL);
605     }
606     else if (status != NULL)
607     {
608         xic = XCreateIC(xim,
609               XNInputStyle, ximStyle,
610               XNStatusAttributes, status,
611               XNClientWindow, win,
612               XNFocusWindow, win,
613               XNDestroyCallback, &destroy,
614               NULL);
615     }
616     else
617     {
618         xic = XCreateIC(xim,
619               XNInputStyle, ximStyle,
620               XNClientWindow, win,
621               XNFocusWindow, win,
622               XNDestroyCallback, &destroy,
623               NULL);
624     }
625
626     TRACE("xic = %p\n", xic);
627     data->xic = xic;
628
629     if (preedit != NULL)
630         XFree(preedit);
631     if (status != NULL)
632         XFree(status);
633
634     wine_tsx11_unlock();
635
636     return xic;
637 }