942d96c6b40c37a220683dd539d584a009ba4062
[platform/core/uifw/dali-adaptor.git] / adaptors / ecore / wayland / imf-manager-impl-ecore-wl.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 // Ecore is littered with C style cast
20 #pragma GCC diagnostic push
21 #pragma GCC diagnostic ignored "-Wold-style-cast"
22 #include <imf-manager-impl.h>
23
24 // EXTERNAL INCLUDES
25 #include <dali/public-api/events/key-event.h>
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/integration-api/debug.h>
28
29 // INTERNAL INCLUDES
30 #include <input-method-devel.h>
31 #include <adaptor.h>
32 #include <locale-utils.h>
33 #include <window-render-surface.h>
34 #include <adaptor-impl.h>
35 #include <singleton-service-impl.h>
36
37 #define TOKEN_STRING(x) #x
38
39 Ecore_IMF_Input_Panel_Layout panelLayoutMap[] =
40 {
41    ECORE_IMF_INPUT_PANEL_LAYOUT_NORMAL,
42    ECORE_IMF_INPUT_PANEL_LAYOUT_NUMBER,
43    ECORE_IMF_INPUT_PANEL_LAYOUT_EMAIL,
44    ECORE_IMF_INPUT_PANEL_LAYOUT_URL,
45    ECORE_IMF_INPUT_PANEL_LAYOUT_PHONENUMBER,
46    ECORE_IMF_INPUT_PANEL_LAYOUT_IP,
47    ECORE_IMF_INPUT_PANEL_LAYOUT_MONTH,
48    ECORE_IMF_INPUT_PANEL_LAYOUT_NUMBERONLY,
49    ECORE_IMF_INPUT_PANEL_LAYOUT_HEX,
50    ECORE_IMF_INPUT_PANEL_LAYOUT_TERMINAL,
51    ECORE_IMF_INPUT_PANEL_LAYOUT_PASSWORD,
52    ECORE_IMF_INPUT_PANEL_LAYOUT_DATETIME,
53    ECORE_IMF_INPUT_PANEL_LAYOUT_EMOTICON,
54    ECORE_IMF_INPUT_PANEL_LAYOUT_VOICE
55 };
56
57 Ecore_IMF_Autocapital_Type autoCapitalMap[] =
58 {
59    ECORE_IMF_AUTOCAPITAL_TYPE_NONE,
60    ECORE_IMF_AUTOCAPITAL_TYPE_WORD,
61    ECORE_IMF_AUTOCAPITAL_TYPE_SENTENCE,
62    ECORE_IMF_AUTOCAPITAL_TYPE_ALLCHARACTER,
63 };
64
65 Ecore_IMF_Input_Panel_Return_Key_Type returnKeyTypeMap[] =
66 {
67    ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_DEFAULT,
68    ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_DONE,
69    ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_GO,
70    ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_JOIN,
71    ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_LOGIN,
72    ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_NEXT,
73    ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_SEARCH,
74    ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_SEND,
75    ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_SIGNIN
76 };
77
78 namespace Dali
79 {
80
81 namespace Internal
82 {
83
84 namespace Adaptor
85 {
86
87 namespace
88 {
89 #if defined(DEBUG_ENABLED)
90 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_IMF_MANAGER");
91 #endif
92
93 // Currently this code is internal to dali/dali/internal/event/text/utf8.h but should be made Public and used from there instead.
94 size_t Utf8SequenceLength(const unsigned char leadByte)
95 {
96   size_t length = 0;
97
98   if ((leadByte & 0x80) == 0 )          //ASCII character (lead bit zero)
99   {
100     length = 1;
101   }
102   else if (( leadByte & 0xe0 ) == 0xc0 ) //110x xxxx
103   {
104     length = 2;
105   }
106   else if (( leadByte & 0xf0 ) == 0xe0 ) //1110 xxxx
107   {
108     length = 3;
109   }
110   else if (( leadByte & 0xf8 ) == 0xf0 ) //1111 0xxx
111   {
112     length = 4;
113   }
114
115   return length;
116 }
117
118 // Static function calls used by ecore 'c' style callback registration
119 void Commit( void *data, Ecore_IMF_Context *imfContext, void *event_info )
120 {
121   if ( data )
122   {
123     ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
124     imfManager->CommitReceived( data, imfContext, event_info );
125   }
126 }
127
128 void PreEdit( void *data, Ecore_IMF_Context *imfContext, void *event_info )
129 {
130   if ( data )
131   {
132     ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
133     imfManager->PreEditChanged( data, imfContext, event_info );
134   }
135 }
136
137 Eina_Bool ImfRetrieveSurrounding(void *data, Ecore_IMF_Context *imfContext, char** text, int* cursorPosition )
138 {
139   if ( data )
140   {
141     ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
142     return imfManager->RetrieveSurrounding( data, imfContext, text, cursorPosition );
143   }
144   else
145   {
146     return false;
147   }
148 }
149
150 void InputPanelStateChangeCallback( void* data, Ecore_IMF_Context* context, int value )
151 {
152   if (!data)
153   {
154     return;
155   }
156   ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
157   switch (value)
158   {
159     case ECORE_IMF_INPUT_PANEL_STATE_SHOW:
160     {
161       imfManager->StatusChangedSignal().Emit( true );
162       break;
163     }
164
165     case ECORE_IMF_INPUT_PANEL_STATE_HIDE:
166     {
167       imfManager->StatusChangedSignal().Emit( false );
168       break;
169     }
170
171     case ECORE_IMF_INPUT_PANEL_STATE_WILL_SHOW:
172     default:
173     {
174       // Do nothing
175       break;
176     }
177   }
178 }
179
180 void InputPanelLanguageChangeCallback( void* data, Ecore_IMF_Context* context, int value )
181 {
182   if (!data)
183   {
184     return;
185   }
186   ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
187   // Emit the signal that the language has changed
188   imfManager->LanguageChangedSignal().Emit();
189 }
190
191 void InputPanelGeometryChangedCallback ( void *data, Ecore_IMF_Context *context, int value )
192 {
193   if (!data)
194   {
195     return;
196   }
197   ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
198   // Emit signal that the keyboard is resized
199   imfManager->ResizedSignal().Emit();
200 }
201
202 void InputPanelKeyboardTypeChangedCallback( void *data, Ecore_IMF_Context *context, int value )
203 {
204   if( !data )
205   {
206     return;
207   }
208
209   ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
210   switch (value)
211   {
212     case ECORE_IMF_INPUT_PANEL_SW_KEYBOARD_MODE:
213     {
214       // Emit Signal that the keyboard type is changed to Software Keyboard
215       imfManager->KeyboardTypeChangedSignal().Emit( Dali::ImfManager::KeyboardType::SOFTWARE_KEYBOARD );
216       break;
217     }
218     case ECORE_IMF_INPUT_PANEL_HW_KEYBOARD_MODE:
219     {
220       // Emit Signal that the keyboard type is changed to Hardware Keyboard
221       imfManager->KeyboardTypeChangedSignal().Emit( Dali::ImfManager::KeyboardType::HARDWARE_KEYBOARD );
222       break;
223     }
224   }
225 }
226
227 /**
228  * Called when an IMF delete surrounding event is received.
229  * Here we tell the application that it should delete a certain range.
230  */
231 void ImfDeleteSurrounding( void *data, Ecore_IMF_Context *imfContext, void *event_info )
232 {
233   if ( data )
234   {
235     ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
236     imfManager->DeleteSurrounding( data, imfContext, event_info );
237   }
238 }
239
240 /**
241  * Called when the input method sends a private command.
242  */
243 void PrivateCommand( void *data, Ecore_IMF_Context *imfContext, void *event_info )
244 {
245   if ( data )
246   {
247     ImfManager* imfManager = reinterpret_cast< ImfManager* > ( data );
248     imfManager->SendPrivateCommand( data, imfContext, event_info );
249   }
250 }
251
252 BaseHandle Create()
253 {
254   return ImfManager::Get();
255 }
256
257 TypeRegistration IMF_MANAGER_TYPE( typeid(Dali::ImfManager), typeid(Dali::BaseHandle), Create );
258
259 } // unnamed namespace
260
261 void ImfManager::Finalize()
262 {
263   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Finalize\n" );
264   if ( mInited )
265   {
266     DisconnectCallbacks();
267     DeleteContext();
268     ecore_imf_shutdown();
269     mInited = false;
270   }
271 }
272
273 bool ImfManager::IsAvailable()
274 {
275   bool available( false );
276
277   Dali::SingletonService service( SingletonService::Get() );
278   if ( service )
279   {
280     available = service.GetSingleton( typeid( Dali::ImfManager ) );
281   }
282
283   return available;
284 }
285
286 Dali::ImfManager ImfManager::Get()
287 {
288   Dali::ImfManager manager;
289   ImfManager *imfManager = NULL;
290
291   Dali::SingletonService service( SingletonService::Get() );
292   if ( service )
293   {
294     // Check whether the singleton is already created
295     Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::ImfManager ) );
296     if( handle )
297     {
298       // If so, downcast the handle
299       imfManager = dynamic_cast< ImfManager* >( handle.GetObjectPtr() );
300       manager = Dali::ImfManager( imfManager );
301     }
302     else if ( Adaptor::IsAvailable() )
303     {
304       // Create instance and register singleton only if the adaptor is available
305       Adaptor& adaptorImpl( Adaptor::GetImplementation( Adaptor::Get() ) );
306       Any nativeWindow = adaptorImpl.GetNativeWindowHandle();
307
308       // The Ecore_Wl_Window needs to use the ImfManager.
309       // Only when the render surface is window, we can get the Ecore_Wl_Window.
310       Ecore_Wl_Window *ecoreWwin( AnyCast< Ecore_Wl_Window* >( nativeWindow ) );
311       if (ecoreWwin)
312       {
313         // If we fail to get Ecore_Wl_Window, we can't use the ImfManager correctly.
314         // Thus you have to call "ecore_imf_context_client_window_set" somewhere.
315         // In EvasPlugIn, this function is called in EvasPlugin::ConnectEcoreEvent().
316         imfManager = new ImfManager( ecoreWwin );
317         manager = Dali::ImfManager( imfManager );
318         service.Register( typeid( manager ), manager );
319       }
320       else
321       {
322         DALI_LOG_ERROR("Failed to get native window handle\n");
323       }
324     }
325   }
326
327   if ( ( imfManager != NULL ) && !imfManager->mInited )
328   {
329     ecore_imf_init();
330     imfManager->CreateContext( imfManager->mEcoreWlwin );
331
332     imfManager->ConnectCallbacks();
333     imfManager->mInited = true;
334   }
335
336   return manager;
337 }
338
339 ImfManager::ImfManager( Ecore_Wl_Window *ecoreWlwin )
340 : mIMFContext(),
341   mEcoreWlwin( ecoreWlwin ),
342   mIMFCursorPosition( 0 ),
343   mSurroundingText(),
344   mInited( false ),
345   mRestoreAfterFocusLost( false ),
346   mIdleCallbackConnected( false )
347 {
348 }
349
350 ImfManager::~ImfManager()
351 {
352   Finalize();
353 }
354
355 void ImfManager::CreateContext( Ecore_Wl_Window *ecoreWlwin )
356 {
357   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::CreateContext\n" );
358   const char *contextId = ecore_imf_context_default_id_get();
359   if( contextId )
360   {
361     mIMFContext = ecore_imf_context_add( contextId );
362
363     if( mIMFContext )
364     {
365       if( ecoreWlwin )
366       {
367           ecore_imf_context_client_window_set( mIMFContext,
368             reinterpret_cast<void*>( ecore_wl_window_id_get(ecoreWlwin)) );
369       }
370     }
371     else
372     {
373       DALI_LOG_WARNING("IMF Unable to get IMF Context\n");
374     }
375   }
376   else
377   {
378     DALI_LOG_WARNING("IMF Unable to get IMF Context\n");
379   }
380 }
381
382 void ImfManager::DeleteContext()
383 {
384   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DeleteContext\n" );
385
386   if ( mIMFContext )
387   {
388     ecore_imf_context_del( mIMFContext );
389     mIMFContext = NULL;
390   }
391 }
392
393 // Callbacks for predicitive text support.
394 void ImfManager::ConnectCallbacks()
395 {
396   if ( mIMFContext )
397   {
398     DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::ConnectCallbacks\n" );
399
400     ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_PREEDIT_CHANGED,      PreEdit,    this );
401     ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_COMMIT,               Commit,     this );
402     ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_DELETE_SURROUNDING,   ImfDeleteSurrounding, this );
403     ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_PRIVATE_COMMAND_SEND, PrivateCommand, this );
404
405     ecore_imf_context_input_panel_event_callback_add( mIMFContext, ECORE_IMF_INPUT_PANEL_STATE_EVENT,    InputPanelStateChangeCallback, this );
406     ecore_imf_context_input_panel_event_callback_add( mIMFContext, ECORE_IMF_INPUT_PANEL_LANGUAGE_EVENT, InputPanelLanguageChangeCallback, this );
407     ecore_imf_context_input_panel_event_callback_add( mIMFContext, ECORE_IMF_INPUT_PANEL_GEOMETRY_EVENT, InputPanelGeometryChangedCallback, this );
408     ecore_imf_context_input_panel_event_callback_add( mIMFContext, ECORE_IMF_INPUT_PANEL_KEYBOARD_MODE_EVENT, InputPanelKeyboardTypeChangedCallback, this );
409
410     ecore_imf_context_retrieve_surrounding_callback_set( mIMFContext, ImfRetrieveSurrounding, this);
411   }
412 }
413
414 void ImfManager::DisconnectCallbacks()
415 {
416   if ( mIMFContext )
417   {
418     DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DisconnectCallbacks\n" );
419
420     ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_PREEDIT_CHANGED,      PreEdit );
421     ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_COMMIT,               Commit );
422     ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_DELETE_SURROUNDING,   ImfDeleteSurrounding );
423     ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_PRIVATE_COMMAND_SEND, PrivateCommand );
424
425     ecore_imf_context_input_panel_event_callback_del( mIMFContext, ECORE_IMF_INPUT_PANEL_STATE_EVENT,    InputPanelStateChangeCallback     );
426     ecore_imf_context_input_panel_event_callback_del( mIMFContext, ECORE_IMF_INPUT_PANEL_LANGUAGE_EVENT, InputPanelLanguageChangeCallback  );
427     ecore_imf_context_input_panel_event_callback_del( mIMFContext, ECORE_IMF_INPUT_PANEL_GEOMETRY_EVENT, InputPanelGeometryChangedCallback );
428     ecore_imf_context_input_panel_event_callback_del( mIMFContext, ECORE_IMF_INPUT_PANEL_KEYBOARD_MODE_EVENT, InputPanelKeyboardTypeChangedCallback );
429
430     // We do not need to unset the retrieve surrounding callback.
431   }
432 }
433
434 void ImfManager::Activate()
435 {
436   // Reset mIdleCallbackConnected
437   mIdleCallbackConnected = false;
438
439   if ( mIMFContext )
440   {
441     DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Activate\n" );
442
443     ecore_imf_context_focus_in( mIMFContext );
444
445     // emit keyboard activated signal
446     Dali::ImfManager handle( this );
447     mActivatedSignal.Emit( handle );
448   }
449 }
450
451 void ImfManager::Deactivate()
452 {
453   if( mIMFContext )
454   {
455     DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Deactivate\n" );
456
457     Reset();
458     ecore_imf_context_focus_out( mIMFContext );
459   }
460
461   // Reset mIdleCallbackConnected
462   mIdleCallbackConnected = false;
463 }
464
465 void ImfManager::Reset()
466 {
467   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Reset\n" );
468
469   if ( mIMFContext )
470   {
471     ecore_imf_context_reset( mIMFContext );
472   }
473 }
474
475 Ecore_IMF_Context* ImfManager::GetContext()
476 {
477   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetContext\n" );
478
479   return mIMFContext;
480 }
481
482 bool ImfManager::RestoreAfterFocusLost() const
483 {
484   return mRestoreAfterFocusLost;
485 }
486
487 void ImfManager::SetRestoreAfterFocusLost( bool toggle )
488 {
489   mRestoreAfterFocusLost = toggle;
490 }
491
492 /**
493  * Called when an IMF Pre-Edit changed event is received.
494  * We are still predicting what the user is typing.  The latest string is what the IMF module thinks
495  * the user wants to type.
496  */
497 void ImfManager::PreEditChanged( void*, Ecore_IMF_Context* imfContext, void* event_info )
498 {
499   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::PreEditChanged\n" );
500
501   char* preEditString( NULL );
502   int cursorPosition( 0 );
503   Eina_List* attrs = NULL;
504   Eina_List* l = NULL;
505
506   Ecore_IMF_Preedit_Attr* attr;
507
508   // Retrieves attributes as well as the string the cursor position offset from start of pre-edit string.
509   // the attributes (attrs) is used in languages that use the soft arrows keys to insert characters into a current pre-edit string.
510   ecore_imf_context_preedit_string_with_attributes_get( imfContext, &preEditString, &attrs, &cursorPosition );
511
512   if ( attrs )
513   {
514     // iterate through the list of attributes getting the type, start and end position.
515     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) ) ))
516     {
517 #ifdef DALI_PROFILE_UBUNTU
518       if ( attr->preedit_type == ECORE_IMF_PREEDIT_TYPE_SUB3 ) // (Ecore_IMF)
519 #else // DALI_PROFILE_UBUNTU
520       if ( attr->preedit_type == ECORE_IMF_PREEDIT_TYPE_SUB4 ) // (Ecore_IMF)
521 #endif // DALI_PROFILE_UBUNTU
522       {
523         // 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.
524
525         size_t visualCharacterIndex = 0;
526         size_t byteIndex = 0;
527
528         // iterate through null terminated string checking each character's position against the given byte position ( attr->end_index ).
529         const char leadByte = preEditString[byteIndex];
530         while( leadByte != '\0' )
531         {
532           // attr->end_index is provided as a byte position not character and we need to know the character position.
533           const size_t currentSequenceLength = Utf8SequenceLength( leadByte ); // returns number of bytes used to represent character.
534           if ( byteIndex == attr->end_index )
535           {
536             cursorPosition = visualCharacterIndex;
537             break;
538             // end loop as found cursor position that matches byte position
539           }
540           else
541           {
542             byteIndex += currentSequenceLength; // jump to next character
543             visualCharacterIndex++;  // increment character count so we know our position for when we get a match
544           }
545
546           DALI_ASSERT_DEBUG( visualCharacterIndex < strlen( preEditString ));
547         }
548       }
549     }
550   }
551
552   if ( Dali::Adaptor::IsAvailable() )
553   {
554     Dali::ImfManager handle( this );
555     Dali::ImfManager::ImfEventData imfEventData( Dali::ImfManager::PREEDIT, preEditString, cursorPosition, 0 );
556     Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfEventData );
557
558     if ( callbackData.update )
559     {
560       SetCursorPosition( callbackData.cursorPosition );
561       SetSurroundingText( callbackData.currentText );
562
563       NotifyCursorPosition();
564     }
565
566     if ( callbackData.preeditResetRequired )
567     {
568       Reset();
569     }
570   }
571   free( preEditString );
572 }
573
574 void ImfManager::CommitReceived( void*, Ecore_IMF_Context* imfContext, void* event_info )
575 {
576   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::CommitReceived\n" );
577
578   if ( Dali::Adaptor::IsAvailable() )
579   {
580     const std::string keyString( static_cast<char*>( event_info ) );
581
582     Dali::ImfManager handle( this );
583     Dali::ImfManager::ImfEventData imfEventData( Dali::ImfManager::COMMIT, keyString, 0, 0 );
584     Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfEventData );
585
586     if( callbackData.update )
587     {
588       SetCursorPosition( callbackData.cursorPosition );
589       SetSurroundingText( callbackData.currentText );
590
591       NotifyCursorPosition();
592     }
593   }
594 }
595
596 /**
597  * Called when an IMF retrieve surround event is received.
598  * Here the IMF module wishes to know the string we are working with and where within the string the cursor is
599  * We need to signal the application to tell us this information.
600  */
601 Eina_Bool ImfManager::RetrieveSurrounding( void* data, Ecore_IMF_Context* imfContext, char** text, int* cursorPosition )
602 {
603   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::RetrieveSurrounding\n" );
604
605   Dali::ImfManager::ImfEventData imfData( Dali::ImfManager::GETSURROUNDING, std::string(), 0, 0 );
606   Dali::ImfManager handle( this );
607   Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfData );
608
609   if( callbackData.update )
610   {
611     if( text )
612     {
613       *text = strdup( callbackData.currentText.c_str() );
614     }
615
616     if( cursorPosition )
617     {
618       mIMFCursorPosition = static_cast<int>( callbackData.cursorPosition );
619       *cursorPosition = mIMFCursorPosition;
620     }
621   }
622
623   return EINA_TRUE;
624 }
625
626 /**
627  * Called when an IMF delete surrounding event is received.
628  * Here we tell the application that it should delete a certain range.
629  */
630 void ImfManager::DeleteSurrounding( void* data, Ecore_IMF_Context* imfContext, void* event_info )
631 {
632   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DeleteSurrounding\n" );
633
634   if( Dali::Adaptor::IsAvailable() )
635   {
636     Ecore_IMF_Event_Delete_Surrounding* deleteSurroundingEvent = static_cast<Ecore_IMF_Event_Delete_Surrounding*>( event_info );
637
638     Dali::ImfManager::ImfEventData imfData( Dali::ImfManager::DELETESURROUNDING, std::string(), deleteSurroundingEvent->offset, deleteSurroundingEvent->n_chars );
639     Dali::ImfManager handle( this );
640     mEventSignal.Emit( handle, imfData );
641   }
642 }
643
644 /**
645  * Called when the input method sends a private command.
646  */
647 void ImfManager::SendPrivateCommand( void* data, Ecore_IMF_Context* imfContext, void* event_info )
648 {
649   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SendPrivateCommand\n" );
650
651   if( Dali::Adaptor::IsAvailable() )
652   {
653     const char* privateCommandSendEvent = static_cast<const char*>( event_info );
654
655     Dali::ImfManager::ImfEventData imfData( Dali::ImfManager::PRIVATECOMMAND, privateCommandSendEvent, 0, 0 );
656     Dali::ImfManager handle( this );
657     mEventSignal.Emit( handle, imfData );
658   }
659 }
660
661 void ImfManager::NotifyCursorPosition()
662 {
663   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::NotifyCursorPosition\n" );
664
665   if( mIMFContext )
666   {
667     ecore_imf_context_cursor_position_set( mIMFContext, mIMFCursorPosition );
668   }
669 }
670
671 void ImfManager::SetCursorPosition( unsigned int cursorPosition )
672 {
673   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetCursorPosition\n" );
674
675   mIMFCursorPosition = static_cast<int>( cursorPosition );
676 }
677
678 unsigned int ImfManager::GetCursorPosition() const
679 {
680   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetCursorPosition\n" );
681
682   return static_cast<unsigned int>( mIMFCursorPosition );
683 }
684
685 void ImfManager::SetSurroundingText( const std::string& text )
686 {
687   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetSurroundingText\n" );
688
689   mSurroundingText = text;
690 }
691
692 const std::string& ImfManager::GetSurroundingText() const
693 {
694   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetSurroundingText\n" );
695
696   return mSurroundingText;
697 }
698
699 void ImfManager::NotifyTextInputMultiLine( bool multiLine )
700 {
701   Ecore_IMF_Input_Hints currentHint = ecore_imf_context_input_hint_get(mIMFContext);
702   ecore_imf_context_input_hint_set( mIMFContext,
703                                     static_cast< Ecore_IMF_Input_Hints >( multiLine ?
704                                       (currentHint | ECORE_IMF_INPUT_HINT_MULTILINE) :
705                                       (currentHint & ~ECORE_IMF_INPUT_HINT_MULTILINE)));
706 }
707
708 Dali::ImfManager::TextDirection ImfManager::GetTextDirection()
709 {
710   Dali::ImfManager::TextDirection direction ( Dali::ImfManager::LeftToRight );
711
712   if ( ImfManager::IsAvailable() /* We do not want to create an instance of ImfManager */ )
713   {
714     if ( mIMFContext )
715     {
716       char* locale( NULL );
717       ecore_imf_context_input_panel_language_locale_get( mIMFContext, &locale );
718
719       if ( locale )
720       {
721         direction = static_cast< Dali::ImfManager::TextDirection >( Locale::GetDirection( std::string( locale ) ) );
722         free( locale );
723       }
724     }
725   }
726   return direction;
727 }
728
729 Rect<int> ImfManager::GetInputMethodArea()
730 {
731   int xPos, yPos, width, height;
732
733   width = height = xPos = yPos = 0;
734
735   if( mIMFContext )
736   {
737     ecore_imf_context_input_panel_geometry_get( mIMFContext, &xPos, &yPos, &width, &height );
738   }
739   else
740   {
741     DALI_LOG_WARNING("VKB Unable to get IMF Context so GetSize unavailable\n");
742   // return 0 as real size unknown.
743   }
744
745   return Rect<int>(xPos,yPos,width,height);
746 }
747
748 void ImfManager::ApplyOptions( const InputMethodOptions& options )
749 {
750   using namespace Dali::InputMethod::Category;
751
752   int index;
753
754   if (mIMFContext == NULL)
755   {
756     DALI_LOG_WARNING("VKB Unable to excute ApplyOptions with Null ImfContext\n");
757     return;
758   }
759
760   if ( mOptions.CompareAndSet(PANEL_LAYOUT, options, index) )
761   {
762     ecore_imf_context_input_panel_layout_set( mIMFContext, panelLayoutMap[index] );
763   }
764   if ( mOptions.CompareAndSet(AUTO_CAPITALISE, options, index) )
765   {
766     ecore_imf_context_autocapital_type_set( mIMFContext, autoCapitalMap[index] );
767   }
768   if ( mOptions.CompareAndSet(ACTION_BUTTON_TITLE, options, index) )
769   {
770     ecore_imf_context_input_panel_return_key_type_set( mIMFContext, returnKeyTypeMap[index] );
771   }
772   if ( mOptions.CompareAndSet(VARIATION, options, index) )
773   {
774     ecore_imf_context_input_panel_layout_variation_set( mIMFContext, index );
775   }
776 }
777
778 void ImfManager::SetInputPanelData( const std::string& data )
779 {
780   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetInputPanelData\n" );
781
782   if( mIMFContext )
783   {
784     int length = data.length();
785     ecore_imf_context_input_panel_imdata_set( mIMFContext, data.c_str(), length );
786   }
787 }
788
789 void ImfManager::GetInputPanelData( std::string& data )
790 {
791   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetInputPanelData\n" );
792
793   if( mIMFContext )
794   {
795     int length = 4096; // The max length is 4096 bytes
796     Dali::Vector< char > buffer;
797     buffer.Resize( length );
798     ecore_imf_context_input_panel_imdata_get( mIMFContext, &buffer[0], &length );
799     data = std::string( buffer.Begin(), buffer.End() );
800   }
801 }
802
803 Dali::ImfManager::State ImfManager::GetInputPanelState()
804 {
805   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetInputPanelState\n" );
806
807   if( mIMFContext )
808   {
809     int value;
810     value = ecore_imf_context_input_panel_state_get( mIMFContext );
811
812     switch (value)
813     {
814       case ECORE_IMF_INPUT_PANEL_STATE_SHOW:
815       {
816         return Dali::ImfManager::SHOW;
817         break;
818       }
819
820       case ECORE_IMF_INPUT_PANEL_STATE_HIDE:
821       {
822         return Dali::ImfManager::HIDE;
823         break;
824       }
825
826       case ECORE_IMF_INPUT_PANEL_STATE_WILL_SHOW:
827       {
828         return Dali::ImfManager::WILL_SHOW;
829         break;
830       }
831
832       default:
833       {
834         return Dali::ImfManager::DEFAULT;
835       }
836     }
837   }
838   return Dali::ImfManager::DEFAULT;
839 }
840
841 void ImfManager::SetReturnKeyState( bool visible )
842 {
843   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetReturnKeyState\n" );
844
845   if( mIMFContext )
846   {
847     ecore_imf_context_input_panel_return_key_disabled_set( mIMFContext, !visible );
848   }
849 }
850
851 void ImfManager::AutoEnableInputPanel( bool enabled )
852 {
853   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::AutoEnableInputPanel\n" );
854
855   if( mIMFContext )
856   {
857     ecore_imf_context_input_panel_enabled_set( mIMFContext, enabled );
858   }
859 }
860
861 void ImfManager::ShowInputPanel()
862 {
863   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::ShowInputPanel\n" );
864
865   if( mIMFContext )
866   {
867     ecore_imf_context_input_panel_show( mIMFContext );
868   }
869 }
870
871 void ImfManager::HideInputPanel()
872 {
873   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::HideInputPanel\n" );
874
875   if( mIMFContext )
876   {
877     ecore_imf_context_input_panel_hide( mIMFContext );
878   }
879 }
880
881 Dali::ImfManager::KeyboardType ImfManager::GetKeyboardType()
882 {
883   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetKeyboardType\n" );
884
885 #ifdef OVER_TIZEN_VERSION_4
886   if( mIMFContext )
887   {
888     int value;
889     value = ecore_imf_context_keyboard_mode_get( mIMFContext );
890
891     switch (value)
892     {
893       case ECORE_IMF_INPUT_PANEL_SW_KEYBOARD_MODE:
894       {
895         return Dali::ImfManager::SOFTWARE_KEYBOARD;
896         break;
897       }
898       case ECORE_IMF_INPUT_PANEL_HW_KEYBOARD_MODE:
899       {
900         return Dali::ImfManager::HARDWARE_KEYBOARD;
901         break;
902       }
903     }
904   }
905 #endif // OVER_TIZEN_VERSION_4
906   return Dali::ImfManager::KeyboardType::SOFTWARE_KEYBOARD;
907 }
908
909 std::string ImfManager::GetInputPanelLocale()
910 {
911   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetInputPanelLocale\n" );
912
913   std::string locale = "";
914
915   if( mIMFContext )
916   {
917     char* value = NULL;
918     ecore_imf_context_input_panel_language_locale_get( mIMFContext, &value );
919
920     if( value )
921     {
922       std::string valueCopy( value );
923       locale = valueCopy;
924
925       // The locale string retrieved must be freed with free().
926       free( value );
927     }
928   }
929   return locale;
930 }
931
932 } // Adaptor
933
934 } // Internal
935
936 } // Dali
937
938 #pragma GCC diagnostic pop