mirror of
https://github.com/hyprwm/Hyprland
synced 2024-11-23 03:05:59 +01:00
ba7e3cd9a1
IPC is now done with UNIX sockets instead of TCP ones (security!), and added Socket2, see Wiki.
130 lines
No EOL
3 KiB
C++
130 lines
No EOL
3 KiB
C++
#include <ctype.h>
|
|
#include <netdb.h>
|
|
#include <netinet/in.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
#include <sys/un.h>
|
|
#include <unistd.h>
|
|
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <fstream>
|
|
#include <string>
|
|
|
|
const std::string USAGE = R"#(
|
|
usage: hyprctl [command] [(opt)args]
|
|
|
|
monitors
|
|
workspaces
|
|
clients
|
|
activewindow
|
|
layers
|
|
dispatch
|
|
keyword
|
|
version
|
|
reload
|
|
)#";
|
|
|
|
void request(std::string arg) {
|
|
const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (SERVERSOCKET < 0) {
|
|
std::cout << "Couldn't open a socket (1)";
|
|
return;
|
|
}
|
|
|
|
const auto SERVER = gethostbyname("localhost");
|
|
|
|
if (!SERVER) {
|
|
std::cout << "Couldn't get host (2)";
|
|
return;
|
|
}
|
|
|
|
sockaddr_un serverAddress = {0};
|
|
serverAddress.sun_family = AF_UNIX;
|
|
strcpy(serverAddress.sun_path, "/tmp/hypr/.socket.sock");
|
|
|
|
if (connect(SERVERSOCKET, (sockaddr*)&serverAddress, SUN_LEN(&serverAddress)) < 0) {
|
|
std::cout << "Couldn't connect to /tmp/hypr/.socket.sock. (3) Is Hyprland running?";
|
|
return;
|
|
}
|
|
|
|
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);
|
|
|
|
if (sizeWritten < 0) {
|
|
std::cout << "Couldn't read (5)";
|
|
return;
|
|
}
|
|
|
|
close(SERVERSOCKET);
|
|
|
|
std::cout << std::string(buffer);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
void batchRequest(int argc, char** argv) {
|
|
std::string rq = "[[BATCH]]" + std::string(argv[2]);
|
|
|
|
request(rq);
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
int bflag = 0, sflag = 0, index, c;
|
|
|
|
if (argc < 2) {
|
|
printf("%s", USAGE.c_str());
|
|
return 1;
|
|
}
|
|
|
|
if (!strcmp(argv[1], "monitors")) request("monitors");
|
|
else if (!strcmp(argv[1], "clients")) request("clients");
|
|
else if (!strcmp(argv[1], "workspaces")) request("workspaces");
|
|
else if (!strcmp(argv[1], "activewindow")) request("activewindow");
|
|
else if (!strcmp(argv[1], "layers")) request("layers");
|
|
else if (!strcmp(argv[1], "version")) request("version");
|
|
else if (!strcmp(argv[1], "reload")) request("reload");
|
|
else if (!strcmp(argv[1], "dispatch")) dispatchRequest(argc, argv);
|
|
else if (!strcmp(argv[1], "keyword")) keywordRequest(argc, argv);
|
|
else if (!strcmp(argv[1], "--batch")) batchRequest(argc, argv);
|
|
else {
|
|
printf("%s", USAGE.c_str());
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
} |