IPC is now a socket

This commit is contained in:
vaxerski 2022-03-21 18:29:41 +01:00
parent 30ae4be181
commit 509f0c9a8a
5 changed files with 126 additions and 75 deletions

View file

@ -32,7 +32,6 @@ Nevertheless, REPORT any you find! Make an issue!
- Fix electron rendering issues - Fix electron rendering issues
- Fix sloppy dragging of windows - Fix sloppy dragging of windows
- Optimization - Optimization
- IPC done correctly with a socket
- Fix weird scroll on XWayland - Fix weird scroll on XWayland
- Become sane - Become sane
- STABILITY - STABILITY

View file

@ -1,11 +1,17 @@
#include <ctype.h> #include <ctype.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <fstream>
#include <iostream> #include <iostream>
#include <string.h> #include <string>
#include <fstream>
#include <string> #include <string>
const std::string USAGE = R"#( const std::string USAGE = R"#(
@ -16,64 +22,76 @@ usage: hyprctl [command] [(opt)args]
clients clients
)#"; )#";
void readReply() { void request(std::string arg) {
std::ifstream ifs; const auto SERVERSOCKET = socket(AF_INET, SOCK_STREAM, 0);
while (1) { if (SERVERSOCKET < 0) {
usleep(1000 * 25); std::cout << "Couldn't open a socket (1)";
return;
ifs.open("/tmp/hypr/.hyprlandrq");
if (ifs.good()) {
std::string reply = "";
std::getline(ifs, reply);
if (reply.find("RPLY:") != std::string::npos) {
reply = "";
std::string temp = "";
while (std::getline(ifs, temp))
reply += temp + '\n';
std::cout << reply;
unlink("/tmp/hypr/.hyprlandrq"); // cleanup
break;
}
}
}
} }
void requestMonitors() { const auto SERVER = gethostbyname("localhost");
std::ofstream ofs;
ofs.open("/tmp/hypr/.hyprlandrq", std::ios::trunc);
ofs << "R>monitors"; if (!SERVER) {
std::cout << "Couldn't get host (2)";
ofs.close(); return;
readReply();
} }
void requestClients() { sockaddr_in serverAddress = {0};
std::ofstream ofs; serverAddress.sin_family = AF_INET;
ofs.open("/tmp/hypr/.hyprlandrq", std::ios::trunc); bcopy((char*)SERVER->h_addr, (char*)&serverAddress.sin_addr.s_addr, SERVER->h_length);
ofs << "R>clients"; std::ifstream socketPortStream;
socketPortStream.open("/tmp/hypr/.socket");
ofs.close(); if (!socketPortStream.good()) {
std::cout << "No socket port file (2a)";
readReply(); return;
} }
void requestWorkspaces() { std::string port = "";
std::ofstream ofs; std::getline(socketPortStream, port);
ofs.open("/tmp/hypr/.hyprlandrq", std::ios::trunc); socketPortStream.close();
ofs << "R>workspaces"; int portInt = 0;
try {
portInt = std::stoi(port.c_str());
} catch (...) {
std::cout << "Port not an int?! (2b)";
return;
}
ofs.close(); if (portInt == 0) {
std::cout << "Port not an int?! (2c)";
return;
}
readReply(); serverAddress.sin_port = portInt;
if (connect(SERVERSOCKET, (sockaddr*)&serverAddress, sizeof(serverAddress)) < 0) {
std::cout << "Couldn't connect to port " << port << " (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);
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
@ -84,9 +102,9 @@ int main(int argc, char** argv) {
return 1; return 1;
} }
if (!strcmp(argv[1], "monitors")) requestMonitors(); if (!strcmp(argv[1], "monitors")) request("R>monitors");
else if (!strcmp(argv[1], "clients")) requestClients(); else if (!strcmp(argv[1], "clients")) request("R>clients");
else if (!strcmp(argv[1], "workspaces")) requestWorkspaces(); else if (!strcmp(argv[1], "workspaces")) request("R>workspaces");
else { else {
printf(USAGE.c_str()); printf(USAGE.c_str());
return 1; return 1;

View file

@ -1,6 +1,12 @@
#include "HyprCtl.hpp" #include "HyprCtl.hpp"
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <string> #include <string>
@ -46,31 +52,58 @@ std::string workspacesRequest() {
return result; return result;
} }
void HyprCtl::tickHyprCtl() { void HyprCtl::startHyprCtlSocket() {
struct stat buf; int port = 9187;
if (stat("/tmp/hypr/.hyprlandrq", &buf) == 0) { std::thread([&]() {
// file exists, let's open it const auto SOCKET = socket(AF_INET, SOCK_STREAM, 0);
requestStream.open("/tmp/hypr/.hyprlandrq"); if (SOCKET < 0) {
Debug::log(ERR, "Couldn't start the Hyprland Socket. (1) IPC will not work.");
return;
}
std::string request = ""; const sockaddr_in SERVERADDRESS = {.sin_family = AF_INET, .sin_port = port, .sin_addr = (in_addr)INADDR_ANY};
std::getline(requestStream, request);
requestStream.close(); if (bind(SOCKET, (sockaddr*)&SERVERADDRESS, sizeof(SERVERADDRESS)) < 0) {
Debug::log(ERR, "Couldn't start the Hyprland Socket. (2) IPC will not work.");
return;
}
// 10 max queued.
listen(SOCKET, 10);
sockaddr_in clientAddress;
socklen_t clientSize = sizeof(clientAddress);
char readBuffer[1024] = {0};
Debug::log(LOG, "Hypr socket started on port %i", SERVERADDRESS.sin_port);
std::string cmd = "rm -f /tmp/hypr/.socket && echo \"" + std::to_string(SERVERADDRESS.sin_port) + "\" > /tmp/hypr/.socket";
system(cmd.c_str()); // forgive me for using system() but it works and it doesnt matter here that much
while(1) {
const auto ACCEPTEDCONNECTION = accept(SOCKET, (sockaddr*)&clientAddress, &clientSize);
if (ACCEPTEDCONNECTION < 0) {
Debug::log(ERR, "Couldn't listen on the Hyprland Socket. (3) IPC will not work.");
break;
}
auto messageSize = read(ACCEPTEDCONNECTION, readBuffer, 1024);
readBuffer[messageSize == 1024 ? 1024 : messageSize] = '\0';
std::string request(readBuffer);
std::string reply = ""; std::string reply = "";
if (request == "R>monitors") reply = monitorsRequest(); if (request == "R>monitors") reply = monitorsRequest();
if (request == "R>workspaces") reply = workspacesRequest(); if (request == "R>workspaces") reply = workspacesRequest();
if (request == "R>clients") reply = clientsRequest(); if (request == "R>clients") reply = clientsRequest();
if (reply != "") { write(ACCEPTEDCONNECTION, reply.c_str(), reply.length());
std::ofstream ofs;
ofs.open("/tmp/hypr/.hyprlandrq", std::ios::trunc);
ofs << "RPLY:\n" << reply;
ofs.close();
}
// the hyprctl app deletes the file when done. close(ACCEPTEDCONNECTION);
} }
}).detach();
} }

View file

@ -4,6 +4,7 @@
#include <fstream> #include <fstream>
namespace HyprCtl { namespace HyprCtl {
void startHyprCtlSocket();
void tickHyprCtl(); void tickHyprCtl();
inline std::ifstream requestStream; inline std::ifstream requestStream;

View file

@ -20,6 +20,8 @@ void CThreadManager::handle() {
g_pConfigManager->init(); g_pConfigManager->init();
HyprCtl::startHyprCtlSocket();
while (3.1415f) { while (3.1415f) {
slowUpdate++; slowUpdate++;
if (slowUpdate >= g_pConfigManager->getInt("general:max_fps")){ if (slowUpdate >= g_pConfigManager->getInt("general:max_fps")){
@ -27,8 +29,6 @@ void CThreadManager::handle() {
slowUpdate = 0; slowUpdate = 0;
} }
HyprCtl::tickHyprCtl();
std::this_thread::sleep_for(std::chrono::microseconds(1000000 / g_pConfigManager->getInt("general:max_fps"))); std::this_thread::sleep_for(std::chrono::microseconds(1000000 / g_pConfigManager->getInt("general:max_fps")));
} }
} }