aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sway/commands/swap.c175
-rw-r--r--sway/tree/container.c174
2 files changed, 175 insertions, 174 deletions
diff --git a/sway/commands/swap.c b/sway/commands/swap.c
index 9355944d..b457f121 100644
--- a/sway/commands/swap.c
+++ b/sway/commands/swap.c
@@ -5,6 +5,7 @@
5#include "sway/commands.h" 5#include "sway/commands.h"
6#include "sway/output.h" 6#include "sway/output.h"
7#include "sway/tree/arrange.h" 7#include "sway/tree/arrange.h"
8#include "sway/tree/container.h"
8#include "sway/tree/root.h" 9#include "sway/tree/root.h"
9#include "sway/tree/view.h" 10#include "sway/tree/view.h"
10#include "sway/tree/workspace.h" 11#include "sway/tree/workspace.h"
@@ -13,180 +14,6 @@
13static const char expected_syntax[] = 14static const char expected_syntax[] =
14 "Expected 'swap container with id|con_id|mark <arg>'"; 15 "Expected 'swap container with id|con_id|mark <arg>'";
15 16
16static void swap_places(struct sway_container *con1,
17 struct sway_container *con2) {
18 struct sway_container *temp = malloc(sizeof(struct sway_container));
19 temp->pending.x = con1->pending.x;
20 temp->pending.y = con1->pending.y;
21 temp->pending.width = con1->pending.width;
22 temp->pending.height = con1->pending.height;
23 temp->width_fraction = con1->width_fraction;
24 temp->height_fraction = con1->height_fraction;
25 temp->pending.parent = con1->pending.parent;
26 temp->pending.workspace = con1->pending.workspace;
27 bool temp_floating = container_is_floating(con1);
28
29 con1->pending.x = con2->pending.x;
30 con1->pending.y = con2->pending.y;
31 con1->pending.width = con2->pending.width;
32 con1->pending.height = con2->pending.height;
33 con1->width_fraction = con2->width_fraction;
34 con1->height_fraction = con2->height_fraction;
35
36 con2->pending.x = temp->pending.x;
37 con2->pending.y = temp->pending.y;
38 con2->pending.width = temp->pending.width;
39 con2->pending.height = temp->pending.height;
40 con2->width_fraction = temp->width_fraction;
41 con2->height_fraction = temp->height_fraction;
42
43 int temp_index = container_sibling_index(con1);
44 if (con2->pending.parent) {
45 container_insert_child(con2->pending.parent, con1,
46 container_sibling_index(con2));
47 } else if (container_is_floating(con2)) {
48 workspace_add_floating(con2->pending.workspace, con1);
49 } else {
50 workspace_insert_tiling(con2->pending.workspace, con1,
51 container_sibling_index(con2));
52 }
53 if (temp->pending.parent) {
54 container_insert_child(temp->pending.parent, con2, temp_index);
55 } else if (temp_floating) {
56 workspace_add_floating(temp->pending.workspace, con2);
57 } else {
58 workspace_insert_tiling(temp->pending.workspace, con2, temp_index);
59 }
60
61 free(temp);
62}
63
64static void swap_focus(struct sway_container *con1,
65 struct sway_container *con2, struct sway_seat *seat,
66 struct sway_container *focus) {
67 if (focus == con1 || focus == con2) {
68 struct sway_workspace *ws1 = con1->pending.workspace;
69 struct sway_workspace *ws2 = con2->pending.workspace;
70 enum sway_container_layout layout1 = container_parent_layout(con1);
71 enum sway_container_layout layout2 = container_parent_layout(con2);
72 if (focus == con1 && (layout2 == L_TABBED || layout2 == L_STACKED)) {
73 if (workspace_is_visible(ws2)) {
74 seat_set_focus(seat, &con2->node);
75 }
76 seat_set_focus_container(seat, ws1 != ws2 ? con2 : con1);
77 } else if (focus == con2 && (layout1 == L_TABBED
78 || layout1 == L_STACKED)) {
79 if (workspace_is_visible(ws1)) {
80 seat_set_focus(seat, &con1->node);
81 }
82 seat_set_focus_container(seat, ws1 != ws2 ? con1 : con2);
83 } else if (ws1 != ws2) {
84 seat_set_focus_container(seat, focus == con1 ? con2 : con1);
85 } else {
86 seat_set_focus_container(seat, focus);
87 }
88 } else {
89 seat_set_focus_container(seat, focus);
90 }
91
92 if (root->fullscreen_global) {
93 seat_set_focus(seat,
94 seat_get_focus_inactive(seat, &root->fullscreen_global->node));
95 }
96}
97
98void container_swap(struct sway_container *con1, struct sway_container *con2) {
99 if (!sway_assert(con1 && con2, "Cannot swap with nothing")) {
100 return;
101 }
102 if (!sway_assert(!container_has_ancestor(con1, con2)
103 && !container_has_ancestor(con2, con1),
104 "Cannot swap ancestor and descendant")) {
105 return;
106 }
107
108 sway_log(SWAY_DEBUG, "Swapping containers %zu and %zu",
109 con1->node.id, con2->node.id);
110
111 bool scratch1 = con1->scratchpad;
112 bool hidden1 = container_is_scratchpad_hidden(con1);
113 bool scratch2 = con2->scratchpad;
114 bool hidden2 = container_is_scratchpad_hidden(con2);
115 if (scratch1) {
116 if (hidden1) {
117 root_scratchpad_show(con1);
118 }
119 root_scratchpad_remove_container(con1);
120 }
121 if (scratch2) {
122 if (hidden2) {
123 root_scratchpad_show(con2);
124 }
125 root_scratchpad_remove_container(con2);
126 }
127
128 enum sway_fullscreen_mode fs1 = con1->pending.fullscreen_mode;
129 if (fs1) {
130 container_fullscreen_disable(con1);
131 }
132 enum sway_fullscreen_mode fs2 = con2->pending.fullscreen_mode;
133 if (fs2) {
134 container_fullscreen_disable(con2);
135 }
136
137 struct sway_seat *seat = config->handler_context.seat;
138 struct sway_container *focus = seat_get_focused_container(seat);
139 struct sway_workspace *vis1 =
140 output_get_active_workspace(con1->pending.workspace->output);
141 struct sway_workspace *vis2 =
142 output_get_active_workspace(con2->pending.workspace->output);
143 if (!sway_assert(vis1 && vis2, "con1 or con2 are on an output without a"
144 "workspace. This should not happen")) {
145 return;
146 }
147
148 char *stored_prev_name = NULL;
149 if (seat->prev_workspace_name) {
150 stored_prev_name = strdup(seat->prev_workspace_name);
151 }
152
153 swap_places(con1, con2);
154
155 if (!workspace_is_visible(vis1)) {
156 seat_set_focus(seat, seat_get_focus_inactive(seat, &vis1->node));
157 }
158 if (!workspace_is_visible(vis2)) {
159 seat_set_focus(seat, seat_get_focus_inactive(seat, &vis2->node));
160 }
161
162 swap_focus(con1, con2, seat, focus);
163
164 if (stored_prev_name) {
165 free(seat->prev_workspace_name);
166 seat->prev_workspace_name = stored_prev_name;
167 }
168
169 if (scratch1) {
170 root_scratchpad_add_container(con2, NULL);
171 if (!hidden1) {
172 root_scratchpad_show(con2);
173 }
174 }
175 if (scratch2) {
176 root_scratchpad_add_container(con1, NULL);
177 if (!hidden2) {
178 root_scratchpad_show(con1);
179 }
180 }
181
182 if (fs1) {
183 container_set_fullscreen(con2, fs1);
184 }
185 if (fs2) {
186 container_set_fullscreen(con1, fs2);
187 }
188}
189
190static bool test_con_id(struct sway_container *container, void *data) { 17static bool test_con_id(struct sway_container *container, void *data) {
191 size_t *con_id = data; 18 size_t *con_id = data;
192 return container->node.id == *con_id; 19 return container->node.id == *con_id;
diff --git a/sway/tree/container.c b/sway/tree/container.c
index 20701081..335dae87 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -1764,3 +1764,177 @@ int container_squash(struct sway_container *con) {
1764 } 1764 }
1765 return change; 1765 return change;
1766} 1766}
1767
1768static void swap_places(struct sway_container *con1,
1769 struct sway_container *con2) {
1770 struct sway_container *temp = malloc(sizeof(struct sway_container));
1771 temp->pending.x = con1->pending.x;
1772 temp->pending.y = con1->pending.y;
1773 temp->pending.width = con1->pending.width;
1774 temp->pending.height = con1->pending.height;
1775 temp->width_fraction = con1->width_fraction;
1776 temp->height_fraction = con1->height_fraction;
1777 temp->pending.parent = con1->pending.parent;
1778 temp->pending.workspace = con1->pending.workspace;
1779 bool temp_floating = container_is_floating(con1);
1780
1781 con1->pending.x = con2->pending.x;
1782 con1->pending.y = con2->pending.y;
1783 con1->pending.width = con2->pending.width;
1784 con1->pending.height = con2->pending.height;
1785 con1->width_fraction = con2->width_fraction;
1786 con1->height_fraction = con2->height_fraction;
1787
1788 con2->pending.x = temp->pending.x;
1789 con2->pending.y = temp->pending.y;
1790 con2->pending.width = temp->pending.width;
1791 con2->pending.height = temp->pending.height;
1792 con2->width_fraction = temp->width_fraction;
1793 con2->height_fraction = temp->height_fraction;
1794
1795 int temp_index = container_sibling_index(con1);
1796 if (con2->pending.parent) {
1797 container_insert_child(con2->pending.parent, con1,
1798 container_sibling_index(con2));
1799 } else if (container_is_floating(con2)) {
1800 workspace_add_floating(con2->pending.workspace, con1);
1801 } else {
1802 workspace_insert_tiling(con2->pending.workspace, con1,
1803 container_sibling_index(con2));
1804 }
1805 if (temp->pending.parent) {
1806 container_insert_child(temp->pending.parent, con2, temp_index);
1807 } else if (temp_floating) {
1808 workspace_add_floating(temp->pending.workspace, con2);
1809 } else {
1810 workspace_insert_tiling(temp->pending.workspace, con2, temp_index);
1811 }
1812
1813 free(temp);
1814}
1815
1816static void swap_focus(struct sway_container *con1,
1817 struct sway_container *con2, struct sway_seat *seat,
1818 struct sway_container *focus) {
1819 if (focus == con1 || focus == con2) {
1820 struct sway_workspace *ws1 = con1->pending.workspace;
1821 struct sway_workspace *ws2 = con2->pending.workspace;
1822 enum sway_container_layout layout1 = container_parent_layout(con1);
1823 enum sway_container_layout layout2 = container_parent_layout(con2);
1824 if (focus == con1 && (layout2 == L_TABBED || layout2 == L_STACKED)) {
1825 if (workspace_is_visible(ws2)) {
1826 seat_set_focus(seat, &con2->node);
1827 }
1828 seat_set_focus_container(seat, ws1 != ws2 ? con2 : con1);
1829 } else if (focus == con2 && (layout1 == L_TABBED
1830 || layout1 == L_STACKED)) {
1831 if (workspace_is_visible(ws1)) {
1832 seat_set_focus(seat, &con1->node);
1833 }
1834 seat_set_focus_container(seat, ws1 != ws2 ? con1 : con2);
1835 } else if (ws1 != ws2) {
1836 seat_set_focus_container(seat, focus == con1 ? con2 : con1);
1837 } else {
1838 seat_set_focus_container(seat, focus);
1839 }
1840 } else {
1841 seat_set_focus_container(seat, focus);
1842 }
1843
1844 if (root->fullscreen_global) {
1845 seat_set_focus(seat,
1846 seat_get_focus_inactive(seat, &root->fullscreen_global->node));
1847 }
1848}
1849
1850void container_swap(struct sway_container *con1, struct sway_container *con2) {
1851 if (!sway_assert(con1 && con2, "Cannot swap with nothing")) {
1852 return;
1853 }
1854 if (!sway_assert(!container_has_ancestor(con1, con2)
1855 && !container_has_ancestor(con2, con1),
1856 "Cannot swap ancestor and descendant")) {
1857 return;
1858 }
1859
1860 sway_log(SWAY_DEBUG, "Swapping containers %zu and %zu",
1861 con1->node.id, con2->node.id);
1862
1863 bool scratch1 = con1->scratchpad;
1864 bool hidden1 = container_is_scratchpad_hidden(con1);
1865 bool scratch2 = con2->scratchpad;
1866 bool hidden2 = container_is_scratchpad_hidden(con2);
1867 if (scratch1) {
1868 if (hidden1) {
1869 root_scratchpad_show(con1);
1870 }
1871 root_scratchpad_remove_container(con1);
1872 }
1873 if (scratch2) {
1874 if (hidden2) {
1875 root_scratchpad_show(con2);
1876 }
1877 root_scratchpad_remove_container(con2);
1878 }
1879
1880 enum sway_fullscreen_mode fs1 = con1->pending.fullscreen_mode;
1881 if (fs1) {
1882 container_fullscreen_disable(con1);
1883 }
1884 enum sway_fullscreen_mode fs2 = con2->pending.fullscreen_mode;
1885 if (fs2) {
1886 container_fullscreen_disable(con2);
1887 }
1888
1889 struct sway_seat *seat = input_manager_current_seat();
1890 struct sway_container *focus = seat_get_focused_container(seat);
1891 struct sway_workspace *vis1 =
1892 output_get_active_workspace(con1->pending.workspace->output);
1893 struct sway_workspace *vis2 =
1894 output_get_active_workspace(con2->pending.workspace->output);
1895 if (!sway_assert(vis1 && vis2, "con1 or con2 are on an output without a"
1896 "workspace. This should not happen")) {
1897 return;
1898 }
1899
1900 char *stored_prev_name = NULL;
1901 if (seat->prev_workspace_name) {
1902 stored_prev_name = strdup(seat->prev_workspace_name);
1903 }
1904
1905 swap_places(con1, con2);
1906
1907 if (!workspace_is_visible(vis1)) {
1908 seat_set_focus(seat, seat_get_focus_inactive(seat, &vis1->node));
1909 }
1910 if (!workspace_is_visible(vis2)) {
1911 seat_set_focus(seat, seat_get_focus_inactive(seat, &vis2->node));
1912 }
1913
1914 swap_focus(con1, con2, seat, focus);
1915
1916 if (stored_prev_name) {
1917 free(seat->prev_workspace_name);
1918 seat->prev_workspace_name = stored_prev_name;
1919 }
1920
1921 if (scratch1) {
1922 root_scratchpad_add_container(con2, NULL);
1923 if (!hidden1) {
1924 root_scratchpad_show(con2);
1925 }
1926 }
1927 if (scratch2) {
1928 root_scratchpad_add_container(con1, NULL);
1929 if (!hidden2) {
1930 root_scratchpad_show(con1);
1931 }
1932 }
1933
1934 if (fs1) {
1935 container_set_fullscreen(con2, fs1);
1936 }
1937 if (fs2) {
1938 container_set_fullscreen(con1, fs2);
1939 }
1940}