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