aboutsummaryrefslogtreecommitdiff
path: root/sway
diff options
context:
space:
mode:
Diffstat (limited to 'sway')
-rw-r--r--sway/commands.c3
-rw-r--r--sway/commands/move.c19
-rw-r--r--sway/commands/scratchpad.c36
-rw-r--r--sway/criteria.c9
-rw-r--r--sway/meson.build2
-rw-r--r--sway/scratchpad.c175
-rw-r--r--sway/tree/container.c8
-rw-r--r--sway/tree/layout.c8
-rw-r--r--sway/tree/view.c2
9 files changed, 257 insertions, 5 deletions
diff --git a/sway/commands.c b/sway/commands.c
index f40f0e9d..fdae1961 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -149,6 +149,7 @@ static struct cmd_handler command_handlers[] = {
{ "reload", cmd_reload },
{ "rename", cmd_rename },
{ "resize", cmd_resize },
+ { "scratchpad", cmd_scratchpad },
{ "split", cmd_split },
{ "splith", cmd_splith },
{ "splitt", cmd_splitt },
@@ -326,7 +327,7 @@ struct cmd_results *execute_command(char *_exec, struct sway_seat *seat) {
} while(head);
cleanup:
free(exec);
- free(views);
+ list_free(views);
if (!results) {
results = cmd_results_new(CMD_SUCCESS, NULL, NULL);
}
diff --git a/sway/commands/move.c b/sway/commands/move.c
index 6ec050a8..1940043d 100644
--- a/sway/commands/move.c
+++ b/sway/commands/move.c
@@ -9,6 +9,7 @@
#include "sway/input/cursor.h"
#include "sway/input/seat.h"
#include "sway/output.h"
+#include "sway/scratchpad.h"
#include "sway/tree/arrange.h"
#include "sway/tree/container.h"
#include "sway/tree/layout.h"
@@ -296,6 +297,19 @@ static struct cmd_results *move_to_position(struct sway_container *container,
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}
+static struct cmd_results *move_to_scratchpad(struct sway_container *con) {
+ if (con->type != C_CONTAINER && con->type != C_VIEW) {
+ return cmd_results_new(CMD_INVALID, "move",
+ "Only views and containers can be moved to the scratchpad");
+ }
+ if (con->scratchpad) {
+ return cmd_results_new(CMD_INVALID, "move",
+ "Container is already in the scratchpad");
+ }
+ scratchpad_add_container(con);
+ return cmd_results_new(CMD_SUCCESS, NULL, NULL);
+}
+
struct cmd_results *cmd_move(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "move", EXPECTED_AT_LEAST, 1))) {
@@ -317,10 +331,9 @@ struct cmd_results *cmd_move(int argc, char **argv) {
} else if (strcasecmp(argv[0], "workspace") == 0) {
return cmd_move_workspace(current, argc, argv);
} else if (strcasecmp(argv[0], "scratchpad") == 0
- || (strcasecmp(argv[0], "to") == 0
+ || (strcasecmp(argv[0], "to") == 0 && argc == 2
&& strcasecmp(argv[1], "scratchpad") == 0)) {
- // TODO: scratchpad
- return cmd_results_new(CMD_FAILURE, "move", "Unimplemented");
+ return move_to_scratchpad(current);
} else if (strcasecmp(argv[0], "position") == 0) {
return move_to_position(current, argc, argv);
} else if (strcasecmp(argv[0], "absolute") == 0) {
diff --git a/sway/commands/scratchpad.c b/sway/commands/scratchpad.c
new file mode 100644
index 00000000..ccc07c87
--- /dev/null
+++ b/sway/commands/scratchpad.c
@@ -0,0 +1,36 @@
+#include "log.h"
+#include "sway/commands.h"
+#include "sway/config.h"
+#include "sway/scratchpad.h"
+#include "sway/tree/container.h"
+
+struct cmd_results *cmd_scratchpad(int argc, char **argv) {
+ struct cmd_results *error = NULL;
+ if ((error = checkarg(argc, "scratchpad", EXPECTED_EQUAL_TO, 1))) {
+ return error;
+ }
+ if (strcmp(argv[0], "show") != 0) {
+ return cmd_results_new(CMD_INVALID, "scratchpad",
+ "Expected 'scratchpad show'");
+ }
+ if (!root_container.sway_root->scratchpad->length) {
+ return cmd_results_new(CMD_INVALID, "scratchpad",
+ "Scratchpad is empty");
+ }
+
+ if (config->handler_context.using_criteria) {
+ // If using criteria, this command is executed for every container which
+ // matches the criteria. If this container isn't in the scratchpad,
+ // we'll just silently return a success.
+ struct sway_container *con = config->handler_context.current_container;
+ wlr_log(WLR_INFO, "cmd_scratchpad(%s)", con->name);
+ if (!con->scratchpad) {
+ return cmd_results_new(CMD_SUCCESS, NULL, NULL);
+ }
+ scratchpad_toggle_container(con);
+ } else {
+ scratchpad_toggle_auto();
+ }
+
+ return cmd_results_new(CMD_SUCCESS, NULL, NULL);
+}
diff --git a/sway/criteria.c b/sway/criteria.c
index e2b248de..c2e9c07e 100644
--- a/sway/criteria.c
+++ b/sway/criteria.c
@@ -225,6 +225,15 @@ list_t *criteria_get_views(struct criteria *criteria) {
};
container_for_each_descendant_dfs(&root_container,
criteria_get_views_iterator, &data);
+
+ // Scratchpad items which are hidden are not in the tree.
+ for (int i = 0; i < root_container.sway_root->scratchpad->length; ++i) {
+ struct sway_container *con =
+ root_container.sway_root->scratchpad->items[i];
+ if (!con->parent) {
+ criteria_get_views_iterator(con, &data);
+ }
+ }
return matches;
}
diff --git a/sway/meson.build b/sway/meson.build
index f4fdc8ea..30c848e2 100644
--- a/sway/meson.build
+++ b/sway/meson.build
@@ -7,6 +7,7 @@ sway_sources = files(
'debug-tree.c',
'ipc-json.c',
'ipc-server.c',
+ 'scratchpad.c',
'security.c',
'desktop/desktop.c',
@@ -67,6 +68,7 @@ sway_sources = files(
'commands/reload.c',
'commands/rename.c',
'commands/resize.c',
+ 'commands/scratchpad.c',
'commands/seat.c',
'commands/seat/attach.c',
'commands/seat/cursor.c',
diff --git a/sway/scratchpad.c b/sway/scratchpad.c
new file mode 100644
index 00000000..1e836e7d
--- /dev/null
+++ b/sway/scratchpad.c
@@ -0,0 +1,175 @@
+#define _XOPEN_SOURCE 700
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include "sway/scratchpad.h"
+#include "sway/input/seat.h"
+#include "sway/tree/arrange.h"
+#include "sway/tree/container.h"
+#include "sway/tree/view.h"
+#include "sway/tree/workspace.h"
+#include "list.h"
+#include "log.h"
+
+void scratchpad_add_container(struct sway_container *con) {
+ if (!sway_assert(!con->scratchpad, "Container is already in scratchpad")) {
+ return;
+ }
+ con->scratchpad = true;
+ list_add(root_container.sway_root->scratchpad, con);
+
+ struct sway_container *parent = con->parent;
+ container_set_floating(con, true);
+ container_remove_child(con);
+ arrange_windows(parent);
+
+ struct sway_seat *seat = input_manager_current_seat(input_manager);
+ seat_set_focus(seat, seat_get_focus_inactive(seat, parent));
+}
+
+void scratchpad_remove_container(struct sway_container *con) {
+ if (!sway_assert(con->scratchpad, "Container is not in scratchpad")) {
+ return;
+ }
+ con->scratchpad = false;
+ for (int i = 0; i < root_container.sway_root->scratchpad->length; ++i) {
+ if (root_container.sway_root->scratchpad->items[i] == con) {
+ list_del(root_container.sway_root->scratchpad, i);
+ break;
+ }
+ }
+}
+
+/**
+ * Show a single scratchpad container.
+ * The container might be visible on another workspace already.
+ */
+static void scratchpad_show(struct sway_container *con) {
+ struct sway_seat *seat = input_manager_current_seat(input_manager);
+ struct sway_container *ws = seat_get_focus(seat);
+ if (ws->type != C_WORKSPACE) {
+ ws = container_parent(ws, C_WORKSPACE);
+ }
+
+ // If the current con or any of its parents are in fullscreen mode, we
+ // first need to disable it before showing the scratchpad con.
+ if (ws->sway_workspace->fullscreen) {
+ view_set_fullscreen(ws->sway_workspace->fullscreen, false);
+ }
+
+ // Show the container
+ if (con->parent) {
+ container_remove_child(con);
+ }
+ container_add_child(ws->sway_workspace->floating, con);
+
+ // Make sure the container's center point overlaps this workspace
+ double center_lx = con->x + con->width / 2;
+ double center_ly = con->y + con->height / 2;
+
+ struct wlr_box workspace_box;
+ container_get_box(ws, &workspace_box);
+ if (!wlr_box_contains_point(&workspace_box, center_lx, center_ly)) {
+ // Maybe resize it
+ if (con->width > ws->width || con->height > ws->height) {
+ // TODO: Do this properly once we can float C_CONTAINERs
+ if (con->type == C_VIEW) {
+ view_init_floating(con->sway_view);
+ arrange_windows(con);
+ }
+ }
+
+ // Center it
+ double new_lx = ws->x + (ws->width - con->width) / 2;
+ double new_ly = ws->y + (ws->height - con->height) / 2;
+ container_floating_move_to(con, new_lx, new_ly);
+ }
+
+ seat_set_focus(seat, con);
+
+ container_set_dirty(con->parent);
+}
+
+/**
+ * Hide a single scratchpad container.
+ * The container might not be the focused container (eg. when using criteria).
+ */
+static void scratchpad_hide(struct sway_container *con) {
+ struct sway_seat *seat = input_manager_current_seat(input_manager);
+ struct sway_container *focus = seat_get_focus(seat);
+ struct sway_container *ws = container_parent(con, C_WORKSPACE);
+
+ container_remove_child(con);
+ arrange_windows(ws);
+ if (con == focus) {
+ seat_set_focus(seat, seat_get_focus_inactive(seat, ws));
+ }
+ list_move_to_end(root_container.sway_root->scratchpad, con);
+}
+
+void scratchpad_toggle_auto(void) {
+ struct sway_seat *seat = input_manager_current_seat(input_manager);
+ struct sway_container *focus = seat_get_focus(seat);
+ struct sway_container *ws = focus->type == C_WORKSPACE ?
+ focus : container_parent(focus, C_WORKSPACE);
+
+ // Check if the currently focused window is a scratchpad window and should
+ // be hidden again.
+ if (focus->scratchpad) {
+ wlr_log(WLR_DEBUG, "Focus is a scratchpad window - hiding %s",
+ focus->name);
+ scratchpad_hide(focus);
+ return;
+ }
+
+ // Check if there is an unfocused scratchpad window on the current workspace
+ // and focus it.
+ for (int i = 0; i < ws->sway_workspace->floating->children->length; ++i) {
+ struct sway_container *floater =
+ ws->sway_workspace->floating->children->items[i];
+ if (floater->scratchpad && focus != floater) {
+ wlr_log(WLR_DEBUG,
+ "Focusing other scratchpad window (%s) in this workspace",
+ floater->name);
+ scratchpad_show(floater);
+ return;
+ }
+ }
+
+ // Check if there is a visible scratchpad window on another workspace.
+ // In this case we move it to the current workspace.
+ for (int i = 0; i < root_container.sway_root->scratchpad->length; ++i) {
+ struct sway_container *con =
+ root_container.sway_root->scratchpad->items[i];
+ if (con->parent) {
+ wlr_log(WLR_DEBUG,
+ "Moving a visible scratchpad window (%s) to this workspace",
+ con->name);
+ scratchpad_show(con);
+ return;
+ }
+ }
+
+ // Take the container at the bottom of the scratchpad list
+ if (!sway_assert(root_container.sway_root->scratchpad->length,
+ "Scratchpad is empty")) {
+ return;
+ }
+ struct sway_container *con = root_container.sway_root->scratchpad->items[0];
+ wlr_log(WLR_DEBUG, "Showing %s from list", con->name);
+ scratchpad_show(con);
+}
+
+void scratchpad_toggle_container(struct sway_container *con) {
+ if (!sway_assert(con->scratchpad, "Container isn't in the scratchpad")) {
+ return;
+ }
+
+ // Check if it matches a currently visible scratchpad window and hide it.
+ if (con->parent) {
+ scratchpad_hide(con);
+ return;
+ }
+
+ scratchpad_show(con);
+}
diff --git a/sway/tree/container.c b/sway/tree/container.c
index 42c1d024..4f743c40 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -17,6 +17,7 @@
#include "sway/input/seat.h"
#include "sway/ipc-server.h"
#include "sway/output.h"
+#include "sway/scratchpad.h"
#include "sway/server.h"
#include "sway/tree/arrange.h"
#include "sway/tree/layout.h"
@@ -328,6 +329,10 @@ static struct sway_container *container_destroy_noreaping(
con->destroying = true;
container_set_dirty(con);
+ if (con->scratchpad) {
+ scratchpad_remove_container(con);
+ }
+
if (!con->parent) {
return NULL;
}
@@ -955,6 +960,9 @@ void container_set_floating(struct sway_container *container, bool enable) {
container_reap_empty_recursive(workspace);
} else {
// Returning to tiled
+ if (container->scratchpad) {
+ scratchpad_remove_container(container);
+ }
container_remove_child(container);
container_add_child(workspace, container);
container->width = container->parent->width;
diff --git a/sway/tree/layout.c b/sway/tree/layout.c
index 533906fa..a2be0ef3 100644
--- a/sway/tree/layout.c
+++ b/sway/tree/layout.c
@@ -42,6 +42,7 @@ void layout_init(void) {
wl_list_init(&root_container.sway_root->xwayland_unmanaged);
wl_list_init(&root_container.sway_root->drag_icons);
wl_signal_init(&root_container.sway_root->events.new_container);
+ root_container.sway_root->scratchpad = create_list();
root_container.sway_root->output_layout_change.notify =
output_layout_handle_change;
@@ -135,6 +136,10 @@ void container_add_child(struct sway_container *parent,
list_add(parent->children, child);
child->parent = parent;
container_handle_fullscreen_reparent(child, old_parent);
+ if (old_parent) {
+ container_set_dirty(old_parent);
+ }
+ container_set_dirty(child);
}
struct sway_container *container_remove_child(struct sway_container *child) {
@@ -153,6 +158,9 @@ struct sway_container *container_remove_child(struct sway_container *child) {
child->parent = NULL;
container_notify_subtree_changed(parent);
+ container_set_dirty(parent);
+ container_set_dirty(child);
+
return parent;
}
diff --git a/sway/tree/view.c b/sway/tree/view.c
index 89150a69..9d88d7aa 100644
--- a/sway/tree/view.c
+++ b/sway/tree/view.c
@@ -1065,7 +1065,7 @@ void view_update_marks_textures(struct sway_view *view) {
}
bool view_is_visible(struct sway_view *view) {
- if (!view->swayc || view->swayc->destroying) {
+ if (!view->swayc || view->swayc->destroying || !view->swayc->parent) {
return false;
}
struct sway_container *workspace =