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
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);
48 /* We get the current outputs and workspaces, to
49 * reconfigure all bars with the current configuration */
50 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
51 if (!config.disable_ws) {
52 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
57 * Schedule a reconnect
62 if ((reconn = malloc(sizeof(ev_timer))) == NULL) {
63 ELOG("malloc() failed: %s\n", strerror(errno));
67 ev_timer_stop(main_loop, reconn);
69 ev_timer_init(reconn, retry_connection, 0.25, 0.25);
70 ev_timer_start(main_loop, reconn);
74 * Called, when we get a reply to a command from i3.
75 * Since i3 does not give us much feedback on commands, we do not much
78 void got_command_reply(char *reply) {
79 /* TODO: Error handling for command-replies */
83 * Called, when we get a reply with workspaces-data
86 void got_workspace_reply(char *reply) {
87 DLOG("Got Workspace-Data!\n");
88 parse_workspaces_json(reply);
93 * Called, when we get a reply for a subscription.
94 * Since i3 does not give us much feedback on commands, we do not much
97 void got_subscribe_reply(char *reply) {
98 DLOG("Got Subscribe Reply: %s\n", reply);
99 /* TODO: Error handling for subscribe-commands */
103 * Called, when we get a reply with outputs-data
106 void got_output_reply(char *reply) {
107 DLOG("Parsing Outputs-JSON...\n");
108 parse_outputs_json(reply);
109 DLOG("Reconfiguring Windows...\n");
114 /* Data-structure to easily call the reply-handlers later */
115 handler_t reply_handlers[] = {
117 &got_workspace_reply,
118 &got_subscribe_reply,
123 * Called, when a workspace-event arrives (i.e. the user changed the workspace)
126 void got_workspace_event(char *event) {
127 DLOG("Got Workspace Event!\n");
128 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
132 * Called, when an output-event arrives (i.e. the screen-configuration changed)
135 void got_output_event(char *event) {
136 DLOG("Got Output Event!\n");
137 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
138 if (!config.disable_ws) {
139 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
143 /* Data-structure to easily call the reply-handlers later */
144 handler_t event_handlers[] = {
145 &got_workspace_event,
150 * Called, when we get a message from i3
153 void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
155 int fd = watcher->fd;
157 /* First we only read the header, because we know its length */
158 uint32_t header_len = strlen(I3_IPC_MAGIC) + sizeof(uint32_t)*2;
159 char *header = malloc(header_len);
160 if (header == NULL) {
161 ELOG("Could not allocate memory: %s\n", strerror(errno));
165 /* We first parse the fixed-length IPC-header, to know, how much data
166 * we have to expect */
168 while (rec < header_len) {
169 int n = read(fd, header + rec, header_len - rec);
171 ELOG("read() failed: %s\n", strerror(errno));
175 /* EOF received. We try to recover a few times, because most likely
176 * i3 just restarted */
177 ELOG("EOF received, try to recover...\n");
178 destroy_connection();
185 if (strncmp(header, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC))) {
186 ELOG("Wrong magic code: %.*s\n Expected: %s\n",
187 (int) strlen(I3_IPC_MAGIC),
193 char *walk = header + strlen(I3_IPC_MAGIC);
195 memcpy(&size, (uint32_t*)walk, sizeof(uint32_t));
196 walk += sizeof(uint32_t);
198 memcpy(&type, (uint32_t*)walk, sizeof(uint32_t));
200 /* Now that we know, what to expect, we can start read()ing the rest
202 char *buffer = malloc(size + 1);
203 if (buffer == NULL) {
204 /* EOF received. We try to recover a few times, because most likely
205 * i3 just restarted */
206 ELOG("EOF received, try to recover...\n");
207 destroy_connection();
214 int n = read(fd, buffer + rec, size - rec);
216 ELOG("read() failed: %s\n", strerror(errno));
220 ELOG("Nothing to read!\n");
227 /* And call the callback (indexed by the type) */
228 if (type & (1 << 31)) {
230 event_handlers[type](buffer);
232 reply_handlers[type](buffer);
240 * Sends a Message to i3.
241 * type must be a valid I3_IPC_MESSAGE_TYPE (see i3/ipc.h for further information)
244 int i3_send_msg(uint32_t type, const char *payload) {
246 if (payload != NULL) {
247 len = strlen(payload);
250 /* We are a wellbehaved client and send a proper header first */
251 uint32_t to_write = strlen (I3_IPC_MAGIC) + sizeof(uint32_t)*2 + len;
252 /* TODO: I'm not entirely sure if this buffer really has to contain more
253 * than the pure header (why not just write() the payload from *payload?),
254 * but we leave it for now */
255 char *buffer = malloc(to_write);
256 if (buffer == NULL) {
257 ELOG("Could not allocate memory: %s\n", strerror(errno));
263 strncpy(buffer, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC));
264 walk += strlen(I3_IPC_MAGIC);
265 memcpy(walk, &len, sizeof(uint32_t));
266 walk += sizeof(uint32_t);
267 memcpy(walk, &type, sizeof(uint32_t));
268 walk += sizeof(uint32_t);
271 strncpy(walk, payload, len);
273 uint32_t written = 0;
275 while (to_write > 0) {
276 int n = write(i3_connection->fd, buffer + written, to_write);
278 ELOG("write() failed: %s\n", strerror(errno));
292 * Initiate a connection to i3.
293 * socket-path must be a valid path to the ipc_socket of i3
296 int init_connection(const char *socket_path) {
297 sock_path = socket_path;
298 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
300 ELOG("Could not create Socket: %s\n", strerror(errno));
304 struct sockaddr_un addr;
305 memset(&addr, 0, sizeof(struct sockaddr_un));
306 addr.sun_family = AF_LOCAL;
307 strcpy(addr.sun_path, sock_path);
308 if (connect(sockfd, (const struct sockaddr*) &addr, sizeof(struct sockaddr_un)) < 0) {
309 ELOG("Could not connect to i3! %s: %s\n", sock_path, strerror(errno));
314 i3_connection = malloc(sizeof(ev_io));
315 if (i3_connection == NULL) {
316 ELOG("malloc() failed: %s\n", strerror(errno));
319 ev_io_init(i3_connection, &got_data, sockfd, EV_READ);
320 ev_io_start(main_loop, i3_connection);
325 * Destroy the connection to i3.
327 void destroy_connection() {
328 close(i3_connection->fd);
329 ev_io_stop(main_loop, i3_connection);
333 * Subscribe to all the i3-events, we need
336 void subscribe_events() {
337 if (config.disable_ws) {
338 i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"output\" ]");
340 i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"workspace\", \"output\" ]");