summaryrefslogtreecommitdiff
path: root/src/IPC.cpp
diff options
context:
space:
mode:
authorBossCode45 <human.cyborg42@gmail.com>2024-12-19 15:00:45 +1300
committerBossCode45 <human.cyborg42@gmail.com>2024-12-24 15:56:39 +1300
commitd1d4a63d4473cd4910b678cf5b385f622186fbd3 (patch)
treee6a4a478ad63e29ab12479d88a700238b970ec26 /src/IPC.cpp
parentc3791254bfb127d64b329098bc792acaa9c30832 (diff)
downloadYATwm-d1d4a63d4473cd4910b678cf5b385f622186fbd3.tar.gz
YATwm-d1d4a63d4473cd4910b678cf5b385f622186fbd3.zip
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.
Diffstat (limited to 'src/IPC.cpp')
-rw-r--r--src/IPC.cpp79
1 files changed, 74 insertions, 5 deletions
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 <X11/Xlib.h>
#include <cstring>
#include <string>
#include <sys/socket.h>
@@ -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);
+}