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