From 5238cfff72fa4f8c7fb5c78f6f516a0fdfa94a23 Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Sun, 23 Aug 2020 01:28:38 -0600 Subject: [PATCH] Add option to force user to select a choice box. --- include/slurp.h | 3 ++- main.c | 23 +++++++++++++++++++---- slurp.1.scd | 9 +++++++-- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/include/slurp.h b/include/slurp.h index a70b956..f24d9fd 100644 --- a/include/slurp.h +++ b/include/slurp.h @@ -14,8 +14,8 @@ struct slurp_box { int32_t x, y; int32_t width, height; - struct wl_list link; char *label; + struct wl_list link; }; struct slurp_selection { @@ -51,6 +51,7 @@ struct slurp_state { uint32_t border_weight; bool display_dimensions; bool single_point; + bool restrict_selection; struct wl_list boxes; // slurp_box::link struct slurp_box result; diff --git a/main.c b/main.c index 59da391..249b7ef 100644 --- a/main.c +++ b/main.c @@ -171,14 +171,19 @@ static void pointer_handle_motion(void *data, struct wl_pointer *wl_pointer, static void handle_selection_start(struct slurp_seat *seat, struct slurp_selection *current_selection) { struct slurp_state *state = seat->state; - current_selection->has_selection = true; if (state->single_point) { state->result.x = current_selection->x; state->result.y = current_selection->y; state->result.width = state->result.height = 1; state->running = false; + } else if (state->restrict_selection) { + if (current_selection->has_selection) { + state->result = current_selection->selection; + state->running = false; + } } else { + current_selection->has_selection = true; current_selection->anchor_x = current_selection->x; current_selection->anchor_y = current_selection->y; } @@ -187,7 +192,7 @@ static void handle_selection_start(struct slurp_seat *seat, static void handle_selection_end(struct slurp_seat *seat, struct slurp_selection *current_selection) { struct slurp_state *state = seat->state; - if (state->single_point) { + if (state->single_point || state->restrict_selection) { return; } if (current_selection->has_selection) { @@ -645,7 +650,8 @@ static const char usage[] = " -w n Set border weight.\n" " -f s Set output format.\n" " -o Select a display output.\n" - " -p Select a single point.\n"; + " -p Select a single point.\n" + " -r Restrict selection to predefined boxes.\n"; uint32_t parse_color(const char *color) { if (color[0] == '#') { @@ -748,12 +754,13 @@ int main(int argc, char *argv[]) { }, .border_weight = 2, .display_dimensions = false, + .restrict_selection = false, }; int opt; char *format = "%x,%y %wx%h\n"; bool output_boxes = false; - while ((opt = getopt(argc, argv, "hdb:c:s:B:w:pof:")) != -1) { + while ((opt = getopt(argc, argv, "hdb:c:s:B:w:prof:")) != -1) { switch (opt) { case 'h': printf("%s", usage); @@ -792,12 +799,20 @@ int main(int argc, char *argv[]) { case 'o': output_boxes = true; break; + case 'r': + state.restrict_selection = true; + break; default: printf("%s", usage); return EXIT_FAILURE; } } + if (state.single_point && state.restrict_selection) { + fprintf(stderr, "-p and -r cannot be used together\n"); + return EXIT_FAILURE; + } + wl_list_init(&state.boxes); if (!isatty(STDIN_FILENO) && !state.single_point) { char *line = NULL; diff --git a/slurp.1.scd b/slurp.1.scd index 42ec28d..d3fb3c1 100644 --- a/slurp.1.scd +++ b/slurp.1.scd @@ -14,8 +14,8 @@ slurp is a command-line utility to select a region from Wayland compositors which support the layer-shell protocol. It lets the user hold the pointer to select, or click to cancel the selection. -If the standard input is not a TTY, slurp will read a list of predefined -rectangles for quick selection. Each line must be in the form +If the standard input is not a TTY or the -r option is used, slurp will read a +list of predefined rectangles for quick selection. Each line must be in the form ", x [label]". The label is optional and can be any string that doesn't contain newlines. It can be accessed using the "%l" sequence in a format string. @@ -58,6 +58,11 @@ held, the selection is moved instead of being resized. Add predefined rectangles for all outputs, as if provided on standard input. The label will be the name of the output. +*-r* + Require the user to select one of the predefined rectangles. These can come + from standard input, if *-o* is used, the rectangles of all display outputs. + This option conflicts with *-p*. + # COLORS Colors may be specified in #RRGGBB or #RRGGBBAA format. The # is optional.