Fix the bug of searching Application
[apps/agl-service-windowmanager.git] / src / window_manager.cpp
index 4efae89..59ef812 100644 (file)
@@ -61,6 +61,16 @@ static sd_event_source *g_timer_ev_src = nullptr;
 static AppList g_app_list;
 static WindowManager *g_context;
 
+struct AfbClosure {
+public:
+    AfbClosure(unsigned pid, unsigned ppid, unsigned surface)
+        : pid(pid), ppid(ppid), surface(surface) {}
+    ~AfbClosure() = default;
+    unsigned pid;
+    unsigned ppid;
+    unsigned surface;
+};
+
 namespace
 {
 
@@ -198,6 +208,22 @@ 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);
@@ -226,31 +252,15 @@ char const *WindowManager::api_request_surface(char const *appid, char const *dr
         g_app_list.addClient(s_appid, l_id, s_role);
     }
 
-    unsigned sid = std::stol(ivi_id);
-
     auto rname = this->id_alloc.lookup(s_role);
     if (rname)
     {
         return "Surface already present";
     }
-    if(this->tmp_surface2app.count(sid) != 0)
-    {
-        this->tmp_surface2app.erase(sid);
-    }
-
 
     // 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
-    // g_app_list.addClient(appid_str, lid, s_role);
     auto client = g_app_list.lookUpClient(s_appid);
     client->addSurface(sid);
 
@@ -260,7 +270,7 @@ 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)
+bool WindowManager::api_set_role(char const *appid, char const *drawing_name)
 {
     bool ret = false;
 
@@ -293,16 +303,17 @@ bool WindowManager::api_set_role(char const *appid, char const *drawing_name, un
         // 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;
+        this->rolenew2old[role] = string(drawing_name);
     }
 
-    for(auto itr = this->tmp_surface2app.begin();
-            itr != this->tmp_surface2app.end() ; ++itr)
+    // for(auto itr = this->tmp_surface2app.begin();
+    //         itr != this->tmp_surface2app.end() ; ++itr)
+    // {
+    for(auto& x : this->tmp_surface2app)
     {
-        // const auto& x : this->tmp_surface2app)
-        if(itr->second.appid == s_appid)
+        if(x.second.appid == s_appid)
         {
-            unsigned surface = itr->first;
+            unsigned surface = x.first;
             auto client = g_app_list.lookUpClient(s_appid);
             client->addSurface(surface);
             this->tmp_surface2app.erase(surface);
@@ -501,6 +512,33 @@ 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;
+    string id = appid;
+    auto client = g_app_list.lookUpClient(id);
+    if(client)
+    {
+        client->setRenderOrder(render_order);
+    }
+    return ret;
+}
+
+string WindowManager::api_client_attach_service_surface
+    (const char* appid, const char* dest, const char* service_surface)
+{
+    string uuid, s_dest = dest;
+    auto client = g_app_list.lookUpClient(s_dest);
+    if(!client)
+    {
+        HMI_ERROR("Failed to look up destination [%s]", dest);
+        return uuid;
+    }
+    uuid = client->attachTmpServiceSurface(appid, service_surface);
+    this->tmp_services.emplace_back(TmpService{appid, dest, service_surface, uuid});
+    return uuid;
+}
+
 result<json_object *> WindowManager::api_get_display_info()
 {
     Screen screen = this->lc->getScreenInfo();
@@ -580,11 +618,78 @@ void WindowManager::send_event(char const *evname, char const *label, char const
     }
 }
 
+string WindowManager::searchApp(unsigned pid, unsigned ppid, unsigned surface, json_object* resp)
+{
+    // retrieve appid from pid from application manager
+    string appid;
+    // check appid then add it to the client
+    HMI_INFO("Runners:%s", json_object_get_string(resp));
+    int size = json_object_array_length(resp);
+    HMI_INFO("pid %d, ppid %d, surface %d",pid, ppid, surface);
+    for(int i = 0; i < size; i++)
+    {
+        json_object *j = json_object_array_get_idx(resp, i);
+        int runid      = jh::getIntFromJson(j, "runid");
+        const char* id = jh::getStringFromJson(j, "id");
+        HMI_DEBUG("Appid %s, runid %d", id, runid);
+        if(id && (runid == ppid))
+        {
+            string s_id = id;
+            s_id.erase(s_id.find('@'));
+            appid = s_id;
+            HMI_INFO("App found %s", appid.c_str());
+            break;
+        }
+    }
+    if(appid.empty())
+    {
+        HMI_WARNING("Failed to retrieve id");
+    }
+    return appid;
+}
+
+void WindowManager::storeSurface(const string& appid, unsigned ppid, unsigned surface)
+{
+    auto elem = std::find_if(this->tmp_services.begin(), this->tmp_services.end(),
+                [&appid](TmpService& ts){
+                    return (ts.dest == appid );
+                });
+
+    if(elem != this->tmp_services.end())
+    {
+        // attachApp
+        auto client = g_app_list.lookUpClient(elem->dest);
+        if(client == nullptr)
+        {
+            return;
+        }
+        HMI_INFO("Attach surface %d (service %s) to app %s", surface, elem->service.c_str(), elem->dest.c_str());
+        client->attachServiceSurface(elem->service, surface);
+    }
+    else
+    {
+        // setRole
+        auto client = g_app_list.lookUpClient(appid);
+        if(client != nullptr)
+        {
+            client->addSurface(surface);
+            this->id_alloc.register_name_id(client->role(), surface);
+        }
+        else
+        {
+            // Store tmp surface and appid for application
+            // who requests setRole after creating shell surface
+            this->tmp_surface2app.emplace(surface, TmpClient{appid, ppid});
+        }
+    }
+}
+
 /**
  * proxied events
  */
 void WindowManager::surface_created(unsigned pid, unsigned surface_id)
 {
+    // requestSurface
     if(this->tmp_surface2app.count(surface_id) != 0)
     {
         string appid = this->tmp_surface2app[surface_id].appid;
@@ -612,7 +717,7 @@ void WindowManager::surface_created(unsigned pid, unsigned surface_id)
         unsigned ppid = 0;
         if(!ifs.fail() && std::getline(ifs, str))
         {
-            std::sscanf(str.data(), "%*d %*s %*c %*d %d", &ppid);
+            std::sscanf(str.data(), "%*d %*s %*c %d", &ppid);
             HMI_INFO("Retrieve ppid %d", ppid);
         }
         else
@@ -621,49 +726,30 @@ void WindowManager::surface_created(unsigned pid, unsigned surface_id)
             HMI_ERROR("File system may be different");
             return;
         }
+        struct AfbClosure* c = new struct AfbClosure(pid, ppid, surface_id);
         // 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))
+        afb_service_call("afm-main", "runners", json_object_new_object(),
+            [](void* closure, int stat, json_object* resp){
+                HMI_DEBUG("check %s", json_object_get_string(resp));
+                struct AfbClosure* c = static_cast<struct AfbClosure*>(closure);
+                HMI_DEBUG("check");
+                if(stat != 0)
                 {
-                    if(runid == ppid)
+                    HMI_ERROR("Failed to call runners");
+                }
+                else
+                {
+                    json_object* j;
+                    json_object_object_get_ex(resp, "response", &j);
+                    string appid = g_context->searchApp(c->pid, c->ppid, c->surface, j);
+                    if(!appid.empty())
                     {
-                        appid = id;
-                        break;
+                        g_context->storeSurface(appid, c->ppid, c->surface);
                     }
                 }
-            }
-            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);
-        }
-        struct TmpClient tmp_cl = {appid, ppid};
-        this->tmp_surface2app[surface_id] = tmp_cl; /* Store for requestSurfaceXDG */
+                json_object_put(resp);
+                delete c;
+            }, c);
     }
 }