]> git.sur5r.net Git - i3/i3/blob - i3-msg/main.c
Merge branch 'master' into next
[i3/i3] / i3-msg / main.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * i3-msg/main.c: Utility which sends messages to a running i3-instance using
8  * IPC via UNIX domain sockets.
9  *
10  * This (in combination with libi3/ipc_send_message.c and
11  * libi3/ipc_recv_message.c) serves as an example for how to send your own
12  * messages to i3.
13  *
14  * Additionally, it’s even useful sometimes :-).
15  *
16  */
17 #include <stdio.h>
18 #include <stdbool.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <err.h>
27 #include <stdint.h>
28 #include <getopt.h>
29 #include <limits.h>
30
31 #include <xcb/xcb.h>
32 #include <xcb/xcb_aux.h>
33
34 #include "libi3.h"
35 #include <i3/ipc.h>
36
37 static char *socket_path;
38
39 /*
40  * Having verboselog() and errorlog() is necessary when using libi3.
41  *
42  */
43 void verboselog(char *fmt, ...) {
44     va_list args;
45
46     va_start(args, fmt);
47     vfprintf(stdout, fmt, args);
48     va_end(args);
49 }
50
51 void errorlog(char *fmt, ...) {
52     va_list args;
53
54     va_start(args, fmt);
55     vfprintf(stderr, fmt, args);
56     va_end(args);
57 }
58
59 int main(int argc, char *argv[]) {
60     socket_path = getenv("I3SOCK");
61     int o, option_index = 0;
62     int message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
63     char *payload = NULL;
64     bool quiet = false;
65
66     static struct option long_options[] = {
67         {"socket", required_argument, 0, 's'},
68         {"type", required_argument, 0, 't'},
69         {"version", no_argument, 0, 'v'},
70         {"quiet", no_argument, 0, 'q'},
71         {"help", no_argument, 0, 'h'},
72         {0, 0, 0, 0}
73     };
74
75     char *options_string = "s:t:vhq";
76
77     while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
78         if (o == 's') {
79             if (socket_path != NULL)
80                 free(socket_path);
81             socket_path = sstrdup(optarg);
82         } else if (o == 't') {
83             if (strcasecmp(optarg, "command") == 0)
84                 message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
85             else if (strcasecmp(optarg, "get_workspaces") == 0)
86                 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
87             else if (strcasecmp(optarg, "get_outputs") == 0)
88                 message_type = I3_IPC_MESSAGE_TYPE_GET_OUTPUTS;
89             else if (strcasecmp(optarg, "get_tree") == 0)
90                 message_type = I3_IPC_MESSAGE_TYPE_GET_TREE;
91             else if (strcasecmp(optarg, "get_marks") == 0)
92                 message_type = I3_IPC_MESSAGE_TYPE_GET_MARKS;
93             else if (strcasecmp(optarg, "get_bar_config") == 0)
94                 message_type = I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG;
95             else if (strcasecmp(optarg, "get_version") == 0)
96                 message_type = I3_IPC_MESSAGE_TYPE_GET_VERSION;
97             else {
98                 printf("Unknown message type\n");
99                 printf("Known types: command, get_workspaces, get_outputs, get_tree, get_marks, get_bar_config, get_version\n");
100                 exit(EXIT_FAILURE);
101             }
102         } else if (o == 'q') {
103             quiet = true;
104         } else if (o == 'v') {
105             printf("i3-msg " I3_VERSION "\n");
106             return 0;
107         } else if (o == 'h') {
108             printf("i3-msg " I3_VERSION "\n");
109             printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
110             return 0;
111         }
112     }
113
114     if (socket_path == NULL)
115         socket_path = root_atom_contents("I3_SOCKET_PATH");
116
117     /* Fall back to the default socket path */
118     if (socket_path == NULL)
119         socket_path = sstrdup("/tmp/i3-ipc.sock");
120
121     /* Use all arguments, separated by whitespace, as payload.
122      * This way, you don’t have to do i3-msg 'mark foo', you can use
123      * i3-msg mark foo */
124     while (optind < argc) {
125         if (!payload) {
126             payload = sstrdup(argv[optind]);
127         } else {
128             char *both;
129             if (asprintf(&both, "%s %s", payload, argv[optind]) == -1)
130                 err(EXIT_FAILURE, "asprintf");
131             free(payload);
132             payload = both;
133         }
134         optind++;
135     }
136
137     if (!payload)
138         payload = "";
139
140     int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
141     if (sockfd == -1)
142         err(EXIT_FAILURE, "Could not create socket");
143
144     struct sockaddr_un addr;
145     memset(&addr, 0, sizeof(struct sockaddr_un));
146     addr.sun_family = AF_LOCAL;
147     strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
148     if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
149         err(EXIT_FAILURE, "Could not connect to i3 on socket \"%s\"", socket_path);
150
151     if (ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t*)payload) == -1)
152         err(EXIT_FAILURE, "IPC: write()");
153
154     if (quiet)
155         return 0;
156
157     uint32_t reply_length;
158     uint32_t reply_type;
159     uint8_t *reply;
160     int ret;
161     if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
162         if (ret == -1)
163             err(EXIT_FAILURE, "IPC: read()");
164         exit(1);
165     }
166     if (reply_type != message_type)
167         errx(EXIT_FAILURE, "IPC: Received reply of type %d but expected %d", reply_type, message_type);
168     printf("%.*s\n", reply_length, reply);
169     free(reply);
170
171     close(sockfd);
172
173     return 0;
174 }