Merge branch 'devel/master(1.2.18)' into tizen
[platform/core/uifw/dali-adaptor.git] / adaptors / ecore / common / ecore-virtual-keyboard.cpp
1 /*
2  * Copyright (c) 2015 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 #include "virtual-keyboard-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <algorithm>
23
24 #include <dali/integration-api/debug.h>
25
26 // INTERNAL INCLUDES
27 #include "ecore-virtual-keyboard.h"
28 #include <adaptor.h>
29 #include <locale-utils.h>
30 #include <imf-manager-impl.h>
31
32 namespace Dali
33 {
34
35 namespace Internal
36 {
37
38 namespace Adaptor
39 {
40
41 namespace VirtualKeyboard
42 {
43
44 namespace
45 {
46 #if defined(DEBUG_ENABLED)
47 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VIRTUAL_KEYBOARD");
48 #endif
49
50 #define TOKEN_STRING(x) #x
51
52 //forward declarations
53 void InputPanelGeometryChangedCallback ( void *data, Ecore_IMF_Context *context, int value );
54 void InputPanelLanguageChangeCallback( void* data, Ecore_IMF_Context* context, int value );
55
56 // Signals
57 Dali::VirtualKeyboard::StatusSignalType gKeyboardStatusSignal;
58 Dali::VirtualKeyboard::VoidSignalType   gKeyboardResizeSignal;
59 Dali::VirtualKeyboard::VoidSignalType   gKeyboardLanguageChangedSignal;
60
61 void InputPanelStateChangeCallback( void* data, Ecore_IMF_Context* context, int value )
62 {
63   switch (value)
64   {
65     case ECORE_IMF_INPUT_PANEL_STATE_SHOW:
66     {
67       DALI_LOG_INFO( gLogFilter, Debug::General, "VKB ECORE_IMF_INPUT_PANEL_STATE_SHOW\n" );
68
69       gKeyboardStatusSignal.Emit( true );
70
71       break;
72     }
73
74     case ECORE_IMF_INPUT_PANEL_STATE_HIDE:
75     {
76       DALI_LOG_INFO( gLogFilter, Debug::General, "VKB ECORE_IMF_INPUT_PANEL_STATE_HIDE\n" );
77
78       gKeyboardStatusSignal.Emit( false );
79
80       break;
81     }
82
83     case ECORE_IMF_INPUT_PANEL_STATE_WILL_SHOW:
84     default:
85     {
86       // Do nothing
87       break;
88     }
89   }
90 }
91
92 void InputPanelLanguageChangeCallback( void* data, Ecore_IMF_Context* context, int value )
93 {
94   DALI_LOG_INFO( gLogFilter, Debug::General, "VKB InputPanelLanguageChangeCallback\n" );
95
96   // Emit the signal that the language has changed
97   gKeyboardLanguageChangedSignal.Emit();
98 }
99
100 void InputPanelGeometryChangedCallback ( void *data, Ecore_IMF_Context *context, int value )
101 {
102   DALI_LOG_INFO( gLogFilter, Debug::General, "VKB InputPanelGeometryChangedCallback\n" );
103
104   // Emit signal that the keyboard is resized
105   gKeyboardResizeSignal.Emit();
106 }
107
108 } // unnamed namespace
109
110 void ConnectCallbacks( Ecore_IMF_Context *imfContext )
111 {
112   if( imfContext )
113   {
114     DALI_LOG_INFO( gLogFilter, Debug::General, "VKB ConnectPanelCallbacks\n" );
115
116     ecore_imf_context_input_panel_event_callback_add( imfContext, ECORE_IMF_INPUT_PANEL_STATE_EVENT,    InputPanelStateChangeCallback,     NULL );
117     ecore_imf_context_input_panel_event_callback_add( imfContext, ECORE_IMF_INPUT_PANEL_LANGUAGE_EVENT, InputPanelLanguageChangeCallback,  NULL );
118     ecore_imf_context_input_panel_event_callback_add( imfContext, ECORE_IMF_INPUT_PANEL_GEOMETRY_EVENT, InputPanelGeometryChangedCallback, NULL );
119   }
120 }
121
122 void DisconnectCallbacks( Ecore_IMF_Context *imfContext )
123 {
124   if( imfContext )
125   {
126     DALI_LOG_INFO( gLogFilter, Debug::General, "VKB DisconnectPanelCallbacks\n" );
127
128     ecore_imf_context_input_panel_event_callback_del( imfContext, ECORE_IMF_INPUT_PANEL_STATE_EVENT,    InputPanelStateChangeCallback     );
129     ecore_imf_context_input_panel_event_callback_del( imfContext, ECORE_IMF_INPUT_PANEL_LANGUAGE_EVENT, InputPanelLanguageChangeCallback  );
130     ecore_imf_context_input_panel_event_callback_del( imfContext, ECORE_IMF_INPUT_PANEL_GEOMETRY_EVENT, InputPanelGeometryChangedCallback );
131   }
132 }
133
134 void Show()
135 {
136   Dali::ImfManager imfManager = ImfManager::Get(); // Create ImfManager instance (if required) to show the keyboard
137
138   if( imfManager )
139   {
140     Ecore_IMF_Context* imfContext = ImfManager::GetImplementation( imfManager ).GetContext();
141
142     if( imfContext )
143     {
144       ecore_imf_context_input_panel_show( imfContext );
145     }
146   }
147 }
148
149 void Hide()
150 {
151   if( ImfManager::IsAvailable() /* We do not want to create an ImfManager instance*/ )
152   {
153     Dali::ImfManager imfManager = ImfManager::Get();
154     Ecore_IMF_Context* imfContext = ImfManager::GetImplementation( imfManager ).GetContext();
155
156     if( imfContext )
157     {
158       ecore_imf_context_input_panel_hide( imfContext );
159     }
160   }
161 }
162
163 bool IsVisible()
164 {
165   if( ImfManager::IsAvailable() /* We do not want to create an ImfManager instance */ )
166   {
167     DALI_LOG_INFO( gLogFilter, Debug::General, "IMF IsVisible\n" );
168
169     Dali::ImfManager imfManager = ImfManager::Get();
170     Ecore_IMF_Context* imfContext = ImfManager::GetImplementation( imfManager ).GetContext();
171
172     if ( imfContext )
173     {
174       if (ecore_imf_context_input_panel_state_get(imfContext) == ECORE_IMF_INPUT_PANEL_STATE_SHOW ||
175           ecore_imf_context_input_panel_state_get(imfContext) == ECORE_IMF_INPUT_PANEL_STATE_WILL_SHOW)
176       {
177         return true;
178       }
179     }
180   }
181
182   return false;
183 }
184
185 void ApplySettings( const Property::Map& settingsMap )
186 {
187   using namespace InputMethod; // Allows exclusion of namespace in TOKEN_STRING.
188
189   for ( unsigned int i = 0, count = settingsMap.Count(); i < count; ++i )
190   {
191     Property::Key key = settingsMap.GetKeyAt( i );
192     if( key.type == Property::Key::INDEX )
193     {
194       continue;
195     }
196
197     Property::Value item = settingsMap.GetValue(i);
198
199     if ( key == TOKEN_STRING( ACTION_BUTTON ) )
200     {
201       if ( item.GetType() == Property::INTEGER )
202       {
203         int value = item.Get< int >();
204         VirtualKeyboard::SetReturnKeyType( static_cast<InputMethod::ActionButton>(value) );
205       }
206     }
207     else
208     {
209       DALI_LOG_INFO( gLogFilter, Debug::General, "Provided Settings Key not supported\n" );
210     }
211   }
212 }
213
214 void EnablePrediction(const bool enable)
215 {
216   Dali::ImfManager imfManager = ImfManager::Get(); // Create ImfManager instance (if required) when enabling prediction
217
218   if( imfManager )
219   {
220     Ecore_IMF_Context* imfContext = ImfManager::GetImplementation( imfManager ).GetContext();
221
222     if ( imfContext )
223     {
224       ecore_imf_context_prediction_allow_set( imfContext, (enable)? EINA_TRUE : EINA_FALSE);
225     }
226   }
227 }
228
229 bool IsPredictionEnabled()
230 {
231   if ( ImfManager::IsAvailable() /* We do not want to create an instance of ImfManger */ )
232   {
233     Dali::ImfManager imfManager = ImfManager::Get();
234     Ecore_IMF_Context* imfContext = ImfManager::GetImplementation( imfManager ).GetContext();
235
236     if ( imfContext )
237     {
238       // predictive text is enabled.
239       if ( ecore_imf_context_input_panel_enabled_get( imfContext ) == EINA_TRUE )
240       {
241         return true;
242       }
243     }
244   }
245
246   return false;
247 }
248
249 Rect<int> GetSizeAndPosition()
250 {
251   int xPos, yPos, width, height;
252
253   width = height = xPos = yPos = 0;
254   Dali::ImfManager imfManager = ImfManager::Get(); // Create ImfManager instance (if required) as we may need to do some size related setup in the application
255
256   if( imfManager )
257   {
258     Ecore_IMF_Context* imfContext = ImfManager::GetImplementation( imfManager ).GetContext();
259
260     if( imfContext )
261     {
262       ecore_imf_context_input_panel_geometry_get(imfContext, &xPos, &yPos, &width, &height);
263     }
264     else
265     {
266       DALI_LOG_WARNING("VKB Unable to get IMF Context so GetSize unavailable\n");
267     // return 0 as real size unknown.
268     }
269   }
270
271   return Rect<int>(xPos,yPos,width,height);
272 }
273
274 Dali::VirtualKeyboard::StatusSignalType& StatusChangedSignal()
275 {
276   return gKeyboardStatusSignal;
277 }
278
279 Dali::VirtualKeyboard::VoidSignalType& ResizedSignal()
280 {
281   return gKeyboardResizeSignal;
282 }
283
284 Dali::VirtualKeyboard::VoidSignalType& LanguageChangedSignal()
285 {
286   return gKeyboardLanguageChangedSignal;
287 }
288
289 Dali::VirtualKeyboard::TextDirection GetTextDirection()
290 {
291   Dali::VirtualKeyboard::TextDirection direction ( Dali::VirtualKeyboard::LeftToRight );
292
293   if ( ImfManager::IsAvailable() /* We do not want to create an instance of ImfManager */ )
294   {
295     Dali::ImfManager imfManager = ImfManager::Get();
296
297     if ( imfManager )
298     {
299       Ecore_IMF_Context* imfContext = ImfManager::GetImplementation( imfManager ).GetContext();
300
301       if ( imfContext )
302       {
303         char* locale( NULL );
304         ecore_imf_context_input_panel_language_locale_get( imfContext, &locale );
305
306         if ( locale )
307         {
308           direction = Locale::GetTextDirection( std::string( locale ) );
309           free( locale );
310         }
311       }
312     }
313   }
314   return direction;
315 }
316
317 } // namespace VirtualKeyboard
318
319 } // namespace Adaptor
320
321 } // namespace Internal
322
323 } // namespace Dali