Fix event dispatcher of surface and layer
[apps/agl-service-windowmanager.git] / src / wm_layer_control.cpp
index 8770236..31f6c4f 100644 (file)
@@ -23,7 +23,8 @@
 
 #define LC_AREA_PATH "/etc/areas.db"
 #define LC_LAYER_SETTING_PATH "/etc/layers_setting.json"
-#define LC_DEFAULT_AREA "normal.full"
+#define LC_DEFAULT_AREA "fullscreen"
+#define BACK_GROUND_LAYER "BackGroundLayer"
 
 using std::string;
 using std::vector;
@@ -122,11 +123,14 @@ void LayerControl::createNewLayer(unsigned id)
     HMI_INFO("create new ID :%d", id);
     struct rect rct = this->area2size[LC_DEFAULT_AREA];
     ilm_layerCreateWithDimension(&id, rct.w, rct.h);
-    ilm_layerSetSourceRectangle(id, rct.x, rct.y, rct.w, rct.h);
+    //ilm_layerSetSourceRectangle(id, rct.x, rct.y, rct.w, rct.h);
     //ilm_layerSetDestinationRectangle(id, rct.x, rct.y, rct.w, rct.h);
     ilm_layerSetOpacity(id, 1.0);
-    ilm_layerSetVisibility(id, ILM_TRUE);
+    ilm_layerSetVisibility(id, ILM_FALSE);
     ilm_commitChanges();
+    auto wm_layer = getWMLayer(id);
+    wm_layer->addLayerToState(id);
+    this->renderLayers();
 }
 
 unsigned LayerControl::getNewLayerID(const string& role, string* layer_name)
@@ -138,18 +142,38 @@ unsigned LayerControl::getNewLayerID(const string& role, string* layer_name)
         if(ret != 0)
         {
             *layer_name = l->layerName();
+            unsigned uid = l->getUuid();
+            this->lid2wmlid[ret] = uid;
             break;
         }
     }
     return ret;
 }
 
+shared_ptr<WMLayer> LayerControl::getWMLayer(unsigned layer)
+{
+    unsigned uuid = this->lid2wmlid[layer];
+    return this->wm_layers[uuid];
+}
+
+std::shared_ptr<WMLayer> LayerControl::getWMLayer(std::string layer_name)
+{
+    for(auto &l : this->wm_layers)
+    {
+        if(l->layerName() == layer_name)
+        {
+            return l;
+        }
+    }
+    return nullptr;
+}
+
 struct rect LayerControl::getAreaSize(const std::string& area)
 {
     return area2size[area];
 }
 
-void LayerControl::setupArea(double scaling)
+void LayerControl::setupArea(const rectangle& base_rct, double scaling)
 {
     struct rect rct;
     this->scaling = scaling;
@@ -160,8 +184,8 @@ void LayerControl::setupArea(double scaling)
 
     for (auto &i : this->area2size)
     {
-        i.second.x = static_cast<int>(scaling * i.second.x + 0.5);
-        i.second.y = static_cast<int>(scaling * i.second.y + 0.5);
+        i.second.x = base_rct.left() + static_cast<int>(scaling * i.second.x + 0.5);
+        i.second.y = base_rct.top() + static_cast<int>(scaling * i.second.y + 0.5);
         i.second.w = static_cast<int>(scaling * i.second.w + 0.5);
         i.second.h = static_cast<int>(scaling * i.second.h + 0.5);
 
@@ -185,18 +209,25 @@ WMError LayerControl::updateLayer(LayerState& layer_state)
     return WMError::SUCCESS;
 }
 
-WMError LayerControl::commitChange()
+WMError LayerControl::renderLayers()
 {
+    HMI_INFO("Commit change");
     WMError rc = WMError::SUCCESS;
+
+    // Check the number of layers
     vector<unsigned> ivi_l_ids;
-    for(const auto& l : this->wm_layers)
+    for(auto& l : this->wm_layers)
     {
         auto state = l->getLayerState();
+        HMI_DEBUG("layer %s", l->layerName().c_str());
         for(const auto& id : state.getIviIdList())
         {
+            HMI_DEBUG("Add %d", id);
             ivi_l_ids.push_back(id);
         }
     }
+
+    // Create render order
     t_ilm_layer* id_array = new t_ilm_layer[ivi_l_ids.size()];
     if(id_array == nullptr)
     {
@@ -211,17 +242,32 @@ WMError LayerControl::commitChange()
         ++count;
     }
 
+    // Display
     ilmErrorTypes ret = ilm_displaySetRenderOrder(this->screenID, id_array, ivi_l_ids.size());
     if(ret != ILM_SUCCESS)
     {
         this->undoUpdate();
         rc = WMError::FAIL;
     }
+    else
+    {
+        for(auto& l : this->wm_layers)
+        {
+            l->update();
+        }
+    }
+    ilm_commitChanges();
     delete id_array;
     return rc;
 }
 
-void LayerControl::undoUpdate() {}
+void LayerControl::undoUpdate()
+{
+    for(auto& l : this->wm_layers)
+    {
+        l->undo();
+    }
+}
 
 WMError LayerControl::loadLayerSetting(const string &path)
 {
@@ -231,7 +277,7 @@ WMError LayerControl::loadLayerSetting(const string &path)
     int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
     if (0 > ret)
     {
-        HMI_DEBUG("Could not open %s, so use default area information", path.c_str());
+        HMI_ERROR("Could not open %s", path.c_str());
         return WMError::FAIL;
     }
     HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
@@ -250,8 +296,9 @@ WMError LayerControl::loadLayerSetting(const string &path)
         json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
         HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
 
-        this->wm_layers.emplace_back(std::make_shared<WMLayer>(json_tmp));
+        this->wm_layers.emplace_back(std::make_shared<WMLayer>(json_tmp, i));
     }
+    json_object_put(json_obj);
 
     return WMError::SUCCESS;
 }
@@ -263,7 +310,7 @@ WMError LayerControl::loadAreaDb(const std::string& path)
     int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
     if (0 > ret)
     {
-        HMI_DEBUG("Could not open %s, so use default area information", path.c_str());
+        HMI_ERROR("Could not open %s", path.c_str());
         return WMError::FAIL;
     }
     HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
@@ -326,7 +373,6 @@ WMError LayerControl::loadAreaDb(const std::string& path)
 
 WMError LayerControl::layoutChange(const WMAction& action)
 {
-    WMError ret = WMError::FAIL;
     if (action.visible == TaskVisible::INVISIBLE)
     {
         // Visibility is not change -> no redraw is required
@@ -342,20 +388,24 @@ WMError LayerControl::layoutChange(const WMAction& action)
     // Layout Manager
     // WMError ret = this->setLayerSize(layer, action.area);
     auto rect = this->getAreaSize(action.area);
-    ilmErrorTypes err = ilm_layerSetDestinationRectangle(layer, rect.x, rect.y, rect.w, rect.h);
+    HMI_DEBUG("Set layout %d, %d, %d, %d",rect.x, rect.y, rect.w, rect.h);
+    ilm_layerSetSourceRectangle(layer, 0, 0, rect.w, rect.h);
+    ilm_commitChanges();
+    ilm_layerSetDestinationRectangle(layer, rect.x, rect.y, rect.w, rect.h);
+    ilm_commitChanges();
     for(auto &wm_layer: this->wm_layers)
     {
         if(wm_layer->hasLayerID(layer))
         {
+            wm_layer->setAreaToState(action.client->appID(), action.area);
+            /* TODO: manipulate state directly
             LayerState ls = wm_layer->getLayerState();
             ls.setArea(action.client->appID(), action.area);
+            wm_layer->dump(); */
         }
     }
-    if(err == ILM_SUCCESS)
-    {
-        ret = WMError::SUCCESS;
-    }
-    return ret;
+
+    return WMError::SUCCESS;
 }
 
 WMError LayerControl::visibilityChange(const WMAction& action)
@@ -367,17 +417,25 @@ WMError LayerControl::visibilityChange(const WMAction& action)
         return WMError::NOT_REGISTERED;
     }
 
-    if (action.visible != TaskVisible::INVISIBLE)
+    if (action.visible == TaskVisible::VISIBLE)
     {
         ret = this->makeVisible(action.client);
     }
-    else
+    else if (action.visible == TaskVisible::INVISIBLE)
     {
         ret = this->makeInvisible(action.client);
     }
     return ret;
 }
 
+void LayerControl::terminateApp(const shared_ptr<WMClient> client)
+{
+    for(auto& l : this->wm_layers)
+    {
+        l->terminateApp(client->layerID());
+    }
+}
+
 void LayerControl::dispatchCreateEvent(ilmObjectType object, unsigned id, bool created)
 {
     if (ILM_SURFACE == object)
@@ -388,14 +446,17 @@ void LayerControl::dispatchCreateEvent(ilmObjectType object, unsigned id, bool c
             ilmErrorTypes rc;
             rc = ilm_getPropertiesOfSurface(id, &sp);
             if(rc != ILM_SUCCESS)
+            {
+                HMI_ERROR("Failed to get surface %d property due to %d", id, ilm_getError());
                 return;
+            }
             this->cb.surfaceCreated(sp.creatorPid, id);
             ilm_surfaceAddNotification(id, surfaceCallback_static);
-            ilm_surfaceSetSourceRectangle(id, 0, 0, sp.origSourceWidth, sp.origSourceHeight);
+            ilm_surfaceSetVisibility(id, ILM_TRUE);
         }
         else
         {
-            // this->cb->surfaceDestroyed(id);
+            this->cb.surfaceDestroyed(id);
         }
     }
     if (ILM_LAYER == object)
@@ -403,11 +464,12 @@ void LayerControl::dispatchCreateEvent(ilmObjectType object, unsigned id, bool c
         if(created)
         {
             ilm_layerAddNotification(id, layerCallback_static);
-            // this->cb->layerCreated(id);
         }
         else
         {
-            // this->cb->layerDestroyed(id); // Means Application is dead.
+            // Ignore here. Nothing to do currently.
+            // Process of application dead is handled by Window Manager
+            // from binder notification
         }
     }
 }
@@ -416,62 +478,32 @@ void LayerControl::dispatchPropertyChangeEvent(unsigned id,
         struct ilmSurfaceProperties* sprop,
         t_ilm_notification_mask mask)
 {
-    pid_t pid = sprop->creatorPid;
-    HMI_DEBUG("pid : %d", pid);
-
+    /*
+      ILM_NOTIFICATION_CONTENT_AVAILABLE & ILM_NOTIFICATION_CONTENT_REMOVED
+      are not handled here, handled in create/destroy event
+     */
     if (ILM_NOTIFICATION_VISIBILITY & mask)
     {
-        //this->cb->surfaceVisibilityChanged(id, sprop->visibility);
+        HMI_DEBUG("surface %d turns visibility %d", id, sprop->visibility);
     }
     if (ILM_NOTIFICATION_OPACITY & mask)
     {
-    }
-    if (ILM_NOTIFICATION_ORIENTATION & mask)
-    {
+        HMI_DEBUG("surface %d turns opacity %f", id, sprop->opacity);
     }
     if (ILM_NOTIFICATION_SOURCE_RECT & mask)
     {
-        // this->cb->surfaceSourceRectChanged(id, )
+        HMI_DEBUG("surface %d source rect changes", id);
     }
     if (ILM_NOTIFICATION_DEST_RECT & mask)
     {
-        // this->cb->surfaceSourceRectChanged(id, )
-    }
-    if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
-    {
-    }
-    if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
-    {
-        /* application being down */
-        // m_appLayers.remove(pid);
+        HMI_DEBUG("surface %d dest rect changes", id);
     }
     if (ILM_NOTIFICATION_CONFIGURED & mask)
     {
-        /* qDebug("ILM_NOTIFICATION_CONFIGURED");
-        qDebug("  surfaceProperties %d", surface);
-        qDebug("    surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
-        qDebug("    surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);
-
-        if (surface == WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID) {
-            addSurfaceToLayer(surface, WINDOWMANAGER_LAYER_HOMESCREEN);
-            configureHomeScreenMainSurface(surface, surfaceProperties->origSourceWidth, surfaceProperties->origSourceHeight);
-        } else {
-            ilmErrorTypes result;
-            t_ilm_layer layer = addSurfaceToAppLayer(pid, surface);
-
-            if (layer != 0) {
-                configureAppSurface(surface,
-                                    surfaceProperties->origSourceWidth,
-                                    surfaceProperties->origSourceHeight);
-
-                result = ilm_layerAddSurface(layer, surface);
-                if (result != ILM_SUCCESS) {
-                    qDebug("ilm_layerAddSurface(%d,%d) failed.", layer, surface);
-                }
-                ilm_commitChanges();
-            }
-        }
-        updateScreen(); */
+        HMI_DEBUG("surface %d size %d, %d, %d, %d", id,
+            sprop->sourceX, sprop->sourceY, sprop->origSourceWidth, sprop->origSourceHeight);
+        ilm_surfaceSetSourceRectangle(id, 0, 0, sprop->origSourceWidth, sprop->origSourceHeight);
+        ilm_surfaceSetDestinationRectangle(id, 0, 0, sprop->origSourceWidth, sprop->origSourceHeight);
     }
 }
 
@@ -479,59 +511,25 @@ void LayerControl::dispatchPropertyChangeEvent(unsigned id,
         struct ilmLayerProperties* lprop,
         t_ilm_notification_mask mask)
 {
+    /*
+      ILM_NOTIFICATION_CONTENT_AVAILABLE & ILM_NOTIFICATION_CONTENT_REMOVED
+      are not handled here, handled in create/destroy event
+     */
     if (ILM_NOTIFICATION_VISIBILITY & mask)
     {
-        //this->cb->layerVisibilityChanged(id, sprop->visibility);
+        HMI_DEBUG("layer %d turns visibility %d", id, lprop->visibility);
     }
     if (ILM_NOTIFICATION_OPACITY & mask)
     {
-    }
-    if (ILM_NOTIFICATION_ORIENTATION & mask)
-    {
+        HMI_DEBUG("layer %d turns opacity %f", id, lprop->opacity);
     }
     if (ILM_NOTIFICATION_SOURCE_RECT & mask)
     {
-        // this->cb->surfaceSourceRectChanged(id, )
+        HMI_DEBUG("layer %d source rect changes", id);
     }
     if (ILM_NOTIFICATION_DEST_RECT & mask)
     {
-        // this->cb->surfaceSourceRectChanged(id, )
-    }
-    if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
-    {
-    }
-    if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
-    {
-        /* application being down */
-        // m_appLayers.remove(pid);
-    }
-    if (ILM_NOTIFICATION_CONFIGURED & mask)
-    {
-        /* qDebug("ILM_NOTIFICATION_CONFIGURED");
-        qDebug("  surfaceProperties %d", surface);
-        qDebug("    surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
-        qDebug("    surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);
-
-        if (surface == WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID) {
-            addSurfaceToLayer(surface, WINDOWMANAGER_LAYER_HOMESCREEN);
-            configureHomeScreenMainSurface(surface, surfaceProperties->origSourceWidth, surfaceProperties->origSourceHeight);
-        } else {
-            ilmErrorTypes result;
-            t_ilm_layer layer = addSurfaceToAppLayer(pid, surface);
-
-            if (layer != 0) {
-                configureAppSurface(surface,
-                                    surfaceProperties->origSourceWidth,
-                                    surfaceProperties->origSourceHeight);
-
-                result = ilm_layerAddSurface(layer, surface);
-                if (result != ILM_SUCCESS) {
-                    qDebug("ilm_layerAddSurface(%d,%d) failed.", layer, surface);
-                }
-                ilm_commitChanges();
-            }
-        }
-        updateScreen(); */
+        HMI_DEBUG("layer %d dest rect changes", id);
     }
 }
 
@@ -541,17 +539,21 @@ WMError LayerControl::makeVisible(const shared_ptr<WMClient> client)
     // Don't check here the client is not nullptr
     unsigned layer = client->layerID();
 
-    for(auto& wm_layer : this->wm_layers)
+    this->moveForeGround(client);
+
+    ilm_layerSetVisibility(layer, ILM_TRUE);
+
+    /* for(auto& wm_layer : this->wm_layers)
     {
         if(wm_layer->hasLayerID(layer))
         {
             LayerState ls = wm_layer->getLayerState();
             ls.addLayer(layer);;
         }
-    }
+    } */
 
     // Move foreground from back ground layer
-    for(auto& wm_layer : this->wm_layers)
+    /* for(auto& wm_layer : this->wm_layers)
     {
         if(wm_layer->layerName() == "BackGroundLayer")
         {
@@ -562,7 +564,7 @@ WMError LayerControl::makeVisible(const shared_ptr<WMClient> client)
             }
             break;
         }
-    }
+    } */
 
     return ret;
 }
@@ -570,32 +572,87 @@ WMError LayerControl::makeVisible(const shared_ptr<WMClient> client)
 WMError LayerControl::makeInvisible(const shared_ptr<WMClient> client)
 {
     WMError ret = WMError::SUCCESS;
-    // Don't check here the client is not nullptr
-    unsigned layer = client->layerID();
+    unsigned layer = client->layerID(); // Don't check here the client is not nullptr
+
+    bool mv_ok = this->moveBackGround(client);
 
-    for(auto& wm_layer : this->wm_layers)
+    if(!mv_ok)
+    {
+        HMI_INFO("make invisible client %s", client->appID().c_str());
+        ilm_layerSetVisibility(layer, ILM_FALSE);
+    }
+
+    //ilm_layerSetDestinationRectangle(layer, 0, 0, 0, 0);
+
+    /* for(auto& wm_layer : this->wm_layers)
     {
         if(wm_layer->hasLayerID(layer))
         {
             LayerState ls = wm_layer->getLayerState();
             ls.removeLayer(layer);;
         }
-    }
+    } */
 
-    // Move foreground from back ground layer
-    for(auto& wm_layer : this->wm_layers)
+
+
+    return ret;
+}
+
+bool LayerControl::moveBackGround(const shared_ptr<WMClient> client)
+{
+    bool ret = false;
+
+    // Move background from foreground layer
+    auto bg = this->getWMLayer(BACK_GROUND_LAYER);
+    if(bg != nullptr)
     {
-        if(wm_layer->layerName() == "BackGroundLayer")
+        HMI_DEBUG("client %s role %s", client->appID().c_str(), client->role().c_str());
+        unsigned layer = client->layerID();
+        if(bg->hasRole(client->role()))
         {
-            if(wm_layer->hasRole(client->role()))
-            {
-                LayerState ls = wm_layer->getLayerState();
-                ls.addLayer(layer);
-            }
-            break;
+            HMI_INFO("%s go to background", client->appID().c_str());
+            bg->addLayerToState(layer);
+            auto wm_layer = this->getWMLayer(layer);
+            wm_layer->removeLayerFromState(layer);
+            /* TODO: manipulate state directly
+            LayerState bg_ls = bg->getLayerState();
+            bg_ls.addLayer(layer);
+            LayerState ls = wm_layer->getLayerState();
+            ls.removeLayer(layer); */
+            bg->dump();
+            wm_layer->dump();
+            ret = true;
         }
     }
+    return ret;
+}
+
+bool LayerControl::moveForeGround(const shared_ptr<WMClient> client)
+{
+    bool ret = false;
 
+    // Move foreground from foreground layer
+    auto bg = this->getWMLayer(BACK_GROUND_LAYER);
+    if(bg != nullptr)
+    {
+        if(bg->hasRole(client->role()))
+        {
+            unsigned layer = client->layerID();
+            HMI_INFO("%s go to foreground", client->appID().c_str());
+            bg->removeLayerFromState(layer);
+            auto wm_layer = this->getWMLayer(layer);
+            wm_layer->addLayerToState(layer);
+            /* TODO: manipulate state directly
+            LayerState bg_ls = bg->getLayerState();
+            bg_ls.removeLayer(layer);
+            LayerState ls = wm_layer->getLayerState();
+            ls.addLayer(layer); */
+            bg->dump();
+            wm_layer->dump();
+            ret = true;
+        }
+    }
     return ret;
 }
+
 } // namespace wm
\ No newline at end of file