appcore_ui_base: change version of tizen_policy from 1 to 7.
[platform/core/appfw/app-core.git] / src / ui_base / appcore_ui_base.c
index d6521b3..90d85a0 100644 (file)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#define _GNU_SOURCE
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/un.h>
@@ -62,6 +63,7 @@ typedef struct _appcore_ui_base_context {
        unsigned int hint;
        char *below_app;
        char *appid;
+       bool bg_state;
 
        int state;
        Ecore_Event_Handler *hshow;
@@ -86,7 +88,109 @@ static appcore_ui_base_context __context;
 static struct wl_display *dsp;
 static struct wl_registry *reg;
 static struct tizen_policy *tz_policy;
-static bool bg_state = false;
+
+static void _wl_cb_conformant(void *data,
+               struct tizen_policy *tizen_policy,
+               struct wl_surface *surface_resource,
+               uint32_t is_conformant)
+{
+       ; // nothing to do.
+}
+
+static void _wl_cb_conformant_area(void *data,
+               struct tizen_policy *tizen_policy,
+               struct wl_surface *surface_resource,
+               uint32_t conformant_part,
+               uint32_t state,
+               int32_t x, int32_t y, int32_t w, int32_t h)
+{
+       ; // nothing to do.
+}
+
+static void _wl_cb_notification_done(void *data,
+               struct tizen_policy *tizen_policy,
+               struct wl_surface *surface,
+               int32_t level,
+               uint32_t state)
+{
+       ; // nothing to do.
+}
+
+static void _wl_cb_transient_for_done(void *data,
+               struct tizen_policy *tizen_policy,
+               uint32_t child_id)
+{
+       ; // nothing to do.
+}
+
+static void _wl_cb_scr_mode_done(void *data,
+               struct tizen_policy *tizen_policy,
+               struct wl_surface *surface,
+               uint32_t mode,
+               uint32_t state)
+{
+       ; // nothing to do.
+}
+
+static void _wl_cb_iconify_state_changed(void *data,
+               struct tizen_policy *tizen_policy,
+               struct wl_surface *surface_resource,
+               uint32_t iconified,
+               uint32_t force)
+{
+       ; // nothing to do.
+}
+
+static void _wl_cb_supported_aux_hints(void *data,
+               struct tizen_policy *tizen_policy,
+               struct wl_surface *surface_resource,
+               struct wl_array *hints,
+               uint32_t num_hints)
+{
+       ; // nothing to do.
+}
+
+static void _wl_cb_allowed_aux_hint(void *data,
+               struct tizen_policy *tizen_policy,
+               struct wl_surface *surface_resource,
+               int id)
+{
+       ; // nothing to do.
+}
+
+static void _wl_cb_aux_message(void *data,
+               struct tizen_policy *tizen_policy,
+               struct wl_surface *surface_resource,
+               const char *key,
+               const char *val,
+               struct wl_array *options)
+{
+       ; // nothing to do.
+}
+
+static void _wl_cb_conformant_region(void *data,
+               struct tizen_policy *tizen_policy,
+               struct wl_surface *surface,
+               uint32_t conformant_part,
+               uint32_t state,
+               int32_t x, int32_t y, int32_t w, int32_t h,
+               uint32_t serial)
+{
+       ; // nothing to do.
+}
+
+static const struct tizen_policy_listener _tizen_policy_listener = {
+       _wl_cb_conformant,
+       _wl_cb_conformant_area,
+       _wl_cb_notification_done,
+       _wl_cb_transient_for_done,
+       _wl_cb_scr_mode_done,
+       _wl_cb_iconify_state_changed,
+       _wl_cb_supported_aux_hints,
+       _wl_cb_allowed_aux_hint,
+       _wl_cb_aux_message,
+       _wl_cb_conformant_region,
+};
 
 static void __wl_listener_cb(void *data, struct wl_registry *reg,
                uint32_t id, const char *interface, uint32_t ver)
@@ -94,7 +198,10 @@ static void __wl_listener_cb(void *data, struct wl_registry *reg,
        if (interface && !strcmp(interface, "tizen_policy")) {
                if (!tz_policy)
                        tz_policy = wl_registry_bind(reg, id,
-                                       &tizen_policy_interface, 1);
+                                       &tizen_policy_interface, 7);
+               if (tz_policy) {
+                       tizen_policy_add_listener(tz_policy, &_tizen_policy_listener, dsp);
+               }
        }
 }
 
@@ -194,7 +301,6 @@ static void __do_resume(void)
                        __context.ops.resume(__context.data);
                        traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
                        LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:done]", __context.appid);
-                       LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:Launching:done]", __context.appid);
                }
                if ((__context.hint & APPCORE_UI_BASE_HINT_WINDOW_STACK_CONTROL) &&
                                __context.below_app) {
@@ -406,8 +512,8 @@ static void __set_bg_state(void)
 
        tizen_policy_set_background_state(tz_policy, getpid());
        wl_display_roundtrip(dsp);
-       bg_state = true;
-       _DBG("bg state: %d", bg_state);
+       __context.bg_state = true;
+       _DBG("bg state: %d", __context.bg_state);
 }
 
 static void __unset_bg_state(void)
@@ -417,8 +523,8 @@ static void __unset_bg_state(void)
 
        tizen_policy_unset_background_state(tz_policy, getpid());
        wl_display_roundtrip(dsp);
-       bg_state = false;
-       _DBG("bg state: %d", bg_state);
+       __context.bg_state = false;
+       _DBG("bg state: %d", __context.bg_state);
 }
 
 static void __do_start(bundle *b)
@@ -445,20 +551,43 @@ static void __do_start(bundle *b)
        if (__context.hint & APPCORE_UI_BASE_HINT_BG_LAUNCH_CONTROL) {
                bg_launch = bundle_get_val(b, AUL_SVC_K_BG_LAUNCH);
                if (bg_launch && strcmp(bg_launch, "enable") == 0) {
-                       if (!bg_state && __context.state != AS_RUNNING)
+                       if (!__context.bg_state &&
+                                       __context.state != AS_RUNNING)
                                __set_bg_state();
                } else {
-                       if (bg_state)
+                       if (__context.bg_state)
                                __unset_bg_state();
                }
        }
 
        if (__context.hint & APPCORE_UI_BASE_HINT_WINDOW_AUTO_CONTROL) {
-               if (!bg_state)
+               if (!__context.bg_state)
                        __raise_win();
        }
 }
 
+static int __is_legacy_lifecycle(void)
+{
+       static int is_legacy = -1;
+       const char *api_version;
+
+       if (is_legacy != -1)
+               return is_legacy;
+
+       api_version = getenv("TIZEN_API_VERSION");
+       if (api_version) {
+               if (strverscmp("2.4", api_version) > 0 &&
+                               strverscmp("2.2.1", api_version) < 0)
+                       is_legacy = 1;
+               else
+                       is_legacy = 0;
+       } else {
+               is_legacy = 0;
+       }
+
+       return is_legacy;
+}
+
 EXPORT_API int appcore_ui_base_on_receive(aul_type type, bundle *b)
 {
        if (__context.state == AS_DYING) {
@@ -477,9 +606,15 @@ EXPORT_API int appcore_ui_base_on_receive(aul_type type, bundle *b)
        switch (type) {
        case AUL_START:
                __do_start(b);
+               if (__context.hint & APPCORE_UI_BASE_HINT_LEGACY_CONTROL) {
+                       if (!__context.bg_state && __is_legacy_lifecycle()) {
+                               _DBG("Legacy lifecycle");
+                               __do_resume();
+                       }
+               }
                break;
        case AUL_RESUME:
-               if (bg_state)
+               if (__context.bg_state)
                        __unset_bg_state();
                __raise_win();
                break;
@@ -502,17 +637,66 @@ EXPORT_API int appcore_ui_base_on_receive(aul_type type, bundle *b)
        return 0;
 }
 
-EXPORT_API int appcore_ui_base_on_create(void)
+static void __add_ecore_events(void)
 {
-       appcore_base_on_create();
+       __context.hshow = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW,
+                       __stub_show_cb, NULL);
+       if (!__context.hshow)
+               _ERR("Failed to add ECORE_WL_EVENT_WINDOW_SHOW event");
+
+       __context.hhide = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE,
+                       __stub_hide_cb, NULL);
+       if (!__context.hhide)
+               _ERR("Failed to add ECORE_WL_EVENT_WINDOW_HIDE event");
 
-       __context.hshow = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __stub_show_cb, NULL);
-       __context.hhide = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __stub_hide_cb, NULL);
        __context.hvchange = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE,
-                               __stub_visibility_cb, NULL);
-       __context.hlower = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER, __stub_lower_cb, NULL);
+                       __stub_visibility_cb, NULL);
+       if (!__context.hvchange)
+               _ERR("Failed to add ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE event");
+
+       __context.hlower = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER,
+                       __stub_lower_cb, NULL);
+       if (!__context.hlower)
+               _ERR("Failed to add ECORE_WL_EVENT_WINDOW_LOWER event");
+
        __context.hpvchange = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_PRE_VISIBILITY_CHANGE,
                        __stub_pre_visibility_cb, NULL);
+       if (!__context.hpvchange)
+               _ERR("Failed to add ECORE_WL_EVENT_WINDOW_PRE_VISIBILITY_CHANGE event");
+}
+
+static void __del_ecore_events(void)
+{
+       if (__context.hshow) {
+               ecore_event_handler_del(__context.hshow);
+               __context.hshow = NULL;
+       }
+
+       if (__context.hhide) {
+               ecore_event_handler_del(__context.hhide);
+               __context.hhide = NULL;
+       }
+
+       if (__context.hvchange) {
+               ecore_event_handler_del(__context.hvchange);
+               __context.hvchange = NULL;
+       }
+
+       if (__context.hlower) {
+               ecore_event_handler_del(__context.hlower);
+               __context.hlower = NULL;
+       }
+
+       if (__context.hpvchange) {
+               ecore_event_handler_del(__context.hpvchange);
+               __context.hpvchange = NULL;
+       }
+}
+
+EXPORT_API int appcore_ui_base_on_create(void)
+{
+       __add_ecore_events();
+       appcore_base_on_create();
        __context.state = AS_CREATED;
        LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:create:done]", __context.appid);
 
@@ -716,6 +900,8 @@ EXPORT_API int appcore_ui_base_init(appcore_ui_base_ops ops, int argc, char **ar
        bundle *b;
        char appid[PATH_MAX] = {0, };
 
+       ecore_wl_init(NULL);
+
        appcore_ui_plugin_init(&ops, argc, argv, &hint);
        aul_app_get_appid_bypid(getpid(), appid, sizeof(appid));
        __context.ops = ops;
@@ -743,24 +929,15 @@ EXPORT_API int appcore_ui_base_init(appcore_ui_base_ops ops, int argc, char **ar
 
 EXPORT_API void appcore_ui_base_fini(void)
 {
-       if (__context.hshow)
-               ecore_event_handler_del(__context.hshow);
-       if (__context.hhide)
-               ecore_event_handler_del(__context.hhide);
-       if (__context.hvchange)
-               ecore_event_handler_del(__context.hvchange);
-       if (__context.hlower)
-               ecore_event_handler_del(__context.hlower);
-       if (__context.hpvchange)
-               ecore_event_handler_del(__context.hpvchange);
-
+       __del_ecore_events();
        __finish_wl();
 
        free(__context.appid);
        __context.appid = NULL;
 
-       appcore_ui_plugin_fini();
        appcore_base_fini();
+       appcore_ui_plugin_fini();
+       ecore_wl_shutdown();
 }
 
 EXPORT_API void appcore_ui_base_pause(void)
@@ -799,6 +976,16 @@ EXPORT_API int appcore_ui_base_get_hint(void)
        return __context.hint;
 }
 
+EXPORT_API bool appcore_ui_base_get_bg_state(void)
+{
+       return __context.bg_state;
+}
+
+EXPORT_API void appcore_ui_base_set_bg_state(bool bg_state)
+{
+       __context.bg_state = bg_state;
+}
+
 static int __on_receive(aul_type type, bundle *b, void *data)
 {
        return appcore_ui_base_on_receive(type, b);