diff options
Diffstat (limited to 'sway/focus.c')
-rw-r--r-- | sway/focus.c | 277 |
1 files changed, 0 insertions, 277 deletions
diff --git a/sway/focus.c b/sway/focus.c deleted file mode 100644 index e9b032f8..00000000 --- a/sway/focus.c +++ /dev/null | |||
@@ -1,277 +0,0 @@ | |||
1 | #include "stdbool.h" | ||
2 | #include <wlc/wlc.h> | ||
3 | #include "sway/focus.h" | ||
4 | #include "sway/workspace.h" | ||
5 | #include "sway/layout.h" | ||
6 | #include "sway/config.h" | ||
7 | #include "sway/extensions.h" | ||
8 | #include "sway/input_state.h" | ||
9 | #include "sway/ipc-server.h" | ||
10 | #include "sway/border.h" | ||
11 | #include "log.h" | ||
12 | |||
13 | bool locked_container_focus = false; | ||
14 | bool suspend_workspace_cleanup = false; | ||
15 | |||
16 | // switches parent focus to c. will switch it accordingly | ||
17 | static void update_focus(swayc_t *c) { | ||
18 | // Handle if focus switches | ||
19 | swayc_t *parent = c->parent; | ||
20 | if (!parent) return; | ||
21 | if (parent->focused != c) { | ||
22 | // Get previous focus | ||
23 | swayc_t *prev = parent->focused; | ||
24 | // Set new focus | ||
25 | parent->focused = c; | ||
26 | |||
27 | switch (c->type) { | ||
28 | // Shouldn't happen | ||
29 | case C_ROOT: return; | ||
30 | |||
31 | // Case where output changes | ||
32 | case C_OUTPUT: | ||
33 | wlc_output_focus(c->handle); | ||
34 | break; | ||
35 | |||
36 | // Case where workspace changes | ||
37 | case C_WORKSPACE: | ||
38 | if (prev) { | ||
39 | ipc_event_workspace(prev, c, "focus"); | ||
40 | |||
41 | // if the old workspace has no children, destroy it | ||
42 | if(prev->children->length == 0 && prev->floating->length == 0 && !suspend_workspace_cleanup) { | ||
43 | destroy_workspace(prev); | ||
44 | } else { | ||
45 | // update visibility of old workspace | ||
46 | update_visibility(prev); | ||
47 | } | ||
48 | } | ||
49 | // Update visibility of newly focused workspace | ||
50 | update_visibility(c); | ||
51 | break; | ||
52 | |||
53 | default: | ||
54 | case C_VIEW: | ||
55 | case C_CONTAINER: | ||
56 | break; | ||
57 | } | ||
58 | } | ||
59 | } | ||
60 | |||
61 | bool move_focus(enum movement_direction direction) { | ||
62 | swayc_t *old_view = get_focused_container(&root_container); | ||
63 | swayc_t *new_view = get_swayc_in_direction(old_view, direction); | ||
64 | if (!new_view) { | ||
65 | return false; | ||
66 | } else if (new_view->type == C_ROOT) { | ||
67 | sway_log(L_DEBUG, "Not setting focus above the workspace level"); | ||
68 | return false; | ||
69 | } else if (new_view->type == C_OUTPUT) { | ||
70 | return set_focused_container(swayc_active_workspace_for(new_view)); | ||
71 | } else if (direction == MOVE_PARENT || direction == MOVE_CHILD) { | ||
72 | return set_focused_container(new_view); | ||
73 | } else if (config->mouse_warping) { | ||
74 | swayc_t *old_op = old_view->type == C_OUTPUT ? | ||
75 | old_view : swayc_parent_by_type(old_view, C_OUTPUT); | ||
76 | swayc_t *focused = get_focused_view(new_view); | ||
77 | if (set_focused_container(focused)) { | ||
78 | if (old_op != swayc_active_output() && focused && focused->type == C_VIEW) { | ||
79 | center_pointer_on(focused); | ||
80 | } | ||
81 | return true; | ||
82 | } | ||
83 | } else { | ||
84 | return set_focused_container(get_focused_view(new_view)); | ||
85 | } | ||
86 | return false; | ||
87 | } | ||
88 | |||
89 | swayc_t *get_focused_container(swayc_t *parent) { | ||
90 | if (!parent) { | ||
91 | return swayc_active_workspace(); | ||
92 | } | ||
93 | while (!parent->is_focused && parent->focused) { | ||
94 | parent = parent->focused; | ||
95 | } | ||
96 | return parent; | ||
97 | } | ||
98 | |||
99 | bool set_focused_container(swayc_t *c) { | ||
100 | if (locked_container_focus || !c || !c->parent) { | ||
101 | return false; | ||
102 | } | ||
103 | |||
104 | // current ("old") workspace for sending workspace change event later | ||
105 | swayc_t *old_ws = swayc_active_workspace(); | ||
106 | // keep track of child count so we can determine if it gets destroyed | ||
107 | int old_ws_child_count = 0; | ||
108 | if (old_ws) { | ||
109 | old_ws_child_count = old_ws->children->length + old_ws->floating->length; | ||
110 | } | ||
111 | |||
112 | // current ("old") focused container | ||
113 | swayc_t *old_focus = get_focused_container(&root_container); | ||
114 | // if old_focus is a workspace, then it's the same workspace as | ||
115 | // old_ws, and we'll need to null its pointer too, since it will | ||
116 | // be destroyed in the update_focus() call | ||
117 | bool old_focus_was_ws = (old_focus->type == C_WORKSPACE); | ||
118 | |||
119 | // workspace of new focused container | ||
120 | swayc_t *workspace = swayc_active_workspace_for(c); | ||
121 | |||
122 | if (swayc_is_fullscreen(get_focused_container(workspace))) { | ||
123 | // if switching to a workspace with a fullscreen view, | ||
124 | // focus on the fullscreen view | ||
125 | c = get_focused_container(workspace); | ||
126 | } | ||
127 | |||
128 | swayc_log(L_DEBUG, c, "Setting focus to %p:%" PRIuPTR, c, c->handle); | ||
129 | |||
130 | if (c->type == C_VIEW) { | ||
131 | // dispatch a window event | ||
132 | ipc_event_window(c, "focus"); | ||
133 | } | ||
134 | |||
135 | // update the global pointer | ||
136 | current_focus = c; | ||
137 | |||
138 | // update container focus from here to root, making necessary changes along | ||
139 | // the way | ||
140 | swayc_t *p = c; | ||
141 | if (p->type != C_OUTPUT && p->type != C_ROOT) { | ||
142 | p->is_focused = true; | ||
143 | } | ||
144 | while (p != &root_container) { | ||
145 | update_focus(p); | ||
146 | p = p->parent; | ||
147 | p->is_focused = false; | ||
148 | } | ||
149 | |||
150 | if (old_focus_was_ws && old_ws_child_count == 0) { | ||
151 | // this workspace was destroyed in update_focus(), so null the pointers | ||
152 | old_focus = NULL; | ||
153 | old_ws = NULL; | ||
154 | } | ||
155 | |||
156 | if (!(wlc_view_get_type(p->handle) & WLC_BIT_POPUP)) { | ||
157 | if (old_focus) { | ||
158 | if (old_focus->type == C_VIEW) { | ||
159 | wlc_view_set_state(old_focus->handle, WLC_BIT_ACTIVATED, false); | ||
160 | } | ||
161 | update_container_border(old_focus); | ||
162 | } | ||
163 | if (c->type == C_VIEW) { | ||
164 | wlc_view_set_state(c->handle, WLC_BIT_ACTIVATED, true); | ||
165 | } | ||
166 | if (!desktop_shell.is_locked) { | ||
167 | // If the system is locked, we do everything _but_ actually setting | ||
168 | // focus. This includes making our internals think that this view is | ||
169 | // focused. | ||
170 | wlc_view_focus(c->handle); | ||
171 | } | ||
172 | if (c->parent->layout != L_TABBED && c->parent->layout != L_STACKED) { | ||
173 | update_container_border(c); | ||
174 | } | ||
175 | |||
176 | swayc_t *parent = swayc_tabbed_stacked_ancestor(c); | ||
177 | if (parent != NULL) { | ||
178 | arrange_backgrounds(); | ||
179 | arrange_windows(parent, -1, -1); | ||
180 | } | ||
181 | } | ||
182 | |||
183 | if (old_ws != workspace) { | ||
184 | // old_ws might be NULL here but that's ok | ||
185 | ipc_event_workspace(old_ws, workspace, "focus"); | ||
186 | } | ||
187 | |||
188 | return true; | ||
189 | } | ||
190 | |||
191 | bool set_focused_container_for(swayc_t *a, swayc_t *c) { | ||
192 | if (locked_container_focus || !c) { | ||
193 | return false; | ||
194 | } | ||
195 | swayc_t *find = c; | ||
196 | while (find != a && (find = find->parent)) { | ||
197 | if (find == &root_container) { | ||
198 | return false; | ||
199 | } | ||
200 | } | ||
201 | |||
202 | // Get workspace for c, get that workspaces current focused container. | ||
203 | swayc_t *workspace = swayc_active_workspace_for(c); | ||
204 | swayc_t *focused = get_focused_view(workspace); | ||
205 | // if the workspace we are changing focus to has a fullscreen view return | ||
206 | if (swayc_is_fullscreen(focused) && c != focused) { | ||
207 | return false; | ||
208 | } | ||
209 | |||
210 | // Check if we are changing a parent container that will see change | ||
211 | bool effective = true; | ||
212 | while (find != &root_container) { | ||
213 | if (find->parent->focused != find) { | ||
214 | effective = false; | ||
215 | } | ||
216 | find = find->parent; | ||
217 | } | ||
218 | if (effective) { | ||
219 | // Go to set_focused_container | ||
220 | return set_focused_container(c); | ||
221 | } | ||
222 | |||
223 | sway_log(L_DEBUG, "Setting focus for %p:%" PRIuPTR " to %p:%" PRIuPTR, | ||
224 | a, a->handle, c, c->handle); | ||
225 | |||
226 | c->is_focused = true; | ||
227 | swayc_t *p = c; | ||
228 | while (p != a) { | ||
229 | update_focus(p); | ||
230 | p = p->parent; | ||
231 | p->is_focused = false; | ||
232 | } | ||
233 | return true; | ||
234 | } | ||
235 | |||
236 | swayc_t *get_focused_view(swayc_t *parent) { | ||
237 | swayc_t *c = parent; | ||
238 | while (c && c->type != C_VIEW) { | ||
239 | if (c->type == C_WORKSPACE && c->focused == NULL) { | ||
240 | return c; | ||
241 | } | ||
242 | c = c->focused; | ||
243 | } | ||
244 | if (c == NULL) { | ||
245 | c = swayc_active_workspace_for(parent); | ||
246 | } | ||
247 | return c; | ||
248 | } | ||
249 | |||
250 | swayc_t *get_focused_float(swayc_t *ws) { | ||
251 | if(!sway_assert(ws->type == C_WORKSPACE, "must be of workspace type")) { | ||
252 | ws = swayc_active_workspace(); | ||
253 | } | ||
254 | if (ws->floating->length) { | ||
255 | return ws->floating->items[ws->floating->length - 1]; | ||
256 | } | ||
257 | return NULL; | ||
258 | } | ||
259 | |||
260 | swayc_t *get_focused_view_include_floating(swayc_t *parent) { | ||
261 | swayc_t *c = parent; | ||
262 | swayc_t *f = NULL; | ||
263 | |||
264 | while (c && c->type != C_VIEW) { | ||
265 | if (c->type == C_WORKSPACE && c->focused == NULL) { | ||
266 | return ((f = get_focused_float(c))) ? f : c; | ||
267 | } | ||
268 | |||
269 | c = c->focused; | ||
270 | } | ||
271 | |||
272 | if (c == NULL) { | ||
273 | c = swayc_active_workspace_for(parent); | ||
274 | } | ||
275 | |||
276 | return c; | ||
277 | } | ||