aboutsummaryrefslogtreecommitdiff
path: root/swaymsg
diff options
context:
space:
mode:
Diffstat (limited to 'swaymsg')
-rw-r--r--swaymsg/main.c82
-rw-r--r--swaymsg/swaymsg.1.scd11
2 files changed, 77 insertions, 16 deletions
diff --git a/swaymsg/main.c b/swaymsg/main.c
index 243b5fdc..e5eee631 100644
--- a/swaymsg/main.c
+++ b/swaymsg/main.c
@@ -1,4 +1,4 @@
-#define _XOPEN_SOURCE 500
+#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -12,7 +12,6 @@
#include <json-c/json.h>
#include "stringop.h"
#include "ipc-client.h"
-#include "readline.h"
#include "log.h"
void sway_terminate(int exit_code) {
@@ -32,6 +31,9 @@ static bool success_object(json_object *result) {
// Iterate results array and return false if any of them failed
static bool success(json_object *r, bool fallback) {
if (!json_object_is_type(r, json_type_array)) {
+ if (json_object_is_type(r, json_type_object)) {
+ return success_object(r);
+ }
return fallback;
}
@@ -111,7 +113,7 @@ static const char *pretty_type_name(const char *name) {
}
static void pretty_print_input(json_object *i) {
- json_object *id, *name, *type, *product, *vendor, *kbdlayout;
+ json_object *id, *name, *type, *product, *vendor, *kbdlayout, *events;
json_object_object_get_ex(i, "identifier", &id);
json_object_object_get_ex(i, "name", &name);
json_object_object_get_ex(i, "type", &type);
@@ -137,6 +139,10 @@ static void pretty_print_input(json_object *i) {
json_object_get_string(kbdlayout));
}
+ if (json_object_object_get_ex(i, "libinput_send_events", &events)) {
+ printf(" Libinput Send Events: %s\n", json_object_get_string(events));
+ }
+
printf("\n");
}
@@ -305,8 +311,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 +321,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 +334,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 +346,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 +412,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 +442,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;
}
diff --git a/swaymsg/swaymsg.1.scd b/swaymsg/swaymsg.1.scd
index eaac8105..f55f86a9 100644
--- a/swaymsg/swaymsg.1.scd
+++ b/swaymsg/swaymsg.1.scd
@@ -13,6 +13,12 @@ _swaymsg_ [options...] [message]
*-h, --help*
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*
Sends the IPC message but does not print the response from sway.
@@ -71,3 +77,8 @@ _swaymsg_ [options...] [message]
*send\_tick*
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.