]> git.sur5r.net Git - i3/i3/blob - i3-msg/main.c
33bedc7cbe198334cef13757911139bcdad0c430
[i3/i3] / i3-msg / main.c
1 /*
2  * vim:ts=8: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 <glob.h>
31
32 #include <i3/ipc.h>
33
34 /*
35  * This function resolves ~ in pathnames (and more, see glob(3)).
36  *
37  */
38 static char *glob_path(const char *path) {
39         static glob_t globbuf;
40         if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
41                 errx(EXIT_FAILURE, "glob() failed");
42         char *result = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
43         if (result == NULL)
44                 err(EXIT_FAILURE, "malloc() failed");
45         globfree(&globbuf);
46         return result;
47 }
48
49 /*
50  * Formats a message (payload) of the given size and type and sends it to i3 via
51  * the given socket file descriptor.
52  *
53  */
54 static void ipc_send_message(int sockfd, uint32_t message_size,
55                              uint32_t message_type, uint8_t *payload) {
56         int buffer_size = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t) + message_size;
57         char msg[buffer_size];
58         char *walk = msg;
59
60         strcpy(walk, I3_IPC_MAGIC);
61         walk += strlen(I3_IPC_MAGIC);
62         memcpy(walk, &message_size, sizeof(uint32_t));
63         walk += sizeof(uint32_t);
64         memcpy(walk, &message_type, sizeof(uint32_t));
65         walk += sizeof(uint32_t);
66         memcpy(walk, payload, message_size);
67
68         int sent_bytes = 0;
69         int bytes_to_go = buffer_size;
70         while (sent_bytes < bytes_to_go) {
71                 int n = write(sockfd, msg + sent_bytes, bytes_to_go);
72                 if (n == -1)
73                         err(EXIT_FAILURE, "write() failed");
74
75                 sent_bytes += n;
76                 bytes_to_go -= n;
77         }
78 }
79
80 static void ipc_recv_message(int sockfd, uint32_t message_type,
81                              uint32_t *reply_length, uint8_t **reply) {
82         /* Read the message header first */
83         uint32_t to_read = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t);
84         char msg[to_read];
85         char *walk = msg;
86
87         uint32_t read_bytes = 0;
88         while (read_bytes < to_read) {
89                 int n = read(sockfd, msg + read_bytes, to_read);
90                 if (n == -1)
91                         err(EXIT_FAILURE, "read() failed");
92                 if (n == 0)
93                         errx(EXIT_FAILURE, "received EOF instead of reply");
94
95                 read_bytes += n;
96                 to_read -= n;
97         }
98
99         if (memcmp(walk, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0)
100                 errx(EXIT_FAILURE, "invalid magic in reply");
101
102         walk += strlen(I3_IPC_MAGIC);
103         *reply_length = *((uint32_t*)walk);
104         walk += sizeof(uint32_t);
105         if (*((uint32_t*)walk) != message_type)
106                 errx(EXIT_FAILURE, "unexpected reply type (got %d, expected %d)", *((uint32_t*)walk), message_type);
107         walk += sizeof(uint32_t);
108
109         *reply = malloc(*reply_length);
110         if ((*reply) == NULL)
111                 err(EXIT_FAILURE, "malloc() failed");
112
113         to_read = *reply_length;
114         read_bytes = 0;
115         while (read_bytes < to_read) {
116                 int n = read(sockfd, *reply + read_bytes, to_read);
117                 if (n == -1)
118                         err(EXIT_FAILURE, "read() failed");
119
120                 read_bytes += n;
121                 to_read -= n;
122         }
123 }
124
125 int main(int argc, char *argv[]) {
126         char *socket_path = glob_path("~/.i3/ipc.sock");
127         int o, option_index = 0;
128         int message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
129         char *payload = "";
130         bool quiet = false;
131
132         static struct option long_options[] = {
133                 {"socket", required_argument, 0, 's'},
134                 {"type", required_argument, 0, 't'},
135                 {"version", no_argument, 0, 'v'},
136                 {"quiet", no_argument, 0, 'q'},
137                 {"help", no_argument, 0, 'h'},
138                 {0, 0, 0, 0}
139         };
140
141         char *options_string = "s:t:vhq";
142
143         while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
144                 if (o == 's') {
145                         socket_path = glob_path(optarg);
146                 } else if (o == 't') {
147                         if (strcasecmp(optarg, "command") == 0)
148                                 message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
149                         else if (strcasecmp(optarg, "get_workspaces") == 0)
150                                 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
151                         else {
152                                 printf("Unknown message type\n");
153                                 printf("Known types: command, get_workspaces\n");
154                                 exit(EXIT_FAILURE);
155                         }
156                 } else if (o == 'q') {
157                         quiet = true;
158                 } else if (o == 'v') {
159                         printf("i3-msg " I3_VERSION);
160                         return 0;
161                 } else if (o == 'h') {
162                         printf("i3-msg " I3_VERSION);
163                         printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
164                         return 0;
165                 }
166         }
167
168         if (optind < argc)
169                 payload = argv[optind];
170
171         int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
172         if (sockfd == -1)
173                 err(EXIT_FAILURE, "Could not create socket");
174
175         struct sockaddr_un addr;
176         memset(&addr, 0, sizeof(struct sockaddr_un));
177         addr.sun_family = AF_LOCAL;
178         strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
179         if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
180                 err(EXIT_FAILURE, "Could not connect to i3");
181
182         ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t*)payload);
183
184         if (quiet)
185                 return 0;
186
187         uint32_t reply_length;
188         uint8_t *reply;
189         ipc_recv_message(sockfd, message_type, &reply_length, &reply);
190         printf("%.*s", reply_length, reply);
191         free(reply);
192
193         close(sockfd);
194
195         return 0;
196 }