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>
31 #include <yajl/yajl_parse.h>
32 #include <yajl/yajl_version.h>
35 #include <xcb/xcb_aux.h>
40 static char *socket_path;
43 * Having verboselog() and errorlog() is necessary when using libi3.
46 void verboselog(char *fmt, ...) {
50 vfprintf(stdout, fmt, args);
54 void errorlog(char *fmt, ...) {
58 vfprintf(stderr, fmt, args);
62 static char *last_key = NULL;
64 typedef struct reply_t {
71 static reply_t last_reply;
73 static int reply_boolean_cb(void *params, int val) {
74 if (strcmp(last_key, "success") == 0)
75 last_reply.success = val;
80 static int reply_string_cb(void *params, const unsigned char *val, size_t len) {
82 static int reply_string_cb(void *params, const unsigned char *val, unsigned int len) {
84 char *str = scalloc(len + 1);
85 strncpy(str, (const char*)val, len);
86 if (strcmp(last_key, "error") == 0)
87 last_reply.error = str;
88 else if (strcmp(last_key, "input") == 0)
89 last_reply.input = str;
90 else if (strcmp(last_key, "errorposition") == 0)
91 last_reply.errorposition = str;
96 static int reply_start_map_cb(void *params) {
100 static int reply_end_map_cb(void *params) {
101 if (!last_reply.success) {
102 fprintf(stderr, "ERROR: Your command: %s\n", last_reply.input);
103 fprintf(stderr, "ERROR: %s\n", last_reply.errorposition);
104 fprintf(stderr, "ERROR: %s\n", last_reply.error);
111 static int reply_map_key_cb(void *params, const unsigned char *keyVal, size_t keyLen) {
113 static int reply_map_key_cb(void *params, const unsigned char *keyVal, unsigned keyLen) {
116 last_key = scalloc(keyLen + 1);
117 strncpy(last_key, (const char*)keyVal, keyLen);
121 yajl_callbacks reply_callbacks = {
135 int main(int argc, char *argv[]) {
136 socket_path = getenv("I3SOCK");
137 int o, option_index = 0;
138 int message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
139 char *payload = NULL;
142 static struct option long_options[] = {
143 {"socket", required_argument, 0, 's'},
144 {"type", required_argument, 0, 't'},
145 {"version", no_argument, 0, 'v'},
146 {"quiet", no_argument, 0, 'q'},
147 {"help", no_argument, 0, 'h'},
151 char *options_string = "s:t:vhq";
153 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
155 if (socket_path != NULL)
157 socket_path = sstrdup(optarg);
158 } else if (o == 't') {
159 if (strcasecmp(optarg, "command") == 0)
160 message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
161 else if (strcasecmp(optarg, "get_workspaces") == 0)
162 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
163 else if (strcasecmp(optarg, "get_outputs") == 0)
164 message_type = I3_IPC_MESSAGE_TYPE_GET_OUTPUTS;
165 else if (strcasecmp(optarg, "get_tree") == 0)
166 message_type = I3_IPC_MESSAGE_TYPE_GET_TREE;
167 else if (strcasecmp(optarg, "get_marks") == 0)
168 message_type = I3_IPC_MESSAGE_TYPE_GET_MARKS;
169 else if (strcasecmp(optarg, "get_bar_config") == 0)
170 message_type = I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG;
171 else if (strcasecmp(optarg, "get_version") == 0)
172 message_type = I3_IPC_MESSAGE_TYPE_GET_VERSION;
174 printf("Unknown message type\n");
175 printf("Known types: command, get_workspaces, get_outputs, get_tree, get_marks, get_bar_config, get_version\n");
178 } else if (o == 'q') {
180 } else if (o == 'v') {
181 printf("i3-msg " I3_VERSION "\n");
183 } else if (o == 'h') {
184 printf("i3-msg " I3_VERSION "\n");
185 printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
190 if (socket_path == NULL)
191 socket_path = root_atom_contents("I3_SOCKET_PATH");
193 /* Fall back to the default socket path */
194 if (socket_path == NULL)
195 socket_path = sstrdup("/tmp/i3-ipc.sock");
197 /* Use all arguments, separated by whitespace, as payload.
198 * This way, you don’t have to do i3-msg 'mark foo', you can use
200 while (optind < argc) {
202 payload = sstrdup(argv[optind]);
205 if (asprintf(&both, "%s %s", payload, argv[optind]) == -1)
206 err(EXIT_FAILURE, "asprintf");
216 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
218 err(EXIT_FAILURE, "Could not create socket");
220 struct sockaddr_un addr;
221 memset(&addr, 0, sizeof(struct sockaddr_un));
222 addr.sun_family = AF_LOCAL;
223 strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
224 if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
225 err(EXIT_FAILURE, "Could not connect to i3 on socket \"%s\"", socket_path);
227 if (ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t*)payload) == -1)
228 err(EXIT_FAILURE, "IPC: write()");
233 uint32_t reply_length;
237 if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
239 err(EXIT_FAILURE, "IPC: read()");
242 if (reply_type != message_type)
243 errx(EXIT_FAILURE, "IPC: Received reply of type %d but expected %d", reply_type, message_type);
244 /* For the reply of commands, have a look if that command was successful.
245 * If not, nicely format the error message. */
246 if (reply_type == I3_IPC_MESSAGE_TYPE_COMMAND) {
249 yajl_parser_config parse_conf = { 0, 0 };
251 handle = yajl_alloc(&reply_callbacks, &parse_conf, NULL, NULL);
253 handle = yajl_alloc(&reply_callbacks, NULL, NULL);
255 yajl_status state = yajl_parse(handle, (const unsigned char*)reply, reply_length);
259 case yajl_status_client_canceled:
261 case yajl_status_insufficient_data:
263 case yajl_status_error:
264 errx(EXIT_FAILURE, "IPC: Could not parse JSON reply.");
267 /* NB: We still fall-through and print the reply, because even if one
268 * command failed, that doesn’t mean that all commands failed. */
270 printf("%.*s\n", reply_length, reply);