2024-06-14 12:11:40 +02:00
|
|
|
#include <cctype>
|
2022-03-21 18:29:41 +01:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
2024-06-14 12:11:40 +02:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2022-03-21 18:29:41 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2022-05-24 19:42:43 +02:00
|
|
|
#include <sys/un.h>
|
2024-04-28 23:25:24 +02:00
|
|
|
#include <pwd.h>
|
2022-03-20 16:51:14 +01:00
|
|
|
#include <unistd.h>
|
2022-07-19 14:27:52 +02:00
|
|
|
#include <ranges>
|
2022-07-19 14:28:37 +02:00
|
|
|
#include <algorithm>
|
2024-06-14 12:11:40 +02:00
|
|
|
#include <csignal>
|
2023-09-20 09:26:20 +02:00
|
|
|
#include <format>
|
2022-03-20 16:51:14 +01:00
|
|
|
|
|
|
|
#include <iostream>
|
2022-03-21 18:29:41 +01:00
|
|
|
#include <string>
|
2024-10-13 14:23:56 +02:00
|
|
|
#include <print>
|
2022-03-21 18:29:41 +01:00
|
|
|
#include <fstream>
|
2022-03-20 16:51:14 +01:00
|
|
|
#include <string>
|
2023-08-08 16:16:34 +02:00
|
|
|
#include <vector>
|
2022-07-15 11:58:09 +02:00
|
|
|
#include <deque>
|
2023-08-08 16:16:34 +02:00
|
|
|
#include <filesystem>
|
2024-06-14 12:11:40 +02:00
|
|
|
#include <cstdarg>
|
2024-03-26 14:38:54 +01:00
|
|
|
#include <regex>
|
2024-06-14 12:11:40 +02:00
|
|
|
#include <sys/socket.h>
|
2024-06-11 17:17:45 +02:00
|
|
|
#include <hyprutils/string/String.hpp>
|
2024-08-10 22:09:12 +02:00
|
|
|
#include <cstring>
|
2024-06-11 17:17:45 +02:00
|
|
|
using namespace Hyprutils::String;
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2024-04-03 22:41:10 +02:00
|
|
|
#include "Strings.hpp"
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2023-05-02 15:51:52 +02:00
|
|
|
#define PAD
|
|
|
|
|
2023-08-08 18:04:20 +02:00
|
|
|
std::string instanceSignature;
|
2024-06-09 21:16:29 +02:00
|
|
|
bool quiet = false;
|
2023-08-08 18:04:20 +02:00
|
|
|
|
|
|
|
struct SInstanceData {
|
|
|
|
std::string id;
|
|
|
|
uint64_t time;
|
|
|
|
uint64_t pid;
|
|
|
|
std::string wlSocket;
|
|
|
|
bool valid = true;
|
|
|
|
};
|
|
|
|
|
2024-10-13 14:23:56 +02:00
|
|
|
void log(const std::string& str) {
|
2024-06-09 21:16:29 +02:00
|
|
|
if (quiet)
|
|
|
|
return;
|
|
|
|
|
2024-10-13 14:23:56 +02:00
|
|
|
std::println("{}", str);
|
2024-06-09 21:16:29 +02:00
|
|
|
}
|
|
|
|
|
2024-04-30 15:17:32 +02:00
|
|
|
std::string getRuntimeDir() {
|
|
|
|
const auto XDG = getenv("XDG_RUNTIME_DIR");
|
|
|
|
|
|
|
|
if (!XDG) {
|
|
|
|
const std::string USERID = std::to_string(getpwuid(getuid())->pw_uid);
|
|
|
|
return "/run/user/" + USERID + "/hypr";
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::string{XDG} + "/hypr";
|
|
|
|
}
|
|
|
|
|
2023-08-08 18:04:20 +02:00
|
|
|
std::vector<SInstanceData> instances() {
|
|
|
|
std::vector<SInstanceData> result;
|
|
|
|
|
2024-11-25 15:42:06 +01:00
|
|
|
try {
|
|
|
|
if (!std::filesystem::exists(getRuntimeDir()))
|
|
|
|
return {};
|
|
|
|
} catch (std::exception& e) { return {}; }
|
|
|
|
|
2024-04-30 15:17:32 +02:00
|
|
|
for (const auto& el : std::filesystem::directory_iterator(getRuntimeDir())) {
|
2024-04-28 23:25:24 +02:00
|
|
|
if (!el.is_directory() || !std::filesystem::exists(el.path().string() + "/hyprland.lock"))
|
2023-08-08 18:04:20 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// read lock
|
|
|
|
SInstanceData* data = &result.emplace_back();
|
2024-05-01 17:47:31 +02:00
|
|
|
data->id = el.path().filename().string();
|
2023-08-08 18:04:20 +02:00
|
|
|
|
2024-02-19 00:31:43 +01:00
|
|
|
try {
|
2024-04-28 21:06:40 +02:00
|
|
|
data->time = std::stoull(data->id.substr(data->id.find_first_of('_') + 1, data->id.find_last_of('_') - (data->id.find_first_of('_') + 1)));
|
2024-02-24 18:36:29 +01:00
|
|
|
} catch (std::exception& e) { continue; }
|
2023-08-08 18:04:20 +02:00
|
|
|
|
|
|
|
// read file
|
2024-04-28 23:25:24 +02:00
|
|
|
std::ifstream ifs(el.path().string() + "/hyprland.lock");
|
2023-08-08 18:04:20 +02:00
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (std::string line; std::getline(ifs, line); ++i) {
|
|
|
|
if (i == 0) {
|
2024-02-19 00:31:43 +01:00
|
|
|
try {
|
|
|
|
data->pid = std::stoull(line);
|
2024-02-24 18:36:29 +01:00
|
|
|
} catch (std::exception& e) { continue; }
|
2023-08-08 18:04:20 +02:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2024-06-14 12:11:40 +02:00
|
|
|
static volatile bool sigintReceived = false;
|
|
|
|
void intHandler(int sig) {
|
|
|
|
sigintReceived = true;
|
2024-10-13 14:23:56 +02:00
|
|
|
std::println("[hyprctl] SIGINT received, closing connection");
|
2024-06-14 12:11:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int rollingRead(const int socket) {
|
|
|
|
sigintReceived = false;
|
|
|
|
signal(SIGINT, intHandler);
|
|
|
|
|
|
|
|
constexpr size_t BUFFER_SIZE = 8192;
|
|
|
|
std::array<char, BUFFER_SIZE> buffer = {0};
|
2024-08-08 21:01:50 +02:00
|
|
|
long sizeWritten = 0;
|
2024-10-13 14:23:56 +02:00
|
|
|
std::println("[hyprctl] reading from socket following up log:");
|
2024-06-14 12:11:40 +02:00
|
|
|
while (!sigintReceived) {
|
|
|
|
sizeWritten = read(socket, buffer.data(), BUFFER_SIZE);
|
|
|
|
if (sizeWritten < 0 && errno != EAGAIN) {
|
|
|
|
if (errno != EINTR)
|
2024-10-13 14:23:56 +02:00
|
|
|
std::println("Couldn't read (5): {}: {}", strerror(errno), errno);
|
2024-06-14 12:11:40 +02:00
|
|
|
close(socket);
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sizeWritten == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (sizeWritten > 0) {
|
2024-10-13 14:23:56 +02:00
|
|
|
std::println("{}", std::string(buffer.data(), sizeWritten));
|
2024-06-14 12:11:40 +02:00
|
|
|
buffer.fill('\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
usleep(100000);
|
|
|
|
}
|
|
|
|
close(socket);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int request(std::string arg, int minArgs = 0, bool needRoll = false) {
|
2023-01-24 20:05:34 +01:00
|
|
|
const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0);
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2024-08-02 21:49:47 +02:00
|
|
|
auto t = timeval{.tv_sec = 5, .tv_usec = 0};
|
2024-06-14 12:11:40 +02:00
|
|
|
setsockopt(SERVERSOCKET, SOL_SOCKET, SO_RCVTIMEO, &t, sizeof(struct timeval));
|
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
const auto ARGS = std::count(arg.begin(), arg.end(), ' ');
|
2022-12-03 16:56:07 +01:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
if (ARGS < minArgs) {
|
2024-06-14 12:11:40 +02:00
|
|
|
log(std::format("Not enough arguments in '{}', expected at least {}", arg, minArgs));
|
2024-05-15 17:54:23 +02:00
|
|
|
return -1;
|
2022-12-03 16:56:07 +01:00
|
|
|
}
|
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
if (SERVERSOCKET < 0) {
|
2024-06-09 21:16:29 +02:00
|
|
|
log("Couldn't open a socket (1)");
|
2024-05-15 17:54:23 +02:00
|
|
|
return 1;
|
2022-03-21 18:29:41 +01:00
|
|
|
}
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2023-08-08 18:04:20 +02:00
|
|
|
if (instanceSignature.empty()) {
|
2024-06-09 21:16:29 +02:00
|
|
|
log("HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)");
|
2024-05-15 17:54:23 +02:00
|
|
|
return 2;
|
2022-06-03 17:41:57 +02:00
|
|
|
}
|
|
|
|
|
2024-04-28 23:25:24 +02:00
|
|
|
const std::string USERID = std::to_string(getpwuid(getuid())->pw_uid);
|
2022-06-03 17:41:57 +02:00
|
|
|
|
2024-04-28 23:25:24 +02:00
|
|
|
sockaddr_un serverAddress = {0};
|
|
|
|
serverAddress.sun_family = AF_UNIX;
|
|
|
|
|
2024-04-30 15:17:32 +02:00
|
|
|
std::string socketPath = getRuntimeDir() + "/" + instanceSignature + "/.socket.sock";
|
2022-06-03 17:41:57 +02:00
|
|
|
|
2023-04-23 22:20:29 +02:00
|
|
|
strncpy(serverAddress.sun_path, socketPath.c_str(), sizeof(serverAddress.sun_path) - 1);
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
if (connect(SERVERSOCKET, (sockaddr*)&serverAddress, SUN_LEN(&serverAddress)) < 0) {
|
2024-06-09 21:16:29 +02:00
|
|
|
log("Couldn't connect to " + socketPath + ". (3)");
|
2024-05-15 17:54:23 +02:00
|
|
|
return 3;
|
2022-03-21 18:29:41 +01:00
|
|
|
}
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
auto sizeWritten = write(SERVERSOCKET, arg.c_str(), arg.length());
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
if (sizeWritten < 0) {
|
2024-06-09 21:16:29 +02:00
|
|
|
log("Couldn't write (4)");
|
2024-05-15 17:54:23 +02:00
|
|
|
return 4;
|
2022-03-21 18:29:41 +01:00
|
|
|
}
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2024-06-14 12:11:40 +02:00
|
|
|
if (needRoll)
|
|
|
|
return rollingRead(SERVERSOCKET);
|
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
std::string reply = "";
|
|
|
|
char buffer[8192] = {0};
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
sizeWritten = read(SERVERSOCKET, buffer, 8192);
|
2022-07-02 18:27:44 +02:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
if (sizeWritten < 0) {
|
2024-06-22 00:41:23 +02:00
|
|
|
if (errno == EWOULDBLOCK)
|
|
|
|
log("Hyprland IPC didn't respond in time\n");
|
2024-06-09 21:16:29 +02:00
|
|
|
log("Couldn't read (5)");
|
2024-05-15 17:54:23 +02:00
|
|
|
return 5;
|
2022-07-02 18:27:44 +02:00
|
|
|
}
|
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
reply += std::string(buffer, sizeWritten);
|
2022-10-26 14:11:05 +02:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
while (sizeWritten == 8192) {
|
|
|
|
sizeWritten = read(SERVERSOCKET, buffer, 8192);
|
|
|
|
if (sizeWritten < 0) {
|
2024-06-09 21:16:29 +02:00
|
|
|
log("Couldn't read (5)");
|
2024-05-15 17:54:23 +02:00
|
|
|
return 5;
|
2022-10-26 14:11:05 +02:00
|
|
|
}
|
2023-01-24 20:05:34 +01:00
|
|
|
reply += std::string(buffer, sizeWritten);
|
2022-10-26 14:11:05 +02:00
|
|
|
}
|
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
close(SERVERSOCKET);
|
2022-07-02 18:27:44 +02:00
|
|
|
|
2024-06-09 21:16:29 +02:00
|
|
|
log(reply);
|
2024-05-15 17:54:23 +02:00
|
|
|
|
|
|
|
return 0;
|
2022-07-02 18:27:44 +02:00
|
|
|
}
|
|
|
|
|
2024-05-15 17:54:23 +02:00
|
|
|
int requestHyprpaper(std::string arg) {
|
2022-07-02 18:27:44 +02:00
|
|
|
const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
|
|
|
|
if (SERVERSOCKET < 0) {
|
2024-06-09 21:16:29 +02:00
|
|
|
log("Couldn't open a socket (1)");
|
2024-05-15 17:54:23 +02:00
|
|
|
return 1;
|
2022-07-02 18:27:44 +02:00
|
|
|
}
|
|
|
|
|
2023-08-13 21:42:11 +02:00
|
|
|
if (instanceSignature.empty()) {
|
2024-06-09 21:16:29 +02:00
|
|
|
log("HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)");
|
2024-05-15 17:54:23 +02:00
|
|
|
return 2;
|
2022-07-02 18:27:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sockaddr_un serverAddress = {0};
|
2022-12-20 03:18:47 +01:00
|
|
|
serverAddress.sun_family = AF_UNIX;
|
2022-07-02 18:27:44 +02:00
|
|
|
|
2024-04-28 23:25:24 +02:00
|
|
|
const std::string USERID = std::to_string(getpwuid(getuid())->pw_uid);
|
|
|
|
|
2024-04-30 15:17:32 +02:00
|
|
|
std::string socketPath = getRuntimeDir() + "/" + instanceSignature + "/.hyprpaper.sock";
|
2022-07-02 18:27:44 +02:00
|
|
|
|
2023-04-23 22:20:29 +02:00
|
|
|
strncpy(serverAddress.sun_path, socketPath.c_str(), sizeof(serverAddress.sun_path) - 1);
|
2022-07-02 18:27:44 +02:00
|
|
|
|
|
|
|
if (connect(SERVERSOCKET, (sockaddr*)&serverAddress, SUN_LEN(&serverAddress)) < 0) {
|
2024-06-09 21:16:29 +02:00
|
|
|
log("Couldn't connect to " + socketPath + ". (3)");
|
2024-05-15 17:54:23 +02:00
|
|
|
return 3;
|
2022-07-02 18:27:44 +02:00
|
|
|
}
|
|
|
|
|
2023-08-13 21:42:11 +02:00
|
|
|
arg = arg.substr(arg.find_first_of('/') + 1); // strip flags
|
|
|
|
arg = arg.substr(arg.find_first_of(' ') + 1); // strip "hyprpaper"
|
|
|
|
|
2022-07-02 18:27:44 +02:00
|
|
|
auto sizeWritten = write(SERVERSOCKET, arg.c_str(), arg.length());
|
|
|
|
|
|
|
|
if (sizeWritten < 0) {
|
2024-06-09 21:16:29 +02:00
|
|
|
log("Couldn't write (4)");
|
2024-05-15 17:54:23 +02:00
|
|
|
return 4;
|
2022-07-02 18:27:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char buffer[8192] = {0};
|
|
|
|
|
|
|
|
sizeWritten = read(SERVERSOCKET, buffer, 8192);
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2022-03-21 18:29:41 +01:00
|
|
|
if (sizeWritten < 0) {
|
2024-06-09 21:16:29 +02:00
|
|
|
log("Couldn't read (5)");
|
2024-05-15 17:54:23 +02:00
|
|
|
return 5;
|
2022-03-21 18:29:41 +01:00
|
|
|
}
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2022-03-21 18:29:41 +01:00
|
|
|
close(SERVERSOCKET);
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2024-06-09 21:16:29 +02:00
|
|
|
log(std::string(buffer));
|
2024-05-15 17:54:23 +02:00
|
|
|
|
|
|
|
return 0;
|
2022-03-20 16:51:14 +01:00
|
|
|
}
|
|
|
|
|
2024-03-26 14:38:54 +01:00
|
|
|
void batchRequest(std::string arg, bool json) {
|
|
|
|
std::string commands = arg.substr(arg.find_first_of(" ") + 1);
|
2022-09-25 20:07:48 +02:00
|
|
|
|
2024-03-26 14:38:54 +01:00
|
|
|
if (json) {
|
|
|
|
commands = "j/" + std::regex_replace(commands, std::regex(";\\s*"), ";j/");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string rq = "[[BATCH]]" + commands;
|
2022-04-29 19:44:09 +02:00
|
|
|
request(rq);
|
|
|
|
}
|
|
|
|
|
2023-08-08 16:16:34 +02:00
|
|
|
void instancesRequest(bool json) {
|
|
|
|
std::string result = "";
|
|
|
|
|
|
|
|
// gather instance data
|
2023-08-08 18:04:20 +02:00
|
|
|
std::vector<SInstanceData> inst = instances();
|
2023-08-08 16:16:34 +02:00
|
|
|
|
|
|
|
if (!json) {
|
2024-08-26 20:24:30 +02:00
|
|
|
for (auto const& el : inst) {
|
2023-09-20 09:26:20 +02:00
|
|
|
result += std::format("instance {}:\n\ttime: {}\n\tpid: {}\n\twl socket: {}\n\n", el.id, el.time, el.pid, el.wlSocket);
|
2023-08-08 16:16:34 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result += '[';
|
2024-08-26 20:24:30 +02:00
|
|
|
for (auto const& el : inst) {
|
2023-09-20 09:26:20 +02:00
|
|
|
result += std::format(R"#(
|
|
|
|
{{
|
|
|
|
"instance": "{}",
|
|
|
|
"time": {},
|
|
|
|
"pid": {},
|
|
|
|
"wl_socket": "{}"
|
|
|
|
}},)#",
|
|
|
|
el.id, el.time, el.pid, el.wlSocket);
|
2023-08-08 16:16:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
result.pop_back();
|
|
|
|
result += "\n]";
|
|
|
|
}
|
|
|
|
|
2024-06-09 21:16:29 +02:00
|
|
|
log(result + "\n");
|
2023-08-08 16:16:34 +02:00
|
|
|
}
|
|
|
|
|
2022-07-15 11:58:09 +02: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-03-20 16:51:14 +01:00
|
|
|
int main(int argc, char** argv) {
|
2022-11-10 16:36:36 +01:00
|
|
|
bool parseArgs = true;
|
2022-03-20 16:51:14 +01:00
|
|
|
|
|
|
|
if (argc < 2) {
|
2024-10-13 14:23:56 +02:00
|
|
|
std::println("{}", USAGE);
|
2022-03-20 16:51:14 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-08-08 18:04:20 +02:00
|
|
|
std::string fullRequest = "";
|
|
|
|
std::string fullArgs = "";
|
|
|
|
const auto ARGS = splitArgs(argc, argv);
|
|
|
|
bool json = false;
|
2024-06-14 12:11:40 +02:00
|
|
|
bool needRoll = false;
|
2023-08-08 18:04:20 +02:00
|
|
|
std::string overrideInstance = "";
|
2022-07-15 11:58:09 +02:00
|
|
|
|
2023-11-10 22:45:20 +01:00
|
|
|
for (std::size_t i = 0; i < ARGS.size(); ++i) {
|
2022-11-10 16:36:36 +01:00
|
|
|
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 11:58:09 +02:00
|
|
|
// parse
|
|
|
|
if (ARGS[i] == "-j" && !fullArgs.contains("j")) {
|
|
|
|
fullArgs += "j";
|
2023-08-08 16:16:34 +02:00
|
|
|
json = true;
|
2024-02-24 15:01:58 +01:00
|
|
|
} else if (ARGS[i] == "-r" && !fullArgs.contains("r")) {
|
|
|
|
fullArgs += "r";
|
2024-03-08 18:47:12 +01:00
|
|
|
} else if (ARGS[i] == "-a" && !fullArgs.contains("a")) {
|
|
|
|
fullArgs += "a";
|
2024-05-25 22:46:07 +02:00
|
|
|
} else if ((ARGS[i] == "-c" || ARGS[i] == "--config") && !fullArgs.contains("c")) {
|
|
|
|
fullArgs += "c";
|
2024-06-14 12:11:40 +02:00
|
|
|
} else if ((ARGS[i] == "-f" || ARGS[i] == "--follow") && !fullArgs.contains("f")) {
|
|
|
|
fullArgs += "f";
|
|
|
|
needRoll = true;
|
2022-07-15 11:58:09 +02:00
|
|
|
} else if (ARGS[i] == "--batch") {
|
|
|
|
fullRequest = "--batch ";
|
2023-08-08 18:04:20 +02:00
|
|
|
} else if (ARGS[i] == "--instance" || ARGS[i] == "-i") {
|
|
|
|
++i;
|
|
|
|
|
|
|
|
if (i >= ARGS.size()) {
|
2024-10-13 14:23:56 +02:00
|
|
|
std::println("{}", USAGE);
|
2023-08-08 18:04:20 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
overrideInstance = ARGS[i];
|
2024-06-09 21:16:29 +02:00
|
|
|
} else if (ARGS[i] == "-q" || ARGS[i] == "--quiet") {
|
|
|
|
quiet = true;
|
2024-04-03 22:41:10 +02:00
|
|
|
} else if (ARGS[i] == "--help") {
|
|
|
|
const std::string& cmd = ARGS[0];
|
|
|
|
|
|
|
|
if (cmd == "hyprpaper") {
|
2024-10-13 14:23:56 +02:00
|
|
|
std::println("{}", HYPRPAPER_HELP);
|
2024-04-03 22:41:10 +02:00
|
|
|
} else if (cmd == "notify") {
|
2024-10-13 14:23:56 +02:00
|
|
|
std::println("{}", NOTIFY_HELP);
|
2024-04-03 22:41:10 +02:00
|
|
|
} else if (cmd == "output") {
|
2024-10-13 14:23:56 +02:00
|
|
|
std::println("{}", OUTPUT_HELP);
|
2024-04-03 22:41:10 +02:00
|
|
|
} else if (cmd == "plugin") {
|
2024-10-13 14:23:56 +02:00
|
|
|
std::println("{}", PLUGIN_HELP);
|
2024-04-03 22:41:10 +02:00
|
|
|
} else if (cmd == "setprop") {
|
2024-10-13 14:23:56 +02:00
|
|
|
std::println("{}", SETPROP_HELP);
|
2024-04-03 22:41:10 +02:00
|
|
|
} else if (cmd == "switchxkblayout") {
|
2024-10-13 14:23:56 +02:00
|
|
|
std::println("{}", SWITCHXKBLAYOUT_HELP);
|
2024-04-03 22:41:10 +02:00
|
|
|
} else {
|
2024-10-13 14:23:56 +02:00
|
|
|
std::println("{}", USAGE);
|
2024-04-03 22:41:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2022-07-15 11:58:09 +02:00
|
|
|
} else {
|
2024-10-13 14:23:56 +02:00
|
|
|
std::println("{}", USAGE);
|
2022-07-15 11:58:09 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fullRequest += ARGS[i] + " ";
|
2022-07-13 00:34:28 +02:00
|
|
|
}
|
2022-07-15 11:58:09 +02:00
|
|
|
|
2022-07-15 21:39:39 +02:00
|
|
|
if (fullRequest.empty()) {
|
2024-10-13 14:23:56 +02:00
|
|
|
std::println("{}", USAGE);
|
2022-07-15 21:39:39 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-07-13 00:34:28 +02:00
|
|
|
fullRequest.pop_back(); // remove trailing space
|
2022-07-12 23:17:55 +02:00
|
|
|
|
2022-07-15 11:58:09 +02:00
|
|
|
fullRequest = fullArgs + "/" + fullRequest;
|
2022-12-20 03:18:47 +01:00
|
|
|
|
2023-12-08 16:40:01 +01:00
|
|
|
// instances is HIS-independent
|
|
|
|
if (fullRequest.contains("/instances")) {
|
|
|
|
instancesRequest(json);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-06-14 12:11:40 +02:00
|
|
|
if (needRoll && !fullRequest.contains("/rollinglog")) {
|
|
|
|
log("only 'rollinglog' command supports '--follow' option");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-08-08 18:04:20 +02:00
|
|
|
if (overrideInstance.contains("_"))
|
|
|
|
instanceSignature = overrideInstance;
|
|
|
|
else if (!overrideInstance.empty()) {
|
|
|
|
if (!isNumber(overrideInstance, false)) {
|
2024-06-09 21:16:29 +02:00
|
|
|
log("instance invalid\n");
|
2023-08-08 18:04:20 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto INSTANCENO = std::stoi(overrideInstance);
|
|
|
|
|
|
|
|
const auto INSTANCES = instances();
|
|
|
|
|
2023-11-10 22:45:20 +01:00
|
|
|
if (INSTANCENO < 0 || static_cast<std::size_t>(INSTANCENO) >= INSTANCES.size()) {
|
2024-06-09 21:16:29 +02:00
|
|
|
log("no such instance\n");
|
2023-08-08 18:04:20 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
instanceSignature = INSTANCES[INSTANCENO].id;
|
|
|
|
} else {
|
|
|
|
const auto ISIG = getenv("HYPRLAND_INSTANCE_SIGNATURE");
|
|
|
|
|
|
|
|
if (!ISIG) {
|
2024-06-09 21:16:29 +02:00
|
|
|
log("HYPRLAND_INSTANCE_SIGNATURE not set! (is hyprland running?)\n");
|
2023-08-08 18:04:20 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
instanceSignature = ISIG;
|
|
|
|
}
|
|
|
|
|
2022-10-25 18:04:11 +02:00
|
|
|
int exitStatus = 0;
|
2022-07-12 23:17:55 +02:00
|
|
|
|
2022-12-20 03:18:47 +01:00
|
|
|
if (fullRequest.contains("/--batch"))
|
2024-03-26 14:38:54 +01:00
|
|
|
batchRequest(fullRequest, json);
|
2024-02-12 11:34:21 +01:00
|
|
|
else if (fullRequest.contains("/hyprpaper"))
|
2024-05-15 17:54:23 +02:00
|
|
|
exitStatus = requestHyprpaper(fullRequest);
|
2022-12-20 03:18:47 +01:00
|
|
|
else if (fullRequest.contains("/switchxkblayout"))
|
2024-05-15 17:54:23 +02:00
|
|
|
exitStatus = request(fullRequest, 2);
|
2023-01-22 16:38:17 +01:00
|
|
|
else if (fullRequest.contains("/seterror"))
|
2024-05-15 17:54:23 +02:00
|
|
|
exitStatus = request(fullRequest, 1);
|
2023-01-24 20:05:34 +01:00
|
|
|
else if (fullRequest.contains("/setprop"))
|
2024-05-15 17:54:23 +02:00
|
|
|
exitStatus = request(fullRequest, 3);
|
2023-02-27 13:32:38 +01:00
|
|
|
else if (fullRequest.contains("/plugin"))
|
2024-05-15 17:54:23 +02:00
|
|
|
exitStatus = request(fullRequest, 1);
|
2024-03-02 19:12:31 +01:00
|
|
|
else if (fullRequest.contains("/dismissnotify"))
|
2024-05-15 17:54:23 +02:00
|
|
|
exitStatus = request(fullRequest, 0);
|
2023-03-20 17:00:54 +01:00
|
|
|
else if (fullRequest.contains("/notify"))
|
2024-05-15 17:54:23 +02:00
|
|
|
exitStatus = request(fullRequest, 2);
|
2022-12-20 03:18:47 +01:00
|
|
|
else if (fullRequest.contains("/output"))
|
2024-05-15 17:54:23 +02:00
|
|
|
exitStatus = request(fullRequest, 2);
|
2022-12-20 03:18:47 +01:00
|
|
|
else if (fullRequest.contains("/setcursor"))
|
2024-05-15 17:54:23 +02:00
|
|
|
exitStatus = request(fullRequest, 1);
|
2022-12-20 03:18:47 +01:00
|
|
|
else if (fullRequest.contains("/dispatch"))
|
2024-05-15 17:54:23 +02:00
|
|
|
exitStatus = request(fullRequest, 1);
|
2022-12-20 03:18:47 +01:00
|
|
|
else if (fullRequest.contains("/keyword"))
|
2024-05-15 17:54:23 +02:00
|
|
|
exitStatus = request(fullRequest, 2);
|
2023-12-28 16:38:16 +01:00
|
|
|
else if (fullRequest.contains("/decorations"))
|
2024-05-15 17:54:23 +02:00
|
|
|
exitStatus = request(fullRequest, 1);
|
2022-12-20 03:18:47 +01:00
|
|
|
else if (fullRequest.contains("/--help"))
|
2024-10-13 14:23:56 +02:00
|
|
|
std::println("{}", USAGE);
|
2024-06-14 12:11:40 +02:00
|
|
|
else if (fullRequest.contains("/rollinglog") && needRoll)
|
|
|
|
exitStatus = request(fullRequest, 0, true);
|
2022-03-20 16:51:14 +01:00
|
|
|
else {
|
2024-05-15 17:54:23 +02:00
|
|
|
exitStatus = request(fullRequest);
|
2022-03-20 16:51:14 +01:00
|
|
|
}
|
|
|
|
|
2024-06-09 21:16:29 +02:00
|
|
|
std::cout << std::flush;
|
2022-10-25 18:04:11 +02:00
|
|
|
return exitStatus;
|
2022-06-17 22:02:57 +02:00
|
|
|
}
|