2 * i3bar - an xcb-based status- and ws-bar for i3
4 * © 2010-2011 Axel Wagner and contributors
6 * See file LICNSE for license information
8 * src/ipc.c: Communicating with i3
17 #include <sys/socket.h>
25 ev_timer *reconn = NULL;
27 const char *sock_path;
29 typedef void(*handler_t)(char*);
35 void retry_connection(struct ev_loop *loop, ev_timer *w, int events) {
36 static int retries = 8;
37 if (init_connection(sock_path) == 0) {
39 ELOG("Retried 8 times - connection failed!\n");
46 ev_timer_stop(loop, w);
49 /* We get the current outputs and workspaces, to
50 * reconfigure all bars with the current configuration */
51 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
52 if (!config.disable_ws) {
53 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
58 * Schedule a reconnect
63 if ((reconn = malloc(sizeof(ev_timer))) == NULL) {
64 ELOG("malloc() failed: %s\n", strerror(errno));
68 ev_timer_stop(main_loop, reconn);
70 ev_timer_init(reconn, retry_connection, 0.25, 0.25);
71 ev_timer_start(main_loop, reconn);
75 * Called, when we get a reply to a command from i3.
76 * Since i3 does not give us much feedback on commands, we do not much
79 void got_command_reply(char *reply) {
80 /* TODO: Error handling for command-replies */
84 * Called, when we get a reply with workspaces-data
87 void got_workspace_reply(char *reply) {
88 DLOG("Got Workspace-Data!\n");
89 parse_workspaces_json(reply);
94 * Called, when we get a reply for a subscription.
95 * Since i3 does not give us much feedback on commands, we do not much
98 void got_subscribe_reply(char *reply) {
99 DLOG("Got Subscribe Reply: %s\n", reply);
100 /* TODO: Error handling for subscribe-commands */
104 * Called, when we get a reply with outputs-data
107 void got_output_reply(char *reply) {
108 DLOG("Parsing Outputs-JSON...\n");
109 parse_outputs_json(reply);
110 DLOG("Reconfiguring Windows...\n");
115 /* Data-structure to easily call the reply-handlers later */
116 handler_t reply_handlers[] = {
118 &got_workspace_reply,
119 &got_subscribe_reply,
124 * Called, when a workspace-event arrives (i.e. the user changed the workspace)
127 void got_workspace_event(char *event) {
128 DLOG("Got Workspace Event!\n");
129 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
133 * Called, when an output-event arrives (i.e. the screen-configuration changed)
136 void got_output_event(char *event) {
137 DLOG("Got Output Event!\n");
138 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
139 if (!config.disable_ws) {
140 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
144 /* Data-structure to easily call the reply-handlers later */
145 handler_t event_handlers[] = {
146 &got_workspace_event,
151 * Called, when we get a message from i3
154 void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
156 int fd = watcher->fd;
158 /* First we only read the header, because we know its length */
159 uint32_t header_len = strlen(I3_IPC_MAGIC) + sizeof(uint32_t)*2;
160 char *header = malloc(header_len);
161 if (header == NULL) {
162 ELOG("Could not allocate memory: %s\n", strerror(errno));
166 /* We first parse the fixed-length IPC-header, to know, how much data
167 * we have to expect */
169 while (rec < header_len) {
170 int n = read(fd, header + rec, header_len - rec);
172 ELOG("read() failed: %s\n", strerror(errno));
176 /* EOF received. We try to recover a few times, because most likely
177 * i3 just restarted */
178 ELOG("EOF received, try to recover...\n");
179 destroy_connection();
186 if (strncmp(header, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC))) {
187 ELOG("Wrong magic code: %.*s\n Expected: %s\n",
188 (int) strlen(I3_IPC_MAGIC),
194 char *walk = header + strlen(I3_IPC_MAGIC);
196 memcpy(&size, (uint32_t*)walk, sizeof(uint32_t));
197 walk += sizeof(uint32_t);
199 memcpy(&type, (uint32_t*)walk, sizeof(uint32_t));
201 /* Now that we know, what to expect, we can start read()ing the rest
203 char *buffer = malloc(size + 1);
204 if (buffer == NULL) {
205 /* EOF received. We try to recover a few times, because most likely
206 * i3 just restarted */
207 ELOG("EOF received, try to recover...\n");
208 destroy_connection();
215 int n = read(fd, buffer + rec, size - rec);
217 ELOG("read() failed: %s\n", strerror(errno));
221 ELOG("Nothing to read!\n");
228 /* And call the callback (indexed by the type) */
229 if (type & (1 << 31)) {
231 event_handlers[type](buffer);
233 reply_handlers[type](buffer);
241 * Sends a Message to i3.
242 * type must be a valid I3_IPC_MESSAGE_TYPE (see i3/ipc.h for further information)
245 int i3_send_msg(uint32_t type, const char *payload) {
247 if (payload != NULL) {
248 len = strlen(payload);
251 /* We are a wellbehaved client and send a proper header first */
252 uint32_t to_write = strlen (I3_IPC_MAGIC) + sizeof(uint32_t)*2 + len;
253 /* TODO: I'm not entirely sure if this buffer really has to contain more
254 * than the pure header (why not just write() the payload from *payload?),
255 * but we leave it for now */
256 char *buffer = malloc(to_write);
257 if (buffer == NULL) {
258 ELOG("Could not allocate memory: %s\n", strerror(errno));
264 strncpy(buffer, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC));
265 walk += strlen(I3_IPC_MAGIC);
266 memcpy(walk, &len, sizeof(uint32_t));
267 walk += sizeof(uint32_t);
268 memcpy(walk, &type, sizeof(uint32_t));
269 walk += sizeof(uint32_t);
272 strncpy(walk, payload, len);
274 uint32_t written = 0;
276 while (to_write > 0) {
277 int n = write(i3_connection->fd, buffer + written, to_write);
279 ELOG("write() failed: %s\n", strerror(errno));
293 * Initiate a connection to i3.
294 * socket-path must be a valid path to the ipc_socket of i3
297 int init_connection(const char *socket_path) {
298 sock_path = socket_path;
299 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
301 ELOG("Could not create Socket: %s\n", strerror(errno));
305 struct sockaddr_un addr;
306 memset(&addr, 0, sizeof(struct sockaddr_un));
307 addr.sun_family = AF_LOCAL;
308 strcpy(addr.sun_path, sock_path);
309 if (connect(sockfd, (const struct sockaddr*) &addr, sizeof(struct sockaddr_un)) < 0) {
310 ELOG("Could not connect to i3! %s: %s\n", sock_path, strerror(errno));
315 i3_connection = malloc(sizeof(ev_io));
316 if (i3_connection == NULL) {
317 ELOG("malloc() failed: %s\n", strerror(errno));
320 ev_io_init(i3_connection, &got_data, sockfd, EV_READ);
321 ev_io_start(main_loop, i3_connection);
326 * Destroy the connection to i3.
328 void destroy_connection() {
329 close(i3_connection->fd);
330 ev_io_stop(main_loop, i3_connection);
334 * Subscribe to all the i3-events, we need
337 void subscribe_events() {
338 if (config.disable_ws) {
339 i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"output\" ]");
341 i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"workspace\", \"output\" ]");