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 reply_t last_reply;
72 static int reply_boolean_cb(void *params, int val) {
73 if (strcmp(last_key, "success") == 0)
74 last_reply.success = val;
78 static int reply_string_cb(void *params, const unsigned char *val, size_t len) {
79 char *str = scalloc(len + 1, 1);
80 strncpy(str, (const char *)val, len);
81 if (strcmp(last_key, "error") == 0)
82 last_reply.error = str;
83 else if (strcmp(last_key, "input") == 0)
84 last_reply.input = str;
85 else if (strcmp(last_key, "errorposition") == 0)
86 last_reply.errorposition = str;
92 static int reply_start_map_cb(void *params) {
96 static int reply_end_map_cb(void *params) {
97 if (!last_reply.success) {
98 fprintf(stderr, "ERROR: Your command: %s\n", last_reply.input);
99 fprintf(stderr, "ERROR: %s\n", last_reply.errorposition);
100 fprintf(stderr, "ERROR: %s\n", last_reply.error);
105 static int reply_map_key_cb(void *params, const unsigned char *keyVal, size_t keyLen) {
107 last_key = scalloc(keyLen + 1, 1);
108 strncpy(last_key, (const char *)keyVal, keyLen);
112 static yajl_callbacks reply_callbacks = {
113 .yajl_boolean = reply_boolean_cb,
114 .yajl_string = reply_string_cb,
115 .yajl_start_map = reply_start_map_cb,
116 .yajl_map_key = reply_map_key_cb,
117 .yajl_end_map = reply_end_map_cb,
120 /*******************************************************************************
121 * Config reply callbacks
122 *******************************************************************************/
124 static char *config_last_key = NULL;
126 static int config_string_cb(void *params, const unsigned char *val, size_t len) {
127 char *str = scalloc(len + 1, 1);
128 strncpy(str, (const char *)val, len);
129 if (strcmp(config_last_key, "config") == 0) {
130 fprintf(stdout, "%s", str);
136 static int config_start_map_cb(void *params) {
140 static int config_end_map_cb(void *params) {
144 static int config_map_key_cb(void *params, const unsigned char *keyVal, size_t keyLen) {
145 config_last_key = scalloc(keyLen + 1, 1);
146 strncpy(config_last_key, (const char *)keyVal, keyLen);
150 static yajl_callbacks config_callbacks = {
151 .yajl_string = config_string_cb,
152 .yajl_start_map = config_start_map_cb,
153 .yajl_map_key = config_map_key_cb,
154 .yajl_end_map = config_end_map_cb,
157 int main(int argc, char *argv[]) {
158 #if defined(__OpenBSD__)
159 if (pledge("stdio rpath unix", NULL) == -1)
160 err(EXIT_FAILURE, "pledge");
162 char *socket_path = NULL;
163 int o, option_index = 0;
164 uint32_t message_type = I3_IPC_MESSAGE_TYPE_RUN_COMMAND;
165 char *payload = NULL;
168 static struct option long_options[] = {
169 {"socket", required_argument, 0, 's'},
170 {"type", required_argument, 0, 't'},
171 {"version", no_argument, 0, 'v'},
172 {"quiet", no_argument, 0, 'q'},
173 {"help", no_argument, 0, 'h'},
176 char *options_string = "s:t:vhq";
178 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
181 socket_path = sstrdup(optarg);
182 } else if (o == 't') {
183 if (strcasecmp(optarg, "command") == 0) {
184 message_type = I3_IPC_MESSAGE_TYPE_RUN_COMMAND;
185 } else if (strcasecmp(optarg, "run_command") == 0) {
186 message_type = I3_IPC_MESSAGE_TYPE_RUN_COMMAND;
187 } else if (strcasecmp(optarg, "get_workspaces") == 0) {
188 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
189 } else if (strcasecmp(optarg, "get_outputs") == 0) {
190 message_type = I3_IPC_MESSAGE_TYPE_GET_OUTPUTS;
191 } else if (strcasecmp(optarg, "get_tree") == 0) {
192 message_type = I3_IPC_MESSAGE_TYPE_GET_TREE;
193 } else if (strcasecmp(optarg, "get_marks") == 0) {
194 message_type = I3_IPC_MESSAGE_TYPE_GET_MARKS;
195 } else if (strcasecmp(optarg, "get_bar_config") == 0) {
196 message_type = I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG;
197 } else if (strcasecmp(optarg, "get_binding_modes") == 0) {
198 message_type = I3_IPC_MESSAGE_TYPE_GET_BINDING_MODES;
199 } else if (strcasecmp(optarg, "get_version") == 0) {
200 message_type = I3_IPC_MESSAGE_TYPE_GET_VERSION;
201 } else if (strcasecmp(optarg, "get_config") == 0) {
202 message_type = I3_IPC_MESSAGE_TYPE_GET_CONFIG;
203 } else if (strcasecmp(optarg, "send_tick") == 0) {
204 message_type = I3_IPC_MESSAGE_TYPE_SEND_TICK;
206 printf("Unknown message type\n");
207 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\n");
210 } else if (o == 'q') {
212 } else if (o == 'v') {
213 printf("i3-msg " I3_VERSION "\n");
215 } else if (o == 'h') {
216 printf("i3-msg " I3_VERSION "\n");
217 printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
219 } else if (o == '?') {
224 /* Use all arguments, separated by whitespace, as payload.
225 * This way, you don’t have to do i3-msg 'mark foo', you can use
227 while (optind < argc) {
229 payload = sstrdup(argv[optind]);
232 sasprintf(&both, "%s %s", payload, argv[optind]);
240 payload = sstrdup("");
242 int sockfd = ipc_connect(socket_path);
243 if (ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t *)payload) == -1)
244 err(EXIT_FAILURE, "IPC: write()");
250 uint32_t reply_length;
254 if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
256 err(EXIT_FAILURE, "IPC: read()");
259 if (reply_type != message_type)
260 errx(EXIT_FAILURE, "IPC: Received reply of type %d but expected %d", reply_type, message_type);
261 /* For the reply of commands, have a look if that command was successful.
262 * If not, nicely format the error message. */
263 if (reply_type == I3_IPC_REPLY_TYPE_COMMAND) {
264 yajl_handle handle = yajl_alloc(&reply_callbacks, NULL, NULL);
265 yajl_status state = yajl_parse(handle, (const unsigned char *)reply, reply_length);
271 case yajl_status_client_canceled:
272 case yajl_status_error:
273 errx(EXIT_FAILURE, "IPC: Could not parse JSON reply.");
276 /* NB: We still fall-through and print the reply, because even if one
277 * command failed, that doesn’t mean that all commands failed. */
278 } else if (reply_type == I3_IPC_REPLY_TYPE_CONFIG) {
279 yajl_handle handle = yajl_alloc(&config_callbacks, NULL, NULL);
280 yajl_status state = yajl_parse(handle, (const unsigned char *)reply, reply_length);
286 case yajl_status_client_canceled:
287 case yajl_status_error:
288 errx(EXIT_FAILURE, "IPC: Could not parse JSON reply.");
293 printf("%.*s\n", reply_length, reply);