backend: add default clear animations
[platform/core/uifw/libpui.git] / backends / default / default_ani_clear_immediate.c
1 /*
2  * Copyright © 2019 Samsung Electronics co., Ltd. All Rights Reserved.
3  *
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:
11  *
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.
15  *
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
23  * SOFTWARE.
24  */
25
26 #include "../default_backend.h"
27 static void
28 _ani_backend_clear_immediate_get_led_rgb(default_frame_info_t *frame, int led_idx, unsigned int *r, unsigned int *g, unsigned int *b)
29 {
30         if (!frame) return;
31         if (!r || !g || !b) return;
32         if (led_idx > frame->num_led) return;
33
34         *r = (frame->leds[led_idx].color & LED_MASK_RED) >> 16;
35         *g = (frame->leds[led_idx].color & LED_MASK_GREEN) >> 8;
36         *b = (frame->leds[led_idx].color & LED_MASK_BLUE);
37 }
38
39 static default_frame_info_t *ani_frame = NULL;
40
41 static default_frame_info_t *
42 _ani_clear_immediate_get_frame(default_ani_info *ani_info)
43 {
44         for (int i = 0; i < ani_frame->num_led; i++)
45         {
46                 ani_frame->leds[i].color = 0x0;
47         }
48
49         ani_info->repeat_cur = 1;
50
51         return ani_frame;
52 }
53
54 static pui_bool
55 _ani_clear_immediate_frame_cb(void *data, int serial)
56 {
57         pui_int_error e = PUI_INT_ERROR_NONE;
58         pui_ani_t *ani = (pui_ani_t *)data;
59         pui_backend_ani_data *ani_data = NULL;
60         pui_ani_control_buffer *buffer = NULL;
61         default_frame_info_t *frame;
62         unsigned int r = 0x0, g = 0x0, b = 0x0;
63
64         ani_data = pui_backend_ani_get_ani_data(ani);
65         default_ani_info *ani_info = (default_ani_info *)ani_data->ani_info;
66
67         /* TODO : make use of ani_info */
68         //(void) ani_info;
69
70         buffer = pui_backend_ani_get_buffer(ani);
71         if (!buffer) {
72                 pui_err("Failed to get buffer for animation\n");
73                 return (pui_bool)0;
74         }
75
76         frame = _ani_clear_immediate_get_frame(ani_info);
77         for(int i = 0; i<12; i++)
78         {
79                 _ani_backend_clear_immediate_get_led_rgb(frame, i, &r, &g, &b);
80                 buffer->ptr[4*i] = 0;
81                 buffer->ptr[4*i + 1] = b; /* BLUE */
82                 buffer->ptr[4*i + 2] = g; /* GREEN */
83                 buffer->ptr[4*i + 3] = r; /* RED */
84         }
85         backend_util_cleanup_frame(frame);
86
87         e = pui_backend_ani_set_buffer(ani, buffer);
88
89         if (e != PUI_INT_ERROR_NONE)
90         {
91                 pui_err("Failed on setting buffer on animation !(e=%d)\n", e);
92                 return (pui_bool)0;
93         }
94
95         e = pui_backend_ani_update(ani);
96
97         if (e != PUI_INT_ERROR_NONE)
98         {
99                 pui_err("Failed on updating animation !(e=%d)\n", e);
100                 return (pui_bool)0;
101         }
102
103         pui_info("... update (serial=%d), (repeat| cur: %d, want: %d)\n",
104                 serial, ani_info->repeat_cur, ani_info->repeat);
105
106         if (ani_info->repeat >= 0 &&
107                 ani_info->repeat_cur >= ani_info->repeat)
108         {
109                 ani_data->ani_func->ani_stop(ani, EINA_FALSE);
110         }
111
112         return (pui_bool)1;
113 }
114
115 pui_error
116 _ani_clear_immediate_start(pui_ani_t *ani, int repeat)
117 {
118         pui_bool ret = 0;
119         pui_int_error e = PUI_INT_ERROR_NONE;
120         pui_backend_ani_data *ani_data = NULL;
121
122         ani_data = pui_backend_ani_get_ani_data(ani);
123         default_ani_info *info = (default_ani_info *)ani_data->ani_info;
124
125         pui_info("... info->id: %s, repeat : %d, interval: %d\n", info->id, repeat, info->interval);
126
127         pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STARTED);
128
129         info->repeat = 1;
130
131         info->key_frame_cur = 0;
132         info->frame_idx = 0;
133         info->repeat_cur = 0;
134
135         if (!ani_frame)
136                 ani_frame = backend_util_alloc_frame(info);
137         ERROR_CHECK(ani_frame, return PUI_INT_ERROR_INVALID_RESOURCES, "Failed to alloc memory for frame\n");
138
139         ret = pui_backend_ani_add_frame_cb(ani, _ani_clear_immediate_frame_cb, info->interval / 1000.0);
140         if (!ret)
141         {
142                 pui_err("Failed to add frame callback !\n");
143                 e = PUI_INT_ERROR_INVALID_RESOURCES;
144         }
145
146         return e;
147 }
148
149 pui_error
150 _ani_clear_immediate_stop(pui_ani_t *ani, pui_bool force)
151 {
152         pui_int_error e = PUI_INT_ERROR_NONE;
153         pui_backend_ani_data *ani_data = NULL;
154
155         ani_data = pui_backend_ani_get_ani_data(ani);
156         default_ani_info *info = (default_ani_info *)ani_data->ani_info;
157
158         //TODO
159         (void) info;
160
161         pui_info("... info->id: %s, force=%d\n", info->id, force);
162
163         pui_backend_ani_remove_frame_cb(ani);
164
165         if (force)
166                 pui_backend_ani_status_update(ani, PUI_ANI_STATUS_PAUSED);
167         else
168                 pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STOPPED);
169
170         if (ani_frame)
171         {
172                 backend_util_free_frame(ani_frame);
173                 ani_frame = NULL;
174         }
175
176         return e;
177 }
178
179 void
180 pui_default_backend_ani_clear_immediate_func_set(pui_backend_ani_func *func)
181 {
182         if (!func) return;
183
184         func->ani_start = _ani_clear_immediate_start;
185         func->ani_stop = _ani_clear_immediate_stop;
186 }