From f26892d938ef5a91848d81963a22bbde455f28ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 20 Jan 2019 22:05:12 +0100 Subject: [PATCH] bar: allow user to specify _which_ monitor to place the bar on If not specified, the primary monitor will be used. --- README.md | 4 +++- bar.c | 29 +++++++++++++++++++++++++---- bar.h | 1 + config-verify.c | 2 ++ config.c | 4 ++++ 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9edf0dd..ecec3f1 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ **f00bar** is a light-weight and configurable status panel (_bar_, for short) for X. -It has a number of _modules_ that provides information in the form of +It has a number of _modules_ that provide information in the form of _tags_. For example, the _clock_ module has a _date_ tag that contains the current date. @@ -161,6 +161,8 @@ There are a couple types used that are specific to f00bar. be self-explanatory. - `background` (_color_, **required**): background color, in _rgba_. Thus, in the example above, the background is set to _black_ +- `monitor` (_string_): monitor to place the bar. If not specified, + the primary monitor will be used. - `left-spacing` (_int_): space, in pixels, added **before** each module - `right-spacing` (_int_): space, in pixels, added **after** each module - `spacing` (_int_): short-hand for setting both `left-spacing` and diff --git a/bar.c b/bar.c index c5a556c..2f7e8a7 100644 --- a/bar.c +++ b/bar.c @@ -30,6 +30,7 @@ struct private { /* From bar_config */ + char *monitor; enum bar_location location; int height; int left_spacing, right_spacing; @@ -339,11 +340,18 @@ run(struct bar *_bar) bar->conn, xcb_randr_get_monitors(bar->conn, screen->root, 0), &e); - assert(e == NULL); + + if (e != NULL) { + LOG_ERR("failed to get monitor list: %s", xcb_error(e)); + free(e); + /* TODO: cleanup (disconnect) */ + return 1; + } bar->height_with_border = bar->height + 2 * bar->border.width; /* Find monitor coordinates and width/height */ + bool found_monitor = false; for (xcb_randr_monitor_info_iterator_t it = xcb_randr_get_monitors_monitors_iterator(monitors); it.rem > 0; @@ -356,20 +364,31 @@ run(struct bar *_bar) mon->width, mon->height, mon->x, mon->y, mon->width_in_millimeters, mon->height_in_millimeters); - free(name); - - if (!mon->primary) + if (!((bar->monitor == NULL && mon->primary) || + (bar->monitor != NULL && strcmp(bar->monitor, name) == 0))) + { + free(name); continue; + } + + free(name); bar->x = mon->x; bar->y = mon->y; bar->width = mon->width; bar->y += bar->location == BAR_TOP ? 0 : screen->height_in_pixels - bar->height_with_border; + found_monitor = true; break; } free(monitors); + if (!found_monitor) { + LOG_ERR("no matching monitor"); + /* TODO: cleanup */ + return 1; + } + uint8_t depth = 0; xcb_visualtype_t *vis = xcb_aux_find_visual_by_attrs(screen, -1, 32); @@ -690,6 +709,7 @@ destroy(struct bar *bar) free(b->center.exps); free(b->right.mods); free(b->right.exps); + free(b->monitor); free(bar->private); free(bar); @@ -699,6 +719,7 @@ struct bar * bar_new(const struct bar_config *config) { struct private *priv = malloc(sizeof(*priv)); + priv->monitor = config->monitor != NULL ? strdup(config->monitor) : NULL; priv->location = config->location; priv->height = config->height; priv->background = config->background; diff --git a/bar.h b/bar.h index 5559584..3839b01 100644 --- a/bar.h +++ b/bar.h @@ -17,6 +17,7 @@ struct bar { enum bar_location { BAR_TOP, BAR_BOTTOM }; struct bar_config { + const char *monitor; enum bar_location location; int height; int left_spacing, right_spacing; diff --git a/config-verify.c b/config-verify.c index aca6944..f947979 100644 --- a/config-verify.c +++ b/config-verify.c @@ -359,6 +359,8 @@ conf_verify_bar(const struct yml_node *bar) {"location", true, &verify_bar_location}, {"background", true, &conf_verify_color}, + {"monitor", false, &conf_verify_string}, + {"spacing", false, &conf_verify_int}, {"left-spacing", false, &conf_verify_int}, {"right-spacing", false, &conf_verify_int}, diff --git a/config.c b/config.c index 8c2a6d4..fdfc1b4 100644 --- a/config.c +++ b/config.c @@ -194,6 +194,10 @@ conf_to_bar(const struct yml_node *bar) * Optional attributes */ + const struct yml_node *monitor = yml_get_value(bar, "monitor"); + if (monitor != NULL) + conf.monitor = yml_value_as_string(monitor); + const struct yml_node *spacing = yml_get_value(bar, "spacing"); if (spacing != NULL) conf.left_spacing = conf.right_spacing = yml_value_as_int(spacing);