Compare commits

...

31 Commits

Author SHA1 Message Date
6778471950 Merge branch 'tree' 2023-07-02 23:26:15 +02:00
bee34c02c0 fix various warnings 2023-07-02 22:53:45 +02:00
57ad7142bb fix warning about different signedness integer comparison 2023-07-02 22:44:06 +02:00
ebaa9734dc fix a problem when removing a node that has another split as their sibling 2023-07-02 22:40:37 +02:00
463d095aa0 focus new clients 2023-07-02 21:21:30 +02:00
24a8cf1856 add function and keybinds to switch split mode 2023-07-02 19:53:27 +02:00
1f07ccbfd6 add function to convert tree structure to graphviz tree (DOT file) 2023-07-02 17:08:56 +02:00
921813176d remove unnecessary DEBUG_PRINT statements 2023-07-01 20:19:03 +02:00
e350587aa2 rewrite wm_treenode_ptr_find_client_node, return sibling from wm_treenode_remove_client 2023-07-01 20:16:54 +02:00
64d2adbe49 update directional focus to work with tree structure, focus another client after closing one 2023-07-01 20:10:27 +02:00
867757734f update wm_client_get_focused to find focused client in tree structure 2023-06-01 15:52:09 +02:00
1298543661 use updated util function signatures 2023-06-01 15:23:45 +02:00
cf3bea02da add id field to TreeNode, add debug logs, fixes 2023-06-01 15:22:58 +02:00
e1620b452a change wm_client_to_xwchanges parameter to Client*, remove client from tree when killed 2023-06-01 15:21:58 +02:00
00821acf12 insert client into tree on map request 2023-06-01 15:20:51 +02:00
6ae7143655 update wm_layout function to handle tree structure 2023-05-28 17:35:03 +02:00
ea2c1dcfbd add function to find addresses of client tree nodes 2023-05-28 17:25:39 +02:00
ca70f08995 update DEBUG_PRINT to include file name and line number 2023-05-28 17:21:47 +02:00
38f03105bd add function to insert new client into tree structure 2023-05-28 17:19:34 +02:00
254b6d2ffb add "focused" field to Client struct 2023-05-28 17:17:10 +02:00
7734411ba6 add function for splitting node space, add function to remove client from tree 2023-05-28 17:15:38 +02:00
f17eae5fb0 add PtrArray struct and functions, add function to find focused treenode 2023-05-28 16:22:23 +02:00
c5990665f5 add breadth-first search function to find client nodes in a tree 2023-05-26 20:33:59 +02:00
92a5dc5cf1 change array_init to array_new, working as a constructor 2023-05-26 20:01:27 +02:00
c66eb85cc6 add ClientArray struct and functions, add _free() functions 2023-05-26 19:29:19 +02:00
434e83d570 move Client struct definition to client.h 2023-05-26 19:19:02 +02:00
683354bbd6 change ws field in Client struct from ws index to Workspace struct pointer 2023-05-26 18:49:15 +02:00
c580905144 add SplitMode enum to Workspace struct 2023-05-26 18:40:27 +02:00
88a788d5f4 add workspace struct 2023-05-26 17:08:15 +02:00
71ee3fd10c add "NODE_" prefix to NodeType enum 2023-05-26 17:04:29 +02:00
60829dd1c1 add util.h and util.c 2023-05-26 16:45:49 +02:00
7 changed files with 1059 additions and 83 deletions

View File

@ -18,22 +18,22 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "client.h"
#include "util.h"
#include "wm.h"
#include <X11/X.h>
#include <X11/Xlib.h>
#include <assert.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;
@ -67,7 +67,7 @@ Client* wm_client_create(Wm *wm, Window w)
c->prev = NULL;
c->window = w;
c->m = wm->smon;
c->ws = wm->smon->selws;
c->ws = &wm->smon->workspaces[wm->smon->selws];
c->hidden = true;
c->is_floating = false;
@ -163,14 +163,20 @@ void wm_client_show(Wm *wm, Client *c)
void wm_client_focus(Wm *wm, Client *c)
{
DEBUG_PRINT("%s\n", __func__);
RETURN_IF_NULL(c);
Client *old_focused_client = wm->focused_client;
if (!old_focused_client)
old_focused_client = wm_client_get_focused(wm);
assert(old_focused_client);
old_focused_client->focused = false;
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->focused_client = c;
wm_monitor_clients_border(wm, c->m);
}
@ -199,6 +205,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) {
@ -237,9 +244,14 @@ void wm_client_kill(Wm *wm, Client *c)
if (XSendEvent(wm->display, c->window, false, NoEventMask, &event) != Success)
XKillClient(wm->display, c->window);
TreeNode* parent = wm_treenode_remove_client(&c->ws->tree, c);
wm_client_free(wm, c);
wm_layout(wm, m);
if (parent && parent->type == NODE_CLIENT)
wm_client_focus(wm, parent->client);
}
void wm_client_set_atom(Wm *wm, Client *c, const char* name, const unsigned char *data,
@ -296,7 +308,7 @@ Atom wm_client_get_atom(Wm *wm, Client *c, const char *name, unsigned char **ato
Client* wm_client_get_dir_rel_c(Client *c, int dir)
{
DEBUG_PRINT("%s\n", __func__)
DEBUG_PRINT("%s c: %p\n", __func__, c)
if (!c)
return NULL;
@ -334,12 +346,16 @@ Client* wm_client_get_dir_rel_c(Client *c, int dir)
return NULL;
}
PtrArray client_nodes = wm_treenode_find_client_nodes_ptr(&c->ws->tree);
x+=dx*5;
y+=dy*5;
do {
x+=dx*2;
y+=dy*2;
for (r = c->m->clients; r; r = r->next) {
for (size_t i = 0; i < client_nodes.size; i++) {
Client *r = ((TreeNode**)client_nodes.ptrs)[i]->client;
if ((x > r->x && x < r->x+r->w) && (y > r->y && y < r->y+r->h)) {
DEBUG_PRINT("%s ", __func__)
DEBUG_PRINT("found window %d in direction ", (int)c->window)
@ -356,22 +372,21 @@ Client* wm_client_get_dir_rel_c(Client *c, int dir)
Client* wm_client_get_focused(Wm *wm)
{
Client *c;
Client *c = NULL;
for (c = wm->smon->clients; c; c = c->next) {
if (wm_client_is_focused(wm, c))
return c;
}
Workspace *ws = &wm->smon->workspaces[wm->smon->selws];
TreeNode *node = wm_treenode_ptr_find_focused_client_node(&ws->tree);
assert(node);
return NULL;
return node->client;
}
void wm_client_border(Wm *wm, Client *c)
{
RETURN_IF_NULL(c);
DEBUG_PRINT("border col: %s\nfocused border col: %s\n",
wm->cfg_border_col, wm->cfg_focused_border_col);
// DEBUG_PRINT("border col: %s\nfocused border col: %s\n",
// wm->cfg_border_col, wm->cfg_focused_border_col);
XVisualInfo vinfo;
XColor color;

View File

@ -20,9 +20,33 @@ 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>
XWindowChanges wm_client_to_xwchanges(Client c);
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 focused;
bool has_border;
bool is_floating;
};
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,9 +180,12 @@ 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);
wm_client_focus(wm, c);
}
void wm_motion_handler(Wm *wm, XMotionEvent e)

712
util.c Normal file
View File

@ -0,0 +1,712 @@
#include "util.h"
#include "wm.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
TreeNode wm_treenode_new(NodeType type, TreeNode *parent)
{
DEBUG_PRINT("%s\n", __func__);
TreeNode node;
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) {
case 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;
case 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_node_type_to_str(NodeType type, char *buf, size_t bufsize)
{
switch (type) {
case NODE_CLIENT:
strncpy(buf, "NODE_CLIENT", bufsize);
return;
case NODE_VERTICAL:
strncpy(buf, "NODE_VERTICAL", bufsize);
return;
case NODE_HORIZONTAL:
strncpy(buf, "NODE_HORIZONTAL", bufsize);
return;
case NODE_TAB:
strncpy(buf, "NODE_TAB", bufsize);
return;
case NODE_COUNT:
return;
}
}
void wm_tree_to_DOT(TreeNode *root, const char *filename)
{
DEBUG_PRINT("%s\n", __func__);
FILE *fp = fopen(filename, "w");
assert(fp);
fprintf(fp, "digraph {\n");
PtrArray all_nodes = wm_all_nodes_to_ptrarray(root);
NodeArray printed_nodes = wm_nodearray_new();
const int bufsize = 100;
char buf1[bufsize];
char buf2[bufsize];
for (size_t i = 0; i < all_nodes.size; i++) {
TreeNode *node = all_nodes.ptrs[i];
bool contains = false;
for (size_t j = 0; j < printed_nodes.size; j++) {
if (printed_nodes.nodes[i].id == node->id)
contains = true;
}
if (contains)
continue;
for (size_t j = 0; j < node->children.size; j++) {
memset(buf1, 0, bufsize);
memset(buf2, 0, bufsize);
wm_node_type_to_str(node->type, buf1, bufsize);
wm_node_type_to_str(node->children.nodes[j].type, buf2, bufsize);
fprintf(fp, "_%d_%s -> _%d_%s;\n", node->id, buf1,
node->children.nodes[j].id, buf2);
}
wm_nodearray_push(&printed_nodes, *node);
}
fprintf(fp, "}");
fflush(fp);
fclose(fp);
wm_nodearray_free(&printed_nodes);
wm_ptrarray_free(&all_nodes);
}
int wm_get_node_index(TreeNode *parent, unsigned int node_id)
{
for (size_t i = 0; i < parent->children.size; i++) {
if (node_id == parent->children.nodes[i].id)
return i;
}
return -1;
}
void wm_treenode_remove_node(TreeNode *root, unsigned int node_id)
{
DEBUG_PRINT("%s\n", __func__);
assert(root);
PtrArray all_nodes = wm_all_nodes_to_ptrarray(root);
for (size_t i = 0; i < all_nodes.size; i++) {
TreeNode *node = all_nodes.ptrs[i];
if (node->id == node_id) {
TreeNode *parent = node->parent;
int index = wm_get_node_index(parent, node_id);
assert(index >= 0);
wm_nodearray_remove(&parent->children, index);
}
}
}
TreeNode* wm_treenode_split_get_sibling(TreeNode *node)
{
for (size_t i = 0; i < node->parent->children.size; i++) {
TreeNode *sibling_node = &node->parent->children.nodes[i];
if (sibling_node->id != node->id) {
return sibling_node;
}
}
return NULL;
}
TreeNode* wm_treenode_remove_client(TreeNode *root, Client *client)
{
DEBUG_PRINT("%s\n", __func__);
TreeNode *client_node = wm_treenode_ptr_find_client_node(root, client);
wm_tree_to_DOT(root, "wm_output.dot");
if (client_node->parent == NULL) {
client_node->client = NULL;
wm_nodearray_clear(&client_node->children);
return NULL;
}
TreeNode *node = client_node->parent;
for (size_t i = 0; i < node->children.size; i++) {
if (node->children.nodes[i].type == NODE_CLIENT &&
node->children.nodes[i].client != client) {
node->type = NODE_CLIENT;
node->client = node->children.nodes[i].client;
wm_nodearray_clear(&node->children);
return node;
}
}
DEBUG_PRINT("%s: other_client was NULL!\n", __func__);
TreeNode *sibling_node = wm_treenode_split_get_sibling(client_node);
wm_nodearray_free(&client_node->children);
wm_nodearray_free(&node->children);
if (node->parent == NULL) {
// parent is root node
sibling_node->parent = NULL;
client->ws->tree = *sibling_node;
} else
*node = *sibling_node;
return node;
}
NodeArray wm_nodearray_new()
{
NodeArray arr;
arr.capacity = 10;
arr.nodes = calloc(arr.capacity, sizeof(TreeNode));
arr.size = 0;
return arr;
}
void wm_nodearray_push(NodeArray *arr, TreeNode node)
{
assert(arr);
arr->size++;
if (arr->size >= arr->capacity) {
TreeNode* temp = calloc(arr->capacity, sizeof(TreeNode));
memcpy(temp, arr->nodes, arr->capacity * sizeof(TreeNode));
free(arr->nodes);
size_t old_capacity = arr->capacity;
arr->capacity = old_capacity * 2;
arr->nodes = calloc(arr->capacity, sizeof(TreeNode));
memcpy(arr->nodes, temp, old_capacity * sizeof(TreeNode));
free(temp);
arr->nodes[arr->size - 1] = node;
return;
}
arr->nodes[arr->size - 1] = node;
}
bool wm_nodearray_pop(NodeArray *arr, TreeNode *ret)
{
DEBUG_PRINT("%s\n", __func__);
assert(arr);
assert(ret);
if (arr->size == 0)
return false;
*ret = arr->nodes[arr->size - 1];
arr->size--;
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--;
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)
return false;
if (index >= arr->size)
return false;
if (index == arr->size - 1) {
arr->size--;
return true;
}
memmove(arr->nodes + index, arr->nodes + index+1, arr->size-1 * sizeof(TreeNode));
return true;
}
void wm_nodearray_free(NodeArray *arr)
{
DEBUG_PRINT("%s\n", __func__);
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)
{
DEBUG_PRINT("%s\n", __func__);
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 (size_t i = 0; i < node.children.size; i++) {
TreeNode child_node = node.children.nodes[i];
bool _visited = false;
for (size_t j = 0; j < visited.size; j++) {
if (visited.nodes[j].id == child_node.id) {
_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;
}
PtrArray wm_ptrarray_new()
{
PtrArray arr;
arr.capacity = 10;
arr.ptrs = calloc(arr.capacity, sizeof(void*));
arr.size = 0;
return arr;
}
void wm_ptrarray_push(PtrArray *arr, void* ptr)
{
assert(arr);
arr->size++;
if (arr->size >= arr->capacity) {
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(void*));
memcpy(arr->ptrs, temp, old_capacity * sizeof(void*));
free(temp);
arr->ptrs[arr->size - 1] = ptr;
return;
}
arr->ptrs[arr->size - 1] = ptr;
}
bool wm_ptrarray_pop(PtrArray *arr, void **ret)
{
DEBUG_PRINT("%s\n", __func__);
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--;
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)
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(void*));
return true;
}
void wm_ptrarray_free(PtrArray *arr)
{
DEBUG_PRINT("%s\n", __func__);
assert(arr);
arr->capacity = 0;
arr->size = 0;
free(arr->ptrs);
}
TreeNode* wm_treenode_ptr_find_focused_client_node(TreeNode *root)
{
DEBUG_PRINT("%s\n", __func__);
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 && node->client->focused) {
ret = node;
goto ret;
}
for (size_t i = 0; i < node->children.size; i++) {
TreeNode *child_node = &node->children.nodes[i];
bool _visited = false;
for (size_t j = 0; j < visited.size; j++) {
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);
}
}
}
ret:
wm_nodearray_free(&visited);
wm_ptrarray_free(&queue);
return ret;
}
TreeNode* wm_treenode_ptr_find_client_node(TreeNode *root, Client *client)
{
assert(root);
assert(client);
TreeNode *ret = NULL;
PtrArray client_nodes = wm_treenode_find_client_nodes_ptr(root);
for (size_t i = 0; i < client_nodes.size; i++) {
TreeNode *node = (TreeNode*)client_nodes.ptrs[i];
if (node->client == client) {
ret = node;
goto ret;
}
}
ret:
wm_ptrarray_free(&client_nodes);
return ret;
}
PtrArray wm_treenode_find_client_nodes_ptr(TreeNode *root)
{
DEBUG_PRINT("%s\n", __func__);
assert(root);
PtrArray ret = wm_ptrarray_new();
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 != NULL)
wm_ptrarray_push(&ret, node);
for (size_t i = 0; i < node->children.size; i++) {
TreeNode *child_node = &node->children.nodes[i];
bool _visited = false;
for (size_t j = 0; j < visited.size; j++) {
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);
}
}
}
ret:
wm_nodearray_free(&visited);
wm_ptrarray_free(&queue);
return ret;
}
PtrArray wm_all_nodes_to_ptrarray(TreeNode *root)
{
DEBUG_PRINT("%s\n", __func__);
assert(root);
PtrArray ret = wm_ptrarray_new();
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;
wm_ptrarray_push(&ret, node);
for (size_t i = 0; i < node->children.size; i++) {
TreeNode *child_node = &node->children.nodes[i];
bool _visited = false;
for (size_t j = 0; j < visited.size; j++) {
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);
}
}
}
ret:
wm_nodearray_free(&visited);
wm_ptrarray_free(&queue);
return ret;
}
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)
{
DEBUG_PRINT("%s\n", __func__);
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 * sizeof(Client));
free(temp);
arr->clients[arr->size - 1] = node;
return;
}
arr->clients[arr->size - 1] = node;
}
bool wm_clientarray_pop(ClientArray *arr, Client *ret)
{
DEBUG_PRINT("%s\n", __func__);
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)
{
DEBUG_PRINT("%s\n", __func__);
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)
{
DEBUG_PRINT("%s\n", __func__);
assert(arr);
arr->capacity = 0;
arr->size = 0;
free(arr->clients);
}

102
util.h Normal file
View File

@ -0,0 +1,102 @@
#ifndef UTIL_H
#define UTIL_H
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "client.h"
#define WM_VERT_LEFT 0
#define WM_VERT_RIGHT 1
#define WM_HORIZ_TOP 0
#define WM_HORIZ_BOT 1
static unsigned int node_id = 0;
typedef enum NodeType {
NODE_VERTICAL,
NODE_HORIZONTAL,
NODE_TAB,
NODE_CLIENT,
NODE_COUNT
} NodeType;
typedef struct TreeNode TreeNode;
typedef struct {
TreeNode *nodes;
size_t size;
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;
TreeNode *parent;
NodeArray children;
Rect pos;
Client *client;
unsigned int id;
};
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);
TreeNode* wm_treenode_remove_client(TreeNode *root, Client *client);
void wm_treenode_remove_node(TreeNode *root, unsigned int node_id);
int wm_get_node_index(TreeNode *parent, unsigned int node_id);
void wm_tree_to_DOT(TreeNode *root, const char *filename);
void wm_node_type_to_str(NodeType type, char *buf, size_t bufsize);
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);
PtrArray wm_treenode_find_client_nodes_ptr(TreeNode *root);
TreeNode* wm_treenode_split_get_sibling(TreeNode *node);
PtrArray wm_all_nodes_to_ptrarray(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

190
wm.c
View File

@ -24,14 +24,17 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <X11/Xutil.h>
#include <X11/extensions/Xinerama.h>
#include <asm-generic/errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <assert.h>
#include "handler.h"
#include "client.h"
#include "util.h"
Monitor wm_monitor_open(Display *d, XineramaScreenInfo info)
{
@ -60,12 +63,18 @@ void wm_monitors_open_all(Wm *wm, Display *d)
wm->monitors[i] = wm_monitor_open(d, xsi[i]);
//TODO: cfg
wm->monitors[i].wscount = 9;
wm->monitors[i].wss = malloc(wm->monitors[i].wscount*sizeof(int));
memset(wm->monitors[i].wss, 0, wm->monitors[i].wscount);
wm->monitors[i].selws = wm->monitors[i].wss[0];
wm->monitors[i].workspaces = calloc(wm->monitors[i].wscount, sizeof(Workspace));
wm->monitors[i].selws = 0;
for (int j = 0; j < wm->monitors[i].wscount; j++)
wm->monitors[i].wss[i] = j;
for (size_t j = 0; j < wm->monitors[i].wscount; j++) {
Workspace *ws = &wm->monitors[i].workspaces[i];
ws->index = j;
ws->monitor = &wm->monitors[i];
// TODO config
snprintf(ws->name, WM_WS_NAME_LEN, "%ld", j);
ws->tree = wm_treenode_new(NODE_CLIENT, NULL);
}
//wm->monitors[i].clients = &wm->root;
wm->monitors[i].clients = NULL;
@ -114,7 +123,7 @@ bool wm_window_is_dock(Wm *wm, Window w)
DEBUG_PRINT("actual number of items return: %ld\n", nitems_ret)
DEBUG_PRINT("bytes remaining: %ld\n", nitems_ret)
//DEBUG_PRINT("property return dec: %d\n", prop_ret)
for (int i = 0; i < nitems_ret; i++) {
for (size_t i = 0; i < nitems_ret; i++) {
printf("property return str: %s\n",
XGetAtomName(wm->display, ((Atom*)prop_ret)[i]));
if (strcmp(XGetAtomName(wm->display, ((Atom*)prop_ret)[i]),
@ -128,18 +137,81 @@ bool wm_window_is_dock(Wm *wm, Window w)
return false;
}
void wm_switch_ws(Wm *wm, int ws)
NodeType wm_split_to_nodetype(SplitMode mode)
{
if (ws > wm->smon->wscount || ws < 0)
switch (mode) {
case SPLIT_VERTICAL:
return NODE_VERTICAL;
case SPLIT_HORIZ:
return NODE_HORIZONTAL;
case SPLIT_TAB:
return NODE_TAB;
default:
fprintf(stderr, "wm: unhandled split mode %d in %s, exiting.\n",
mode, __func__);
abort();
}
}
void wm_ws_tree_insert_client(Wm *wm, Workspace *ws, Client *client)
{
DEBUG_PRINT("%s\n", __func__);
int dock_y = 0;
if (wm->dock != ULONG_MAX) {
XWindowAttributes x;
XGetWindowAttributes(wm->display, wm->dock, &x);
dock_y = x.height;
}
if (wm_treenode_is_empty(&ws->tree) && ws->tree.client == NULL) {
ws->tree.type = NODE_CLIENT;
ws->tree.client = client;
ws->tree.pos = (Rect) {
.x = 0, .y = dock_y,
.w = ws->monitor->info.width,
.h = ws->monitor->info.height - dock_y,
};
return;
}
TreeNode *focused_node = wm_treenode_ptr_find_focused_client_node(&ws->tree);
assert(focused_node);
Client *old_client = focused_node->client;
focused_node->type = wm_split_to_nodetype(ws->split);
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;
child2.client = client;
wm_nodearray_push(&focused_node->children, child1);
wm_nodearray_push(&focused_node->children, child2);
}
void wm_switch_ws(Wm *wm, size_t ws)
{
if (ws > wm->smon->wscount)
return;
int wscc = 0;
int xasws;
Client *c;
for (c = wm->smon->clients; c; c = c->next) {
if (c->ws == wm->smon->selws) {
DEBUG_PRINT("wm_switch_ws hide client selws: %d\n", c->ws)
PtrArray clients = wm_treenode_find_client_nodes_ptr(&wm->smon->workspaces[ws].tree);
if (clients.size == 0) {
DEBUG_PRINT("clients size 0, returning\n");
return;
}
for (size_t i = 0; i < clients.size; i++) {
c = ((TreeNode**)clients.ptrs)[i]->client;
assert(c);
if (c->ws->index == wm->smon->selws) {
DEBUG_PRINT("wm_switch_ws hide client selws: %ld\n", c->ws->index)
wm_client_hide(wm, c);
}
}
@ -148,12 +220,10 @@ void wm_switch_ws(Wm *wm, int ws)
wm_client_set_atom(wm, &wm-> root, "_NET_CURRENT_DESKTOP",
(unsigned char*) &(ws), XA_CARDINAL, 1);
// XChangeProperty(wm->display, root.window,
// XInternAtom(wm->display, "_NET_CURRENT_DESKTOP", False), XA_CARDINAL,
// 32, PropModeReplace, (unsigned char *) &(xasws), 1);
for (c = wm->smon->clients; c; c = c->next) {
if (c->ws == wm->smon->selws) {
for (size_t i = 0; i < clients.size; i++) {
c = ((TreeNode**)clients.ptrs)[i]->client;
if (c->ws->index == wm->smon->selws) {
wscc++;
wm_client_show(wm, c);
}
@ -174,7 +244,6 @@ void wm_mstack(Wm *wm, Monitor *m)
int n = 0;
int sx = 0;
int sy = 0;
XTextProperty xt;
XEvent e;
unsigned int value_mask = CWX | CWY | CWWidth | CWHeight;
@ -183,7 +252,7 @@ void wm_mstack(Wm *wm, Monitor *m)
// FIXME: &root probably wrong
if (wm->dock != -1) {
if (wm->dock != ULONG_MAX) {
XWindowAttributes x;
XGetWindowAttributes(wm->display, wm->dock, &x);
@ -194,7 +263,7 @@ void wm_mstack(Wm *wm, Monitor *m)
}
for (c = m->clients; c; c = c->next) {
if (c->ws == m->selws && c != &wm->root && !c->is_floating) {
if (c->ws->index == m->selws && c != &wm->root && !c->is_floating) {
cc_sws++;
}
}
@ -209,17 +278,17 @@ void wm_mstack(Wm *wm, Monitor *m)
// dynamic
if (cc_sws == 1) {
for (c = m->clients; c; c = c->next) {
if (c->ws == m->selws && c != &wm->root)
if (c->ws->index == m->selws && c != &wm->root)
break;
}
c->x = 0;
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);
DEBUG_PRINT("mstack client: 0x%x\n", c);
ch = wm_client_to_xwchanges(c);
DEBUG_PRINT("mstack client: %p\n", c);
// XGetWMName(wm->display, c->window, &xt);
DEBUG_PRINT("mstack window id: %d\n", c->window);
DEBUG_PRINT("mstack window id: %ld\n", c->window);
// DEBUG_PRINT("mstack wm_name: %s\n", xt.value)
// DEBUG_PRINT("mstack client width: %d\n", ch.width)
// DEBUG_PRINT("mstack client height: %d\n", ch.height)
@ -231,7 +300,7 @@ void wm_mstack(Wm *wm, Monitor *m)
else {
// FIXME not working with dock
for (c = m->clients; c; c = c->next) {
if (c->ws == m->selws && c->window != wm->root.window && !c->is_floating) {
if (c->ws->index == m->selws && c->window != wm->root.window && !c->is_floating) {
if (n == 0) {
c->x = 0;
c->y = sy;
@ -245,14 +314,14 @@ 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
// FIXME vv RUNTIME MALLOC ABORT vv
//XGetWMName(wm->display, c->window, &xt);
DEBUG_PRINT("mstack client: 0x%x\n", c);
DEBUG_PRINT("mstack window id: %d\n", c->window);
DEBUG_PRINT("mstack client: %p\n", c);
DEBUG_PRINT("mstack window id: %ld\n", c->window);
// DEBUG_PRINT("mstack wm_name: %s\n", xt.value)
// DEBUG_PRINT("mstack client x: %d ", ch.x)
// DEBUG_PRINT("y: %d\n", ch.y)
@ -262,7 +331,7 @@ void wm_mstack(Wm *wm, Monitor *m)
XConfigureWindow(wm->display, c->window, value_mask, &ch);
wm_client_show(wm, c);
DEBUG_PRINT("%s %d. client next: 0x%x\n", __func__, n, c->next)
DEBUG_PRINT("%s %d. client next: %p\n", __func__, n, c->next)
if (c->next == NULL)
{
@ -285,12 +354,40 @@ void wm_set_layout(Wm *wm, void(*f)(Wm *wm, Monitor*))
wm->active_layout = f;
}
// TODO: layout floating window, selected layout func p
void wm_layout(Wm *wm, Monitor *m)
{
RETURN_IF_NULL(m);
size_t i;
wm_mstack(wm, m);
DEBUG_PRINT("%s\n", __func__);
Workspace *ws = &m->workspaces[m->selws];
PtrArray clients = wm_treenode_find_client_nodes_ptr(&ws->tree);
for (i = 0; i < clients.size; i++) {
TreeNode *node = clients.ptrs[i];
Client *client = node->client;
assert(client);
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((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)
@ -319,9 +416,9 @@ void wm_mainloop(Wm *wm)
struct sockaddr s;
socklen_t t = 108;
getsockname(wm->socket_fd, &s, &t);
int accepted_socket;
char *buffer;
int len;
// int accepted_socket;
// char *buffer;
// accepted_socket = accept(wm->socket_fd, &s, &t);
@ -419,7 +516,6 @@ void wm_init(Wm *wm)
// wm_socket_init();
wm->root.window = XRootWindow(wm->display, DefaultScreen(wm->display));
wm->root.ws = 999;
DEBUG_PRINT("root window: %ld\n", wm->root.window);
@ -472,7 +568,7 @@ void wm_init(Wm *wm)
//XSetErrorHandler(&wm_xerror_handler);
wm->dock = -1;
wm->dock = ULONG_MAX;
wm->running = true;
wm_mainloop(wm);
@ -517,7 +613,7 @@ void wm_grab_keys(Wm *wm)
// from dwm
{
unsigned int i, j;
int i, j;
XModifierKeymap *modmap;
unsigned int numlockmask = 0;
@ -600,9 +696,16 @@ void wm_kb_focus_dir(Wm *wm, Arg *args)
wm_client_focus_dir(wm, c, args->i);
}
void wm_kb_switch_split_mode(Wm *wm, Arg *args)
{
RETURN_IF_NULL(args)
wm->smon->workspaces[wm->smon->selws].split = args->i;
}
void wm_keybinds_init_def(Wm *wm)
{
char *st[] = {"st", NULL};
char *st[] = {"alacritty", NULL};
char **sth = malloc(sizeof(st));
memcpy(sth, st, sizeof(st));
@ -662,6 +765,15 @@ void wm_keybinds_init_def(Wm *wm)
(Keybind) {Mod4Mask, XK_l, *wm_kb_focus_dir,
(Arg) {.i = RIGHT}},
(Keybind) {Mod4Mask, XK_b, *wm_kb_switch_split_mode,
(Arg) {.i = SPLIT_VERTICAL}},
(Keybind) {Mod4Mask, XK_v, *wm_kb_switch_split_mode,
(Arg) {.i = SPLIT_HORIZ}},
(Keybind) {Mod4Mask, XK_t, *wm_kb_switch_split_mode,
(Arg) {.i = SPLIT_TAB}},
};
@ -726,7 +838,7 @@ void wm_settings_message_parse(Wm *wm, char *c, int len)
return;
int op; /* 0 = set, 1 = get */
int i = 0;
size_t i = 0;
char ret[50];
char *token;
char tokens[20][50];

50
wm.h
View File

@ -36,6 +36,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <sys/socket.h>
#include <sys/un.h>
#include "util.h"
// TODO: when only 1 window on ws it is not focused
// TODO: dont draw border on not normal windows, floating dialog window,
// window type/name/... atom member variable on Client struct
@ -53,9 +55,13 @@ if (c == NULL) \
#define DEBUG 1
#define DEBUG_PRINT(fmt, ...) \
do { if (DEBUG) fprintf(stderr, fmt, ##__VA_ARGS__); } while (0);
do { if (DEBUG) { fprintf(stderr, "[%s:%d] ", __FILE__, __LINE__); \
fprintf(stderr, fmt, ##__VA_ARGS__);} } while (0);
#define WM_WS_NAME_LEN 64
typedef struct Client Client;
typedef struct Workspace Workspace;
typedef struct Monitor Monitor;
typedef struct Keybind Keybind;
typedef struct Arg Arg;
@ -68,31 +74,27 @@ enum Direction {
RIGHT
};
typedef enum {
SPLIT_VERTICAL,
SPLIT_HORIZ,
SPLIT_TAB
} SplitMode;
struct Monitor {
Display *display;
XineramaScreenInfo info;
Client *clients;
int *wss;
int selws;
int wscount;
Workspace *workspaces;
size_t selws;
size_t wscount;
};
struct Client {
int x;
int y;
int w;
int h;
int 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;
char name[WM_WS_NAME_LEN];
Monitor *monitor;
SplitMode split;
};
struct Arg {
@ -123,6 +125,8 @@ struct Wm {
Client root;
Window dock;
Client *focused_client;
// TODO: active layout not working
void (*active_layout)(Wm *wm, Monitor*);
float cfg_ms_p;
@ -145,9 +149,12 @@ 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);
void wm_switch_ws(Wm* wm, size_t ws);
void wm_mainloop(Wm* wm);
void wm_init(Wm *wm);
void wm_exit(Wm *wm);
@ -160,6 +167,7 @@ void wm_kb_kill(Wm *wm, Arg *args);
void wm_kb_switch_ws(Wm *wm, Arg *args);
void wm_kb_focus_dir(Wm *wm, Arg *args);
void wm_kb_exit(Wm* wm, Arg *args);
void wm_kb_switch_split_mode(Wm *wm, Arg *args);
void wm_keybinds_init_def(Wm *wm);