moved all desktop utils into this repo

This commit is contained in:
willifan 2024-06-03 11:56:16 +02:00
parent cd7e93374e
commit 2241d30a86
28 changed files with 151 additions and 45 deletions

23
ewwScripts/bluetooth.sh Executable file
View file

@ -0,0 +1,23 @@
#!/usr/bin/env bash
# Get the list of connected Bluetooth devices
devices=$(bluetoothctl devices Connected | awk '{print $2}')
name[0]=""
battery[0]=0
i=0
# Iterate through each device and get its name and battery percentage
for device in $devices; do
# Get the device info using bluetoothctl
info=$(bluetoothctl info "$device")
# Extract the name and battery percentage from the device info
name[i]=$(echo "$info" | awk '/Name:/ {for(i=2; i<=NF; i++) printf "%s ", $i;}')
battery[i]=$(echo "$info" | awk '/Battery Percentage:/ {print $4}' | tr -d '()')
((i=i+1))
done
echo '{"name":"'"${name[0]}"'","battery":"'"${battery[0]}"'"}'

10
ewwScripts/brightness.sh Executable file
View file

@ -0,0 +1,10 @@
#!/usr/bin/env bash
FILE_TO_WATCH="/sys/class/backlight/intel_backlight/brightness" # Replace with the actual file path
cat "$FILE_TO_WATCH"
while true; do
inotifywait -e modify "$FILE_TO_WATCH"
cat "$FILE_TO_WATCH"
done

View file

@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.5)
project(clients)
# Set C++ standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
include_directories(lib)
set(SOURCES
src/main.cpp
)
add_executable(clients ${SOURCES})
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build)

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,294 @@
#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/eww/scripts/ && ./test.sh ") + initClass + " " + pid;
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 = "/tmp/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;
}

6
ewwScripts/media.sh Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
status=$(playerctl status)
name=$(playerctl metadata title)
echo '{"status":"'"$status"'","name":"'"$name"'"}'

40
ewwScripts/monitors.sh Executable file
View file

@ -0,0 +1,40 @@
#!/usr/bin/env bash
unset id name horizontal vertical scale activeWorkspace
i=0
while read -r k n h v s t; do
id[i]="$k"
name[i]="$n"
horizontal[i]="$h"
vertical[i]="$v"
scale[i]="$s"
if [[ $((t%2)) == 1 ]]; then
temp=${vertical[$i]}
vertical[i]=${horizontal[$i]}
horizontal[i]=$temp
fi
((i=i+1))
done < <(hyprctl monitors -j | jq -r '.[]|"\(.id) \(.name) \(.width) \(.height) \(.scale) \(.transform)"')
echo -n "" > "$XDG_CONFIG_HOME"/eww/bar.yuck
eww close-all
sleep 2
for ((i = 0; i < ${#id[@]}; i++)); do
scale_value=$(echo "${scale[$i]}" | bc -l)
new_width_float=$(echo "(${horizontal[$i]} / ${scale_value}) -10" | bc)
new_width=$(echo "$new_width_float / 1" | bc)
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=30
sleep 2
done

43
ewwScripts/network.sh Executable file
View file

@ -0,0 +1,43 @@
#!/usr/bin/env bash
ethernet() {
name="Wired"
image="ethernet"
}
wifi() {
wifiInfo=$(nmcli -t -f active,ssid,signal device wifi list)
signalStrength=$(awk -F: '/yes:/ {print $3}' <<< $wifiInfo)
name=$(awk -F: '/yes:/ {print $2}' <<< $wifiInfo)
if [[ $signalStrength -ge 70 ]]; then
image="wifiHigh"
elif [[ $signalStrength -ge 60 ]]; then
image="wifiMedium"
elif [[ $signalStrength -ge 40 ]]; then
image="wifiLow"
else
image="wifiVeryLow"
fi
}
while true; do
name="No Connection"
image="n.A."
networkInfo=$(nmcli -f NAME,TYPE connection show --active | awk '!/loopback/ && NR > 1')
if [[ $(awk '/wifi/' <<< "$networkInfo") ]]; then
wifi
fi
if [[ $(awk '/ethernet/' <<< "$networkInfo") ]]; then
ethernet
fi
echo '{"name":"'"$name"'","image":"'"$image"'"}'
sleep 2
done

33
ewwScripts/test.sh Executable file
View file

@ -0,0 +1,33 @@
#!/usr/bin/env bash
getIcon() {
local desktop=""
# Search for desktop files by WM class
if [ -n "$1" ]; then
desktop=$(grep -l "StartupWMClass.*$1$" /run/current-system/sw/share/applications/*.desktop)
[ -z "$desktop" ] && desktop=$(grep -Pl "Name\s*=\s*$1$" /run/current-system/sw/share/applications/*.desktop)
[ -z "$desktop" ] && desktop=$(ls /run/current-system/sw/share/applications/"${1}".desktop 2>/dev/null)
fi
# If not found by WM class, search by process name
if [ -z "$desktop" ] && [ -n "$2" ]; then
process=$(ps -p "${2}" -o comm=)
desktop=$(grep -Pl "$process" /run/current-system/sw/share/applications/*.desktop)
[ -n "$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" /run/current-system/sw/share/applications/*.desktop)
fi
echo "$desktop"
}
desktop=$(getIcon "$1" "$2")
if [ -n "$desktop" ]; then
desktop=$(echo "$desktop" | awk 'NR==1')
icon=$(awk -F'[[:space:]]*=[[:space:]]*' '/Icon[[:space:]]*=/ && !seen[$2]++ {print $2; exit}' "$desktop")
echo -n "${icon}.svg"
fi

24
ewwScripts/volume.sh Executable file
View file

@ -0,0 +1,24 @@
#!/usr/bin/env bash
generate() {
message=$(wpctl get-volume @DEFAULT_AUDIO_SINK@)
volume=$(awk '{print $2 * 100}' <<< "$message")
if [[ $(awk '{print $3}' <<< "$message") == "[MUTED]" ]]; then
muted=1
else
muted=0
fi
echo '{"volume":"'"$volume"'","muted":"'"$muted"'"}'
}
generate
pactl subscribe | while read -r event; do
if [[ "$event" == *"Event 'change'"* ]]; then
generate
fi
done