Hyprland/hyprctl/main.cpp

147 lines
3.4 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>
2022-03-20 18:51:14 +03:00
#include <unistd.h>
#include <iostream>
2022-03-21 20:29:41 +03:00
#include <string>
#include <fstream>
2022-03-20 18:51:14 +03:00
#include <string>
const std::string USAGE = R"#(
usage: hyprctl [command] [(opt)args]
monitors
workspaces
clients
2022-03-30 23:40:53 +03:00
activewindow
layers
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-03-20 18:51:14 +03:00
)#";
2022-03-21 20:29:41 +03:00
void request(std::string arg) {
const auto SERVERSOCKET = socket(AF_INET, SOCK_STREAM, 0);
2022-03-20 18:51:14 +03:00
2022-03-21 20:29:41 +03:00
if (SERVERSOCKET < 0) {
std::cout << "Couldn't open a socket (1)";
return;
}
2022-03-20 18:51:14 +03:00
2022-03-21 20:29:41 +03:00
const auto SERVER = gethostbyname("localhost");
2022-03-20 18:51:14 +03:00
2022-03-21 20:29:41 +03:00
if (!SERVER) {
std::cout << "Couldn't get host (2)";
return;
}
2022-03-20 18:51:14 +03:00
2022-03-21 20:29:41 +03:00
sockaddr_in serverAddress = {0};
serverAddress.sin_family = AF_INET;
bcopy((char*)SERVER->h_addr, (char*)&serverAddress.sin_addr.s_addr, SERVER->h_length);
2022-03-20 18:51:14 +03:00
2022-03-21 20:29:41 +03:00
std::ifstream socketPortStream;
socketPortStream.open("/tmp/hypr/.socket");
if (!socketPortStream.good()) {
std::cout << "No socket port file (2a)";
return;
2022-03-20 18:51:14 +03:00
}
2022-03-21 20:29:41 +03:00
std::string port = "";
std::getline(socketPortStream, port);
socketPortStream.close();
2022-03-20 18:51:14 +03:00
2022-03-21 20:29:41 +03:00
int portInt = 0;
try {
portInt = std::stoi(port.c_str());
} catch (...) {
std::cout << "Port not an int?! (2b)";
return;
}
2022-03-20 18:51:14 +03:00
2022-03-21 20:29:41 +03:00
if (portInt == 0) {
std::cout << "Port not an int?! (2c)";
return;
}
serverAddress.sin_port = portInt;
2022-03-20 18:51:14 +03:00
2022-03-21 20:29:41 +03:00
if (connect(SERVERSOCKET, (sockaddr*)&serverAddress, sizeof(serverAddress)) < 0) {
std::cout << "Couldn't connect to port " << port << " (3) Is Hyprland running?";
return;
}
2022-03-20 18:51:14 +03:00
2022-03-21 20:29:41 +03:00
auto sizeWritten = write(SERVERSOCKET, arg.c_str(), arg.length());
2022-03-20 18:51:14 +03:00
2022-03-21 20:29:41 +03:00
if (sizeWritten < 0) {
std::cout << "Couldn't write (4)";
return;
}
2022-03-20 18:51:14 +03:00
2022-03-21 20:29:41 +03:00
char buffer[8192] = {0};
2022-03-20 18:51:14 +03:00
2022-03-21 20:29:41 +03:00
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-04-21 17:11:29 +03: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 17:56:27 +03: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-03-20 18:51:14 +03:00
int main(int argc, char** argv) {
int bflag = 0, sflag = 0, index, c;
if (argc < 2) {
printf(USAGE.c_str());
return 1;
}
2022-03-21 20:34:49 +03: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 18:54:45 +03:00
else if (!strcmp(argv[1], "activewindow")) request("activewindow");
else if (!strcmp(argv[1], "layers")) request("layers");
2022-04-22 19:14:25 +03:00
else if (!strcmp(argv[1], "version")) request("version");
2022-04-21 17:11:29 +03:00
else if (!strcmp(argv[1], "dispatch")) dispatchRequest(argc, argv);
2022-04-21 17:56:27 +03:00
else if (!strcmp(argv[1], "keyword")) keywordRequest(argc, argv);
2022-03-20 18:51:14 +03:00
else {
printf(USAGE.c_str());
return 1;
}
return 0;
}