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