Adopt error code and message into Window Manager
[apps/agl-service-windowmanager.git] / src / applist.cpp
index 477a51b..6b608d0 100644 (file)
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
+#include <iostream>
 #include <algorithm>
 #include "applist.hpp"
 #include "../include/hmi-debug.h"
 using std::shared_ptr;
 using std::string;
 using std::unique_ptr;
+using std::vector;
+
+namespace wm
+{
 
-namespace wm {
+AppList::AppList()
+    : req_list(0),
+      client_list(0),
+      current_seq(1)
+{
+}
 
+AppList::~AppList() {}
 
-AppList::AppList(){}
-AppList::~AppList(){}
+void AppList::addClient(const string &appid, const string &role)
+{
+    shared_ptr<WMClient> client = std::make_shared<WMClient>(appid, role);
+    client_list[appid] = client;
+    client_dump();
+}
 
-void AppList::addClient(const std::string &appid, const std::string &role){
-    shared_ptr<WMClient> client(new WMClient(appid, role));
+void AppList::addClient(const std::string &appid, unsigned layer, unsigned surface, const std::string &role)
+{
+    shared_ptr<WMClient> client = std::make_shared<WMClient>(appid, layer, surface, role);
     client_list[appid] = client;
+    client_dump();
 }
 
-void AppList::removeClient(const string &appid){
+void AppList::removeClient(const string &appid)
+{
     client_list.erase(appid);
 }
 
-bool AppList::contains(const string &appid){
+WMError AppList::contains(const string &appid)
+{
     auto result = client_list.find(appid);
-    return (client_list.end() != result) ? true : false;
+    return (client_list.end() != result) ? WMError::SUCCESS : WMError::NOT_REGISTERED;
 }
 
-/*
-* Call contains before calling this function
-*/
-shared_ptr <WMClient> AppList::lookUpClient(const string &appid)
+void AppList::removeSurface(unsigned surface_id){
+    // This function may be very slow
+    bool ret = false;
+    for (auto &x : client_list)
+    {
+        ret = x.second->removeSurfaceIfExist(surface_id);
+        if(ret){
+            HMI_DEBUG("wm", "remove surface %d from Client %s finish", surface_id, x.second->appID().c_str());
+            break;
+        }
+    }
+}
+
+/**
+ * @brief  get WMClient object. Before call this function, must call "contains"
+ * to check the key is contained, otherwise, you have to take care of std::out_of_range.
+ * @param string[in] application id(key)
+ * @return WMClient object
+ */
+shared_ptr<WMClient> AppList::lookUpClient(const string &appid)
 {
-    /* auto result = client_list.find(appid);
-    return (client_list.end() != result) ?
-        &(result->second()) : nullptr; */
-    return client_list[appid];
+    return client_list.at(appid);
 }
 
-unsigned AppList::currentSequenceNumber(){
+int AppList::countClient()
+{
+    return client_list.size();
+}
+
+unsigned AppList::currentSequenceNumber()
+{
     return current_seq;
 }
 
-unsigned AppList::getSequenceNumber(const string &appid){
-    for(auto x : req_list){
+// Is this function necessary ?
+unsigned AppList::getSequenceNumber(const string &appid)
+{
+    for (const auto &x : req_list)
+    {
         // Since app will not request twice and more, comparing appid is enough?
-        if( (x.appid == appid))
+        if ((x.trigger.appid == appid))
         {
             return x.seq_num;
         }
@@ -68,70 +108,194 @@ unsigned AppList::getSequenceNumber(const string &appid){
     return 0;
 }
 
-unsigned AppList::addAllocateRequest(WMRequest req){
-    req.seq_num = req_list.back().seq_num + 1;
-    req.allocating = false;
-    req.end_draw_finished = false;
+unsigned AppList::addAllocateRequest(WMRequest req)
+{
+    if (req_list.size() == 0)
+    {
+        req.seq_num = current_seq;
+    }
+    else
+    {
+        HMI_SEQ_DEBUG(current_seq, "real: %d", req_list.back().seq_num + 1);
+        req.seq_num = req_list.back().seq_num + 1;
+    }
     req_list.push_back(req);
-    return req.seq_num;
+    return req.seq_num; // return 1; if you test time_expire
 }
 
-bool AppList::requestFinished(){
+bool AppList::requestFinished()
+{
     return req_list.empty();
 }
 
-unsigned AppList::lookUpAllocatingApp(const string &appid){
-    for(auto x: req_list){
-        if(appid == x.appid){
-            if(false == x.allocating)
-                return x.seq_num;
+struct WMTrigger AppList::getRequest(unsigned req_num)
+{
+    for (auto &x : req_list)
+    {
+        if (req_num == x.seq_num)
+        {
+            return x.trigger;
+        }
+    }
+}
+
+const vector<struct WMAction> &AppList::getActions(unsigned req_num)
+{
+    for (auto &x : req_list)
+    {
+        if (req_num == x.seq_num)
+        {
+            return x.sync_draw_req;
         }
     }
-    return 0;
 }
 
-void AppList::setEndDrawFinished(unsigned request_seq, const string &role){
-    for(auto x: req_list){
-        if(request_seq == x.seq_num){
-            if(role == x.role){
-                x.end_draw_finished = true;
+WMError AppList::setAction(unsigned req_num, const struct WMAction &action)
+{
+    WMError result = WMError::FAIL;
+    for (auto &x : req_list)
+    {
+        if (req_num != x.seq_num)
+        {
+            continue;
+        }
+        x.sync_draw_req.push_back(action);
+        result = WMError::SUCCESS;
+        break;
+    }
+
+    return result;
+}
+
+WMError AppList::setAction(unsigned req_num, const string &appid, const string &role, const string &area, bool visible)
+{
+    WMError result = WMError::NOT_REGISTERED;
+    for (auto &x : req_list)
+    {
+        if (req_num != x.seq_num)
+        {
+            continue;
+        }
+        WMAction action{appid, role, area, visible, false};
+
+        x.sync_draw_req.push_back(action);
+        result = WMError::SUCCESS;
+        break;
+    }
+    return result;
+}
+
+bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role)
+{
+    bool result = false;
+    for (auto &x : req_list)
+    {
+        if (req_num < x.seq_num)
+        {
+            break;
+        }
+        if (req_num == x.seq_num)
+        {
+            for (auto &y : x.sync_draw_req)
+            {
+                if (y.appid == appid && y.role == role)
+                {
+                    y.end_draw_finished = true;
+                    result = true;
+                }
             }
         }
     }
+    req_dump();
+    return result;
 }
 
-bool AppList::endDrawFullfilled(unsigned request_seq){
+/**
+ * @brief  check all actions of the requested sequence is finished
+ * @param  unsigned sequence_num
+ * @return true if all action is set.
+ */
+bool AppList::endDrawFullfilled(unsigned req_num)
+{
     bool result = false;
-    for (auto x : req_list)
+    for (const auto &x : req_list)
     {
-        if(request_seq < x.seq_num){
+        if (req_num < x.seq_num)
+        {
             break;
         }
-        if(request_seq == x.seq_num){
-            result = x.end_draw_finished && x.allocating;
-            if(result == false){
-                break;
+        if (req_num == x.seq_num)
+        {
+            result = true;
+            for (const auto &y : x.sync_draw_req)
+            {
+                result &= y.end_draw_finished;
+                if (!result)
+                {
+                    break;
+                }
             }
         }
     }
     return result;
 }
 
-void AppList::removeRequest(unsigned req_seq){
+void AppList::removeRequest(unsigned req_seq)
+{
     req_list.erase(remove_if(req_list.begin(), req_list.end(),
-                            [req_seq](WMRequest x) { return x.seq_num == req_seq;}
-                            ));
+                             [req_seq](WMRequest x) {
+                                 return x.seq_num == req_seq;
+                             }));
+}
+
+void AppList::next()
+{
+    ++this->current_seq;
+    if (0 == this->current_seq)
+    {
+        this->current_seq = 1;
+    }
 }
 
-void AppList::setCurrentSequence(unsigned req_seq){
-    this->current_seq = req_seq;
-    if(0 == this->current_seq){
-       this->current_seq = 1;
-   }
+bool AppList::haveRequest()
+{
+    return !req_list.empty();
 }
 
-bool AppList::haveRequest(){
-    return true;
+void AppList::client_dump()
+{
+    DUMP("======= client dump =====");
+    for (const auto &x : client_list)
+    {
+        const auto &y = x.second;
+        y->dumpInfo();
+    }
+    DUMP("======= client dump end=====");
 }
 
-}
\ No newline at end of file
+void AppList::req_dump()
+{
+    DUMP("======= req dump =====");
+    DUMP("current request : %d", current_seq);
+    for (const auto &x : req_list)
+    {
+        DUMP("requested with  : %d", x.seq_num);
+        DUMP("Trigger : (APPID :%s, ROLE :%s, AREA :%s, TASK: %d)",
+             x.trigger.appid.c_str(),
+             x.trigger.role.c_str(),
+             x.trigger.area.c_str(),
+             x.trigger.task);
+
+        for (const auto &y : x.sync_draw_req)
+        {
+            DUMP(
+                "Action  : (APPID :%s, ROLE :%s, AREA :%s, END_DRAW_FINISHED: %d)",
+                y.appid.c_str(),
+                y.role.c_str(),
+                y.area.c_str(),
+                y.end_draw_finished);
+        }
+    }
+    DUMP("======= req dump end =====\n");
+}
+} // namespace wm
\ No newline at end of file