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 <boost/bind.hpp>
23 #include <dali/public-api/events/key-event.h>
24 #include <dali/public-api/object/type-registry.h>
26 #include <dali/integration-api/debug.h>
29 #include <window-render-surface.h>
30 #include <adaptor-impl.h>
31 #include <singleton-service-impl.h>
32 #include <virtual-keyboard-impl.h>
46 #if defined(DEBUG_ENABLED)
47 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_IMF_MANAGER");
50 // Currently this code is internal to dali/dali/internal/event/text/utf8.h but should be made Public and used from there instead.
51 size_t Utf8SequenceLength(const unsigned char leadByte)
55 if ((leadByte & 0x80) == 0 ) //ASCII character (lead bit zero)
59 else if (( leadByte & 0xe0 ) == 0xc0 ) //110x xxxx
63 else if (( leadByte & 0xf0 ) == 0xe0 ) //1110 xxxx
67 else if (( leadByte & 0xf8 ) == 0xf0 ) //1111 0xxx
75 // Static function calls used by ecore 'c' style callback registration
76 void Commit( void *data, Ecore_IMF_Context *imfContext, void *event_info )
80 ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
81 imfManager->CommitReceived( data, imfContext, event_info );
85 void PreEdit( void *data, Ecore_IMF_Context *imfContext, void *event_info )
89 ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
90 imfManager->PreEditChanged( data, imfContext, event_info );
94 Eina_Bool ImfRetrieveSurrounding(void *data, Ecore_IMF_Context *imfContext, char** text, int* cursorPosition )
98 ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
99 return imfManager->RetrieveSurrounding( data, imfContext, text, cursorPosition );
108 * Called when an IMF delete surrounding event is received.
109 * Here we tell the application that it should delete a certain range.
111 void ImfDeleteSurrounding( void *data, Ecore_IMF_Context *imfContext, void *event_info )
115 ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
116 imfManager->DeleteSurrounding( data, imfContext, event_info );
122 return ImfManager::Get();
125 TypeRegistration IMF_MANAGER_TYPE( typeid(Dali::ImfManager), typeid(Dali::BaseHandle), Create );
127 } // unnamed namespace
129 bool ImfManager::IsAvailable()
131 bool available( false );
133 Dali::SingletonService service( SingletonService::Get() );
136 available = service.GetSingleton( typeid( Dali::ImfManager ) );
142 Dali::ImfManager ImfManager::Get()
144 Dali::ImfManager manager;
146 Dali::SingletonService service( SingletonService::Get() );
149 // Check whether the singleton is already created
150 Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::ImfManager ) );
153 // If so, downcast the handle
154 manager = Dali::ImfManager( dynamic_cast< ImfManager* >( handle.GetObjectPtr() ) );
156 else if ( Adaptor::IsAvailable() )
158 // Create instance and register singleton only if the adaptor is available
160 Adaptor& adaptorImpl( Adaptor::GetImplementation( Adaptor::Get() ) );
162 // The Ecore_X_Window needs to use the ImfManager.
163 // Only when the render surface is window, we can get the Ecore_X_Window.
164 Ecore_X_Window ecoreXwin( 0 );
165 Dali::RenderSurface& surface( adaptorImpl.GetSurface() );
166 if( surface.GetType() == Dali::RenderSurface::WINDOW )
168 ecoreXwin = AnyCast< Ecore_X_Window >( adaptorImpl.GetSurface().GetSurface() );
171 // If we fail to get Ecore_X_Window, we can't use the ImfManager correctly.
172 // Thus you have to call "ecore_imf_context_client_window_set" somewhere.
173 // In EvasPlugIn, this function is called in EvasPlugin::ConnectEcoreEvent().
175 manager = Dali::ImfManager( new ImfManager( ecoreXwin ) );
176 service.Register( typeid( manager ), manager );
183 ImfManager::ImfManager( Ecore_X_Window ecoreXwin )
185 mIMFCursorPosition( 0 ),
186 mSurroundingText(""),
187 mRestoreAfterFocusLost( false ),
188 mIdleCallbackConnected( false ),
192 CreateContext( ecoreXwin );
195 VirtualKeyboard::ConnectCallbacks( mIMFContext );
198 ImfManager::~ImfManager()
200 VirtualKeyboard::DisconnectCallbacks( mIMFContext );
201 DisconnectCallbacks();
204 ecore_imf_shutdown();
207 void ImfManager::CreateContext( Ecore_X_Window ecoreXwin )
209 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::CreateContext\n" );
211 const char *contextId = ecore_imf_context_default_id_get();
214 mIMFContext = ecore_imf_context_add( contextId );
220 ecore_imf_context_client_window_set( mIMFContext, reinterpret_cast<void*>( ecoreXwin ) );
225 DALI_LOG_WARNING("IMF Unable to get IMF Context\n");
230 DALI_LOG_WARNING("IMF Unable to get IMF Context\n");
234 void ImfManager::DeleteContext()
236 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DeleteContext\n" );
244 // Callbacks for predicitive text support.
245 void ImfManager::ConnectCallbacks()
249 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::ConnectCallbacks\n" );
251 ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, PreEdit, this );
252 ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_COMMIT, Commit, this );
253 ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_DELETE_SURROUNDING, ImfDeleteSurrounding, this );
255 ecore_imf_context_retrieve_surrounding_callback_set( mIMFContext, ImfRetrieveSurrounding, this);
259 void ImfManager::DisconnectCallbacks()
263 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DisconnectCallbacks\n" );
265 ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, PreEdit );
266 ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_COMMIT, Commit );
267 ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_DELETE_SURROUNDING, ImfDeleteSurrounding );
269 // We do not need to unset the retrieve surrounding callback.
273 void ImfManager::Activate()
275 // Reset mIdleCallbackConnected
276 mIdleCallbackConnected = false;
280 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Activate\n" );
282 ecore_imf_context_focus_in( mIMFContext );
284 // emit keyboard activated signal
285 Dali::ImfManager handle( this );
286 mActivatedSignalV2.Emit( handle );
290 void ImfManager::Deactivate()
294 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Deactivate\n" );
297 ecore_imf_context_focus_out( mIMFContext );
300 // Reset mIdleCallbackConnected
301 mIdleCallbackConnected = false;
304 void ImfManager::Reset()
306 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Reset\n" );
310 ecore_imf_context_reset( mIMFContext );
314 Ecore_IMF_Context* ImfManager::GetContext()
316 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetContext\n" );
321 bool ImfManager::RestoreAfterFocusLost() const
323 return mRestoreAfterFocusLost;
326 void ImfManager::SetRestoreAfterFocusLost( bool toggle )
328 mRestoreAfterFocusLost = toggle;
332 * Called when an IMF Pre-Edit changed event is received.
333 * We are still predicting what the user is typing. The latest string is what the IMF module thinks
334 * the user wants to type.
336 void ImfManager::PreEditChanged( void *, Ecore_IMF_Context *imfContext, void *event_info )
338 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::PreEditChanged\n" );
340 char *preEditString( NULL );
341 int cursorPosition( 0 );
342 Eina_List *attrs = NULL;
345 Ecore_IMF_Preedit_Attr *attr;
347 // Retrieves attributes as well as the string the cursor position offset from start of pre-edit string.
348 // the attributes (attrs) is used in languages that use the soft arrows keys to insert characters into a current pre-edit string.
349 ecore_imf_context_preedit_string_with_attributes_get( imfContext, &preEditString, &attrs, &cursorPosition );
353 // iterate through the list of attributes getting the type, start and end position.
354 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) ))
356 #ifdef DALI_PROFILE_UBUNTU
357 if ( attr->preedit_type == ECORE_IMF_PREEDIT_TYPE_SUB3 ) // (Ecore_IMF)
358 #else // DALI_PROFILE_UBUNTU
359 if ( attr->preedit_type == ECORE_IMF_PREEDIT_TYPE_SUB4 ) // (Ecore_IMF)
360 #endif // DALI_PROFILE_UBUNTU
362 // 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.
364 size_t visualCharacterIndex = 0;
365 size_t byteIndex = 0;
367 // iterate through null terminated string checking each character's position against the given byte position ( attr->end_index ).
368 while ( preEditString[byteIndex] != '\0' )
370 // attr->end_index is provided as a byte position not character and we need to know the character position.
371 size_t currentSequenceLength = Utf8SequenceLength(preEditString[byteIndex]); // 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 std::string keyString ( preEditString );
393 int numberOfChars( 0 );
395 Dali::ImfManager handle( this );
396 Dali::ImfManager::ImfEventData imfEventData ( Dali::ImfManager::PREEDIT, keyString, cursorPosition, numberOfChars );
397 Dali::ImfManager::ImfCallbackData callbackData = mEventSignalV2.Emit( handle, imfEventData );
399 if ( callbackData.update )
401 SetCursorPosition( callbackData.cursorPosition );
402 SetSurroundingText( callbackData.currentText );
404 NotifyCursorPosition();
407 if ( callbackData.preeditResetRequired )
412 free( preEditString );
415 void ImfManager::CommitReceived( void *, Ecore_IMF_Context *imfContext, void *event_info )
417 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::CommitReceived\n" );
419 if ( Dali::Adaptor::IsAvailable() )
421 const std::string keyString( (char *)event_info );
422 const int cursorOffset( 0 );
423 const int numberOfChars( 0 );
425 Dali::ImfManager handle( this );
426 Dali::ImfManager::ImfEventData imfEventData ( Dali::ImfManager::COMMIT, keyString, cursorOffset, numberOfChars );
427 Dali::ImfManager::ImfCallbackData callbackData = mEventSignalV2.Emit( handle, imfEventData );
429 if ( callbackData.update )
431 SetCursorPosition( callbackData.cursorPosition );
432 SetSurroundingText( callbackData.currentText );
434 NotifyCursorPosition();
440 * Called when an IMF retrieve surround event is received.
441 * Here the IMF module wishes to know the string we are working with and where within the string the cursor is
442 * We need to signal the application to tell us this information.
444 Eina_Bool ImfManager::RetrieveSurrounding( void *data, Ecore_IMF_Context *imfContext, char** text, int* cursorPosition )
446 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::RetrieveSurrounding\n" );
448 std::string keyString ( "" );
449 int cursorOffset( 0 );
450 int numberOfChars( 0 );
452 Dali::ImfManager::ImfEventData imfData ( Dali::ImfManager::GETSURROUNDING , keyString, cursorOffset, numberOfChars );
453 Dali::ImfManager handle( this );
454 mEventSignalV2.Emit( handle, imfData );
458 std::string surroundingText( GetSurroundingText() );
460 if ( !surroundingText.empty() )
462 *text = strdup( surroundingText.c_str() );
466 *text = strdup( "" );
470 if ( cursorPosition )
472 *cursorPosition = GetCursorPosition();
480 * Called when an IMF delete surrounding event is received.
481 * Here we tell the application that it should delete a certain range.
483 void ImfManager::DeleteSurrounding( void *data, Ecore_IMF_Context *imfContext, void *event_info )
485 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DeleteSurrounding\n" );
487 if ( Dali::Adaptor::IsAvailable() )
489 Ecore_IMF_Event_Delete_Surrounding* deleteSurroundingEvent = (Ecore_IMF_Event_Delete_Surrounding*) event_info;
491 const std::string keyString( "" );
492 const int cursorOffset( deleteSurroundingEvent->offset );
493 const int numberOfChars( deleteSurroundingEvent->n_chars );
495 Dali::ImfManager::ImfEventData imfData ( Dali::ImfManager::DELETESURROUNDING , keyString, cursorOffset, numberOfChars );
496 Dali::ImfManager handle( this );
497 mEventSignalV2.Emit( handle, imfData );
501 void ImfManager::NotifyCursorPosition()
503 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::NotifyCursorPosition\n" );
507 ecore_imf_context_cursor_position_set( mIMFContext, mIMFCursorPosition );
511 int ImfManager::GetCursorPosition()
513 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetCursorPosition\n" );
515 return mIMFCursorPosition;
518 void ImfManager::SetCursorPosition( unsigned int cursorPosition )
520 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetCursorPosition\n" );
522 mIMFCursorPosition = ( int )cursorPosition;
525 void ImfManager::SetSurroundingText( std::string text )
527 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetSurroundingText\n" );
529 mSurroundingText = text;
532 std::string ImfManager::GetSurroundingText()
534 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetSurroundingText\n" );
536 return mSurroundingText;