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 BaseHandle handle( ImfManager::Get() );
127 TypeRegistration IMF_MANAGER_TYPE( typeid(Dali::ImfManager), typeid(Dali::BaseHandle), Create, true /* Create Instance At Startup */ );
129 } // unnamed namespace
131 Dali::ImfManager ImfManager::Get()
133 Dali::ImfManager manager;
135 Dali::SingletonService service( SingletonService::Get() );
138 // Check whether the singleton is already created
139 Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::ImfManager ) );
142 // If so, downcast the handle
143 manager = Dali::ImfManager( dynamic_cast< ImfManager* >( handle.GetObjectPtr() ) );
150 ImfManager::~ImfManager()
152 VirtualKeyboard::DisconnectCallbacks( mIMFContext );
153 DisconnectCallbacks();
156 ecore_imf_shutdown();
159 void ImfManager::DeleteContext()
161 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DeleteContext\n" );
169 // Callbacks for predicitive text support.
170 void ImfManager::ConnectCallbacks()
174 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::ConnectCallbacks\n" );
176 ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, PreEdit, this );
177 ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_COMMIT, Commit, this );
178 ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_DELETE_SURROUNDING, ImfDeleteSurrounding, this );
180 ecore_imf_context_retrieve_surrounding_callback_set( mIMFContext, ImfRetrieveSurrounding, this);
184 void ImfManager::DisconnectCallbacks()
188 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DisconnectCallbacks\n" );
190 ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, PreEdit );
191 ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_COMMIT, Commit );
192 ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_DELETE_SURROUNDING, ImfDeleteSurrounding );
194 // We do not need to unset the retrieve surrounding callback.
198 void ImfManager::Activate()
200 // Reset mIdleCallbackConnected
201 mIdleCallbackConnected = false;
205 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Activate\n" );
207 ecore_imf_context_focus_in( mIMFContext );
209 // emit keyboard activated signal
210 Dali::ImfManager handle( this );
211 mActivatedSignalV2.Emit( handle );
215 void ImfManager::Deactivate()
219 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Deactivate\n" );
222 ecore_imf_context_focus_out( mIMFContext );
225 // Reset mIdleCallbackConnected
226 mIdleCallbackConnected = false;
229 void ImfManager::Reset()
231 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Reset\n" );
235 ecore_imf_context_reset( mIMFContext );
239 Ecore_IMF_Context* ImfManager::GetContext()
241 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetContext\n" );
246 bool ImfManager::RestoreAfterFocusLost() const
248 return mRestoreAfterFocusLost;
251 void ImfManager::SetRestoreAferFocusLost( bool toggle )
253 mRestoreAfterFocusLost = toggle;
257 * Called when an IMF Pre-Edit changed event is received.
258 * We are still predicting what the user is typing. The latest string is what the IMF module thinks
259 * the user wants to type.
261 void ImfManager::PreEditChanged( void *, Ecore_IMF_Context *imfContext, void *event_info )
263 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::PreEditChanged\n" );
265 char *preEditString( NULL );
266 int cursorPosition( 0 );
267 Eina_List *attrs = NULL;
270 Ecore_IMF_Preedit_Attr *attr;
272 // Retrieves attributes as well as the string the cursor position offset from start of pre-edit string.
273 // the attributes (attrs) is used in languages that use the soft arrows keys to insert characters into a current pre-edit string.
274 ecore_imf_context_preedit_string_with_attributes_get( imfContext, &preEditString, &attrs, &cursorPosition );
278 // iterate through the list of attributes getting the type, start and end position.
279 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) ))
281 if ( attr->preedit_type == ECORE_IMF_PREEDIT_TYPE_SUB4 ) // (Ecore_IMF)
283 // 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.
285 size_t visualCharacterIndex = 0;
286 size_t byteIndex = 0;
288 // iterate through null terminated string checking each character's position against the given byte position ( attr->end_index ).
289 while ( preEditString[byteIndex] != '\0' )
291 // attr->end_index is provided as a byte position not character and we need to know the character position.
292 size_t currentSequenceLength = Utf8SequenceLength(preEditString[byteIndex]); // returns number of bytes used to represent character.
293 if ( byteIndex == attr->end_index )
295 cursorPosition = visualCharacterIndex;
297 // end loop as found cursor position that matches byte position
301 byteIndex += currentSequenceLength; // jump to next character
302 visualCharacterIndex++; // increment character count so we know our position for when we get a match
305 DALI_ASSERT_DEBUG( visualCharacterIndex < strlen( preEditString ));
311 if ( Dali::Adaptor::IsAvailable() )
313 std::string keyString ( preEditString );
314 int numberOfChars( 0 );
316 Dali::ImfManager handle( this );
317 Dali::ImfManager::ImfEventData imfEventData ( Dali::ImfManager::PREEDIT, keyString, cursorPosition, numberOfChars );
318 Dali::ImfManager::ImfCallbackData callbackData = mEventSignalV2.Emit( handle, imfEventData );
320 if ( callbackData.update )
322 SetCursorPosition( callbackData.cursorPosition );
323 SetSurroundingText( callbackData.currentText );
325 NotifyCursorPosition();
328 if ( callbackData.preeditResetRequired )
333 free( preEditString );
336 void ImfManager::CommitReceived( void *, Ecore_IMF_Context *imfContext, void *event_info )
338 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::CommitReceived\n" );
340 if ( Dali::Adaptor::IsAvailable() )
342 const std::string keyString( (char *)event_info );
343 const int cursorOffset( 0 );
344 const int numberOfChars( 0 );
346 Dali::ImfManager handle( this );
347 Dali::ImfManager::ImfEventData imfEventData ( Dali::ImfManager::COMMIT, keyString, cursorOffset, numberOfChars );
348 Dali::ImfManager::ImfCallbackData callbackData = mEventSignalV2.Emit( handle, imfEventData );
350 if ( callbackData.update )
352 SetCursorPosition( callbackData.cursorPosition );
353 SetSurroundingText( callbackData.currentText );
355 NotifyCursorPosition();
361 * Called when an IMF retrieve surround event is received.
362 * Here the IMF module wishes to know the string we are working with and where within the string the cursor is
363 * We need to signal the application to tell us this information.
365 Eina_Bool ImfManager::RetrieveSurrounding( void *data, Ecore_IMF_Context *imfContext, char** text, int* cursorPosition )
367 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::RetrieveSurrounding\n" );
369 std::string keyString ( "" );
370 int cursorOffset( 0 );
371 int numberOfChars( 0 );
373 Dali::ImfManager::ImfEventData imfData ( Dali::ImfManager::GETSURROUNDING , keyString, cursorOffset, numberOfChars );
374 Dali::ImfManager handle( this );
375 mEventSignalV2.Emit( handle, imfData );
379 std::string surroundingText( GetSurroundingText() );
381 if ( !surroundingText.empty() )
383 *text = strdup( surroundingText.c_str() );
387 *text = strdup( "" );
391 if ( cursorPosition )
393 *cursorPosition = GetCursorPosition();
401 * Called when an IMF delete surrounding event is received.
402 * Here we tell the application that it should delete a certain range.
404 void ImfManager::DeleteSurrounding( void *data, Ecore_IMF_Context *imfContext, void *event_info )
406 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DeleteSurrounding\n" );
408 if ( Dali::Adaptor::IsAvailable() )
410 Ecore_IMF_Event_Delete_Surrounding* deleteSurroundingEvent = (Ecore_IMF_Event_Delete_Surrounding*) event_info;
412 const std::string keyString( "" );
413 const int cursorOffset( deleteSurroundingEvent->offset );
414 const int numberOfChars( deleteSurroundingEvent->n_chars );
416 Dali::ImfManager::ImfEventData imfData ( Dali::ImfManager::DELETESURROUNDING , keyString, cursorOffset, numberOfChars );
417 Dali::ImfManager handle( this );
418 mEventSignalV2.Emit( handle, imfData );
422 void ImfManager::NotifyCursorPosition()
424 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::NotifyCursorPosition\n" );
428 ecore_imf_context_cursor_position_set( mIMFContext, mIMFCursorPosition );
432 int ImfManager::GetCursorPosition()
434 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetCursorPosition\n" );
436 return mIMFCursorPosition;
439 void ImfManager::SetCursorPosition( unsigned int cursorPosition )
441 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetCursorPosition\n" );
443 mIMFCursorPosition = ( int )cursorPosition;
446 void ImfManager::SetSurroundingText( std::string text )
448 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetSurroundingText\n" );
450 mSurroundingText = text;
453 std::string ImfManager::GetSurroundingText()
455 DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetSurroundingText\n" );
457 return mSurroundingText;