replace a SLOT macro with SIGNAL where a signal is triggered
[staging/HomeScreen.git] / HomeScreen / src / layouthandler.cpp
index a4d0f91..6aad54b 100644 (file)
@@ -1,17 +1,21 @@
 #include "layouthandler.h"
+#include <QTimerEvent>
 
 LayoutHandler::LayoutHandler(QObject *parent) :
     QObject(parent),
+    m_secondsTimerId(-1),
     mp_dBusWindowManagerProxy(0),
     mp_dBusPopupProxy(0),
-    m_visibleApps(),
-    m_invisibleApps(),
-    m_requestsToBeVisibleApps()
+    m_visibleSurfaces(),
+    m_invisibleSurfaces(),
+    m_requestsToBeVisibleSurfaces()
 {
     qDBusRegisterMetaType<SimplePoint>();
     qDBusRegisterMetaType<QList<SimplePoint> >();
-    qDBusRegisterMetaType<SimpleRect>();
-    qDBusRegisterMetaType<QList<SimpleRect> >();
+    qDBusRegisterMetaType<LayoutArea>();
+    qDBusRegisterMetaType<QList<LayoutArea> >();
+    qDBusRegisterMetaType<Layout>();
+    qDBusRegisterMetaType<QList<Layout> >();
 
     qDebug("D-Bus: connect to org.agl.windowmanager /windowmanager");
     mp_dBusWindowManagerProxy = new org::agl::windowmanager("org.agl.windowmanager",
@@ -23,6 +27,13 @@ LayoutHandler::LayoutHandler(QObject *parent) :
                                               "/Popup",
                                               QDBusConnection::sessionBus(),
                                               0);
+
+    QDBusConnection::sessionBus().connect("org.agl.windowmanager",
+                                       "/windowmanager",
+                                       "org.agl.windowmanager",
+                                       "surfaceVisibilityChanged",
+                                       this,
+                                       SIGNAL(surfaceVisibilityChanged(int,bool)));
 }
 
 LayoutHandler::~LayoutHandler()
@@ -31,14 +42,14 @@ LayoutHandler::~LayoutHandler()
     delete mp_dBusWindowManagerProxy;
 }
 
-void LayoutHandler::setUpLayers()
+void LayoutHandler::setUpLayouts()
 {
-    qDebug("setUpLayers");
-    QList<SimpleRect> surfaceAreas;
-    SimpleRect surfaceArea;
+    qDebug("setUpLayouts");
+    QList<LayoutArea> surfaceAreas;
+    LayoutArea surfaceArea;
 
     const int SCREEN_WIDTH = 1080;
-    const int SCREEN_HEIGHT = 1080;
+    const int SCREEN_HEIGHT = 1920;
 
     const int STATUSBAR_HEIGHT = 60;
     const int STATUSBAR_WIDTH = SCREEN_WIDTH;
@@ -50,6 +61,7 @@ void LayoutHandler::setUpLayers()
     const int CONTROLBAR_Y = SCREEN_HEIGHT - CONTROLBAR_HEIGHT;
 
 
+    // only one Layout for CES2017 needed
     // layout 1:
     // one app surface, statusbar, control bar
     surfaceArea.x = 0;
@@ -61,7 +73,7 @@ void LayoutHandler::setUpLayers()
 
     mp_dBusWindowManagerProxy->addLayout(1, "one app", surfaceAreas);
 
-
+    /*
     surfaceAreas.clear();
 
     // layout 2:
@@ -77,7 +89,7 @@ void LayoutHandler::setUpLayers()
 
     // bottom surface
     surfaceArea.x = 0;
-    surfaceArea.y = STATUSBAR_HEIGHT / 2;
+    surfaceArea.y = SCREEN_HEIGHT / 2;
     surfaceArea.width = SCREEN_WIDTH;
     surfaceArea.height = (SCREEN_HEIGHT - STATUSBAR_HEIGHT - CONTROLBAR_HEIGHT) / 2;
 
@@ -107,74 +119,172 @@ void LayoutHandler::setUpLayers()
 
     surfaceAreas.append(surfaceArea);
 
-    mp_dBusWindowManagerProxy->addLayout(3, "side by side", surfaceAreas);
+    mp_dBusWindowManagerProxy->addLayout(3, "side by side", surfaceAreas);*/
+}
 
+void LayoutHandler::showAppLayer()
+{
+    // POPUP=0, HOMESCREEN_OVERLAY=1, APPS=2, HOMESCREEN=3
+    mp_dBusWindowManagerProxy->showLayer(2); // TODO: enum
+}
+
+void LayoutHandler::hideAppLayer()
+{
+    // POPUP=0, HOMESCREEN_OVERLAY=1, APPS=2, HOMESCREEN=3
+    mp_dBusWindowManagerProxy->hideLayer(2); // TODO: enum
 }
 
 void LayoutHandler::makeMeVisible(int pid)
 {
     qDebug("makeMeVisible %d", pid);
-    m_requestsToBeVisibleApps.append(pid);
 
-    qDebug("m_visibleApps %d", m_visibleApps.size());
-    qDebug("m_invisibleApps %d", m_invisibleApps.size());
-    qDebug("m_requestsToBeVisibleApps %d", m_requestsToBeVisibleApps.size());
+    m_requestsToBeVisiblePids.append(pid);
 
-    QList<int> availableLayouts = mp_dBusWindowManagerProxy->getAvailableLayouts(m_visibleApps.size() + m_requestsToBeVisibleApps.size());
-    if (0 == availableLayouts.size())
+    // callback every second
+    if (-1 != m_secondsTimerId)
     {
-        // no layout fits the need!
-        // replace the last app
-        qDebug("no layout fits the need!");
-        qDebug("replace the last app");
-
-        m_invisibleApps.append(m_visibleApps.last());
-        m_visibleApps.removeLast();
+        killTimer(m_secondsTimerId);
+        m_secondsTimerId = -1;
+    }
+    m_secondsTimerId = startTimer(1000);
+}
 
-        for (int i = 0; i < m_visibleApps.size(); ++i)
-        {
-            mp_dBusWindowManagerProxy->setSurfaceToLayoutArea(i, i);
-        }
+void LayoutHandler::checkToDoQueue()
+{
+    if ((-1 != m_secondsTimerId) && (0 == m_requestsToBeVisiblePids.size()))
+    {
+        killTimer(m_secondsTimerId);
+        m_secondsTimerId = -1;
     }
-    if (1 == availableLayouts.size())
+
+    if (0 != m_requestsToBeVisiblePids.size())
     {
-        // switch to new layout
-        qDebug("switch to new layout %d", availableLayouts.at(0));
-        m_visibleApps.append(m_requestsToBeVisibleApps);
-        m_requestsToBeVisibleApps.clear();
+        int pid = m_requestsToBeVisiblePids.at(0);
+        qDebug("pid %d wants to be visible", pid);
 
-        mp_dBusWindowManagerProxy->setLayoutById(availableLayouts.at(0));
-        for (int i = 0; i < m_visibleApps.size(); ++i)
+        QList<int> allSurfaces;
+        allSurfaces = mp_dBusWindowManagerProxy->getAllSurfacesOfProcess(pid);
+        if (0 == allSurfaces.size())
         {
-            mp_dBusWindowManagerProxy->setSurfaceToLayoutArea(i, i);
+            qDebug("no surfaces for pid %d. retrying!", pid);
         }
-    }
-    if (1 < availableLayouts.size())
-    {
-        // more than one layout possible! Ask user.
-        qDebug("more than one layout possible! Ask user.");
-
-        QStringList choices;
-        for (int i = 0; i < availableLayouts.size(); ++i)
+        else
         {
-            choices.append(mp_dBusWindowManagerProxy->getLayoutName(availableLayouts.at(i)));
+            m_requestsToBeVisiblePids.removeAt(0);
+            qSort(allSurfaces);
+
+            if (0 != allSurfaces.size())
+            {
+                if (-1 == m_visibleSurfaces.indexOf(allSurfaces.at(0)))
+                {
+                    qDebug("already visible");
+                }
+                if (-1 == m_invisibleSurfaces.indexOf(allSurfaces.at(0)))
+                {
+                    m_invisibleSurfaces.removeAt(m_invisibleSurfaces.indexOf(allSurfaces.at(0)));
+                }
+                if (-1 == m_requestsToBeVisibleSurfaces.indexOf(allSurfaces.at(0)))
+                {
+                    m_requestsToBeVisibleSurfaces.append(allSurfaces.at(0));
+                }
+
+                qDebug("m_visibleSurfaces %d", m_visibleSurfaces.size());
+                qDebug("m_invisibleSurfaces %d", m_invisibleSurfaces.size());
+                qDebug("m_requestsToBeVisibleSurfaces %d", m_requestsToBeVisibleSurfaces.size());
+
+                QList<int> availableLayouts = mp_dBusWindowManagerProxy->getAvailableLayouts(1); // one app only for CES2017
+                if (1 == availableLayouts.size())
+                {
+                    qDebug("active layout: %d", availableLayouts.at(0));
+                    m_invisibleSurfaces.append(m_visibleSurfaces);
+                    m_visibleSurfaces.clear();
+                    m_visibleSurfaces.append(m_requestsToBeVisibleSurfaces);
+                    m_requestsToBeVisibleSurfaces.clear();
+
+                    mp_dBusWindowManagerProxy->setLayoutById(availableLayouts.at(0));
+                    for (int i = 0; i < m_visibleSurfaces.size(); ++i)
+                    {
+                        mp_dBusWindowManagerProxy->setSurfaceToLayoutArea(m_visibleSurfaces.at(i), i);
+                    }
+                }
+                else
+                {
+                    qDebug("this should not happen!?");
+                }
+            }
         }
+    }
+}
+
+QList<int> LayoutHandler::requestGetAllSurfacesOfProcess(int pid)
+{
+    qDebug("requestGetAllSurfacesOfProcess %d", pid);
 
-        mp_dBusPopupProxy->showPopupComboBox("Select Layout", choices);
+    return mp_dBusWindowManagerProxy->getAllSurfacesOfProcess(pid);
+}
+
+int LayoutHandler::requestGetSurfaceStatus(int surfaceId)
+{
+    int result = -1;
 
+    if (-1 != m_visibleSurfaces.indexOf(surfaceId))
+    {
+        result = 0;
+    }
+    if (-1 != m_invisibleSurfaces.indexOf(surfaceId))
+    {
+        result = 1;
     }
+    if (-1 != m_requestsToBeVisibleSurfaces.indexOf(surfaceId))
+    {
+        result = 1;
+    }
+
+    return result;
+}
+
+void LayoutHandler::requestRenderSurfaceToArea(int surfaceId, int layoutArea)
+{
+    qDebug("requestRenderSurfaceToArea %d %d", surfaceId, layoutArea);
+}
+
+bool LayoutHandler::requestRenderSurfaceToAreaAllowed(int surfaceId, int layoutArea)
+{
+    qDebug("requestRenderSurfaceToAreaAllowed %d %d", surfaceId, layoutArea);
+    bool result = true;
+    return result;
+}
+
+void LayoutHandler::requestSurfaceIdToFullScreen(int surfaceId)
+{
+    qDebug("requestSurfaceIdToFullScreen %d", surfaceId);
 }
 
 void LayoutHandler::setLayoutByName(QString layoutName)
 {
     // switch to new layout
     qDebug("setLayout: switch to new layout %s", layoutName.toStdString().c_str());
-    m_visibleApps.append(m_requestsToBeVisibleApps);
-    m_requestsToBeVisibleApps.clear();
+    m_visibleSurfaces.append(m_requestsToBeVisibleSurfaces);
+    m_requestsToBeVisibleSurfaces.clear();
 
     mp_dBusWindowManagerProxy->setLayoutByName(layoutName);
-    for (int i = 0; i < m_visibleApps.size(); ++i)
+    for (int i = 0; i < m_visibleSurfaces.size(); ++i)
     {
         mp_dBusWindowManagerProxy->setSurfaceToLayoutArea(i, i);
     }
 }
+
+void LayoutHandler::requestSurfaceVisibilityChanged(int surfaceId, bool visible)
+{
+    qDebug("requestSurfaceVisibilityChanged %d %s", surfaceId, visible ? "true" : "false");
+    emit surfaceVisibilityChanged(surfaceId, visible);
+}
+
+void LayoutHandler::timerEvent(QTimerEvent *e)
+{
+    if (e->timerId() == m_secondsTimerId)
+    {
+        checkToDoQueue();
+    }
+}
+