2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2012 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 :-).
19 #include <sys/types.h>
20 #include <sys/socket.h>
32 #include <xcb/xcb_aux.h>
37 static char *socket_path;
40 * Having verboselog() and errorlog() is necessary when using libi3.
43 void verboselog(char *fmt, ...) {
47 vfprintf(stdout, fmt, args);
51 void errorlog(char *fmt, ...) {
55 vfprintf(stderr, fmt, args);
59 int main(int argc, char *argv[]) {
60 socket_path = getenv("I3SOCK");
61 int o, option_index = 0;
62 int message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
66 static struct option long_options[] = {
67 {"socket", required_argument, 0, 's'},
68 {"type", required_argument, 0, 't'},
69 {"version", no_argument, 0, 'v'},
70 {"quiet", no_argument, 0, 'q'},
71 {"help", no_argument, 0, 'h'},
75 char *options_string = "s:t:vhq";
77 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
79 if (socket_path != NULL)
81 socket_path = sstrdup(optarg);
82 } else if (o == 't') {
83 if (strcasecmp(optarg, "command") == 0)
84 message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
85 else if (strcasecmp(optarg, "get_workspaces") == 0)
86 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
87 else if (strcasecmp(optarg, "get_outputs") == 0)
88 message_type = I3_IPC_MESSAGE_TYPE_GET_OUTPUTS;
89 else if (strcasecmp(optarg, "get_tree") == 0)
90 message_type = I3_IPC_MESSAGE_TYPE_GET_TREE;
91 else if (strcasecmp(optarg, "get_marks") == 0)
92 message_type = I3_IPC_MESSAGE_TYPE_GET_MARKS;
93 else if (strcasecmp(optarg, "get_bar_config") == 0)
94 message_type = I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG;
95 else if (strcasecmp(optarg, "get_version") == 0)
96 message_type = I3_IPC_MESSAGE_TYPE_GET_VERSION;
98 printf("Unknown message type\n");
99 printf("Known types: command, get_workspaces, get_outputs, get_tree, get_marks, get_bar_config, get_version\n");
102 } else if (o == 'q') {
104 } else if (o == 'v') {
105 printf("i3-msg " I3_VERSION "\n");
107 } else if (o == 'h') {
108 printf("i3-msg " I3_VERSION "\n");
109 printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
114 if (socket_path == NULL)
115 socket_path = root_atom_contents("I3_SOCKET_PATH");
117 /* Fall back to the default socket path */
118 if (socket_path == NULL)
119 socket_path = sstrdup("/tmp/i3-ipc.sock");
121 /* Use all arguments, separated by whitespace, as payload.
122 * This way, you don’t have to do i3-msg 'mark foo', you can use
124 while (optind < argc) {
126 payload = sstrdup(argv[optind]);
129 if (asprintf(&both, "%s %s", payload, argv[optind]) == -1)
130 err(EXIT_FAILURE, "asprintf");
140 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
142 err(EXIT_FAILURE, "Could not create socket");
144 struct sockaddr_un addr;
145 memset(&addr, 0, sizeof(struct sockaddr_un));
146 addr.sun_family = AF_LOCAL;
147 strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
148 if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
149 err(EXIT_FAILURE, "Could not connect to i3 on socket \"%s\"", socket_path);
151 if (ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t*)payload) == -1)
152 err(EXIT_FAILURE, "IPC: write()");
157 uint32_t reply_length;
161 if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
163 err(EXIT_FAILURE, "IPC: read()");
166 if (reply_type != message_type)
167 errx(EXIT_FAILURE, "IPC: Received reply of type %d but expected %d", reply_type, message_type);
168 printf("%.*s\n", reply_length, reply);