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 <dali/internal/input/ubuntu-x11/imf-manager-impl-x.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>
27 #include <dali/integration-api/adaptor.h>
28 #include <dali/internal/window-system/ubuntu-x11/window-render-surface-x.h>
29 #include <dali/internal/adaptor/common/adaptor-impl.h>
30 #include <dali/internal/system/common/locale-utils.h>
31 #include <dali/internal/system/common/singleton-service-impl.h>
32 #include <dali/internal/input/common/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 <dali/internal/input/tizen-wayland/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 ImfManagerX::IsAvailable()
127 bool available( false );
129 Dali::SingletonService service( SingletonService::Get() );
132 available = service.GetSingleton( typeid( Dali::ImfManager ) );
138 Dali::ImfManager ImfManagerX::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 ImfManagerX( ecoreXwin ) );
169 service.Register( typeid( manager ), manager );
173 DALI_LOG_ERROR("Failed to get native window handle\n");
181 ImfManagerX::ImfManagerX( Ecore_X_Window ecoreXwin )
183 mIMFCursorPosition( 0 ),
185 mRestoreAfterFocusLost( false ),
186 mIdleCallbackConnected( false )
189 CreateContext( ecoreXwin );
192 VirtualKeyboard::ConnectCallbacks( mIMFContext );
195 ImfManagerX::~ImfManagerX()
197 VirtualKeyboard::DisconnectCallbacks( mIMFContext );
198 DisconnectCallbacks();
201 ecore_imf_shutdown();
204 void ImfManagerX::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 ImfManagerX::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 ImfManagerX::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 ImfManagerX::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 ImfManagerX::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 ImfManagerX::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 ImfManagerX::Reset()
304 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Reset\n" );
308 ecore_imf_context_reset( mIMFContext );
312 ImfContext* ImfManagerX::GetContext()
314 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetContext\n" );
319 bool ImfManagerX::RestoreAfterFocusLost() const
321 return mRestoreAfterFocusLost;
324 void ImfManagerX::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 ImfManagerX::PreEditChanged( void*, ImfContext * context, void* event_info )
336 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::PreEditChanged\n" );
338 auto imfContext = reinterpret_cast<Ecore_IMF_Context*>(context);
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 = 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) ) ))
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 const char leadByte = preEditString[byteIndex];
369 while( leadByte != '\0' )
371 // attr->end_index is provided as a byte position not character and we need to know the character position.
372 const size_t currentSequenceLength = Utf8SequenceLength( leadByte ); // returns number of bytes used to represent character.
373 if ( byteIndex == attr->end_index )
375 cursorPosition = visualCharacterIndex;
377 // end loop as found cursor position that matches byte position
381 byteIndex += currentSequenceLength; // jump to next character
382 visualCharacterIndex++; // increment character count so we know our position for when we get a match
385 DALI_ASSERT_DEBUG( visualCharacterIndex < strlen( preEditString ));
391 if ( Dali::Adaptor::IsAvailable() )
393 Dali::ImfManager handle( this );
394 Dali::ImfManager::ImfEventData imfEventData( Dali::ImfManager::PREEDIT, preEditString, cursorPosition, 0 );
395 Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfEventData );
397 if( callbackData.update )
399 mIMFCursorPosition = static_cast<int>( callbackData.cursorPosition );
401 NotifyCursorPosition();
404 if( callbackData.preeditResetRequired )
409 free( preEditString );
412 void ImfManagerX::CommitReceived( void*, ImfContext* context, 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 mIMFCursorPosition = static_cast<int>( callbackData.cursorPosition );
428 NotifyCursorPosition();
434 * Called when an IMF retrieve surround event is received.
435 * Here the IMF module wishes to know the string we are working with and where within the string the cursor is
436 * We need to signal the application to tell us this information.
438 bool ImfManagerX::RetrieveSurrounding( void* data, ImfContext* context, char** text, int* cursorPosition )
440 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::RetrieveSurrounding\n" );
442 Dali::ImfManager::ImfEventData imfData( Dali::ImfManager::GETSURROUNDING, std::string(), 0, 0 );
443 Dali::ImfManager handle( this );
444 Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfData );
446 if( callbackData.update )
450 *text = strdup( callbackData.currentText.c_str() );
455 mIMFCursorPosition = static_cast<int>( callbackData.cursorPosition );
456 *cursorPosition = mIMFCursorPosition;
464 * Called when an IMF delete surrounding event is received.
465 * Here we tell the application that it should delete a certain range.
467 void ImfManagerX::DeleteSurrounding( void* data, ImfContext* context, void* event_info )
469 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DeleteSurrounding\n" );
471 if( Dali::Adaptor::IsAvailable() )
473 Ecore_IMF_Event_Delete_Surrounding* deleteSurroundingEvent = static_cast<Ecore_IMF_Event_Delete_Surrounding*>( event_info );
475 Dali::ImfManager::ImfEventData imfData( Dali::ImfManager::DELETESURROUNDING, std::string(), deleteSurroundingEvent->offset, deleteSurroundingEvent->n_chars );
476 Dali::ImfManager handle( this );
477 Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfData );
479 if( callbackData.update )
481 mIMFCursorPosition = static_cast<int>( callbackData.cursorPosition );
483 NotifyCursorPosition();
488 void ImfManagerX::NotifyCursorPosition()
490 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::NotifyCursorPosition\n" );
494 ecore_imf_context_cursor_position_set( mIMFContext, mIMFCursorPosition );
498 void ImfManagerX::SetCursorPosition( unsigned int cursorPosition )
500 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetCursorPosition\n" );
502 mIMFCursorPosition = static_cast<int>( cursorPosition );
505 unsigned int ImfManagerX::GetCursorPosition() const
507 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetCursorPosition\n" );
509 return static_cast<unsigned int>( mIMFCursorPosition );
512 void ImfManagerX::SetSurroundingText( const std::string& text )
514 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetSurroundingText\n" );
516 mSurroundingText = text;
519 const std::string& ImfManagerX::GetSurroundingText() const
521 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetSurroundingText\n" );
523 return mSurroundingText;
526 void ImfManagerX::NotifyTextInputMultiLine( bool multiLine )
530 Dali::ImfManager::TextDirection ImfManagerX::GetTextDirection()
532 Dali::ImfManager::TextDirection direction ( Dali::ImfManager::LeftToRight );
534 if ( ImfManager::IsAvailable() /* We do not want to create an instance of ImfManager */ )
538 char* locale( NULL );
539 ecore_imf_context_input_panel_language_locale_get( mIMFContext, &locale );
543 direction = static_cast< Dali::ImfManager::TextDirection >( Locale::GetDirection( std::string( locale ) ) );
551 Rect<int> ImfManagerX::GetInputMethodArea()
553 int xPos, yPos, width, height;
555 width = height = xPos = yPos = 0;
559 ecore_imf_context_input_panel_geometry_get( mIMFContext, &xPos, &yPos, &width, &height );
563 DALI_LOG_WARNING("VKB Unable to get IMF Context so GetSize unavailable\n");
566 return Rect<int>(xPos,yPos,width,height);
569 void ImfManagerX::ApplyOptions( const InputMethodOptions& options )
571 using namespace Dali::InputMethod::Category;
575 if (mIMFContext == NULL)
577 DALI_LOG_WARNING("VKB Unable to excute ApplyOptions with Null ImfContext\n");
581 if ( mOptions.CompareAndSet(PANEL_LAYOUT, options, index) )
584 if ( mOptions.CompareAndSet(AUTO_CAPITALISE, options, index) )
587 if ( mOptions.CompareAndSet(ACTION_BUTTON_TITLE, options, index) )
590 if ( mOptions.CompareAndSet(VARIATION, options, index) )
595 void ImfManagerX::SetInputPanelData( const std::string& data )
597 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetInputPanelData\n" );
601 int length = data.length();
602 ecore_imf_context_input_panel_imdata_set( mIMFContext, data.c_str(), length );
606 void ImfManagerX::GetInputPanelData( std::string& data )
608 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetInputPanelData\n" );
612 int length = 4096; // The max length is 4096 bytes
613 Dali::Vector< char > buffer;
614 buffer.Resize( length );
615 ecore_imf_context_input_panel_imdata_get( mIMFContext, &buffer[0], &length );
616 data = std::string( buffer.Begin(), buffer.End() );
620 Dali::ImfManager::State ImfManagerX::GetInputPanelState()
622 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetInputPanelState\n" );
627 value = ecore_imf_context_input_panel_state_get( mIMFContext );
631 case ECORE_IMF_INPUT_PANEL_STATE_SHOW:
633 return Dali::ImfManager::SHOW;
637 case ECORE_IMF_INPUT_PANEL_STATE_HIDE:
639 return Dali::ImfManager::HIDE;
643 case ECORE_IMF_INPUT_PANEL_STATE_WILL_SHOW:
645 return Dali::ImfManager::WILL_SHOW;
651 return Dali::ImfManager::DEFAULT;
655 return Dali::ImfManager::DEFAULT;
658 void ImfManagerX::SetReturnKeyState( bool visible )
660 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetReturnKeyState\n" );
664 ecore_imf_context_input_panel_return_key_disabled_set( mIMFContext, !visible );
668 void ImfManagerX::AutoEnableInputPanel( bool enabled )
670 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::AutoEnableInputPanel\n" );
674 ecore_imf_context_input_panel_enabled_set( mIMFContext, enabled );
678 void ImfManagerX::ShowInputPanel()
680 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::ShowInputPanel\n" );
684 ecore_imf_context_input_panel_show( mIMFContext );
688 void ImfManagerX::HideInputPanel()
690 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::HideInputPanel\n" );
694 ecore_imf_context_input_panel_hide( mIMFContext );
698 Dali::ImfManager::KeyboardType ImfManagerX::GetKeyboardType()
700 return Dali::ImfManager::KeyboardType::SOFTWARE_KEYBOARD;
703 std::string ImfManagerX::GetInputPanelLocale()
705 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetInputPanelLocale\n" );
707 std::string locale = "";
712 ecore_imf_context_input_panel_language_locale_get( mIMFContext, &value );
716 std::string valueCopy( value );
719 // The locale string retrieved must be freed with free().
732 #pragma GCC diagnostic pop