Fix the bug of searching Application
[apps/agl-service-windowmanager.git] / src / window_manager.cpp
index a72f4a5..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
 {
 
@@ -293,7 +303,7 @@ bool WindowManager::api_set_role(char const *appid, char const *drawing_name)
         // 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();
@@ -505,9 +515,30 @@ 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();
@@ -587,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;
@@ -628,55 +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);
-        }
-        else
-        {
-            /*
-             * Store tmp surface and appid for application
-             * who requests setRole after creating shell surface
-             */
-            this->tmp_surface2app.emplace(surface_id, TmpClient{appid, ppid});
-        }
+                json_object_put(resp);
+                delete c;
+            }, c);
     }
 }