From a20226772ee1236cbeabc6fbb3efdce64e407932 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Sun, 3 Jan 2016 18:52:53 +0100 Subject: swaybar: rename ipc_listen_sock.. to ipc_event_sock.. --- swaybar/main.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'swaybar') diff --git a/swaybar/main.c b/swaybar/main.c index cbedb200..aa761b7d 100644 --- a/swaybar/main.c +++ b/swaybar/main.c @@ -66,7 +66,7 @@ struct status_block { list_t *status_line = NULL; list_t *workspaces = NULL; -int ipc_socketfd, ipc_listen_socketfd; +int ipc_socketfd, ipc_event_socketfd; pid_t pid; int status_read_fd; char line[1024]; @@ -155,8 +155,8 @@ void swaybar_teardown() { close(ipc_socketfd); } - if (ipc_listen_socketfd) { - close(ipc_listen_socketfd); + if (ipc_event_socketfd) { + close(ipc_event_socketfd); } } @@ -403,9 +403,9 @@ void bar_ipc_init(int outputi, const char *bar_id) { json_object_put(bar_config); free(res); - const char *subscribe_json = "[ \"workspace\" ]"; + const char *subscribe_json = "[ \"workspace\", \"mode\" ]"; len = strlen(subscribe_json); - res = ipc_single_command(ipc_listen_socketfd, IPC_SUBSCRIBE, subscribe_json, &len); + res = ipc_single_command(ipc_event_socketfd, IPC_SUBSCRIBE, subscribe_json, &len); free(res); ipc_update_workspaces(); @@ -1015,7 +1015,7 @@ void poll_for_update() { dirty = false; FD_ZERO(&readfds); - FD_SET(ipc_listen_socketfd, &readfds); + FD_SET(ipc_event_socketfd, &readfds); FD_SET(status_read_fd, &readfds); activity = select(FD_SETSIZE, &readfds, NULL, NULL, NULL); @@ -1023,10 +1023,10 @@ void poll_for_update() { sway_log(L_ERROR, "polling failed: %d", errno); } - if (FD_ISSET(ipc_listen_socketfd, &readfds)) { + if (FD_ISSET(ipc_event_socketfd, &readfds)) { sway_log(L_DEBUG, "Got workspace update."); uint32_t len; - char *buf = ipc_recv_response(ipc_listen_socketfd, &len); + char *buf = ipc_recv_response(ipc_event_socketfd, &len); free(buf); ipc_update_workspaces(); dirty = true; @@ -1131,7 +1131,7 @@ int main(int argc, char **argv) { } } ipc_socketfd = ipc_open_socket(socket_path); - ipc_listen_socketfd = ipc_open_socket(socket_path); + ipc_event_socketfd = ipc_open_socket(socket_path); if (argc == optind) { -- cgit v1.2.3-54-g00ecf From 7298a9c67aed2d7a5c5c1df55bfa6052cac04d51 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Sun, 3 Jan 2016 20:40:50 +0100 Subject: Add type to returned response. Makes `ipc_recv_response` return a struct with size, type and payload rather than just the payload string. This is useful if the type has to be checked on the client. --- common/ipc-client.c | 28 +++++++++++++++++++++------- include/ipc-client.h | 19 ++++++++++++++++--- swaybar/main.c | 5 ++--- 3 files changed, 39 insertions(+), 13 deletions(-) (limited to 'swaybar') diff --git a/common/ipc-client.c b/common/ipc-client.c index e92a09fe..81348913 100644 --- a/common/ipc-client.c +++ b/common/ipc-client.c @@ -10,6 +10,7 @@ #include "stringop.h" #include "ipc.h" #include "readline.h" +#include "ipc-client.h" static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; static const size_t ipc_header_size = sizeof(ipc_magic)+8; @@ -39,7 +40,7 @@ int ipc_open_socket(const char *socket_path) { return socketfd; } -char *ipc_recv_response(int socketfd, uint32_t *len) { +struct ipc_response *ipc_recv_response(int socketfd) { char data[ipc_header_size]; uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic)); @@ -52,21 +53,29 @@ char *ipc_recv_response(int socketfd, uint32_t *len) { total += received; } + struct ipc_response *response = malloc(sizeof(struct ipc_response)); total = 0; - *len = data32[0]; - char *response = malloc(*len + 1); - while (total < *len) { - ssize_t received = recv(socketfd, response + total, *len - total, 0); + response->size = data32[0]; + response->type = data32[1]; + char *payload = malloc(response->size + 1); + while (total < response->size) { + ssize_t received = recv(socketfd, payload + total, response->size - total, 0); if (received < 0) { sway_abort("Unable to receive IPC response"); } total += received; } - response[*len] = '\0'; + payload[response->size] = '\0'; + response->payload = payload; return response; } +void free_ipc_response(struct ipc_response *response) { + free(response->payload); + free(response); +} + char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len) { char data[ipc_header_size]; uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic)); @@ -82,5 +91,10 @@ char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint3 sway_abort("Unable to send IPC payload"); } - return ipc_recv_response(socketfd, len); + struct ipc_response *resp = ipc_recv_response(socketfd); + char *response = resp->payload; + *len = resp->size; + free(resp); + + return response; } diff --git a/include/ipc-client.h b/include/ipc-client.h index a4cfd87f..030c80b6 100644 --- a/include/ipc-client.h +++ b/include/ipc-client.h @@ -3,6 +3,16 @@ #include "ipc.h" +/** + * IPC response including type of IPC response, size of payload and the json + * encoded payload string. + */ +struct ipc_response { + uint32_t size; + uint32_t type; + char *payload; +}; + /** * Gets the path to the IPC socket from sway. */ @@ -17,9 +27,12 @@ int ipc_open_socket(const char *socket_path); */ char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len); /** - * Receives a single IPC resposne and returns the buffer. len will be updated - * with the length of the buffer returned from sway. + * Receives a single IPC response and returns an ipc_response. + */ +struct ipc_response *ipc_recv_response(int socketfd); +/** + * Free ipc_response struct */ -char *ipc_recv_response(int socketfd, uint32_t *len); +void free_ipc_response(struct ipc_response *response); #endif diff --git a/swaybar/main.c b/swaybar/main.c index aa761b7d..d534345a 100644 --- a/swaybar/main.c +++ b/swaybar/main.c @@ -1025,9 +1025,8 @@ void poll_for_update() { if (FD_ISSET(ipc_event_socketfd, &readfds)) { sway_log(L_DEBUG, "Got workspace update."); - uint32_t len; - char *buf = ipc_recv_response(ipc_event_socketfd, &len); - free(buf); + struct ipc_response *resp = ipc_recv_response(ipc_event_socketfd); + free_ipc_response(resp); ipc_update_workspaces(); dirty = true; } -- cgit v1.2.3-54-g00ecf