From 8d97e2899f1c0f018eff382f05f0cf6a7f8dcce3 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 24 Jun 2018 11:16:12 -0400 Subject: [PATCH] Add appearance customization options --- include/slurp.h | 8 +++++++ main.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++--- render.c | 16 +++++++------- slurp.1.scd | 18 +++++++++++++++- 4 files changed, 87 insertions(+), 12 deletions(-) diff --git a/include/slurp.h b/include/slurp.h index c046462..4e05b10 100644 --- a/include/slurp.h +++ b/include/slurp.h @@ -24,6 +24,14 @@ struct slurp_state { struct wl_list outputs; // slurp_output::link struct wl_list pointers; // slurp_pointer::link + struct { + uint32_t background; + uint32_t border; + uint32_t selection; + } colors; + + uint32_t border_weight; + struct slurp_box result; }; diff --git a/main.c b/main.c index c3bdf90..e65f3a6 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,5 @@ #define _POSIX_C_SOURCE 2 +#include #include #include #include @@ -320,21 +321,71 @@ static const struct wl_registry_listener registry_listener = { static const char usage[] = "Usage: slurp [options...]\n" "\n" - " -h Show help message and quit.\n"; + " -h Show help message and quit.\n" + " -b #rrggbb Set background color.\n" + " -c #rrggbb Set border color.\n" + " -s #rrggbb Set selection color.\n" + " -w n Set border weight.\n"; + +uint32_t parse_color(const char *color) { + if (color[0] == '#') { + ++color; + } + + int len = strlen(color); + if (len != 6 && len != 8) { + fprintf(stderr, "Invalid color %s, " + "defaulting to color 0xFFFFFFFF\n", color); + return 0xFFFFFFFF; + } + uint32_t res = (uint32_t)strtoul(color, NULL, 16); + if (strlen(color) == 6) { + res = (res << 8) | 0xFF; + } + return res; +} int main(int argc, char *argv[]) { + struct slurp_state state = { + .colors = { + .background = 0xFFFFFF40, + .border = 0x000000FF, + .selection = 0x00000000, + }, + .border_weight = 2, + }; + int opt; - while ((opt = getopt(argc, argv, "h")) != -1) { + while ((opt = getopt(argc, argv, "hb:c:s:w:")) != -1) { switch (opt) { case 'h': printf("%s", usage); return EXIT_SUCCESS; + case 'b': + state.colors.background = parse_color(optarg); + break; + case 'c': + state.colors.border = parse_color(optarg); + break; + case 's': + state.colors.selection = parse_color(optarg); + break; + case 'w': { + errno = 0; + char *endptr; + state.border_weight = strtol(optarg, &endptr, 10); + if (*endptr || errno) { + fprintf(stderr, "Error: expected numeric argument for -w\n"); + exit(EXIT_FAILURE); + } + break; + } default: + printf("%s", usage); return EXIT_FAILURE; } } - struct slurp_state state = {0}; wl_list_init(&state.outputs); wl_list_init(&state.pointers); diff --git a/render.c b/render.c index 5088c51..ef1445b 100644 --- a/render.c +++ b/render.c @@ -20,15 +20,10 @@ void render(struct slurp_output *output) { cairo_t *cairo = buffer->cairo; int32_t scale = output->scale; - uint32_t border_color = 0x000000FF; - int border_size = 2; - // Clear - cairo_save(cairo); - cairo_set_source_rgba(cairo, 0, 0, 0, 0); cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE); + set_source_u32(cairo, state->colors.background); cairo_paint(cairo); - cairo_restore(cairo); struct slurp_pointer *pointer; wl_list_for_each(pointer, &state->pointers, link) { @@ -41,8 +36,13 @@ void render(struct slurp_output *output) { pointer_get_box(pointer, &x, &y, &width, &height); // Draw border - set_source_u32(cairo, border_color); - cairo_set_line_width(cairo, border_size * scale); + set_source_u32(cairo, state->colors.selection); + cairo_rectangle(cairo, x * scale, y * scale, + width * scale, height * scale); + cairo_fill(cairo); + + set_source_u32(cairo, state->colors.border); + cairo_set_line_width(cairo, state->border_weight * scale); cairo_rectangle(cairo, x * scale, y * scale, width * scale, height * scale); cairo_stroke(cairo); diff --git a/slurp.1.scd b/slurp.1.scd index 32572a1..f85410c 100644 --- a/slurp.1.scd +++ b/slurp.1.scd @@ -11,7 +11,7 @@ slurp - select a region in a Wayland compositor # SYNOPSIS slurp is a command-line utility to select a region from Wayland compositors -which support the layer-shell protocol. It lets the user hold his pointer to +which support the layer-shell protocol. It lets the user hold the pointer to select, or click to cancel the selection. # OPTIONS @@ -19,6 +19,22 @@ select, or click to cancel the selection. *-h* Show help message and quit. +*-b* _color_ + Set background color. See *COLORS* for more detail. + +*-c* _color_ + Set border color. See *COLORS* for more detail. + +*-s* _color_ + Set selection color. See *COLORS* for more detail. + +*-w* _weight_ + Set border weight. + +# COLORS + +Colors may be specified in #RRGGBB or #RRGGBBAA format. The # is optional. + # AUTHORS Maintained by Simon Ser , who is assisted by other