xim.c 17.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Functions for further XIM control
 *
 * Copyright 2003 CodeWeavers, Aric Stewart
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22 23 24 25 26 27 28 29 30 31 32 33 34
 */

#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "wingdi.h"
#include "winnls.h"
#include "x11drv.h"
#include "imm.h"
#include "wine/debug.h"

35
WINE_DEFAULT_DEBUG_CHANNEL(xim);
36

37 38 39 40 41
#ifndef HAVE_XICCALLBACK_CALLBACK
#define XICCallback XIMCallback
#define XICProc XIMProc
#endif

42 43
BOOL ximInComposeMode=FALSE;

44
/* moved here from imm32 for dll separation */
45 46 47 48 49 50 51 52 53
static DWORD dwCompStringLength = 0;
static LPBYTE CompositionString = NULL;
static DWORD dwCompStringSize = 0;

#define STYLE_OFFTHESPOT (XIMPreeditArea | XIMStatusArea)
#define STYLE_OVERTHESPOT (XIMPreeditPosition | XIMStatusNothing)
#define STYLE_ROOT (XIMPreeditNothing | XIMStatusNothing)
/* this uses all the callbacks to utilize full IME support */
#define STYLE_CALLBACK (XIMPreeditCallbacks | XIMStatusNothing)
54
/* in order to enable deadkey support */
55 56
#define STYLE_NONE (XIMPreeditNothing | XIMStatusNothing)

57 58 59 60
static XIMStyle ximStyle = 0;
static XIMStyle ximStyleRoot = 0;
static XIMStyle ximStyleRequest = STYLE_CALLBACK;

61
static void X11DRV_ImmSetInternalString(DWORD dwOffset,
62 63 64
                                        DWORD selLength, LPWSTR lpComp, DWORD dwCompLen)
{
    /* Composition strings are edited in chunks */
65 66 67
    unsigned int byte_length = dwCompLen * sizeof(WCHAR);
    unsigned int byte_offset = dwOffset * sizeof(WCHAR);
    unsigned int byte_selection = selLength * sizeof(WCHAR);
68 69
    int byte_expansion = byte_length - byte_selection;
    LPBYTE ptr_new;
70

71
    TRACE("( %i, %i, %p, %d):\n", dwOffset, selLength, lpComp, dwCompLen );
72

73
    if (byte_expansion + dwCompStringLength >= dwCompStringSize)
74
    {
75 76 77
        if (CompositionString)
            ptr_new = HeapReAlloc(GetProcessHeap(), 0, CompositionString,
                                  dwCompStringSize + byte_expansion);
78
        else
79 80
            ptr_new = HeapAlloc(GetProcessHeap(), 0,
                                dwCompStringSize + byte_expansion);
81

82 83 84 85
        if (ptr_new == NULL)
        {
            ERR("Couldn't expand composition string buffer\n");
            return;
86 87
        }

88 89
        CompositionString = ptr_new;
        dwCompStringSize += byte_expansion;
90 91
    }

92 93 94 95 96
    ptr_new = CompositionString + byte_offset;
    memmove(ptr_new + byte_length, ptr_new + byte_selection,
            dwCompStringLength - byte_offset - byte_selection);
    memcpy(ptr_new, lpComp, byte_length);
    dwCompStringLength += byte_expansion;
97

98 99
    IME_SetCompositionString(SCS_SETSTR, CompositionString,
                             dwCompStringLength, NULL, 0);
100 101 102 103 104
}

void X11DRV_XIMLookupChars( const char *str, DWORD count )
{
    DWORD dwOutput;
105
    WCHAR *wcOutput;
106 107
    HWND focus;

108 109
    TRACE("%p %u\n", str, count);

110 111 112 113 114
    dwOutput = MultiByteToWideChar(CP_UNIXCP, 0, str, count, NULL, 0);
    wcOutput = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * dwOutput);
    if (wcOutput == NULL)
        return;
    MultiByteToWideChar(CP_UNIXCP, 0, str, count, wcOutput, dwOutput);
115

116 117
    if ((focus = GetFocus()))
        IME_UpdateAssociation(focus);
118

119
    IME_SetResultString(wcOutput, dwOutput);
120
    HeapFree(GetProcessHeap(), 0, wcOutput);
121 122
}

123 124 125 126 127 128 129 130 131
static BOOL XIMPreEditStateNotifyCallback(XIC xic, XPointer p, XPointer data)
{
    const struct x11drv_win_data * const win_data = (struct x11drv_win_data *)p;
    const XIMPreeditState state = ((XIMPreeditStateNotifyCallbackStruct *)data)->state;

    TRACE("xic = %p, win = %lx, state = %lu\n", xic, win_data->whole_window, state);
    switch (state)
    {
    case XIMPreeditEnable:
132
        IME_SetOpenStatus(TRUE);
133 134
        break;
    case XIMPreeditDisable:
135
        IME_SetOpenStatus(FALSE);
136 137 138 139 140 141 142
        break;
    default:
        break;
    }
    return TRUE;
}

143 144 145
static int XIMPreEditStartCallback(XIC ic, XPointer client_data, XPointer call_data)
{
    TRACE("PreEditStartCallback %p\n",ic);
146
    IME_SetCompositionStatus(TRUE);
147 148 149 150 151 152 153 154
    ximInComposeMode = TRUE;
    return -1;
}

static void XIMPreEditDoneCallback(XIC ic, XPointer client_data, XPointer call_data)
{
    TRACE("PreeditDoneCallback %p\n",ic);
    ximInComposeMode = FALSE;
155 156 157 158 159
    if (dwCompStringSize)
        HeapFree(GetProcessHeap(), 0, CompositionString);
    dwCompStringSize = 0;
    dwCompStringLength = 0;
    CompositionString = NULL;
160
    IME_SetCompositionStatus(FALSE);
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
}

static void XIMPreEditDrawCallback(XIM ic, XPointer client_data,
                                   XIMPreeditDrawCallbackStruct *P_DR)
{
    TRACE("PreEditDrawCallback %p\n",ic);

    if (P_DR)
    {
        int sel = P_DR->chg_first;
        int len = P_DR->chg_length;
        if (P_DR->text)
        {
            if (! P_DR->text->encoding_is_wchar)
            {
176 177 178
                DWORD dwOutput;
                WCHAR *wcOutput;

179 180 181
                TRACE("multibyte\n");
                dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
                           P_DR->text->string.multi_byte, -1,
182 183 184 185 186 187 188 189 190 191
                           NULL, 0);
                wcOutput = HeapAlloc(GetProcessHeap(), 0, sizeof (WCHAR) * dwOutput);
                if (wcOutput)
                {
                    dwOutput = MultiByteToWideChar(CP_UNIXCP, 0,
                               P_DR->text->string.multi_byte, -1,
                               wcOutput, dwOutput);

                    /* ignore null */
                    dwOutput --;
192
                    X11DRV_ImmSetInternalString (sel, len, wcOutput, dwOutput);
193 194
                    HeapFree(GetProcessHeap(), 0, wcOutput);
                }
195 196 197 198
            }
            else
            {
                FIXME("wchar PROBIBILY WRONG\n");
199
                X11DRV_ImmSetInternalString (sel, len,
200 201 202 203 204
                                             (LPWSTR)P_DR->text->string.wide_char,
                                             P_DR->text->length);
            }
        }
        else
205
            X11DRV_ImmSetInternalString (sel, len, NULL, 0);
206
        IME_SetCursorPos(P_DR->caret);
207 208 209 210 211 212 213
    }
    TRACE("Finished\n");
}

static void XIMPreEditCaretCallback(XIC ic, XPointer client_data,
                                    XIMPreeditCaretCallbackStruct *P_C)
{
214 215 216 217
    TRACE("PreeditCaretCallback %p\n",ic);

    if (P_C)
    {
218
        int pos = IME_GetCursorPos();
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
        TRACE("pos: %d\n", pos);
        switch(P_C->direction)
        {
            case XIMForwardChar:
            case XIMForwardWord:
                pos++;
                break;
            case XIMBackwardChar:
            case XIMBackwardWord:
                pos--;
                break;
            case XIMLineStart:
                pos = 0;
                break;
            case XIMAbsolutePosition:
                pos = P_C->position;
                break;
            case XIMDontChange:
                P_C->position = pos;
                return;
            case XIMCaretUp:
            case XIMCaretDown:
            case XIMPreviousLine:
            case XIMNextLine:
            case XIMLineEnd:
                FIXME("Not implemented\n");
                break;
        }
247
        IME_SetCursorPos(pos);
248 249 250
        P_C->position = pos;
    }
    TRACE("Finished\n");
251 252
}

253
void X11DRV_ForceXIMReset(HWND hwnd)
254 255 256 257 258 259 260 261 262 263 264
{
    XIC ic = X11DRV_get_ic(hwnd);
    if (ic)
    {
        char* leftover;
        TRACE("Forcing Reset %p\n",ic);
        leftover = XmbResetIC(ic);
        XFree(leftover);
    }
}

265
void X11DRV_SetPreeditState(HWND hwnd, BOOL fOpen)
266 267 268
{
    XIC ic;
    XIMPreeditState state;
269
    XVaNestedList attr;
270 271 272

    ic = X11DRV_get_ic(hwnd);
    if (!ic)
273
        return;
274 275 276 277 278 279

    if (fOpen)
        state = XIMPreeditEnable;
    else
        state = XIMPreeditDisable;

280 281 282 283 284 285
    attr = XVaCreateNestedList(0, XNPreeditState, state, NULL);
    if (attr != NULL)
    {
        XSetICValues(ic, XNPreeditAttributes, attr, NULL);
        XFree(attr);
    }
286 287 288
}


289
/***********************************************************************
290 291 292 293 294
 *           X11DRV_InitXIM
 *
 * Process-wide XIM initialization.
 */
BOOL X11DRV_InitXIM( const char *input_style )
295 296 297 298 299 300 301 302
{
    if (!strcasecmp(input_style, "offthespot"))
        ximStyleRequest = STYLE_OFFTHESPOT;
    else if (!strcasecmp(input_style, "overthespot"))
        ximStyleRequest = STYLE_OVERTHESPOT;
    else if (!strcasecmp(input_style, "root"))
        ximStyleRequest = STYLE_ROOT;

303
    if (!XSupportsLocale())
304 305
    {
        WARN("X does not support locale.\n");
306
        return FALSE;
307
    }
308
    if (XSetLocaleModifiers("") == NULL)
309 310
    {
        WARN("Could not set locale modifiers.\n");
311
        return FALSE;
312
    }
313
    return TRUE;
314 315 316
}


317
static void open_xim_callback( Display *display, XPointer ptr, XPointer data );
318 319 320 321 322 323 324 325

static void X11DRV_DestroyIM(XIM xim, XPointer p, XPointer data)
{
    struct x11drv_thread_data *thread_data = x11drv_thread_data();

    TRACE("xim = %p, p = %p\n", xim, p);
    thread_data->xim = NULL;
    ximStyle = 0;
326
    XRegisterIMInstantiateCallback( thread_data->display, NULL, NULL, NULL, open_xim_callback, NULL );
327 328
}

329
/***********************************************************************
330 331 332 333
 *           X11DRV Ime creation
 *
 * Should always be called with the x11 lock held
 */
334
static BOOL open_xim( Display *display )
335
{
336
    struct x11drv_thread_data *thread_data = x11drv_thread_data();
337 338 339 340
    XIMStyle ximStyleCallback, ximStyleNone;
    XIMStyles *ximStyles = NULL;
    INT i;
    XIM xim;
341
    XIMCallback destroy;
342

343 344 345 346
    xim = XOpenIM(display, NULL, NULL, NULL);
    if (xim == NULL)
    {
        WARN("Could not open input method.\n");
347
        return FALSE;
348 349
    }

350 351 352 353 354 355 356 357
    destroy.client_data = NULL;
    destroy.callback = X11DRV_DestroyIM;
    if (XSetIMValues(xim, XNDestroyCallback, &destroy, NULL))
    {
        WARN("Could not set destroy callback.\n");
    }

    TRACE("xim = %p\n", xim);
358 359 360 361 362 363 364
    TRACE("X display of IM = %p\n", XDisplayOfIM(xim));
    TRACE("Using %s locale of Input Method\n", XLocaleOfIM(xim));

    XGetIMValues(xim, XNQueryInputStyle, &ximStyles, NULL);
    if (ximStyles == 0)
    {
        WARN("Could not find supported input style.\n");
365
        XCloseIM(xim);
366
        return FALSE;
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    }
    else
    {
        TRACE("ximStyles->count_styles = %d\n", ximStyles->count_styles);

        ximStyleRoot = 0;
        ximStyleNone = 0;
        ximStyleCallback = 0;

        for (i = 0; i < ximStyles->count_styles; ++i)
        {
            int style = ximStyles->supported_styles[i];
            TRACE("ximStyles[%d] = %s%s%s%s%s\n", i,
                        (style&XIMPreeditArea)?"XIMPreeditArea ":"",
                        (style&XIMPreeditCallbacks)?"XIMPreeditCallbacks ":"",
                        (style&XIMPreeditPosition)?"XIMPreeditPosition ":"",
                        (style&XIMPreeditNothing)?"XIMPreeditNothing ":"",
                        (style&XIMPreeditNone)?"XIMPreeditNone ":"");
            if (!ximStyle && (ximStyles->supported_styles[i] ==
                                ximStyleRequest))
            {
                ximStyle = ximStyleRequest;
                TRACE("Setting Style: ximStyle = ximStyleRequest\n");
            }
            else if (!ximStyleRoot &&(ximStyles->supported_styles[i] ==
                     STYLE_ROOT))
            {
                ximStyleRoot = STYLE_ROOT;
                TRACE("Setting Style: ximStyleRoot = STYLE_ROOT\n");
            }
            else if (!ximStyleCallback &&(ximStyles->supported_styles[i] ==
                     STYLE_CALLBACK))
            {
                ximStyleCallback = STYLE_CALLBACK;
                TRACE("Setting Style: ximStyleCallback = STYLE_CALLBACK\n");
            }
            else if (!ximStyleNone && (ximStyles->supported_styles[i] ==
                     STYLE_NONE))
            {
                TRACE("Setting Style: ximStyleNone = STYLE_NONE\n");
                ximStyleNone = STYLE_NONE;
            }
        }
        XFree(ximStyles);

        if (ximStyle == 0)
            ximStyle = ximStyleRoot;

        if (ximStyle == 0)
            ximStyle = ximStyleNone;

        if (ximStyleCallback == 0)
        {
420
            TRACE("No callback style available\n");
421 422 423
            ximStyleCallback = ximStyle;
        }

424
    }
425

426
    thread_data->xim = xim;
427

428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
    if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0 ||
        (ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
    {
        char **list;
        int count;
        thread_data->font_set = XCreateFontSet(display, "fixed",
                          &list, &count, NULL);
        TRACE("ximFontSet = %p\n", thread_data->font_set);
        TRACE("list = %p, count = %d\n", list, count);
        if (list != NULL)
        {
            int i;
            for (i = 0; i < count; ++i)
                TRACE("list[%d] = %s\n", i, list[i]);
            XFreeStringList(list);
        }
    }
    else
        thread_data->font_set = NULL;

448
    IME_UpdateAssociation(NULL);
449
    return TRUE;
450
}
451

452 453 454 455 456
static void open_xim_callback( Display *display, XPointer ptr, XPointer data )
{
    if (open_xim( display ))
        XUnregisterIMInstantiateCallback( display, NULL, NULL, NULL, open_xim_callback, NULL);
}
457

458 459
void X11DRV_SetupXIM(void)
{
460 461 462 463
    Display *display = thread_display();

    if (!open_xim( display ))
        XRegisterIMInstantiateCallback( display, NULL, NULL, NULL, open_xim_callback, NULL );
464 465
}

466 467 468 469 470 471 472
static BOOL X11DRV_DestroyIC(XIC xic, XPointer p, XPointer data)
{
    struct x11drv_win_data *win_data = (struct x11drv_win_data *)p;
    TRACE("xic = %p, win = %lx\n", xic, win_data->whole_window);
    win_data->xic = NULL;
    return TRUE;
}
473

474 475

XIC X11DRV_CreateIC(XIM xim, struct x11drv_win_data *data)
476 477 478 479 480
{
    XPoint spot = {0};
    XVaNestedList preedit = NULL;
    XVaNestedList status = NULL;
    XIC xic;
481
    XICCallback destroy = {(XPointer)data, X11DRV_DestroyIC};
482
    XICCallback P_StateNotifyCB, P_StartCB, P_DoneCB, P_DrawCB, P_CaretCB;
483
    LANGID langid = PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale()));
484
    Window win = data->whole_window;
485
    XFontSet fontSet = x11drv_thread_data()->font_set;
486 487

    TRACE("xim = %p\n", xim);
488 489 490 491 492 493 494 495 496 497

    /* use complex and slow XIC initialization method only for CJK */
    if (langid != LANG_CHINESE &&
        langid != LANG_JAPANESE &&
        langid != LANG_KOREAN)
    {
        xic = XCreateIC(xim,
                        XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
                        XNClientWindow, win,
                        XNFocusWindow, win,
498
                        XNDestroyCallback, &destroy,
499
                        NULL);
500
        data->xic = xic;
501 502 503 504
        return xic;
    }

    /* create callbacks */
505
    P_StateNotifyCB.client_data = (XPointer)data;
506 507 508 509
    P_StartCB.client_data = NULL;
    P_DoneCB.client_data = NULL;
    P_DrawCB.client_data = NULL;
    P_CaretCB.client_data = NULL;
510 511
    P_StateNotifyCB.callback = XIMPreEditStateNotifyCallback;
    P_StartCB.callback = XIMPreEditStartCallback;
512 513 514
    P_DoneCB.callback = (XICProc)XIMPreEditDoneCallback;
    P_DrawCB.callback = (XICProc)XIMPreEditDrawCallback;
    P_CaretCB.callback = (XICProc)XIMPreEditCaretCallback;
515 516 517 518

    if ((ximStyle & (XIMPreeditNothing | XIMPreeditNone)) == 0)
    {
        preedit = XVaCreateNestedList(0,
519
                        XNFontSet, fontSet,
520
                        XNSpotLocation, &spot,
521
                        XNPreeditStateNotifyCallback, &P_StateNotifyCB,
522 523 524 525 526
                        XNPreeditStartCallback, &P_StartCB,
                        XNPreeditDoneCallback, &P_DoneCB,
                        XNPreeditDrawCallback, &P_DrawCB,
                        XNPreeditCaretCallback, &P_CaretCB,
                        NULL);
527
        TRACE("preedit = %p\n", preedit);
528 529 530 531
    }
    else
    {
        preedit = XVaCreateNestedList(0,
532
                        XNPreeditStateNotifyCallback, &P_StateNotifyCB,
533 534 535 536 537 538
                        XNPreeditStartCallback, &P_StartCB,
                        XNPreeditDoneCallback, &P_DoneCB,
                        XNPreeditDrawCallback, &P_DrawCB,
                        XNPreeditCaretCallback, &P_CaretCB,
                        NULL);

539
        TRACE("preedit = %p\n", preedit);
540 541 542 543 544
    }

    if ((ximStyle & (XIMStatusNothing | XIMStatusNone)) == 0)
    {
        status = XVaCreateNestedList(0,
545
            XNFontSet, fontSet,
546
            NULL);
547
        TRACE("status = %p\n", status);
548 549 550 551 552 553 554 555 556 557
     }

    if (preedit != NULL && status != NULL)
    {
        xic = XCreateIC(xim,
              XNInputStyle, ximStyle,
              XNPreeditAttributes, preedit,
              XNStatusAttributes, status,
              XNClientWindow, win,
              XNFocusWindow, win,
558
              XNDestroyCallback, &destroy,
559 560 561 562 563 564 565 566 567
              NULL);
     }
    else if (preedit != NULL)
    {
        xic = XCreateIC(xim,
              XNInputStyle, ximStyle,
              XNPreeditAttributes, preedit,
              XNClientWindow, win,
              XNFocusWindow, win,
568
              XNDestroyCallback, &destroy,
569 570 571 572 573 574 575 576 577
              NULL);
    }
    else if (status != NULL)
    {
        xic = XCreateIC(xim,
              XNInputStyle, ximStyle,
              XNStatusAttributes, status,
              XNClientWindow, win,
              XNFocusWindow, win,
578
              XNDestroyCallback, &destroy,
579 580 581 582 583 584 585 586
              NULL);
    }
    else
    {
        xic = XCreateIC(xim,
              XNInputStyle, ximStyle,
              XNClientWindow, win,
              XNFocusWindow, win,
587
              XNDestroyCallback, &destroy,
588 589 590
              NULL);
    }

591
    TRACE("xic = %p\n", xic);
592
    data->xic = xic;
593 594 595 596 597 598 599 600

    if (preedit != NULL)
        XFree(preedit);
    if (status != NULL)
        XFree(status);

    return xic;
}