Compare commits

...

4 Commits

5 changed files with 205 additions and 35 deletions

View File

@ -19,8 +19,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "client.h"
#include "wm.h"
#include <X11/X.h>
#include <X11/Xlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>

View File

@ -20,7 +20,30 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef CLIENT_H
#define CLIENT_H
#include "wm.h"
#include <X11/X.h>
#include <X11/Xlib.h>
#include <stdbool.h>
typedef struct Wm Wm;
typedef struct Workspace Workspace;
typedef struct Monitor Monitor;
typedef struct Client Client;
struct Client {
int x;
int y;
int w;
int h;
Workspace *ws;
bool hidden;
Monitor *m;
Window window;
Client *prev;
Client *next;
char *name;
bool has_border;
bool is_floating;
};
XWindowChanges wm_client_to_xwchanges(Client c);
Client* wm_client_find(Wm* wm, Window w);

170
util.c
View File

@ -9,16 +9,18 @@ void wm_treenode_init(TreeNode *node, NodeType type, TreeNode *parent)
node->parent = parent;
node->type = type;
node->children = (NodeArray) {0};
wm_nodearray_init(&node->children);
wm_nodearray_new();
}
void wm_nodearray_init(NodeArray *arr)
NodeArray wm_nodearray_new()
{
assert(arr);
NodeArray arr;
arr->capacity = 10;
arr->nodes = calloc(arr->capacity, sizeof(TreeNode));
arr->size = 0;
arr.capacity = 10;
arr.nodes = calloc(arr.capacity, sizeof(TreeNode));
arr.size = 0;
return arr;
}
void wm_nodearray_push(NodeArray *arr, TreeNode node)
@ -48,9 +50,7 @@ void wm_nodearray_push(NodeArray *arr, TreeNode node)
bool wm_nodearray_pop(NodeArray *arr, TreeNode *ret)
{
assert(arr);
if (!ret || !arr)
return false;
assert(ret);
if (arr->size == 0)
return false;
@ -60,6 +60,21 @@ bool wm_nodearray_pop(NodeArray *arr, TreeNode *ret)
return true;
}
bool wm_nodearray_pop_front(NodeArray *arr, TreeNode *ret)
{
assert(arr);
assert(ret);
if (arr->size == 0)
return false;
*ret = arr->nodes[0];
arr->size--;
memcpy(arr->nodes, arr->nodes+1, arr->size * sizeof(TreeNode));
return true;
}
bool wm_nodearray_remove(NodeArray *arr, size_t index)
{
assert(arr);
@ -79,3 +94,140 @@ bool wm_nodearray_remove(NodeArray *arr, size_t index)
return true;
}
void wm_nodearray_free(NodeArray *arr)
{
assert(arr);
arr->capacity = 0;
arr->size = 0;
free(arr->nodes);
}
// breadth-first search for finding client nodes in tree
ClientArray wm_nodearray_find_clients(TreeNode *root)
{
assert(root);
/* storing visited nodes in an array and searching is
not optimal, but this is not a performance-critical function. */
NodeArray visited = wm_nodearray_new();
wm_nodearray_push(&visited, *root);
NodeArray queue = wm_nodearray_new();
ClientArray clients = wm_clientarray_new();
wm_nodearray_push(&queue, *root);
while (queue.size > 0) {
TreeNode node;
if (!wm_nodearray_pop_front(&queue, &node))
goto ret;
if (node.type == NODE_CLIENT)
wm_clientarray_push(&clients, *node.client);
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++) {
// this comparison may not work
if (visited.nodes[j].children.nodes == child_node.children.nodes) {
_visited = true;
break;
}
}
if (!_visited) {
wm_nodearray_push(&visited, child_node);
wm_nodearray_push(&queue, child_node);
}
}
}
ret:
wm_nodearray_free(&visited);
wm_nodearray_free(&queue);
return clients;
}
ClientArray wm_clientarray_new()
{
ClientArray arr;
arr.capacity = 10;
arr.clients = calloc(arr.capacity, sizeof(TreeNode));
arr.size = 0;
return arr;
}
void wm_clientarray_push(ClientArray *arr, Client node)
{
assert(arr);
arr->size++;
if (arr->size >= arr->capacity) {
Client* temp = calloc(arr->capacity, sizeof(Client));
memcpy(temp, arr->clients, arr->capacity * sizeof(Client));
free(arr->clients);
size_t old_capacity = arr->capacity;
arr->capacity = old_capacity * 2;
arr->clients = calloc(arr->capacity, sizeof(Client));
memcpy(arr->clients, temp, old_capacity);
free(temp);
arr->clients[arr->size - 1] = node;
return;
}
arr->clients[arr->size - 1] = node;
}
bool wm_clientarray_pop(ClientArray *arr, Client *ret)
{
assert(arr);
if (!ret || !arr)
return false;
if (arr->size == 0)
return false;
*ret = arr->clients[arr->size - 1];
arr->size--;
return true;
}
bool wm_clientarray_remove(ClientArray *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->clients + index, arr->clients + index+1, arr->size-1 * sizeof(Client));
return true;
}
void wm_clientarray_free(ClientArray *arr)
{
assert(arr);
arr->capacity = 0;
arr->size = 0;
free(arr->clients);
}

25
util.h
View File

@ -5,6 +5,8 @@
#include <stdlib.h>
#include <string.h>
#include "client.h"
#define WM_VERT_LEFT 0
#define WM_VERT_RIGHT 1
#define WM_HORIZ_TOP 0
@ -19,14 +21,18 @@ typedef enum NodeType {
} NodeType;
typedef struct TreeNode TreeNode;
typedef struct NodeArray NodeArray;
typedef struct Client Client;
struct NodeArray {
typedef struct {
TreeNode *nodes;
size_t size;
size_t capacity;
};
} NodeArray;
typedef struct {
Client *clients;
size_t size;
size_t capacity;
} ClientArray;
struct TreeNode {
NodeType type;
@ -45,9 +51,18 @@ struct TreeNode {
void wm_treenode_init(TreeNode *node, NodeType type, TreeNode *parent);
void wm_nodearray_init(NodeArray *arr);
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);
bool wm_nodearray_remove(NodeArray *arr, size_t index);
void wm_nodearray_free(NodeArray *arr);
ClientArray wm_nodearray_find_clients(TreeNode *root);
ClientArray wm_clientarray_new();
void wm_clientarray_push(ClientArray *arr, Client node);
bool wm_clientarray_pop(ClientArray *arr, Client *ret);
bool wm_clientarray_remove(ClientArray *arr, size_t index);
void wm_clientarray_free(ClientArray *arr);
#endif

18
wm.h
View File

@ -88,24 +88,6 @@ struct Monitor {
size_t wscount;
};
struct Client {
int x;
int y;
int w;
int h;
Workspace *ws;
bool hidden;
Monitor *m;
Window window;
Client *prev;
Client *next;
char *name;
bool has_border;
bool is_floating;
// linked list is not sufficent for manual tiling.
// will propably need a tree.
};
struct Workspace {
TreeNode tree;
size_t index;