diff --git a/include/container.h b/include/container.h index aadba5dea..b0fbfa97c 100644 --- a/include/container.h +++ b/include/container.h @@ -30,6 +30,9 @@ enum swayc_layouts{ struct sway_container { wlc_handle handle; + // Internal container, e.g. scratchpad + bool internal; + enum swayc_types type; enum swayc_layouts layout; diff --git a/include/layout.h b/include/layout.h index 11bf1a28a..3a32f953f 100644 --- a/include/layout.h +++ b/include/layout.h @@ -11,6 +11,8 @@ extern swayc_t root_container; extern int min_sane_w; extern int min_sane_h; +extern swayc_t *scratchpad; + void init_layout(void); void add_child(swayc_t *parent, swayc_t *child); @@ -21,7 +23,8 @@ swayc_t *replace_child(swayc_t *child, swayc_t *new_child); swayc_t *remove_child(swayc_t *child); void swap_container(swayc_t *a, swayc_t *b); -void move_container(swayc_t* container,swayc_t* root,enum movement_direction direction); +void move_container_to_direction(swayc_t* container,swayc_t* root,enum movement_direction direction); +void move_container_to(swayc_t* container, swayc_t* destination); // Layout void update_geometry(swayc_t *view); @@ -33,4 +36,6 @@ swayc_t *get_swayc_in_direction_under(swayc_t *container, enum movement_directio void recursive_resize(swayc_t *container, double amount, enum wlc_resize_edge edge); +void view_set_floating(swayc_t *view, bool floating); + #endif diff --git a/sway/commands.c b/sway/commands.c index 21ff5c7f2..5a601d5bd 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -197,58 +197,26 @@ static bool cmd_floating(struct sway_config *config, int argc, char **argv) { return false; } + swayc_t *view = get_focused_container(&root_container); + + bool floating; if (strcasecmp(argv[0], "toggle") == 0) { - swayc_t *view = get_focused_container(&root_container); - // Prevent running floating commands on things like workspaces - if (view->type != C_VIEW) { - return true; - } - // Change from nonfloating to floating - if (!view->is_floating) { - // Remove view from its current location - destroy_container(remove_child(view)); - - // and move it into workspace floating - add_floating(swayc_active_workspace(),view); - view->x = (swayc_active_workspace()->width - view->width)/2; - view->y = (swayc_active_workspace()->height - view->height)/2; - if (view->desired_width != -1) { - view->width = view->desired_width; - } - if (view->desired_height != -1) { - view->height = view->desired_height; - } - arrange_windows(swayc_active_workspace(), -1, -1); - } else { - // Delete the view from the floating list and unset its is_floating flag - // Using length-1 as the index is safe because the view must be the currently - // focused floating output - remove_child(view); - view->is_floating = false; - // Get the properly focused container, and add in the view there - swayc_t *focused = container_under_pointer(); - // If focused is null, it's because the currently focused container is a workspace - if (focused == NULL) { - focused = swayc_active_workspace(); - } - set_focused_container(focused); - - sway_log(L_DEBUG, "Non-floating focused container is %p", focused); - - // Case of focused workspace, just create as child of it - if (focused->type == C_WORKSPACE) { - add_child(focused, view); - } - // Regular case, create as sibling of current container - else { - add_sibling(focused, view); - } - // Refocus on the view once its been put back into the layout - view->width = view->height = 0; - arrange_windows(swayc_active_workspace(), -1, -1); - } - set_focused_container(view); + floating = !view->is_floating; } + else if (strcasecmp(argv[0], "enable") == 0) { + floating = true; + } + else if (strcasecmp(argv[0], "disable") == 0) { + floating = false; + } + else { + sway_log(L_ERROR, "floating - unknown token '%s', expected one of toggle, enable or disable", argv[0]); + return false; + } + + // Change from non-floating to floating or vice versa + view_set_floating(view, floating); + set_focused_container(view); return true; } @@ -344,26 +312,68 @@ static bool cmd_focus_follows_mouse(struct sway_config *config, int argc, char * } static bool cmd_move(struct sway_config *config, int argc, char **argv) { - if (!checkarg(argc, "workspace", EXPECTED_EQUAL_TO, 1)) { + if (!checkarg(argc, "workspace", EXPECTED_AT_LEAST, 1)) { return false; } swayc_t *view = get_focused_container(&root_container); if (strcasecmp(argv[0], "left") == 0) { - move_container(view,&root_container,MOVE_LEFT); + move_container_to_direction(view, &root_container, MOVE_LEFT); } else if (strcasecmp(argv[0], "right") == 0) { - move_container(view,&root_container,MOVE_RIGHT); + move_container_to_direction(view, &root_container, MOVE_RIGHT); } else if (strcasecmp(argv[0], "up") == 0) { - move_container(view,&root_container,MOVE_UP); + move_container_to_direction(view, &root_container, MOVE_UP); } else if (strcasecmp(argv[0], "down") == 0) { - move_container(view,&root_container,MOVE_DOWN); + move_container_to_direction(view, &root_container, MOVE_DOWN); + } else if (strcasecmp(argv[0], "container") == 0) { + // "move container to workspace x" + if (!checkarg(argc, "move container", EXPECTED_EQUAL_TO, 4) || + strcasecmp(argv[1], "to") != 0 || + strcasecmp(argv[2], "workspace") != 0) { + return false; + } + + if (view->type != C_CONTAINER && view->type != C_VIEW) { + return false; + } + + swayc_t *ws = workspace_by_name(argv[3]); + if (ws == NULL) { + ws = workspace_create(argv[3]); + } + move_container_to(view, ws); + } else if (strcasecmp(argv[0], "scratchpad") == 0 && view->type == C_VIEW) { + view_set_floating(view, true); + move_container_to(view, scratchpad); } else { return false; } return true; } +static bool cmd_scratchpad(struct sway_config *config, int argc, char **argv) { + if (!checkarg(argc, "scratchpad", EXPECTED_EQUAL_TO, 1)) { + return false; + } + + if (strcasecmp(argv[0], "show") != 0) { + sway_log(L_ERROR, "scratchpad - unknown token '%s', expected show", argv[0]); + return false; + } + + if (scratchpad->floating->length == 0) { + return false; + } + + swayc_t *view = scratchpad->floating->items[0]; + sway_log(L_DEBUG, "Popped view %p from scratchpad", view); + remove_child(view); + move_container_to(view, swayc_active_workspace()); + + return true; +} + static bool cmd_output(struct sway_config *config, int argc, char **argv) { if (!checkarg(argc, "output", EXPECTED_AT_LEAST, 1)) { return false; @@ -705,6 +715,7 @@ static struct cmd_handler handlers[] = { { "output", cmd_output}, { "reload", cmd_reload }, { "resize", cmd_resize }, + { "scratchpad", cmd_scratchpad }, { "set", cmd_set }, { "split", cmd_split }, { "splith", cmd_splith }, diff --git a/sway/container.c b/sway/container.c index d23cef8fb..8dca81460 100644 --- a/sway/container.c +++ b/sway/container.c @@ -524,8 +524,10 @@ void set_view_visibility(swayc_t *view, void *data) { if (view->type == C_VIEW) { wlc_view_set_mask(view->handle, *p); if (*p == 2) { + sway_log(L_DEBUG, "Showing view %p(%s)", view, view->name); wlc_view_bring_to_front(view->handle); } else { + sway_log(L_DEBUG, "Hiding view %p(%s)", view, view->name); wlc_view_send_to_back(view->handle); } } diff --git a/sway/layout.c b/sway/layout.c index a37e137c8..c8c7d5dbc 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -8,16 +8,32 @@ #include "container.h" #include "workspace.h" #include "focus.h" +#include "handlers.h" swayc_t root_container; int min_sane_h = 60; int min_sane_w = 100; +static swayc_t null_output; +swayc_t *scratchpad = NULL; + void init_layout(void) { root_container.type = C_ROOT; root_container.layout = L_NONE; + null_output.name = "root"; root_container.children = create_list(); root_container.handle = -1; + + null_output.type = C_OUTPUT; + null_output.layout = L_NONE; + null_output.name = "__i3"; + null_output.children = create_list(); + null_output.handle = -1; + null_output.internal = true; + add_child(&root_container, &null_output); + + scratchpad = new_workspace(&null_output, "__i3_scratch"); + scratchpad->internal = true; } static int index_child(swayc_t *child) { @@ -161,7 +177,7 @@ swayc_t *remove_child(swayc_t *child) { //TODO: Implement horizontal movement. //TODO: Implement move to a different workspace. -void move_container(swayc_t *container,swayc_t* root,enum movement_direction direction){ +void move_container_to_direction(swayc_t *container,swayc_t* root,enum movement_direction direction){ sway_log(L_DEBUG, "Moved window"); swayc_t *temp; int i; @@ -197,7 +213,7 @@ void move_container(swayc_t *container,swayc_t* root,enum movement_direction dir break; } else if (child->children != NULL){ - move_container(container,child,direction); + move_container_to_direction(container,child,direction); } } @@ -228,6 +244,22 @@ void update_geometry(swayc_t *container) { return; } +void move_container_to(swayc_t* container, swayc_t* destination) { + destroy_container(remove_child(container)); + set_focused_container(get_focused_view(&root_container)); + if (container->is_floating) { + add_floating(destination, container); + } else { + add_child(destination, container); + } + // TODO: don't set it invisible here, it could have been + // moved to another visible workspace, or from the scratchpad + uint32_t mask = 2; + set_view_visibility(container, &mask); + arrange_windows(&root_container, -1, -1); +} + + void arrange_windows(swayc_t *container, double width, double height) { int i; if (width == -1 || height == -1) { @@ -499,3 +531,54 @@ void recursive_resize(swayc_t *container, double amount, enum wlc_resize_edge ed } } } + +void view_set_floating(swayc_t *view, bool floating) { + // Prevent running floating commands on things like workspaces + if (view->type != C_VIEW) { + return; + } + + if (floating) { + // Remove view from its current location + destroy_container(remove_child(view)); + + // and move it into workspace floating + add_floating(swayc_active_workspace(),view); + view->x = (swayc_active_workspace()->width - view->width)/2; + view->y = (swayc_active_workspace()->height - view->height)/2; + if (view->desired_width != -1) { + view->width = view->desired_width; + } + if (view->desired_height != -1) { + view->height = view->desired_height; + } + arrange_windows(swayc_active_workspace(), -1, -1); + } else { + // Delete the view from the floating list and unset its is_floating flag + // Using length-1 as the index is safe because the view must be the currently + // focused floating output + remove_child(view); + view->is_floating = false; + // Get the properly focused container, and add in the view there + swayc_t *focused = container_under_pointer(); + // If focused is null, it's because the currently focused container is a workspace + if (focused == NULL) { + focused = swayc_active_workspace(); + } + set_focused_container(focused); + + sway_log(L_DEBUG, "Non-floating focused container is %p", focused); + + // Case of focused workspace, just create as child of it + if (focused->type == C_WORKSPACE) { + add_child(focused, view); + } + // Regular case, create as sibling of current container + else { + add_sibling(focused, view); + } + // Refocus on the view once its been put back into the layout + view->width = view->height = 0; + arrange_windows(swayc_active_workspace(), -1, -1); + } +} diff --git a/sway/workspace.c b/sway/workspace.c index 4c8bb62f0..1a77565d9 100644 --- a/sway/workspace.c +++ b/sway/workspace.c @@ -113,20 +113,17 @@ void workspace_next() { return; } } - if (root_container.children->length > 1) { - for (i = 0; i < root_container.children->length - 1; i++) { - if (root_container.children->items[i] == current_output) { - workspace_switch(((swayc_t *)root_container.children->items[i + 1])->focused); - workspace_output_next(); - return; + + for (i = 0; i < root_container.children->length; i++) { + if (root_container.children->items[i] == current_output) { + swayc_t *next_output = root_container.children->items[(i + 1)%root_container.children->length]; + if (next_output->internal) { + next_output = root_container.children->items[(i + 2)%root_container.children->length]; } + workspace_switch(next_output->focused); + workspace_output_next(); + return; } - // If we're at the last output, then go to the first - workspace_switch(((swayc_t *)root_container.children->items[0])->focused); - workspace_output_next(); - return; - } else { - workspace_switch(current_output->children->items[0]); } } @@ -157,20 +154,18 @@ void workspace_prev() { return; } } - if (root_container.children->length > 1) { - for (i = 1; i < root_container.children->length; i++) { - if (root_container.children->items[i] == current_output) { - workspace_switch(((swayc_t *)root_container.children->items[i - 1])->focused); - workspace_output_next(); - return; + + int num_outputs = root_container.children->length; + for (i = 0; i < num_outputs; i++) { + if (root_container.children->items[i] == current_output) { + swayc_t *next_output = root_container.children->items[(num_outputs + i - 1)%num_outputs]; + if (next_output->internal) { + next_output = root_container.children->items[(num_outputs + i - 2)%num_outputs]; } + workspace_switch(next_output->focused); + workspace_output_next(); + return; } - // If we're at the first output, then go to the last - workspace_switch(((swayc_t *)root_container.children->items[root_container.children->length-1])->focused); - workspace_output_next(); - return; - } else { - workspace_switch(current_output->children->items[current_output->children->length - 1]); } }