From f0913731578d8f6c160deae98f3ae40063787a8b Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Wed, 19 Aug 2020 01:50:52 -0600 Subject: [PATCH] Always render choice boxes To make it easier for the user to see what the available options are, also make the color for the choice boxes configurable. --- include/slurp.h | 1 + main.c | 17 +++++++++++++---- render.c | 42 ++++++++++++++++++++++++++++-------------- slurp.1.scd | 4 ++++ 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/include/slurp.h b/include/slurp.h index d4a5cb8..a70b956 100644 --- a/include/slurp.h +++ b/include/slurp.h @@ -45,6 +45,7 @@ struct slurp_state { uint32_t background; uint32_t border; uint32_t selection; + uint32_t choice; } colors; uint32_t border_weight; diff --git a/main.c b/main.c index 066bdda..59da391 100644 --- a/main.c +++ b/main.c @@ -13,6 +13,10 @@ #include "slurp.h" #include "render.h" +#define BG_COLOR 0xFFFFFF40 +#define BORDER_COLOR 0x000000FF +#define SELECTION_COLOR 0x00000000 + static void noop() { // This space intentionally left blank } @@ -637,6 +641,7 @@ static const char usage[] = " -b #rrggbbaa Set background color.\n" " -c #rrggbbaa Set border color.\n" " -s #rrggbbaa Set selection color.\n" + " -B #rrggbbaa Set option box color.\n" " -w n Set border weight.\n" " -f s Set output format.\n" " -o Select a display output.\n" @@ -736,9 +741,10 @@ int main(int argc, char *argv[]) { struct slurp_state state = { .colors = { - .background = 0xFFFFFF40, - .border = 0x000000FF, - .selection = 0x00000000, + .background = BG_COLOR, + .border = BORDER_COLOR, + .selection = SELECTION_COLOR, + .choice = BG_COLOR, }, .border_weight = 2, .display_dimensions = false, @@ -747,7 +753,7 @@ int main(int argc, char *argv[]) { int opt; char *format = "%x,%y %wx%h\n"; bool output_boxes = false; - while ((opt = getopt(argc, argv, "hdb:c:s:w:pof:")) != -1) { + while ((opt = getopt(argc, argv, "hdb:c:s:B:w:pof:")) != -1) { switch (opt) { case 'h': printf("%s", usage); @@ -764,6 +770,9 @@ int main(int argc, char *argv[]) { case 's': state.colors.selection = parse_color(optarg); break; + case 'B': + state.colors.choice = parse_color(optarg); + break; case 'f': format = optarg; break; diff --git a/render.c b/render.c index 6060f57..af2bfd9 100644 --- a/render.c +++ b/render.c @@ -13,6 +13,17 @@ static void set_source_u32(cairo_t *cairo, uint32_t color) { (color >> (0 * 8) & 0xFF) / 255.0); } +static void draw_rect(cairo_t *cairo, struct slurp_box *box, uint32_t color, int32_t scale) { + set_source_u32(cairo, color); + cairo_rectangle(cairo, box->x * scale, box->y * scale, + box->width * scale, box->height * scale); +} + +static void box_layout_to_output(struct slurp_box *box, struct slurp_output *output) { + box->x -= output->logical_geometry.x; + box->y -= output->logical_geometry.y; +} + void render(struct slurp_output *output) { struct slurp_state *state = output->state; struct pool_buffer *buffer = output->current_buffer; @@ -24,6 +35,18 @@ void render(struct slurp_output *output) { set_source_u32(cairo, state->colors.background); cairo_paint(cairo); + // Draw option boxes from input + struct slurp_box *choice_box; + wl_list_for_each(choice_box, &state->boxes, link) { + if (box_intersect(&output->logical_geometry, + choice_box)) { + struct slurp_box b = *choice_box; + box_layout_to_output(&b, output); + draw_rect(cairo, &b, state->colors.choice, scale); + cairo_fill(cairo); + } + } + struct slurp_seat *seat; wl_list_for_each(seat, &state->seats, link) { struct slurp_selection *current_selection = @@ -31,11 +54,7 @@ void render(struct slurp_output *output) { &seat->touch_selection : &seat->pointer_selection; - if (!seat->wl_pointer) { - continue; - } - - if (!current_selection->has_selection) { + if (!seat->wl_pointer || !current_selection->has_selection) { continue; } @@ -44,19 +63,14 @@ void render(struct slurp_output *output) { continue; } struct slurp_box b = current_selection->selection; - b.x -= output->logical_geometry.x; - b.y -= output->logical_geometry.y; + box_layout_to_output(&b, output); - // Draw border - set_source_u32(cairo, state->colors.selection); - cairo_rectangle(cairo, b.x * scale, b.y * scale, - b.width * scale, b.height * scale); + draw_rect(cairo, &b, state->colors.selection, scale); cairo_fill(cairo); - set_source_u32(cairo, state->colors.border); + // Draw border cairo_set_line_width(cairo, state->border_weight * scale); - cairo_rectangle(cairo, b.x * scale, b.y * scale, - b.width * scale, b.height * scale); + draw_rect(cairo, &b, state->colors.border, scale); cairo_stroke(cairo); if (state->display_dimensions) { diff --git a/slurp.1.scd b/slurp.1.scd index 2f82703..42ec28d 100644 --- a/slurp.1.scd +++ b/slurp.1.scd @@ -40,6 +40,10 @@ held, the selection is moved instead of being resized. *-s* _color_ Set selection color. See *COLORS* for more detail. +*-B* _color_ + Set color for highlighting predefined rectangles from standard input when not + selected. + *-w* _weight_ Set border weight.