Skip to content

Commit

Permalink
Merge pull request i3#2977 from orestisf1993/issue-1627
Browse files Browse the repository at this point in the history
Check container existance during drag events
  • Loading branch information
Airblader authored Sep 23, 2017
2 parents 4ad9199 + 414d23f commit 9fe508b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
7 changes: 7 additions & 0 deletions include/con.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ Con *con_by_window_id(xcb_window_t window);
*/
Con *con_by_con_id(long target);

/**
* Returns true if the given container (still) exists.
* This can be used, e.g., to make sure a container hasn't been closed in the meantime.
*
*/
bool con_exists(Con *con);

/**
* Returns the container with the given frame ID or NULL if no such container
* exists.
Expand Down
9 changes: 9 additions & 0 deletions src/con.c
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,15 @@ Con *con_by_con_id(long target) {
return NULL;
}

/*
* Returns true if the given container (still) exists.
* This can be used, e.g., to make sure a container hasn't been closed in the meantime.
*
*/
bool con_exists(Con *con) {
return con_by_con_id((long)con) != NULL;
}

/*
* Returns the container with the given frame ID or NULL if no such container
* exists.
Expand Down
27 changes: 21 additions & 6 deletions src/floating.c
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,11 @@ void floating_drag_window(Con *con, const xcb_button_press_event_t *event) {
/* Drag the window */
drag_result_t drag_result = drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, XCURSOR_CURSOR_MOVE, drag_window_callback, event);

if (!con_exists(con)) {
DLOG("The container has been closed in the meantime.\n");
return;
}

/* If the user cancelled, undo the changes. */
if (drag_result == DRAG_REVERT)
floating_reposition(con, initial_rect);
Expand Down Expand Up @@ -646,6 +651,11 @@ void floating_resize_window(Con *con, const bool proportional,

drag_result_t drag_result = drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, cursor, resize_window_callback, &params);

if (!con_exists(con)) {
DLOG("The container has been closed in the meantime.\n");
return;
}

/* If the user cancels, undo the resize */
if (drag_result == DRAG_REVERT)
floating_reposition(con, initial_rect);
Expand Down Expand Up @@ -743,12 +753,17 @@ static void xcb_drag_check_cb(EV_P_ ev_check *w, int revents) {
if (last_motion_notify == NULL)
return;

dragloop->callback(
dragloop->con,
&(dragloop->old_rect),
last_motion_notify->root_x,
last_motion_notify->root_y,
dragloop->extra);
/* 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
* for any reason while the user was dragging it. */
if (!dragloop->con || con_exists(dragloop->con)) {
dragloop->callback(
dragloop->con,
&(dragloop->old_rect),
last_motion_notify->root_x,
last_motion_notify->root_y,
dragloop->extra);
}
free(last_motion_notify);
}

Expand Down

0 comments on commit 9fe508b

Please sign in to comment.