+/*
+ * Copyright © 2019 Samsung Electronics co., Ltd. All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "default_backend.h"
+
+static void
+_ani_backend_swupdatedone_get_led_rgb(default_frame_info_t *frame, int led_idx, unsigned int *r, unsigned int *g, unsigned int *b)
+{
+ if (!frame) return;
+ if (!r || !g || !b) return;
+ if (led_idx > frame->num_led) return;
+
+ *r = (frame->leds[led_idx].color & LED_MASK_RED) >> 16;
+ *g = (frame->leds[led_idx].color & LED_MASK_GREEN) >> 8;
+ *b = (frame->leds[led_idx].color & LED_MASK_BLUE);
+}
+
+static void
+_ani_backend_swupdatedone_free_frame(default_frame_info_t *frame)
+{
+ if (!frame) return;
+
+ if (frame->leds) free(frame->leds);
+ free(frame);
+}
+
+static default_frame_info_t *
+_ani_backend_swupdatedone_get_frame(default_ani_info *ani_info)
+{
+#define SMOOTH_FRAME 20
+ default_frame_info_t *frame, *key_frame, *key_frame2;
+ int idx, idx2;
+ unsigned int r, g, b, r2, g2, b2;
+ int r3, g3, b3;
+ double div;
+
+ frame = (default_frame_info_t *)calloc(sizeof(default_frame_info_t), 1);
+ if (!frame) return NULL;
+
+ idx = ((int)(ani_info->frame_idx / SMOOTH_FRAME)) % ani_info->num_key_frames;
+ idx2 = (idx + 1) % ani_info->num_key_frames;
+
+ key_frame = &ani_info->frames[idx];
+ key_frame2 = &ani_info->frames[idx2];
+ frame->num_led = key_frame->num_led;
+ frame->leds = (default_led_info_t *)calloc(sizeof(default_led_info_t), frame->num_led);
+ if (!frame->leds)
+ {
+ free(frame);
+ return NULL;
+ }
+ div = (double)(ani_info->frame_idx % SMOOTH_FRAME) / (double)SMOOTH_FRAME;
+ r = g = b = r2 = g2 = b2 = 0x0;
+ for (int i = 0; i < key_frame->num_led; i++)
+ {
+ _ani_backend_swupdatedone_get_led_rgb(key_frame, i, &r, &g, &b);
+ _ani_backend_swupdatedone_get_led_rgb(key_frame2, i, &r2, &g2, &b2);
+ r3 = (int)(r2 - r) * div + r;
+ g3 = (int)(g2 - g) * div + g;
+ b3 = (int)(b2 - b) * div + b;
+
+ frame->leds[i].color = (r3 << 16) + (g3 << 8) + (b3);
+ }
+ ani_info->frame_idx++;
+ if (ani_info->frame_idx >= (ani_info->num_key_frames * SMOOTH_FRAME))
+ ani_info->frame_idx = 0;
+
+ return frame;
+}
+
+static pui_bool
+_ani_backend_swupdatedone_frame_cb(void *data, int serial)
+{
+ pui_int_error e = PUI_INT_ERROR_NONE;
+ pui_ani_t *ani = (pui_ani_t *)data;
+ pui_backend_ani_data *ani_data = NULL;
+ pui_ani_control_buffer *buffer = NULL;
+ default_frame_info_t *frame;
+ unsigned int r = 0x0, g = 0x0, b = 0x0;
+ double now;
+
+ ani_data = pui_backend_ani_get_ani_data(ani);
+ default_ani_info *ani_info = (default_ani_info *)ani_data->ani_info;
+
+ now = ecore_time_unix_get();
+ pui_info("[time:%.3f] serial=%d\n", now, serial);
+
+ /* TODO : make use of ani_info */
+ //(void) ani_info;
+
+ buffer = pui_backend_ani_get_buffer(ani);
+ if (!buffer) {
+ pui_err("Failed to get buffer for animation\n");
+ return (pui_bool)0;
+ }
+
+ frame = _ani_backend_swupdatedone_get_frame(ani_info);
+ for(int i = 0; i<12; i++)
+ {
+ _ani_backend_swupdatedone_get_led_rgb(frame, i, &r, &g, &b);
+ buffer->ptr[4*i] = 0;
+ buffer->ptr[4*i + 1] = b; /* BLUE */
+ buffer->ptr[4*i + 2] = g; /* GREEN */
+ buffer->ptr[4*i + 3] = r; /* RED */
+ }
+ _ani_backend_swupdatedone_free_frame(frame);
+
+ e = pui_backend_ani_set_buffer(ani, buffer);
+
+ if (e != PUI_INT_ERROR_NONE)
+ {
+ pui_err("Failed on setting buffer on animation !(e=%d)\n", e);
+ return (pui_bool)0;
+ }
+
+ e = pui_backend_ani_update(ani);
+
+ if (e != PUI_INT_ERROR_NONE)
+ {
+ pui_err("Failed on updating animation !(e=%d)\n", e);
+ return (pui_bool)0;
+ }
+
+ pui_info("... update (serial=%d)\n", serial);
+
+ return (pui_bool)1;
+}
+
+pui_error
+_ani_swupdatedone_start(pui_ani_t *ani, int repeat)
+{
+ pui_bool ret = 0;
+ pui_int_error e = PUI_INT_ERROR_NONE;
+ pui_backend_ani_data *ani_data = NULL;
+
+ ani_data = pui_backend_ani_get_ani_data(ani);
+ default_ani_info *info = (default_ani_info *)ani_data->ani_info;
+
+ //TODO
+ (void) info;
+
+ pui_info("... info->id: %s, repeat : %d, interval: %d\n", info->id, repeat, info->interval);
+
+ pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STARTED);
+ ret = pui_backend_ani_add_frame_cb(ani, _ani_backend_swupdatedone_frame_cb, info->interval / 1000.0);
+
+ if (!ret)
+ {
+ pui_err("Failed to add frame callback !\n");
+ e = PUI_INT_ERROR_INVALID_RESOURCES;
+ }
+
+ return e;
+}
+
+pui_error
+_ani_swupdatedone_stop(pui_ani_t *ani)
+{
+ pui_int_error e = PUI_INT_ERROR_NONE;
+ pui_backend_ani_data *ani_data = NULL;
+
+ ani_data = pui_backend_ani_get_ani_data(ani);
+ default_ani_info *info = (default_ani_info *)ani_data->ani_info;
+
+ //TODO
+ (void) info;
+
+ pui_info("... info->id: %s\n", info->id);
+
+ pui_backend_ani_remove_frame_cb(ani);
+ pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STOPPED);
+
+ return e;
+}
+
+
+void
+pui_default_backend_ani_swupdatedone_func_set(pui_backend_ani_func *func)
+{
+ if (!func) return;
+
+ func->ani_start = _ani_swupdatedone_start;
+ func->ani_stop = _ani_swupdatedone_stop;
+}
+
+