diff --git a/cagebreak.c b/cagebreak.c index 40b97d1..a772b0e 100644 --- a/cagebreak.c +++ b/cagebreak.c @@ -357,6 +357,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 76e0ccc..d4f1a52 100644 --- a/keybinding.c +++ b/keybinding.c @@ -1615,6 +1615,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 184dbca..8063ff5 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 a7fa4d6..d7e4d37 100644 --- a/message.c +++ b/message.c @@ -176,11 +176,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"); @@ -202,26 +202,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: { - message->position->x -= width; + switch(anchor) { + case CG_MESSAGE_TOP_LEFT: + message->position->x = 0; + message->position->y = 0; break; - } - case CG_MESSAGE_BOTTOM_LEFT: { + 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: + 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; } @@ -262,12 +271,44 @@ message_printf(struct cg_output *output, const char *fmt, ...) { free(buffer); return; } - box->x = output_get_layout_box(output).width; - box->y = 0; + struct wlr_box output_box=output_get_layout_box(output); 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; + 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, CG_MESSAGE_TOP_RIGHT); + message_set_output(output, buffer, box, + output->server->message_config.anchor); free(buffer); alarm(output->server->message_config.display_time); } @@ -277,7 +318,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, ...) { if(output->destroyed) { free(position); return; @@ -290,7 +331,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..1eb6664 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 28e218e..b76e3ed 100644 --- a/parse.c +++ b/parse.c @@ -720,6 +720,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) { @@ -763,6 +764,22 @@ parse_message_config(char **saveptr, char **errstr) { goto error; } } + } 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;