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>
34 * Formats a message (payload) of the given size and type and sends it to i3 via
35 * the given socket file descriptor.
38 static void ipc_send_message(int sockfd, uint32_t message_size,
39 uint32_t message_type, uint8_t *payload) {
40 int buffer_size = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t) + message_size;
41 char msg[buffer_size];
44 strcpy(walk, I3_IPC_MAGIC);
45 walk += strlen(I3_IPC_MAGIC);
46 memcpy(walk, &message_size, sizeof(uint32_t));
47 walk += sizeof(uint32_t);
48 memcpy(walk, &message_type, sizeof(uint32_t));
49 walk += sizeof(uint32_t);
50 memcpy(walk, payload, message_size);
53 int bytes_to_go = buffer_size;
54 while (sent_bytes < bytes_to_go) {
55 int n = write(sockfd, msg + sent_bytes, bytes_to_go);
57 err(EXIT_FAILURE, "write() failed");
64 static void ipc_recv_message(int sockfd, uint32_t message_type,
65 uint32_t *reply_length, uint8_t **reply) {
66 /* Read the message header first */
67 uint32_t to_read = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t);
71 uint32_t read_bytes = 0;
72 while (read_bytes < to_read) {
73 int n = read(sockfd, msg + read_bytes, to_read);
75 err(EXIT_FAILURE, "read() failed");
77 errx(EXIT_FAILURE, "received EOF instead of reply");
83 if (memcmp(walk, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0)
84 errx(EXIT_FAILURE, "invalid magic in reply");
86 walk += strlen(I3_IPC_MAGIC);
87 *reply_length = *((uint32_t*)walk);
88 walk += sizeof(uint32_t);
89 if (*((uint32_t*)walk) != message_type)
90 errx(EXIT_FAILURE, "unexpected reply type (got %d, expected %d)", *((uint32_t*)walk), message_type);
91 walk += sizeof(uint32_t);
93 *reply = malloc(*reply_length);
95 err(EXIT_FAILURE, "malloc() failed");
97 to_read = *reply_length;
99 while (read_bytes < to_read) {
100 int n = read(sockfd, *reply + read_bytes, to_read);
102 err(EXIT_FAILURE, "read() failed");
109 int main(int argc, char *argv[]) {
111 if ((socket_path = getenv("I3SOCK")) == NULL) {
112 socket_path = strdup("/tmp/i3-ipc.sock");
114 int o, option_index = 0;
115 int message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
119 static struct option long_options[] = {
120 {"socket", required_argument, 0, 's'},
121 {"type", required_argument, 0, 't'},
122 {"version", no_argument, 0, 'v'},
123 {"quiet", no_argument, 0, 'q'},
124 {"help", no_argument, 0, 'h'},
128 char *options_string = "s:t:vhq";
130 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
132 if (socket_path != NULL)
134 socket_path = strdup(optarg);
135 } else if (o == 't') {
136 if (strcasecmp(optarg, "command") == 0)
137 message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
138 else if (strcasecmp(optarg, "get_workspaces") == 0)
139 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
140 else if (strcasecmp(optarg, "get_outputs") == 0)
141 message_type = I3_IPC_MESSAGE_TYPE_GET_OUTPUTS;
142 else if (strcasecmp(optarg, "get_tree") == 0)
143 message_type = I3_IPC_MESSAGE_TYPE_GET_TREE;
145 printf("Unknown message type\n");
146 printf("Known types: command, get_workspaces, get_outputs, get_tree\n");
149 } else if (o == 'q') {
151 } else if (o == 'v') {
152 printf("i3-msg " I3_VERSION "\n");
154 } else if (o == 'h') {
155 printf("i3-msg " I3_VERSION "\n");
156 printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
162 payload = argv[optind];
164 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
166 err(EXIT_FAILURE, "Could not create socket");
168 struct sockaddr_un addr;
169 memset(&addr, 0, sizeof(struct sockaddr_un));
170 addr.sun_family = AF_LOCAL;
171 strcpy(addr.sun_path, socket_path);
172 if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
173 err(EXIT_FAILURE, "Could not connect to i3");
175 ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t*)payload);
180 uint32_t reply_length;
182 ipc_recv_message(sockfd, message_type, &reply_length, &reply);
183 printf("%.*s", reply_length, reply);