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