]> git.sur5r.net Git - i3/i3/blob - i3-msg/main.c
i3-msg: use smalloc, sstrdup 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 = smalloc(*reply_length);
101
102     to_read = *reply_length;
103     read_bytes = 0;
104     while (read_bytes < to_read) {
105         int n = read(sockfd, *reply + read_bytes, to_read);
106         if (n == -1)
107             err(EXIT_FAILURE, "read() failed");
108
109         read_bytes += n;
110         to_read -= n;
111     }
112 }
113
114 int main(int argc, char *argv[]) {
115     socket_path = getenv("I3SOCK");
116     int o, option_index = 0;
117     int message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
118     char *payload = NULL;
119     bool quiet = false;
120
121     static struct option long_options[] = {
122         {"socket", required_argument, 0, 's'},
123         {"type", required_argument, 0, 't'},
124         {"version", no_argument, 0, 'v'},
125         {"quiet", no_argument, 0, 'q'},
126         {"help", no_argument, 0, 'h'},
127         {0, 0, 0, 0}
128     };
129
130     char *options_string = "s:t:vhq";
131
132     while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
133         if (o == 's') {
134             if (socket_path != NULL)
135                 free(socket_path);
136             socket_path = sstrdup(optarg);
137         } else if (o == 't') {
138             if (strcasecmp(optarg, "command") == 0)
139                 message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
140             else if (strcasecmp(optarg, "get_workspaces") == 0)
141                 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
142             else if (strcasecmp(optarg, "get_outputs") == 0)
143                 message_type = I3_IPC_MESSAGE_TYPE_GET_OUTPUTS;
144             else if (strcasecmp(optarg, "get_tree") == 0)
145                 message_type = I3_IPC_MESSAGE_TYPE_GET_TREE;
146             else if (strcasecmp(optarg, "get_marks") == 0)
147                 message_type = I3_IPC_MESSAGE_TYPE_GET_MARKS;
148             else {
149                 printf("Unknown message type\n");
150                 printf("Known types: command, get_workspaces, get_outputs, get_tree, get_marks\n");
151                 exit(EXIT_FAILURE);
152             }
153         } else if (o == 'q') {
154             quiet = true;
155         } else if (o == 'v') {
156             printf("i3-msg " I3_VERSION "\n");
157             return 0;
158         } else if (o == 'h') {
159             printf("i3-msg " I3_VERSION "\n");
160             printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
161             return 0;
162         }
163     }
164
165     if (socket_path == NULL)
166         socket_path = socket_path_from_x11();
167
168     /* Fall back to the default socket path */
169     if (socket_path == NULL)
170         socket_path = sstrdup("/tmp/i3-ipc.sock");
171
172     /* Use all arguments, separated by whitespace, as payload.
173      * This way, you don’t have to do i3-msg 'mark foo', you can use
174      * i3-msg mark foo */
175     while (optind < argc) {
176         if (!payload) {
177             payload = sstrdup(argv[optind]);
178         } else {
179             char *both;
180             if (asprintf(&both, "%s %s", payload, argv[optind]) == -1)
181                 err(EXIT_FAILURE, "asprintf");
182             free(payload);
183             payload = both;
184         }
185         optind++;
186     }
187
188     if (!payload)
189         payload = "";
190
191     int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
192     if (sockfd == -1)
193         err(EXIT_FAILURE, "Could not create socket");
194
195     struct sockaddr_un addr;
196     memset(&addr, 0, sizeof(struct sockaddr_un));
197     addr.sun_family = AF_LOCAL;
198     strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
199     if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
200         err(EXIT_FAILURE, "Could not connect to i3");
201
202     ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t*)payload);
203
204     if (quiet)
205         return 0;
206
207     uint32_t reply_length;
208     uint8_t *reply;
209     ipc_recv_message(sockfd, message_type, &reply_length, &reply);
210     printf("%.*s\n", reply_length, reply);
211     free(reply);
212
213     close(sockfd);
214
215     return 0;
216 }