reorganized repo

This commit is contained in:
willifan 2024-11-10 16:41:53 +01:00
parent b2796241d7
commit 89bbb229b8
13 changed files with 298 additions and 25063 deletions

View file

@ -6,8 +6,6 @@ project(clients)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True) set(CMAKE_CXX_STANDARD_REQUIRED True)
include_directories(lib)
set(SOURCES set(SOURCES
src/main.cpp src/main.cpp
) )

View file

@ -1 +0,0 @@
"explorer.confirmDelete": false

33
default.nix Normal file
View file

@ -0,0 +1,33 @@
{ lib
, stdenv
, cmake
, nlohmann_json
}:
stdenv.mkDerivation {
pname = "desktop-utils";
version = "1.0.0";
src = builtins.path { path = ./.; name = "desktop-utils"; };
nativeBuildInputs = [
cmake
];
buildInputs = [
nlohmann_json
];
installPhase = ''
mkdir -p $out/bin
cp clients $out/bin/clients
'';
meta = with lib; {
description = "utils for my desktop";
homepage = " git.huwe.mooo.com/willifan/desktop-utils";
license = licenses.gpl3;
platforms = platforms.unix;
};
}

Binary file not shown.

File diff suppressed because it is too large Load diff

View file

@ -1,294 +0,0 @@
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include "nlohmann/json.hpp"
using json = nlohmann::json;
json workspacesOutput;
std::map<std::string, std::string> iconMap;
std::map<int, std::string> monitorMap;
std::map<std::string, int> specialWorkspaceMap = {
{"special:ctrl", 0},
{"special:alt", 1},
{"special:altgr", 2},
{"special:strg", 3}
};
/*json workspacesOutput = json::parse(R"(
[
"normal":
{
"activeOn": str
"icon": str
"id": int
"occupied": int
},
"special":
{
"activeOn": str
"icon": str
"id": str
"occupied": int
}
]
)");*/
std::string command(std::string inputCommand)
{
const char* command = inputCommand.c_str();
char buffer[128];
std::string result;
FILE* pipe = popen(command, "r");
if(!pipe)
{
std::cerr << "No pipe opened" << std::endl;
return "";
}
while(!feof(pipe))
{
if(fgets(buffer, 128, pipe) != NULL)
{
result += buffer;
}
}
pclose(pipe);
return result;
}
void generateIconMap()
{
json clients = json::parse(command("hyprctl clients -j"));
for (const json& client : clients)
{
std::string pid = std::to_string(static_cast<int>(client["pid"]));
if (pid == "-1")
{
continue;
}
std::string initClass = client["initialClass"];
if (initClass == "")
{
initClass = "aguiienagi";
}
//TODO unjank
std::string cmd ="cd /home/willifan/.config/eww/scripts/ && ./test.sh ";
std::string test = std::string("cd /home/willifan/.config/desktop-utils/ewwScripts && ./test.sh ") + initClass + " " + pid;
std::cout << test << std::endl;
iconMap[client["address"]] = command(test);
}
return;
}
void generateMonitorMap()
{
json monitorsInput = json::parse(command("hyprctl monitors -j"));
monitorMap.clear();
for (const json& monitor : monitorsInput)
{
monitorMap[monitor["activeWorkspace"]["id"]] = std::to_string(static_cast<int>(monitor["id"]));
}
}
json getWorkspace(json workspaceInput)
{
json workspaceOutput;
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")) //workspace not empty
{
workspaceOutput["icon"] = iconMap[workspaceInput["lastwindow"]];
}
else //workspace empty
{
workspaceOutput["icon"] = "";
}
return workspaceOutput;
}
//TODO fix special workspaces
void getAllWorkspaces()
{
json workspacesInput = json::parse(command("hyprctl workspaces -j"));
int specialIndex = 4; //next index after specialWorkspaceMap
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);
}
else if (std::string(workspace["name"]).find("special:") == 0)
{
std::string name = workspace["name"];
int currentIndex;
if (specialWorkspaceMap.contains(name))
{
currentIndex = specialWorkspaceMap[name];
}
else
{
currentIndex = specialIndex;
specialIndex++;
}
workspacesOutput[currentIndex]["special"].clear();
workspacesOutput[currentIndex]["special"] = getWorkspace(workspace);
}
}
return;
}
//TODO optimize whatever
void handle(std::string message)
{
/*
Call generateMonitorMap when workspaces change their monitor
Call generateIconMap when clients are opened or closed
*/
if (message.find("workspacev2>>") == 0) //emitted ONCE when switching to a workspace
{
generateMonitorMap();
getAllWorkspaces();
std::cout << workspacesOutput << std::endl;
}
else if (message.find("moveworkspacev2>>") == 0) //emitted when a workspace switches to another monitor, TWICE when swaping
{
generateMonitorMap();
getAllWorkspaces();
std::cout << workspacesOutput << std::endl;
}
else if (message.find("openwindow>>") == 0) //emitted ONCE when a new client is created
{
generateIconMap();
getAllWorkspaces();
std::cout << workspacesOutput << std::endl;
}
else if (message.find("closewindow>>") == 0) //emitted ONCE when a client gets closed
{
generateIconMap();
getAllWorkspaces();
std::cout << workspacesOutput << std::endl;
}
else if (message.find("movewindowv2>>") == 0) //emitted ONCE when a client changes its workspace
{
getAllWorkspaces();
std::cout << workspacesOutput << std::endl;
}
else if (message.find("activewindow>>v2") == 0) //emitted ONCE when focus changes to another client
{
getAllWorkspaces();
std::cout << workspacesOutput << std::endl;
}
}
int main(int argc, char const *argv[])
{
generateIconMap();
generateMonitorMap();
getAllWorkspaces();
std::cout << workspacesOutput << std::endl;
std::string socketPath = std::string(std::getenv("XDG_RUNTIME_DIR")) + "/hypr/" + std::string(std::getenv("HYPRLAND_INSTANCE_SIGNATURE")) + "/.socket2.sock";
// Create a socket
int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockfd == -1) {
std::cerr << "Error: Failed to create socket\n";
return 1;
}
// Define the address of the IPC socket
struct sockaddr_un addr;
addr.sun_family = AF_UNIX;
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) {
std::cerr << "Error: Failed to connect to IPC socket\n";
close(sockfd);
return 1;
}
// Receive and print messages from the IPC socket
char buffer[1024];
ssize_t bytes_received;
while ((bytes_received = recv(sockfd, buffer, sizeof(buffer), 0)) > 0) {
std::string message = std::string(buffer, bytes_received);
std::istringstream iss(message);
std::string messageLine;
while(std::getline(iss, messageLine)){
handle(messageLine);
}
}
if (bytes_received == -1) {
std::cerr << "Error: Failed to receive message\n";
}
// Close the socket
close(sockfd);
return 0;
}

1
result Symbolic link
View file

@ -0,0 +1 @@
/nix/store/4p7d68cla74k2k9fqr2ps612zb5zy3xg-desktop-utils-1.0.0

View file

0
shell.nix Normal file
View file

264
src/main.cpp Normal file
View file

@ -0,0 +1,264 @@
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
json workspacesOutput;
std::map<std::string, std::string> iconMap;
std::map<int, std::string> monitorMap;
std::map<std::string, int> specialWorkspaceMap = {{"special:ctrl", 0},
{"special:alt", 1},
{"special:altgr", 2},
{"special:strg", 3}};
/*json workspacesOutput = json::parse(R"(
[
"normal":
{
"activeOn": str
"icon": str
"id": int
"occupied": int
},
"special":
{
"activeOn": str
"icon": str
"id": str
"occupied": int
}
]
)");*/
std::string command(std::string inputCommand) {
const char *command = inputCommand.c_str();
char buffer[128];
std::string result;
FILE *pipe = popen(command, "r");
if (!pipe) {
std::cerr << "No pipe opened" << std::endl;
return "";
}
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != NULL) {
result += buffer;
}
}
pclose(pipe);
return result;
}
void generateIconMap() {
json clients = json::parse(command("hyprctl clients -j"));
for (const json &client : clients) {
std::string pid = std::to_string(static_cast<int>(client["pid"]));
if (pid == "-1") {
continue;
}
std::string initClass = client["initialClass"];
if (initClass == "") {
initClass = "aguiienagi";
}
// TODO unjank
std::string cmd = "cd /home/willifan/.config/eww/scripts/ && ./test.sh ";
std::string test =
std::string("cd /home/willifan/.config/desktop-utils/ewwScripts && "
"./test.sh ") +
initClass + " " + pid;
std::cout << test << std::endl;
iconMap[client["address"]] = command(test);
}
return;
}
void generateMonitorMap() {
json monitorsInput = json::parse(command("hyprctl monitors -j"));
monitorMap.clear();
for (const json &monitor : monitorsInput) {
monitorMap[monitor["activeWorkspace"]["id"]] =
std::to_string(static_cast<int>(monitor["id"]));
}
}
json getWorkspace(json workspaceInput) {
json workspaceOutput;
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")) // workspace not empty
{
workspaceOutput["icon"] = iconMap[workspaceInput["lastwindow"]];
} else // workspace empty
{
workspaceOutput["icon"] = "";
}
return workspaceOutput;
}
// TODO fix special workspaces
void getAllWorkspaces() {
json workspacesInput = json::parse(command("hyprctl workspaces -j"));
int specialIndex = 4; // next index after specialWorkspaceMap
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);
} else if (std::string(workspace["name"]).find("special:") == 0) {
std::string name = workspace["name"];
int currentIndex;
if (specialWorkspaceMap.contains(name)) {
currentIndex = specialWorkspaceMap[name];
} else {
currentIndex = specialIndex;
specialIndex++;
}
workspacesOutput[currentIndex]["special"].clear();
workspacesOutput[currentIndex]["special"] = getWorkspace(workspace);
}
}
return;
}
// TODO optimize whatever
void handle(std::string message) {
/*
Call generateMonitorMap when workspaces change their monitor
Call generateIconMap when clients are opened or closed
*/
if (message.find("workspacev2>>") ==
0) // emitted ONCE when switching to a workspace
{
generateMonitorMap();
getAllWorkspaces();
std::cout << workspacesOutput << std::endl;
} else if (message.find("moveworkspacev2>>") ==
0) // emitted when a workspace switches to another monitor, TWICE
// when swaping
{
generateMonitorMap();
getAllWorkspaces();
std::cout << workspacesOutput << std::endl;
} else if (message.find("openwindow>>") ==
0) // emitted ONCE when a new client is created
{
generateIconMap();
getAllWorkspaces();
std::cout << workspacesOutput << std::endl;
} else if (message.find("closewindow>>") ==
0) // emitted ONCE when a client gets closed
{
generateIconMap();
getAllWorkspaces();
std::cout << workspacesOutput << std::endl;
} else if (message.find("movewindowv2>>") ==
0) // emitted ONCE when a client changes its workspace
{
getAllWorkspaces();
std::cout << workspacesOutput << std::endl;
} else if (message.find("activewindow>>v2") ==
0) // emitted ONCE when focus changes to another client
{
getAllWorkspaces();
std::cout << workspacesOutput << std::endl;
}
}
int main(int argc, char const *argv[]) {
generateIconMap();
generateMonitorMap();
getAllWorkspaces();
std::cout << workspacesOutput << std::endl;
std::string socketPath =
std::string(std::getenv("XDG_RUNTIME_DIR")) + "/hypr/" +
std::string(std::getenv("HYPRLAND_INSTANCE_SIGNATURE")) +
"/.socket2.sock";
// Create a socket
int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockfd == -1) {
std::cerr << "Error: Failed to create socket\n";
return 1;
}
// Define the address of the IPC socket
struct sockaddr_un addr;
addr.sun_family = AF_UNIX;
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) {
std::cerr << "Error: Failed to connect to IPC socket\n";
close(sockfd);
return 1;
}
// Receive and print messages from the IPC socket
char buffer[1024];
ssize_t bytes_received;
while ((bytes_received = recv(sockfd, buffer, sizeof(buffer), 0)) > 0) {
std::string message = std::string(buffer, bytes_received);
std::istringstream iss(message);
std::string messageLine;
while (std::getline(iss, messageLine)) {
handle(messageLine);
}
}
if (bytes_received == -1) {
std::cerr << "Error: Failed to receive message\n";
}
// Close the socket
close(sockfd);
return 0;
}