d476e29fafab6a625259aff7f7bc6a052ba1eacf
[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 bool ImfManager::IsAvailable()
262 {
263   bool available( false );
264
265   Dali::SingletonService service( SingletonService::Get() );
266   if ( service )
267   {
268     available = service.GetSingleton( typeid( Dali::ImfManager ) );
269   }
270
271   return available;
272 }
273
274 Dali::ImfManager ImfManager::Get()
275 {
276   Dali::ImfManager manager;
277
278   Dali::SingletonService service( SingletonService::Get() );
279   if ( service )
280   {
281     // Check whether the singleton is already created
282     Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::ImfManager ) );
283     if( handle )
284     {
285       // If so, downcast the handle
286       manager = Dali::ImfManager( dynamic_cast< ImfManager* >( handle.GetObjectPtr() ) );
287     }
288     else if ( Adaptor::IsAvailable() )
289     {
290       // Create instance and register singleton only if the adaptor is available
291
292       Adaptor& adaptorImpl( Adaptor::GetImplementation( Adaptor::Get() ) );
293       Any nativeWindow = adaptorImpl.GetNativeWindowHandle();
294
295       // The Ecore_Wl_Window needs to use the ImfManager.
296       // Only when the render surface is window, we can get the Ecore_Wl_Window.
297       Ecore_Wl_Window *ecoreWwin( AnyCast< Ecore_Wl_Window* >( nativeWindow ) );
298       if (ecoreWwin)
299       {
300         // If we fail to get Ecore_Wl_Window, we can't use the ImfManager correctly.
301         // Thus you have to call "ecore_imf_context_client_window_set" somewhere.
302         // In EvasPlugIn, this function is called in EvasPlugin::ConnectEcoreEvent().
303
304         manager = Dali::ImfManager( new ImfManager( ecoreWwin ) );
305         service.Register( typeid( manager ), manager );
306       }
307       else
308       {
309         DALI_LOG_ERROR("Failed to get native window handle\n");
310       }
311     }
312   }
313
314   return manager;
315 }
316
317 ImfManager::ImfManager( Ecore_Wl_Window *ecoreWlwin )
318 : mIMFContext(),
319   mIMFCursorPosition( 0 ),
320   mSurroundingText(),
321   mRestoreAfterFocusLost( false ),
322   mIdleCallbackConnected( false )
323 {
324   ecore_imf_init();
325   CreateContext( ecoreWlwin );
326
327   ConnectCallbacks();
328 }
329
330 ImfManager::~ImfManager()
331 {
332   DisconnectCallbacks();
333
334   DeleteContext();
335   ecore_imf_shutdown();
336 }
337
338
339 void ImfManager::CreateContext( Ecore_Wl_Window *ecoreWlwin )
340 {
341   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::CreateContext\n" );
342
343   const char *contextId = ecore_imf_context_default_id_get();
344   if( contextId )
345   {
346     mIMFContext = ecore_imf_context_add( contextId );
347
348     if( mIMFContext )
349     {
350       if( ecoreWlwin )
351       {
352           ecore_imf_context_client_window_set( mIMFContext,
353             reinterpret_cast<void*>( ecore_wl_window_id_get(ecoreWlwin)) );
354       }
355     }
356     else
357     {
358       DALI_LOG_WARNING("IMF Unable to get IMF Context\n");
359     }
360   }
361   else
362   {
363     DALI_LOG_WARNING("IMF Unable to get IMF Context\n");
364   }
365 }
366
367 void ImfManager::DeleteContext()
368 {
369   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DeleteContext\n" );
370
371   if ( mIMFContext )
372   {
373     ecore_imf_context_del( mIMFContext );
374     mIMFContext = NULL;
375   }
376 }
377
378 // Callbacks for predicitive text support.
379 void ImfManager::ConnectCallbacks()
380 {
381   if ( mIMFContext )
382   {
383     DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::ConnectCallbacks\n" );
384
385     ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_PREEDIT_CHANGED,      PreEdit,    this );
386     ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_COMMIT,               Commit,     this );
387     ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_DELETE_SURROUNDING,   ImfDeleteSurrounding, this );
388     ecore_imf_context_event_callback_add( mIMFContext, ECORE_IMF_CALLBACK_PRIVATE_COMMAND_SEND, PrivateCommand, this );
389
390     ecore_imf_context_input_panel_event_callback_add( mIMFContext, ECORE_IMF_INPUT_PANEL_STATE_EVENT,    InputPanelStateChangeCallback, this );
391     ecore_imf_context_input_panel_event_callback_add( mIMFContext, ECORE_IMF_INPUT_PANEL_LANGUAGE_EVENT, InputPanelLanguageChangeCallback, this );
392     ecore_imf_context_input_panel_event_callback_add( mIMFContext, ECORE_IMF_INPUT_PANEL_GEOMETRY_EVENT, InputPanelGeometryChangedCallback, this );
393     ecore_imf_context_input_panel_event_callback_add( mIMFContext, ECORE_IMF_INPUT_PANEL_KEYBOARD_MODE_EVENT, InputPanelKeyboardTypeChangedCallback, this );
394
395     ecore_imf_context_retrieve_surrounding_callback_set( mIMFContext, ImfRetrieveSurrounding, this);
396   }
397 }
398
399 void ImfManager::DisconnectCallbacks()
400 {
401   if ( mIMFContext )
402   {
403     DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DisconnectCallbacks\n" );
404
405     ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_PREEDIT_CHANGED,      PreEdit );
406     ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_COMMIT,               Commit );
407     ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_DELETE_SURROUNDING,   ImfDeleteSurrounding );
408     ecore_imf_context_event_callback_del( mIMFContext, ECORE_IMF_CALLBACK_PRIVATE_COMMAND_SEND, PrivateCommand );
409
410     ecore_imf_context_input_panel_event_callback_del( mIMFContext, ECORE_IMF_INPUT_PANEL_STATE_EVENT,    InputPanelStateChangeCallback     );
411     ecore_imf_context_input_panel_event_callback_del( mIMFContext, ECORE_IMF_INPUT_PANEL_LANGUAGE_EVENT, InputPanelLanguageChangeCallback  );
412     ecore_imf_context_input_panel_event_callback_del( mIMFContext, ECORE_IMF_INPUT_PANEL_GEOMETRY_EVENT, InputPanelGeometryChangedCallback );
413     ecore_imf_context_input_panel_event_callback_del( mIMFContext, ECORE_IMF_INPUT_PANEL_KEYBOARD_MODE_EVENT, InputPanelKeyboardTypeChangedCallback );
414
415     // We do not need to unset the retrieve surrounding callback.
416   }
417 }
418
419 void ImfManager::Activate()
420 {
421   // Reset mIdleCallbackConnected
422   mIdleCallbackConnected = false;
423
424   if ( mIMFContext )
425   {
426     DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Activate\n" );
427
428     ecore_imf_context_focus_in( mIMFContext );
429
430     // emit keyboard activated signal
431     Dali::ImfManager handle( this );
432     mActivatedSignal.Emit( handle );
433   }
434 }
435
436 void ImfManager::Deactivate()
437 {
438   if( mIMFContext )
439   {
440     DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Deactivate\n" );
441
442     Reset();
443     ecore_imf_context_focus_out( mIMFContext );
444   }
445
446   // Reset mIdleCallbackConnected
447   mIdleCallbackConnected = false;
448 }
449
450 void ImfManager::Reset()
451 {
452   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::Reset\n" );
453
454   if ( mIMFContext )
455   {
456     ecore_imf_context_reset( mIMFContext );
457   }
458 }
459
460 Ecore_IMF_Context* ImfManager::GetContext()
461 {
462   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetContext\n" );
463
464   return mIMFContext;
465 }
466
467 bool ImfManager::RestoreAfterFocusLost() const
468 {
469   return mRestoreAfterFocusLost;
470 }
471
472 void ImfManager::SetRestoreAfterFocusLost( bool toggle )
473 {
474   mRestoreAfterFocusLost = toggle;
475 }
476
477 /**
478  * Called when an IMF Pre-Edit changed event is received.
479  * We are still predicting what the user is typing.  The latest string is what the IMF module thinks
480  * the user wants to type.
481  */
482 void ImfManager::PreEditChanged( void*, Ecore_IMF_Context* imfContext, void* event_info )
483 {
484   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::PreEditChanged\n" );
485
486   char* preEditString( NULL );
487   int cursorPosition( 0 );
488   Eina_List* attrs = NULL;
489   Eina_List* l = NULL;
490
491   Ecore_IMF_Preedit_Attr* attr;
492
493   // Retrieves attributes as well as the string the cursor position offset from start of pre-edit string.
494   // the attributes (attrs) is used in languages that use the soft arrows keys to insert characters into a current pre-edit string.
495   ecore_imf_context_preedit_string_with_attributes_get( imfContext, &preEditString, &attrs, &cursorPosition );
496
497   if ( attrs )
498   {
499     // iterate through the list of attributes getting the type, start and end position.
500     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) ) ))
501     {
502 #ifdef DALI_PROFILE_UBUNTU
503       if ( attr->preedit_type == ECORE_IMF_PREEDIT_TYPE_SUB3 ) // (Ecore_IMF)
504 #else // DALI_PROFILE_UBUNTU
505       if ( attr->preedit_type == ECORE_IMF_PREEDIT_TYPE_SUB4 ) // (Ecore_IMF)
506 #endif // DALI_PROFILE_UBUNTU
507       {
508         // 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.
509
510         size_t visualCharacterIndex = 0;
511         size_t byteIndex = 0;
512
513         // iterate through null terminated string checking each character's position against the given byte position ( attr->end_index ).
514         const char leadByte = preEditString[byteIndex];
515         while( leadByte != '\0' )
516         {
517           // attr->end_index is provided as a byte position not character and we need to know the character position.
518           const size_t currentSequenceLength = Utf8SequenceLength( leadByte ); // returns number of bytes used to represent character.
519           if ( byteIndex == attr->end_index )
520           {
521             cursorPosition = visualCharacterIndex;
522             break;
523             // end loop as found cursor position that matches byte position
524           }
525           else
526           {
527             byteIndex += currentSequenceLength; // jump to next character
528             visualCharacterIndex++;  // increment character count so we know our position for when we get a match
529           }
530
531           DALI_ASSERT_DEBUG( visualCharacterIndex < strlen( preEditString ));
532         }
533       }
534     }
535   }
536
537   if ( Dali::Adaptor::IsAvailable() )
538   {
539     Dali::ImfManager handle( this );
540     Dali::ImfManager::ImfEventData imfEventData( Dali::ImfManager::PREEDIT, preEditString, cursorPosition, 0 );
541     Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfEventData );
542
543     if ( callbackData.update )
544     {
545       SetCursorPosition( callbackData.cursorPosition );
546       SetSurroundingText( callbackData.currentText );
547
548       NotifyCursorPosition();
549     }
550
551     if ( callbackData.preeditResetRequired )
552     {
553       Reset();
554     }
555   }
556   free( preEditString );
557 }
558
559 void ImfManager::CommitReceived( void*, Ecore_IMF_Context* imfContext, void* event_info )
560 {
561   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::CommitReceived\n" );
562
563   if ( Dali::Adaptor::IsAvailable() )
564   {
565     const std::string keyString( static_cast<char*>( event_info ) );
566
567     Dali::ImfManager handle( this );
568     Dali::ImfManager::ImfEventData imfEventData( Dali::ImfManager::COMMIT, keyString, 0, 0 );
569     Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfEventData );
570
571     if( callbackData.update )
572     {
573       SetCursorPosition( callbackData.cursorPosition );
574       SetSurroundingText( callbackData.currentText );
575
576       NotifyCursorPosition();
577     }
578   }
579 }
580
581 /**
582  * Called when an IMF retrieve surround event is received.
583  * Here the IMF module wishes to know the string we are working with and where within the string the cursor is
584  * We need to signal the application to tell us this information.
585  */
586 Eina_Bool ImfManager::RetrieveSurrounding( void* data, Ecore_IMF_Context* imfContext, char** text, int* cursorPosition )
587 {
588   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::RetrieveSurrounding\n" );
589
590   Dali::ImfManager::ImfEventData imfData( Dali::ImfManager::GETSURROUNDING, std::string(), 0, 0 );
591   Dali::ImfManager handle( this );
592   Dali::ImfManager::ImfCallbackData callbackData = mEventSignal.Emit( handle, imfData );
593
594   if( callbackData.update )
595   {
596     if( text )
597     {
598       // The memory allocated by strdup() can be freed by ecore_imf_context_surrounding_get() internally.
599       *text = strdup( callbackData.currentText.c_str() );
600     }
601
602     if( cursorPosition )
603     {
604       mIMFCursorPosition = static_cast<int>( callbackData.cursorPosition );
605       *cursorPosition = mIMFCursorPosition;
606     }
607   }
608
609   return EINA_TRUE;
610 }
611
612 /**
613  * Called when an IMF delete surrounding event is received.
614  * Here we tell the application that it should delete a certain range.
615  */
616 void ImfManager::DeleteSurrounding( void* data, Ecore_IMF_Context* imfContext, void* event_info )
617 {
618   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::DeleteSurrounding\n" );
619
620   if( Dali::Adaptor::IsAvailable() )
621   {
622     Ecore_IMF_Event_Delete_Surrounding* deleteSurroundingEvent = static_cast<Ecore_IMF_Event_Delete_Surrounding*>( event_info );
623
624     Dali::ImfManager::ImfEventData imfData( Dali::ImfManager::DELETESURROUNDING, std::string(), deleteSurroundingEvent->offset, deleteSurroundingEvent->n_chars );
625     Dali::ImfManager handle( this );
626     mEventSignal.Emit( handle, imfData );
627   }
628 }
629
630 /**
631  * Called when the input method sends a private command.
632  */
633 void ImfManager::SendPrivateCommand( void* data, Ecore_IMF_Context* imfContext, void* event_info )
634 {
635   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SendPrivateCommand\n" );
636
637   if( Dali::Adaptor::IsAvailable() )
638   {
639     const char* privateCommandSendEvent = static_cast<const char*>( event_info );
640
641     Dali::ImfManager::ImfEventData imfData( Dali::ImfManager::PRIVATECOMMAND, privateCommandSendEvent, 0, 0 );
642     Dali::ImfManager handle( this );
643     mEventSignal.Emit( handle, imfData );
644   }
645 }
646
647 void ImfManager::NotifyCursorPosition()
648 {
649   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::NotifyCursorPosition\n" );
650
651   if( mIMFContext )
652   {
653     ecore_imf_context_cursor_position_set( mIMFContext, mIMFCursorPosition );
654   }
655 }
656
657 void ImfManager::SetCursorPosition( unsigned int cursorPosition )
658 {
659   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetCursorPosition\n" );
660
661   mIMFCursorPosition = static_cast<int>( cursorPosition );
662 }
663
664 unsigned int ImfManager::GetCursorPosition() const
665 {
666   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetCursorPosition\n" );
667
668   return static_cast<unsigned int>( mIMFCursorPosition );
669 }
670
671 void ImfManager::SetSurroundingText( const std::string& text )
672 {
673   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetSurroundingText\n" );
674
675   mSurroundingText = text;
676 }
677
678 const std::string& ImfManager::GetSurroundingText() const
679 {
680   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetSurroundingText\n" );
681
682   return mSurroundingText;
683 }
684
685 void ImfManager::NotifyTextInputMultiLine( bool multiLine )
686 {
687   Ecore_IMF_Input_Hints currentHint = ecore_imf_context_input_hint_get(mIMFContext);
688   ecore_imf_context_input_hint_set( mIMFContext,
689                                     static_cast< Ecore_IMF_Input_Hints >( multiLine ?
690                                       (currentHint | ECORE_IMF_INPUT_HINT_MULTILINE) :
691                                       (currentHint & ~ECORE_IMF_INPUT_HINT_MULTILINE)));
692 }
693
694 Dali::ImfManager::TextDirection ImfManager::GetTextDirection()
695 {
696   Dali::ImfManager::TextDirection direction ( Dali::ImfManager::LeftToRight );
697
698   if ( ImfManager::IsAvailable() /* We do not want to create an instance of ImfManager */ )
699   {
700     if ( mIMFContext )
701     {
702       char* locale( NULL );
703       ecore_imf_context_input_panel_language_locale_get( mIMFContext, &locale );
704
705       if ( locale )
706       {
707         direction = static_cast< Dali::ImfManager::TextDirection >( Locale::GetDirection( std::string( locale ) ) );
708         free( locale );
709       }
710     }
711   }
712   return direction;
713 }
714
715 Rect<int> ImfManager::GetInputMethodArea()
716 {
717   int xPos, yPos, width, height;
718
719   width = height = xPos = yPos = 0;
720
721   if( mIMFContext )
722   {
723     ecore_imf_context_input_panel_geometry_get( mIMFContext, &xPos, &yPos, &width, &height );
724   }
725   else
726   {
727     DALI_LOG_WARNING("VKB Unable to get IMF Context so GetSize unavailable\n");
728   // return 0 as real size unknown.
729   }
730
731   return Rect<int>(xPos,yPos,width,height);
732 }
733
734 void ImfManager::ApplyOptions( const InputMethodOptions& options )
735 {
736   using namespace Dali::InputMethod::Category;
737
738   int index;
739
740   if (mIMFContext == NULL)
741   {
742     DALI_LOG_WARNING("VKB Unable to excute ApplyOptions with Null ImfContext\n");
743     return;
744   }
745
746   if ( mOptions.CompareAndSet(PANEL_LAYOUT, options, index) )
747   {
748     ecore_imf_context_input_panel_layout_set( mIMFContext, panelLayoutMap[index] );
749   }
750   if ( mOptions.CompareAndSet(AUTO_CAPITALISE, options, index) )
751   {
752     ecore_imf_context_autocapital_type_set( mIMFContext, autoCapitalMap[index] );
753   }
754   if ( mOptions.CompareAndSet(ACTION_BUTTON_TITLE, options, index) )
755   {
756     ecore_imf_context_input_panel_return_key_type_set( mIMFContext, returnKeyTypeMap[index] );
757   }
758   if ( mOptions.CompareAndSet(VARIATION, options, index) )
759   {
760     ecore_imf_context_input_panel_layout_variation_set( mIMFContext, index );
761   }
762 }
763
764 void ImfManager::SetInputPanelData( const std::string& data )
765 {
766   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetInputPanelData\n" );
767
768   if( mIMFContext )
769   {
770     int length = data.length();
771     ecore_imf_context_input_panel_imdata_set( mIMFContext, data.c_str(), length );
772   }
773 }
774
775 void ImfManager::GetInputPanelData( std::string& data )
776 {
777   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetInputPanelData\n" );
778
779   if( mIMFContext )
780   {
781     int length = 4096; // The max length is 4096 bytes
782     Dali::Vector< char > buffer;
783     buffer.Resize( length );
784     ecore_imf_context_input_panel_imdata_get( mIMFContext, &buffer[0], &length );
785     data = std::string( buffer.Begin(), buffer.End() );
786   }
787 }
788
789 Dali::ImfManager::State ImfManager::GetInputPanelState()
790 {
791   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetInputPanelState\n" );
792
793   if( mIMFContext )
794   {
795     int value;
796     value = ecore_imf_context_input_panel_state_get( mIMFContext );
797
798     switch (value)
799     {
800       case ECORE_IMF_INPUT_PANEL_STATE_SHOW:
801       {
802         return Dali::ImfManager::SHOW;
803         break;
804       }
805
806       case ECORE_IMF_INPUT_PANEL_STATE_HIDE:
807       {
808         return Dali::ImfManager::HIDE;
809         break;
810       }
811
812       case ECORE_IMF_INPUT_PANEL_STATE_WILL_SHOW:
813       {
814         return Dali::ImfManager::WILL_SHOW;
815         break;
816       }
817
818       default:
819       {
820         return Dali::ImfManager::DEFAULT;
821       }
822     }
823   }
824   return Dali::ImfManager::DEFAULT;
825 }
826
827 void ImfManager::SetReturnKeyState( bool visible )
828 {
829   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::SetReturnKeyState\n" );
830
831   if( mIMFContext )
832   {
833     ecore_imf_context_input_panel_return_key_disabled_set( mIMFContext, !visible );
834   }
835 }
836
837 void ImfManager::AutoEnableInputPanel( bool enabled )
838 {
839   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::AutoEnableInputPanel\n" );
840
841   if( mIMFContext )
842   {
843     ecore_imf_context_input_panel_enabled_set( mIMFContext, enabled );
844   }
845 }
846
847 void ImfManager::ShowInputPanel()
848 {
849   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::ShowInputPanel\n" );
850
851   if( mIMFContext )
852   {
853     ecore_imf_context_input_panel_show( mIMFContext );
854   }
855 }
856
857 void ImfManager::HideInputPanel()
858 {
859   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::HideInputPanel\n" );
860
861   if( mIMFContext )
862   {
863     ecore_imf_context_input_panel_hide( mIMFContext );
864   }
865 }
866
867 Dali::ImfManager::KeyboardType ImfManager::GetKeyboardType()
868 {
869   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetKeyboardType\n" );
870
871 #ifdef OVER_TIZEN_VERSION_4
872   if( mIMFContext )
873   {
874     int value;
875     value = ecore_imf_context_keyboard_mode_get( mIMFContext );
876
877     switch (value)
878     {
879       case ECORE_IMF_INPUT_PANEL_SW_KEYBOARD_MODE:
880       {
881         return Dali::ImfManager::SOFTWARE_KEYBOARD;
882         break;
883       }
884       case ECORE_IMF_INPUT_PANEL_HW_KEYBOARD_MODE:
885       {
886         return Dali::ImfManager::HARDWARE_KEYBOARD;
887         break;
888       }
889     }
890   }
891 #endif // OVER_TIZEN_VERSION_4
892   return Dali::ImfManager::KeyboardType::SOFTWARE_KEYBOARD;
893 }
894
895 std::string ImfManager::GetInputPanelLocale()
896 {
897   DALI_LOG_INFO( gLogFilter, Debug::General, "ImfManager::GetInputPanelLocale\n" );
898
899   std::string locale = "";
900
901   if( mIMFContext )
902   {
903     char* value = NULL;
904     ecore_imf_context_input_panel_language_locale_get( mIMFContext, &value );
905
906     if( value )
907     {
908       std::string valueCopy( value );
909       locale = valueCopy;
910
911       // The locale string retrieved must be freed with free().
912       free( value );
913     }
914   }
915   return locale;
916 }
917
918 } // Adaptor
919
920 } // Internal
921
922 } // Dali
923
924 #pragma GCC diagnostic pop