renew pui prototype again
[platform/core/uifw/libpui.git] / src / PUI_ani.c
index 2ac1d0d..6ed1b2d 100644 (file)
@@ -1,5 +1,6 @@
-#include "PUI.h"
 #include "PUI_internal.h"
+#include "PUI_backend.h"
+#include "PUI.h"
 #include <Eina.h>
 
 static int KEY_WL_BUFFER = 0xabcdbeaf;
@@ -53,8 +54,9 @@ _cb_focus_out(void *data, int type EINA_UNUSED, void *event)
 static Eina_Bool
 _cb_visibility_change(void *data, int type EINA_UNUSED, void *event)
 {
-       pui_ani_h handle =  (pui_ani_h)data;
-       pui_h ph = handle->pui_handle;
+       pui_ani_h ani_h =  (pui_ani_h)data;
+       pui_ani_t *ani = ani_h->ani;
+       pui_h ph = ani_h->pui_handle;
 
        Ecore_Wl2_Event_Window_Visibility_Change *ev;
        PUI_Event_Animation_Status *e = NULL;
@@ -63,48 +65,65 @@ _cb_visibility_change(void *data, int type EINA_UNUSED, void *event)
 
        pui_info("Visibility change (window=0x%x, fully_obscured=%d)\n", ev->win, ev->fully_obscured);
 
-       ph->visiblity = !(ev->fully_obscured);
+       /* check if this is needed */
+       ph->visibility = !(ev->fully_obscured);
 
        if (ev->fully_obscured)
        {
-               //TODO : e
-               if (handle->status == PUI_ANI_STATUS_RUNNING)
+               if (ani->status == PUI_ANI_STATUS_RUNNING)
                {
-                       handle->status = PUI_ANI_STATUS_STOPPED;
+                       ani->status = PUI_ANI_STATUS_STOPPED;
 
-                       //PUI_Event_Animation_Status e;
-                       if (e = (PUI_Event_Animation_Status *)calloc(1, sizeof(PUI_Event_Animation_Status)))
-                       {
-                               e->win = ev->win;
-                               e->status = PUI_ANI_STATUS_FORCE_STOPPED;
+                       e = (PUI_Event_Animation_Status *)calloc(1, sizeof(PUI_Event_Animation_Status));
 
-                               ecore_event_add(PUI_EVENT_ANI_STOPPED, e, NULL, handle);
+                       if (!e)
+                       {
+                               pui_err("Failed to allocate memory for PUI Event !\n");
+                               return ECORE_CALLBACK_PASS_ON;
                        }
+                       
+                       e->win = ev->win;
+                       e->status = PUI_ANI_STATUS_FORCE_STOPPED;
+                       
+                       ecore_event_add(PUI_EVENT_ANI_STOPPED, e, NULL, ani_h);
                }
        }
        else
        {
-               //TODO : e
-               if (e = (PUI_Event_Animation_Status *)calloc(1, sizeof(PUI_Event_Animation_Status)))
+               e = (PUI_Event_Animation_Status *)calloc(1, sizeof(PUI_Event_Animation_Status));
+               
+               if (!e)
                {
-                       e->win = ev->win;
-                       e->status = handle->status;
-
-                       if (handle->status == PUI_ANI_STATUS_STOPPED)
-                       {
-                               ecore_event_add(PUI_EVENT_ANI_READY_TO_START, e, NULL, handle);
-                       }
-                       else if(handle->status == PUI_ANI_STATUS_PAUSED)
-                       {
-                               ecore_event_add(PUI_EVENT_ANI_READY_TO_RESUME, e, NULL, handle);
-                       }
+                       pui_err("Failed to allocate memory for PUI Event !\n");
+                       return ECORE_CALLBACK_PASS_ON;
                }
+               
+               e->win = ev->win;
+               e->status = ani->status;
+               
+               if (ani->status == PUI_ANI_STATUS_STOPPED)
+                       ecore_event_add(PUI_EVENT_ANI_READY_TO_START, e, NULL, ani_h);
+               else if(ani->status == PUI_ANI_STATUS_PAUSED)
+                       ecore_event_add(PUI_EVENT_ANI_READY_TO_RESUME, e, NULL, ani_h);
        }
 
        return ECORE_CALLBACK_PASS_ON;
 }
 
 static void
+_pui_ani_cb_frame_done(Ecore_Wl2_Window *win, uint32_t timestamp EINA_UNUSED, void *data)
+{
+       pui_h handle = (pui_h) data;
+
+       pui_info("Frame done ! (window=%p)\n", win);
+
+       // TODO
+       (void) handle;
+
+       return;
+}
+
+static void
 _buffer_release(void *data, struct wl_buffer *buffer)
 {
        tbm_surface_h surface = (tbm_surface_h)data;
@@ -161,10 +180,10 @@ pui_ani_get_buffer(pui_ani_h ani_h)
                return NULL;
        }
 
-       tbm_surface_map(surface, TBM_SURF_OPTION_READ | TBM_SURF_OPTION_WRITE, &handle->sinfo);
+       tbm_surface_map(surface, TBM_SURF_OPTION_READ | TBM_SURF_OPTION_WRITE, &handle->current_sinfo);
 
        handle->current_surface = surface;
-       buffer = (pui_ani_control_buffer *)&(handle->sinfo.planes[0]);
+       buffer = (pui_ani_control_buffer *)&(handle->current_sinfo.planes[0]);
 
        return buffer;
 }
@@ -207,6 +226,7 @@ pui_ani_update(pui_ani_h ani_h)
 {
        tbm_surface_h surface;
        tbm_surface_error_e ret;
+       struct wl_buffer *wl_buffer = NULL;
 
        pui_h handle = NULL;
 
@@ -243,7 +263,7 @@ pui_ani_update(pui_ani_h ani_h)
        if (ret != TBM_SURFACE_ERROR_NONE)
        {
                pui_err("[UPDATE] enqueue err:%d\n", ret);
-               return;
+               return PUI_INT_ERROR_INVALID_SURFACE;
        }
 
        ret = tbm_surface_queue_acquire(handle->tbm_queue, &surface);
@@ -251,7 +271,7 @@ pui_ani_update(pui_ani_h ani_h)
        if (ret != TBM_SURFACE_ERROR_NONE)
        {
                pui_err("[UPDATE] acquire err:%d\n", ret);
-               return;
+               return PUI_INT_ERROR_INVALID_SURFACE;
        }
 
        if (!tbm_surface_internal_get_user_data(surface, (unsigned long)&KEY_WL_BUFFER, (void **)&wl_buffer)) {
@@ -260,7 +280,7 @@ pui_ani_update(pui_ani_h ani_h)
                if (!wl_buffer)
                {
                        pui_err("[UPDATE] failed to create wl_buffer tbm_surface:%p\n", surface);
-                       return;
+                       return PUI_INT_ERROR_INVALID_BUFFER;
                }
 
                wl_buffer_add_listener(wl_buffer, &buffer_listener, surface);
@@ -274,7 +294,7 @@ pui_ani_update(pui_ani_h ani_h)
        if (!wl_buffer)
        {
                pui_err("[UPDATE] dequeue err:%d\n", ret);
-               return;
+               return PUI_INT_ERROR_INVALID_BUFFER;
        }
 
        ecore_wl2_window_buffer_attach(handle->win, wl_buffer, 0, 0, 0);
@@ -302,17 +322,17 @@ _pui_ani_event_handlers_init(pui_ani_h ani_h)
        if (!ani_h->ecore_event_hdls)
                ani_h->ecore_event_hdls = eina_array_new(5);
 
-       h = ecore_event_handler_add(ECORE_WL2_EVENT_WINDOW_SHOW, _cb_window_show, handle);
-       eina_array_push(ani_h->_ecore_event_hdls, h);
+       h = ecore_event_handler_add(ECORE_WL2_EVENT_WINDOW_SHOW, _cb_window_show, ani_h);
+       eina_array_push(ani_h->ecore_event_hdls, h);
 
-       h = ecore_event_handler_add(ECORE_WL2_EVENT_FOCUS_IN, _cb_focus_in, handle);
-       eina_array_push(ani_h->_ecore_event_hdls, h);
+       h = ecore_event_handler_add(ECORE_WL2_EVENT_FOCUS_IN, _cb_focus_in, ani_h);
+       eina_array_push(ani_h->ecore_event_hdls, h);
 
-       h = ecore_event_handler_add(ECORE_WL2_EVENT_FOCUS_OUT, _cb_focus_out, handle);
-       eina_array_push(ani_h->_ecore_event_hdls, h);
+       h = ecore_event_handler_add(ECORE_WL2_EVENT_FOCUS_OUT, _cb_focus_out, ani_h);
+       eina_array_push(ani_h->ecore_event_hdls, h);
 
-       h = ecore_event_handler_add(ECORE_WL2_EVENT_WINDOW_VISIBILITY_CHANGE, _cb_visibility_change, handle);
-       eina_array_push(ani_h->_ecore_event_hdls, h);
+       h = ecore_event_handler_add(ECORE_WL2_EVENT_WINDOW_VISIBILITY_CHANGE, _cb_visibility_change, ani_h);
+       eina_array_push(ani_h->ecore_event_hdls, h);
 
 }
 
@@ -325,58 +345,201 @@ _pui_ani_event_handlers_shutdown(pui_ani_h ani_h)
                return;
        }
 
-       while (eina_array_count(ani_h->_ecore_event_hdls))
-               ecore_event_handler_del(eina_array_pop(ani_h->_ecore_event_hdls));
-       eina_array_free(ani_h->_ecore_event_hdls);
+       while (eina_array_count(ani_h->ecore_event_hdls))
+               ecore_event_handler_del(eina_array_pop(ani_h->ecore_event_hdls));
+       eina_array_free(ani_h->ecore_event_hdls);
+
+       ani_h->ecore_event_hdls = NULL;
+}
+
+Eina_Bool _pui_ani_frame_cb(void *data)
+{
+       Eina_Bool ret;
+
+       pui_ani_t *ani = (pui_ani_t *)data;
+
+       if (!ani)
+       {
+               pui_err("Invalid pui ani !\n");
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       if (!ani->backend_frame_cb)
+       {
+               pui_err("Invalid backend frame_cb !\n");
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       pui_info("frame_cb !\n");
+
+       ret = (Eina_Bool)ani->backend_frame_cb(ani, ++ani->serial);
+
+       if (!ani->frame_cb_timer)
+               return ECORE_CALLBACK_CANCEL;
+
+       pui_ani_status_update(ani, PUI_ANI_STATUS_RUNNING);
+
+       return ret;
+}
+
+void
+pui_ani_add_frame_cb(pui_ani_t *ani, pui_bool (*frame_cb)(void *data, int serial), double frame_interval)
+{
+       Ecore_Timer *timer = NULL;
+
+       if (!ani)
+       {
+               pui_err("Invalid put ani !\n");
+               return;
+       }
+
+       ani->frame_cb = _pui_ani_frame_cb;
+       ani->backend_frame_cb = frame_cb;
+       ani->frame_interval = frame_interval;
+       ani->frame_cb_data = ani;
+       ani->serial = 0;
+
+       timer = ecore_timer_add(frame_interval, (Ecore_Task_Cb)ani->frame_cb, ani->frame_cb_data);
+
+       if (ani->frame_cb_timer)
+       {
+               ecore_timer_del(ani->frame_cb_timer);
+               ani->frame_cb_timer = NULL;
+       }
+
+       ani->frame_cb_timer = timer;
 
-       ani_h->_ecore_event_hdls = NULL;
+       /* call frame_cb for starting the first frame */
+       _pui_ani_frame_cb(ani);
+}
+
+void
+pui_ani_remove_frame_cb(pui_ani_t *ani)
+{
+       if (!ani)
+               return;
+
+       if (ani->frame_cb_timer)
+       {
+               ecore_timer_del(ani->frame_cb_timer);
+               ani->frame_cb_timer = NULL;
+       }
+
+       ani->frame_cb = NULL;
+       ani->backend_frame_cb = NULL;
+       ani->frame_interval = 0;
+       ani->frame_cb_data = NULL;
 }
 
 pui_id
 pui_ani_get_id(pui_ani_h ani_h)
 {
-       if (ani_h && ani_h->id)
-               return ani_h->id;
+       if (!ani_h || !ani_h->ani)
+               return NULL;
+
+       return ani_h->ani->id;
 }
 
 pui_ani_cmd
 pui_ani_get_cmd(pui_ani_h ani_h)
 {
-       if (ani_h && ani_h->cmd)
-               return ani_h->cmd;
+       if (!ani_h || !ani_h->ani)
+               return PUI_ANI_CMD_NONE;
+
+       return ani_h->ani->cmd;
+}
+
+int
+pui_ani_get_repeat(pui_ani_h ani_h)
+{
+       if (!ani_h || !ani_h->ani)
+               return 0;
+
+       return ani_h->ani->repeat;
+}
+
+pui_backend_ani_data *
+pui_ani_get_ani_data(pui_ani_t *ani)
+{
+       if (!ani)
+               return NULL;
+
+       return ani->ani_data;
+}
+
+void
+pui_ani_status_update(pui_ani_t *ani, pui_ani_status status)
+{
+       //TODO : add PUI EVENT
+
+       ani->status = status;
 }
 
-pui_ani_opt
-pui_ani_get_opt(pui_ani_h ani_h)
+pui_ani_status
+pui_ani_status_get(pui_ani_t *ani)
 {
-       if (ani_h && ani_h->opt)
-               return ani_h->opt;
+       pui_ani_status status = PUI_ANI_STATUS_UNKNOWN;
+
+       if (!ani)
+       {
+               pui_err("Invalid pui ani !\n");
+               return status;
+       }
+
+       return ani->status;
 }
 
 pui_error
-pui_ani_control(pui_ani_h ani_h, pui_ani_cmd cmd, pui_ani_opt opt)
+pui_ani_control(pui_ani_h ani_h, pui_ani_cmd cmd, int repeat)
 {
+       pui_ani_t *ani = NULL;
+       pui_h handle = NULL;
+       pui_backend_ani_func *ani_func = NULL;
+
        if (!ani_h)
                return PUI_ERROR_INVALID_ANI_HANDLE;
 
        if (cmd < PUI_ANI_CMD_START || cmd >= PUI_ANI_CMD_LAST)
+       {
+               pui_err("Invalid cmd ! (repeat=%d)\n", repeat);
                return PUI_ERROR_INVALID_ANI_CMD;
+       }
 
-       if (opt < PUI_ANI_OPT_NONE || opt >= PUI_ANI_OPT_LAST)
-               return PUI_ERROR_INVALID_ANI_OPT;
+       if (repeat < -1)
+       {
+               pui_err("Invalid repeat count ! (repeat=%d)\n", repeat);
+               return PUI_ERROR_INVALID_ANI_REPEAT;
+       }
+
+       //TODO : check if there is any animation being played already
+       //NOTE : the id of the animation being played can be different from ani_h->id
+
+       handle = ani_h->pui_handle;
+       ani = ani_h->ani;
 
-       if (ani_h->id && ani_h->status == PUI_ANI_STATUS_RUNNING)
+       if (!ani || !ani->ani_data)
        {
-               
+               pui_err("Invalid ani or ani_data !\n");
+               return PUI_ERROR_INTERNAL;
+       }
+
+       ani_func = ani->ani_data->ani_func;
+
+       if (cmd == PUI_ANI_CMD_START)
+       {
+               ani_func->ani_start(ani, repeat);
+       }
+       else//cmd == PUI_ANI_CMD_STOP
+       {
+               ani_func->ani_stop(ani);
        }
 
-       ani_h->cmd = cmd;
-       ani_h->opt = opt;
-       ani_h->status = PUI_ANI_STATUS_STOPPED;
+       ani->cmd = cmd;
+       ani->repeat = repeat;
 
-       ani_h->frame_cb = ecore_wl2_window_frame_callback_add(win, _pui_cb_frame, handle);
+       ani_h->frame_done_cb = ecore_wl2_window_frame_callback_add(handle->win, _pui_ani_cb_frame_done, handle);
 
-       if (!ani_h->frame_cb)
+       if (!ani_h->frame_done_cb)
        {
                pui_err("Failed to add frame callback !");
                goto err;
@@ -394,20 +557,20 @@ pui_ani_h
 pui_ani_create(pui_h handle, pui_id id)
 {
        pui_ani_h ani_h = NULL;
-       pui_ani_mgr *ani_mgr = NULL;
-       pui_backend_ani_func *ani_func = NULL;
+       pui_ani_t *ani = NULL;
+       pui_backend_ani_data *ani_data = NULL;
 
-       if (!handle || !handle->backend_module_data))
+       if (!handle || !handle->backend_module_data)
        {
                pui_err("Invalid pui handle or backend module data !\n");
                return NULL;
        }
 
-       ani_func = handle->backend_module_data->get_ani_func(id);
+       ani_data = handle->backend_module_data->ani_create(id);
 
-       if (!ani_func)
+       if (!ani_data)
        {
-               pui_err("Invalid ani func from backend module data !\n");
+               pui_err("Invalid ani data from backend module data !\n");
                return NULL;
        }
 
@@ -416,49 +579,80 @@ pui_ani_create(pui_h handle, pui_id id)
        if (!ani_h)
        {
                pui_err("Failed to allocate memory for pui ani handle !\n");
-               return NULL;
+               goto err;
        }
 
        ani_h->ecore_event_hdls = NULL;
        _pui_ani_event_handlers_init(ani_h);
 
-       ani_mgr = (pui_ani_mgr *)calloc(1, sizeof(pui_ani_mgr));
+       ani = (pui_ani_t *)calloc(1, sizeof(pui_ani_t));
 
-       if (!ani_mgr)
+       if (!ani)
        {
                pui_err("Failed to allocate memory for pui ani mgr !\n");
                goto err;
        }
 
-       ani_mgr->ani_h = ani_h;
-       ani_mgr->id = id;
-       ani_mgr->status = PUI_ANI_STATUS_INITIAL;
-       ani_mgr->ani_func = ani_func;
+       ani->ani_h = ani_h;
+       ani->id = id;
+       ani->status = PUI_ANI_STATUS_INITIAL;
+       ani->ani_data = ani_data;
+
+       handle->ani_handles = eina_list_append(handle->ani_handles, ani_h);
 
        return ani_h;
 
 err:
+       if (ani_data)
+       {
+               handle->backend_module_data->ani_destroy(ani_data);
+       }
+
        if (ani_h)
                free(ani_h);
+
        return NULL;
 }
 
 void
 pui_ani_destroy(pui_ani_h ani_h)
 {
-       if (!ani_h)
+       pui_h handle = NULL;
+       pui_ani_t *ani = NULL;
+       pui_backend_module_data *backend_module_data = NULL;
+
+       if (!ani_h || !ani_h->pui_handle)
                return;
 
-       if (ani_h->ani_mgr->status != PUI_ANI_STATUS_STOPPED)
-               pui_ani_control(ani_h, PUI_ANI_CMD_STOP, PUI_ANI_OPT_NONE);
-       free(ani_h->ani_mgr);
+       handle = ani_h->pui_handle;
+       ani = ani_h->ani;
+
+       /* stop the animation being played already if any */
+       if (ani->status != PUI_ANI_STATUS_STOPPED)
+               pui_ani_control(ani_h, PUI_ANI_CMD_STOP, 0);
+
+       backend_module_data = handle->backend_module_data;
+       backend_module_data->ani_destroy(ani->ani_data);
+       ani->ani_data = NULL;
+
+       if (ani->frame_cb_timer)
+       {
+               ecore_timer_del(ani->frame_cb_timer);
+               ani->frame_cb_timer = NULL;
+       }
+
+       free(ani_h->ani);
 
        _pui_ani_event_handlers_shutdown(ani_h);
        
-       if (ani_h->frame_cb)
+       if (ani_h->frame_done_cb)
        {
-               ecore_wl2_window_frame_callback_del(ani_h->frame_cb);
-               ani_h->frame_cb = NULL;
+               ecore_wl2_window_frame_callback_del(ani_h->frame_done_cb);
+               ani_h->frame_done_cb = NULL;
        }
+
+       handle->ani_handles = eina_list_remove(handle->ani_handles, ani_h);
+
+       free(ani_h);
 }