From 00f9c9ce293d58862f53a39bcaad2718c5adbce5 Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Thu, 8 Feb 2024 13:01:06 +0100 Subject: Implement tag switching action --- src/dwm.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/src/dwm.c b/src/dwm.c index 990f269..8a4aad5 100644 --- a/src/dwm.c +++ b/src/dwm.c @@ -113,6 +113,47 @@ static void ipc_subscribe(DwmIpc *dwm, const char *event) g_free(msg); } +static void ipc_run_command(DwmIpc *dwm, const char *cmd, JsonNode **args) +{ + // { + // "command": "", + // "args": [ ... ] + // } + + JsonBuilder *builder = json_builder_new(); + + json_builder_begin_object(builder); + + json_builder_set_member_name(builder, "command"); + json_builder_add_string_value(builder, cmd); + + json_builder_set_member_name(builder, "args"); + json_builder_begin_array(builder); + + while (*args != NULL) { + json_builder_add_value(builder, *args++); + } + + json_builder_end_array(builder); + json_builder_end_object(builder); + + JsonNode *root = json_builder_get_root(builder); + + JsonGenerator *gen = json_generator_new(); + json_generator_set_root(gen, root); + + gsize msg_len = 0; + gchar *msg = json_generator_to_data(gen, &msg_len); + + log_info("Sending command to dwm ipc: %s", msg); + ipc_send(dwm, IPC_TYPE_RUN_COMMAND, msg, msg_len); + + json_node_free(root); + g_object_unref(gen); + g_object_unref(builder); + g_free(msg); +} + static void ipc_handle(DwmIpc *dwm, DwmIpcMessage type, char *reply, uint32_t reply_size) { GError *error = NULL; @@ -170,6 +211,7 @@ static void ipc_handle(DwmIpc *dwm, DwmIpcMessage type, char *reply, uint32_t re } } else { log_debug("Ignoring dwm ipc message [type=%d]", type); + //log_debug("%s", reply); } g_object_unref (reader); @@ -276,11 +318,18 @@ DwmIpc *dwm_create(State *state, const char *socket) static void change_tag_action(Button *btn) { ButtonSimple *sbtn = CAST(btn, ButtonSimple); + DwmIpc *dwm = sbtn->action_data; int n = sbtn->text[0] - '0'; g_assert(n >= 1 && n <= 9); - log_debug("Toggled tag %d",n ); + log_info("Toggled tag %d", n); + + JsonNode *args[] = { + json_node_init_int(json_node_alloc(), 1 << (n - 1)), + NULL + }; + ipc_run_command(dwm, "view", args); } void dwm_register_tags(DwmIpc *dwm, Color color, Color selected, Color text_color, Color line_color) @@ -293,7 +342,7 @@ void dwm_register_tags(DwmIpc *dwm, Color color, Color selected, Color text_colo char text[] = { '1' + i, '\0' }; dwm->tags[i] = button_simple_create(PANGO_ALIGN_LEFT, color, line_color); button_simple_set_text(dwm->tags[i], g_strdup(text), text_color); - button_simple_set_action(dwm->tags[i], change_tag_action, dwm->state); + button_simple_set_action(dwm->tags[i], change_tag_action, dwm); state_add_button(dwm->state, dwm->tags[i]); } } -- cgit v1.2.3