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 <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 BaseHandle handle( ImfManager::Get() );
126 TypeRegistration IMF_MANAGER_TYPE( typeid(Dali::ImfManager), typeid(Dali::BaseHandle), Create, true /* Create Instance At Startup */ );
128 } // unnamed namespace
130 Dali::ImfManager ImfManager::Get()
132 Dali::ImfManager manager;
134 if ( Adaptor::IsAvailable() )
136 // Check whether the singleton is already created
137 Dali::BaseHandle handle = Dali::Adaptor::Get().GetSingleton( typeid( Dali::ImfManager ) );
140 // If so, downcast the handle
141 manager = Dali::ImfManager( dynamic_cast< ImfManager* >( handle.GetObjectPtr() ) );
148 ImfManager::~ImfManager()
150 VirtualKeyboard::DisconnectCallbacks( mIMFContext );
151 DisconnectCallbacks();
154 ecore_imf_shutdown();
157 void ImfManager::DeleteContext()
159 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DeleteContext\n" );
167 // Callbacks for predicitive text support.
168 void ImfManager::ConnectCallbacks()
172 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::ConnectCallbacks\n" );
174 ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, PreEdit, this );
175 ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_COMMIT, Commit, this );
176 ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_DELETE_SURROUNDING, ImfDeleteSurrounding, this );
178 ecore_imf_context_retrieve_surrounding_callback_set( mIMFContext, ImfRetrieveSurrounding, this);
182 void ImfManager::DisconnectCallbacks()
186 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DisconnectCallbacks\n" );
188 ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, PreEdit );
189 ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_COMMIT, Commit );
190 ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_DELETE_SURROUNDING, ImfDeleteSurrounding );
192 // We do not need to unset the retrieve surrounding callback.
196 void ImfManager::Activate()
198 // Reset mIdleCallbackConnected
199 mIdleCallbackConnected = false;
203 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Activate\n" );
205 ecore_imf_context_focus_in( mIMFContext );
207 // emit keyboard activated signal
208 Dali::ImfManager handle( this );
209 mActivatedSignalV2.Emit( handle );
213 void ImfManager::Deactivate()
217 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Deactivate\n" );
220 ecore_imf_context_focus_out( mIMFContext );
223 // Reset mIdleCallbackConnected
224 mIdleCallbackConnected = false;
227 void ImfManager::Reset()
229 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Reset\n" );
233 ecore_imf_context_reset( mIMFContext );
237 Ecore_IMF_Context* ImfManager::GetContext()
239 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetContext\n" );
244 bool ImfManager::RestoreAfterFocusLost() const
246 return mRestoreAfterFocusLost;
249 void ImfManager::SetRestoreAferFocusLost( bool toggle )
251 mRestoreAfterFocusLost = toggle;
255 * Called when an IMF Pre-Edit changed event is received.
256 * We are still predicting what the user is typing. The latest string is what the IMF module thinks
257 * the user wants to type.
259 void ImfManager::PreEditChanged( void *, Ecore_IMF_Context *imfContext, void *event_info )
261 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::PreEditChanged\n" );
263 char *preEditString( NULL );
264 int cursorPosition( 0 );
265 Eina_List *attrs = NULL;
268 Ecore_IMF_Preedit_Attr *attr;
270 // Retrieves attributes as well as the string the cursor position offset from start of pre-edit string.
271 // the attributes (attrs) is used in languages that use the soft arrows keys to insert characters into a current pre-edit string.
272 ecore_imf_context_preedit_string_with_attributes_get( imfContext, &preEditString, &attrs, &cursorPosition );
276 // iterate through the list of attributes getting the type, start and end position.
277 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) ))
279 if ( attr->preedit_type == ECORE_IMF_PREEDIT_TYPE_SUB4 ) // (Ecore_IMF)
281 // 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.
283 size_t visualCharacterIndex = 0;
284 size_t byteIndex = 0;
286 // iterate through null terminated string checking each character's position against the given byte position ( attr->end_index ).
287 while ( preEditString[byteIndex] != '\0' )
289 // attr->end_index is provided as a byte position not character and we need to know the character position.
290 size_t currentSequenceLength = Utf8SequenceLength(preEditString[byteIndex]); // returns number of bytes used to represent character.
291 if ( byteIndex == attr->end_index )
293 cursorPosition = visualCharacterIndex;
295 // end loop as found cursor position that matches byte position
299 byteIndex += currentSequenceLength; // jump to next character
300 visualCharacterIndex++; // increment character count so we know our position for when we get a match
303 DALI_ASSERT_DEBUG( visualCharacterIndex < strlen( preEditString ));
309 if ( Dali::Adaptor::IsAvailable() )
311 std::string keyString ( preEditString );
312 int numberOfChars( 0 );
314 Dali::ImfManager handle( this );
315 Dali::ImfManager::ImfEventData imfEventData ( Dali::ImfManager::PREEDIT, keyString, cursorPosition, numberOfChars );
316 Dali::ImfManager::ImfCallbackData callbackData = mEventSignalV2.Emit( handle, imfEventData );
318 if ( callbackData.update )
320 SetCursorPosition( callbackData.cursorPosition );
321 SetSurroundingText( callbackData.currentText );
323 NotifyCursorPosition();
326 if ( callbackData.preeditResetRequired )
331 free( preEditString );
334 void ImfManager::CommitReceived( void *, Ecore_IMF_Context *imfContext, void *event_info )
336 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::CommitReceived\n" );
338 if ( Dali::Adaptor::IsAvailable() )
340 const std::string keyString( (char *)event_info );
341 const int cursorOffset( 0 );
342 const int numberOfChars( 0 );
344 Dali::ImfManager handle( this );
345 Dali::ImfManager::ImfEventData imfEventData ( Dali::ImfManager::COMMIT, keyString, cursorOffset, numberOfChars );
346 Dali::ImfManager::ImfCallbackData callbackData = mEventSignalV2.Emit( handle, imfEventData );
348 if ( callbackData.update )
350 SetCursorPosition( callbackData.cursorPosition );
351 SetSurroundingText( callbackData.currentText );
353 NotifyCursorPosition();
359 * Called when an IMF retrieve surround event is received.
360 * Here the IMF module wishes to know the string we are working with and where within the string the cursor is
361 * We need to signal the application to tell us this information.
363 Eina_Bool ImfManager::RetrieveSurrounding( void *data, Ecore_IMF_Context *imfContext, char** text, int* cursorPosition )
365 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::RetrieveSurrounding\n" );
367 std::string keyString ( "" );
368 int cursorOffset( 0 );
369 int numberOfChars( 0 );
371 Dali::ImfManager::ImfEventData imfData ( Dali::ImfManager::GETSURROUNDING , keyString, cursorOffset, numberOfChars );
372 Dali::ImfManager handle( this );
373 mEventSignalV2.Emit( handle, imfData );
377 std::string surroundingText( GetSurroundingText() );
379 if ( !surroundingText.empty() )
381 *text = strdup( surroundingText.c_str() );
385 *text = strdup( "" );
389 if ( cursorPosition )
391 *cursorPosition = GetCursorPosition();
399 * Called when an IMF delete surrounding event is received.
400 * Here we tell the application that it should delete a certain range.
402 void ImfManager::DeleteSurrounding( void *data, Ecore_IMF_Context *imfContext, void *event_info )
404 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DeleteSurrounding\n" );
406 if ( Dali::Adaptor::IsAvailable() )
408 Ecore_IMF_Event_Delete_Surrounding* deleteSurroundingEvent = (Ecore_IMF_Event_Delete_Surrounding*) event_info;
410 const std::string keyString( "" );
411 const int cursorOffset( deleteSurroundingEvent->offset );
412 const int numberOfChars( deleteSurroundingEvent->n_chars );
414 Dali::ImfManager::ImfEventData imfData ( Dali::ImfManager::DELETESURROUNDING , keyString, cursorOffset, numberOfChars );
415 Dali::ImfManager handle( this );
416 mEventSignalV2.Emit( handle, imfData );
420 void ImfManager::NotifyCursorPosition()
422 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::NotifyCursorPosition\n" );
426 ecore_imf_context_cursor_position_set( mIMFContext, mIMFCursorPosition );
430 int ImfManager::GetCursorPosition()
432 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetCursorPosition\n" );
434 return mIMFCursorPosition;
437 void ImfManager::SetCursorPosition( unsigned int cursorPosition )
439 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetCursorPosition\n" );
441 mIMFCursorPosition = ( int )cursorPosition;
444 void ImfManager::SetSurroundingText( std::string text )
446 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetSurroundingText\n" );
448 mSurroundingText = text;
451 std::string ImfManager::GetSurroundingText()
453 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetSurroundingText\n" );
455 return mSurroundingText;