[Tizen] Move KeyGrab from internal tizen to public API
[platform/core/uifw/dali-adaptor.git] / dali / internal / input / tizen-wayland / key-grab-ecore-wl.cpp
1 /*
2  * Copyright (c) 2016 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/public-api/adaptor-framework/key-grab.h>
20
21 // EXTERNAL INCLUDES
22 #include <Ecore_Wayland.h>
23 #include <dali/integration-api/debug.h>
24
25 // INTERNAL INCLUDES
26 #include <dali/public-api/adaptor-framework/window.h>
27 #include <dali/internal/input/common/key-impl.h>
28
29 #include <iostream>
30 #include <string.h>
31
32 using namespace std;
33
34 namespace Dali
35 {
36
37 namespace KeyGrab
38 {
39
40 bool GrabKeyTopmost( Window window, Dali::KEY daliKey )
41 {
42   return GrabKey( window, daliKey, TOPMOST);
43 }
44
45
46 bool UngrabKeyTopmost( Window window, Dali::KEY daliKey )
47 {
48   return UngrabKey( window, daliKey );
49 }
50
51
52 bool GrabKey( Window window, Dali::KEY daliKey, KeyGrabMode grabMode )
53 {
54   Ecore_Wl_Window_Keygrab_Mode wlGrabMode;
55   if( grabMode == TOPMOST )
56   {
57     wlGrabMode = ECORE_WL_WINDOW_KEYGRAB_TOPMOST;
58   }
59   else if( grabMode == SHARED )
60   {
61     wlGrabMode = ECORE_WL_WINDOW_KEYGRAB_SHARED;
62   }
63   else if( grabMode == OVERRIDE_EXCLUSIVE )
64   {
65     wlGrabMode = ECORE_WL_WINDOW_KEYGRAB_OVERRIDE_EXCLUSIVE;
66   }
67   else if( grabMode == EXCLUSIVE )
68   {
69     wlGrabMode = ECORE_WL_WINDOW_KEYGRAB_EXCLUSIVE;
70   }
71   else
72   {
73     return false;
74   }
75
76   return ecore_wl_window_keygrab_set( AnyCast<Ecore_Wl_Window*>( window.GetNativeHandle() ),
77                                       Dali::Internal::Adaptor::KeyLookup::GetKeyName( daliKey ),
78                                       0, 0, 0, wlGrabMode );
79 }
80
81
82 bool UngrabKey( Window window, Dali::KEY daliKey )
83 {
84   return ecore_wl_window_keygrab_unset( AnyCast<Ecore_Wl_Window*>( window.GetNativeHandle() ),
85                                       Dali::Internal::Adaptor::KeyLookup::GetKeyName( daliKey ),
86                                       0, 0 );
87 }
88
89
90 bool GrabKeyList( Window window, const Dali::Vector<Dali::KEY>& daliKeyVector, const Dali::Vector<KeyGrabMode>& grabModeVector, Dali::Vector<bool>& returnVector)
91 {
92   const Dali::Vector<bool>::SizeType returnCount = returnVector.Count();
93   const Dali::Vector<Dali::KEY>::SizeType keyCount = daliKeyVector.Count();
94   const Dali::Vector<KeyGrabMode>::SizeType keyGrabModeCount = grabModeVector.Count();
95
96   if( keyCount != keyGrabModeCount || keyCount != returnCount || keyCount == 0 )
97   {
98     return false;
99   }
100
101   eina_init();
102
103   Eina_List* keyList = NULL;
104   {
105     for( Dali::Vector<float>::SizeType index = 0; index < keyCount; ++index )
106     {
107       Ecore_Wl_Window_Keygrab_Info info;
108       info.key = const_cast<char*>(Dali::Internal::Adaptor::KeyLookup::GetKeyName( daliKeyVector[index] ));
109
110       switch( grabModeVector[index] )
111       {
112         case TOPMOST:
113         {
114           info.mode = ECORE_WL_WINDOW_KEYGRAB_TOPMOST;
115           break;
116         }
117         case SHARED:
118         {
119           info.mode = ECORE_WL_WINDOW_KEYGRAB_SHARED;
120           break;
121         }
122         case OVERRIDE_EXCLUSIVE:
123         {
124           info.mode = ECORE_WL_WINDOW_KEYGRAB_OVERRIDE_EXCLUSIVE;
125           break;
126         }
127         case EXCLUSIVE:
128         {
129           info.mode = ECORE_WL_WINDOW_KEYGRAB_EXCLUSIVE;
130           break;
131         }
132         default:
133         {
134           info.mode = ECORE_WL_WINDOW_KEYGRAB_UNKNOWN;
135           break;
136         }
137       }
138
139       keyList = eina_list_append( keyList, &info );
140     }
141   }
142
143   Eina_List* grabList = ecore_wl_window_keygrab_list_set( AnyCast<Ecore_Wl_Window*>( window.GetNativeHandle() ), keyList );
144
145   returnVector.Resize( keyCount, true );
146
147   Eina_List* l = NULL;
148   Eina_List* m = NULL;
149   void *listData = NULL;
150   void *data = NULL;
151   if( grabList != NULL )
152   {
153     EINA_LIST_FOREACH( grabList, m, data )
154     {
155       Dali::Vector<float>::SizeType index = 0;
156       EINA_LIST_FOREACH( keyList, l, listData )
157       {
158         if((static_cast<Ecore_Wl_Window_Keygrab_Info*>(listData))->key == NULL)
159         {
160           DALI_LOG_ERROR("input key list has null data!");
161           break;
162         }
163
164         if( strcmp( static_cast<char*>(data), static_cast<Ecore_Wl_Window_Keygrab_Info*>(listData)->key ) == 0 )
165         {
166           returnVector[index] = false;
167         }
168         ++index;
169       }
170     }
171   }
172
173   eina_list_free( keyList );
174   eina_list_free( grabList );
175   eina_shutdown();
176
177   return true;
178 }
179
180 bool UngrabKeyList( Window window, const Dali::Vector<Dali::KEY>& daliKeyVector, Dali::Vector<bool>& returnVector)
181 {
182   const Dali::Vector<bool>::SizeType returnCount = returnVector.Count();
183   const Dali::Vector<Dali::KEY>::SizeType keyCount = daliKeyVector.Count();
184
185   if( keyCount != returnCount ||keyCount == 0 )
186   {
187     return false;
188   }
189
190   eina_init();
191
192   Eina_List* keyList = NULL;
193   {
194     for( Dali::Vector<float>::SizeType index = 0; index < keyCount; ++index )
195     {
196       Ecore_Wl_Window_Keygrab_Info info;
197       info.key = const_cast<char*>(Dali::Internal::Adaptor::KeyLookup::GetKeyName( daliKeyVector[index] ));
198       keyList = eina_list_append( keyList, &info );
199     }
200   }
201
202   Eina_List* ungrabList = ecore_wl_window_keygrab_list_unset( AnyCast<Ecore_Wl_Window*>( window.GetNativeHandle() ), keyList );
203
204   returnVector.Resize( keyCount, true );
205
206   Eina_List* l = NULL;
207   Eina_List* m = NULL;
208   void *listData = NULL;
209   void *data = NULL;
210
211   if( ungrabList != NULL )
212   {
213     EINA_LIST_FOREACH( ungrabList, m, data )
214     {
215       Dali::Vector<float>::SizeType index = 0;
216       EINA_LIST_FOREACH( keyList, l, listData )
217       {
218         if( strcmp( static_cast<char*>(data), static_cast<Ecore_Wl_Window_Keygrab_Info*>(listData)->key ) == 0 )
219         {
220           returnVector[index] = false;
221         }
222         ++index;
223       }
224     }
225   }
226
227   eina_list_free( keyList );
228   eina_list_free( ungrabList );
229   eina_shutdown();
230
231   return true;
232 }
233
234 } // namespace KeyGrab
235
236 } // namespace Dali
237