Dump client infomation when client is added
[apps/agl-service-windowmanager.git] / src / app.cpp
index 4ec4059..e64b248 100644 (file)
@@ -122,9 +122,14 @@ processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata)
 
 void App::timerHandler()
 {
-    // TODO: write reset process
     unsigned seq = app_list.currentSequenceNumber();
+    HMI_SEQ_DEBUG(seq, "Timer expired remove Request");
     app_list.removeRequest(seq);
+    app_list.next();
+    if (app_list.haveRequest())
+    {
+        this->process_request();
+    }
 }
 
 /**
@@ -400,29 +405,13 @@ void App::layout_commit()
     this->display->flush();
 }
 
-void App::do_allocate_window_resource(unsigned sequence_number)
-{
-    do
-    {
-        // TODO: Tasks will be changed according to policy manager result
-        // do task(onTransition (activate))
-    } while (!app_list.requestFinished());
-
-    // lm_.updateLayout(jobj);
-    // TODO: emit syncDraw with application
-    do
-    {
-        //client->emit_syncdraw(role);
-    } while (!app_list.requestFinished());
-
-    // is the below necessary?
-    //app_list.setAllocated(sequence_number);
-
+void App::set_timer(){
+    HMI_SEQ_DEBUG(app_list.currentSequenceNumber(), "Timer set activate");
     if (timer_ev_src != nullptr)
     {
         // firsttime set into sd_event
         int ret = sd_event_add_time(afb_daemon_get_event_loop(), &timer_ev_src,
-                                    CLOCK_BOOTTIME, time(NULL) + TIME_OUT, 0, processTimerHandler, this);
+                                    CLOCK_REALTIME, time(NULL) + TIME_OUT, 0, processTimerHandler, this);
         if (ret < 0)
         {
             HMI_ERROR("wm", "Can't set timer");
@@ -436,54 +425,13 @@ void App::do_allocate_window_resource(unsigned sequence_number)
     }
 }
 
-void App::api_activate_surface(char const *appid, char const *drawing_name, char const *drawing_area, const reply_func &reply)
+bool App::do_allocate_window_resource(unsigned request_seq)
 {
-    ST();
-
-    /*
-   * Check Phase
-   */
-
-    auto const &surface_id = this->lookup_id(drawing_name);
-    std::string id = appid;
-    std::string role = drawing_name;
-    std::string area = drawing_area;
-
-    if(!app_list.contains(id)){
-        reply("app doesn't request 'requestSurface' yet");
-        return;
-    }
-
-    auto client = app_list.lookUpClient(id);
-
-    /*
-   * Queueing Phase
-   */
-    unsigned current = app_list.currentSequenceNumber();
-    unsigned requested_num = app_list.getSequenceNumber(id);
-    if (requested_num != 0)
-    {
-        HMI_SEQ_INFO(requested_num, "%s %s %s request is already queued", id.c_str(), role.c_str(), area.c_str());
-        reply("already requested");
-        return;
-    }
-
-    WMRequest req = WMRequest(id, role, area, Task::TASK_ALLOCATE);
-    unsigned new_req = app_list.addAllocateRequest(req);
-
-    HMI_SEQ_DEBUG(new_req, "%s start sequence with %s, %s", id.c_str(), role.c_str(), area.c_str());
-
-    if (new_req != current)
-    {
-        // Add request, then invoked after the previous task is finished
-        HMI_SEQ_DEBUG(new_req, "request is accepted");
-        reply(nullptr);
-        return;
-    }
-
     /*
     * Check Policy
     */
+   // get current trigger
+    auto trigger = app_list.getRequest(request_seq);
 
     // json_object* newState = checkPolicy(role);
     /* The following error check is not necessary because main.cpp will reject the message form not registered object
@@ -491,53 +439,31 @@ void App::api_activate_surface(char const *appid, char const *drawing_name, char
        reply("client is not registered");
        return;
    } */
+    HMI_SEQ_NOTICE(request_seq, "ATM, Policy manager does't exist, then set WMAction as is");
 
-    // get surfaceID from role
-    /* auto const &surface_id_tmp = client->surfaceID(role);
-   auto const &layer_id_tmp = client->layerID();
-
-   if(!surface_id_tmp && !layer_id_tmp){
-       reply("invalid window manager client");
-       HMI_DEBUG("appid:%s, requested_role:%s, surfaceID:%d in layer %d",
-          appid, role, surface_id_tmp, layer_id_tmp);
-   } */
+    bool ret = app_list.setAction(request_seq, trigger.appid, trigger.role, trigger.area);
 
-    /*
-    * Do allocate tasks
-    */
-    this->do_allocate_window_resource(new_req);
+    app_list.req_dump();
+    // TODO: Tasks will be changed according to policy manager result
+    // do task(onTransition (activate))
 
-    if (!surface_id)
-    {
-        reply("Surface does not exist");
-        return;
-    }
+    // lm_.updateLayout(jobj);
+    // TODO: emit syncDraw with application
 
-    if (!this->controller->surface_exists(*surface_id))
-    {
-        reply("Surface does not exist in controller!");
-        return;
+    //client->emit_syncdraw(role);
+    if (ret) {
+        this->set_timer();
+    } else {
+        //this->emit_error(request_seq, 0 /*error_num*/, "error happens"); // test
     }
+    return ret;
+}
 
+void App::lm_layout_change(unsigned req, const char* drawing_name)
+{
+    auto const &surface_id = this->lookup_id(drawing_name);
     auto layer_id = this->layers.get_layer_id(*surface_id);
-
-    if (!layer_id)
-    {
-        reply("Surface is not on any layer!");
-        return;
-    }
-
     auto o_state = *this->layers.get_layout_state(*surface_id);
-
-    if (o_state == nullptr)
-    {
-        reply("Could not find layer for surface");
-        return;
-    }
-
-    HMI_DEBUG("wm", "surface %d is detected", *surface_id);
-    reply(nullptr);
-
     struct LayoutState &state = *o_state;
 
     // disable layers that are above our current layer
@@ -678,6 +604,106 @@ void App::api_activate_surface(char const *appid, char const *drawing_name, char
     }
 }
 
+const char* App::check_surface_exist(unsigned req, const char* drawing_name)
+{
+    auto const &surface_id = this->lookup_id(drawing_name);
+    if (!surface_id)
+    {
+        //reply("Surface does not exist");
+        return "Surface does not exist";
+    }
+
+    if (!this->controller->surface_exists(*surface_id))
+    {
+        //reply("Surface does not exist in controller!");
+        return "Surface does not exist in controller!";
+    }
+
+    auto layer_id = this->layers.get_layer_id(*surface_id);
+
+    if (!layer_id)
+    {
+        //reply("Surface is not on any layer!");
+        return "Surface is not on any layer!";
+    }
+
+    auto o_state = *this->layers.get_layout_state(*surface_id);
+
+    if (o_state == nullptr)
+    {
+        //reply("Could not find layer for surface");
+        return "Could not find layer for surface";
+    }
+
+    HMI_DEBUG("wm", "surface %d is detected", *surface_id);
+    return nullptr;
+    //reply(nullptr);
+}
+
+void App::api_activate_surface(char const *appid, char const *drawing_name, char const *drawing_area, const reply_func &reply)
+{
+    ST();
+
+    /*
+   * Check Phase
+   */
+
+    std::string id = appid;
+    std::string role = drawing_name;
+    std::string area = drawing_area;
+
+    if(!app_list.contains(id)){
+        reply("app doesn't request 'requestSurface' yet");
+        return;
+    }
+
+    auto client = app_list.lookUpClient(id);
+
+    /*
+   * Queueing Phase
+   */
+    unsigned current = app_list.currentSequenceNumber();
+    unsigned requested_num = app_list.getSequenceNumber(id);
+    if (requested_num != 0)
+    {
+        HMI_SEQ_INFO(requested_num, "%s %s %s request is already queued", id.c_str(), role.c_str(), area.c_str());
+        reply("already requested");
+        return;
+    }
+
+    WMRequest req = WMRequest(id, role, area, Task::TASK_ALLOCATE);
+    unsigned new_req = app_list.addAllocateRequest(req);
+    app_list.req_dump();
+
+    HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", id.c_str(), role.c_str(), area.c_str());
+
+    if (new_req != current)
+    {
+        // Add request, then invoked after the previous task is finished
+        HMI_SEQ_DEBUG(new_req, "request is accepted");
+        reply(nullptr);
+        return;
+    }
+
+    /*
+    * Do allocate tasks
+    */
+    bool ret = this->do_allocate_window_resource(new_req);
+
+    // layer manager task
+    const char* msg = this->check_surface_exist(new_req, drawing_name); // this function will be integrated in do_allocate_window_resource()
+
+    if(msg){
+        HMI_SEQ_DEBUG(new_req,"surface doesn't exist");
+        reply(msg);
+        app_list.removeRequest(new_req);
+        return;
+    }
+    this->lm_layout_change(new_req, drawing_name); // this function will be integrated in do_allocate_window_resource()
+
+    reply(nullptr);
+}
+
 void App::api_deactivate_surface(char const *appid, char const *drawing_name, const reply_func &reply)
 {
     ST();
@@ -805,56 +831,70 @@ void App::check_flushdraw(int surface_id)
     }
 }
 
-void App::do_enddraw(unsigned sequence_number)
-{
-    HMI_SEQ_INFO(sequence_number, "do endDraw");
-    do
+void App::lm_enddraw(const char* drawing_name){
+    HMI_DEBUG("wm", "end draw %s", drawing_name);
+    for (unsigned i = 0, iend = this->pending_end_draw.size(); i < iend; i++)
     {
-        // make visible application
-    } while (!app_list.requestFinished());
+        auto n = this->lookup_name(this->pending_end_draw[i]);
+        if (n && *n == drawing_name)
+        {
+            std::swap(this->pending_end_draw[i], this->pending_end_draw[iend - 1]);
+            this->pending_end_draw.resize(iend - 1);
+            this->activate(this->pending_end_draw[i]);
+            this->emit_flushdraw(drawing_name);
+        }
+    }
+}
+
+void App::do_enddraw(unsigned request_seq)
+{
+    // get actions
+    auto actions = app_list.getActions(request_seq);
+    HMI_SEQ_INFO(request_seq, "do endDraw");
 
-    HMI_SEQ_INFO(sequence_number, "emit flushDraw");
-    do
+    for(const auto& act : actions){
+        HMI_SEQ_DEBUG(request_seq, "visible %s", act.role.c_str());
+        this->lm_enddraw(act.role.c_str());
+    }
+
+    HMI_SEQ_INFO(request_seq, "emit flushDraw");
+/*     do
     {
         // emit flush Draw
         //emitFlushDrawToAll(&app_list, request_seq);
         // emit status change event
-    } while (!app_list.requestFinished());
+    } while (!app_list.requestFinished());*/
 }
 
 void App::process_request()
 {
+    unsigned req = app_list.currentSequenceNumber();
+    HMI_SEQ_DEBUG(req, "Do next request");
+    do_allocate_window_resource(req);
 }
 
 void App::api_enddraw(char const *appid, char const *drawing_name)
 {
     std::string id(appid);
-    unsigned request_seq = app_list.lookUpAllocatingApp(id);
+    std::string role(drawing_name);
     unsigned current_seq = app_list.currentSequenceNumber();
-    if (current_seq != request_seq)
+    bool result = app_list.setEndDrawFinished(current_seq, id, role);
+
+    if (!result)
     {
-        if (request_seq == 0)
-        {
-            HMI_ERROR("wm", "You don't have Window Resource");
-        }
-        else
-        {
-            HMI_ERROR("wm", "unknown error. Application may not obey the sequence manner. please call endDraw after syncDraw");
-        }
+        HMI_ERROR("wm", "%s doesn't have Window Resource", id.c_str());
         return;
     }
 
-    std::string role = drawing_name;
-    //std::string area = drawing_area;
-    app_list.setEndDrawFinished(request_seq, role);
-
-    if (app_list.endDrawFullfilled(request_seq))
+    if (app_list.endDrawFullfilled(current_seq))
     {
         // do task for endDraw
-        this->do_enddraw(request_seq);
-        app_list.removeRequest(request_seq);
-        HMI_SEQ_INFO(request_seq, "Finish sequence");
-        app_list.setCurrentSequence(request_seq + 1);
+        //this->stop_timer();
+        this->do_enddraw(current_seq);
+
+        app_list.removeRequest(current_seq);
+        HMI_SEQ_INFO(current_seq, "Finish request");
+        app_list.next();
         if (app_list.haveRequest())
         {
             this->process_request();
@@ -862,20 +902,9 @@ void App::api_enddraw(char const *appid, char const *drawing_name)
     }
     else
     {
-        HMI_SEQ_INFO(request_seq, "Wait other App call endDraw");
+        HMI_SEQ_INFO(current_seq, "Wait other App call endDraw");
         return;
     }
-    for (unsigned i = 0, iend = this->pending_end_draw.size(); i < iend; i++)
-    {
-        auto n = this->lookup_name(this->pending_end_draw[i]);
-        if (n && *n == drawing_name)
-        {
-            std::swap(this->pending_end_draw[i], this->pending_end_draw[iend - 1]);
-            this->pending_end_draw.resize(iend - 1);
-            this->activate(this->pending_end_draw[i]);
-            this->emit_flushdraw(drawing_name);
-        }
-    }
 }
 
 void App::api_ping() { this->dispatch_pending_events(); }