Fix/Simplify get_clipboard ipc-server impl

This commit is contained in:
nyorain 2017-07-07 15:46:17 +02:00
parent 02c75ebe37
commit 42547cafb6

View file

@ -617,8 +617,8 @@ void ipc_client_handle_command(struct ipc_client *client) {
} }
size_t size; size_t size;
bool found = false;
const char **types = wlc_get_selection_types(&size); const char **types = wlc_get_selection_types(&size);
const char *type = NULL;
if (types == NULL || size == 0) { if (types == NULL || size == 0) {
const char *error = "{ \"success\": false, \"error\": " const char *error = "{ \"success\": false, \"error\": "
"\"Empty clipboard\" }"; "\"Empty clipboard\" }";
@ -627,48 +627,50 @@ void ipc_client_handle_command(struct ipc_client *client) {
} }
for (size_t i = 0; i < size; ++i) { for (size_t i = 0; i < size; ++i) {
if (strcmp(types[i], "text/plain;charset=utf-8") != 0 if (strcmp(types[i], "text/plain;charset=utf-8") == 0
&& strcmp(types[i], "text/plain") != 0) { || strcmp(types[i], "text/plain") == 0) {
continue; type = types[i];
break;
}
} }
if (type) {
struct get_clipboard_request *req = malloc(sizeof(*req)); struct get_clipboard_request *req = malloc(sizeof(*req));
if (!req) { if (!req) {
sway_log(L_ERROR, "Unable to allocate get_clipboard_request"); sway_log(L_ERROR, "Unable to allocate get_clipboard_request");
goto exit_cleanup; goto clipboard_error;
} }
int pipes[2]; int pipes[2];
if (pipe(pipes) == -1) { if (pipe(pipes) == -1) {
sway_log_errno(L_ERROR, "pipe call failed"); sway_log_errno(L_ERROR, "pipe call failed");
free(req); free(req);
break; goto clipboard_error;
} }
fcntl(pipes[0], F_SETFD, FD_CLOEXEC | O_NONBLOCK); fcntl(pipes[0], F_SETFD, FD_CLOEXEC | O_NONBLOCK);
fcntl(pipes[1], F_SETFD, FD_CLOEXEC | O_NONBLOCK); fcntl(pipes[1], F_SETFD, FD_CLOEXEC | O_NONBLOCK);
if (!wlc_get_selection_data(types[i], pipes[1])) { if (!wlc_get_selection_data(type, pipes[1])) {
close(pipes[0]); close(pipes[0]);
close(pipes[1]); close(pipes[1]);
free(req); free(req);
sway_log(L_ERROR, "wlc_get_selection_data failed"); sway_log(L_ERROR, "wlc_get_selection_data failed");
break; goto clipboard_error;
} }
req->client = client; req->client = client;
req->event_source = wlc_event_loop_add_fd(pipes[0], req->event_source = wlc_event_loop_add_fd(pipes[0],
WLC_EVENT_READABLE | WLC_EVENT_ERROR | WLC_EVENT_HANGUP, WLC_EVENT_READABLE | WLC_EVENT_ERROR | WLC_EVENT_HANGUP,
&ipc_selection_data_cb, req); &ipc_selection_data_cb, req);
found = true; free(types);
break; goto exit_cleanup;
} }
if (!found) { clipboard_error:
sway_log(L_INFO, "Clipboard has to text data"); sway_log(L_INFO, "Clipboard has to text data");
const char *error = "{ \"success\": false, \"error\": " const char *error = "{ \"success\": false, \"error\": "
"\"Could not receive text data from clipboard\" }"; "\"Could not receive text data from clipboard\" }";
ipc_send_reply(client, error, (uint32_t)strlen(error)); ipc_send_reply(client, error, (uint32_t)strlen(error));
}
free(types); free(types);
goto exit_cleanup; goto exit_cleanup;