2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
6 * © 2009-2010 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * i3-msg/main.c: Utility which sends messages to a running i3-instance using
11 * IPC via UNIX domain sockets.
13 * This serves as an example for how to send your own messages to i3.
14 * Additionally, it’s even useful sometimes :-).
20 #include <sys/types.h>
21 #include <sys/socket.h>
33 #include <xcb/xcb_aux.h>
37 static char *socket_path;
40 * Try to get the socket path from X11 and return NULL if it doesn’t work.
41 * As i3-msg is a short-running tool, we don’t bother with cleaning up the
42 * connection and leave it up to the operating system on exit.
45 static char *socket_path_from_x11() {
46 xcb_connection_t *conn;
48 if ((conn = xcb_connect(NULL, &screen)) == NULL ||
49 xcb_connection_has_error(conn))
51 xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screen);
52 xcb_window_t root = root_screen->root;
54 xcb_intern_atom_cookie_t atom_cookie;
55 xcb_intern_atom_reply_t *atom_reply;
57 atom_cookie = xcb_intern_atom(conn, 0, strlen("I3_SOCKET_PATH"), "I3_SOCKET_PATH");
58 atom_reply = xcb_intern_atom_reply(conn, atom_cookie, NULL);
59 if (atom_reply == NULL)
62 xcb_get_property_cookie_t prop_cookie;
63 xcb_get_property_reply_t *prop_reply;
64 prop_cookie = xcb_get_property_unchecked(conn, false, root, atom_reply->atom,
65 XCB_GET_PROPERTY_TYPE_ANY, 0, PATH_MAX);
66 prop_reply = xcb_get_property_reply(conn, prop_cookie, NULL);
67 if (prop_reply == NULL || xcb_get_property_value_length(prop_reply) == 0)
69 if (asprintf(&socket_path, "%.*s", xcb_get_property_value_length(prop_reply),
70 (char*)xcb_get_property_value(prop_reply)) == -1)
76 * Formats a message (payload) of the given size and type and sends it to i3 via
77 * the given socket file descriptor.
80 static void ipc_send_message(int sockfd, uint32_t message_size,
81 uint32_t message_type, uint8_t *payload) {
82 int buffer_size = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t) + message_size;
83 char msg[buffer_size];
86 strcpy(walk, I3_IPC_MAGIC);
87 walk += strlen(I3_IPC_MAGIC);
88 memcpy(walk, &message_size, sizeof(uint32_t));
89 walk += sizeof(uint32_t);
90 memcpy(walk, &message_type, sizeof(uint32_t));
91 walk += sizeof(uint32_t);
92 memcpy(walk, payload, message_size);
95 int bytes_to_go = buffer_size;
96 while (sent_bytes < bytes_to_go) {
97 int n = write(sockfd, msg + sent_bytes, bytes_to_go);
99 err(EXIT_FAILURE, "write() failed");
106 static void ipc_recv_message(int sockfd, uint32_t message_type,
107 uint32_t *reply_length, uint8_t **reply) {
108 /* Read the message header first */
109 uint32_t to_read = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t);
113 uint32_t read_bytes = 0;
114 while (read_bytes < to_read) {
115 int n = read(sockfd, msg + read_bytes, to_read);
117 err(EXIT_FAILURE, "read() failed");
119 errx(EXIT_FAILURE, "received EOF instead of reply");
125 if (memcmp(walk, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0)
126 errx(EXIT_FAILURE, "invalid magic in reply");
128 walk += strlen(I3_IPC_MAGIC);
129 *reply_length = *((uint32_t*)walk);
130 walk += sizeof(uint32_t);
131 if (*((uint32_t*)walk) != message_type)
132 errx(EXIT_FAILURE, "unexpected reply type (got %d, expected %d)", *((uint32_t*)walk), message_type);
133 walk += sizeof(uint32_t);
135 *reply = malloc(*reply_length);
136 if ((*reply) == NULL)
137 err(EXIT_FAILURE, "malloc() failed");
139 to_read = *reply_length;
141 while (read_bytes < to_read) {
142 int n = read(sockfd, *reply + read_bytes, to_read);
144 err(EXIT_FAILURE, "read() failed");
151 int main(int argc, char *argv[]) {
152 socket_path = getenv("I3SOCK");
153 int o, option_index = 0;
154 int message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
155 char *payload = NULL;
158 static struct option long_options[] = {
159 {"socket", required_argument, 0, 's'},
160 {"type", required_argument, 0, 't'},
161 {"version", no_argument, 0, 'v'},
162 {"quiet", no_argument, 0, 'q'},
163 {"help", no_argument, 0, 'h'},
167 char *options_string = "s:t:vhq";
169 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
171 if (socket_path != NULL)
173 socket_path = strdup(optarg);
174 } else if (o == 't') {
175 if (strcasecmp(optarg, "command") == 0)
176 message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
177 else if (strcasecmp(optarg, "get_workspaces") == 0)
178 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
179 else if (strcasecmp(optarg, "get_outputs") == 0)
180 message_type = I3_IPC_MESSAGE_TYPE_GET_OUTPUTS;
181 else if (strcasecmp(optarg, "get_tree") == 0)
182 message_type = I3_IPC_MESSAGE_TYPE_GET_TREE;
184 printf("Unknown message type\n");
185 printf("Known types: command, get_workspaces, get_outputs, get_tree\n");
188 } else if (o == 'q') {
190 } else if (o == 'v') {
191 printf("i3-msg " I3_VERSION "\n");
193 } else if (o == 'h') {
194 printf("i3-msg " I3_VERSION "\n");
195 printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
200 if (socket_path == NULL)
201 socket_path = socket_path_from_x11();
203 /* Fall back to the default socket path */
204 if (socket_path == NULL)
205 socket_path = strdup("/tmp/i3-ipc.sock");
207 /* Use all arguments, separated by whitespace, as payload.
208 * This way, you don’t have to do i3-msg 'mark foo', you can use
210 while (optind < argc) {
212 if (!(payload = strdup(argv[optind])))
213 err(EXIT_FAILURE, "strdup(argv[optind])");
216 if (asprintf(&both, "%s %s", payload, argv[optind]) == -1)
217 err(EXIT_FAILURE, "asprintf");
227 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
229 err(EXIT_FAILURE, "Could not create socket");
231 struct sockaddr_un addr;
232 memset(&addr, 0, sizeof(struct sockaddr_un));
233 addr.sun_family = AF_LOCAL;
234 strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
235 if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
236 err(EXIT_FAILURE, "Could not connect to i3");
238 ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t*)payload);
243 uint32_t reply_length;
245 ipc_recv_message(sockfd, message_type, &reply_length, &reply);
246 printf("%.*s", reply_length, reply);