sway-patched-tray-menu-github/sway/layout.c

241 lines
5.8 KiB
C
Raw Normal View History

#include <stdlib.h>
#include <stdbool.h>
#include <wlc/wlc.h>
2015-08-06 14:40:16 +02:00
#include "list.h"
#include "log.h"
#include "layout.h"
#include "container.h"
2015-08-10 22:31:23 +02:00
#include "workspace.h"
2015-08-08 23:44:51 +02:00
swayc_t root_container;
void init_layout(void) {
root_container.type = C_ROOT;
root_container.layout = L_NONE;
root_container.children = create_list();
root_container.handle = -1;
}
static int index_child(swayc_t *parent, swayc_t *child) {
int i;
for (i = 0; i < parent->children->length; ++i) {
if (parent->children->items[i] == child) {
break;
}
}
return i;
}
void add_child(swayc_t *parent, swayc_t *child) {
sway_log(L_DEBUG, "Adding %p (%d, %dx%d) to %p (%d, %dx%d)", child, child->type,
child->width, child->height, parent, parent->type, parent->width, parent->height);
list_add(parent->children, child);
child->parent = parent;
2015-08-14 21:44:35 +02:00
if (parent->focused == NULL) {
parent->focused = child;
}
}
swayc_t *add_sibling(swayc_t *sibling, swayc_t *child) {
swayc_t *parent = sibling->parent;
int i = index_child(parent, sibling);
if (i == parent->children->length) {
--i;
}
list_insert(parent->children, i+1, child);
child->parent = parent;
return child->parent;
}
swayc_t *replace_child(swayc_t *child, swayc_t *new_child) {
swayc_t *parent = child->parent;
if (parent == NULL) {
2015-08-09 15:23:10 +02:00
return NULL;
}
int i = index_child(parent, child);
parent->children->items[i] = new_child;
new_child->parent = child->parent;
if (child->parent->focused == child) {
child->parent->focused = new_child;
}
child->parent = NULL;
return parent;
}
swayc_t *remove_child(swayc_t *parent, swayc_t *child) {
2015-08-09 15:23:10 +02:00
int i;
for (i = 0; i < parent->children->length; ++i) {
if (parent->children->items[i] == child) {
list_del(parent->children, i);
break;
2015-08-09 15:23:10 +02:00
}
}
if (parent->focused == child) {
parent->focused = NULL;
}
return parent;
2015-08-09 15:23:10 +02:00
}
2015-08-09 00:17:08 +02:00
void arrange_windows(swayc_t *container, int width, int height) {
int i;
if (width == -1 || height == -1) {
sway_log(L_DEBUG, "Arranging layout for %p", container);
width = container->width;
height = container->height;
}
int x = 0, y = 0;
switch (container->type) {
case C_ROOT:
2015-08-09 00:17:08 +02:00
for (i = 0; i < container->children->length; ++i) {
swayc_t *child = container->children->items[i];
sway_log(L_DEBUG, "Arranging output at %d", x);
child->x = x;
child->y = y;
2015-08-11 05:54:23 +02:00
arrange_windows(child, -1, -1);
x += child->width;
2015-08-09 00:17:08 +02:00
}
return;
2015-08-11 05:54:23 +02:00
case C_OUTPUT:
container->width = width;
container->height = height;
x -= container->x;
y -= container->y;
break;
case C_VIEW:
2015-08-10 02:58:03 +02:00
{
struct wlc_geometry geometry = {
.origin = {
.x = container->x,
.y = container->y
},
.size = {
.w = width,
.h = height
}
};
if (wlc_view_get_state(container->handle) & WLC_BIT_FULLSCREEN) {
swayc_t *parent = container;
2015-08-14 21:44:35 +02:00
while (parent->type != C_OUTPUT) {
2015-08-10 02:58:03 +02:00
parent = parent->parent;
}
geometry.origin.x = 0;
geometry.origin.y = 0;
geometry.size.w = parent->width;
geometry.size.h = parent->height;
wlc_view_set_geometry(container->handle, &geometry);
wlc_view_bring_to_front(container->handle);
} else {
wlc_view_set_geometry(container->handle, &geometry);
container->width = width;
container->height = height;
2015-08-09 00:17:08 +02:00
}
2015-08-10 02:58:03 +02:00
sway_log(L_DEBUG, "Set view to %d x %d @ %d, %d", geometry.size.w, geometry.size.h,
geometry.origin.x, geometry.origin.y);
2015-08-10 02:49:19 +02:00
}
2015-08-09 00:17:08 +02:00
return;
default:
container->width = width;
container->height = height;
break;
2015-08-09 00:17:08 +02:00
}
double total_weight = 0;
for (i = 0; i < container->children->length; ++i) {
swayc_t *child = container->children->items[i];
total_weight += child->weight;
}
switch (container->layout) {
case L_HORIZ:
default:
2015-08-10 01:27:25 +02:00
sway_log(L_DEBUG, "Arranging %p horizontally", container);
2015-08-09 00:17:08 +02:00
for (i = 0; i < container->children->length; ++i) {
swayc_t *child = container->children->items[i];
double percent = child->weight / total_weight;
sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will receive %.2f of %d)", child, child->type, percent, width);
child->x = x + container->x;
child->y = y + container->y;
int w = width * percent;
int h = height;
arrange_windows(child, w, h);
x += w;
}
break;
case L_VERT:
2015-08-10 01:27:25 +02:00
sway_log(L_DEBUG, "Arranging %p vertically", container);
for (i = 0; i < container->children->length; ++i) {
swayc_t *child = container->children->items[i];
double percent = child->weight / total_weight;
sway_log(L_DEBUG, "Calculating arrangement for %p:%d (will receive %.2f of %d)", child, child->type, percent, width);
child->x = x + container->x;
child->y = y + container->y;
int w = width;
int h = height * percent;
arrange_windows(child, w, h);
y += h;
}
break;
2015-08-09 00:17:08 +02:00
}
layout_log(&root_container, 0);
}
2015-08-06 14:40:16 +02:00
2015-08-08 23:44:51 +02:00
swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent) {
if (parent->children == NULL) {
return NULL;
}
int i;
for (i = 0; i < parent->children->length; ++i) {
swayc_t *child = parent->children->items[i];
if (child->handle == handle) {
return child;
} else {
2015-08-08 23:44:51 +02:00
swayc_t *res;
if ((res = get_swayc_for_handle(handle, child))) {
return res;
}
}
}
return NULL;
}
2015-08-08 23:44:51 +02:00
swayc_t *get_focused_container(swayc_t *parent) {
if (parent->focused == NULL) {
return parent;
}
2015-08-08 23:44:51 +02:00
return get_focused_container(parent->focused);
}
void unfocus_all(swayc_t *container) {
if (container->children == NULL) {
return;
}
int i;
for (i = 0; i < container->children->length; ++i) {
swayc_t *view = container->children->items[i];
if (view->type == C_VIEW) {
wlc_view_set_state(view->handle, WLC_BIT_ACTIVATED, false);
} else {
unfocus_all(view);
}
}
}
2015-08-08 23:44:51 +02:00
void focus_view(swayc_t *view) {
2015-08-15 17:34:09 +02:00
sway_log(L_DEBUG, "Setting focus for %p", view);
while (view != &root_container) {
view->parent->focused = view;
view = view->parent;
}
while (view && view->type != C_VIEW) {
view = view->focused;
}
if (view) {
wlc_view_set_state(view->handle, WLC_BIT_ACTIVATED, true);
wlc_view_focus(view->handle);
2015-08-08 23:44:51 +02:00
}
2015-08-06 14:40:16 +02:00
}