]> 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 <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 if (strcasecmp(optarg, "get_version") == 0)
77                 message_type = I3_IPC_MESSAGE_TYPE_GET_VERSION;
78             else {
79                 printf("Unknown message type\n");
80                 printf("Known types: command, get_workspaces, get_outputs, get_tree, get_marks, get_bar_config, get_version\n");
81                 exit(EXIT_FAILURE);
82             }
83         } else if (o == 'q') {
84             quiet = true;
85         } else if (o == 'v') {
86             printf("i3-msg " I3_VERSION "\n");
87             return 0;
88         } else if (o == 'h') {
89             printf("i3-msg " I3_VERSION "\n");
90             printf("i3-msg [-s <socket>] [-t <type>] <message>\n");
91             return 0;
92         }
93     }
94
95     if (socket_path == NULL)
96         socket_path = root_atom_contents("I3_SOCKET_PATH");
97
98     /* Fall back to the default socket path */
99     if (socket_path == NULL)
100         socket_path = sstrdup("/tmp/i3-ipc.sock");
101
102     /* Use all arguments, separated by whitespace, as payload.
103      * This way, you don’t have to do i3-msg 'mark foo', you can use
104      * i3-msg mark foo */
105     while (optind < argc) {
106         if (!payload) {
107             payload = sstrdup(argv[optind]);
108         } else {
109             char *both;
110             if (asprintf(&both, "%s %s", payload, argv[optind]) == -1)
111                 err(EXIT_FAILURE, "asprintf");
112             free(payload);
113             payload = both;
114         }
115         optind++;
116     }
117
118     if (!payload)
119         payload = "";
120
121     int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
122     if (sockfd == -1)
123         err(EXIT_FAILURE, "Could not create socket");
124
125     struct sockaddr_un addr;
126     memset(&addr, 0, sizeof(struct sockaddr_un));
127     addr.sun_family = AF_LOCAL;
128     strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
129     if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
130         err(EXIT_FAILURE, "Could not connect to i3");
131
132     if (ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t*)payload) == -1)
133         err(EXIT_FAILURE, "IPC: write()");
134
135     if (quiet)
136         return 0;
137
138     uint32_t reply_length;
139     uint8_t *reply;
140     int ret;
141     if ((ret = ipc_recv_message(sockfd, message_type, &reply_length, &reply)) != 0) {
142         if (ret == -1)
143             err(EXIT_FAILURE, "IPC: read()");
144         exit(1);
145     }
146     printf("%.*s\n", reply_length, reply);
147     free(reply);
148
149     close(sockfd);
150
151     return 0;
152 }