Skip to content

Commit

Permalink
Add a no-drag option
Browse files Browse the repository at this point in the history
Clicking and dragging to create a selection could be bit cumbersome
when using a touch-pad.

Add an option to switch to a mode that you click to start a selection,
and click again to end it.

Fixes: emersion#76
  • Loading branch information
Grillo-0 committed Oct 29, 2023
1 parent 0a4c335 commit 7503be7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
4 changes: 4 additions & 0 deletions include/slurp.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ struct slurp_selection {
struct slurp_state {
bool running;
bool edit_anchor;
bool is_selecting;
bool just_pressed;


struct wl_display *display;
struct wl_registry *registry;
Expand Down Expand Up @@ -59,6 +62,7 @@ struct slurp_state {
struct wl_list boxes; // slurp_box::link
bool fixed_aspect_ratio;
double aspect_ratio; // h / w
bool no_drag;

struct slurp_box result;
};
Expand Down
55 changes: 50 additions & 5 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ static void pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,
output->cursor_image->hotspot_y / output->scale);
wl_surface_commit(seat->cursor_surface);
}

if (seat->state->no_drag) {
seat->state->just_pressed = false;
seat->state->is_selecting = false;
}
}

static void pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
Expand All @@ -185,6 +190,7 @@ static void pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
static void pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
struct slurp_seat *seat = data;
struct slurp_state *state = seat->state;
// the places the cursor moved away from are also dirty
if (seat->pointer_selection.has_selection) {
seat_set_outputs_dirty(seat);
Expand All @@ -194,13 +200,30 @@ static void pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,

switch (seat->button_state) {
case WL_POINTER_BUTTON_STATE_RELEASED:
seat_update_selection(seat);
if (state->no_drag) {
state->just_pressed = false;
} else {
state->is_selecting = false;
}
break;
case WL_POINTER_BUTTON_STATE_PRESSED:;
handle_active_selection_motion(seat, &seat->pointer_selection);
if (state->no_drag) {
if (!state->just_pressed) {
state->is_selecting = !state->is_selecting;
state->just_pressed = true;
}
} else {
state->is_selecting = true;
}
break;
}

if (state->is_selecting) {
handle_active_selection_motion(seat, &seat->pointer_selection);
} else {
seat_update_selection(seat);
}

if (seat->pointer_selection.has_selection) {
seat_set_outputs_dirty(seat);
}
Expand Down Expand Up @@ -242,6 +265,7 @@ static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
uint32_t serial, uint32_t time, uint32_t button,
uint32_t button_state) {
struct slurp_seat *seat = data;
struct slurp_state *state = seat->state;
if (seat->touch_selection.has_selection) {
return;
}
Expand All @@ -250,12 +274,29 @@ static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer,

switch (button_state) {
case WL_POINTER_BUTTON_STATE_PRESSED:
handle_selection_start(seat, &seat->pointer_selection);
if (state->no_drag) {
if (!state->just_pressed) {
state->is_selecting = !state->is_selecting;
state->just_pressed = true;
}
} else {
state->is_selecting = true;
}
break;
case WL_POINTER_BUTTON_STATE_RELEASED:
handle_selection_end(seat, &seat->pointer_selection);
if (state->no_drag) {
state->just_pressed = false;
} else {
state->is_selecting = false;
}
break;
}

if (state->is_selecting) {
handle_selection_start(seat, &seat->pointer_selection);
} else {
handle_selection_end(seat, &seat->pointer_selection);
}
}

static const struct wl_pointer_listener pointer_listener = {
Expand Down Expand Up @@ -884,6 +925,7 @@ int main(int argc, char *argv[]) {
.display_dimensions = false,
.restrict_selection = false,
.fixed_aspect_ratio = false,
.no_drag = false,
.aspect_ratio = 0,
.font_family = FONT_FAMILY
};
Expand All @@ -892,14 +934,17 @@ int main(int argc, char *argv[]) {
char *format = "%x,%y %wx%h\n";
bool output_boxes = false;
int w, h;
while ((opt = getopt(argc, argv, "hdb:c:s:B:w:proa:f:F:")) != -1) {
while ((opt = getopt(argc, argv, "hdDb:c:s:B:w:proa:f:F:")) != -1) {
switch (opt) {
case 'h':
printf("%s", usage);
return EXIT_SUCCESS;
case 'd':
state.display_dimensions = true;
break;
case 'D':
state.no_drag = true;
break;
case 'b':
state.colors.background = parse_color(optarg);
break;
Expand Down
4 changes: 4 additions & 0 deletions slurp.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ held, the selection is moved instead of being resized.
Force selections to have the given aspect ratio. This constraint is not
applied to the predefined rectangles specified using *-o*.

*-D*
Allow to click twice to select a rectangle, instead of the usual
click-and-drag method.

# COLORS

Colors may be specified in #RRGGBB or #RRGGBBAA format. The # is optional.
Expand Down

0 comments on commit 7503be7

Please sign in to comment.