Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Removed additional event socket to reduce synchronisation problems
and limited workspace update requests count.
  • Loading branch information
ilyaluk committed Aug 4, 2017
1 parent f8d0e1f commit 8dec814
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 19 deletions.
6 changes: 5 additions & 1 deletion common/ipc-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void free_ipc_response(struct ipc_response *response) {
free(response);
}

char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len) {
void ipc_single_command_no_response(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));
memcpy(data, ipc_magic, sizeof(ipc_magic));
Expand All @@ -102,6 +102,10 @@ char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint3
if (write(socketfd, payload, *len) == -1) {
sway_abort("Unable to send IPC payload");
}
}

char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len) {
ipc_single_command_no_response(socketfd, type, payload, len);

struct ipc_response *resp = ipc_recv_response(socketfd);
char *response = resp->payload;
Expand Down
4 changes: 4 additions & 0 deletions include/ipc-client.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ char *get_socketpath(void);
* Opens the sway socket.
*/
int ipc_open_socket(const char *socket_path);
/**
* Issues a single IPC command without reading response.
*/
void ipc_single_command_no_response(int socketfd, uint32_t type, const char *payload, uint32_t *len);
/**
* Issues a single IPC command and returns the buffer. len will be updated with
* the length of the buffer returned from sway.
Expand Down
2 changes: 2 additions & 0 deletions include/swaybar/bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ struct bar {
int ipc_socketfd;
int status_read_fd;
pid_t status_command_pid;

int pending_ipc_requests;
};

struct output {
Expand Down
9 changes: 2 additions & 7 deletions swaybar/bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id) {

/* connect to sway ipc */
bar->ipc_socketfd = ipc_open_socket(socket_path);
bar->ipc_event_socketfd = ipc_open_socket(socket_path);

ipc_bar_init(bar, bar_id);

Expand Down Expand Up @@ -205,7 +204,7 @@ bool dirty = true;

static void respond_ipc(int fd, short mask, void *_bar) {
struct bar *bar = (struct bar *)_bar;
sway_log(L_DEBUG, "Got IPC event.");
sway_log(L_DEBUG, "Got IPC event or reply.");
dirty = handle_ipc_event(bar);
}

Expand All @@ -222,7 +221,7 @@ static void respond_output(int fd, short mask, void *_output) {
}

void bar_run(struct bar *bar) {
add_event(bar->ipc_event_socketfd, POLLIN, respond_ipc, bar);
add_event(bar->ipc_socketfd, POLLIN, respond_ipc, bar);
add_event(bar->status_read_fd, POLLIN, respond_command, bar);

int i;
Expand Down Expand Up @@ -322,10 +321,6 @@ void bar_teardown(struct bar *bar) {
close(bar->ipc_socketfd);
}

if (bar->ipc_event_socketfd) {
close(bar->ipc_event_socketfd);
}

/* terminate status command process */
terminate_status_command(bar->status_command_pid);
}
47 changes: 36 additions & 11 deletions swaybar/ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void ipc_send_workspace_command(const char *workspace_name) {
char command[size];
sprintf(command, "workspace \"%s\"", workspace_name);

ipc_single_command(swaybar.ipc_socketfd, IPC_COMMAND, command, &size);
ipc_single_command_no_response(swaybar.ipc_socketfd, IPC_COMMAND, command, &size);
}

static void ipc_parse_config(struct config *config, const char *payload) {
Expand Down Expand Up @@ -249,7 +249,27 @@ static void ipc_parse_config(struct config *config, const char *payload) {
json_object_put(bar_config);
}

static void ipc_update_workspaces(struct bar *bar) {
static void ipc_update_workspaces_request(struct bar *bar) {
uint32_t len = 0;

if (bar->pending_ipc_requests >= 10) {
sway_log(L_DEBUG, "Ignoring update request");
return;
}
bar->pending_ipc_requests++;
sway_log(L_DEBUG, "Sending update request, %d pending", bar->pending_ipc_requests);

ipc_single_command_no_response(bar->ipc_socketfd, IPC_GET_WORKSPACES, NULL, &len);
}

static void ipc_update_workspaces_response(struct bar *bar, char *res) {
bar->pending_ipc_requests--;
sway_log(L_DEBUG, "Got update response, %d pending", bar->pending_ipc_requests);
if (bar->pending_ipc_requests < 0) {
sway_log(L_DEBUG, "Unexpected update response");
bar->pending_ipc_requests = 0;
}

int i;
for (i = 0; i < bar->outputs->length; ++i) {
struct output *output = bar->outputs->items[i];
Expand All @@ -259,11 +279,8 @@ static void ipc_update_workspaces(struct bar *bar) {
output->workspaces = create_list();
}

uint32_t len = 0;
char *res = ipc_single_command(bar->ipc_socketfd, IPC_GET_WORKSPACES, NULL, &len);
json_object *results = json_tokener_parse(res);
if (!results) {
free(res);
return;
}

Expand Down Expand Up @@ -303,7 +320,6 @@ static void ipc_update_workspaces(struct bar *bar) {
}

json_object_put(results);
free(res);
}

void ipc_bar_init(struct bar *bar, const char *bar_id) {
Expand Down Expand Up @@ -359,22 +375,27 @@ void ipc_bar_init(struct bar *bar, const char *bar_id) {
free(res);
json_object_put(outputs);

ipc_update_workspaces_request(bar);
// This will handle update response, since we haven't subscribed yet
handle_ipc_event(bar);

const char *subscribe_json = "[ \"workspace\", \"mode\" ]";
len = strlen(subscribe_json);
res = ipc_single_command(bar->ipc_event_socketfd, IPC_SUBSCRIBE, subscribe_json, &len);
res = ipc_single_command(bar->ipc_socketfd, IPC_SUBSCRIBE, subscribe_json, &len);
free(res);

ipc_update_workspaces(bar);
}

bool handle_ipc_event(struct bar *bar) {
struct ipc_response *resp = ipc_recv_response(bar->ipc_event_socketfd);
struct ipc_response *resp = ipc_recv_response(bar->ipc_socketfd);
if (!resp) {
return false;
}
switch (resp->type) {
case IPC_EVENT_WORKSPACE:
ipc_update_workspaces(bar);
ipc_update_workspaces_request(bar);
// This should read update response, but if there are events
// in socket, there shouldn't be problems.
handle_ipc_event(bar);
break;
case IPC_EVENT_MODE: {
json_object *result = json_tokener_parse(resp->payload);
Expand All @@ -400,7 +421,11 @@ bool handle_ipc_event(struct bar *bar) {
json_object_put(result);
break;
}
case IPC_GET_WORKSPACES:
ipc_update_workspaces_response(bar, resp->payload);
break;
default:
sway_log(L_DEBUG, "Unknown message type: 0x%x", resp->type);
free_ipc_response(resp);
return false;
}
Expand Down

0 comments on commit 8dec814

Please sign in to comment.