]> git.sur5r.net Git - i3/i3/blob - i3-msg/main.c
Merge branch 'fix-move-ws'
[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 int main(int argc, char *argv[]) {
40     socket_path = getenv("I3SOCK");
41     int o, option_index = 0;
42     int message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
43     char *payload = NULL;
44     bool quiet = false;
45
46     static struct option long_options[] = {
47         {"socket", required_argument, 0, 's'},
48         {"type", required_argument, 0, 't'},
49         {"version", no_argument, 0, 'v'},
50         {"quiet", no_argument, 0, 'q'},
51         {"help", no_argument, 0, 'h'},
52         {0, 0, 0, 0}
53     };
54
55     char *options_string = "s:t:vhq";
56
57     while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
58         if (o == 's') {
59             if (socket_path != NULL)
60                 free(socket_path);
61             socket_path = sstrdup(optarg);
62         } else if (o == 't') {
63             if (strcasecmp(optarg, "command") == 0)
64                 message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
65             else if (strcasecmp(optarg, "get_workspaces") == 0)
66                 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
67             else if (strcasecmp(optarg, "get_outputs") == 0)
68                 message_type = I3_IPC_MESSAGE_TYPE_GET_OUTPUTS;
69             else if (strcasecmp(optarg, "get_tree") == 0)
70                 message_type = I3_IPC_MESSAGE_TYPE_GET_TREE;
71             else if (strcasecmp(optarg, "get_marks") == 0)
72                 message_type = I3_IPC_MESSAGE_TYPE_GET_MARKS;
73             else if (strcasecmp(optarg, "get_bar_config") == 0)
74                 message_type = I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG;
75             else if (strcasecmp(optarg, "get_version") == 0)
76                 message_type = I3_IPC_MESSAGE_TYPE_GET_VERSION;
77             else {
78                 printf("Unknown message type\n");
79                 printf("Known types: command, get_workspaces, get_outputs, get_tree, get_marks, get_bar_config, get_version\n");
80                 exit(EXIT_FAILURE);
81             }
82         } else if (o == 'q') {
83             quiet = true;
84         } else if (o == 'v') {
85             printf("i3-msg " I3_VERSION "\n");
86             return 0;
87         } else if (o == 'h') {
88             printf("i3-msg " I3_VERSION "\n");
89             printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
90             return 0;
91         }
92     }
93
94     if (socket_path == NULL)
95         socket_path = root_atom_contents("I3_SOCKET_PATH");
96
97     /* Fall back to the default socket path */
98     if (socket_path == NULL)
99         socket_path = sstrdup("/tmp/i3-ipc.sock");
100
101     /* Use all arguments, separated by whitespace, as payload.
102      * This way, you don’t have to do i3-msg 'mark foo', you can use
103      * i3-msg mark foo */
104     while (optind < argc) {
105         if (!payload) {
106             payload = sstrdup(argv[optind]);
107         } else {
108             char *both;
109             if (asprintf(&both, "%s %s", payload, argv[optind]) == -1)
110                 err(EXIT_FAILURE, "asprintf");
111             free(payload);
112             payload = both;
113         }
114         optind++;
115     }
116
117     if (!payload)
118         payload = "";
119
120     int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
121     if (sockfd == -1)
122         err(EXIT_FAILURE, "Could not create socket");
123
124     struct sockaddr_un addr;
125     memset(&addr, 0, sizeof(struct sockaddr_un));
126     addr.sun_family = AF_LOCAL;
127     strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
128     if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
129         err(EXIT_FAILURE, "Could not connect to i3");
130
131     if (ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t*)payload) == -1)
132         err(EXIT_FAILURE, "IPC: write()");
133
134     if (quiet)
135         return 0;
136
137     uint32_t reply_length;
138     uint8_t *reply;
139     int ret;
140     if ((ret = ipc_recv_message(sockfd, message_type, &reply_length, &reply)) != 0) {
141         if (ret == -1)
142             err(EXIT_FAILURE, "IPC: read()");
143         exit(1);
144     }
145     printf("%.*s\n", reply_length, reply);
146     free(reply);
147
148     close(sockfd);
149
150     return 0;
151 }