From 19670b6024baea31d2271b0799ced8689c42ba80 Mon Sep 17 00:00:00 2001
From: Alexander Tuxen <tuxen@silpion.de>
Date: Thu, 29 Sep 2022 01:17:35 +0200
Subject: [PATCH] Improved Surface handling and debug output

Lots of cleanup to do but this should work as a demo/test
---
 multiscreen-compositor/qml/IviChrome.qml |  4 --
 multiscreen-compositor/qml/main.qml      | 84 +++++++++++++++++++++---
 2 files changed, 74 insertions(+), 14 deletions(-)

diff --git a/multiscreen-compositor/qml/IviChrome.qml b/multiscreen-compositor/qml/IviChrome.qml
index c864624..d3aba36 100644
--- a/multiscreen-compositor/qml/IviChrome.qml
+++ b/multiscreen-compositor/qml/IviChrome.qml
@@ -72,9 +72,5 @@ Item {
     }
 
 
-    function setVisible(){
-        visible = true;
-    }
-
 
 }
diff --git a/multiscreen-compositor/qml/main.qml b/multiscreen-compositor/qml/main.qml
index 16629dc..a84858f 100644
--- a/multiscreen-compositor/qml/main.qml
+++ b/multiscreen-compositor/qml/main.qml
@@ -126,6 +126,53 @@ WaylandCompositor {
        console.log("Surface requested: client", client, "id", id, "version", version, "pid", client.processId);
     }
 
+    function createShellSurfaceItem(shellSurface, moveItem, output) {
+        // ![parenting]
+        var parentSurfaceItem = output.viewsBySurface[shellSurface.parentSurface];
+        var parent = parentSurfaceItem || output.surfaceArea;
+        // ![parenting]
+        var item = chromeComponent.createObject(parent, {
+            "shellSurface": shellSurface,
+            "moveItem": moveItem,
+            "output": output
+        });
+        if (parentSurfaceItem) {
+            item.x += output.position.x;
+            item.y += output.position.y;
+        }
+        output.viewsBySurface[shellSurface.surface] = item;
+        return item;
+    }
+
+    function handleShellSurfaceCreated(shellSurface) {
+        var moveItem = moveItemComponent.createObject(rootItem, {
+            "x": screens.objectAt(0).position.x,
+            "y": screens.objectAt(0).position.y,
+            "width": Qt.binding(function() { return shellSurface.surface.width; }),
+            "height": Qt.binding(function() { return shellSurface.surface.height; })
+        });
+        //! [createShellSurfaceItems]
+        for (var i = 0; i < screens.count; ++i)
+        {
+            var item = createShellSurfaceItem(shellSurface, moveItem, screens.objectAt(i));
+
+            item.shellSurface.toplevel.sendFullscreen(Qt.size(1024, 600));
+
+            var show = (active_screen == i) ? true : false;
+            item.visible = show;
+
+            if (active_screen == i)
+            {
+                var surfaceOnScreen = ({ 'surface': item, 'screen': active_screen });
+                activeSurfaces.push(surfaceOnScreen);
+            }
+        }
+        //! [createShellSurfaceItems]
+        //createShellSurfaceItem(shellSurface, moveItem, screens.objectAt(active_screen));
+    }
+
+
+    /*
     function createShellSurfaceItem(shellSurface, output) {
         console.log("createShellSurfaceItem: Creating surface", shellSurface, "on output", output);
         cleanUpApplist();
@@ -141,6 +188,7 @@ WaylandCompositor {
         var surfaceOnScreen = ({ 'surface': shellSurface, 'screen': active_screen });
         activeSurfaces.push(surfaceOnScreen);
     }
+    */
 
     function createIviSurfaceItem(iviSurface, output) {
         console.log("createIviSurfaceItem: surface", iviSurface, "output", output);
@@ -192,6 +240,8 @@ WaylandCompositor {
     // =============================================
     onSurfaceAboutToBeDestroyed: {
         // Removing this seems to have fixed the crashes.
+        // no, it didn't.
+        cleanUpApplist();
         //Also, with the new way to keep the applist clean of undefined objects, it is unnecessary.
         /*
         console.log("Surface will be destroyed", surface);
@@ -244,8 +294,9 @@ WaylandCompositor {
                 console.log("found IVI Shell Surface", i, id, item.surface.shellSurface, item.surface.shellSurface.surface)
                 return item.surface;
             }
-            else if ((item.surface.surface) && ( id === item.surface.surface.client.processId)) {
-                console.log("found XDG Shell Surface", i, id, item.surface)
+            else if ((item.surface.shellSurface) && ( id === item.surface.shellSurface.surface.client.processId)) {
+            //else if ((item.surface.surface) && ( id === item.surface.surface.client.processId)) {
+                console.log("found XDG Shell Surface", i, id, item.surface, "containing surface", item.surface.surface)
                 return item.surface;
             }
         }
@@ -282,19 +333,32 @@ WaylandCompositor {
             if( !surface ) return;
 
             console.log("Move", surface, "to screen", screenid)
-            surface.parent = screens.objectAt(screenid).surfaceArea;
+            if (surface.shellSurface.iviId)
+                surface.parent = screens.objectAt(screenid).surfaceArea;
+            else
+            {
+                surface.visible = (active_screen === screenid) ? true : false;
+            }
 
             updateScreenAssignment(surface, screenid);
         }
+
         function windowVisibility(windowid, show){
             var surface = findSurfaceById(windowid)
             if( !surface ) return;
 
             if (surface.shellSurface){
-                console.log("Set visable to", show,"for", surface.shellSurface)
+                console.log("Set visible to", show,"for", surface)
+                surface.visible = show
+            }
+            else if (surface.surface){
+                console.log("Set visible to", show,"for", surface.toplevel)
+                if (show)
+                    surface.toplevel.sendFullscreen(surface.surface.size);
+                else
+                    surface.toplevel.sendFullscreen(surface.surface.size);
+                    surface.toplevel.setMinimized();
             }
-            console.log("Set visable to", show,"for", surface)
-            surface.visible = show
         }
 
         function onHideWindow(windowid){
@@ -311,12 +375,12 @@ WaylandCompositor {
             console.log("Active surfaces:")
             for (var i=0; i < activeSurfaces.length; i++) {
                 var item = activeSurfaces[i];
-                if (item.surface.surface) {
-                    console.log("- Surface", i, item.surface, "pid", item.surface.surface.client.processId, "on screen", item.screen);
-                }
-                else if (item.surface.shellSurface) {
+                if (item.surface.shellSurface.iviId) {
                     console.log("- Surface", i, "iviId:", item.surface.shellSurface.iviId, item.surface, "on screen", item.screen);
                 }
+                else if (item.shellSurface) {
+                    console.log("- Surface", i, item.shellSurface, "pid", item.surface.surface.client.processId, "on screen", item.screen);
+                }
             }
         }
         function onGetScreen(windowid){
-- 
GitLab