2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2010 Michael Stapelberg and contributors (see also: LICENSE)
7 * i3-msg/main.c: Utility which sends messages to a running i3-instance using
8 * IPC via UNIX domain sockets.
10 * This (in combination with libi3/ipc_send_message.c and
11 * libi3/ipc_recv_message.c) serves as an example for how to send your own
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;
40 int main(int argc, char *argv[]) {
41 socket_path = getenv("I3SOCK");
42 int o, option_index = 0;
43 int message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
47 static struct option long_options[] = {
48 {"socket", required_argument, 0, 's'},
49 {"type", required_argument, 0, 't'},
50 {"version", no_argument, 0, 'v'},
51 {"quiet", no_argument, 0, 'q'},
52 {"help", no_argument, 0, 'h'},
56 char *options_string = "s:t:vhq";
58 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
60 if (socket_path != NULL)
62 socket_path = sstrdup(optarg);
63 } else if (o == 't') {
64 if (strcasecmp(optarg, "command") == 0)
65 message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
66 else if (strcasecmp(optarg, "get_workspaces") == 0)
67 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
68 else if (strcasecmp(optarg, "get_outputs") == 0)
69 message_type = I3_IPC_MESSAGE_TYPE_GET_OUTPUTS;
70 else if (strcasecmp(optarg, "get_tree") == 0)
71 message_type = I3_IPC_MESSAGE_TYPE_GET_TREE;
72 else if (strcasecmp(optarg, "get_marks") == 0)
73 message_type = I3_IPC_MESSAGE_TYPE_GET_MARKS;
74 else if (strcasecmp(optarg, "get_bar_config") == 0)
75 message_type = I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG;
77 printf("Unknown message type\n");
78 printf("Known types: command, get_workspaces, get_outputs, get_tree, get_marks, get_bar_config\n");
81 } else if (o == 'q') {
83 } else if (o == 'v') {
84 printf("i3-msg " I3_VERSION "\n");
86 } else if (o == 'h') {
87 printf("i3-msg " I3_VERSION "\n");
88 printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
93 if (socket_path == NULL)
94 socket_path = socket_path_from_x11();
96 /* Fall back to the default socket path */
97 if (socket_path == NULL)
98 socket_path = sstrdup("/tmp/i3-ipc.sock");
100 /* Use all arguments, separated by whitespace, as payload.
101 * This way, you don’t have to do i3-msg 'mark foo', you can use
103 while (optind < argc) {
105 payload = sstrdup(argv[optind]);
108 if (asprintf(&both, "%s %s", payload, argv[optind]) == -1)
109 err(EXIT_FAILURE, "asprintf");
119 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
121 err(EXIT_FAILURE, "Could not create socket");
123 struct sockaddr_un addr;
124 memset(&addr, 0, sizeof(struct sockaddr_un));
125 addr.sun_family = AF_LOCAL;
126 strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
127 if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
128 err(EXIT_FAILURE, "Could not connect to i3");
130 if (ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t*)payload) == -1)
131 err(EXIT_FAILURE, "IPC: write()");
136 uint32_t reply_length;
139 if ((ret = ipc_recv_message(sockfd, message_type, &reply_length, &reply)) != 0) {
141 err(EXIT_FAILURE, "IPC: read()");
144 printf("%.*s\n", reply_length, reply);