From 618c02c14b016dd8bc36a0d4bb1e601f2e7b542a Mon Sep 17 00:00:00 2001
From: willifan <willifan@protonmail.com>
Date: Sun, 17 Mar 2024 21:55:28 +0100
Subject: [PATCH] Added monitor check

---
 .gitignore                   |   4 +-
 Struktur.md                  |  53 ++++++++++++++++--
 newbar.yuck                  |   8 +--
 scripts/clients/src/main.cpp | 101 ++++++++++++++++++++---------------
 scripts/monitors.sh          |   3 ++
 scripts/test.sh              |  50 +++++++++--------
 6 files changed, 143 insertions(+), 76 deletions(-)

diff --git a/.gitignore b/.gitignore
index fae3105..5ad2fc5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1 @@
-scripts/clients/build/CMakeFiles/clients.dir/src/main.cpp.o.d
-scripts/clients/build/CMakeFiles/clients.dir/src/main.cpp.o
-scripts/clients/build/clients
+scripts/clients/build/*
\ No newline at end of file
diff --git a/Struktur.md b/Struktur.md
index d0f2b16..c057975 100644
--- a/Struktur.md
+++ b/Struktur.md
@@ -2,12 +2,57 @@
     - normal
         - ID: int (ID of workspace)
         - occupied: bool (if workspace is occupied)
-        - monitorID: int (ID of monitor workspace is on)
-        - active: bool (if workspace is open)
+        - activeOn: int (ID of monitor workspace is on)
         - icon: string (path of icon of client)
     - special
         - ID: int (ID of workspace)
         - occupied: bool (if workspace is occupied)
-        - monitorID: int (ID of monitor workspace is on)
-        - active: bool (if workspace is open)
+        - activeOn: int (ID of monitor workspace is on)
         - icon: string (path of icon of client)
+
+
+Events:
+
+workspacev2>>WORKSPACEID,WORKSPACENAME
+
+workspace changed, update activeOn
+
+
+createworkspacev2>>WORKSPACEID,WORKSPACENAME
+
+workspace got created, update activeOn
+
+
+destroyworkspacev2>>WORKSPACEID,WORKSPACENAME
+
+workspace got destroyed, update activeOn
+
+
+moveworkspacev2>>WORKSPACEID,WORKSPACENAME,MONNAME
+
+workspace got moved to other monitor, update activeOn
+
+
+activespecial>>WORKSPACENAME,MONNAME
+
+
+
+
+activewindowv2>>WINDOWADDRESS
+
+client focus changed, update icon
+
+
+openwindow>>WINDOWADDRESS,WORKSPACENAME,WINDOWCLASS,WINDOWTITLE
+
+new client, update icon
+
+
+closewindow>>WINDOWADDRESS
+
+closed client, update icon
+
+
+movewindowv2>>WINDOWADDRESS,WORKSPACENAME
+
+client moved to other workspace, update icon
\ No newline at end of file
diff --git a/newbar.yuck b/newbar.yuck
index ec26b08..4ac90ee 100644
--- a/newbar.yuck
+++ b/newbar.yuck
@@ -25,8 +25,8 @@
         :spacing 3
         (for workspace in workspaces
             (eventbox
-                :onclick `/home/willifan/.config/scripts/workspaces.sh ${workspace.normal.ID}`
-                :class {workspace.normal.monitorID == "${monitor}" ? "activeWorkspace" : "smallBox"}
+                :onclick `/home/willifan/.config/scripts/workspaces.sh ${workspace.normal.id}`
+                :class {workspace.normal.activeOn == "${monitor}" ? "activeWorkspace" : "smallBox"}
                 :valign "center"
                 :halign "center"
                 :height 20
@@ -36,9 +36,9 @@
                     :space-evenly false
                     (smallSpacer)
                     (label
-                        :class {workspace.normal.occupied == true ? "" : "unoccupiedWorkspace"}
+                        :class {workspace.normal.occupied > 0 ? "" : "unoccupiedWorkspace"}
                         :width 16
-                        :text "${workspace.normal.ID}")
+                        :text "${workspace.normal.id}")
                     (imageDPI
                         :visible "${workspace.normal.occupied > 0 ? true : false}"
                         :image "${workspace.normal.icon}"
diff --git a/scripts/clients/src/main.cpp b/scripts/clients/src/main.cpp
index 91a3859..4d252f6 100644
--- a/scripts/clients/src/main.cpp
+++ b/scripts/clients/src/main.cpp
@@ -81,13 +81,54 @@ std::string getIcon(std::string windowAddress)
 }
 
 
-void getWorkspaces()
+json getWorkspace(json workspaceInput, char mode)
+{
+	json workspaceOutput;
+
+	json monitorsInput = json::parse(command("hyprctl monitors -j"));
+
+	std::string keyValue;
+
+	if (mode == 'n')
+	{
+		keyValue = "id";
+	}
+	else if (mode == 's')
+	{
+		keyValue = "name";
+	}
+
+	workspaceOutput[keyValue] = workspaceInput[keyValue];
+
+	for (auto& monitor : monitorsInput)
+	{
+		if (monitor["activeWorkspace"][keyValue] == workspaceInput[keyValue])
+		{
+			workspaceOutput["activeOn"] = monitor[keyValue];
+		}
+	}
+	
+	workspaceOutput["occupied"] = workspaceInput["windows"];
+	
+	if (!(workspaceInput["lastwindow"] == "0x0"))
+	{
+		workspaceOutput["icon"] = getIcon(workspaceInput["lastwindow"]);
+	}
+	else
+	{
+		workspaceOutput["icon"] = "";
+	}
+
+	return workspaceOutput;
+
+}
+
+
+json getAllWorkspaces()
 {
 
 	workspacesInput = json::parse(command("hyprctl workspaces -j"));
 
-	workspacesOutput.clear();
-
 	int specialIndex = 0;
 
 	for(auto& workspace : workspacesInput)
@@ -96,49 +137,21 @@ void getWorkspaces()
 		{
 			int index = workspace["id"].get<int>() - 1;
 
-			workspacesOutput[index]["normal"]["ID"] = index + 1;
-
-			workspacesOutput[index]["normal"]["occupied"] = workspace["windows"];
-			
-			workspacesOutput[index]["normal"]["monitorID"] = workspace["monitorID"];
-			
-			if (!(workspace["lastwindow"] == "0x0"))
-			{
-				
-				workspacesOutput[index]["normal"]["icon"] = getIcon(workspace["lastwindow"]);
-				
-			}
-			else
-			{
-				workspacesOutput[index]["normal"]["icon"] = "";
-			}
-
+			workspacesOutput[index]["normal"].clear();
+			workspacesOutput[index]["normal"] = getWorkspace(workspace, 'n');
 		}
 		else if (std::string(workspace["name"]).find("special:") == 0)
 		{
-			
-			workspacesOutput[specialIndex]["special"]["ID"] = specialIndex;
-
-			workspacesOutput[specialIndex]["special"]["name"] = workspace["name"];
-
-			workspacesOutput[specialIndex]["special"]["occupied"] = workspace["windows"];
-			
-			workspacesOutput[specialIndex]["special"]["monitorID"] = workspace["monitorID"];
-			
-			if (!(workspace["lastwindow"] == "0x0"))
-			{
-				
-				workspacesOutput[specialIndex]["special"]["icon"] = getIcon(workspace["lastwindow"]);
-				
-			}
+			workspacesOutput[specialIndex]["special"].clear();
+			workspacesOutput[specialIndex]["special"] = getWorkspace(workspace, 's');
 
 			specialIndex++;
-
 		}
 
 	}
 
-	std::cout << workspacesOutput << std::endl;
+	return workspacesOutput;
+
 }
 
 
@@ -173,27 +186,27 @@ void handle(std::string message)
 
 	if (message.find("workspace") == 0)
 	{
-		getWorkspaces();
+		std::cout << getAllWorkspaces() << std::endl;
 	}
 	else if (message.find("moveworkspace") == 0)
 	{
-		getWorkspaces();
+		std::cout << getAllWorkspaces() << std::endl;
 	}
 	else if (message.find("openwindow") == 0)
 	{
-		getWorkspaces();
+		std::cout << getAllWorkspaces() << std::endl;
 	}
 	else if (message.find("closewindow") == 0)
 	{
-		getWorkspaces();
+		std::cout << getAllWorkspaces() << std::endl;
 	}
 	else if (message.find("movewindow") == 0)
 	{
-		getWorkspaces();
+		std::cout << getAllWorkspaces() << std::endl;
 	}
 	else if (message.find("activewindow") == 0)
 	{
-		getWorkspaces();
+		std::cout << getAllWorkspaces() << std::endl;
 	}
 	
 
@@ -202,6 +215,8 @@ void handle(std::string message)
 int main(int argc, char const *argv[])
 {
 
+	std::cout << getAllWorkspaces() << std::endl;
+
 	std::string socketPath = "/tmp/hypr/" + std::string(std::getenv("HYPRLAND_INSTANCE_SIGNATURE")) + "/.socket2.sock";
 	
 
diff --git a/scripts/monitors.sh b/scripts/monitors.sh
index 1e349ea..4293f61 100755
--- a/scripts/monitors.sh
+++ b/scripts/monitors.sh
@@ -44,6 +44,8 @@ generate() {
 
     eww close-all
 
+    sleep 2
+
   	for ((i = 0; i < ${#id[@]}; i++)); do
   		scale_value=$(echo "${scale[$i]}" | bc -l)
 
@@ -56,6 +58,7 @@ generate() {
         monitorID=$(xrandr --listactivemonitors | grep +${name[$i]} | awk '{sub(/.$/,"",$1); print $1}')
 
         eww open bar --id bar${monitorID} --arg monitor=${monitorID} --arg width=${new_width} --arg height=25
+        sleep 2
 
   	done
 
diff --git a/scripts/test.sh b/scripts/test.sh
index 48d0ff4..3c5ce6a 100755
--- a/scripts/test.sh
+++ b/scripts/test.sh
@@ -1,26 +1,32 @@
 #!/bin/bash
 
+getIcon() {
+    local desktop=""
+    
+    # Search for desktop files by WM class
+    if [ -n "$1" ]; then
+        desktop=$(grep -l "StartupWMClass.*$1$" /usr/share/applications/*.desktop)
+        [ -z "$desktop" ] && desktop=$(grep -Pl "Name\s*=\s*$1$" /usr/share/applications/*.desktop)
+        [ -z "$desktop" ] && desktop=$(ls /usr/share/applications/${1}.desktop 2>/dev/null)
+    fi
+    
+    # If not found by WM class, search by process name
+    if [ -z "$desktop" ] && [ -n "$2" ]; then
+        desktop=$(pgrep -fl "$2" | awk '{print $2}' | grep -m1 -o '[^/]*\.desktop' | head -n1)
+        [ -n "$desktop" ] && desktop="/usr/share/applications/$desktop"
+    fi
+    
+    # If not found by WM class or process name, search by name or description
+    if [ -z "$desktop" ] && [ -n "$1" ]; then
+        desktop=$(grep -Pl "$1" /usr/share/applications/*.desktop)
+    fi
+    
+    echo "$desktop"
+}
 
-if [ $1 ]; then
-    desktop=$(grep -l "StartupWMClass.*$1$" /usr/share/applications/*.desktop)
-    if [ -z "$desktop" ]; then
-        desktop=$(grep -Pl "Name\s*=\s*$1$" /usr/share/applications/*.desktop)
-    fi
-    if [ -z "$desktop" ]; then
-        desktop=$(ls /usr/share/applications | grep -l /usr/share/applications/${1}.desktop)
-    fi
+desktop=$(getIcon "$1" "$2")
+if [ -n "$desktop" ]; then
+    image=$(awk -F'[[:space:]]*=[[:space:]]*' '/Icon[[:space:]]*=/ && !seen[$2]++ {print $2; exit}' "$desktop")
+    icon="/usr/share/icons/Papirus-Dark/128x128/apps/${image}.svg"
+    echo -n "$icon"
 fi
-if [ $2 ]; then
-    if [ -z "$desktop" ]; then
-        process=$(ps -p ${2} -o comm=)
-        desktop=$(grep -Pl "$process" /usr/share/applications/*.desktop)
-    fi
-fi
-if [ $1 ]; then
-    if [ -z "$desktop" ]; then
-        desktop=$(grep -Pli "$1" /usr/share/applications/*.desktop)
-    fi
-fi
-image=$(awk -F'[[:space:]]*=[[:space:]]*' '/Icon[[:space:]]*=/ && !seen[$2]++ {print $2; exit}' $desktop)
-icon="/usr/share/icons/Papirus-Dark/128x128/apps/${image}.svg"
-echo -n $icon
\ No newline at end of file