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>
41 static char *socket_path;
44 * Having verboselog() and errorlog() is necessary when using libi3.
47 void verboselog(char *fmt, ...) {
51 vfprintf(stdout, fmt, args);
55 void errorlog(char *fmt, ...) {
59 vfprintf(stderr, fmt, args);
63 static char *last_key = NULL;
65 typedef struct reply_t {
72 static reply_t last_reply;
74 static int reply_boolean_cb(void *params, int val) {
75 if (strcmp(last_key, "success") == 0)
76 last_reply.success = val;
80 static int reply_string_cb(void *params, const unsigned char *val, size_t len) {
81 char *str = scalloc(len + 1, 1);
82 strncpy(str, (const char *)val, len);
83 if (strcmp(last_key, "error") == 0)
84 last_reply.error = str;
85 else if (strcmp(last_key, "input") == 0)
86 last_reply.input = str;
87 else if (strcmp(last_key, "errorposition") == 0)
88 last_reply.errorposition = str;
94 static int reply_start_map_cb(void *params) {
98 static int reply_end_map_cb(void *params) {
99 if (!last_reply.success) {
100 fprintf(stderr, "ERROR: Your command: %s\n", last_reply.input);
101 fprintf(stderr, "ERROR: %s\n", last_reply.errorposition);
102 fprintf(stderr, "ERROR: %s\n", last_reply.error);
107 static int reply_map_key_cb(void *params, const unsigned char *keyVal, size_t keyLen) {
109 last_key = scalloc(keyLen + 1, 1);
110 strncpy(last_key, (const char *)keyVal, keyLen);
114 static yajl_callbacks reply_callbacks = {
115 .yajl_boolean = reply_boolean_cb,
116 .yajl_string = reply_string_cb,
117 .yajl_start_map = reply_start_map_cb,
118 .yajl_map_key = reply_map_key_cb,
119 .yajl_end_map = reply_end_map_cb,
122 int main(int argc, char *argv[]) {
123 #if defined(__OpenBSD__)
124 if (pledge("stdio rpath unix", NULL) == -1)
125 err(EXIT_FAILURE, "pledge");
127 char *env_socket_path = getenv("I3SOCK");
129 socket_path = sstrdup(env_socket_path);
132 int o, option_index = 0;
133 uint32_t message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
134 char *payload = NULL;
137 static struct option long_options[] = {
138 {"socket", required_argument, 0, 's'},
139 {"type", required_argument, 0, 't'},
140 {"version", no_argument, 0, 'v'},
141 {"quiet", no_argument, 0, 'q'},
142 {"help", no_argument, 0, 'h'},
145 char *options_string = "s:t:vhq";
147 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
149 if (socket_path != NULL)
151 socket_path = sstrdup(optarg);
152 } else if (o == 't') {
153 if (strcasecmp(optarg, "command") == 0)
154 message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
155 else if (strcasecmp(optarg, "get_workspaces") == 0)
156 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
157 else if (strcasecmp(optarg, "get_outputs") == 0)
158 message_type = I3_IPC_MESSAGE_TYPE_GET_OUTPUTS;
159 else if (strcasecmp(optarg, "get_tree") == 0)
160 message_type = I3_IPC_MESSAGE_TYPE_GET_TREE;
161 else if (strcasecmp(optarg, "get_marks") == 0)
162 message_type = I3_IPC_MESSAGE_TYPE_GET_MARKS;
163 else if (strcasecmp(optarg, "get_bar_config") == 0)
164 message_type = I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG;
165 else if (strcasecmp(optarg, "get_binding_modes") == 0)
166 message_type = I3_IPC_MESSAGE_TYPE_GET_BINDING_MODES;
167 else if (strcasecmp(optarg, "get_version") == 0)
168 message_type = I3_IPC_MESSAGE_TYPE_GET_VERSION;
170 printf("Unknown message type\n");
171 printf("Known types: command, get_workspaces, get_outputs, get_tree, get_marks, get_bar_config, get_binding_modes, get_version\n");
174 } else if (o == 'q') {
176 } else if (o == 'v') {
177 printf("i3-msg " I3_VERSION "\n");
179 } else if (o == 'h') {
180 printf("i3-msg " I3_VERSION "\n");
181 printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
186 if (socket_path == NULL)
187 socket_path = root_atom_contents("I3_SOCKET_PATH", NULL, 0);
189 /* Fall back to the default socket path */
190 if (socket_path == NULL)
191 socket_path = sstrdup("/tmp/i3-ipc.sock");
193 /* Use all arguments, separated by whitespace, as payload.
194 * This way, you don’t have to do i3-msg 'mark foo', you can use
196 while (optind < argc) {
198 payload = sstrdup(argv[optind]);
201 sasprintf(&both, "%s %s", payload, argv[optind]);
209 payload = sstrdup("");
211 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
213 err(EXIT_FAILURE, "Could not create socket");
215 struct sockaddr_un addr;
216 memset(&addr, 0, sizeof(struct sockaddr_un));
217 addr.sun_family = AF_LOCAL;
218 strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
219 if (connect(sockfd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0)
220 err(EXIT_FAILURE, "Could not connect to i3 on socket \"%s\"", socket_path);
222 if (ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t *)payload) == -1)
223 err(EXIT_FAILURE, "IPC: write()");
229 uint32_t reply_length;
233 if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
235 err(EXIT_FAILURE, "IPC: read()");
238 if (reply_type != message_type)
239 errx(EXIT_FAILURE, "IPC: Received reply of type %d but expected %d", reply_type, message_type);
240 /* For the reply of commands, have a look if that command was successful.
241 * If not, nicely format the error message. */
242 if (reply_type == I3_IPC_MESSAGE_TYPE_COMMAND) {
243 yajl_handle handle = yajl_alloc(&reply_callbacks, NULL, NULL);
244 yajl_status state = yajl_parse(handle, (const unsigned char *)reply, reply_length);
250 case yajl_status_client_canceled:
251 case yajl_status_error:
252 errx(EXIT_FAILURE, "IPC: Could not parse JSON reply.");
255 /* NB: We still fall-through and print the reply, because even if one
256 * command failed, that doesn’t mean that all commands failed. */
258 printf("%.*s\n", reply_length, reply);