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_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_buffer(handle);
94 pui_ani_set_buffer(pui_ani_h ani_h, pui_ani_control_buffer *buffer)
96 pui_error e = PUI_ERROR_NONE;
99 if (!PUI_MAGIC_CHECK(ani_h, PUI_MAGIC_ANI_H))
101 PUI_MAGIC_FAIL(ani_h, PUI_MAGIC_ANI_H, __FUNCTION__);
102 return PUI_ERROR_INVALID_HANDLE;
105 handle = ani_h->pui_handle;
107 if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
109 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
110 return PUI_ERROR_INVALID_HANDLE;
113 e = pui_display_set_buffer(handle, buffer);
115 return _pui_ani_error_to_int_error(e);
119 pui_ani_update(pui_ani_h ani_h)
121 pui_error e = PUI_ERROR_NONE;
126 pui_err("Invalid ani handle !\n");
127 return PUI_INT_ERROR_INVALID_HANDLE;
130 handle = ani_h->pui_handle;
131 e = pui_display_update(handle);
133 return _pui_ani_error_to_int_error(e);
137 _pui_ani_frame_cb(void *data)
140 Ecore_Timer *timer = NULL;
142 pui_ani_t *ani = (pui_ani_t *)data;
144 if (!PUI_MAGIC_CHECK(ani, PUI_MAGIC_ANI_T))
146 PUI_MAGIC_FAIL(ani, PUI_MAGIC_ANI_T, __FUNCTION__);
147 return ECORE_CALLBACK_CANCEL;
150 if (!ani->backend_frame_cb)
152 pui_err("Invalid backend frame_cb !\n");
153 return ECORE_CALLBACK_CANCEL;
156 if (!ani->frame_cb_timer)
158 pui_err("Invalid frame_cb timer !\n");
159 return ECORE_CALLBACK_CANCEL;
164 ret = (Eina_Bool)ani->backend_frame_cb(ani, ++ani->serial);
168 pui_err("Failed on backend's frame_cb !Frame_cb will be removed forcefuly !\n");
170 pui_ani_remove_frame_cb(ani);
175 if (ret && PUI_ANI_STATUS_STARTED == ani->status)
176 pui_ani_status_update(ani, PUI_ANI_STATUS_RUNNING);
178 ecore_timer_del(ani->frame_cb_timer);
179 ani->frame_cb_timer = NULL;
181 timer = ecore_timer_add(ani->frame_interval, _pui_ani_frame_cb, ani);
185 pui_err("Failed to add ecore timer !\n");
189 ani->frame_cb_timer = timer;
195 pui_ani_add_frame_cb(pui_ani_t *ani, pui_bool (*frame_cb)(void *data, int serial), double frame_interval)
197 Ecore_Timer *timer = NULL;
199 if (!PUI_MAGIC_CHECK(ani, PUI_MAGIC_ANI_T))
201 PUI_MAGIC_FAIL(ani, PUI_MAGIC_ANI_T, __FUNCTION__);
205 if (frame_interval <= 0.0f)
207 pui_err("Invalid frame interval (%.2f) ! frame interval must be larger than 0.\n", frame_interval);
211 ani->frame_cb = _pui_ani_frame_cb;
212 ani->backend_frame_cb = frame_cb;
213 ani->frame_interval = frame_interval;
214 ani->frame_cb_data = ani;
217 timer = ecore_timer_add(frame_interval, _pui_ani_frame_cb, ani);
221 pui_err("Failed to add ecore timer !\n");
225 ani->frame_cb_timer = timer;
227 pui_info("[Frame callback added][ani id=%s] frame_interval=%.2f\n", ani->id, frame_interval);
229 /* call frame_cb for starting the first frame */
230 _pui_ani_frame_cb(ani);
236 pui_ani_remove_frame_cb(pui_ani_t *ani)
238 if (!PUI_MAGIC_CHECK(ani, PUI_MAGIC_ANI_T))
240 PUI_MAGIC_FAIL(ani, PUI_MAGIC_ANI_T, __FUNCTION__);
244 if (ani->frame_cb_timer)
246 ecore_timer_del(ani->frame_cb_timer);
247 ani->frame_cb_timer = NULL;
250 ani->frame_cb = NULL;
251 ani->backend_frame_cb = NULL;
252 ani->frame_interval = 0;
253 ani->frame_cb_data = NULL;
255 pui_info("[Frame callback removed][ani id=%s]\n", ani->id);
259 pui_ani_get_id(pui_ani_h ani_h)
261 if (!PUI_MAGIC_CHECK(ani_h, PUI_MAGIC_ANI_H))
263 PUI_MAGIC_FAIL(ani_h, PUI_MAGIC_ANI_H, __FUNCTION__);
267 if (!PUI_MAGIC_CHECK(ani_h->ani, PUI_MAGIC_ANI_T))
269 PUI_MAGIC_FAIL(ani_h->ani, PUI_MAGIC_ANI_T, __FUNCTION__);
273 return ani_h->ani->id;
277 pui_ani_get_cmd(pui_ani_h ani_h)
279 if (!PUI_MAGIC_CHECK(ani_h, PUI_MAGIC_ANI_H))
281 PUI_MAGIC_FAIL(ani_h, PUI_MAGIC_ANI_H, __FUNCTION__);
282 return PUI_ANI_CMD_NONE;
285 if (!PUI_MAGIC_CHECK(ani_h->ani, PUI_MAGIC_ANI_T))
287 PUI_MAGIC_FAIL(ani_h->ani, PUI_MAGIC_ANI_T, __FUNCTION__);
288 return PUI_ANI_CMD_NONE;
291 return ani_h->ani->cmd;
295 pui_ani_get_repeat(pui_ani_h ani_h)
297 if (!PUI_MAGIC_CHECK(ani_h, PUI_MAGIC_ANI_H))
299 PUI_MAGIC_FAIL(ani_h, PUI_MAGIC_ANI_H, __FUNCTION__);
303 if (!PUI_MAGIC_CHECK(ani_h->ani, PUI_MAGIC_ANI_T))
305 PUI_MAGIC_FAIL(ani_h->ani, PUI_MAGIC_ANI_T, __FUNCTION__);
309 return ani_h->ani->repeat;
312 pui_backend_ani_data *
313 pui_ani_get_ani_data(pui_ani_t *ani)
315 if (!PUI_MAGIC_CHECK(ani, PUI_MAGIC_ANI_T))
317 PUI_MAGIC_FAIL(ani, PUI_MAGIC_ANI_T, __FUNCTION__);
321 return ani->ani_data;
325 pui_ani_status_update(pui_ani_t *ani, pui_ani_status status)
329 PUI_Event_Animation_Status *e = NULL;
331 if (!PUI_MAGIC_CHECK(ani, PUI_MAGIC_ANI_T))
333 PUI_MAGIC_FAIL(ani, PUI_MAGIC_ANI_T, __FUNCTION__);
337 if (ani->status == status)
342 ani->status = status;
346 case PUI_ANI_STATUS_STARTED:
347 ev_type = PUI_EVENT_ANI_STARTED;
348 pui_info("[Status update][ani id:%s] PUI_EVENT_ANI_STARTED event has been added.\n", ani->id);
351 case PUI_ANI_STATUS_RUNNING:
352 pui_info("[Status update][ani id:%s] PUI_ANI_STATUS_RUNNING !\n", ani->id);
355 case PUI_ANI_STATUS_PAUSED:
356 ev_type = PUI_EVENT_ANI_PAUSED;
357 pui_info("[Status update][ani id:%s] PUI_EVENT_ANI_PAUSED event has been added.\n", ani->id);
360 case PUI_ANI_STATUS_STOPPED:
361 ev_type = PUI_EVENT_ANI_STOPPED;
362 pui_info("[Status update][ani id:%s] PUI_EVENT_ANI_STOPPED event has been added.\n", ani->id);
366 pui_err("Unknown status !(ani status=%d, id=%s) !\n", status, ani->id);
372 e = (PUI_Event_Animation_Status *)calloc(1, sizeof(PUI_Event_Animation_Status));
376 pui_err("Failed to allocate memory for PUI Event !\n");
381 e->win = ecore_wl2_window_id_get(ani_h->pui_handle->win);
384 ecore_event_add(ev_type, e, NULL, ani_h);
389 pui_ani_status_get(pui_ani_t *ani)
391 pui_ani_status status = PUI_ANI_STATUS_UNKNOWN;
393 if (!PUI_MAGIC_CHECK(ani, PUI_MAGIC_ANI_T))
395 PUI_MAGIC_FAIL(ani, PUI_MAGIC_ANI_T, __FUNCTION__);
403 _pui_ani_control_with_force(pui_ani_h ani_h, pui_ani_cmd cmd, int repeat, pui_bool force)
405 pui_int_error ei = PUI_INT_ERROR_NONE;
406 pui_ani_t *ani = NULL;
408 pui_backend_ani_func *ani_func = NULL;
410 if (!PUI_MAGIC_CHECK(ani_h, PUI_MAGIC_ANI_H))
412 PUI_MAGIC_FAIL(ani_h, PUI_MAGIC_ANI_H, __FUNCTION__);
413 return PUI_ERROR_INVALID_ANI_HANDLE;
416 if (cmd < PUI_ANI_CMD_START || cmd >= PUI_ANI_CMD_LAST)
418 pui_err("Invalid cmd ! (cmd=%d)\n", cmd);
419 return PUI_ERROR_INVALID_ANI_CMD;
424 pui_err("Invalid repeat count ! (repeat=%d)\n", repeat);
425 return PUI_ERROR_INVALID_ANI_REPEAT;
428 handle = ani_h->pui_handle;
431 if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
433 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
434 return PUI_ERROR_INTERNAL;
437 if (!PUI_MAGIC_CHECK(ani, PUI_MAGIC_ANI_T))
439 PUI_MAGIC_FAIL(ani, PUI_MAGIC_ANI_T, __FUNCTION__);
440 return PUI_ERROR_INTERNAL;
445 pui_err("Invalid ani_data !\n");
446 return PUI_ERROR_INTERNAL;
449 ani_func = ani->ani_data->ani_func;
452 ani->repeat = repeat;
454 if (cmd == PUI_ANI_CMD_START)
456 if (handle->manual_render)
458 pui_err("Manual render has been set ! Unable to start animation.\n");
459 return PUI_ERROR_MANUAL_RENDER_ENABLED;
462 if (handle->current_ani_h && handle->current_ani_h != ani_h)
464 pui_ani_t *current_ani = handle->current_ani_h->ani;
466 if (current_ani->status >= PUI_ANI_STATUS_STARTED &&
467 current_ani->status <= PUI_ANI_STATUS_RUNNING)
469 pui_info("current_ani id=%s, status=%d\n", current_ani->id, current_ani->status);
471 ei = _pui_ani_control_with_force(handle->current_ani_h, PUI_ANI_CMD_STOP, 0, force);
473 if (ei != PUI_INT_ERROR_NONE)
474 pui_info("Failed to stop running previous animation ! (id:%s)\n", current_ani->id);
477 handle->current_ani_h = NULL;
481 ei = ani_func->ani_start(ani, repeat);
483 if (ei != PUI_INT_ERROR_NONE)
485 pui_err("Error on starting animation ! (id:%s, repeat:%d, status=%d))\n", ani->id, repeat, ani->status);
487 if (ani->status != PUI_ANI_STATUS_RUNNING)
488 _pui_ani_control_with_force(ani_h, PUI_ANI_CMD_STOP, 0, 0);
490 return PUI_ERROR_INTERNAL;
493 handle->current_ani_h = ani_h;
495 else//cmd == PUI_ANI_CMD_STOP
497 ei = ani_func->ani_stop(ani, force);
499 if (ani->frame_cb_timer)
500 pui_ani_remove_frame_cb(ani);
502 if (ei != PUI_INT_ERROR_NONE)
504 pui_err("Failied on stopping animation ! (id:%s)\n", ani->id);
506 if (ani->status != PUI_ANI_STATUS_STOPPED)
507 pui_ani_status_update(ani, PUI_ANI_STATUS_STOPPED);
509 return PUI_ERROR_INTERNAL;
512 return PUI_ERROR_NONE;
515 ani_h->frame_done_cb = ecore_wl2_window_frame_callback_add(handle->win, _pui_ani_cb_frame_done, handle);
517 if (!ani_h->frame_done_cb)
519 pui_err("Failed to add frame callback !");
523 return PUI_ERROR_NONE;
526 return PUI_ERROR_INTERNAL;
530 pui_ani_control(pui_ani_h ani_h, pui_ani_cmd cmd, int repeat)
532 return _pui_ani_control_with_force(ani_h, cmd, repeat, 0);
536 _cb_visibility_change(void *data, int type EINA_UNUSED, void *event)
538 pui_ani_h ani_h = (pui_ani_h)data;
539 pui_ani_t *ani = ani_h->ani;
540 pui_h ph = ani_h->pui_handle;
542 Ecore_Wl2_Event_Window_Visibility_Change *ev;
543 PUI_Event_Animation_Status *e = NULL;
545 if (!PUI_MAGIC_CHECK(ani_h, PUI_MAGIC_ANI_H))
547 PUI_MAGIC_FAIL(ani_h, PUI_MAGIC_ANI_H, __FUNCTION__);
548 return ECORE_CALLBACK_PASS_ON;
551 if (!PUI_MAGIC_CHECK(ani, PUI_MAGIC_ANI_T))
553 PUI_MAGIC_FAIL(ani, PUI_MAGIC_ANI_T, __FUNCTION__);
554 return ECORE_CALLBACK_PASS_ON;
557 if (!PUI_MAGIC_CHECK(ph, PUI_MAGIC_PUI_H))
559 PUI_MAGIC_FAIL(ph, PUI_MAGIC_PUI_H, __FUNCTION__);
560 return ECORE_CALLBACK_PASS_ON;
565 /* check if this is needed */
566 ph->visibility = !(ev->fully_obscured);
568 if (ev->fully_obscured)
570 if (ani->status == PUI_ANI_STATUS_RUNNING)
572 pui_info("animation(%s) will be stopped as it lost its priority !\n", ani->id);
574 _pui_ani_control_with_force(ani_h, PUI_ANI_CMD_STOP, 0, 1);
579 if (ani->status == PUI_ANI_STATUS_PAUSED)
581 e = (PUI_Event_Animation_Status *)calloc(1, sizeof(PUI_Event_Animation_Status));
585 pui_err("Failed to allocate memory for PUI Event !\n");
586 return ECORE_CALLBACK_PASS_ON;
591 e->status = ani->status;
593 if(ani->status == PUI_ANI_STATUS_PAUSED)
595 pui_info("[Event added][ani id:%s] PUI_EVENT_ANI_READY_TO_RESUME event has been added.\n", ani_h->id);
596 ecore_event_add(PUI_EVENT_ANI_READY_TO_RESUME, e, NULL, ani_h);
601 return ECORE_CALLBACK_PASS_ON;
605 _pui_ani_event_handlers_init(pui_ani_h ani_h)
607 Ecore_Event_Handler *h = NULL;
609 if (!PUI_MAGIC_CHECK(ani_h, PUI_MAGIC_ANI_H))
611 PUI_MAGIC_FAIL(ani_h, PUI_MAGIC_ANI_H, __FUNCTION__);
615 if (!ani_h->ecore_event_hdls)
616 ani_h->ecore_event_hdls = eina_array_new(1);
618 h = ecore_event_handler_add(ECORE_WL2_EVENT_WINDOW_VISIBILITY_CHANGE, _cb_visibility_change, ani_h);
619 eina_array_push(ani_h->ecore_event_hdls, h);
624 _pui_ani_event_handlers_shutdown(pui_ani_h ani_h)
626 if (!PUI_MAGIC_CHECK(ani_h, PUI_MAGIC_ANI_H))
628 PUI_MAGIC_FAIL(ani_h, PUI_MAGIC_ANI_H, __FUNCTION__);
632 if (ani_h->ecore_event_hdls)
634 while (eina_array_count(ani_h->ecore_event_hdls))
635 ecore_event_handler_del(eina_array_pop(ani_h->ecore_event_hdls));
637 eina_array_free(ani_h->ecore_event_hdls);
638 ani_h->ecore_event_hdls = NULL;
643 pui_ani_create(pui_h handle, pui_id id)
645 pui_ani_h ani_h = NULL;
646 pui_ani_t *ani = NULL;
647 pui_backend_ani_data *ani_data = NULL;
649 if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
651 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
655 if (!handle->backend_module_data)
657 pui_err("Invalid backend module data !\n");
661 ani_data = handle->backend_module_data->ani_create(id);
665 pui_err("Invalid ani data from backend module data !\n");
669 ani_h = (pui_ani_h)calloc(1, sizeof(pui_ani));
673 pui_err("Failed to allocate memory for pui ani handle !\n");
677 ani_h->id = strdup(id);
678 ani_h->pui_handle = handle;
679 ani_h->ecore_event_hdls = NULL;
681 PUI_MAGIC_SET(ani_h, PUI_MAGIC_ANI_H);
683 _pui_ani_event_handlers_init(ani_h);
685 ani = (pui_ani_t *)calloc(1, sizeof(pui_ani_t));
689 pui_err("Failed to allocate memory for pui ani mgr !\n");
695 ani->cmd = PUI_ANI_CMD_NONE;
697 ani->status = PUI_ANI_STATUS_INITIAL;
698 ani->ani_data = ani_data;
702 handle->ani_handles = eina_list_append(handle->ani_handles, ani_h);
704 PUI_MAGIC_SET(ani, PUI_MAGIC_ANI_T);
711 handle->backend_module_data->ani_destroy(ani_data);
716 PUI_MAGIC_SET(ani_h, PUI_MAGIC_NONE);
724 pui_ani_destroy(pui_ani_h ani_h)
727 pui_ani_t *ani = NULL;
728 pui_backend_module_data *backend_module_data = NULL;
730 if (!PUI_MAGIC_CHECK(ani_h, PUI_MAGIC_ANI_H))
732 PUI_MAGIC_FAIL(ani_h, PUI_MAGIC_ANI_H, __FUNCTION__);
736 handle = ani_h->pui_handle;
739 if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
741 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
745 if (!PUI_MAGIC_CHECK(ani, PUI_MAGIC_ANI_T))
747 PUI_MAGIC_FAIL(ani, PUI_MAGIC_ANI_T, __FUNCTION__);
751 /* stop the animation being played already if any */
752 if (ani->status == PUI_ANI_STATUS_STARTED || ani->status == PUI_ANI_STATUS_RUNNING)
753 pui_ani_control(ani_h, PUI_ANI_CMD_STOP, 0);
755 backend_module_data = handle->backend_module_data;
756 backend_module_data->ani_destroy(ani->ani_data);
757 ani->ani_data = NULL;
759 if (ani->frame_cb_timer)
761 ecore_timer_del(ani->frame_cb_timer);
762 ani->frame_cb_timer = NULL;
765 PUI_MAGIC_SET(ani_h->ani, PUI_MAGIC_NONE);
768 _pui_ani_event_handlers_shutdown(ani_h);
770 if (ani_h->frame_done_cb)
772 ecore_wl2_window_frame_callback_del(ani_h->frame_done_cb);
773 ani_h->frame_done_cb = NULL;
776 handle->ani_handles = eina_list_remove(handle->ani_handles, ani_h);
778 PUI_MAGIC_SET(ani_h, PUI_MAGIC_NONE);