summaryrefslogtreecommitdiff
path: root/src/IPC.cpp
diff options
context:
space:
mode:
authorBossCode45 <human.cyborg42@gmail.com>2024-12-01 19:24:09 +1300
committerBossCode45 <human.cyborg42@gmail.com>2024-12-01 19:24:09 +1300
commit8dca89a1be23f0de2dd1676b95feb6b46cbdd5f2 (patch)
tree7ebc1a5161bbe70d6ab6a0e5353a1a622240c915 /src/IPC.cpp
parente162dff48c251e262f475de9261f0ecfa0f39dc4 (diff)
parent434ec6542d0d79190c6aa7003aac91b03cad4398 (diff)
downloadYATwm-8dca89a1be23f0de2dd1676b95feb6b46cbdd5f2.tar.gz
YATwm-8dca89a1be23f0de2dd1676b95feb6b46cbdd5f2.zip
Merge branch 'IPC'
Diffstat (limited to 'src/IPC.cpp')
-rw-r--r--src/IPC.cpp91
1 files changed, 91 insertions, 0 deletions
diff --git a/src/IPC.cpp b/src/IPC.cpp
new file mode 100644
index 0000000..709cc5c
--- /dev/null
+++ b/src/IPC.cpp
@@ -0,0 +1,91 @@
+#include "IPC.h"
+#include "ewmh.h"
+
+#include <cstring>
+#include <string>
+#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)
+{
+}
+
+void IPCModule::init()
+{
+ 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 " << errno << endl;
+ }
+ cout << "SOCKETED" << endl;
+ setIPCPath((unsigned char*)path, strlen(path));
+ ready = true;
+}
+
+void IPCModule::doListen()
+{
+ if(!ready)
+ return;
+ if(listen(sockfd, 1) != 0)
+ {
+ cout << "ERROR 2" << endl;
+ return;
+ }
+ if(first)
+ {
+ first = false;
+ return;
+ }
+ unsigned int socklen = 0;
+ sockaddr_un remote;
+ int newsock = accept(sockfd, (sockaddr*)&remote, &socklen);
+ 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;
+ }
+ const char* message = "RAN COMMAND";
+ send(newsock, message, strlen(message), 0);
+ shutdown(newsock, SHUT_RDWR);
+ close(newsock);
+}
+
+void IPCModule::quitIPC()
+{
+ if(!ready)
+ return;
+ close(sockfd);
+ ready = false;
+}
+
+int IPCModule::getFD()
+{
+ if(!ready)
+ return -1;
+ if(sockfd > 0)
+ return sockfd;
+ return -1;
+}