From d1d4a63d4473cd4910b678cf5b385f622186fbd3 Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Thu, 19 Dec 2024 15:00:45 +1300 Subject: feat: Added IPC client side to main binary Updated command line arguments to use `getopt_long' Made it so both -v and --version work Made it so that if you give it non recognised arguments they are sent through the socket to the running window manager if possible. --- src/IPC.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 74 insertions(+), 5 deletions(-) (limited to 'src/IPC.cpp') diff --git a/src/IPC.cpp b/src/IPC.cpp index 709cc5c..3ed1bb6 100644 --- a/src/IPC.cpp +++ b/src/IPC.cpp @@ -1,6 +1,7 @@ #include "IPC.h" #include "ewmh.h" +#include #include #include #include @@ -11,14 +12,14 @@ using std::cout, std::endl; static const char* path = "/tmp/YATwm.sock"; -IPCModule::IPCModule(CommandsModule& commandsModule, Config& cfg, Globals& globals) +IPCServerModule::IPCServerModule(CommandsModule& commandsModule, Config& cfg, Globals& globals) :commandsModule(commandsModule), cfg(cfg), globals(globals) { } -void IPCModule::init() +void IPCServerModule::init() { sockfd = socket(AF_UNIX, SOCK_STREAM, 0); address.sun_family = AF_UNIX; @@ -35,7 +36,7 @@ void IPCModule::init() ready = true; } -void IPCModule::doListen() +void IPCServerModule::doListen() { if(!ready) return; @@ -73,7 +74,7 @@ void IPCModule::doListen() close(newsock); } -void IPCModule::quitIPC() +void IPCServerModule::quitIPC() { if(!ready) return; @@ -81,7 +82,7 @@ void IPCModule::quitIPC() ready = false; } -int IPCModule::getFD() +int IPCServerModule::getFD() { if(!ready) return -1; @@ -89,3 +90,71 @@ int IPCModule::getFD() return sockfd; return -1; } + +IPCClientModule::IPCClientModule() +{ + ready = false; +} + +int IPCClientModule::init() +{ + Display* dpy = XOpenDisplay(nullptr); + Window root = Window(DefaultRootWindow(dpy)); + Atom propName = XInternAtom(dpy, "YATWM_SOCKET_PATH", false); + Atom propType = XInternAtom(dpy, "STRING", false); + int format; + unsigned long length; + unsigned long after; + Atom type; + unsigned char* sockPath; + + if(XGetWindowProperty(dpy, root, propName, 0L, 32L, False, propType, &type, &format, &length, &after, &sockPath) != Success || type == None) + { + cout << "Failed to get path" << endl; + XFree(sockPath); + return 1; + } + + sockfd = socket(AF_UNIX, SOCK_STREAM, 0); + if(sockfd == -1) + { + cout << "Failed to create socket" << endl; + XFree(sockPath); + return -1; + } + sockaddr_un address; + address.sun_family = AF_UNIX; + strcpy(address.sun_path, (const char*)sockPath); + if(connect(sockfd, (sockaddr*) &address, sizeof(address)) == -1) + { + cout << "Failed connect" << endl; + XFree(sockPath); + return -1; + } + XFree(sockPath); + XCloseDisplay(dpy); + + ready = true; + return 0; +} + +int IPCClientModule::sendMessage(const char* message, int length) +{ + if(!ready) + return 1; + return write(sockfd, message, length); +} + +int IPCClientModule::getMessage(char* buff, int buffsize) +{ + if(!ready) + return 1; + return read(sockfd, buff, buffsize); +} + +void IPCClientModule::quit() +{ + if(!ready) + return; + close(sockfd); +} -- cgit v1.2.3