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;
83 pui_err("Invalid pui ani handle !\n");
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 handle = ani_h->pui_handle;
100 e = pui_display_set_buffer(handle, buffer);
102 return _pui_ani_error_to_int_error(e);
106 pui_ani_update(pui_ani_h ani_h)
108 pui_error e = PUI_ERROR_NONE;
113 pui_err("Invalid ani handle !\n");
114 return PUI_INT_ERROR_INVALID_HANDLE;
117 handle = ani_h->pui_handle;
118 e = pui_display_update(handle);
120 return _pui_ani_error_to_int_error(e);
124 _pui_ani_frame_cb(void *data)
127 Ecore_Timer *timer = NULL;
129 pui_ani_t *ani = (pui_ani_t *)data;
133 pui_err("Invalid pui ani !\n");
134 return ECORE_CALLBACK_CANCEL;
137 if (!ani->backend_frame_cb)
139 pui_err("Invalid backend frame_cb !\n");
140 return ECORE_CALLBACK_CANCEL;
143 if (!ani->frame_cb_timer)
145 pui_err("Invalid frame_cb timer !\n");
146 return ECORE_CALLBACK_CANCEL;
151 ret = (Eina_Bool)ani->backend_frame_cb(ani, ++ani->serial);
155 pui_err("Failed on backend's frame_cb !Frame_cb will be removed forcefuly !\n");
157 pui_ani_remove_frame_cb(ani);
162 if (ret && PUI_ANI_STATUS_STARTED == ani->status)
163 pui_ani_status_update(ani, PUI_ANI_STATUS_RUNNING);
165 ecore_timer_del(ani->frame_cb_timer);
166 ani->frame_cb_timer = NULL;
168 timer = ecore_timer_add(ani->frame_interval, _pui_ani_frame_cb, ani);
172 pui_err("Failed to add ecore timer !\n");
176 ani->frame_cb_timer = timer;
182 pui_ani_add_frame_cb(pui_ani_t *ani, pui_bool (*frame_cb)(void *data, int serial), double frame_interval)
184 Ecore_Timer *timer = NULL;
188 pui_err("Invalid put ani !\n");
192 if (frame_interval <= 0.0f)
194 pui_err("Invalid frame interval (%.2f) ! frame interval must be larger than 0.\n", frame_interval);
198 ani->frame_cb = _pui_ani_frame_cb;
199 ani->backend_frame_cb = frame_cb;
200 ani->frame_interval = frame_interval;
201 ani->frame_cb_data = ani;
204 timer = ecore_timer_add(frame_interval, _pui_ani_frame_cb, ani);
208 pui_err("Failed to add ecore timer !\n");
212 ani->frame_cb_timer = timer;
214 pui_info("[Frame callback added][ani id=%s] frame_interval=%.2f\n", ani->id, frame_interval);
216 /* call frame_cb for starting the first frame */
217 _pui_ani_frame_cb(ani);
223 pui_ani_remove_frame_cb(pui_ani_t *ani)
227 pui_err("Invalid put ani !\n");
231 if (ani->frame_cb_timer)
233 ecore_timer_del(ani->frame_cb_timer);
234 ani->frame_cb_timer = NULL;
237 ani->frame_cb = NULL;
238 ani->backend_frame_cb = NULL;
239 ani->frame_interval = 0;
240 ani->frame_cb_data = NULL;
242 pui_info("[Frame callback removed][ani id=%s]\n", ani->id);
246 pui_ani_get_id(pui_ani_h ani_h)
248 if (!ani_h || !ani_h->ani)
251 return ani_h->ani->id;
255 pui_ani_get_cmd(pui_ani_h ani_h)
257 if (!ani_h || !ani_h->ani)
258 return PUI_ANI_CMD_NONE;
260 return ani_h->ani->cmd;
264 pui_ani_get_repeat(pui_ani_h ani_h)
266 if (!ani_h || !ani_h->ani)
269 return ani_h->ani->repeat;
272 pui_backend_ani_data *
273 pui_ani_get_ani_data(pui_ani_t *ani)
278 return ani->ani_data;
282 pui_ani_status_update(pui_ani_t *ani, pui_ani_status status)
286 PUI_Event_Animation_Status *e = NULL;
290 pui_err("Invalid pui ani !\n");
294 if (ani->status == status)
299 ani->status = status;
303 case PUI_ANI_STATUS_STARTED:
304 ev_type = PUI_EVENT_ANI_STARTED;
305 pui_info("[Status update][ani id:%s] PUI_EVENT_ANI_STARTED event has been added.\n", ani->id);
308 case PUI_ANI_STATUS_RUNNING:
309 pui_info("[Status update][ani id:%s] PUI_ANI_STATUS_RUNNING !\n", ani->id);
312 case PUI_ANI_STATUS_PAUSED:
313 ev_type = PUI_EVENT_ANI_PAUSED;
314 pui_info("[Status update][ani id:%s] PUI_EVENT_ANI_PAUSED event has been added.\n", ani->id);
317 case PUI_ANI_STATUS_STOPPED:
318 ev_type = PUI_EVENT_ANI_STOPPED;
319 pui_info("[Status update][ani id:%s] PUI_EVENT_ANI_STOPPED event has been added.\n", ani->id);
323 pui_err("Unknown status !(ani status=%d, id=%s) !\n", status, ani->id);
329 e = (PUI_Event_Animation_Status *)calloc(1, sizeof(PUI_Event_Animation_Status));
333 pui_err("Failed to allocate memory for PUI Event !\n");
338 e->win = ecore_wl2_window_id_get(ani_h->pui_handle->win);
341 ecore_event_add(ev_type, e, NULL, ani_h);
346 pui_ani_status_get(pui_ani_t *ani)
348 pui_ani_status status = PUI_ANI_STATUS_UNKNOWN;
352 pui_err("Invalid pui ani !\n");
360 _pui_ani_control_with_force(pui_ani_h ani_h, pui_ani_cmd cmd, int repeat, pui_bool force)
362 pui_int_error ei = PUI_INT_ERROR_NONE;
363 pui_ani_t *ani = NULL;
365 pui_backend_ani_func *ani_func = NULL;
368 return PUI_ERROR_INVALID_ANI_HANDLE;
370 if (cmd < PUI_ANI_CMD_START || cmd >= PUI_ANI_CMD_LAST)
372 pui_err("Invalid cmd ! (cmd=%d)\n", cmd);
373 return PUI_ERROR_INVALID_ANI_CMD;
378 pui_err("Invalid repeat count ! (repeat=%d)\n", repeat);
379 return PUI_ERROR_INVALID_ANI_REPEAT;
382 handle = ani_h->pui_handle;
385 if (!ani || !ani->ani_data)
387 pui_err("Invalid ani or ani_data !\n");
388 return PUI_ERROR_INTERNAL;
391 ani_func = ani->ani_data->ani_func;
394 ani->repeat = repeat;
396 if (cmd == PUI_ANI_CMD_START)
398 if (handle->manual_render)
400 pui_err("Manual render has been set ! Unable to start animation.\n");
401 return PUI_ERROR_MANUAL_RENDER_ENABLED;
404 if (handle->current_ani_h && handle->current_ani_h != ani_h)
406 pui_ani_t *current_ani = handle->current_ani_h->ani;
408 if (current_ani->status >= PUI_ANI_STATUS_STARTED &&
409 current_ani->status <= PUI_ANI_STATUS_RUNNING)
411 pui_info("current_ani id=%s, status=%d\n", current_ani->id, current_ani->status);
413 ei = _pui_ani_control_with_force(handle->current_ani_h, PUI_ANI_CMD_STOP, 0, force);
415 if (ei != PUI_INT_ERROR_NONE)
416 pui_info("Failed to stop running previous animation ! (id:%s)\n", current_ani->id);
419 handle->current_ani_h = NULL;
423 ei = ani_func->ani_start(ani, repeat);
425 if (ei != PUI_INT_ERROR_NONE)
427 pui_err("Error on starting animation ! (id:%s, repeat:%d, status=%d))\n", ani->id, repeat, ani->status);
429 if (ani->status != PUI_ANI_STATUS_RUNNING)
430 _pui_ani_control_with_force(ani_h, PUI_ANI_CMD_STOP, 0, 0);
432 return PUI_ERROR_INTERNAL;
435 handle->current_ani_h = ani_h;
437 else//cmd == PUI_ANI_CMD_STOP
439 ei = ani_func->ani_stop(ani, force);
441 if (ani->frame_cb_timer)
442 pui_ani_remove_frame_cb(ani);
444 if (ei != PUI_INT_ERROR_NONE)
446 pui_err("Failied on stopping animation ! (id:%s)\n", ani->id);
448 if (ani->status != PUI_ANI_STATUS_STOPPED)
449 pui_ani_status_update(ani, PUI_ANI_STATUS_STOPPED);
451 return PUI_ERROR_INTERNAL;
454 return PUI_ERROR_NONE;
457 ani_h->frame_done_cb = ecore_wl2_window_frame_callback_add(handle->win, _pui_ani_cb_frame_done, handle);
459 if (!ani_h->frame_done_cb)
461 pui_err("Failed to add frame callback !");
465 return PUI_ERROR_NONE;
468 return PUI_ERROR_INTERNAL;
472 pui_ani_control(pui_ani_h ani_h, pui_ani_cmd cmd, int repeat)
474 return _pui_ani_control_with_force(ani_h, cmd, repeat, 0);
478 _cb_visibility_change(void *data, int type EINA_UNUSED, void *event)
480 pui_ani_h ani_h = (pui_ani_h)data;
481 pui_ani_t *ani = ani_h->ani;
482 pui_h ph = ani_h->pui_handle;
484 Ecore_Wl2_Event_Window_Visibility_Change *ev;
485 PUI_Event_Animation_Status *e = NULL;
489 /* check if this is needed */
490 ph->visibility = !(ev->fully_obscured);
492 if (ev->fully_obscured)
494 if (ani->status == PUI_ANI_STATUS_RUNNING)
496 pui_info("animation(%s) will be stopped as it lost its priority !\n", ani->id);
498 _pui_ani_control_with_force(ani_h, PUI_ANI_CMD_STOP, 0, 1);
503 if (ani->status == PUI_ANI_STATUS_PAUSED)
505 e = (PUI_Event_Animation_Status *)calloc(1, sizeof(PUI_Event_Animation_Status));
509 pui_err("Failed to allocate memory for PUI Event !\n");
510 return ECORE_CALLBACK_PASS_ON;
515 e->status = ani->status;
517 if(ani->status == PUI_ANI_STATUS_PAUSED)
519 pui_info("[Event added][ani id:%s] PUI_EVENT_ANI_READY_TO_RESUME event has been added.\n", ani_h->id);
520 ecore_event_add(PUI_EVENT_ANI_READY_TO_RESUME, e, NULL, ani_h);
525 return ECORE_CALLBACK_PASS_ON;
529 _pui_ani_event_handlers_init(pui_ani_h ani_h)
531 Ecore_Event_Handler *h = NULL;
535 pui_err("Invalid handle !\n");
539 if (!ani_h->ecore_event_hdls)
540 ani_h->ecore_event_hdls = eina_array_new(1);
542 h = ecore_event_handler_add(ECORE_WL2_EVENT_WINDOW_VISIBILITY_CHANGE, _cb_visibility_change, ani_h);
543 eina_array_push(ani_h->ecore_event_hdls, h);
548 _pui_ani_event_handlers_shutdown(pui_ani_h ani_h)
552 pui_err("Invalid handle !\n");
556 if (ani_h->ecore_event_hdls)
558 while (eina_array_count(ani_h->ecore_event_hdls))
559 ecore_event_handler_del(eina_array_pop(ani_h->ecore_event_hdls));
561 eina_array_free(ani_h->ecore_event_hdls);
562 ani_h->ecore_event_hdls = NULL;
567 pui_ani_create(pui_h handle, pui_id id)
569 pui_ani_h ani_h = NULL;
570 pui_ani_t *ani = NULL;
571 pui_backend_ani_data *ani_data = NULL;
573 if (!handle || !handle->backend_module_data)
575 pui_err("Invalid pui handle or backend module data !\n");
579 ani_data = handle->backend_module_data->ani_create(id);
583 pui_err("Invalid ani data from backend module data !\n");
587 ani_h = (pui_ani_h)calloc(1, sizeof(pui_ani));
591 pui_err("Failed to allocate memory for pui ani handle !\n");
595 ani_h->id = strdup(id);
596 ani_h->pui_handle = handle;
597 ani_h->ecore_event_hdls = NULL;
599 _pui_ani_event_handlers_init(ani_h);
601 ani = (pui_ani_t *)calloc(1, sizeof(pui_ani_t));
605 pui_err("Failed to allocate memory for pui ani mgr !\n");
611 ani->cmd = PUI_ANI_CMD_NONE;
613 ani->status = PUI_ANI_STATUS_INITIAL;
614 ani->ani_data = ani_data;
618 handle->ani_handles = eina_list_append(handle->ani_handles, ani_h);
625 handle->backend_module_data->ani_destroy(ani_data);
635 pui_ani_destroy(pui_ani_h ani_h)
638 pui_ani_t *ani = NULL;
639 pui_backend_module_data *backend_module_data = NULL;
641 if (!ani_h || !ani_h->pui_handle)
644 handle = ani_h->pui_handle;
647 /* stop the animation being played already if any */
648 if (ani->status == PUI_ANI_STATUS_STARTED || ani->status == PUI_ANI_STATUS_RUNNING)
649 pui_ani_control(ani_h, PUI_ANI_CMD_STOP, 0);
651 backend_module_data = handle->backend_module_data;
652 backend_module_data->ani_destroy(ani->ani_data);
653 ani->ani_data = NULL;
655 if (ani->frame_cb_timer)
657 ecore_timer_del(ani->frame_cb_timer);
658 ani->frame_cb_timer = NULL;
663 _pui_ani_event_handlers_shutdown(ani_h);
665 if (ani_h->frame_done_cb)
667 ecore_wl2_window_frame_callback_del(ani_h->frame_done_cb);
668 ani_h->frame_done_cb = NULL;
671 handle->ani_handles = eina_list_remove(handle->ani_handles, ani_h);