LCOV - code coverage report
Current view: top level - libreoffice/vcl/unx/generic/app - i18n_im.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 144 0.0 %
Date: 2012-12-27 Functions: 0 21 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : 
      21             : #include <stdio.h>
      22             : #include <string.h>
      23             : 
      24             : #ifdef LINUX
      25             : #  ifndef __USE_XOPEN
      26             : #    define __USE_XOPEN
      27             : #  endif
      28             : #endif
      29             : #include <poll.h>
      30             : 
      31             : #include <tools/prex.h>
      32             : #include <X11/Xlocale.h>
      33             : #include <X11/Xlib.h>
      34             : #include <unx/XIM.h>
      35             : #include <tools/postx.h>
      36             : 
      37             : #include "unx/salunx.h"
      38             : #include "unx/saldisp.hxx"
      39             : #include "unx/i18n_im.hxx"
      40             : #include "unx/i18n_status.hxx"
      41             : 
      42             : #include <osl/thread.h>
      43             : #include <osl/process.h>
      44             : 
      45             : using namespace vcl;
      46             : #include "unx/i18n_cb.hxx"
      47             : #if defined(SOLARIS) ||  defined(LINUX)
      48             : extern "C" char * XSetIMValues(XIM im, ...);
      49             : #endif
      50             : 
      51             : // ------------------------------------------------------------------------------------
      52             : //
      53             : // kinput2 IME needs special key handling since key release events are filtered in
      54             : // preeditmode and XmbResetIC does not work
      55             : //
      56             : // ------------------------------------------------------------------------------------
      57             : 
      58             : Bool
      59           0 : IMServerKinput2 ()
      60             : {
      61           0 :     const static char* p_xmodifiers = getenv ("XMODIFIERS");
      62             :     const static Bool  b_kinput2    =    (p_xmodifiers != NULL)
      63           0 :                                       && (strcmp(p_xmodifiers, "@im=kinput2") == 0);
      64             : 
      65           0 :     return b_kinput2;
      66             : }
      67             : 
      68             : class XKeyEventOp : XKeyEvent
      69             : {
      70             :     private:
      71             :         void            init();
      72             : 
      73             :     public:
      74             :                         XKeyEventOp();
      75             :                         ~XKeyEventOp();
      76             : 
      77             :         XKeyEventOp&    operator= (const XKeyEvent &rEvent);
      78             :         void            erase ();
      79             :         Bool            match (const XKeyEvent &rEvent) const;
      80             : };
      81             : 
      82             : void
      83           0 : XKeyEventOp::init()
      84             : {
      85           0 :     type        = 0; /* serial = 0; */
      86           0 :     send_event  = 0; display   = 0;
      87           0 :     window      = 0; root      = 0;
      88           0 :     subwindow   = 0; /* time   = 0; */
      89             :  /* x           = 0; y         = 0; */
      90             :  /* x_root      = 0; y_root    = 0; */
      91           0 :     state       = 0; keycode   = 0;
      92           0 :     same_screen = 0;
      93           0 : }
      94             : 
      95           0 : XKeyEventOp::XKeyEventOp()
      96             : {
      97           0 :     init();
      98           0 : }
      99             : 
     100           0 : XKeyEventOp::~XKeyEventOp()
     101             : {
     102           0 : }
     103             : 
     104             : XKeyEventOp&
     105           0 : XKeyEventOp::operator= (const XKeyEvent &rEvent)
     106             : {
     107           0 :     type        = rEvent.type;     /* serial  = rEvent.serial; */
     108           0 :     send_event  = rEvent.send_event;  display = rEvent.display;
     109           0 :     window      = rEvent.window;      root    = rEvent.root;
     110           0 :     subwindow   = rEvent.subwindow;/* time    = rEvent.time;   */
     111             :  /* x           = rEvent.x,           y       = rEvent.y;      */
     112             :  /* x_root      = rEvent.x_root,      y_root  = rEvent.y_root; */
     113           0 :     state       = rEvent.state;       keycode = rEvent.keycode;
     114           0 :     same_screen = rEvent.same_screen;
     115             : 
     116           0 :     return *this;
     117             : }
     118             : 
     119             : void
     120           0 : XKeyEventOp::erase ()
     121             : {
     122           0 :     init();
     123           0 : }
     124             : 
     125             : Bool
     126           0 : XKeyEventOp::match (const XKeyEvent &rEvent) const
     127             : {
     128             :     return (   (type == XLIB_KeyPress   && rEvent.type == KeyRelease)
     129             :             || (type == KeyRelease && rEvent.type == XLIB_KeyPress  ))
     130             :          /* && serial      == rEvent.serial */
     131             :             && send_event  == rEvent.send_event
     132             :             && display     == rEvent.display
     133             :             && window      == rEvent.window
     134             :             && root        == rEvent.root
     135             :             && subwindow   == rEvent.subwindow
     136             :          /* && time        == rEvent.time
     137             :             && x           == rEvent.x
     138             :             && y           == rEvent.y
     139             :             && x_root      == rEvent.x_root
     140             :             && y_root      == rEvent.y_root */
     141             :             && state       == rEvent.state
     142             :             && keycode     == rEvent.keycode
     143           0 :             && same_screen == rEvent.same_screen;
     144             : }
     145             : 
     146             : // -------------------------------------------------------------------------
     147             : //
     148             : // locale handling
     149             : //
     150             : // -------------------------------------------------------------------------
     151             : 
     152             : //  Locale handling of the operating system layer
     153             : 
     154             : static char*
     155           0 : SetSystemLocale( const char* p_inlocale )
     156             : {
     157             :     char *p_outlocale;
     158             : 
     159           0 :     if ( (p_outlocale = setlocale(LC_ALL, p_inlocale)) == NULL )
     160             :     {
     161             :         fprintf( stderr, "I18N: Operating system doesn't support locale \"%s\"\n",
     162           0 :             p_inlocale );
     163             :     }
     164             : 
     165           0 :     return p_outlocale;
     166             : }
     167             : 
     168             : #ifdef SOLARIS
     169             : static void
     170             : SetSystemEnvironment( const rtl::OUString& rLocale )
     171             : {
     172             :     rtl::OUString LC_ALL_Var(RTL_CONSTASCII_USTRINGPARAM("LC_ALL"));
     173             :     osl_setEnvironment(LC_ALL_Var.pData, rLocale.pData);
     174             : 
     175             :     rtl::OUString LANG_Var(RTL_CONSTASCII_USTRINGPARAM("LANG"));
     176             :     osl_setEnvironment(LANG_Var.pData, rLocale.pData);
     177             : }
     178             : #endif
     179             : 
     180             : static Bool
     181           0 : IsPosixLocale( const char* p_locale )
     182             : {
     183           0 :     if ( p_locale == NULL )
     184           0 :         return False;
     185           0 :     if ( (p_locale[ 0 ] == 'C') && (p_locale[ 1 ] == '\0') )
     186           0 :         return True;
     187           0 :     if ( strncmp(p_locale, "POSIX", sizeof("POSIX")) == 0 )
     188           0 :         return True;
     189             : 
     190           0 :     return False;
     191             : }
     192             : 
     193             : //  Locale handling of the X Window System layer
     194             : 
     195             : static Bool
     196           0 : IsXWindowCompatibleLocale( const char* p_locale )
     197             : {
     198           0 :     if ( p_locale == NULL )
     199           0 :         return False;
     200             : 
     201           0 :     if ( !XSupportsLocale() )
     202             :     {
     203             :         fprintf (stderr, "I18N: X Window System doesn't support locale \"%s\"\n",
     204           0 :                 p_locale );
     205           0 :         return False;
     206             :     }
     207           0 :     return True;
     208             : }
     209             : 
     210             : // Set the operating system locale prior to trying to open an
     211             : // XIM InputMethod.
     212             : // Handle the cases where the current locale is either not supported by the
     213             : // operating system (LANG=gaga) or by the XWindow system (LANG=aa_ER@saaho)
     214             : // by providing a fallback.
     215             : // Upgrade "C" or "POSIX" to "en_US" locale to allow umlauts and accents
     216             : // see i8988, i9188, i8930, i16318
     217             : // on Solaris the environment needs to be set equivalent to the locale (#i37047#)
     218             : 
     219             : Bool
     220           0 : SalI18N_InputMethod::SetLocale( const char* pLocale )
     221             : {
     222             :     // check whether we want an Input Method engine, if we don't we
     223             :     // do not need to set the locale
     224           0 :     if ( mbUseable )
     225             :     {
     226           0 :         char *locale = SetSystemLocale( pLocale );
     227           0 :         if ( (!IsXWindowCompatibleLocale(locale)) || IsPosixLocale(locale) )
     228             :         {
     229           0 :             osl_setThreadTextEncoding (RTL_TEXTENCODING_ISO_8859_1);
     230           0 :             locale = SetSystemLocale( "en_US" );
     231             :             #ifdef SOLARIS
     232             :             SetSystemEnvironment( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("en_US")) );
     233             :             #endif
     234           0 :             if (! IsXWindowCompatibleLocale(locale))
     235             :             {
     236           0 :                 locale = SetSystemLocale( "C" );
     237             :                 #ifdef SOLARIS
     238             :                 SetSystemEnvironment( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("C")) );
     239             :                 #endif
     240           0 :                 if (! IsXWindowCompatibleLocale(locale))
     241           0 :                     mbUseable = False;
     242             :             }
     243             :         }
     244             : 
     245             :         // must not fail if mbUseable since XSupportsLocale() asserts success
     246           0 :         if ( mbUseable && XSetLocaleModifiers("") == NULL )
     247             :         {
     248             :             fprintf (stderr, "I18N: Can't set X modifiers for locale \"%s\"\n",
     249           0 :                 locale);
     250           0 :             mbUseable = False;
     251             :         }
     252             :     }
     253             : 
     254           0 :     return mbUseable;
     255             : }
     256             : 
     257             : Bool
     258           0 : SalI18N_InputMethod::PosixLocale()
     259             : {
     260           0 :     if (maMethod)
     261           0 :         return IsPosixLocale (XLocaleOfIM (maMethod));
     262           0 :     return False;
     263             : }
     264             : 
     265             : // ------------------------------------------------------------------------
     266             : //
     267             : // Constructor / Destructor / Initialisation
     268             : //
     269             : // ------------------------------------------------------------------------
     270             : 
     271           0 : SalI18N_InputMethod::SalI18N_InputMethod( ) : mbUseable( bUseInputMethodDefault ),
     272             :                                               maMethod( (XIM)NULL ),
     273           0 :                                                 mpStyles( (XIMStyles*)NULL )
     274             : {
     275           0 :     const char *pUseInputMethod = getenv( "SAL_USEINPUTMETHOD" );
     276           0 :     if ( pUseInputMethod != NULL )
     277           0 :         mbUseable = pUseInputMethod[0] != '\0' ;
     278           0 : }
     279             : 
     280           0 : SalI18N_InputMethod::~SalI18N_InputMethod()
     281             : {
     282           0 :     ::vcl::I18NStatus::free();
     283           0 :     if ( mpStyles != NULL )
     284           0 :         XFree( mpStyles );
     285           0 :     if ( maMethod != NULL )
     286           0 :         XCloseIM ( maMethod );
     287           0 : }
     288             : 
     289             : //
     290             : // XXX
     291             : // debug routine: lets have a look at the provided method styles
     292             : //
     293             : 
     294             : #if OSL_DEBUG_LEVEL > 1
     295             : 
     296             : extern "C" char*
     297             : GetMethodName( XIMStyle nStyle, char *pBuf, int nBufSize)
     298             : {
     299             :     struct StyleName {
     300             :         const XIMStyle nStyle;
     301             :         const char    *pName;
     302             :         const int      nNameLen;
     303             :     };
     304             : 
     305             :     StyleName *pDescPtr;
     306             :     static const StyleName pDescription[] = {
     307             :         { XIMPreeditArea,      "PreeditArea ",     sizeof("PreeditArea ")   },
     308             :         { XIMPreeditCallbacks, "PreeditCallbacks ",sizeof("PreeditCallbacks ")},
     309             :         { XIMPreeditPosition,  "PreeditPosition ", sizeof("PreeditPosition ") },
     310             :         { XIMPreeditNothing,   "PreeditNothing ",  sizeof("PreeditNothing ")  },
     311             :         { XIMPreeditNone,      "PreeditNone ",     sizeof("PreeditNone ")   },
     312             :         { XIMStatusArea,       "StatusArea ",      sizeof("StatusArea ")    },
     313             :         { XIMStatusCallbacks,  "StatusCallbacks ", sizeof("StatusCallbacks ") },
     314             :         { XIMStatusNothing,    "StatusNothing ",   sizeof("StatusNothing ") },
     315             :         { XIMStatusNone,       "StatusNone ",      sizeof("StatusNone ")    },
     316             :         { 0, "NULL", 0 }
     317             :     };
     318             : 
     319             :     if ( nBufSize > 0 )
     320             :         pBuf[0] = '\0';
     321             : 
     322             :     char *pBufPtr = pBuf;
     323             :     for ( pDescPtr = const_cast<StyleName*>(pDescription); pDescPtr->nStyle != 0; pDescPtr++ )
     324             :     {
     325             :         int nSize = pDescPtr->nNameLen - 1;
     326             :         if ( (nStyle & pDescPtr->nStyle) && (nBufSize > nSize) )
     327             :         {
     328             :             strncpy( pBufPtr, pDescPtr->pName, nSize + 1);
     329             :             pBufPtr  += nSize;
     330             :             nBufSize -= nSize;
     331             :         }
     332             :     }
     333             : 
     334             :     return pBuf;
     335             : }
     336             : 
     337             : extern "C" void
     338             : PrintInputStyle( XIMStyles *pStyle )
     339             : {
     340             :     char pBuf[ 128 ];
     341             :     int  nBuf = sizeof( pBuf );
     342             : 
     343             :     if ( pStyle == NULL )
     344             :         fprintf( stderr, "no input method styles\n");
     345             :     else
     346             :     for ( int nStyle = 0; nStyle < pStyle->count_styles; nStyle++ )
     347             :     {
     348             :         fprintf( stderr, "style #%i = %s\n", nStyle,
     349             :               GetMethodName(pStyle->supported_styles[nStyle], pBuf, nBuf) );
     350             :     }
     351             : }
     352             : 
     353             : #endif
     354             : 
     355             : //
     356             : // this is the real constructing routine, since locale setting has to be done
     357             : // prior to xopendisplay, the xopenim call has to be delayed
     358             : //
     359             : 
     360             : Bool
     361           0 : SalI18N_InputMethod::CreateMethod ( Display *pDisplay )
     362             : {
     363           0 :     if ( mbUseable )
     364             :     {
     365           0 :         maMethod = XOpenIM(pDisplay, NULL, NULL, NULL);
     366             : 
     367           0 :         if ((maMethod == (XIM)NULL) && (getenv("XMODIFIERS") != NULL))
     368             :         {
     369           0 :                 rtl::OUString envVar(RTL_CONSTASCII_USTRINGPARAM("XMODIFIERS"));
     370           0 :                 osl_clearEnvironment(envVar.pData);
     371           0 :                 XSetLocaleModifiers("");
     372           0 :                 maMethod = XOpenIM(pDisplay, NULL, NULL, NULL);
     373             :         }
     374             : 
     375           0 :         if ( maMethod != (XIM)NULL )
     376             :         {
     377           0 :             if (   XGetIMValues(maMethod, XNQueryInputStyle, &mpStyles, NULL)
     378             :                 != NULL)
     379           0 :                 mbUseable = False;
     380             :             #if OSL_DEBUG_LEVEL > 1
     381             :             fprintf(stderr, "Creating Mono-Lingual InputMethod\n" );
     382             :             PrintInputStyle( mpStyles );
     383             :             #endif
     384             :         }
     385             :         else
     386             :         {
     387           0 :             mbUseable = False;
     388             :         }
     389             :     }
     390             : 
     391             :     #if OSL_DEBUG_LEVEL > 1
     392             :     if ( !mbUseable )
     393             :         fprintf(stderr, "input method creation failed\n");
     394             :     #endif
     395             : 
     396           0 :     maDestroyCallback.callback    = (XIMProc)IM_IMDestroyCallback;
     397           0 :     maDestroyCallback.client_data = (XPointer)this;
     398           0 :     if (mbUseable && maMethod != NULL)
     399           0 :         XSetIMValues(maMethod, XNDestroyCallback, &maDestroyCallback, NULL);
     400             : 
     401           0 :     return mbUseable;
     402             : }
     403             : 
     404             : //
     405             : // give IM the opportunity to look at the event, and possibly hide it
     406             : //
     407             : 
     408             : Bool
     409           0 : SalI18N_InputMethod::FilterEvent( XEvent *pEvent, XLIB_Window window    )
     410             : {
     411           0 :     if (!mbUseable)
     412           0 :         return False;
     413             : 
     414           0 :     Bool bFilterEvent = XFilterEvent (pEvent, window);
     415             : 
     416           0 :     if (pEvent->type != XLIB_KeyPress && pEvent->type != KeyRelease)
     417           0 :         return bFilterEvent;
     418             : 
     419             :     /*
     420             :      * fix broken key release handling of some IMs
     421             :      */
     422           0 :     XKeyEvent*         pKeyEvent = &(pEvent->xkey);
     423           0 :     static XKeyEventOp maLastKeyPress;
     424             : 
     425           0 :     if (bFilterEvent)
     426             :     {
     427           0 :         if (pKeyEvent->type == KeyRelease)
     428           0 :             bFilterEvent = !maLastKeyPress.match (*pKeyEvent);
     429           0 :         maLastKeyPress.erase();
     430             :     }
     431             :     else /* (!bFilterEvent) */
     432             :     {
     433           0 :         if (pKeyEvent->type == XLIB_KeyPress)
     434           0 :             maLastKeyPress = *pKeyEvent;
     435             :         else
     436           0 :             maLastKeyPress.erase();
     437             :     }
     438             : 
     439           0 :     return bFilterEvent;
     440             : }
     441             : 
     442             : void
     443           0 : SalI18N_InputMethod::HandleDestroyIM()
     444             : {
     445           0 :     mbUseable       = False;
     446           0 :     maMethod        = NULL;
     447           0 : }
     448             : 
     449             : // ------------------------------------------------------------------------
     450             : //
     451             : // add a connection watch into the SalXLib yieldTable to allow iiimp
     452             : // connection processing: soffice waits in select() not in XNextEvent(), so
     453             : // there may be requests pending on the iiimp internal connection that will
     454             : // not be processed until XNextEvent is called the next time. If we do not
     455             : // have the focus because the atok12 lookup choice aux window has it we stay
     456             : // deaf and dump otherwise.
     457             : //
     458             : // ------------------------------------------------------------------------
     459             : 
     460             : int
     461           0 : InputMethod_HasPendingEvent(int nFileDescriptor, void *pData)
     462             : {
     463           0 :     if (pData == NULL)
     464           0 :         return 0;
     465             : 
     466             :     struct pollfd aFileDescriptor;
     467             :     #ifdef SOLARIS
     468             :     nfds_t        nNumDescriptor = 1;
     469             :     #else
     470           0 :     unsigned int      nNumDescriptor = 1;
     471             :     #endif
     472           0 :     aFileDescriptor.fd      = nFileDescriptor;
     473           0 :     aFileDescriptor.events  = POLLRDNORM;
     474           0 :     aFileDescriptor.revents = 0;
     475             : 
     476           0 :     int nPoll = poll (&aFileDescriptor, nNumDescriptor, 0 /* timeout */ );
     477             : 
     478           0 :     if (nPoll > 0)
     479             :     {
     480             :         /* at least some conditions in revent are set */
     481           0 :         if (   (aFileDescriptor.revents & POLLHUP)
     482             :             || (aFileDescriptor.revents & POLLERR)
     483             :             || (aFileDescriptor.revents & POLLNVAL))
     484           0 :             return 0; /* oops error condition set */
     485             : 
     486           0 :         if (aFileDescriptor.revents & POLLRDNORM)
     487           0 :             return 1; /* success */
     488             :     }
     489             : 
     490             :     /* nPoll == 0 means timeout, nPoll < 0 means error */
     491           0 :     return 0;
     492             : }
     493             : 
     494             : int
     495           0 : InputMethod_IsEventQueued(int nFileDescriptor, void *pData)
     496             : {
     497           0 :     return InputMethod_HasPendingEvent (nFileDescriptor, pData);
     498             : }
     499             : 
     500             : int
     501           0 : InputMethod_HandleNextEvent(int nFileDescriptor, void *pData)
     502             : {
     503           0 :     if (pData != NULL)
     504           0 :         XProcessInternalConnection((Display*)pData, nFileDescriptor);
     505             : 
     506           0 :     return 0;
     507             : }
     508             : 
     509             : extern "C" void
     510           0 : InputMethod_ConnectionWatchProc (Display *pDisplay, XPointer pClientData,
     511             :     int nFileDescriptor, Bool bOpening, XPointer*)
     512             : {
     513           0 :     SalXLib *pConnectionHandler = (SalXLib*)pClientData;
     514             : 
     515           0 :     if (pConnectionHandler == NULL)
     516           0 :         return;
     517             : 
     518           0 :     if (bOpening)
     519             :     {
     520             :         pConnectionHandler->Insert (nFileDescriptor, pDisplay,
     521             :                                     InputMethod_HasPendingEvent,
     522             :                                     InputMethod_IsEventQueued,
     523           0 :                                     InputMethod_HandleNextEvent);
     524             :     }
     525             :     else
     526             :     {
     527           0 :         pConnectionHandler->Remove (nFileDescriptor);
     528             :     }
     529             : }
     530             : 
     531             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10