Compare commits

...

4 Commits

6 changed files with 104 additions and 60 deletions

View File

@ -18,20 +18,21 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "client.h"
#include "util.h"
#include "wm.h"
#include <stdio.h>
#include <string.h>
#include <limits.h>
// TODO
XWindowChanges wm_client_to_xwchanges(Client c)
XWindowChanges wm_client_to_xwchanges(Client *c)
{
XWindowChanges ch;
ch.x = c.x;
ch.y = c.y;
ch.width = c.w;
ch.height = c.h;
ch.x = c->x;
ch.y = c->y;
ch.width = c->w;
ch.height = c->h;
// ch.border_width = 0;
// ch.stack_mode = Above;
@ -166,9 +167,7 @@ void wm_client_focus(Wm *wm, Client *c)
XSetInputFocus(wm->display, c->window, RevertToPointerRoot, CurrentTime);
wm_client_set_atom(wm, c, "_NET_ACTIVE_WINDOW", (unsigned char*) &c->window,
XA_WINDOW, 1);
// XChangeProperty(wm->display, root.window,
// XInternAtom(wm->display, "_NET_ACTIVE_WINDOW", False),
// 33, 32, PropModeReplace, (unsigned char *) &c->window, 1);
c->focused = true;
wm_monitor_clients_border(wm, c->m);
}
@ -197,6 +196,7 @@ void wm_client_focus_dir(Wm *wm, Client *c, int dir)
void wm_client_free(Wm *wm, Client *c)
{
DEBUG_PRINT("%s\n", __func__);
RETURN_IF_NULL(c);
if (c == wm->smon->clients) {
@ -235,6 +235,8 @@ void wm_client_kill(Wm *wm, Client *c)
if (XSendEvent(wm->display, c->window, false, NoEventMask, &event) != Success)
XKillClient(wm->display, c->window);
wm_treenode_remove_client(&c->ws->tree, c);
wm_client_free(wm, c);
wm_layout(wm, m);

View File

@ -46,7 +46,7 @@ struct Client {
bool is_floating;
};
XWindowChanges wm_client_to_xwchanges(Client c);
XWindowChanges wm_client_to_xwchanges(Client *c);
Client* wm_client_find(Wm* wm, Window w);
Client *wm_get_last_client(Wm *wm, Monitor m);
Client* wm_client_create(Wm *wm, Window w);

View File

@ -180,6 +180,8 @@ void wm_maprequest_handler(Wm *wm, XMapRequestEvent e)
c = wm_client_create(wm, e.window);
RETURN_IF_NULL(c);
wm_ws_tree_insert_client(wm, c->ws, c);
wm_client_handle_window_types(wm, c, e);
wm_layout(wm, c->m);

92
util.c
View File

@ -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
View File

@ -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);

45
wm.c
View File

@ -72,7 +72,7 @@ void wm_monitors_open_all(Wm *wm, Display *d)
// TODO config
snprintf(ws->name, WM_WS_NAME_LEN, "%d", j);
wm_treenode_init(&ws->tree, NODE_CLIENT, NULL);
ws->tree = wm_treenode_new(NODE_CLIENT, NULL);
}
//wm->monitors[i].clients = &wm->root;
@ -150,6 +150,7 @@ NodeType wm_split_to_nodetype(SplitMode mode)
void wm_ws_tree_insert_client(Wm *wm, Workspace *ws, Client *client)
{
DEBUG_PRINT("%s\n", __func__);
int dock_y = 0;
if (wm->dock != -1) {
@ -159,7 +160,7 @@ void wm_ws_tree_insert_client(Wm *wm, Workspace *ws, Client *client)
dock_y = x.height;
}
if (wm_treenode_is_empty(&ws->tree)) {
if (wm_treenode_is_empty(&ws->tree) && ws->tree.client == NULL) {
ws->tree.type = NODE_CLIENT;
ws->tree.client = client;
ws->tree.pos = (Rect) {
@ -171,12 +172,13 @@ void wm_ws_tree_insert_client(Wm *wm, Workspace *ws, Client *client)
}
TreeNode *focused_node = wm_treenode_ptr_find_focused_client_node(&ws->tree);
assert(focused_node);
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);
TreeNode child1 = wm_treenode_new(NODE_CLIENT, focused_node);
TreeNode child2 = wm_treenode_new(NODE_CLIENT, focused_node);
wm_treenode_split_space(focused_node, &child1.pos, &child2.pos);
child1.client = old_client;
@ -271,7 +273,7 @@ void wm_mstack(Wm *wm, Monitor *m)
c->y = sy;
c->w = wm->smon->info.width-wm->cfg_border_width*2;
c->h = (wm->smon->info.height-wm->cfg_border_width*2)-sy;
ch = wm_client_to_xwchanges(*c);
ch = wm_client_to_xwchanges(c);
DEBUG_PRINT("mstack client: 0x%x\n", c);
// XGetWMName(wm->display, c->window, &xt);
DEBUG_PRINT("mstack window id: %d\n", c->window);
@ -300,7 +302,7 @@ void wm_mstack(Wm *wm, Monitor *m)
c->h = ((m->info.height)/(cc_sws-1)-wm->cfg_border_width*2);
}
n++;
ch = wm_client_to_xwchanges(*c);
ch = wm_client_to_xwchanges(c);
// TODO store wm name when client is created
@ -343,32 +345,37 @@ void wm_set_layout(Wm *wm, void(*f)(Wm *wm, Monitor*))
void wm_layout(Wm *wm, Monitor *m)
{
RETURN_IF_NULL(m);
size_t i;
DEBUG_PRINT("%s\n", __func__);
Workspace *ws = &m->workspaces[m->selws];
PtrArray clients = wm_treenode_find_client_nodes_ptr(&ws->tree);
for (size_t i = 0; i < clients.size; i++) {
TreeNode *node = (TreeNode*) clients.ptrs[i];
assert(node->client);
for (i = 0; i < clients.size; i++) {
TreeNode *node = clients.ptrs[i];
Client *client = node->client;
assert(client);
node->client->x = node->pos.x;
node->client->y = node->pos.y;
node->client->w = node->pos.w;
node->client->h = node->pos.h;
client->x = node->pos.x;
client->y = node->pos.y;
client->w = node->pos.w;
client->h = node->pos.h;
unsigned int value_mask = CWX | CWY | CWWidth | CWHeight;
XWindowChanges xwc = wm_client_to_xwchanges(*node->client);
XConfigureWindow(wm->display, node->client->window, value_mask, &xwc);
if (node->client->focused)
wm_client_focus(wm, node->client);
XWindowChanges xwc = wm_client_to_xwchanges((client));
XConfigureWindow(wm->display, client->window, value_mask, &xwc);
wm_client_show(wm, client);
}
wm_client_focus(wm, clients.ptrs[i]);
XEvent e;
XSync(wm->display, False);
while(XCheckMaskEvent(wm->display, EnterWindowMask, &e));
wm_monitor_clients_border(wm, m);
wm_ptrarray_free(&clients);
}
Client *wm_get_last_client(Wm *wm, Monitor m)