]> git.sur5r.net Git - i3/i3/blob - i3-msg/main.c
add an IPC request to get the bar configuration (by ID)
[i3/i3] / i3-msg / main.c
1 /*
2  * vim:ts=4:sw=4: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 (in combination with libi3/ipc_send_message.c and
14  * libi3/ipc_recv_message.c) serves as an example for how to send your own
15  * messages to i3.
16  *
17  * Additionally, it’s even useful sometimes :-).
18  *
19  */
20 #include <ev.h>
21 #include <stdio.h>
22 #include <stdbool.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <err.h>
31 #include <stdint.h>
32 #include <getopt.h>
33 #include <limits.h>
34
35 #include <xcb/xcb.h>
36 #include <xcb/xcb_aux.h>
37
38 #include "libi3.h"
39 #include <i3/ipc.h>
40
41 static char *socket_path;
42
43 int main(int argc, char *argv[]) {
44     socket_path = getenv("I3SOCK");
45     int o, option_index = 0;
46     int message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
47     char *payload = NULL;
48     bool quiet = false;
49
50     static struct option long_options[] = {
51         {"socket", required_argument, 0, 's'},
52         {"type", required_argument, 0, 't'},
53         {"version", no_argument, 0, 'v'},
54         {"quiet", no_argument, 0, 'q'},
55         {"help", no_argument, 0, 'h'},
56         {0, 0, 0, 0}
57     };
58
59     char *options_string = "s:t:vhq";
60
61     while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
62         if (o == 's') {
63             if (socket_path != NULL)
64                 free(socket_path);
65             socket_path = sstrdup(optarg);
66         } else if (o == 't') {
67             if (strcasecmp(optarg, "command") == 0)
68                 message_type = I3_IPC_MESSAGE_TYPE_COMMAND;
69             else if (strcasecmp(optarg, "get_workspaces") == 0)
70                 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
71             else if (strcasecmp(optarg, "get_outputs") == 0)
72                 message_type = I3_IPC_MESSAGE_TYPE_GET_OUTPUTS;
73             else if (strcasecmp(optarg, "get_tree") == 0)
74                 message_type = I3_IPC_MESSAGE_TYPE_GET_TREE;
75             else if (strcasecmp(optarg, "get_marks") == 0)
76                 message_type = I3_IPC_MESSAGE_TYPE_GET_MARKS;
77             else if (strcasecmp(optarg, "get_bar_config") == 0)
78                 message_type = I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG;
79             else {
80                 printf("Unknown message type\n");
81                 printf("Known types: command, get_workspaces, get_outputs, get_tree, get_marks, get_bar_config\n");
82                 exit(EXIT_FAILURE);
83             }
84         } else if (o == 'q') {
85             quiet = true;
86         } else if (o == 'v') {
87             printf("i3-msg " I3_VERSION "\n");
88             return 0;
89         } else if (o == 'h') {
90             printf("i3-msg " I3_VERSION "\n");
91             printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
92             return 0;
93         }
94     }
95
96     if (socket_path == NULL)
97         socket_path = socket_path_from_x11();
98
99     /* Fall back to the default socket path */
100     if (socket_path == NULL)
101         socket_path = sstrdup("/tmp/i3-ipc.sock");
102
103     /* Use all arguments, separated by whitespace, as payload.
104      * This way, you don’t have to do i3-msg 'mark foo', you can use
105      * i3-msg mark foo */
106     while (optind < argc) {
107         if (!payload) {
108             payload = sstrdup(argv[optind]);
109         } else {
110             char *both;
111             if (asprintf(&both, "%s %s", payload, argv[optind]) == -1)
112                 err(EXIT_FAILURE, "asprintf");
113             free(payload);
114             payload = both;
115         }
116         optind++;
117     }
118
119     if (!payload)
120         payload = "";
121
122     int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
123     if (sockfd == -1)
124         err(EXIT_FAILURE, "Could not create socket");
125
126     struct sockaddr_un addr;
127     memset(&addr, 0, sizeof(struct sockaddr_un));
128     addr.sun_family = AF_LOCAL;
129     strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
130     if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
131         err(EXIT_FAILURE, "Could not connect to i3");
132
133     if (ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t*)payload) == -1)
134         err(EXIT_FAILURE, "IPC: write()");
135
136     if (quiet)
137         return 0;
138
139     uint32_t reply_length;
140     uint8_t *reply;
141     int ret;
142     if ((ret = ipc_recv_message(sockfd, message_type, &reply_length, &reply)) != 0) {
143         if (ret == -1)
144             err(EXIT_FAILURE, "IPC: read()");
145         exit(1);
146     }
147     printf("%.*s\n", reply_length, reply);
148     free(reply);
149
150     close(sockfd);
151
152     return 0;
153 }