aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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.c232
-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, 320 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 c954ce61..6b642419 100644
--- a/include/handlers.h
+++ b/include/handlers.h
@@ -1,12 +1,13 @@
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;
8extern uint32_t keys_pressed[32]; 8extern uint32_t keys_pressed[32];
9
9//set focus to current pointer location and return focused container 10//set focus to current pointer location and return focused container
10swayc_t *focus_pointer(void); 11swayc_t *container_under_pointer(void);
11 12
12#endif 13#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 f47edd2f..548cb8ed 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"
@@ -219,11 +219,12 @@ static bool cmd_floating(struct sway_config *config, int argc, char **argv) {
219 view->is_floating = false; 219 view->is_floating = false;
220 active_workspace->focused = NULL; 220 active_workspace->focused = NULL;
221 // Get the properly focused container, and add in the view there 221 // Get the properly focused container, and add in the view there
222 swayc_t *focused = focus_pointer(); 222 swayc_t *focused = container_under_pointer();
223 // If focused is null, it's because the currently focused container is a workspace 223 // If focused is null, it's because the currently focused container is a workspace
224 if (focused == NULL) { 224 if (focused == NULL) {
225 focused = active_workspace; 225 focused = active_workspace;
226 } 226 }
227 set_focused_container(focused);
227 228
228 sway_log(L_DEBUG, "Non-floating focused container is %p", focused); 229 sway_log(L_DEBUG, "Non-floating focused container is %p", focused);
229 230
@@ -236,7 +237,7 @@ static bool cmd_floating(struct sway_config *config, int argc, char **argv) {
236 add_sibling(focused, view); 237 add_sibling(focused, view);
237 } 238 }
238 // Refocus on the view once its been put back into the layout 239 // Refocus on the view once its been put back into the layout
239 focus_view(view); 240 set_focused_container(view);
240 arrange_windows(active_workspace, -1, -1); 241 arrange_windows(active_workspace, -1, -1);
241 return true; 242 return true;
242 } 243 }
@@ -357,7 +358,7 @@ static bool _do_split(struct sway_config *config, int argc, char **argv, int lay
357 else { 358 else {
358 sway_log(L_DEBUG, "Adding new container around current focused container"); 359 sway_log(L_DEBUG, "Adding new container around current focused container");
359 swayc_t *parent = new_container(focused, layout); 360 swayc_t *parent = new_container(focused, layout);
360 focus_view(focused); 361 set_focused_container(focused);
361 arrange_windows(parent, -1, -1); 362 arrange_windows(parent, -1, -1);
362 } 363 }
363 364
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 c39c1628..2a5113cc 100644
--- a/sway/handlers.c
+++ b/sway/handlers.c
@@ -3,20 +3,20 @@
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
15uint32_t keys_pressed[32]; 17uint32_t keys_pressed[32];
16 18
17static struct wlc_origin mouse_origin; 19static struct wlc_origin mouse_origin;
18//Keyboard input is being overrided by window (dmenu)
19static bool override_redirect = false;
20 20
21static bool m1_held = false; 21static bool m1_held = false;
22static bool m2_held = false; 22static bool m2_held = false;
@@ -28,27 +28,57 @@ static bool pointer_test(swayc_t *view, void *_origin) {
28 while (parent->type != C_OUTPUT) { 28 while (parent->type != C_OUTPUT) {
29 parent = parent->parent; 29 parent = parent->parent;
30 } 30 }
31 if (view->type == C_VIEW && origin->x >= view->x && origin->y >= view->y 31 if (origin->x >= view->x && origin->y >= view->y
32 && origin->x < view->x + view->width && origin->y < view->y + view->height 32 && origin->x < view->x + view->width && origin->y < view->y + view->height
33 && view->visible && parent == root_container.focused) { 33 && view->visible && parent == root_container.focused) {
34 return true; 34 return true;
35 } 35 }
36 return false; 36 return false;
37} 37}
38 38
39swayc_t *focus_pointer(void) { 39swayc_t *container_under_pointer(void) {
40 swayc_t *focused = get_focused_container(&root_container); 40 //root.output->workspace
41 if (!(wlc_view_get_state(focused->handle) & WLC_BIT_FULLSCREEN)) { 41 swayc_t *lookup = root_container.focused->focused;
42 swayc_t *pointer = find_container(&root_container, pointer_test, &mouse_origin); 42 //Case of empty workspace
43 if (pointer && focused != pointer) { 43 if (lookup->children == 0) {
44 unfocus_all(&root_container); 44 return NULL;
45 focus_view(pointer); 45 }
46 } else if (!focused) { 46 while (lookup->type != C_VIEW) {
47 focus_view(active_workspace); 47 int i;
48 int len;
49 //if tabbed/stacked go directly to focused container, otherwise search
50 //children
51 if (lookup->layout == L_TABBED || lookup->layout == L_STACKED) {
52 lookup = lookup->focused;
53 continue;
54 }
55 //if workspace, search floating
56 if (lookup->type == C_WORKSPACE) {
57 len = lookup->floating->length;
58 for (i = 0; i < len; ++i) {
59 if (pointer_test(lookup->floating->items[i], &mouse_origin)) {
60 lookup = lookup->floating->items[i];
61 break;
62 }
63 }
64 if (i < len) {
65 continue;
66 }
67 }
68 //search children
69 len = lookup->children->length;
70 for (i = 0; i < len; ++i) {
71 if (pointer_test(lookup->children->items[i], &mouse_origin)) {
72 lookup = lookup->children->items[i];
73 break;
74 }
75 }
76 //when border and titles are done, this could happen
77 if (i == len) {
78 break;
48 } 79 }
49 focused = pointer;
50 } 80 }
51 return focused; 81 return lookup;
52} 82}
53 83
54static bool handle_output_created(wlc_handle output) { 84static bool handle_output_created(wlc_handle output) {
@@ -86,90 +116,80 @@ static void handle_output_resolution_change(wlc_handle output, const struct wlc_
86 116
87static void handle_output_focused(wlc_handle output, bool focus) { 117static void handle_output_focused(wlc_handle output, bool focus) {
88 swayc_t *c = get_swayc_for_handle(output, &root_container); 118 swayc_t *c = get_swayc_for_handle(output, &root_container);
89 if (!c) return; 119 //if for some reason this output doesnt exist, create it.
120 if (!c) {
121 handle_output_created(output);
122 }
90 if (focus) { 123 if (focus) {
91 unfocus_all(&root_container); 124 set_focused_container(c);
92 focus_view(c);
93 } 125 }
94} 126}
95 127
96static bool handle_view_created(wlc_handle handle) { 128static bool handle_view_created(wlc_handle handle) {
97 swayc_t *focused = get_focused_container(&root_container); 129 swayc_t *focused = get_focused_container(&root_container);
98 uint32_t type = wlc_view_get_type(handle); 130 swayc_t *newview = NULL;
99 // If override_redirect/unmanaged/popup/modal/splach 131 switch (wlc_view_get_type(handle)) {
100 if (type) { 132 //regular view created regularly
101 sway_log(L_DEBUG,"Unmanaged window of type %x left alone", type); 133 case 0:
102 wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true); 134 newview = new_view(focused, handle);
103 if (type & WLC_BIT_UNMANAGED) {
104 return true;
105 }
106 // For things like Dmenu
107 if (type & WLC_BIT_OVERRIDE_REDIRECT) {
108 override_redirect = true;
109 wlc_view_focus(handle);
110 }
111
112 // Float popups
113 if (type & WLC_BIT_POPUP) {
114 swayc_t *view = new_floating_view(handle);
115 wlc_view_set_state(handle, WLC_BIT_MAXIMIZED, false);
116 focus_view(view);
117 arrange_windows(active_workspace, -1, -1);
118 }
119 } else {
120 swayc_t *view = new_view(focused, handle);
121 //Set maximize flag for windows.
122 //TODO: floating windows have this unset
123 wlc_view_set_state(handle, WLC_BIT_MAXIMIZED, true); 135 wlc_view_set_state(handle, WLC_BIT_MAXIMIZED, true);
124 unfocus_all(&root_container); 136 break;
125 focus_view(view); 137 //takes keyboard focus
126 arrange_windows(view->parent, -1, -1); 138 case WLC_BIT_OVERRIDE_REDIRECT:
139 sway_log(L_DEBUG, "view %ld with OVERRIDE_REDIRECT", handle);
140 locked_view_focus = true;
141 wlc_view_focus(handle);
142 wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true);
143 wlc_view_bring_to_front(handle);
144 break;
145 //Takes container focus
146 case WLC_BIT_OVERRIDE_REDIRECT|WLC_BIT_UNMANAGED:
147 sway_log(L_DEBUG, "view %ld with OVERRIDE_REDIRECT|WLC_BIT_MANAGED", handle);
148 wlc_view_bring_to_front(handle);
149 locked_container_focus = true;
150 break;
151 //set modals as floating containers
152 case WLC_BIT_MODAL:
153 wlc_view_bring_to_front(handle);
154 newview = new_floating_view(handle);
155 case WLC_BIT_POPUP:
156 break;
127 } 157 }
128 if (wlc_view_get_state(focused->handle) & WLC_BIT_FULLSCREEN) { 158 if (newview) {
129 unfocus_all(&root_container); 159 set_focused_container(newview);
130 focus_view(focused); 160 arrange_windows(newview->parent, -1, -1);
131 arrange_windows(focused, -1, -1);
132 } 161 }
133 return true; 162 return true;
134} 163}
135 164
136static void handle_view_destroyed(wlc_handle handle) { 165static void handle_view_destroyed(wlc_handle handle) {
137 sway_log(L_DEBUG, "Destroying window %u", (unsigned int)handle); 166 sway_log(L_DEBUG, "Destroying window %lu", handle);
138
139 // Properly handle unmanaged views
140 uint32_t type = wlc_view_get_type(handle);
141 if (type) {
142 wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true);
143 sway_log(L_DEBUG,"Unmanaged window of type %x was destroyed", type);
144 if (type & WLC_BIT_UNMANAGED) {
145 // We need to call focus_view() on focus_pointer because unmanaged windows
146 // do not alter the focus structure of the container tree. This makes focus_pointer()
147 // think that it doesn't need to do anything, so we manually focus the result.
148 focus_view(focus_pointer());
149 return;
150 }
151
152 if (type & WLC_BIT_OVERRIDE_REDIRECT) {
153 override_redirect = false;
154 focus_view(focus_pointer());
155 return;
156 }
157
158 // WLC_BIT_POPUP doesn't need to be dealt with since it's
159 // treated as a floating view.
160 }
161
162 swayc_t *view = get_swayc_for_handle(handle, &root_container); 167 swayc_t *view = get_swayc_for_handle(handle, &root_container);
163 swayc_t *parent;
164 swayc_t *focused = get_focused_container(&root_container); 168 swayc_t *focused = get_focused_container(&root_container);
165 169
166 if (view) { 170 switch (wlc_view_get_type(handle)) {
167 parent = destroy_view(view); 171 //regular view created regularly
168 arrange_windows(parent, -1, -1); 172 case 0:
169 } 173 case WLC_BIT_MODAL:
170 if (!focused || focused == view) { 174 if (view) {
171 focus_pointer(); 175 swayc_t *parent = destroy_view(view);
176 arrange_windows(parent, -1, -1);
177 if (!focused || focused == view) {
178 set_focused_container(container_under_pointer());
179 }
180 }
181 break;
182 //takes keyboard focus
183 case WLC_BIT_OVERRIDE_REDIRECT:
184 locked_view_focus = false;
185 break;
186 //Takes container focus
187 case WLC_BIT_OVERRIDE_REDIRECT|WLC_BIT_UNMANAGED:
188 locked_container_focus = false;
189 case WLC_BIT_POPUP:
190 break;
172 } 191 }
192 set_focused_container(get_focused_view(&root_container));
173} 193}
174 194
175static void handle_view_focus(wlc_handle view, bool focus) { 195static void handle_view_focus(wlc_handle view, bool focus) {
@@ -177,6 +197,8 @@ static void handle_view_focus(wlc_handle view, bool focus) {
177} 197}
178 198
179static void handle_view_geometry_request(wlc_handle handle, const struct wlc_geometry* geometry) { 199static void handle_view_geometry_request(wlc_handle handle, const struct wlc_geometry* geometry) {
200 sway_log(L_DEBUG, "geometry request %d x %d : %d x %d",
201 geometry->origin.x, geometry->origin.y, geometry->size.w,geometry->size.h);
180 // If the view is floating, then apply the geometry. 202 // If the view is floating, then apply the geometry.
181 // Otherwise save the desired width/height for the view. 203 // Otherwise save the desired width/height for the view.
182 // This will not do anything for the time being as WLC improperly sends geometry requests 204 // This will not do anything for the time being as WLC improperly sends geometry requests
@@ -196,28 +218,27 @@ static void handle_view_geometry_request(wlc_handle handle, const struct wlc_geo
196} 218}
197 219
198static void handle_view_state_request(wlc_handle view, enum wlc_view_state_bit state, bool toggle) { 220static void handle_view_state_request(wlc_handle view, enum wlc_view_state_bit state, bool toggle) {
221 swayc_t *c = NULL;
199 switch(state) { 222 switch(state) {
200 case WLC_BIT_FULLSCREEN: 223 case WLC_BIT_FULLSCREEN:
201 { 224 //I3 just lets it become fullscreen
202 //I3 just lets it become fullscreen 225 wlc_view_set_state(view, state, toggle);
203 wlc_view_set_state(view,state,toggle); 226 c = get_swayc_for_handle(view, &root_container);
204 swayc_t *c = get_swayc_for_handle(view, &root_container); 227 sway_log(L_DEBUG, "setting view %ld %s, fullscreen %d",view,c->name,toggle);
205 sway_log(L_DEBUG, "setting view %ld %s, fullscreen %d",view,c->name,toggle); 228 if (c) {
206 if (c) { 229 arrange_windows(c->parent, -1, -1);
207 arrange_windows(c->parent, -1, -1); 230 //Set it as focused window for that workspace if its going
208 //Set it as focused window for that workspace if its going 231 //fullscreen
209 //fullscreen 232 if (toggle) {
210 if (toggle) { 233 swayc_t *ws = c;
211 swayc_t *ws = c; 234 while (ws->type != C_WORKSPACE) {
212 while (ws->type != C_WORKSPACE) { 235 ws = ws->parent;
213 ws = ws->parent;
214 }
215 //Set ws focus to c
216 focus_view_for(ws, c);
217 } 236 }
237 //Set ws focus to c
238 set_focused_container_for(ws, c);
218 } 239 }
219 break;
220 } 240 }
241 break;
221 case WLC_BIT_MAXIMIZED: 242 case WLC_BIT_MAXIMIZED:
222 case WLC_BIT_RESIZING: 243 case WLC_BIT_RESIZING:
223 case WLC_BIT_MOVING: 244 case WLC_BIT_MOVING:
@@ -231,7 +252,7 @@ static void handle_view_state_request(wlc_handle view, enum wlc_view_state_bit s
231static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifiers 252static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifiers
232 *modifiers, uint32_t key, uint32_t sym, enum wlc_key_state state) { 253 *modifiers, uint32_t key, uint32_t sym, enum wlc_key_state state) {
233 enum { QSIZE = 32 }; 254 enum { QSIZE = 32 };
234 if (override_redirect) { 255 if (locked_view_focus) {
235 return false; 256 return false;
236 } 257 }
237 static uint8_t head = 0; 258 static uint8_t head = 0;
@@ -381,7 +402,7 @@ static bool handle_pointer_motion(wlc_handle handle, uint32_t time, const struct
381 } 402 }
382 } 403 }
383 if (config->focus_follows_mouse && prev_handle != handle) { 404 if (config->focus_follows_mouse && prev_handle != handle) {
384 focus_pointer(); 405 set_focused_container(container_under_pointer());
385 } 406 }
386 prev_handle = handle; 407 prev_handle = handle;
387 prev_pos = mouse_origin; 408 prev_pos = mouse_origin;
@@ -403,7 +424,8 @@ static bool handle_pointer_button(wlc_handle view, uint32_t time, const struct w
403 if (button == 273) { 424 if (button == 273) {
404 m2_held = true; 425 m2_held = true;
405 } 426 }
406 swayc_t *pointer = focus_pointer(); 427 swayc_t *pointer = container_under_pointer();
428 set_focused_container(pointer);
407 return (pointer && pointer != focused); 429 return (pointer && pointer != focused);
408 } else { 430 } else {
409 sway_log(L_DEBUG, "Mouse button %u released", button); 431 sway_log(L_DEBUG, "Mouse button %u released", button);
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|");