fix build-break regarding libslp-utilx
[platform/core/api/efl-util.git] / src / efl_util.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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 #define LOG_TAG "TIZEN_N_EFL_UTIL"
19
20 #include <efl_util.h>
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <Elementary.h>
26
27 #if ECORE_X_FOUND
28 #include <Ecore_X.h>
29 #include <utilX.h>
30 #endif
31
32 typedef struct _notification_error_cb_info
33 {
34         Evas_Object *window;
35         efl_util_notification_window_level_error_cb err_cb;
36         void *user_data;
37 } notification_error_cb_info;
38
39 Eina_List *_g_notification_error_cb_info_list;
40 static Ecore_Event_Handler* _noti_level_access_result_handler = NULL;
41 static int _noti_handler_count = 0;
42
43 static notification_error_cb_info *_notification_error_cb_info_find(Evas_Object *window);
44 static Eina_Bool _efl_util_notification_info_add(Evas_Object *window, efl_util_notification_window_level_error_cb callback, void *user_data);
45 static Eina_Bool _efl_util_notification_info_del(Evas_Object *window);
46
47 #if ECORE_X_FOUND
48 static unsigned int _noti_level_access_result_atom = 0;
49
50 static Eina_Bool _efl_util_client_message(void *data, int type, void *event);
51 static notification_error_cb_info *_notification_error_cb_info_find_by_xwin(unsigned int xwin);
52 #endif
53
54
55 int efl_util_set_notification_window_level (Evas_Object* window, efl_util_notification_level_e level)
56 {
57         EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
58         EINA_SAFETY_ON_FALSE_RETURN_VAL(level >= EFL_UTIL_NOTIFICATION_LEVEL_1 &&
59                                         level <= EFL_UTIL_NOTIFICATION_LEVEL_3,
60                                         EFL_UTIL_ERROR_INVALID_PARAMETER);
61
62 #if ECORE_X_FOUND
63         Ecore_X_Window xwin = elm_win_xwindow_get(window);
64         if (xwin)
65         {
66                 Ecore_X_Window_Type window_type;
67                 if(ecore_x_netwm_window_type_get(xwin, &window_type) == EINA_TRUE)
68                 {
69                         // success to get window type
70                         if(window_type != ECORE_X_WINDOW_TYPE_NOTIFICATION)
71                         {
72                                 // given EFL window's type is not notification type.
73                                 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
74                         }
75                 }
76                 else
77                         return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
78
79                 utilx_set_system_notification_level(ecore_x_display_get(), xwin,
80                                                     level);
81                 return EFL_UTIL_ERROR_NONE;
82         }
83 #endif
84
85 #if ECORE_WAYLAND_FOUND
86         Ecore_Wl_Window wl_win = elm_win_wl_window_get(window);
87         if (wl_win)
88         {
89                 printf("not implemented for wayland yet\n");
90                 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
91         }
92 #endif
93
94        return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
95 }
96
97
98
99 int efl_util_get_notification_window_level (Evas_Object* window, efl_util_notification_level_e* level)
100 {
101         
102         EINA_SAFETY_ON_NULL_RETURN_VAL(window,
103                                        EFL_UTIL_ERROR_INVALID_PARAMETER);
104         EINA_SAFETY_ON_NULL_RETURN_VAL(level,
105                                        EFL_UTIL_ERROR_INVALID_PARAMETER);
106
107 #if ECORE_X_FOUND
108         Ecore_X_Window_Type window_type;
109         Utilx_Notification_Level utilx_level;
110         Ecore_X_Window xwin = elm_win_xwindow_get(window);
111         if (xwin)
112         {
113                 if(ecore_x_netwm_window_type_get(xwin, &window_type) == EINA_TRUE)
114                 {
115                         // success to get window type
116                         if(window_type != ECORE_X_WINDOW_TYPE_NOTIFICATION)
117                         {
118                                 // given EFL window's type is not notification type.
119                                 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
120                         }
121                 
122                         utilx_level = utilx_get_system_notification_level (ecore_x_display_get(), xwin);
123
124                         if(utilx_level == UTILX_NOTIFICATION_LEVEL_LOW)
125                         {
126                                 *level = EFL_UTIL_NOTIFICATION_LEVEL_1;
127                         }
128                         else if(utilx_level == UTILX_NOTIFICATION_LEVEL_NORMAL)
129                         {
130                                 *level = EFL_UTIL_NOTIFICATION_LEVEL_2;
131                         }
132                         else if(utilx_level == UTILX_NOTIFICATION_LEVEL_HIGH)
133                         {
134                                 *level = EFL_UTIL_NOTIFICATION_LEVEL_3;
135                         }
136                         else
137                         {
138                                 return EFL_UTIL_ERROR_INVALID_PARAMETER;
139                         }
140                 
141                 }
142                 else
143                 {
144                         // fail to get window type
145                         return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
146                 }
147         
148                 return EFL_UTIL_ERROR_NONE;
149         }
150 #endif
151
152 #if ECORE_WAYLAND_FOUND
153         Ecore_Wl_Window wl_win = elm_win_wl_window_get(window);
154         if (wl_win)
155         {
156                 printf("not implemented for wayland yet\n");
157                 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
158         }
159 #endif
160         return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
161 }
162
163 int efl_util_set_notification_window_level_error_cb(Evas_Object *window, efl_util_notification_window_level_error_cb callback, void *user_data)
164 {
165         Eina_Bool ret = EINA_FALSE;
166
167         EINA_SAFETY_ON_NULL_RETURN_VAL(window,
168                                        EFL_UTIL_ERROR_INVALID_PARAMETER);
169         EINA_SAFETY_ON_NULL_RETURN_VAL(callback,
170                                        EFL_UTIL_ERROR_INVALID_PARAMETER);
171
172         ret = _efl_util_notification_info_add(window, callback, user_data);
173         if (ret)
174         {
175 #if ECORE_X_FOUND
176                 if (!_noti_level_access_result_atom)
177                         _noti_level_access_result_atom = ecore_x_atom_get("_E_NOTIFICATION_LEVEL_ACCESS_RESULT");
178
179                 if (!_noti_level_access_result_handler)
180                         _noti_level_access_result_handler = ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE, _efl_util_client_message, NULL);
181                 _noti_handler_count++;
182
183                 return EFL_UTIL_ERROR_NONE;
184 #endif
185
186 #if ECORE_WAYLAND_FOUND
187                 printf("not implemented for wayland yet\n");
188                 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
189 #endif
190         }
191
192         return EFL_UTIL_ERROR_OUT_OF_MEMORY;
193 }
194
195 int efl_util_unset_notification_window_level_error_cb(Evas_Object *window)
196 {
197         Eina_Bool ret = EINA_FALSE;
198
199         EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
200
201         ret = _efl_util_notification_info_del(window);
202         if (ret)
203         {
204                 _noti_handler_count--;
205                 if (_noti_handler_count == 0)
206                 {
207                         if (_noti_level_access_result_handler)
208                         {
209                                 ecore_event_handler_del(_noti_level_access_result_handler);
210                                 _noti_level_access_result_handler = NULL;
211                         }
212                 }
213                 return EFL_UTIL_ERROR_NONE;
214         }
215
216         return EFL_UTIL_ERROR_INVALID_PARAMETER;
217 }
218
219 int efl_util_grab_key (Evas_Object *obj, const char* key, int grab_mode)
220 {
221         fprintf (stderr, "%s deprecated\n", __FUNCTION__);
222
223         return 0;
224 }
225
226 int efl_util_ungrab_key (Evas_Object *obj, const char* key)
227 {
228         fprintf (stderr, "%s deprecated\n", __FUNCTION__);
229
230         return 0;
231 }
232
233 void efl_util_set_system_notification_level (Evas_Object *obj, Efl_Util_Notification_Level level)
234 {
235         fprintf (stderr, "%s deprecated\n", __FUNCTION__);
236 }
237
238 Efl_Util_Notification_Level efl_util_get_system_notification_level (Evas_Object *obj)
239 {
240         fprintf (stderr, "%s deprecated\n", __FUNCTION__);
241         return EFL_UTIL_NOTIFICATION_LEVEL_UNKNOWN;
242 }
243
244 void efl_util_netwm_window_type_set(Evas_Object *obj, Efl_Util_Window_Type type)
245 {
246         fprintf (stderr, "%s deprecated\n", __FUNCTION__);
247 }
248
249 void efl_util_set_window_effect_style(Evas_Object *win, Efl_Util_Effect_Type type, Efl_Util_Effect_Style style)
250 {
251         fprintf (stderr, "%s deprecated\n", __FUNCTION__);
252 }
253
254 int efl_util_set_window_opaque_state (Evas_Object *win, Efl_Util_Opaque_State state)
255 {
256         fprintf (stderr, "%s deprecated\n", __FUNCTION__);
257         return 0;
258 }
259
260 #if ECORE_X_FOUND
261 static Eina_Bool _efl_util_client_message(void *data, int type, void *event)
262 {
263         Ecore_X_Event_Client_Message *ev;
264
265         ev = event;
266         if (!ev) return ECORE_CALLBACK_PASS_ON;
267
268         if (ev->message_type == _noti_level_access_result_atom)
269         {
270                 Ecore_X_Window xwin;
271                 xwin = ev->win;
272
273                 notification_error_cb_info *cb_info = NULL;
274                 cb_info = _notification_error_cb_info_find_by_xwin(xwin);
275                 if (cb_info)
276                 {
277                         int access = ev->data.l[1];
278                         if (access == 0) // permission denied
279                         {
280                                 if (cb_info->err_cb)
281                                 {
282                                         cb_info->err_cb(cb_info->window, EFL_UTIL_ERROR_PERMISSION_DENIED, cb_info->user_data);
283                                 }
284                         }
285                 }
286         }
287
288         return ECORE_CALLBACK_PASS_ON;
289 }
290
291 static notification_error_cb_info *_notification_error_cb_info_find_by_xwin(unsigned int xwin)
292 {
293         Eina_List *l;
294         notification_error_cb_info* temp;
295         unsigned int temp_xwin;
296
297         EINA_LIST_FOREACH(_g_notification_error_cb_info_list, l, temp)
298         {
299                 if (temp->window)
300                 {
301                         temp_xwin = elm_win_xwindow_get(temp->window);
302                         if (xwin == temp_xwin)
303                         {
304                                 return temp;
305                         }
306                 }
307         }
308
309         return NULL;
310 }
311 #endif
312
313 static notification_error_cb_info *_notification_error_cb_info_find(Evas_Object *window)
314 {
315         Eina_List *l;
316         notification_error_cb_info* temp;
317
318         EINA_LIST_FOREACH(_g_notification_error_cb_info_list, l, temp)
319         {
320                 if (temp->window == window)
321                 {
322                         return temp;
323                 }
324         }
325
326         return NULL;
327 }
328
329 static Eina_Bool _efl_util_notification_info_add(Evas_Object *window, efl_util_notification_window_level_error_cb callback, void *user_data)
330 {
331         notification_error_cb_info* _err_info = _notification_error_cb_info_find(window);
332
333         if (_err_info)
334         {
335                 _g_notification_error_cb_info_list = eina_list_remove(_g_notification_error_cb_info_list, _err_info);
336                 free(_err_info);
337                 _err_info = NULL;
338         }
339
340         _err_info = (notification_error_cb_info*)calloc(1, sizeof(notification_error_cb_info));
341         if (!_err_info)
342         {
343                 return EINA_FALSE;
344         }
345         _err_info->window = window;
346         _err_info->err_cb = callback;
347         _err_info->user_data = user_data;
348
349         _g_notification_error_cb_info_list = eina_list_append(_g_notification_error_cb_info_list, _err_info);
350
351         return EINA_TRUE;
352 }
353
354 static Eina_Bool _efl_util_notification_info_del(Evas_Object *window)
355 {
356         notification_error_cb_info* _err_info = _notification_error_cb_info_find(window);
357         if (!_err_info)
358         {
359                 return EINA_FALSE;
360         }
361
362         _g_notification_error_cb_info_list = eina_list_remove(_g_notification_error_cb_info_list, _err_info);
363         free(_err_info);
364
365         return EINA_TRUE;
366 }