diff options
Diffstat (limited to 'sway/focus.c')
-rw-r--r-- | sway/focus.c | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/sway/focus.c b/sway/focus.c new file mode 100644 index 00000000..2999c6d0 --- /dev/null +++ b/sway/focus.c | |||
@@ -0,0 +1,186 @@ | |||
1 | #include <wlc/wlc.h> | ||
2 | |||
3 | #include "focus.h" | ||
4 | #include "log.h" | ||
5 | #include "workspace.h" | ||
6 | |||
7 | bool locked_container_focus = false; | ||
8 | bool locked_view_focus = false; | ||
9 | |||
10 | //switches parent focus to c. will switch it accordingly | ||
11 | //TODO, everything needs a handle, so we can set front/back position properly | ||
12 | static void update_focus(swayc_t *c) { | ||
13 | //Handle if focus switches | ||
14 | swayc_t *parent = c->parent; | ||
15 | if (parent->focused != c) { | ||
16 | switch (c->type) { | ||
17 | case C_ROOT: return; | ||
18 | case C_OUTPUT: | ||
19 | wlc_output_focus(c->parent->handle); | ||
20 | break; | ||
21 | //switching workspaces | ||
22 | case C_WORKSPACE: | ||
23 | if (parent->focused) { | ||
24 | swayc_t *ws = parent->focused; | ||
25 | //hide visibility of old workspace | ||
26 | uint32_t mask = 1; | ||
27 | container_map(ws, set_view_visibility, &mask); | ||
28 | //set visibility of new workspace | ||
29 | mask = 2; | ||
30 | container_map(c, set_view_visibility, &mask); | ||
31 | wlc_output_set_mask(parent->handle, 2); | ||
32 | destroy_workspace(ws); | ||
33 | } | ||
34 | active_workspace = c; | ||
35 | break; | ||
36 | case C_VIEW: | ||
37 | case C_CONTAINER: | ||
38 | //TODO whatever to do when container changes | ||
39 | //for example, stacked and tabbing change whatever. | ||
40 | break; | ||
41 | } | ||
42 | } | ||
43 | c->parent->focused = c; | ||
44 | } | ||
45 | |||
46 | bool move_focus(enum movement_direction direction) { | ||
47 | if (locked_container_focus) { | ||
48 | return false; | ||
49 | } | ||
50 | swayc_t *current = get_focused_container(&root_container); | ||
51 | swayc_t *parent = current->parent; | ||
52 | |||
53 | if (direction == MOVE_PARENT) { | ||
54 | if (parent->type == C_OUTPUT) { | ||
55 | sway_log(L_DEBUG, "Focus cannot move to parent"); | ||
56 | return false; | ||
57 | } else { | ||
58 | sway_log(L_DEBUG, "Moving focus from %p:%ld to %p:%ld", | ||
59 | current, current->handle, parent, parent->handle); | ||
60 | set_focused_container(parent); | ||
61 | return true; | ||
62 | } | ||
63 | } | ||
64 | |||
65 | while (true) { | ||
66 | sway_log(L_DEBUG, "Moving focus away from %p", current); | ||
67 | |||
68 | // Test if we can even make a difference here | ||
69 | bool can_move = false; | ||
70 | int diff = 0; | ||
71 | if (direction == MOVE_LEFT || direction == MOVE_RIGHT) { | ||
72 | if (parent->layout == L_HORIZ || parent->type == C_ROOT) { | ||
73 | can_move = true; | ||
74 | diff = direction == MOVE_LEFT ? -1 : 1; | ||
75 | } | ||
76 | } else { | ||
77 | if (parent->layout == L_VERT) { | ||
78 | can_move = true; | ||
79 | diff = direction == MOVE_UP ? -1 : 1; | ||
80 | } | ||
81 | } | ||
82 | sway_log(L_DEBUG, "Can move? %s", can_move ? "yes" : "no"); | ||
83 | if (can_move) { | ||
84 | int i; | ||
85 | for (i = 0; i < parent->children->length; ++i) { | ||
86 | swayc_t *child = parent->children->items[i]; | ||
87 | if (child == current) { | ||
88 | break; | ||
89 | } | ||
90 | } | ||
91 | int desired = i + diff; | ||
92 | sway_log(L_DEBUG, "Moving from %d to %d", i, desired); | ||
93 | if (desired < 0 || desired >= parent->children->length) { | ||
94 | can_move = false; | ||
95 | } else { | ||
96 | swayc_t *newview = parent->children->items[desired]; | ||
97 | set_focused_container(get_focused_view(newview)); | ||
98 | return true; | ||
99 | } | ||
100 | } | ||
101 | if (!can_move) { | ||
102 | sway_log(L_DEBUG, "Can't move at current level, moving up tree"); | ||
103 | current = parent; | ||
104 | parent = parent->parent; | ||
105 | if (!parent) { | ||
106 | // Nothing we can do | ||
107 | return false; | ||
108 | } | ||
109 | } | ||
110 | } | ||
111 | } | ||
112 | |||
113 | swayc_t *get_focused_container(swayc_t *parent) { | ||
114 | while (parent && !parent->is_focused) { | ||
115 | parent = parent->focused; | ||
116 | } | ||
117 | return parent; | ||
118 | } | ||
119 | |||
120 | void set_focused_container(swayc_t *c) { | ||
121 | if (locked_container_focus || !c) { | ||
122 | return; | ||
123 | } | ||
124 | sway_log(L_DEBUG, "Setting focus to %p:%ld", c, c->handle); | ||
125 | if (c->type != C_ROOT && c->type != C_OUTPUT) { | ||
126 | c->is_focused = true; | ||
127 | } | ||
128 | swayc_t *prev_view = get_focused_view(&root_container); | ||
129 | swayc_t *p = c; | ||
130 | while (p != &root_container) { | ||
131 | update_focus(p); | ||
132 | p = p->parent; | ||
133 | p->is_focused = false; | ||
134 | } | ||
135 | if (!locked_view_focus) { | ||
136 | p = get_focused_view(c); | ||
137 | //Set focus to p | ||
138 | if (p && p != prev_view && !(wlc_view_get_type(p->handle) & WLC_BIT_POPUP)) { | ||
139 | if (prev_view) { | ||
140 | wlc_view_set_state(prev_view->handle, WLC_BIT_ACTIVATED, false); | ||
141 | } | ||
142 | wlc_view_focus(p->handle); | ||
143 | wlc_view_set_state(p->handle, WLC_BIT_ACTIVATED, true); | ||
144 | } | ||
145 | } | ||
146 | } | ||
147 | |||
148 | void set_focused_container_for(swayc_t *a, swayc_t *c) { | ||
149 | if (locked_container_focus || !c) { | ||
150 | return; | ||
151 | } | ||
152 | swayc_t *find = c; | ||
153 | //Ensure that a is an ancestor of c | ||
154 | while (find != a && (find = find->parent)) { | ||
155 | if (find == &root_container) { | ||
156 | return; | ||
157 | } | ||
158 | } | ||
159 | |||
160 | sway_log(L_DEBUG, "Setting focus for %p:%ld to %p:%ld", | ||
161 | a, a->handle, c, c->handle); | ||
162 | |||
163 | c->is_focused = true; | ||
164 | swayc_t *p = c; | ||
165 | while (p != a) { | ||
166 | update_focus(p); | ||
167 | p = p->parent; | ||
168 | p->is_focused = false; | ||
169 | } | ||
170 | if (!locked_view_focus) { | ||
171 | p = get_focused_view(c); | ||
172 | //Set focus to p | ||
173 | if (p) { | ||
174 | wlc_view_focus(p->handle); | ||
175 | wlc_view_set_state(p->handle, WLC_BIT_ACTIVATED, true); | ||
176 | } | ||
177 | } | ||
178 | } | ||
179 | |||
180 | swayc_t *get_focused_view(swayc_t *parent) { | ||
181 | while (parent && parent->type != C_VIEW) { | ||
182 | parent = parent->focused; | ||
183 | } | ||
184 | return parent; | ||
185 | } | ||
186 | |||