]> git.sur5r.net Git - i3/i3/blob - i3-msg/main.c
Update userguide "Focus Parent": add the default
[i3/i3] / i3-msg / main.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * i3-msg/main.c: Utility which sends messages to a running i3-instance using
8  * IPC via UNIX domain sockets.
9  *
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
12  * messages to i3.
13  *
14  * Additionally, it’s even useful sometimes :-).
15  *
16  */
17 #include "libi3.h"
18
19 #include <stdio.h>
20 #include <stdbool.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <err.h>
29 #include <stdint.h>
30 #include <getopt.h>
31 #include <limits.h>
32
33 #include <yajl/yajl_parse.h>
34 #include <yajl/yajl_version.h>
35
36 #include <xcb/xcb.h>
37 #include <xcb/xcb_aux.h>
38
39 #include <i3/ipc.h>
40
41 /*
42  * Having verboselog() and errorlog() is necessary when using libi3.
43  *
44  */
45 void verboselog(char *fmt, ...) {
46     va_list args;
47
48     va_start(args, fmt);
49     vfprintf(stdout, fmt, args);
50     va_end(args);
51 }
52
53 void errorlog(char *fmt, ...) {
54     va_list args;
55
56     va_start(args, fmt);
57     vfprintf(stderr, fmt, args);
58     va_end(args);
59 }
60
61 static char *last_key = NULL;
62
63 typedef struct reply_t {
64     bool success;
65     char *error;
66     char *input;
67     char *errorposition;
68 } reply_t;
69
70 static reply_t last_reply;
71
72 static int reply_boolean_cb(void *params, int val) {
73     if (strcmp(last_key, "success") == 0)
74         last_reply.success = val;
75     return 1;
76 }
77
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;
87     else
88         free(str);
89     return 1;
90 }
91
92 static int reply_start_map_cb(void *params) {
93     return 1;
94 }
95
96 static int reply_end_map_cb(void *params) {
97     if (!last_reply.success) {
98         if (last_reply.input) {
99             fprintf(stderr, "ERROR: Your command: %s\n", last_reply.input);
100             fprintf(stderr, "ERROR:               %s\n", last_reply.errorposition);
101         }
102         fprintf(stderr, "ERROR: %s\n", last_reply.error);
103     }
104     return 1;
105 }
106
107 static int reply_map_key_cb(void *params, const unsigned char *keyVal, size_t keyLen) {
108     free(last_key);
109     last_key = scalloc(keyLen + 1, 1);
110     strncpy(last_key, (const char *)keyVal, keyLen);
111     return 1;
112 }
113
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,
120 };
121
122 /*******************************************************************************
123  * Config reply callbacks
124  *******************************************************************************/
125
126 static char *config_last_key = NULL;
127
128 static int config_string_cb(void *params, const unsigned char *val, size_t len) {
129     char *str = scalloc(len + 1, 1);
130     strncpy(str, (const char *)val, len);
131     if (strcmp(config_last_key, "config") == 0) {
132         fprintf(stdout, "%s", str);
133     }
134     free(str);
135     return 1;
136 }
137
138 static int config_start_map_cb(void *params) {
139     return 1;
140 }
141
142 static int config_end_map_cb(void *params) {
143     return 1;
144 }
145
146 static int config_map_key_cb(void *params, const unsigned char *keyVal, size_t keyLen) {
147     config_last_key = scalloc(keyLen + 1, 1);
148     strncpy(config_last_key, (const char *)keyVal, keyLen);
149     return 1;
150 }
151
152 static yajl_callbacks config_callbacks = {
153     .yajl_string = config_string_cb,
154     .yajl_start_map = config_start_map_cb,
155     .yajl_map_key = config_map_key_cb,
156     .yajl_end_map = config_end_map_cb,
157 };
158
159 int main(int argc, char *argv[]) {
160 #if defined(__OpenBSD__)
161     if (pledge("stdio rpath unix", NULL) == -1)
162         err(EXIT_FAILURE, "pledge");
163 #endif
164     char *socket_path = NULL;
165     int o, option_index = 0;
166     uint32_t message_type = I3_IPC_MESSAGE_TYPE_RUN_COMMAND;
167     char *payload = NULL;
168     bool quiet = false;
169     bool monitor = false;
170
171     static struct option long_options[] = {
172         {"socket", required_argument, 0, 's'},
173         {"type", required_argument, 0, 't'},
174         {"version", no_argument, 0, 'v'},
175         {"quiet", no_argument, 0, 'q'},
176         {"monitor", no_argument, 0, 'm'},
177         {"help", no_argument, 0, 'h'},
178         {0, 0, 0, 0}};
179
180     char *options_string = "s:t:vhqm";
181
182     while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
183         if (o == 's') {
184             free(socket_path);
185             socket_path = sstrdup(optarg);
186         } else if (o == 't') {
187             if (strcasecmp(optarg, "command") == 0) {
188                 message_type = I3_IPC_MESSAGE_TYPE_RUN_COMMAND;
189             } else if (strcasecmp(optarg, "run_command") == 0) {
190                 message_type = I3_IPC_MESSAGE_TYPE_RUN_COMMAND;
191             } else if (strcasecmp(optarg, "get_workspaces") == 0) {
192                 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
193             } else if (strcasecmp(optarg, "get_outputs") == 0) {
194                 message_type = I3_IPC_MESSAGE_TYPE_GET_OUTPUTS;
195             } else if (strcasecmp(optarg, "get_tree") == 0) {
196                 message_type = I3_IPC_MESSAGE_TYPE_GET_TREE;
197             } else if (strcasecmp(optarg, "get_marks") == 0) {
198                 message_type = I3_IPC_MESSAGE_TYPE_GET_MARKS;
199             } else if (strcasecmp(optarg, "get_bar_config") == 0) {
200                 message_type = I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG;
201             } else if (strcasecmp(optarg, "get_binding_modes") == 0) {
202                 message_type = I3_IPC_MESSAGE_TYPE_GET_BINDING_MODES;
203             } else if (strcasecmp(optarg, "get_version") == 0) {
204                 message_type = I3_IPC_MESSAGE_TYPE_GET_VERSION;
205             } else if (strcasecmp(optarg, "get_config") == 0) {
206                 message_type = I3_IPC_MESSAGE_TYPE_GET_CONFIG;
207             } else if (strcasecmp(optarg, "send_tick") == 0) {
208                 message_type = I3_IPC_MESSAGE_TYPE_SEND_TICK;
209             } else if (strcasecmp(optarg, "subscribe") == 0) {
210                 message_type = I3_IPC_MESSAGE_TYPE_SUBSCRIBE;
211             } else {
212                 printf("Unknown message type\n");
213                 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");
214                 exit(EXIT_FAILURE);
215             }
216         } else if (o == 'q') {
217             quiet = true;
218         } else if (o == 'm') {
219             monitor = true;
220         } else if (o == 'v') {
221             printf("i3-msg " I3_VERSION "\n");
222             return 0;
223         } else if (o == 'h') {
224             printf("i3-msg " I3_VERSION "\n");
225             printf("i3-msg [-s <socket>] [-t <type>] [-m] <message>\n");
226             return 0;
227         } else if (o == '?') {
228             exit(EXIT_FAILURE);
229         }
230     }
231
232     if (monitor && message_type != I3_IPC_MESSAGE_TYPE_SUBSCRIBE) {
233         fprintf(stderr, "The monitor option -m is used with -t SUBSCRIBE exclusively.\n");
234         exit(EXIT_FAILURE);
235     }
236
237     /* Use all arguments, separated by whitespace, as payload.
238      * This way, you don’t have to do i3-msg 'mark foo', you can use
239      * i3-msg mark foo */
240     while (optind < argc) {
241         if (!payload) {
242             payload = sstrdup(argv[optind]);
243         } else {
244             char *both;
245             sasprintf(&both, "%s %s", payload, argv[optind]);
246             free(payload);
247             payload = both;
248         }
249         optind++;
250     }
251
252     if (!payload)
253         payload = sstrdup("");
254
255     int sockfd = ipc_connect(socket_path);
256     if (ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t *)payload) == -1)
257         err(EXIT_FAILURE, "IPC: write()");
258     free(payload);
259
260     uint32_t reply_length;
261     uint32_t reply_type;
262     uint8_t *reply;
263     int ret;
264     if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
265         if (ret == -1)
266             err(EXIT_FAILURE, "IPC: read()");
267         exit(1);
268     }
269     if (reply_type != message_type)
270         errx(EXIT_FAILURE, "IPC: Received reply of type %d but expected %d", reply_type, message_type);
271     /* For the reply of commands, have a look if that command was successful.
272      * If not, nicely format the error message. */
273     if (reply_type == I3_IPC_REPLY_TYPE_COMMAND) {
274         yajl_handle handle = yajl_alloc(&reply_callbacks, NULL, NULL);
275         yajl_status state = yajl_parse(handle, (const unsigned char *)reply, reply_length);
276         yajl_free(handle);
277
278         switch (state) {
279             case yajl_status_ok:
280                 break;
281             case yajl_status_client_canceled:
282             case yajl_status_error:
283                 errx(EXIT_FAILURE, "IPC: Could not parse JSON reply.");
284         }
285
286         if (!quiet) {
287             printf("%.*s\n", reply_length, reply);
288         }
289     } else if (reply_type == I3_IPC_REPLY_TYPE_CONFIG) {
290         yajl_handle handle = yajl_alloc(&config_callbacks, NULL, NULL);
291         yajl_status state = yajl_parse(handle, (const unsigned char *)reply, reply_length);
292         yajl_free(handle);
293
294         switch (state) {
295             case yajl_status_ok:
296                 break;
297             case yajl_status_client_canceled:
298             case yajl_status_error:
299                 errx(EXIT_FAILURE, "IPC: Could not parse JSON reply.");
300         }
301     } else if (reply_type == I3_IPC_REPLY_TYPE_SUBSCRIBE) {
302         do {
303             free(reply);
304             if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
305                 if (ret == -1)
306                     err(EXIT_FAILURE, "IPC: read()");
307                 exit(1);
308             }
309
310             if (!(reply_type & I3_IPC_EVENT_MASK)) {
311                 errx(EXIT_FAILURE, "IPC: Received reply of type %d but expected an event", reply_type);
312             }
313
314             if (!quiet) {
315                 fprintf(stdout, "%.*s\n", reply_length, reply);
316                 fflush(stdout);
317             }
318         } while (monitor);
319     } else {
320         if (!quiet) {
321             printf("%.*s\n", reply_length, reply);
322         }
323     }
324
325     free(reply);
326
327     close(sockfd);
328
329     return 0;
330 }