]> 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  *
6  * © 2009-2010 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  * i3-msg/main.c: Utility which sends messages to a running i3-instance using
11  * IPC via UNIX domain sockets.
12  *
13  * This serves as an example for how to send your own messages to i3.
14  * Additionally, it’s even useful sometimes :-).
15  *
16  */
17 #include <ev.h>
18 #include <stdio.h>
19 #include <stdbool.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <sys/un.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <err.h>
28 #include <stdint.h>
29 #include <getopt.h>
30 #include <limits.h>
31
32 #include <xcb/xcb.h>
33 #include <xcb/xcb_aux.h>
34
35 #include <i3/ipc.h>
36
37 static char *socket_path;
38
39 /*
40  * Try to get the socket path from X11 and return NULL if it doesn’t work.
41  * As i3-msg is a short-running tool, we don’t bother with cleaning up the
42  * connection and leave it up to the operating system on exit.
43  *
44  */
45 static char *socket_path_from_x11() {
46     xcb_connection_t *conn;
47     int screen;
48     if ((conn = xcb_connect(NULL, &screen)) == NULL ||
49         xcb_connection_has_error(conn))
50         return NULL;
51     xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screen);
52     xcb_window_t root = root_screen->root;
53
54     xcb_intern_atom_cookie_t atom_cookie;
55     xcb_intern_atom_reply_t *atom_reply;
56
57     atom_cookie = xcb_intern_atom(conn, 0, strlen("I3_SOCKET_PATH"), "I3_SOCKET_PATH");
58     atom_reply = xcb_intern_atom_reply(conn, atom_cookie, NULL);
59     if (atom_reply == NULL)
60         return NULL;
61
62     xcb_get_property_cookie_t prop_cookie;
63     xcb_get_property_reply_t *prop_reply;
64     prop_cookie = xcb_get_property_unchecked(conn, false, root, atom_reply->atom,
65                                              XCB_GET_PROPERTY_TYPE_ANY, 0, PATH_MAX);
66     prop_reply = xcb_get_property_reply(conn, prop_cookie, NULL);
67     if (prop_reply == NULL || xcb_get_property_value_length(prop_reply) == 0)
68         return NULL;
69     if (asprintf(&socket_path, "%.*s", xcb_get_property_value_length(prop_reply),
70                  (char*)xcb_get_property_value(prop_reply)) == -1)
71         return NULL;
72     return socket_path;
73 }
74
75 /*
76  * Formats a message (payload) of the given size and type and sends it to i3 via
77  * the given socket file descriptor.
78  *
79  */
80 static void ipc_send_message(int sockfd, uint32_t message_size,
81                              uint32_t message_type, uint8_t *payload) {
82     int buffer_size = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t) + message_size;
83     char msg[buffer_size];
84     char *walk = msg;
85
86     strcpy(walk, I3_IPC_MAGIC);
87     walk += strlen(I3_IPC_MAGIC);
88     memcpy(walk, &message_size, sizeof(uint32_t));
89     walk += sizeof(uint32_t);
90     memcpy(walk, &message_type, sizeof(uint32_t));
91     walk += sizeof(uint32_t);
92     memcpy(walk, payload, message_size);
93
94     int sent_bytes = 0;
95     int bytes_to_go = buffer_size;
96     while (sent_bytes < bytes_to_go) {
97         int n = write(sockfd, msg + sent_bytes, bytes_to_go);
98         if (n == -1)
99             err(EXIT_FAILURE, "write() failed");
100
101         sent_bytes += n;
102         bytes_to_go -= n;
103     }
104 }
105
106 static void ipc_recv_message(int sockfd, uint32_t message_type,
107                              uint32_t *reply_length, uint8_t **reply) {
108     /* Read the message header first */
109     uint32_t to_read = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t);
110     char msg[to_read];
111     char *walk = msg;
112
113     uint32_t read_bytes = 0;
114     while (read_bytes < to_read) {
115         int n = read(sockfd, msg + read_bytes, to_read);
116         if (n == -1)
117             err(EXIT_FAILURE, "read() failed");
118         if (n == 0)
119             errx(EXIT_FAILURE, "received EOF instead of reply");
120
121         read_bytes += n;
122         to_read -= n;
123     }
124
125     if (memcmp(walk, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0)
126         errx(EXIT_FAILURE, "invalid magic in reply");
127
128     walk += strlen(I3_IPC_MAGIC);
129     *reply_length = *((uint32_t*)walk);
130     walk += sizeof(uint32_t);
131     if (*((uint32_t*)walk) != message_type)
132         errx(EXIT_FAILURE, "unexpected reply type (got %d, expected %d)", *((uint32_t*)walk), message_type);
133     walk += sizeof(uint32_t);
134
135     *reply = malloc(*reply_length);
136     if ((*reply) == NULL)
137         err(EXIT_FAILURE, "malloc() failed");
138
139     to_read = *reply_length;
140     read_bytes = 0;
141     while (read_bytes < to_read) {
142         int n = read(sockfd, *reply + read_bytes, to_read);
143         if (n == -1)
144             err(EXIT_FAILURE, "read() failed");
145
146         read_bytes += n;
147         to_read -= n;
148     }
149 }
150
151 int main(int argc, char *argv[]) {
152     socket_path = getenv("I3SOCK");
153     int o, option_index = 0;
154     int message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
155     char *payload = NULL;
156     bool quiet = false;
157
158     static struct option long_options[] = {
159         {"socket", required_argument, 0, 's'},
160         {"type", required_argument, 0, 't'},
161         {"version", no_argument, 0, 'v'},
162         {"quiet", no_argument, 0, 'q'},
163         {"help", no_argument, 0, 'h'},
164         {0, 0, 0, 0}
165     };
166
167     char *options_string = "s:t:vhq";
168
169     while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
170         if (o == 's') {
171             if (socket_path != NULL)
172                 free(socket_path);
173             socket_path = strdup(optarg);
174         } else if (o == 't') {
175             if (strcasecmp(optarg, "command") == 0)
176                 message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
177             else if (strcasecmp(optarg, "get_workspaces") == 0)
178                 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
179             else if (strcasecmp(optarg, "get_outputs") == 0)
180                 message_type = I3_IPC_MESSAGE_TYPE_GET_OUTPUTS;
181             else if (strcasecmp(optarg, "get_tree") == 0)
182                 message_type = I3_IPC_MESSAGE_TYPE_GET_TREE;
183             else if (strcasecmp(optarg, "get_marks") == 0)
184                 message_type = I3_IPC_MESSAGE_TYPE_GET_MARKS;
185             else {
186                 printf("Unknown message type\n");
187                 printf("Known types: command, get_workspaces, get_outputs, get_tree, get_marks\n");
188                 exit(EXIT_FAILURE);
189             }
190         } else if (o == 'q') {
191             quiet = true;
192         } else if (o == 'v') {
193             printf("i3-msg " I3_VERSION "\n");
194             return 0;
195         } else if (o == 'h') {
196             printf("i3-msg " I3_VERSION "\n");
197             printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
198             return 0;
199         }
200     }
201
202     if (socket_path == NULL)
203         socket_path = socket_path_from_x11();
204
205     /* Fall back to the default socket path */
206     if (socket_path == NULL)
207         socket_path = strdup("/tmp/i3-ipc.sock");
208
209     /* Use all arguments, separated by whitespace, as payload.
210      * This way, you don’t have to do i3-msg 'mark foo', you can use
211      * i3-msg mark foo */
212     while (optind < argc) {
213         if (!payload) {
214             if (!(payload = strdup(argv[optind])))
215                 err(EXIT_FAILURE, "strdup(argv[optind])");
216         } else {
217             char *both;
218             if (asprintf(&both, "%s %s", payload, argv[optind]) == -1)
219                 err(EXIT_FAILURE, "asprintf");
220             free(payload);
221             payload = both;
222         }
223         optind++;
224     }
225
226     if (!payload)
227         payload = "";
228
229     int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
230     if (sockfd == -1)
231         err(EXIT_FAILURE, "Could not create socket");
232
233     struct sockaddr_un addr;
234     memset(&addr, 0, sizeof(struct sockaddr_un));
235     addr.sun_family = AF_LOCAL;
236     strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
237     if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
238         err(EXIT_FAILURE, "Could not connect to i3");
239
240     ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t*)payload);
241
242     if (quiet)
243         return 0;
244
245     uint32_t reply_length;
246     uint8_t *reply;
247     ipc_recv_message(sockfd, message_type, &reply_length, &reply);
248     printf("%.*s\n", reply_length, reply);
249     free(reply);
250
251     close(sockfd);
252
253     return 0;
254 }