First working version

This commit is contained in:
willifan 2024-03-17 15:44:15 +01:00
parent c8ac4b08ea
commit d507864ec1
8 changed files with 194 additions and 135 deletions

View file

Binary file not shown.

View file

@ -1,50 +1,150 @@
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include "nlohmann/json.hpp"
#include <fstream>
#include <map>
//test
using json = nlohmann::json;
json workspaces, clients;
json workspacesInput, workspacesOutput;
std::string command(const char* command);
/*json workspacesOutput = json::parse(R"(
{
"normal": [
{
"occupied": bool,
"monitorID": int,
"active": bool,
"icon": str
}
],
"special": [
{
"ID": int,
"name": str,
"occupied": bool,
"monitorID": int,
"active": bool,
"icon": str
}
]
}
)");*/
json generate()
std::map<int, std::string> test;
std::string command(std::string command);
std::string getIcon(std::string windowAddress);
std::string getIcon(std::string windowAddress)
{
return "";
json clients = json::parse(command("hyprctl clients -j"));
for (json& client : clients)
{
if (windowAddress == client["address"])
{
std::string initClass = client["initialClass"];
if (initClass == "")
{
initClass = "aguiienagi";
}
int pid = client["pid"];
std::string cmd ="cd /home/willifan/.config/eww/scripts/ && ./test.sh ";
cmd.append(initClass);
cmd.append(" ");
cmd.append(std::to_string(pid));
return command(cmd);
}
}
return "placeholder";
}
json getWorkspaces()
void getWorkspaces()
{
workspaces = json::parse(command("hyprctl workspaces -j"));
return "";
workspacesInput = json::parse(command("hyprctl workspaces -j"));
workspacesOutput.clear();
int specialIndex = 0;
for(auto& workspace : workspacesInput)
{
if(workspace["id"] >= 1 && workspace["id"] <= 9)
{
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"] = "";
}
}
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"]);
}
specialIndex++;
}
}
std::cout << workspacesOutput << std::endl;
}
json getClients()
{
clients = json::parse(command("hyprctl clients -j"));
return "";
}
std::string command(const char* command)
std::string command(std::string inputCommand)
{
const char* command = inputCommand.c_str();
char buffer[128];
std::string result;
FILE* pipe = popen(command, "r");
@ -73,35 +173,27 @@ void handle(std::string message)
if (message.find("workspace") == 0)
{
std::cout << "1" << std::endl;
workspaces = getWorkspaces();
}
else if (message.find("createworkspace") == 0)
{
std::cout << "2" << std::endl;
workspaces = getWorkspaces();
}
else if (message.find("destroyworkspace") == 0)
{
std::cout << "3" << std::endl;
workspaces = getWorkspaces();
getWorkspaces();
}
else if (message.find("moveworkspace") == 0)
{
std::cout << "4" << std::endl;
workspaces = getWorkspaces();
getWorkspaces();
}
else if (message.find("openwindow") == 0)
{
std::cout << "5" << std::endl;
getWorkspaces();
}
else if (message.find("closewindow") == 0)
{
std::cout << "6" << std::endl;
getWorkspaces();
}
else if (message.find("movewindow") == 0)
{
std::cout << "7" << std::endl;
getWorkspaces();
}
else if (message.find("activewindow") == 0)
{
getWorkspaces();
}
@ -111,14 +203,7 @@ int main(int argc, char const *argv[])
{
std::string socketPath = "/tmp/hypr/" + std::string(std::getenv("HYPRLAND_INSTANCE_SIGNATURE")) + "/.socket2.sock";
std::ifstream socketStream(socketPath);
while(socketStream)
{
std::string line;
socketStream >> line;
std::cout << line << std::endl;
}
const char* socketPathPtr = socketPath.c_str();
// Create a socket
int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
@ -132,7 +217,7 @@ int main(int argc, char const *argv[])
// Define the address of the IPC socket
struct sockaddr_un addr;
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, socketPathPtr, sizeof(addr.sun_path) - 1);
strncpy(addr.sun_path, socketPath.c_str(), sizeof(addr.sun_path) - 1);
// Connect to the IPC socket
if (connect(sockfd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
@ -152,11 +237,9 @@ int main(int argc, char const *argv[])
std::string messageLine;
while(std::getline(iss, messageLine)){
std::cout << messageLine << std::endl;
handle(messageLine);
}
}
if (bytes_received == -1) {
std::cerr << "Error: Failed to receive message\n";

View file

@ -1,64 +1,26 @@
#!/bin/bash
pid=()
classes=()
clientOnWorkspace=()
images=()
numberOnWorkspace=()
area=()
i=0
while read -r p w x y c; do
if [[ $c ]]; then
pid[$i]="$p"
classes[$i]="$c"
clientOnWorkspace[$i]="$w"
((numberOnWorkspace[w]+=1))
area[$i]=$((x * y))
desktop=$(grep -l "StartupWMClass.*$c$" /usr/share/applications/*.desktop)
echo $c
if [ -z $desktop ]; then
desktop=$(grep -Pl "Name\s*=\s*$c$" /usr/share/applications/*.desktop)
fi
if [ -z $desktop ]; then
desktop=$(ls /usr/share/applications | grep -l /usr/share/applications/${c}.desktop)
fi
if [ -z $desktop ]; then
process=$(ps -p ${p} -o comm=)
desktop=$(grep -Pl "$process" /usr/share/applications/*.desktop)
fi
if [ -z $desktop ]; then
desktop=$(grep -Pli "$c" /usr/share/applications/*.desktop)
fi
if [ -z $desktop ]; then
continue
fi
images[$i]=$(awk -F'[[:space:]]*=[[:space:]]*' '/Icon[[:space:]]*=/{print $2}' $desktop)
ls /usr/share/icons/ePapirus-Dark/128x128/apps/${images[$i]}.svg
((i=$i+1))
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
done < <(hyprctl clients -j | jq -r '.[]|"\(.pid) \(.workspace.id) \(.size[0]) \(.size[1]) \(.initialClass)"')
for ((i=0; i<${#clientOnWorkspace[@]}; i++)); do
clientInfos[i]='{"onWorkspace":"'${clientOnWorkspace[$i]}'","pid":"'${pid[$i]}'","class":"'${classes[$i]}'","mainOn":"''","icon":"'${images[$i]}'"}'
echo ${clientInfos[$i]}
done
#for window in ${!windows[@]}; do
# echo -n "${window} "
# echo $(ps -p ${windows[$window]} -o comm=)
#done
#test=$(ps -p ${windows[2]} -o comm=)
#$(feh /usr/share/icons/Papirus-Dark/128x128/apps/${test}.svg)
if [ -z "$desktop" ]; then
desktop=$(ls /usr/share/applications | grep -l /usr/share/applications/${1}.desktop)
fi
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