Skip to content

Commit 9fe508b

Browse files
authored
Merge pull request i3#2977 from orestisf1993/issue-1627
Check container existance during drag events
2 parents 4ad9199 + 414d23f commit 9fe508b

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

include/con.h

+7
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ Con *con_by_window_id(xcb_window_t window);
165165
*/
166166
Con *con_by_con_id(long target);
167167

168+
/**
169+
* Returns true if the given container (still) exists.
170+
* This can be used, e.g., to make sure a container hasn't been closed in the meantime.
171+
*
172+
*/
173+
bool con_exists(Con *con);
174+
168175
/**
169176
* Returns the container with the given frame ID or NULL if no such container
170177
* exists.

src/con.c

+9
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,15 @@ Con *con_by_con_id(long target) {
604604
return NULL;
605605
}
606606

607+
/*
608+
* Returns true if the given container (still) exists.
609+
* This can be used, e.g., to make sure a container hasn't been closed in the meantime.
610+
*
611+
*/
612+
bool con_exists(Con *con) {
613+
return con_by_con_id((long)con) != NULL;
614+
}
615+
607616
/*
608617
* Returns the container with the given frame ID or NULL if no such container
609618
* exists.

src/floating.c

+21-6
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,11 @@ void floating_drag_window(Con *con, const xcb_button_press_event_t *event) {
535535
/* Drag the window */
536536
drag_result_t drag_result = drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, XCURSOR_CURSOR_MOVE, drag_window_callback, event);
537537

538+
if (!con_exists(con)) {
539+
DLOG("The container has been closed in the meantime.\n");
540+
return;
541+
}
542+
538543
/* If the user cancelled, undo the changes. */
539544
if (drag_result == DRAG_REVERT)
540545
floating_reposition(con, initial_rect);
@@ -646,6 +651,11 @@ void floating_resize_window(Con *con, const bool proportional,
646651

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

654+
if (!con_exists(con)) {
655+
DLOG("The container has been closed in the meantime.\n");
656+
return;
657+
}
658+
649659
/* If the user cancels, undo the resize */
650660
if (drag_result == DRAG_REVERT)
651661
floating_reposition(con, initial_rect);
@@ -743,12 +753,17 @@ static void xcb_drag_check_cb(EV_P_ ev_check *w, int revents) {
743753
if (last_motion_notify == NULL)
744754
return;
745755

746-
dragloop->callback(
747-
dragloop->con,
748-
&(dragloop->old_rect),
749-
last_motion_notify->root_x,
750-
last_motion_notify->root_y,
751-
dragloop->extra);
756+
/* Ensure that we are either dragging the resize handle (con is NULL) or that the
757+
* container still exists. The latter might not be true, e.g., if the window closed
758+
* for any reason while the user was dragging it. */
759+
if (!dragloop->con || con_exists(dragloop->con)) {
760+
dragloop->callback(
761+
dragloop->con,
762+
&(dragloop->old_rect),
763+
last_motion_notify->root_x,
764+
last_motion_notify->root_y,
765+
dragloop->extra);
766+
}
752767
free(last_motion_notify);
753768
}
754769

0 commit comments

Comments
 (0)