2009-07-28 21:26:36 +02:00
|
|
|
|
/*
|
2011-07-13 17:53:07 +02:00
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
2009-07-28 21:26:36 +02:00
|
|
|
|
*
|
|
|
|
|
* i3 - an improved dynamic tiling window manager
|
2015-04-04 02:17:56 +02:00
|
|
|
|
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
|
2009-07-28 21:26:36 +02:00
|
|
|
|
*
|
|
|
|
|
* i3-msg/main.c: Utility which sends messages to a running i3-instance using
|
|
|
|
|
* IPC via UNIX domain sockets.
|
|
|
|
|
*
|
2011-10-02 19:33:10 +02:00
|
|
|
|
* This (in combination with libi3/ipc_send_message.c and
|
|
|
|
|
* libi3/ipc_recv_message.c) serves as an example for how to send your own
|
|
|
|
|
* messages to i3.
|
|
|
|
|
*
|
2009-07-28 21:26:36 +02:00
|
|
|
|
* Additionally, it’s even useful sometimes :-).
|
|
|
|
|
*
|
|
|
|
|
*/
|
2016-10-11 09:13:35 +02:00
|
|
|
|
#include "libi3.h"
|
|
|
|
|
|
2020-04-19 09:43:48 +02:00
|
|
|
|
#include <err.h>
|
|
|
|
|
#include <getopt.h>
|
|
|
|
|
#include <i3/ipc.h>
|
2010-03-12 15:30:09 +01:00
|
|
|
|
#include <stdbool.h>
|
2020-04-19 09:43:48 +02:00
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2009-07-28 21:26:36 +02:00
|
|
|
|
#include <unistd.h>
|
2013-01-26 17:56:43 +01:00
|
|
|
|
|
2020-04-19 09:43:48 +02:00
|
|
|
|
#include <yajl/yajl_parse.h>
|
2010-03-12 15:30:09 +01:00
|
|
|
|
|
2013-01-23 18:50:21 +01:00
|
|
|
|
/*
|
|
|
|
|
* Having verboselog() and errorlog() is necessary when using libi3.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
void verboselog(char *fmt, ...) {
|
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
vfprintf(stdout, fmt, args);
|
|
|
|
|
va_end(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void errorlog(char *fmt, ...) {
|
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
vfprintf(stderr, fmt, args);
|
|
|
|
|
va_end(args);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-26 17:56:43 +01:00
|
|
|
|
static char *last_key = NULL;
|
|
|
|
|
|
2009-07-28 21:26:36 +02:00
|
|
|
|
int main(int argc, char *argv[]) {
|
2016-01-14 10:06:34 +01:00
|
|
|
|
#if defined(__OpenBSD__)
|
|
|
|
|
if (pledge("stdio rpath unix", NULL) == -1)
|
|
|
|
|
err(EXIT_FAILURE, "pledge");
|
|
|
|
|
#endif
|
2017-11-26 16:41:59 +01:00
|
|
|
|
char *socket_path = NULL;
|
2011-07-13 17:53:07 +02:00
|
|
|
|
int o, option_index = 0;
|
2011-07-13 18:01:24 +02:00
|
|
|
|
char *payload = NULL;
|
2011-07-13 17:53:07 +02:00
|
|
|
|
|
|
|
|
|
static struct option long_options[] = {
|
|
|
|
|
{"socket", required_argument, 0, 's'},
|
|
|
|
|
{"version", no_argument, 0, 'v'},
|
|
|
|
|
{"help", no_argument, 0, 'h'},
|
2014-06-15 19:07:02 +02:00
|
|
|
|
{0, 0, 0, 0}};
|
2011-07-13 17:53:07 +02:00
|
|
|
|
|
2014-09-05 22:47:27 +02:00
|
|
|
|
char *options_string = "s:t:vhqm";
|
2011-07-13 17:53:07 +02:00
|
|
|
|
|
|
|
|
|
while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
|
|
|
|
|
if (o == 's') {
|
2017-11-26 16:41:59 +01:00
|
|
|
|
free(socket_path);
|
2011-10-02 19:11:01 +02:00
|
|
|
|
socket_path = sstrdup(optarg);
|
2011-07-13 17:53:07 +02:00
|
|
|
|
} else if (o == 'v') {
|
2020-05-10 01:33:55 +02:00
|
|
|
|
puts("i3-msg " I3_VERSION "\n");
|
2011-07-13 17:53:07 +02:00
|
|
|
|
return 0;
|
|
|
|
|
} else if (o == 'h') {
|
2020-05-10 01:33:55 +02:00
|
|
|
|
puts("i3-msg " I3_VERSION "\n");
|
|
|
|
|
puts("i3-msg [-s <socket>] [-m] <message>\n");
|
2011-07-13 17:53:07 +02:00
|
|
|
|
return 0;
|
2017-06-22 16:53:08 +02:00
|
|
|
|
} else if (o == '?') {
|
|
|
|
|
exit(EXIT_FAILURE);
|
2009-07-28 21:26:36 +02:00
|
|
|
|
}
|
2011-07-13 17:53:07 +02:00
|
|
|
|
}
|
2009-07-28 21:26:36 +02:00
|
|
|
|
|
2011-07-13 18:01:24 +02:00
|
|
|
|
/* Use all arguments, separated by whitespace, as payload.
|
|
|
|
|
* This way, you don’t have to do i3-msg 'mark foo', you can use
|
|
|
|
|
* i3-msg mark foo */
|
|
|
|
|
while (optind < argc) {
|
|
|
|
|
if (!payload) {
|
2011-10-02 19:11:01 +02:00
|
|
|
|
payload = sstrdup(argv[optind]);
|
2011-07-13 18:01:24 +02:00
|
|
|
|
} else {
|
|
|
|
|
char *both;
|
2015-08-03 11:50:50 +02:00
|
|
|
|
sasprintf(&both, "%s %s", payload, argv[optind]);
|
2011-07-13 18:01:24 +02:00
|
|
|
|
free(payload);
|
|
|
|
|
payload = both;
|
|
|
|
|
}
|
|
|
|
|
optind++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!payload)
|
2016-11-03 08:18:18 +01:00
|
|
|
|
payload = sstrdup("");
|
2009-07-28 21:26:36 +02:00
|
|
|
|
|
2017-11-26 16:41:59 +01:00
|
|
|
|
int sockfd = ipc_connect(socket_path);
|
2020-05-10 01:33:55 +02:00
|
|
|
|
if (ipc_send_message(sockfd, strlen(payload), (uint8_t *)payload) == -1)
|
2011-10-02 19:33:10 +02:00
|
|
|
|
err(EXIT_FAILURE, "IPC: write()");
|
2016-11-03 08:18:18 +01:00
|
|
|
|
free(payload);
|
2010-03-12 15:30:09 +01:00
|
|
|
|
|
2011-07-13 17:53:07 +02:00
|
|
|
|
uint32_t reply_length;
|
|
|
|
|
uint8_t *reply;
|
2011-10-02 19:33:10 +02:00
|
|
|
|
int ret;
|
2020-05-10 01:33:55 +02:00
|
|
|
|
if ((ret = ipc_recv_message(sockfd, &reply_length, &reply)) != 0) {
|
2011-10-02 19:33:10 +02:00
|
|
|
|
if (ret == -1)
|
|
|
|
|
err(EXIT_FAILURE, "IPC: read()");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2020-05-10 01:33:55 +02:00
|
|
|
|
puts(reply);
|
|
|
|
|
return 0;
|
2009-07-28 21:26:36 +02:00
|
|
|
|
}
|