2 * i3bar - an xcb-based status- and ws-bar for i3
4 * © 2010 Axel Wagner and contributors
6 * See file LICNSE for license information
8 * src/ipc.c: Communicating with i3
16 #include <sys/socket.h>
24 ev_timer *reconn = NULL;
26 const char *sock_path;
28 typedef void(*handler_t)(char*);
34 void retry_connection(struct ev_loop *loop, ev_timer *w, int events) {
35 static int retries = 8;
36 if (init_connection(sock_path) == 0) {
38 ELOG("Retried 8 times - connection failed!\n");
45 ev_timer_stop(loop, w);
51 * Schedule a reconnect
56 if ((reconn = malloc(sizeof(ev_timer))) == NULL) {
57 ELOG("malloc() failed: %s\n", strerror(errno));
61 ev_timer_stop(main_loop, reconn);
63 ev_timer_init(reconn, retry_connection, 0.25, 0.25);
64 ev_timer_start(main_loop, reconn);
68 * Called, when we get a reply to a command from i3.
69 * Since i3 does not give us much feedback on commands, we do not much
72 void got_command_reply(char *reply) {
73 /* TODO: Error handling for command-replies */
77 * Called, when we get a reply with workspaces-data
80 void got_workspace_reply(char *reply) {
81 DLOG("Got Workspace-Data!\n");
82 parse_workspaces_json(reply);
87 * Called, when we get a reply for a subscription.
88 * Since i3 does not give us much feedback on commands, we do not much
91 void got_subscribe_reply(char *reply) {
92 DLOG("Got Subscribe Reply: %s\n", reply);
93 /* TODO: Error handling for subscribe-commands */
97 * Called, when we get a reply with outputs-data
100 void got_output_reply(char *reply) {
101 DLOG("Parsing Outputs-JSON...\n");
102 parse_outputs_json(reply);
103 DLOG("Reconfiguring Windows...\n");
108 /* Data-structure to easily call the reply-handlers later */
109 handler_t reply_handlers[] = {
111 &got_workspace_reply,
112 &got_subscribe_reply,
117 * Called, when a workspace-event arrives (i.e. the user changed the workspace)
120 void got_workspace_event(char *event) {
121 DLOG("Got Workspace Event!\n");
122 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
126 * Called, when an output-event arrives (i.e. the screen-configuration changed)
129 void got_output_event(char *event) {
130 DLOG("Got Output Event!\n");
131 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
132 if (!config.disable_ws) {
133 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
137 /* Data-structure to easily call the reply-handlers later */
138 handler_t event_handlers[] = {
139 &got_workspace_event,
144 * Called, when we get a message from i3
147 void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
149 int fd = watcher->fd;
151 /* First we only read the header, because we know its length */
152 uint32_t header_len = strlen(I3_IPC_MAGIC) + sizeof(uint32_t)*2;
153 char *header = malloc(header_len);
154 if (header == NULL) {
155 ELOG("Could not allocate memory: %s\n", strerror(errno));
159 /* We first parse the fixed-length IPC-header, to know, how much data
160 * we have to expect */
162 while (rec < header_len) {
163 int n = read(fd, header + rec, header_len - rec);
165 ELOG("read() failed: %s\n", strerror(errno));
169 /* EOF received. We try to recover a few times, because most likely
170 * i3 just restarted */
171 ELOG("EOF received, try to recover...\n");
172 destroy_connection();
179 if (strncmp(header, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC))) {
180 ELOG("Wrong magic code: %.*s\n Expected: %s\n",
181 (int) strlen(I3_IPC_MAGIC),
187 char *walk = header + strlen(I3_IPC_MAGIC);
189 memcpy(&size, (uint32_t*)walk, sizeof(uint32_t));
190 walk += sizeof(uint32_t);
192 memcpy(&type, (uint32_t*)walk, sizeof(uint32_t));
194 /* Now that we know, what to expect, we can start read()ing the rest
196 char *buffer = malloc(size + 1);
197 if (buffer == NULL) {
198 /* EOF received. We try to recover a few times, because most likely
199 * i3 just restarted */
200 ELOG("EOF received, try to recover...\n");
201 destroy_connection();
208 int n = read(fd, buffer + rec, size - rec);
210 ELOG("read() failed: %s\n", strerror(errno));
214 ELOG("Nothing to read!\n");
221 /* And call the callback (indexed by the type) */
222 if (type & (1 << 31)) {
224 event_handlers[type](buffer);
226 reply_handlers[type](buffer);
234 * Sends a Message to i3.
235 * type must be a valid I3_IPC_MESSAGE_TYPE (see i3/ipc.h for further information)
238 int i3_send_msg(uint32_t type, const char *payload) {
240 if (payload != NULL) {
241 len = strlen(payload);
244 /* We are a wellbehaved client and send a proper header first */
245 uint32_t to_write = strlen (I3_IPC_MAGIC) + sizeof(uint32_t)*2 + len;
246 /* TODO: I'm not entirely sure if this buffer really has to contain more
247 * than the pure header (why not just write() the payload from *payload?),
248 * but we leave it for now */
249 char *buffer = malloc(to_write);
250 if (buffer == NULL) {
251 ELOG("Could not allocate memory: %s\n", strerror(errno));
257 strncpy(buffer, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC));
258 walk += strlen(I3_IPC_MAGIC);
259 memcpy(walk, &len, sizeof(uint32_t));
260 walk += sizeof(uint32_t);
261 memcpy(walk, &type, sizeof(uint32_t));
262 walk += sizeof(uint32_t);
265 strncpy(walk, payload, len);
267 uint32_t written = 0;
269 while (to_write > 0) {
270 int n = write(i3_connection->fd, buffer + written, to_write);
272 ELOG("write() failed: %s\n", strerror(errno));
286 * Initiate a connection to i3.
287 * socket-path must be a valid path to the ipc_socket of i3
290 int init_connection(const char *socket_path) {
291 sock_path = socket_path;
292 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
294 ELOG("Could not create Socket: %s\n", strerror(errno));
298 struct sockaddr_un addr;
299 memset(&addr, 0, sizeof(struct sockaddr_un));
300 addr.sun_family = AF_LOCAL;
301 strcpy(addr.sun_path, sock_path);
302 if (connect(sockfd, (const struct sockaddr*) &addr, sizeof(struct sockaddr_un)) < 0) {
303 ELOG("Could not connect to i3! %s: %s\n", sock_path, strerror(errno));
308 i3_connection = malloc(sizeof(ev_io));
309 if (i3_connection == NULL) {
310 ELOG("malloc() failed: %s\n", strerror(errno));
313 ev_io_init(i3_connection, &got_data, sockfd, EV_READ);
314 ev_io_start(main_loop, i3_connection);
319 * Destroy the connection to i3.
321 void destroy_connection() {
322 close(i3_connection->fd);
323 ev_io_stop(main_loop, i3_connection);
327 * Subscribe to all the i3-events, we need
330 void subscribe_events() {
331 if (config.disable_ws) {
332 i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"output\" ]");
334 i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"workspace\", \"output\" ]");