add id field to TreeNode, add debug logs, fixes
This commit is contained in:
parent
e1620b452a
commit
cf3bea02da
92
util.c
92
util.c
@ -1,32 +1,40 @@
|
||||
#include "util.h"
|
||||
#include "wm.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
void wm_treenode_init(TreeNode *node, NodeType type, TreeNode *parent)
|
||||
TreeNode wm_treenode_new(NodeType type, TreeNode *parent)
|
||||
{
|
||||
assert(node);
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
TreeNode node;
|
||||
|
||||
node->parent = parent;
|
||||
node->type = type;
|
||||
node->children = (NodeArray) {0};
|
||||
wm_nodearray_new();
|
||||
node.parent = parent;
|
||||
node.type = type;
|
||||
node.children = wm_nodearray_new();
|
||||
|
||||
node_id++;
|
||||
node.id = node_id;
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
bool wm_treenode_is_empty(TreeNode *node)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
return (node->children.size == 0);
|
||||
}
|
||||
|
||||
void wm_treenode_split_space(TreeNode *node, Rect *ret1, Rect *ret2)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
assert(node);
|
||||
assert(ret1);
|
||||
assert(ret2);
|
||||
|
||||
switch (node->type) {
|
||||
NODE_VERTICAL:
|
||||
case NODE_VERTICAL:
|
||||
ret1->w = node->pos.w / 2;
|
||||
ret1->h = node->pos.h;
|
||||
ret2->w = node->pos.w / 2;
|
||||
@ -38,7 +46,7 @@ void wm_treenode_split_space(TreeNode *node, Rect *ret1, Rect *ret2)
|
||||
ret2->x = node->pos.x + ret1->w + 1;
|
||||
ret2->y = node->pos.y;
|
||||
return;
|
||||
NODE_HORIZONTAL:
|
||||
case NODE_HORIZONTAL:
|
||||
ret1->w = node->pos.w;
|
||||
ret1->h = node->pos.h / 2;
|
||||
ret2->w = node->pos.w;
|
||||
@ -59,6 +67,7 @@ void wm_treenode_split_space(TreeNode *node, Rect *ret1, Rect *ret2)
|
||||
|
||||
void wm_treenode_remove_client(TreeNode *root, Client *client)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
TreeNode *client_node = wm_treenode_ptr_find_client_node(root, client);
|
||||
|
||||
if (client_node->parent == NULL) {
|
||||
@ -85,6 +94,7 @@ void wm_treenode_remove_client(TreeNode *root, Client *client)
|
||||
|
||||
NodeArray wm_nodearray_new()
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
NodeArray arr;
|
||||
|
||||
arr.capacity = 10;
|
||||
@ -96,6 +106,7 @@ NodeArray wm_nodearray_new()
|
||||
|
||||
void wm_nodearray_push(NodeArray *arr, TreeNode node)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
assert(arr);
|
||||
|
||||
arr->size++;
|
||||
@ -108,7 +119,7 @@ void wm_nodearray_push(NodeArray *arr, TreeNode node)
|
||||
arr->capacity = old_capacity * 2;
|
||||
|
||||
arr->nodes = calloc(arr->capacity, sizeof(TreeNode));
|
||||
memcpy(arr->nodes, temp, old_capacity);
|
||||
memcpy(arr->nodes, temp, old_capacity * sizeof(TreeNode));
|
||||
|
||||
free(temp);
|
||||
arr->nodes[arr->size - 1] = node;
|
||||
@ -120,6 +131,7 @@ void wm_nodearray_push(NodeArray *arr, TreeNode node)
|
||||
|
||||
bool wm_nodearray_pop(NodeArray *arr, TreeNode *ret)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
assert(arr);
|
||||
assert(ret);
|
||||
|
||||
@ -133,6 +145,7 @@ bool wm_nodearray_pop(NodeArray *arr, TreeNode *ret)
|
||||
|
||||
bool wm_nodearray_pop_front(NodeArray *arr, TreeNode *ret)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
assert(arr);
|
||||
assert(ret);
|
||||
|
||||
@ -141,19 +154,21 @@ bool wm_nodearray_pop_front(NodeArray *arr, TreeNode *ret)
|
||||
|
||||
*ret = arr->nodes[0];
|
||||
arr->size--;
|
||||
memcpy(arr->nodes, arr->nodes+1, arr->size * sizeof(TreeNode));
|
||||
memmove(arr->nodes, arr->nodes+1, arr->size * sizeof(TreeNode));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void wm_nodearray_clear(NodeArray *arr)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
arr->size = 0;
|
||||
memset(arr->nodes, 0, arr->capacity * sizeof(TreeNode));
|
||||
}
|
||||
|
||||
bool wm_nodearray_remove(NodeArray *arr, size_t index)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
assert(arr);
|
||||
|
||||
if (!arr)
|
||||
@ -174,6 +189,7 @@ bool wm_nodearray_remove(NodeArray *arr, size_t index)
|
||||
|
||||
void wm_nodearray_free(NodeArray *arr)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
assert(arr);
|
||||
|
||||
arr->capacity = 0;
|
||||
@ -184,6 +200,7 @@ void wm_nodearray_free(NodeArray *arr)
|
||||
// breadth-first search for finding client nodes in tree
|
||||
ClientArray wm_nodearray_find_clients(TreeNode *root)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
assert(root);
|
||||
|
||||
/* storing visited nodes in an array and searching is
|
||||
@ -210,8 +227,7 @@ ClientArray wm_nodearray_find_clients(TreeNode *root)
|
||||
bool _visited = false;
|
||||
|
||||
for (int j = 0; j < visited.size; j++) {
|
||||
// this comparison may not work
|
||||
if (visited.nodes[j].children.nodes == child_node.children.nodes) {
|
||||
if (visited.nodes[j].id == child_node.id) {
|
||||
_visited = true;
|
||||
break;
|
||||
}
|
||||
@ -232,10 +248,11 @@ ret:
|
||||
|
||||
PtrArray wm_ptrarray_new()
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
PtrArray arr;
|
||||
|
||||
arr.capacity = 10;
|
||||
arr.ptrs = calloc(arr.capacity, sizeof(TreeNode));
|
||||
arr.ptrs = calloc(arr.capacity, sizeof(void*));
|
||||
arr.size = 0;
|
||||
|
||||
return arr;
|
||||
@ -243,19 +260,20 @@ PtrArray wm_ptrarray_new()
|
||||
|
||||
void wm_ptrarray_push(PtrArray *arr, void* ptr)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
assert(arr);
|
||||
|
||||
arr->size++;
|
||||
|
||||
if (arr->size >= arr->capacity) {
|
||||
TreeNode* temp = calloc(arr->capacity, sizeof(TreeNode));
|
||||
memcpy(temp, arr->ptrs, arr->capacity * sizeof(TreeNode));
|
||||
void* temp = calloc(arr->capacity, sizeof(void*));
|
||||
memcpy(temp, arr->ptrs, arr->capacity * sizeof(void*));
|
||||
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);
|
||||
arr->ptrs = calloc(arr->capacity, sizeof(void*));
|
||||
memcpy(arr->ptrs, temp, old_capacity * sizeof(void*));
|
||||
|
||||
free(temp);
|
||||
arr->ptrs[arr->size - 1] = ptr;
|
||||
@ -267,6 +285,7 @@ void wm_ptrarray_push(PtrArray *arr, void* ptr)
|
||||
|
||||
bool wm_ptrarray_pop(PtrArray *arr, void **ret)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
assert(arr);
|
||||
assert(ret);
|
||||
|
||||
@ -280,6 +299,7 @@ bool wm_ptrarray_pop(PtrArray *arr, void **ret)
|
||||
|
||||
bool wm_ptrarray_pop_front(PtrArray *arr, void **ret)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
assert(arr);
|
||||
assert(ret);
|
||||
|
||||
@ -288,13 +308,14 @@ bool wm_ptrarray_pop_front(PtrArray *arr, void **ret)
|
||||
|
||||
*ret = arr->ptrs[0];
|
||||
arr->size--;
|
||||
memcpy(arr->ptrs, arr->ptrs+1, arr->size * sizeof(TreeNode));
|
||||
memmove(arr->ptrs, arr->ptrs+1, arr->size * sizeof(void*));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool wm_ptrarray_remove(PtrArray *arr, size_t index)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
assert(arr);
|
||||
|
||||
if (!arr)
|
||||
@ -308,13 +329,14 @@ bool wm_ptrarray_remove(PtrArray *arr, size_t index)
|
||||
return true;
|
||||
}
|
||||
|
||||
memmove(arr->ptrs + index, arr->ptrs + index+1, arr->size-1 * sizeof(TreeNode));
|
||||
memmove(arr->ptrs + index, arr->ptrs + index+1, arr->size-1 * sizeof(void*));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void wm_ptrarray_free(PtrArray *arr)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
assert(arr);
|
||||
|
||||
arr->capacity = 0;
|
||||
@ -324,6 +346,7 @@ void wm_ptrarray_free(PtrArray *arr)
|
||||
|
||||
TreeNode* wm_treenode_ptr_find_focused_client_node(TreeNode *root)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
assert(root);
|
||||
|
||||
TreeNode *ret = NULL;
|
||||
@ -347,19 +370,19 @@ TreeNode* wm_treenode_ptr_find_focused_client_node(TreeNode *root)
|
||||
}
|
||||
|
||||
for (int i = 0; i < node->children.size; i++) {
|
||||
TreeNode child_node = node->children.nodes[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) {
|
||||
if (visited.nodes[j].id == child_node->id) {
|
||||
_visited = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!_visited) {
|
||||
wm_nodearray_push(&visited, child_node);
|
||||
wm_ptrarray_push(&queue, &child_node);
|
||||
wm_nodearray_push(&visited, *child_node);
|
||||
wm_ptrarray_push(&queue, child_node);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -372,6 +395,7 @@ ret:
|
||||
|
||||
TreeNode* wm_treenode_ptr_find_client_node(TreeNode *root, Client *client)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
assert(root);
|
||||
|
||||
TreeNode *ret = NULL;
|
||||
@ -386,7 +410,7 @@ TreeNode* wm_treenode_ptr_find_client_node(TreeNode *root, Client *client)
|
||||
while (queue.size > 0) {
|
||||
TreeNode *node;
|
||||
|
||||
if (!wm_ptrarray_pop_front(&queue, (void**)&node))
|
||||
if (!wm_ptrarray_pop_front(&queue, (void*)node))
|
||||
goto ret;
|
||||
|
||||
if (node->client == client) {
|
||||
@ -399,7 +423,7 @@ TreeNode* wm_treenode_ptr_find_client_node(TreeNode *root, Client *client)
|
||||
bool _visited = false;
|
||||
|
||||
for (int j = 0; j < visited.size; j++) {
|
||||
if (visited.nodes[j].children.nodes == child_node.children.nodes) {
|
||||
if (visited.nodes[j].id == child_node.id) {
|
||||
_visited = true;
|
||||
break;
|
||||
}
|
||||
@ -420,6 +444,7 @@ ret:
|
||||
|
||||
PtrArray wm_treenode_find_client_nodes_ptr(TreeNode *root)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
assert(root);
|
||||
|
||||
PtrArray ret = wm_ptrarray_new();
|
||||
@ -437,23 +462,23 @@ PtrArray wm_treenode_find_client_nodes_ptr(TreeNode *root)
|
||||
if (!wm_ptrarray_pop_front(&queue, (void**)&node))
|
||||
goto ret;
|
||||
|
||||
if (node->type == NODE_CLIENT)
|
||||
if (node->type == NODE_CLIENT && node->client != NULL)
|
||||
wm_ptrarray_push(&ret, node);
|
||||
|
||||
for (int i = 0; i < node->children.size; i++) {
|
||||
TreeNode child_node = node->children.nodes[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) {
|
||||
if (visited.nodes[j].id == child_node->id) {
|
||||
_visited = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!_visited) {
|
||||
wm_nodearray_push(&visited, child_node);
|
||||
wm_ptrarray_push(&queue, &child_node);
|
||||
wm_nodearray_push(&visited, *child_node);
|
||||
wm_ptrarray_push(&queue, child_node);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -466,6 +491,7 @@ ret:
|
||||
|
||||
ClientArray wm_clientarray_new()
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
ClientArray arr;
|
||||
|
||||
arr.capacity = 10;
|
||||
@ -477,6 +503,7 @@ ClientArray wm_clientarray_new()
|
||||
|
||||
void wm_clientarray_push(ClientArray *arr, Client node)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
assert(arr);
|
||||
|
||||
arr->size++;
|
||||
@ -489,7 +516,7 @@ void wm_clientarray_push(ClientArray *arr, Client node)
|
||||
arr->capacity = old_capacity * 2;
|
||||
|
||||
arr->clients = calloc(arr->capacity, sizeof(Client));
|
||||
memcpy(arr->clients, temp, old_capacity);
|
||||
memcpy(arr->clients, temp, old_capacity * sizeof(Client));
|
||||
|
||||
free(temp);
|
||||
arr->clients[arr->size - 1] = node;
|
||||
@ -501,6 +528,7 @@ void wm_clientarray_push(ClientArray *arr, Client node)
|
||||
|
||||
bool wm_clientarray_pop(ClientArray *arr, Client *ret)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
assert(arr);
|
||||
|
||||
if (!ret || !arr)
|
||||
@ -516,6 +544,7 @@ bool wm_clientarray_pop(ClientArray *arr, Client *ret)
|
||||
|
||||
bool wm_clientarray_remove(ClientArray *arr, size_t index)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
assert(arr);
|
||||
|
||||
if (!arr)
|
||||
@ -536,6 +565,7 @@ bool wm_clientarray_remove(ClientArray *arr, size_t index)
|
||||
|
||||
void wm_clientarray_free(ClientArray *arr)
|
||||
{
|
||||
DEBUG_PRINT("%s\n", __func__);
|
||||
assert(arr);
|
||||
|
||||
arr->capacity = 0;
|
||||
|
5
util.h
5
util.h
@ -12,6 +12,8 @@
|
||||
#define WM_HORIZ_TOP 0
|
||||
#define WM_HORIZ_BOT 1
|
||||
|
||||
static unsigned int node_id = 0;
|
||||
|
||||
typedef enum NodeType {
|
||||
NODE_VERTICAL,
|
||||
NODE_HORIZONTAL,
|
||||
@ -57,9 +59,10 @@ struct TreeNode {
|
||||
Rect pos;
|
||||
|
||||
Client *client;
|
||||
unsigned int id;
|
||||
};
|
||||
|
||||
void wm_treenode_init(TreeNode *node, NodeType type, TreeNode *parent);
|
||||
TreeNode wm_treenode_new(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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user