2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
5 * © 2010-2012 Axel Wagner and contributors (see also: LICENSE)
7 * ipc.c: Communicating with i3
16 #include <sys/socket.h>
25 const char *sock_path;
27 typedef void(*handler_t)(char*);
30 * Called, when we get a reply to a command from i3.
31 * Since i3 does not give us much feedback on commands, we do not much
34 void got_command_reply(char *reply) {
35 /* TODO: Error handling for command-replies */
39 * Called, when we get a reply with workspaces-data
42 void got_workspace_reply(char *reply) {
43 DLOG("Got Workspace-Data!\n");
44 parse_workspaces_json(reply);
49 * Called, when we get a reply for a subscription.
50 * Since i3 does not give us much feedback on commands, we do not much
53 void got_subscribe_reply(char *reply) {
54 DLOG("Got Subscribe Reply: %s\n", reply);
55 /* TODO: Error handling for subscribe-commands */
59 * Called, when we get a reply with outputs-data
62 void got_output_reply(char *reply) {
63 DLOG("Parsing Outputs-JSON...\n");
64 parse_outputs_json(reply);
65 DLOG("Reconfiguring Windows...\n");
70 SLIST_FOREACH(o_walk, outputs, slist) {
71 kick_tray_clients(o_walk);
78 * Called when we get the configuration for our bar instance
81 void got_bar_config(char *reply) {
82 DLOG("Received bar config \"%s\"\n", reply);
83 /* We initiate the main-function by requesting infos about the outputs and
84 * workspaces. Everything else (creating the bars, showing the right workspace-
85 * buttons and more) is taken care of by the event-drivenness of the code */
86 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
87 parse_config_json(reply);
89 /* Now we can actually use 'config', so let's subscribe to the appropriate
90 * events and request the workspaces if necessary. */
92 if (!config.disable_ws)
93 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
95 /* Initialize the rest of XCB */
96 init_xcb_late(config.fontname);
98 /* Resolve color strings to colorpixels and save them, then free the strings. */
99 init_colors(&(config.colors));
100 free_colors(&(config.colors));
102 /* The name of this function is actually misleading. Even if no command is
103 * specified, this function initiates the watchers to listen on stdin and
104 * react accordingly */
105 start_child(config.command);
106 FREE(config.command);
109 /* Data-structure to easily call the reply-handlers later */
110 handler_t reply_handlers[] = {
112 &got_workspace_reply,
113 &got_subscribe_reply,
121 * Called, when a workspace-event arrives (i.e. the user changed the workspace)
124 void got_workspace_event(char *event) {
125 DLOG("Got Workspace Event!\n");
126 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
130 * Called, when an output-event arrives (i.e. the screen-configuration changed)
133 void got_output_event(char *event) {
134 DLOG("Got Output Event!\n");
135 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
136 if (!config.disable_ws) {
137 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
141 /* Data-structure to easily call the reply-handlers later */
142 handler_t event_handlers[] = {
143 &got_workspace_event,
148 * Called, when we get a message from i3
151 void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
153 int fd = watcher->fd;
155 /* First we only read the header, because we know its length */
156 uint32_t header_len = strlen(I3_IPC_MAGIC) + sizeof(uint32_t)*2;
157 char *header = smalloc(header_len);
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. Since i3 will restart i3bar instances as appropriate,
171 DLOG("EOF received, exiting...\n");
178 if (strncmp(header, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC))) {
179 ELOG("Wrong magic code: %.*s\n Expected: %s\n",
180 (int) strlen(I3_IPC_MAGIC),
186 char *walk = header + strlen(I3_IPC_MAGIC);
188 memcpy(&size, (uint32_t*)walk, sizeof(uint32_t));
189 walk += sizeof(uint32_t);
191 memcpy(&type, (uint32_t*)walk, sizeof(uint32_t));
193 /* Now that we know, what to expect, we can start read()ing the rest
195 char *buffer = smalloc(size + 1);
199 int n = read(fd, buffer + rec, size - rec);
201 ELOG("read() failed: %s\n", strerror(errno));
205 ELOG("Nothing to read!\n");
212 /* And call the callback (indexed by the type) */
213 if (type & (1 << 31)) {
215 event_handlers[type](buffer);
217 if (reply_handlers[type])
218 reply_handlers[type](buffer);
226 * Sends a Message to i3.
227 * type must be a valid I3_IPC_MESSAGE_TYPE (see i3/ipc.h for further information)
230 int i3_send_msg(uint32_t type, const char *payload) {
232 if (payload != NULL) {
233 len = strlen(payload);
236 /* We are a wellbehaved client and send a proper header first */
237 uint32_t to_write = strlen (I3_IPC_MAGIC) + sizeof(uint32_t)*2 + len;
238 /* TODO: I'm not entirely sure if this buffer really has to contain more
239 * than the pure header (why not just write() the payload from *payload?),
240 * but we leave it for now */
241 char *buffer = smalloc(to_write);
244 strncpy(buffer, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC));
245 walk += strlen(I3_IPC_MAGIC);
246 memcpy(walk, &len, sizeof(uint32_t));
247 walk += sizeof(uint32_t);
248 memcpy(walk, &type, sizeof(uint32_t));
249 walk += sizeof(uint32_t);
252 strncpy(walk, payload, len);
254 uint32_t written = 0;
256 while (to_write > 0) {
257 int n = write(i3_connection->fd, buffer + written, to_write);
259 ELOG("write() failed: %s\n", strerror(errno));
273 * Initiate a connection to i3.
274 * socket-path must be a valid path to the ipc_socket of i3
277 int init_connection(const char *socket_path) {
278 sock_path = socket_path;
279 int sockfd = ipc_connect(socket_path);
280 i3_connection = smalloc(sizeof(ev_io));
281 ev_io_init(i3_connection, &got_data, sockfd, EV_READ);
282 ev_io_start(main_loop, i3_connection);
287 * Destroy the connection to i3.
289 void destroy_connection(void) {
290 close(i3_connection->fd);
291 ev_io_stop(main_loop, i3_connection);
295 * Subscribe to all the i3-events, we need
298 void subscribe_events(void) {
299 if (config.disable_ws) {
300 i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"output\" ]");
302 i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"workspace\", \"output\" ]");