2015-08-06 14:24:14 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <wlc/wlc.h>
|
2015-08-06 14:40:16 +02:00
|
|
|
#include "list.h"
|
2015-08-08 23:01:22 +02:00
|
|
|
#include "log.h"
|
2015-08-06 14:24:14 +02:00
|
|
|
#include "layout.h"
|
|
|
|
|
2015-08-08 23:44:51 +02:00
|
|
|
swayc_t root_container;
|
2015-08-08 23:01:22 +02:00
|
|
|
|
|
|
|
void arrange_windows() {
|
2015-08-08 23:44:51 +02:00
|
|
|
// TODO
|
2015-08-08 23:01:22 +02:00
|
|
|
}
|
2015-08-06 14:40:16 +02:00
|
|
|
|
|
|
|
void init_layout() {
|
2015-08-08 23:44:51 +02:00
|
|
|
root_container.type = C_ROOT;
|
|
|
|
root_container.layout = L_HORIZ; // TODO: Default layout
|
|
|
|
root_container.children = create_list();
|
|
|
|
root_container.handle = -1;
|
2015-08-06 14:40:16 +02:00
|
|
|
}
|
|
|
|
|
2015-08-08 23:44:51 +02:00
|
|
|
void free_swayc(swayc_t *container) {
|
|
|
|
// NOTE: Does not handle moving children into a different container
|
|
|
|
list_free(container->children);
|
|
|
|
free(container);
|
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;
|
2015-08-08 23:01:22 +02:00
|
|
|
} else {
|
2015-08-08 23:44:51 +02:00
|
|
|
swayc_t *res;
|
|
|
|
if ((res = get_swayc_for_handle(handle, child))) {
|
2015-08-08 23:01:22 +02:00
|
|
|
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:01:22 +02:00
|
|
|
}
|
2015-08-08 23:44:51 +02:00
|
|
|
return get_focused_container(parent->focused);
|
2015-08-08 23:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void add_view(wlc_handle view_handle) {
|
2015-08-08 23:44:51 +02:00
|
|
|
swayc_t *parent = get_focused_container(&root_container);
|
|
|
|
sway_log(L_DEBUG, "Adding new view %d under container %d", view_handle, (int)parent->type);
|
|
|
|
|
|
|
|
while (parent->type == C_VIEW) {
|
|
|
|
parent = parent->parent;
|
2015-08-08 23:01:22 +02:00
|
|
|
}
|
|
|
|
|
2015-08-08 23:44:51 +02:00
|
|
|
swayc_t *view = calloc(1, sizeof(swayc_t));
|
|
|
|
view->layout = L_NONE;
|
|
|
|
view->handle = view_handle;
|
|
|
|
view->parent = parent;
|
|
|
|
view->type = C_VIEW;
|
|
|
|
list_add(parent->children, view);
|
2015-08-08 23:01:22 +02:00
|
|
|
|
|
|
|
wlc_view_focus(view_handle);
|
|
|
|
|
|
|
|
arrange_windows();
|
|
|
|
}
|
|
|
|
|
2015-08-08 23:44:51 +02:00
|
|
|
void destroy_view(swayc_t *view) {
|
|
|
|
sway_log(L_DEBUG, "Destroying container %p", view);
|
|
|
|
if (!view->parent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < view->parent->children->length; ++i) {
|
|
|
|
if (view->parent->children->items[i] == view) {
|
|
|
|
list_del(view->parent->children, i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-08-08 23:01:22 +02:00
|
|
|
|
2015-08-08 23:44:51 +02:00
|
|
|
free_swayc(view);
|
2015-08-08 23:01:22 +02:00
|
|
|
|
2015-08-08 23:44:51 +02:00
|
|
|
// TODO: Focus some other window
|
2015-08-08 23:01:22 +02:00
|
|
|
|
|
|
|
arrange_windows();
|
|
|
|
}
|
|
|
|
|
2015-08-08 23:44:51 +02:00
|
|
|
void focus_view(swayc_t *view) {
|
|
|
|
if (view == &root_container) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
view->parent->focused = view;
|
|
|
|
focus_view(view->parent);
|
|
|
|
}
|
|
|
|
|
2015-08-06 14:40:16 +02:00
|
|
|
void add_output(wlc_handle output) {
|
2015-08-08 23:44:51 +02:00
|
|
|
sway_log(L_DEBUG, "Adding output %d", output);
|
|
|
|
const struct wlc_size* size = wlc_output_get_resolution(output);
|
|
|
|
|
|
|
|
swayc_t *container = calloc(1, sizeof(swayc_t));
|
|
|
|
container->handle = output;
|
|
|
|
container->type = C_OUTPUT;
|
2015-08-06 14:40:16 +02:00
|
|
|
container->children = create_list();
|
2015-08-08 23:44:51 +02:00
|
|
|
container->parent = &root_container;
|
|
|
|
container->layout = L_NONE;
|
|
|
|
container->width = size->w;
|
|
|
|
container->height = size->h;
|
|
|
|
|
|
|
|
list_add(root_container.children, container);
|
|
|
|
|
|
|
|
swayc_t *workspace = calloc(1, sizeof(swayc_t));
|
|
|
|
workspace->handle = -1;
|
|
|
|
workspace->type = C_WORKSPACE;
|
|
|
|
workspace->parent = container;
|
|
|
|
workspace->width = size->w; // TODO: gaps
|
|
|
|
workspace->height = size->h;
|
|
|
|
workspace->layout = L_HORIZ; // TODO: Get default layout from config
|
|
|
|
workspace->children = create_list();
|
|
|
|
|
|
|
|
list_add(container->children, workspace);
|
|
|
|
|
|
|
|
if (root_container.focused == NULL) {
|
|
|
|
focus_view(workspace);
|
|
|
|
}
|
2015-08-06 14:40:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void destroy_output(wlc_handle output) {
|
2015-08-08 23:44:51 +02:00
|
|
|
sway_log(L_DEBUG, "Destroying output %d", output);
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < root_container.children->length; ++i) {
|
|
|
|
swayc_t *c = root_container.children->items[i];
|
|
|
|
if (c->handle == output) {
|
|
|
|
list_del(root_container.children, i);
|
|
|
|
free_swayc(c);
|
|
|
|
return;
|
|
|
|
}
|
2015-08-06 14:40:16 +02:00
|
|
|
}
|
2015-08-06 14:24:14 +02:00
|
|
|
}
|