Merge pull request #27 from taiyu-len/master

rewrote and grouped swayc related functions together.
This commit is contained in:
Drew DeVault 2015-08-14 16:59:38 -04:00
commit 5a8f464bc1
10 changed files with 438 additions and 328 deletions

View file

@ -228,33 +228,25 @@ static bool cmd_set(struct sway_config *config, int argc, char **argv) {
} }
static bool _do_split(struct sway_config *config, int argc, char **argv, int layout) { static bool _do_split(struct sway_config *config, int argc, char **argv, int layout) {
char *name = layout == L_VERT ? "splitv": char *name = layout == L_VERT ? "splitv" :
layout == L_HORIZ ? "splith":"split"; layout == L_HORIZ ? "splith" : "split";
if (!checkarg(argc, name, EXPECTED_EQUAL_TO, 0)) { if (!checkarg(argc, name, EXPECTED_EQUAL_TO, 0)) {
return false; return false;
} }
swayc_t *focused = get_focused_container(&root_container); swayc_t *focused = get_focused_container(&root_container);
/* Case that focus is on an empty workspace. change its layout */
if (focused->type == C_WORKSPACE) { if (focused->type == C_WORKSPACE) {
sway_log(L_DEBUG, "Dont split workspaces");
if (focused->children->length == 0) {
focused->layout = layout; focused->layout = layout;
}
return true; return true;
} }
swayc_t *parent = focused->parent; /* Case of no siblings. change parent layout */
sway_log(L_DEBUG, "Splitting %p vertically with %p", parent, focused); if (focused->parent->children->length == 1) {
int index = remove_container_from_parent(parent, focused); focused->parent->layout = layout;
swayc_t *new_container = create_container(parent, -1); return true;
new_container->layout = layout; }
new_container->weight = focused->weight; /* regular case where new split container is build around focused container */
new_container->width = focused->width; swayc_t *parent = new_container(focused, layout);
new_container->height = focused->height;
new_container->x = focused->x;
new_container->y = focused->y;
focused->weight = 1;
focused->parent = new_container;
list_insert(parent->children, index, new_container);
list_add(new_container->children, focused);
focus_view(focused); focus_view(focused);
arrange_windows(parent, -1, -1); arrange_windows(parent, -1, -1);
return true; return true;
@ -302,8 +294,7 @@ static bool cmd_workspace(struct sway_config *config, int argc, char **argv) {
swayc_t *workspace = workspace_find_by_name(argv[0]); swayc_t *workspace = workspace_find_by_name(argv[0]);
if (!workspace) { if (!workspace) {
workspace = workspace_create(argv[0]); workspace = workspace_create(argv[0]);
} else sway_log(L_DEBUG, "workspace exists, all ok"); }
workspace_switch(workspace); workspace_switch(workspace);
return true; return true;
} }

View file

@ -1,18 +1,213 @@
#include <stdlib.h>
#include <stdbool.h>
#include <strings.h>
#include "container.h" #include "container.h"
#include "workspace.h"
#include "layout.h" #include "layout.h"
#include "log.h"
static swayc_t *new_swayc(enum swayc_types type) {
swayc_t *c = calloc(1, sizeof(swayc_t));
c->handle = -1;
c->layout = L_NONE;
c->type = type;
c->weight = 1;
if (type != C_VIEW) {
c->children = create_list();
}
return c;
}
static void free_swayc(swayc_t *c) {
//TODO does not properly handle containers with children,
//TODO but functions that call this usually check for that
if (c->children) {
list_free(c->children);
}
if (c->parent) {
remove_child(c->parent, c);
}
free(c);
}
/* New containers */
static void add_output_widths(swayc_t *container, void *_width) {
int *width = _width;
if (container->type == C_OUTPUT) {
*width += container->width;
}
}
swayc_t *new_output(wlc_handle handle) {
sway_log(L_DEBUG, "Added output %d", handle);
const struct wlc_size* size = wlc_output_get_resolution(handle);
swayc_t *output = new_swayc(C_OUTPUT);
output->width = size->w;
output->height = size->h;
output->handle = handle;
add_child(&root_container, output);
//TODO something with this
int total_width = 0;
container_map(&root_container, add_output_widths, &total_width);
//Create workspace
char *ws_name = workspace_next_name();
new_workspace(output, ws_name);
free(ws_name);
return output;
}
swayc_t *new_workspace(swayc_t * output, const char *name) {
sway_log(L_DEBUG, "Added workspace %s for output %d", name, output->handle);
swayc_t *workspace = new_swayc(C_WORKSPACE);
workspace->layout = L_HORIZ; // TODO:default layout
workspace->width = output->width;
workspace->height = output->height;
workspace->name = strdup(name);
workspace->visible = true;
add_child(output, workspace);
return workspace;
}
swayc_t *new_container(swayc_t *child, enum swayc_layouts layout) {
swayc_t *cont = new_swayc(C_CONTAINER);
sway_log(L_DEBUG, "creating container %p around %p", cont, child);
cont->layout = layout;
cont->width = child->width;
cont->height = child->height;
cont->x = child->x;
cont->y = child->y;
cont->visible = child->visible;
swayc_t *parent = replace_child(child, cont);
if (parent) {
add_child(cont, child);
}
return cont;
}
swayc_t *new_view(swayc_t *sibling, wlc_handle handle) {
const uint32_t type = wlc_view_get_type(handle);
const char *title = wlc_view_get_title(handle);
/* Skip if unmanaged window */
if ((type & WLC_BIT_OVERRIDE_REDIRECT) || (type & WLC_BIT_UNMANAGED) ||
(type & WLC_BIT_POPUP) || (type & WLC_BIT_MODAL) || (type & WLC_BIT_SPLASH)) {
sway_log(L_DEBUG, "Leaving view %d:%s alone (unmanaged)", handle, title);
return NULL;
}
swayc_t *view = new_swayc(C_VIEW);
sway_log(L_DEBUG, "Adding new view %d:%s:%d to container %p %d",
handle, title, type, sibling, sibling?sibling->type:0);
//Setup values
view->handle = handle;
view->name = strdup(title);
view->visible = true;
//Case of focused workspace, just create as child of it
if (sibling->type == C_WORKSPACE) {
add_child(sibling, view);
}
//Regular case, create as sibling of current container
else {
add_sibling(sibling, view);
}
return view;
}
swayc_t *destroy_output(swayc_t *output) {
if (output->children->length == 0) {
//TODO move workspaces to other outputs
}
sway_log(L_DEBUG, "OUTPUT: Destroying output '%d'", output->handle);
free_swayc(output);
return &root_container;
}
swayc_t *destroy_workspace(swayc_t *workspace) {
//TODO move containers to other workspaces?
//for now just dont delete
if (workspace->children->length == 0) {
sway_log(L_DEBUG, "Workspace: Destroying workspace '%s'", workspace->name);
swayc_t *parent = workspace->parent;
free_swayc(workspace);
return parent;
}
return NULL;
}
swayc_t *destroy_container(swayc_t *container) {
while (container->children->length == 0 && container->type == C_CONTAINER) {
sway_log(L_DEBUG, "Container: Destroying container '%p'", container);
swayc_t *parent = container->parent;
free_swayc(container);
if (parent->focused == container) {
parent->focused = NULL;
}
container = parent;
}
return container;
}
swayc_t *destroy_view(swayc_t *view) {
if (view == NULL) {
sway_log(L_DEBUG, "Warning: NULL passed into destroy_view");
return NULL;
}
sway_log(L_DEBUG, "Destroying view '%p'", view);
swayc_t *parent = view->parent;
free_swayc(view);
if (parent->focused == view) {
parent->focused = NULL;
}
//Destroy empty containers
if (parent->type == C_CONTAINER) {
return destroy_container(parent);
}
return parent;
}
swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data) {
if (!container->children) {
return NULL;
}
int i;
for (i = 0; i < container->children->length; ++i) {
swayc_t *child = container->children->items[i];
if (test(child, data)) {
return child;
} else {
swayc_t *_ = find_container(child, test, data);
if (_) {
return _;
}
}
}
return NULL;
}
void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data) { void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data) {
if (!container->children) { if (!container->children || !container->children->length) {
return; return;
} }
int i; int i;
for (i = 0; i < container->children->length; ++i) { for (i = 0; i < container->children->length; ++i) {
swayc_t *child = container->children->items[i]; swayc_t *child = container->children->items[i];
f(child, data); f(child, data);
if (child->children) {
container_map(child, f, data); container_map(child, f, data);
} }
}
} }

View file

@ -1,8 +1,71 @@
#ifndef _SWAY_CONTAINER_H #ifndef _SWAY_CONTAINER_H
#define _SWAY_CONTAINER_H #define _SWAY_CONTAINER_H
#include <wlc/wlc.h>
typedef struct sway_container swayc_t;
#include "layout.h" #include "layout.h"
enum swayc_types{
C_ROOT,
C_OUTPUT,
C_WORKSPACE,
C_CONTAINER,
C_VIEW,
//Keep last
C_TYPES,
};
enum swayc_layouts{
L_NONE,
L_HORIZ,
L_VERT,
L_STACKED,
L_TABBED,
L_FLOATING,
//Keep last
L_LAYOUTS,
};
struct sway_container {
wlc_handle handle;
enum swayc_types type;
enum swayc_layouts layout;
// Not including borders or margins
int width, height;
int x, y;
bool visible;
int weight;
char *name;
list_t *children;
struct sway_container *parent;
struct sway_container *focused;
};
swayc_t *new_output(wlc_handle handle);
swayc_t *new_workspace(swayc_t * output, const char *name);
//Creates container Around child (parent child) -> (parent (container child))
swayc_t *new_container(swayc_t *child, enum swayc_layouts layout);
//Creates view as a sibling of current focused container, or as child of a workspace
swayc_t *new_view(swayc_t *sibling, wlc_handle handle);
swayc_t *destroy_output(swayc_t *output);
//destroys workspace if empty and returns parent pointer, else returns NULL
swayc_t *destroy_workspace(swayc_t *workspace);
swayc_t *destroy_container(swayc_t *container);
swayc_t *destroy_view(swayc_t *view);
swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data);
void container_map(swayc_t *, void (*f)(swayc_t *, void *), void *); void container_map(swayc_t *, void (*f)(swayc_t *, void *), void *);
#endif #endif

View file

@ -9,14 +9,53 @@
#include "commands.h" #include "commands.h"
#include "handlers.h" #include "handlers.h"
#include "stringop.h" #include "stringop.h"
#include "workspace.h"
static struct wlc_origin mouse_origin;
static bool pointer_test(swayc_t *view, void *_origin) {
const struct wlc_origin *origin = _origin;
if (view->type == C_VIEW && origin->x >= view->x && origin->y >= view->y
&& origin->x < view->x + view->width && origin->y < view->y + view->height
&& view->visible) {
return true;
}
return false;
}
void focus_pointer(void) {
swayc_t *focused = find_container(&root_container, pointer_test, &mouse_origin);
if (focused) {
sway_log(L_DEBUG, "Switching focus to %p", focused);
unfocus_all(&root_container);
focus_view(focused);
} else {
focus_view(active_workspace);
}
}
static bool handle_output_created(wlc_handle output) { static bool handle_output_created(wlc_handle output) {
add_output(output); swayc_t *op = new_output(output);
//Switch to workspace if we need to
if (active_workspace == NULL) {
swayc_t *ws = op->children->items[0];
workspace_switch(ws);
}
return true; return true;
} }
static void handle_output_destroyed(wlc_handle output) { static void handle_output_destroyed(wlc_handle output) {
destroy_output(output); int i;
list_t *list = root_container.children;
for (i = 0; i < list->length; ++i) {
if (((swayc_t *)list->items[i])->handle == output) {
break;
}
}
if (i < list->length) {
destroy_output(list->items[i]);
}
} }
static void handle_output_resolution_change(wlc_handle output, const struct wlc_size *from, const struct wlc_size *to) { static void handle_output_resolution_change(wlc_handle output, const struct wlc_size *from, const struct wlc_size *to) {
@ -37,14 +76,33 @@ static void handle_output_focused(wlc_handle output, bool focus) {
} }
} }
static bool handle_view_created(wlc_handle view) { static bool handle_view_created(wlc_handle handle) {
add_view(view); swayc_t *container = get_focused_container(&root_container);
swayc_t *view = new_view(container, handle);
unfocus_all(&root_container);
if (view) {
focus_view(view);
arrange_windows(view->parent, -1, -1);
} else { //Unmanaged view
wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true);
wlc_view_focus(handle);
}
return true; return true;
} }
static void handle_view_destroyed(wlc_handle view) { static void handle_view_destroyed(wlc_handle handle) {
sway_log(L_DEBUG, "Destroying window %d", view); sway_log(L_DEBUG, "Destroying window %d", handle);
destroy_view(get_swayc_for_handle(view, &root_container)); swayc_t *view = get_swayc_for_handle(handle, &root_container);
swayc_t *parent;
swayc_t *focused = get_focused_container(&root_container);
if (view) {
parent = destroy_view(view);
arrange_windows(parent, -1, -1);
}
if (!focused || focused == view) {
focus_pointer();
}
} }
static void handle_view_focus(wlc_handle view, bool focus) { static void handle_view_focus(wlc_handle view, bool focus) {
@ -121,18 +179,6 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier
return cmd_success; return cmd_success;
} }
bool pointer_test(swayc_t *view, void *_origin) {
const struct wlc_origin *origin = _origin;
if (view->type == C_VIEW && origin->x >= view->x && origin->y >= view->y
&& origin->x < view->x + view->width && origin->y < view->y + view->height
&& view->visible) {
return true;
}
return false;
}
struct wlc_origin mouse_origin;
static bool handle_pointer_motion(wlc_handle view, uint32_t time, const struct wlc_origin *origin) { static bool handle_pointer_motion(wlc_handle view, uint32_t time, const struct wlc_origin *origin) {
mouse_origin = *origin; mouse_origin = *origin;
if (!config->focus_follows_mouse) { if (!config->focus_follows_mouse) {

View file

@ -6,4 +6,7 @@
extern struct wlc_interface interface; extern struct wlc_interface interface;
//set focus to current pointer location
void focus_pointer(void);
#endif #endif

View file

@ -9,25 +9,75 @@
swayc_t root_container; swayc_t root_container;
swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data) { void init_layout(void) {
if (!container->children) { root_container.type = C_ROOT;
return NULL; root_container.layout = L_NONE;
} root_container.children = create_list();
int i; root_container.handle = -1;
for (i = 0; i < container->children->length; ++i) {
swayc_t *child = container->children->items[i];
if (test(child, data)) {
return child;
} else {
swayc_t *_ = find_container(child, test, data);
if (_) {
return _;
}
}
}
return NULL;
} }
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;
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) {
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) {
int i;
for (i = 0; i < parent->children->length; ++i) {
if (parent->children->items[i] == child) {
list_del(parent->children, i);
break;
}
}
if (parent->focused == child) {
parent->focused = NULL;
}
return parent;
}
void arrange_windows(swayc_t *container, int width, int height) { void arrange_windows(swayc_t *container, int width, int height) {
int i; int i;
if (width == -1 || height == -1) { if (width == -1 || height == -1) {
@ -68,7 +118,7 @@ void arrange_windows(swayc_t *container, int width, int height) {
}; };
if (wlc_view_get_state(container->handle) & WLC_BIT_FULLSCREEN) { if (wlc_view_get_state(container->handle) & WLC_BIT_FULLSCREEN) {
swayc_t *parent = container; swayc_t *parent = container;
while(parent->type != C_OUTPUT) { while (parent->type != C_OUTPUT) {
parent = parent->parent; parent = parent->parent;
} }
geometry.origin.x = 0; geometry.origin.x = 0;
@ -131,25 +181,6 @@ void arrange_windows(swayc_t *container, int width, int height) {
} }
} }
void init_layout(void) {
root_container.type = C_ROOT;
root_container.layout = L_NONE;
root_container.children = create_list();
root_container.handle = -1;
}
void free_swayc(swayc_t *container) {
// NOTE: Does not handle moving children into a different container
if (container->parent) {
remove_container_from_parent(container->parent, container);
}
list_free(container->children);
if (container->name) {
free(container->name);
}
free(container);
}
swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent) { swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent) {
if (parent->children == NULL) { if (parent->children == NULL) {
return NULL; return NULL;
@ -176,99 +207,6 @@ swayc_t *get_focused_container(swayc_t *parent) {
return get_focused_container(parent->focused); return get_focused_container(parent->focused);
} }
void add_view(wlc_handle view_handle) {
const uint32_t type = wlc_view_get_type(view_handle);
const char *title = wlc_view_get_title(view_handle);
if ((type & WLC_BIT_OVERRIDE_REDIRECT) || (type & WLC_BIT_UNMANAGED) || (type &
WLC_BIT_POPUP) || (type & WLC_BIT_MODAL) || (type & WLC_BIT_SPLASH)) {
sway_log(L_DEBUG, "Leaving view %d:%s alone (unmanaged)", view_handle, title);
unfocus_all(&root_container);
wlc_view_set_state(view_handle, WLC_BIT_ACTIVATED, true);
wlc_view_focus(view_handle);
return;
}
swayc_t *parent = get_focused_container(&root_container);
sway_log(L_DEBUG, "Adding new view %d:%s:%d under container %p %d", view_handle, title, type, parent, parent->type);
while (parent->type == C_VIEW) {
parent = parent->parent;
}
swayc_t *view = calloc(1, sizeof(swayc_t));
view->weight = 1;
view->layout = L_NONE;
view->handle = view_handle;
view->parent = parent;
view->type = C_VIEW;
view->visible = true;
if (title) {
view->name = malloc(strlen(title) + 1);
strcpy(view->name, title);
}
add_child(parent, view);
unfocus_all(&root_container);
focus_view(view);
arrange_windows(parent, -1, -1);
}
int remove_container_from_parent(swayc_t *parent, swayc_t *container) {
int i;
for (i = 0; i < parent->children->length; ++i) {
if (parent->children->items[i] == container) {
list_del(parent->children, i);
break;
}
}
if (parent->focused == container) {
parent->focused = NULL;
}
return i;
}
void destroy_view(swayc_t *view) {
if (view == NULL) {
sway_log(L_DEBUG, "Warning: NULL passed into destroy_view");
return;
}
sway_log(L_DEBUG, "Destroying container %p", view);
swayc_t *parent = view->parent;
if (!parent) {
return;
}
int i;
for (i = 0; i < parent->children->length; ++i) {
if (parent->children->items[i] == view) {
list_del(parent->children, i);
break;
}
}
free_swayc(view);
if (parent->focused == view) {
parent->focused = NULL;
}
unfocus_all(&root_container);
if (parent->children->length != 0) {
focus_view(parent->children->items[0]);
} else {
focus_view(parent);
}
arrange_windows(parent, -1, -1);
if (parent->children->length == 0 && parent->type == C_CONTAINER) {
destroy_view(parent);
}
}
void unfocus_all(swayc_t *container) { void unfocus_all(swayc_t *container) {
if (container->children == NULL) { if (container->children == NULL) {
return; return;
@ -285,85 +223,16 @@ void unfocus_all(swayc_t *container) {
} }
void focus_view(swayc_t *view) { void focus_view(swayc_t *view) {
sway_log(L_DEBUG, "Setting focus for %p", view); sway_log(L_DEBUG, "Setting focus to %p", view);
if (view == &root_container) { if (view->type == C_VIEW) {
// Propegate wayland focus down wlc_view_set_state(view->handle, WLC_BIT_ACTIVATED, true);
swayc_t *child = view->focused; wlc_view_bring_to_front(view->handle);
while (child && child->type != C_VIEW) { wlc_view_focus(view->handle);
child = child->focused;
}
if (child) {
wlc_view_set_state(child->handle, WLC_BIT_ACTIVATED, true);
wlc_view_focus(child->handle);
}
return;
} }
// Propagete focus up
while (view != &root_container) {
view->parent->focused = view; view->parent->focused = view;
focus_view(view->parent); view = view->parent;
}
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);
}
swayc_t *create_container(swayc_t *parent, wlc_handle handle) {
swayc_t *c = calloc(1, sizeof(swayc_t));
c->weight = 1;
c->handle = handle;
c->parent = parent;
c->layout = L_NONE;
c->type = C_CONTAINER;
c->children = create_list();
return c;
}
void add_output_widths(swayc_t *container, void *_width) {
int *width = _width;
if (container->type == C_OUTPUT) {
*width += container->width;
} }
} }
void add_output(wlc_handle output) {
sway_log(L_DEBUG, "Adding output %d", output);
const struct wlc_size* size = wlc_output_get_resolution(output);
swayc_t *container = create_container(&root_container, output);
container->type = C_OUTPUT;
container->width = size->w;
container->height = size->h;
add_child(&root_container, container);
int total_width = 0;
container_map(&root_container, add_output_widths, &total_width);
swayc_t *workspace = create_container(container, -1);
workspace->type = C_WORKSPACE;
workspace->name = workspace_next_name();
workspace->width = size->w; // TODO: gaps
workspace->height = size->h;
workspace->layout = L_HORIZ; // TODO: Get default layout from config
add_child(container, workspace);
sway_log(L_DEBUG, "Added workspace %s for output %d", workspace->name, output);
if (root_container.focused == NULL) {
workspace_switch(workspace);
unfocus_all(&root_container);
focus_view(workspace);
}
}
void destroy_output(wlc_handle output) {
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;
}
}
}

View file

@ -3,62 +3,23 @@
#include <wlc/wlc.h> #include <wlc/wlc.h>
#include "list.h" #include "list.h"
#include "container.h"
struct sway_container {
wlc_handle handle;
enum {
C_ROOT,
C_OUTPUT,
C_WORKSPACE,
C_CONTAINER,
C_VIEW
} type;
enum {
L_NONE,
L_HORIZ,
L_VERT,
L_STACKED,
L_TABBED,
L_FLOATING
} layout;
// Not including borders or margins
int width, height;
int x, y;
bool visible;
int weight;
char *name;
list_t *children;
struct sway_container *parent;
struct sway_container *focused;
};
typedef struct sway_container swayc_t;
extern swayc_t root_container; extern swayc_t root_container;
void init_layout(void); void init_layout(void);
void add_child(swayc_t *parent, swayc_t *child); void add_child(swayc_t *parent, swayc_t *child);
void add_output(wlc_handle output); //Returns parent container wihch needs to be rearranged.
void destroy_output(wlc_handle output); swayc_t *add_sibling(swayc_t *sibling, swayc_t *child);
void destroy_view(swayc_t *view); swayc_t *replace_child(swayc_t *child, swayc_t *new_child);
void add_view(wlc_handle view); swayc_t *remove_child(swayc_t *parent, swayc_t *child);
void unfocus_all(swayc_t *container); void unfocus_all(swayc_t *container);
void focus_view(swayc_t *view); void focus_view(swayc_t *view);
void arrange_windows(swayc_t *container, int width, int height); void arrange_windows(swayc_t *container, int width, int height);
swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data);
swayc_t *get_focused_container(swayc_t *parent); swayc_t *get_focused_container(swayc_t *parent);
int remove_container_from_parent(swayc_t *parent, swayc_t *container);
swayc_t *create_container(swayc_t *parent, wlc_handle handle);
void free_swayc(swayc_t *container);
swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent); swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent);
#endif #endif

View file

@ -10,14 +10,12 @@ bool move_focus(enum movement_direction direction) {
swayc_t *parent = current->parent; swayc_t *parent = current->parent;
if (direction == MOVE_PARENT) { if (direction == MOVE_PARENT) {
current = parent; if (parent->type == C_OUTPUT) {
parent = parent->parent;
if (parent->type == C_ROOT) {
sway_log(L_DEBUG, "Focus cannot move to parent"); sway_log(L_DEBUG, "Focus cannot move to parent");
return false; return false;
} else { } else {
sway_log(L_DEBUG, "Moving focus away from %p", current); sway_log(L_DEBUG, "Moving focus away from %p to %p", current, parent);
unfocus_all(parent); unfocus_all(parent->parent);
focus_view(parent); focus_view(parent);
return true; return true;
} }

View file

@ -6,6 +6,7 @@
#include "list.h" #include "list.h"
#include "log.h" #include "log.h"
#include "container.h" #include "container.h"
#include "handlers.h"
#include "config.h" #include "config.h"
#include "stringop.h" #include "stringop.h"
@ -68,19 +69,10 @@ char *workspace_next_name(void) {
swayc_t *workspace_create(const char* name) { swayc_t *workspace_create(const char* name) {
swayc_t *parent = get_focused_container(&root_container); swayc_t *parent = get_focused_container(&root_container);
while(parent->type != C_OUTPUT) { while (parent->type != C_OUTPUT) {
parent = parent->parent; parent = parent->parent;
} }
return new_workspace(parent, name);
swayc_t *workspace = create_container(parent, -1);
workspace->type = C_WORKSPACE;
workspace->name = strdup(name);
workspace->width = parent->width;
workspace->height = parent->height;
workspace->layout = L_HORIZ; // todo: thing
add_child(parent, workspace);
return workspace;
} }
bool workspace_by_name(swayc_t *view, void *data) { bool workspace_by_name(swayc_t *view, void *data) {
@ -88,23 +80,13 @@ bool workspace_by_name(swayc_t *view, void *data) {
(strcasecmp(view->name, (char *) data) == 0); (strcasecmp(view->name, (char *) data) == 0);
} }
bool workspace_destroy(swayc_t *workspace) {
//Dont destroy if there are children
if (workspace->children->length) {
return false;
}
sway_log(L_DEBUG, "Workspace: Destroying workspace '%s'", workspace->name);
free_swayc(workspace);
return true;
}
void set_mask(swayc_t *view, void *data) { void set_mask(swayc_t *view, void *data) {
uint32_t *p = data; uint32_t *p = data;
if(view->type == C_VIEW) { if (view->type == C_VIEW) {
wlc_view_set_mask(view->handle, *p); wlc_view_set_mask(view->handle, *p);
view->visible = (*p == 2);
} }
view->visible = (*p == 2);
} }
swayc_t *workspace_find_by_name(const char* name) { swayc_t *workspace_find_by_name(const char* name) {
@ -123,9 +105,9 @@ void workspace_switch(swayc_t *workspace) {
container_map(workspace, set_mask, &mask); container_map(workspace, set_mask, &mask);
wlc_output_set_mask(wlc_get_focused_output(), 2); wlc_output_set_mask(wlc_get_focused_output(), 2);
unfocus_all(active_workspace); unfocus_all(&root_container);
focus_view(workspace); focus_view(workspace);
workspace_destroy(active_workspace); destroy_workspace(active_workspace);
} }
active_workspace = workspace; active_workspace = workspace;
} }

View file

@ -5,6 +5,8 @@
#include "list.h" #include "list.h"
#include "layout.h" #include "layout.h"
extern swayc_t *active_workspace;
char *workspace_next_name(void); char *workspace_next_name(void);
swayc_t *workspace_create(const char*); swayc_t *workspace_create(const char*);
swayc_t *workspace_find_by_name(const char*); swayc_t *workspace_find_by_name(const char*);