Compare commits
4 Commits
c5990665f5
...
38f03105bd
Author | SHA1 | Date | |
---|---|---|---|
38f03105bd | |||
254b6d2ffb | |||
7734411ba6 | |||
f17eae5fb0 |
1
client.h
1
client.h
@ -41,6 +41,7 @@ struct Client {
|
||||
Client *prev;
|
||||
Client *next;
|
||||
char *name;
|
||||
bool focused;
|
||||
bool has_border;
|
||||
bool is_floating;
|
||||
};
|
||||
|
265
util.c
265
util.c
@ -1,4 +1,6 @@
|
||||
#include "util.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
@ -12,6 +14,75 @@ void wm_treenode_init(TreeNode *node, NodeType type, TreeNode *parent)
|
||||
wm_nodearray_new();
|
||||
}
|
||||
|
||||
bool wm_treenode_is_empty(TreeNode *node)
|
||||
{
|
||||
return (node->children.size == 0);
|
||||
}
|
||||
|
||||
void wm_treenode_split_space(TreeNode *node, Rect *ret1, Rect *ret2)
|
||||
{
|
||||
assert(node);
|
||||
assert(ret1);
|
||||
assert(ret2);
|
||||
|
||||
switch (node->type) {
|
||||
NODE_VERTICAL:
|
||||
ret1->w = node->pos.w / 2;
|
||||
ret1->h = node->pos.h;
|
||||
ret2->w = node->pos.w / 2;
|
||||
ret2->h = node->pos.h;
|
||||
|
||||
ret1->x = node->pos.x;
|
||||
ret1->y = node->pos.y;
|
||||
|
||||
ret2->x = node->pos.x + ret1->w + 1;
|
||||
ret2->y = node->pos.y;
|
||||
return;
|
||||
NODE_HORIZONTAL:
|
||||
ret1->w = node->pos.w;
|
||||
ret1->h = node->pos.h / 2;
|
||||
ret2->w = node->pos.w;
|
||||
ret2->h = node->pos.h / 2;
|
||||
|
||||
ret1->x = node->pos.x;
|
||||
ret1->y = node->pos.y;
|
||||
|
||||
ret2->x = node->pos.x;
|
||||
ret2->y = node->pos.y + ret1->h + 1;
|
||||
return;
|
||||
default:
|
||||
fprintf(stderr, "wm: unhandled treenode type %d in %s, exiting.\n",
|
||||
node->type, __func__);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
void wm_treenode_remove_client(TreeNode *root, Client *client)
|
||||
{
|
||||
TreeNode *client_node = wm_treenode_ptr_find_client_node(root, client);
|
||||
|
||||
if (client_node->parent == NULL) {
|
||||
client_node->client = NULL;
|
||||
wm_nodearray_clear(&client_node->children);
|
||||
return;
|
||||
}
|
||||
|
||||
TreeNode *parent = client_node->parent;
|
||||
parent->type = NODE_CLIENT;
|
||||
Client *other_client = NULL;
|
||||
|
||||
for (size_t i = 0; i < parent->children.size; i++) {
|
||||
if (parent->children.nodes[i].type == NODE_CLIENT &&
|
||||
parent->children.nodes[i].client != client) {
|
||||
other_client = parent->children.nodes[i].client;
|
||||
}
|
||||
}
|
||||
|
||||
assert(other_client);
|
||||
parent->client = other_client;
|
||||
wm_nodearray_clear(&parent->children);
|
||||
}
|
||||
|
||||
NodeArray wm_nodearray_new()
|
||||
{
|
||||
NodeArray arr;
|
||||
@ -75,6 +146,12 @@ bool wm_nodearray_pop_front(NodeArray *arr, TreeNode *ret)
|
||||
return true;
|
||||
}
|
||||
|
||||
void wm_nodearray_clear(NodeArray *arr)
|
||||
{
|
||||
arr->size = 0;
|
||||
memset(arr->nodes, 0, arr->capacity * sizeof(TreeNode));
|
||||
}
|
||||
|
||||
bool wm_nodearray_remove(NodeArray *arr, size_t index)
|
||||
{
|
||||
assert(arr);
|
||||
@ -153,6 +230,194 @@ ret:
|
||||
return clients;
|
||||
}
|
||||
|
||||
PtrArray wm_ptrarray_new()
|
||||
{
|
||||
PtrArray arr;
|
||||
|
||||
arr.capacity = 10;
|
||||
arr.ptrs = calloc(arr.capacity, sizeof(TreeNode));
|
||||
arr.size = 0;
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
void wm_ptrarray_push(PtrArray *arr, void* ptr)
|
||||
{
|
||||
assert(arr);
|
||||
|
||||
arr->size++;
|
||||
|
||||
if (arr->size >= arr->capacity) {
|
||||
TreeNode* temp = calloc(arr->capacity, sizeof(TreeNode));
|
||||
memcpy(temp, arr->ptrs, arr->capacity * sizeof(TreeNode));
|
||||
free(arr->ptrs);
|
||||
size_t old_capacity = arr->capacity;
|
||||
arr->capacity = old_capacity * 2;
|
||||
|
||||
arr->ptrs = calloc(arr->capacity, sizeof(TreeNode));
|
||||
memcpy(arr->ptrs, temp, old_capacity);
|
||||
|
||||
free(temp);
|
||||
arr->ptrs[arr->size - 1] = ptr;
|
||||
return;
|
||||
}
|
||||
|
||||
arr->ptrs[arr->size - 1] = ptr;
|
||||
}
|
||||
|
||||
bool wm_ptrarray_pop(PtrArray *arr, void **ret)
|
||||
{
|
||||
assert(arr);
|
||||
assert(ret);
|
||||
|
||||
if (arr->size == 0)
|
||||
return false;
|
||||
|
||||
*ret = arr->ptrs[arr->size - 1];
|
||||
arr->size--;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool wm_ptrarray_pop_front(PtrArray *arr, void **ret)
|
||||
{
|
||||
assert(arr);
|
||||
assert(ret);
|
||||
|
||||
if (arr->size == 0)
|
||||
return false;
|
||||
|
||||
*ret = arr->ptrs[0];
|
||||
arr->size--;
|
||||
memcpy(arr->ptrs, arr->ptrs+1, arr->size * sizeof(TreeNode));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool wm_ptrarray_remove(PtrArray *arr, size_t index)
|
||||
{
|
||||
assert(arr);
|
||||
|
||||
if (!arr)
|
||||
return false;
|
||||
|
||||
if (index >= arr->size)
|
||||
return false;
|
||||
|
||||
if (index == arr->size - 1) {
|
||||
arr->size--;
|
||||
return true;
|
||||
}
|
||||
|
||||
memmove(arr->ptrs + index, arr->ptrs + index+1, arr->size-1 * sizeof(TreeNode));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void wm_ptrarray_free(PtrArray *arr)
|
||||
{
|
||||
assert(arr);
|
||||
|
||||
arr->capacity = 0;
|
||||
arr->size = 0;
|
||||
free(arr->ptrs);
|
||||
}
|
||||
|
||||
TreeNode* wm_treenode_ptr_find_focused_client_node(TreeNode *root)
|
||||
{
|
||||
assert(root);
|
||||
|
||||
TreeNode *ret = NULL;
|
||||
|
||||
NodeArray visited = wm_nodearray_new();
|
||||
wm_nodearray_push(&visited, *root);
|
||||
|
||||
PtrArray queue = wm_ptrarray_new();
|
||||
|
||||
wm_ptrarray_push(&queue, root);
|
||||
|
||||
while (queue.size > 0) {
|
||||
TreeNode *node;
|
||||
|
||||
if (!wm_ptrarray_pop_front(&queue, (void**)&node))
|
||||
goto ret;
|
||||
|
||||
if (node->type == NODE_CLIENT && node->client->focused) {
|
||||
ret = node;
|
||||
goto ret;
|
||||
}
|
||||
|
||||
for (int i = 0; i < node->children.size; i++) {
|
||||
TreeNode child_node = node->children.nodes[i];
|
||||
bool _visited = false;
|
||||
|
||||
for (int j = 0; j < visited.size; j++) {
|
||||
if (visited.nodes[j].children.nodes == child_node.children.nodes) {
|
||||
_visited = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!_visited) {
|
||||
wm_nodearray_push(&visited, child_node);
|
||||
wm_ptrarray_push(&queue, &child_node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ret:
|
||||
wm_nodearray_free(&visited);
|
||||
wm_ptrarray_free(&queue);
|
||||
return ret;
|
||||
}
|
||||
|
||||
TreeNode* wm_treenode_ptr_find_client_node(TreeNode *root, Client *client)
|
||||
{
|
||||
assert(root);
|
||||
|
||||
TreeNode *ret = NULL;
|
||||
|
||||
NodeArray visited = wm_nodearray_new();
|
||||
wm_nodearray_push(&visited, *root);
|
||||
|
||||
PtrArray queue = wm_ptrarray_new();
|
||||
|
||||
wm_ptrarray_push(&queue, root);
|
||||
|
||||
while (queue.size > 0) {
|
||||
TreeNode *node;
|
||||
|
||||
if (!wm_ptrarray_pop_front(&queue, (void**)&node))
|
||||
goto ret;
|
||||
|
||||
if (node->client == client) {
|
||||
ret = node;
|
||||
goto ret;
|
||||
}
|
||||
|
||||
for (int i = 0; i < node->children.size; i++) {
|
||||
TreeNode child_node = node->children.nodes[i];
|
||||
bool _visited = false;
|
||||
|
||||
for (int j = 0; j < visited.size; j++) {
|
||||
if (visited.nodes[j].children.nodes == child_node.children.nodes) {
|
||||
_visited = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!_visited) {
|
||||
wm_nodearray_push(&visited, child_node);
|
||||
wm_ptrarray_push(&queue, &child_node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ret:
|
||||
wm_nodearray_free(&visited);
|
||||
wm_ptrarray_free(&queue);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ClientArray wm_clientarray_new()
|
||||
{
|
||||
ClientArray arr;
|
||||
|
32
util.h
32
util.h
@ -28,12 +28,25 @@ typedef struct {
|
||||
size_t capacity;
|
||||
} NodeArray;
|
||||
|
||||
typedef struct {
|
||||
void **ptrs;
|
||||
size_t size;
|
||||
size_t capacity;
|
||||
} PtrArray;
|
||||
|
||||
typedef struct {
|
||||
Client *clients;
|
||||
size_t size;
|
||||
size_t capacity;
|
||||
} ClientArray;
|
||||
|
||||
typedef struct {
|
||||
int x;
|
||||
int y;
|
||||
int w;
|
||||
int h;
|
||||
} Rect;
|
||||
|
||||
struct TreeNode {
|
||||
NodeType type;
|
||||
|
||||
@ -41,24 +54,35 @@ struct TreeNode {
|
||||
|
||||
NodeArray children;
|
||||
|
||||
int x;
|
||||
int y;
|
||||
int w;
|
||||
int h;
|
||||
Rect pos;
|
||||
|
||||
Client *client;
|
||||
};
|
||||
|
||||
void wm_treenode_init(TreeNode *node, NodeType type, TreeNode *parent);
|
||||
bool wm_treenode_is_empty(TreeNode *node);
|
||||
void wm_treenode_split_space(TreeNode *node, Rect *ret1, Rect *ret2);
|
||||
void wm_treenode_remove_client(TreeNode *root, Client *client);
|
||||
|
||||
NodeArray wm_nodearray_new();
|
||||
void wm_nodearray_push(NodeArray *arr, TreeNode node);
|
||||
bool wm_nodearray_pop(NodeArray *arr, TreeNode *ret);
|
||||
bool wm_nodearray_pop_front(NodeArray *arr, TreeNode *ret);
|
||||
void wm_nodearray_clear(NodeArray *arr);
|
||||
bool wm_nodearray_remove(NodeArray *arr, size_t index);
|
||||
void wm_nodearray_free(NodeArray *arr);
|
||||
ClientArray wm_nodearray_find_clients(TreeNode *root);
|
||||
|
||||
|
||||
PtrArray wm_ptrarray_new();
|
||||
void wm_ptrarray_push(PtrArray *arr, void* ptr);
|
||||
bool wm_ptrarray_pop(PtrArray *arr, void **ret);
|
||||
bool wm_ptrarray_pop_front(PtrArray *arr, void **ret);
|
||||
bool wm_ptrarray_remove(PtrArray *arr, size_t index);
|
||||
void wm_ptrarray_free(PtrArray *arr);
|
||||
TreeNode* wm_treenode_ptr_find_focused_client_node(TreeNode *root);
|
||||
TreeNode* wm_treenode_ptr_find_client_node(TreeNode *root, Client *client);
|
||||
|
||||
ClientArray wm_clientarray_new();
|
||||
void wm_clientarray_push(ClientArray *arr, Client node);
|
||||
bool wm_clientarray_pop(ClientArray *arr, Client *ret);
|
||||
|
50
wm.c
50
wm.c
@ -135,6 +135,56 @@ bool wm_window_is_dock(Wm *wm, Window w)
|
||||
return false;
|
||||
}
|
||||
|
||||
NodeType wm_split_to_nodetype(SplitMode mode)
|
||||
{
|
||||
switch (mode) {
|
||||
case SPLIT_VERTICAL:
|
||||
return NODE_VERTICAL;
|
||||
case SPLIT_HORIZ:
|
||||
return NODE_HORIZONTAL;
|
||||
case SPLIT_TAB:
|
||||
return NODE_TAB;
|
||||
}
|
||||
}
|
||||
|
||||
void wm_ws_tree_insert_client(Wm *wm, Workspace *ws, Client *client)
|
||||
{
|
||||
int dock_y = 0;
|
||||
|
||||
if (wm->dock != -1) {
|
||||
XWindowAttributes x;
|
||||
XGetWindowAttributes(wm->display, wm->dock, &x);
|
||||
|
||||
dock_y = x.height;
|
||||
}
|
||||
|
||||
if (wm_treenode_is_empty(&ws->tree)) {
|
||||
ws->tree.type = NODE_CLIENT;
|
||||
ws->tree.client = client;
|
||||
ws->tree.pos = (Rect) {
|
||||
.x = 0, .y = dock_y + 1,
|
||||
.w = ws->monitor->info.width,
|
||||
.h = ws->monitor->info.height - dock_y + 1
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
TreeNode *focused_node = wm_treenode_ptr_find_focused_client_node(&ws->tree);
|
||||
Client *old_client = ws->tree.client;
|
||||
focused_node->type = wm_split_to_nodetype(ws->split);
|
||||
|
||||
TreeNode child1, child2;
|
||||
wm_treenode_init(&child1, NODE_CLIENT, focused_node);
|
||||
wm_treenode_init(&child2, NODE_CLIENT, focused_node);
|
||||
|
||||
wm_treenode_split_space(focused_node, &child1.pos, &child2.pos);
|
||||
child1.client = old_client;
|
||||
child2.client = client;
|
||||
|
||||
wm_nodearray_push(&focused_node->children, child1);
|
||||
wm_nodearray_push(&focused_node->children, child2);
|
||||
}
|
||||
|
||||
void wm_switch_ws(Wm *wm, int ws)
|
||||
{
|
||||
if (ws > wm->smon->wscount || ws < 0)
|
||||
|
3
wm.h
3
wm.h
@ -146,6 +146,9 @@ void wm_set_layout(Wm *wm, void(*f)(Wm *wm, Monitor*));
|
||||
void wm_layout(Wm *wm, Monitor *m);
|
||||
bool wm_window_is_dock(Wm* wm, Window w);
|
||||
|
||||
NodeType wm_split_to_nodetype(SplitMode mode);
|
||||
void wm_ws_tree_insert_client(Wm *wm, Workspace *ws, Client *client);
|
||||
|
||||
void wm_spawn(Wm* wm, char **str);
|
||||
|
||||
void wm_switch_ws(Wm* wm, int ws);
|
||||
|
Loading…
x
Reference in New Issue
Block a user