Move to launcher when the top application is terminated
[apps/agl-service-windowmanager-2017.git] / src / main.cpp
index e982337..e1351be 100644 (file)
@@ -29,17 +29,6 @@ extern "C"
 #include <systemd/sd-event.h>
 }
 
-typedef struct WMClientCtxt
-{
-    std::string name;
-    std::string role;
-    WMClientCtxt(const char *appName, const char* appRole)
-    {
-        name = appName;
-        role = appRole;
-    }
-} WMClientCtxt;
-
 struct afb_instance
 {
     std::unique_ptr<wl::display> display;
@@ -176,35 +165,32 @@ int binding_init() noexcept
     return -1;
 }
 
-static bool checkFirstReq(afb_req req)
-{
-    WMClientCtxt *ctxt = (WMClientCtxt *)afb_req_context_get(req);
-    return (ctxt) ? false : true;
-}
-
 static void cbRemoveClientCtxt(void *data)
 {
-    WMClientCtxt *ctxt = (WMClientCtxt *)data;
+    wm::WMClientCtxt *ctxt = (wm::WMClientCtxt *)data;
     if (ctxt == nullptr)
     {
         return;
     }
     HMI_DEBUG("wm", "remove app %s", ctxt->name.c_str());
-    // Lookup surfaceID and remove it because App is dead.
-    auto pSid = g_afb_instance->wmgr.id_alloc.lookup(ctxt->role.c_str());
-    if (pSid)
-    {
-        auto sid = *pSid;
-        g_afb_instance->wmgr.id_alloc.remove_id(sid);
-        g_afb_instance->wmgr.layers.remove_surface(sid);
-        g_afb_instance->wmgr.controller->sprops.erase(sid);
-        g_afb_instance->wmgr.controller->surfaces.erase(sid);
-        HMI_DEBUG("wm", "delete surfaceID %d", sid);
-    }
-    g_afb_instance->wmgr.removeClient(ctxt->name);
+    g_afb_instance->wmgr.onApplicationTerminated(*ctxt);
     delete ctxt;
 }
 
+static void createSecurityContext(afb_req req, const char* appid, const char* role)
+{
+    wm::WMClientCtxt *ctxt = (wm::WMClientCtxt *)afb_req_context_get(req);
+    if (!ctxt)
+    {
+        // Create Security Context at first time
+        const char *new_role = g_afb_instance->wmgr.convertRoleOldToNew(role);
+        wm::WMClientCtxt *ctxt = new wm::WMClientCtxt(appid, new_role);
+        HMI_DEBUG("wm", "create session for %s", ctxt->name.c_str());
+        afb_req_session_set_LOA(req, 1);
+        afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
+    }
+}
+
 void windowmanager_requestsurface(afb_req req) noexcept
 {
     std::lock_guard<std::mutex> guard(binding_m);
@@ -226,41 +212,17 @@ void windowmanager_requestsurface(afb_req req) noexcept
             return;
         }
 
-        /* Create Security Context */
-        bool isFirstReq = checkFirstReq(req);
-        if (!isFirstReq)
-        {
-            WMClientCtxt *ctxt = (WMClientCtxt *)afb_req_context_get(req);
-            HMI_DEBUG("wm", "You're %s.", ctxt->name.c_str());
-            if (ctxt->name != std::string(a_drawing_name))
-            {
-                afb_req_fail_f(req, "failed", "Dont request with other name: %s for now", a_drawing_name);
-                HMI_DEBUG("wm", "Don't request with other name: %s for now", a_drawing_name);
-                return;
-            }
-        }
-
+        const char *appid = afb_req_get_application_id(req);
         auto ret = g_afb_instance->wmgr.api_request_surface(
-            afb_req_get_application_id(req), a_drawing_name);
-
-        if (isFirstReq)
-        {
-            WMClientCtxt *ctxt = new WMClientCtxt(afb_req_get_application_id(req), a_drawing_name);
-            HMI_DEBUG("wm", "create session for %s", ctxt->name.c_str());
-            afb_req_session_set_LOA(req, 1);
-            afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
-        }
-        else
-        {
-            HMI_DEBUG("wm", "session already created for %s", a_drawing_name);
-        }
-
+            appid, a_drawing_name);
         if (ret.is_err())
         {
             afb_req_fail(req, "failed", ret.unwrap_err());
             return;
         }
 
+        createSecurityContext(req, appid, a_drawing_name);
+
         afb_req_success(req, json_object_new_int(ret.unwrap()), "success");
     }
     catch (std::exception &e)
@@ -301,15 +263,18 @@ void windowmanager_requestsurfacexdg(afb_req req) noexcept
             return;
         }
         char const *a_ivi_id = json_object_get_string(j_ivi_id);
-
+        char const *appid = afb_req_get_application_id(req);
         auto ret = g_afb_instance->wmgr.api_request_surface(
-            afb_req_get_application_id(req), a_drawing_name, a_ivi_id);
+            appid, a_drawing_name, a_ivi_id);
+
         if (ret != nullptr)
         {
             afb_req_fail(req, "failed", ret);
             return;
         }
 
+        createSecurityContext(req, appid, a_drawing_name);
+
         afb_req_success(req, NULL, "success");
     }
     catch (std::exception &e)