pui_sample : add PUI event handlers, creates pui ani handlers @ intial time
[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         { "alarm calm", PUI_ANI_CMD_START, 1 },
72         { "bixby listening", PUI_ANI_CMD_START, -1 },
73         { "bixby speaking", PUI_ANI_CMD_START, -1 },
74         { "bixby_error", PUI_ANI_CMD_START, 1 },
75         { "blinking", PUI_ANI_CMD_START, -1 },
76         { "notification", PUI_ANI_CMD_START, -1 },
77 };
78
79 pui_ani_h ani_handles[sizeof(ani_collection) / sizeof(animation_t)];
80
81 static void
82 ani_collection_play(app_data_t *app)
83 {
84         pui_error e = PUI_ERROR_NONE;
85         pui_ani_h ani_h = NULL;
86
87         if (!app->ph)
88         {
89                 debug_error("Invalid pui_h handle !\n");
90                 return;
91         }
92
93         while (!ani_handles[app->ani_idx])
94         {
95                 app->ani_idx++;
96
97                 if (++app->ani_idx >= app->n_animation)
98                         app->ani_idx = 0;
99         }
100
101         debug_info("Animation(%s) will be started !\n", pui_ani_get_id(ani_handles[app->ani_idx]));
102
103         /* play animation */
104         ani_h = ani_handles[app->ani_idx];
105         e = pui_ani_control(ani_h, PUI_ANI_CMD_START, ani_collection[app->ani_idx].repeat);
106
107         if (PUI_ERROR_NONE != e)
108         {
109                 debug_error("Failed on playing an animation ! (cmd:%d, repeat:%d)\n", PUI_ANI_CMD_START, ani_collection[app->ani_idx].repeat);
110                 return;
111         }
112
113         if (++app->ani_idx >= app->n_animation)
114                 app->ani_idx = 0;
115
116         return;
117 }
118
119 static Eina_Bool
120 _cb_key_up(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
121 {
122         app_data_t *app =  (app_data_t *)data;
123         Ecore_Event_Key *ev = event;
124
125         debug_info("KEY: name:%s, sym:%s, code:%d\n", ev->keyname, ev->key, ev->keycode);
126
127         ani_collection_play(app);
128
129         return ECORE_CALLBACK_PASS_ON;
130 }
131
132 static Eina_Bool
133 _cb_focus_in(void *data, int type EINA_UNUSED, void *event)
134 {
135         app_data_t *app =  (app_data_t *)data;
136         Ecore_Wl2_Event_Focus_In *ev = (Ecore_Wl2_Event_Focus_In *)event;
137
138         debug_info("\n");
139
140         /* TODO */
141         (void) app;
142         (void) ev;
143
144         return ECORE_CALLBACK_PASS_ON;
145 }
146
147 static Eina_Bool
148 _cb_focus_out(void *data, int type EINA_UNUSED, void *event)
149 {
150         app_data_t *app =  (app_data_t *)data;
151         Ecore_Wl2_Event_Focus_Out *ev = (Ecore_Wl2_Event_Focus_Out *)event;
152
153         debug_info("\n");
154
155         /* TODO */
156         (void) app;
157         (void) ev;
158
159         return ECORE_CALLBACK_PASS_ON;
160 }
161
162 static Eina_Bool
163 _cb_visibility_change(void *data, int type EINA_UNUSED, void *event)
164 {
165         app_data_t *app = (app_data_t *)data;
166         Ecore_Wl2_Event_Window_Visibility_Change *ev;
167
168         (void) app;
169         ev = event;
170         debug_info("Visibility change (window=0x%x, fully_obscured=%d)\n", ev->win, ev->fully_obscured);
171
172         return ECORE_CALLBACK_PASS_ON;
173 }
174
175 static Eina_Bool
176 _cb_ani_started(void *data, int type EINA_UNUSED, void *event)
177 {
178         app_data_t *app = (app_data_t *)data;
179         PUI_Event_Animation_Status *ev;
180
181         (void) app;
182         ev = event;
183         debug_info("[%s] ani id=%s, status=%d, window=0x%x\n", __FUNCTION__, pui_ani_get_id(ev->ani_h), ev->status, ev->win);
184
185         return ECORE_CALLBACK_PASS_ON;
186 }
187
188 static Eina_Bool
189 _cb_ani_stopped(void *data, int type EINA_UNUSED, void *event)
190 {
191         app_data_t *app = (app_data_t *)data;
192         PUI_Event_Animation_Status *ev;
193
194         (void) app;
195         ev = event;
196         debug_info("[%s] ani id=%s, status=%d, window=0x%x\n", __FUNCTION__, pui_ani_get_id(ev->ani_h), ev->status, ev->win);
197
198         /* decrease animation idx for starting from stopped animation */
199         app->ani_idx--;
200
201         return ECORE_CALLBACK_PASS_ON;
202 }
203
204 static Eina_Bool
205 _cb_ani_ready_to_start(void *data, int type EINA_UNUSED, void *event)
206 {
207         app_data_t *app = (app_data_t *)data;
208         PUI_Event_Animation_Status *ev;
209
210         (void) app;
211         ev = event;
212         debug_info("[%s] ani id=%s, status=%d, window=0x%x\n", __FUNCTION__, pui_ani_get_id(ev->ani_h), ev->status, ev->win);
213
214         /* start animation */
215         ani_collection_play(app);
216
217         return ECORE_CALLBACK_PASS_ON;
218 }
219
220 static void
221 event_handlers_init(app_data_t *app)
222 {
223         Ecore_Event_Handler *h = NULL;
224         _ecore_event_hdls = eina_array_new(NUM_ECORE_EVENT_HANDLERS);
225
226         h = ecore_event_handler_add(ECORE_EVENT_KEY_UP, _cb_key_up, app);
227         eina_array_push(_ecore_event_hdls, h);
228
229         h = ecore_event_handler_add(ECORE_WL2_EVENT_FOCUS_IN, _cb_focus_in, app);
230         eina_array_push(_ecore_event_hdls, h);
231
232         h = ecore_event_handler_add(ECORE_WL2_EVENT_FOCUS_OUT, _cb_focus_out, app);
233         eina_array_push(_ecore_event_hdls, h);
234
235         h = ecore_event_handler_add(ECORE_WL2_EVENT_WINDOW_VISIBILITY_CHANGE, _cb_visibility_change, app);
236         eina_array_push(_ecore_event_hdls, h);
237
238         h = ecore_event_handler_add(PUI_EVENT_ANI_STARTED, _cb_ani_started, app);
239         eina_array_push(_ecore_event_hdls, h);
240
241         h = ecore_event_handler_add(PUI_EVENT_ANI_STOPPED, _cb_ani_stopped, app);
242         eina_array_push(_ecore_event_hdls, h);
243
244         h = ecore_event_handler_add(PUI_EVENT_ANI_READY_TO_START, _cb_ani_ready_to_start, app);
245         eina_array_push(_ecore_event_hdls, h);
246 }
247
248 int main()
249 {
250         app_data_t *app = NULL;
251         const char *socket_name = NULL;
252
253         gpid = getpid();
254
255         if (!ecore_wl2_init())
256         {
257                 debug_error("Failed to init ecore wl2 !\n");
258                 return EXIT_SUCCESS;
259         }
260
261         socket_name = getenv("WAYLAND_DISPLAY");
262
263         if (!socket_name)
264                 socket_name = "wayland-0";
265
266         app = (app_data_t *)calloc(1, sizeof(app_data_t));
267
268         if (!app)
269         {
270                 debug_error("Failed to allocate app data !\n");
271                 goto err;
272         }
273
274         app->ani_idx = 0;
275         app->ewd = ecore_wl2_display_connect(socket_name);
276
277         if (!app->ewd)
278         {
279                 debug_error("Failed to connect to display !\n");
280                 goto err;
281         }
282
283         app->win = ecore_wl2_window_new(app->ewd, NULL, 0, 0, 1, 1);
284
285         if (!app->win)
286         {
287                 debug_error("Failed to create a window !\n");
288                 goto err;
289         }
290
291         ecore_wl2_window_alpha_set(app->win, EINA_FALSE);
292         ecore_wl2_window_show(app->win);
293         ecore_wl2_window_commit(app->win, EINA_TRUE);
294         ecore_wl2_window_activate(app->win);
295
296         if (!pui_init())
297         {
298                 debug_error("Failed to init pui !\n");
299                 goto err;
300         }
301
302         app->ph = pui_create(app->win);
303
304         if (!app->ph)
305         {
306                 debug_error("Failed to create PUI handle !\n");
307                 goto err;
308         }
309
310         app->n_animation = sizeof(ani_collection) / sizeof(animation_t);
311
312         for(int i=0;i<app->n_animation;i++)
313         {
314                 ani_handles[i] = pui_ani_create(app->ph, ani_collection[i].id);
315
316                 if (!ani_handles[i])
317                         debug_error("Failed to create pui ani handle !(id:%s)\n", ani_collection[i].id);
318         }
319
320         event_handlers_init(app);
321
322         ecore_main_loop_begin();
323 err:
324         if (app->n_animation > 0)
325         {
326                 for(int i=0;i<app->n_animation;i++)
327                 {
328                         pui_ani_destroy(ani_handles[i]);
329                 }
330         }
331
332         if (app->ph)
333                 pui_destroy(app->ph);
334
335         pui_shutdown();
336         ecore_wl2_shutdown();
337
338         return EXIT_SUCCESS;
339 }