wined3d: Move an extension check into the state template.
[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(x11drv);
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 static LPBYTE ResultString = NULL;
49 static DWORD dwResultStringSize = 0;
50
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)
58
59 static XIMStyle ximStyle = 0;
60 static XIMStyle ximStyleRoot = 0;
61 static XIMStyle ximStyleRequest = STYLE_CALLBACK;
62
63 static BOOL X11DRV_ImmSetInternalString(DWORD dwIndex, DWORD dwOffset,
64                                         DWORD selLength, LPWSTR lpComp, DWORD dwCompLen)
65 {
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);
70     BOOL rc = FALSE;
71
72     TRACE("( %i, %i, %d, %p, %d):\n", dwOffset, selLength, dwIndex, lpComp, dwCompLen );
73
74     if (dwIndex == GCS_COMPSTR)
75     {
76         unsigned int i,j;
77         LPBYTE ptr_new;
78         LPBYTE ptr_old;
79
80         if ((dwCompLen == 0) && (selLength == 0))
81         {
82             /* DO Nothing */
83         }
84         /* deletion occurred */
85         else if ((dwCompLen== 0) && (selLength != 0))
86         {
87             if (dwCompStringLength)
88             {
89                 for (i = 0; i < byte_selection; i++)
90                 {
91                     if (byte_offset+byte_selection+i <
92                         dwCompStringLength)
93                     {
94                         CompositionString[byte_offset + i] =
95                         CompositionString[byte_offset + byte_selection + i];
96                     }
97                     else
98                         CompositionString[byte_offset + i] = 0;
99                 }
100                 /* clean up the end */
101                 dwCompStringLength -= byte_selection;
102
103                 i = dwCompStringLength;
104                 while (i < dwCompStringSize)
105                 {
106                     CompositionString[i++] = 0;
107                 }
108             }
109         }
110         else
111         {
112             int byte_expansion = byte_length - byte_selection;
113
114             if (byte_expansion + dwCompStringLength >= dwCompStringSize)
115             {
116                 if (CompositionString)
117                     CompositionString =
118                         HeapReAlloc(GetProcessHeap(), 0,
119                                     CompositionString,
120                                     dwCompStringSize +
121                                     byte_expansion);
122                 else
123                      CompositionString =
124                         HeapAlloc(GetProcessHeap(), 0, dwCompStringSize +
125                                     byte_expansion);
126
127                 memset(&(CompositionString[dwCompStringSize]), 0,
128                         byte_expansion);
129
130                 dwCompStringSize += byte_expansion;
131             }
132
133             ptr_new =  ((LPBYTE)lpComp);
134             ptr_old = CompositionString + byte_offset + byte_selection;
135
136             dwCompStringLength += byte_expansion;
137
138             for (j=0,i = byte_offset; i < dwCompStringSize; i++)
139             {
140                 if (j < byte_length)
141                 {
142                     CompositionString[i] = ptr_new[j++];
143                 }
144                 else
145                 {
146                     if (ptr_old < CompositionString + dwCompStringSize)
147                     {
148                         CompositionString[i] = *ptr_old;
149                         ptr_old++;
150                             }
151                     else
152                         CompositionString[i] = 0;
153                 }
154             }
155         }
156
157         rc = IME_SetCompositionString(SCS_SETSTR, (LPWSTR)CompositionString,
158                                       dwCompStringLength, NULL, 0);
159     }
160     else if ((dwIndex == GCS_RESULTSTR) && (lpComp) && (dwCompLen))
161     {
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);
167
168         rc = IME_SetCompositionString(SCS_SETSTR, (LPWSTR)ResultString,
169                                      dwResultStringSize, NULL, 0);
170
171         IME_NotifyIME( NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
172     }
173
174     return rc;
175 }
176
177 void X11DRV_XIMLookupChars( const char *str, DWORD count )
178 {
179     DWORD dwOutput;
180     WCHAR wcOutput[64];
181     HWND focus;
182
183     dwOutput = MultiByteToWideChar(CP_UNIXCP, 0, str, count, wcOutput, sizeof(wcOutput)/sizeof(WCHAR));
184
185     if ((focus = GetFocus()))
186         IME_UpdateAssociation(focus);
187
188     X11DRV_ImmSetInternalString(GCS_RESULTSTR,0,0,wcOutput,dwOutput);
189 }
190
191 static void X11DRV_ImmSetOpenStatus(BOOL fOpen)
192 {
193     if (fOpen == FALSE)
194     {
195         if (dwCompStringSize)
196             HeapFree(GetProcessHeap(),0,CompositionString);
197
198         dwCompStringSize = 0;
199         dwCompStringLength = 0;
200         CompositionString = NULL;
201
202         if (dwResultStringSize)
203             HeapFree(GetProcessHeap(),0,ResultString);
204
205         dwResultStringSize = 0;
206         ResultString = NULL;
207     }
208
209     IME_SetOpenStatus(fOpen);
210 }
211
212 static int XIMPreEditStartCallback(XIC ic, XPointer client_data, XPointer call_data)
213 {
214     TRACE("PreEditStartCallback %p\n",ic);
215     X11DRV_ImmSetOpenStatus(TRUE);
216     ximInComposeMode = TRUE;
217     return -1;
218 }
219
220 static void XIMPreEditDoneCallback(XIC ic, XPointer client_data, XPointer call_data)
221 {
222     TRACE("PreeditDoneCallback %p\n",ic);
223     ximInComposeMode = FALSE;
224     X11DRV_ImmSetOpenStatus(FALSE);
225 }
226
227 static void XIMPreEditDrawCallback(XIM ic, XPointer client_data,
228                                    XIMPreeditDrawCallbackStruct *P_DR)
229 {
230     TRACE("PreEditDrawCallback %p\n",ic);
231
232     if (P_DR)
233     {
234         int sel = P_DR->chg_first;
235         int len = P_DR->chg_length;
236         if (P_DR->text)
237         {
238             if (! P_DR->text->encoding_is_wchar)
239             {
240                 DWORD dwOutput;
241                 WCHAR *wcOutput;
242
243                 TRACE("multibyte\n");
244                 dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
245                            P_DR->text->string.multi_byte, -1,
246                            NULL, 0);
247                 wcOutput = HeapAlloc(GetProcessHeap(), 0, sizeof (WCHAR) * dwOutput);
248                 if (wcOutput)
249                 {
250                     dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
251                                P_DR->text->string.multi_byte, -1,
252                                wcOutput, dwOutput);
253
254                     /* ignore null */
255                     dwOutput --;
256                     X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, wcOutput, dwOutput);
257                     HeapFree(GetProcessHeap(), 0, wcOutput);
258                 }
259             }
260             else
261             {
262                 FIXME("wchar PROBIBILY WRONG\n");
263                 X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len,
264                                              (LPWSTR)P_DR->text->string.wide_char,
265                                              P_DR->text->length);
266             }
267         }
268         else
269             X11DRV_ImmSetInternalString (GCS_COMPSTR, sel, len, NULL, 0);
270         IME_SetCursorPos(P_DR->caret);
271     }
272     TRACE("Finished\n");
273 }
274
275 static void XIMPreEditCaretCallback(XIC ic, XPointer client_data,
276                                     XIMPreeditCaretCallbackStruct *P_C)
277 {
278     TRACE("PreeditCaretCallback %p\n",ic);
279
280     if (P_C)
281     {
282         int pos = IME_GetCursorPos();
283         TRACE("pos: %d\n", pos);
284         switch(P_C->direction)
285         {
286             case XIMForwardChar:
287             case XIMForwardWord:
288                 pos++;
289                 break;
290             case XIMBackwardChar:
291             case XIMBackwardWord:
292                 pos--;
293                 break;
294             case XIMLineStart:
295                 pos = 0;
296                 break;
297             case XIMAbsolutePosition:
298                 pos = P_C->position;
299                 break;
300             case XIMDontChange:
301                 P_C->position = pos;
302                 return;
303             case XIMCaretUp:
304             case XIMCaretDown:
305             case XIMPreviousLine:
306             case XIMNextLine:
307             case XIMLineEnd:
308                 FIXME("Not implemented\n");
309                 break;
310         }
311         IME_SetCursorPos(pos);
312         P_C->position = pos;
313     }
314     TRACE("Finished\n");
315 }
316
317 void X11DRV_ForceXIMReset(HWND hwnd)
318 {
319     XIC ic = X11DRV_get_ic(hwnd);
320     if (ic)
321     {
322         char* leftover;
323         TRACE("Forcing Reset %p\n",ic);
324         wine_tsx11_lock();
325         leftover = XmbResetIC(ic);
326         XFree(leftover);
327         wine_tsx11_unlock();
328     }
329 }
330
331 /***********************************************************************
332  *           X11DRV_InitXIM
333  *
334  * Process-wide XIM initialization.
335  */
336 BOOL X11DRV_InitXIM( const char *input_style )
337 {
338     BOOL ret;
339
340     if (!strcasecmp(input_style, "offthespot"))
341         ximStyleRequest = STYLE_OFFTHESPOT;
342     else if (!strcasecmp(input_style, "overthespot"))
343         ximStyleRequest = STYLE_OVERTHESPOT;
344     else if (!strcasecmp(input_style, "root"))
345         ximStyleRequest = STYLE_ROOT;
346
347     wine_tsx11_lock();
348     if (!(ret = XSupportsLocale()))
349     {
350         WARN("X does not support locale.\n");
351     }
352     else if (XSetLocaleModifiers("") == NULL)
353     {
354         WARN("Could not set locale modifiers.\n");
355         ret = FALSE;
356     }
357     wine_tsx11_unlock();
358     return ret;
359 }
360
361
362 static void open_xim_callback( Display *display, XPointer ptr, XPointer data );
363
364 static void X11DRV_DestroyIM(XIM xim, XPointer p, XPointer data)
365 {
366     struct x11drv_thread_data *thread_data = x11drv_thread_data();
367
368     TRACE("xim = %p, p = %p\n", xim, p);
369     thread_data->xim = NULL;
370     ximStyle = 0;
371     wine_tsx11_lock();
372     XRegisterIMInstantiateCallback( thread_data->display, NULL, NULL, NULL, open_xim_callback, NULL );
373     wine_tsx11_unlock();
374 }
375
376 /***********************************************************************
377  *           X11DRV Ime creation
378  *
379  * Should always be called with the x11 lock held
380  */
381 static BOOL open_xim( Display *display )
382 {
383     struct x11drv_thread_data *thread_data = x11drv_thread_data();
384     XIMStyle ximStyleCallback, ximStyleNone;
385     XIMStyles *ximStyles = NULL;
386     INT i;
387     XIM xim;
388     XIMCallback destroy;
389
390     xim = XOpenIM(display, NULL, NULL, NULL);
391     if (xim == NULL)
392     {
393         WARN("Could not open input method.\n");
394         return FALSE;
395     }
396
397     destroy.client_data = NULL;
398     destroy.callback = X11DRV_DestroyIM;
399     if (XSetIMValues(xim, XNDestroyCallback, &destroy, NULL))
400     {
401         WARN("Could not set destroy callback.\n");
402     }
403
404     TRACE("xim = %p\n", xim);
405     TRACE("X display of IM = %p\n", XDisplayOfIM(xim));
406     TRACE("Using %s locale of Input Method\n", XLocaleOfIM(xim));
407
408     XGetIMValues(xim, XNQueryInputStyle, &ximStyles, NULL);
409     if (ximStyles == 0)
410     {
411         WARN("Could not find supported input style.\n");
412         XCloseIM(xim);
413         return FALSE;
414     }
415     else
416     {
417         TRACE("ximStyles->count_styles = %d\n", ximStyles->count_styles);
418
419         ximStyleRoot = 0;
420         ximStyleNone = 0;
421         ximStyleCallback = 0;
422
423         for (i = 0; i < ximStyles->count_styles; ++i)
424         {
425             int style = ximStyles->supported_styles[i];
426             TRACE("ximStyles[%d] = %s%s%s%s%s\n", i,
427                         (style&XIMPreeditArea)?"XIMPreeditArea ":"",
428                         (style&XIMPreeditCallbacks)?"XIMPreeditCallbacks ":"",
429                         (style&XIMPreeditPosition)?"XIMPreeditPosition ":"",
430                         (style&XIMPreeditNothing)?"XIMPreeditNothing ":"",
431                         (style&XIMPreeditNone)?"XIMPreeditNone ":"");
432             if (!ximStyle && (ximStyles->supported_styles[i] ==
433                                 ximStyleRequest))
434             {
435                 ximStyle = ximStyleRequest;
436                 TRACE("Setting Style: ximStyle = ximStyleRequest\n");
437             }
438             else if (!ximStyleRoot &&(ximStyles->supported_styles[i] ==
439                      STYLE_ROOT))
440             {
441                 ximStyleRoot = STYLE_ROOT;
442                 TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
443             }
444             else if (!ximStyleCallback &&(ximStyles->supported_styles[i] ==
445                      STYLE_CALLBACK))
446             {
447                 ximStyleCallback = STYLE_CALLBACK;
448                 TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
449             }
450             else if (!ximStyleNone && (ximStyles->supported_styles[i] ==
451                      STYLE_NONE))
452             {
453                 TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
454                 ximStyleNone = STYLE_NONE;
455             }
456         }
457         XFree(ximStyles);
458
459         if (ximStyle == 0)
460             ximStyle = ximStyleRoot;
461
462         if (ximStyle == 0)
463             ximStyle = ximStyleNone;
464
465         if (ximStyleCallback == 0)
466         {
467             TRACE("No callback style avalable\n");
468             ximStyleCallback = ximStyle;
469         }
470
471     }
472
473     thread_data->xim = xim;
474
475     wine_tsx11_unlock();
476     IME_UpdateAssociation(NULL);
477     wine_tsx11_lock();
478     return TRUE;
479 }
480
481 static void open_xim_callback( Display *display, XPointer ptr, XPointer data )
482 {
483     if (open_xim( display ))
484         XUnregisterIMInstantiateCallback( display, NULL, NULL, NULL, open_xim_callback, NULL);
485 }
486
487 void X11DRV_SetupXIM(void)
488 {
489     Display *display = thread_display();
490
491     wine_tsx11_lock();
492     if (!open_xim( display ))
493         XRegisterIMInstantiateCallback( display, NULL, NULL, NULL, open_xim_callback, NULL );
494     wine_tsx11_unlock();
495 }
496
497 static BOOL X11DRV_DestroyIC(XIC xic, XPointer p, XPointer data)
498 {
499     struct x11drv_win_data *win_data = (struct x11drv_win_data *)p;
500     TRACE("xic = %p, win = %lx\n", xic, win_data->whole_window);
501     win_data->xic = NULL;
502     return TRUE;
503 }
504
505
506 XIC X11DRV_CreateIC(XIM xim, struct x11drv_win_data *data)
507 {
508     XPoint spot = {0};
509     XVaNestedList preedit = NULL;
510     XVaNestedList status = NULL;
511     XIC xic;
512     XICCallback destroy = {(XPointer)data, (XICProc)X11DRV_DestroyIC};
513     XICCallback P_StartCB, P_DoneCB, P_DrawCB, P_CaretCB;
514     LANGID langid = PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale()));
515     Window win = data->whole_window;
516
517     TRACE("xim = %p\n", xim);
518
519     wine_tsx11_lock();
520
521     /* use complex and slow XIC initialization method only for CJK */
522     if (langid != LANG_CHINESE &&
523         langid != LANG_JAPANESE &&
524         langid != LANG_KOREAN)
525     {
526         xic = XCreateIC(xim,
527                         XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
528                         XNClientWindow, win,
529                         XNFocusWindow, win,
530                         XNDestroyCallback, &destroy,
531                         NULL);
532         wine_tsx11_unlock();
533         data->xic = xic;
534         return xic;
535     }
536
537     /* create callbacks */
538     P_StartCB.client_data = NULL;
539     P_DoneCB.client_data = NULL;
540     P_DrawCB.client_data = NULL;
541     P_CaretCB.client_data = NULL;
542     P_StartCB.callback = (XICProc)XIMPreEditStartCallback;
543     P_DoneCB.callback = (XICProc)XIMPreEditDoneCallback;
544     P_DrawCB.callback = (XICProc)XIMPreEditDrawCallback;
545     P_CaretCB.callback = (XICProc)XIMPreEditCaretCallback;
546
547     if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0)
548     {
549         preedit = XVaCreateNestedList(0,
550                         XNSpotLocation, &spot,
551                         XNPreeditStartCallback, &P_StartCB,
552                         XNPreeditDoneCallback, &P_DoneCB,
553                         XNPreeditDrawCallback, &P_DrawCB,
554                         XNPreeditCaretCallback, &P_CaretCB,
555                         NULL);
556         TRACE("preedit = %p\n", preedit);
557     }
558     else
559     {
560         preedit = XVaCreateNestedList(0,
561                         XNPreeditStartCallback, &P_StartCB,
562                         XNPreeditDoneCallback, &P_DoneCB,
563                         XNPreeditDrawCallback, &P_DrawCB,
564                         XNPreeditCaretCallback, &P_CaretCB,
565                         NULL);
566
567         TRACE("preedit = %p\n", preedit);
568     }
569
570     if ((ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
571     {
572         status = XVaCreateNestedList(0,
573             NULL);
574         TRACE("status = %p\n", status);
575      }
576
577     if (preedit != NULL && status != NULL)
578     {
579         xic = XCreateIC(xim,
580               XNInputStyle, ximStyle,
581               XNPreeditAttributes, preedit,
582               XNStatusAttributes, status,
583               XNClientWindow, win,
584               XNFocusWindow, win,
585               XNDestroyCallback, &destroy,
586               NULL);
587      }
588     else if (preedit != NULL)
589     {
590         xic = XCreateIC(xim,
591               XNInputStyle, ximStyle,
592               XNPreeditAttributes, preedit,
593               XNClientWindow, win,
594               XNFocusWindow, win,
595               XNDestroyCallback, &destroy,
596               NULL);
597     }
598     else if (status != NULL)
599     {
600         xic = XCreateIC(xim,
601               XNInputStyle, ximStyle,
602               XNStatusAttributes, status,
603               XNClientWindow, win,
604               XNFocusWindow, win,
605               XNDestroyCallback, &destroy,
606               NULL);
607     }
608     else
609     {
610         xic = XCreateIC(xim,
611               XNInputStyle, ximStyle,
612               XNClientWindow, win,
613               XNFocusWindow, win,
614               XNDestroyCallback, &destroy,
615               NULL);
616     }
617
618     TRACE("xic = %p\n", xic);
619     data->xic = xic;
620
621     if (preedit != NULL)
622         XFree(preedit);
623     if (status != NULL)
624         XFree(status);
625
626     wine_tsx11_unlock();
627
628     return xic;
629 }