]> git.sur5r.net Git - i3/i3/blob - i3-msg/main.c
Merge branch 'master' into next
[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-2012 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 <stdio.h>
18 #include <stdbool.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <err.h>
27 #include <stdint.h>
28 #include <getopt.h>
29 #include <limits.h>
30
31 #include <yajl/yajl_parse.h>
32 #include <yajl/yajl_version.h>
33
34 #include <xcb/xcb.h>
35 #include <xcb/xcb_aux.h>
36
37 #include "libi3.h"
38 #include <i3/ipc.h>
39
40 static char *socket_path;
41
42 /*
43  * Having verboselog() and errorlog() is necessary when using libi3.
44  *
45  */
46 void verboselog(char *fmt, ...) {
47     va_list args;
48
49     va_start(args, fmt);
50     vfprintf(stdout, fmt, args);
51     va_end(args);
52 }
53
54 void errorlog(char *fmt, ...) {
55     va_list args;
56
57     va_start(args, fmt);
58     vfprintf(stderr, fmt, args);
59     va_end(args);
60 }
61
62 static char *last_key = NULL;
63
64 typedef struct reply_t {
65     bool success;
66     char *error;
67     char *input;
68     char *errorposition;
69 } reply_t;
70
71 static reply_t last_reply;
72
73 static int reply_boolean_cb(void *params, int val) {
74     if (strcmp(last_key, "success") == 0)
75         last_reply.success = val;
76     return 1;
77 }
78
79 #if YAJL_MAJOR >= 2
80 static int reply_string_cb(void *params, const unsigned char *val, size_t len) {
81 #else
82 static int reply_string_cb(void *params, const unsigned char *val, unsigned int len) {
83 #endif
84     char *str = scalloc(len + 1);
85     strncpy(str, (const char*)val, len);
86     if (strcmp(last_key, "error") == 0)
87         last_reply.error = str;
88     else if (strcmp(last_key, "input") == 0)
89         last_reply.input = str;
90     else if (strcmp(last_key, "errorposition") == 0)
91         last_reply.errorposition = str;
92     else free(str);
93     return 1;
94 }
95
96 static int reply_start_map_cb(void *params) {
97     return 1;
98 }
99
100 static int reply_end_map_cb(void *params) {
101     if (!last_reply.success) {
102         fprintf(stderr, "ERROR: Your command: %s\n", last_reply.input);
103         fprintf(stderr, "ERROR:               %s\n", last_reply.errorposition);
104         fprintf(stderr, "ERROR: %s\n", last_reply.error);
105     }
106     return 1;
107 }
108
109
110 #if YAJL_MAJOR >= 2
111 static int reply_map_key_cb(void *params, const unsigned char *keyVal, size_t keyLen) {
112 #else
113 static int reply_map_key_cb(void *params, const unsigned char *keyVal, unsigned keyLen) {
114 #endif
115     free(last_key);
116     last_key = scalloc(keyLen + 1);
117     strncpy(last_key, (const char*)keyVal, keyLen);
118     return 1;
119 }
120
121 static yajl_callbacks reply_callbacks = {
122     .yajl_boolean = reply_boolean_cb,
123     .yajl_string = reply_string_cb,
124     .yajl_start_map = reply_start_map_cb,
125     .yajl_map_key = reply_map_key_cb,
126     .yajl_end_map = reply_end_map_cb,
127 };
128
129 int main(int argc, char *argv[]) {
130     socket_path = getenv("I3SOCK");
131     int o, option_index = 0;
132     uint32_t message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
133     char *payload = NULL;
134     bool quiet = false;
135
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'},
142         {0, 0, 0, 0}
143     };
144
145     char *options_string = "s:t:vhq";
146
147     while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
148         if (o == 's') {
149             if (socket_path != NULL)
150                 free(socket_path);
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_version") == 0)
166                 message_type = I3_IPC_MESSAGE_TYPE_GET_VERSION;
167             else {
168                 printf("Unknown message type\n");
169                 printf("Known types: command, get_workspaces, get_outputs, get_tree, get_marks, get_bar_config, get_version\n");
170                 exit(EXIT_FAILURE);
171             }
172         } else if (o == 'q') {
173             quiet = true;
174         } else if (o == 'v') {
175             printf("i3-msg " I3_VERSION "\n");
176             return 0;
177         } else if (o == 'h') {
178             printf("i3-msg " I3_VERSION "\n");
179             printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
180             return 0;
181         }
182     }
183
184     if (socket_path == NULL)
185         socket_path = root_atom_contents("I3_SOCKET_PATH", NULL, 0);
186
187     /* Fall back to the default socket path */
188     if (socket_path == NULL)
189         socket_path = sstrdup("/tmp/i3-ipc.sock");
190
191     /* Use all arguments, separated by whitespace, as payload.
192      * This way, you don’t have to do i3-msg 'mark foo', you can use
193      * i3-msg mark foo */
194     while (optind < argc) {
195         if (!payload) {
196             payload = sstrdup(argv[optind]);
197         } else {
198             char *both;
199             if (asprintf(&both, "%s %s", payload, argv[optind]) == -1)
200                 err(EXIT_FAILURE, "asprintf");
201             free(payload);
202             payload = both;
203         }
204         optind++;
205     }
206
207     if (!payload)
208         payload = "";
209
210     int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
211     if (sockfd == -1)
212         err(EXIT_FAILURE, "Could not create socket");
213
214     struct sockaddr_un addr;
215     memset(&addr, 0, sizeof(struct sockaddr_un));
216     addr.sun_family = AF_LOCAL;
217     strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
218     if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
219         err(EXIT_FAILURE, "Could not connect to i3 on socket \"%s\"", socket_path);
220
221     if (ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t*)payload) == -1)
222         err(EXIT_FAILURE, "IPC: write()");
223
224     if (quiet)
225         return 0;
226
227     uint32_t reply_length;
228     uint32_t reply_type;
229     uint8_t *reply;
230     int ret;
231     if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
232         if (ret == -1)
233             err(EXIT_FAILURE, "IPC: read()");
234         exit(1);
235     }
236     if (reply_type != message_type)
237         errx(EXIT_FAILURE, "IPC: Received reply of type %d but expected %d", reply_type, message_type);
238     /* For the reply of commands, have a look if that command was successful.
239      * If not, nicely format the error message. */
240     if (reply_type == I3_IPC_MESSAGE_TYPE_COMMAND) {
241         yajl_handle handle;
242 #if YAJL_MAJOR < 2
243         yajl_parser_config parse_conf = { 0, 0 };
244
245         handle = yajl_alloc(&reply_callbacks, &parse_conf, NULL, NULL);
246 #else
247         handle = yajl_alloc(&reply_callbacks, NULL, NULL);
248 #endif
249         yajl_status state = yajl_parse(handle, (const unsigned char*)reply, reply_length);
250         switch (state) {
251             case yajl_status_ok:
252                 break;
253             case yajl_status_client_canceled:
254 #if YAJL_MAJOR < 2
255             case yajl_status_insufficient_data:
256 #endif
257             case yajl_status_error:
258                 errx(EXIT_FAILURE, "IPC: Could not parse JSON reply.");
259         }
260
261         /* NB: We still fall-through and print the reply, because even if one
262          * command failed, that doesn’t mean that all commands failed. */
263     }
264     printf("%.*s\n", reply_length, reply);
265     free(reply);
266
267     close(sockfd);
268
269     return 0;
270 }