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 = malloc(*reply_length);
101 if ((*reply) == NULL)
102 err(EXIT_FAILURE, "malloc() failed");
104 to_read = *reply_length;
106 while (read_bytes < to_read) {
107 int n = read(sockfd, *reply + read_bytes, to_read);
109 err(EXIT_FAILURE, "read() failed");
116 int main(int argc, char *argv[]) {
117 socket_path = getenv("I3SOCK");
118 int o, option_index = 0;
119 int message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
120 char *payload = NULL;
123 static struct option long_options[] = {
124 {"socket", required_argument, 0, 's'},
125 {"type", required_argument, 0, 't'},
126 {"version", no_argument, 0, 'v'},
127 {"quiet", no_argument, 0, 'q'},
128 {"help", no_argument, 0, 'h'},
132 char *options_string = "s:t:vhq";
134 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
136 if (socket_path != NULL)
138 socket_path = strdup(optarg);
139 } else if (o == 't') {
140 if (strcasecmp(optarg, "command") == 0)
141 message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
142 else if (strcasecmp(optarg, "get_workspaces") == 0)
143 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
144 else if (strcasecmp(optarg, "get_outputs") == 0)
145 message_type = I3_IPC_MESSAGE_TYPE_GET_OUTPUTS;
146 else if (strcasecmp(optarg, "get_tree") == 0)
147 message_type = I3_IPC_MESSAGE_TYPE_GET_TREE;
148 else if (strcasecmp(optarg, "get_marks") == 0)
149 message_type = I3_IPC_MESSAGE_TYPE_GET_MARKS;
151 printf("Unknown message type\n");
152 printf("Known types: command, get_workspaces, get_outputs, get_tree, get_marks\n");
155 } else if (o == 'q') {
157 } else if (o == 'v') {
158 printf("i3-msg " I3_VERSION "\n");
160 } else if (o == 'h') {
161 printf("i3-msg " I3_VERSION "\n");
162 printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
167 if (socket_path == NULL)
168 socket_path = socket_path_from_x11();
170 /* Fall back to the default socket path */
171 if (socket_path == NULL)
172 socket_path = strdup("/tmp/i3-ipc.sock");
174 /* Use all arguments, separated by whitespace, as payload.
175 * This way, you don’t have to do i3-msg 'mark foo', you can use
177 while (optind < argc) {
179 if (!(payload = strdup(argv[optind])))
180 err(EXIT_FAILURE, "strdup(argv[optind])");
183 if (asprintf(&both, "%s %s", payload, argv[optind]) == -1)
184 err(EXIT_FAILURE, "asprintf");
194 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
196 err(EXIT_FAILURE, "Could not create socket");
198 struct sockaddr_un addr;
199 memset(&addr, 0, sizeof(struct sockaddr_un));
200 addr.sun_family = AF_LOCAL;
201 strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
202 if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
203 err(EXIT_FAILURE, "Could not connect to i3");
205 ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t*)payload);
210 uint32_t reply_length;
212 ipc_recv_message(sockfd, message_type, &reply_length, &reply);
213 printf("%.*s\n", reply_length, reply);