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 | Remove destroyed views from scratchpad (diff) | |
parent | Add get_clipbard ipc errors; Adapt swaymsg (diff) | |
download | sway-6d83a59b461d07c4c9c5ee5809da658f5c56afc3.tar.gz sway-6d83a59b461d07c4c9c5ee5809da658f5c56afc3.tar.zst sway-6d83a59b461d07c4c9c5ee5809da658f5c56afc3.zip |
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) { | |||
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"); |
@@ -144,10 +146,43 @@ static void pretty_print_version(json_object *v) { | |||
144 | printf("sway version %s\n", json_object_get_string(ver)); | 146 | printf("sway version %s\n", json_object_get_string(ver)); |
145 | } | 147 | } |
146 | 148 | ||
149 | static void pretty_print_clipboard(json_object *v) { | ||
150 | if (success(v, true)) { | ||
151 | if (json_object_is_type(v, json_type_array)) { | ||
152 | for (int i = 0; i < json_object_array_length(v); ++i) { | ||
153 | json_object *o = json_object_array_get_idx(v, i); | ||
154 | printf("%s\n", json_object_get_string(o)); | ||
155 | } | ||
156 | } else { | ||
157 | // NOTE: could be extended to print all received types | ||
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 | json_object *obj = json_object_iter_peek_value(&iter); | ||
164 | if (success(obj, false)) { | ||
165 | json_object *content; | ||
166 | json_object_object_get_ex(obj, "content", &content); | ||
167 | printf("%s\n", json_object_get_string(content)); | ||
168 | } else { | ||
169 | json_object *error; | ||
170 | json_object_object_get_ex(obj, "error", &error); | ||
171 | printf("Error: %s\n", json_object_get_string(error)); | ||
172 | } | ||
173 | } | ||
174 | } | ||
175 | } else { | ||
176 | json_object *error; | ||
177 | json_object_object_get_ex(v, "error", &error); | ||
178 | printf("Error: %s\n", json_object_get_string(error)); | ||
179 | } | ||
180 | } | ||
181 | |||
147 | static void pretty_print(int type, json_object *resp) { | 182 | static void pretty_print(int type, json_object *resp) { |
148 | if (type != IPC_COMMAND && type != IPC_GET_WORKSPACES && | 183 | if (type != IPC_COMMAND && type != IPC_GET_WORKSPACES && |
149 | type != IPC_GET_INPUTS && type != IPC_GET_OUTPUTS && | 184 | type != IPC_GET_INPUTS && type != IPC_GET_OUTPUTS && |
150 | type != IPC_GET_VERSION) { | 185 | type != IPC_GET_VERSION && type != IPC_GET_CLIPBOARD) { |
151 | printf("%s\n", json_object_to_json_string_ext(resp, | 186 | printf("%s\n", json_object_to_json_string_ext(resp, |
152 | JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); | 187 | JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); |
153 | return; | 188 | return; |
@@ -158,6 +193,11 @@ static void pretty_print(int type, json_object *resp) { | |||
158 | return; | 193 | return; |
159 | } | 194 | } |
160 | 195 | ||
196 | if (type == IPC_GET_CLIPBOARD) { | ||
197 | pretty_print_clipboard(resp); | ||
198 | return; | ||
199 | } | ||
200 | |||
161 | json_object *obj; | 201 | json_object *obj; |
162 | size_t len = json_object_array_length(resp); | 202 | size_t len = json_object_array_length(resp); |
163 | for (size_t i = 0; i < len; ++i) { | 203 | for (size_t i = 0; i < len; ++i) { |
@@ -267,6 +307,8 @@ int main(int argc, char **argv) { | |||
267 | type = IPC_GET_BAR_CONFIG; | 307 | type = IPC_GET_BAR_CONFIG; |
268 | } else if (strcasecmp(cmdtype, "get_version") == 0) { | 308 | } else if (strcasecmp(cmdtype, "get_version") == 0) { |
269 | type = IPC_GET_VERSION; | 309 | type = IPC_GET_VERSION; |
310 | } else if (strcasecmp(cmdtype, "get_clipboard") == 0) { | ||
311 | type = IPC_GET_CLIPBOARD; | ||
270 | } else { | 312 | } else { |
271 | sway_abort("Unknown message type %s", cmdtype); | 313 | sway_abort("Unknown message type %s", cmdtype); |
272 | } | 314 | } |
@@ -290,6 +332,9 @@ int main(int argc, char **argv) { | |||
290 | printf("%s\n", resp); | 332 | printf("%s\n", resp); |
291 | ret = 1; | 333 | ret = 1; |
292 | } else { | 334 | } else { |
335 | if (!success(obj, true)) { | ||
336 | ret = 1; | ||
337 | } | ||
293 | if (raw) { | 338 | if (raw) { |
294 | printf("%s\n", json_object_to_json_string_ext(obj, | 339 | printf("%s\n", json_object_to_json_string_ext(obj, |
295 | JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); | 340 | 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 | |||
65 | *get_version*:: | 65 | *get_version*:: |
66 | Get JSON-encoded version information for the running instance of sway. | 66 | Get JSON-encoded version information for the running instance of sway. |
67 | 67 | ||
68 | *get_clipboard*:: | ||
69 | Get JSON-encoded information about the clipboard. | ||
70 | Returns the current clipboard mime-types if called without | ||
71 | arguments, otherwise returns the clipboard data in the requested | ||
72 | formats. Encodes the data using base64 for non-text mime types. | ||
73 | |||
68 | Authors | 74 | Authors |
69 | ------- | 75 | ------- |
70 | 76 | ||