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>
45 #if defined(DEBUG_ENABLED)
46 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_IMF_MANAGER");
49 // Currently this code is internal to dali/dali/internal/event/text/utf8.h but should be made Public and used from there instead.
50 size_t Utf8SequenceLength(const unsigned char leadByte)
54 if ((leadByte & 0x80) == 0 ) //ASCII character (lead bit zero)
58 else if (( leadByte & 0xe0 ) == 0xc0 ) //110x xxxx
62 else if (( leadByte & 0xf0 ) == 0xe0 ) //1110 xxxx
66 else if (( leadByte & 0xf8 ) == 0xf0 ) //1111 0xxx
74 // Static function calls used by ecore 'c' style callback registration
75 void Commit( void *data, Ecore_IMF_Context *imfContext, void *event_info )
79 ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
80 imfManager->CommitReceived( data, imfContext, event_info );
84 void PreEdit( void *data, Ecore_IMF_Context *imfContext, void *event_info )
88 ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
89 imfManager->PreEditChanged( data, imfContext, event_info );
93 Eina_Bool ImfRetrieveSurrounding(void *data, Ecore_IMF_Context *imfContext, char** text, int* cursorPosition )
97 ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
98 return imfManager->RetrieveSurrounding( data, imfContext, text, cursorPosition );
107 * Called when an IMF delete surrounding event is received.
108 * Here we tell the application that it should delete a certain range.
110 void ImfDeleteSurrounding( void *data, Ecore_IMF_Context *imfContext, void *event_info )
114 ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
115 imfManager->DeleteSurrounding( data, imfContext, event_info );
121 return ImfManager::Get();
124 TypeRegistration IMF_MANAGER_TYPE( typeid(Dali::ImfManager), typeid(Dali::BaseHandle), Create );
126 } // unnamed namespace
128 bool ImfManager::IsAvailable()
130 bool available( false );
132 Dali::SingletonService service( SingletonService::Get() );
135 available = service.GetSingleton( typeid( Dali::ImfManager ) );
141 Dali::ImfManager ImfManager::Get()
143 Dali::ImfManager manager;
145 Dali::SingletonService service( SingletonService::Get() );
148 // Check whether the singleton is already created
149 Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::ImfManager ) );
152 // If so, downcast the handle
153 manager = Dali::ImfManager( dynamic_cast< ImfManager* >( handle.GetObjectPtr() ) );
155 else if ( Adaptor::IsAvailable() )
157 // Create instance and register singleton only if the adaptor is available
159 Adaptor& adaptorImpl( Adaptor::GetImplementation( Adaptor::Get() ) );
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( 0 );
164 Dali::RenderSurface& surface( adaptorImpl.GetSurface() );
165 if( surface.GetType() == Dali::RenderSurface::WINDOW )
167 ecoreXwin = AnyCast< Ecore_X_Window >( adaptorImpl.GetSurface().GetSurface() );
170 // If we fail to get Ecore_X_Window, we can't use the ImfManager correctly.
171 // Thus you have to call "ecore_imf_context_client_window_set" somewhere.
172 // In EvasPlugIn, this function is called in EvasPlugin::ConnectEcoreEvent().
174 manager = Dali::ImfManager( new ImfManager( ecoreXwin ) );
175 service.Register( typeid( manager ), manager );
182 ImfManager::ImfManager( Ecore_X_Window ecoreXwin )
184 mIMFCursorPosition( 0 ),
185 mSurroundingText(""),
186 mRestoreAfterFocusLost( false ),
187 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 = (Ecore_IMF_Preedit_Attr*)eina_list_data_get(l) ); l; l = eina_list_next(l), ( attr = (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 while ( preEditString[byteIndex] != '\0' )
369 // attr->end_index is provided as a byte position not character and we need to know the character position.
370 size_t currentSequenceLength = Utf8SequenceLength(preEditString[byteIndex]); // 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 std::string keyString ( preEditString );
392 int numberOfChars( 0 );
394 Dali::ImfManager handle( this );
395 Dali::ImfManager::ImfEventData imfEventData ( Dali::ImfManager::PREEDIT, keyString, cursorPosition, numberOfChars );
396 Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfEventData );
398 if ( callbackData.update )
400 SetCursorPosition( callbackData.cursorPosition );
401 SetSurroundingText( callbackData.currentText );
403 NotifyCursorPosition();
406 if ( callbackData.preeditResetRequired )
411 free( preEditString );
414 void ImfManager::CommitReceived( void *, Ecore_IMF_Context *imfContext, void *event_info )
416 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::CommitReceived\n" );
418 if ( Dali::Adaptor::IsAvailable() )
420 const std::string keyString( (char *)event_info );
421 const int cursorOffset( 0 );
422 const int numberOfChars( 0 );
424 Dali::ImfManager handle( this );
425 Dali::ImfManager::ImfEventData imfEventData ( Dali::ImfManager::COMMIT, keyString, cursorOffset, numberOfChars );
426 Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfEventData );
428 if ( callbackData.update )
430 SetCursorPosition( callbackData.cursorPosition );
431 SetSurroundingText( callbackData.currentText );
433 NotifyCursorPosition();
439 * Called when an IMF retrieve surround event is received.
440 * Here the IMF module wishes to know the string we are working with and where within the string the cursor is
441 * We need to signal the application to tell us this information.
443 Eina_Bool ImfManager::RetrieveSurrounding( void *data, Ecore_IMF_Context *imfContext, char** text, int* cursorPosition )
445 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::RetrieveSurrounding\n" );
447 std::string keyString ( "" );
448 int cursorOffset( 0 );
449 int numberOfChars( 0 );
451 Dali::ImfManager::ImfEventData imfData ( Dali::ImfManager::GETSURROUNDING , keyString, cursorOffset, numberOfChars );
452 Dali::ImfManager handle( this );
453 mEventSignal.Emit( handle, imfData );
457 std::string surroundingText( GetSurroundingText() );
459 if ( !surroundingText.empty() )
461 *text = strdup( surroundingText.c_str() );
465 *text = strdup( "" );
469 if ( cursorPosition )
471 *cursorPosition = GetCursorPosition();
479 * Called when an IMF delete surrounding event is received.
480 * Here we tell the application that it should delete a certain range.
482 void ImfManager::DeleteSurrounding( void *data, Ecore_IMF_Context *imfContext, void *event_info )
484 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DeleteSurrounding\n" );
486 if ( Dali::Adaptor::IsAvailable() )
488 Ecore_IMF_Event_Delete_Surrounding* deleteSurroundingEvent = (Ecore_IMF_Event_Delete_Surrounding*) event_info;
490 const std::string keyString( "" );
491 const int cursorOffset( deleteSurroundingEvent->offset );
492 const int numberOfChars( deleteSurroundingEvent->n_chars );
494 Dali::ImfManager::ImfEventData imfData ( Dali::ImfManager::DELETESURROUNDING , keyString, cursorOffset, numberOfChars );
495 Dali::ImfManager handle( this );
496 mEventSignal.Emit( handle, imfData );
500 void ImfManager::NotifyCursorPosition()
502 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::NotifyCursorPosition\n" );
506 ecore_imf_context_cursor_position_set( mIMFContext, mIMFCursorPosition );
510 int ImfManager::GetCursorPosition()
512 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetCursorPosition\n" );
514 return mIMFCursorPosition;
517 void ImfManager::SetCursorPosition( unsigned int cursorPosition )
519 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetCursorPosition\n" );
521 mIMFCursorPosition = ( int )cursorPosition;
524 void ImfManager::SetSurroundingText( std::string text )
526 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetSurroundingText\n" );
528 mSurroundingText = text;
531 std::string ImfManager::GetSurroundingText()
533 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetSurroundingText\n" );
535 return mSurroundingText;