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 #include "ecore-virtual-keyboard.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 );
120 } // unnamed namespace
122 bool ImfManager::IsAvailable()
124 bool available( false );
126 Dali::SingletonService service( SingletonService::Get() );
129 available = service.GetSingleton( typeid( Dali::ImfManager ) );
135 Dali::ImfManager ImfManager::Get()
137 Dali::ImfManager manager;
139 Dali::SingletonService service( SingletonService::Get() );
142 // Check whether the singleton is already created
143 Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::ImfManager ) );
146 // If so, downcast the handle
147 manager = Dali::ImfManager( dynamic_cast< ImfManager* >( handle.GetObjectPtr() ) );
149 else if ( Adaptor::IsAvailable() )
151 // Create instance and register singleton only if the adaptor is available
153 Adaptor& adaptorImpl( Adaptor::GetImplementation( Adaptor::Get() ) );
154 Any nativeWindow = adaptorImpl.GetNativeWindowHandle();
156 // The Ecore_X_Window needs to use the ImfManager.
157 // Only when the render surface is window, we can get the Ecore_X_Window.
158 Ecore_X_Window ecoreXwin( AnyCast<Ecore_X_Window>(nativeWindow) );
161 // If we fail to get Ecore_X_Window, we can't use the ImfManager correctly.
162 // Thus you have to call "ecore_imf_context_client_window_set" somewhere.
163 // In EvasPlugIn, this function is called in EvasPlugin::ConnectEcoreEvent().
165 manager = Dali::ImfManager( new ImfManager( ecoreXwin ) );
166 service.Register( typeid( manager ), manager );
170 DALI_LOG_ERROR("Failed to get native window handle\n");
178 ImfManager::ImfManager( Ecore_X_Window ecoreXwin )
180 mIMFCursorPosition( 0 ),
182 mRestoreAfterFocusLost( false ),
183 mIdleCallbackConnected( false )
186 CreateContext( ecoreXwin );
189 VirtualKeyboard::ConnectCallbacks( mIMFContext );
192 ImfManager::~ImfManager()
194 VirtualKeyboard::DisconnectCallbacks( mIMFContext );
195 DisconnectCallbacks();
198 ecore_imf_shutdown();
201 void ImfManager::CreateContext( Ecore_X_Window ecoreXwin )
203 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::CreateContext\n" );
205 const char *contextId = ecore_imf_context_default_id_get();
208 mIMFContext = ecore_imf_context_add( contextId );
214 ecore_imf_context_client_window_set( mIMFContext, reinterpret_cast<void*>( ecoreXwin ) );
219 DALI_LOG_INFO( gLogFilter, Debug::General, "IMF Unable to get IMF Context\n");
224 DALI_LOG_INFO( gLogFilter, Debug::General, "IMF Unable to get IMF Context\n");
228 void ImfManager::DeleteContext()
230 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DeleteContext\n" );
238 // Callbacks for predicitive text support.
239 void ImfManager::ConnectCallbacks()
243 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::ConnectCallbacks\n" );
245 ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, PreEdit, this );
246 ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_COMMIT, Commit, this );
247 ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_DELETE_SURROUNDING, ImfDeleteSurrounding, this );
249 ecore_imf_context_retrieve_surrounding_callback_set( mIMFContext, ImfRetrieveSurrounding, this);
253 void ImfManager::DisconnectCallbacks()
257 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DisconnectCallbacks\n" );
259 ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, PreEdit );
260 ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_COMMIT, Commit );
261 ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_DELETE_SURROUNDING, ImfDeleteSurrounding );
263 // We do not need to unset the retrieve surrounding callback.
267 void ImfManager::Activate()
269 // Reset mIdleCallbackConnected
270 mIdleCallbackConnected = false;
274 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Activate\n" );
276 ecore_imf_context_focus_in( mIMFContext );
278 // emit keyboard activated signal
279 Dali::ImfManager handle( this );
280 mActivatedSignal.Emit( handle );
284 void ImfManager::Deactivate()
288 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Deactivate\n" );
291 ecore_imf_context_focus_out( mIMFContext );
294 // Reset mIdleCallbackConnected
295 mIdleCallbackConnected = false;
298 void ImfManager::Reset()
300 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Reset\n" );
304 ecore_imf_context_reset( mIMFContext );
308 Ecore_IMF_Context* ImfManager::GetContext()
310 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetContext\n" );
315 bool ImfManager::RestoreAfterFocusLost() const
317 return mRestoreAfterFocusLost;
320 void ImfManager::SetRestoreAfterFocusLost( bool toggle )
322 mRestoreAfterFocusLost = toggle;
326 * Called when an IMF Pre-Edit changed event is received.
327 * We are still predicting what the user is typing. The latest string is what the IMF module thinks
328 * the user wants to type.
330 void ImfManager::PreEditChanged( void*, Ecore_IMF_Context* imfContext, void* event_info )
332 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::PreEditChanged\n" );
334 char* preEditString( NULL );
335 int cursorPosition( 0 );
336 Eina_List* attrs = NULL;
339 Ecore_IMF_Preedit_Attr* attr;
341 // Retrieves attributes as well as the string the cursor position offset from start of pre-edit string.
342 // the attributes (attrs) is used in languages that use the soft arrows keys to insert characters into a current pre-edit string.
343 ecore_imf_context_preedit_string_with_attributes_get( imfContext, &preEditString, &attrs, &cursorPosition );
347 // iterate through the list of attributes getting the type, start and end position.
348 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) ) ))
350 #ifdef DALI_PROFILE_UBUNTU
351 if ( attr->preedit_type == ECORE_IMF_PREEDIT_TYPE_SUB3 ) // (Ecore_IMF)
352 #else // DALI_PROFILE_UBUNTU
353 if ( attr->preedit_type == ECORE_IMF_PREEDIT_TYPE_SUB4 ) // (Ecore_IMF)
354 #endif // DALI_PROFILE_UBUNTU
356 // 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.
358 size_t visualCharacterIndex = 0;
359 size_t byteIndex = 0;
361 // iterate through null terminated string checking each character's position against the given byte position ( attr->end_index ).
362 const char leadByte = preEditString[byteIndex];
363 while( leadByte != '\0' )
365 // attr->end_index is provided as a byte position not character and we need to know the character position.
366 const size_t currentSequenceLength = Utf8SequenceLength( leadByte ); // returns number of bytes used to represent character.
367 if ( byteIndex == attr->end_index )
369 cursorPosition = visualCharacterIndex;
371 // end loop as found cursor position that matches byte position
375 byteIndex += currentSequenceLength; // jump to next character
376 visualCharacterIndex++; // increment character count so we know our position for when we get a match
379 DALI_ASSERT_DEBUG( visualCharacterIndex < strlen( preEditString ));
385 if ( Dali::Adaptor::IsAvailable() )
387 Dali::ImfManager handle( this );
388 Dali::ImfManager::ImfEventData imfEventData( Dali::ImfManager::PREEDIT, preEditString, cursorPosition, 0 );
389 Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfEventData );
391 if( callbackData.update )
393 mIMFCursorPosition = static_cast<int>( callbackData.cursorPosition );
395 NotifyCursorPosition();
398 if( callbackData.preeditResetRequired )
403 free( preEditString );
406 void ImfManager::CommitReceived( void*, Ecore_IMF_Context* imfContext, void* event_info )
408 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::CommitReceived\n" );
410 if ( Dali::Adaptor::IsAvailable() )
412 const std::string keyString( static_cast<char*>( event_info ) );
414 Dali::ImfManager handle( this );
415 Dali::ImfManager::ImfEventData imfEventData( Dali::ImfManager::COMMIT, keyString, 0, 0 );
416 Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfEventData );
418 if( callbackData.update )
420 mIMFCursorPosition = static_cast<int>( callbackData.cursorPosition );
422 NotifyCursorPosition();
428 * Called when an IMF retrieve surround event is received.
429 * Here the IMF module wishes to know the string we are working with and where within the string the cursor is
430 * We need to signal the application to tell us this information.
432 Eina_Bool ImfManager::RetrieveSurrounding( void* data, Ecore_IMF_Context* imfContext, char** text, int* cursorPosition )
434 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::RetrieveSurrounding\n" );
436 Dali::ImfManager::ImfEventData imfData( Dali::ImfManager::GETSURROUNDING, std::string(), 0, 0 );
437 Dali::ImfManager handle( this );
438 Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfData );
440 if( callbackData.update )
444 *text = strdup( callbackData.currentText.c_str() );
449 mIMFCursorPosition = static_cast<int>( callbackData.cursorPosition );
450 *cursorPosition = mIMFCursorPosition;
458 * Called when an IMF delete surrounding event is received.
459 * Here we tell the application that it should delete a certain range.
461 void ImfManager::DeleteSurrounding( void* data, Ecore_IMF_Context* imfContext, void* event_info )
463 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DeleteSurrounding\n" );
465 if( Dali::Adaptor::IsAvailable() )
467 Ecore_IMF_Event_Delete_Surrounding* deleteSurroundingEvent = static_cast<Ecore_IMF_Event_Delete_Surrounding*>( event_info );
469 Dali::ImfManager::ImfEventData imfData( Dali::ImfManager::DELETESURROUNDING, std::string(), deleteSurroundingEvent->offset, deleteSurroundingEvent->n_chars );
470 Dali::ImfManager handle( this );
471 Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfData );
473 if( callbackData.update )
475 mIMFCursorPosition = static_cast<int>( callbackData.cursorPosition );
477 NotifyCursorPosition();
482 void ImfManager::NotifyCursorPosition()
484 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::NotifyCursorPosition\n" );
488 ecore_imf_context_cursor_position_set( mIMFContext, mIMFCursorPosition );
492 void ImfManager::SetCursorPosition( unsigned int cursorPosition )
494 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetCursorPosition\n" );
496 mIMFCursorPosition = static_cast<int>( cursorPosition );
499 unsigned int ImfManager::GetCursorPosition() const
501 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetCursorPosition\n" );
503 return static_cast<unsigned int>( mIMFCursorPosition );
506 void ImfManager::SetSurroundingText( const std::string& text )
508 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetSurroundingText\n" );
510 mSurroundingText = text;
513 const std::string& ImfManager::GetSurroundingText() const
515 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetSurroundingText\n" );
517 return mSurroundingText;
520 void ImfManager::NotifyTextInputMultiLine( bool multiLine )
524 Dali::ImfManager::TextDirection ImfManager::GetTextDirection()
526 Dali::ImfManager::TextDirection direction ( Dali::ImfManager::LeftToRight );
528 if ( ImfManager::IsAvailable() /* We do not want to create an instance of ImfManager */ )
532 char* locale( NULL );
533 ecore_imf_context_input_panel_language_locale_get( mIMFContext, &locale );
537 direction = Locale::GetTextDirection( std::string( locale ) );
545 Rect<int> ImfManager::GetInputMethodArea()
547 int xPos, yPos, width, height;
549 width = height = xPos = yPos = 0;
553 ecore_imf_context_input_panel_geometry_get( mIMFContext, &xPos, &yPos, &width, &height );
557 DALI_LOG_WARNING("VKB Unable to get IMF Context so GetSize unavailable\n");
560 return Rect<int>(xPos,yPos,width,height);
563 void ImfManager::ApplyOptions( const InputMethodOptions& options )
565 using namespace Dali::InputMethod::Category;
569 if (mIMFContext == NULL)
571 DALI_LOG_WARNING("VKB Unable to excute ApplyOptions with Null ImfContext\n");
575 if ( mOptions.CompareAndSet(PANEL_LAYOUT, options, index) )
578 if ( mOptions.CompareAndSet(AUTO_CAPITALISE, options, index) )
581 if ( mOptions.CompareAndSet(ACTION_BUTTON_TITLE, options, index) )
584 if ( mOptions.CompareAndSet(VARIATION, options, index) )