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");
81 if (memcmp(walk, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0)
82 errx(EXIT_FAILURE, "invalid magic in reply");
84 walk += strlen(I3_IPC_MAGIC);
85 *reply_length = *((uint32_t*)walk);
86 walk += sizeof(uint32_t);
87 if (*((uint32_t*)walk) != message_type)
88 errx(EXIT_FAILURE, "unexpected reply type (got %d, expected %d)", *((uint32_t*)walk), message_type);
89 walk += sizeof(uint32_t);
91 *reply = malloc(*reply_length);
93 err(EXIT_FAILURE, "malloc() failed");
95 to_read = *reply_length;
97 while (read_bytes < to_read) {
98 int n = read(sockfd, *reply + read_bytes, to_read);
100 err(EXIT_FAILURE, "read() failed");
107 int main(int argc, char *argv[]) {
108 char *socket_path = "/tmp/i3-ipc.sock";
109 int o, option_index = 0;
110 int message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
114 static struct option long_options[] = {
115 {"socket", required_argument, 0, 's'},
116 {"type", required_argument, 0, 't'},
117 {"version", no_argument, 0, 'v'},
118 {"quiet", no_argument, 0, 'q'},
119 {"help", no_argument, 0, 'h'},
123 char *options_string = "s:t:vhq";
125 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
127 socket_path = strdup(optarg);
129 } else if (o == 't') {
130 if (strcasecmp(optarg, "command") == 0)
131 message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
132 else if (strcasecmp(optarg, "get_workspaces") == 0)
133 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
135 printf("Unknown message type\n");
136 printf("Known types: command, get_workspaces\n");
139 } else if (o == 'q') {
141 } else if (o == 'v') {
142 printf("i3-msg " I3_VERSION);
144 } else if (o == 'h') {
145 printf("i3-msg " I3_VERSION);
146 printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
152 payload = argv[optind];
154 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
156 err(EXIT_FAILURE, "Could not create socket");
158 struct sockaddr_un addr;
159 memset(&addr, 0, sizeof(struct sockaddr_un));
160 addr.sun_family = AF_LOCAL;
161 strcpy(addr.sun_path, socket_path);
162 if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
163 err(EXIT_FAILURE, "Could not connect to i3");
165 ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t*)payload);
170 uint32_t reply_length;
172 ipc_recv_message(sockfd, message_type, &reply_length, &reply);
173 printf("%.*s", reply_length, reply);