Signal base64 in clipboard type; Reimplement loop

master
nyorain 8 years ago
parent 1e894c1166
commit f0463dab32

@ -349,43 +349,53 @@ static int ipc_selection_data_cb(int fd, uint32_t mask, void *data) {
} }
if (mask & WLC_EVENT_READABLE) { if (mask & WLC_EVENT_READABLE) {
static const int step_size = 512; static const int max_size = 8192 * 1000;
char *data = NULL; int len = 512;
int ret = 0; int i = 0;
int current = 0; char *buf = malloc(len);
// read data as long as there is data avilable // read data as long as there is data avilable
// grow the buffer step_size in every iteration // grow the buffer step_size in every iteration
do { for(;;) {
if (data == NULL) { int amt = read(fd, buf + i, len - i - 1);
data = malloc(step_size); if (amt <= 0)
} else { break;
data = realloc(data, current + step_size);
} i += amt;
if (i >= len - 1) {
if (len >= max_size) {
sway_log(L_ERROR, "selection data too large");
free(buf);
goto cleanup;
}
char *next = realloc(buf, (len *= 2));
if (!next) {
sway_log_errno(L_ERROR, "relloc failed");
free(buf);
goto cleanup;
}
ret = read(fd, data + current, step_size - 1); buf = next;
if (ret < 0) {
sway_log_errno(L_ERROR, "Reading from selection data fd failed");
goto cleanup;
} }
}
current += ret; buf[i] = '\0';
} while (ret == step_size - 1);
data[current] = '\0';
if (is_text_target(req->type)) { if (is_text_target(req->type)) {
json_object_object_add(req->json, req->type, json_object_object_add(req->json, req->type,
json_object_new_string(data)); json_object_new_string(buf));
} else { } else {
size_t outlen; size_t outlen;
char *b64 = b64_encode(data, current, &outlen); char *b64 = b64_encode(buf, i, &outlen);
json_object_object_add(req->json, req->type, char *type = malloc(strlen(req->type) + 8);
strcat(type, ";base64");
json_object_object_add(req->json, type,
json_object_new_string(b64)); json_object_new_string(b64));
free(type);
free(b64); free(b64);
} }
free(data); free(buf);
} }
cleanup: cleanup:

Loading…
Cancel
Save