From 02c75ebe37d50ff97c4fd8b6be169ac4e1074b07 Mon Sep 17 00:00:00 2001 From: nyorain Date: Fri, 7 Jul 2017 15:38:45 +0200 Subject: Implement ipc get_clipboard --- sway/commands/ipc.c | 1 + sway/ipc-server.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) (limited to 'sway') diff --git a/sway/commands/ipc.c b/sway/commands/ipc.c index f0b3035a..0c678961 100644 --- a/sway/commands/ipc.c +++ b/sway/commands/ipc.c @@ -89,6 +89,7 @@ struct cmd_results *cmd_ipc_cmd(int argc, char **argv) { { "marks", IPC_FEATURE_GET_MARKS }, { "bar-config", IPC_FEATURE_GET_BAR_CONFIG }, { "inputs", IPC_FEATURE_GET_INPUTS }, + { "clipboard", IPC_FEATURE_GET_CLIPBOARD }, }; uint32_t type = 0; diff --git a/sway/ipc-server.c b/sway/ipc-server.c index dca881fa..5bc64b1f 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -55,6 +55,11 @@ struct get_pixels_request { struct wlc_geometry geo; }; +struct get_clipboard_request { + struct ipc_client *client; + struct wlc_event_source *event_source; +}; + struct sockaddr_un *ipc_user_sockaddr(void); int ipc_handle_connection(int fd, uint32_t mask, void *data); int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data); @@ -322,6 +327,46 @@ void ipc_get_pixels(wlc_handle output) { ipc_get_pixel_requests = unhandled; } +static int ipc_selection_data_cb(int fd, uint32_t mask, void *data) +{ + assert(data); + struct get_clipboard_request *req = (struct get_clipboard_request*) data; + + if (mask & WLC_EVENT_ERROR) { + sway_log(L_ERROR, "Selection data fd error"); + const char *error = "{ \"success\": false, \"error\": " + "\"Could not receive text data from clipboard\" }"; + ipc_send_reply(req->client, error, (uint32_t)strlen(error)); + goto cleanup; + } + + if (mask & WLC_EVENT_READABLE) { + char buf[512]; + int ret = read(fd, buf, 511); + if (ret < 0) { + sway_log_errno(L_ERROR, "Reading from selection data fd failed"); + const char *error = "{ \"success\": false, \"error\": " + "\"Could not receive text data from clipboard\" }"; + ipc_send_reply(req->client, error, (uint32_t)strlen(error)); + goto cleanup; + } + + buf[ret] = '\0'; + json_object *obj = json_object_new_object(); + json_object_object_add(obj, "success", json_object_new_boolean(true)); + json_object_object_add(obj, "content", json_object_new_string(buf)); + const char *str = json_object_to_json_string(obj); + ipc_send_reply(req->client, str, (uint32_t)strlen(str)); + json_object_put(obj); + } + +cleanup: + wlc_event_source_remove(req->event_source); + close(fd); + free(req); + return 0; +} + void ipc_client_handle_command(struct ipc_client *client) { if (!sway_assert(client != NULL, "client != NULL")) { return; @@ -565,6 +610,70 @@ void ipc_client_handle_command(struct ipc_client *client) { goto exit_cleanup; } + case IPC_GET_CLIPBOARD: + { + if (!(client->security_policy & IPC_FEATURE_GET_CLIPBOARD)) { + goto exit_denied; + } + + size_t size; + bool found = false; + const char **types = wlc_get_selection_types(&size); + if (types == NULL || size == 0) { + const char *error = "{ \"success\": false, \"error\": " + "\"Empty clipboard\" }"; + ipc_send_reply(client, error, (uint32_t)strlen(error)); + goto exit_cleanup; + } + + for (size_t i = 0; i < size; ++i) { + if (strcmp(types[i], "text/plain;charset=utf-8") != 0 + && strcmp(types[i], "text/plain") != 0) { + continue; + } + + struct get_clipboard_request *req = malloc(sizeof(*req)); + if (!req) { + sway_log(L_ERROR, "Unable to allocate get_clipboard_request"); + goto exit_cleanup; + } + + int pipes[2]; + if (pipe(pipes) == -1) { + sway_log_errno(L_ERROR, "pipe call failed"); + free(req); + break; + } + + fcntl(pipes[0], F_SETFD, FD_CLOEXEC | O_NONBLOCK); + fcntl(pipes[1], F_SETFD, FD_CLOEXEC | O_NONBLOCK); + if (!wlc_get_selection_data(types[i], pipes[1])) { + close(pipes[0]); + close(pipes[1]); + free(req); + sway_log(L_ERROR, "wlc_get_selection_data failed"); + break; + } + + req->client = client; + req->event_source = wlc_event_loop_add_fd(pipes[0], + WLC_EVENT_READABLE | WLC_EVENT_ERROR | WLC_EVENT_HANGUP, + &ipc_selection_data_cb, req); + found = true; + break; + } + + if (!found) { + sway_log(L_INFO, "Clipboard has to text data"); + const char *error = "{ \"success\": false, \"error\": " + "\"Could not receive text data from clipboard\" }"; + ipc_send_reply(client, error, (uint32_t)strlen(error)); + } + + free(types); + goto exit_cleanup; + } + default: sway_log(L_INFO, "Unknown IPC command type %i", client->current_command); goto exit_cleanup; -- cgit v1.2.3 From 42547cafb64be99debf5cbcd5986e2c1fa30216a Mon Sep 17 00:00:00 2001 From: nyorain Date: Fri, 7 Jul 2017 15:46:17 +0200 Subject: Fix/Simplify get_clipboard ipc-server impl --- sway/ipc-server.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'sway') diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 5bc64b1f..b6268404 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -617,8 +617,8 @@ void ipc_client_handle_command(struct ipc_client *client) { } size_t size; - bool found = false; const char **types = wlc_get_selection_types(&size); + const char *type = NULL; if (types == NULL || size == 0) { const char *error = "{ \"success\": false, \"error\": " "\"Empty clipboard\" }"; @@ -627,48 +627,50 @@ void ipc_client_handle_command(struct ipc_client *client) { } for (size_t i = 0; i < size; ++i) { - if (strcmp(types[i], "text/plain;charset=utf-8") != 0 - && strcmp(types[i], "text/plain") != 0) { - continue; + if (strcmp(types[i], "text/plain;charset=utf-8") == 0 + || strcmp(types[i], "text/plain") == 0) { + type = types[i]; + break; } + } + if (type) { struct get_clipboard_request *req = malloc(sizeof(*req)); if (!req) { sway_log(L_ERROR, "Unable to allocate get_clipboard_request"); - goto exit_cleanup; + goto clipboard_error; } int pipes[2]; if (pipe(pipes) == -1) { sway_log_errno(L_ERROR, "pipe call failed"); free(req); - break; + goto clipboard_error; } fcntl(pipes[0], F_SETFD, FD_CLOEXEC | O_NONBLOCK); fcntl(pipes[1], F_SETFD, FD_CLOEXEC | O_NONBLOCK); - if (!wlc_get_selection_data(types[i], pipes[1])) { + if (!wlc_get_selection_data(type, pipes[1])) { close(pipes[0]); close(pipes[1]); free(req); sway_log(L_ERROR, "wlc_get_selection_data failed"); - break; + goto clipboard_error; } req->client = client; req->event_source = wlc_event_loop_add_fd(pipes[0], WLC_EVENT_READABLE | WLC_EVENT_ERROR | WLC_EVENT_HANGUP, &ipc_selection_data_cb, req); - found = true; - break; + free(types); + goto exit_cleanup; } - if (!found) { - sway_log(L_INFO, "Clipboard has to text data"); - const char *error = "{ \"success\": false, \"error\": " - "\"Could not receive text data from clipboard\" }"; - ipc_send_reply(client, error, (uint32_t)strlen(error)); - } +clipboard_error: + sway_log(L_INFO, "Clipboard has to text data"); + const char *error = "{ \"success\": false, \"error\": " + "\"Could not receive text data from clipboard\" }"; + ipc_send_reply(client, error, (uint32_t)strlen(error)); free(types); goto exit_cleanup; -- cgit v1.2.3 From c0f2acce4e08eecdcf29058335aece113467daee Mon Sep 17 00:00:00 2001 From: nyorain Date: Fri, 7 Jul 2017 21:51:34 +0200 Subject: Rework get_clipboard implementation --- completions/zsh/_swaymsg | 1 + include/sway/config.h | 2 +- include/util.h | 5 + sway/CMakeLists.txt | 3 +- sway/base64.c | 221 +++++++++++++++++++++++++++++++++++++ sway/ipc-server.c | 275 ++++++++++++++++++++++++++++++++--------------- swaymsg/main.c | 41 ++++--- swaymsg/swaymsg.1.txt | 6 ++ 8 files changed, 453 insertions(+), 101 deletions(-) create mode 100644 sway/base64.c (limited to 'sway') diff --git a/completions/zsh/_swaymsg b/completions/zsh/_swaymsg index 4207ce3b..1c3ccd65 100644 --- a/completions/zsh/_swaymsg +++ b/completions/zsh/_swaymsg @@ -21,6 +21,7 @@ types=( 'get_marks' 'get_bar_config' 'get_version' +'get_clipboard' ) _arguments -s \ diff --git a/include/sway/config.h b/include/sway/config.h index b0cd86eb..a05d5ede 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -235,7 +235,7 @@ enum ipc_feature { IPC_FEATURE_EVENT_WINDOW = 2048, IPC_FEATURE_EVENT_BINDING = 4096, IPC_FEATURE_EVENT_INPUT = 8192, - IPC_FEATURE_GET_CLIPBOARD = 16384, + IPC_FEATURE_GET_CLIPBOARD = 16384, IPC_FEATURE_ALL_COMMANDS = 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | 16384, IPC_FEATURE_ALL_EVENTS = 256 | 512 | 1024 | 2048 | 4096 | 8192, diff --git a/include/util.h b/include/util.h index e5365458..f68deae8 100644 --- a/include/util.h +++ b/include/util.h @@ -3,6 +3,7 @@ #include #include +#include #include /** @@ -57,4 +58,8 @@ uint32_t parse_color(const char *color); * to a dangling symlink, NULL is returned. */ char* resolve_path(const char* path); + +char *b64_encode(const char* binaryData, size_t len, size_t *flen); +unsigned char *b64_decode(const char *ascii, size_t len, size_t *flen); + #endif diff --git a/sway/CMakeLists.txt b/sway/CMakeLists.txt index bf0b2e7f..11bec4df 100644 --- a/sway/CMakeLists.txt +++ b/sway/CMakeLists.txt @@ -19,6 +19,7 @@ file(GLOB cmds add_executable(sway commands.c ${cmds} + base64.c config.c container.c criteria.c @@ -35,7 +36,7 @@ add_executable(sway output.c workspace.c border.c - security.c + security.c ) add_definitions( diff --git a/sway/base64.c b/sway/base64.c new file mode 100644 index 00000000..0d51b5c7 --- /dev/null +++ b/sway/base64.c @@ -0,0 +1,221 @@ +/* + * Adapted from https://github.com/littlstar/b64.c + * License under the MIT License: + * Copyright (c) 2014 Little Star Media, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include "util.h" + +static const char b64_table[] = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', + 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', + 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', + 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', + 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', + 'w', 'x', 'y', 'z', '0', '1', '2', '3', + '4', '5', '6', '7', '8', '9', '+', '/' +}; + +char *b64_encode(const char *src, size_t len, size_t *flen) { + int i = 0; + int j = 0; + char *enc = NULL; + size_t size = len * 4 / 3; + size_t idx = 0; + unsigned char buf[4]; + char tmp[3]; + + // alloc + enc = (char *) malloc(size + 1); + if (NULL == enc) { return NULL; } + + // parse until end of source + while (len--) { + // read up to 3 bytes at a time into `tmp' + tmp[i++] = *(src++); + + // if 3 bytes read then encode into `buf' + if (3 == i) { + buf[0] = (tmp[0] & 0xfc) >> 2; + buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4); + buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6); + buf[3] = tmp[2] & 0x3f; + + // shouldn't really happen + if (idx + 4 > size) { + size += 16; + enc = (char *) realloc(enc, size + 1); + } + for (i = 0; i < 4; ++i) { + enc[idx++] = b64_table[buf[i]]; + } + + // reset index + i = 0; + } + } + + // remainder + if (i > 0) { + // fill `tmp' with `\0' at most 3 times + for (j = i; j < 3; ++j) { + tmp[j] = '\0'; + } + + // perform same codec as above + buf[0] = (tmp[0] & 0xfc) >> 2; + buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4); + buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6); + buf[3] = tmp[2] & 0x3f; + + // perform same write to `enc` with new allocation + size_t delta = (i > 3 ? 0 : 3 - i) + (j > i + 1 ? 0 : i + 1 - j); + if (idx + delta > size) { + size += delta; + enc = (char *) realloc(enc, size + 1); + } + for (j = 0; (j < i + 1); ++j) { + enc[idx++] = b64_table[buf[j]]; + } + + // while there is still a remainder + // append `=' to `enc' + while ((i++ < 3)) { + enc[idx++] = '='; + } + } + + enc[idx] = '\0'; + + if (flen) + *flen = size; + return enc; +} + +unsigned char *b64_decode(const char *src, size_t len, size_t *decsize) { + int i = 0; + int j = 0; + int l = 0; + // max size estimate + size_t size = len * 3 / 4; + size_t idx = 0; + unsigned char *dec = NULL; + unsigned char buf[3]; + unsigned char tmp[4]; + + // alloc + dec = (unsigned char *) malloc(size + 1); + if (NULL == dec) { return NULL; } + + // parse until end of source + while (len--) { + if (isspace(src[j])) { j++; continue; } + // break if char is `=' or not base64 char + if ('=' == src[j]) { break; } + if (!(isalnum(src[j]) || '+' == src[j] || '/' == src[j])) { break; } + + // read up to 4 bytes at a time into `tmp' + tmp[i++] = src[j++]; + + // if 4 bytes read then decode into `buf' + if (4 == i) { + // translate values in `tmp' from table + for (i = 0; i < 4; ++i) { + // find translation char in `b64_table' + for (l = 0; l < 64; ++l) { + if (tmp[i] == b64_table[l]) { + tmp[i] = l; + break; + } + } + } + + // decode + buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4); + buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2); + buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3]; + + // unlikely + if (idx + 3 > size) { + size += 16; + dec = (unsigned char *) realloc(dec, size + 1); + } + if (dec != NULL){ + for (i = 0; i < 3; ++i) { + dec[idx++] = buf[i]; + } + } else { + return NULL; + } + + // reset + i = 0; + } + } + + // remainder + if (i > 0) { + // fill `tmp' with `\0' at most 4 times + for (j = i; j < 4; ++j) { + tmp[j] = '\0'; + } + + // translate remainder + for (j = 0; j < 4; ++j) { + // find translation char in `b64_table' + for (l = 0; l < 64; ++l) { + if (tmp[j] == b64_table[l]) { + tmp[j] = l; + break; + } + } + } + + // decode remainder + buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4); + buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2); + buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3]; + + // write remainer decoded buffer to `dec' + if (idx + (i - 1) > size) { + size += 16; + dec = (unsigned char *) realloc(dec, size + 1); + } + if (dec != NULL){ + for (j = 0; (j < i - 1); ++j) { + dec[idx++] = buf[j]; + } + } else { + return NULL; + } + } + + dec[idx] = '\0'; + // Return back the size of decoded string if demanded. + if (decsize != NULL) { + *decsize = size; + } + + return dec; +} diff --git a/sway/ipc-server.c b/sway/ipc-server.c index b6268404..70692f64 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -1,5 +1,6 @@ // See https://i3wm.org/docs/ipc.html for protocol information +#define _XOPEN_SOURCE 700 #include #include #include @@ -56,8 +57,11 @@ struct get_pixels_request { }; struct get_clipboard_request { - struct ipc_client *client; - struct wlc_event_source *event_source; + struct ipc_client *client; + json_object *json; + struct wlc_event_source *event_source; + char *type; + unsigned int *pending; }; struct sockaddr_un *ipc_user_sockaddr(void); @@ -327,44 +331,201 @@ void ipc_get_pixels(wlc_handle output) { ipc_get_pixel_requests = unhandled; } -static int ipc_selection_data_cb(int fd, uint32_t mask, void *data) -{ +static int ipc_selection_data_cb(int fd, uint32_t mask, void *data) { assert(data); - struct get_clipboard_request *req = (struct get_clipboard_request*) data; + struct get_clipboard_request *req = (struct get_clipboard_request *)data; if (mask & WLC_EVENT_ERROR) { sway_log(L_ERROR, "Selection data fd error"); - const char *error = "{ \"success\": false, \"error\": " - "\"Could not receive text data from clipboard\" }"; - ipc_send_reply(req->client, error, (uint32_t)strlen(error)); goto cleanup; } - if (mask & WLC_EVENT_READABLE) { - char buf[512]; - int ret = read(fd, buf, 511); - if (ret < 0) { - sway_log_errno(L_ERROR, "Reading from selection data fd failed"); - const char *error = "{ \"success\": false, \"error\": " - "\"Could not receive text data from clipboard\" }"; - ipc_send_reply(req->client, error, (uint32_t)strlen(error)); - goto cleanup; + if (mask & WLC_EVENT_READABLE || true) { + static const int step_size = 512; + char *data = NULL; + int ret = 0; + int current = 0; + + // read data as long as there is data avilable + // grow the buffer step_size in every iteration + do { + if (data == NULL) { + data = malloc(step_size); + } else { + data = realloc(data, current + step_size); + } + + ret = read(fd, data + current, step_size - 1); + if (ret < 0) { + sway_log_errno(L_ERROR, "Reading from selection data fd failed"); + goto cleanup; + } + + current += ret; + } while (ret == step_size - 1); + + data[current] = '\0'; + + if (strncmp(req->type, "text/", 5) == 0) { + json_object_object_add(req->json, req->type, + json_object_new_string(data)); + } else { + size_t outlen; + char *b64 = b64_encode(data, current, &outlen); + json_object_object_add(req->json, req->type, + json_object_new_string(b64)); + free(b64); } - buf[ret] = '\0'; - json_object *obj = json_object_new_object(); - json_object_object_add(obj, "success", json_object_new_boolean(true)); - json_object_object_add(obj, "content", json_object_new_string(buf)); - const char *str = json_object_to_json_string(obj); + free(data); + } else { + return 0; // TODO + } + +cleanup: + close(fd); + + if (--(*req->pending) == 0) { + const char *str = json_object_to_json_string(req->json); ipc_send_reply(req->client, str, (uint32_t)strlen(str)); + json_object_put(req->json); + } + + free(req->type); + wlc_event_source_remove(req->event_source); + free(req); + return 0; +} + +// greedy wildcard (only "*") matching +bool mime_type_matches(const char *mime_type, const char *pattern) { + const char* wildcard = NULL; + while (*mime_type && *pattern) { + if (*pattern == '*' && !wildcard) { + wildcard = pattern; + ++pattern; + } + + if (*mime_type != *pattern) { + if (!wildcard) + return false; + + pattern = wildcard; + ++mime_type; + continue; + } + + ++mime_type; + ++pattern; + } + + while (*pattern == '*') { + ++pattern; + } + + return (*mime_type == *pattern); +} + +void ipc_get_clipboard(struct ipc_client *client, char *buf) { + static const char *error_json = "{ \"success\": false, \"error\": " + "\"Failed to retrieve clipboard data\" }"; + + size_t size; + const char **types = wlc_get_selection_types(&size); + if (client->payload_length == 0) { + json_object *obj = json_object_new_array(); + for (size_t i = 0; i < size; ++i) { + json_object_array_add(obj, json_object_new_string(types[i])); + } + + const char *str = json_object_to_json_string(obj); + ipc_send_reply(client, str, strlen(str)); json_object_put(obj); + return; + } + + unescape_string(buf); + strip_quotes(buf); + list_t *requested = split_string(buf, " "); + json_object *json = json_object_new_object(); + unsigned int *pending = malloc(sizeof(unsigned int)); + *pending = 0; + + for (size_t l = 0; l < (size_t) requested->length; ++l) { + const char *pattern = requested->items[l]; + bool found = false; + for (size_t i = 0; i < size; ++i) { + if (mime_type_matches(types[i], pattern)) { + found = true; + + struct get_clipboard_request *req = malloc(sizeof(*req)); + if (!req) { + sway_log(L_ERROR, "Cannot allocate get_clipboard_request"); + goto data_error; + } + + int pipes[2]; + if (pipe(pipes) == -1) { + sway_log_errno(L_ERROR, "pipe call failed"); + free(req); + goto data_error; + } + + fcntl(pipes[0], F_SETFD, FD_CLOEXEC | O_NONBLOCK); + fcntl(pipes[1], F_SETFD, FD_CLOEXEC | O_NONBLOCK); + + if (!wlc_get_selection_data(types[i], pipes[1])) { + close(pipes[0]); + close(pipes[1]); + free(req); + sway_log(L_ERROR, "wlc_get_selection_data failed"); + goto data_error; + } + + (*pending)++; + + req->client = client; + req->type = strdup(types[i]); + req->json = json; + req->pending = pending; + req->event_source = wlc_event_loop_add_fd(pipes[0], + WLC_EVENT_READABLE | WLC_EVENT_ERROR | WLC_EVENT_HANGUP, + &ipc_selection_data_cb, req); + + // NOTE: remove this goto to enable retrieving multiple + // targets at once. The whole implementation is already + // made for it. The only reason it was disabled + // at the time of writing is that neither wlc's xselection + // implementation nor (apparently) gtk on wayland supports + // multiple send requests at the same time which makes + // every request except the last one fail (and therefore + // return empty data) + goto cleanup; + } + } + + if (!found) { + sway_log(L_INFO, "Invalid clipboard type %s requested", pattern); + } } + if (*pending == 0) { + static const char *empty = "[]"; + ipc_send_reply(client, empty, (uint32_t)strlen(empty)); + free(json); + free(pending); + } + + goto cleanup; + +data_error: + ipc_send_reply(client, error_json, (uint32_t)strlen(error_json)); + free(json); + free(pending); + cleanup: - wlc_event_source_remove(req->event_source); - close(fd); - free(req); - return 0; + list_free(requested); + free(types); } void ipc_client_handle_command(struct ipc_client *client) { @@ -610,69 +771,13 @@ void ipc_client_handle_command(struct ipc_client *client) { goto exit_cleanup; } - case IPC_GET_CLIPBOARD: - { + case IPC_GET_CLIPBOARD: + { if (!(client->security_policy & IPC_FEATURE_GET_CLIPBOARD)) { goto exit_denied; } - size_t size; - const char **types = wlc_get_selection_types(&size); - const char *type = NULL; - if (types == NULL || size == 0) { - const char *error = "{ \"success\": false, \"error\": " - "\"Empty clipboard\" }"; - ipc_send_reply(client, error, (uint32_t)strlen(error)); - goto exit_cleanup; - } - - for (size_t i = 0; i < size; ++i) { - if (strcmp(types[i], "text/plain;charset=utf-8") == 0 - || strcmp(types[i], "text/plain") == 0) { - type = types[i]; - break; - } - } - - if (type) { - struct get_clipboard_request *req = malloc(sizeof(*req)); - if (!req) { - sway_log(L_ERROR, "Unable to allocate get_clipboard_request"); - goto clipboard_error; - } - - int pipes[2]; - if (pipe(pipes) == -1) { - sway_log_errno(L_ERROR, "pipe call failed"); - free(req); - goto clipboard_error; - } - - fcntl(pipes[0], F_SETFD, FD_CLOEXEC | O_NONBLOCK); - fcntl(pipes[1], F_SETFD, FD_CLOEXEC | O_NONBLOCK); - if (!wlc_get_selection_data(type, pipes[1])) { - close(pipes[0]); - close(pipes[1]); - free(req); - sway_log(L_ERROR, "wlc_get_selection_data failed"); - goto clipboard_error; - } - - req->client = client; - req->event_source = wlc_event_loop_add_fd(pipes[0], - WLC_EVENT_READABLE | WLC_EVENT_ERROR | WLC_EVENT_HANGUP, - &ipc_selection_data_cb, req); - free(types); - goto exit_cleanup; - } - -clipboard_error: - sway_log(L_INFO, "Clipboard has to text data"); - const char *error = "{ \"success\": false, \"error\": " - "\"Could not receive text data from clipboard\" }"; - ipc_send_reply(client, error, (uint32_t)strlen(error)); - - free(types); + ipc_get_clipboard(client, buf); goto exit_cleanup; } diff --git a/swaymsg/main.c b/swaymsg/main.c index fa28553b..450df673 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"); @@ -145,15 +147,23 @@ static void pretty_print_version(json_object *v) { } static void pretty_print_clipboard(json_object *v) { - bool _success; - json_object *success; - json_object_object_get_ex(v, "success", &success); - _success = json_object_get_boolean(success); - - if (_success) { - json_object *ver; - json_object_object_get_ex(v, "content", &ver); - printf("%s\n", json_object_get_string(ver)); + 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)) { + printf("%s\n", json_object_get_string( + json_object_iter_peek_value(&iter))); + } + } } } @@ -310,6 +320,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 1f03bee3..6fd08e81 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 ------- -- cgit v1.2.3 From 1e894c1166d1563dacb68d6b91ade455c311b10c Mon Sep 17 00:00:00 2001 From: nyorain Date: Fri, 7 Jul 2017 22:34:00 +0200 Subject: Handle x11 text atoms in get_clipboard ipc --- sway/ipc-server.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'sway') diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 70692f64..22e0137b 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -331,6 +331,14 @@ void ipc_get_pixels(wlc_handle output) { ipc_get_pixel_requests = unhandled; } +static bool is_text_target(const char *target) { + return (strncmp(target, "text/", 5) == 0 + || strcmp(target, "UTF8_STRING") == 0 + || strcmp(target, "STRING") == 0 + || strcmp(target, "TEXT") == 0 + || strcmp(target, "COMPOUND_TEXT") == 0); +} + static int ipc_selection_data_cb(int fd, uint32_t mask, void *data) { assert(data); struct get_clipboard_request *req = (struct get_clipboard_request *)data; @@ -340,7 +348,7 @@ static int ipc_selection_data_cb(int fd, uint32_t mask, void *data) { goto cleanup; } - if (mask & WLC_EVENT_READABLE || true) { + if (mask & WLC_EVENT_READABLE) { static const int step_size = 512; char *data = NULL; int ret = 0; @@ -366,7 +374,7 @@ static int ipc_selection_data_cb(int fd, uint32_t mask, void *data) { data[current] = '\0'; - if (strncmp(req->type, "text/", 5) == 0) { + if (is_text_target(req->type)) { json_object_object_add(req->json, req->type, json_object_new_string(data)); } else { @@ -378,8 +386,6 @@ static int ipc_selection_data_cb(int fd, uint32_t mask, void *data) { } free(data); - } else { - return 0; // TODO } cleanup: -- cgit v1.2.3 From f0463dab329708832ea43e5d46456ff57ba62354 Mon Sep 17 00:00:00 2001 From: nyorain Date: Fri, 7 Jul 2017 23:34:58 +0200 Subject: Signal base64 in clipboard type; Reimplement loop --- sway/ipc-server.c | 54 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 22 deletions(-) (limited to 'sway') diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 22e0137b..ae758883 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -349,43 +349,53 @@ static int ipc_selection_data_cb(int fd, uint32_t mask, void *data) { } if (mask & WLC_EVENT_READABLE) { - static const int step_size = 512; - char *data = NULL; - int ret = 0; - int current = 0; + static const int max_size = 8192 * 1000; + int len = 512; + int i = 0; + char *buf = malloc(len); // read data as long as there is data avilable // grow the buffer step_size in every iteration - do { - if (data == NULL) { - data = malloc(step_size); - } else { - data = realloc(data, current + step_size); - } + for(;;) { + int amt = read(fd, buf + i, len - i - 1); + if (amt <= 0) + break; + + i += amt; + if (i >= len - 1) { + if (len >= max_size) { + sway_log(L_ERROR, "selection data too large"); + free(buf); + goto cleanup; + } + char *next = realloc(buf, (len *= 2)); + if (!next) { + sway_log_errno(L_ERROR, "relloc failed"); + free(buf); + goto cleanup; + } - ret = read(fd, data + current, step_size - 1); - if (ret < 0) { - sway_log_errno(L_ERROR, "Reading from selection data fd failed"); - goto cleanup; + buf = next; } + } - current += ret; - } while (ret == step_size - 1); - - data[current] = '\0'; + buf[i] = '\0'; if (is_text_target(req->type)) { json_object_object_add(req->json, req->type, - json_object_new_string(data)); + json_object_new_string(buf)); } else { size_t outlen; - char *b64 = b64_encode(data, current, &outlen); - json_object_object_add(req->json, req->type, + char *b64 = b64_encode(buf, i, &outlen); + char *type = malloc(strlen(req->type) + 8); + strcat(type, ";base64"); + json_object_object_add(req->json, type, json_object_new_string(b64)); + free(type); free(b64); } - free(data); + free(buf); } cleanup: -- cgit v1.2.3 From 727215c90754a8c20d1a5b78d8c4194f885178b9 Mon Sep 17 00:00:00 2001 From: nyorain Date: Tue, 11 Jul 2017 17:07:04 +0200 Subject: Add timeout; Fix receive loop & style issues --- sway/ipc-server.c | 196 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 114 insertions(+), 82 deletions(-) (limited to 'sway') diff --git a/sway/ipc-server.c b/sway/ipc-server.c index ae758883..937382af 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -59,9 +59,13 @@ struct get_pixels_request { struct get_clipboard_request { struct ipc_client *client; json_object *json; - struct wlc_event_source *event_source; + struct wlc_event_source *fd_event_source; + struct wlc_event_source *timer_event_source; char *type; unsigned int *pending; + char *buf; + size_t buf_size; + size_t buf_position; }; struct sockaddr_un *ipc_user_sockaddr(void); @@ -339,54 +343,69 @@ static bool is_text_target(const char *target) { || strcmp(target, "COMPOUND_TEXT") == 0); } +static void release_clipboard_request(struct get_clipboard_request *req) { + if (--(*req->pending) == 0) { + const char *str = json_object_to_json_string(req->json); + ipc_send_reply(req->client, str, (uint32_t)strlen(str)); + json_object_put(req->json); + } + + free(req->type); + free(req->buf); + wlc_event_source_remove(req->fd_event_source); + wlc_event_source_remove(req->timer_event_source); + free(req); +} + static int ipc_selection_data_cb(int fd, uint32_t mask, void *data) { assert(data); struct get_clipboard_request *req = (struct get_clipboard_request *)data; if (mask & WLC_EVENT_ERROR) { sway_log(L_ERROR, "Selection data fd error"); - goto cleanup; + goto release; } if (mask & WLC_EVENT_READABLE) { - static const int max_size = 8192 * 1000; - int len = 512; - int i = 0; - char *buf = malloc(len); - - // read data as long as there is data avilable - // grow the buffer step_size in every iteration - for(;;) { - int amt = read(fd, buf + i, len - i - 1); - if (amt <= 0) - break; - - i += amt; - if (i >= len - 1) { - if (len >= max_size) { - sway_log(L_ERROR, "selection data too large"); - free(buf); - goto cleanup; + static const unsigned int max_size = 8192 * 1024; + int amt = 0; + + do { + int size = req->buf_size - req->buf_position; + int amt = read(fd, req->buf + req->buf_position, size - 1); + if (amt < 0) { + if (errno == EAGAIN) { + return 0; + } + + sway_log_errno(L_INFO, "Failed to read from clipboard data fd"); + goto release; + } + + req->buf_position += amt; + if (req->buf_position >= req->buf_size - 1) { + if (req->buf_size >= max_size) { + sway_log(L_ERROR, "get_clipbard: selection data too large"); + goto release; } - char *next = realloc(buf, (len *= 2)); + char *next = realloc(req->buf, req->buf_size *= 2); if (!next) { - sway_log_errno(L_ERROR, "relloc failed"); - free(buf); - goto cleanup; + sway_log_errno(L_ERROR, "get_clipboard: realloc data buffer failed"); + goto release; } - buf = next; + req->buf = next; } - } + } while(amt != 0); - buf[i] = '\0'; + req->buf[req->buf_position] = '\0'; if (is_text_target(req->type)) { json_object_object_add(req->json, req->type, - json_object_new_string(buf)); + json_object_new_string(req->buf)); } else { size_t outlen; - char *b64 = b64_encode(buf, i, &outlen); + char *b64 = b64_encode(req->buf, req->buf_position, &outlen); char *type = malloc(strlen(req->type) + 8); strcat(type, ";base64"); json_object_object_add(req->json, type, @@ -394,22 +413,19 @@ static int ipc_selection_data_cb(int fd, uint32_t mask, void *data) { free(type); free(b64); } - - free(buf); } -cleanup: - close(fd); +release: + release_clipboard_request(req); + return 0; +} - if (--(*req->pending) == 0) { - const char *str = json_object_to_json_string(req->json); - ipc_send_reply(req->client, str, (uint32_t)strlen(str)); - json_object_put(req->json); - } +static int ipc_selection_timer_cb(void *data) { + assert(data); + struct get_clipboard_request *req = (struct get_clipboard_request *)data; - free(req->type); - wlc_event_source_remove(req->event_source); - free(req); + sway_log(L_INFO, "get_clipbard: timeout for type %s", req->type); + release_clipboard_request(req); return 0; } @@ -471,53 +487,69 @@ void ipc_get_clipboard(struct ipc_client *client, char *buf) { const char *pattern = requested->items[l]; bool found = false; for (size_t i = 0; i < size; ++i) { - if (mime_type_matches(types[i], pattern)) { - found = true; + if (!mime_type_matches(types[i], pattern)) { + continue; + } - struct get_clipboard_request *req = malloc(sizeof(*req)); - if (!req) { - sway_log(L_ERROR, "Cannot allocate get_clipboard_request"); - goto data_error; - } + found = true; - int pipes[2]; - if (pipe(pipes) == -1) { - sway_log_errno(L_ERROR, "pipe call failed"); - free(req); - goto data_error; - } + struct get_clipboard_request *req = malloc(sizeof(*req)); + if (!req) { + sway_log(L_ERROR, "get_clipboard: request malloc failed"); + goto data_error; + } - fcntl(pipes[0], F_SETFD, FD_CLOEXEC | O_NONBLOCK); - fcntl(pipes[1], F_SETFD, FD_CLOEXEC | O_NONBLOCK); + int pipes[2]; + if (pipe(pipes) == -1) { + sway_log_errno(L_ERROR, "get_clipboard: pipe call failed"); + free(req); + goto data_error; + } - if (!wlc_get_selection_data(types[i], pipes[1])) { - close(pipes[0]); - close(pipes[1]); - free(req); - sway_log(L_ERROR, "wlc_get_selection_data failed"); - goto data_error; - } + fcntl(pipes[0], F_SETFD, FD_CLOEXEC | O_NONBLOCK); + fcntl(pipes[1], F_SETFD, FD_CLOEXEC | O_NONBLOCK); + + if (!wlc_get_selection_data(types[i], pipes[1])) { + close(pipes[0]); + close(pipes[1]); + free(req); + sway_log(L_ERROR, "get_clipboard: failed to retrieve " + "selection data"); + goto data_error; + } - (*pending)++; - - req->client = client; - req->type = strdup(types[i]); - req->json = json; - req->pending = pending; - req->event_source = wlc_event_loop_add_fd(pipes[0], - WLC_EVENT_READABLE | WLC_EVENT_ERROR | WLC_EVENT_HANGUP, - &ipc_selection_data_cb, req); - - // NOTE: remove this goto to enable retrieving multiple - // targets at once. The whole implementation is already - // made for it. The only reason it was disabled - // at the time of writing is that neither wlc's xselection - // implementation nor (apparently) gtk on wayland supports - // multiple send requests at the same time which makes - // every request except the last one fail (and therefore - // return empty data) - goto cleanup; + if (!(req->buf = malloc(512))) { + close(pipes[0]); + close(pipes[1]); + free(req); + sway_log_errno(L_ERROR, "get_clipboard: buf malloc failed"); + goto data_error; } + + (*pending)++; + + req->client = client; + req->type = strdup(types[i]); + req->json = json; + req->pending = pending; + req->buf_position = 0; + req->buf_size = 512; + req->timer_event_source = wlc_event_loop_add_timer(ipc_selection_timer_cb, req); + req->fd_event_source = wlc_event_loop_add_fd(pipes[0], + WLC_EVENT_READABLE | WLC_EVENT_ERROR | WLC_EVENT_HANGUP, + &ipc_selection_data_cb, req); + + wlc_event_source_timer_update(req->timer_event_source, 1000); + + // NOTE: remove this goto to enable retrieving multiple + // targets at once. The whole implementation is already + // made for it. The only reason it was disabled + // at the time of writing is that neither wlc's xselection + // implementation nor (apparently) gtk on wayland supports + // multiple send requests at the same time which makes + // every request except the last one fail (and therefore + // return empty data) + goto cleanup; } if (!found) { -- cgit v1.2.3 From 20888fbb5e19e2bd7e45afb6fdbc2c1ed8cc769d Mon Sep 17 00:00:00 2001 From: nyorain Date: Tue, 11 Jul 2017 17:12:53 +0200 Subject: Close fd in clipboard request --- sway/ipc-server.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'sway') diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 937382af..829b8bf3 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -59,6 +59,7 @@ struct get_pixels_request { struct get_clipboard_request { struct ipc_client *client; json_object *json; + int fd; struct wlc_event_source *fd_event_source; struct wlc_event_source *timer_event_source; char *type; @@ -354,6 +355,7 @@ static void release_clipboard_request(struct get_clipboard_request *req) { free(req->buf); wlc_event_source_remove(req->fd_event_source); wlc_event_source_remove(req->timer_event_source); + close(req->fd); free(req); } @@ -534,6 +536,7 @@ void ipc_get_clipboard(struct ipc_client *client, char *buf) { req->pending = pending; req->buf_position = 0; req->buf_size = 512; + req->fd = pipes[0]; req->timer_event_source = wlc_event_loop_add_timer(ipc_selection_timer_cb, req); req->fd_event_source = wlc_event_loop_add_fd(pipes[0], WLC_EVENT_READABLE | WLC_EVENT_ERROR | WLC_EVENT_HANGUP, -- cgit v1.2.3 From 1cca551c6fc69bca76a4992b33ef685371cac26b Mon Sep 17 00:00:00 2001 From: nyorain Date: Tue, 11 Jul 2017 18:04:24 +0200 Subject: Add get_clipbard ipc errors; Adapt swaymsg Also increase the get_clipboard timeout to 30 secs --- sway/ipc-server.c | 49 +++++++++++++++++++++++++++++++++---------------- swaymsg/main.c | 16 ++++++++++++++-- 2 files changed, 47 insertions(+), 18 deletions(-) (limited to 'sway') diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 829b8bf3..5e1e93ce 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -365,7 +365,7 @@ static int ipc_selection_data_cb(int fd, uint32_t mask, void *data) { if (mask & WLC_EVENT_ERROR) { sway_log(L_ERROR, "Selection data fd error"); - goto release; + goto error; } if (mask & WLC_EVENT_READABLE) { @@ -388,12 +388,12 @@ static int ipc_selection_data_cb(int fd, uint32_t mask, void *data) { if (req->buf_position >= req->buf_size - 1) { if (req->buf_size >= max_size) { sway_log(L_ERROR, "get_clipbard: selection data too large"); - goto release; + goto error; } char *next = realloc(req->buf, req->buf_size *= 2); if (!next) { sway_log_errno(L_ERROR, "get_clipboard: realloc data buffer failed"); - goto release; + goto error; } req->buf = next; @@ -402,21 +402,33 @@ static int ipc_selection_data_cb(int fd, uint32_t mask, void *data) { req->buf[req->buf_position] = '\0'; + json_object *obj = json_object_new_object(); + json_object_object_add(obj, "success", json_object_new_boolean(true)); if (is_text_target(req->type)) { - json_object_object_add(req->json, req->type, - json_object_new_string(req->buf)); + json_object_object_add(obj, "content", json_object_new_string(req->buf)); + json_object_object_add(req->json, req->type, obj); } else { size_t outlen; char *b64 = b64_encode(req->buf, req->buf_position, &outlen); + json_object_object_add(obj, "content", json_object_new_string(b64)); + free(b64); + char *type = malloc(strlen(req->type) + 8); strcat(type, ";base64"); - json_object_object_add(req->json, type, - json_object_new_string(b64)); + json_object_object_add(req->json, type, obj); free(type); - free(b64); } } + goto release; + +error:; + json_object *obj = json_object_new_object(); + json_object_object_add(obj, "success", json_object_new_boolean(false)); + json_object_object_add(obj, "error", + json_object_new_string("Failed to retrieve data")); + json_object_object_add(req->json, req->type, obj); + release: release_clipboard_request(req); return 0; @@ -427,13 +439,18 @@ static int ipc_selection_timer_cb(void *data) { struct get_clipboard_request *req = (struct get_clipboard_request *)data; sway_log(L_INFO, "get_clipbard: timeout for type %s", req->type); + json_object *obj = json_object_new_object(); + json_object_object_add(obj, "success", json_object_new_boolean(false)); + json_object_object_add(obj, "error", json_object_new_string("Timeout")); + json_object_object_add(req->json, req->type, obj); + release_clipboard_request(req); return 0; } // greedy wildcard (only "*") matching bool mime_type_matches(const char *mime_type, const char *pattern) { - const char* wildcard = NULL; + const char *wildcard = NULL; while (*mime_type && *pattern) { if (*pattern == '*' && !wildcard) { wildcard = pattern; @@ -461,9 +478,6 @@ bool mime_type_matches(const char *mime_type, const char *pattern) { } void ipc_get_clipboard(struct ipc_client *client, char *buf) { - static const char *error_json = "{ \"success\": false, \"error\": " - "\"Failed to retrieve clipboard data\" }"; - size_t size; const char **types = wlc_get_selection_types(&size); if (client->payload_length == 0) { @@ -542,7 +556,7 @@ void ipc_get_clipboard(struct ipc_client *client, char *buf) { WLC_EVENT_READABLE | WLC_EVENT_ERROR | WLC_EVENT_HANGUP, &ipc_selection_data_cb, req); - wlc_event_source_timer_update(req->timer_event_source, 1000); + wlc_event_source_timer_update(req->timer_event_source, 30000); // NOTE: remove this goto to enable retrieving multiple // targets at once. The whole implementation is already @@ -561,15 +575,18 @@ void ipc_get_clipboard(struct ipc_client *client, char *buf) { } if (*pending == 0) { - static const char *empty = "[]"; - ipc_send_reply(client, empty, (uint32_t)strlen(empty)); + static const char *error_empty = "{ \"success\": false, \"error\": " + "\"No matching types found\" }"; + ipc_send_reply(client, error_empty, (uint32_t)strlen(error_empty)); free(json); free(pending); } goto cleanup; -data_error: +data_error:; + static const char *error_json = "{ \"success\": false, \"error\": " + "\"Failed to create clipboard data request\" }"; ipc_send_reply(client, error_json, (uint32_t)strlen(error_json)); free(json); free(pending); diff --git a/swaymsg/main.c b/swaymsg/main.c index 450df673..2f9cfb14 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -160,10 +160,22 @@ static void pretty_print_clipboard(json_object *v) { 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)) { - printf("%s\n", json_object_get_string( - json_object_iter_peek_value(&iter))); + 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)); } } -- cgit v1.2.3