Omit std::string to string
[apps/agl-service-windowmanager.git] / src / window_manager.cpp
index 2f8e3e9..4e41d40 100644 (file)
@@ -26,6 +26,9 @@ extern "C"
 #include <systemd/sd-event.h>
 }
 
+using std::string;
+using std::vector;
+
 namespace wm
 {
 
@@ -100,7 +103,7 @@ static int processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata
     return 0;
 }
 
-static void onStateTransitioned(std::vector<WMAction> actions)
+static void onStateTransitioned(vector<WMAction> actions)
 {
     g_context->startTransitionWrapper(actions);
 }
@@ -124,15 +127,15 @@ WindowManager::WindowManager(wl::display *d)
       pending_events(false)
 {
     char const *path_layers_json = getenv("AFM_APP_INSTALL_DIR");
-    std::string path;
+    string path;
     if (!path_layers_json)
     {
         HMI_ERROR("wm", "AFM_APP_INSTALL_DIR is not defined");
-        path = std::string(path_layers_json);
+        path = string(path_layers_json);
     }
     else
     {
-        path = std::string(path_layers_json) + std::string("/etc/layers.json");
+        path = string(path_layers_json) + string("/etc/layers.json");
     }
 
     try
@@ -245,13 +248,13 @@ result<int> WindowManager::api_request_surface(char const *appid, char const *dr
     //       so convert role old to new
     const char *role = this->convertRoleOldToNew(drawing_name);
 
-    auto lid = this->layers.get_layer_id(std::string(role));
+    auto lid = this->layers.get_layer_id(string(role));
     if (!lid)
     {
         /**
        * register drawing_name as fallback and make it displayed.
        */
-        lid = this->layers.get_layer_id(std::string("fallback"));
+        lid = this->layers.get_layer_id(string("fallback"));
         HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", role);
         if (!lid)
         {
@@ -275,11 +278,11 @@ result<int> WindowManager::api_request_surface(char const *appid, char const *dr
         }
 
         // add client into the db
-        std::string appid_str(appid);
-        g_app_list.addClient(appid_str, *lid, id, std::string(role));
+        string appid_str(appid);
+        g_app_list.addClient(appid_str, *lid, id, string(role));
 
         // Set role map of (new, old)
-        this->rolenew2old[role] = std::string(drawing_name);
+        this->rolenew2old[role] = string(drawing_name);
 
         return Ok<int>(id);
     }
@@ -297,7 +300,7 @@ char const *WindowManager::api_request_surface(char const *appid, char const *dr
     //       so convert role old to new
     const char *role = this->convertRoleOldToNew(drawing_name);
 
-    auto lid = this->layers.get_layer_id(std::string(role));
+    auto lid = this->layers.get_layer_id(string(role));
     unsigned sid = std::stol(ivi_id);
 
     if (!lid)
@@ -305,7 +308,7 @@ char const *WindowManager::api_request_surface(char const *appid, char const *dr
         /**
        * register drawing_name as fallback and make it displayed.
        */
-        lid = this->layers.get_layer_id(std::string("fallback"));
+        lid = this->layers.get_layer_id(string("fallback"));
         HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", role);
         if (!lid)
         {
@@ -331,15 +334,106 @@ char const *WindowManager::api_request_surface(char const *appid, char const *dr
     this->layout_commit();
 
     // add client into the db
-    std::string appid_str(appid);
-    g_app_list.addClient(appid_str, *lid, sid, std::string(role));
+    string appid_str(appid);
+    g_app_list.addClient(appid_str, *lid, sid, string(role));
 
     // Set role map of (new, old)
-    this->rolenew2old[role] = std::string(drawing_name);
+    this->rolenew2old[role] = string(drawing_name);
 
     return nullptr;
 }
 
+/**
+ * This function is substitute of requestSurface
+ * If surface creation is faster than application request of this function,
+ * WM will bind surfaceID with application and role.
+ * If surface creation is slower than application request of thie function,
+ * WM will put Client into pending list.
+ *
+ * Note :
+ * Application can request with pid but this is temporary solution for now.
+ * This will be removed.
+ * */
+bool WindowManager::api_set_role(char const *appid, char const *drawing_name, unsigned pid){
+    string id = appid;
+    string role = drawing_name;
+    unsigned surface = 0;
+    WMError wm_err = WMError::UNKNOWN;
+    bool ret = false;
+
+    // get layer ID which role should be in
+    auto lid = this->layers.get_layer_id(role);
+    if (!lid)
+    {
+        /**
+       * register drawing_name as fallback and make it displayed.
+       */
+        lid = this->layers.get_layer_id(string("fallback"));
+        HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", role.c_str());
+        if (!lid)
+        {
+            HMI_ERROR("wm", "Drawing name does not match any role, fallback is disabled");
+            return ret;
+        }
+    }
+
+    if(0 != pid){
+        // search floating surfaceID from pid if pid is designated.
+        // It is not good that application request with its pid
+        wm_err = g_app_list.popFloatingSurface(pid, &surface);
+    }
+    else{
+        // get floating surface with appid. If WM queries appid from pid,
+        // WM can bind surface and role with appid(not implemented yet)
+        //wm_err = g_app_list.popFloatingSurface(id);
+    }
+    if(wm_err != WMError::SUCCESS){
+        HMI_ERROR("wm", "No floating surface for app: %s", id.c_str());
+        g_app_list.addFloatingClient(id, *lid, role);
+        HMI_NOTICE("wm", "%s : Waiting for surface creation", id.c_str());
+        return ret;
+    }
+
+    ret = true;
+    if (g_app_list.contains(id))
+    {
+        HMI_INFO("wm", "Add role: %s with surface: %d. Client %s has multi surfaces.",
+                 role.c_str(), surface, id.c_str());
+        wm_err = g_app_list.appendRole(id, role, surface);
+        if(wm_err != WMError::SUCCESS){
+            HMI_INFO("wm", errorDescription(wm_err));
+        }
+    }
+    else{
+        HMI_INFO("wm", "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, surface, role);
+    }
+
+    // register pair drawing_name and ivi_id
+    this->id_alloc.register_name_id(role.c_str(), surface);
+    this->layers.add_surface(surface, *lid);
+
+    // this surface is already created
+    HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface, *lid);
+
+    const auto &o_layer = this->layers.get_layer(*lid);
+    auto rect = o_layer.value().rect;
+    if(rect.w < 0)
+    {
+        rect.w = this->controller->output_size.w + 1 + rect.w;
+    }
+    if(rect.h < 0)
+    {
+        rect.h = this->controller->output_size.h + 1 + rect.h;
+    }
+
+    this->controller->layers[*lid]->add_surface(surface);
+    this->layout_commit();
+
+    return ret;
+}
+
 void WindowManager::api_activate_surface(char const *appid, char const *drawing_name,
                                char const *drawing_area, const reply_func &reply)
 {
@@ -349,9 +443,26 @@ void WindowManager::api_activate_surface(char const *appid, char const *drawing_
     //       so convert role old to new
     const char *c_role = this->convertRoleOldToNew(drawing_name);
 
-    std::string id = appid;
-    std::string role = c_role;
-    std::string area = drawing_area;
+    string id = appid;
+    string role = c_role;
+    string area = drawing_area;
+
+    if(!g_app_list.contains(id))
+    {
+        reply("app doesn't request 'requestSurface' or 'setRole' yet");
+        return;
+    }
+    auto client = g_app_list.lookUpClient(id);
+
+    unsigned srfc = client->surfaceID(role);
+    if(srfc == 0)
+    {
+        HMI_ERROR("wm", "role sould be set with surface");
+        reply("role sould be set with surface");
+        return;
+    }
+    g_app_list.removeFloatingSurface(client->surfaceID());
+
     Task task = Task::TASK_ALLOCATE;
     unsigned req_num = 0;
     WMError ret = WMError::UNKNOWN;
@@ -399,9 +510,9 @@ void WindowManager::api_deactivate_surface(char const *appid, char const *drawin
     /*
     * Check Phase
     */
-    std::string id = appid;
-    std::string role = c_role;
-    std::string area = ""; //drawing_area;
+    string id = appid;
+    string role = c_role;
+    string area = ""; //drawing_area;
     Task task = Task::TASK_RELEASE;
     unsigned req_num = 0;
     WMError ret = WMError::UNKNOWN;
@@ -443,8 +554,8 @@ void WindowManager::api_enddraw(char const *appid, char const *drawing_name)
     //       so convert role old to new
     const char *c_role = this->convertRoleOldToNew(drawing_name);
 
-    std::string id = appid;
-    std::string role = c_role;
+    string id = appid;
+    string role = c_role;
     unsigned current_req = g_app_list.currentRequestNumber();
     bool result = g_app_list.setEndDrawFinished(current_req, id, role);
 
@@ -617,7 +728,7 @@ void WindowManager::surface_properties(unsigned surface_id, unsigned pid)
     afb_service_call_sync("afm-main", "runners", nullptr, &response);
 
     // retrieve appid from pid from application manager
-    std::string appid = "";
+    string appid = "";
     if(response == nullptr)
     {
         HMI_ERROR("wm", "No runners");
@@ -647,7 +758,7 @@ void WindowManager::surface_properties(unsigned surface_id, unsigned pid)
     g_app_list.addFloatingSurface(appid, surface_id, pid);*/
 }
 
-void WindowManager::removeClient(const std::string &appid)
+void WindowManager::removeClient(const string &appid)
 {
     HMI_DEBUG("wm", "Remove clinet %s from list", appid.c_str());
     g_app_list.removeClient(appid);
@@ -671,7 +782,7 @@ void WindowManager::timerHandler()
     this->processNextRequest();
 }
 
-void WindowManager::startTransitionWrapper(std::vector<WMAction> &actions)
+void WindowManager::startTransitionWrapper(vector<WMAction> &actions)
 {
     WMError ret;
     unsigned req_num = g_app_list.currentRequestNumber();
@@ -696,7 +807,7 @@ void WindowManager::startTransitionWrapper(std::vector<WMAction> &actions)
         {
             bool found;
             auto const &surface_id = this->lookup_id(act.role.c_str());
-            std::string appid = g_app_list.getAppID(*surface_id, act.role, &found);
+            string appid = g_app_list.getAppID(*surface_id, act.role, &found);
             if (!found)
             {
                 if (TaskVisible::INVISIBLE == act.visible)
@@ -772,9 +883,9 @@ bool WindowManager::pop_pending_events()
 
 optional<int> WindowManager::lookup_id(char const *name)
 {
-    return this->id_alloc.lookup(std::string(name));
+    return this->id_alloc.lookup(string(name));
 }
-optional<std::string> WindowManager::lookup_name(int id)
+optional<string> WindowManager::lookup_name(int id)
 {
     return this->id_alloc.lookup(id);
 }
@@ -850,7 +961,7 @@ int WindowManager::init_layers()
     return 0;
 }
 
-void WindowManager::surface_set_layout(int surface_id, const std::string& area)
+void WindowManager::surface_set_layout(int surface_id, const string& area)
 {
     if (!this->controller->surface_exists(surface_id))
     {
@@ -916,7 +1027,7 @@ void WindowManager::emit_syncdraw(char const *label, char const *area, int x, in
     this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
 }
 
-void WindowManager::emit_syncdraw(const std::string &role, const std::string &area)
+void WindowManager::emit_syncdraw(const string &role, const string &area)
 {
     compositor::rect rect = this->layers.getAreaSize(area);
     this->send_event(kListEventName[Event_SyncDraw],
@@ -1039,7 +1150,7 @@ void WindowManager::deactivate(int id)
     }
 }
 
-WMError WindowManager::setRequest(const std::string& appid, const std::string &role, const std::string &area,
+WMError WindowManager::setRequest(const string& appid, const string &role, const string &area,
                             Task task, unsigned* req_num)
 {
     if (!g_app_list.contains(appid))
@@ -1092,7 +1203,7 @@ WMError WindowManager::checkPolicy(unsigned req_num)
         ret = WMError::NO_ENTRY;
         return ret;
     }
-    std::string req_area = trigger.area;
+    string req_area = trigger.area;
 
     if (trigger.task == Task::TASK_ALLOCATE)
     {
@@ -1148,7 +1259,7 @@ WMError WindowManager::startTransition(unsigned req_num)
 
             // TODO: application requests by old role,
             //       so convert role new to old for emitting event
-            std::string old_role = this->rolenew2old[action.role];
+            string old_role = this->rolenew2old[action.role];
 
             this->emit_syncdraw(old_role, action.area);
             /* TODO: emit event for app not subscriber
@@ -1229,7 +1340,7 @@ WMError WindowManager::doEndDraw(unsigned req_num)
         {
             // TODO: application requests by old role,
             //       so convert role new to old for emitting event
-            std::string old_role = this->rolenew2old[act_flush.role];
+            string old_role = this->rolenew2old[act_flush.role];
 
             this->emit_flushdraw(old_role.c_str());
         }
@@ -1284,7 +1395,7 @@ WMError WindowManager::visibilityChange(const WMAction &action)
     return WMError::SUCCESS;
 }
 
-WMError WindowManager::setSurfaceSize(unsigned surface, const std::string &area)
+WMError WindowManager::setSurfaceSize(unsigned surface, const string &area)
 {
     this->surface_set_layout(surface, area);
 
@@ -1410,14 +1521,14 @@ int WindowManager::loadOldRoleDb()
     char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
     HMI_DEBUG("wm", "afm_app_install_dir:%s", afm_app_install_dir);
 
-    std::string file_name;
+    string file_name;
     if (!afm_app_install_dir)
     {
         HMI_ERROR("wm", "AFM_APP_INSTALL_DIR is not defined");
     }
     else
     {
-        file_name = std::string(afm_app_install_dir) + std::string("/etc/old_roles.db");
+        file_name = string(afm_app_install_dir) + string("/etc/old_roles.db");
     }
 
     // Load old_role.db
@@ -1460,7 +1571,7 @@ int WindowManager::loadOldRoleDb()
             return -1;
         }
 
-        this->roleold2new[old_role] = std::string(new_role);
+        this->roleold2new[old_role] = string(new_role);
     }
 
     // Check