From 19a3d1c992fb0a567734e4abc714586e2c27ae21 Mon Sep 17 00:00:00 2001 From: unsigned-enby Date: Mon, 20 Nov 2023 23:50:27 -0600 Subject: [PATCH 1/2] Allow for the configuration of message position --- cagebreak.c | 1 + keybinding.c | 3 ++ man/cagebreak-config.5.md | 4 +- message.c | 79 +++++++++++++++++++++++++++++---------- message.h | 8 +++- parse.c | 17 ++++++++- 6 files changed, 88 insertions(+), 24 deletions(-) diff --git a/cagebreak.c b/cagebreak.c index 4437331..6940db9 100644 --- a/cagebreak.c +++ b/cagebreak.c @@ -356,6 +356,7 @@ main(int argc, char *argv[]) { server.message_config.display_time = 2; server.message_config.font = strdup("pango:Monospace 10"); + server.message_config.anchor = CG_MESSAGE_TOP_RIGHT; event_loop = wl_display_get_event_loop(server.wl_display); sigint_source = diff --git a/keybinding.c b/keybinding.c index eaf2ae7..38ba60b 100644 --- a/keybinding.c +++ b/keybinding.c @@ -1620,6 +1620,9 @@ keybinding_configure_message(struct cg_server *server, server->message_config.fg_color[2] = config->fg_color[2]; server->message_config.fg_color[3] = config->fg_color[3]; } + if(config->anchor != CG_MESSAGE_NOPT) { + server->message_config.anchor = config->anchor; + } ipc_send_event(server, "{\"event_name\":\"configure_message\"}"); } diff --git a/man/cagebreak-config.5.md b/man/cagebreak-config.5.md index f894d04..04536aa 100644 --- a/man/cagebreak-config.5.md +++ b/man/cagebreak-config.5.md @@ -52,7 +52,7 @@ definekey root Close current window - This may be useful for windows of applications which do not offer any method of closing them. -*configure_message [font |[f|b]g_color b> |display_time ]* +*configure_message [font |[f|b]g_color b> |display_time |align ]* Configure message characteristics - - font sets - is @@ -61,6 +61,8 @@ definekey root - fg_color sets RGBA of foreground - bg_color sets RGBA of background - display_time sets display time in seconds + - anchor sets the position of the message + - may be one of {top,bottom}_{left,center,right} or center ``` # Set font diff --git a/message.c b/message.c index 74b1d0a..56a1111 100644 --- a/message.c +++ b/message.c @@ -177,11 +177,11 @@ create_message_texture(const char *string, const struct cg_output *output) { #if CG_HAS_FANALYZE #pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak" +#pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak" //NOLINT #endif void message_set_output(struct cg_output *output, const char *string, - struct wlr_box *box, enum cg_message_align align) { + struct wlr_box *box, enum cg_message_anchor anchor) { struct cg_message *message = malloc(sizeof(struct cg_message)); if(!message) { wlr_log(WLR_ERROR, "Error allocating message structure"); @@ -203,26 +203,35 @@ message_set_output(struct cg_output *output, const char *string, int height = buf->base.height / scale; message->position->width = width; message->position->height = height; - switch(align) { - case CG_MESSAGE_TOP_RIGHT: { + switch(anchor) { + case CG_MESSAGE_TOP_LEFT: + message->position->x = 0; + message->position->y = 0; + break; + case CG_MESSAGE_TOP_CENTER: + message->position->x -= width / 2; + message->position->y = 0; + break; + case CG_MESSAGE_TOP_RIGHT: message->position->x -= width; + message->position->y = 0; break; - } - case CG_MESSAGE_BOTTOM_LEFT: { + case CG_MESSAGE_BOTTOM_LEFT: + message->position->x = 0; message->position->y -= height; break; - } - case CG_MESSAGE_BOTTOM_RIGHT: { + case CG_MESSAGE_BOTTOM_CENTER: + message->position->x -= width / 2; + message->position->y -= height; + break; + case CG_MESSAGE_BOTTOM_RIGHT: message->position->x -= width; message->position->y -= height; break; - } - case CG_MESSAGE_CENTER: { + case CG_MESSAGE_CENTER: message->position->x -= width / 2; message->position->y -= height / 2; break; - } - case CG_MESSAGE_TOP_LEFT: default: break; } @@ -265,13 +274,43 @@ message_printf(struct cg_output *output, const char *fmt, ...) { struct wlr_box output_box; wlr_output_layout_get_box(output->server->output_layout, output->wlr_output, &output_box); - - box->x = output_box.width; - box->y = 0; - box->width = 0; + + box->width = 0; box->height = 0; - - message_set_output(output, buffer, box, CG_MESSAGE_TOP_RIGHT); + switch(output->server->message_config.anchor) { + case CG_MESSAGE_TOP_LEFT: + box->x = 0; + box->y = 0; + break; + case CG_MESSAGE_TOP_CENTER: + box->x = output_box.width /2; + box->y = 0; + break; + case CG_MESSAGE_TOP_RIGHT: + box->x = output_box.width; + box->y = 0; + break; + case CG_MESSAGE_BOTTOM_LEFT: + box->x = 0; + box->y = output_box.height; + break; + case CG_MESSAGE_BOTTOM_CENTER: + box->x = output_box.width /2; + box->y = output_box.height; + break; + case CG_MESSAGE_BOTTOM_RIGHT: + box->x = output_box.width; + box->y = output_box.height; + break; + case CG_MESSAGE_CENTER: + box->x = output_box.width / 2; + box->y = output_box.height / 2; + break; + default: + break; + } + + message_set_output(output, buffer, box, output->server->message_config.anchor); free(buffer); alarm(output->server->message_config.display_time); } @@ -281,7 +320,7 @@ message_printf(struct cg_output *output, const char *fmt, ...) { void message_printf_pos(struct cg_output *output, struct wlr_box *position, - const enum cg_message_align align, const char *fmt, ...) { + const enum cg_message_anchor anchor, const char *fmt, ...) { uint16_t buf_len = 256; char *buffer = (char *)malloc(buf_len * sizeof(char)); va_list ap; @@ -290,7 +329,7 @@ message_printf_pos(struct cg_output *output, struct wlr_box *position, vsnprintf(buffer, buf_len, fmt, ap); va_end(ap); - message_set_output(output, buffer, position, align); + message_set_output(output, buffer, position, anchor); free(buffer); alarm(output->server->message_config.display_time); } diff --git a/message.h b/message.h index 806307c..715fde3 100644 --- a/message.h +++ b/message.h @@ -11,12 +11,15 @@ struct cg_output; struct wlr_box; struct wlr_buffer; -enum cg_message_align { +enum cg_message_anchor { CG_MESSAGE_TOP_LEFT, + CG_MESSAGE_TOP_CENTER, CG_MESSAGE_TOP_RIGHT, CG_MESSAGE_BOTTOM_LEFT, + CG_MESSAGE_BOTTOM_CENTER, CG_MESSAGE_BOTTOM_RIGHT, CG_MESSAGE_CENTER, + CG_MESSAGE_NOPT }; struct cg_message_config { @@ -24,6 +27,7 @@ struct cg_message_config { int display_time; float bg_color[4]; float fg_color[4]; + enum cg_message_anchor anchor; }; struct cg_message { @@ -37,7 +41,7 @@ void message_printf(struct cg_output *output, const char *fmt, ...); void message_printf_pos(struct cg_output *output, struct wlr_box *position, - enum cg_message_align, const char *fmt, ...); + enum cg_message_anchor, const char *fmt, ...); void message_clear(struct cg_output *output); diff --git a/parse.c b/parse.c index c163b3a..70d2724 100644 --- a/parse.c +++ b/parse.c @@ -702,6 +702,7 @@ parse_message_config(char **saveptr, char **errstr) { cfg->fg_color[0] = -1; cfg->display_time = -1; cfg->font = NULL; + cfg->anchor = CG_MESSAGE_NOPT; char *setting = strtok_r(NULL, " ", saveptr); if(setting == NULL) { @@ -745,7 +746,21 @@ parse_message_config(char **saveptr, char **errstr) { goto error; } } - } else { + } else if(strcmp(setting, "anchor") == 0) { + char* anchors[] = {"top_left", "top_center", "top_right", "bottom_left", "bottom_center", "bottom_right", "center"}; + for(int i = 0; i < 7; ++i) { + if(strcmp(*saveptr, anchors[i]) == 0) { + cfg->anchor = i; + break; + } + } + if(cfg->anchor == CG_MESSAGE_NOPT) { + *errstr = log_error( + "Error parsing command \"configure_message anchor\", " + "the given anchor value is not a valid option"); + goto error; + } + } else { *errstr = log_error("Invalid option to command \"configure_message\""); goto error; } From 7c8cfdc380617fd17ec793f6447dd5f3e5ab8120 Mon Sep 17 00:00:00 2001 From: unsigned-enby Date: Tue, 21 Nov 2023 00:00:58 -0600 Subject: [PATCH 2/2] Fixed indentation --- cagebreak.c | 2 +- keybinding.c | 6 ++-- man/cagebreak-config.5.md | 4 +-- message.c | 67 ++++++++++++++++++++------------------- message.h | 8 ++--- parse.c | 32 ++++++++++--------- 6 files changed, 61 insertions(+), 58 deletions(-) diff --git a/cagebreak.c b/cagebreak.c index 6940db9..78d6fdb 100644 --- a/cagebreak.c +++ b/cagebreak.c @@ -356,7 +356,7 @@ main(int argc, char *argv[]) { server.message_config.display_time = 2; server.message_config.font = strdup("pango:Monospace 10"); - server.message_config.anchor = CG_MESSAGE_TOP_RIGHT; + server.message_config.anchor = CG_MESSAGE_TOP_RIGHT; event_loop = wl_display_get_event_loop(server.wl_display); sigint_source = diff --git a/keybinding.c b/keybinding.c index 38ba60b..64cacfb 100644 --- a/keybinding.c +++ b/keybinding.c @@ -1620,9 +1620,9 @@ keybinding_configure_message(struct cg_server *server, server->message_config.fg_color[2] = config->fg_color[2]; server->message_config.fg_color[3] = config->fg_color[3]; } - if(config->anchor != CG_MESSAGE_NOPT) { - server->message_config.anchor = config->anchor; - } + if(config->anchor != CG_MESSAGE_NOPT) { + server->message_config.anchor = config->anchor; + } ipc_send_event(server, "{\"event_name\":\"configure_message\"}"); } diff --git a/man/cagebreak-config.5.md b/man/cagebreak-config.5.md index 04536aa..dc3517b 100644 --- a/man/cagebreak-config.5.md +++ b/man/cagebreak-config.5.md @@ -61,8 +61,8 @@ definekey root - fg_color sets RGBA of foreground - bg_color sets RGBA of background - display_time sets display time in seconds - - anchor sets the position of the message - - may be one of {top,bottom}_{left,center,right} or center + - anchor sets the position of the message + - may be one of {top,bottom}_{left,center,right} or center ``` # Set font diff --git a/message.c b/message.c index 56a1111..04a3d55 100644 --- a/message.c +++ b/message.c @@ -177,7 +177,7 @@ create_message_texture(const char *string, const struct cg_output *output) { #if CG_HAS_FANALYZE #pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak" //NOLINT +#pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak" // NOLINT #endif void message_set_output(struct cg_output *output, const char *string, @@ -205,23 +205,23 @@ message_set_output(struct cg_output *output, const char *string, message->position->height = height; switch(anchor) { case CG_MESSAGE_TOP_LEFT: - message->position->x = 0; - message->position->y = 0; - break; + message->position->x = 0; + message->position->y = 0; + break; case CG_MESSAGE_TOP_CENTER: - message->position->x -= width / 2; - message->position->y = 0; - break; + message->position->x -= width / 2; + message->position->y = 0; + break; case CG_MESSAGE_TOP_RIGHT: message->position->x -= width; - message->position->y = 0; + message->position->y = 0; break; case CG_MESSAGE_BOTTOM_LEFT: - message->position->x = 0; + message->position->x = 0; message->position->y -= height; break; case CG_MESSAGE_BOTTOM_CENTER: - message->position->x -= width / 2; + message->position->x -= width / 2; message->position->y -= height; break; case CG_MESSAGE_BOTTOM_RIGHT: @@ -274,43 +274,44 @@ message_printf(struct cg_output *output, const char *fmt, ...) { struct wlr_box output_box; wlr_output_layout_get_box(output->server->output_layout, output->wlr_output, &output_box); - - box->width = 0; + + box->width = 0; box->height = 0; switch(output->server->message_config.anchor) { case CG_MESSAGE_TOP_LEFT: - box->x = 0; - box->y = 0; - break; - case CG_MESSAGE_TOP_CENTER: - box->x = output_box.width /2; - box->y = 0; - break; + box->x = 0; + box->y = 0; + break; + case CG_MESSAGE_TOP_CENTER: + box->x = output_box.width / 2; + box->y = 0; + break; case CG_MESSAGE_TOP_RIGHT: - box->x = output_box.width; - box->y = 0; + box->x = output_box.width; + box->y = 0; break; case CG_MESSAGE_BOTTOM_LEFT: - box->x = 0; - box->y = output_box.height; + box->x = 0; + box->y = output_box.height; + break; + case CG_MESSAGE_BOTTOM_CENTER: + box->x = output_box.width / 2; + box->y = output_box.height; break; - case CG_MESSAGE_BOTTOM_CENTER: - box->x = output_box.width /2; - box->y = output_box.height; - break; case CG_MESSAGE_BOTTOM_RIGHT: - box->x = output_box.width; - box->y = output_box.height; + box->x = output_box.width; + box->y = output_box.height; break; case CG_MESSAGE_CENTER: - box->x = output_box.width / 2; - box->y = output_box.height / 2; + box->x = output_box.width / 2; + box->y = output_box.height / 2; break; default: break; } - - message_set_output(output, buffer, box, output->server->message_config.anchor); + + message_set_output(output, buffer, box, + output->server->message_config.anchor); free(buffer); alarm(output->server->message_config.display_time); } diff --git a/message.h b/message.h index 715fde3..1eb6664 100644 --- a/message.h +++ b/message.h @@ -13,13 +13,13 @@ struct wlr_buffer; enum cg_message_anchor { CG_MESSAGE_TOP_LEFT, - CG_MESSAGE_TOP_CENTER, + CG_MESSAGE_TOP_CENTER, CG_MESSAGE_TOP_RIGHT, CG_MESSAGE_BOTTOM_LEFT, - CG_MESSAGE_BOTTOM_CENTER, + CG_MESSAGE_BOTTOM_CENTER, CG_MESSAGE_BOTTOM_RIGHT, CG_MESSAGE_CENTER, - CG_MESSAGE_NOPT + CG_MESSAGE_NOPT }; struct cg_message_config { @@ -27,7 +27,7 @@ struct cg_message_config { int display_time; float bg_color[4]; float fg_color[4]; - enum cg_message_anchor anchor; + enum cg_message_anchor anchor; }; struct cg_message { diff --git a/parse.c b/parse.c index 70d2724..63d88f4 100644 --- a/parse.c +++ b/parse.c @@ -702,7 +702,7 @@ parse_message_config(char **saveptr, char **errstr) { cfg->fg_color[0] = -1; cfg->display_time = -1; cfg->font = NULL; - cfg->anchor = CG_MESSAGE_NOPT; + cfg->anchor = CG_MESSAGE_NOPT; char *setting = strtok_r(NULL, " ", saveptr); if(setting == NULL) { @@ -747,20 +747,22 @@ parse_message_config(char **saveptr, char **errstr) { } } } else if(strcmp(setting, "anchor") == 0) { - char* anchors[] = {"top_left", "top_center", "top_right", "bottom_left", "bottom_center", "bottom_right", "center"}; - for(int i = 0; i < 7; ++i) { - if(strcmp(*saveptr, anchors[i]) == 0) { - cfg->anchor = i; - break; - } - } - if(cfg->anchor == CG_MESSAGE_NOPT) { - *errstr = log_error( - "Error parsing command \"configure_message anchor\", " - "the given anchor value is not a valid option"); - goto error; - } - } else { + char *anchors[] = {"top_left", "top_center", "top_right", + "bottom_left", "bottom_center", "bottom_right", + "center"}; + for(int i = 0; i < 7; ++i) { + if(strcmp(*saveptr, anchors[i]) == 0) { + cfg->anchor = i; + break; + } + } + if(cfg->anchor == CG_MESSAGE_NOPT) { + *errstr = + log_error("Error parsing command \"configure_message anchor\", " + "the given anchor value is not a valid option"); + goto error; + } + } else { *errstr = log_error("Invalid option to command \"configure_message\""); goto error; }