diff options
-rw-r--r-- | sway/ipc-server.c | 49 | ||||
-rw-r--r-- | swaymsg/main.c | 16 |
2 files changed, 47 insertions, 18 deletions
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) { | |||
365 | 365 | ||
366 | if (mask & WLC_EVENT_ERROR) { | 366 | if (mask & WLC_EVENT_ERROR) { |
367 | sway_log(L_ERROR, "Selection data fd error"); | 367 | sway_log(L_ERROR, "Selection data fd error"); |
368 | goto release; | 368 | goto error; |
369 | } | 369 | } |
370 | 370 | ||
371 | if (mask & WLC_EVENT_READABLE) { | 371 | if (mask & WLC_EVENT_READABLE) { |
@@ -388,12 +388,12 @@ static int ipc_selection_data_cb(int fd, uint32_t mask, void *data) { | |||
388 | if (req->buf_position >= req->buf_size - 1) { | 388 | if (req->buf_position >= req->buf_size - 1) { |
389 | if (req->buf_size >= max_size) { | 389 | if (req->buf_size >= max_size) { |
390 | sway_log(L_ERROR, "get_clipbard: selection data too large"); | 390 | sway_log(L_ERROR, "get_clipbard: selection data too large"); |
391 | goto release; | 391 | goto error; |
392 | } | 392 | } |
393 | char *next = realloc(req->buf, req->buf_size *= 2); | 393 | char *next = realloc(req->buf, req->buf_size *= 2); |
394 | if (!next) { | 394 | if (!next) { |
395 | sway_log_errno(L_ERROR, "get_clipboard: realloc data buffer failed"); | 395 | sway_log_errno(L_ERROR, "get_clipboard: realloc data buffer failed"); |
396 | goto release; | 396 | goto error; |
397 | } | 397 | } |
398 | 398 | ||
399 | req->buf = next; | 399 | req->buf = next; |
@@ -402,21 +402,33 @@ static int ipc_selection_data_cb(int fd, uint32_t mask, void *data) { | |||
402 | 402 | ||
403 | req->buf[req->buf_position] = '\0'; | 403 | req->buf[req->buf_position] = '\0'; |
404 | 404 | ||
405 | json_object *obj = json_object_new_object(); | ||
406 | json_object_object_add(obj, "success", json_object_new_boolean(true)); | ||
405 | if (is_text_target(req->type)) { | 407 | if (is_text_target(req->type)) { |
406 | json_object_object_add(req->json, req->type, | 408 | json_object_object_add(obj, "content", json_object_new_string(req->buf)); |
407 | json_object_new_string(req->buf)); | 409 | json_object_object_add(req->json, req->type, obj); |
408 | } else { | 410 | } else { |
409 | size_t outlen; | 411 | size_t outlen; |
410 | char *b64 = b64_encode(req->buf, req->buf_position, &outlen); | 412 | char *b64 = b64_encode(req->buf, req->buf_position, &outlen); |
413 | json_object_object_add(obj, "content", json_object_new_string(b64)); | ||
414 | free(b64); | ||
415 | |||
411 | char *type = malloc(strlen(req->type) + 8); | 416 | char *type = malloc(strlen(req->type) + 8); |
412 | strcat(type, ";base64"); | 417 | strcat(type, ";base64"); |
413 | json_object_object_add(req->json, type, | 418 | json_object_object_add(req->json, type, obj); |
414 | json_object_new_string(b64)); | ||
415 | free(type); | 419 | free(type); |
416 | free(b64); | ||
417 | } | 420 | } |
418 | } | 421 | } |
419 | 422 | ||
423 | goto release; | ||
424 | |||
425 | error:; | ||
426 | json_object *obj = json_object_new_object(); | ||
427 | json_object_object_add(obj, "success", json_object_new_boolean(false)); | ||
428 | json_object_object_add(obj, "error", | ||
429 | json_object_new_string("Failed to retrieve data")); | ||
430 | json_object_object_add(req->json, req->type, obj); | ||
431 | |||
420 | release: | 432 | release: |
421 | release_clipboard_request(req); | 433 | release_clipboard_request(req); |
422 | return 0; | 434 | return 0; |
@@ -427,13 +439,18 @@ static int ipc_selection_timer_cb(void *data) { | |||
427 | struct get_clipboard_request *req = (struct get_clipboard_request *)data; | 439 | struct get_clipboard_request *req = (struct get_clipboard_request *)data; |
428 | 440 | ||
429 | sway_log(L_INFO, "get_clipbard: timeout for type %s", req->type); | 441 | sway_log(L_INFO, "get_clipbard: timeout for type %s", req->type); |
442 | json_object *obj = json_object_new_object(); | ||
443 | json_object_object_add(obj, "success", json_object_new_boolean(false)); | ||
444 | json_object_object_add(obj, "error", json_object_new_string("Timeout")); | ||
445 | json_object_object_add(req->json, req->type, obj); | ||
446 | |||
430 | release_clipboard_request(req); | 447 | release_clipboard_request(req); |
431 | return 0; | 448 | return 0; |
432 | } | 449 | } |
433 | 450 | ||
434 | // greedy wildcard (only "*") matching | 451 | // greedy wildcard (only "*") matching |
435 | bool mime_type_matches(const char *mime_type, const char *pattern) { | 452 | bool mime_type_matches(const char *mime_type, const char *pattern) { |
436 | const char* wildcard = NULL; | 453 | const char *wildcard = NULL; |
437 | while (*mime_type && *pattern) { | 454 | while (*mime_type && *pattern) { |
438 | if (*pattern == '*' && !wildcard) { | 455 | if (*pattern == '*' && !wildcard) { |
439 | wildcard = pattern; | 456 | wildcard = pattern; |
@@ -461,9 +478,6 @@ bool mime_type_matches(const char *mime_type, const char *pattern) { | |||
461 | } | 478 | } |
462 | 479 | ||
463 | void ipc_get_clipboard(struct ipc_client *client, char *buf) { | 480 | void ipc_get_clipboard(struct ipc_client *client, char *buf) { |
464 | static const char *error_json = "{ \"success\": false, \"error\": " | ||
465 | "\"Failed to retrieve clipboard data\" }"; | ||
466 | |||
467 | size_t size; | 481 | size_t size; |
468 | const char **types = wlc_get_selection_types(&size); | 482 | const char **types = wlc_get_selection_types(&size); |
469 | if (client->payload_length == 0) { | 483 | if (client->payload_length == 0) { |
@@ -542,7 +556,7 @@ void ipc_get_clipboard(struct ipc_client *client, char *buf) { | |||
542 | WLC_EVENT_READABLE | WLC_EVENT_ERROR | WLC_EVENT_HANGUP, | 556 | WLC_EVENT_READABLE | WLC_EVENT_ERROR | WLC_EVENT_HANGUP, |
543 | &ipc_selection_data_cb, req); | 557 | &ipc_selection_data_cb, req); |
544 | 558 | ||
545 | wlc_event_source_timer_update(req->timer_event_source, 1000); | 559 | wlc_event_source_timer_update(req->timer_event_source, 30000); |
546 | 560 | ||
547 | // NOTE: remove this goto to enable retrieving multiple | 561 | // NOTE: remove this goto to enable retrieving multiple |
548 | // targets at once. The whole implementation is already | 562 | // targets at once. The whole implementation is already |
@@ -561,15 +575,18 @@ void ipc_get_clipboard(struct ipc_client *client, char *buf) { | |||
561 | } | 575 | } |
562 | 576 | ||
563 | if (*pending == 0) { | 577 | if (*pending == 0) { |
564 | static const char *empty = "[]"; | 578 | static const char *error_empty = "{ \"success\": false, \"error\": " |
565 | ipc_send_reply(client, empty, (uint32_t)strlen(empty)); | 579 | "\"No matching types found\" }"; |
580 | ipc_send_reply(client, error_empty, (uint32_t)strlen(error_empty)); | ||
566 | free(json); | 581 | free(json); |
567 | free(pending); | 582 | free(pending); |
568 | } | 583 | } |
569 | 584 | ||
570 | goto cleanup; | 585 | goto cleanup; |
571 | 586 | ||
572 | data_error: | 587 | data_error:; |
588 | static const char *error_json = "{ \"success\": false, \"error\": " | ||
589 | "\"Failed to create clipboard data request\" }"; | ||
573 | ipc_send_reply(client, error_json, (uint32_t)strlen(error_json)); | 590 | ipc_send_reply(client, error_json, (uint32_t)strlen(error_json)); |
574 | free(json); | 591 | free(json); |
575 | free(pending); | 592 | 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) { | |||
160 | struct json_object_iterator iter = json_object_iter_begin(v); | 160 | struct json_object_iterator iter = json_object_iter_begin(v); |
161 | struct json_object_iterator end = json_object_iter_end(v); | 161 | struct json_object_iterator end = json_object_iter_end(v); |
162 | if (!json_object_iter_equal(&iter, &end)) { | 162 | if (!json_object_iter_equal(&iter, &end)) { |
163 | printf("%s\n", json_object_get_string( | 163 | json_object *obj = json_object_iter_peek_value(&iter); |
164 | 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 | } | ||
165 | } | 173 | } |
166 | } | 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)); | ||
167 | } | 179 | } |
168 | } | 180 | } |
169 | 181 | ||