LCOV - code coverage report
Current view: top level - vcl/unx/generic/app - i18n_im.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 122 0.0 %
Date: 2014-04-14 Functions: 0 16 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             : #include <stdio.h>
      21             : #include <string.h>
      22             : 
      23             : #ifdef LINUX
      24             : #  ifndef __USE_XOPEN
      25             : #    define __USE_XOPEN
      26             : #  endif
      27             : #endif
      28             : #include <poll.h>
      29             : 
      30             : #include <prex.h>
      31             : #include <X11/Xlocale.h>
      32             : #include <X11/Xlib.h>
      33             : #include <unx/XIM.h>
      34             : #include <postx.h>
      35             : 
      36             : #include "unx/salunx.h"
      37             : #include "unx/saldisp.hxx"
      38             : #include "unx/i18n_im.hxx"
      39             : #include "unx/i18n_status.hxx"
      40             : 
      41             : #include <osl/thread.h>
      42             : #include <osl/process.h>
      43             : 
      44             : using namespace vcl;
      45             : #include "unx/i18n_cb.hxx"
      46             : #if defined(SOLARIS) ||  defined(LINUX)
      47             : extern "C" char * XSetIMValues(XIM im, ...);
      48             : #endif
      49             : 
      50             : // kinput2 IME needs special key handling since key release events are filtered in
      51             : // preeditmode and XmbResetIC does not work
      52             : 
      53             : class XKeyEventOp : XKeyEvent
      54             : {
      55             :     private:
      56             :         void            init();
      57             : 
      58             :     public:
      59             :                         XKeyEventOp();
      60             :                         ~XKeyEventOp();
      61             : 
      62             :         XKeyEventOp&    operator= (const XKeyEvent &rEvent);
      63             :         void            erase ();
      64             :         bool            match (const XKeyEvent &rEvent) const;
      65             : };
      66             : 
      67             : void
      68           0 : XKeyEventOp::init()
      69             : {
      70           0 :     type        = 0; /* serial = 0; */
      71           0 :     send_event  = 0; display   = 0;
      72           0 :     window      = 0; root      = 0;
      73           0 :     subwindow   = 0; /* time   = 0; */
      74             :  /* x           = 0; y         = 0; */
      75             :  /* x_root      = 0; y_root    = 0; */
      76           0 :     state       = 0; keycode   = 0;
      77           0 :     same_screen = 0;
      78           0 : }
      79             : 
      80           0 : XKeyEventOp::XKeyEventOp()
      81             : {
      82           0 :     init();
      83           0 : }
      84             : 
      85           0 : XKeyEventOp::~XKeyEventOp()
      86             : {
      87           0 : }
      88             : 
      89             : XKeyEventOp&
      90           0 : XKeyEventOp::operator= (const XKeyEvent &rEvent)
      91             : {
      92           0 :     type        = rEvent.type;     /* serial  = rEvent.serial; */
      93           0 :     send_event  = rEvent.send_event;  display = rEvent.display;
      94           0 :     window      = rEvent.window;      root    = rEvent.root;
      95           0 :     subwindow   = rEvent.subwindow;/* time    = rEvent.time;   */
      96             :  /* x           = rEvent.x,           y       = rEvent.y;      */
      97             :  /* x_root      = rEvent.x_root,      y_root  = rEvent.y_root; */
      98           0 :     state       = rEvent.state;       keycode = rEvent.keycode;
      99           0 :     same_screen = rEvent.same_screen;
     100             : 
     101           0 :     return *this;
     102             : }
     103             : 
     104             : void
     105           0 : XKeyEventOp::erase ()
     106             : {
     107           0 :     init();
     108           0 : }
     109             : 
     110             : bool
     111           0 : XKeyEventOp::match (const XKeyEvent &rEvent) const
     112             : {
     113           0 :     return (   (type == XLIB_KeyPress   && rEvent.type == KeyRelease)
     114           0 :             || (type == KeyRelease && rEvent.type == XLIB_KeyPress  ))
     115             :          /* && serial      == rEvent.serial */
     116           0 :             && send_event  == rEvent.send_event
     117           0 :             && display     == rEvent.display
     118           0 :             && window      == rEvent.window
     119           0 :             && root        == rEvent.root
     120           0 :             && subwindow   == rEvent.subwindow
     121             :          /* && time        == rEvent.time
     122             :             && x           == rEvent.x
     123             :             && y           == rEvent.y
     124             :             && x_root      == rEvent.x_root
     125             :             && y_root      == rEvent.y_root */
     126           0 :             && state       == rEvent.state
     127           0 :             && keycode     == rEvent.keycode
     128           0 :             && same_screen == rEvent.same_screen;
     129             : }
     130             : 
     131             : // locale handling
     132             : 
     133             : //  Locale handling of the operating system layer
     134             : 
     135             : static char*
     136           0 : SetSystemLocale( const char* p_inlocale )
     137             : {
     138             :     char *p_outlocale;
     139             : 
     140           0 :     if ( (p_outlocale = setlocale(LC_ALL, p_inlocale)) == NULL )
     141             :     {
     142             :         fprintf( stderr, "I18N: Operating system doesn't support locale \"%s\"\n",
     143           0 :             p_inlocale );
     144             :     }
     145             : 
     146           0 :     return p_outlocale;
     147             : }
     148             : 
     149             : #ifdef SOLARIS
     150             : static void
     151             : SetSystemEnvironment( const OUString& rLocale )
     152             : {
     153             :     OUString LC_ALL_Var("LC_ALL");
     154             :     osl_setEnvironment(LC_ALL_Var.pData, rLocale.pData);
     155             : 
     156             :     OUString LANG_Var("LANG");
     157             :     osl_setEnvironment(LANG_Var.pData, rLocale.pData);
     158             : }
     159             : #endif
     160             : 
     161             : static Bool
     162           0 : IsPosixLocale( const char* p_locale )
     163             : {
     164           0 :     if ( p_locale == NULL )
     165           0 :         return False;
     166           0 :     if ( (p_locale[ 0 ] == 'C') && (p_locale[ 1 ] == '\0') )
     167           0 :         return True;
     168           0 :     if ( strncmp(p_locale, "POSIX", sizeof("POSIX")) == 0 )
     169           0 :         return True;
     170             : 
     171           0 :     return False;
     172             : }
     173             : 
     174             : //  Locale handling of the X Window System layer
     175             : 
     176             : static Bool
     177           0 : IsXWindowCompatibleLocale( const char* p_locale )
     178             : {
     179           0 :     if ( p_locale == NULL )
     180           0 :         return False;
     181             : 
     182           0 :     if ( !XSupportsLocale() )
     183             :     {
     184             :         fprintf (stderr, "I18N: X Window System doesn't support locale \"%s\"\n",
     185           0 :                 p_locale );
     186           0 :         return False;
     187             :     }
     188           0 :     return True;
     189             : }
     190             : 
     191             : // Set the operating system locale prior to trying to open an
     192             : // XIM InputMethod.
     193             : // Handle the cases where the current locale is either not supported by the
     194             : // operating system (LANG=gaga) or by the XWindow system (LANG=aa_ER@saaho)
     195             : // by providing a fallback.
     196             : // Upgrade "C" or "POSIX" to "en_US" locale to allow umlauts and accents
     197             : // see i8988, i9188, i8930, i16318
     198             : // on Solaris the environment needs to be set equivalent to the locale (#i37047#)
     199             : 
     200             : bool
     201           0 : SalI18N_InputMethod::SetLocale( const char* pLocale )
     202             : {
     203             :     // check whether we want an Input Method engine, if we don't we
     204             :     // do not need to set the locale
     205           0 :     if ( mbUseable )
     206             :     {
     207           0 :         char *locale = SetSystemLocale( pLocale );
     208           0 :         if ( (!IsXWindowCompatibleLocale(locale)) || IsPosixLocale(locale) )
     209             :         {
     210           0 :             osl_setThreadTextEncoding (RTL_TEXTENCODING_ISO_8859_1);
     211           0 :             locale = SetSystemLocale( "en_US" );
     212             :             #ifdef SOLARIS
     213             :             SetSystemEnvironment( "en_US" );
     214             :             #endif
     215           0 :             if (! IsXWindowCompatibleLocale(locale))
     216             :             {
     217           0 :                 locale = SetSystemLocale( "C" );
     218             :                 #ifdef SOLARIS
     219             :                 SetSystemEnvironment( "C" );
     220             :                 #endif
     221           0 :                 if (! IsXWindowCompatibleLocale(locale))
     222           0 :                     mbUseable = False;
     223             :             }
     224             :         }
     225             : 
     226             :         // must not fail if mbUseable since XSupportsLocale() asserts success
     227           0 :         if ( mbUseable && XSetLocaleModifiers("") == NULL )
     228             :         {
     229             :             fprintf (stderr, "I18N: Can't set X modifiers for locale \"%s\"\n",
     230           0 :                 locale);
     231           0 :             mbUseable = False;
     232             :         }
     233             :     }
     234             : 
     235           0 :     return mbUseable;
     236             : }
     237             : 
     238             : Bool
     239           0 : SalI18N_InputMethod::PosixLocale()
     240             : {
     241           0 :     if (maMethod)
     242           0 :         return IsPosixLocale (XLocaleOfIM (maMethod));
     243           0 :     return False;
     244             : }
     245             : 
     246             : // Constructor / Destructor / Initialisation
     247             : 
     248           0 : SalI18N_InputMethod::SalI18N_InputMethod( ) : mbUseable( bUseInputMethodDefault ),
     249             :                                               maMethod( (XIM)NULL ),
     250           0 :                                                 mpStyles( (XIMStyles*)NULL )
     251             : {
     252           0 :     const char *pUseInputMethod = getenv( "SAL_USEINPUTMETHOD" );
     253           0 :     if ( pUseInputMethod != NULL )
     254           0 :         mbUseable = pUseInputMethod[0] != '\0' ;
     255           0 : }
     256             : 
     257           0 : SalI18N_InputMethod::~SalI18N_InputMethod()
     258             : {
     259           0 :     ::vcl::I18NStatus::free();
     260           0 :     if ( mpStyles != NULL )
     261           0 :         XFree( mpStyles );
     262           0 :     if ( maMethod != NULL )
     263           0 :         XCloseIM ( maMethod );
     264           0 : }
     265             : 
     266             : // XXX
     267             : // debug routine: lets have a look at the provided method styles
     268             : 
     269             : #if OSL_DEBUG_LEVEL > 1
     270             : 
     271             : extern "C" char*
     272             : GetMethodName( XIMStyle nStyle, char *pBuf, int nBufSize)
     273             : {
     274             :     struct StyleName {
     275             :         const XIMStyle nStyle;
     276             :         const char    *pName;
     277             :         const int      nNameLen;
     278             :     };
     279             : 
     280             :     StyleName *pDescPtr;
     281             :     static const StyleName pDescription[] = {
     282             :         { XIMPreeditArea,      "PreeditArea ",     sizeof("PreeditArea ")   },
     283             :         { XIMPreeditCallbacks, "PreeditCallbacks ",sizeof("PreeditCallbacks ")},
     284             :         { XIMPreeditPosition,  "PreeditPosition ", sizeof("PreeditPosition ") },
     285             :         { XIMPreeditNothing,   "PreeditNothing ",  sizeof("PreeditNothing ")  },
     286             :         { XIMPreeditNone,      "PreeditNone ",     sizeof("PreeditNone ")   },
     287             :         { XIMStatusArea,       "StatusArea ",      sizeof("StatusArea ")    },
     288             :         { XIMStatusCallbacks,  "StatusCallbacks ", sizeof("StatusCallbacks ") },
     289             :         { XIMStatusNothing,    "StatusNothing ",   sizeof("StatusNothing ") },
     290             :         { XIMStatusNone,       "StatusNone ",      sizeof("StatusNone ")    },
     291             :         { 0, "NULL", 0 }
     292             :     };
     293             : 
     294             :     if ( nBufSize > 0 )
     295             :         pBuf[0] = '\0';
     296             : 
     297             :     char *pBufPtr = pBuf;
     298             :     for ( pDescPtr = const_cast<StyleName*>(pDescription); pDescPtr->nStyle != 0; pDescPtr++ )
     299             :     {
     300             :         int nSize = pDescPtr->nNameLen - 1;
     301             :         if ( (nStyle & pDescPtr->nStyle) && (nBufSize > nSize) )
     302             :         {
     303             :             strncpy( pBufPtr, pDescPtr->pName, nSize + 1);
     304             :             pBufPtr  += nSize;
     305             :             nBufSize -= nSize;
     306             :         }
     307             :     }
     308             : 
     309             :     return pBuf;
     310             : }
     311             : 
     312             : extern "C" void
     313             : PrintInputStyle( XIMStyles *pStyle )
     314             : {
     315             :     char pBuf[ 128 ];
     316             :     int  nBuf = sizeof( pBuf );
     317             : 
     318             :     if ( pStyle == NULL )
     319             :         fprintf( stderr, "no input method styles\n");
     320             :     else
     321             :     for ( int nStyle = 0; nStyle < pStyle->count_styles; nStyle++ )
     322             :     {
     323             :         fprintf( stderr, "style #%i = %s\n", nStyle,
     324             :               GetMethodName(pStyle->supported_styles[nStyle], pBuf, nBuf) );
     325             :     }
     326             : }
     327             : 
     328             : #endif
     329             : 
     330             : // this is the real constructing routine, since locale setting has to be done
     331             : // prior to xopendisplay, the xopenim call has to be delayed
     332             : 
     333             : bool
     334           0 : SalI18N_InputMethod::CreateMethod ( Display *pDisplay )
     335             : {
     336           0 :     if ( mbUseable )
     337             :     {
     338           0 :         maMethod = XOpenIM(pDisplay, NULL, NULL, NULL);
     339             : 
     340           0 :         if ((maMethod == (XIM)NULL) && (getenv("XMODIFIERS") != NULL))
     341             :         {
     342           0 :                 OUString envVar("XMODIFIERS");
     343           0 :                 osl_clearEnvironment(envVar.pData);
     344           0 :                 XSetLocaleModifiers("");
     345           0 :                 maMethod = XOpenIM(pDisplay, NULL, NULL, NULL);
     346             :         }
     347             : 
     348           0 :         if ( maMethod != (XIM)NULL )
     349             :         {
     350           0 :             if (   XGetIMValues(maMethod, XNQueryInputStyle, &mpStyles, NULL)
     351             :                 != NULL)
     352           0 :                 mbUseable = False;
     353             :             #if OSL_DEBUG_LEVEL > 1
     354             :             fprintf(stderr, "Creating Mono-Lingual InputMethod\n" );
     355             :             PrintInputStyle( mpStyles );
     356             :             #endif
     357             :         }
     358             :         else
     359             :         {
     360           0 :             mbUseable = False;
     361             :         }
     362             :     }
     363             : 
     364             :     #if OSL_DEBUG_LEVEL > 1
     365             :     if ( !mbUseable )
     366             :         fprintf(stderr, "input method creation failed\n");
     367             :     #endif
     368             : 
     369           0 :     maDestroyCallback.callback    = (XIMProc)IM_IMDestroyCallback;
     370           0 :     maDestroyCallback.client_data = (XPointer)this;
     371           0 :     if (mbUseable && maMethod != NULL)
     372           0 :         XSetIMValues(maMethod, XNDestroyCallback, &maDestroyCallback, NULL);
     373             : 
     374           0 :     return mbUseable;
     375             : }
     376             : 
     377             : // give IM the opportunity to look at the event, and possibly hide it
     378             : 
     379             : bool
     380           0 : SalI18N_InputMethod::FilterEvent( XEvent *pEvent, XLIB_Window window    )
     381             : {
     382           0 :     if (!mbUseable)
     383           0 :         return False;
     384             : 
     385           0 :     bool bFilterEvent = XFilterEvent (pEvent, window);
     386             : 
     387           0 :     if (pEvent->type != XLIB_KeyPress && pEvent->type != KeyRelease)
     388           0 :         return bFilterEvent;
     389             : 
     390             :     /*
     391             :      * fix broken key release handling of some IMs
     392             :      */
     393           0 :     XKeyEvent*         pKeyEvent = &(pEvent->xkey);
     394           0 :     static XKeyEventOp maLastKeyPress;
     395             : 
     396           0 :     if (bFilterEvent)
     397             :     {
     398           0 :         if (pKeyEvent->type == KeyRelease)
     399           0 :             bFilterEvent = !maLastKeyPress.match (*pKeyEvent);
     400           0 :         maLastKeyPress.erase();
     401             :     }
     402             :     else /* (!bFilterEvent) */
     403             :     {
     404           0 :         if (pKeyEvent->type == XLIB_KeyPress)
     405           0 :             maLastKeyPress = *pKeyEvent;
     406             :         else
     407           0 :             maLastKeyPress.erase();
     408             :     }
     409             : 
     410           0 :     return bFilterEvent;
     411             : }
     412             : 
     413             : void
     414           0 : SalI18N_InputMethod::HandleDestroyIM()
     415             : {
     416           0 :     mbUseable       = False;
     417           0 :     maMethod        = NULL;
     418           0 : }
     419             : 
     420             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10