1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
#include "IPC.h"
#include "ewmh.h"
#include <X11/Xlib.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";
IPCServerModule::IPCServerModule(CommandsModule& commandsModule, Config& cfg, Globals& globals)
:commandsModule(commandsModule),
cfg(cfg),
globals(globals)
{
}
void IPCServerModule::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 IPCServerModule::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 IPCServerModule::quitIPC()
{
if(!ready)
return;
close(sockfd);
ready = false;
}
int IPCServerModule::getFD()
{
if(!ready)
return -1;
if(sockfd > 0)
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);
}
|