Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add in an option to configure the anchor position of messages #61

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cagebreak.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
3 changes: 3 additions & 0 deletions keybinding.c
Original file line number Diff line number Diff line change
Expand Up @@ -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\"}");
}

Expand Down
4 changes: 3 additions & 1 deletion man/cagebreak-config.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ definekey root <key> <command>
Close current window - This may be useful for windows of
applications which do not offer any method of closing them.

*configure_message [font <font description>|[f|b]g_color <r> <g> b> <a>|display_time <n>]*
*configure_message [font <font description>|[f|b]g_color <r> <g> b> <a>|display_time <n>|align <alignment>]*
Configure message characteristics -
- font <font description> sets
- <font description> is
Expand All @@ -61,6 +61,8 @@ definekey root <key> <command>
- fg_color <r> <g> <b> <a> sets RGBA of foreground
- bg_color <r> <g> <b> <a> sets RGBA of background
- display_time <n> sets display time in seconds
- anchor <position> sets the position of the message
- <position> may be one of {top,bottom}_{left,center,right} or center

```
# Set font
Expand Down
74 changes: 57 additions & 17 deletions message.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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;
}
Expand Down Expand Up @@ -266,12 +275,43 @@ message_printf(struct cg_output *output, const char *fmt, ...) {
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->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);
}
Expand All @@ -281,7 +321,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;
Expand All @@ -290,7 +330,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);
}
Expand Down
8 changes: 6 additions & 2 deletions message.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ 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 {
char *font;
int display_time;
float bg_color[4];
float fg_color[4];
enum cg_message_anchor anchor;
};

struct cg_message {
Expand All @@ -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);

Expand Down
17 changes: 17 additions & 0 deletions parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -745,6 +746,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;
Expand Down