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