[Tizen] Add key grab list API
[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     info->key = (char*)Dali::Internal::Adaptor::KeyLookup::GetKeyName( daliKeyVector[index] );
103
104     switch(grabModeVector[index])
105     {
106       case TOPMOST:
107         info->mode = ECORE_WL_WINDOW_KEYGRAB_TOPMOST;
108         break;
109       case SHARED:
110         info->mode = ECORE_WL_WINDOW_KEYGRAB_SHARED;
111         break;
112       case OVERRIDE_EXCLUSIVE:
113         info->mode = ECORE_WL_WINDOW_KEYGRAB_EXCLUSIVE;
114         break;
115       case EXCLUSIVE:
116         info->mode = ECORE_WL_WINDOW_KEYGRAB_OVERRIDE_EXCLUSIVE;
117         break;
118       default:
119         info->mode = ECORE_WL_WINDOW_KEYGRAB_UNKNOWN;
120         break;
121     }
122
123     keyList = eina_list_append(keyList, info);
124   }
125
126   grabList = ecore_wl_window_keygrab_list_set(AnyCast<Ecore_Wl_Window*>( window.GetNativeHandle() ), keyList);
127
128   for( Dali::Vector<float>::SizeType index = 0; index < keyCount; ++index )
129   {
130     resultVector.PushBack(true);
131   }
132
133   if( grabList != NULL)
134   {
135     EINA_LIST_FOREACH(grabList, m, data)
136     {
137       Dali::Vector<float>::SizeType index = 0;
138       EINA_LIST_FOREACH(keyList, l, listData)
139       {
140         if(strcmp((char*)data, ((Ecore_Wl_Window_Keygrab_Info*)listData)->key) == 0)
141           resultVector[index] = false;
142
143         ++index;
144       }
145     }
146   }
147
148   eina_list_free(keyList);
149   eina_list_free(grabList);
150   eina_shutdown();
151
152   return resultVector;
153 }
154
155 Dali::Vector<bool> UngrabKeyList( Window window, const Dali::Vector<Dali::KEY>& daliKeyVector )
156 {
157   Dali::Vector<bool> resultVector;
158   Eina_List *keyList = NULL, *ungrabList = NULL, *l = NULL, *m = NULL;
159   void *listData = NULL, *data = NULL;
160   Dali::Vector<Dali::KEY>::SizeType keyCount = daliKeyVector.Count();
161
162
163   if(keyCount == 0)
164     return resultVector;
165
166   eina_init();
167
168   for( Dali::Vector<float>::SizeType index = 0; index < keyCount; ++index )
169   {
170     Ecore_Wl_Window_Keygrab_Info *info = (Ecore_Wl_Window_Keygrab_Info*)malloc(sizeof(Ecore_Wl_Window_Keygrab_Info));
171     info->key = (char*)Dali::Internal::Adaptor::KeyLookup::GetKeyName( daliKeyVector[index] );
172     keyList = eina_list_append(keyList, info);
173   }
174
175   ungrabList = ecore_wl_window_keygrab_list_unset(AnyCast<Ecore_Wl_Window*>( window.GetNativeHandle() ), keyList);
176
177   for( Dali::Vector<float>::SizeType index = 0; index < keyCount; ++index )
178   {
179     resultVector.PushBack(true);
180   }
181
182   if( ungrabList != NULL)
183   {
184     EINA_LIST_FOREACH(ungrabList, m, data)
185     {
186       Dali::Vector<float>::SizeType index = 0;
187       EINA_LIST_FOREACH(keyList, l, listData)
188       {
189         if(strcmp((char*)data, ((Ecore_Wl_Window_Keygrab_Info*)listData)->key) == 0)
190           resultVector[index] = false;
191
192         ++index;
193       }
194     }
195   }
196
197   eina_list_free(keyList);
198   eina_list_free(ungrabList);
199   eina_shutdown();
200
201   return resultVector;
202 }
203
204 } // namespace KeyGrab
205
206 } // namespace Dali
207
208