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)
128 pui_ani_t *ani = (pui_ani_t *)data;
132 pui_err("Invalid pui ani !\n");
133 return ECORE_CALLBACK_CANCEL;
136 if (!ani->backend_frame_cb)
138 pui_err("Invalid backend frame_cb !\n");
139 return ECORE_CALLBACK_CANCEL;
142 if (!ani->frame_cb_timer)
144 pui_err("Invalid frame_cb timer !\n");
145 return ECORE_CALLBACK_CANCEL;
150 ret = (Eina_Bool)ani->backend_frame_cb(ani, ++ani->serial);
154 pui_err("Failed on backend's frame_cb !Frame_cb will be removed forcefuly !\n");
156 pui_ani_remove_frame_cb(ani);
161 if (ret && PUI_ANI_STATUS_STARTED == ani->status)
162 pui_ani_status_update(ani, PUI_ANI_STATUS_RUNNING);
168 pui_ani_add_frame_cb(pui_ani_t *ani, pui_bool (*frame_cb)(void *data, int serial), double frame_interval)
170 Ecore_Timer *timer = NULL;
174 pui_err("Invalid put ani !\n");
178 if (frame_interval <= 0.0f)
180 pui_err("Invalid frame interval (%.2f) ! frame interval must be larger than 0.\n", frame_interval);
184 ani->frame_cb = _pui_ani_frame_cb;
185 ani->backend_frame_cb = frame_cb;
186 ani->frame_interval = frame_interval;
187 ani->frame_cb_data = ani;
190 timer = ecore_timer_add(frame_interval, _pui_ani_frame_cb, ani);
194 pui_err("Failed to add ecore timer !\n");
198 ani->frame_cb_timer = timer;
200 pui_info("[Frame callback added][ani id=%s] frame_interval=%.2f\n", ani->id, frame_interval);
202 /* call frame_cb for starting the first frame */
203 _pui_ani_frame_cb(ani);
209 pui_ani_remove_frame_cb(pui_ani_t *ani)
213 pui_err("Invalid put ani !\n");
217 if (ani->frame_cb_timer)
219 ecore_timer_del(ani->frame_cb_timer);
220 ani->frame_cb_timer = NULL;
223 ani->frame_cb = NULL;
224 ani->backend_frame_cb = NULL;
225 ani->frame_interval = 0;
226 ani->frame_cb_data = NULL;
228 pui_info("[Frame callback removed][ani id=%s]\n", ani->id);
232 pui_ani_get_id(pui_ani_h ani_h)
234 if (!ani_h || !ani_h->ani)
237 return ani_h->ani->id;
241 pui_ani_get_cmd(pui_ani_h ani_h)
243 if (!ani_h || !ani_h->ani)
244 return PUI_ANI_CMD_NONE;
246 return ani_h->ani->cmd;
250 pui_ani_get_repeat(pui_ani_h ani_h)
252 if (!ani_h || !ani_h->ani)
255 return ani_h->ani->repeat;
258 pui_backend_ani_data *
259 pui_ani_get_ani_data(pui_ani_t *ani)
264 return ani->ani_data;
268 pui_ani_status_update(pui_ani_t *ani, pui_ani_status status)
272 PUI_Event_Animation_Status *e = NULL;
276 pui_err("Invalid pui ani !\n");
280 if (ani->status == status)
285 e = (PUI_Event_Animation_Status *)calloc(1, sizeof(PUI_Event_Animation_Status));
289 pui_err("Failed to allocate memory for PUI Event !\n");
293 ani->status = status;
295 e->win = ecore_wl2_window_id_get(ani_h->pui_handle->win);
300 case PUI_ANI_STATUS_STARTED:
301 ev_type = PUI_EVENT_ANI_STARTED;
302 pui_info("[Status update][ani id:%s] PUI_EVENT_ANI_STARTED event has been added.\n", ani->id);
305 case PUI_ANI_STATUS_RUNNING:
306 pui_info("[Status update][ani id:%s] PUI_ANI_STATUS_RUNNING !\n", ani->id);
309 case PUI_ANI_STATUS_PAUSED:
310 ev_type = PUI_EVENT_ANI_PAUSED;
311 pui_info("[Status update][ani id:%s] PUI_EVENT_ANI_PAUSED event has been added.\n", ani->id);
314 case PUI_ANI_STATUS_STOPPED:
315 ev_type = PUI_EVENT_ANI_STOPPED;
316 pui_info("[Status update][ani id:%s] PUI_EVENT_ANI_STOPPED event has been added.\n", ani->id);
320 pui_err("Unknown status !(ani status=%d, id=%s) !\n", status, ani->id);
326 ecore_event_add(ev_type, e, NULL, ani_h);
331 pui_ani_status_get(pui_ani_t *ani)
333 pui_ani_status status = PUI_ANI_STATUS_UNKNOWN;
337 pui_err("Invalid pui ani !\n");
345 _pui_ani_control_with_force(pui_ani_h ani_h, pui_ani_cmd cmd, int repeat, pui_bool force)
347 pui_int_error ei = PUI_INT_ERROR_NONE;
348 pui_ani_t *ani = NULL;
350 pui_backend_ani_func *ani_func = NULL;
353 return PUI_ERROR_INVALID_ANI_HANDLE;
355 if (cmd < PUI_ANI_CMD_START || cmd >= PUI_ANI_CMD_LAST)
357 pui_err("Invalid cmd ! (cmd=%d)\n", cmd);
358 return PUI_ERROR_INVALID_ANI_CMD;
363 pui_err("Invalid repeat count ! (repeat=%d)\n", repeat);
364 return PUI_ERROR_INVALID_ANI_REPEAT;
367 handle = ani_h->pui_handle;
370 if (!ani || !ani->ani_data)
372 pui_err("Invalid ani or ani_data !\n");
373 return PUI_ERROR_INTERNAL;
376 ani_func = ani->ani_data->ani_func;
379 ani->repeat = repeat;
381 if (cmd == PUI_ANI_CMD_START)
383 if (handle->manual_render)
385 pui_err("Manual render has been set ! Unable to start animation.\n");
386 return PUI_ERROR_MANUAL_RENDER_ENABLED;
389 if (handle->current_ani_h && handle->current_ani_h != ani_h)
391 pui_ani_t *current_ani = handle->current_ani_h->ani;
393 if (current_ani->status >= PUI_ANI_STATUS_STARTED &&
394 current_ani->status <= PUI_ANI_STATUS_RUNNING)
396 pui_info("current_ani id=%s, status=%d\n", current_ani->id, current_ani->status);
398 ei = _pui_ani_control_with_force(handle->current_ani_h, PUI_ANI_CMD_STOP, 0, force);
400 if (ei != PUI_INT_ERROR_NONE)
401 pui_info("Failed to stop running previous animation ! (id:%s)\n", current_ani->id);
404 handle->current_ani_h = NULL;
408 ei = ani_func->ani_start(ani, repeat);
410 if (ei != PUI_INT_ERROR_NONE)
412 pui_err("Error on starting animation ! (id:%s, repeat:%d, status=%d))\n", ani->id, repeat, ani->status);
414 if (ani->status != PUI_ANI_STATUS_RUNNING)
415 _pui_ani_control_with_force(ani_h, PUI_ANI_CMD_STOP, 0, 0);
417 return PUI_ERROR_INTERNAL;
420 handle->current_ani_h = ani_h;
422 else//cmd == PUI_ANI_CMD_STOP
424 ei = ani_func->ani_stop(ani, force);
426 if (ei != PUI_INT_ERROR_NONE)
428 pui_err("Failied on stopping animation ! (id:%s)\n", ani->id);
430 if (ani->status != PUI_ANI_STATUS_STOPPED)
431 pui_ani_status_update(ani, PUI_ANI_STATUS_STOPPED);
433 if (ani->frame_cb_timer)
434 pui_ani_remove_frame_cb(ani);
436 return PUI_ERROR_INTERNAL;
439 return PUI_ERROR_NONE;
442 ani_h->frame_done_cb = ecore_wl2_window_frame_callback_add(handle->win, _pui_ani_cb_frame_done, handle);
444 if (!ani_h->frame_done_cb)
446 pui_err("Failed to add frame callback !");
450 return PUI_ERROR_NONE;
453 return PUI_ERROR_INTERNAL;
457 pui_ani_control(pui_ani_h ani_h, pui_ani_cmd cmd, int repeat)
459 return _pui_ani_control_with_force(ani_h, cmd, repeat, 0);
463 _cb_visibility_change(void *data, int type EINA_UNUSED, void *event)
465 pui_ani_h ani_h = (pui_ani_h)data;
466 pui_ani_t *ani = ani_h->ani;
467 pui_h ph = ani_h->pui_handle;
469 Ecore_Wl2_Event_Window_Visibility_Change *ev;
470 PUI_Event_Animation_Status *e = NULL;
474 /* check if this is needed */
475 ph->visibility = !(ev->fully_obscured);
477 if (ev->fully_obscured)
479 if (ani->status == PUI_ANI_STATUS_RUNNING)
481 pui_info("animation(%s) will be stopped as it lost its priority !\n", ani->id);
483 _pui_ani_control_with_force(ani_h, PUI_ANI_CMD_STOP, 0, 1);
488 if (ani->status == PUI_ANI_STATUS_PAUSED)
490 e = (PUI_Event_Animation_Status *)calloc(1, sizeof(PUI_Event_Animation_Status));
494 pui_err("Failed to allocate memory for PUI Event !\n");
495 return ECORE_CALLBACK_PASS_ON;
500 e->status = ani->status;
502 if(ani->status == PUI_ANI_STATUS_PAUSED)
504 pui_info("[Event added][ani id:%s] PUI_EVENT_ANI_READY_TO_RESUME event has been added.\n", ani_h->id);
505 ecore_event_add(PUI_EVENT_ANI_READY_TO_RESUME, e, NULL, ani_h);
510 return ECORE_CALLBACK_PASS_ON;
514 _pui_ani_event_handlers_init(pui_ani_h ani_h)
516 Ecore_Event_Handler *h = NULL;
520 pui_err("Invalid handle !\n");
524 if (!ani_h->ecore_event_hdls)
525 ani_h->ecore_event_hdls = eina_array_new(1);
527 h = ecore_event_handler_add(ECORE_WL2_EVENT_WINDOW_VISIBILITY_CHANGE, _cb_visibility_change, ani_h);
528 eina_array_push(ani_h->ecore_event_hdls, h);
533 _pui_ani_event_handlers_shutdown(pui_ani_h ani_h)
537 pui_err("Invalid handle !\n");
541 if (ani_h->ecore_event_hdls)
543 while (eina_array_count(ani_h->ecore_event_hdls))
544 ecore_event_handler_del(eina_array_pop(ani_h->ecore_event_hdls));
546 eina_array_free(ani_h->ecore_event_hdls);
547 ani_h->ecore_event_hdls = NULL;
552 pui_ani_create(pui_h handle, pui_id id)
554 pui_ani_h ani_h = NULL;
555 pui_ani_t *ani = NULL;
556 pui_backend_ani_data *ani_data = NULL;
558 if (!handle || !handle->backend_module_data)
560 pui_err("Invalid pui handle or backend module data !\n");
564 ani_data = handle->backend_module_data->ani_create(id);
568 pui_err("Invalid ani data from backend module data !\n");
572 ani_h = (pui_ani_h)calloc(1, sizeof(pui_ani));
576 pui_err("Failed to allocate memory for pui ani handle !\n");
580 ani_h->id = strdup(id);
581 ani_h->pui_handle = handle;
582 ani_h->ecore_event_hdls = NULL;
584 _pui_ani_event_handlers_init(ani_h);
586 ani = (pui_ani_t *)calloc(1, sizeof(pui_ani_t));
590 pui_err("Failed to allocate memory for pui ani mgr !\n");
596 ani->cmd = PUI_ANI_CMD_NONE;
598 ani->status = PUI_ANI_STATUS_INITIAL;
599 ani->ani_data = ani_data;
603 handle->ani_handles = eina_list_append(handle->ani_handles, ani_h);
610 handle->backend_module_data->ani_destroy(ani_data);
620 pui_ani_destroy(pui_ani_h ani_h)
623 pui_ani_t *ani = NULL;
624 pui_backend_module_data *backend_module_data = NULL;
626 if (!ani_h || !ani_h->pui_handle)
629 handle = ani_h->pui_handle;
632 /* stop the animation being played already if any */
633 if (ani->status == PUI_ANI_STATUS_STARTED || ani->status == PUI_ANI_STATUS_RUNNING)
634 pui_ani_control(ani_h, PUI_ANI_CMD_STOP, 0);
636 backend_module_data = handle->backend_module_data;
637 backend_module_data->ani_destroy(ani->ani_data);
638 ani->ani_data = NULL;
640 if (ani->frame_cb_timer)
642 ecore_timer_del(ani->frame_cb_timer);
643 ani->frame_cb_timer = NULL;
648 _pui_ani_event_handlers_shutdown(ani_h);
650 if (ani_h->frame_done_cb)
652 ecore_wl2_window_frame_callback_del(ani_h->frame_done_cb);
653 ani_h->frame_done_cb = NULL;
656 handle->ani_handles = eina_list_remove(handle->ani_handles, ani_h);