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 // The memory allocated by strdup() can be freed by ecore_imf_context_surrounding_get() internally.
449 *text = strdup( callbackData.currentText.c_str() );
454 mIMFCursorPosition = static_cast<int>( callbackData.cursorPosition );
455 *cursorPosition = mIMFCursorPosition;
463 * Called when an IMF delete surrounding event is received.
464 * Here we tell the application that it should delete a certain range.
466 void ImfManager::DeleteSurrounding( void* data, Ecore_IMF_Context* imfContext, void* event_info )
468 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DeleteSurrounding\n" );
470 if( Dali::Adaptor::IsAvailable() )
472 Ecore_IMF_Event_Delete_Surrounding* deleteSurroundingEvent = static_cast<Ecore_IMF_Event_Delete_Surrounding*>( event_info );
474 Dali::ImfManager::ImfEventData imfData( Dali::ImfManager::DELETESURROUNDING, std::string(), deleteSurroundingEvent->offset, deleteSurroundingEvent->n_chars );
475 Dali::ImfManager handle( this );
476 Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfData );
478 if( callbackData.update )
480 mIMFCursorPosition = static_cast<int>( callbackData.cursorPosition );
482 NotifyCursorPosition();
487 void ImfManager::NotifyCursorPosition()
489 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::NotifyCursorPosition\n" );
493 ecore_imf_context_cursor_position_set( mIMFContext, mIMFCursorPosition );
497 void ImfManager::SetCursorPosition( unsigned int cursorPosition )
499 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetCursorPosition\n" );
501 mIMFCursorPosition = static_cast<int>( cursorPosition );
504 unsigned int ImfManager::GetCursorPosition() const
506 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetCursorPosition\n" );
508 return static_cast<unsigned int>( mIMFCursorPosition );
511 void ImfManager::SetSurroundingText( const std::string& text )
513 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetSurroundingText\n" );
515 mSurroundingText = text;
518 const std::string& ImfManager::GetSurroundingText() const
520 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetSurroundingText\n" );
522 return mSurroundingText;
525 void ImfManager::NotifyTextInputMultiLine( bool multiLine )
529 Dali::ImfManager::TextDirection ImfManager::GetTextDirection()
531 Dali::ImfManager::TextDirection direction ( Dali::ImfManager::LeftToRight );
533 if ( ImfManager::IsAvailable() /* We do not want to create an instance of ImfManager */ )
537 char* locale( NULL );
538 ecore_imf_context_input_panel_language_locale_get( mIMFContext, &locale );
542 direction = static_cast< Dali::ImfManager::TextDirection >( Locale::GetDirection( std::string( locale ) ) );
550 Rect<int> ImfManager::GetInputMethodArea()
552 int xPos, yPos, width, height;
554 width = height = xPos = yPos = 0;
558 ecore_imf_context_input_panel_geometry_get( mIMFContext, &xPos, &yPos, &width, &height );
562 DALI_LOG_WARNING("VKB Unable to get IMF Context so GetSize unavailable\n");
565 return Rect<int>(xPos,yPos,width,height);
568 void ImfManager::ApplyOptions( const InputMethodOptions& options )
570 using namespace Dali::InputMethod::Category;
574 if (mIMFContext == NULL)
576 DALI_LOG_WARNING("VKB Unable to excute ApplyOptions with Null ImfContext\n");
580 if ( mOptions.CompareAndSet(PANEL_LAYOUT, options, index) )
583 if ( mOptions.CompareAndSet(AUTO_CAPITALISE, options, index) )
586 if ( mOptions.CompareAndSet(ACTION_BUTTON_TITLE, options, index) )
589 if ( mOptions.CompareAndSet(VARIATION, options, index) )
594 void ImfManager::SetInputPanelData( const std::string& data )
596 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetInputPanelData\n" );
600 int length = data.length();
601 ecore_imf_context_input_panel_imdata_set( mIMFContext, data.c_str(), length );
605 void ImfManager::GetInputPanelData( std::string& data )
607 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetInputPanelData\n" );
611 int length = 4096; // The max length is 4096 bytes
612 Dali::Vector< char > buffer;
613 buffer.Resize( length );
614 ecore_imf_context_input_panel_imdata_get( mIMFContext, &buffer[0], &length );
615 data = std::string( buffer.Begin(), buffer.End() );
619 Dali::ImfManager::State ImfManager::GetInputPanelState()
621 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetInputPanelState\n" );
626 value = ecore_imf_context_input_panel_state_get( mIMFContext );
630 case ECORE_IMF_INPUT_PANEL_STATE_SHOW:
632 return Dali::ImfManager::SHOW;
636 case ECORE_IMF_INPUT_PANEL_STATE_HIDE:
638 return Dali::ImfManager::HIDE;
642 case ECORE_IMF_INPUT_PANEL_STATE_WILL_SHOW:
644 return Dali::ImfManager::WILL_SHOW;
650 return Dali::ImfManager::DEFAULT;
654 return Dali::ImfManager::DEFAULT;
657 void ImfManager::SetReturnKeyState( bool visible )
659 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetReturnKeyState\n" );
663 ecore_imf_context_input_panel_return_key_disabled_set( mIMFContext, !visible );
667 void ImfManager::AutoEnableInputPanel( bool enabled )
669 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::AutoEnableInputPanel\n" );
673 ecore_imf_context_input_panel_enabled_set( mIMFContext, enabled );
677 void ImfManager::ShowInputPanel()
679 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::ShowInputPanel\n" );
683 ecore_imf_context_input_panel_show( mIMFContext );
687 void ImfManager::HideInputPanel()
689 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::HideInputPanel\n" );
693 ecore_imf_context_input_panel_hide( mIMFContext );
697 Dali::ImfManager::KeyboardType ImfManager::GetKeyboardType()
699 return Dali::ImfManager::KeyboardType::SOFTWARE_KEYBOARD;
702 std::string ImfManager::GetInputPanelLocale()
704 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetInputPanelLocale\n" );
706 std::string locale = "";
711 ecore_imf_context_input_panel_language_locale_get( mIMFContext, &value );
715 std::string valueCopy( value );
718 // The locale string retrieved must be freed with free().
731 #pragma GCC diagnostic pop