diff options
author | Brian Ashworth <[email protected]> | 2018-11-28 11:19:18 -0500 |
---|---|---|
committer | Brian Ashworth <[email protected]> | 2018-11-28 11:19:18 -0500 |
commit | bf9a52bab0f8ae9b4ace43c7d9c75ece0c76b562 (patch) | |
tree | 812af4f8fee1f289bc244136ac21acca44de15b0 /swaymsg/main.c | |
parent | a22d0c0ff60469d57de733bb767333d5b222df2d (diff) |
Implement support for swaymsg -t SUBSCRIBE [-m]
In `i3 4.16`, `i3-msg` can be used with the message type `subscribe`
and has the ability to monitor for responses until killed. This adds
support for both to swaymsg.
If the JSON array of event types is malformed or contains an invalid
event, sway will send a response with `success` set to `false`. If
swaymsg sees this, it will not display the failure and exit.
If the `subscribe` event is successful, swaymsg will wait for the first
response and display that instead of the success message. If
`-m/--monitor` is given, swaymsg will continue monitor for responses
until killed or a malformed response is received.
For the `subscribe` event, the responses will always be printed as JSON.
If `-r/--raw` is given, the JSON will not be pretty printed, which may
be preferred when monitoring due to there being multiple responses.
Example: `swaymsg -t SUBSCRIBE -m "['window']"`
Diffstat (limited to 'swaymsg/main.c')
-rw-r--r-- | swaymsg/main.c | 70 |
1 files changed, 57 insertions, 13 deletions
diff --git a/swaymsg/main.c b/swaymsg/main.c index e640cadf..3e61b94a 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -305,8 +305,9 @@ static void pretty_print(int type, json_object *resp) { } int main(int argc, char **argv) { - static int quiet = 0; - static int raw = 0; + static bool quiet = false; + static bool raw = false; + static bool monitor = false; char *socket_path = NULL; char *cmdtype = NULL; @@ -314,6 +315,7 @@ int main(int argc, char **argv) { static struct option long_options[] = { {"help", no_argument, NULL, 'h'}, + {"monitor", no_argument, NULL, 'm'}, {"quiet", no_argument, NULL, 'q'}, {"raw", no_argument, NULL, 'r'}, {"socket", required_argument, NULL, 's'}, @@ -326,6 +328,7 @@ int main(int argc, char **argv) { "Usage: swaymsg [options] [message]\n" "\n" " -h, --help Show help message and quit.\n" + " -m, --monitor Monitor until killed (-t SUBSCRIBE only)\n" " -q, --quiet Be quiet.\n" " -r, --raw Use raw output even if using a tty\n" " -s, --socket <socket> Use the specified socket.\n" @@ -337,16 +340,19 @@ int main(int argc, char **argv) { int c; while (1) { int option_index = 0; - c = getopt_long(argc, argv, "hqrs:t:v", long_options, &option_index); + c = getopt_long(argc, argv, "hmqrs:t:v", long_options, &option_index); if (c == -1) { break; } switch (c) { + case 'm': // Monitor + monitor = true; + break; case 'q': // Quiet - quiet = 1; + quiet = true; break; case 'r': // Raw - raw = 1; + raw = true; break; case 's': // Socket socket_path = strdup(optarg); @@ -400,12 +406,20 @@ int main(int argc, char **argv) { type = IPC_GET_CONFIG; } else if (strcasecmp(cmdtype, "send_tick") == 0) { type = IPC_SEND_TICK; + } else if (strcasecmp(cmdtype, "subscribe") == 0) { + type = IPC_SUBSCRIBE; } else { sway_abort("Unknown message type %s", cmdtype); } free(cmdtype); + if (monitor && type != IPC_SUBSCRIBE) { + wlr_log(WLR_ERROR, "Monitor can only be used with -t SUBSCRIBE"); + free(socket_path); + return 1; + } + char *command = NULL; if (optind < argc) { command = join_args(argv + optind, argc - optind); @@ -422,26 +436,56 @@ int main(int argc, char **argv) { json_object *obj = json_tokener_parse(resp); if (obj == NULL) { - fprintf(stderr, "ERROR: Could not parse json response from ipc. This is a bug in sway."); + fprintf(stderr, "ERROR: Could not parse json response from ipc. " + "This is a bug in sway."); printf("%s\n", resp); ret = 1; } else { if (!success(obj, true)) { ret = 1; } - if (raw) { - printf("%s\n", json_object_to_json_string_ext(obj, - JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); - } else { - pretty_print(type, obj); + if (type != IPC_SUBSCRIBE || ret != 0) { + if (raw) { + printf("%s\n", json_object_to_json_string_ext(obj, + JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); + } else { + pretty_print(type, obj); + } } json_object_put(obj); } } - close(socketfd); - free(command); free(resp); + + if (type == IPC_SUBSCRIBE && ret == 0) { + do { + struct ipc_response *reply = ipc_recv_response(socketfd); + if (!reply) { + break; + } + + json_object *obj = json_tokener_parse(reply->payload); + if (obj == NULL) { + fprintf(stderr, "ERROR: Could not parse json response from ipc" + ". This is a bug in sway."); + ret = 1; + break; + } else { + if (raw) { + printf("%s\n", json_object_to_json_string(obj)); + } else { + printf("%s\n", json_object_to_json_string_ext(obj, + JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); + } + json_object_put(obj); + } + + free_ipc_response(reply); + } while (monitor); + } + + close(socketfd); free(socket_path); return ret; } |