From 5dacf6d6d24eab71994e02b6b3c4fc6f6e309958 Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Fri, 29 Sep 2023 00:55:29 +1300 Subject: feat: IPC works kinda Still quite buggy, but first version --- IPC.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ IPC.h | 25 +++++++++++++++++++ main.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++--------------------- test | 2 ++ 4 files changed, 164 insertions(+), 28 deletions(-) create mode 100644 IPC.cpp create mode 100644 IPC.h diff --git a/IPC.cpp b/IPC.cpp new file mode 100644 index 0000000..215a15c --- /dev/null +++ b/IPC.cpp @@ -0,0 +1,80 @@ +#include "IPC.h" + +#include +#include +#include +#include + +using std::cout, std::endl; + +static const char* path = "/tmp/YATwm.sock"; + +IPCModule::IPCModule(CommandsModule& commandsModule, Config& cfg, Globals& globals) + :commandsModule(commandsModule), + cfg(cfg), + globals(globals) +{ + sockfd = socket(AF_UNIX, SOCK_STREAM, 0); + address.sun_family = AF_UNIX; + strcpy(address.sun_path, path); + unlink(address.sun_path); + len = strlen(address.sun_path) + sizeof(address.sun_family); + + if(bind(sockfd, (sockaddr*)&address, len) == -1) + { + cout << "ERROR" << endl; + } + cout << "SOCKETED" << endl; +} + +void IPCModule::doListen() +{ + if(listen(sockfd, 1) != 0) + { + cout << "ERROR 2" << endl; + return; + } + if(first) + { + first = false; + return; + } + cout << "DOLISTEN" << endl; + unsigned int socklen = 0; + sockaddr_un remote; + int newsock = accept(sockfd, (sockaddr*)&remote, &socklen); + cout << "LISTENING" << endl; + char buffer[256]; + memset(buffer, 0, 256); + read(newsock, buffer, 256); + std::string command(buffer); + while(command[command.size() - 1] == 0 || command[command.size() - 1] == '\n') + command = command.substr(0, command.size() - 1); + //cout << '"' << command << '"' << endl; + try + { + commandsModule.runCommand(command); + } + catch(Err e) + { + cout << e.code << " " << e.message << endl; + } + char* message = "RAN COMMAND"; + send(newsock, message, strlen(message), 0); + cout << "RAN COMMAND" << endl; + shutdown(newsock, SHUT_RDWR); + close(newsock); +} + +void IPCModule::quitIPC() +{ + close(sockfd); + cout << path << endl; +} + +int IPCModule::getFD() +{ + if(sockfd > 0) + return sockfd; + return -1; +} diff --git a/IPC.h b/IPC.h new file mode 100644 index 0000000..9e497de --- /dev/null +++ b/IPC.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include + +#include "commands.h" +#include "config.h" +#include "util.h" + +class IPCModule +{ +public: + IPCModule(CommandsModule& commandsModule, Config& cfg, Globals& globals); + void doListen(); + void quitIPC(); + int getFD(); +private: + CommandsModule& commandsModule; + Config& cfg; + Globals& globals; + int sockfd; + int len; + bool first = true; + sockaddr_un address; +}; diff --git a/main.cpp b/main.cpp index cc3d2bb..eb5f8a9 100644 --- a/main.cpp +++ b/main.cpp @@ -2,13 +2,12 @@ #include #include #include +#include +#include #include - #include -#include -#include #include #include #include @@ -19,12 +18,16 @@ #include #include #include +#include +#include #include #include #include #include #include +#include +#include "IPC.h" #include "commands.h" #include "keybinds.h" #include "structs.h" @@ -59,6 +62,7 @@ void updateMousePos(); CommandsModule commandsModule; Config cfg(commandsModule); KeybindsModule keybindsModule(commandsModule, cfg, globals, &updateMousePos); +IPCModule ipc(commandsModule, cfg, globals); int sW, sH; int bH; @@ -1090,39 +1094,64 @@ int main(int argc, char** argv) XSetInputFocus(dpy, root, RevertToNone, CurrentTime); XWarpPointer(dpy, root, root, 0, 0, 0, 0, 960, 540); + fd_set fdset; + int x11fd = ConnectionNumber(dpy); + FD_ZERO(&fdset); + FD_SET(x11fd, &fdset); + FD_SET(ipc.getFD(), &fdset); + log("Begin mainloop"); while(keepGoing) { - XEvent e; - XNextEvent(dpy, &e); + FD_ZERO(&fdset); + FD_SET(x11fd, &fdset); + FD_SET(ipc.getFD(), &fdset); + int ready = select(x11fd + 1, &fdset, NULL, NULL, NULL); + if(FD_ISSET(ipc.getFD(), &fdset)) + { + ipc.doListen(); + } + if(FD_ISSET(x11fd, &fdset)) + { + XEvent e; + while(XPending(dpy)) + { + XNextEvent(dpy, &e); - switch(e.type) + switch(e.type) + { + case KeyPress: + keybindsModule.handleKeypress(e.xkey); + break; + case ConfigureRequest: + configureRequest(e.xconfigurerequest); + break; + case MapRequest: + mapRequest(e.xmaprequest); + break; + case DestroyNotify: + destroyNotify(e.xdestroywindow); + break; + case EnterNotify: + enterNotify(e.xcrossing); + break; + case ClientMessage: + clientMessage(e.xclient); + break; + default: + // cout << "Unhandled event: " << getEventName(e.type) << endl; + break; + } + } + } + if(ready == -1) { - case KeyPress: - keybindsModule.handleKeypress(e.xkey); - break; - case ConfigureRequest: - configureRequest(e.xconfigurerequest); - break; - case MapRequest: - mapRequest(e.xmaprequest); - break; - case DestroyNotify: - destroyNotify(e.xdestroywindow); - break; - case EnterNotify: - enterNotify(e.xcrossing); - break; - case ClientMessage: - clientMessage(e.xclient); - break; - default: - // cout << "Unhandled event: " << getEventName(e.type) << endl; - break; + cout << "E" << endl; + cout << "ERROR" << endl; } } //Kill children - + ipc.quitIPC(); XCloseDisplay(dpy); } diff --git a/test b/test index 49cbf49..dcea754 100644 --- a/test +++ b/test @@ -7,6 +7,8 @@ Xephyr -screen 1600x900+2080+90 :1 & sleep 0.1 +rm /tmp/YATwm.sock + DISPLAY=:1 ./YATwm pkill Xephyr -- cgit v1.2.3