diff options
Diffstat (limited to 'src/IPC.cpp')
| -rw-r--r-- | src/IPC.cpp | 79 |
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); +} |
