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
15 #include <sys/socket.h>
24 typedef void(*handler_t)(char*);
27 * Get a connect to the IPC-interface of i3 and return a filedescriptor
30 int get_ipc_fd(const char *socket_path) {
31 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
33 printf("ERROR: Could not create Socket!\n");
37 struct sockaddr_un addr;
38 memset(&addr, 0, sizeof(struct sockaddr_un));
39 addr.sun_family = AF_LOCAL;
40 strcpy(addr.sun_path, socket_path);
41 if (connect(sockfd, (const struct sockaddr*) &addr, sizeof(struct sockaddr_un)) < 0) {
42 printf("ERROR: Could not connct to i3\n");
49 * Called, when we get a reply to a command from i3.
50 * Since i3 does not give us much feedback on commands, we do not much
53 void got_command_reply(char *reply) {
54 /* TODO: Error handling for command-replies */
58 * Called, when we get a reply with workspaces-data
61 void got_workspace_reply(char *reply) {
62 printf("Got Workspace-Data!\n");
63 parse_workspaces_json(reply);
68 * Called, when we get a reply for a subscription.
69 * Since i3 does not give us much feedback on commands, we do not much
72 void got_subscribe_reply(char *reply) {
73 printf("Got Subscribe Reply: %s\n", reply);
74 /* TODO: Error handling for subscribe-commands */
78 * Called, when we get a reply with outputs-data
81 void got_output_reply(char *reply) {
82 printf("Parsing Outputs-JSON...\n");
83 parse_outputs_json(reply);
84 printf("Reconfiguring Windows...\n");
88 /* Data-structure to easily call the reply-handlers later */
89 handler_t reply_handlers[] = {
97 * Called, when a workspace-event arrives (i.e. the user changed the workspace)
100 void got_workspace_event(char *event) {
101 printf("Got Workspace Event!\n");
102 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
106 * Called, when an output-event arrives (i.e. the screen-configuration changed)
109 void got_output_event(char *event) {
110 printf("Got Output Event!\n");
111 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
112 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
115 /* Data-structure to easily call the reply-handlers later */
116 handler_t event_handlers[] = {
117 &got_workspace_event,
122 * Called, when we get a message from i3
125 void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
126 printf("Got data!\n");
127 int fd = watcher->fd;
129 /* First we only read the header, because we know it's length */
130 uint32_t header_len = strlen(I3_IPC_MAGIC) + sizeof(uint32_t)*2;
131 char *header = malloc(header_len);
132 if (header == NULL) {
133 printf("ERROR: Could not allocate memory!\n");
137 /* We first parse the fixed-length IPC-header, to know, how much data
138 * we have to expect */
140 while (rec < header_len) {
141 int n = read(fd, header + rec, header_len - rec);
143 printf("ERROR: read() failed!\n");
147 printf("ERROR: Nothing to read!\n");
153 if (strncmp(header, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC))) {
154 printf("ERROR: Wrong magic code: %.*s\n Expected: %s\n",
155 (int) strlen(I3_IPC_MAGIC),
161 char *walk = header + strlen(I3_IPC_MAGIC);
162 uint32_t size = *((uint32_t*) walk);
163 walk += sizeof(uint32_t);
164 uint32_t type = *((uint32_t*) walk);
166 /* Now that we know, what to expect, we can start read()ing the rest
168 char *buffer = malloc(size + 1);
169 if (buffer == NULL) {
170 printf("ERROR: Could not allocate memory!\n");
176 int n = read(fd, buffer + rec, size - rec);
178 printf("ERROR: read() failed!\n");
182 printf("ERROR: Nothing to read!\n");
189 /* And call the callback (indexed by the type) */
190 if (type & (1 << 31)) {
192 event_handlers[type](buffer);
194 reply_handlers[type](buffer);
202 * Sends a Message to i3.
203 * type must be a valid I3_IPC_MESSAGE_TYPE (see i3/ipc.h for further information)
206 int i3_send_msg(uint32_t type, const char *payload) {
208 if (payload != NULL) {
209 len = strlen(payload);
212 /* We are a wellbehaved client and send a proper header first */
213 uint32_t to_write = strlen (I3_IPC_MAGIC) + sizeof(uint32_t)*2 + len;
214 /* TODO: I'm not entirely sure if this buffer really has to contain more
215 * than the pure header (why not just write() the payload from *payload?),
216 * but we leave it for now */
217 char *buffer = malloc(to_write);
218 if (buffer == NULL) {
219 printf("ERROR: Could not allocate memory\n");
225 strncpy(buffer, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC));
226 walk += strlen(I3_IPC_MAGIC);
227 memcpy(walk, &len, sizeof(uint32_t));
228 walk += sizeof(uint32_t);
229 memcpy(walk, &type, sizeof(uint32_t));
230 walk += sizeof(uint32_t);
232 strncpy(walk, payload, len);
234 uint32_t written = 0;
236 while (to_write > 0) {
237 int n = write(i3_connection->fd, buffer + written, to_write);
239 printf("ERROR: write() failed!\n");
253 * Initiate a connection to i3.
254 * socket-path must be a valid path to the ipc_socket of i3
257 int init_connection(const char *socket_path) {
258 int sockfd = get_ipc_fd(socket_path);
260 i3_connection = malloc(sizeof(ev_io));
261 ev_io_init(i3_connection, &got_data, sockfd, EV_READ);
262 ev_io_start(main_loop, i3_connection);
268 * Subscribe to all the i3-events, we need
271 void subscribe_events() {
272 i3_send_msg(I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[ \"workspace\", \"output\" ]");