Adopt error code and message into Window Manager
[apps/agl-service-windowmanager.git] / src / app.cpp
index be53808..cedf70c 100644 (file)
@@ -123,7 +123,7 @@ processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata)
 void App::timerHandler()
 {
     unsigned seq = app_list.currentSequenceNumber();
-    HMI_SEQ_DEBUG(seq, "Timer expired remove Request");
+    HMI_SEQ_DEBUG(seq, "Timer expired, remove Request");
     app_list.req_dump();
     app_list.removeRequest(seq);
     app_list.next();
@@ -134,11 +134,22 @@ void App::timerHandler()
     }
 }
 
-void App::removeClient(const std::string &appid){
+void App::removeClient(const std::string &appid)
+{
     HMI_DEBUG("wm", "Remove clinet %s from list", appid.c_str());
     app_list.removeClient(appid);
 }
 
+bool App::subscribeEventForApp(const std::string &appid, afb_req req, const std::string &evname)
+{
+    if(app_list.contains(appid) != WMError::SUCCESS){
+        HMI_DEBUG("wm", "Client %s is not registered", appid.c_str());
+        return false;
+    }
+    auto client = app_list.lookUpClient(appid);
+    return client->subscribe(req, evname);
+}
+
 /**
  * App Impl
  */
@@ -412,7 +423,8 @@ void App::layout_commit()
     this->display->flush();
 }
 
-void App::set_timer(){
+void App::set_timer()
+{
     HMI_SEQ_DEBUG(app_list.currentSequenceNumber(), "Timer set activate");
     if (timer_ev_src == nullptr)
     {
@@ -432,30 +444,33 @@ void App::set_timer(){
     }
 }
 
-void App::stop_timer(){
+void App::stop_timer()
+{
     unsigned seq = app_list.currentSequenceNumber();
     HMI_SEQ_DEBUG(seq, "Timer stop");
     int rc = sd_event_source_set_enabled(timer_ev_src, SD_EVENT_OFF);
-    if(rc < 0){
+    if (rc < 0)
+    {
         HMI_SEQ_ERROR(seq, "Timer stop failed");
     }
 }
 
-bool App::lm_release(const struct WMAction &action)
+WMError App::lm_release(const struct WMAction &action)
 {
     //auto const &surface_id = this->lookup_id(drawing_name);
+    WMError ret = WMError::LAYOUT_CHANGE_FAIL;
     unsigned req_num = app_list.currentSequenceNumber();
     auto const &surface_id = this->lookup_id(action.role.c_str());
     if (!surface_id)
     {
         HMI_SEQ_ERROR(req_num, "Surface does not exist");
-        return false;
+        return ret;
     }
 
     if (*surface_id == this->layers.main_surface)
     {
         HMI_SEQ_ERROR(req_num, "Cannot deactivate main_surface");
-        return false;
+        return ret;
     }
 
     auto o_state = *this->layers.get_layout_state(*surface_id);
@@ -463,7 +478,7 @@ bool App::lm_release(const struct WMAction &action)
     if (o_state == nullptr)
     {
         HMI_SEQ_ERROR(req_num, "Could not find layer for surface");
-        return false;
+        return ret;
     }
 
     struct LayoutState &state = *o_state;
@@ -471,7 +486,7 @@ bool App::lm_release(const struct WMAction &action)
     if (state.main == -1)
     {
         HMI_SEQ_ERROR(req_num, "No surface active");
-        return false;
+        return ret;
     }
 
     // Check against main_surface, main_surface_name is the configuration item.
@@ -479,12 +494,12 @@ bool App::lm_release(const struct WMAction &action)
     {
         HMI_SEQ_DEBUG(req_num, "Refusing to deactivate main_surface %d", *surface_id);
         //reply(nullptr);
-        return true;
+        return WMError::SUCCESS;
     }
     if ((state.main == *surface_id) && (state.sub == *surface_id))
     {
         HMI_SEQ_ERROR(req_num, "Surface is not active");
-        return false;
+        return ret;
     }
 
     if (state.main == *surface_id)
@@ -534,10 +549,10 @@ bool App::lm_release(const struct WMAction &action)
                 this->enqueue_flushdraw(state.main);
             });
     }
-    return true;
+    return WMError::SUCCESS;
 }
 
-bool App::lm_layout_change(const struct WMAction &action)
+WMError App::lm_layout_change(const struct WMAction &action)
 {
     const char *msg = this->check_surface_exist(action.role.c_str());
 
@@ -548,13 +563,13 @@ bool App::lm_layout_change(const struct WMAction &action)
     {
         HMI_SEQ_DEBUG(app_list.currentSequenceNumber(), msg);
         //app_list.removeRequest(req_num);
-        return false;
+        return WMError::LAYOUT_CHANGE_FAIL;
     }
     this->lm_layout_change(action.role.c_str());
-    return true;
+    return WMError::SUCCESS;
 }
 
-bool App::do_transition(unsigned req_num)
+WMError App::do_transition(unsigned req_num)
 {
     /*
     * Check Policy
@@ -583,10 +598,11 @@ bool App::do_transition(unsigned req_num)
     {
         is_activate = false;
     }
-    bool ret = app_list.setAction(req_num, trigger.appid, trigger.role, trigger.area, is_activate);
+    WMError ret = app_list.setAction(req_num, trigger.appid, trigger.role, trigger.area, is_activate);
     app_list.req_dump();
 
-    if(!ret){
+    if (ret != WMError::SUCCESS)
+    {
         HMI_SEQ_ERROR(req_num, "Failed to set action");
         return ret;
     }
@@ -598,7 +614,7 @@ bool App::do_transition(unsigned req_num)
         /*
         do_task(y);
         */
-       /*  TODO
+        /*  TODO
            but current we can't do do_task,
            so divide the processing into lm_layout_change and lm_release
         */
@@ -606,15 +622,18 @@ bool App::do_transition(unsigned req_num)
         {
             sync_draw_happen = true;
             ret = lm_layout_change(y);
-            if(!ret){
-                HMI_SEQ_ERROR(req_num, "Failed layout change: %s", y.appid.c_str());
+            if (ret != WMError::SUCCESS)
+            {
+                HMI_SEQ_ERROR(req_num, "%s: appid: %s, role: %s, area: %s",
+                    errorDescription(ret), y.appid.c_str(), y.role.c_str(), y.area.c_str());
                 app_list.removeRequest(req_num);
                 break;
                 // TODO: if transition fails, what should we do?
             }
             /* app_list.lookUpClient(y.appid)->emit_syncdraw(y.role, y.area); */
         }
-        else{
+        else
+        {
             ret = lm_release(y);
             if (!ret)
             {
@@ -627,19 +646,22 @@ bool App::do_transition(unsigned req_num)
         }
     }
 
-    if(!ret){
+    if (ret != WMError::SUCCESS)
+    {
         //this->emit_error(request_seq, 0 /*error_num*/, "error happens"); // test
     }
-    else if (ret && sync_draw_happen){
+    else if (sync_draw_happen)
+    {
         this->set_timer();
     }
-    else{
+    else
+    {
         app_list.removeRequest(req_num); // HACK!!!
     }
     return ret;
 }
 
-void App::lm_layout_change(const chardrawing_name)
+void App::lm_layout_change(const char *drawing_name)
 {
     auto const &surface_id = this->lookup_id(drawing_name);
     auto layer_id = this->layers.get_layer_id(*surface_id);
@@ -784,7 +806,7 @@ void App::lm_layout_change(const char* drawing_name)
     }
 }
 
-const char* App::check_surface_exist(const char* drawing_name)
+const char *App::check_surface_exist(const char *drawing_name)
 {
     auto const &surface_id = this->lookup_id(drawing_name);
     if (!surface_id)
@@ -832,7 +854,8 @@ void App::api_activate_surface(char const *appid, char const *drawing_name, char
     std::string role = drawing_name;
     std::string area = drawing_area;
 
-    if(!app_list.contains(id)){
+    if (app_list.contains(id) != WMError::SUCCESS)
+    {
         reply("app doesn't request 'requestSurface' yet");
         return;
     }
@@ -868,10 +891,11 @@ void App::api_activate_surface(char const *appid, char const *drawing_name, char
     /*
     * Do allocate tasks
     */
-    bool ret = this->do_transition(new_req);
+    WMError ret = this->do_transition(new_req);
 
-    if(!ret){
-        HMI_SEQ_ERROR(new_req, "failed to do_transition");
+    if (ret != WMError::SUCCESS)
+    {
+        HMI_SEQ_ERROR(new_req, errorDescription(ret));
         //this->emit_error()
     }
 }
@@ -885,9 +909,10 @@ void App::api_deactivate_surface(char const *appid, char const *drawing_name, co
    */
     std::string id = appid;
     std::string role = drawing_name;
-    std::string area = "";    //drawing_area;
+    std::string area = ""; //drawing_area;
 
-    if(!app_list.contains(id)){
+    if (app_list.contains(id) != WMError::SUCCESS)
+    {
         reply("app doesn't request 'requestSurface' yet");
         return;
     }
@@ -922,11 +947,11 @@ void App::api_deactivate_surface(char const *appid, char const *drawing_name, co
     /*
     * Do allocate tasks
     */
-    bool ret = this->do_transition(new_req);
+    WMError ret = this->do_transition(new_req);
 
-    if (!ret)
+    if (ret != WMError::SUCCESS)
     {
-        HMI_SEQ_ERROR(new_req, "failed to do_transition");
+        HMI_SEQ_ERROR(new_req, errorDescription(ret));
         //this->emit_error()
     }
 }
@@ -954,7 +979,8 @@ void App::check_flushdraw(int surface_id)
     }
 }
 
-void App::lm_enddraw(const char* drawing_name){
+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++)
     {
@@ -975,13 +1001,14 @@ void App::do_enddraw(unsigned request_seq)
     auto actions = app_list.getActions(request_seq);
     HMI_SEQ_INFO(request_seq, "do endDraw");
 
-    for(const auto& act : actions){
+    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
+    /*     do
     {
         // emit flush Draw
         //emitFlushDrawToAll(&app_list, request_seq);
@@ -993,7 +1020,10 @@ void App::process_request()
 {
     unsigned req = app_list.currentSequenceNumber();
     HMI_SEQ_DEBUG(req, "Do next request");
-    do_transition(req);
+    WMError rc = do_transition(req);
+    if(rc != WMError::SUCCESS){
+        HMI_SEQ_ERROR(req, errorDescription(rc));
+    }
 }
 
 void App::api_enddraw(char const *appid, char const *drawing_name)
@@ -1096,6 +1126,12 @@ void App::surface_created(uint32_t surface_id)
       this->api_activate_surface(
          this->lookup_name(surface_id).value_or("unknown-name").c_str());
    }*/
+
+    // search pid from surfaceID
+
+    // pick up appid from pid from application manager
+
+    // check appid then add it to the client
 }
 
 void App::surface_removed(uint32_t surface_id)