summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBossCode45 <human.cyborg42@gmail.com>2023-09-29 00:55:29 +1300
committerBossCode45 <human.cyborg42@gmail.com>2023-11-12 15:03:19 +1300
commit5dacf6d6d24eab71994e02b6b3c4fc6f6e309958 (patch)
tree204b77d98627d390eb8d6c1a50b767605221d6ec
parent6b5c246a431dcaff119833724137f0716fa2a002 (diff)
downloadYATwm-5dacf6d6d24eab71994e02b6b3c4fc6f6e309958.tar.gz
YATwm-5dacf6d6d24eab71994e02b6b3c4fc6f6e309958.zip
feat: IPC works kinda
Still quite buggy, but first version
-rw-r--r--IPC.cpp80
-rw-r--r--IPC.h25
-rw-r--r--main.cpp85
-rw-r--r--test2
4 files changed, 164 insertions, 28 deletions
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 <cstring>
+#include <sys/socket.h>
+#include <iostream>
+#include <unistd.h>
+
+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 <sys/socket.h>
+#include <sys/un.h>
+
+#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 <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>
+#include <X11/Xutil.h>
+#include <X11/extensions/Xrandr.h>
#include <libnotify/notification.h>
-
#include <libnotify/notify.h>
-#include <X11/Xutil.h>
-#include <X11/extensions/Xrandr.h>
#include <chrono>
#include <cstdio>
#include <cstdlib>
@@ -19,12 +18,16 @@
#include <map>
#include <ostream>
#include <string>
+#include <sys/poll.h>
+#include <sys/select.h>
#include <vector>
#include <unistd.h>
#include <cstring>
#include <algorithm>
#include <fcntl.h>
+#include <poll.h>
+#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