default_backend: implements frame_duration to proceed each frame at a different speed
[platform/core/uifw/libpui.git] / backends / default_backend.c
index 4f3eb32..8ed95cf 100644 (file)
  * SOFTWARE.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <PUI_backend.h>
-
-#include <json-c/json.h>
-#include <dirent.h>
-#include <errno.h>
-#include <Eina.h>
-#include <Ecore.h>
-#include <config.h>
-
-#define ANI_COLLECTION_DIR "/usr/share/pui/"
-#define MAX_STR 1024
-
-#define ERROR_CHECK(exp, action, fmt, ...) \
-       do { \
-               if (!(exp)) \
-               { \
-                       printf(fmt, ##__VA_ARGS__);     \
-                       action; \
-               } \
-       } while (0)
+#include "default_backend.h"
 
 pui_backend_ani_func *ani_func = NULL;
 Eina_Hash *_animations_hash = NULL;
+pui_backend_ani_data *g_ani_data = NULL;
 
-typedef enum
-{
-       None,
-       Linear,
-       EaseInSine,
-       EaseOutSine,
-       EaseInQuart,
-       EaseOutQuart
-} pui_effect_func;
-
-typedef struct _default_ani_info default_ani_info;
-typedef struct _default_frame_info_t default_frame_info_t;
-typedef struct _default_led_info_t default_led_info_t;
-
-struct _default_ani_info
+#if 0
+static double
+_ani_backend_ease_function_get_intensity(pui_effect_func func, double interval)
 {
-       pui_id id;
-       pui_ani_status status;
-       pui_ani_control_buffer *buffer;
-       unsigned int repeat;
-
-       unsigned int key_frame_idx;
-       unsigned int num_key_frames;
-       default_frame_info_t *frames;
-       int interval;
-       pui_effect_func effect_func;
-};
+       double intensity = interval;
+       switch (func)
+       {
+               case None:
+                       break;
+               case Linear:
+                       break;
+               case EaseInSine:
+                       intensity = 1 - cos(PI / 2 * interval);
+                       break;
+               case EaseOutSine:
+                       intensity = sin(PI / 2 * interval);
+                       break;
+               case EaseInQuart:
+                       intensity = interval * interval;
+                       break;
+               case EaseOutQuart:
+                       intensity = interval * (2 - interval);
+                       break;
+               default:
+                       break;
+       }
 
-struct _default_frame_info_t
-{
-       default_led_info_t *leds;
-       int num_led;
-};
+       return intensity;
+}
 
-struct _default_led_info_t
+static unsigned int
+_ani_backend_get_value(unsigned int end_frame, unsigned int start_frame, double interval)
 {
-       unsigned int color;
-};
+       double res = 0.0;
 
-pui_backend_ani_data *g_ani_data = NULL;
+       // interval: frame ratio between key frame to key frame
+       // end_frame and start_frame is key frame
+
+       res = (end_frame - start_frame) * interval + start_frame;
+
+       return res;
+}
+#endif
 
 static pui_bool
 _ani_backend_frame_cb(void *data, int serial)
@@ -121,9 +104,9 @@ _ani_backend_frame_cb(void *data, int serial)
        for(int i = 0; i<12; i++)
        {
                buffer->ptr[4*i] = 0;
-               buffer->ptr[4*i + 1] = (ani_info->frames[ani_info->key_frame_idx].leds[i].color & 0xFF0000) >> 16;//R
-               buffer->ptr[4*i + 2] = (ani_info->frames[ani_info->key_frame_idx].leds[i].color & 0x00FF00) >> 8;//G
-               buffer->ptr[4*i + 3] = ani_info->frames[ani_info->key_frame_idx].leds[i].color & 0x0000FF;//B
+               buffer->ptr[4*i + 1] = (ani_info->frames[ani_info->frame_idx].leds[i].color & LED_MASK_RED) >> 16;//R
+               buffer->ptr[4*i + 2] = (ani_info->frames[ani_info->frame_idx].leds[i].color & LED_MASK_GREEN) >> 8;//G
+               buffer->ptr[4*i + 3] = ani_info->frames[ani_info->frame_idx].leds[i].color & LED_MASK_BLUE;//B
        }
 
        e = pui_backend_ani_set_buffer(ani, buffer);
@@ -195,7 +178,7 @@ _ani_start(pui_ani_t *ani, int repeat)
 }
 
 pui_error
-_ani_stop(pui_ani_t *ani)
+_ani_stop(pui_ani_t *ani, pui_bool force)
 {
        pui_int_error e = PUI_INT_ERROR_NONE;
        pui_backend_ani_data *ani_data = NULL;
@@ -206,10 +189,14 @@ _ani_stop(pui_ani_t *ani)
        //TODO
        (void) info;
 
-       pui_info("... info->id: %s\n", info->id);
+       pui_info("... info->id: %s, force=%d\n", info->id, force);
 
        pui_backend_ani_remove_frame_cb(ani);
-       pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STOPPED);
+
+       if (force)
+               pui_backend_ani_status_update(ani, PUI_ANI_STATUS_PAUSED);
+       else
+               pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STOPPED);
 
        return e;
 }
@@ -272,7 +259,7 @@ _read_json(const char *path)
        ani_info->num_key_frames = frame_len;
        ani_info->frames = (default_frame_info_t *)calloc(sizeof(default_frame_info_t), frame_len);
        ERROR_CHECK(ani_info->frames, goto error, "Failed to alloc for default_frame_info_t\n");
-       
+
        for (i = 0; i < frame_len; i++) {
                frame_obj = json_object_array_get_idx(data_obj, i);
 
@@ -280,13 +267,19 @@ _read_json(const char *path)
                //printf("\tframe id: %d\n", json_object_get_int(frame_data_obj));
                frame_id = json_object_get_int(frame_data_obj);
 
+               frame_data_obj = json_object_object_get(frame_obj, "frame_duration");
+               if (frame_data_obj)
+                       ani_info->frames[frame_id - 1].frame_duration = json_object_get_int(frame_data_obj);
+               else
+                       ani_info->frames[frame_id - 1].frame_duration = ani_info->interval;
+
                frame_data_obj = json_object_object_get(frame_obj, "led");
                led_len = json_object_array_length(frame_data_obj);
-               
+
                ani_info->frames[frame_id - 1].num_led = led_len;
                ani_info->frames[frame_id - 1].leds = (default_led_info_t *)calloc(sizeof(default_led_info_t), led_len);
                ERROR_CHECK(ani_info->frames[frame_id - 1].leds, goto error, "Failed to alloc for default_led_info_t\n");
-               
+
                for (j = 0; j < led_len; j++) {
                        led_obj = json_object_array_get_idx(frame_data_obj, j);
 
@@ -320,16 +313,46 @@ error:
        return NULL;
 }
 
-static int
-_scandir_filter(const struct dirent *dirent)
+int
+_find_directory(const char *path, Eina_List **json_list)
 {
-       if (!strncmp(dirent->d_name, ".", sizeof(".")) ||
-               !strncmp(dirent->d_name, "..", sizeof("..")))
-               return 0;
-       if (!strstr(dirent->d_name, ".json"))
-               return 0;
+       DIR *dir;
+       struct dirent *cur;
+       int count = 0, ret_cnt = 0;
 
-       return 1;
+       dir = opendir(path);
+       if (!dir)
+       {
+               pui_err("Failed to open %s.\n", path);
+               return count;
+       }
+
+       while ((cur = readdir(dir)) != NULL)
+       {
+               char next_path[MAX_STR] = {0, };
+               if (cur->d_type == DT_DIR)
+               {
+                       if (!strncmp(cur->d_name, ".", sizeof(".")) ||
+                               !strncmp(cur->d_name, "..", sizeof("..")))
+                               continue;
+
+                       snprintf(next_path, MAX_STR, "%s/%s", path, cur->d_name);
+                       ret_cnt = _find_directory(next_path, json_list);
+                       count += ret_cnt;
+               }
+               else
+               {
+                       if (strstr(cur->d_name, ".json"))
+                       {
+                               snprintf(next_path, MAX_STR, "%s/%s", path, cur->d_name);
+                               *json_list = eina_list_append(*json_list, eina_stringshare_add(next_path));
+                               count++;
+                       }
+               }
+       }
+       closedir(dir);
+
+       return count;
 }
 
 
@@ -338,33 +361,25 @@ _create_ani_collection(void)
 {
        pui_int_error e = PUI_INT_ERROR_NONE;
        default_ani_info *ani_info;
-       struct dirent **files;
-       int count, i;
-       char file_path[MAX_STR] = {0, };
+       Eina_List *json_list = NULL;
+       Eina_List *l, *l_next;
+       Eina_Stringshare *path;
 
        // load and store all of animation data
 
-       if ((count = scandir(ANI_COLLECTION_DIR, &files, _scandir_filter, alphasort)) == -1) {
-               printf("Failed to get %s directory (%s)\n", ANI_COLLECTION_DIR, strerror(errno));
-               e = PUI_INT_ERROR_INVALID_RESOURCES;
-               return e;
-       }
-
-       for (i = 0; i < count; i++) {
-               snprintf(file_path, sizeof(file_path), "%s%s", ANI_COLLECTION_DIR, files[i]->d_name);
+       _find_directory(ANI_COLLECTION_DIR, &json_list);
 
-               ani_info = _read_json(file_path);
+       EINA_LIST_FOREACH_SAFE(json_list, l, l_next, path)
+       {
+               ani_info = _read_json(path);
                if (ani_info) {
                        eina_hash_add(_animations_hash, ani_info->id, ani_info);
-                       printf("Success to load %s animation\n", files[i]->d_name);
+                       pui_info("Success to load %s animation (id: %s)\n", path, ani_info->id);
                }
+               eina_stringshare_del(path);
+               json_list = eina_list_remove_list(json_list, l);
        }
 
-       for (i = 0; i < count; i++) {
-               free(files[i]);
-       }
-       free(files);
-
        //TODO
 
        return e;
@@ -437,13 +452,9 @@ _ani_create(pui_id id)
                return NULL;
        }
 
-       /* Assign each function pointer that corresponds to the given id if needed. */
-       ani_func->ani_start = _ani_start;
-       ani_func->ani_stop = _ani_stop;
-
        /* get animation info associate with the given id from animation collection */
        ani_info = get_ani_info_from_ani_collection(id);
-       
+
        if (!ani_info)
        {
                pui_err("Failed to get ani info from animation collection !\n");
@@ -452,13 +463,86 @@ _ani_create(pui_id id)
 
        /* allocate backend ani_data and return it to pui ani core */
        ani_data = (pui_backend_ani_data *)calloc(1, sizeof(pui_backend_ani_data));
-       
+
        if (!ani_data)
        {
                pui_err("Failed to allocate memory for pui backend ani data !\n");
                goto err;
        }
-       
+
+       /* Assign each function pointer that corresponds to the given id if needed. */
+       if (!strncmp(ani_info->id, "system/easy_setup", sizeof("system/easy_setup")))
+       {
+               pui_default_backend_ani_easysetup_func_set(ani_func);
+       }
+       else if (!strncmp(ani_info->id, "system/processing", sizeof("system/processing")))
+       {
+               pui_default_backend_ani_system_processing_func_set(ani_func);
+       }
+       else if (!strncmp(ani_info->id, "system/sw_update_done", sizeof("system/sw_update_done")))
+       {
+               pui_default_backend_ani_swupdatedone_func_set(ani_func);
+       }
+       else if (!strncmp(ani_info->id, "system/mic_off", sizeof("system/mic_off")))
+       {
+               pui_default_backend_ani_micoff_func_set(ani_func);
+       }
+       else if (!strncmp(ani_info->id, "voice/listening", sizeof("voice/listening")))
+       {
+               pui_default_backend_ani_listening_func_set(ani_func);
+       }
+       else if (!strncmp(ani_info->id, "voice/streaming", sizeof("voice/streaming")))
+       {
+               pui_default_backend_ani_streaming_func_set(ani_func);
+       }
+       else if (!strncmp(ani_info->id, "voice/processing", sizeof("voice/processing")))
+       {
+               pui_default_backend_ani_processing_func_set(ani_func);
+       }
+       else if (!strncmp(ani_info->id, "voice/speaking", sizeof("voice/speaking")))
+       {
+               pui_default_backend_ani_speaking_func_set(ani_func);
+       }
+       else if (!strncmp(ani_info->id, "voice/timeout", sizeof("voice/timeout")))
+       {
+               pui_default_backend_ani_timeout_func_set(ani_func);
+       }
+       else if (!strncmp(ani_info->id, "notification/normal", sizeof("notification/normal")))
+       {
+               pui_default_backend_ani_normal_func_set(ani_func);
+       }
+       else if (!strncmp(ani_info->id, "notification/emergency", sizeof("notification/emergency")))
+       {
+               pui_default_backend_ani_emergency_func_set(ani_func);
+       }
+       else if (!strncmp(ani_info->id, "notification/network_error", sizeof("notification/network_error")))
+       {
+               pui_default_backend_ani_networkerror_func_set(ani_func);
+       }
+       else if (!strncmp(ani_info->id, "notification/error", sizeof("notification/error")))
+       {
+               pui_default_backend_ani_error_func_set(ani_func);
+       }
+       else if (!strncmp(ani_info->id, "notification/alarm", sizeof("notification/alarm")))
+       {
+               pui_default_backend_ani_alarm_func_set(ani_func);
+       }
+       else if (!strncmp(ani_info->id, "bt/pairing", sizeof("bt/pairing")))
+       {
+               pui_default_backend_ani_pairing_func_set(ani_func);
+       }
+       else if (!strncmp(ani_info->id, "bt/connected", sizeof("bt/connected")))
+       {
+               pui_default_backend_ani_connected_func_set(ani_func);
+       }
+       else
+       {
+               pui_info("%s animation has no animation handler, using default handler\n", ani_info->id);
+               /* Assign each function pointer that corresponds to the given id if needed. */
+               ani_func->ani_start = _ani_start;
+               ani_func->ani_stop = _ani_stop;
+       }
+
        ani_data->ani_func = ani_func;
        ani_data->ani_info = (pui_backend_ani_info *)ani_info;