Implementing app launch and app surface control workflow. Using WindowManager to...
[staging/HomeScreen.git] / WindowManager / src / windowmanager.cpp
index dcab2f4..d35db9b 100644 (file)
  */
 
 #include "windowmanager.hpp"
+#include <wayland-client.h>
+#include <QFile>
 
 //////////////////////////////////////////
 // THIS IS STILL UNDER HEAVY DEVELOPMENT!
 // DO NOT JUDGE THE SOURCE CODE :)
 //////////////////////////////////////////
 
+void* WindowManager::myThis = 0;
 
 WindowManager::WindowManager(QObject *parent) :
-    QObject(parent)
+    QObject(parent),
+    m_layouts(),
+    m_layoutNames(),
+    m_currentLayout(-1)
 {
     qDebug("WindowManager");
-#ifdef __arm__
-    ilmErrorTypes err;
-    if (true)//!ilm_isInitialized()) ...crashes...
-    {
-        err = ilm_init();
-        qDebug("ilm_init = %d", err);
-    }
+    // publish windowmanager interface
+    mp_windowManagerAdaptor = new WindowmanagerAdaptor((QObject*)this);
 
+    QDBusConnection dbus = QDBusConnection::sessionBus();
+    dbus.registerObject("/windowmanager", this);
+    dbus.registerService("org.agl.windowmanager");
 
-    t_ilm_uint screenID = 0;
-    t_ilm_uint width;
-    t_ilm_uint height;
-
-    err = ilm_getScreenResolution(screenID, &width, &height);
-    qDebug("ilm_getScreenResolution = %d", err);
-    qDebug("pWidth %d, pHeight %d", width, height);
+#ifdef __arm__
+    mp_processLayers = new QList<int>;
+    mp_surfaces = new QMap<t_ilm_uint, SurfaceInfo>;
 
+    ilmErrorTypes err;
 
-    t_ilm_layer layerId = 42; // does not matter
-    err = ilm_layerCreateWithDimension(&layerId, width, height);
-    qDebug("ilm_layerCreateWithDimension = %d", err);
-    qDebug("layerId = %d", layerId);
+    err = ilm_init();
+    qDebug("ilm_init = %d", err);
 
-    err = ilm_layerSetVisibility(layerId, true);
-    qDebug("ilm_layerSetVisibility = %d", err);
+    myThis = this;
+    err =  ilm_registerNotification(WindowManager::notificationFunc_static, this);
 
-    ilm_commitChanges();
 
-    err = ilm_displaySetRenderOrder(screenID, &layerId, 1);
-    qDebug("ilm_displaySetRenderOrder = %d", err);
+#endif
+}
 
-    ilm_commitChanges();
+WindowManager::~WindowManager()
+{
+    delete mp_windowManagerAdaptor;
+#ifdef __arm__
+    delete mp_surfaces;
 
-    err =  ilm_layerSetSourceRectangle(layerId, 0, 0, width, height);
-    qDebug("ilm_layerSetSourceRectangle = %d", err);
-    err =  ilm_layerSetDestinationRectangle(layerId, 0, 0, width, height);
-    qDebug("layerSetDestinationRectangle = %d", err);
+    ilm_destroy();
+#endif
+}
 
-    ilm_commitChanges();
+void WindowManager::dumpScene()
+{
+    qDebug("\n");
+    qDebug("current layout   : %d", m_currentLayout);
+    qDebug("available layouts: %d", m_layouts.size());
+    QMap<int, QList<SimpleRect> >::iterator i = m_layouts.begin();
 
-    t_ilm_float opacity = 1.0;
+    QList<int> result;
+    while (i != m_layouts.constEnd())
+    {
+        qDebug("--[id: %d]--[%s]--", i.key(), m_layoutNames.find(i.key()).value().toStdString().c_str());
+        qDebug("  %d surface areas", i.value().size());
+        for (int j = 0; j < i.value().size(); ++j)
+        {
+            qDebug("  -area %d", j);
+            qDebug("    -x     : %d", i.value().at(j).x);
+            qDebug("    -y     : %d", i.value().at(j).y);
+            qDebug("    -width : %d", i.value().at(j).width);
+            qDebug("    -height: %d", i.value().at(j).height);
+        }
 
-    err =  ilm_layerSetOpacity(layerId, opacity);
-    qDebug("ilm_layerSetOpacity = %d", err);
+        ++i;
+    }
 
+}
 
-    t_ilm_int length;
-    t_ilm_surface* pArray;
+#ifdef __arm__
 
-    err = ilm_getSurfaceIDs(&length, &pArray);
-    qDebug("ilm_getSurfaceIDs = %d", err);
-    qDebug("length %d pArray[0] %d", length, pArray[0]);
+void WindowManager::createNewLayer(int layerId)
+{
+    ilmErrorTypes err;
 
-    if (length > 0)
-    {
-        t_ilm_surface surfaceId = pArray[0];
+    t_ilm_uint screenID = 0;
+    t_ilm_uint width;
+    t_ilm_uint height;
 
-        err = ilm_layerAddSurface(layerId, surfaceId);
-        qDebug("ilm_layerAddSurface = %d", err);
+    err = ilm_getScreenResolution(screenID, &width, &height);
 
-        t_ilm_bool visibility;
-        err = ilm_surfaceGetVisibility(surfaceId, &visibility);
-        qDebug("ilm_surfaceGetVisibility = %d", err);
-        qDebug("visibility %d", visibility);
+    t_ilm_layer newLayerId = layerId;
+    err = ilm_layerCreateWithDimension(&newLayerId, width, height);
+    qDebug("ilm_layerCreateWithDimension = %d", err);
+    qDebug("layerIdWallpaper = %d", newLayerId);
 
-        err = ilm_surfaceSetVisibility(surfaceId, true);
-        qDebug("ilm_surfaceSetVisibility = %d", err);
+    err = ilm_layerSetVisibility(newLayerId, true);
+    qDebug("ilm_layerSetVisibility = %d", err);
 
-        err = ilm_surfaceSetOpacity(surfaceId, opacity);
-        qDebug("ilm_surfaceSetOpacity = %d", err);
+    t_ilm_float opacity = 1.0;
+    err =  ilm_layerSetOpacity(newLayerId, opacity);
 
-        err = ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, 200, 200);
-        qDebug("ilm_surfaceSetSourceRectangle = %d", err);
-        err = ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, 200, 200);
-        qDebug("surfaceSetDestinationRectangle = %d", err);
+    ilm_commitChanges();
+}
 
+void WindowManager::addSurfaceToLayer(int surfaceId, int layerId)
+{
+    t_ilm_int length;
+    t_ilm_layer* pArray;
 
+    ilm_getLayerIDs(&length, &pArray);
+    bool layerFound(false);
+    for (int i = 0; i< length; ++i)
+    {
+        if (layerId == pArray[i])
+        {
+            layerFound = true;
+        }
     }
 
-    ilm_commitChanges();
+    if (!layerFound)
+    {
+        createNewLayer(layerId);
+    }
 
-    struct ilmScreenProperties screenProperties;
-    struct ilmLayerProperties layerProperties;
     struct ilmSurfaceProperties surfaceProperties;
+    ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
+    qDebug("  origSourceWidth : %d", surfaceProperties.origSourceWidth);
+    qDebug("  origSourceHeight: %d", surfaceProperties.origSourceHeight);
 
-    err =  ilm_getPropertiesOfScreen(0, &screenProperties);
-    qDebug("ilm_getPropertiesOfScreen = %d", err);
-    err =  ilm_getPropertiesOfLayer(layerId, &layerProperties);
-    qDebug("ilm_getPropertiesOfLayer = %d", err);
-    err =  ilm_getPropertiesOfSurface(pArray[0], &surfaceProperties);
-    qDebug("ilm_getPropertiesOfSurface = %d", err);
-
-
-    qDebug("screen");
-    qDebug("t_ilm_uint %d", screenProperties.layerCount);          /*!< number of layers displayed on the screen */
-    //qDebug("t_ilm_layer* %d", screenProperties.layerIds[0]);          /*!< array of layer ids */
-    qDebug("t_ilm_uint %d", screenProperties.harwareLayerCount);   /*!< number of hardware layers */
-    qDebug("t_ilm_uint %d", screenProperties.screenWidth);         /*!< width value of screen in pixels */
-    qDebug("t_ilm_uint %d", screenProperties.screenHeight);        /*!< height value of screen in pixels */
-
-    qDebug("layer");
-    qDebug("t_ilm_float %f", layerProperties.opacity);         /*!< opacity value of the layer */
-    qDebug("t_ilm_uint %d", layerProperties.sourceX);          /*!< x source position value of the layer */
-    qDebug("t_ilm_uint %d", layerProperties.sourceY);          /*!< y source position value of the layer */
-    qDebug("t_ilm_uint %d", layerProperties.sourceWidth);      /*!< source width value of the layer */
-    qDebug("t_ilm_uint %d", layerProperties.sourceHeight);     /*!< source height value of the layer */
-    qDebug("t_ilm_uint %d", layerProperties.origSourceWidth);  /*!< original source width value of the layer */
-    qDebug("t_ilm_uint %d", layerProperties.origSourceHeight); /*!< original source height value of the layer */
-    qDebug("t_ilm_uint %d", layerProperties.destX);            /*!< x destination position value of the layer */
-    qDebug("t_ilm_uint %d", layerProperties.destY);            /*!< y desitination position value of the layer */
-    qDebug("t_ilm_uint %d", layerProperties.destWidth);        /*!< destination width value of the layer */
-    qDebug("t_ilm_uint %d", layerProperties.destHeight);       /*!< destination height value of the layer */
-    qDebug("ilmOrientation%d",  layerProperties.orientation);  /*!< orientation value of the layer */
-    qDebug("t_ilm_bool %d", layerProperties.visibility);       /*!< visibility value of the layer */
-    qDebug("t_ilm_uint %d", layerProperties.type);             /*!< type of layer */
-    qDebug("t_ilm_int  %d", layerProperties.creatorPid);       /*!< process id of application that created this layer */
-
-    qDebug("surface");
-    qDebug("t_ilm_float %f", surfaceProperties.opacity);                    /*!< opacity value of the surface */
-    qDebug("t_ilm_uint %d", surfaceProperties.sourceX);                     /*!< x source position value of the surface */
-    qDebug("t_ilm_uint %d", surfaceProperties.sourceY);                     /*!< y source position value of the surface */
-    qDebug("t_ilm_uint %d", surfaceProperties.sourceWidth);                 /*!< source width value of the surface */
-    qDebug("t_ilm_uint %d", surfaceProperties.sourceHeight);                /*!< source height value of the surface */
-    qDebug("t_ilm_uint %d", surfaceProperties.origSourceWidth);             /*!< original source width value of the surface */
-    qDebug("t_ilm_uint %d", surfaceProperties.origSourceHeight);            /*!< original source height value of the surface */
-    qDebug("t_ilm_uint %d", surfaceProperties.destX);                       /*!< x destination position value of the surface */
-    qDebug("t_ilm_uint %d", surfaceProperties.destY);                       /*!< y desitination position value of the surface */
-    qDebug("t_ilm_uint %d", surfaceProperties.destWidth);                   /*!< destination width value of the surface */
-    qDebug("t_ilm_uint %d", surfaceProperties.destHeight);                  /*!< destination height value of the surface */
-    qDebug("ilmOrientation %d", surfaceProperties.orientation);             /*!< orientation value of the surface */
-    qDebug("t_ilm_bool %d", surfaceProperties.visibility);                  /*!< visibility value of the surface */
-    qDebug("t_ilm_uint %d", surfaceProperties.frameCounter);                /*!< already rendered frames of surface */
-    qDebug("t_ilm_uint %d", surfaceProperties.drawCounter);                 /*!< content updates of surface */
-    qDebug("t_ilm_uint %d", surfaceProperties.updateCounter);               /*!< content updates of surface */
-    qDebug("t_ilm_uint %d", surfaceProperties.pixelformat);                 /*!< pixel format of surface */
-    qDebug("t_ilm_uint %d", surfaceProperties.nativeSurface);               /*!< native surface handle of surface */
-    qDebug("t_ilm_int  %d", surfaceProperties.creatorPid);                  /*!< process id of application that created this surface */
-    qDebug("ilmInputDevice %d", surfaceProperties.focus);                   /*!< bitmask of every type of device that this surface has focus in */
+    ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
+    ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
+    ilm_surfaceSetOpacity(surfaceId, 1.0);
+    ilm_surfaceSetVisibility(surfaceId, true);
 
-    err =  ilm_registerNotification(WindowManager::notificationFunc_static, this);
-#endif
-}
+    ilm_layerAddSurface(layerId, surfaceId);
 
-WindowManager::~WindowManager()
-{
-#ifdef __arm__
-    ilmErrorTypes err;
-    if (ilm_isInitialized())
-    {
-        err = ilm_destroy();
-        qDebug("ilm_destroy = %d", err);
-    }
-#endif
+    ilm_commitChanges();
 }
 
-#ifdef __arm__
 void WindowManager::notificationFunc_non_static(ilmObjectType object,
                                     t_ilm_uint id,
                                     t_ilm_bool created)
 {
-    qDebug("notificationFunc_non_static");
     if (ILM_SURFACE == object)
     {
         struct ilmSurfaceProperties surfaceProperties;
@@ -194,53 +163,74 @@ void WindowManager::notificationFunc_non_static(ilmObjectType object,
         if (created)
         {
             qDebug("Surface created, ID: %d", id);
-
-            ilm_layerAddSurface(42 /*always use layer 42 for now*/, id);
-            ilm_surfaceSetOpacity(id, 1.0);
-            ilm_surfaceSetVisibility(id, true);
             ilm_getPropertiesOfSurface(id, &surfaceProperties);
-            ilm_surfaceSetSourceRectangle(id, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
+            qDebug("  origSourceWidth : %d", surfaceProperties.origSourceWidth);
+            qDebug("  origSourceHeight: %d", surfaceProperties.origSourceHeight);
+
+            addSurfaceToLayer(id, surfaceProperties.creatorPid);
+
+            t_ilm_int length;
+            t_ilm_surface* pArray;
+            ilm_getSurfaceIDs(&length, &pArray);
+            ilm_layerSetRenderOrder(42, pArray, length);
 
             ilm_commitChanges();
+
+            SurfaceInfo surfaceInfo;
+            surfaceInfo.pid = surfaceProperties.creatorPid;
+            QString procInfoFileName = QString("/proc/") + QString::number(surfaceInfo.pid) + QString("/comm");
+            QFile procInfo(procInfoFileName);
+            if (procInfo.open(QIODevice::ReadOnly))
+            {
+                QTextStream in(&procInfo);
+                surfaceInfo.processName = in.readLine();
+                qDebug("surface id %d, pid %d: %s", id, surfaceInfo.pid, surfaceInfo.processName.toStdString().c_str());
+            }
+
+            mp_surfaces->insert(id, surfaceInfo);
+            ilm_surfaceAddNotification(id, surfaceCallbackFunction_static);
         }
         else
         {
             qDebug("Surface destroyed, ID: %d", id);
+            mp_surfaces->erase(mp_surfaces->find(id));
+            ilm_surfaceRemoveNotification(id);
         }
 
+        // rearrange surfaces on screen
         t_ilm_uint screenID = 0;
         t_ilm_uint width;
         t_ilm_uint height;
         ilm_getScreenResolution(screenID, &width, &height);
 
-        t_ilm_int length;
-        t_ilm_surface* pArray;
-
-        ilm_getSurfaceIDs(&length, &pArray);
-        qDebug("length %d", length);
-
+        qDebug("%d surfaces to show", mp_surfaces->count());
 
-        for (int i = 0; i < length; ++i)
+        QMap<t_ilm_uint, SurfaceInfo>::const_iterator i = mp_surfaces->constBegin();
+        int counter(0);
+        while (i != mp_surfaces->constEnd())
         {
-            //ilm_getPropertiesOfSurface(pArray[i], &surfaceProperties);
-            qDebug("place surface %d at x: %f, y: %f, width: %f, height: %f",
-                   pArray[i],
-                   i * (width / (1.0 * length)),
+            qDebug("place surface %d at x: %f, y: %d, width: %f, height: %d",
+                   i.key(),
+                   counter * (width / (1.0 * mp_surfaces->count())),
                    0,
-                   width / (1.0 * length),
+                   width / (1.0 * mp_surfaces->count()),
                    height);
-            ilm_surfaceSetDestinationRectangle(pArray[(int)i],
-                    i * (width / (1.0 * length)),
+            ilm_surfaceSetDestinationRectangle(i.key(),
+                    counter * (width / (1.0 * mp_surfaces->count())),
                     0,
-                    width / (1.0 * length),
+                    width / (1.0 * mp_surfaces->count()),
                     height);
+
+            ++i;
+            ++counter;
         }
 
+
         ilm_commitChanges();
     }
     if (ILM_LAYER == object)
     {
-        qDebug("Layer.. we don't care...");
+        //qDebug("Layer.. we don't care...");
     }
 }
 
@@ -249,7 +239,132 @@ void WindowManager::notificationFunc_static(ilmObjectType object,
                                             t_ilm_bool created,
                                             void* user_data)
 {
-    qDebug("notificationFunc_static");
-    static_cast<WindowManager*>(user_data)->notificationFunc_non_static(object, id, created);
+    static_cast<WindowManager*>(WindowManager::myThis)->notificationFunc_non_static(object, id, created);
+}
+
+
+
+
+void WindowManager::surfaceCallbackFunction_non_static(t_ilm_surface surface,
+                                    struct ilmSurfaceProperties* surfaceProperties,
+                                    t_ilm_notification_mask mask)
+{
+    qDebug("surfaceCallbackFunction_non_static changes for surface %d", surface);
+    if (ILM_NOTIFICATION_VISIBILITY & mask)
+    {
+        qDebug("ILM_NOTIFICATION_VISIBILITY");
+    }
+
+    if (ILM_NOTIFICATION_OPACITY & mask)
+    {
+        qDebug("ILM_NOTIFICATION_OPACITY");
+    }
+
+    if (ILM_NOTIFICATION_ORIENTATION & mask)
+    {
+        qDebug("ILM_NOTIFICATION_ORIENTATION");
+    }
+
+    if (ILM_NOTIFICATION_SOURCE_RECT & mask)
+    {
+        qDebug("ILM_NOTIFICATION_SOURCE_RECT");
+    }
+
+    if (ILM_NOTIFICATION_DEST_RECT & mask)
+    {
+        qDebug("ILM_NOTIFICATION_DEST_RECT");
+    }
+}
+
+void WindowManager::surfaceCallbackFunction_static(t_ilm_surface surface,
+                                    struct ilmSurfaceProperties* surfaceProperties,
+                                    t_ilm_notification_mask mask)
+
+{
+    static_cast<WindowManager*>(WindowManager::myThis)->surfaceCallbackFunction_non_static(surface, surfaceProperties, mask);
 }
 #endif
+
+int WindowManager::addLayout(int layoutId, const QString &layoutName, const QList<SimpleRect> &surfaceAreas)
+{
+    m_layouts.insert(layoutId, surfaceAreas);
+    m_layoutNames.insert(layoutId, layoutName);
+    qDebug("addLayout %d %s, size %d", layoutId, layoutName.toStdString().c_str(), surfaceAreas.size());
+
+    dumpScene();
+
+    return true;
+}
+
+QList<int> WindowManager::getAvailableLayouts(int numberOfAppSurfaces)
+{
+    QMap<int, QList<SimpleRect> >::iterator i = m_layouts.begin();
+
+    QList<int> result;
+    while (i != m_layouts.constEnd())
+    {
+        if (i.value().size() == numberOfAppSurfaces)
+        {
+            result.append(i.key());
+        }
+
+        ++i;
+    }
+
+    return result;
+}
+
+// maybe not needed anymore
+QList<SimplePoint> WindowManager::getAvailableSurfaces()
+{
+    QList<SimplePoint> points;
+    SimplePoint point;
+    point.x = 1;
+    point.y = 2;
+    points.append(point);
+    point.x = 11;
+    point.y = 22;
+    points.append(point);
+    point.x = 111;
+    point.y = 222;
+    points.append(point);
+
+    return points;
+}
+
+int WindowManager::getLayout()
+{
+    return m_currentLayout;
+}
+
+QString WindowManager::getLayoutName(int layoutId)
+{
+    return m_layoutNames.find(layoutId).value();
+}
+
+void WindowManager::setLayoutById(int layoutId)
+{
+    m_currentLayout = layoutId;
+
+    dumpScene();
+}
+
+void WindowManager::setLayoutByName(const QString &layoutName)
+{
+    QMap<int, QString>::iterator i = m_layoutNames.begin();
+    while (i != m_layoutNames.constEnd())
+    {
+        if (i.value() == layoutName)
+        {
+            m_currentLayout = i.key();
+        }
+        ++i;
+    }
+
+    dumpScene();
+}
+
+void WindowManager::setSurfaceToLayoutArea(int surfaceId, int layoutAreaId)
+{
+    dumpScene();
+}