mirror of
https://github.com/hyprwm/Hyprland
synced 2024-11-02 15:06:00 +01:00
hyprctl: add --instance
This commit is contained in:
parent
b3393c429f
commit
36052abd33
1 changed files with 96 additions and 53 deletions
149
hyprctl/main.cpp
149
hyprctl/main.cpp
|
@ -54,10 +54,58 @@ commands:
|
||||||
flags:
|
flags:
|
||||||
-j -> output in JSON
|
-j -> output in JSON
|
||||||
--batch -> execute a batch of commands, separated by ';'
|
--batch -> execute a batch of commands, separated by ';'
|
||||||
|
--instance (-i) -> use a specific instance. Can be either signature or index in hyprctl instances (0, 1, etc)
|
||||||
)#";
|
)#";
|
||||||
|
|
||||||
#define PAD
|
#define PAD
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
std::string getFormat(const char* fmt, ...) {
|
std::string getFormat(const char* fmt, ...) {
|
||||||
char* outputStr = nullptr;
|
char* outputStr = nullptr;
|
||||||
|
|
||||||
|
@ -87,20 +135,15 @@ void request(std::string arg, int minArgs = 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the instance signature
|
if (instanceSignature.empty()) {
|
||||||
auto instanceSig = getenv("HYPRLAND_INSTANCE_SIGNATURE");
|
|
||||||
|
|
||||||
if (!instanceSig) {
|
|
||||||
std::cout << "HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)";
|
std::cout << "HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string instanceSigStr = std::string(instanceSig);
|
|
||||||
|
|
||||||
sockaddr_un serverAddress = {0};
|
sockaddr_un serverAddress = {0};
|
||||||
serverAddress.sun_family = AF_UNIX;
|
serverAddress.sun_family = AF_UNIX;
|
||||||
|
|
||||||
std::string socketPath = "/tmp/hypr/" + instanceSigStr + "/.socket.sock";
|
std::string socketPath = "/tmp/hypr/" + instanceSignature + "/.socket.sock";
|
||||||
|
|
||||||
strncpy(serverAddress.sun_path, socketPath.c_str(), sizeof(serverAddress.sun_path) - 1);
|
strncpy(serverAddress.sun_path, socketPath.c_str(), sizeof(serverAddress.sun_path) - 1);
|
||||||
|
|
||||||
|
@ -282,55 +325,16 @@ void batchRequest(std::string arg) {
|
||||||
void instancesRequest(bool json) {
|
void instancesRequest(bool json) {
|
||||||
std::string result = "";
|
std::string result = "";
|
||||||
|
|
||||||
struct SInstanceData {
|
|
||||||
std::string id;
|
|
||||||
uint64_t time;
|
|
||||||
uint64_t pid;
|
|
||||||
std::string wlSocket;
|
|
||||||
bool valid = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
// gather instance data
|
// gather instance data
|
||||||
std::vector<SInstanceData> instances;
|
std::vector<SInstanceData> inst = instances();
|
||||||
|
|
||||||
for (const auto& el : std::filesystem::directory_iterator("/tmp/hypr")) {
|
|
||||||
if (el.is_directory())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// read lock
|
|
||||||
SInstanceData* data = &instances.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(instances, [&](const auto& el) { return kill(el.pid, 0) != 0 && errno == ESRCH; });
|
|
||||||
|
|
||||||
std::sort(instances.begin(), instances.end(), [&](const auto& a, const auto& b) { return a.time < b.time; });
|
|
||||||
|
|
||||||
if (!json) {
|
if (!json) {
|
||||||
for (auto& el : instances) {
|
for (auto& el : inst) {
|
||||||
result += getFormat("instance %s:\n\ttime: %llu\n\tpid: %llu\n\twl socket: %s\n\n", el.id.c_str(), el.time, el.pid, el.wlSocket.c_str());
|
result += getFormat("instance %s:\n\ttime: %llu\n\tpid: %llu\n\twl socket: %s\n\n", el.id.c_str(), el.time, el.pid, el.wlSocket.c_str());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
result += '[';
|
result += '[';
|
||||||
for (auto& el : instances) {
|
for (auto& el : inst) {
|
||||||
result += getFormat(R"#(
|
result += getFormat(R"#(
|
||||||
{
|
{
|
||||||
"instance": "%s",
|
"instance": "%s",
|
||||||
|
@ -372,10 +376,11 @@ int main(int argc, char** argv) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string fullRequest = "";
|
std::string fullRequest = "";
|
||||||
std::string fullArgs = "";
|
std::string fullArgs = "";
|
||||||
const auto ARGS = splitArgs(argc, argv);
|
const auto ARGS = splitArgs(argc, argv);
|
||||||
bool json = false;
|
bool json = false;
|
||||||
|
std::string overrideInstance = "";
|
||||||
|
|
||||||
for (auto i = 0; i < ARGS.size(); ++i) {
|
for (auto i = 0; i < ARGS.size(); ++i) {
|
||||||
if (ARGS[i] == "--") {
|
if (ARGS[i] == "--") {
|
||||||
|
@ -390,6 +395,15 @@ int main(int argc, char** argv) {
|
||||||
json = true;
|
json = true;
|
||||||
} else if (ARGS[i] == "--batch") {
|
} else if (ARGS[i] == "--batch") {
|
||||||
fullRequest = "--batch ";
|
fullRequest = "--batch ";
|
||||||
|
} else if (ARGS[i] == "--instance" || ARGS[i] == "-i") {
|
||||||
|
++i;
|
||||||
|
|
||||||
|
if (i >= ARGS.size()) {
|
||||||
|
printf("%s\n", USAGE.c_str());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
overrideInstance = ARGS[i];
|
||||||
} else {
|
} else {
|
||||||
printf("%s\n", USAGE.c_str());
|
printf("%s\n", USAGE.c_str());
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -410,6 +424,35 @@ int main(int argc, char** argv) {
|
||||||
|
|
||||||
fullRequest = fullArgs + "/" + fullRequest;
|
fullRequest = fullArgs + "/" + fullRequest;
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
if (INSTANCENO < 0 || INSTANCENO >= INSTANCES.size()) {
|
||||||
|
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;
|
int exitStatus = 0;
|
||||||
|
|
||||||
if (fullRequest.contains("/--batch"))
|
if (fullRequest.contains("/--batch"))
|
||||||
|
|
Loading…
Reference in a new issue