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