Resolve unused variable and functions warnings on wayland environment
[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         EINA_SAFETY_ON_NULL_RETURN_VAL(obj, 0);
222
223 #if ECORE_X_FOUND
224         Ecore_X_Window x_win;
225         Ecore_X_Display *x_disp;
226
227         x_win = elm_win_xwindow_get(obj);
228         if (x_win)
229         {
230                 x_disp = ecore_x_display_get();
231                 return utilx_grab_key(x_disp, x_win, key, grab_mode);
232         }
233
234 #endif
235
236 #if ECORE_WAYLAND_FOUND
237         Ecore_Wl_Window wl_win = elm_win_wl_window_get(obj);
238         if (wl_win)
239         {
240                 printf("not implemented for wayland yet\n");
241                 return 0;
242         }
243 #endif
244
245         return 0;
246 }
247
248 int efl_util_ungrab_key (Evas_Object *obj, const char* key)
249 {
250         EINA_SAFETY_ON_NULL_RETURN_VAL(obj, 0);
251
252 #if ECORE_X_FOUND
253         Ecore_X_Window x_win;
254         Ecore_X_Display *x_disp;
255
256         x_win = elm_win_xwindow_get(obj);
257         if (x_win)
258         {
259                 x_disp = ecore_x_display_get();
260                 return utilx_ungrab_key (x_disp, x_win, key);
261         }
262 #endif
263
264 #if ECORE_WAYLAND_FOUND
265         Ecore_Wl_Window wl_win = elm_win_wl_window_get(obj);
266         if (wl_win)
267         {
268                 printf("not implemented for wayland yet\n");
269                 return 0;
270         }
271 #endif
272
273         return 0;
274 }
275
276 void efl_util_set_system_notification_level (Evas_Object *obj, Efl_Util_Notification_Level level)
277 {
278         EINA_SAFETY_ON_NULL_RETURN(obj);
279         EINA_SAFETY_ON_FALSE_RETURN(level >= EFL_UTIL_NOTIFICATION_LEVEL_LOW &&
280                                     level <= EFL_UTIL_NOTIFICATION_LEVEL_UNKNOWN);
281
282 #if ECORE_X_FOUND
283         Ecore_X_Window x_win;
284         Ecore_X_Display *x_disp;
285
286         x_win = elm_win_xwindow_get(obj);
287         if (x_win)
288         {
289                 x_disp = ecore_x_display_get();
290                 utilx_set_system_notification_level(x_disp, x_win, level);
291                 return;
292         }
293 #endif
294
295 #if ECORE_WAYLAND_FOUND
296         Ecore_Wl_Window wl_win = elm_win_wl_window_get(obj);
297         if (wl_win)
298         {
299                 printf("not implemented for wayland yet\n");
300         }
301 #endif
302 }
303
304 Efl_Util_Notification_Level efl_util_get_system_notification_level (Evas_Object *obj)
305 {
306         EINA_SAFETY_ON_NULL_RETURN_VAL(obj, EFL_UTIL_NOTIFICATION_LEVEL_UNKNOWN);
307
308 #if ECORE_X_FOUND
309         Ecore_X_Window x_win;
310         Ecore_X_Display *x_disp;
311
312         x_win = elm_win_xwindow_get(obj);
313         if (x_win)
314         {
315                 x_disp = ecore_x_display_get();
316                 return utilx_get_system_notification_level(x_disp, x_win);
317         }
318
319 #endif
320
321 #if ECORE_WAYLAND_FOUND
322         Ecore_Wl_Window wl_win = elm_win_wl_window_get(obj);
323         if (wl_win)
324         {
325                 printf("not implemented for wayland yet\n");
326                 return EFL_UTIL_NOTIFICATION_LEVEL_UNKNOWN;
327         }
328 #endif
329
330         return EFL_UTIL_NOTIFICATION_LEVEL_UNKNOWN;
331 }
332
333 void efl_util_netwm_window_type_set(Evas_Object *obj, Efl_Util_Window_Type type)
334 {
335         EINA_SAFETY_ON_NULL_RETURN(obj);
336         EINA_SAFETY_ON_FALSE_RETURN(type == EFL_UTIL_WINDOW_TYPE_NORMAL ||
337                                     type == EFL_UTIL_WINDOW_TYPE_NOTIFICATION);
338
339 #if ECORE_X_FOUND
340         Ecore_X_Window x_win;
341
342         x_win = elm_win_xwindow_get(obj);
343         if (x_win)
344         {
345                 ecore_x_netwm_window_type_set(x_win, type);
346                 return;
347         }
348
349 #endif
350
351 #if ECORE_WAYLAND_FOUND
352         Ecore_Wl_Window wl_win = elm_win_wl_window_get(obj);
353         if (wl_win)
354                 printf("not implemented for wayland yet\n");
355 #endif
356 }
357
358 void efl_util_set_window_effect_style(Evas_Object *win, Efl_Util_Effect_Type type, Efl_Util_Effect_Style style)
359 {
360
361         EINA_SAFETY_ON_NULL_RETURN(win);
362
363 #if ECORE_X_FOUND
364         Ecore_X_Window x_win;
365         Ecore_X_Display *x_disp;
366
367         x_win = elm_win_xwindow_get(win);
368         if (x_win)
369         {
370                 x_disp = ecore_x_display_get();
371                 utilx_set_window_effect_style(x_disp, x_win, type, style);
372                 return;
373         }
374 #endif
375
376 #if ECORE_WAYLAND_FOUND
377         Ecore_Wl_Window wl_win = elm_win_wl_window_get(win);
378         if (wl_win)
379                 printf("not implemented for wayland yet\n");
380 #endif
381 }
382
383 int efl_util_set_window_opaque_state (Evas_Object *win, Efl_Util_Opaque_State state)
384 {
385    EINA_SAFETY_ON_NULL_RETURN_VAL(win, 0);
386
387 #if ECORE_X_FOUND
388         Ecore_X_Window x_win;
389         Ecore_X_Display *x_disp;
390
391         x_win = elm_win_xwindow_get(win);
392         if (x_win)
393         {
394                 x_disp = ecore_x_display_get();
395                 return utilx_set_window_opaque_state(x_disp, x_win, state);
396         }
397 #endif
398
399 #if ECORE_WAYLAND_FOUND
400         Ecore_Wl_Window wl_win = elm_win_wl_window_get(win);
401         if (wl_win)
402                 printf("not implemented for wayland yet\n");
403 #endif
404
405         return 0;
406 }
407
408 #if ECORE_X_FOUND
409 static Eina_Bool _efl_util_client_message(void *data, int type, void *event)
410 {
411         Ecore_X_Event_Client_Message *ev;
412
413         ev = event;
414         if (!ev) return ECORE_CALLBACK_PASS_ON;
415
416         if (ev->message_type == _noti_level_access_result_atom)
417         {
418                 Ecore_X_Window xwin;
419                 xwin = ev->win;
420
421                 notification_error_cb_info *cb_info = NULL;
422                 cb_info = _notification_error_cb_info_find_by_xwin(xwin);
423                 if (cb_info)
424                 {
425                         int access = ev->data.l[1];
426                         if (access == 0) // permission denied
427                         {
428                                 if (cb_info->err_cb)
429                                 {
430                                         cb_info->err_cb(cb_info->window, EFL_UTIL_ERROR_PERMISSION_DENIED, cb_info->user_data);
431                                 }
432                         }
433                 }
434         }
435
436         return ECORE_CALLBACK_PASS_ON;
437 }
438
439 static notification_error_cb_info *_notification_error_cb_info_find_by_xwin(unsigned int xwin)
440 {
441         Eina_List *l;
442         notification_error_cb_info* temp;
443         unsigned int temp_xwin;
444
445         EINA_LIST_FOREACH(_g_notification_error_cb_info_list, l, temp)
446         {
447                 if (temp->window)
448                 {
449                         temp_xwin = elm_win_xwindow_get(temp->window);
450                         if (xwin == temp_xwin)
451                         {
452                                 return temp;
453                         }
454                 }
455         }
456
457         return NULL;
458 }
459 #endif
460
461 static notification_error_cb_info *_notification_error_cb_info_find(Evas_Object *window)
462 {
463         Eina_List *l;
464         notification_error_cb_info* temp;
465
466         EINA_LIST_FOREACH(_g_notification_error_cb_info_list, l, temp)
467         {
468                 if (temp->window == window)
469                 {
470                         return temp;
471                 }
472         }
473
474         return NULL;
475 }
476
477 static Eina_Bool _efl_util_notification_info_add(Evas_Object *window, efl_util_notification_window_level_error_cb callback, void *user_data)
478 {
479         notification_error_cb_info* _err_info = _notification_error_cb_info_find(window);
480
481         if (_err_info)
482         {
483                 _g_notification_error_cb_info_list = eina_list_remove(_g_notification_error_cb_info_list, _err_info);
484                 free(_err_info);
485                 _err_info = NULL;
486         }
487
488         _err_info = (notification_error_cb_info*)calloc(1, sizeof(notification_error_cb_info));
489         if (!_err_info)
490         {
491                 return EINA_FALSE;
492         }
493         _err_info->window = window;
494         _err_info->err_cb = callback;
495         _err_info->user_data = user_data;
496
497         _g_notification_error_cb_info_list = eina_list_append(_g_notification_error_cb_info_list, _err_info);
498
499         return EINA_TRUE;
500 }
501
502 static Eina_Bool _efl_util_notification_info_del(Evas_Object *window)
503 {
504         notification_error_cb_info* _err_info = _notification_error_cb_info_find(window);
505         if (!_err_info)
506         {
507                 return EINA_FALSE;
508         }
509
510         _g_notification_error_cb_info_list = eina_list_remove(_g_notification_error_cb_info_list, _err_info);
511         free(_err_info);
512
513         return EINA_TRUE;
514 }