drag_pointer(): add use_treshold parameter

This commit is contained in:
Albert Safin 2018-12-12 15:35:11 +07:00
parent 2795c51d4b
commit 551ec20941
4 changed files with 62 additions and 21 deletions

View File

@ -12,12 +12,13 @@
#include <config.h> #include <config.h>
/** Callback for dragging */ /** Callback for dragging */
typedef void (*callback_t)(Con *, Rect *, uint32_t, uint32_t, const void *); typedef void (*callback_t)(Con *, Rect *, uint32_t, uint32_t,
const xcb_button_press_event_t *, const void *);
/** Macro to create a callback function for dragging */ /** Macro to create a callback function for dragging */
#define DRAGGING_CB(name) \ #define DRAGGING_CB(name) \
static void name(Con *con, Rect *old_rect, uint32_t new_x, \ static void name(Con *con, Rect *old_rect, uint32_t new_x, uint32_t new_y, \
uint32_t new_y, const void *extra) const xcb_button_press_event_t *event, const void *extra)
/** /**
* This is the return value of a drag operation like drag_pointer. * This is the return value of a drag operation like drag_pointer.
@ -46,10 +47,15 @@ typedef enum {
* This function grabs your pointer and keyboard and lets you drag stuff around * This function grabs your pointer and keyboard and lets you drag stuff around
* (borders). Every time you move your mouse, an XCB_MOTION_NOTIFY event will * (borders). Every time you move your mouse, an XCB_MOTION_NOTIFY event will
* be received and the given callback will be called with the parameters * be received and the given callback will be called with the parameters
* specified (client, border on which the click originally was), the original * specified (client, the original event), the original rect of the client,
* rect of the client, the event and the new coordinates (x, y). * and the new coordinates (x, y).
*
* If use_threshold is set, dragging only starts after the user moves the
* pointer past a certain threshold. That is, the cursor will not be set and the
* callback will not be called until then.
* *
*/ */
drag_result_t drag_pointer(Con *con, const xcb_button_press_event_t *event, drag_result_t drag_pointer(Con *con, const xcb_button_press_event_t *event,
xcb_window_t confine_to, int cursor, xcb_window_t confine_to, int cursor,
callback_t callback, const void *extra); bool use_threshold, callback_t callback,
const void *extra);

View File

@ -20,16 +20,32 @@ struct drag_x11_cb {
* drag of the resize handle. */ * drag of the resize handle. */
Con *con; Con *con;
/* The original event that initiated the drag. */
const xcb_button_press_event_t *event;
/* The dimensions of con when the loop was started. */ /* The dimensions of con when the loop was started. */
Rect old_rect; Rect old_rect;
/* The callback to invoke after every pointer movement. */ /* The callback to invoke after every pointer movement. */
callback_t callback; callback_t callback;
/* Drag distance threshold exceeded. If use_threshold is not set, then
* threshold_exceeded is always true. */
bool threshold_exceeded;
/* Cursor to set after the threshold is exceeded. */
xcb_cursor_t xcursor;
/* User data pointer for callback. */ /* User data pointer for callback. */
const void *extra; const void *extra;
}; };
static bool threshold_exceeded(uint32_t x1, uint32_t y1,
uint32_t x2, uint32_t y2) {
const uint32_t threshold = 9;
return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) > threshold * threshold;
}
static bool drain_drag_events(EV_P, struct drag_x11_cb *dragloop) { static bool drain_drag_events(EV_P, struct drag_x11_cb *dragloop) {
xcb_motion_notify_event_t *last_motion_notify = NULL; xcb_motion_notify_event_t *last_motion_notify = NULL;
xcb_generic_event_t *event; xcb_generic_event_t *event;
@ -105,15 +121,29 @@ static bool drain_drag_events(EV_P, struct drag_x11_cb *dragloop) {
return true; return true;
} }
if (!dragloop->threshold_exceeded &&
threshold_exceeded(last_motion_notify->root_x, last_motion_notify->root_y,
dragloop->event->root_x, dragloop->event->root_y)) {
if (dragloop->xcursor != XCB_NONE) {
xcb_change_active_pointer_grab(
conn,
dragloop->xcursor,
XCB_CURRENT_TIME,
XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION);
}
dragloop->threshold_exceeded = true;
}
/* Ensure that we are either dragging the resize handle (con is NULL) or that the /* Ensure that we are either dragging the resize handle (con is NULL) or that the
* container still exists. The latter might not be true, e.g., if the window closed * container still exists. The latter might not be true, e.g., if the window closed
* for any reason while the user was dragging it. */ * for any reason while the user was dragging it. */
if (!dragloop->con || con_exists(dragloop->con)) { if (dragloop->threshold_exceeded && (!dragloop->con || con_exists(dragloop->con))) {
dragloop->callback( dragloop->callback(
dragloop->con, dragloop->con,
&(dragloop->old_rect), &(dragloop->old_rect),
last_motion_notify->root_x, last_motion_notify->root_x,
last_motion_notify->root_y, last_motion_notify->root_y,
dragloop->event,
dragloop->extra); dragloop->extra);
} }
FREE(last_motion_notify); FREE(last_motion_notify);
@ -133,12 +163,18 @@ static void xcb_drag_prepare_cb(EV_P_ ev_prepare *w, int revents) {
* This function grabs your pointer and keyboard and lets you drag stuff around * This function grabs your pointer and keyboard and lets you drag stuff around
* (borders). Every time you move your mouse, an XCB_MOTION_NOTIFY event will * (borders). Every time you move your mouse, an XCB_MOTION_NOTIFY event will
* be received and the given callback will be called with the parameters * be received and the given callback will be called with the parameters
* specified (client, border on which the click originally was), the original * specified (client, the original event), the original rect of the client,
* rect of the client, the event and the new coordinates (x, y). * and the new coordinates (x, y).
*
* If use_threshold is set, dragging only starts after the user moves the
* pointer past a certain threshold. That is, the cursor will not be set and the
* callback will not be called until then.
* *
*/ */
drag_result_t drag_pointer(Con *con, const xcb_button_press_event_t *event, xcb_window_t confine_to, drag_result_t drag_pointer(Con *con, const xcb_button_press_event_t *event,
int cursor, callback_t callback, const void *extra) { xcb_window_t confine_to, int cursor,
bool use_threshold, callback_t callback,
const void *extra) {
xcb_cursor_t xcursor = (cursor && xcursor_supported) ? xcursor_get_cursor(cursor) : XCB_NONE; xcb_cursor_t xcursor = (cursor && xcursor_supported) ? xcursor_get_cursor(cursor) : XCB_NONE;
/* Grab the pointer */ /* Grab the pointer */
@ -153,7 +189,7 @@ drag_result_t drag_pointer(Con *con, const xcb_button_press_event_t *event, xcb_
XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */ XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
XCB_GRAB_MODE_ASYNC, /* keyboard mode */ XCB_GRAB_MODE_ASYNC, /* keyboard mode */
confine_to, /* confine_to = in which window should the cursor stay */ confine_to, /* confine_to = in which window should the cursor stay */
xcursor, /* possibly display a special cursor */ use_threshold ? XCB_NONE : xcursor, /* possibly display a special cursor */
XCB_CURRENT_TIME); XCB_CURRENT_TIME);
if ((reply = xcb_grab_pointer_reply(conn, cookie, &error)) == NULL) { if ((reply = xcb_grab_pointer_reply(conn, cookie, &error)) == NULL) {
@ -189,7 +225,10 @@ drag_result_t drag_pointer(Con *con, const xcb_button_press_event_t *event, xcb_
struct drag_x11_cb loop = { struct drag_x11_cb loop = {
.result = DRAGGING, .result = DRAGGING,
.con = con, .con = con,
.event = event,
.callback = callback, .callback = callback,
.threshold_exceeded = !use_threshold,
.xcursor = xcursor,
.extra = extra, .extra = extra,
}; };
ev_prepare *prepare = &loop.prepare; ev_prepare *prepare = &loop.prepare;

View File

@ -561,8 +561,6 @@ void floating_move_to_pointer(Con *con) {
} }
DRAGGING_CB(drag_window_callback) { DRAGGING_CB(drag_window_callback) {
const struct xcb_button_press_event_t *event = extra;
/* Reposition the client correctly while moving */ /* Reposition the client correctly while moving */
con->rect.x = old_rect->x + (new_x - event->root_x); con->rect.x = old_rect->x + (new_x - event->root_x);
con->rect.y = old_rect->y + (new_y - event->root_y); con->rect.y = old_rect->y + (new_y - event->root_y);
@ -595,7 +593,7 @@ void floating_drag_window(Con *con, const xcb_button_press_event_t *event) {
Rect initial_rect = con->rect; Rect initial_rect = con->rect;
/* Drag the window */ /* Drag the window */
drag_result_t drag_result = drag_pointer(con, event, XCB_NONE, XCURSOR_CURSOR_MOVE, drag_window_callback, event); drag_result_t drag_result = drag_pointer(con, event, XCB_NONE, XCURSOR_CURSOR_MOVE, false, drag_window_callback, NULL);
if (!con_exists(con)) { if (!con_exists(con)) {
DLOG("The container has been closed in the meantime.\n"); DLOG("The container has been closed in the meantime.\n");
@ -625,12 +623,10 @@ void floating_drag_window(Con *con, const xcb_button_press_event_t *event) {
struct resize_window_callback_params { struct resize_window_callback_params {
const border_t corner; const border_t corner;
const bool proportional; const bool proportional;
const xcb_button_press_event_t *event;
}; };
DRAGGING_CB(resize_window_callback) { DRAGGING_CB(resize_window_callback) {
const struct resize_window_callback_params *params = extra; const struct resize_window_callback_params *params = extra;
const xcb_button_press_event_t *event = params->event;
border_t corner = params->corner; border_t corner = params->corner;
int32_t dest_x = con->rect.x; int32_t dest_x = con->rect.x;
@ -706,12 +702,12 @@ void floating_resize_window(Con *con, const bool proportional,
cursor = (corner & BORDER_LEFT) ? XCURSOR_CURSOR_BOTTOM_LEFT_CORNER : XCURSOR_CURSOR_BOTTOM_RIGHT_CORNER; cursor = (corner & BORDER_LEFT) ? XCURSOR_CURSOR_BOTTOM_LEFT_CORNER : XCURSOR_CURSOR_BOTTOM_RIGHT_CORNER;
} }
struct resize_window_callback_params params = {corner, proportional, event}; struct resize_window_callback_params params = {corner, proportional};
/* get the initial rect in case of revert/cancel */ /* get the initial rect in case of revert/cancel */
Rect initial_rect = con->rect; Rect initial_rect = con->rect;
drag_result_t drag_result = drag_pointer(con, event, XCB_NONE, cursor, resize_window_callback, &params); drag_result_t drag_result = drag_pointer(con, event, XCB_NONE, cursor, false, resize_window_callback, &params);
if (!con_exists(con)) { if (!con_exists(con)) {
DLOG("The container has been closed in the meantime.\n"); DLOG("The container has been closed in the meantime.\n");

View File

@ -208,7 +208,7 @@ void resize_graphical_handler(Con *first, Con *second, orientation_t orientation
const struct callback_params params = {orientation, output, helpwin, &new_position}; const struct callback_params params = {orientation, output, helpwin, &new_position};
/* `drag_pointer' blocks until the drag is completed. */ /* `drag_pointer' blocks until the drag is completed. */
drag_result_t drag_result = drag_pointer(NULL, event, grabwin, 0, resize_callback, &params); drag_result_t drag_result = drag_pointer(NULL, event, grabwin, 0, false, resize_callback, &params);
xcb_destroy_window(conn, helpwin); xcb_destroy_window(conn, helpwin);
xcb_destroy_window(conn, grabwin); xcb_destroy_window(conn, grabwin);