New API: setRenderOrder
[apps/agl-service-windowmanager.git] / src / window_manager.cpp
index 2a50e76..a72f4a5 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <fstream>
 #include <regex>
+#include <sstream>
 
 #include "window_manager.hpp"
 #include "json_helper.hpp"
@@ -106,7 +107,7 @@ int WindowManager::init()
     // Load old_role.db
     LayerControlCallbacks lmcb;
     lmcb.surfaceCreated = [&](unsigned pid, unsigned surface){
-        this->surface_created(surface);
+        this->surface_created(pid, surface);
         };
     lmcb.surfaceDestroyed = [&](unsigned surface){
         this->surface_removed(surface);
@@ -182,7 +183,7 @@ result<int> WindowManager::api_request_surface(char const *appid, char const *dr
     {
         // name does not exist yet, allocate surface id...
         auto id = int(this->id_alloc.generate_id(role));
-        this->tmp_surface2app[id] = s_appid;
+        this->tmp_surface2app[id] = {s_appid, 0};
 
         // Set role map of (new, old)
         this->rolenew2old[role] = string(drawing_name);
@@ -197,31 +198,51 @@ result<int> WindowManager::api_request_surface(char const *appid, char const *dr
 char const *WindowManager::api_request_surface(char const *appid, char const *drawing_name,
                                      char const *ivi_id)
 {
+    unsigned sid = std::stol(ivi_id);
+
+    HMI_DEBUG("This API(requestSurfaceXDG) is for XDG Application using runXDG");
+    /*
+     * IVI-shell doesn't send surface_size event via ivi-wm protocol
+     * if the application is using XDG surface.
+     * So WM has to set surface size with original size here
+     */
+    WMError ret = this->lc->setXDGSurfaceOriginSize(sid);
+    if(ret != SUCCESS)
+    {
+        HMI_ERROR("%s", errorDescription(ret));
+        HMI_WARNING("The main user of this API is runXDG");
+        return "fail";
+    }
+
     // TODO: application requests by old role,
     //       so convert role old to new
     const char *role = this->convertRoleOldToNew(drawing_name);
-    string str = role;
+    string s_role = role;
+    string s_appid = appid;
     string l_name;
 
-    // auto lid = this->layers.get_layer_id(string(role));
-    unsigned lid = this->lc->getNewLayerID(str, &l_name);
-    unsigned sid = std::stol(ivi_id);
-
-    if (lid == 0)
+    if(!g_app_list.contains(s_appid))
     {
-        //register drawing_name as fallback and make it displayed.
-        lid = this->lc->getNewLayerID(str, &l_name);
-        HMI_DEBUG("%s is not registered in layers.json, then fallback as normal app", role);
-        if (lid == 0)
+        // auto lid = this->layers.get_layer_id(string(role));
+        unsigned l_id = this->lc->getNewLayerID(s_role, &l_name);
+        if (l_id == 0)
         {
-            return "Drawing name does not match any role, fallback is disabled";
+            /**
+             * register drawing_name as fallback and make it displayed.
+             */
+            l_id = this->lc->getNewLayerID("fallback", &l_name);
+            HMI_DEBUG("%s is not registered in layers.json, then fallback as normal app", role);
+            if (l_id == 0)
+            {
+                return "Designated role does not match any role, fallback is disabled";
+            }
         }
+        this->lc->createNewLayer(l_id);
+        // add client into the db
+        g_app_list.addClient(s_appid, l_id, s_role);
     }
 
-    this->lc->createNewLayer(lid);
-
-    auto rname = this->id_alloc.lookup(str);
-
+    auto rname = this->id_alloc.lookup(s_role);
     if (rname)
     {
         return "Surface already present";
@@ -229,17 +250,9 @@ char const *WindowManager::api_request_surface(char const *appid, char const *dr
 
     // register pair drawing_name and ivi_id
     this->id_alloc.register_name_id(role, sid);
-    // this->layers.add_surface(sid, *lid);
-
-    // this surface is already created
-    // HMI_DEBUG("surface_id is %u, layer_id is %u", sid, *lid);
 
-    // this->controller->layers[*lid]->add_surface(sid);
-    // this->layout_commit();
-
-    // add client into the db
-    string appid_str(appid);
-    g_app_list.addClient(appid_str, lid, string(role));
+    auto client = g_app_list.lookUpClient(s_appid);
+    client->addSurface(sid);
 
     // Set role map of (new, old)
     this->rolenew2old[role] = string(drawing_name);
@@ -247,29 +260,59 @@ char const *WindowManager::api_request_surface(char const *appid, char const *dr
     return nullptr;
 }
 
-bool WindowManager::api_set_role(char const *appid, char const *drawing_name, unsigned pid){
-    string id = appid;
-    string role = drawing_name;
-    string l_name;
-    unsigned surface = 0;
-    WMError wm_err = WMError::UNKNOWN;
+bool WindowManager::api_set_role(char const *appid, char const *drawing_name)
+{
     bool ret = false;
 
-    // get layer ID which role should be in
-    // auto lid = this->layers.get_layer_id(role);
-    unsigned lid = this->lc->getNewLayerID(role, &l_name);
-    if (lid == 0)
+    // TODO: application requests by old role,
+    //       so convert role old to new
+    const char *role = this->convertRoleOldToNew(drawing_name);
+    string s_role = role;
+    string s_appid = appid;
+    string l_name;
+
+    // Create WMClient
+    if(!g_app_list.contains(s_appid))
     {
-        // lid = this->layers.get_layer_id(string("fallback"));
-        HMI_DEBUG("%s is not registered in layers.json, then fallback as normal app", role.c_str());
-        if (lid == 0)
+        // auto lid = this->layers.get_layer_id(string(role));
+        unsigned l_id = this->lc->getNewLayerID(s_role, &l_name);
+        if (l_id == 0)
         {
-            HMI_ERROR("Drawing name does not match any role, fallback is disabled");
-            return ret;
+            /**
+             * register drawing_name as fallback and make it displayed.
+             */
+            l_id = this->lc->getNewLayerID("fallback", &l_name);
+            HMI_DEBUG("%s is not registered in layers.json, then fallback as normal app", role);
+            if (l_id == 0)
+            {
+                HMI_ERROR("Designated role does not match any role, fallback is disabled");
+                return ret;
+            }
+        }
+        this->lc->createNewLayer(l_id);
+        // add client into the db
+        g_app_list.addClient(s_appid, l_id, s_role);
+        // Set role map of (new, old)
+        this->rolenew2old[role] = s_role;
+    }
+
+    // for(auto itr = this->tmp_surface2app.begin();
+    //         itr != this->tmp_surface2app.end() ; ++itr)
+    // {
+    for(auto& x : this->tmp_surface2app)
+    {
+        if(x.second.appid == s_appid)
+        {
+            unsigned surface = x.first;
+            auto client = g_app_list.lookUpClient(s_appid);
+            client->addSurface(surface);
+            this->tmp_surface2app.erase(surface);
+            this->id_alloc.register_name_id(s_role, surface);
+            break;
         }
     }
 
-    if(0 != pid){
+/*     if(0 != pid){
         // search floating surfaceID from pid if pid is designated.
         wm_err = g_app_list.popFloatingSurface(pid, &surface);
     }
@@ -297,15 +340,11 @@ bool WindowManager::api_set_role(char const *appid, char const *drawing_name, un
         HMI_INFO("Create new client: %s, surface: %d into layer: %d with role: %s",
                  id.c_str(), surface, lid, role.c_str());
         g_app_list.addClient(id, lid, role);
-    }
+    } */
 
     // register pair drawing_name and ivi_id
-    this->id_alloc.register_name_id(role.c_str(), surface);
-
-    // this surface is already created
-    HMI_DEBUG("surface_id is %u, layer_id is %u", surface, lid);
 
-    return ret;
+    return true;
 }
 
 void WindowManager::api_activate_surface(char const *appid, char const *drawing_name,
@@ -463,6 +502,12 @@ void WindowManager::api_enddraw(char const *appid, char const *drawing_name)
     }
 }
 
+bool WindowManager::api_client_set_render_order(char const* appid, const vector<string>& render_order)
+{
+    bool ret = false;
+    return ret;
+}
+
 result<json_object *> WindowManager::api_get_display_info()
 {
     Screen screen = this->lc->getScreenInfo();
@@ -545,15 +590,14 @@ void WindowManager::send_event(char const *evname, char const *label, char const
 /**
  * proxied events
  */
-void WindowManager::surface_created(uint32_t surface_id)
+void WindowManager::surface_created(unsigned pid, unsigned surface_id)
 {
     if(this->tmp_surface2app.count(surface_id) != 0)
     {
-        string appid = this->tmp_surface2app[surface_id];
-        this->tmp_surface2app.erase(surface_id);
-        if(g_app_list.contains(appid))
+        string appid = this->tmp_surface2app[surface_id].appid;
+        auto client = g_app_list.lookUpClient(appid);
+        if(client != nullptr)
         {
-            auto client = g_app_list.lookUpClient(appid);
             WMError ret = client->addSurface(surface_id);
             HMI_INFO("Add surface %d to \"%s\"", surface_id, appid.c_str());
             if(ret != WMError::SUCCESS)
@@ -561,14 +605,82 @@ void WindowManager::surface_created(uint32_t surface_id)
                 HMI_ERROR("Failed to add surface to client %s", client->appID().c_str());
             }
         }
+        this->tmp_surface2app.erase(surface_id);
     }
     else
     {
         HMI_NOTICE("Unknown surface %d", surface_id);
+        // retrieve ppid
+        std::ostringstream os;
+        os << pid ;
+        string path = "/proc/" + os.str() + "/stat";
+        std::ifstream ifs(path.c_str());
+        string str;
+        unsigned ppid = 0;
+        if(!ifs.fail() && std::getline(ifs, str))
+        {
+            std::sscanf(str.data(), "%*d %*s %*c %d", &ppid);
+            HMI_INFO("Retrieve ppid %d", ppid);
+        }
+        else
+        {
+            HMI_ERROR("Failed to open /proc/%d/stat", pid);
+            HMI_ERROR("File system may be different");
+            return;
+        }
+        // search pid from surfaceID
+        json_object *response;
+        afb_service_call_sync("afm-main", "runners", nullptr, &response);
+
+        // retrieve appid from pid from application manager
+        string appid = "";
+        if(response == nullptr)
+        {
+            HMI_ERROR("No runners");
+        }
+        else
+        {
+            // check appid then add it to the client
+            HMI_INFO("Runners:%s", json_object_get_string(response));
+            int size = json_object_array_length(response);
+            for(int i = 0; i < size; i++)
+            {
+                json_object *j = json_object_array_get_idx(response, i);
+                const char* id = jh::getStringFromJson(j, "id");
+                int runid      = jh::getIntFromJson(j, "runid");
+                if(id && (runid > 0))
+                {
+                    if(runid == ppid)
+                    {
+                        appid = id;
+                        break;
+                    }
+                }
+            }
+            if(appid == "")
+            {
+                HMI_ERROR("Not found");
+            }
+        }
+        json_object_put(response);
+        auto client = g_app_list.lookUpClient(appid);
+        if(client != nullptr)
+        {
+            client->addSurface(surface_id);
+            this->id_alloc.register_name_id(client->role(), surface_id);
+        }
+        else
+        {
+            /*
+             * Store tmp surface and appid for application
+             * who requests setRole after creating shell surface
+             */
+            this->tmp_surface2app.emplace(surface_id, TmpClient{appid, ppid});
+        }
     }
 }
 
-void WindowManager::surface_removed(uint32_t surface_id)
+void WindowManager::surface_removed(unsigned surface_id)
 {
     HMI_DEBUG("Delete surface_id %u", surface_id);
     this->id_alloc.remove_id(surface_id);
@@ -883,6 +995,9 @@ WMError WindowManager::startTransition(unsigned req_num)
         for (const auto &x : actions)
         {
             this->lc->visibilityChange(x);
+            string old_role = this->rolenew2old[x.role];
+            emit_deactivated(old_role.c_str());
+
             /* if (g_app_list.contains(x.appid))
             {
                 auto client = g_app_list.lookUpClient(x.appid);
@@ -923,6 +1038,18 @@ WMError WindowManager::doEndDraw(unsigned req_num)
                 return ret;
             }
             ret = this->lc->visibilityChange(act);
+
+            // Emit active/deactive event
+            string old_role = this->rolenew2old[act.role];
+            if(act.visible == VISIBLE)
+            {
+                emit_activated(old_role.c_str());
+            }
+            else
+            {
+                emit_deactivated(old_role.c_str());
+            }
+
             if (ret != WMError::SUCCESS)
             {
                 HMI_SEQ_WARNING(req_num,