2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 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 :-).
21 #include <sys/types.h>
22 #include <sys/socket.h>
33 #include <yajl/yajl_parse.h>
34 #include <yajl/yajl_version.h>
37 #include <xcb/xcb_aux.h>
42 * Having verboselog() and errorlog() is necessary when using libi3.
45 void verboselog(char *fmt, ...) {
49 vfprintf(stdout, fmt, args);
53 void errorlog(char *fmt, ...) {
57 vfprintf(stderr, fmt, args);
61 static char *last_key = NULL;
63 typedef struct reply_t {
70 static int exit_code = 0;
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;
79 static int reply_string_cb(void *params, const unsigned char *val, size_t len) {
80 char *str = sstrndup((const char *)val, len);
82 if (strcmp(last_key, "error") == 0)
83 last_reply.error = str;
84 else if (strcmp(last_key, "input") == 0)
85 last_reply.input = str;
86 else if (strcmp(last_key, "errorposition") == 0)
87 last_reply.errorposition = str;
93 static int reply_start_map_cb(void *params) {
97 static int reply_end_map_cb(void *params) {
98 if (!last_reply.success) {
99 if (last_reply.input) {
100 fprintf(stderr, "ERROR: Your command: %s\n", last_reply.input);
101 fprintf(stderr, "ERROR: %s\n", last_reply.errorposition);
103 fprintf(stderr, "ERROR: %s\n", last_reply.error);
109 static int reply_map_key_cb(void *params, const unsigned char *keyVal, size_t keyLen) {
111 last_key = sstrndup((const char *)keyVal, keyLen);
115 static yajl_callbacks reply_callbacks = {
116 .yajl_boolean = reply_boolean_cb,
117 .yajl_string = reply_string_cb,
118 .yajl_start_map = reply_start_map_cb,
119 .yajl_map_key = reply_map_key_cb,
120 .yajl_end_map = reply_end_map_cb,
123 /*******************************************************************************
124 * Config reply callbacks
125 *******************************************************************************/
127 static char *config_last_key = NULL;
129 static int config_string_cb(void *params, const unsigned char *val, size_t len) {
130 char *str = sstrndup((const char *)val, len);
131 if (strcmp(config_last_key, "config") == 0) {
132 fprintf(stdout, "%s", str);
138 static int config_start_map_cb(void *params) {
142 static int config_end_map_cb(void *params) {
146 static int config_map_key_cb(void *params, const unsigned char *keyVal, size_t keyLen) {
147 config_last_key = sstrndup((const char *)keyVal, keyLen);
151 static yajl_callbacks config_callbacks = {
152 .yajl_string = config_string_cb,
153 .yajl_start_map = config_start_map_cb,
154 .yajl_map_key = config_map_key_cb,
155 .yajl_end_map = config_end_map_cb,
158 int main(int argc, char *argv[]) {
159 #if defined(__OpenBSD__)
160 if (pledge("stdio rpath unix", NULL) == -1)
161 err(EXIT_FAILURE, "pledge");
163 char *socket_path = NULL;
164 int o, option_index = 0;
165 uint32_t message_type = I3_IPC_MESSAGE_TYPE_RUN_COMMAND;
166 char *payload = NULL;
168 bool monitor = false;
170 static struct option long_options[] = {
171 {"socket", required_argument, 0, 's'},
172 {"type", required_argument, 0, 't'},
173 {"version", no_argument, 0, 'v'},
174 {"quiet", no_argument, 0, 'q'},
175 {"monitor", no_argument, 0, 'm'},
176 {"help", no_argument, 0, 'h'},
179 char *options_string = "s:t:vhqm";
181 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
184 socket_path = sstrdup(optarg);
185 } else if (o == 't') {
186 if (strcasecmp(optarg, "command") == 0) {
187 message_type = I3_IPC_MESSAGE_TYPE_RUN_COMMAND;
188 } else if (strcasecmp(optarg, "run_command") == 0) {
189 message_type = I3_IPC_MESSAGE_TYPE_RUN_COMMAND;
190 } else if (strcasecmp(optarg, "get_workspaces") == 0) {
191 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
192 } else if (strcasecmp(optarg, "get_outputs") == 0) {
193 message_type = I3_IPC_MESSAGE_TYPE_GET_OUTPUTS;
194 } else if (strcasecmp(optarg, "get_tree") == 0) {
195 message_type = I3_IPC_MESSAGE_TYPE_GET_TREE;
196 } else if (strcasecmp(optarg, "get_marks") == 0) {
197 message_type = I3_IPC_MESSAGE_TYPE_GET_MARKS;
198 } else if (strcasecmp(optarg, "get_bar_config") == 0) {
199 message_type = I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG;
200 } else if (strcasecmp(optarg, "get_binding_modes") == 0) {
201 message_type = I3_IPC_MESSAGE_TYPE_GET_BINDING_MODES;
202 } else if (strcasecmp(optarg, "get_version") == 0) {
203 message_type = I3_IPC_MESSAGE_TYPE_GET_VERSION;
204 } else if (strcasecmp(optarg, "get_config") == 0) {
205 message_type = I3_IPC_MESSAGE_TYPE_GET_CONFIG;
206 } else if (strcasecmp(optarg, "send_tick") == 0) {
207 message_type = I3_IPC_MESSAGE_TYPE_SEND_TICK;
208 } else if (strcasecmp(optarg, "subscribe") == 0) {
209 message_type = I3_IPC_MESSAGE_TYPE_SUBSCRIBE;
211 printf("Unknown message type\n");
212 printf("Known types: run_command, get_workspaces, get_outputs, get_tree, get_marks, get_bar_config, get_binding_modes, get_version, get_config, send_tick, subscribe\n");
215 } else if (o == 'q') {
217 } else if (o == 'm') {
219 } else if (o == 'v') {
220 printf("i3-msg " I3_VERSION "\n");
222 } else if (o == 'h') {
223 printf("i3-msg " I3_VERSION "\n");
224 printf("i3-msg [-s <socket>] [-t <type>] [-m] <message>\n");
226 } else if (o == '?') {
231 if (monitor && message_type != I3_IPC_MESSAGE_TYPE_SUBSCRIBE) {
232 fprintf(stderr, "The monitor option -m is used with -t SUBSCRIBE exclusively.\n");
236 /* Use all arguments, separated by whitespace, as payload.
237 * This way, you don’t have to do i3-msg 'mark foo', you can use
239 while (optind < argc) {
241 payload = sstrdup(argv[optind]);
244 sasprintf(&both, "%s %s", payload, argv[optind]);
252 payload = sstrdup("");
254 int sockfd = ipc_connect(socket_path);
255 if (ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t *)payload) == -1)
256 err(EXIT_FAILURE, "IPC: write()");
259 uint32_t reply_length;
263 if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
265 err(EXIT_FAILURE, "IPC: read()");
268 if (reply_type != message_type)
269 errx(EXIT_FAILURE, "IPC: Received reply of type %d but expected %d", reply_type, message_type);
270 /* For the reply of commands, have a look if that command was successful.
271 * If not, nicely format the error message. */
272 if (reply_type == I3_IPC_REPLY_TYPE_COMMAND) {
273 yajl_handle handle = yajl_alloc(&reply_callbacks, NULL, NULL);
274 yajl_status state = yajl_parse(handle, (const unsigned char *)reply, reply_length);
280 case yajl_status_client_canceled:
281 case yajl_status_error:
282 errx(EXIT_FAILURE, "IPC: Could not parse JSON reply.");
286 printf("%.*s\n", reply_length, reply);
288 } else if (reply_type == I3_IPC_REPLY_TYPE_CONFIG) {
289 yajl_handle handle = yajl_alloc(&config_callbacks, NULL, NULL);
290 yajl_status state = yajl_parse(handle, (const unsigned char *)reply, reply_length);
296 case yajl_status_client_canceled:
297 case yajl_status_error:
298 errx(EXIT_FAILURE, "IPC: Could not parse JSON reply.");
300 } else if (reply_type == I3_IPC_REPLY_TYPE_SUBSCRIBE) {
303 if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
305 err(EXIT_FAILURE, "IPC: read()");
309 if (!(reply_type & I3_IPC_EVENT_MASK)) {
310 errx(EXIT_FAILURE, "IPC: Received reply of type %d but expected an event", reply_type);
314 fprintf(stdout, "%.*s\n", reply_length, reply);
320 printf("%.*s\n", reply_length, reply);