Skip to content

Commit

Permalink
Added an option to use a timeout to start gestures
Browse files Browse the repository at this point in the history
Mouse movement within this timeout of pressing the button binding will be interpreted as a gesture. This is mainly intended to allow the use of touchpads.

Unfortunately, this means adding a delay for any click with the button configured for gestures. Currently, it is not possible to distinguish between events coming from different pointer devices, so this will apply to all of them. Also, there is no way to know when the user lifts their finger from the touchpad, so the gesture is also ended with the timeout.
  • Loading branch information
dkondor committed Dec 7, 2020
1 parent f163e26 commit 8f96039
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
22 changes: 21 additions & 1 deletion easystroke_gestures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class wstroke : public wf::plugin_interface_t, ActionVisitor {
wf::option_wrapper_t<wf::buttonbinding_t> initiate{"wstroke/initiate"};
wf::option_wrapper_t<bool> target_mouse{"wstroke/target_view_mouse"};
wf::option_wrapper_t<std::string> focus_mode{"wstroke/focus_mode"};
wf::option_wrapper_t<int> start_timeout{"wstroke/start_timeout"};
wf::option_wrapper_t<int> end_timeout{"wstroke/end_timeout"};

PreStroke ps;
std::unique_ptr<ActionDB> actions;
Expand All @@ -90,6 +92,9 @@ class wstroke : public wf::plugin_interface_t, ActionVisitor {
bool active = false;
bool is_gesture = false;

bool ptr_moved = false;
wf::wl_timer timeout;

std::string config_dir;

public:
Expand Down Expand Up @@ -125,11 +130,16 @@ class wstroke : public wf::plugin_interface_t, ActionVisitor {
grab_interface->name = "wstroke";
grab_interface->capabilities = wf::CAPABILITY_GRAB_INPUT;
grab_interface->callbacks.pointer.motion = [=](int32_t x, int32_t y) {
ptr_moved = true;
handle_input_move(x, y);
};
grab_interface->callbacks.pointer.button = [=](uint32_t button, uint32_t state) {
// LOGI("button: ", button, "state: ", state);
wf::buttonbinding_t tmp = initiate;
if(button == tmp.get_button() && state == WLR_BUTTON_RELEASED) end_stroke();
if(button == tmp.get_button() && state == WLR_BUTTON_RELEASED) {
if(start_timeout > 0 && !ptr_moved) timeout.set_timeout(start_timeout, [this]() { end_stroke(); });
else end_stroke();
}
};
output->add_button(initiate, &stroke_initiate);
}
Expand Down Expand Up @@ -369,6 +379,11 @@ class wstroke : public wf::plugin_interface_t, ActionVisitor {
ps.add(t);
if(is_gesture) draw_line(ps[ps.size()-2].x, ps[ps.size()-2].y,
ps.back().x, ps.back().y);
if(timeout.is_connected()) {
timeout.disconnect();
int timeout_len = end_timeout > 0 ? end_timeout : start_timeout;
timeout.set_timeout(timeout_len, [this]() { end_stroke(); });
}
}

/* start drawing the stroke on the screen */
Expand All @@ -379,6 +394,9 @@ class wstroke : public wf::plugin_interface_t, ActionVisitor {

/* callback when the mouse button is released */
void end_stroke() {
if(!active) return; /* in case the timeout was not disconnected */
timeout.disconnect();
ptr_moved = false;
clear_lines();
grab_interface->ungrab();
output->deactivate_plugin(grab_interface);
Expand Down Expand Up @@ -443,7 +461,9 @@ class wstroke : public wf::plugin_interface_t, ActionVisitor {
clear_lines();
if(target_mouse) output->focus_view(initial_active_view, false);
active = false;
ptr_moved = false;
is_gesture = false;
timeout.disconnect();
}

static constexpr std::array<std::pair<enum wlr_keyboard_modifier, uint32_t>, 4> mod_map = {
Expand Down
10 changes: 10 additions & 0 deletions wstroke.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,15 @@
<_name>Always</_name>
</desc>
</option>
<option name="start_timeout" type="int">
<_short>Start timeout for strokes</_short>
<_long>Timeout after a right click to start a stroke. Can be used to generate strokes on touchpads.</_long>
<default>0</default>
</option>
<option name="end_timeout" type="int">
<_short>End timeout for strokes</_short>
<_long>Use this timeout for ending gestures if they were started with the previous timeout.</_long>
<default>0</default>
</option>
</plugin>
</wayfire>

0 comments on commit 8f96039

Please sign in to comment.