]> git.sur5r.net Git - i3/i3/blob - i3-msg/main.c
i3-msg: use socket_path_from_x11 from libi3
[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 "libi3.h"
36 #include <i3/ipc.h>
37
38 static char *socket_path;
39
40 /*
41  * Formats a message (payload) of the given size and type and sends it to i3 via
42  * the given socket file descriptor.
43  *
44  */
45 static void ipc_send_message(int sockfd, uint32_t message_size,
46                              uint32_t message_type, uint8_t *payload) {
47     int buffer_size = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t) + message_size;
48     char msg[buffer_size];
49     char *walk = msg;
50
51     strcpy(walk, I3_IPC_MAGIC);
52     walk += strlen(I3_IPC_MAGIC);
53     memcpy(walk, &message_size, sizeof(uint32_t));
54     walk += sizeof(uint32_t);
55     memcpy(walk, &message_type, sizeof(uint32_t));
56     walk += sizeof(uint32_t);
57     memcpy(walk, payload, message_size);
58
59     int sent_bytes = 0;
60     int bytes_to_go = buffer_size;
61     while (sent_bytes < bytes_to_go) {
62         int n = write(sockfd, msg + sent_bytes, bytes_to_go);
63         if (n == -1)
64             err(EXIT_FAILURE, "write() failed");
65
66         sent_bytes += n;
67         bytes_to_go -= n;
68     }
69 }
70
71 static void ipc_recv_message(int sockfd, uint32_t message_type,
72                              uint32_t *reply_length, uint8_t **reply) {
73     /* Read the message header first */
74     uint32_t to_read = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t);
75     char msg[to_read];
76     char *walk = msg;
77
78     uint32_t read_bytes = 0;
79     while (read_bytes < to_read) {
80         int n = read(sockfd, msg + read_bytes, to_read);
81         if (n == -1)
82             err(EXIT_FAILURE, "read() failed");
83         if (n == 0)
84             errx(EXIT_FAILURE, "received EOF instead of reply");
85
86         read_bytes += n;
87         to_read -= n;
88     }
89
90     if (memcmp(walk, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0)
91         errx(EXIT_FAILURE, "invalid magic in reply");
92
93     walk += strlen(I3_IPC_MAGIC);
94     *reply_length = *((uint32_t*)walk);
95     walk += sizeof(uint32_t);
96     if (*((uint32_t*)walk) != message_type)
97         errx(EXIT_FAILURE, "unexpected reply type (got %d, expected %d)", *((uint32_t*)walk), message_type);
98     walk += sizeof(uint32_t);
99
100     *reply = malloc(*reply_length);
101     if ((*reply) == NULL)
102         err(EXIT_FAILURE, "malloc() failed");
103
104     to_read = *reply_length;
105     read_bytes = 0;
106     while (read_bytes < to_read) {
107         int n = read(sockfd, *reply + read_bytes, to_read);
108         if (n == -1)
109             err(EXIT_FAILURE, "read() failed");
110
111         read_bytes += n;
112         to_read -= n;
113     }
114 }
115
116 int main(int argc, char *argv[]) {
117     socket_path = getenv("I3SOCK");
118     int o, option_index = 0;
119     int message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
120     char *payload = NULL;
121     bool quiet = false;
122
123     static struct option long_options[] = {
124         {"socket", required_argument, 0, 's'},
125         {"type", required_argument, 0, 't'},
126         {"version", no_argument, 0, 'v'},
127         {"quiet", no_argument, 0, 'q'},
128         {"help", no_argument, 0, 'h'},
129         {0, 0, 0, 0}
130     };
131
132     char *options_string = "s:t:vhq";
133
134     while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
135         if (o == 's') {
136             if (socket_path != NULL)
137                 free(socket_path);
138             socket_path = strdup(optarg);
139         } else if (o == 't') {
140             if (strcasecmp(optarg, "command") == 0)
141                 message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
142             else if (strcasecmp(optarg, "get_workspaces") == 0)
143                 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
144             else if (strcasecmp(optarg, "get_outputs") == 0)
145                 message_type = I3_IPC_MESSAGE_TYPE_GET_OUTPUTS;
146             else if (strcasecmp(optarg, "get_tree") == 0)
147                 message_type = I3_IPC_MESSAGE_TYPE_GET_TREE;
148             else if (strcasecmp(optarg, "get_marks") == 0)
149                 message_type = I3_IPC_MESSAGE_TYPE_GET_MARKS;
150             else {
151                 printf("Unknown message type\n");
152                 printf("Known types: command, get_workspaces, get_outputs, get_tree, get_marks\n");
153                 exit(EXIT_FAILURE);
154             }
155         } else if (o == 'q') {
156             quiet = true;
157         } else if (o == 'v') {
158             printf("i3-msg " I3_VERSION "\n");
159             return 0;
160         } else if (o == 'h') {
161             printf("i3-msg " I3_VERSION "\n");
162             printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
163             return 0;
164         }
165     }
166
167     if (socket_path == NULL)
168         socket_path = socket_path_from_x11();
169
170     /* Fall back to the default socket path */
171     if (socket_path == NULL)
172         socket_path = strdup("/tmp/i3-ipc.sock");
173
174     /* Use all arguments, separated by whitespace, as payload.
175      * This way, you don’t have to do i3-msg 'mark foo', you can use
176      * i3-msg mark foo */
177     while (optind < argc) {
178         if (!payload) {
179             if (!(payload = strdup(argv[optind])))
180                 err(EXIT_FAILURE, "strdup(argv[optind])");
181         } else {
182             char *both;
183             if (asprintf(&both, "%s %s", payload, argv[optind]) == -1)
184                 err(EXIT_FAILURE, "asprintf");
185             free(payload);
186             payload = both;
187         }
188         optind++;
189     }
190
191     if (!payload)
192         payload = "";
193
194     int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
195     if (sockfd == -1)
196         err(EXIT_FAILURE, "Could not create socket");
197
198     struct sockaddr_un addr;
199     memset(&addr, 0, sizeof(struct sockaddr_un));
200     addr.sun_family = AF_LOCAL;
201     strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
202     if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
203         err(EXIT_FAILURE, "Could not connect to i3");
204
205     ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t*)payload);
206
207     if (quiet)
208         return 0;
209
210     uint32_t reply_length;
211     uint8_t *reply;
212     ipc_recv_message(sockfd, message_type, &reply_length, &reply);
213     printf("%.*s\n", reply_length, reply);
214     free(reply);
215
216     close(sockfd);
217
218     return 0;
219 }