2022-03-20 16:51:14 +01:00
|
|
|
#include <ctype.h>
|
2022-03-21 18:29:41 +01:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
2022-03-20 16:51:14 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2022-03-21 18:29:41 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2022-05-24 19:42:43 +02:00
|
|
|
#include <sys/un.h>
|
2022-03-20 16:51:14 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <iostream>
|
2022-03-21 18:29:41 +01:00
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
2022-03-20 16:51:14 +01:00
|
|
|
#include <string>
|
|
|
|
|
2022-06-17 12:18:25 +02:00
|
|
|
const std::string USAGE = R"#(usage: hyprctl [command] [(opt)args]
|
2022-03-20 16:51:14 +01:00
|
|
|
|
|
|
|
monitors
|
|
|
|
workspaces
|
|
|
|
clients
|
2022-03-30 22:40:53 +02:00
|
|
|
activewindow
|
|
|
|
layers
|
2022-06-02 13:59:33 +02:00
|
|
|
devices
|
2022-04-21 16:11:29 +02:00
|
|
|
dispatch
|
2022-04-21 16:56:27 +02:00
|
|
|
keyword
|
2022-04-22 18:14:25 +02:00
|
|
|
version
|
2022-06-17 12:18:25 +02:00
|
|
|
reload)#";
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2022-03-21 18:29:41 +01:00
|
|
|
void request(std::string arg) {
|
2022-05-24 19:42:43 +02:00
|
|
|
const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0);
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2022-03-21 18:29:41 +01:00
|
|
|
if (SERVERSOCKET < 0) {
|
|
|
|
std::cout << "Couldn't open a socket (1)";
|
|
|
|
return;
|
|
|
|
}
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2022-03-21 18:29:41 +01:00
|
|
|
const auto SERVER = gethostbyname("localhost");
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2022-03-21 18:29:41 +01:00
|
|
|
if (!SERVER) {
|
|
|
|
std::cout << "Couldn't get host (2)";
|
|
|
|
return;
|
|
|
|
}
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2022-06-03 17:41:57 +02:00
|
|
|
// get the instance signature
|
|
|
|
auto instanceSig = getenv("HYPRLAND_INSTANCE_SIGNATURE");
|
|
|
|
|
|
|
|
if (!instanceSig) {
|
|
|
|
std::cout << "HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string instanceSigStr = std::string(instanceSig);
|
|
|
|
|
2022-05-24 19:42:43 +02:00
|
|
|
sockaddr_un serverAddress = {0};
|
|
|
|
serverAddress.sun_family = AF_UNIX;
|
2022-06-03 17:41:57 +02:00
|
|
|
|
|
|
|
std::string socketPath = "/tmp/hypr/" + instanceSigStr + "/.socket.sock";
|
|
|
|
|
|
|
|
strcpy(serverAddress.sun_path, socketPath.c_str());
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2022-05-24 19:42:43 +02:00
|
|
|
if (connect(SERVERSOCKET, (sockaddr*)&serverAddress, SUN_LEN(&serverAddress)) < 0) {
|
2022-06-03 17:41:57 +02:00
|
|
|
std::cout << "Couldn't connect to " << socketPath << ". (3)";
|
2022-03-21 18:29:41 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2022-03-21 18:29:41 +01:00
|
|
|
auto sizeWritten = write(SERVERSOCKET, arg.c_str(), arg.length());
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2022-03-21 18:29:41 +01:00
|
|
|
if (sizeWritten < 0) {
|
|
|
|
std::cout << "Couldn't write (4)";
|
|
|
|
return;
|
|
|
|
}
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2022-03-21 18:29:41 +01:00
|
|
|
char buffer[8192] = {0};
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2022-03-21 18:29:41 +01:00
|
|
|
sizeWritten = read(SERVERSOCKET,buffer, 8192);
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2022-03-21 18:29:41 +01:00
|
|
|
if (sizeWritten < 0) {
|
|
|
|
std::cout << "Couldn't read (5)";
|
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
|
2022-03-21 18:29:41 +01:00
|
|
|
std::cout << std::string(buffer);
|
2022-03-20 16:51:14 +01:00
|
|
|
}
|
|
|
|
|
2022-04-21 16:11:29 +02:00
|
|
|
void dispatchRequest(int argc, char** argv) {
|
|
|
|
|
|
|
|
if (argc < 4) {
|
|
|
|
std::cout << "dispatch requires 2 params";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string rq = "dispatch " + std::string(argv[2]) + " " + std::string(argv[3]);
|
|
|
|
|
|
|
|
request(rq);
|
|
|
|
}
|
|
|
|
|
2022-04-21 16:56:27 +02:00
|
|
|
void keywordRequest(int argc, char** argv) {
|
|
|
|
if (argc < 4) {
|
|
|
|
std::cout << "keyword requires 2 params";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string rq = "keyword " + std::string(argv[2]) + " " + std::string(argv[3]);
|
|
|
|
|
|
|
|
request(rq);
|
|
|
|
}
|
|
|
|
|
2022-04-29 19:44:09 +02:00
|
|
|
void batchRequest(int argc, char** argv) {
|
|
|
|
std::string rq = "[[BATCH]]" + std::string(argv[2]);
|
|
|
|
|
|
|
|
request(rq);
|
|
|
|
}
|
|
|
|
|
2022-03-20 16:51:14 +01:00
|
|
|
int main(int argc, char** argv) {
|
|
|
|
int bflag = 0, sflag = 0, index, c;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
2022-06-17 12:18:25 +02:00
|
|
|
printf("%s\n", USAGE.c_str());
|
2022-03-20 16:51:14 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-03-21 18:34:49 +01:00
|
|
|
if (!strcmp(argv[1], "monitors")) request("monitors");
|
|
|
|
else if (!strcmp(argv[1], "clients")) request("clients");
|
|
|
|
else if (!strcmp(argv[1], "workspaces")) request("workspaces");
|
2022-03-22 16:54:45 +01:00
|
|
|
else if (!strcmp(argv[1], "activewindow")) request("activewindow");
|
|
|
|
else if (!strcmp(argv[1], "layers")) request("layers");
|
2022-04-22 18:14:25 +02:00
|
|
|
else if (!strcmp(argv[1], "version")) request("version");
|
2022-06-02 13:59:33 +02:00
|
|
|
else if (!strcmp(argv[1], "devices")) request("devices");
|
2022-05-08 15:28:45 +02:00
|
|
|
else if (!strcmp(argv[1], "reload")) request("reload");
|
2022-04-21 16:11:29 +02:00
|
|
|
else if (!strcmp(argv[1], "dispatch")) dispatchRequest(argc, argv);
|
2022-04-21 16:56:27 +02:00
|
|
|
else if (!strcmp(argv[1], "keyword")) keywordRequest(argc, argv);
|
2022-04-29 19:44:09 +02:00
|
|
|
else if (!strcmp(argv[1], "--batch")) batchRequest(argc, argv);
|
2022-06-17 22:02:57 +02:00
|
|
|
else if (!strcmp(argv[1], "--help")) printf("%s", USAGE.c_str());
|
2022-03-20 16:51:14 +01:00
|
|
|
else {
|
2022-06-17 12:18:25 +02:00
|
|
|
printf("%s\n", USAGE.c_str());
|
2022-03-20 16:51:14 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-06-17 12:18:25 +02:00
|
|
|
printf("\n");
|
2022-03-20 16:51:14 +01:00
|
|
|
return 0;
|
2022-06-17 22:02:57 +02:00
|
|
|
}
|