]> git.sur5r.net Git - i3/i3/blob - i3bar/src/ipc.c
Be more strict with encapsulation
[i3/i3] / i3bar / src / ipc.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <stdint.h>
5 #include <sys/socket.h>
6 #include <sys/un.h>
7 #include <i3/ipc.h>
8 #include <ev.h>
9
10 #include "common.h"
11
12 ev_io *i3_connection;
13
14 typedef void(*handler_t)(char*);
15
16 int get_ipc_fd(const char *socket_path) {
17     int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
18     if (sockfd == -1) {
19         printf("ERROR: Could not create Socket!\n");
20         exit(EXIT_FAILURE);
21     }
22
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");
29         exit(EXIT_FAILURE);
30     }
31     return sockfd;
32 }
33
34 void got_command_reply(char *reply) {
35     /* FIXME: Error handling for command-replies */
36 }
37
38 void got_workspace_reply(char *reply) {
39     printf("Got Workspace-Data!\n");
40     parse_workspaces_json(reply);
41     draw_bars();
42 }
43
44 void got_subscribe_reply(char *reply) {
45     printf("Got Subscribe Reply: %s\n", reply);
46     /* FIXME: Error handling for subscribe-commands */
47 }
48
49 void got_output_reply(char *reply) {
50     printf("Got Outputs-Data!\nDestroying Windows...\n");
51     destroy_windows();
52     printf("Parsing JSON...\n");
53     parse_outputs_json(reply);
54     printf("Creating_Windows...\n");
55     create_windows();
56 }
57
58 handler_t reply_handlers[] = {
59     &got_command_reply,
60     &got_workspace_reply,
61     &got_subscribe_reply,
62     &got_output_reply,
63 };
64
65 void got_workspace_event(char *event) {
66     printf("Got Workspace Event!\n");
67     i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
68 }
69
70 void got_output_event(char *event) {
71     printf("Got Output Event!\n");
72     i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
73 }
74
75 handler_t event_handlers[] = {
76     &got_workspace_event,
77     &got_output_event
78 };
79
80 void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
81     printf("Got data!\n");
82     int fd = watcher->fd;
83     uint32_t header_len = strlen(I3_IPC_MAGIC) + sizeof(uint32_t)*2;
84     char *header = malloc(header_len);
85     if (header == NULL) {
86         printf("ERROR: Could not allocate memory!\n");
87         exit(EXIT_FAILURE);
88     }
89
90     uint32_t rec = 0;
91     while (rec < header_len) {
92         int n = read(fd, header + rec, header_len - rec);
93         if (n == -1) {
94             printf("ERROR: read() failed!\n");
95             exit(EXIT_FAILURE);
96         }
97         if (n == 0) {
98             printf("ERROR: Nothing to read!\n");
99             exit(EXIT_FAILURE);
100         }
101         rec += n;
102     }
103
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),
107                header,
108                I3_IPC_MAGIC);
109         exit(EXIT_FAILURE);
110     }
111
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");
119         exit(EXIT_FAILURE);
120     }
121     rec = 0;
122
123     while (rec < size) {
124         int n = read(fd, buffer + rec, size - rec);
125         if (n == -1) {
126             printf("ERROR: read() failed!\n");
127             exit(EXIT_FAILURE);
128         }
129         if (n == 0) {
130             printf("ERROR: Nothing to read!\n");
131             exit(EXIT_FAILURE);
132         }
133         rec += n;
134     }
135     buffer[size] = '\0';
136
137     if (type & (1 << 31)) {
138         type ^= 1 << 31;
139         event_handlers[type](buffer);
140     } else {
141         reply_handlers[type](buffer);
142     }
143
144     FREE(header);
145     FREE(buffer);
146 }
147
148 int i3_send_msg(uint32_t type, const char *payload) {
149     uint32_t len = 0;
150     if (payload != NULL) {
151         len = strlen(payload);
152     }
153
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");
158         exit(EXIT_FAILURE);
159     }
160
161     char *walk = buffer;
162
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);
169
170     strncpy(walk, payload, len);
171
172     uint32_t written = 0;
173
174     while (to_write > 0) {
175         int n = write(i3_connection->fd, buffer + written, to_write);
176         if (n == -1) {
177             printf("ERROR: write() failed!\n");
178             exit(EXIT_FAILURE);
179         }
180
181         to_write -= n;
182         written += n;
183     }
184
185     FREE(buffer);
186
187     return 1;
188 }
189
190 int init_connection(const char *socket_path) {
191     int sockfd = get_ipc_fd(socket_path);
192
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);
196
197     return 1;
198 }
199
200 void subscribe_events() {
201     i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"workspace\", \"output\" ]");
202 }