2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <imf-manager-impl.h>
22 #include <dali/public-api/events/key-event.h>
23 #include <dali/public-api/object/type-registry.h>
24 #include <dali/integration-api/debug.h>
28 #include <window-render-surface.h>
29 #include <adaptor-impl.h>
30 #include <locale-utils.h>
31 #include <singleton-service-impl.h>
32 #include <virtual-keyboard-impl.h>
33 // Ecore is littered with C style cast
34 #pragma GCC diagnostic push
35 #pragma GCC diagnostic ignored "-Wold-style-cast"
36 #include "ecore-virtual-keyboard.h"
49 #if defined(DEBUG_ENABLED)
50 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_IMF_MANAGER");
53 // Currently this code is internal to dali/dali/internal/event/text/utf8.h but should be made Public and used from there instead.
54 size_t Utf8SequenceLength(const unsigned char leadByte)
58 if ((leadByte & 0x80) == 0 ) //ASCII character (lead bit zero)
62 else if (( leadByte & 0xe0 ) == 0xc0 ) //110x xxxx
66 else if (( leadByte & 0xf0 ) == 0xe0 ) //1110 xxxx
70 else if (( leadByte & 0xf8 ) == 0xf0 ) //1111 0xxx
78 // Static function calls used by ecore 'c' style callback registration
79 void Commit( void *data, Ecore_IMF_Context *imfContext, void *event_info )
83 ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
84 imfManager->CommitReceived( data, imfContext, event_info );
88 void PreEdit( void *data, Ecore_IMF_Context *imfContext, void *event_info )
92 ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
93 imfManager->PreEditChanged( data, imfContext, event_info );
97 Eina_Bool ImfRetrieveSurrounding(void *data, Ecore_IMF_Context *imfContext, char** text, int* cursorPosition )
101 ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
102 return imfManager->RetrieveSurrounding( data, imfContext, text, cursorPosition );
111 * Called when an IMF delete surrounding event is received.
112 * Here we tell the application that it should delete a certain range.
114 void ImfDeleteSurrounding( void *data, Ecore_IMF_Context *imfContext, void *event_info )
118 ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
119 imfManager->DeleteSurrounding( data, imfContext, event_info );
123 } // unnamed namespace
125 bool ImfManager::IsAvailable()
127 bool available( false );
129 Dali::SingletonService service( SingletonService::Get() );
132 available = service.GetSingleton( typeid( Dali::ImfManager ) );
138 Dali::ImfManager ImfManager::Get()
140 Dali::ImfManager manager;
142 Dali::SingletonService service( SingletonService::Get() );
145 // Check whether the singleton is already created
146 Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::ImfManager ) );
149 // If so, downcast the handle
150 manager = Dali::ImfManager( dynamic_cast< ImfManager* >( handle.GetObjectPtr() ) );
152 else if ( Adaptor::IsAvailable() )
154 // Create instance and register singleton only if the adaptor is available
156 Adaptor& adaptorImpl( Adaptor::GetImplementation( Adaptor::Get() ) );
157 Any nativeWindow = adaptorImpl.GetNativeWindowHandle();
159 // The Ecore_X_Window needs to use the ImfManager.
160 // Only when the render surface is window, we can get the Ecore_X_Window.
161 Ecore_X_Window ecoreXwin( AnyCast<Ecore_X_Window>(nativeWindow) );
164 // If we fail to get Ecore_X_Window, we can't use the ImfManager correctly.
165 // Thus you have to call "ecore_imf_context_client_window_set" somewhere.
166 // In EvasPlugIn, this function is called in EvasPlugin::ConnectEcoreEvent().
168 manager = Dali::ImfManager( new ImfManager( ecoreXwin ) );
169 service.Register( typeid( manager ), manager );
173 DALI_LOG_ERROR("Failed to get native window handle\n");
181 ImfManager::ImfManager( Ecore_X_Window ecoreXwin )
183 mIMFCursorPosition( 0 ),
185 mRestoreAfterFocusLost( false ),
186 mIdleCallbackConnected( false )
189 CreateContext( ecoreXwin );
192 VirtualKeyboard::ConnectCallbacks( mIMFContext );
195 ImfManager::~ImfManager()
197 VirtualKeyboard::DisconnectCallbacks( mIMFContext );
198 DisconnectCallbacks();
201 ecore_imf_shutdown();
204 void ImfManager::CreateContext( Ecore_X_Window ecoreXwin )
206 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::CreateContext\n" );
208 const char *contextId = ecore_imf_context_default_id_get();
211 mIMFContext = ecore_imf_context_add( contextId );
217 ecore_imf_context_client_window_set( mIMFContext, reinterpret_cast<void*>( ecoreXwin ) );
222 DALI_LOG_INFO( gLogFilter, Debug::General, "IMF Unable to get IMF Context\n");
227 DALI_LOG_INFO( gLogFilter, Debug::General, "IMF Unable to get IMF Context\n");
231 void ImfManager::DeleteContext()
233 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DeleteContext\n" );
237 ecore_imf_context_del( mIMFContext );
242 // Callbacks for predicitive text support.
243 void ImfManager::ConnectCallbacks()
247 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::ConnectCallbacks\n" );
249 ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, PreEdit, this );
250 ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_COMMIT, Commit, this );
251 ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_DELETE_SURROUNDING, ImfDeleteSurrounding, this );
253 ecore_imf_context_retrieve_surrounding_callback_set( mIMFContext, ImfRetrieveSurrounding, this);
257 void ImfManager::DisconnectCallbacks()
261 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DisconnectCallbacks\n" );
263 ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, PreEdit );
264 ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_COMMIT, Commit );
265 ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_DELETE_SURROUNDING, ImfDeleteSurrounding );
267 // We do not need to unset the retrieve surrounding callback.
271 void ImfManager::Activate()
273 // Reset mIdleCallbackConnected
274 mIdleCallbackConnected = false;
278 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Activate\n" );
280 ecore_imf_context_focus_in( mIMFContext );
282 // emit keyboard activated signal
283 Dali::ImfManager handle( this );
284 mActivatedSignal.Emit( handle );
288 void ImfManager::Deactivate()
292 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Deactivate\n" );
295 ecore_imf_context_focus_out( mIMFContext );
298 // Reset mIdleCallbackConnected
299 mIdleCallbackConnected = false;
302 void ImfManager::Reset()
304 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Reset\n" );
308 ecore_imf_context_reset( mIMFContext );
312 Ecore_IMF_Context* ImfManager::GetContext()
314 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetContext\n" );
319 bool ImfManager::RestoreAfterFocusLost() const
321 return mRestoreAfterFocusLost;
324 void ImfManager::SetRestoreAfterFocusLost( bool toggle )
326 mRestoreAfterFocusLost = toggle;
330 * Called when an IMF Pre-Edit changed event is received.
331 * We are still predicting what the user is typing. The latest string is what the IMF module thinks
332 * the user wants to type.
334 void ImfManager::PreEditChanged( void*, Ecore_IMF_Context* imfContext, void* event_info )
336 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::PreEditChanged\n" );
338 char* preEditString( NULL );
339 int cursorPosition( 0 );
340 Eina_List* attrs = NULL;
343 Ecore_IMF_Preedit_Attr* attr;
345 // Retrieves attributes as well as the string the cursor position offset from start of pre-edit string.
346 // the attributes (attrs) is used in languages that use the soft arrows keys to insert characters into a current pre-edit string.
347 ecore_imf_context_preedit_string_with_attributes_get( imfContext, &preEditString, &attrs, &cursorPosition );
351 // iterate through the list of attributes getting the type, start and end position.
352 for ( l = attrs, (attr = static_cast<Ecore_IMF_Preedit_Attr*>( eina_list_data_get(l) ) ); l; l = eina_list_next(l), ( attr = static_cast<Ecore_IMF_Preedit_Attr*>( eina_list_data_get(l) ) ))
354 #ifdef DALI_PROFILE_UBUNTU
355 if ( attr->preedit_type == ECORE_IMF_PREEDIT_TYPE_SUB3 ) // (Ecore_IMF)
356 #else // DALI_PROFILE_UBUNTU
357 if ( attr->preedit_type == ECORE_IMF_PREEDIT_TYPE_SUB4 ) // (Ecore_IMF)
358 #endif // DALI_PROFILE_UBUNTU
360 // check first byte so know how many bytes a character is represented by as keyboard returns cursor position in bytes. Which is different for some languages.
362 size_t visualCharacterIndex = 0;
363 size_t byteIndex = 0;
365 // iterate through null terminated string checking each character's position against the given byte position ( attr->end_index ).
366 const char leadByte = preEditString[byteIndex];
367 while( leadByte != '\0' )
369 // attr->end_index is provided as a byte position not character and we need to know the character position.
370 const size_t currentSequenceLength = Utf8SequenceLength( leadByte ); // returns number of bytes used to represent character.
371 if ( byteIndex == attr->end_index )
373 cursorPosition = visualCharacterIndex;
375 // end loop as found cursor position that matches byte position
379 byteIndex += currentSequenceLength; // jump to next character
380 visualCharacterIndex++; // increment character count so we know our position for when we get a match
383 DALI_ASSERT_DEBUG( visualCharacterIndex < strlen( preEditString ));
389 if ( Dali::Adaptor::IsAvailable() )
391 Dali::ImfManager handle( this );
392 Dali::ImfManager::ImfEventData imfEventData( Dali::ImfManager::PREEDIT, preEditString, cursorPosition, 0 );
393 Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfEventData );
395 if( callbackData.update )
397 mIMFCursorPosition = static_cast<int>( callbackData.cursorPosition );
399 NotifyCursorPosition();
402 if( callbackData.preeditResetRequired )
407 free( preEditString );
410 void ImfManager::CommitReceived( void*, Ecore_IMF_Context* imfContext, void* event_info )
412 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::CommitReceived\n" );
414 if ( Dali::Adaptor::IsAvailable() )
416 const std::string keyString( static_cast<char*>( event_info ) );
418 Dali::ImfManager handle( this );
419 Dali::ImfManager::ImfEventData imfEventData( Dali::ImfManager::COMMIT, keyString, 0, 0 );
420 Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfEventData );
422 if( callbackData.update )
424 mIMFCursorPosition = static_cast<int>( callbackData.cursorPosition );
426 NotifyCursorPosition();
432 * Called when an IMF retrieve surround event is received.
433 * Here the IMF module wishes to know the string we are working with and where within the string the cursor is
434 * We need to signal the application to tell us this information.
436 Eina_Bool ImfManager::RetrieveSurrounding( void* data, Ecore_IMF_Context* imfContext, char** text, int* cursorPosition )
438 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::RetrieveSurrounding\n" );
440 Dali::ImfManager::ImfEventData imfData( Dali::ImfManager::GETSURROUNDING, std::string(), 0, 0 );
441 Dali::ImfManager handle( this );
442 Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfData );
444 if( callbackData.update )
448 *text = strdup( callbackData.currentText.c_str() );
453 mIMFCursorPosition = static_cast<int>( callbackData.cursorPosition );
454 *cursorPosition = mIMFCursorPosition;
462 * Called when an IMF delete surrounding event is received.
463 * Here we tell the application that it should delete a certain range.
465 void ImfManager::DeleteSurrounding( void* data, Ecore_IMF_Context* imfContext, void* event_info )
467 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DeleteSurrounding\n" );
469 if( Dali::Adaptor::IsAvailable() )
471 Ecore_IMF_Event_Delete_Surrounding* deleteSurroundingEvent = static_cast<Ecore_IMF_Event_Delete_Surrounding*>( event_info );
473 Dali::ImfManager::ImfEventData imfData( Dali::ImfManager::DELETESURROUNDING, std::string(), deleteSurroundingEvent->offset, deleteSurroundingEvent->n_chars );
474 Dali::ImfManager handle( this );
475 Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfData );
477 if( callbackData.update )
479 mIMFCursorPosition = static_cast<int>( callbackData.cursorPosition );
481 NotifyCursorPosition();
486 void ImfManager::NotifyCursorPosition()
488 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::NotifyCursorPosition\n" );
492 ecore_imf_context_cursor_position_set( mIMFContext, mIMFCursorPosition );
496 void ImfManager::SetCursorPosition( unsigned int cursorPosition )
498 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetCursorPosition\n" );
500 mIMFCursorPosition = static_cast<int>( cursorPosition );
503 unsigned int ImfManager::GetCursorPosition() const
505 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetCursorPosition\n" );
507 return static_cast<unsigned int>( mIMFCursorPosition );
510 void ImfManager::SetSurroundingText( const std::string& text )
512 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetSurroundingText\n" );
514 mSurroundingText = text;
517 const std::string& ImfManager::GetSurroundingText() const
519 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetSurroundingText\n" );
521 return mSurroundingText;
524 void ImfManager::NotifyTextInputMultiLine( bool multiLine )
528 Dali::ImfManager::TextDirection ImfManager::GetTextDirection()
530 Dali::ImfManager::TextDirection direction ( Dali::ImfManager::LeftToRight );
532 if ( ImfManager::IsAvailable() /* We do not want to create an instance of ImfManager */ )
536 char* locale( NULL );
537 ecore_imf_context_input_panel_language_locale_get( mIMFContext, &locale );
541 direction = Locale::GetTextDirection( std::string( locale ) );
549 Rect<int> ImfManager::GetInputMethodArea()
551 int xPos, yPos, width, height;
553 width = height = xPos = yPos = 0;
557 ecore_imf_context_input_panel_geometry_get( mIMFContext, &xPos, &yPos, &width, &height );
561 DALI_LOG_WARNING("VKB Unable to get IMF Context so GetSize unavailable\n");
564 return Rect<int>(xPos,yPos,width,height);
567 void ImfManager::ApplyOptions( const InputMethodOptions& options )
569 using namespace Dali::InputMethod::Category;
573 if (mIMFContext == NULL)
575 DALI_LOG_WARNING("VKB Unable to excute ApplyOptions with Null ImfContext\n");
579 if ( mOptions.CompareAndSet(PANEL_LAYOUT, options, index) )
582 if ( mOptions.CompareAndSet(AUTO_CAPITALISE, options, index) )
585 if ( mOptions.CompareAndSet(ACTION_BUTTON_TITLE, options, index) )
588 if ( mOptions.CompareAndSet(VARIATION, options, index) )
593 void ImfManager::SetInputPanelUserData( const std::string& data )
595 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetInputPanelUserData\n" );
599 int length = data.length();
600 ecore_imf_context_input_panel_imdata_set( mIMFContext, &data, length );
604 void ImfManager::GetInputPanelUserData( std::string& data )
606 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetInputPanelUserData\n" );
611 ecore_imf_context_input_panel_imdata_get( mIMFContext, &data, length );
615 Dali::ImfManager::State ImfManager::GetInputPanelState()
617 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetInputPanelState\n" );
622 value = ecore_imf_context_input_panel_state_get( mIMFContext );
626 case ECORE_IMF_INPUT_PANEL_STATE_SHOW:
628 return Dali::ImfManager::SHOW;
632 case ECORE_IMF_INPUT_PANEL_STATE_HIDE:
634 return Dali::ImfManager::HIDE;
638 case ECORE_IMF_INPUT_PANEL_STATE_WILL_SHOW:
640 return Dali::ImfManager::WILL_SHOW;
646 return Dali::ImfManager::DEFAULT;
650 return Dali::ImfManager::DEFAULT;
653 void ImfManager::SetReturnKeyState( bool visible )
655 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetReturnKeyState\n" );
659 ecore_imf_context_input_panel_return_key_disabled_set( mIMFContext, !visible );
663 void ImfManager::AutoEnableInputPanel( bool enabled )
665 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::AutoEnableInputPanel\n" );
669 ecore_imf_context_input_panel_enabled_set( mIMFContext, enabled );
673 void ImfManager::ShowInputPanel()
675 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::ShowInputPanel\n" );
679 ecore_imf_context_input_panel_show( mIMFContext );
683 void ImfManager::HideInputPanel()
685 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::HideInputPanel\n" );
689 ecore_imf_context_input_panel_hide( mIMFContext );
693 Dali::ImfManager::KeyboardType ImfManager::GetKeyboardType()
695 return Dali::ImfManager::KeyboardType::SOFTWARE_KEYBOARD;
698 std::string ImfManager::GetInputPanelLocale()
700 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetInputPanelLocale\n" );
702 std::string locale = "";
707 ecore_imf_context_input_panel_language_locale_get( mIMFContext, &value );
711 std::string valueCopy( value );
714 // The locale string retrieved must be freed with free().
727 #pragma GCC diagnostic pop