Check if con_id exists in cmd_swap (#2898)

Also adds some testcases for swap using con_id.

Fixes #2895
This commit is contained in:
Orestis 2017-09-06 08:34:14 +03:00 committed by Michael Stapelberg
parent 85eb097677
commit 66f2148236
4 changed files with 56 additions and 1 deletions

View File

@ -152,6 +152,13 @@ bool con_has_parent(Con *con, Con *parent);
*/
Con *con_by_window_id(xcb_window_t window);
/**
* Returns the container with the given container ID or NULL if no such
* container exists.
*
*/
Con *con_by_con_id(long target);
/**
* Returns the container with the given frame ID or NULL if no such container
* exists.

View File

@ -1841,7 +1841,7 @@ void cmd_swap(I3_CMD, const char *mode, const char *arg) {
return;
}
con = (Con *)target;
con = con_by_con_id(target);
} else if (strcmp(mode, "mark") == 0) {
con = con_by_mark(arg);
} else {

View File

@ -557,6 +557,22 @@ Con *con_by_window_id(xcb_window_t window) {
return NULL;
}
/*
* Returns the container with the given container ID or NULL if no such
* container exists.
*
*/
Con *con_by_con_id(long target) {
Con *con;
TAILQ_FOREACH(con, &all_cons, all_cons) {
if (con == (Con *)target) {
return con;
}
}
return NULL;
}
/*
* Returns the container with the given frame ID or NULL if no such container
* exists.

View File

@ -32,6 +32,38 @@ my ($nodes, $expected_focus, $A, $B, $F);
my ($result);
my @urgent;
###############################################################################
# Invalid con_id should not crash i3
# See issue #2895.
###############################################################################
$pid = launch_with_config($config);
$ws = fresh_workspace;
open_window;
cmd "swap container with con_id 1";
does_i3_live;
exit_gracefully($pid);
###############################################################################
# Swap 2 windows in different workspaces using con_id
###############################################################################
$pid = launch_with_config($config);
$ws = fresh_workspace;
open_window;
$A = get_focused($ws);
$ws = fresh_workspace;
open_window;
cmd "swap container with con_id $A";
is(get_focused($ws), $A, 'A is now focused');
exit_gracefully($pid);
###############################################################################
# Swap two containers next to each other.
# Focus should stay on B because both windows are on the focused workspace.