From e191f7cb1eaae0cb79f06a78ccd7e2edf0c3419c Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Thu, 8 Feb 2024 14:43:15 +0100 Subject: Change dwm ipc socket and fix latency --- src/dwm.c | 62 ++++++++++++++++++++++---------------------------------------- 1 file changed, 22 insertions(+), 40 deletions(-) diff --git a/src/dwm.c b/src/dwm.c index 8a4aad5..d856675 100644 --- a/src/dwm.c +++ b/src/dwm.c @@ -18,8 +18,8 @@ typedef struct { } __attribute__((packed)) DwmIpcHeader; struct DwmIpc { - GSource source; GSocket *socket; + GSource *source; State *state; Color color; Color selected; @@ -104,7 +104,7 @@ static void ipc_subscribe(DwmIpc *dwm, const char *event) gsize msg_len = 0; gchar *msg = json_generator_to_data(gen, &msg_len); - log_info("Subscribing to dwm ipc: %s", msg); + log_debug("Subscribing to dwm ipc: %s", msg); ipc_send(dwm, IPC_TYPE_SUBSCRIBE, msg, msg_len); json_node_free(root); @@ -145,7 +145,7 @@ static void ipc_run_command(DwmIpc *dwm, const char *cmd, JsonNode **args) gsize msg_len = 0; gchar *msg = json_generator_to_data(gen, &msg_len); - log_info("Sending command to dwm ipc: %s", msg); + log_debug("Sending command to dwm ipc: %s", msg); ipc_send(dwm, IPC_TYPE_RUN_COMMAND, msg, msg_len); json_node_free(root); @@ -177,9 +177,8 @@ static void ipc_handle(DwmIpc *dwm, DwmIpcMessage type, char *reply, uint32_t re json_reader_end_member (reader); json_reader_end_member (reader); - log_debug("Dwm tag mask: %ld", selected); + log_debug("Selected tag mask: %ld", selected); - // FIXME: Why is it so slow?? if (dwm->tags[0] != NULL) { for (int i = 0; i < 9; i++) dwm->tags[i]->color = selected & (1 << i) ? dwm->selected : dwm->color; @@ -218,22 +217,15 @@ static void ipc_handle(DwmIpc *dwm, DwmIpcMessage type, char *reply, uint32_t re g_object_unref (parser); } -static gboolean source_check(GSource *source) +static gboolean socket_callback(GSocket *socket, GIOCondition condition, gpointer data) { - DwmIpc *dwm = (DwmIpc *)source; - GIOCondition flags = g_socket_condition_check(dwm->socket, G_IO_IN); - return flags & G_IO_IN; -} - -static gboolean source_dispatch(GSource *source, GSourceFunc callback, gpointer data) -{ - DwmIpc *dwm = (DwmIpc *)source; - DwmIpcHeader header; + if (condition & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) return G_SOURCE_REMOVE; GError *error = NULL; + DwmIpcHeader header; gssize size = 0; while (size < sizeof(DwmIpcHeader)) { - gssize read = g_socket_receive(dwm->socket, (void *)&header + size, sizeof(DwmIpcHeader) - size, NULL, &error); + gssize read = g_socket_receive(socket, (void *)&header + size, sizeof(DwmIpcHeader) - size, NULL, &error); if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) { g_error_free(error); continue; @@ -243,7 +235,7 @@ static gboolean source_dispatch(GSource *source, GSourceFunc callback, gpointer } if (memcmp(&header, IPC_MAGIC, IPC_MAGIC_LEN) != 0) { - log_warning("Erroneous ipc message"); + log_warning("Malformed dwm ipc message"); return G_SOURCE_CONTINUE;; } @@ -254,10 +246,9 @@ static gboolean source_dispatch(GSource *source, GSourceFunc callback, gpointer memcpy(&msg_type, (void *)&header + IPC_MAGIC_LEN + sizeof(uint32_t), sizeof(uint8_t)); char *reply = g_malloc(reply_size); - size = 0; while (size < reply_size) { - gssize read = g_socket_receive(dwm->socket, reply + size, reply_size - size, NULL, NULL); + gssize read = g_socket_receive(socket, reply + size, reply_size - size, NULL, NULL); if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) { g_error_free(error); continue; @@ -267,27 +258,16 @@ static gboolean source_dispatch(GSource *source, GSourceFunc callback, gpointer } //log_debug("Received dwm ipc response: %s", reply); - ipc_handle(dwm, msg_type, reply, reply_size); + ipc_handle(data, msg_type, reply, reply_size); return G_SOURCE_CONTINUE; } -static void source_finalize(GSource *source) -{ - g_object_unref(((DwmIpc *)source)->socket); -} - -static GSourceFuncs source_fns = { - NULL, - source_check, - source_dispatch, - source_finalize, -}; - DwmIpc *dwm_create(State *state, const char *socket) { - DwmIpc *dwm = (DwmIpc *)g_source_new(&source_fns, sizeof(DwmIpc)); - g_source_set_static_name((GSource *)dwm, "DwmIpc"); + DwmIpc *dwm = g_malloc0(sizeof(DwmIpc)); + dwm->state = state; + dwm->tags[0] = NULL; GError *error = NULL; dwm->socket = g_socket_new(G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, 0, &error); @@ -299,17 +279,17 @@ DwmIpc *dwm_create(State *state, const char *socket) log_error("Failed to connect to %s: %s", socket, error->message); } - dwm->state = state; - dwm->tags[0] = NULL; + dwm->source = g_socket_create_source(dwm->socket, G_IO_IN, NULL); + g_source_set_static_name(dwm->source, "DwmIpcSource"); + g_source_set_callback(dwm->source, (GSourceFunc)socket_callback, dwm, NULL); const char *events[] = { IPC_EVENT_TAG_CHANGE, - IPC_EVENT_FOCUSED_STATE_CHANGE, }; for (int i = 0; i < G_N_ELEMENTS(events); ++i) ipc_subscribe(dwm, events[i]); - g_source_attach((GSource *)dwm, NULL); + g_source_attach(dwm->source, NULL); log_debug("Attached dwm ipc source"); return dwm; @@ -349,8 +329,10 @@ void dwm_register_tags(DwmIpc *dwm, Color color, Color selected, Color text_colo void dwm_destroy(DwmIpc *dwm) { - g_source_destroy((GSource *)dwm); - g_source_unref((GSource *)dwm); + g_source_destroy(dwm->source); + g_source_unref(dwm->source); + g_object_unref(dwm->socket); + g_free(dwm); } // vim: ts=4 sw=4 et -- cgit v1.2.3