2 * Copyright © 2019 Samsung Electronics co., Ltd. All Rights Reserved.
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:
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.
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
26 #include "PUI_internal.h"
27 #include "PUI_backend.h"
32 _pui_ani_error_to_int_error(pui_error e)
39 ei = PUI_INT_ERROR_NONE;
42 case PUI_ERROR_INVALID_HANDLE:
43 ei = PUI_INT_ERROR_INVALID_HANDLE;
46 case PUI_ERROR_INVALID_SURFACE:
47 ei = PUI_INT_ERROR_INVALID_SURFACE;
50 case PUI_ERROR_INVALID_BUFFER:
51 ei = PUI_INT_ERROR_INVALID_BUFFER;
55 ei = PUI_INT_ERROR_UNKNOWN;
63 _pui_ani_cb_frame_done(Ecore_Wl2_Window *win, uint32_t timestamp EINA_UNUSED, void *data)
65 pui_h handle = (pui_h) data;
67 pui_info("Frame done ! (window=0x%x)\n", ecore_wl2_window_id_get(win));
69 /* TODO : make use of handle */
75 pui_ani_control_buffer *
76 pui_ani_get_last_buffer(pui_ani_h ani_h)
79 pui_ani_control_buffer *buffer = NULL;
81 if (!PUI_MAGIC_CHECK(ani_h, PUI_MAGIC_ANI_H))
83 PUI_MAGIC_FAIL(ani_h, PUI_MAGIC_ANI_H, __FUNCTION__);
87 handle = ani_h->pui_handle;
88 buffer = pui_display_get_last_buffer(handle);
93 pui_ani_control_buffer *
94 pui_ani_get_buffer(pui_ani_h ani_h)
97 pui_ani_control_buffer *buffer = NULL;
99 if (!PUI_MAGIC_CHECK(ani_h, PUI_MAGIC_ANI_H))
101 PUI_MAGIC_FAIL(ani_h, PUI_MAGIC_ANI_H, __FUNCTION__);
105 handle = ani_h->pui_handle;
106 buffer = pui_display_get_buffer(handle);
112 pui_ani_set_buffer(pui_ani_h ani_h, pui_ani_control_buffer *buffer)
114 pui_error e = PUI_ERROR_NONE;
117 if (!PUI_MAGIC_CHECK(ani_h, PUI_MAGIC_ANI_H))
119 PUI_MAGIC_FAIL(ani_h, PUI_MAGIC_ANI_H, __FUNCTION__);
120 return PUI_ERROR_INVALID_HANDLE;
123 handle = ani_h->pui_handle;
125 if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
127 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
128 return PUI_ERROR_INVALID_HANDLE;
131 e = pui_display_set_buffer(handle, buffer);
133 return _pui_ani_error_to_int_error(e);
137 pui_ani_update(pui_ani_h ani_h)
139 pui_error e = PUI_ERROR_NONE;
144 pui_err("Invalid ani handle !\n");
145 return PUI_INT_ERROR_INVALID_HANDLE;
148 handle = ani_h->pui_handle;
149 e = pui_display_update(handle);
151 return _pui_ani_error_to_int_error(e);
155 _pui_ani_frame_cb(void *data)
158 pui_ani_t *ani = (pui_ani_t *)data;
160 if (!PUI_MAGIC_CHECK(ani, PUI_MAGIC_ANI_T))
162 PUI_MAGIC_FAIL(ani, PUI_MAGIC_ANI_T, __FUNCTION__);
163 return ECORE_CALLBACK_CANCEL;
166 if (!ani->backend_frame_cb)
168 pui_err("Invalid backend frame_cb !\n");
169 return ECORE_CALLBACK_CANCEL;
172 if (!ani->frame_cb_timer)
174 pui_err("Invalid frame_cb timer !\n");
175 return ECORE_CALLBACK_CANCEL;
180 ret = (Eina_Bool)ani->backend_frame_cb(ani, ++ani->serial);
184 pui_err("Failed on backend's frame_cb !Frame_cb will be removed forcefuly !\n");
186 pui_ani_remove_frame_cb(ani);
191 if (ret && PUI_ANI_STATUS_STARTED == ani->status)
192 pui_ani_status_update(ani, PUI_ANI_STATUS_RUNNING);
194 return ECORE_CALLBACK_RENEW;
198 pui_ani_add_frame_cb(pui_ani_t *ani, pui_bool (*frame_cb)(void *data, int serial), double frame_interval)
200 Ecore_Timer *timer = NULL;
202 if (!PUI_MAGIC_CHECK(ani, PUI_MAGIC_ANI_T))
204 PUI_MAGIC_FAIL(ani, PUI_MAGIC_ANI_T, __FUNCTION__);
208 if (frame_interval <= 0.0f)
210 pui_err("Invalid frame interval (%.2f) ! frame interval must be larger than 0.\n", frame_interval);
214 ani->frame_cb = _pui_ani_frame_cb;
215 ani->backend_frame_cb = frame_cb;
216 ani->frame_interval = frame_interval;
217 ani->frame_cb_data = ani;
220 timer = ecore_timer_add(frame_interval, _pui_ani_frame_cb, ani);
224 pui_err("Failed to add ecore timer !\n");
228 ani->frame_cb_timer = timer;
230 pui_info("[Frame callback added][ani id=%s] frame_interval=%.2f\n", ani->id, frame_interval);
232 /* call frame_cb for starting the first frame */
233 _pui_ani_frame_cb(ani);
239 pui_ani_remove_frame_cb(pui_ani_t *ani)
241 if (!PUI_MAGIC_CHECK(ani, PUI_MAGIC_ANI_T))
243 PUI_MAGIC_FAIL(ani, PUI_MAGIC_ANI_T, __FUNCTION__);
247 if (ani->frame_cb_timer)
249 ecore_timer_del(ani->frame_cb_timer);
250 ani->frame_cb_timer = NULL;
253 ani->frame_cb = NULL;
254 ani->backend_frame_cb = NULL;
255 ani->frame_interval = 0;
256 ani->frame_cb_data = NULL;
258 pui_info("[Frame callback removed][ani id=%s]\n", ani->id);
262 pui_ani_get_id(pui_ani_h ani_h)
264 if (!PUI_MAGIC_CHECK(ani_h, PUI_MAGIC_ANI_H))
266 PUI_MAGIC_FAIL(ani_h, PUI_MAGIC_ANI_H, __FUNCTION__);
270 if (!PUI_MAGIC_CHECK(ani_h->ani, PUI_MAGIC_ANI_T))
272 PUI_MAGIC_FAIL(ani_h->ani, PUI_MAGIC_ANI_T, __FUNCTION__);
276 return ani_h->ani->id;
280 pui_ani_get_cmd(pui_ani_h ani_h)
282 if (!PUI_MAGIC_CHECK(ani_h, PUI_MAGIC_ANI_H))
284 PUI_MAGIC_FAIL(ani_h, PUI_MAGIC_ANI_H, __FUNCTION__);
285 return PUI_ANI_CMD_NONE;
288 if (!PUI_MAGIC_CHECK(ani_h->ani, PUI_MAGIC_ANI_T))
290 PUI_MAGIC_FAIL(ani_h->ani, PUI_MAGIC_ANI_T, __FUNCTION__);
291 return PUI_ANI_CMD_NONE;
294 return ani_h->ani->cmd;
298 pui_ani_get_repeat(pui_ani_h ani_h)
300 if (!PUI_MAGIC_CHECK(ani_h, PUI_MAGIC_ANI_H))
302 PUI_MAGIC_FAIL(ani_h, PUI_MAGIC_ANI_H, __FUNCTION__);
306 if (!PUI_MAGIC_CHECK(ani_h->ani, PUI_MAGIC_ANI_T))
308 PUI_MAGIC_FAIL(ani_h->ani, PUI_MAGIC_ANI_T, __FUNCTION__);
312 return ani_h->ani->repeat;
315 pui_backend_ani_data *
316 pui_ani_get_ani_data(pui_ani_t *ani)
318 if (!PUI_MAGIC_CHECK(ani, PUI_MAGIC_ANI_T))
320 PUI_MAGIC_FAIL(ani, PUI_MAGIC_ANI_T, __FUNCTION__);
324 return ani->ani_data;
328 pui_ani_status_update(pui_ani_t *ani, pui_ani_status status)
332 PUI_Event_Animation_Status *e = NULL;
334 if (!PUI_MAGIC_CHECK(ani, PUI_MAGIC_ANI_T))
336 PUI_MAGIC_FAIL(ani, PUI_MAGIC_ANI_T, __FUNCTION__);
340 if (ani->status == status)
345 ani->status = status;
349 case PUI_ANI_STATUS_STARTED:
350 ev_type = PUI_EVENT_ANI_STARTED;
351 pui_info("[Status update][ani id:%s] PUI_EVENT_ANI_STARTED event has been added.\n", ani->id);
354 case PUI_ANI_STATUS_RUNNING:
355 pui_info("[Status update][ani id:%s] PUI_ANI_STATUS_RUNNING !\n", ani->id);
358 case PUI_ANI_STATUS_PAUSED:
359 ev_type = PUI_EVENT_ANI_PAUSED;
360 pui_info("[Status update][ani id:%s] PUI_EVENT_ANI_PAUSED event has been added.\n", ani->id);
363 case PUI_ANI_STATUS_STOPPED:
364 ev_type = PUI_EVENT_ANI_STOPPED;
365 pui_info("[Status update][ani id:%s] PUI_EVENT_ANI_STOPPED event has been added.\n", ani->id);
369 pui_err("Unknown status !(ani status=%d, id=%s) !\n", status, ani->id);
375 e = (PUI_Event_Animation_Status *)calloc(1, sizeof(PUI_Event_Animation_Status));
379 pui_err("Failed to allocate memory for PUI Event !\n");
384 e->win = ecore_wl2_window_id_get(ani_h->pui_handle->win);
387 ecore_event_add(ev_type, e, NULL, ani_h);
392 pui_ani_status_get(pui_ani_t *ani)
394 pui_ani_status status = PUI_ANI_STATUS_UNKNOWN;
396 if (!PUI_MAGIC_CHECK(ani, PUI_MAGIC_ANI_T))
398 PUI_MAGIC_FAIL(ani, PUI_MAGIC_ANI_T, __FUNCTION__);
406 _pui_ani_control_with_force(pui_ani_h ani_h, pui_ani_cmd cmd, int repeat, pui_bool force)
408 pui_int_error ei = PUI_INT_ERROR_NONE;
409 pui_ani_t *ani = NULL;
411 pui_backend_ani_func *ani_func = NULL;
413 if (!PUI_MAGIC_CHECK(ani_h, PUI_MAGIC_ANI_H))
415 PUI_MAGIC_FAIL(ani_h, PUI_MAGIC_ANI_H, __FUNCTION__);
416 return PUI_ERROR_INVALID_ANI_HANDLE;
419 if (cmd < PUI_ANI_CMD_START || cmd >= PUI_ANI_CMD_LAST)
421 pui_err("Invalid cmd ! (cmd=%d)\n", cmd);
422 return PUI_ERROR_INVALID_ANI_CMD;
427 pui_err("Invalid repeat count ! (repeat=%d)\n", repeat);
428 return PUI_ERROR_INVALID_ANI_REPEAT;
431 handle = ani_h->pui_handle;
434 if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
436 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
437 return PUI_ERROR_INTERNAL;
440 if (!PUI_MAGIC_CHECK(ani, PUI_MAGIC_ANI_T))
442 PUI_MAGIC_FAIL(ani, PUI_MAGIC_ANI_T, __FUNCTION__);
443 return PUI_ERROR_INTERNAL;
448 pui_err("Invalid ani_data !\n");
449 return PUI_ERROR_INTERNAL;
452 ani_func = ani->ani_data->ani_func;
455 ani->repeat = repeat;
457 if (cmd == PUI_ANI_CMD_START)
459 if (handle->manual_render)
461 pui_err("Manual render has been set ! Unable to start animation.\n");
462 return PUI_ERROR_MANUAL_RENDER_ENABLED;
465 if (handle->current_ani_h && handle->current_ani_h != ani_h)
467 pui_ani_t *current_ani = handle->current_ani_h->ani;
469 if (current_ani->status >= PUI_ANI_STATUS_STARTED &&
470 current_ani->status <= PUI_ANI_STATUS_RUNNING)
472 pui_info("current_ani id=%s, status=%d\n", current_ani->id, current_ani->status);
474 ei = _pui_ani_control_with_force(handle->current_ani_h, PUI_ANI_CMD_STOP, 0, force);
476 if (ei != PUI_INT_ERROR_NONE)
477 pui_info("Failed to stop running previous animation ! (id:%s)\n", current_ani->id);
480 handle->current_ani_h = NULL;
484 ei = ani_func->ani_start(ani, repeat);
486 if (ei != PUI_INT_ERROR_NONE)
488 pui_err("Error on starting animation ! (id:%s, repeat:%d, status=%d))\n", ani->id, repeat, ani->status);
490 if (ani->status != PUI_ANI_STATUS_RUNNING)
491 _pui_ani_control_with_force(ani_h, PUI_ANI_CMD_STOP, 0, 0);
493 return PUI_ERROR_INTERNAL;
496 handle->current_ani_h = ani_h;
498 else//cmd == PUI_ANI_CMD_STOP
500 ei = ani_func->ani_stop(ani, force);
502 if (ani->frame_cb_timer)
503 pui_ani_remove_frame_cb(ani);
505 if (ei != PUI_INT_ERROR_NONE)
507 pui_err("Failied on stopping animation ! (id:%s)\n", ani->id);
509 if (ani->status != PUI_ANI_STATUS_STOPPED)
510 pui_ani_status_update(ani, PUI_ANI_STATUS_STOPPED);
512 return PUI_ERROR_INTERNAL;
515 return PUI_ERROR_NONE;
518 ani_h->frame_done_cb = ecore_wl2_window_frame_callback_add(handle->win, _pui_ani_cb_frame_done, handle);
520 if (!ani_h->frame_done_cb)
522 pui_err("Failed to add frame callback !");
526 return PUI_ERROR_NONE;
529 return PUI_ERROR_INTERNAL;
533 pui_ani_control(pui_ani_h ani_h, pui_ani_cmd cmd, int repeat)
535 return _pui_ani_control_with_force(ani_h, cmd, repeat, 0);
539 _cb_visibility_change(void *data, int type EINA_UNUSED, void *event)
541 pui_ani_h ani_h = (pui_ani_h)data;
542 pui_ani_t *ani = ani_h->ani;
543 pui_h ph = ani_h->pui_handle;
545 Ecore_Wl2_Event_Window_Visibility_Change *ev;
546 PUI_Event_Animation_Status *e = NULL;
548 if (!PUI_MAGIC_CHECK(ani_h, PUI_MAGIC_ANI_H))
550 PUI_MAGIC_FAIL(ani_h, PUI_MAGIC_ANI_H, __FUNCTION__);
551 return ECORE_CALLBACK_PASS_ON;
554 if (!PUI_MAGIC_CHECK(ani, PUI_MAGIC_ANI_T))
556 PUI_MAGIC_FAIL(ani, PUI_MAGIC_ANI_T, __FUNCTION__);
557 return ECORE_CALLBACK_PASS_ON;
560 if (!PUI_MAGIC_CHECK(ph, PUI_MAGIC_PUI_H))
562 PUI_MAGIC_FAIL(ph, PUI_MAGIC_PUI_H, __FUNCTION__);
563 return ECORE_CALLBACK_PASS_ON;
568 /* check if this is needed */
569 ph->visibility = !(ev->fully_obscured);
571 if (ev->fully_obscured)
573 if (ani->status == PUI_ANI_STATUS_RUNNING)
575 pui_info("animation(%s) will be stopped as it lost its priority !\n", ani->id);
577 _pui_ani_control_with_force(ani_h, PUI_ANI_CMD_STOP, 0, 1);
582 if (ani->status == PUI_ANI_STATUS_PAUSED)
584 e = (PUI_Event_Animation_Status *)calloc(1, sizeof(PUI_Event_Animation_Status));
588 pui_err("Failed to allocate memory for PUI Event !\n");
589 return ECORE_CALLBACK_PASS_ON;
594 e->status = ani->status;
596 if(ani->status == PUI_ANI_STATUS_PAUSED)
598 pui_info("[Event added][ani id:%s] PUI_EVENT_ANI_READY_TO_RESUME event has been added.\n", ani_h->id);
599 ecore_event_add(PUI_EVENT_ANI_READY_TO_RESUME, e, NULL, ani_h);
604 return ECORE_CALLBACK_PASS_ON;
608 _pui_ani_event_handlers_init(pui_ani_h ani_h)
610 Ecore_Event_Handler *h = NULL;
612 if (!PUI_MAGIC_CHECK(ani_h, PUI_MAGIC_ANI_H))
614 PUI_MAGIC_FAIL(ani_h, PUI_MAGIC_ANI_H, __FUNCTION__);
618 if (!ani_h->ecore_event_hdls)
619 ani_h->ecore_event_hdls = eina_array_new(1);
621 h = ecore_event_handler_add(ECORE_WL2_EVENT_WINDOW_VISIBILITY_CHANGE, _cb_visibility_change, ani_h);
622 eina_array_push(ani_h->ecore_event_hdls, h);
627 _pui_ani_event_handlers_shutdown(pui_ani_h ani_h)
629 if (!PUI_MAGIC_CHECK(ani_h, PUI_MAGIC_ANI_H))
631 PUI_MAGIC_FAIL(ani_h, PUI_MAGIC_ANI_H, __FUNCTION__);
635 if (ani_h->ecore_event_hdls)
637 while (eina_array_count(ani_h->ecore_event_hdls))
638 ecore_event_handler_del(eina_array_pop(ani_h->ecore_event_hdls));
640 eina_array_free(ani_h->ecore_event_hdls);
641 ani_h->ecore_event_hdls = NULL;
646 pui_ani_create(pui_h handle, pui_id id)
648 pui_ani_h ani_h = NULL;
649 pui_ani_t *ani = NULL;
650 pui_backend_ani_data *ani_data = NULL;
652 if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
654 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
658 if (!handle->backend_module_data)
660 pui_err("Invalid backend module data !\n");
664 ani_data = handle->backend_module_data->ani_create(id);
668 pui_err("Invalid ani data from backend module data !\n");
672 ani_h = (pui_ani_h)calloc(1, sizeof(pui_ani));
676 pui_err("Failed to allocate memory for pui ani handle !\n");
680 ani_h->id = strdup(id);
681 ani_h->pui_handle = handle;
682 ani_h->ecore_event_hdls = NULL;
684 PUI_MAGIC_SET(ani_h, PUI_MAGIC_ANI_H);
686 _pui_ani_event_handlers_init(ani_h);
688 ani = (pui_ani_t *)calloc(1, sizeof(pui_ani_t));
692 pui_err("Failed to allocate memory for pui ani mgr !\n");
698 ani->cmd = PUI_ANI_CMD_NONE;
700 ani->status = PUI_ANI_STATUS_INITIAL;
701 ani->ani_data = ani_data;
705 handle->ani_handles = eina_list_append(handle->ani_handles, ani_h);
707 PUI_MAGIC_SET(ani, PUI_MAGIC_ANI_T);
714 handle->backend_module_data->ani_destroy(ani_data);
719 PUI_MAGIC_SET(ani_h, PUI_MAGIC_NONE);
727 pui_ani_destroy(pui_ani_h ani_h)
730 pui_ani_t *ani = NULL;
731 pui_backend_module_data *backend_module_data = NULL;
733 if (!PUI_MAGIC_CHECK(ani_h, PUI_MAGIC_ANI_H))
735 PUI_MAGIC_FAIL(ani_h, PUI_MAGIC_ANI_H, __FUNCTION__);
739 handle = ani_h->pui_handle;
742 if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
744 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
748 if (!PUI_MAGIC_CHECK(ani, PUI_MAGIC_ANI_T))
750 PUI_MAGIC_FAIL(ani, PUI_MAGIC_ANI_T, __FUNCTION__);
754 /* stop the animation being played already if any */
755 if (ani->status == PUI_ANI_STATUS_STARTED || ani->status == PUI_ANI_STATUS_RUNNING)
756 pui_ani_control(ani_h, PUI_ANI_CMD_STOP, 0);
758 backend_module_data = handle->backend_module_data;
759 backend_module_data->ani_destroy(ani->ani_data);
760 ani->ani_data = NULL;
762 if (ani->frame_cb_timer)
764 ecore_timer_del(ani->frame_cb_timer);
765 ani->frame_cb_timer = NULL;
768 PUI_MAGIC_SET(ani_h->ani, PUI_MAGIC_NONE);
771 _pui_ani_event_handlers_shutdown(ani_h);
773 if (ani_h->frame_done_cb)
775 ecore_wl2_window_frame_callback_del(ani_h->frame_done_cb);
776 ani_h->frame_done_cb = NULL;
779 handle->ani_handles = eina_list_remove(handle->ani_handles, ani_h);
781 PUI_MAGIC_SET(ani_h, PUI_MAGIC_NONE);