4 * i3 - an improved dynamic tiling window manager
6 * © 2009 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 :-).
19 #include <sys/types.h>
20 #include <sys/socket.h>
30 static void ipc_send_message(int sockfd, uint32_t message_size,
31 uint32_t message_type, uint8_t *payload) {
32 int buffer_size = strlen("i3-ipc") + sizeof(uint32_t) + sizeof(uint32_t) + message_size;
33 char msg[buffer_size];
36 strcpy(walk, "i3-ipc");
37 walk += strlen("i3-ipc");
38 memcpy(walk, &message_size, sizeof(uint32_t));
39 walk += sizeof(uint32_t);
40 memcpy(walk, &message_type, sizeof(uint32_t));
41 walk += sizeof(uint32_t);
42 memcpy(walk, payload, message_size);
45 int bytes_to_go = buffer_size;
46 while (sent_bytes < bytes_to_go) {
47 int n = write(sockfd, msg + sent_bytes, bytes_to_go);
49 err(EXIT_FAILURE, "write() failed");
56 int main(int argc, char *argv[]) {
57 char *socket_path = "/tmp/i3-ipc.sock";
58 int o, option_index = 0;
60 static struct option long_options[] = {
61 {"socket", required_argument, 0, 's'},
62 {"type", required_argument, 0, 't'},
63 {"version", no_argument, 0, 'v'},
64 {"help", no_argument, 0, 'h'},
68 char *options_string = "s:t:vh";
70 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
72 socket_path = strdup(optarg);
74 } else if (o == 't') {
75 printf("currently only commands are implemented\n");
76 } else if (o == 'v') {
77 printf("i3-msg " I3_VERSION);
79 } else if (o == 'h') {
80 printf("i3-msg " I3_VERSION);
81 printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
87 fprintf(stderr, "Error: missing message\n");
88 fprintf(stderr, "i3-msg [-s <socket>] [-t <type>] <message>\n");
92 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
93 struct sockaddr_un addr;
94 memset(&addr, 0, sizeof(struct sockaddr_un));
95 addr.sun_family = AF_LOCAL;
96 strcpy(addr.sun_path, socket_path);
97 if (connect(sockfd, &addr, sizeof(struct sockaddr_un)) < 0)
98 err(-1, "Could not connect to i3");
100 ipc_send_message(sockfd, strlen(argv[optind]), 0, (uint8_t*)argv[optind]);