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 void ImfManager::Finalize()
127 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Finalize\n" );
130 VirtualKeyboard::DisconnectCallbacks( mIMFContext );
131 DisconnectCallbacks();
133 ecore_imf_shutdown();
138 bool ImfManager::IsAvailable()
140 bool available( false );
142 Dali::SingletonService service( SingletonService::Get() );
145 available = service.GetSingleton( typeid( Dali::ImfManager ) );
151 Dali::ImfManager ImfManager::Get()
153 Dali::ImfManager manager;
154 ImfManager *imfManager = NULL;
156 Dali::SingletonService service( SingletonService::Get() );
159 // Check whether the singleton is already created
160 Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::ImfManager ) );
163 // If so, downcast the handle
164 imfManager = dynamic_cast< ImfManager* >( handle.GetObjectPtr() );
165 manager = Dali::ImfManager( imfManager );
167 else if ( Adaptor::IsAvailable() )
169 // Create instance and register singleton only if the adaptor is available
171 Adaptor& adaptorImpl( Adaptor::GetImplementation( Adaptor::Get() ) );
172 Any nativeWindow = adaptorImpl.GetNativeWindowHandle();
174 // The Ecore_X_Window needs to use the ImfManager.
175 // Only when the render surface is window, we can get the Ecore_X_Window.
176 Ecore_X_Window *ecoreXwin( AnyCast< Ecore_X_Window* >( nativeWindow ) );
179 // If we fail to get Ecore_X_Window, we can't use the ImfManager correctly.
180 // Thus you have to call "ecore_imf_context_client_window_set" somewhere.
181 // In EvasPlugIn, this function is called in EvasPlugin::ConnectEcoreEvent().
182 imfManager = new ImfManager( ecoreXwin );
183 manager = Dali::ImfManager( imfManager );
184 service.Register( typeid( manager ), manager );
188 DALI_LOG_ERROR("Failed to get native window handle\n");
193 if ( ( imfManager != NULL ) !imfManager->mInited )
196 imfManager->CreateContext( imfManager->mEcoreXWin );
198 imfManager->ConnectCallbacks();
199 VirtualKeyboard::ConnectCallbacks( mIMFContext );
200 imfManager->mInited = true;
206 ImfManager::ImfManager( Ecore_X_Window* ecoreXwin )
208 mEcoreXWin( ecoreXwin ),
209 mIMFCursorPosition( 0 ),
212 mRestoreAfterFocusLost( false ),
213 mIdleCallbackConnected( false )
217 ImfManager::~ImfManager()
222 void ImfManager::CreateContext( Ecore_X_Window* ecoreXwin )
224 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::CreateContext\n" );
226 const char *contextId = ecore_imf_context_default_id_get();
229 mIMFContext = ecore_imf_context_add( contextId );
235 ecore_imf_context_client_window_set( mIMFContext, reinterpret_cast<void*>( ecoreXwin ) );
240 DALI_LOG_INFO( gLogFilter, Debug::General, "IMF Unable to get IMF Context\n");
245 DALI_LOG_INFO( gLogFilter, Debug::General, "IMF Unable to get IMF Context\n");
249 void ImfManager::DeleteContext()
251 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DeleteContext\n" );
255 ecore_imf_context_del( mIMFContext );
260 // Callbacks for predicitive text support.
261 void ImfManager::ConnectCallbacks()
265 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::ConnectCallbacks\n" );
267 ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, PreEdit, this );
268 ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_COMMIT, Commit, this );
269 ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_DELETE_SURROUNDING, ImfDeleteSurrounding, this );
271 ecore_imf_context_retrieve_surrounding_callback_set( mIMFContext, ImfRetrieveSurrounding, this);
275 void ImfManager::DisconnectCallbacks()
279 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DisconnectCallbacks\n" );
281 ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, PreEdit );
282 ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_COMMIT, Commit );
283 ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_DELETE_SURROUNDING, ImfDeleteSurrounding );
285 // We do not need to unset the retrieve surrounding callback.
289 void ImfManager::Activate()
291 // Reset mIdleCallbackConnected
292 mIdleCallbackConnected = false;
296 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Activate\n" );
298 ecore_imf_context_focus_in( mIMFContext );
300 // emit keyboard activated signal
301 Dali::ImfManager handle( this );
302 mActivatedSignal.Emit( handle );
306 void ImfManager::Deactivate()
310 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Deactivate\n" );
313 ecore_imf_context_focus_out( mIMFContext );
316 // Reset mIdleCallbackConnected
317 mIdleCallbackConnected = false;
320 void ImfManager::Reset()
322 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Reset\n" );
326 ecore_imf_context_reset( mIMFContext );
330 Ecore_IMF_Context* ImfManager::GetContext()
332 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetContext\n" );
337 bool ImfManager::RestoreAfterFocusLost() const
339 return mRestoreAfterFocusLost;
342 void ImfManager::SetRestoreAfterFocusLost( bool toggle )
344 mRestoreAfterFocusLost = toggle;
348 * Called when an IMF Pre-Edit changed event is received.
349 * We are still predicting what the user is typing. The latest string is what the IMF module thinks
350 * the user wants to type.
352 void ImfManager::PreEditChanged( void*, Ecore_IMF_Context* imfContext, void* event_info )
354 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::PreEditChanged\n" );
356 char* preEditString( NULL );
357 int cursorPosition( 0 );
358 Eina_List* attrs = NULL;
361 Ecore_IMF_Preedit_Attr* attr;
363 // Retrieves attributes as well as the string the cursor position offset from start of pre-edit string.
364 // the attributes (attrs) is used in languages that use the soft arrows keys to insert characters into a current pre-edit string.
365 ecore_imf_context_preedit_string_with_attributes_get( imfContext, &preEditString, &attrs, &cursorPosition );
369 // iterate through the list of attributes getting the type, start and end position.
370 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) ) ))
372 #ifdef DALI_PROFILE_UBUNTU
373 if ( attr->preedit_type == ECORE_IMF_PREEDIT_TYPE_SUB3 ) // (Ecore_IMF)
374 #else // DALI_PROFILE_UBUNTU
375 if ( attr->preedit_type == ECORE_IMF_PREEDIT_TYPE_SUB4 ) // (Ecore_IMF)
376 #endif // DALI_PROFILE_UBUNTU
378 // 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.
380 size_t visualCharacterIndex = 0;
381 size_t byteIndex = 0;
383 // iterate through null terminated string checking each character's position against the given byte position ( attr->end_index ).
384 const char leadByte = preEditString[byteIndex];
385 while( leadByte != '\0' )
387 // attr->end_index is provided as a byte position not character and we need to know the character position.
388 const size_t currentSequenceLength = Utf8SequenceLength( leadByte ); // returns number of bytes used to represent character.
389 if ( byteIndex == attr->end_index )
391 cursorPosition = visualCharacterIndex;
393 // end loop as found cursor position that matches byte position
397 byteIndex += currentSequenceLength; // jump to next character
398 visualCharacterIndex++; // increment character count so we know our position for when we get a match
401 DALI_ASSERT_DEBUG( visualCharacterIndex < strlen( preEditString ));
407 if ( Dali::Adaptor::IsAvailable() )
409 Dali::ImfManager handle( this );
410 Dali::ImfManager::ImfEventData imfEventData( Dali::ImfManager::PREEDIT, preEditString, cursorPosition, 0 );
411 Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfEventData );
413 if( callbackData.update )
415 mIMFCursorPosition = static_cast<int>( callbackData.cursorPosition );
417 NotifyCursorPosition();
420 if( callbackData.preeditResetRequired )
425 free( preEditString );
428 void ImfManager::CommitReceived( void*, Ecore_IMF_Context* imfContext, void* event_info )
430 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::CommitReceived\n" );
432 if ( Dali::Adaptor::IsAvailable() )
434 const std::string keyString( static_cast<char*>( event_info ) );
436 Dali::ImfManager handle( this );
437 Dali::ImfManager::ImfEventData imfEventData( Dali::ImfManager::COMMIT, keyString, 0, 0 );
438 Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfEventData );
440 if( callbackData.update )
442 mIMFCursorPosition = static_cast<int>( callbackData.cursorPosition );
444 NotifyCursorPosition();
450 * Called when an IMF retrieve surround event is received.
451 * Here the IMF module wishes to know the string we are working with and where within the string the cursor is
452 * We need to signal the application to tell us this information.
454 Eina_Bool ImfManager::RetrieveSurrounding( void* data, Ecore_IMF_Context* imfContext, char** text, int* cursorPosition )
456 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::RetrieveSurrounding\n" );
458 Dali::ImfManager::ImfEventData imfData( Dali::ImfManager::GETSURROUNDING, std::string(), 0, 0 );
459 Dali::ImfManager handle( this );
460 Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfData );
462 if( callbackData.update )
466 // The memory allocated by strdup() can be freed by ecore_imf_context_surrounding_get() internally.
467 *text = strdup( callbackData.currentText.c_str() );
472 mIMFCursorPosition = static_cast<int>( callbackData.cursorPosition );
473 *cursorPosition = mIMFCursorPosition;
481 * Called when an IMF delete surrounding event is received.
482 * Here we tell the application that it should delete a certain range.
484 void ImfManager::DeleteSurrounding( void* data, Ecore_IMF_Context* imfContext, void* event_info )
486 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DeleteSurrounding\n" );
488 if( Dali::Adaptor::IsAvailable() )
490 Ecore_IMF_Event_Delete_Surrounding* deleteSurroundingEvent = static_cast<Ecore_IMF_Event_Delete_Surrounding*>( event_info );
492 Dali::ImfManager::ImfEventData imfData( Dali::ImfManager::DELETESURROUNDING, std::string(), deleteSurroundingEvent->offset, deleteSurroundingEvent->n_chars );
493 Dali::ImfManager handle( this );
494 Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfData );
496 if( callbackData.update )
498 mIMFCursorPosition = static_cast<int>( callbackData.cursorPosition );
500 NotifyCursorPosition();
505 void ImfManager::NotifyCursorPosition()
507 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::NotifyCursorPosition\n" );
511 ecore_imf_context_cursor_position_set( mIMFContext, mIMFCursorPosition );
515 void ImfManager::SetCursorPosition( unsigned int cursorPosition )
517 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetCursorPosition\n" );
519 mIMFCursorPosition = static_cast<int>( cursorPosition );
522 unsigned int ImfManager::GetCursorPosition() const
524 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetCursorPosition\n" );
526 return static_cast<unsigned int>( mIMFCursorPosition );
529 void ImfManager::SetSurroundingText( const std::string& text )
531 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetSurroundingText\n" );
533 mSurroundingText = text;
536 const std::string& ImfManager::GetSurroundingText() const
538 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetSurroundingText\n" );
540 return mSurroundingText;
543 void ImfManager::NotifyTextInputMultiLine( bool multiLine )
547 Dali::ImfManager::TextDirection ImfManager::GetTextDirection()
549 Dali::ImfManager::TextDirection direction ( Dali::ImfManager::LeftToRight );
551 if ( ImfManager::IsAvailable() /* We do not want to create an instance of ImfManager */ )
555 char* locale( NULL );
556 ecore_imf_context_input_panel_language_locale_get( mIMFContext, &locale );
560 direction = static_cast< Dali::ImfManager::TextDirection >( Locale::GetDirection( std::string( locale ) ) );
568 Rect<int> ImfManager::GetInputMethodArea()
570 int xPos, yPos, width, height;
572 width = height = xPos = yPos = 0;
576 ecore_imf_context_input_panel_geometry_get( mIMFContext, &xPos, &yPos, &width, &height );
580 DALI_LOG_WARNING("VKB Unable to get IMF Context so GetSize unavailable\n");
583 return Rect<int>(xPos,yPos,width,height);
586 void ImfManager::ApplyOptions( const InputMethodOptions& options )
588 using namespace Dali::InputMethod::Category;
592 if (mIMFContext == NULL)
594 DALI_LOG_WARNING("VKB Unable to excute ApplyOptions with Null ImfContext\n");
598 if ( mOptions.CompareAndSet(PANEL_LAYOUT, options, index) )
601 if ( mOptions.CompareAndSet(AUTO_CAPITALISE, options, index) )
604 if ( mOptions.CompareAndSet(ACTION_BUTTON_TITLE, options, index) )
607 if ( mOptions.CompareAndSet(VARIATION, options, index) )
612 void ImfManager::SetInputPanelData( const std::string& data )
614 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetInputPanelData\n" );
618 int length = data.length();
619 ecore_imf_context_input_panel_imdata_set( mIMFContext, data.c_str(), length );
623 void ImfManager::GetInputPanelData( std::string& data )
625 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetInputPanelData\n" );
629 int length = 4096; // The max length is 4096 bytes
630 Dali::Vector< char > buffer;
631 buffer.Resize( length );
632 ecore_imf_context_input_panel_imdata_get( mIMFContext, &buffer[0], &length );
633 data = std::string( buffer.Begin(), buffer.End() );
637 Dali::ImfManager::State ImfManager::GetInputPanelState()
639 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetInputPanelState\n" );
644 value = ecore_imf_context_input_panel_state_get( mIMFContext );
648 case ECORE_IMF_INPUT_PANEL_STATE_SHOW:
650 return Dali::ImfManager::SHOW;
654 case ECORE_IMF_INPUT_PANEL_STATE_HIDE:
656 return Dali::ImfManager::HIDE;
660 case ECORE_IMF_INPUT_PANEL_STATE_WILL_SHOW:
662 return Dali::ImfManager::WILL_SHOW;
668 return Dali::ImfManager::DEFAULT;
672 return Dali::ImfManager::DEFAULT;
675 void ImfManager::SetReturnKeyState( bool visible )
677 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetReturnKeyState\n" );
681 ecore_imf_context_input_panel_return_key_disabled_set( mIMFContext, !visible );
685 void ImfManager::AutoEnableInputPanel( bool enabled )
687 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::AutoEnableInputPanel\n" );
691 ecore_imf_context_input_panel_enabled_set( mIMFContext, enabled );
695 void ImfManager::ShowInputPanel()
697 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::ShowInputPanel\n" );
701 ecore_imf_context_input_panel_show( mIMFContext );
705 void ImfManager::HideInputPanel()
707 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::HideInputPanel\n" );
711 ecore_imf_context_input_panel_hide( mIMFContext );
715 Dali::ImfManager::KeyboardType ImfManager::GetKeyboardType()
717 return Dali::ImfManager::KeyboardType::SOFTWARE_KEYBOARD;
720 std::string ImfManager::GetInputPanelLocale()
722 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetInputPanelLocale\n" );
724 std::string locale = "";
729 ecore_imf_context_input_panel_language_locale_get( mIMFContext, &value );
733 std::string valueCopy( value );
736 // The locale string retrieved must be freed with free().
749 #pragma GCC diagnostic pop