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>
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
55 ev_timer_init(&reconn, retry_connection, 0.25, 0.25);
56 ev_timer_start(main_loop, &reconn);
60 * Called, when we get a reply to a command from i3.
61 * Since i3 does not give us much feedback on commands, we do not much
64 void got_command_reply(char *reply) {
65 /* TODO: Error handling for command-replies */
69 * Called, when we get a reply with workspaces-data
72 void got_workspace_reply(char *reply) {
73 DLOG("Got Workspace-Data!\n");
74 parse_workspaces_json(reply);
79 * Called, when we get a reply for a subscription.
80 * Since i3 does not give us much feedback on commands, we do not much
83 void got_subscribe_reply(char *reply) {
84 DLOG("Got Subscribe Reply: %s\n", reply);
85 /* TODO: Error handling for subscribe-commands */
89 * Called, when we get a reply with outputs-data
92 void got_output_reply(char *reply) {
93 DLOG("Parsing Outputs-JSON...\n");
94 parse_outputs_json(reply);
95 DLOG("Reconfiguring Windows...\n");
100 /* Data-structure to easily call the reply-handlers later */
101 handler_t reply_handlers[] = {
103 &got_workspace_reply,
104 &got_subscribe_reply,
109 * Called, when a workspace-event arrives (i.e. the user changed the workspace)
112 void got_workspace_event(char *event) {
113 DLOG("Got Workspace Event!\n");
114 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
118 * Called, when an output-event arrives (i.e. the screen-configuration changed)
121 void got_output_event(char *event) {
122 DLOG("Got Output Event!\n");
123 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
124 if (!config.disable_ws) {
125 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
129 /* Data-structure to easily call the reply-handlers later */
130 handler_t event_handlers[] = {
131 &got_workspace_event,
136 * Called, when we get a message from i3
139 void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
141 int fd = watcher->fd;
143 /* First we only read the header, because we know its length */
144 uint32_t header_len = strlen(I3_IPC_MAGIC) + sizeof(uint32_t)*2;
145 char *header = malloc(header_len);
146 if (header == NULL) {
147 ELOG("Could not allocate memory: %s\n", strerror(errno));
151 /* We first parse the fixed-length IPC-header, to know, how much data
152 * we have to expect */
154 while (rec < header_len) {
155 int n = read(fd, header + rec, header_len - rec);
157 ELOG("read() failed: %s\n", strerror(errno));
161 /* EOF received. We try to recover a few times, because most likely
162 * i3 just restarted */
163 ELOG("EOF received, try to recover...\n");
164 destroy_connection();
171 if (strncmp(header, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC))) {
172 ELOG("Wrong magic code: %.*s\n Expected: %s\n",
173 (int) strlen(I3_IPC_MAGIC),
179 char *walk = header + strlen(I3_IPC_MAGIC);
180 uint32_t size = *((uint32_t*) walk);
181 walk += sizeof(uint32_t);
182 uint32_t type = *((uint32_t*) walk);
184 /* Now that we know, what to expect, we can start read()ing the rest
186 char *buffer = malloc(size + 1);
187 if (buffer == NULL) {
188 /* EOF received. We try to recover a few times, because most likely
189 * i3 just restarted */
190 ELOG("EOF received, try to recover...\n");
191 destroy_connection();
198 int n = read(fd, buffer + rec, size - rec);
200 ELOG("read() failed: %s\n", strerror(errno));
204 ELOG("Nothing to read!\n");
211 /* And call the callback (indexed by the type) */
212 if (type & (1 << 31)) {
214 event_handlers[type](buffer);
216 reply_handlers[type](buffer);
224 * Sends a Message to i3.
225 * type must be a valid I3_IPC_MESSAGE_TYPE (see i3/ipc.h for further information)
228 int i3_send_msg(uint32_t type, const char *payload) {
230 if (payload != NULL) {
231 len = strlen(payload);
234 /* We are a wellbehaved client and send a proper header first */
235 uint32_t to_write = strlen (I3_IPC_MAGIC) + sizeof(uint32_t)*2 + len;
236 /* TODO: I'm not entirely sure if this buffer really has to contain more
237 * than the pure header (why not just write() the payload from *payload?),
238 * but we leave it for now */
239 char *buffer = malloc(to_write);
240 if (buffer == NULL) {
241 ELOG("Could not allocate memory: %s\n", strerror(errno));
247 strncpy(buffer, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC));
248 walk += strlen(I3_IPC_MAGIC);
249 memcpy(walk, &len, sizeof(uint32_t));
250 walk += sizeof(uint32_t);
251 memcpy(walk, &type, sizeof(uint32_t));
252 walk += sizeof(uint32_t);
255 strncpy(walk, payload, len);
257 uint32_t written = 0;
259 while (to_write > 0) {
260 int n = write(i3_connection.fd, buffer + written, to_write);
262 ELOG("write() failed: %s\n", strerror(errno));
276 * Initiate a connection to i3.
277 * socket-path must be a valid path to the ipc_socket of i3
280 int init_connection(const char *socket_path) {
281 sock_path = socket_path;
282 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
284 ELOG("Could not create Socket: %s\n", strerror(errno));
288 struct sockaddr_un addr;
289 memset(&addr, 0, sizeof(struct sockaddr_un));
290 addr.sun_family = AF_LOCAL;
291 strcpy(addr.sun_path, sock_path);
292 if (connect(sockfd, (const struct sockaddr*) &addr, sizeof(struct sockaddr_un)) < 0) {
293 ELOG("Could not connect to i3! %s: %s\n", sock_path, strerror(errno));
298 ev_io_init(&i3_connection, &got_data, sockfd, EV_READ);
299 ev_io_start(main_loop, &i3_connection);
304 * Destroy the connection to i3.
306 void destroy_connection() {
307 close(i3_connection.fd);
308 ev_io_stop(main_loop, &i3_connection);
312 * Subscribe to all the i3-events, we need
315 void subscribe_events() {
316 if (config.disable_ws) {
317 i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"output\" ]");
319 i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"workspace\", \"output\" ]");