summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar taiyu <taiyu.len@gmail.com>2015-08-18 00:28:44 -0700
committerLibravatar taiyu <taiyu.len@gmail.com>2015-08-18 00:28:44 -0700
commit11321ca2dd355be82175213795d88dcbfd0540ee (patch)
treeb0f9bb55eb8c2448139dc63447edf01ffab7fb58
parentAdd support for workspace [name] output [name] (diff)
downloadsway-11321ca2dd355be82175213795d88dcbfd0540ee.tar.gz
sway-11321ca2dd355be82175213795d88dcbfd0540ee.tar.zst
sway-11321ca2dd355be82175213795d88dcbfd0540ee.zip
changed handling of focus, handling of view by type
-rw-r--r--include/container.h2
-rw-r--r--include/focus.h36
-rw-r--r--include/handlers.h5
-rw-r--r--include/movement.h17
-rw-r--r--sway/commands.c9
-rw-r--r--sway/container.c36
-rw-r--r--sway/focus.c186
-rw-r--r--sway/handlers.c235
-rw-r--r--sway/layout.c66
-rw-r--r--sway/log.c2
-rw-r--r--sway/movement.c70
-rw-r--r--sway/workspace.c50
12 files changed, 391 insertions, 323 deletions
diff --git a/include/container.h b/include/container.h
index e395a55b..5f386368 100644
--- a/include/container.h
+++ b/include/container.h
@@ -45,6 +45,8 @@ struct sway_container {
45 45
46 bool is_floating; 46 bool is_floating;
47 47
48 bool is_focused;
49
48 int weight; 50 int weight;
49 51
50 char *name; 52 char *name;
diff --git a/include/focus.h b/include/focus.h
new file mode 100644
index 00000000..185910f3
--- /dev/null
+++ b/include/focus.h
@@ -0,0 +1,36 @@
1#ifndef _SWAY_FOCUS_H
2#define _SWAY_FOCUS_H
3#include "container.h"
4
5enum movement_direction {
6 MOVE_LEFT,
7 MOVE_RIGHT,
8 MOVE_UP,
9 MOVE_DOWN,
10 MOVE_PARENT
11};
12
13//focused_container - the container found by following the `focused` pointer
14//from a given container to a container with `is_focused` boolean set
15//---
16//focused_view - the container found by following the `focused` pointer from a
17//given container to a view.
18//---
19
20swayc_t *get_focused_container(swayc_t *parent);
21swayc_t *get_focused_view(swayc_t *parent);
22
23void set_focused_container(swayc_t *container);
24void set_focused_container_for(swayc_t *ancestor, swayc_t *container);
25
26//lock focused container/view. locked by windows with OVERRIDE attribute
27//and unlocked when they are destroyed
28
29extern bool locked_container_focus;
30extern bool locked_view_focus;
31
32
33bool move_focus(enum movement_direction direction);
34
35#endif
36
diff --git a/include/handlers.h b/include/handlers.h
index d1742cce..6ea4bbf1 100644
--- a/include/handlers.h
+++ b/include/handlers.h
@@ -1,12 +1,11 @@
1#ifndef _SWAY_HANDLERS_H 1#ifndef _SWAY_HANDLERS_H
2#define _SWAY_HANDLERS_H 2#define _SWAY_HANDLERS_H
3 3#include "container.h"
4#include <stdbool.h> 4#include <stdbool.h>
5#include <wlc/wlc.h> 5#include <wlc/wlc.h>
6 6
7extern struct wlc_interface interface; 7extern struct wlc_interface interface;
8 8
9//set focus to current pointer location and return focused container 9swayc_t *container_under_pointer(void);
10swayc_t *focus_pointer(void);
11 10
12#endif 11#endif
diff --git a/include/movement.h b/include/movement.h
deleted file mode 100644
index dd701877..00000000
--- a/include/movement.h
+++ /dev/null
@@ -1,17 +0,0 @@
1#ifndef _SWAY_MOVEMENT_H
2#define _SWAY_MOVEMENT_H
3
4#include <wlc/wlc.h>
5#include "list.h"
6
7enum movement_direction {
8 MOVE_LEFT,
9 MOVE_RIGHT,
10 MOVE_UP,
11 MOVE_DOWN,
12 MOVE_PARENT
13};
14
15bool move_focus(enum movement_direction direction);
16
17#endif
diff --git a/sway/commands.c b/sway/commands.c
index 444e6159..134593c7 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -8,7 +8,7 @@
8#include <ctype.h> 8#include <ctype.h>
9#include "stringop.h" 9#include "stringop.h"
10#include "layout.h" 10#include "layout.h"
11#include "movement.h" 11#include "focus.h"
12#include "log.h" 12#include "log.h"
13#include "workspace.h" 13#include "workspace.h"
14#include "commands.h" 14#include "commands.h"
@@ -215,11 +215,12 @@ static bool cmd_floating(struct sway_config *config, int argc, char **argv) {
215 view->is_floating = false; 215 view->is_floating = false;
216 active_workspace->focused = NULL; 216 active_workspace->focused = NULL;
217 // Get the properly focused container, and add in the view there 217 // Get the properly focused container, and add in the view there
218 swayc_t *focused = focus_pointer(); 218 swayc_t *focused = container_under_pointer();
219 // If focused is null, it's because the currently focused container is a workspace 219 // If focused is null, it's because the currently focused container is a workspace
220 if (focused == NULL) { 220 if (focused == NULL) {
221 focused = active_workspace; 221 focused = active_workspace;
222 } 222 }
223 set_focused_container(focused);
223 224
224 sway_log(L_DEBUG, "Non-floating focused container is %p", focused); 225 sway_log(L_DEBUG, "Non-floating focused container is %p", focused);
225 226
@@ -232,7 +233,7 @@ static bool cmd_floating(struct sway_config *config, int argc, char **argv) {
232 add_sibling(focused, view); 233 add_sibling(focused, view);
233 } 234 }
234 // Refocus on the view once its been put back into the layout 235 // Refocus on the view once its been put back into the layout
235 focus_view(view); 236 set_focused_container(view);
236 arrange_windows(active_workspace, -1, -1); 237 arrange_windows(active_workspace, -1, -1);
237 return true; 238 return true;
238 } 239 }
@@ -345,7 +346,7 @@ static bool _do_split(struct sway_config *config, int argc, char **argv, int lay
345 else { 346 else {
346 sway_log(L_DEBUG, "Adding new container around current focused container"); 347 sway_log(L_DEBUG, "Adding new container around current focused container");
347 swayc_t *parent = new_container(focused, layout); 348 swayc_t *parent = new_container(focused, layout);
348 focus_view(focused); 349 set_focused_container(focused);
349 arrange_windows(parent, -1, -1); 350 arrange_windows(parent, -1, -1);
350 } 351 }
351 352
diff --git a/sway/container.c b/sway/container.c
index 3534721d..67132a48 100644
--- a/sway/container.c
+++ b/sway/container.c
@@ -39,17 +39,11 @@ static void free_swayc(swayc_t *c) {
39} 39}
40 40
41/* New containers */ 41/* New containers */
42static void add_output_widths(swayc_t *container, void *_width) {
43 int *width = _width;
44 if (container->type == C_OUTPUT) {
45 *width += container->width;
46 }
47}
48 42
49swayc_t *new_output(wlc_handle handle) { 43swayc_t *new_output(wlc_handle handle) {
50 const struct wlc_size* size = wlc_output_get_resolution(handle); 44 const struct wlc_size* size = wlc_output_get_resolution(handle);
51 const char *name = wlc_output_get_name(handle); 45 const char *name = wlc_output_get_name(handle);
52 sway_log(L_DEBUG, "Added output %u %s", (unsigned int)handle, name); 46 sway_log(L_DEBUG, "Added output %lu:%s", handle, name);
53 47
54 swayc_t *output = new_swayc(C_OUTPUT); 48 swayc_t *output = new_swayc(C_OUTPUT);
55 output->width = size->w; 49 output->width = size->w;
@@ -59,9 +53,12 @@ swayc_t *new_output(wlc_handle handle) {
59 53
60 add_child(&root_container, output); 54 add_child(&root_container, output);
61 55
62 //TODO something with this 56//TODO still dont know why this is here?
63 int total_width = 0; 57// int total_width = 0;
64 container_map(&root_container, add_output_widths, &total_width); 58// int i;
59// for (i = 0; i < root_container.children->length; ++i) {
60// total_width += ((swayc_t*)root_container.children->items[i])->width;
61// }
65 62
66 //Create workspace 63 //Create workspace
67 char *ws_name = NULL; 64 char *ws_name = NULL;
@@ -79,7 +76,10 @@ swayc_t *new_output(wlc_handle handle) {
79 if (!ws_name) { 76 if (!ws_name) {
80 ws_name = workspace_next_name(); 77 ws_name = workspace_next_name();
81 } 78 }
82 new_workspace(output, ws_name); 79 //create and initilize default workspace
80 swayc_t *ws = new_workspace(output, ws_name);
81 ws->is_focused = true;
82
83 free(ws_name); 83 free(ws_name);
84 84
85 return output; 85 return output;
@@ -139,14 +139,15 @@ swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) {
139} 139}
140 140
141swayc_t *new_view(swayc_t *sibling, wlc_handle handle) { 141swayc_t *new_view(swayc_t *sibling, wlc_handle handle) {
142 const char *title = wlc_view_get_title(handle); 142 const char *title = wlc_view_get_title(handle);
143 swayc_t *view = new_swayc(C_VIEW); 143 swayc_t *view = new_swayc(C_VIEW);
144 sway_log(L_DEBUG, "Adding new view %u:%s to container %p %d", 144 sway_log(L_DEBUG, "Adding new view %lu:%s to container %p %d",
145 (unsigned int)handle, title, sibling, sibling?sibling->type:0); 145 handle, title, sibling, sibling ? sibling->type : 0);
146 //Setup values 146 //Setup values
147 view->handle = handle; 147 view->handle = handle;
148 view->name = title ? strdup(title) : NULL; 148 view->name = title ? strdup(title) : NULL;
149 view->visible = true; 149 view->visible = true;
150 view->is_focused = true;
150 151
151 view->desired_width = -1; 152 view->desired_width = -1;
152 view->desired_height = -1; 153 view->desired_height = -1;
@@ -168,8 +169,8 @@ swayc_t *new_view(swayc_t *sibling, wlc_handle handle) {
168swayc_t *new_floating_view(wlc_handle handle) { 169swayc_t *new_floating_view(wlc_handle handle) {
169 const char *title = wlc_view_get_title(handle); 170 const char *title = wlc_view_get_title(handle);
170 swayc_t *view = new_swayc(C_VIEW); 171 swayc_t *view = new_swayc(C_VIEW);
171 sway_log(L_DEBUG, "Adding new view %u:%s as a floating view", 172 sway_log(L_DEBUG, "Adding new view %lu:%x:%s as a floating view",
172 (unsigned int)handle, title); 173 handle, wlc_view_get_type(handle), title);
173 //Setup values 174 //Setup values
174 view->handle = handle; 175 view->handle = handle;
175 view->name = title ? strdup(title) : NULL; 176 view->name = title ? strdup(title) : NULL;
@@ -197,6 +198,7 @@ swayc_t *new_floating_view(wlc_handle handle) {
197 return view; 198 return view;
198} 199}
199 200
201/* Destroy container */
200 202
201swayc_t *destroy_output(swayc_t *output) { 203swayc_t *destroy_output(swayc_t *output) {
202 if (output->children->length == 0) { 204 if (output->children->length == 0) {
@@ -300,3 +302,5 @@ void set_view_visibility(swayc_t *view, void *data) {
300 } 302 }
301 view->visible = (*p == 2); 303 view->visible = (*p == 2);
302} 304}
305
306
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
7bool locked_container_focus = false;
8bool 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
12static 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
46bool 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
113swayc_t *get_focused_container(swayc_t *parent) {
114 while (parent && !parent->is_focused) {
115 parent = parent->focused;
116 }
117 return parent;
118}
119
120void 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
148void 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
180swayc_t *get_focused_view(swayc_t *parent) {
181 while (parent && parent->type != C_VIEW) {
182 parent = parent->focused;
183 }
184 return parent;
185}
186
diff --git a/sway/handlers.c b/sway/handlers.c
index e17aefee..99f597af 100644
--- a/sway/handlers.c
+++ b/sway/handlers.c
@@ -3,18 +3,18 @@
3#include <stdbool.h> 3#include <stdbool.h>
4#include <wlc/wlc.h> 4#include <wlc/wlc.h>
5#include <ctype.h> 5#include <ctype.h>
6#include "layout.h" 6
7#include "handlers.h"
7#include "log.h" 8#include "log.h"
9#include "layout.h"
8#include "config.h" 10#include "config.h"
9#include "commands.h" 11#include "commands.h"
10#include "handlers.h"
11#include "stringop.h" 12#include "stringop.h"
12#include "workspace.h" 13#include "workspace.h"
13#include "container.h" 14#include "container.h"
15#include "focus.h"
14 16
15static struct wlc_origin mouse_origin; 17static struct wlc_origin mouse_origin;
16//Keyboard input is being overrided by window (dmenu)
17static bool override_redirect = false;
18 18
19static bool pointer_test(swayc_t *view, void *_origin) { 19static bool pointer_test(swayc_t *view, void *_origin) {
20 const struct wlc_origin *origin = _origin; 20 const struct wlc_origin *origin = _origin;
@@ -23,27 +23,57 @@ static bool pointer_test(swayc_t *view, void *_origin) {
23 while (parent->type != C_OUTPUT) { 23 while (parent->type != C_OUTPUT) {
24 parent = parent->parent; 24 parent = parent->parent;
25 } 25 }
26 if (view->type == C_VIEW && origin->x >= view->x && origin->y >= view->y 26 if (origin->x >= view->x && origin->y >= view->y
27 && origin->x < view->x + view->width && origin->y < view->y + view->height 27 && origin->x < view->x + view->width && origin->y < view->y + view->height
28 && view->visible && parent == root_container.focused) { 28 && view->visible && parent == root_container.focused) {
29 return true; 29 return true;
30 } 30 }
31 return false; 31 return false;
32} 32}
33 33
34swayc_t *focus_pointer(void) { 34swayc_t *container_under_pointer(void) {
35 swayc_t *focused = get_focused_container(&root_container); 35 //root.output->workspace
36 if (!(wlc_view_get_state(focused->handle) & WLC_BIT_FULLSCREEN)) { 36 swayc_t *lookup = root_container.focused->focused;
37 swayc_t *pointer = find_container(&root_container, pointer_test, &mouse_origin); 37 //Case of empty workspace
38 if (pointer && focused != pointer) { 38 if (lookup->children == 0) {
39 unfocus_all(&root_container); 39 return NULL;
40 focus_view(pointer); 40 }
41 } else if (!focused) { 41 while (lookup->type != C_VIEW) {
42 focus_view(active_workspace); 42 int i;
43 int len;
44 //if tabbed/stacked go directly to focused container, otherwise search
45 //children
46 if (lookup->layout == L_TABBED || lookup->layout == L_STACKED) {
47 lookup = lookup->focused;
48 continue;
49 }
50 //if workspace, search floating
51 if (lookup->type == C_WORKSPACE) {
52 len = lookup->floating->length;
53 for (i = 0; i < len; ++i) {
54 if (pointer_test(lookup->floating->items[i], &mouse_origin)) {
55 lookup = lookup->floating->items[i];
56 break;
57 }
58 }
59 if (i < len) {
60 continue;
61 }
62 }
63 //search children
64 len = lookup->children->length;
65 for (i = 0; i < len; ++i) {
66 if (pointer_test(lookup->children->items[i], &mouse_origin)) {
67 lookup = lookup->children->items[i];
68 break;
69 }
70 }
71 //when border and titles are done, this could happen
72 if (i == len) {
73 break;
43 } 74 }
44 focused = pointer;
45 } 75 }
46 return focused; 76 return lookup;
47} 77}
48 78
49static bool handle_output_created(wlc_handle output) { 79static bool handle_output_created(wlc_handle output) {
@@ -81,90 +111,79 @@ static void handle_output_resolution_change(wlc_handle output, const struct wlc_
81 111
82static void handle_output_focused(wlc_handle output, bool focus) { 112static void handle_output_focused(wlc_handle output, bool focus) {
83 swayc_t *c = get_swayc_for_handle(output, &root_container); 113 swayc_t *c = get_swayc_for_handle(output, &root_container);
84 if (!c) return; 114 //if for some reason this output doesnt exist, create it.
115 if (!c) {
116 handle_output_created(output);
117 }
85 if (focus) { 118 if (focus) {
86 unfocus_all(&root_container); 119 set_focused_container(c);
87 focus_view(c);
88 } 120 }
89} 121}
90 122
91static bool handle_view_created(wlc_handle handle) { 123static bool handle_view_created(wlc_handle handle) {
92 swayc_t *focused = get_focused_container(&root_container); 124 swayc_t *focused = get_focused_container(&root_container);
93 uint32_t type = wlc_view_get_type(handle); 125 swayc_t *newview = NULL;
94 // If override_redirect/unmanaged/popup/modal/splach 126 switch (wlc_view_get_type(handle)) {
95 if (type) { 127 //regular view created regularly
96 sway_log(L_DEBUG,"Unmanaged window of type %x left alone", type); 128 case 0:
97 wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true); 129 newview = new_view(focused, handle);
98 if (type & WLC_BIT_UNMANAGED) {
99 return true;
100 }
101 // For things like Dmenu
102 if (type & WLC_BIT_OVERRIDE_REDIRECT) {
103 override_redirect = true;
104 wlc_view_focus(handle);
105 }
106
107 // Float popups
108 if (type & WLC_BIT_POPUP) {
109 swayc_t *view = new_floating_view(handle);
110 wlc_view_set_state(handle, WLC_BIT_MAXIMIZED, false);
111 focus_view(view);
112 arrange_windows(active_workspace, -1, -1);
113 }
114 } else {
115 swayc_t *view = new_view(focused, handle);
116 //Set maximize flag for windows.
117 //TODO: floating windows have this unset
118 wlc_view_set_state(handle, WLC_BIT_MAXIMIZED, true); 130 wlc_view_set_state(handle, WLC_BIT_MAXIMIZED, true);
119 unfocus_all(&root_container); 131 break;
120 focus_view(view); 132 //takes keyboard focus
121 arrange_windows(view->parent, -1, -1); 133 case WLC_BIT_OVERRIDE_REDIRECT:
134 sway_log(L_DEBUG, "view %ld with OVERRIDE_REDIRECT", handle);
135 locked_view_focus = true;
136 wlc_view_focus(handle);
137 wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true);
138 wlc_view_bring_to_front(handle);
139 break;
140 //Takes container focus
141 case WLC_BIT_OVERRIDE_REDIRECT|WLC_BIT_UNMANAGED:
142 sway_log(L_DEBUG, "view %ld with OVERRIDE_REDIRECT|WLC_BIT_MANAGED", handle);
143 wlc_view_bring_to_front(handle);
144 locked_container_focus = true;
145 break;
146 //set modals as floating containers
147 case WLC_BIT_POPUP:
148 case WLC_BIT_MODAL:
149 wlc_view_bring_to_front(handle);
150 newview = new_floating_view(handle);
151 break;
122 } 152 }
123 if (wlc_view_get_state(focused->handle) & WLC_BIT_FULLSCREEN) { 153 if (newview) {
124 unfocus_all(&root_container); 154 set_focused_container(newview);
125 focus_view(focused); 155 arrange_windows(newview->parent, -1, -1);
126 arrange_windows(focused, -1, -1);
127 } 156 }
128 return true; 157 return true;
129} 158}
130 159
131static void handle_view_destroyed(wlc_handle handle) { 160static void handle_view_destroyed(wlc_handle handle) {
132 sway_log(L_DEBUG, "Destroying window %u", (unsigned int)handle); 161 sway_log(L_DEBUG, "Destroying window %lu", handle);
133
134 // Properly handle unmanaged views
135 uint32_t type = wlc_view_get_type(handle);
136 if (type) {
137 wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true);
138 sway_log(L_DEBUG,"Unmanaged window of type %x was destroyed", type);
139 if (type & WLC_BIT_UNMANAGED) {
140 // We need to call focus_view() on focus_pointer because unmanaged windows
141 // do not alter the focus structure of the container tree. This makes focus_pointer()
142 // think that it doesn't need to do anything, so we manually focus the result.
143 focus_view(focus_pointer());
144 return;
145 }
146
147 if (type & WLC_BIT_OVERRIDE_REDIRECT) {
148 override_redirect = false;
149 focus_view(focus_pointer());
150 return;
151 }
152
153 // WLC_BIT_POPUP doesn't need to be dealt with since it's
154 // treated as a floating view.
155 }
156
157 swayc_t *view = get_swayc_for_handle(handle, &root_container); 162 swayc_t *view = get_swayc_for_handle(handle, &root_container);
158 swayc_t *parent;
159 swayc_t *focused = get_focused_container(&root_container); 163 swayc_t *focused = get_focused_container(&root_container);
160 164
161 if (view) { 165 switch (wlc_view_get_type(handle)) {
162 parent = destroy_view(view); 166 //regular view created regularly
163 arrange_windows(parent, -1, -1); 167 case 0:
164 } 168 case WLC_BIT_MODAL:
165 if (!focused || focused == view) { 169 case WLC_BIT_POPUP:
166 focus_pointer(); 170 if (view) {
171 arrange_windows(destroy_view(view), -1, -1);
172 if (!focused || focused == view) {
173 set_focused_container(container_under_pointer());
174 }
175 }
176 break;
177 //takes keyboard focus
178 case WLC_BIT_OVERRIDE_REDIRECT:
179 locked_view_focus = false;
180 break;
181 //Takes container focus
182 case WLC_BIT_OVERRIDE_REDIRECT|WLC_BIT_UNMANAGED:
183 locked_container_focus = false;
184 break;
167 } 185 }
186 set_focused_container(get_focused_view(&root_container));
168} 187}
169 188
170static void handle_view_focus(wlc_handle view, bool focus) { 189static void handle_view_focus(wlc_handle view, bool focus) {
@@ -172,6 +191,8 @@ static void handle_view_focus(wlc_handle view, bool focus) {
172} 191}
173 192
174static void handle_view_geometry_request(wlc_handle handle, const struct wlc_geometry* geometry) { 193static void handle_view_geometry_request(wlc_handle handle, const struct wlc_geometry* geometry) {
194 sway_log(L_DEBUG, "geometry request %d x %d : %d x %d",
195 geometry->origin.x, geometry->origin.y, geometry->size.w, geometry->size.h);
175 // If the view is floating, then apply the geometry. 196 // If the view is floating, then apply the geometry.
176 // Otherwise save the desired width/height for the view. 197 // Otherwise save the desired width/height for the view.
177 // This will not do anything for the time being as WLC improperly sends geometry requests 198 // This will not do anything for the time being as WLC improperly sends geometry requests
@@ -186,33 +207,32 @@ static void handle_view_geometry_request(wlc_handle handle, const struct wlc_geo
186 view->x = geometry->origin.x; 207 view->x = geometry->origin.x;
187 view->y = geometry->origin.y; 208 view->y = geometry->origin.y;
188 arrange_windows(view->parent, -1, -1); 209 arrange_windows(view->parent, -1, -1);
189 } 210 }
190 } 211 }
191} 212}
192 213
193static void handle_view_state_request(wlc_handle view, enum wlc_view_state_bit state, bool toggle) { 214static void handle_view_state_request(wlc_handle view, enum wlc_view_state_bit state, bool toggle) {
194 switch(state) { 215 swayc_t *c = NULL;
216 switch (state) {
195 case WLC_BIT_FULLSCREEN: 217 case WLC_BIT_FULLSCREEN:
196 { 218 //I3 just lets it become fullscreen
197 //I3 just lets it become fullscreen 219 wlc_view_set_state(view, state, toggle);
198 wlc_view_set_state(view,state,toggle); 220 c = get_swayc_for_handle(view, &root_container);
199 swayc_t *c = get_swayc_for_handle(view, &root_container); 221 sway_log(L_DEBUG, "setting view %ld %s, fullscreen %d", view, c->name, toggle);
200 sway_log(L_DEBUG, "setting view %ld %s, fullscreen %d",view,c->name,toggle); 222 if (c) {
201 if (c) { 223 arrange_windows(c->parent, -1, -1);
202 arrange_windows(c->parent, -1, -1); 224 //Set it as focused window for that workspace if its going
203 //Set it as focused window for that workspace if its going 225 //fullscreen
204 //fullscreen 226 if (toggle) {
205 if (toggle) { 227 swayc_t *ws = c;
206 swayc_t *ws = c; 228 while (ws->type != C_WORKSPACE) {
207 while (ws->type != C_WORKSPACE) { 229 ws = ws->parent;
208 ws = ws->parent;
209 }
210 //Set ws focus to c
211 focus_view_for(ws, c);
212 } 230 }
231 //Set ws focus to c
232 set_focused_container_for(ws, c);
213 } 233 }
214 break;
215 } 234 }
235 break;
216 case WLC_BIT_MAXIMIZED: 236 case WLC_BIT_MAXIMIZED:
217 case WLC_BIT_RESIZING: 237 case WLC_BIT_RESIZING:
218 case WLC_BIT_MOVING: 238 case WLC_BIT_MOVING:
@@ -226,7 +246,7 @@ static void handle_view_state_request(wlc_handle view, enum wlc_view_state_bit s
226static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifiers 246static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifiers
227 *modifiers, uint32_t key, uint32_t sym, enum wlc_key_state state) { 247 *modifiers, uint32_t key, uint32_t sym, enum wlc_key_state state) {
228 enum { QSIZE = 32 }; 248 enum { QSIZE = 32 };
229 if (override_redirect) { 249 if (locked_view_focus) {
230 return false; 250 return false;
231 } 251 }
232 static uint8_t head = 0; 252 static uint8_t head = 0;
@@ -295,7 +315,7 @@ static bool handle_pointer_motion(wlc_handle view, uint32_t time, const struct w
295 static wlc_handle prev_view = 0; 315 static wlc_handle prev_view = 0;
296 mouse_origin = *origin; 316 mouse_origin = *origin;
297 if (config->focus_follows_mouse && prev_view != view) { 317 if (config->focus_follows_mouse && prev_view != view) {
298 focus_pointer(); 318 set_focused_container(container_under_pointer());
299 } 319 }
300 prev_view = view; 320 prev_view = view;
301 return false; 321 return false;
@@ -305,7 +325,8 @@ static bool handle_pointer_button(wlc_handle view, uint32_t time, const struct w
305 uint32_t button, enum wlc_button_state state) { 325 uint32_t button, enum wlc_button_state state) {
306 swayc_t *focused = get_focused_container(&root_container); 326 swayc_t *focused = get_focused_container(&root_container);
307 if (state == WLC_BUTTON_STATE_PRESSED) { 327 if (state == WLC_BUTTON_STATE_PRESSED) {
308 swayc_t *pointer = focus_pointer(); 328 swayc_t *pointer = container_under_pointer();
329 set_focused_container(pointer);
309 return (pointer && pointer != focused); 330 return (pointer && pointer != focused);
310 } 331 }
311 return false; 332 return false;
diff --git a/sway/layout.c b/sway/layout.c
index f4079d4b..de7ef370 100644
--- a/sway/layout.c
+++ b/sway/layout.c
@@ -31,9 +31,6 @@ void add_child(swayc_t *parent, swayc_t *child) {
31 child->width, child->height, parent, parent->type, parent->width, parent->height); 31 child->width, child->height, parent, parent->type, parent->width, parent->height);
32 list_add(parent->children, child); 32 list_add(parent->children, child);
33 child->parent = parent; 33 child->parent = parent;
34 if (parent->focused == NULL) {
35 parent->focused = child;
36 }
37} 34}
38 35
39swayc_t *add_sibling(swayc_t *sibling, swayc_t *child) { 36swayc_t *add_sibling(swayc_t *sibling, swayc_t *child) {
@@ -206,7 +203,7 @@ void arrange_windows(swayc_t *container, int width, int height) {
206 // Arrage floating layouts for workspaces last 203 // Arrage floating layouts for workspaces last
207 if (container->type == C_WORKSPACE) { 204 if (container->type == C_WORKSPACE) {
208 for (i = 0; i < container->floating->length; ++i) { 205 for (i = 0; i < container->floating->length; ++i) {
209 swayc_t *view = ((swayc_t *)container->floating->items[i]); 206 swayc_t *view = container->floating->items[i];
210 // Set the geometry 207 // Set the geometry
211 struct wlc_geometry geometry = { 208 struct wlc_geometry geometry = {
212 .origin = { 209 .origin = {
@@ -262,64 +259,3 @@ swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent) {
262 return NULL; 259 return NULL;
263} 260}
264 261
265swayc_t *get_focused_container(swayc_t *parent) {
266 if (parent->focused == NULL) {
267 return parent;
268 }
269 return get_focused_container(parent->focused);
270}
271
272void unfocus_all(swayc_t *container) {
273 if (container->children == NULL) {
274 return;
275 }
276 int i;
277 for (i = 0; i < container->children->length; ++i) {
278 swayc_t *view = container->children->items[i];
279 if (view->type == C_VIEW) {
280 wlc_view_set_state(view->handle, WLC_BIT_ACTIVATED, false);
281 } else {
282 unfocus_all(view);
283 }
284 }
285}
286
287void focus_view(swayc_t *view) {
288 if (!view) {
289 return;
290 }
291 sway_log(L_DEBUG, "Setting focus for %p:%ld", view, view->handle);
292 swayc_t *c = view;
293 //Set focus from root to view
294 while (c != &root_container) {
295 c->parent->focused = c;
296 c = c->parent;
297 }
298 //Set output
299 wlc_output_focus(c->focused->handle);
300 //get focus for views focused window
301 while (view && view->type != C_VIEW) {
302 view = view->focused;
303 }
304 if (view) {
305 wlc_view_set_state(view->handle, WLC_BIT_ACTIVATED, true);
306 wlc_view_focus(view->handle);
307 }
308}
309
310void focus_view_for(swayc_t *top, swayc_t *view) {
311 swayc_t *find = view;
312 //Make sure top is a ancestor of view
313 while (find != top) {
314 if (find == &root_container) {
315 return;
316 }
317 find = find->parent;
318 }
319 //Set focus for top to go to view
320 while (view != top) {
321 view->parent->focused = view;
322 view = view->parent;
323 }
324}
325
diff --git a/sway/log.c b/sway/log.c
index b9048b34..03639ae4 100644
--- a/sway/log.c
+++ b/sway/log.c
@@ -8,7 +8,7 @@
8int colored = 1; 8int colored = 1;
9int v = 0; 9int v = 0;
10 10
11const char *verbosity_colors[] = { 11static const char *verbosity_colors[] = {
12 "", // L_SILENT 12 "", // L_SILENT
13 "\x1B[1;31m", // L_ERROR 13 "\x1B[1;31m", // L_ERROR
14 "\x1B[1;34m", // L_INFO 14 "\x1B[1;34m", // L_INFO
diff --git a/sway/movement.c b/sway/movement.c
deleted file mode 100644
index 12726392..00000000
--- a/sway/movement.c
+++ /dev/null
@@ -1,70 +0,0 @@
1#include <stdlib.h>
2#include <stdbool.h>
3#include "list.h"
4#include "log.h"
5#include "layout.h"
6#include "movement.h"
7
8bool move_focus(enum movement_direction direction) {
9 swayc_t *current = get_focused_container(&root_container);
10 swayc_t *parent = current->parent;
11
12 if (direction == MOVE_PARENT) {
13 if (parent->type == C_OUTPUT) {
14 sway_log(L_DEBUG, "Focus cannot move to parent");
15 return false;
16 } else {
17 sway_log(L_DEBUG, "Moving focus away from %p to %p", current, parent);
18 unfocus_all(parent->parent);
19 focus_view(parent);
20 return true;
21 }
22 }
23
24 while (true) {
25 sway_log(L_DEBUG, "Moving focus away from %p", current);
26
27 // Test if we can even make a difference here
28 bool can_move = false;
29 int diff = 0;
30 if (direction == MOVE_LEFT || direction == MOVE_RIGHT) {
31 if (parent->layout == L_HORIZ || parent->type == C_ROOT) {
32 can_move = true;
33 diff = direction == MOVE_LEFT ? -1 : 1;
34 }
35 } else {
36 if (parent->layout == L_VERT) {
37 can_move = true;
38 diff = direction == MOVE_UP ? -1 : 1;
39 }
40 }
41 sway_log(L_DEBUG, "Can move? %s", can_move ? "yes" : "no");
42 if (can_move) {
43 int i;
44 for (i = 0; i < parent->children->length; ++i) {
45 swayc_t *child = parent->children->items[i];
46 if (child == current) {
47 break;
48 }
49 }
50 int desired = i + diff;
51 sway_log(L_DEBUG, "Moving from %d to %d", i, desired);
52 if (desired < 0 || desired >= parent->children->length) {
53 can_move = false;
54 } else {
55 unfocus_all(&root_container);
56 focus_view(parent->children->items[desired]);
57 return true;
58 }
59 }
60 if (!can_move) {
61 sway_log(L_DEBUG, "Can't move at current level, moving up tree");
62 current = parent;
63 parent = parent->parent;
64 if (!parent) {
65 // Nothing we can do
66 return false;
67 }
68 }
69 }
70}
diff --git a/sway/workspace.c b/sway/workspace.c
index df646445..bc0fa2c8 100644
--- a/sway/workspace.c
+++ b/sway/workspace.c
@@ -9,6 +9,7 @@
9#include "handlers.h" 9#include "handlers.h"
10#include "config.h" 10#include "config.h"
11#include "stringop.h" 11#include "stringop.h"
12#include "focus.h"
12 13
13swayc_t *active_workspace = NULL; 14swayc_t *active_workspace = NULL;
14 15
@@ -173,64 +174,33 @@ void workspace_prev() {
173} 174}
174 175
175void workspace_switch(swayc_t *workspace) { 176void workspace_switch(swayc_t *workspace) {
176 swayc_t *ws_output = workspace->parent; 177 set_focused_container(workspace);
177 while (ws_output->type != C_OUTPUT) {
178 ws_output = ws_output->parent;
179 }
180 // The current workspace of the output our target workspace is in
181 swayc_t *focused_workspace = ws_output->focused;
182 if (workspace != focused_workspace && focused_workspace) {
183 sway_log(L_DEBUG, "workspace: changing from '%s' to '%s'", focused_workspace->name, workspace->name);
184 uint32_t mask = 1;
185
186 // set all c_views in the old workspace to the invisible mask if the workspace
187 // is in the same output & c_views in the new workspace to the visible mask
188 container_map(focused_workspace, set_view_visibility, &mask);
189 mask = 2;
190 container_map(workspace, set_view_visibility, &mask);
191 wlc_output_set_mask(ws_output->handle, 2);
192
193 destroy_workspace(focused_workspace);
194 }
195 unfocus_all(&root_container);
196 focus_view(workspace);
197
198 // focus the output this workspace is on
199 swayc_t *output = workspace->parent;
200 sway_log(L_DEBUG, "Switching focus to output %p (%d)", output, output->type);
201 while (output && output->type != C_OUTPUT) {
202 output = output->parent;
203 }
204 if (output) {
205 sway_log(L_DEBUG, "Switching focus to output %p (%d)", output, output->type);
206 wlc_output_focus(output->handle);
207 }
208 active_workspace = workspace; 178 active_workspace = workspace;
209} 179}
210 180
211/* XXX:DEBUG:XXX */ 181/* XXX:DEBUG:XXX */
212static void container_log(const swayc_t *c) { 182static void container_log(const swayc_t *c) {
213 fprintf(stderr, "focus:%c|", 183 fprintf(stderr, "focus:%c|",
214 c == get_focused_container(&root_container) ? 'F' : //Focused 184 c->is_focused ? 'F' : //Focused
215 c == active_workspace ? 'W' : //active workspace 185 c == active_workspace ? 'W' : //active workspace
216 c == &root_container ? 'R' : //root 186 c == &root_container ? 'R' : //root
217 'X');//not any others 187 'X');//not any others
218 fprintf(stderr,"(%p)",c); 188 fprintf(stderr,"(%p)",c);
219 fprintf(stderr,"(p:%p)",c->parent); 189 fprintf(stderr,"(p:%p)",c->parent);
220 fprintf(stderr,"(f:%p)",c->focused); 190 fprintf(stderr,"(f:%p)",c->focused);
191 fprintf(stderr,"(h:%ld)",c->handle);
221 fprintf(stderr,"Type:"); 192 fprintf(stderr,"Type:");
222 fprintf(stderr, 193 fprintf(stderr,
223 c->type == C_ROOT ? "Root|" : 194 c->type == C_ROOT ? "Root|" :
224 c->type == C_OUTPUT ? "Output|" : 195 c->type == C_OUTPUT ? "Output|" :
225 c->type == C_WORKSPACE ? "Workspace|" : 196 c->type == C_WORKSPACE ? "Workspace|" :
226 c->type == C_CONTAINER ? "Container|" : 197 c->type == C_CONTAINER ? "Container|" :
227 c->type == C_VIEW ? "View|" : 198 c->type == C_VIEW ? "View|" : "Unknown|");
228 "Unknown|");
229 fprintf(stderr,"layout:"); 199 fprintf(stderr,"layout:");
230 fprintf(stderr, 200 fprintf(stderr,
231 c->layout == L_NONE ? "NONE|" : 201 c->layout == L_NONE ? "NONE|" :
232 c->layout == L_HORIZ ? "Horiz|": 202 c->layout == L_HORIZ ? "Horiz|":
233 c->layout == L_VERT ? "Vert|": 203 c->layout == L_VERT ? "Vert|":
234 c->layout == L_STACKED ? "Stacked|": 204 c->layout == L_STACKED ? "Stacked|":
235 c->layout == L_FLOATING ? "Floating|": 205 c->layout == L_FLOATING ? "Floating|":
236 "Unknown|"); 206 "Unknown|");