renew pui prototype again
[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         { "bixby_listening", PUI_ANI_CMD_START, 1 },
40         { "bixby_processing", PUI_ANI_CMD_START, -1 },
41         { "bixby_speaking", PUI_ANI_CMD_START, -1 },
42         { "bixby_error", PUI_ANI_CMD_START, 1 },
43         { "alarm", 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         /* stop animation running already */
53         e = pui_ani_control(app->ani_h, PUI_ANI_CMD_STOP, 0);
54
55         if (PUI_ERROR_NONE != e)
56         {
57                 debug_error("Failed on stopping an animation !(cmd:%d, repeat:%d)\n", PUI_ANI_CMD_STOP, 0);
58                 return;
59         }
60
61         debug_info("Animation(%s) will be stopped !\n", pui_ani_get_id(app->ani_h));
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         ani_h = pui_ani_create(app->ph, ani_collection[ani_idx].id);
87
88         if (!ani_h)
89         {
90                 debug_error("Failed to create new PUI animation handle !\n");
91                 return;
92         }
93
94         if (app->ani_h)
95         {
96                 /* stop animation running already */
97                 e = pui_ani_control(app->ani_h, PUI_ANI_CMD_STOP, 0);
98
99                 if (PUI_ERROR_NONE != e)
100                 {
101                         debug_error("Failed on stopping an animation !(cmd:%d, repeat:%d)\n", PUI_ANI_CMD_STOP, 0);
102                         return;
103                 }
104
105                 debug_info("Animation(%s) will be stopped !\n", pui_ani_get_id(ani_h));
106
107                 app->ani_h = NULL;
108         }
109
110         app->ani_h = ani_h;
111
112         /* play animation */
113         e = pui_ani_control(app->ani_h, PUI_ANI_CMD_START, ani_collection[ani_idx].repeat);
114
115         if (PUI_ERROR_NONE != e)
116         {
117                 debug_error("Failed on playing an animation ! (cmd:%d, repeat:%d)\n", PUI_ANI_CMD_START, ani_collection[ani_idx].repeat);
118                 return;
119         }
120
121         debug_info("Animation(%s) will be started !\n", pui_ani_get_id(app->ani_h));
122
123         if (++ani_idx >= n_animation)
124                 ani_idx = 0;
125
126         return;
127 }
128
129 static Eina_Bool
130 _cb_key_up(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
131 {
132         app_data_t *app =  (app_data_t *)data;
133         Ecore_Event_Key *ev = event;
134
135         debug_info("KEY: name:%s, sym:%s, code:%d\n", ev->keyname, ev->key, ev->keycode);
136
137         ani_collection_play(app);
138
139         return ECORE_CALLBACK_PASS_ON;
140 }
141
142 static Eina_Bool
143 _cb_focus_in(void *data, int type EINA_UNUSED, void *event)
144 {
145         app_data_t *app =  (app_data_t *)data;
146         Ecore_Wl2_Event_Focus_In *ev = (Ecore_Wl2_Event_Focus_In *)event;
147
148         debug_info("\n");
149
150         /* TODO */
151         (void) app;
152         (void) ev;
153
154         return ECORE_CALLBACK_PASS_ON;
155 }
156
157 static Eina_Bool
158 _cb_focus_out(void *data, int type EINA_UNUSED, void *event)
159 {
160         app_data_t *app =  (app_data_t *)data;
161         Ecore_Wl2_Event_Focus_Out *ev = (Ecore_Wl2_Event_Focus_Out *)event;
162
163         debug_info("\n");
164
165         /* TODO */
166         (void) app;
167         (void) ev;
168
169         return ECORE_CALLBACK_PASS_ON;
170 }
171
172 static Eina_Bool
173 _cb_visibility_change(void *data, int type EINA_UNUSED, void *event)
174 {
175         app_data_t *app = (app_data_t *)data;
176         Ecore_Wl2_Event_Window_Visibility_Change *ev;
177
178         ev = event;
179
180         debug_info("Visibility change (window=0x%x, fully_obscured=%d)\n", ev->win, ev->fully_obscured);
181
182         if (ev->fully_obscured)
183         {
184                 debug_info("Loose LED control !\n");
185                 ani_stop(app);
186         }
187         else
188         {
189                 debug_info("Gain LED control !\n");
190                 ani_collection_play(app);
191         }
192
193         return ECORE_CALLBACK_PASS_ON;
194 }
195
196 static void
197 event_handlers_init(app_data_t *app)
198 {
199         Ecore_Event_Handler *h = NULL;
200         _ecore_event_hdls = eina_array_new(NUM_ECORE_EVENT_HANDLERS);
201
202         h = ecore_event_handler_add(ECORE_EVENT_KEY_UP, _cb_key_up, app);
203         eina_array_push(_ecore_event_hdls, h);
204
205         h = ecore_event_handler_add(ECORE_WL2_EVENT_FOCUS_IN, _cb_focus_in, app);
206         eina_array_push(_ecore_event_hdls, h);
207
208         h = ecore_event_handler_add(ECORE_WL2_EVENT_FOCUS_OUT, _cb_focus_out, app);
209         eina_array_push(_ecore_event_hdls, h);
210
211         h = ecore_event_handler_add(ECORE_WL2_EVENT_WINDOW_VISIBILITY_CHANGE, _cb_visibility_change, app);
212         eina_array_push(_ecore_event_hdls, h);
213 }
214
215 int main()
216 {
217         app_data_t *app = NULL;
218         const char *socket_name = NULL;
219
220         if (!ecore_wl2_init())
221         {
222                 fprintf(stderr, "Failed to init ecore wl2 !\n");
223                 return EXIT_SUCCESS;
224         }
225
226         socket_name = getenv("WAYLAND_DISPLAY");
227
228         if (!socket_name)
229                 socket_name = "wayland-0";
230
231         app = (app_data_t *)calloc(1, sizeof(app_data_t));
232
233         if (!app)
234         {
235                 debug_error("Failed to allocate app data !\n");
236                 goto err;
237         }
238
239         app->ewd = ecore_wl2_display_connect(socket_name);
240
241         if (!app->ewd)
242         {
243                 debug_error("Failed to connect to display !\n");
244                 goto err;
245         }
246
247         app->win = ecore_wl2_window_new(app->ewd, NULL, 0, 0, 1, 1);
248
249         if (!app->win)
250         {
251                 debug_error("Failed to create a window !\n");
252                 goto err;
253         }
254
255         ecore_wl2_window_alpha_set(app->win, EINA_FALSE);
256         ecore_wl2_window_show(app->win);
257         ecore_wl2_window_commit(app->win, EINA_TRUE);
258         ecore_wl2_window_activate(app->win);
259
260         if (!pui_init())
261         {
262                 debug_error("Failed to init pui !\n");
263                 goto err;
264         }
265
266         app->ph = pui_create(app->win);
267
268         if (!app->ph)
269         {
270                 debug_error("Failed to create PUI handle !\n");
271                 goto err;
272         }
273
274         event_handlers_init(app);
275
276         ecore_main_loop_begin();
277 err:
278         if (app->ani_h)
279                 pui_ani_destroy(app->ani_h);
280         if (app->ph)
281                 pui_destroy(app->ph);
282
283         pui_shutdown();
284         ecore_wl2_shutdown();
285
286         return EXIT_SUCCESS;
287 }