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>
35 * This function resolves ~ in pathnames (and more, see glob(3)).
38 static char *glob_path(const char *path) {
39 static glob_t globbuf;
40 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
41 errx(EXIT_FAILURE, "glob() failed");
42 char *result = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
44 err(EXIT_FAILURE, "malloc() failed");
50 * Formats a message (payload) of the given size and type and sends it to i3 via
51 * the given socket file descriptor.
54 static void ipc_send_message(int sockfd, uint32_t message_size,
55 uint32_t message_type, uint8_t *payload) {
56 int buffer_size = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t) + message_size;
57 char msg[buffer_size];
60 strcpy(walk, I3_IPC_MAGIC);
61 walk += strlen(I3_IPC_MAGIC);
62 memcpy(walk, &message_size, sizeof(uint32_t));
63 walk += sizeof(uint32_t);
64 memcpy(walk, &message_type, sizeof(uint32_t));
65 walk += sizeof(uint32_t);
66 memcpy(walk, payload, message_size);
69 int bytes_to_go = buffer_size;
70 while (sent_bytes < bytes_to_go) {
71 int n = write(sockfd, msg + sent_bytes, bytes_to_go);
73 err(EXIT_FAILURE, "write() failed");
80 static void ipc_recv_message(int sockfd, uint32_t message_type,
81 uint32_t *reply_length, uint8_t **reply) {
82 /* Read the message header first */
83 uint32_t to_read = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t);
87 uint32_t read_bytes = 0;
88 while (read_bytes < to_read) {
89 int n = read(sockfd, msg + read_bytes, to_read);
91 err(EXIT_FAILURE, "read() failed");
93 errx(EXIT_FAILURE, "received EOF instead of reply");
99 if (memcmp(walk, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0)
100 errx(EXIT_FAILURE, "invalid magic in reply");
102 walk += strlen(I3_IPC_MAGIC);
103 *reply_length = *((uint32_t*)walk);
104 walk += sizeof(uint32_t);
105 if (*((uint32_t*)walk) != message_type)
106 errx(EXIT_FAILURE, "unexpected reply type (got %d, expected %d)", *((uint32_t*)walk), message_type);
107 walk += sizeof(uint32_t);
109 *reply = malloc(*reply_length);
110 if ((*reply) == NULL)
111 err(EXIT_FAILURE, "malloc() failed");
113 to_read = *reply_length;
115 while (read_bytes < to_read) {
116 int n = read(sockfd, *reply + read_bytes, to_read);
118 err(EXIT_FAILURE, "read() failed");
125 int main(int argc, char *argv[]) {
126 char *socket_path = glob_path("~/.i3/ipc.sock");
127 int o, option_index = 0;
128 int message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
132 static struct option long_options[] = {
133 {"socket", required_argument, 0, 's'},
134 {"type", required_argument, 0, 't'},
135 {"version", no_argument, 0, 'v'},
136 {"quiet", no_argument, 0, 'q'},
137 {"help", no_argument, 0, 'h'},
141 char *options_string = "s:t:vhq";
143 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
145 socket_path = glob_path(optarg);
146 } else if (o == 't') {
147 if (strcasecmp(optarg, "command") == 0)
148 message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
149 else if (strcasecmp(optarg, "get_workspaces") == 0)
150 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
152 printf("Unknown message type\n");
153 printf("Known types: command, get_workspaces\n");
156 } else if (o == 'q') {
158 } else if (o == 'v') {
159 printf("i3-msg " I3_VERSION);
161 } else if (o == 'h') {
162 printf("i3-msg " I3_VERSION);
163 printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
169 payload = argv[optind];
171 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
173 err(EXIT_FAILURE, "Could not create socket");
175 struct sockaddr_un addr;
176 memset(&addr, 0, sizeof(struct sockaddr_un));
177 addr.sun_family = AF_LOCAL;
178 strcpy(addr.sun_path, socket_path);
179 if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
180 err(EXIT_FAILURE, "Could not connect to i3");
182 ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t*)payload);
187 uint32_t reply_length;
189 ipc_recv_message(sockfd, message_type, &reply_length, &reply);
190 printf("%.*s", reply_length, reply);