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