aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar nyorain <nyorain@gmail.com>2017-07-07 23:34:58 +0200
committerLibravatar nyorain <nyorain@gmail.com>2017-07-07 23:34:58 +0200
commitf0463dab329708832ea43e5d46456ff57ba62354 (patch)
tree1852488225b3a3121f38fe5087030dff936ec393
parentHandle x11 text atoms in get_clipboard ipc (diff)
downloadsway-f0463dab329708832ea43e5d46456ff57ba62354.tar.gz
sway-f0463dab329708832ea43e5d46456ff57ba62354.tar.zst
sway-f0463dab329708832ea43e5d46456ff57ba62354.zip
Signal base64 in clipboard type; Reimplement loop
-rw-r--r--sway/ipc-server.c54
1 files changed, 32 insertions, 22 deletions
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) {
349 } 349 }
350 350
351 if (mask & WLC_EVENT_READABLE) { 351 if (mask & WLC_EVENT_READABLE) {
352 static const int step_size = 512; 352 static const int max_size = 8192 * 1000;
353 char *data = NULL; 353 int len = 512;
354 int ret = 0; 354 int i = 0;
355 int current = 0; 355 char *buf = malloc(len);
356 356
357 // read data as long as there is data avilable 357 // read data as long as there is data avilable
358 // grow the buffer step_size in every iteration 358 // grow the buffer step_size in every iteration
359 do { 359 for(;;) {
360 if (data == NULL) { 360 int amt = read(fd, buf + i, len - i - 1);
361 data = malloc(step_size); 361 if (amt <= 0)
362 } else { 362 break;
363 data = realloc(data, current + step_size); 363
364 } 364 i += amt;
365 if (i >= len - 1) {
366 if (len >= max_size) {
367 sway_log(L_ERROR, "selection data too large");
368 free(buf);
369 goto cleanup;
370 }
371 char *next = realloc(buf, (len *= 2));
372 if (!next) {
373 sway_log_errno(L_ERROR, "relloc failed");
374 free(buf);
375 goto cleanup;
376 }
365 377
366 ret = read(fd, data + current, step_size - 1); 378 buf = next;
367 if (ret < 0) {
368 sway_log_errno(L_ERROR, "Reading from selection data fd failed");
369 goto cleanup;
370 } 379 }
380 }
371 381
372 current += ret; 382 buf[i] = '\0';
373 } while (ret == step_size - 1);
374
375 data[current] = '\0';
376 383
377 if (is_text_target(req->type)) { 384 if (is_text_target(req->type)) {
378 json_object_object_add(req->json, req->type, 385 json_object_object_add(req->json, req->type,
379 json_object_new_string(data)); 386 json_object_new_string(buf));
380 } else { 387 } else {
381 size_t outlen; 388 size_t outlen;
382 char *b64 = b64_encode(data, current, &outlen); 389 char *b64 = b64_encode(buf, i, &outlen);
383 json_object_object_add(req->json, req->type, 390 char *type = malloc(strlen(req->type) + 8);
391 strcat(type, ";base64");
392 json_object_object_add(req->json, type,
384 json_object_new_string(b64)); 393 json_object_new_string(b64));
394 free(type);
385 free(b64); 395 free(b64);
386 } 396 }
387 397
388 free(data); 398 free(buf);
389 } 399 }
390 400
391cleanup: 401cleanup: