summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2015-08-08 18:17:08 -0400
committerLibravatar Drew DeVault <sir@cmpwn.com>2015-08-08 18:17:08 -0400
commit6066467dff2a1c55c124f618af8a5406bfc6fac9 (patch)
tree3206799ee54eb8acdb6afe5178869a53e441e660
parentRefactor in-memory tree (diff)
downloadsway-6066467dff2a1c55c124f618af8a5406bfc6fac9.tar.gz
sway-6066467dff2a1c55c124f618af8a5406bfc6fac9.tar.zst
sway-6066467dff2a1c55c124f618af8a5406bfc6fac9.zip
Tile some windows baby
-rw-r--r--sway/handlers.c7
-rw-r--r--sway/layout.c94
-rw-r--r--sway/layout.h6
3 files changed, 93 insertions, 14 deletions
diff --git a/sway/handlers.c b/sway/handlers.c
index 27087295..79cbd31d 100644
--- a/sway/handlers.c
+++ b/sway/handlers.c
@@ -2,6 +2,7 @@
2#include <stdbool.h> 2#include <stdbool.h>
3#include <wlc/wlc.h> 3#include <wlc/wlc.h>
4#include "layout.h" 4#include "layout.h"
5#include "log.h"
5#include "handlers.h" 6#include "handlers.h"
6 7
7bool handle_output_created(wlc_handle output) { 8bool handle_output_created(wlc_handle output) {
@@ -14,6 +15,12 @@ void handle_output_destroyed(wlc_handle output) {
14} 15}
15 16
16void handle_output_resolution_change(wlc_handle output, const struct wlc_size *from, const struct wlc_size *to) { 17void handle_output_resolution_change(wlc_handle output, const struct wlc_size *from, const struct wlc_size *to) {
18 sway_log(L_DEBUG, "Output %d resolution changed to %d x %d", output, to->w, to->h);
19 swayc_t *c = get_swayc_for_handle(output, &root_container);
20 if (!c) return;
21 c->width = to->w;
22 c->height = to->h;
23 arrange_windows(&root_container, -1, -1);
17} 24}
18 25
19bool handle_view_created(wlc_handle view) { 26bool handle_view_created(wlc_handle view) {
diff --git a/sway/layout.c b/sway/layout.c
index f2fe4021..bbdcb844 100644
--- a/sway/layout.c
+++ b/sway/layout.c
@@ -7,8 +7,66 @@
7 7
8swayc_t root_container; 8swayc_t root_container;
9 9
10void arrange_windows() { 10void arrange_windows(swayc_t *container, int width, int height) {
11 // TODO 11 int i;
12 if (width == -1 || height == -1) {
13 sway_log(L_DEBUG, "Arranging layout for %p", container);
14 }
15 if (width == -1) {
16 width = container->width;
17 }
18 if (height == -1) {
19 height = container->height;
20 }
21
22 if (container->type == C_ROOT) {
23 for (i = 0; i < container->children->length; ++i) {
24 arrange_windows(container->children->items[i], -1, -1);
25 }
26 return;
27 }
28
29 if (container->type == C_VIEW) {
30 sway_log(L_DEBUG, "Setting view to %d x %d @ %d, %d", width, height, container->x, container->y);
31 struct wlc_geometry geometry = {
32 .origin = {
33 .x = container->x,
34 .y = container->y
35 },
36 .size = {
37 .w = width,
38 .h = height
39 }
40 };
41 wlc_view_set_geometry(container->handle, &geometry);
42 return;
43 }
44 container->width = width;
45 container->height = height;
46
47 double total_weight = 0;
48 for (i = 0; i < container->children->length; ++i) {
49 swayc_t *child = container->children->items[i];
50 total_weight += child->weight;
51 }
52
53 int x = 0, y = 0;
54 switch (container->layout) {
55 case L_HORIZ:
56 default:
57 for (i = 0; i < container->children->length; ++i) {
58 swayc_t *child = container->children->items[i];
59 double percent = child->weight / total_weight;
60 sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will receive %.2f of %d)", child, child->type, percent, width);
61 child->x = x + container->x;
62 child->y = y + container->y;
63 int w = width * percent;
64 int h = height;
65 arrange_windows(child, w, h);
66 x += w;
67 }
68 break;
69 }
12} 70}
13 71
14void init_layout() { 72void init_layout() {
@@ -52,34 +110,36 @@ swayc_t *get_focused_container(swayc_t *parent) {
52 110
53void add_view(wlc_handle view_handle) { 111void add_view(wlc_handle view_handle) {
54 swayc_t *parent = get_focused_container(&root_container); 112 swayc_t *parent = get_focused_container(&root_container);
55 sway_log(L_DEBUG, "Adding new view %d under container %d", view_handle, (int)parent->type); 113 sway_log(L_DEBUG, "Adding new view %d under container %p %d", view_handle, parent, parent->type);
56 114
57 while (parent->type == C_VIEW) { 115 while (parent->type == C_VIEW) {
58 parent = parent->parent; 116 parent = parent->parent;
59 } 117 }
60 118
61 swayc_t *view = calloc(1, sizeof(swayc_t)); 119 swayc_t *view = calloc(1, sizeof(swayc_t));
120 view->weight = 1;
62 view->layout = L_NONE; 121 view->layout = L_NONE;
63 view->handle = view_handle; 122 view->handle = view_handle;
64 view->parent = parent; 123 view->parent = parent;
65 view->type = C_VIEW; 124 view->type = C_VIEW;
66 list_add(parent->children, view); 125 add_child(parent, view);
67 126
68 wlc_view_focus(view_handle); 127 wlc_view_focus(view_handle);
69 128
70 arrange_windows(); 129 arrange_windows(parent, -1, -1);
71} 130}
72 131
73void destroy_view(swayc_t *view) { 132void destroy_view(swayc_t *view) {
74 sway_log(L_DEBUG, "Destroying container %p", view); 133 sway_log(L_DEBUG, "Destroying container %p", view);
75 if (!view->parent) { 134 swayc_t *parent = view->parent;
135 if (!parent) {
76 return; 136 return;
77 } 137 }
78 138
79 int i; 139 int i;
80 for (i = 0; i < view->parent->children->length; ++i) { 140 for (i = 0; i < parent->children->length; ++i) {
81 if (view->parent->children->items[i] == view) { 141 if (parent->children->items[i] == view) {
82 list_del(view->parent->children, i); 142 list_del(parent->children, i);
83 break; 143 break;
84 } 144 }
85 } 145 }
@@ -88,7 +148,7 @@ void destroy_view(swayc_t *view) {
88 148
89 // TODO: Focus some other window 149 // TODO: Focus some other window
90 150
91 arrange_windows(); 151 arrange_windows(parent, -1, -1);
92} 152}
93 153
94void focus_view(swayc_t *view) { 154void focus_view(swayc_t *view) {
@@ -99,11 +159,18 @@ void focus_view(swayc_t *view) {
99 focus_view(view->parent); 159 focus_view(view->parent);
100} 160}
101 161
162void add_child(swayc_t *parent, swayc_t *child) {
163 sway_log(L_DEBUG, "Adding %p (%d, %dx%d) to %p (%d, %dx%d)", child, child->type,
164 child->width, child->height, parent, parent->type, parent->width, parent->height);
165 list_add(parent->children, child);
166}
167
102void add_output(wlc_handle output) { 168void add_output(wlc_handle output) {
103 sway_log(L_DEBUG, "Adding output %d", output); 169 sway_log(L_DEBUG, "Adding output %d", output);
104 const struct wlc_size* size = wlc_output_get_resolution(output); 170 const struct wlc_size* size = wlc_output_get_resolution(output);
105 171
106 swayc_t *container = calloc(1, sizeof(swayc_t)); 172 swayc_t *container = calloc(1, sizeof(swayc_t));
173 container->weight = 1;
107 container->handle = output; 174 container->handle = output;
108 container->type = C_OUTPUT; 175 container->type = C_OUTPUT;
109 container->children = create_list(); 176 container->children = create_list();
@@ -111,10 +178,10 @@ void add_output(wlc_handle output) {
111 container->layout = L_NONE; 178 container->layout = L_NONE;
112 container->width = size->w; 179 container->width = size->w;
113 container->height = size->h; 180 container->height = size->h;
114 181 add_child(&root_container, container);
115 list_add(root_container.children, container);
116 182
117 swayc_t *workspace = calloc(1, sizeof(swayc_t)); 183 swayc_t *workspace = calloc(1, sizeof(swayc_t));
184 workspace->weight = 1;
118 workspace->handle = -1; 185 workspace->handle = -1;
119 workspace->type = C_WORKSPACE; 186 workspace->type = C_WORKSPACE;
120 workspace->parent = container; 187 workspace->parent = container;
@@ -122,8 +189,7 @@ void add_output(wlc_handle output) {
122 workspace->height = size->h; 189 workspace->height = size->h;
123 workspace->layout = L_HORIZ; // TODO: Get default layout from config 190 workspace->layout = L_HORIZ; // TODO: Get default layout from config
124 workspace->children = create_list(); 191 workspace->children = create_list();
125 192 add_child(container, workspace);
126 list_add(container->children, workspace);
127 193
128 if (root_container.focused == NULL) { 194 if (root_container.focused == NULL) {
129 focus_view(workspace); 195 focus_view(workspace);
diff --git a/sway/layout.h b/sway/layout.h
index 94e78a93..801faf90 100644
--- a/sway/layout.h
+++ b/sway/layout.h
@@ -27,6 +27,10 @@ struct sway_container {
27 // Not including borders or margins 27 // Not including borders or margins
28 int width, height; 28 int width, height;
29 29
30 int x, y;
31
32 int weight;
33
30 char *name; 34 char *name;
31 35
32 list_t *children; 36 list_t *children;
@@ -40,11 +44,13 @@ typedef struct sway_container swayc_t;
40extern swayc_t root_container; 44extern swayc_t root_container;
41 45
42void init_layout(); 46void init_layout();
47void add_child(swayc_t *parent, swayc_t *child);
43void add_output(wlc_handle output); 48void add_output(wlc_handle output);
44void destroy_output(wlc_handle output); 49void destroy_output(wlc_handle output);
45void destroy_view(swayc_t *view); 50void destroy_view(swayc_t *view);
46void add_view(wlc_handle view); 51void add_view(wlc_handle view);
47void focus_view(swayc_t *view); 52void focus_view(swayc_t *view);
53void arrange_windows(swayc_t *container, int width, int height);
48 54
49swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent); 55swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent);
50 56