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