sway/swaymsg/main.c

166 lines
4.2 KiB
C
Raw Normal View History

2015-11-26 17:41:24 +00:00
#include <stdio.h>
#include <stdlib.h>
2015-11-26 19:31:29 +00:00
#include <string.h>
#include <getopt.h>
#include <stdint.h>
2015-11-26 20:06:41 +00:00
#include <sys/un.h>
#include <sys/socket.h>
#include <unistd.h>
#include <json-c/json.h>
#include <json-c/printbuf.h>
2015-11-26 19:31:29 +00:00
#include "stringop.h"
#include "ipc-client.h"
2015-11-26 19:31:29 +00:00
#include "readline.h"
#include "log.h"
2015-11-26 17:41:24 +00:00
void sway_terminate(void) {
2015-11-28 13:47:44 +00:00
exit(EXIT_FAILURE);
2015-11-26 17:41:24 +00:00
}
void scrub_escaped_forward_slash(const char *line) {
char *sp;
const char *search = "\\/";
const char *replace = "/";
int search_len = strlen(search);
int replace_len = strlen(replace);
// this is only safe because replace_len < search_len
while ((sp = strstr(line, search)) != NULL) {
int tail_len = strlen(sp+search_len);
memmove(sp+replace_len, sp+search_len, tail_len+1);
memcpy(sp, replace, replace_len);
}
};
2015-11-26 19:31:29 +00:00
int main(int argc, char **argv) {
static int quiet = 0;
static int pretty = 0;
2015-11-26 19:31:29 +00:00
char *socket_path = NULL;
char *cmdtype = NULL;
2015-11-26 20:06:41 +00:00
init_log(L_INFO);
2015-11-26 19:31:29 +00:00
static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"quiet", no_argument, NULL, 'q'},
2015-11-26 19:31:29 +00:00
{"version", no_argument, NULL, 'v'},
{"socket", required_argument, NULL, 's'},
{"type", required_argument, NULL, 't'},
{0, 0, 0, 0}
};
const char *usage =
"Usage: swaymsg [options] [message]\n"
"\n"
" -h, --help Show help message and quit.\n"
" -p, --pretty Decode the JSON response and format it.\n"
" -q, --quiet Be quiet.\n"
" -v, --version Show the version number and quit.\n"
" -s, --socket <socket> Use the specified socket.\n"
" -t, --type <type> Specify the message type.\n";
2015-11-26 19:31:29 +00:00
int c;
while (1) {
int option_index = 0;
c = getopt_long(argc, argv, "hpqvs:t:", long_options, &option_index);
2015-11-26 19:31:29 +00:00
if (c == -1) {
break;
}
switch (c) {
case 'q': // Quiet
quiet = 1;
2015-11-26 19:31:29 +00:00
break;
case 'p':
pretty = 1;
break;
2015-11-26 19:31:29 +00:00
case 's': // Socket
socket_path = strdup(optarg);
break;
case 't': // Type
cmdtype = strdup(optarg);
break;
case 'v':
#if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE
fprintf(stdout, "sway version %s (%s, branch \"%s\")\n", SWAY_GIT_VERSION, SWAY_VERSION_DATE, SWAY_GIT_BRANCH);
#else
fprintf(stdout, "version not detected\n");
#endif
2015-11-28 13:47:44 +00:00
exit(EXIT_SUCCESS);
2015-11-26 19:31:29 +00:00
break;
default:
fprintf(stderr, "%s", usage);
exit(EXIT_FAILURE);
2015-11-26 19:31:29 +00:00
}
}
if (!cmdtype) {
2015-11-26 20:06:41 +00:00
cmdtype = strdup("command");
2015-11-26 19:31:29 +00:00
}
if (!socket_path) {
socket_path = get_socketpath();
if (!socket_path) {
sway_abort("Unable to retrieve socket path");
}
}
2015-11-27 14:42:24 +00:00
uint32_t type = IPC_COMMAND;
2015-11-26 19:31:29 +00:00
if (strcasecmp(cmdtype, "command") == 0) {
type = IPC_COMMAND;
} else if (strcasecmp(cmdtype, "get_workspaces") == 0) {
type = IPC_GET_WORKSPACES;
2016-01-17 10:53:37 +00:00
} else if (strcasecmp(cmdtype, "get_inputs") == 0) {
type = IPC_GET_INPUTS;
2015-11-26 19:31:29 +00:00
} else if (strcasecmp(cmdtype, "get_outputs") == 0) {
type = IPC_GET_OUTPUTS;
} else if (strcasecmp(cmdtype, "get_tree") == 0) {
type = IPC_GET_TREE;
} else if (strcasecmp(cmdtype, "get_marks") == 0) {
type = IPC_GET_MARKS;
} else if (strcasecmp(cmdtype, "get_bar_config") == 0) {
type = IPC_GET_BAR_CONFIG;
} else if (strcasecmp(cmdtype, "get_version") == 0) {
type = IPC_GET_VERSION;
2015-11-27 14:42:24 +00:00
} else {
sway_abort("Unknown message type %s", cmdtype);
2015-11-26 19:31:29 +00:00
}
2015-11-26 20:06:41 +00:00
free(cmdtype);
2015-11-26 19:31:29 +00:00
char *command = strdup("");
if (optind < argc) {
command = join_args(argv + optind, argc - optind);
}
int socketfd = ipc_open_socket(socket_path);
uint32_t len = strlen(command);
char *resp = ipc_single_command(socketfd, type, command, &len);
if (pretty) {
struct printbuf *pb;
if(!(pb = printbuf_new())) {
return -1;
}
printbuf_memappend(pb, resp, sizeof(char)*strlen(resp));
struct json_object *obj = json_tokener_parse(pb->buf);
const char *resp_json = json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PRETTY);
// Temporary workaround until json-c has a new release
// at which point json_object_to_json_string_ext has a flag that
// prevents the need for the following line.
scrub_escaped_forward_slash(resp_json);
printf("%s\n", resp_json);
free((char*)resp_json);
printbuf_free(pb);
} else if (!quiet) {
2015-12-02 20:40:50 +00:00
printf("%s\n", resp);
2015-11-26 20:06:41 +00:00
}
close(socketfd);
2015-11-26 20:06:41 +00:00
free(command);
free(resp);
2015-11-26 19:31:29 +00:00
free(socket_path);
2015-11-26 17:41:24 +00:00
return 0;
}