diff options
| author | Drew DeVault <sir@cmpwn.com> | 2017-10-08 11:05:54 -0400 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-10-08 11:05:54 -0400 | 
| commit | 6d83a59b461d07c4c9c5ee5809da658f5c56afc3 (patch) | |
| tree | 6fd6b55d7f335a3fb4a2f06d81417464087f1dfc /swaymsg | |
| parent | 5ecedc7199fcd9788ae2d96f0ed1f25a2cbe7364 (diff) | |
| parent | 1cca551c6fc69bca76a4992b33ef685371cac26b (diff) | |
| download | sway-6d83a59b461d07c4c9c5ee5809da658f5c56afc3.tar.xz | |
Merge pull request #1263 from nyorain/master
Implement get_clipboard ipc message
Diffstat (limited to 'swaymsg')
| -rw-r--r-- | swaymsg/main.c | 57 | ||||
| -rw-r--r-- | swaymsg/swaymsg.1.txt | 6 | 
2 files changed, 57 insertions, 6 deletions
diff --git a/swaymsg/main.c b/swaymsg/main.c index efd0ec25..2f9cfb14 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -19,15 +19,17 @@ void sway_terminate(int exit_code) {  	exit(exit_code);  } -static void pretty_print_cmd(json_object *r) { -	bool _success; +static bool success(json_object *r, bool fallback) {  	json_object *success;  	if (!json_object_object_get_ex(r, "success", &success)) { -		_success = true; +		return fallback;  	} else { -		_success = json_object_get_boolean(success); +		return json_object_get_boolean(success);  	} -	if (!_success) { +} + +static void pretty_print_cmd(json_object *r) { +	if (!success(r, true)) {  		json_object *error;  		if (!json_object_object_get_ex(r, "error", &error)) {  			printf("An unknkown error occured"); @@ -144,10 +146,43 @@ static void pretty_print_version(json_object *v) {  	printf("sway version %s\n", json_object_get_string(ver));  } +static void pretty_print_clipboard(json_object *v) { +	if (success(v, true)) { +		if (json_object_is_type(v, json_type_array)) { +			for (int i = 0; i < json_object_array_length(v); ++i) { +				json_object *o = json_object_array_get_idx(v, i); +				printf("%s\n", json_object_get_string(o)); +			} +		} else { +			// NOTE: could be extended to print all received types +			// instead just the first one when sways ipc server +			// supports it +			struct json_object_iterator iter = json_object_iter_begin(v); +			struct json_object_iterator end = json_object_iter_end(v); +			if (!json_object_iter_equal(&iter, &end)) { +				json_object *obj = json_object_iter_peek_value(&iter); +				if (success(obj, false)) { +					json_object *content; +					json_object_object_get_ex(obj, "content", &content); +					printf("%s\n", json_object_get_string(content)); +				} else { +					json_object *error; +					json_object_object_get_ex(obj, "error", &error); +					printf("Error: %s\n", json_object_get_string(error)); +				} +			} +		} +	} else { +		json_object *error; +		json_object_object_get_ex(v, "error", &error); +		printf("Error: %s\n", json_object_get_string(error)); +	} +} +  static void pretty_print(int type, json_object *resp) {  	if (type != IPC_COMMAND && type != IPC_GET_WORKSPACES &&  			type != IPC_GET_INPUTS && type != IPC_GET_OUTPUTS && -			type != IPC_GET_VERSION) { +			type != IPC_GET_VERSION && type != IPC_GET_CLIPBOARD) {  		printf("%s\n", json_object_to_json_string_ext(resp,  			JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED));  		return; @@ -158,6 +193,11 @@ static void pretty_print(int type, json_object *resp) {  		return;  	} +	if (type == IPC_GET_CLIPBOARD) { +		pretty_print_clipboard(resp); +		return; +	} +  	json_object *obj;  	size_t len = json_object_array_length(resp);  	for (size_t i = 0; i < len; ++i) { @@ -267,6 +307,8 @@ int main(int argc, char **argv) {  		type = IPC_GET_BAR_CONFIG;  	} else if (strcasecmp(cmdtype, "get_version") == 0) {  		type = IPC_GET_VERSION; +	} else if (strcasecmp(cmdtype, "get_clipboard") == 0) { +		type = IPC_GET_CLIPBOARD;  	} else {  		sway_abort("Unknown message type %s", cmdtype);  	} @@ -290,6 +332,9 @@ int main(int argc, char **argv) {  			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)); diff --git a/swaymsg/swaymsg.1.txt b/swaymsg/swaymsg.1.txt index 1466a0fa..27813588 100644 --- a/swaymsg/swaymsg.1.txt +++ b/swaymsg/swaymsg.1.txt @@ -65,6 +65,12 @@ IPC Message Types  *get_version*::  	Get JSON-encoded version information for the running instance of sway. +*get_clipboard*:: +	Get JSON-encoded information about the clipboard. +	Returns the current clipboard mime-types if called without +	arguments, otherwise returns the clipboard data in the requested +	formats. Encodes the data using base64 for non-text mime types. +  Authors  -------  | 
