Way to many changes, new start

This commit is contained in:
willifan 2024-04-01 15:24:03 +02:00
parent 63904bec07
commit a95d18c761
25 changed files with 1799 additions and 334 deletions
scripts/clients/src

View file

@ -2,6 +2,7 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <cstdlib>
#include <cstdio>
@ -13,36 +14,37 @@
#include "nlohmann/json.hpp"
#include <map>
using json = nlohmann::json;
json workspacesInput, workspacesOutput;
json workspacesOutput;
std::map<std::string, std::string> iconMap;
std::map<int, std::string> monitorMap;
const std::map<std::string, int> specialWorkspaceMap = {
{"special:super", 0},
{"special:ctrl", 1},
{"special:alt", 2},
{"special:gr", 3}
};
/*json workspacesOutput = json::parse(R"(
{
"normal": [
{
"occupied": bool,
"monitorID": int,
"active": bool,
"activeOn": str
"icon": str
"id": int
"occupied": int
}
],
"special": [
{
"ID": int,
"name": str,
"occupied": bool,
"monitorID": int,
"active": bool,
"activeOn": str
"icon": str
"id": str
"occupied": int
}
]
}
@ -77,10 +79,9 @@ std::string command(std::string inputCommand)
void generateIconMap()
{
json clients = json::parse(command("hyprctl clients -j"));
for (json& client : clients)
for (const json& client : clients)
{
std::string pid = std::to_string(static_cast<int>(client["pid"]));
@ -103,84 +104,78 @@ void generateIconMap()
iconMap[client["address"]] = command(test);
}
return;
}
void generateMonitorMap()
{
json monitorsInput = json::parse(command("hyprctl monitors -j"));
monitorMap.clear();
for (json& monitor : monitorsInput)
for (const json& monitor : monitorsInput)
{
monitorMap[monitor["activeWorkspace"]["id"]] = std::to_string(static_cast<int>(monitor["id"]));
}
}
json getWorkspace(json workspaceInput, char mode)
json getWorkspace(json workspaceInput)
{
json workspaceOutput;
std::string keyValue;
if (mode == 'n')
keyValue = "id";
else if (mode == 's')
keyValue = "name";
workspaceOutput[keyValue] = workspaceInput[keyValue];
if (workspaceInput["id"] > 0) //normal workspace
{
workspaceOutput["id"] = workspaceInput["id"];
}
else //special workspace
{
workspaceOutput["id"] = workspaceInput["name"];
}
workspaceOutput["activeOn"] = monitorMap[workspaceInput["id"]];
workspaceOutput["occupied"] = workspaceInput["windows"];
if (!(workspaceInput["lastwindow"] == "0x0"))
if (!(workspaceInput["lastwindow"] == "0x0")) //workspace not empty
{
workspaceOutput["icon"] = iconMap[workspaceInput["lastwindow"]];
else
}
else //workspace empty
{
workspaceOutput["icon"] = "";
}
return workspaceOutput;
}
json getAllWorkspaces()
void getAllWorkspaces()
{
workspacesInput = json::parse(command("hyprctl workspaces -j"));
generateIconMap();
generateMonitorMap();
json workspacesInput = json::parse(command("hyprctl workspaces -j"));
int specialIndex = 0;
for(auto& workspace : workspacesInput)
for(const auto& workspace : workspacesInput)
{
if(workspace["id"] >= 1 && workspace["id"] <= 9)
{
int index = workspace["id"].get<int>() - 1;
workspacesOutput[index]["normal"].clear();
workspacesOutput[index]["normal"] = getWorkspace(workspace, 'n');
workspacesOutput[index]["normal"] = getWorkspace(workspace);
}
else if (std::string(workspace["name"]).find("special:") == 0)
{
workspacesOutput[specialIndex]["special"].clear();
workspacesOutput[specialIndex]["special"] = getWorkspace(workspace, 's');
workspacesOutput[specialIndex]["special"] = getWorkspace(workspace);
specialIndex++;
}
}
return workspacesOutput;
return;
}
@ -189,39 +184,53 @@ json getAllWorkspaces()
void handle(std::string message)
{
/*
Call generateMonitorMap when workspaces change their monitor
Call generateIconMap when clients are opened or closed
*/
if (message.find("workspace") == 0)
if (message.find("workspacev2>>") == 0) //emitted ONCE when switching to a workspace
{
std::cout << getAllWorkspaces() << std::endl;
generateMonitorMap();
getAllWorkspaces();
std::cout << workspacesOutput << std::endl;
}
else if (message.find("moveworkspace") == 0)
else if (message.find("moveworkspacev2>>") == 0) //emitted when a workspace switches to another monitor, TWICE when swaping
{
std::cout << getAllWorkspaces() << std::endl;
generateMonitorMap();
getAllWorkspaces();
std::cout << workspacesOutput << std::endl;
}
else if (message.find("openwindow") == 0)
else if (message.find("openwindow>>") == 0) //emitted ONCE when a new client is created
{
std::cout << getAllWorkspaces() << std::endl;
generateIconMap();
getAllWorkspaces();
std::cout << workspacesOutput << std::endl;
}
else if (message.find("closewindow") == 0)
else if (message.find("closewindow>>") == 0) //emitted ONCE when a client gets closed
{
std::cout << getAllWorkspaces() << std::endl;
generateIconMap();
getAllWorkspaces();
std::cout << workspacesOutput << std::endl;
}
else if (message.find("movewindow") == 0)
else if (message.find("movewindowv2>>") == 0) //emitted ONCE when a client changes its workspace
{
std::cout << getAllWorkspaces() << std::endl;
getAllWorkspaces();
std::cout << workspacesOutput << std::endl;
}
else if (message.find("activewindow") == 0)
else if (message.find("activewindow>>v2") == 0) //emitted ONCE when focus changes to another client
{
std::cout << getAllWorkspaces() << std::endl;
getAllWorkspaces();
std::cout << workspacesOutput << std::endl;
}
}
int main(int argc, char const *argv[])
{
std::cout << getAllWorkspaces() << std::endl;
generateIconMap();
generateMonitorMap();
getAllWorkspaces();
std::cout << workspacesOutput << std::endl;
std::string socketPath = "/tmp/hypr/" + std::string(std::getenv("HYPRLAND_INSTANCE_SIGNATURE")) + "/.socket2.sock";