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

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;
}

View file

@ -1,149 +0,0 @@
//use serde::{Deserialize, Serialize};
//use serde_json::{self, json};
use std::collections::HashMap;
use std::env;
use std::fs;
use std::io::BufRead;
use std::io::BufReader;
use std::os::unix::net::UnixStream;
use std::process::Command;
use std::str;
use std::string::String;
use std::thread::sleep;
use std::time::Duration;
const DESKTOP_PATH: &str = "/usr/share/applications/";
const ENV_INSTANCE_SIGNATURE: &str = "HYPRLAND_INSTANCE_SIGNATURE";
//struct Workspace {
// id: i32,
// active_on: u32,
// num_windows: u32,
// icon: String,
//}
//fn fetch_workspaces() {}
fn fetch_monitors() {
let monitors = Command::new("hyprctl monitors")
.arg("-j")
.output()
.unwrap()
.stdout;
}
//fn fetch_clients() {}
fn generate_icon_map() -> HashMap<String, String> {
let files = fs::read_dir(DESKTOP_PATH).unwrap();
let mut vector = vec![];
let mut i = 0;
for file in files {
let file = match file {
Ok(file) => file,
Err(_) => continue,
};
let path = file.path();
let extension = path.extension().expect("error");
if extension != "desktop" {
continue;
}
let mut class = String::new();
let mut icon = String::new();
let file = fs::read_to_string(path).unwrap();
let mut got_icon = false;
let mut got_class = false;
for line in file.lines() {
let line = line.replace(' ', "");
match (got_icon, &line) {
(true, _) => (),
(false, line) if line.starts_with("Icon=") => {
icon = line.split_once('=').expect("error").1.to_string();
got_icon = true;
if got_class {
break;
}
continue;
}
_ => (),
}
match (got_class, &line) {
(true, _) => (),
(false, line) if line.starts_with("StartupWMClass=") => {
class = line.split_once('=').expect("error").1.to_string();
got_class = true;
if got_icon {
break;
}
continue;
}
(false, line) if line.starts_with("Name=") => {
class = line.split_once('=').expect("error").1.to_string();
continue;
}
_ => (),
}
}
vector.push((class, icon));
i += 1;
}
let mut icon_map: HashMap<String, String> = HashMap::with_capacity(i);
icon_map.extend(vector);
for (key, value) in &icon_map {
println!("Key: {}, Value: {}", key, value);
}
icon_map
}
fn handle_workspace() {
fetch_monitors();
}
fn handle_moveworkspace() {}
fn handle_activespecial() {}
fn handle_openwindow() {}
fn handle_closewindow() {}
fn handle_movewindow() {}
fn handle_activewindow() {}
fn handle(message: &str) {
println!("{}", message);
match message {
s if s.starts_with("workspacev2>>") => handle_workspace(),
s if s.starts_with("moveworkspacev2>>") => handle_moveworkspace(),
s if s.starts_with("activespecial>>") => handle_activespecial(),
s if s.starts_with("openwindow>>") => handle_openwindow(),
s if s.starts_with("closewindow>>") => handle_closewindow(),
s if s.starts_with("movewindowv2>>") => handle_movewindow(),
s if s.starts_with("activewindow>>v2") => handle_activewindow(),
_ => (),
};
}
fn main() {
let icon_map = generate_icon_map();
let hyprland_instance_signature = env::var(ENV_INSTANCE_SIGNATURE).unwrap();
let address = format!("/tmp/hypr/{}/.socket2.sock", hyprland_instance_signature);
let stream = UnixStream::connect(address).unwrap();
let mut reader = BufReader::new(stream);
loop {
let mut buf = String::new();
reader.read_line(&mut buf).unwrap();
handle(buf.trim());
sleep(Duration::from_millis(100));
}
}

View file

@ -1,64 +0,0 @@
use std::fs;
use std::time::Instant;
const PATH: &str = "/usr/share/applications/";
fn read(path: &str) -> std::io::Result<()> {
let files = fs::read_dir(path)?;
for file in files {
let file = file?;
let path = file.path();
let extension = path.extension().expect("error");
if extension != "desktop" {
continue;
}
let mut class: &str = "";
let mut name: &str = "";
let mut icon: &str = "";
let file = fs::read_to_string(path)?;
for line in file.lines() {
match line {
line if line.starts_with("StartupWMClass") => {
class = match line.split_once('=') {
Some((_, class)) => class,
None => "",
}
}
line if line.starts_with("Name") => {
name = match line.split_once('=') {
Some((_, name)) => name,
None => "",
}
}
line if line.starts_with("Icon") => {
icon = match line.split_once('=') {
Some((_, icon)) => icon,
None => "",
}
}
_ => (),
}
}
println!("Class: {class}, Name: {name}, Icon: {icon}");
}
Ok(())
}
fn main() {
let start_time = Instant::now();
read(PATH).expect("error");
let end_time = Instant::now();
let time = end_time - start_time;
println!(
"Time taken: {}.{:03}s",
time.as_secs(),
time.subsec_millis()
);
}