aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Mikkel Oscar Lyderik <mikkeloscar@gmail.com>2016-01-03 20:40:50 +0100
committerLibravatar Mikkel Oscar Lyderik <mikkeloscar@gmail.com>2016-01-03 20:40:50 +0100
commit7298a9c67aed2d7a5c5c1df55bfa6052cac04d51 (patch)
treeaf92196751fdc7cc326b8e6a32c1c62983fd30bb
parentAdd IPC event types (diff)
downloadsway-7298a9c67aed2d7a5c5c1df55bfa6052cac04d51.tar.gz
sway-7298a9c67aed2d7a5c5c1df55bfa6052cac04d51.tar.zst
sway-7298a9c67aed2d7a5c5c1df55bfa6052cac04d51.zip
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.
-rw-r--r--common/ipc-client.c28
-rw-r--r--include/ipc-client.h19
-rw-r--r--swaybar/main.c5
3 files changed, 39 insertions, 13 deletions
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 @@
10#include "stringop.h" 10#include "stringop.h"
11#include "ipc.h" 11#include "ipc.h"
12#include "readline.h" 12#include "readline.h"
13#include "ipc-client.h"
13 14
14static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; 15static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
15static const size_t ipc_header_size = sizeof(ipc_magic)+8; 16static const size_t ipc_header_size = sizeof(ipc_magic)+8;
@@ -39,7 +40,7 @@ int ipc_open_socket(const char *socket_path) {
39 return socketfd; 40 return socketfd;
40} 41}
41 42
42char *ipc_recv_response(int socketfd, uint32_t *len) { 43struct ipc_response *ipc_recv_response(int socketfd) {
43 char data[ipc_header_size]; 44 char data[ipc_header_size];
44 uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic)); 45 uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic));
45 46
@@ -52,21 +53,29 @@ char *ipc_recv_response(int socketfd, uint32_t *len) {
52 total += received; 53 total += received;
53 } 54 }
54 55
56 struct ipc_response *response = malloc(sizeof(struct ipc_response));
55 total = 0; 57 total = 0;
56 *len = data32[0]; 58 response->size = data32[0];
57 char *response = malloc(*len + 1); 59 response->type = data32[1];
58 while (total < *len) { 60 char *payload = malloc(response->size + 1);
59 ssize_t received = recv(socketfd, response + total, *len - total, 0); 61 while (total < response->size) {
62 ssize_t received = recv(socketfd, payload + total, response->size - total, 0);
60 if (received < 0) { 63 if (received < 0) {
61 sway_abort("Unable to receive IPC response"); 64 sway_abort("Unable to receive IPC response");
62 } 65 }
63 total += received; 66 total += received;
64 } 67 }
65 response[*len] = '\0'; 68 payload[response->size] = '\0';
69 response->payload = payload;
66 70
67 return response; 71 return response;
68} 72}
69 73
74void free_ipc_response(struct ipc_response *response) {
75 free(response->payload);
76 free(response);
77}
78
70char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len) { 79char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len) {
71 char data[ipc_header_size]; 80 char data[ipc_header_size];
72 uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic)); 81 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
82 sway_abort("Unable to send IPC payload"); 91 sway_abort("Unable to send IPC payload");
83 } 92 }
84 93
85 return ipc_recv_response(socketfd, len); 94 struct ipc_response *resp = ipc_recv_response(socketfd);
95 char *response = resp->payload;
96 *len = resp->size;
97 free(resp);
98
99 return response;
86} 100}
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
@@ -4,6 +4,16 @@
4#include "ipc.h" 4#include "ipc.h"
5 5
6/** 6/**
7 * IPC response including type of IPC response, size of payload and the json
8 * encoded payload string.
9 */
10struct ipc_response {
11 uint32_t size;
12 uint32_t type;
13 char *payload;
14};
15
16/**
7 * Gets the path to the IPC socket from sway. 17 * Gets the path to the IPC socket from sway.
8 */ 18 */
9char *get_socketpath(void); 19char *get_socketpath(void);
@@ -17,9 +27,12 @@ int ipc_open_socket(const char *socket_path);
17 */ 27 */
18char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len); 28char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len);
19/** 29/**
20 * Receives a single IPC resposne and returns the buffer. len will be updated 30 * Receives a single IPC response and returns an ipc_response.
21 * with the length of the buffer returned from sway. 31 */
32struct ipc_response *ipc_recv_response(int socketfd);
33/**
34 * Free ipc_response struct
22 */ 35 */
23char *ipc_recv_response(int socketfd, uint32_t *len); 36void free_ipc_response(struct ipc_response *response);
24 37
25#endif 38#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() {
1025 1025
1026 if (FD_ISSET(ipc_event_socketfd, &readfds)) { 1026 if (FD_ISSET(ipc_event_socketfd, &readfds)) {
1027 sway_log(L_DEBUG, "Got workspace update."); 1027 sway_log(L_DEBUG, "Got workspace update.");
1028 uint32_t len; 1028 struct ipc_response *resp = ipc_recv_response(ipc_event_socketfd);
1029 char *buf = ipc_recv_response(ipc_event_socketfd, &len); 1029 free_ipc_response(resp);
1030 free(buf);
1031 ipc_update_workspaces(); 1030 ipc_update_workspaces();
1032 dirty = true; 1031 dirty = true;
1033 } 1032 }