summaryrefslogtreecommitdiff
path: root/YATmsg/YATmsg.cpp
blob: dbc09b9606b509ddd9fd58ef6df1caca001a4014 (plain) (blame)
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
#include <X11/X.h>
#include <X11/Xlib.h>

#include <iostream>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

using std::cout, std::endl;

int main()
{
	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)
	{
		cout << "Failed to get path" << endl;
		XFree(sockPath);
		return 1;
	}

	int 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;
	}
	const char* command = "echo Hello from YATmsg!!!";
	if(write(sockfd, command, 22) == -1)
	{
		cout << "Failed write" << endl;
		XFree(sockPath);
		return 1;
	}

	char recv[128];
	read(sockfd, recv, 128);
	cout << recv << endl;
	
	close(sockfd);
	XFree(sockPath);
	return 0;
}