diff options
author | nyorain <nyorain@gmail.com> | 2017-07-11 18:04:24 +0200 |
---|---|---|
committer | nyorain <nyorain@gmail.com> | 2017-07-11 18:04:28 +0200 |
commit | 1cca551c6fc69bca76a4992b33ef685371cac26b (patch) | |
tree | a81f5598adbfbb53be0b99bdbbf6a224c17145c4 /sway | |
parent | Close fd in clipboard request (diff) | |
download | sway-1cca551c6fc69bca76a4992b33ef685371cac26b.tar.gz sway-1cca551c6fc69bca76a4992b33ef685371cac26b.tar.zst sway-1cca551c6fc69bca76a4992b33ef685371cac26b.zip |
Add get_clipbard ipc errors; Adapt swaymsg
Also increase the get_clipboard timeout to 30 secs
Diffstat (limited to 'sway')
-rw-r--r-- | sway/ipc-server.c | 49 |
1 files changed, 33 insertions, 16 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); |