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