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 :-).
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;
79 static int reply_string_cb(void *params, const unsigned char *val, size_t len) {
80 char *str = scalloc(len + 1, 1);
81 strncpy(str, (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 fprintf(stderr, "ERROR: Your command: %s\n", last_reply.input);
100 fprintf(stderr, "ERROR: %s\n", last_reply.errorposition);
101 fprintf(stderr, "ERROR: %s\n", last_reply.error);
106 static int reply_map_key_cb(void *params, const unsigned char *keyVal, size_t keyLen) {
108 last_key = scalloc(keyLen + 1, 1);
109 strncpy(last_key, (const char *)keyVal, keyLen);
113 static yajl_callbacks reply_callbacks = {
114 .yajl_boolean = reply_boolean_cb,
115 .yajl_string = reply_string_cb,
116 .yajl_start_map = reply_start_map_cb,
117 .yajl_map_key = reply_map_key_cb,
118 .yajl_end_map = reply_end_map_cb,
121 int main(int argc, char *argv[]) {
122 #if defined(__OpenBSD__)
123 if (pledge("stdio rpath unix", NULL) == -1)
124 err(EXIT_FAILURE, "pledge");
126 char *env_socket_path = getenv("I3SOCK");
128 socket_path = sstrdup(env_socket_path);
131 int o, option_index = 0;
132 uint32_t message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
133 char *payload = NULL;
136 static struct option long_options[] = {
137 {"socket", required_argument, 0, 's'},
138 {"type", required_argument, 0, 't'},
139 {"version", no_argument, 0, 'v'},
140 {"quiet", no_argument, 0, 'q'},
141 {"help", no_argument, 0, 'h'},
144 char *options_string = "s:t:vhq";
146 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
148 if (socket_path != NULL)
150 socket_path = sstrdup(optarg);
151 } else if (o == 't') {
152 if (strcasecmp(optarg, "command") == 0)
153 message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
154 else if (strcasecmp(optarg, "get_workspaces") == 0)
155 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
156 else if (strcasecmp(optarg, "get_outputs") == 0)
157 message_type = I3_IPC_MESSAGE_TYPE_GET_OUTPUTS;
158 else if (strcasecmp(optarg, "get_tree") == 0)
159 message_type = I3_IPC_MESSAGE_TYPE_GET_TREE;
160 else if (strcasecmp(optarg, "get_marks") == 0)
161 message_type = I3_IPC_MESSAGE_TYPE_GET_MARKS;
162 else if (strcasecmp(optarg, "get_bar_config") == 0)
163 message_type = I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG;
164 else if (strcasecmp(optarg, "get_version") == 0)
165 message_type = I3_IPC_MESSAGE_TYPE_GET_VERSION;
167 printf("Unknown message type\n");
168 printf("Known types: command, get_workspaces, get_outputs, get_tree, get_marks, get_bar_config, get_version\n");
171 } else if (o == 'q') {
173 } else if (o == 'v') {
174 printf("i3-msg " I3_VERSION "\n");
176 } else if (o == 'h') {
177 printf("i3-msg " I3_VERSION "\n");
178 printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
183 if (socket_path == NULL)
184 socket_path = root_atom_contents("I3_SOCKET_PATH", NULL, 0);
186 /* Fall back to the default socket path */
187 if (socket_path == NULL)
188 socket_path = sstrdup("/tmp/i3-ipc.sock");
190 /* Use all arguments, separated by whitespace, as payload.
191 * This way, you don’t have to do i3-msg 'mark foo', you can use
193 while (optind < argc) {
195 payload = sstrdup(argv[optind]);
198 sasprintf(&both, "%s %s", payload, argv[optind]);
208 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
210 err(EXIT_FAILURE, "Could not create socket");
212 struct sockaddr_un addr;
213 memset(&addr, 0, sizeof(struct sockaddr_un));
214 addr.sun_family = AF_LOCAL;
215 strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
216 if (connect(sockfd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0)
217 err(EXIT_FAILURE, "Could not connect to i3 on socket \"%s\"", socket_path);
219 if (ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t *)payload) == -1)
220 err(EXIT_FAILURE, "IPC: write()");
225 uint32_t reply_length;
229 if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
231 err(EXIT_FAILURE, "IPC: read()");
234 if (reply_type != message_type)
235 errx(EXIT_FAILURE, "IPC: Received reply of type %d but expected %d", reply_type, message_type);
236 /* For the reply of commands, have a look if that command was successful.
237 * If not, nicely format the error message. */
238 if (reply_type == I3_IPC_MESSAGE_TYPE_COMMAND) {
240 handle = yajl_alloc(&reply_callbacks, NULL, NULL);
241 yajl_status state = yajl_parse(handle, (const unsigned char *)reply, reply_length);
245 case yajl_status_client_canceled:
246 case yajl_status_error:
247 errx(EXIT_FAILURE, "IPC: Could not parse JSON reply.");
250 /* NB: We still fall-through and print the reply, because even if one
251 * command failed, that doesn’t mean that all commands failed. */
253 printf("%.*s\n", reply_length, reply);