Merge pull request #3206 from RedSoxFan/ipc-subscribe

Implement support for swaymsg -t SUBSCRIBE [-m]
This commit is contained in:
emersion 2018-11-28 17:58:36 +01:00 committed by GitHub
commit 67c7cc53ae
Failed to generate hash of commit
3 changed files with 76 additions and 18 deletions

View file

@ -668,7 +668,8 @@ void ipc_client_handle_command(struct ipc_client *client) {
// TODO: Check if they're permitted to use these events // TODO: Check if they're permitted to use these events
struct json_object *request = json_tokener_parse(buf); struct json_object *request = json_tokener_parse(buf);
if (request == NULL) { if (request == NULL) {
client_valid = ipc_send_reply(client, "{\"success\": false}", 18); const char msg[] = "[{\"success\": false}]";
client_valid = ipc_send_reply(client, msg, strlen(msg));
wlr_log(WLR_INFO, "Failed to parse subscribe request"); wlr_log(WLR_INFO, "Failed to parse subscribe request");
goto exit_cleanup; goto exit_cleanup;
} }
@ -695,8 +696,8 @@ void ipc_client_handle_command(struct ipc_client *client) {
client->subscribed_events |= event_mask(IPC_EVENT_TICK); client->subscribed_events |= event_mask(IPC_EVENT_TICK);
is_tick = true; is_tick = true;
} else { } else {
client_valid = const char msg[] = "[{\"success\": false}]";
ipc_send_reply(client, "{\"success\": false}", 18); client_valid = ipc_send_reply(client, msg, strlen(msg));
json_object_put(request); json_object_put(request);
wlr_log(WLR_INFO, "Unsupported event type in subscribe request"); wlr_log(WLR_INFO, "Unsupported event type in subscribe request");
goto exit_cleanup; goto exit_cleanup;
@ -704,10 +705,12 @@ void ipc_client_handle_command(struct ipc_client *client) {
} }
json_object_put(request); json_object_put(request);
client_valid = ipc_send_reply(client, "{\"success\": true}", 17); const char msg[] = "[{\"success\": true}]";
client_valid = ipc_send_reply(client, msg, strlen(msg));
if (is_tick) { if (is_tick) {
client->current_command = IPC_EVENT_TICK; client->current_command = IPC_EVENT_TICK;
ipc_send_reply(client, "{\"first\": true, \"payload\": \"\"}", 30); const char tickmsg[] = "{\"first\": true, \"payload\": \"\"}";
ipc_send_reply(client, tickmsg, strlen(tickmsg));
} }
goto exit_cleanup; goto exit_cleanup;
} }

View file

@ -305,8 +305,9 @@ static void pretty_print(int type, json_object *resp) {
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
static int quiet = 0; static bool quiet = false;
static int raw = 0; static bool raw = false;
static bool monitor = false;
char *socket_path = NULL; char *socket_path = NULL;
char *cmdtype = NULL; char *cmdtype = NULL;
@ -314,6 +315,7 @@ int main(int argc, char **argv) {
static struct option long_options[] = { static struct option long_options[] = {
{"help", no_argument, NULL, 'h'}, {"help", no_argument, NULL, 'h'},
{"monitor", no_argument, NULL, 'm'},
{"quiet", no_argument, NULL, 'q'}, {"quiet", no_argument, NULL, 'q'},
{"raw", no_argument, NULL, 'r'}, {"raw", no_argument, NULL, 'r'},
{"socket", required_argument, NULL, 's'}, {"socket", required_argument, NULL, 's'},
@ -326,6 +328,7 @@ int main(int argc, char **argv) {
"Usage: swaymsg [options] [message]\n" "Usage: swaymsg [options] [message]\n"
"\n" "\n"
" -h, --help Show help message and quit.\n" " -h, --help Show help message and quit.\n"
" -m, --monitor Monitor until killed (-t SUBSCRIBE only)\n"
" -q, --quiet Be quiet.\n" " -q, --quiet Be quiet.\n"
" -r, --raw Use raw output even if using a tty\n" " -r, --raw Use raw output even if using a tty\n"
" -s, --socket <socket> Use the specified socket.\n" " -s, --socket <socket> Use the specified socket.\n"
@ -337,16 +340,19 @@ int main(int argc, char **argv) {
int c; int c;
while (1) { while (1) {
int option_index = 0; 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) { if (c == -1) {
break; break;
} }
switch (c) { switch (c) {
case 'm': // Monitor
monitor = true;
break;
case 'q': // Quiet case 'q': // Quiet
quiet = 1; quiet = true;
break; break;
case 'r': // Raw case 'r': // Raw
raw = 1; raw = true;
break; break;
case 's': // Socket case 's': // Socket
socket_path = strdup(optarg); socket_path = strdup(optarg);
@ -400,12 +406,20 @@ int main(int argc, char **argv) {
type = IPC_GET_CONFIG; type = IPC_GET_CONFIG;
} else if (strcasecmp(cmdtype, "send_tick") == 0) { } else if (strcasecmp(cmdtype, "send_tick") == 0) {
type = IPC_SEND_TICK; type = IPC_SEND_TICK;
} else if (strcasecmp(cmdtype, "subscribe") == 0) {
type = IPC_SUBSCRIBE;
} else { } else {
sway_abort("Unknown message type %s", cmdtype); sway_abort("Unknown message type %s", cmdtype);
} }
free(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; char *command = NULL;
if (optind < argc) { if (optind < argc) {
command = join_args(argv + optind, argc - optind); command = join_args(argv + optind, argc - optind);
@ -422,26 +436,56 @@ int main(int argc, char **argv) {
json_object *obj = json_tokener_parse(resp); json_object *obj = json_tokener_parse(resp);
if (obj == NULL) { 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); printf("%s\n", resp);
ret = 1; ret = 1;
} else { } else {
if (!success(obj, true)) { if (!success(obj, true)) {
ret = 1; ret = 1;
} }
if (raw) { if (type != IPC_SUBSCRIBE || ret != 0) {
printf("%s\n", json_object_to_json_string_ext(obj, if (raw) {
JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); printf("%s\n", json_object_to_json_string_ext(obj,
} else { JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED));
pretty_print(type, obj); } else {
pretty_print(type, obj);
}
} }
json_object_put(obj); json_object_put(obj);
} }
} }
close(socketfd);
free(command); free(command);
free(resp); 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); free(socket_path);
return ret; return ret;
} }

View file

@ -13,6 +13,12 @@ _swaymsg_ [options...] [message]
*-h, --help* *-h, --help*
Show help message and quit. Show help message and quit.
*-m, --monitor*
Monitor for responses until killed instead of exiting after the first
response. This can only be used with the IPC message type _subscribe_. If
there is a malformed response or an invalid event type was requested,
swaymsg will stop monitoring and exit.
*-q, --quiet* *-q, --quiet*
Sends the IPC message but does not print the response from sway. Sends the IPC message but does not print the response from sway.
@ -71,3 +77,8 @@ _swaymsg_ [options...] [message]
*send\_tick* *send\_tick*
Sends a tick event to all subscribed clients. Sends a tick event to all subscribed clients.
*subscribe*
Subscribe to a list of event types. The argument for this type should be
provided in the form of a valid JSON array. If any of the types are invalid
or if an valid JSON array is not provided, this will result in an failure.