(Window) Set class-name before Map
[platform/core/uifw/dali-adaptor.git] / adaptors / common / virtual-keyboard-impl.cpp
1 /*
2  * Copyright (c) 2014 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/public-api/common/vector-wrapper.h>
25 #include <dali/integration-api/debug.h>
26
27 // INTERNAL INCLUDES
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::Verbose, 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" );
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   Ecore_IMF_Context* imfContext = reinterpret_cast<Ecore_IMF_Context*>( imfManager.GetContext() );
138
139   if( imfContext )
140   {
141     ecore_imf_context_input_panel_show( imfContext );
142   }
143 }
144
145 void Hide()
146 {
147   if( ImfManager::IsAvailable() /* We do not want to create an ImfManager instance*/ )
148   {
149     Dali::ImfManager imfManager = ImfManager::Get();
150     Ecore_IMF_Context* imfContext = reinterpret_cast<Ecore_IMF_Context*>( imfManager.GetContext() );
151
152     if( imfContext )
153     {
154       ecore_imf_context_input_panel_hide( imfContext );
155     }
156   }
157 }
158
159 bool IsVisible()
160 {
161   if( ImfManager::IsAvailable() /* We do not want to create an ImfManager instance */ )
162   {
163     DALI_LOG_INFO( gLogFilter, Debug::General, "IsVisible\n" );
164
165     Dali::ImfManager imfManager = ImfManager::Get();
166     Ecore_IMF_Context* imfContext = reinterpret_cast<Ecore_IMF_Context*>( imfManager.GetContext() );
167
168     if ( imfContext )
169     {
170       if (ecore_imf_context_input_panel_state_get(imfContext) == ECORE_IMF_INPUT_PANEL_STATE_SHOW ||
171           ecore_imf_context_input_panel_state_get(imfContext) == ECORE_IMF_INPUT_PANEL_STATE_WILL_SHOW)
172       {
173         return true;
174       }
175     }
176   }
177
178   return false;
179 }
180
181 void ApplySettings( const Property::Map& settingsMap )
182 {
183   using namespace InputMethod; // Allows exclusion of namespace in TOKEN_STRING.
184
185   for ( unsigned int i = 0, count = settingsMap.Count(); i < count; ++i )
186   {
187     std::string key = settingsMap.GetKey( i );
188     Property::Value item = settingsMap.GetValue(i);
189
190     if ( key == TOKEN_STRING( ACTION_BUTTON ) )
191     {
192       if ( item.GetType() == Property::INTEGER )
193       {
194         int value = item.Get< int >();
195         VirtualKeyboard::SetReturnKeyType( static_cast<InputMethod::ActionButton>(value) );
196       }
197     }
198     else
199     {
200       DALI_LOG_INFO( gLogFilter, Debug::General, "Provided Settings Key not supported\n" );
201     }
202    }
203 }
204
205 void EnablePrediction(const bool enable)
206 {
207   Dali::ImfManager imfManager = ImfManager::Get(); // Create ImfManager instance (if required) when enabling prediction
208   Ecore_IMF_Context* imfContext = reinterpret_cast<Ecore_IMF_Context*>( imfManager.GetContext() );
209
210   if ( imfContext )
211   {
212     ecore_imf_context_prediction_allow_set( imfContext, (enable)? EINA_TRUE : EINA_FALSE);
213   }
214 }
215
216 bool IsPredictionEnabled()
217 {
218   if ( ImfManager::IsAvailable() /* We do not want to create an instance of ImfManger */ )
219   {
220     Dali::ImfManager imfManager = ImfManager::Get();
221     Ecore_IMF_Context* imfContext = reinterpret_cast<Ecore_IMF_Context*>( imfManager.GetContext() );
222
223     if ( imfContext )
224     {
225       // predictive text is enabled.
226       if ( ecore_imf_context_input_panel_enabled_get( imfContext ) == EINA_TRUE )
227       {
228         return true;
229       }
230     }
231   }
232
233   return false;
234 }
235
236 Rect<int> GetSizeAndPosition()
237 {
238   int xPos, yPos, width, height;
239
240   width = height = xPos = yPos = 0;
241   Dali::ImfManager imfManager = ImfManager::Get(); // Create ImfManager instance (if required) as we may need to do some size related setup in the application
242   Ecore_IMF_Context* imfContext = reinterpret_cast<Ecore_IMF_Context*>( imfManager.GetContext() );
243
244   if( imfContext )
245   {
246     ecore_imf_context_input_panel_geometry_get(imfContext, &xPos, &yPos, &width, &height);
247   }
248   else
249   {
250     DALI_LOG_WARNING("VKB Unable to get IMF Context so GetSize unavailable\n");
251     // return 0 as real size unknown.
252   }
253
254   return Rect<int>(xPos,yPos,width,height);
255 }
256
257 Dali::VirtualKeyboard::StatusSignalType& StatusChangedSignal()
258 {
259   return gKeyboardStatusSignal;
260 }
261
262 Dali::VirtualKeyboard::VoidSignalType& ResizedSignal()
263 {
264   return gKeyboardResizeSignal;
265 }
266
267 Dali::VirtualKeyboard::VoidSignalType& LanguageChangedSignal()
268 {
269   return gKeyboardLanguageChangedSignal;
270 }
271
272 Dali::VirtualKeyboard::TextDirection GetTextDirection()
273 {
274   Dali::VirtualKeyboard::TextDirection direction ( Dali::VirtualKeyboard::LeftToRight );
275
276   if ( ImfManager::IsAvailable() /* We do not want to create an instance of ImfManager */ )
277   {
278     Dali::ImfManager imfManager = ImfManager::Get();
279
280     if ( imfManager )
281     {
282       Ecore_IMF_Context* imfContext = reinterpret_cast<Ecore_IMF_Context*>( imfManager.GetContext() );
283
284       if ( imfContext )
285       {
286         char* locale( NULL );
287         ecore_imf_context_input_panel_language_locale_get( imfContext, &locale );
288
289         if ( locale )
290         {
291           direction = Locale::GetTextDirection( std::string( locale ) );
292           free( locale );
293         }
294       }
295     }
296   }
297   return direction;
298 }
299
300 } // namespace VirtualKeyboard
301
302 } // namespace Adaptor
303
304 } // namespace Internal
305
306 } // namespace Dali