]> git.sur5r.net Git - i3/i3/blob - i3-msg/main.c
a945d3b173330559c93214630f15db64bd3067f6
[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                 if (n == 0)
77                         errx(EXIT_FAILURE, "received EOF instead of reply");
78
79                 read_bytes += n;
80                 to_read -= n;
81         }
82
83         if (memcmp(walk, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0)
84                 errx(EXIT_FAILURE, "invalid magic in reply");
85
86         walk += strlen(I3_IPC_MAGIC);
87         *reply_length = *((uint32_t*)walk);
88         walk += sizeof(uint32_t);
89         if (*((uint32_t*)walk) != message_type)
90                 errx(EXIT_FAILURE, "unexpected reply type (got %d, expected %d)", *((uint32_t*)walk), message_type);
91         walk += sizeof(uint32_t);
92
93         *reply = malloc(*reply_length);
94         if ((*reply) == NULL)
95                 err(EXIT_FAILURE, "malloc() failed");
96
97         to_read = *reply_length;
98         read_bytes = 0;
99         while (read_bytes < to_read) {
100                 int n = read(sockfd, *reply + read_bytes, to_read);
101                 if (n == -1)
102                         err(EXIT_FAILURE, "read() failed");
103
104                 read_bytes += n;
105                 to_read -= n;
106         }
107 }
108
109 int main(int argc, char *argv[]) {
110         char *socket_path = "/tmp/i3-ipc.sock";
111         int o, option_index = 0;
112         int message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
113         char *payload = "";
114         bool quiet = false;
115
116         static struct option long_options[] = {
117                 {"socket", required_argument, 0, 's'},
118                 {"type", required_argument, 0, 't'},
119                 {"version", no_argument, 0, 'v'},
120                 {"quiet", no_argument, 0, 'q'},
121                 {"help", no_argument, 0, 'h'},
122                 {0, 0, 0, 0}
123         };
124
125         char *options_string = "s:t:vhq";
126
127         while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
128                 if (o == 's') {
129                         socket_path = strdup(optarg);
130                 } else if (o == 't') {
131                         if (strcasecmp(optarg, "command") == 0)
132                                 message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
133                         else if (strcasecmp(optarg, "get_workspaces") == 0)
134                                 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
135                         else if (strcasecmp(optarg, "get_outputs") == 0)
136                                 message_type = I3_IPC_MESSAGE_TYPE_GET_OUTPUTS;
137                         else if (strcasecmp(optarg, "get_tree") == 0)
138                                 message_type = I3_IPC_MESSAGE_TYPE_GET_TREE;
139                         else {
140                                 printf("Unknown message type\n");
141                                 printf("Known types: command, get_workspaces, get_outputs, get_tree\n");
142                                 exit(EXIT_FAILURE);
143                         }
144                 } else if (o == 'q') {
145                         quiet = true;
146                 } else if (o == 'v') {
147                         printf("i3-msg " I3_VERSION "\n");
148                         return 0;
149                 } else if (o == 'h') {
150                         printf("i3-msg " I3_VERSION "\n");
151                         printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
152                         return 0;
153                 }
154         }
155
156         if (optind < argc)
157                 payload = argv[optind];
158
159         int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
160         if (sockfd == -1)
161                 err(EXIT_FAILURE, "Could not create socket");
162
163         struct sockaddr_un addr;
164         memset(&addr, 0, sizeof(struct sockaddr_un));
165         addr.sun_family = AF_LOCAL;
166         strcpy(addr.sun_path, socket_path);
167         if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
168                 err(EXIT_FAILURE, "Could not connect to i3");
169
170         ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t*)payload);
171
172         if (quiet)
173                 return 0;
174
175         uint32_t reply_length;
176         uint8_t *reply;
177         ipc_recv_message(sockfd, message_type, &reply_length, &reply);
178         printf("%.*s", reply_length, reply);
179         free(reply);
180
181         close(sockfd);
182
183         return 0;
184 }