Hyprland/hyprctl/main.cpp

436 lines
12 KiB
C++
Raw Normal View History

2022-03-20 18:51:14 +03:00
#include <ctype.h>
2022-03-21 20:29:41 +03:00
#include <netdb.h>
#include <netinet/in.h>
2022-03-20 18:51:14 +03:00
#include <stdio.h>
#include <stdlib.h>
2022-03-21 20:29:41 +03:00
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
2022-03-20 18:51:14 +03:00
#include <unistd.h>
2022-07-19 15:27:52 +03:00
#include <ranges>
2022-07-19 15:28:37 +03:00
#include <algorithm>
#include <signal.h>
#include <format>
2022-03-20 18:51:14 +03:00
#include <iostream>
2022-03-21 20:29:41 +03:00
#include <string>
#include <fstream>
2022-03-20 18:51:14 +03:00
#include <string>
#include <vector>
2022-07-15 12:58:09 +03:00
#include <deque>
#include <filesystem>
#include <stdarg.h>
2022-03-20 18:51:14 +03:00
2022-07-15 12:58:09 +03:00
const std::string USAGE = R"#(usage: hyprctl [(opt)flags] [command] [(opt)args]
2022-09-25 21:07:48 +03:00
2022-07-13 00:21:00 +03:00
commands:
2022-03-20 18:51:14 +03:00
monitors
workspaces
activeworkspace
workspacerules
2022-03-20 18:51:14 +03:00
clients
2022-03-30 23:40:53 +03:00
activewindow
layers
2022-06-02 14:59:33 +03:00
devices
2023-01-06 16:32:25 +03:00
binds
2022-04-21 17:11:29 +03:00
dispatch
2022-04-21 17:56:27 +03:00
keyword
2022-04-22 19:14:25 +03:00
version
2022-06-27 14:42:20 +03:00
kill
2022-07-10 16:41:26 +03:00
splash
2022-07-02 19:27:44 +03:00
hyprpaper
2022-07-13 00:21:00 +03:00
reload
2022-08-10 22:22:11 +03:00
setcursor
2022-08-11 22:16:38 +03:00
getoption
2022-10-26 15:19:37 +03:00
cursorpos
2022-12-03 18:56:07 +03:00
switchxkblayout
2023-01-24 22:05:34 +03:00
seterror
setprop
plugin
2023-03-20 19:00:54 +03:00
notify
globalshortcuts
instances
layouts
2022-09-25 21:07:48 +03:00
2022-07-13 00:21:00 +03:00
flags:
2022-07-28 15:29:16 +03:00
-j -> output in JSON
--batch -> execute a batch of commands, separated by ';'
2023-08-08 19:04:20 +03:00
--instance (-i) -> use a specific instance. Can be either signature or index in hyprctl instances (0, 1, etc)
2022-07-13 00:21:00 +03:00
)#";
2022-03-20 18:51:14 +03:00
#define PAD
2023-08-08 19:04:20 +03:00
std::string instanceSignature;
struct SInstanceData {
std::string id;
uint64_t time;
uint64_t pid;
std::string wlSocket;
bool valid = true;
};
std::vector<SInstanceData> instances() {
std::vector<SInstanceData> result;
for (const auto& el : std::filesystem::directory_iterator("/tmp/hypr")) {
if (el.is_directory())
continue;
// read lock
SInstanceData* data = &result.emplace_back();
data->id = el.path().string();
data->id = data->id.substr(data->id.find_last_of('/') + 1, data->id.find(".lock") - data->id.find_last_of('/') - 1);
data->time = std::stoull(data->id.substr(data->id.find_first_of('_') + 1));
// read file
std::ifstream ifs(el.path().string());
int i = 0;
for (std::string line; std::getline(ifs, line); ++i) {
if (i == 0) {
data->pid = std::stoull(line);
} else if (i == 1) {
data->wlSocket = line;
} else
break;
}
ifs.close();
}
std::erase_if(result, [&](const auto& el) { return kill(el.pid, 0) != 0 && errno == ESRCH; });
std::sort(result.begin(), result.end(), [&](const auto& a, const auto& b) { return a.time < b.time; });
return result;
}
void request(std::string arg, int minArgs = 0) {
2023-01-24 22:05:34 +03:00
const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0);
2022-03-20 18:51:14 +03:00
2023-01-24 22:05:34 +03:00
const auto ARGS = std::count(arg.begin(), arg.end(), ' ');
2022-12-03 18:56:07 +03:00
2023-01-24 22:05:34 +03:00
if (ARGS < minArgs) {
std::cout << "Not enough arguments, expected at least " << minArgs;
return;
2022-12-03 18:56:07 +03:00
}
2023-01-24 22:05:34 +03:00
if (SERVERSOCKET < 0) {
std::cout << "Couldn't open a socket (1)";
return;
2022-03-21 20:29:41 +03:00
}
2022-03-20 18:51:14 +03:00
2023-08-08 19:04:20 +03:00
if (instanceSignature.empty()) {
2023-01-24 22:05:34 +03:00
std::cout << "HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)";
return;
}
2023-01-24 22:05:34 +03:00
sockaddr_un serverAddress = {0};
serverAddress.sun_family = AF_UNIX;
2023-08-08 19:04:20 +03:00
std::string socketPath = "/tmp/hypr/" + instanceSignature + "/.socket.sock";
strncpy(serverAddress.sun_path, socketPath.c_str(), sizeof(serverAddress.sun_path) - 1);
2022-03-20 18:51:14 +03:00
2023-01-24 22:05:34 +03:00
if (connect(SERVERSOCKET, (sockaddr*)&serverAddress, SUN_LEN(&serverAddress)) < 0) {
std::cout << "Couldn't connect to " << socketPath << ". (3)";
return;
2022-03-21 20:29:41 +03:00
}
2022-03-20 18:51:14 +03:00
2023-01-24 22:05:34 +03:00
auto sizeWritten = write(SERVERSOCKET, arg.c_str(), arg.length());
2022-03-20 18:51:14 +03:00
2023-01-24 22:05:34 +03:00
if (sizeWritten < 0) {
std::cout << "Couldn't write (4)";
return;
2022-03-21 20:29:41 +03:00
}
2022-03-20 18:51:14 +03:00
2023-01-24 22:05:34 +03:00
std::string reply = "";
char buffer[8192] = {0};
2022-03-20 18:51:14 +03:00
2023-01-24 22:05:34 +03:00
sizeWritten = read(SERVERSOCKET, buffer, 8192);
2022-07-02 19:27:44 +03:00
2023-01-24 22:05:34 +03:00
if (sizeWritten < 0) {
std::cout << "Couldn't read (5)";
return;
2022-07-02 19:27:44 +03:00
}
2023-01-24 22:05:34 +03:00
reply += std::string(buffer, sizeWritten);
2022-10-26 15:11:05 +03:00
2023-01-24 22:05:34 +03:00
while (sizeWritten == 8192) {
sizeWritten = read(SERVERSOCKET, buffer, 8192);
if (sizeWritten < 0) {
std::cout << "Couldn't read (5)";
return;
2022-10-26 15:11:05 +03:00
}
2023-01-24 22:05:34 +03:00
reply += std::string(buffer, sizeWritten);
2022-10-26 15:11:05 +03:00
}
2023-01-24 22:05:34 +03:00
close(SERVERSOCKET);
2022-07-02 19:27:44 +03:00
2023-01-24 22:05:34 +03:00
std::cout << reply;
2022-07-02 19:27:44 +03:00
}
void requestHyprpaper(std::string arg) {
const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0);
if (SERVERSOCKET < 0) {
std::cout << "Couldn't open a socket (1)";
return;
}
2023-08-13 22:42:11 +03:00
if (instanceSignature.empty()) {
2022-07-02 19:27:44 +03:00
std::cout << "HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)";
return;
}
sockaddr_un serverAddress = {0};
serverAddress.sun_family = AF_UNIX;
2022-07-02 19:27:44 +03:00
2023-08-13 22:42:11 +03:00
std::string socketPath = "/tmp/hypr/" + instanceSignature + "/.hyprpaper.sock";
2022-07-02 19:27:44 +03:00
strncpy(serverAddress.sun_path, socketPath.c_str(), sizeof(serverAddress.sun_path) - 1);
2022-07-02 19:27:44 +03:00
if (connect(SERVERSOCKET, (sockaddr*)&serverAddress, SUN_LEN(&serverAddress)) < 0) {
std::cout << "Couldn't connect to " << socketPath << ". (3)";
return;
}
2023-08-13 22:42:11 +03:00
arg = arg.substr(arg.find_first_of('/') + 1); // strip flags
arg = arg.substr(arg.find_first_of(' ') + 1); // strip "hyprpaper"
2022-07-02 19:27:44 +03:00
auto sizeWritten = write(SERVERSOCKET, arg.c_str(), arg.length());
if (sizeWritten < 0) {
std::cout << "Couldn't write (4)";
return;
}
char buffer[8192] = {0};
sizeWritten = read(SERVERSOCKET, buffer, 8192);
2022-03-20 18:51:14 +03:00
2022-03-21 20:29:41 +03:00
if (sizeWritten < 0) {
std::cout << "Couldn't read (5)";
return;
}
2022-03-20 18:51:14 +03:00
2022-03-21 20:29:41 +03:00
close(SERVERSOCKET);
2022-03-20 18:51:14 +03:00
2022-03-21 20:29:41 +03:00
std::cout << std::string(buffer);
2022-03-20 18:51:14 +03:00
}
2022-07-13 01:34:28 +03:00
void batchRequest(std::string arg) {
std::string rq = "[[BATCH]]" + arg.substr(arg.find_first_of(" ") + 1);
2022-09-25 21:07:48 +03:00
2022-04-29 20:44:09 +03:00
request(rq);
}
void instancesRequest(bool json) {
std::string result = "";
// gather instance data
2023-08-08 19:04:20 +03:00
std::vector<SInstanceData> inst = instances();
if (!json) {
2023-08-08 19:04:20 +03:00
for (auto& el : inst) {
result += std::format("instance {}:\n\ttime: {}\n\tpid: {}\n\twl socket: {}\n\n", el.id, el.time, el.pid, el.wlSocket);
}
} else {
result += '[';
2023-08-08 19:04:20 +03:00
for (auto& el : inst) {
result += std::format(R"#(
{{
"instance": "{}",
"time": {},
"pid": {},
"wl_socket": "{}"
}},)#",
el.id, el.time, el.pid, el.wlSocket);
}
result.pop_back();
result += "\n]";
}
std::cout << result << "\n";
}
2022-07-15 12:58:09 +03:00
std::deque<std::string> splitArgs(int argc, char** argv) {
std::deque<std::string> result;
for (auto i = 1 /* skip the executable */; i < argc; ++i)
result.push_back(std::string(argv[i]));
return result;
}
2022-07-19 15:24:03 +03:00
bool isNumber(const std::string& str, bool allowfloat) {
2022-11-11 17:04:35 +03:00
if (str.empty())
return false;
2022-07-19 15:24:03 +03:00
return std::ranges::all_of(str.begin(), str.end(), [&](char c) { return isdigit(c) != 0 || c == '-' || (allowfloat && c == '.'); });
}
2022-03-20 18:51:14 +03:00
int main(int argc, char** argv) {
bool parseArgs = true;
2022-03-20 18:51:14 +03:00
if (argc < 2) {
printf("%s\n", USAGE.c_str());
2022-03-20 18:51:14 +03:00
return 1;
}
2023-08-08 19:04:20 +03:00
std::string fullRequest = "";
std::string fullArgs = "";
const auto ARGS = splitArgs(argc, argv);
bool json = false;
std::string overrideInstance = "";
2022-07-15 12:58:09 +03:00
2023-11-11 00:45:20 +03:00
for (std::size_t i = 0; i < ARGS.size(); ++i) {
if (ARGS[i] == "--") {
// Stop parsing arguments after --
parseArgs = false;
continue;
}
if (parseArgs && (ARGS[i][0] == '-') && !isNumber(ARGS[i], true) /* For stuff like -2 */) {
2022-07-15 12:58:09 +03:00
// parse
if (ARGS[i] == "-j" && !fullArgs.contains("j")) {
fullArgs += "j";
json = true;
2022-07-15 12:58:09 +03:00
} else if (ARGS[i] == "--batch") {
fullRequest = "--batch ";
2023-08-08 19:04:20 +03:00
} else if (ARGS[i] == "--instance" || ARGS[i] == "-i") {
++i;
if (i >= ARGS.size()) {
printf("%s\n", USAGE.c_str());
return 1;
}
overrideInstance = ARGS[i];
2022-07-15 12:58:09 +03:00
} else {
printf("%s\n", USAGE.c_str());
return 1;
}
continue;
}
fullRequest += ARGS[i] + " ";
2022-07-13 01:34:28 +03:00
}
2022-07-15 12:58:09 +03:00
if (fullRequest.empty()) {
printf("%s\n", USAGE.c_str());
return 1;
}
2022-07-13 01:34:28 +03:00
fullRequest.pop_back(); // remove trailing space
2022-07-13 00:17:55 +03:00
2022-07-15 12:58:09 +03:00
fullRequest = fullArgs + "/" + fullRequest;
2023-08-08 19:04:20 +03:00
if (overrideInstance.contains("_"))
instanceSignature = overrideInstance;
else if (!overrideInstance.empty()) {
if (!isNumber(overrideInstance, false)) {
std::cout << "instance invalid\n";
return 1;
}
const auto INSTANCENO = std::stoi(overrideInstance);
const auto INSTANCES = instances();
2023-11-11 00:45:20 +03:00
if (INSTANCENO < 0 || static_cast<std::size_t>(INSTANCENO) >= INSTANCES.size()) {
2023-08-08 19:04:20 +03:00
std::cout << "no such instance\n";
return 1;
}
instanceSignature = INSTANCES[INSTANCENO].id;
} else {
const auto ISIG = getenv("HYPRLAND_INSTANCE_SIGNATURE");
if (!ISIG) {
std::cout << "HYPRLAND_INSTANCE_SIGNATURE not set! (is hyprland running?)";
return 1;
}
instanceSignature = ISIG;
}
int exitStatus = 0;
2022-07-13 00:17:55 +03:00
if (fullRequest.contains("/--batch"))
batchRequest(fullRequest);
else if (fullRequest.contains("/monitors"))
request(fullRequest);
else if (fullRequest.contains("/clients"))
request(fullRequest);
else if (fullRequest.contains("/workspaces"))
request(fullRequest);
else if (fullRequest.contains("/activeworkspace"))
request(fullRequest);
else if (fullRequest.contains("/workspacerules"))
request(fullRequest);
else if (fullRequest.contains("/activewindow"))
request(fullRequest);
else if (fullRequest.contains("/layers"))
request(fullRequest);
else if (fullRequest.contains("/version"))
request(fullRequest);
else if (fullRequest.contains("/kill"))
request(fullRequest);
else if (fullRequest.contains("/splash"))
request(fullRequest);
else if (fullRequest.contains("/devices"))
request(fullRequest);
else if (fullRequest.contains("/reload"))
request(fullRequest);
else if (fullRequest.contains("/getoption"))
request(fullRequest);
2023-01-06 16:32:25 +03:00
else if (fullRequest.contains("/binds"))
request(fullRequest);
else if (fullRequest.contains("/cursorpos"))
request(fullRequest);
2023-01-25 18:16:28 +03:00
else if (fullRequest.contains("/animations"))
request(fullRequest);
else if (fullRequest.contains("/globalshortcuts"))
request(fullRequest);
else if (fullRequest.contains("/rollinglog"))
request(fullRequest);
else if (fullRequest.contains("/instances"))
instancesRequest(json);
else if (fullRequest.contains("/switchxkblayout"))
request(fullRequest, 2);
2023-01-22 18:38:17 +03:00
else if (fullRequest.contains("/seterror"))
request(fullRequest, 1);
2023-01-24 22:05:34 +03:00
else if (fullRequest.contains("/setprop"))
request(fullRequest, 3);
else if (fullRequest.contains("/plugin"))
request(fullRequest, 1);
2023-03-20 19:00:54 +03:00
else if (fullRequest.contains("/notify"))
request(fullRequest, 2);
else if (fullRequest.contains("/output"))
2023-08-08 19:41:00 +03:00
request(fullRequest, 2);
else if (fullRequest.contains("/setcursor"))
2023-08-08 19:41:00 +03:00
request(fullRequest, 1);
else if (fullRequest.contains("/dispatch"))
2023-08-08 19:41:00 +03:00
request(fullRequest, 1);
else if (fullRequest.contains("/keyword"))
2023-08-08 19:41:00 +03:00
request(fullRequest, 2);
else if (fullRequest.contains("/hyprpaper"))
2023-08-13 22:42:11 +03:00
requestHyprpaper(fullRequest);
else if (fullRequest.contains("/layouts"))
request(fullRequest);
else if (fullRequest.contains("/--help"))
printf("%s", USAGE.c_str());
2022-03-20 18:51:14 +03:00
else {
printf("%s\n", USAGE.c_str());
2022-03-20 18:51:14 +03:00
return 1;
}
printf("\n");
return exitStatus;
2022-06-17 23:02:57 +03:00
}