pui: declare ecore_wl2 internal APIs as extern APIs
[platform/core/uifw/libpui.git] / src / PUI.c
1 /*
2  * Copyright © 2019 Samsung Electronics co., Ltd. All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include "PUI_internal.h"
27 #include "PUI_backend.h"
28 #include "PUI.h"
29
30 #include <wayland-tbm-client.h>
31 #include <tbm_surface_internal.h>
32 #include <stdio.h>
33 #include <dlfcn.h>
34
35 #ifndef PUI_MODULE_DIR
36 #define PUI_MODULE_DIR "/usr/lib"
37 #endif
38
39 extern EAPI void ecore_wl2_window_buffer_attach(Ecore_Wl2_Window *win, void *buffer, int x, int y, Eina_Bool implicit);
40 extern EAPI void ecore_wl2_window_damage(Ecore_Wl2_Window *window, Eina_Rectangle *rects, unsigned int count);
41
42 static int _pui_init_count = 0;
43 static pui_module_data *pui_module = NULL;
44
45 int PUI_EVENT_ANI_STARTED = 0;
46 int PUI_EVENT_ANI_STOPPED = 0;
47 int PUI_EVENT_ANI_PAUSED = 0;
48 int PUI_EVENT_ANI_READY_TO_START = 0;
49 int PUI_EVENT_ANI_READY_TO_RESUME = 0;
50
51 static int KEY_WL_BUFFER = 0xabcdbeaf;
52 static int KEY_CLIENT = 0xdcbabeaf;
53
54 static const char *
55 _pui_magic_string_get(PUI_Magic m)
56 {
57         switch (m)
58         {
59                 case PUI_MAGIC_NONE:
60                         return "None (Freed Object)";
61                         break;
62
63                 case PUI_MAGIC_PUI_M:
64                         return "PUI MODULE (PUI_M)";
65                         break;
66
67                 case PUI_MAGIC_PUI_H:
68                         return "PUI HANDLE (PUI_H)";
69                         break;
70
71                 case PUI_MAGIC_ANI_H:
72                         return "PUI ANI HANDLE (ANI_H)";
73                         break;
74
75                 case PUI_MAGIC_ANI_T:
76                         return "PUI ANI RUNTIME HANDLE (ANI_T)";
77                         break;
78
79                 default:
80                         return "<Unknown>";
81         }
82 }
83
84 PUI_API void
85 _pui_magic_fail(const void *p, PUI_Magic m, PUI_Magic req_m, const char *fname)
86 {
87         pui_err(" ### PUI ERROR : PUI Magic Check Failed !!! in %s().\n", fname);
88
89         if (!p)
90                 pui_err("   Given handle/pointer is NULL !\n");
91         else if (m == PUI_MAGIC_NONE)
92                 pui_err("   Given handle/pointer has been freed !\n");
93         else if (m != req_m)
94                 pui_err("   Given handle/pointer is wrong type\n"
95                                 "      Expected: %08x - %s\n"
96                                 "      Supplied: %08x - %s",
97                                 (unsigned int)req_m, _pui_magic_string_get(req_m),
98                                 (unsigned int)m, _pui_magic_string_get(m));
99
100         /* abort here as a failure of magic can be caused by a memory corruption */
101         pui_err("### PUI MAGIC FAILED ! Abort ! ###\n");
102         abort();
103 }
104
105 pui_error_string
106 pui_error_to_string(pui_error e)
107 {
108         pui_error_string str = NULL;
109
110         switch (e)
111         {
112                 case PUI_ERROR_NONE:
113                         str = "PUI_No_Error";
114                         break;
115
116                 case PUI_ERROR_INVALID_ANI_HANDLE:
117                         str = "PUI_Invalid_Animation_Handle";
118                         break;
119
120                 case PUI_ERROR_INVALID_ANI_CMD:
121                         str = "PUI_Invalid_Animation_Command";
122                         break;
123
124                 case PUI_ERROR_INTERNAL:
125                         str = "PUI_Internal_Error";
126                         break;
127
128                 case PUI_ERROR_MANUAL_RENDER_ENABLED:
129                         str = "PUI_Manual_Render_Enabled";
130                         break;
131
132                 case PUI_ERROR_UNABLE_SET_MANUAL_RENDER:
133                         str = "PUI_Unable_To_Set_Manual_Render";
134                         break;
135
136                 default:
137                         str = "PUI_Unknown_Error";
138         }
139
140         return str;
141 }
142
143 static void
144 _buffer_release(void *data, struct wl_buffer *buffer)
145 {
146         tbm_surface_h surface = (tbm_surface_h)data;
147         pui_h handle;
148
149         tbm_surface_internal_get_user_data(surface, (unsigned long)&KEY_CLIENT, (void **)&handle);
150         tbm_surface_queue_release(handle->tbm_queue, surface);
151
152         pui_info("[UPDATE] release wl_buffer:%p, surface:%p\n", buffer, surface);
153 }
154
155 static const struct wl_buffer_listener buffer_listener = {
156     _buffer_release
157 };
158
159 pui_ani_control_buffer *
160 pui_display_get_last_buffer(pui_h handle)
161 {
162         if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
163         {
164                 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
165                 return NULL;
166         }
167
168         if (handle->last_buffer.size <= 0)
169         {
170                 pui_err("Failed to get last buffer\n");
171                 return NULL;
172         }
173
174         return &handle->last_buffer;
175 }
176
177
178 pui_ani_control_buffer *
179 pui_display_get_buffer(pui_h handle)
180 {
181         tbm_surface_error_e ret;
182         tbm_surface_h surface;
183         pui_ani_control_buffer *buffer = NULL;
184
185         if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
186         {
187                 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
188                 return NULL;
189         }
190
191         if (handle->current_surface)
192         {
193                 pui_debug("[GET BUFFER] Current_surface is not used !\n");
194         }
195
196         if (!tbm_surface_queue_can_dequeue(handle->tbm_queue, 0))
197         {
198                 pui_err("[GET BUFFER] Failed to dequeue a tbm_surface !\n");
199                 return NULL;
200         }
201
202         ret = tbm_surface_queue_dequeue(handle->tbm_queue, &surface);
203
204         if (ret != TBM_SURFACE_ERROR_NONE)
205         {
206                 pui_err("[GET BUFFER] Dequeue err:%d\n", ret);
207                 return NULL;
208         }
209
210         tbm_surface_map(surface, TBM_SURF_OPTION_READ | TBM_SURF_OPTION_WRITE, &handle->current_sinfo);
211
212         handle->current_surface = surface;
213         buffer = (pui_ani_control_buffer *)&(handle->current_sinfo.planes[0]);
214
215         return buffer;
216 }
217
218 pui_error
219 pui_display_set_buffer(pui_h handle, pui_ani_control_buffer *buffer)
220 {
221         if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
222         {
223                 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
224                 return PUI_ERROR_INVALID_HANDLE;
225         }
226
227         if (!handle->current_surface)
228         {
229                 pui_err("Current_surface is not valid !\n");
230                 return PUI_ERROR_INVALID_SURFACE;
231         }
232
233         if (!buffer || !buffer->ptr || !buffer->size)
234                 return PUI_ERROR_INVALID_BUFFER;
235
236         handle->is_buffer_set = 1;
237
238         return PUI_ERROR_NONE;
239 }
240
241 pui_error
242 pui_display_update(pui_h handle)
243 {
244         tbm_surface_h surface;
245         tbm_surface_error_e ret;
246         struct wl_buffer *wl_buffer = NULL;
247
248         if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
249         {
250                 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
251                 return PUI_ERROR_INVALID_HANDLE;
252         }
253
254         if (!handle->current_surface)
255                 return PUI_ERROR_INVALID_SURFACE;
256
257         if (!handle->is_buffer_set)
258         {
259                 pui_err("[UPDATE] Buffer is not set !\n");
260                 return PUI_ERROR_INVALID_BUFFER;
261         }
262
263         surface = handle->current_surface;
264         handle->current_surface = NULL;
265
266         tbm_surface_unmap(surface);
267
268         ret = tbm_surface_queue_enqueue(handle->tbm_queue, surface);
269
270         if (ret != TBM_SURFACE_ERROR_NONE)
271         {
272                 pui_err("[UPDATE] enqueue err:%d\n", ret);
273                 return PUI_ERROR_INVALID_SURFACE;
274         }
275
276         ret = tbm_surface_queue_acquire(handle->tbm_queue, &surface);
277
278         if (ret != TBM_SURFACE_ERROR_NONE)
279         {
280                 pui_err("[UPDATE] acquire err:%d\n", ret);
281                 return PUI_ERROR_INVALID_SURFACE;
282         }
283
284         if (!tbm_surface_internal_get_user_data(surface, (unsigned long)&KEY_WL_BUFFER, (void **)&wl_buffer)) {
285                 wl_buffer = wayland_tbm_client_create_buffer(handle->wl_tbm_client, surface);
286
287                 if (!wl_buffer)
288                 {
289                         pui_err("[UPDATE] failed to create wl_buffer tbm_surface:%p\n", surface);
290                         return PUI_ERROR_INVALID_BUFFER;
291                 }
292
293                 wl_buffer_add_listener(wl_buffer, &buffer_listener, surface);
294
295                 tbm_surface_internal_add_user_data(surface, (unsigned long)&KEY_WL_BUFFER, NULL);
296                 tbm_surface_internal_set_user_data(surface, (unsigned long)&KEY_WL_BUFFER, wl_buffer);
297                 tbm_surface_internal_add_user_data(surface, (unsigned long)&KEY_CLIENT, NULL);
298                 tbm_surface_internal_set_user_data(surface, (unsigned long)&KEY_CLIENT, handle);
299         }
300
301         if (!wl_buffer)
302         {
303                 pui_err("[UPDATE] dequeue err:%d\n", ret);
304                 return PUI_ERROR_INVALID_BUFFER;
305         }
306
307         ecore_wl2_window_buffer_attach(handle->win, wl_buffer, 0, 0, 0);
308         ecore_wl2_window_damage(handle->win, NULL, 0);
309         ecore_wl2_window_commit(handle->win, EINA_TRUE);
310
311         pui_info("[UPDATE] commit wl_buffer:%p, surface:%p\n", wl_buffer, surface);
312
313         handle->is_buffer_set = 0;
314
315         if (handle->last_buffer.size > 0)
316                 memcpy(handle->last_buffer.ptr, handle->current_sinfo.planes[0].ptr, handle->last_buffer.size);
317
318         return PUI_ERROR_NONE;
319 }
320
321 pui_error
322 pui_display_manual_render_set(pui_h handle, pui_bool set)
323 {
324         pui_ani_h ani_h = NULL;
325
326         if (!PUI_MAGIC_CHECK(pui_module, PUI_MAGIC_PUI_M))
327         {
328                 PUI_MAGIC_FAIL(pui_module, PUI_MAGIC_PUI_M, __FUNCTION__);
329                 return PUI_ERROR_INVALID_HANDLE;
330         }
331
332         ani_h = handle->current_ani_h;
333
334         if (ani_h)
335         {
336                 if (pui_ani_status_get(ani_h->ani) == PUI_ANI_STATUS_RUNNING ||
337                         pui_ani_status_get(ani_h->ani) == PUI_ANI_STATUS_PAUSED)
338                 {
339                         pui_err("Please stop the current animation.\n");
340                         return PUI_ERROR_UNABLE_SET_MANUAL_RENDER;
341                 }
342         }
343
344         handle->manual_render = !!set;
345
346         return PUI_ERROR_NONE;
347 }
348
349 pui_bool
350 pui_display_manual_render_get(pui_h handle)
351 {
352         if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
353         {
354                 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
355                 return 0;
356         }
357
358         return handle->manual_render;
359 }
360
361 pui_bool
362 pui_display_geometry_get(pui_h handle, int *width, int *height)
363 {
364         if (!width || !height)
365                 return 0;
366
367         *width = 0;
368         *height = 0;
369         (void) handle;
370
371         if (!PUI_MAGIC_CHECK(pui_module, PUI_MAGIC_PUI_M))
372         {
373                 PUI_MAGIC_FAIL(pui_module, PUI_MAGIC_PUI_M, __FUNCTION__);
374                 return 0;
375         }
376
377         if (!pui_module->backend_module_data) {
378                 pui_err("pui module data is not loaded\n");
379                 return 0;
380         }
381
382         if (!pui_module->backend_module_data->geometry_get)
383         {
384                 pui_err("Backend doesn't have geometry_get() !\n");
385                 return 0;
386         }
387
388         return pui_module->backend_module_data->geometry_get(width, height);
389 }
390
391 pui_h
392 pui_create(Ecore_Wl2_Window *win)
393 {
394         pui_h handle = NULL;
395         Ecore_Wl2_Display *ewd = ecore_wl2_window_display_get(win);
396         struct wayland_tbm_client *wl_tbm_client = NULL;
397         int w = 0, h = 0;
398         pui_bool res;
399
400         if (!win || !ewd)
401         {
402                 pui_err("Invalid window or display !\n");
403                 return NULL;
404         }
405
406         if (!PUI_MAGIC_CHECK(pui_module, PUI_MAGIC_PUI_M))
407         {
408                 PUI_MAGIC_FAIL(pui_module, PUI_MAGIC_PUI_M, __FUNCTION__);
409                 return NULL;
410         }
411
412         wl_tbm_client = wayland_tbm_client_init(ecore_wl2_display_get(ewd));
413
414         if (!wl_tbm_client)
415         {
416                 pui_err("Failed to init wayland_tbm_client !\n");
417                 return NULL;
418         }
419
420         handle = (pui_h)calloc(1, sizeof(pui));
421
422         if (!handle)
423                 return NULL;
424
425         handle->win = win;
426         handle->ewd = ewd;
427         handle->visibility = 0;
428         handle->wl_tbm_client = wl_tbm_client;
429         handle->ani_handles = NULL;
430         handle->current_ani_h = NULL;
431         handle->manual_render = 0;
432         handle->backend_module_data = pui_module->backend_module_data;
433
434         res = pui_display_geometry_get(handle, &w, &h);
435         if (!res)
436         {
437                 /* NOTE: This is a default value for tbm_queue.
438                  *       This value is set before this patch.
439                  */
440                 w = h = 100;
441         }
442
443         handle->last_buffer.ptr = (unsigned char *)calloc(w * h * 4, sizeof(unsigned char));
444         if (handle->last_buffer.ptr)
445                 handle->last_buffer.size = sizeof(unsigned char) * w * h * 4;
446
447         handle->tbm_queue = wayland_tbm_client_create_surface_queue(handle->wl_tbm_client,
448                                                                 ecore_wl2_window_surface_get(handle->win),
449                                                                 3, w, h, TBM_FORMAT_ABGR8888);
450
451         if (!handle->tbm_queue)
452         {
453                 pui_err("Failed to create a surface queue !");
454                 goto err;
455         }
456
457         PUI_MAGIC_SET(handle, PUI_MAGIC_PUI_H);
458         return handle;
459
460 err:
461         pui_destroy(handle);
462
463         return NULL;
464 }
465
466 void
467 pui_destroy(pui_h handle)
468 {
469         pui_ani_h ani_h = NULL;
470
471         if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
472         {
473                 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
474                 return;
475         }
476
477         EINA_LIST_FREE(handle->ani_handles, ani_h)
478         {
479                 pui_ani_destroy(ani_h);
480         }
481
482         if (handle->tbm_queue)
483         {
484                 tbm_surface_queue_destroy(handle->tbm_queue);
485         }
486
487         if (handle->wl_tbm_client)
488         {
489                 wayland_tbm_client_deinit(handle->wl_tbm_client);
490                 handle->wl_tbm_client = NULL;
491         }
492
493         if (handle->last_buffer.ptr)
494         {
495                 free(handle->last_buffer.ptr);
496                 handle->last_buffer.ptr = NULL;
497                 handle->last_buffer.size = 0;
498         }
499
500         PUI_MAGIC_SET(handle, PUI_MAGIC_NONE);
501         free(handle);
502 }
503
504 #define PREFIX_LIB    "libpui-"
505 #define SUFFIX_LIB    ".so"
506 #define DEFAULT_LIB   PREFIX_LIB"default-backend"SUFFIX_LIB
507
508 static void
509 _pui_load_backend_module(void)
510 {
511         //char path[PATH_MAX] = {0, };
512         void *module_info = NULL;
513         pui_backend_module *backend_module_info = NULL;
514         int backend_module_major, backend_module_minor;
515         int pui_backend_major, pui_backend_minor;
516
517         pui_backend_module_data *backend_module_data = NULL;
518
519         if (!PUI_MAGIC_CHECK(pui_module, PUI_MAGIC_PUI_M))
520         {
521                 PUI_MAGIC_FAIL(pui_module, PUI_MAGIC_PUI_M, __FUNCTION__);
522                 return;
523         }
524
525         module_info = dlopen(DEFAULT_LIB, RTLD_LAZY);
526
527         if (!module_info)
528         {
529                 pui_err("Failed to dlopen(error:%s, path:%s) !\n", dlerror(), DEFAULT_LIB);
530                 return;
531         }
532
533         backend_module_info = dlsym(module_info, "pui_backend_module_info");
534
535         if (!backend_module_info) {
536                 pui_err("Backend module(%s) doesn't have pui_backend_module_info !\n", DEFAULT_LIB);
537                 goto err;
538         }
539
540         pui_backend_major = PUI_BACKEND_GET_ABI_MAJOR(PUI_BACKEND_AB_VERSION_LAST);
541         pui_backend_minor = PUI_BACKEND_GET_ABI_MINOR(PUI_BACKEND_AB_VERSION_LAST);
542
543         backend_module_major = PUI_BACKEND_GET_ABI_MAJOR(backend_module_info->abi_version);
544         backend_module_minor = PUI_BACKEND_GET_ABI_MINOR(backend_module_info->abi_version);
545
546         if (backend_module_major > pui_backend_major) {
547                 pui_err("PUI backend module ABI major ver(%d) is newer than the PUI's ver(%d)\n",
548                         backend_module_major, pui_backend_major);
549                 goto err;
550         } else if (backend_module_minor > pui_backend_minor) {
551                 pui_err("PUI backend module ABI minor ver(%d) is newer than the PUI's ver(%d)\n",
552                         backend_module_minor, pui_backend_minor);
553                 goto err;
554         }
555
556         if (!backend_module_info->backend_init || !backend_module_info->backend_deinit)
557         {
558                 pui_err("Backend module doesn't have backend_init/backend_deinit function !\n");
559                 goto err;
560         }
561
562         backend_module_data = backend_module_info->backend_init();
563
564         if (!backend_module_data)
565         {
566                 pui_err("Failed to init module (%s) !\n", DEFAULT_LIB);
567                 goto err;
568         }
569
570         pui_module->module_info = module_info;
571         pui_module->backend_module_info = backend_module_info;
572         pui_module->backend_module_data = backend_module_data;
573
574         return;
575
576 err:
577         if (backend_module_info && backend_module_info->backend_deinit)
578                 backend_module_info->backend_deinit(backend_module_data);
579
580         if (module_info)
581                 dlclose(module_info);
582
583         return;
584 }
585
586 static void
587 _pui_unload_backend_module(void)
588 {
589         if (!PUI_MAGIC_CHECK(pui_module, PUI_MAGIC_PUI_M))
590         {
591                 PUI_MAGIC_FAIL(pui_module, PUI_MAGIC_PUI_M, __FUNCTION__);
592                 return;
593         }
594
595         if (pui_module->backend_module_info)
596         {
597                 pui_module->backend_module_info->backend_deinit(pui_module->backend_module_data);
598                 pui_module->backend_module_data = NULL;
599                 pui_module->backend_module_info = NULL;
600         }
601
602         if (pui_module->module_info)
603                 dlclose(pui_module->module_info);
604 }
605
606 static void
607 _pui_load_backend_collect_animations(void)
608 {
609         pui_int_error ret;
610
611         if (!PUI_MAGIC_CHECK(pui_module, PUI_MAGIC_PUI_M))
612         {
613                 PUI_MAGIC_FAIL(pui_module, PUI_MAGIC_PUI_M, __FUNCTION__);
614                 return;
615         }
616
617         if (!pui_module->backend_module_data) {
618                 pui_err("pui module data is not loaded\n");
619                 return;
620         }
621
622         if (!pui_module->backend_module_data->create_ani_collection)
623         {
624                 pui_err("Backend doesn't have create_ani_collection() !\n");
625                 return;
626         }
627
628         ret = pui_module->backend_module_data->create_ani_collection();
629         if (ret != PUI_INT_ERROR_NONE) {
630                 pui_err("Failed to collect animations data (%s)\n",
631                         pui_error_to_string(ret));
632         }
633 }
634
635 static void
636 _pui_load(void)
637 {
638         _pui_load_backend_module();
639         _pui_load_backend_collect_animations();
640 }
641
642 static void
643 _pui_unload(void)
644 {
645         _pui_unload_backend_module();
646 }
647
648 static void
649 _pui_event_init(void)
650 {
651         PUI_EVENT_ANI_STARTED = ecore_event_type_new();
652         PUI_EVENT_ANI_STOPPED = ecore_event_type_new();
653         PUI_EVENT_ANI_PAUSED = ecore_event_type_new();
654         PUI_EVENT_ANI_READY_TO_START = ecore_event_type_new();
655         PUI_EVENT_ANI_READY_TO_RESUME = ecore_event_type_new();
656 }
657
658 static void
659 _pui_event_shutdown(void)
660 {
661         ecore_event_type_flush(PUI_EVENT_ANI_STARTED,
662                                         PUI_EVENT_ANI_STOPPED,
663                                         PUI_EVENT_ANI_PAUSED,
664                                         PUI_EVENT_ANI_READY_TO_START,
665                                         PUI_EVENT_ANI_READY_TO_RESUME);
666
667         PUI_EVENT_ANI_STARTED = -1;
668         PUI_EVENT_ANI_STOPPED = -1;
669         PUI_EVENT_ANI_PAUSED = -1;
670         PUI_EVENT_ANI_READY_TO_START = -1;
671         PUI_EVENT_ANI_READY_TO_RESUME = -1;
672 }
673
674 int
675 pui_init(void)
676 {
677         const char *cp = NULL;
678
679         if (++_pui_init_count != 1)
680           return _pui_init_count;
681
682         _pui_log_level = PUI_LOG_LEVEL_DEBUG;
683
684         cp = getenv("LIBPUI_LOG_LEVEL");
685
686         if (cp)
687         {
688                 _pui_log_level = atoi(cp);
689         }
690
691         if (pui_module)
692         {
693                 pui_err("Invalid calling of pui_init() !\n");
694                 goto error;
695         }
696
697         pui_module = (pui_module_data *)calloc(1, sizeof(pui_module_data));
698
699         if (!pui_module)
700         {
701                 pui_err("Failed to allocate memory for pui module data !\n");
702                 goto error;
703         }
704
705         PUI_MAGIC_SET(pui_module, PUI_MAGIC_PUI_M);
706
707         ecore_wl2_init();
708
709         _pui_event_init();
710
711         _pui_load();
712
713         return _pui_init_count;
714
715 error:
716         return --_pui_init_count;
717 }
718
719 int
720 pui_shutdown(void)
721 {
722         if (_pui_init_count <= 0)
723         {
724                 pui_err("Invalid pui init count : %d\n", _pui_init_count);
725                 _pui_init_count = 0;
726                 return 0;
727         }
728
729         if (!PUI_MAGIC_CHECK(pui_module, PUI_MAGIC_PUI_M))
730         {
731                 PUI_MAGIC_FAIL(pui_module, PUI_MAGIC_PUI_M, __FUNCTION__);
732                 return _pui_init_count;
733         }
734
735         _pui_init_count--;
736
737         //TODO
738         _pui_unload();
739
740         _pui_event_shutdown();
741
742         ecore_wl2_shutdown();
743
744         PUI_MAGIC_SET(pui_module, PUI_MAGIC_NONE);
745         free(pui_module);
746
747         return _pui_init_count;
748 }
749