Skip to content

Set the current seat in seatop handlers #5950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 0 additions & 174 deletions sway/commands/swap.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,180 +13,6 @@
static const char expected_syntax[] =
"Expected 'swap container with id|con_id|mark <arg>'";

static void swap_places(struct sway_container *con1,
struct sway_container *con2) {
struct sway_container *temp = malloc(sizeof(struct sway_container));
temp->x = con1->x;
temp->y = con1->y;
temp->width = con1->width;
temp->height = con1->height;
temp->width_fraction = con1->width_fraction;
temp->height_fraction = con1->height_fraction;
temp->parent = con1->parent;
temp->workspace = con1->workspace;
bool temp_floating = container_is_floating(con1);

con1->x = con2->x;
con1->y = con2->y;
con1->width = con2->width;
con1->height = con2->height;
con1->width_fraction = con2->width_fraction;
con1->height_fraction = con2->height_fraction;

con2->x = temp->x;
con2->y = temp->y;
con2->width = temp->width;
con2->height = temp->height;
con2->width_fraction = temp->width_fraction;
con2->height_fraction = temp->height_fraction;

int temp_index = container_sibling_index(con1);
if (con2->parent) {
container_insert_child(con2->parent, con1,
container_sibling_index(con2));
} else if (container_is_floating(con2)) {
workspace_add_floating(con2->workspace, con1);
} else {
workspace_insert_tiling(con2->workspace, con1,
container_sibling_index(con2));
}
if (temp->parent) {
container_insert_child(temp->parent, con2, temp_index);
} else if (temp_floating) {
workspace_add_floating(temp->workspace, con2);
} else {
workspace_insert_tiling(temp->workspace, con2, temp_index);
}

free(temp);
}

static void swap_focus(struct sway_container *con1,
struct sway_container *con2, struct sway_seat *seat,
struct sway_container *focus) {
if (focus == con1 || focus == con2) {
struct sway_workspace *ws1 = con1->workspace;
struct sway_workspace *ws2 = con2->workspace;
enum sway_container_layout layout1 = container_parent_layout(con1);
enum sway_container_layout layout2 = container_parent_layout(con2);
if (focus == con1 && (layout2 == L_TABBED || layout2 == L_STACKED)) {
if (workspace_is_visible(ws2)) {
seat_set_focus(seat, &con2->node);
}
seat_set_focus_container(seat, ws1 != ws2 ? con2 : con1);
} else if (focus == con2 && (layout1 == L_TABBED
|| layout1 == L_STACKED)) {
if (workspace_is_visible(ws1)) {
seat_set_focus(seat, &con1->node);
}
seat_set_focus_container(seat, ws1 != ws2 ? con1 : con2);
} else if (ws1 != ws2) {
seat_set_focus_container(seat, focus == con1 ? con2 : con1);
} else {
seat_set_focus_container(seat, focus);
}
} else {
seat_set_focus_container(seat, focus);
}

if (root->fullscreen_global) {
seat_set_focus(seat,
seat_get_focus_inactive(seat, &root->fullscreen_global->node));
}
}

void container_swap(struct sway_container *con1, struct sway_container *con2) {
if (!sway_assert(con1 && con2, "Cannot swap with nothing")) {
return;
}
if (!sway_assert(!container_has_ancestor(con1, con2)
&& !container_has_ancestor(con2, con1),
"Cannot swap ancestor and descendant")) {
return;
}

sway_log(SWAY_DEBUG, "Swapping containers %zu and %zu",
con1->node.id, con2->node.id);

bool scratch1 = con1->scratchpad;
bool hidden1 = container_is_scratchpad_hidden(con1);
bool scratch2 = con2->scratchpad;
bool hidden2 = container_is_scratchpad_hidden(con2);
if (scratch1) {
if (hidden1) {
root_scratchpad_show(con1);
}
root_scratchpad_remove_container(con1);
}
if (scratch2) {
if (hidden2) {
root_scratchpad_show(con2);
}
root_scratchpad_remove_container(con2);
}

enum sway_fullscreen_mode fs1 = con1->fullscreen_mode;
enum sway_fullscreen_mode fs2 = con2->fullscreen_mode;
if (fs1) {
container_fullscreen_disable(con1);
}
if (fs2) {
container_fullscreen_disable(con2);
}

struct sway_seat *seat = config->handler_context.seat;
struct sway_container *focus = seat_get_focused_container(seat);
struct sway_workspace *vis1 =
output_get_active_workspace(con1->workspace->output);
struct sway_workspace *vis2 =
output_get_active_workspace(con2->workspace->output);
if (!sway_assert(vis1 && vis2, "con1 or con2 are on an output without a"
"workspace. This should not happen")) {
return;
}

char *stored_prev_name = NULL;
if (seat->prev_workspace_name) {
stored_prev_name = strdup(seat->prev_workspace_name);
}

swap_places(con1, con2);

if (!workspace_is_visible(vis1)) {
seat_set_focus(seat, seat_get_focus_inactive(seat, &vis1->node));
}
if (!workspace_is_visible(vis2)) {
seat_set_focus(seat, seat_get_focus_inactive(seat, &vis2->node));
}

swap_focus(con1, con2, seat, focus);

if (stored_prev_name) {
free(seat->prev_workspace_name);
seat->prev_workspace_name = stored_prev_name;
}

if (scratch1) {
root_scratchpad_add_container(con2, NULL);
if (!hidden1) {
root_scratchpad_show(con2);
}
}
if (scratch2) {
root_scratchpad_add_container(con1, NULL);
if (!hidden2) {
root_scratchpad_show(con1);
}
}

if (fs1) {
container_set_fullscreen(con2, fs1);
}
if (fs2) {
container_set_fullscreen(con1, fs2);
}
}

static bool test_con_id(struct sway_container *container, void *data) {
size_t *con_id = data;
return container->node.id == *con_id;
Expand Down
1 change: 1 addition & 0 deletions sway/input/seatop_move_tiling.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ static void finalize_move(struct sway_seat *seat) {
seatop_begin_default(seat);
return;
}
config->handler_context.seat = seat;

struct sway_container *con = e->con;
struct sway_container *old_parent = con->parent;
Expand Down
2 changes: 2 additions & 0 deletions sway/input/seatop_resize_floating.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ static void handle_pointer_motion(struct sway_seat *seat, uint32_t time_msec) {
mouse_move_y = 0;
}

config->handler_context.seat = seat;

double grow_width = edge & WLR_EDGE_LEFT ? -mouse_move_x : mouse_move_x;
double grow_height = edge & WLR_EDGE_TOP ? -mouse_move_y : mouse_move_y;

Expand Down
2 changes: 2 additions & 0 deletions sway/input/seatop_resize_tiling.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ static void handle_pointer_motion(struct sway_seat *seat, uint32_t time_msec) {
}
}

config->handler_context.seat = seat;

if (amount_x != 0) {
container_resize_tiled(e->h_con, e->edge_x, amount_x);
}
Expand Down
177 changes: 176 additions & 1 deletion sway/tree/container.c
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,7 @@ struct sway_container *container_split(struct sway_container *child,
}
}

struct sway_seat *seat = input_manager_get_default_seat();
struct sway_seat *seat = input_manager_current_seat();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why container_split should use the default seat while the reset use the current seat, so I've changed it.

bool set_focus = (seat_get_focus(seat) == &child->node);

if (container_is_floating(child) && child->view) {
Expand Down Expand Up @@ -1614,6 +1614,181 @@ void container_raise_floating(struct sway_container *con) {
}
}

static void swap_focus(struct sway_container *con1,
struct sway_container *con2, struct sway_seat *seat,
struct sway_container *focus) {
if (focus == con1 || focus == con2) {
struct sway_workspace *ws1 = con1->workspace;
struct sway_workspace *ws2 = con2->workspace;
enum sway_container_layout layout1 = container_parent_layout(con1);
enum sway_container_layout layout2 = container_parent_layout(con2);
if (focus == con1 && (layout2 == L_TABBED || layout2 == L_STACKED)) {
if (workspace_is_visible(ws2)) {
seat_set_focus(seat, &con2->node);
}
seat_set_focus_container(seat, ws1 != ws2 ? con2 : con1);
} else if (focus == con2 && (layout1 == L_TABBED
|| layout1 == L_STACKED)) {
if (workspace_is_visible(ws1)) {
seat_set_focus(seat, &con1->node);
}
seat_set_focus_container(seat, ws1 != ws2 ? con1 : con2);
} else if (ws1 != ws2) {
seat_set_focus_container(seat, focus == con1 ? con2 : con1);
} else {
seat_set_focus_container(seat, focus);
}
} else {
seat_set_focus_container(seat, focus);
}

if (root->fullscreen_global) {
seat_set_focus(seat,
seat_get_focus_inactive(seat, &root->fullscreen_global->node));
}
}

static void swap_places(struct sway_container *con1,
struct sway_container *con2) {
struct sway_container *temp = malloc(sizeof(struct sway_container));
temp->x = con1->x;
temp->y = con1->y;
temp->width = con1->width;
temp->height = con1->height;
temp->width_fraction = con1->width_fraction;
temp->height_fraction = con1->height_fraction;
temp->parent = con1->parent;
temp->workspace = con1->workspace;
bool temp_floating = container_is_floating(con1);

con1->x = con2->x;
con1->y = con2->y;
con1->width = con2->width;
con1->height = con2->height;
con1->width_fraction = con2->width_fraction;
con1->height_fraction = con2->height_fraction;

con2->x = temp->x;
con2->y = temp->y;
con2->width = temp->width;
con2->height = temp->height;
con2->width_fraction = temp->width_fraction;
con2->height_fraction = temp->height_fraction;

int temp_index = container_sibling_index(con1);
if (con2->parent) {
container_insert_child(con2->parent, con1,
container_sibling_index(con2));
} else if (container_is_floating(con2)) {
workspace_add_floating(con2->workspace, con1);
} else {
workspace_insert_tiling(con2->workspace, con1,
container_sibling_index(con2));
}
if (temp->parent) {
container_insert_child(temp->parent, con2, temp_index);
} else if (temp_floating) {
workspace_add_floating(temp->workspace, con2);
} else {
workspace_insert_tiling(temp->workspace, con2, temp_index);
}

free(temp);
}

void container_swap(struct sway_container *con1, struct sway_container *con2) {
if (!sway_assert(con1 && con2, "Cannot swap with nothing")) {
return;
}
if (!sway_assert(!container_has_ancestor(con1, con2)
&& !container_has_ancestor(con2, con1),
"Cannot swap ancestor and descendant")) {
return;
}

sway_log(SWAY_DEBUG, "Swapping containers %zu and %zu",
con1->node.id, con2->node.id);

bool scratch1 = con1->scratchpad;
bool hidden1 = container_is_scratchpad_hidden(con1);
bool scratch2 = con2->scratchpad;
bool hidden2 = container_is_scratchpad_hidden(con2);
if (scratch1) {
if (hidden1) {
root_scratchpad_show(con1);
}
root_scratchpad_remove_container(con1);
}
if (scratch2) {
if (hidden2) {
root_scratchpad_show(con2);
}
root_scratchpad_remove_container(con2);
}

enum sway_fullscreen_mode fs1 = con1->fullscreen_mode;
enum sway_fullscreen_mode fs2 = con2->fullscreen_mode;
if (fs1) {
container_fullscreen_disable(con1);
}
if (fs2) {
container_fullscreen_disable(con2);
}

struct sway_seat *seat = input_manager_current_seat();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only line changed in this commit. Otherwise these three functions were lifted directly from commands/swap.c

struct sway_container *focus = seat_get_focused_container(seat);
struct sway_workspace *vis1 =
output_get_active_workspace(con1->workspace->output);
struct sway_workspace *vis2 =
output_get_active_workspace(con2->workspace->output);
if (!sway_assert(vis1 && vis2, "con1 or con2 are on an output without a"
"workspace. This should not happen")) {
return;
}

char *stored_prev_name = NULL;
if (seat->prev_workspace_name) {
stored_prev_name = strdup(seat->prev_workspace_name);
}

swap_places(con1, con2);

if (!workspace_is_visible(vis1)) {
seat_set_focus(seat, seat_get_focus_inactive(seat, &vis1->node));
}
if (!workspace_is_visible(vis2)) {
seat_set_focus(seat, seat_get_focus_inactive(seat, &vis2->node));
}

swap_focus(con1, con2, seat, focus);

if (stored_prev_name) {
free(seat->prev_workspace_name);
seat->prev_workspace_name = stored_prev_name;
}

if (scratch1) {
root_scratchpad_add_container(con2, NULL);
if (!hidden1) {
root_scratchpad_show(con2);
}
}
if (scratch2) {
root_scratchpad_add_container(con1, NULL);
if (!hidden2) {
root_scratchpad_show(con1);
}
}

if (fs1) {
container_set_fullscreen(con2, fs1);
}
if (fs2) {
container_set_fullscreen(con1, fs2);
}
}


bool container_is_scratchpad_hidden(struct sway_container *con) {
return con->scratchpad && !con->workspace;
}
Expand Down