fix for als2019 8.0.0
[apps/agl-service-windowmanager.git] / src / window_manager.cpp
index 1432769..52a7144 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <fstream>
 #include <regex>
+#include <unistd.h>
 
 #include "window_manager.hpp"
 #include "json_helper.hpp"
@@ -80,8 +81,10 @@ static const vector<string> kListEventName{
 
 static const char kPathOldRolesConfigFile[] = "/etc/old_roles.json";
 static sd_event_source *g_timer_ev_src = nullptr;
+static sd_event_source *g_timer_endInit_src = nullptr;
 static AppList g_app_list;
 static WindowManager *g_context;
+static bool testFlg = false;
 static vector<string> white_list_area_size_change = {
     "homescreen"
 };
@@ -106,6 +109,12 @@ static int processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata
     return 0;
 }
 
+static int endInitTimerHandler(sd_event_source *s, uint64_t usec, void *userdata)
+{
+    reinterpret_cast<wm::WindowManager *>(userdata)->sendHandshake();
+    return 0;
+}
+
 static void onStateTransitioned(vector<WMAction> actions)
 {
     g_context->startTransitionWrapper(actions);
@@ -129,7 +138,7 @@ WindowManager::WindowManager()
     : wmcon{},
       id_alloc{}
 {
-    this->end_init = false;
+    this->subscribed = false;
 
     const char *path = getenv("AFM_APP_INSTALL_DIR");
     if (!path)
@@ -208,11 +217,59 @@ int WindowManager::init()
     double scale = static_cast<double>(dp_bg.height()) / css_bg.h;
     this->lc->setupArea(dp_bg, scale);
 
-    this->end_init = true;
+    sendHandshake();
 
     return 0; //init_layers();
 }
 
+void WindowManager::sendHandshake()
+{
+    struct timespec ts;
+
+    HMI_DEBUG("Check End Init");
+
+    if (this->subscribed && this->wmcon.getEndInit())
+    {
+        HMI_DEBUG("End Inited");
+        api_handshake();
+    }
+    else
+    {
+        HMI_DEBUG("Not End Init");
+        if (clock_gettime(CLOCK_BOOTTIME, &ts) != 0)
+        {
+            HMI_ERROR("Could't set time (clock_gettime() returns with error");
+            return;
+        }
+
+        uint64_t sleep = this->wmcon.getSleepTime();
+        uint64_t usec = (ts.tv_sec * 1000000000ULL) + ts.tv_nsec + (sleep * 1000000);
+
+        if (usec > 0)
+        {
+            usec /= 1000;
+        }
+
+        if (g_timer_endInit_src == nullptr)
+        {
+            int ret = sd_event_add_time(afb_daemon_get_event_loop(), &g_timer_endInit_src,
+                CLOCK_BOOTTIME, usec, 1, endInitTimerHandler, this);
+        }
+        else
+        {
+            sd_event_source_set_time(g_timer_endInit_src, usec);
+            sd_event_source_set_enabled(g_timer_endInit_src, SD_EVENT_ONESHOT);
+        }
+    }
+}
+
+void WindowManager::setSubscribed(bool flg)
+{
+    usleep(this->wmcon.getSleepTime() * 1000);
+
+    this->subscribed = flg;
+}
+
 result<int> WindowManager::api_request_surface(char const *appid, char const *drawing_name)
 {
     // TODO: application requests by old role,
@@ -473,7 +530,7 @@ void WindowManager::api_activate_window(char const *appid, char const *drawing_n
     unsigned layer = client->layerID();
 
     //TODO  Deactivate remote viewing app for remote view change to local view.
-    if(this->lc->hasRemoteLayer(layer) && this->wmcon.getAppIdToEcuName(appid) != "")
+    if(this->lc->hasRemoteLayer(layer) != "" && this->wmcon.getAppIdToEcuName(appid) != "")
     {
         HMI_DEBUG("Deactivate remote App %s", drawing_name);
         std::string tmp_area = this->wmcon.getAppIdToEcuName(appid) + ".split.sub";
@@ -517,7 +574,7 @@ void WindowManager::api_activate_window(char const *appid, char const *drawing_n
         return;
     }
 
-    this->wmcon.setAppIdToEcuName(id, "");
+    this->wmcon.setAppIdToEcuName(id, this->wmcon.getEcuName());
 
     reply(nullptr);
     if (req_num != g_app_list.currentRequestNumber())
@@ -634,6 +691,37 @@ void WindowManager::api_activate_surface_to_master(
     // g_app_list.removeFloatingSurface(client->surfaceID());
     // g_app_list.removeFloatingSurface(client);
 
+    unsigned layer = client->layerID();
+
+    //TODO  Deactivate remote viewing app for remote view change to local view.
+    if(this->lc->hasRemoteLayer(layer) != "" && this->wmcon.getAppIdToEcuName(appid) != "")
+    {
+        HMI_DEBUG("Deactivate remote App %s", drawing_name);
+        std::string tmp_area = this->wmcon.getAppIdToEcuName(appid) + ".split.sub";
+        HMI_DEBUG("Deactivate area name: %s", tmp_area.c_str());
+        this->wmcon.sendRequest("deactivateWindow", appid, drawing_name, tmp_area.c_str());
+
+        unsigned req_num = g_app_list.currentRequestNumber();
+        const char *c_role = this->convertRoleOldToNew(drawing_name);
+        bool end_draw_finished = true;
+        WMAction act
+        {
+            req_num,
+            client,
+            string(c_role),
+            "",
+            TaskVisible::REMOTE_INVISIBLE,
+            end_draw_finished,
+            TaskCarState::NO_TASK
+        };
+
+        this->lc->renderLayers();
+        this->lc->visibilityChange(act);
+
+        this->emit_invisible(role);
+        this->emit_deactivated(role);
+    }
+
     Task task = Task::TASK_ALLOCATE;
     unsigned req_num = 0;
     WMError ret = WMError::UNKNOWN;
@@ -880,10 +968,7 @@ int WindowManager::api_subscribe(afb_req req, int event_id)
 
 void WindowManager::api_handshake()
 {
-    if (this->end_init && this->wmcon.getEndInit())
-    {
-        this->send_event(kListEventName[Event_Handshake]);
-    }
+    this->send_event(kListEventName[Event_Handshake]);
 }
 
 void WindowManager::api_enddraw_for_remote(char const *appid, char const *drawing_name)
@@ -1425,9 +1510,11 @@ void WindowManager::startTransitionWrapper(vector<WMAction> &actions)
             act.req_num = req_num;
             act.client = client;
 
-            if (this->wmcon.getAppIdToEcuName(appid) != "")
+            std::string appToEcuName = this->wmcon.getAppIdToEcuName(appid);
+
+            if (this->wmcon.isRemoteEcu(appid))
             {
-                if (TaskVisible::VISIBLE == act.visible && this->wmcon.getAreaToEcuName(act.area.c_str()) == this->wmcon.getEcuName())
+                if (TaskVisible::VISIBLE == act.visible)
                 {
                     HMI_DEBUG("Set TaskVisible::REQ_REMOTE_VISIBLE");
                     act.visible = TaskVisible::REQ_REMOTE_VISIBLE;
@@ -1604,7 +1691,7 @@ void WindowManager::processForRemoteRequest(json_object *data)
             //       so convert role old to new
             const char *c_role = this->convertRoleOldToNew(drawing_name);
 
-            if(!this->lc->hasRemoteLayer(client->layerID()))
+            if(!this->wmcon.isRemoteEcu(appid))
             {
                 HMI_DEBUG("Deactivated");
                 return;
@@ -1628,6 +1715,8 @@ void WindowManager::processForRemoteRequest(json_object *data)
 
             this->emit_invisible(role);
             this->emit_deactivated(role);
+
+
         }
         else if ("flushDraw" == request)
         {