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 e = (PUI_Event_Animation_Status *)calloc(1, sizeof(PUI_Event_Animation_Status));
303 pui_err("Failed to allocate memory for PUI Event !\n");
307 ani->status = status;
309 e->win = ecore_wl2_window_id_get(ani_h->pui_handle->win);
314 case PUI_ANI_STATUS_STARTED:
315 ev_type = PUI_EVENT_ANI_STARTED;
316 pui_info("[Status update][ani id:%s] PUI_EVENT_ANI_STARTED event has been added.\n", ani->id);
319 case PUI_ANI_STATUS_RUNNING:
320 pui_info("[Status update][ani id:%s] PUI_ANI_STATUS_RUNNING !\n", ani->id);
323 case PUI_ANI_STATUS_PAUSED:
324 ev_type = PUI_EVENT_ANI_PAUSED;
325 pui_info("[Status update][ani id:%s] PUI_EVENT_ANI_PAUSED event has been added.\n", ani->id);
328 case PUI_ANI_STATUS_STOPPED:
329 ev_type = PUI_EVENT_ANI_STOPPED;
330 pui_info("[Status update][ani id:%s] PUI_EVENT_ANI_STOPPED event has been added.\n", ani->id);
334 pui_err("Unknown status !(ani status=%d, id=%s) !\n", status, ani->id);
340 ecore_event_add(ev_type, e, NULL, ani_h);
345 pui_ani_status_get(pui_ani_t *ani)
347 pui_ani_status status = PUI_ANI_STATUS_UNKNOWN;
351 pui_err("Invalid pui ani !\n");
359 _pui_ani_control_with_force(pui_ani_h ani_h, pui_ani_cmd cmd, int repeat, pui_bool force)
361 pui_int_error ei = PUI_INT_ERROR_NONE;
362 pui_ani_t *ani = NULL;
364 pui_backend_ani_func *ani_func = NULL;
367 return PUI_ERROR_INVALID_ANI_HANDLE;
369 if (cmd < PUI_ANI_CMD_START || cmd >= PUI_ANI_CMD_LAST)
371 pui_err("Invalid cmd ! (cmd=%d)\n", cmd);
372 return PUI_ERROR_INVALID_ANI_CMD;
377 pui_err("Invalid repeat count ! (repeat=%d)\n", repeat);
378 return PUI_ERROR_INVALID_ANI_REPEAT;
381 handle = ani_h->pui_handle;
384 if (!ani || !ani->ani_data)
386 pui_err("Invalid ani or ani_data !\n");
387 return PUI_ERROR_INTERNAL;
390 ani_func = ani->ani_data->ani_func;
393 ani->repeat = repeat;
395 if (cmd == PUI_ANI_CMD_START)
397 if (handle->manual_render)
399 pui_err("Manual render has been set ! Unable to start animation.\n");
400 return PUI_ERROR_MANUAL_RENDER_ENABLED;
403 if (handle->current_ani_h && handle->current_ani_h != ani_h)
405 pui_ani_t *current_ani = handle->current_ani_h->ani;
407 if (current_ani->status >= PUI_ANI_STATUS_STARTED &&
408 current_ani->status <= PUI_ANI_STATUS_RUNNING)
410 pui_info("current_ani id=%s, status=%d\n", current_ani->id, current_ani->status);
412 ei = _pui_ani_control_with_force(handle->current_ani_h, PUI_ANI_CMD_STOP, 0, force);
414 if (ei != PUI_INT_ERROR_NONE)
415 pui_info("Failed to stop running previous animation ! (id:%s)\n", current_ani->id);
418 handle->current_ani_h = NULL;
422 ei = ani_func->ani_start(ani, repeat);
424 if (ei != PUI_INT_ERROR_NONE)
426 pui_err("Error on starting animation ! (id:%s, repeat:%d, status=%d))\n", ani->id, repeat, ani->status);
428 if (ani->status != PUI_ANI_STATUS_RUNNING)
429 _pui_ani_control_with_force(ani_h, PUI_ANI_CMD_STOP, 0, 0);
431 return PUI_ERROR_INTERNAL;
434 handle->current_ani_h = ani_h;
436 else//cmd == PUI_ANI_CMD_STOP
438 ei = ani_func->ani_stop(ani, force);
440 if (ani->frame_cb_timer)
441 pui_ani_remove_frame_cb(ani);
443 if (ei != PUI_INT_ERROR_NONE)
445 pui_err("Failied on stopping animation ! (id:%s)\n", ani->id);
447 if (ani->status != PUI_ANI_STATUS_STOPPED)
448 pui_ani_status_update(ani, PUI_ANI_STATUS_STOPPED);
450 return PUI_ERROR_INTERNAL;
453 return PUI_ERROR_NONE;
456 ani_h->frame_done_cb = ecore_wl2_window_frame_callback_add(handle->win, _pui_ani_cb_frame_done, handle);
458 if (!ani_h->frame_done_cb)
460 pui_err("Failed to add frame callback !");
464 return PUI_ERROR_NONE;
467 return PUI_ERROR_INTERNAL;
471 pui_ani_control(pui_ani_h ani_h, pui_ani_cmd cmd, int repeat)
473 return _pui_ani_control_with_force(ani_h, cmd, repeat, 0);
477 _cb_visibility_change(void *data, int type EINA_UNUSED, void *event)
479 pui_ani_h ani_h = (pui_ani_h)data;
480 pui_ani_t *ani = ani_h->ani;
481 pui_h ph = ani_h->pui_handle;
483 Ecore_Wl2_Event_Window_Visibility_Change *ev;
484 PUI_Event_Animation_Status *e = NULL;
488 /* check if this is needed */
489 ph->visibility = !(ev->fully_obscured);
491 if (ev->fully_obscured)
493 if (ani->status == PUI_ANI_STATUS_RUNNING)
495 pui_info("animation(%s) will be stopped as it lost its priority !\n", ani->id);
497 _pui_ani_control_with_force(ani_h, PUI_ANI_CMD_STOP, 0, 1);
502 if (ani->status == PUI_ANI_STATUS_PAUSED)
504 e = (PUI_Event_Animation_Status *)calloc(1, sizeof(PUI_Event_Animation_Status));
508 pui_err("Failed to allocate memory for PUI Event !\n");
509 return ECORE_CALLBACK_PASS_ON;
514 e->status = ani->status;
516 if(ani->status == PUI_ANI_STATUS_PAUSED)
518 pui_info("[Event added][ani id:%s] PUI_EVENT_ANI_READY_TO_RESUME event has been added.\n", ani_h->id);
519 ecore_event_add(PUI_EVENT_ANI_READY_TO_RESUME, e, NULL, ani_h);
524 return ECORE_CALLBACK_PASS_ON;
528 _pui_ani_event_handlers_init(pui_ani_h ani_h)
530 Ecore_Event_Handler *h = NULL;
534 pui_err("Invalid handle !\n");
538 if (!ani_h->ecore_event_hdls)
539 ani_h->ecore_event_hdls = eina_array_new(1);
541 h = ecore_event_handler_add(ECORE_WL2_EVENT_WINDOW_VISIBILITY_CHANGE, _cb_visibility_change, ani_h);
542 eina_array_push(ani_h->ecore_event_hdls, h);
547 _pui_ani_event_handlers_shutdown(pui_ani_h ani_h)
551 pui_err("Invalid handle !\n");
555 if (ani_h->ecore_event_hdls)
557 while (eina_array_count(ani_h->ecore_event_hdls))
558 ecore_event_handler_del(eina_array_pop(ani_h->ecore_event_hdls));
560 eina_array_free(ani_h->ecore_event_hdls);
561 ani_h->ecore_event_hdls = NULL;
566 pui_ani_create(pui_h handle, pui_id id)
568 pui_ani_h ani_h = NULL;
569 pui_ani_t *ani = NULL;
570 pui_backend_ani_data *ani_data = NULL;
572 if (!handle || !handle->backend_module_data)
574 pui_err("Invalid pui handle or backend module data !\n");
578 ani_data = handle->backend_module_data->ani_create(id);
582 pui_err("Invalid ani data from backend module data !\n");
586 ani_h = (pui_ani_h)calloc(1, sizeof(pui_ani));
590 pui_err("Failed to allocate memory for pui ani handle !\n");
594 ani_h->id = strdup(id);
595 ani_h->pui_handle = handle;
596 ani_h->ecore_event_hdls = NULL;
598 _pui_ani_event_handlers_init(ani_h);
600 ani = (pui_ani_t *)calloc(1, sizeof(pui_ani_t));
604 pui_err("Failed to allocate memory for pui ani mgr !\n");
610 ani->cmd = PUI_ANI_CMD_NONE;
612 ani->status = PUI_ANI_STATUS_INITIAL;
613 ani->ani_data = ani_data;
617 handle->ani_handles = eina_list_append(handle->ani_handles, ani_h);
624 handle->backend_module_data->ani_destroy(ani_data);
634 pui_ani_destroy(pui_ani_h ani_h)
637 pui_ani_t *ani = NULL;
638 pui_backend_module_data *backend_module_data = NULL;
640 if (!ani_h || !ani_h->pui_handle)
643 handle = ani_h->pui_handle;
646 /* stop the animation being played already if any */
647 if (ani->status == PUI_ANI_STATUS_STARTED || ani->status == PUI_ANI_STATUS_RUNNING)
648 pui_ani_control(ani_h, PUI_ANI_CMD_STOP, 0);
650 backend_module_data = handle->backend_module_data;
651 backend_module_data->ani_destroy(ani->ani_data);
652 ani->ani_data = NULL;
654 if (ani->frame_cb_timer)
656 ecore_timer_del(ani->frame_cb_timer);
657 ani->frame_cb_timer = NULL;
662 _pui_ani_event_handlers_shutdown(ani_h);
664 if (ani_h->frame_done_cb)
666 ecore_wl2_window_frame_callback_del(ani_h->frame_done_cb);
667 ani_h->frame_done_cb = NULL;
670 handle->ani_handles = eina_list_remove(handle->ani_handles, ani_h);