Skip to content

Commit

Permalink
Add option to force user to select a choice box.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmccombs committed Sep 8, 2020
1 parent a9a8a36 commit 5238cff
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
3 changes: 2 additions & 1 deletion include/slurp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
23 changes: 19 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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] == '#') {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions slurp.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -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>,<y> <width>x<height> [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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 5238cff

Please sign in to comment.