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>
38 static char *socket_path;
41 * Formats a message (payload) of the given size and type and sends it to i3 via
42 * the given socket file descriptor.
45 static void ipc_send_message(int sockfd, uint32_t message_size,
46 uint32_t message_type, uint8_t *payload) {
47 int buffer_size = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t) + message_size;
48 char msg[buffer_size];
51 strcpy(walk, I3_IPC_MAGIC);
52 walk += strlen(I3_IPC_MAGIC);
53 memcpy(walk, &message_size, sizeof(uint32_t));
54 walk += sizeof(uint32_t);
55 memcpy(walk, &message_type, sizeof(uint32_t));
56 walk += sizeof(uint32_t);
57 memcpy(walk, payload, message_size);
60 int bytes_to_go = buffer_size;
61 while (sent_bytes < bytes_to_go) {
62 int n = write(sockfd, msg + sent_bytes, bytes_to_go);
64 err(EXIT_FAILURE, "write() failed");
71 static void ipc_recv_message(int sockfd, uint32_t message_type,
72 uint32_t *reply_length, uint8_t **reply) {
73 /* Read the message header first */
74 uint32_t to_read = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t);
78 uint32_t read_bytes = 0;
79 while (read_bytes < to_read) {
80 int n = read(sockfd, msg + read_bytes, to_read);
82 err(EXIT_FAILURE, "read() failed");
84 errx(EXIT_FAILURE, "received EOF instead of reply");
90 if (memcmp(walk, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0)
91 errx(EXIT_FAILURE, "invalid magic in reply");
93 walk += strlen(I3_IPC_MAGIC);
94 *reply_length = *((uint32_t*)walk);
95 walk += sizeof(uint32_t);
96 if (*((uint32_t*)walk) != message_type)
97 errx(EXIT_FAILURE, "unexpected reply type (got %d, expected %d)", *((uint32_t*)walk), message_type);
98 walk += sizeof(uint32_t);
100 *reply = smalloc(*reply_length);
102 to_read = *reply_length;
104 while (read_bytes < to_read) {
105 int n = read(sockfd, *reply + read_bytes, to_read);
107 err(EXIT_FAILURE, "read() failed");
114 int main(int argc, char *argv[]) {
115 socket_path = getenv("I3SOCK");
116 int o, option_index = 0;
117 int message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
118 char *payload = NULL;
121 static struct option long_options[] = {
122 {"socket", required_argument, 0, 's'},
123 {"type", required_argument, 0, 't'},
124 {"version", no_argument, 0, 'v'},
125 {"quiet", no_argument, 0, 'q'},
126 {"help", no_argument, 0, 'h'},
130 char *options_string = "s:t:vhq";
132 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
134 if (socket_path != NULL)
136 socket_path = sstrdup(optarg);
137 } else if (o == 't') {
138 if (strcasecmp(optarg, "command") == 0)
139 message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
140 else if (strcasecmp(optarg, "get_workspaces") == 0)
141 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
142 else if (strcasecmp(optarg, "get_outputs") == 0)
143 message_type = I3_IPC_MESSAGE_TYPE_GET_OUTPUTS;
144 else if (strcasecmp(optarg, "get_tree") == 0)
145 message_type = I3_IPC_MESSAGE_TYPE_GET_TREE;
146 else if (strcasecmp(optarg, "get_marks") == 0)
147 message_type = I3_IPC_MESSAGE_TYPE_GET_MARKS;
149 printf("Unknown message type\n");
150 printf("Known types: command, get_workspaces, get_outputs, get_tree, get_marks\n");
153 } else if (o == 'q') {
155 } else if (o == 'v') {
156 printf("i3-msg " I3_VERSION "\n");
158 } else if (o == 'h') {
159 printf("i3-msg " I3_VERSION "\n");
160 printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
165 if (socket_path == NULL)
166 socket_path = socket_path_from_x11();
168 /* Fall back to the default socket path */
169 if (socket_path == NULL)
170 socket_path = sstrdup("/tmp/i3-ipc.sock");
172 /* Use all arguments, separated by whitespace, as payload.
173 * This way, you don’t have to do i3-msg 'mark foo', you can use
175 while (optind < argc) {
177 payload = sstrdup(argv[optind]);
180 if (asprintf(&both, "%s %s", payload, argv[optind]) == -1)
181 err(EXIT_FAILURE, "asprintf");
191 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
193 err(EXIT_FAILURE, "Could not create socket");
195 struct sockaddr_un addr;
196 memset(&addr, 0, sizeof(struct sockaddr_un));
197 addr.sun_family = AF_LOCAL;
198 strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
199 if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
200 err(EXIT_FAILURE, "Could not connect to i3");
202 ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t*)payload);
207 uint32_t reply_length;
209 ipc_recv_message(sockfd, message_type, &reply_length, &reply);
210 printf("%.*s\n", reply_length, reply);