2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
6 * © 2010-2011 Axel Wagner and contributors
8 * See file LICNSE for license information
10 * src/ipc.c: Communicating with i3
19 #include <sys/socket.h>
27 ev_timer *reconn = NULL;
29 const char *sock_path;
31 typedef void(*handler_t)(char*);
37 void retry_connection(struct ev_loop *loop, ev_timer *w, int events) {
38 static int retries = 8;
39 if (init_connection(sock_path) == 0) {
41 ELOG("Retried 8 times - connection failed!\n");
48 ev_timer_stop(loop, w);
51 /* We get the current outputs and workspaces, to
52 * reconfigure all bars with the current configuration */
53 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
54 if (!config.disable_ws) {
55 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
60 * Schedule a reconnect
65 if ((reconn = malloc(sizeof(ev_timer))) == NULL) {
66 ELOG("malloc() failed: %s\n", strerror(errno));
70 ev_timer_stop(main_loop, reconn);
72 ev_timer_init(reconn, retry_connection, 0.25, 0.25);
73 ev_timer_start(main_loop, reconn);
77 * Called, when we get a reply to a command from i3.
78 * Since i3 does not give us much feedback on commands, we do not much
81 void got_command_reply(char *reply) {
82 /* TODO: Error handling for command-replies */
86 * Called, when we get a reply with workspaces-data
89 void got_workspace_reply(char *reply) {
90 DLOG("Got Workspace-Data!\n");
91 parse_workspaces_json(reply);
96 * Called, when we get a reply for a subscription.
97 * Since i3 does not give us much feedback on commands, we do not much
100 void got_subscribe_reply(char *reply) {
101 DLOG("Got Subscribe Reply: %s\n", reply);
102 /* TODO: Error handling for subscribe-commands */
106 * Called, when we get a reply with outputs-data
109 void got_output_reply(char *reply) {
110 DLOG("Parsing Outputs-JSON...\n");
111 parse_outputs_json(reply);
112 DLOG("Reconfiguring Windows...\n");
117 /* Data-structure to easily call the reply-handlers later */
118 handler_t reply_handlers[] = {
120 &got_workspace_reply,
121 &got_subscribe_reply,
126 * Called, when a workspace-event arrives (i.e. the user changed the workspace)
129 void got_workspace_event(char *event) {
130 DLOG("Got Workspace Event!\n");
131 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
135 * Called, when an output-event arrives (i.e. the screen-configuration changed)
138 void got_output_event(char *event) {
139 DLOG("Got Output Event!\n");
140 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
141 if (!config.disable_ws) {
142 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
146 /* Data-structure to easily call the reply-handlers later */
147 handler_t event_handlers[] = {
148 &got_workspace_event,
153 * Called, when we get a message from i3
156 void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
158 int fd = watcher->fd;
160 /* First we only read the header, because we know its length */
161 uint32_t header_len = strlen(I3_IPC_MAGIC) + sizeof(uint32_t)*2;
162 char *header = malloc(header_len);
163 if (header == NULL) {
164 ELOG("Could not allocate memory: %s\n", strerror(errno));
168 /* We first parse the fixed-length IPC-header, to know, how much data
169 * we have to expect */
171 while (rec < header_len) {
172 int n = read(fd, header + rec, header_len - rec);
174 ELOG("read() failed: %s\n", strerror(errno));
178 /* EOF received. We try to recover a few times, because most likely
179 * i3 just restarted */
180 ELOG("EOF received, try to recover...\n");
181 destroy_connection();
188 if (strncmp(header, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC))) {
189 ELOG("Wrong magic code: %.*s\n Expected: %s\n",
190 (int) strlen(I3_IPC_MAGIC),
196 char *walk = header + strlen(I3_IPC_MAGIC);
198 memcpy(&size, (uint32_t*)walk, sizeof(uint32_t));
199 walk += sizeof(uint32_t);
201 memcpy(&type, (uint32_t*)walk, sizeof(uint32_t));
203 /* Now that we know, what to expect, we can start read()ing the rest
205 char *buffer = malloc(size + 1);
206 if (buffer == NULL) {
207 /* EOF received. We try to recover a few times, because most likely
208 * i3 just restarted */
209 ELOG("EOF received, try to recover...\n");
210 destroy_connection();
217 int n = read(fd, buffer + rec, size - rec);
219 ELOG("read() failed: %s\n", strerror(errno));
223 ELOG("Nothing to read!\n");
230 /* And call the callback (indexed by the type) */
231 if (type & (1 << 31)) {
233 event_handlers[type](buffer);
235 reply_handlers[type](buffer);
243 * Sends a Message to i3.
244 * type must be a valid I3_IPC_MESSAGE_TYPE (see i3/ipc.h for further information)
247 int i3_send_msg(uint32_t type, const char *payload) {
249 if (payload != NULL) {
250 len = strlen(payload);
253 /* We are a wellbehaved client and send a proper header first */
254 uint32_t to_write = strlen (I3_IPC_MAGIC) + sizeof(uint32_t)*2 + len;
255 /* TODO: I'm not entirely sure if this buffer really has to contain more
256 * than the pure header (why not just write() the payload from *payload?),
257 * but we leave it for now */
258 char *buffer = malloc(to_write);
259 if (buffer == NULL) {
260 ELOG("Could not allocate memory: %s\n", strerror(errno));
266 strncpy(buffer, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC));
267 walk += strlen(I3_IPC_MAGIC);
268 memcpy(walk, &len, sizeof(uint32_t));
269 walk += sizeof(uint32_t);
270 memcpy(walk, &type, sizeof(uint32_t));
271 walk += sizeof(uint32_t);
274 strncpy(walk, payload, len);
276 uint32_t written = 0;
278 while (to_write > 0) {
279 int n = write(i3_connection->fd, buffer + written, to_write);
281 ELOG("write() failed: %s\n", strerror(errno));
295 * Initiate a connection to i3.
296 * socket-path must be a valid path to the ipc_socket of i3
299 int init_connection(const char *socket_path) {
300 sock_path = socket_path;
301 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
303 ELOG("Could not create Socket: %s\n", strerror(errno));
307 struct sockaddr_un addr;
308 memset(&addr, 0, sizeof(struct sockaddr_un));
309 addr.sun_family = AF_LOCAL;
310 strcpy(addr.sun_path, sock_path);
311 if (connect(sockfd, (const struct sockaddr*) &addr, sizeof(struct sockaddr_un)) < 0) {
312 ELOG("Could not connect to i3! %s: %s\n", sock_path, strerror(errno));
317 i3_connection = malloc(sizeof(ev_io));
318 if (i3_connection == NULL) {
319 ELOG("malloc() failed: %s\n", strerror(errno));
322 ev_io_init(i3_connection, &got_data, sockfd, EV_READ);
323 ev_io_start(main_loop, i3_connection);
328 * Destroy the connection to i3.
330 void destroy_connection() {
331 close(i3_connection->fd);
332 ev_io_stop(main_loop, i3_connection);
336 * Subscribe to all the i3-events, we need
339 void subscribe_events() {
340 if (config.disable_ws) {
341 i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"output\" ]");
343 i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"workspace\", \"output\" ]");