default_backend: add some animations - easy_setup, emergency, mic_off, normal, sw_upd...
[platform/core/uifw/libpui.git] / samples / PUI_sample.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 <stdio.h>
27 #include <unistd.h>
28
29 #define NUM_ECORE_EVENT_HANDLERS 7
30
31 /* pre-requisite to use ecore_wl2 APIs */
32 #define EFL_BETA_API_SUPPORT
33
34 #include <Ecore_Wl2.h>
35 #include <Ecore_Input.h>
36 #include <PUI.h>
37
38 int gpid;
39
40 #define debug_error(msg, ...)                                                                                   \
41         do {                                                                                                                            \
42                 fprintf(stderr, "[PUI sample][ERROR][PID:%d][%s] " msg, gpid, __FUNCTION__, ##__VA_ARGS__);     \
43         } while(0)
44
45 #define debug_info(msg, ...)                                                                                            \
46         do {                                                                                                                            \
47                 fprintf(stdout, "[PUI sample][INFO][PID:%d][%s] " msg, gpid, __FUNCTION__, ##__VA_ARGS__);      \
48         } while(0)
49
50 typedef struct _animation animation_t;
51 struct _animation
52 {
53         pui_id id;
54         pui_ani_cmd cmd;
55         int repeat;
56 };
57
58 typedef struct app_data app_data_t;
59 struct app_data
60 {
61         pui_h ph;
62         int ani_idx;
63         int n_animation;
64
65         Ecore_Wl2_Display *ewd;
66         Ecore_Wl2_Window *win;
67 };
68
69 static Eina_Array *_ecore_event_hdls = NULL;
70 static animation_t ani_collection[] = {
71         { "processing", PUI_ANI_CMD_START, -1 },
72         { "listening", PUI_ANI_CMD_START, -1 },
73         { "speaking", PUI_ANI_CMD_START, -1 },
74         { "streaming", PUI_ANI_CMD_START, -1 },
75         { "emergency", PUI_ANI_CMD_START, -1 },
76         { "easy_setup", PUI_ANI_CMD_START, -1 },
77         { "sw_update_done", PUI_ANI_CMD_START, -1 },
78         { "mic_off", PUI_ANI_CMD_START, -1 },
79         { "time_out", PUI_ANI_CMD_START, -1 },
80         { "normal", PUI_ANI_CMD_START, -1 }
81 };
82
83 pui_ani_h ani_handles[sizeof(ani_collection) / sizeof(animation_t)];
84
85 static void
86 ani_collection_play(app_data_t *app)
87 {
88         pui_error e = PUI_ERROR_NONE;
89         pui_ani_h ani_h = NULL;
90
91         if (!app->ph)
92         {
93                 debug_error("Invalid pui_h handle !\n");
94                 return;
95         }
96
97         while (!ani_handles[app->ani_idx])
98         {
99                 app->ani_idx++;
100
101                 if (++app->ani_idx >= app->n_animation)
102                         app->ani_idx = 0;
103         }
104
105         debug_info("Animation(%s) will be started !\n", pui_ani_get_id(ani_handles[app->ani_idx]));
106
107         /* play animation */
108         ani_h = ani_handles[app->ani_idx];
109         e = pui_ani_control(ani_h, PUI_ANI_CMD_START, ani_collection[app->ani_idx].repeat);
110
111         if (PUI_ERROR_NONE != e)
112         {
113                 debug_error("Failed on playing an animation ! (cmd:%d, repeat:%d)\n", PUI_ANI_CMD_START, ani_collection[app->ani_idx].repeat);
114                 return;
115         }
116
117         if (++app->ani_idx >= app->n_animation)
118                 app->ani_idx = 0;
119
120         return;
121 }
122
123 static Eina_Bool
124 _cb_key_up(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
125 {
126         app_data_t *app =  (app_data_t *)data;
127         Ecore_Event_Key *ev = event;
128
129         debug_info("KEY: name:%s, sym:%s, code:%d\n", ev->keyname, ev->key, ev->keycode);
130
131         ani_collection_play(app);
132
133         return ECORE_CALLBACK_PASS_ON;
134 }
135
136 static Eina_Bool
137 _cb_focus_in(void *data, int type EINA_UNUSED, void *event)
138 {
139         app_data_t *app =  (app_data_t *)data;
140         Ecore_Wl2_Event_Focus_In *ev = (Ecore_Wl2_Event_Focus_In *)event;
141
142         debug_info("\n");
143
144         /* TODO */
145         (void) app;
146         (void) ev;
147
148         return ECORE_CALLBACK_PASS_ON;
149 }
150
151 static Eina_Bool
152 _cb_focus_out(void *data, int type EINA_UNUSED, void *event)
153 {
154         app_data_t *app =  (app_data_t *)data;
155         Ecore_Wl2_Event_Focus_Out *ev = (Ecore_Wl2_Event_Focus_Out *)event;
156
157         debug_info("\n");
158
159         /* TODO */
160         (void) app;
161         (void) ev;
162
163         return ECORE_CALLBACK_PASS_ON;
164 }
165
166 static Eina_Bool
167 _cb_visibility_change(void *data, int type EINA_UNUSED, void *event)
168 {
169         app_data_t *app = (app_data_t *)data;
170         Ecore_Wl2_Event_Window_Visibility_Change *ev;
171
172         (void) app;
173         ev = event;
174         debug_info("Visibility change (window=0x%x, fully_obscured=%d)\n", ev->win, ev->fully_obscured);
175
176         return ECORE_CALLBACK_PASS_ON;
177 }
178
179 static Eina_Bool
180 _cb_ani_started(void *data, int type EINA_UNUSED, void *event)
181 {
182         app_data_t *app = (app_data_t *)data;
183         PUI_Event_Animation_Status *ev;
184
185         (void) app;
186         ev = event;
187         debug_info("[%s] ani id=%s, status=%d, window=0x%x\n", __FUNCTION__, pui_ani_get_id(ev->ani_h), ev->status, ev->win);
188
189         return ECORE_CALLBACK_PASS_ON;
190 }
191
192 static Eina_Bool
193 _cb_ani_stopped(void *data, int type EINA_UNUSED, void *event)
194 {
195         app_data_t *app = (app_data_t *)data;
196         PUI_Event_Animation_Status *ev;
197
198         (void) app;
199         ev = event;
200         debug_info("[%s] ani id=%s, status=%d, window=0x%x\n", __FUNCTION__, pui_ani_get_id(ev->ani_h), ev->status, ev->win);
201
202         /* decrease animation idx for starting from stopped animation */
203         app->ani_idx--;
204
205         return ECORE_CALLBACK_PASS_ON;
206 }
207
208 static Eina_Bool
209 _cb_ani_ready_to_start(void *data, int type EINA_UNUSED, void *event)
210 {
211         app_data_t *app = (app_data_t *)data;
212         PUI_Event_Animation_Status *ev;
213
214         (void) app;
215         ev = event;
216         debug_info("[%s] ani id=%s, status=%d, window=0x%x\n", __FUNCTION__, pui_ani_get_id(ev->ani_h), ev->status, ev->win);
217
218         /* start animation */
219         ani_collection_play(app);
220
221         return ECORE_CALLBACK_PASS_ON;
222 }
223
224 static void
225 event_handlers_init(app_data_t *app)
226 {
227         Ecore_Event_Handler *h = NULL;
228         _ecore_event_hdls = eina_array_new(NUM_ECORE_EVENT_HANDLERS);
229
230         h = ecore_event_handler_add(ECORE_EVENT_KEY_UP, _cb_key_up, app);
231         eina_array_push(_ecore_event_hdls, h);
232
233         h = ecore_event_handler_add(ECORE_WL2_EVENT_FOCUS_IN, _cb_focus_in, app);
234         eina_array_push(_ecore_event_hdls, h);
235
236         h = ecore_event_handler_add(ECORE_WL2_EVENT_FOCUS_OUT, _cb_focus_out, app);
237         eina_array_push(_ecore_event_hdls, h);
238
239         h = ecore_event_handler_add(ECORE_WL2_EVENT_WINDOW_VISIBILITY_CHANGE, _cb_visibility_change, app);
240         eina_array_push(_ecore_event_hdls, h);
241
242         h = ecore_event_handler_add(PUI_EVENT_ANI_STARTED, _cb_ani_started, app);
243         eina_array_push(_ecore_event_hdls, h);
244
245         h = ecore_event_handler_add(PUI_EVENT_ANI_STOPPED, _cb_ani_stopped, app);
246         eina_array_push(_ecore_event_hdls, h);
247
248         h = ecore_event_handler_add(PUI_EVENT_ANI_READY_TO_START, _cb_ani_ready_to_start, app);
249         eina_array_push(_ecore_event_hdls, h);
250 }
251
252 int main()
253 {
254         app_data_t *app = NULL;
255         const char *socket_name = NULL;
256
257         gpid = getpid();
258
259         if (!ecore_wl2_init())
260         {
261                 debug_error("Failed to init ecore wl2 !\n");
262                 return EXIT_SUCCESS;
263         }
264
265         socket_name = getenv("WAYLAND_DISPLAY");
266
267         if (!socket_name)
268                 socket_name = "wayland-0";
269
270         app = (app_data_t *)calloc(1, sizeof(app_data_t));
271
272         if (!app)
273         {
274                 debug_error("Failed to allocate app data !\n");
275                 goto err;
276         }
277
278         app->ani_idx = 0;
279         app->ewd = ecore_wl2_display_connect(socket_name);
280
281         if (!app->ewd)
282         {
283                 debug_error("Failed to connect to display !\n");
284                 goto err;
285         }
286
287         app->win = ecore_wl2_window_new(app->ewd, NULL, 0, 0, 1, 1);
288
289         if (!app->win)
290         {
291                 debug_error("Failed to create a window !\n");
292                 goto err;
293         }
294
295         ecore_wl2_window_alpha_set(app->win, EINA_FALSE);
296         ecore_wl2_window_show(app->win);
297         ecore_wl2_window_commit(app->win, EINA_TRUE);
298         ecore_wl2_window_activate(app->win);
299
300         if (!pui_init())
301         {
302                 debug_error("Failed to init pui !\n");
303                 goto err;
304         }
305
306         app->ph = pui_create(app->win);
307
308         if (!app->ph)
309         {
310                 debug_error("Failed to create PUI handle !\n");
311                 goto err;
312         }
313
314         app->n_animation = sizeof(ani_collection) / sizeof(animation_t);
315
316         for(int i=0;i<app->n_animation;i++)
317         {
318                 ani_handles[i] = pui_ani_create(app->ph, ani_collection[i].id);
319
320                 if (!ani_handles[i])
321                         debug_error("Failed to create pui ani handle !(id:%s)\n", ani_collection[i].id);
322         }
323
324         event_handlers_init(app);
325
326         ecore_main_loop_begin();
327 err:
328         if (app->n_animation > 0)
329         {
330                 for(int i=0;i<app->n_animation;i++)
331                 {
332                         pui_ani_destroy(ani_handles[i]);
333                 }
334         }
335
336         if (app->ph)
337                 pui_destroy(app->ph);
338
339         pui_shutdown();
340         ecore_wl2_shutdown();
341
342         return EXIT_SUCCESS;
343 }