Control iconify state according to resume/pause
[platform/core/appfw/appcore-widget.git] / src / efl_base / widget_app.c
1 /*
2  * Copyright (c) 2015 - 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <stdlib.h>
19 #include <glib.h>
20
21 #include <bundle.h>
22 #include <aul.h>
23 #include <aul_widget.h>
24 #include <dlog.h>
25 #include <Elementary.h>
26 #include <widget_errno.h>
27 #include <widget_instance.h>
28
29 #include "widget_base.h"
30 #include "widget_app.h"
31 #include "widget-log.h"
32 #include "widget-private.h"
33 #include "widget_app_internal.h"
34
35 #ifdef LOG_TAG
36 #undef LOG_TAG
37 #endif
38
39 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
40 #define ICONIFY_TIMEOUT 500
41
42 struct instance_data {
43         Evas_Object *win;
44         guint iconify_timer;
45         bool is_iconified;
46 };
47
48 struct app_cb_info {
49         widget_app_lifecycle_callback_s *callback;
50         void *user_data;
51 };
52
53 struct app_class_cb_info {
54         widget_instance_lifecycle_callback_s callback;
55         void *user_data;
56 };
57
58 static GList *__class_data_list;
59
60 static int __class_resize(widget_base_instance_h instance_h, int w, int h,
61                 void *class_data)
62 {
63         int ret = 0;
64         struct instance_data *data;
65         struct app_class_cb_info *callback_data =
66                         (struct app_class_cb_info *)class_data;
67
68         widget_base_class_on_resize(instance_h, w, h);
69         data = (struct instance_data *)
70                         widget_base_context_get_user_data(instance_h);
71
72         if (!data) {
73                 _E("widget_base_context_get_user_data() returns null");
74
75                 return -1;
76         }
77
78         if (data->win)
79                 evas_object_resize(data->win, w, h);
80         else
81                 _E("unable to find window");
82
83         if (callback_data && callback_data->callback.resize) {
84                 ret = callback_data->callback.resize(
85                                 (widget_context_h)instance_h,
86                                 w, h, callback_data->user_data);
87         }
88
89         return ret;
90 }
91
92 static int __class_update(widget_base_instance_h instance_h, bundle *content,
93                 int force, void *class_data)
94 {
95         int ret = 0;
96         struct app_class_cb_info *callback_data =
97                         (struct app_class_cb_info *)class_data;
98
99         widget_base_class_on_update(instance_h, content, force);
100         if (callback_data && callback_data->callback.update) {
101                 ret = callback_data->callback.update(
102                                 (widget_context_h)instance_h,
103                                 content, force, callback_data->user_data);
104         }
105
106         return ret;
107 }
108
109 static int __class_create(widget_base_instance_h instance_h, bundle *content,
110                 int w, int h, void *class_data)
111 {
112         int ret = -1;
113         struct app_class_cb_info *callback_data =
114                         (struct app_class_cb_info *)class_data;
115
116         widget_base_class_on_create(instance_h, content, w, h);
117         if (callback_data && callback_data->callback.create) {
118                 ret = callback_data->callback.create(
119                                 (widget_context_h)instance_h,
120                                 content, w, h, callback_data->user_data);
121                 aul_widget_write_log(LOG_TAG, "[%s:%d]  ret : %d",
122                         __FUNCTION__, __LINE__, ret);
123         }
124         return ret;
125 }
126
127 static int __class_destroy(widget_base_instance_h instance_h,
128                 widget_base_destroy_type_e reason, bundle *content,
129                 void *class_data)
130 {
131         int ret = 0;
132         struct instance_data *data;
133         struct app_class_cb_info *callback_data =
134                         (struct app_class_cb_info *)class_data;
135
136         if (callback_data && callback_data->callback.destroy) {
137                 ret = callback_data->callback.destroy(
138                                 (widget_context_h)instance_h,
139                                 reason, content, callback_data->user_data);
140                 aul_widget_write_log(LOG_TAG, "[%s:%d]  ret : %d",
141                         __FUNCTION__, __LINE__, ret);
142         }
143
144         data = (struct instance_data *)widget_base_context_get_user_data(instance_h);
145         if (data != NULL) {
146                 widget_base_context_set_user_data(instance_h, NULL);
147                 if (data->iconify_timer > 0)
148                         g_source_remove(data->iconify_timer);
149                 free(data);
150         }
151
152         widget_base_class_on_destroy(instance_h, reason, content);
153
154         return ret;
155 }
156
157 static gboolean __iconify_timeout_cb(gpointer user_data)
158 {
159         struct instance_data *data = user_data;
160         Ecore_Wl2_Window *win = ecore_evas_wayland2_window_get(
161                         ecore_evas_ecore_evas_get(evas_object_evas_get(data->win)));
162
163         if (win) {
164                 ecore_wl2_window_iconified_set(win, EINA_TRUE);
165                 data->is_iconified = true;
166                 _D("set iconify true");
167         }
168
169         data->iconify_timer = 0;
170
171         return G_SOURCE_REMOVE;
172 }
173
174 static int __class_pause(widget_base_instance_h instance_h, void *class_data)
175 {
176         int ret = 0;
177         struct app_class_cb_info *callback_data =
178                         (struct app_class_cb_info *)class_data;
179         struct instance_data *data = (struct instance_data *)
180                         widget_base_context_get_user_data(instance_h);
181
182         if (data->iconify_timer > 0)
183                 g_source_remove(data->iconify_timer);
184
185         data->iconify_timer = g_timeout_add(ICONIFY_TIMEOUT,
186                         __iconify_timeout_cb, data);
187
188         widget_base_class_on_pause(instance_h);
189         if (callback_data && callback_data->callback.pause) {
190                 ret = callback_data->callback.pause(
191                                 (widget_context_h)instance_h,
192                                 callback_data->user_data);
193         }
194
195         return ret;
196 }
197
198 static int __class_resume(widget_base_instance_h instance_h, void *class_data)
199 {
200         int ret = 0;
201         struct app_class_cb_info *callback_data =
202                         (struct app_class_cb_info *)class_data;
203         Ecore_Wl2_Window *win;
204         struct instance_data *data = (struct instance_data *)
205                         widget_base_context_get_user_data(instance_h);
206
207         if (data->iconify_timer > 0) {
208                 g_source_remove(data->iconify_timer);
209                 data->iconify_timer = 0;
210         }
211
212         if (data->is_iconified) {
213                 win = ecore_evas_wayland2_window_get(
214                         ecore_evas_ecore_evas_get(evas_object_evas_get(data->win)));
215                 if (win) {
216                         ecore_wl2_window_iconified_set(win, EINA_FALSE);
217                         data->is_iconified = false;
218                         _D("set iconify false");
219                 }
220         }
221
222         widget_base_class_on_resume(instance_h);
223         if (callback_data && callback_data->callback.resume) {
224                 ret = callback_data->callback.resume(
225                                 (widget_context_h)instance_h,
226                                 callback_data->user_data);
227         }
228
229         return ret;
230 }
231
232 static int __widget_app_create(void *data)
233 {
234         struct app_cb_info *cb_info = (struct app_cb_info *)data;
235         widget_app_lifecycle_callback_s *callback;
236
237         widget_base_on_create();
238         if (cb_info && cb_info->callback && cb_info->callback->create) {
239                 callback = cb_info->callback;
240                 if (callback->create(cb_info->user_data) == NULL) {
241                         _D("fail to create widget");
242                         return -1;
243                 }
244                 _D("widget app is created");
245                 aul_widget_write_log(LOG_TAG, "[%s:%d]", __FUNCTION__, __LINE__);
246                 return 0;
247         }
248
249         return -1;
250 }
251
252 static int __widget_app_terminate(void *data)
253 {
254         struct app_cb_info *cb_info = (struct app_cb_info *)data;
255         widget_app_lifecycle_callback_s *callback;
256
257         if (cb_info && cb_info->callback && cb_info->callback->terminate) {
258                 callback = cb_info->callback;
259                 callback->terminate(cb_info->user_data);
260                 widget_base_on_terminate();
261                 _D("widget app is terminated");
262                 aul_widget_write_log(LOG_TAG, "[%s:%d]", __FUNCTION__, __LINE__);
263                 return 0;
264         }
265
266         widget_base_on_terminate();
267
268         return -1;
269 }
270
271 static void __widget_app_init(int argc, char **argv, void *data)
272 {
273         elm_init(argc, argv);
274 }
275
276 static void __widget_app_finish(void)
277 {
278         elm_shutdown();
279 }
280
281 static void __widget_app_run(void *data)
282 {
283         elm_run();
284 }
285
286 static void __widget_app_exit(void *data)
287 {
288         elm_exit();
289 }
290
291 EXPORT_API int widget_app_main(int argc, char **argv,
292                 widget_app_lifecycle_callback_s *callback, void *user_data)
293 {
294         widget_base_ops ops;
295         struct app_cb_info cb_info;
296         int r;
297
298         if (argc <= 0 || argv == NULL || callback == NULL)
299                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
300                                 __FUNCTION__, NULL);
301
302         if (callback->create == NULL)
303                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
304                                 __FUNCTION__,
305                                 "widget_app_create_cb() callback must be "
306                                 "registered");
307
308         ops.create = __widget_app_create;
309         ops.terminate = __widget_app_terminate;
310         ops.init = __widget_app_init;
311         ops.finish = __widget_app_finish;
312         ops.run = __widget_app_run;
313         ops.exit = __widget_app_exit;
314
315         cb_info.callback = callback;
316         cb_info.user_data = user_data;
317
318         r = widget_base_init(ops, argc, argv, &cb_info);
319         widget_base_fini();
320
321         if (__class_data_list) {
322                 g_list_free_full(__class_data_list, free);
323                 __class_data_list = NULL;
324         }
325
326         return r;
327 }
328
329 EXPORT_API int widget_app_exit(void)
330 {
331         return widget_base_exit();
332 }
333
334 EXPORT_API int widget_app_terminate_context(widget_context_h context)
335 {
336         return widget_base_terminate_context((widget_base_instance_h)context);
337 }
338
339 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
340 {
341         return widget_base_foreach_context((widget_base_instance_cb)cb, data);
342 }
343
344 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
345                                         app_event_type_e event_type,
346                                         app_event_cb callback,
347                                         void *user_data)
348 {
349         return widget_base_add_event_handler(event_handler, event_type,
350                                         callback, user_data);
351 }
352
353 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
354                                                 event_handler)
355 {
356         return widget_base_remove_event_handler(event_handler);
357 }
358
359 EXPORT_API const char *widget_app_get_id(widget_context_h context)
360 {
361         int ret;
362         char *id;
363
364         if (!context) {
365                 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
366                 return NULL;
367         }
368
369         ret = widget_base_context_get_id((widget_base_instance_h)context, &id);
370         if (ret != WIDGET_BASE_ERROR_NONE) {
371                 _E("failed to get context id"); /* LCOV_EXCL_LINE */
372                 set_last_result(ret); /* LCOV_EXCL_LINE */
373                 return NULL; /* LCOV_EXCL_LINE */
374         }
375
376         set_last_result(WIDGET_ERROR_NONE);
377         return id;
378 }
379
380 static void __win_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
381 {
382         char *plug_id;
383         plug_id = evas_object_data_del(obj, "___PLUGID");
384         free(plug_id);
385 }
386
387 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
388                                         Evas_Object **win)
389 {
390         Evas_Object *ret_win = NULL;
391         Ecore_Wl2_Window *wl_win;
392         struct instance_data *data;
393         char buffer[256];
394         int rots[3] = {0};
395         int win_id;
396         char *id;
397         int ret;
398
399         if (context == NULL || win == NULL)
400                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
401                                 __FUNCTION__, NULL);
402
403         ret = widget_base_context_get_id((widget_base_instance_h)context, &id);
404         if (ret != WIDGET_BASE_ERROR_NONE) {
405                 _E("failed to get context id"); /* LCOV_EXCL_LINE */
406                 goto fault; /* LCOV_EXCL_LINE */
407         }
408
409         ret_win = elm_win_add(NULL, id, ELM_WIN_BASIC);
410         if (ret_win == NULL) {
411                 _E("failed to create window"); /* LCOV_EXCL_LINE */
412                 goto fault; /* LCOV_EXCL_LINE */
413         }
414
415         elm_win_wm_rotation_preferred_rotation_set(ret_win, -1);
416         elm_win_wm_rotation_available_rotations_set(ret_win, rots, 1);
417
418         wl_win = ecore_evas_wayland2_window_get(ecore_evas_ecore_evas_get(evas_object_evas_get(ret_win)));
419         if (wl_win == NULL) {
420                 _E("failed to get wayland window"); /* LCOV_EXCL_LINE */
421                 goto fault;
422         }
423
424         ecore_wl2_window_class_set(wl_win, id);
425         elm_win_aux_hint_add(ret_win, "wm.policy.win.user.geometry", "1");
426         widget_base_context_window_bind((widget_base_instance_h)context,        id, wl_win);
427
428         /* Set data to use in accessibility */
429         snprintf(buffer, sizeof(buffer), "%s:%d", id, getpid());
430         evas_object_data_set(ret_win, "___PLUGID", strdup(buffer));
431         evas_object_event_callback_add(ret_win, EVAS_CALLBACK_DEL, __win_del_cb, NULL);
432
433         win_id = ecore_wl2_window_id_get(wl_win);
434         _D("window created: %d", win_id);
435
436         data = (struct instance_data *)widget_base_context_get_user_data(
437                         (widget_base_instance_h)context);
438         if (data == NULL) {
439                 data = calloc(1, sizeof(struct instance_data));
440                 if (data == NULL) {
441                         _E("failed to alloc instance_data"); /* LCOV_EXCL_LINE */
442                         goto fault; /* LCOV_EXCL_LINE */
443                 }
444
445                 ret = widget_base_context_set_user_data((widget_base_instance_h)context, data);
446                 if (ret != WIDGET_BASE_ERROR_NONE) {
447                         _E("fail to set extra data"); /* LCOV_EXCL_LINE */
448                         goto fault; /* LCOV_EXCL_LINE */
449                 }
450         }
451
452         data->win = ret_win;
453         *win = ret_win;
454
455         return WIDGET_ERROR_NONE;
456
457 fault:
458         if (ret_win)    /* LCOV_EXCL_LINE */
459                 evas_object_del(ret_win); /* LCOV_EXCL_LINE */
460
461         return WIDGET_ERROR_FAULT; /* LCOV_EXCL_LINE */
462 }
463
464 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class,
465                 const char *class_id,
466                 widget_instance_lifecycle_callback_s callback, void *user_data)
467 {
468         widget_base_class cls;
469         struct app_class_cb_info *callback_data;
470         widget_class_h wc;
471
472         cls = widget_base_class_get_default();
473
474         /* override methods */
475         cls.ops.create = __class_create;
476         cls.ops.destroy = __class_destroy;
477         cls.ops.pause = __class_pause;
478         cls.ops.resume = __class_resume;
479         cls.ops.resize = __class_resize;
480         cls.ops.update = __class_update;
481
482         callback_data = calloc(1, sizeof(struct app_class_cb_info));
483         if (!callback_data) {
484                 _E("failed to calloc : %s", __FUNCTION__);
485                 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY);
486                 return NULL;
487         }
488         callback_data->callback = callback;
489         callback_data->user_data = user_data;
490
491         wc = (widget_class_h)widget_base_class_add(cls, class_id,
492                         callback_data);
493
494         if (!wc) {
495                 free(callback_data);
496                 return NULL;
497         }
498
499         __class_data_list = g_list_append(__class_data_list, callback_data);
500         set_last_result(WIDGET_ERROR_NONE);
501
502         return wc;
503 }
504
505 EXPORT_API widget_class_h widget_app_class_create(
506                 widget_instance_lifecycle_callback_s callback, void *user_data)
507 {
508         char *appid;
509         widget_class_h wc;
510
511         app_get_id(&appid);
512         if (!appid) {
513                 LOGE("appid is NULL");
514                 return NULL;
515         }
516
517         wc = (widget_class_h)widget_app_class_add(NULL, appid, callback,
518                         user_data);
519         free(appid);
520
521         return wc;
522 }
523
524 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
525 {
526         int ret = 0;
527
528         ret = widget_base_context_set_tag((widget_base_instance_h)context, tag);
529         if (ret != WIDGET_BASE_ERROR_NONE)
530                 return widget_app_error(ret, __FUNCTION__, NULL);
531
532         return WIDGET_ERROR_NONE;
533 }
534
535 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
536 {
537         int ret = 0;
538
539         ret = widget_base_context_get_tag((widget_base_instance_h)context, tag);
540         if (ret != WIDGET_BASE_ERROR_NONE)
541                 return widget_app_error(ret, __FUNCTION__, NULL);
542
543         return WIDGET_ERROR_NONE;
544 }
545
546 EXPORT_API int widget_app_context_set_content_info(widget_context_h context,
547                 bundle *content_info)
548 {
549         int ret = 0;
550
551         ret = widget_base_context_set_content_info(
552                         (widget_base_instance_h)context, content_info);
553         if (ret != WIDGET_BASE_ERROR_NONE)
554                 return widget_app_error(ret, __FUNCTION__, NULL);
555
556         return WIDGET_ERROR_NONE;
557 }
558
559 EXPORT_API int widget_app_context_set_title(widget_context_h context,
560                 const char *title)
561 {
562         struct instance_data *data = NULL;
563         int ret;
564
565         if (!context || !title) {
566                 _E("Invalid parameter %p %p", context, title);
567                 return WIDGET_ERROR_INVALID_PARAMETER;
568         }
569
570         data = (struct instance_data *)widget_base_context_get_user_data(
571                         (widget_base_instance_h)context);
572         if (data == NULL) {
573                 data = calloc(1, sizeof(struct instance_data));
574                 if (data == NULL) {
575                         return widget_app_error(WIDGET_ERROR_FAULT,
576                                         __FUNCTION__, NULL);
577                 }
578                 ret = widget_base_context_set_user_data(context, data);
579                 if (ret != WIDGET_BASE_ERROR_NONE)
580                         widget_app_error(ret, __FUNCTION__, NULL);
581         }
582
583         if (data->win)
584                 elm_win_title_set(data->win, title);
585
586         return WIDGET_ERROR_NONE;
587 }