2 * Copyright (c) 2014 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 <singleton-service-impl.h>
31 #include <virtual-keyboard-impl.h>
44 #if defined(DEBUG_ENABLED)
45 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_IMF_MANAGER");
48 // Currently this code is internal to dali/dali/internal/event/text/utf8.h but should be made Public and used from there instead.
49 size_t Utf8SequenceLength(const unsigned char leadByte)
53 if ((leadByte & 0x80) == 0 ) //ASCII character (lead bit zero)
57 else if (( leadByte & 0xe0 ) == 0xc0 ) //110x xxxx
61 else if (( leadByte & 0xf0 ) == 0xe0 ) //1110 xxxx
65 else if (( leadByte & 0xf8 ) == 0xf0 ) //1111 0xxx
73 // Static function calls used by ecore 'c' style callback registration
74 void Commit( void *data, Ecore_IMF_Context *imfContext, void *event_info )
78 ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
79 imfManager->CommitReceived( data, imfContext, event_info );
83 void PreEdit( void *data, Ecore_IMF_Context *imfContext, void *event_info )
87 ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
88 imfManager->PreEditChanged( data, imfContext, event_info );
92 Eina_Bool ImfRetrieveSurrounding(void *data, Ecore_IMF_Context *imfContext, char** text, int* cursorPosition )
96 ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
97 return imfManager->RetrieveSurrounding( data, imfContext, text, cursorPosition );
106 * Called when an IMF delete surrounding event is received.
107 * Here we tell the application that it should delete a certain range.
109 void ImfDeleteSurrounding( void *data, Ecore_IMF_Context *imfContext, void *event_info )
113 ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
114 imfManager->DeleteSurrounding( data, imfContext, event_info );
120 return ImfManager::Get();
123 TypeRegistration IMF_MANAGER_TYPE( typeid(Dali::ImfManager), typeid(Dali::BaseHandle), Create );
125 } // unnamed namespace
127 bool ImfManager::IsAvailable()
129 bool available( false );
131 Dali::SingletonService service( SingletonService::Get() );
134 available = service.GetSingleton( typeid( Dali::ImfManager ) );
140 Dali::ImfManager ImfManager::Get()
142 Dali::ImfManager manager;
144 Dali::SingletonService service( SingletonService::Get() );
147 // Check whether the singleton is already created
148 Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::ImfManager ) );
151 // If so, downcast the handle
152 manager = Dali::ImfManager( dynamic_cast< ImfManager* >( handle.GetObjectPtr() ) );
154 else if ( Adaptor::IsAvailable() )
156 // Create instance and register singleton only if the adaptor is available
158 Adaptor& adaptorImpl( Adaptor::GetImplementation( Adaptor::Get() ) );
159 Any nativeWindow = adaptorImpl.GetNativeWindowHandle();
161 // The Ecore_X_Window needs to use the ImfManager.
162 // Only when the render surface is window, we can get the Ecore_X_Window.
163 Ecore_X_Window ecoreXwin( AnyCast<Ecore_X_Window>(nativeWindow) );
166 // If we fail to get Ecore_X_Window, we can't use the ImfManager correctly.
167 // Thus you have to call "ecore_imf_context_client_window_set" somewhere.
168 // In EvasPlugIn, this function is called in EvasPlugin::ConnectEcoreEvent().
170 manager = Dali::ImfManager( new ImfManager( ecoreXwin ) );
171 service.Register( typeid( manager ), manager );
175 DALI_LOG_ERROR("Failed to get native window handle");
183 ImfManager::ImfManager( Ecore_X_Window ecoreXwin )
185 mIMFCursorPosition( 0 ),
187 mRestoreAfterFocusLost( false ),
188 mIdleCallbackConnected( false )
191 CreateContext( ecoreXwin );
194 VirtualKeyboard::ConnectCallbacks( mIMFContext );
197 ImfManager::~ImfManager()
199 VirtualKeyboard::DisconnectCallbacks( mIMFContext );
200 DisconnectCallbacks();
203 ecore_imf_shutdown();
206 void ImfManager::CreateContext( Ecore_X_Window ecoreXwin )
208 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::CreateContext\n" );
210 const char *contextId = ecore_imf_context_default_id_get();
213 mIMFContext = ecore_imf_context_add( contextId );
219 ecore_imf_context_client_window_set( mIMFContext, reinterpret_cast<void*>( ecoreXwin ) );
224 DALI_LOG_WARNING("IMF Unable to get IMF Context\n");
229 DALI_LOG_WARNING("IMF Unable to get IMF Context\n");
233 void ImfManager::DeleteContext()
235 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DeleteContext\n" );
243 // Callbacks for predicitive text support.
244 void ImfManager::ConnectCallbacks()
248 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::ConnectCallbacks\n" );
250 ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, PreEdit, this );
251 ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_COMMIT, Commit, this );
252 ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_DELETE_SURROUNDING, ImfDeleteSurrounding, this );
254 ecore_imf_context_retrieve_surrounding_callback_set( mIMFContext, ImfRetrieveSurrounding, this);
258 void ImfManager::DisconnectCallbacks()
262 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DisconnectCallbacks\n" );
264 ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, PreEdit );
265 ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_COMMIT, Commit );
266 ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_DELETE_SURROUNDING, ImfDeleteSurrounding );
268 // We do not need to unset the retrieve surrounding callback.
272 void ImfManager::Activate()
274 // Reset mIdleCallbackConnected
275 mIdleCallbackConnected = false;
279 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Activate\n" );
281 ecore_imf_context_focus_in( mIMFContext );
283 // emit keyboard activated signal
284 Dali::ImfManager handle( this );
285 mActivatedSignal.Emit( handle );
289 void ImfManager::Deactivate()
293 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Deactivate\n" );
296 ecore_imf_context_focus_out( mIMFContext );
299 // Reset mIdleCallbackConnected
300 mIdleCallbackConnected = false;
303 void ImfManager::Reset()
305 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Reset\n" );
309 ecore_imf_context_reset( mIMFContext );
313 Ecore_IMF_Context* ImfManager::GetContext()
315 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetContext\n" );
320 bool ImfManager::RestoreAfterFocusLost() const
322 return mRestoreAfterFocusLost;
325 void ImfManager::SetRestoreAfterFocusLost( bool toggle )
327 mRestoreAfterFocusLost = toggle;
331 * Called when an IMF Pre-Edit changed event is received.
332 * We are still predicting what the user is typing. The latest string is what the IMF module thinks
333 * the user wants to type.
335 void ImfManager::PreEditChanged( void*, Ecore_IMF_Context* imfContext, void* event_info )
337 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::PreEditChanged\n" );
339 char* preEditString( NULL );
340 int cursorPosition( 0 );
341 Eina_List* attrs = NULL;
344 Ecore_IMF_Preedit_Attr* attr;
346 // Retrieves attributes as well as the string the cursor position offset from start of pre-edit string.
347 // the attributes (attrs) is used in languages that use the soft arrows keys to insert characters into a current pre-edit string.
348 ecore_imf_context_preedit_string_with_attributes_get( imfContext, &preEditString, &attrs, &cursorPosition );
352 // iterate through the list of attributes getting the type, start and end position.
353 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) ) ))
355 #ifdef DALI_PROFILE_UBUNTU
356 if ( attr->preedit_type == ECORE_IMF_PREEDIT_TYPE_SUB3 ) // (Ecore_IMF)
357 #else // DALI_PROFILE_UBUNTU
358 if ( attr->preedit_type == ECORE_IMF_PREEDIT_TYPE_SUB4 ) // (Ecore_IMF)
359 #endif // DALI_PROFILE_UBUNTU
361 // 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.
363 size_t visualCharacterIndex = 0;
364 size_t byteIndex = 0;
366 // iterate through null terminated string checking each character's position against the given byte position ( attr->end_index ).
367 const char leadByte = preEditString[byteIndex];
368 while( leadByte != '\0' )
370 // attr->end_index is provided as a byte position not character and we need to know the character position.
371 const size_t currentSequenceLength = Utf8SequenceLength( leadByte ); // returns number of bytes used to represent character.
372 if ( byteIndex == attr->end_index )
374 cursorPosition = visualCharacterIndex;
376 // end loop as found cursor position that matches byte position
380 byteIndex += currentSequenceLength; // jump to next character
381 visualCharacterIndex++; // increment character count so we know our position for when we get a match
384 DALI_ASSERT_DEBUG( visualCharacterIndex < strlen( preEditString ));
390 if ( Dali::Adaptor::IsAvailable() )
392 Dali::ImfManager handle( this );
393 Dali::ImfManager::ImfEventData imfEventData( Dali::ImfManager::PREEDIT, preEditString, cursorPosition, 0 );
394 Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfEventData );
396 if ( callbackData.update )
398 SetCursorPosition( callbackData.cursorPosition );
399 SetSurroundingText( callbackData.currentText );
401 NotifyCursorPosition();
404 if ( callbackData.preeditResetRequired )
409 free( preEditString );
412 void ImfManager::CommitReceived( void*, Ecore_IMF_Context* imfContext, void* event_info )
414 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::CommitReceived\n" );
416 if ( Dali::Adaptor::IsAvailable() )
418 const std::string keyString( static_cast<char*>( event_info ) );
420 Dali::ImfManager handle( this );
421 Dali::ImfManager::ImfEventData imfEventData( Dali::ImfManager::COMMIT, keyString, 0, 0 );
422 Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfEventData );
424 if( callbackData.update )
426 SetCursorPosition( callbackData.cursorPosition );
427 SetSurroundingText( callbackData.currentText );
429 NotifyCursorPosition();
435 * Called when an IMF retrieve surround event is received.
436 * Here the IMF module wishes to know the string we are working with and where within the string the cursor is
437 * We need to signal the application to tell us this information.
439 Eina_Bool ImfManager::RetrieveSurrounding( void* data, Ecore_IMF_Context* imfContext, char** text, int* cursorPosition )
441 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::RetrieveSurrounding\n" );
443 Dali::ImfManager::ImfEventData imfData( Dali::ImfManager::GETSURROUNDING, std::string(), 0, 0 );
444 Dali::ImfManager handle( this );
445 mEventSignal.Emit( handle, imfData );
449 *text = strdup( mSurroundingText.c_str() );
454 *cursorPosition = mIMFCursorPosition;
461 * Called when an IMF delete surrounding event is received.
462 * Here we tell the application that it should delete a certain range.
464 void ImfManager::DeleteSurrounding( void* data, Ecore_IMF_Context* imfContext, void* event_info )
466 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DeleteSurrounding\n" );
468 if( Dali::Adaptor::IsAvailable() )
470 Ecore_IMF_Event_Delete_Surrounding* deleteSurroundingEvent = static_cast<Ecore_IMF_Event_Delete_Surrounding*>( event_info );
472 Dali::ImfManager::ImfEventData imfData( Dali::ImfManager::DELETESURROUNDING, std::string(), deleteSurroundingEvent->offset, deleteSurroundingEvent->n_chars );
473 Dali::ImfManager handle( this );
474 mEventSignal.Emit( handle, imfData );
478 void ImfManager::NotifyCursorPosition()
480 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::NotifyCursorPosition\n" );
484 ecore_imf_context_cursor_position_set( mIMFContext, mIMFCursorPosition );
488 void ImfManager::SetCursorPosition( unsigned int cursorPosition )
490 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetCursorPosition\n" );
492 mIMFCursorPosition = static_cast<int>( cursorPosition );
495 unsigned int ImfManager::GetCursorPosition() const
497 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetCursorPosition\n" );
499 return static_cast<unsigned int>( mIMFCursorPosition );
502 void ImfManager::SetSurroundingText( const std::string& text )
504 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetSurroundingText\n" );
506 mSurroundingText = text;
509 const std::string& ImfManager::GetSurroundingText() const
511 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetSurroundingText\n" );
513 return mSurroundingText;