From f6481fd8baf793de17cb071e17ef7208f9db40c0 Mon Sep 17 00:00:00 2001 From: Phil Jones Date: Fri, 14 Oct 2022 17:02:04 +0100 Subject: Add --exclusive-zone option. --- completions/tofi | 1 + doc/config | 10 +++++ doc/tofi.5.md | 10 +++++ doc/tofi.5.scd | 9 +++++ src/config.c | 115 +++++++++++++++++++++++++++++++++++++++++++------------ src/main.c | 5 ++- src/tofi.h | 2 + 7 files changed, 127 insertions(+), 25 deletions(-) diff --git a/completions/tofi b/completions/tofi index 4d46d27..b23e4a8 100644 --- a/completions/tofi +++ b/completions/tofi @@ -32,6 +32,7 @@ _tofi() --text-color --width --height + --exclusive-zone --margin-top --margin-bottom --margin-left diff --git a/doc/config b/doc/config index 4f81163..d7ec8b5 100644 --- a/doc/config +++ b/doc/config @@ -111,6 +111,16 @@ # bottom, bottom-left, left, center. anchor = center + # Set the size of the exclusive zone. + # + # A value of -1 means ignore exclusive zones completely. + # A value of 0 will move tofi out of the way of other windows' zones. + # A value greater than 0 will set that much space as an exclusive zone. + # + # Values greater than 0 are only meaningful when tofi is anchored to a + # single edge. + exclusive-zone = -1 + # Window offset from edge of screen. Only has an effect when anchored # to the relevant edge. Can be pixels or a percentage. margin-top = 0 diff --git a/doc/tofi.5.md b/doc/tofi.5.md index 319171d..2ddabb4 100644 --- a/doc/tofi.5.md +++ b/doc/tofi.5.md @@ -240,6 +240,16 @@ options. > > Default: center +**exclusive-zone**=*-1\|px\|%* + +> Set the size of the exclusive zone. A value of -1 means ignore +> exclusive zones completely. A value of 0 will move tofi out of the way +> of other windows' exclusive zones. A value greater than 0 will set +> that much space as an exclusive zone. Values greater than 0 are only +> meaningful when tofi is anchored to a single edge. +> +> Default: -1 + **output**=*name* > The name of the output to appear on, if multiple outputs are present. diff --git a/doc/tofi.5.scd b/doc/tofi.5.scd index cad1dbb..7e16ea7 100644 --- a/doc/tofi.5.scd +++ b/doc/tofi.5.scd @@ -210,6 +210,15 @@ options. Default: center +*exclusive-zone*=_-1|px|%_ + Set the size of the exclusive zone. A value of -1 means ignore exclusive + zones completely. A value of 0 will move tofi out of the way of other + windows' exclusive zones. A value greater than 0 will set that much space as + an exclusive zone. Values greater than 0 are only meaningful when tofi is + anchored to a single edge. + + Default: -1 + *output*=_name_ The name of the output to appear on, if multiple outputs are present. If empty, the compositor will choose which output to display the window diff --git a/src/config.c b/src/config.c index 59b8012..a1bfb7a 100644 --- a/src/config.c +++ b/src/config.c @@ -21,6 +21,50 @@ /* Anyone with a 10M config file is doing something very wrong */ #define MAX_CONFIG_SIZE (10*1024*1024) +/* Convenience macros for anchor combinations */ +#define ANCHOR_TOP_LEFT (\ + ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP \ + | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT \ + ) +#define ANCHOR_TOP (\ + ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP \ + | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT \ + | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT \ + ) +#define ANCHOR_TOP_RIGHT (\ + ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP \ + | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT \ + ) +#define ANCHOR_RIGHT (\ + ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT \ + | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP \ + | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM \ + ) +#define ANCHOR_BOTTOM_RIGHT (\ + ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM \ + | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT \ + ) +#define ANCHOR_BOTTOM (\ + ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM \ + | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT \ + | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT \ + ) +#define ANCHOR_BOTTOM_LEFT (\ + ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM \ + | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT \ + ) +#define ANCHOR_LEFT (\ + ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT \ + | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP \ + | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM \ + ) +#define ANCHOR_CENTER (\ + ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP \ + | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM \ + | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT \ + | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT \ + ) + struct uint32_percent { uint32_t value; bool percent; @@ -308,6 +352,14 @@ bool parse_option(struct tofi *tofi, const char *filename, size_t lineno, const tofi->window.entry.selection_background_padding = parse_int32(filename, lineno, value, &err); } else if (strcasecmp(option, "selection-background") == 0) { tofi->window.entry.selection_background_color = parse_color(filename, lineno, value, &err); + } else if (strcasecmp(option, "exclusive-zone") == 0) { + if (strcmp(value, "-1") == 0) { + tofi->window.exclusive_zone = -1; + } else { + percent = parse_uint32_percent(filename, lineno, value, &err); + tofi->window.exclusive_zone = percent.value; + tofi->window.exclusive_zone_is_percent = percent.percent; + } } else if (strcasecmp(option, "width") == 0) { percent = parse_uint32_percent(filename, lineno, value, &err); tofi->window.width = percent.value; @@ -489,6 +541,36 @@ void config_fixup_values(struct tofi *tofi) tofi->window.entry.padding_right_is_percent, tofi->window.scale, tofi->use_scale); + + /* Exclusive zone base depends on anchor. */ + switch (tofi->anchor) { + case ANCHOR_TOP: + case ANCHOR_BOTTOM: + tofi->window.exclusive_zone = fixup_percentage( + tofi->window.exclusive_zone, + tofi->output_height, + tofi->window.exclusive_zone_is_percent, + tofi->window.scale, + tofi->use_scale); + break; + case ANCHOR_LEFT: + case ANCHOR_RIGHT: + tofi->window.exclusive_zone = fixup_percentage( + tofi->window.exclusive_zone, + tofi->output_width, + tofi->window.exclusive_zone_is_percent, + tofi->window.scale, + tofi->use_scale); + break; + default: + /* + * Exclusive zone >0 is meaningless for other anchor + * positions. + */ + tofi->window.exclusive_zone = + MIN(tofi->window.exclusive_zone, 0); + break; + } } char *get_config_path() @@ -544,46 +626,31 @@ wchar_t parse_wchar(const char *filename, size_t lineno, const char *str, bool * uint32_t parse_anchor(const char *filename, size_t lineno, const char *str, bool *err) { if(strcasecmp(str, "top-left") == 0) { - return ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP - | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT; + return ANCHOR_TOP_LEFT; } if (strcasecmp(str, "top") == 0) { - return ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP - | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT - | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + return ANCHOR_TOP; } if (strcasecmp(str, "top-right") == 0) { - return ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP - | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + return ANCHOR_TOP_RIGHT; } if (strcasecmp(str, "right") == 0) { - return ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT - | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP - | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM; + return ANCHOR_RIGHT; } if (strcasecmp(str, "bottom-right") == 0) { - return ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM - | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + return ANCHOR_BOTTOM_RIGHT; } if (strcasecmp(str, "bottom") == 0) { - return ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM - | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT - | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + return ANCHOR_BOTTOM; } if (strcasecmp(str, "bottom-left") == 0) { - return ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM - | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT; + return ANCHOR_BOTTOM_LEFT; } if (strcasecmp(str, "left") == 0) { - return ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT - | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP - | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM; + return ANCHOR_LEFT; } if (strcasecmp(str, "center") == 0) { - return ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP - | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM - | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT - | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + return ANCHOR_CENTER; } PARSE_ERROR(filename, lineno, "Invalid anchor \"%s\".\n", str); *err = true; diff --git a/src/main.c b/src/main.c index 9c14cda..212211d 100644 --- a/src/main.c +++ b/src/main.c @@ -816,6 +816,7 @@ static void usage() " --output Name of output to display window on.\n" " --scale Follow the output's scale factor.\n" " --anchor Location on screen to anchor window.\n" +" --exclusive-zone <-1|px|%> Exclusive zone size, or -1 for none.\n" " --margin-top Offset from top of screen.\n" " --margin-bottom Offset from bottom of screen.\n" " --margin-left Offset from left of screen.\n" @@ -848,6 +849,7 @@ const struct option long_options[] = { {"config", required_argument, NULL, 'c'}, {"include", required_argument, NULL, 0}, {"anchor", required_argument, NULL, 0}, + {"exclusive-zone", required_argument, NULL, 0}, {"background-color", required_argument, NULL, 0}, {"corner-radius", required_argument, NULL, 0}, {"font", required_argument, NULL, 0}, @@ -1022,6 +1024,7 @@ int main(int argc, char *argv[]) .scale = 1, .width = 1280, .height = 720, + .exclusive_zone = -1, .entry = { .font_name = "Sans", .font_size = 24, @@ -1321,7 +1324,7 @@ int main(int argc, char *argv[]) tofi.anchor); zwlr_layer_surface_v1_set_exclusive_zone( tofi.window.zwlr_layer_surface, - -1); + tofi.window.exclusive_zone); zwlr_layer_surface_v1_set_size( tofi.window.zwlr_layer_surface, tofi.window.width / tofi.window.scale, diff --git a/src/tofi.h b/src/tofi.h index 419f86e..be3e821 100644 --- a/src/tofi.h +++ b/src/tofi.h @@ -56,12 +56,14 @@ struct tofi { uint32_t height; uint32_t scale; int32_t transform; + int32_t exclusive_zone; int32_t margin_top; int32_t margin_bottom; int32_t margin_left; int32_t margin_right; bool width_is_percent; bool height_is_percent; + bool exclusive_zone_is_percent; bool margin_top_is_percent; bool margin_bottom_is_percent; bool margin_left_is_percent; -- cgit v1.2.3