gri3-wm/i3-msg/main.c

120 lines
3.0 KiB
C
Raw Normal View History

/*
2011-07-13 17:53:07 +02:00
* vim:ts=4:sw=4:expandtab
*
* i3 - an improved dynamic tiling window manager
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
*
* i3-msg/main.c: Utility which sends messages to a running i3-instance using
* IPC via UNIX domain sockets.
*
* 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.
*
* Additionally, its even useful sometimes :-).
*
*/
#include "libi3.h"
#include <err.h>
#include <getopt.h>
#include <i3/ipc.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <yajl/yajl_parse.h>
/*
* 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);
}
static char *last_key = NULL;
int main(int argc, char *argv[]) {
#if defined(__OpenBSD__)
if (pledge("stdio rpath unix", NULL) == -1)
err(EXIT_FAILURE, "pledge");
#endif
char *socket_path = NULL;
2011-07-13 17:53:07 +02:00
int o, option_index = 0;
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'},
{0, 0, 0, 0}};
2011-07-13 17:53:07 +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') {
free(socket_path);
socket_path = sstrdup(optarg);
2011-07-13 17:53:07 +02:00
} else if (o == 'v') {
puts("i3-msg " I3_VERSION "\n");
2011-07-13 17:53:07 +02:00
return 0;
} else if (o == 'h') {
puts("i3-msg " I3_VERSION "\n");
puts("i3-msg [-s <socket>] [-m] <message>\n");
2011-07-13 17:53:07 +02:00
return 0;
} else if (o == '?') {
exit(EXIT_FAILURE);
}
2011-07-13 17:53:07 +02:00
}
/* Use all arguments, separated by whitespace, as payload.
* This way, you dont have to do i3-msg 'mark foo', you can use
* i3-msg mark foo */
while (optind < argc) {
if (!payload) {
payload = sstrdup(argv[optind]);
} else {
char *both;
2015-08-03 11:50:50 +02:00
sasprintf(&both, "%s %s", payload, argv[optind]);
free(payload);
payload = both;
}
optind++;
}
if (!payload)
2016-11-03 08:18:18 +01:00
payload = sstrdup("");
int sockfd = ipc_connect(socket_path);
if (ipc_send_message(sockfd, strlen(payload), (uint8_t *)payload) == -1)
err(EXIT_FAILURE, "IPC: write()");
2016-11-03 08:18:18 +01:00
free(payload);
2011-07-13 17:53:07 +02:00
uint32_t reply_length;
uint8_t *reply;
int ret;
if ((ret = ipc_recv_message(sockfd, &reply_length, &reply)) != 0) {
if (ret == -1)
err(EXIT_FAILURE, "IPC: read()");
exit(1);
}
2020-05-12 14:56:06 +02:00
printf("%s", reply);
return 0;
}