5 #include <sys/socket.h>
14 typedef void(*handler_t)(char*);
16 int get_ipc_fd(const char *socket_path) {
17 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
19 printf("ERROR: Could not create Socket!\n");
23 struct sockaddr_un addr;
24 memset(&addr, 0, sizeof(struct sockaddr_un));
25 addr.sun_family = AF_LOCAL;
26 strcpy(addr.sun_path, socket_path);
27 if (connect(sockfd, (const struct sockaddr*) &addr, sizeof(struct sockaddr_un)) < 0) {
28 printf("ERROR: Could not connct to i3\n");
34 void got_command_reply(char *reply) {
35 /* FIXME: Error handling for command-replies */
38 void got_workspace_reply(char *reply) {
39 printf("Got Workspace-Data!\n");
40 parse_workspaces_json(reply);
44 void got_subscribe_reply(char *reply) {
45 printf("Got Subscribe Reply: %s\n", reply);
46 /* FIXME: Error handling for subscribe-commands */
49 void got_output_reply(char *reply) {
50 printf("Got Outputs-Data!\nDestroying Windows...\n");
52 printf("Parsing JSON...\n");
53 parse_outputs_json(reply);
54 printf("Creating_Windows...\n");
58 handler_t reply_handlers[] = {
65 void got_workspace_event(char *event) {
66 printf("Got Workspace Event!\n");
67 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
70 void got_output_event(char *event) {
71 printf("Got Output Event!\n");
72 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
75 handler_t event_handlers[] = {
80 void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
81 printf("Got data!\n");
83 uint32_t header_len = strlen(I3_IPC_MAGIC) + sizeof(uint32_t)*2;
84 char *header = malloc(header_len);
86 printf("ERROR: Could not allocate memory!\n");
91 while (rec < header_len) {
92 int n = read(fd, header + rec, header_len - rec);
94 printf("ERROR: read() failed!\n");
98 printf("ERROR: Nothing to read!\n");
104 if (strncmp(header, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC))) {
105 printf("ERROR: Wrong magic code: %.*s\n Expected: %s\n",
106 (int) strlen(I3_IPC_MAGIC),
112 char *walk = header + strlen(I3_IPC_MAGIC);
113 uint32_t size = *((uint32_t*) walk);
114 walk += sizeof(uint32_t);
115 uint32_t type = *((uint32_t*) walk);
116 char *buffer = malloc(size + 1);
117 if (buffer == NULL) {
118 printf("ERROR: Could not allocate memory!\n");
124 int n = read(fd, buffer + rec, size - rec);
126 printf("ERROR: read() failed!\n");
130 printf("ERROR: Nothing to read!\n");
137 if (type & (1 << 31)) {
139 event_handlers[type](buffer);
141 reply_handlers[type](buffer);
148 int i3_send_msg(uint32_t type, const char *payload) {
150 if (payload != NULL) {
151 len = strlen(payload);
154 uint32_t to_write = strlen (I3_IPC_MAGIC) + sizeof(uint32_t)*2 + len;
155 char *buffer = malloc(to_write);
156 if (buffer == NULL) {
157 printf("ERROR: Could not allocate memory\n");
163 strncpy(buffer, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC));
164 walk += strlen(I3_IPC_MAGIC);
165 memcpy(walk, &len, sizeof(uint32_t));
166 walk += sizeof(uint32_t);
167 memcpy(walk, &type, sizeof(uint32_t));
168 walk += sizeof(uint32_t);
170 strncpy(walk, payload, len);
172 uint32_t written = 0;
174 while (to_write > 0) {
175 int n = write(i3_connection->fd, buffer + written, to_write);
177 printf("ERROR: write() failed!\n");
190 int init_connection(const char *socket_path) {
191 int sockfd = get_ipc_fd(socket_path);
193 i3_connection = malloc(sizeof(ev_io));
194 ev_io_init(i3_connection, &got_data, sockfd, EV_READ);
195 ev_io_start(main_loop, i3_connection);
200 void subscribe_events() {
201 i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"workspace\", \"output\" ]");