diff options
author | nyorain <nyorain@gmail.com> | 2017-07-07 21:51:34 +0200 |
---|---|---|
committer | nyorain <nyorain@gmail.com> | 2017-07-07 21:51:34 +0200 |
commit | c0f2acce4e08eecdcf29058335aece113467daee (patch) | |
tree | 7f871abe8b5d1cdf4a74512810d68e9e66f0d57f /swaymsg/main.c | |
parent | Fix/Simplify get_clipboard ipc-server impl (diff) | |
download | sway-c0f2acce4e08eecdcf29058335aece113467daee.tar.gz sway-c0f2acce4e08eecdcf29058335aece113467daee.tar.zst sway-c0f2acce4e08eecdcf29058335aece113467daee.zip |
Rework get_clipboard implementation
Diffstat (limited to 'swaymsg/main.c')
-rw-r--r-- | swaymsg/main.c | 41 |
1 files changed, 27 insertions, 14 deletions
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) { | |||
19 | exit(exit_code); | 19 | exit(exit_code); |
20 | } | 20 | } |
21 | 21 | ||
22 | static void pretty_print_cmd(json_object *r) { | 22 | static bool success(json_object *r, bool fallback) { |
23 | bool _success; | ||
24 | json_object *success; | 23 | json_object *success; |
25 | if (!json_object_object_get_ex(r, "success", &success)) { | 24 | if (!json_object_object_get_ex(r, "success", &success)) { |
26 | _success = true; | 25 | return fallback; |
27 | } else { | 26 | } else { |
28 | _success = json_object_get_boolean(success); | 27 | return json_object_get_boolean(success); |
29 | } | 28 | } |
30 | if (!_success) { | 29 | } |
30 | |||
31 | static void pretty_print_cmd(json_object *r) { | ||
32 | if (!success(r, true)) { | ||
31 | json_object *error; | 33 | json_object *error; |
32 | if (!json_object_object_get_ex(r, "error", &error)) { | 34 | if (!json_object_object_get_ex(r, "error", &error)) { |
33 | printf("An unknkown error occured"); | 35 | printf("An unknkown error occured"); |
@@ -145,15 +147,23 @@ static void pretty_print_version(json_object *v) { | |||
145 | } | 147 | } |
146 | 148 | ||
147 | static void pretty_print_clipboard(json_object *v) { | 149 | static void pretty_print_clipboard(json_object *v) { |
148 | bool _success; | 150 | if (success(v, true)) { |
149 | json_object *success; | 151 | if (json_object_is_type(v, json_type_array)) { |
150 | json_object_object_get_ex(v, "success", &success); | 152 | for (int i = 0; i < json_object_array_length(v); ++i) { |
151 | _success = json_object_get_boolean(success); | 153 | json_object *o = json_object_array_get_idx(v, i); |
152 | 154 | printf("%s\n", json_object_get_string(o)); | |
153 | if (_success) { | 155 | } |
154 | json_object *ver; | 156 | } else { |
155 | json_object_object_get_ex(v, "content", &ver); | 157 | // NOTE: could be extended to print all received types |
156 | printf("%s\n", json_object_get_string(ver)); | 158 | // instead just the first one when sways ipc server |
159 | // supports it | ||
160 | struct json_object_iterator iter = json_object_iter_begin(v); | ||
161 | struct json_object_iterator end = json_object_iter_end(v); | ||
162 | if (!json_object_iter_equal(&iter, &end)) { | ||
163 | printf("%s\n", json_object_get_string( | ||
164 | json_object_iter_peek_value(&iter))); | ||
165 | } | ||
166 | } | ||
157 | } | 167 | } |
158 | } | 168 | } |
159 | 169 | ||
@@ -310,6 +320,9 @@ int main(int argc, char **argv) { | |||
310 | printf("%s\n", resp); | 320 | printf("%s\n", resp); |
311 | ret = 1; | 321 | ret = 1; |
312 | } else { | 322 | } else { |
323 | if (!success(obj, true)) { | ||
324 | ret = 1; | ||
325 | } | ||
313 | if (raw) { | 326 | if (raw) { |
314 | printf("%s\n", json_object_to_json_string_ext(obj, | 327 | printf("%s\n", json_object_to_json_string_ext(obj, |
315 | JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); | 328 | JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); |