Compare commits

...

4 Commits

7 changed files with 504 additions and 197 deletions

View File

@ -20,6 +20,7 @@ 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/Xft/Xft.h>
#include <X11/Xlib.h>
#include <assert.h>
@ -158,6 +159,7 @@ void wm_client_handle_window_types(Wm *wm, Client *c, XMapRequestEvent e)
void wm_client_hide(Wm *wm, Client *c)
{
RETURN_IF_NULL(c);
DEBUG_PRINT("%s\n", __func__);
if (c->hidden)
return;
@ -165,11 +167,13 @@ void wm_client_hide(Wm *wm, Client *c)
c->hidden = true;
XUnmapWindow(wm->display, c->frame);
XUnmapWindow(wm->display, c->window);
XSync(wm->display, False);
}
void wm_client_show(Wm *wm, Client *c)
{
RETURN_IF_NULL(c);
DEBUG_PRINT("%s\n", __func__);
if (!c->hidden)
return;
@ -177,6 +181,7 @@ void wm_client_show(Wm *wm, Client *c)
c->hidden = false;
XMapWindow(wm->display, c->frame);
XMapWindow(wm->display, c->window);
XSync(wm->display, False);
}
void wm_client_focus(Wm *wm, Client *c)
@ -190,13 +195,35 @@ void wm_client_focus(Wm *wm, Client *c)
if (old_focused_client)
old_focused_client->focused = false;
XWindowAttributes att;
XGetWindowAttributes(wm->display, c->window, &att);
if (att.map_state == IsUnmapped)
XMapWindow(wm->display, c->window);
XGetWindowAttributes(wm->display, c->frame, &att);
if (att.map_state == IsUnmapped)
XMapWindow(wm->display, c->frame);
c->hidden = false;
XSetInputFocus(wm->display, c->window, RevertToPointerRoot, CurrentTime);
wm_client_set_atom(wm, c, "_NET_ACTIVE_WINDOW", (unsigned char*) &c->window,
XA_WINDOW, 1);
XSync(wm->display, False);
TreeNode *node = wm_treenode_ptr_find_client_node(c->ws->tree, c);
if (node && node->parent && node->parent->type == NODE_TAB) {
for (size_t i = 0; i < node->parent->children->size; i++) {
node->parent->children->nodes[i]->client->focused = false;
}
}
c->focused = true;
wm->focused_client = c;
TreeNode *node = wm_treenode_ptr_find_client_node(c->ws->tree, c);
c->ws->focused_node = node;
if (node && node->parent && node->parent->type == NODE_TAB) {
wm_layout(wm, c->ws->monitor);
}
wm_monitor_clients_border(wm, c->m);
wm_monitor_draw_all_clients_frame(wm, c->m);
}
@ -342,6 +369,37 @@ Client* wm_find_client_direction(Client *c, int dir)
if (!c)
return NULL;
TreeNode *node = wm_treenode_ptr_find_client_node(c->ws->tree, c);
if (node->parent && node->parent->type == NODE_TAB) {
TreeNode *parent = node->parent;
int client_index = INT_MAX;
for (int i = 0; i < parent->children->size; i++) {
if (parent->children->nodes[i]->client == c) {
client_index = i;
break;
}
}
assert(client_index != INT_MAX);
switch (dir) {
case LEFT:
if (client_index - 1 < 0)
return parent->children->nodes[parent->children->size-1]->client;
else
return parent->children->nodes[client_index - 1]->client;
break;
case RIGHT:
if (client_index + 1 >= parent->children->size)
return parent->children->nodes[0]->client;
else
return parent->children->nodes[client_index + 1]->client;
break;
}
}
Client *ret = NULL;
int x, y, dx, dy;
@ -416,6 +474,7 @@ Client* wm_client_get_focused(Wm *wm)
void wm_client_border(Wm *wm, Client *c)
{
if (wm->config.show_titlebar) return;
RETURN_IF_NULL(c);
XVisualInfo vinfo;
@ -444,6 +503,7 @@ void wm_client_border(Wm *wm, Client *c)
void wm_monitor_clients_border(Wm *wm, Monitor *m)
{
if (wm->config.show_titlebar) return;
DEBUG_PRINT("%s\n", __func__)
RETURN_IF_NULL(m);
NodeArray *clients = wm_treenode_find_client_nodes(m->workspaces[m->selected_ws].tree);
@ -482,6 +542,10 @@ void wm_client_draw_frame(Wm *wm, Client *c)
if (!wm->config.show_titlebar) return;
DEBUG_PRINT("%s\n", __func__);
TreeNode *node = wm_treenode_ptr_find_client_node(CURRENT_WS(wm).tree, c);
if (node->parent && node->parent->type == NODE_TAB) {
return;
}
int screen = DefaultScreen(wm->display);
Visual *visual = DefaultVisual(wm->display, screen);
@ -504,6 +568,31 @@ void wm_monitor_draw_all_clients_frame(Wm* wm, Monitor *m)
if (clients->size == 0)
return;
NodeArray *nodes = wm_all_nodes_to_array(m->workspaces[m->selected_ws].tree);
for (size_t i = 0; i < nodes->size; i++) {
TreeNode *node = nodes->nodes[i];
if (node->type != NODE_TAB) continue;
int screen = DefaultScreen(wm->display);
Visual *visual = DefaultVisual(wm->display, screen);
Colormap cmap = DefaultColormap(wm->display, screen);
for (size_t j = 0; j < node->children->size; j++) {
Client *client = node->children->nodes[j]->client;
if (client->ws->index != m->selected_ws) continue;
XftDraw *draw = XftDrawCreate(wm->display, node->tab_frame, visual, cmap);
XftDrawRect(draw, (wm_client_is_focused(wm, client) ? &wm->titlebar_focused_color :
&wm->titlebar_color), j * client->w / node->children->size, 0, client->w, client->h);
XftDrawStringUtf8(draw, &wm->titlebar_text_color, wm->titlebar_font,
20 + j * client->w / node->children->size, 30, (void*)client->name, strlen(client->name));
XftDrawDestroy(draw);
}
}
wm_nodearray_free(nodes);
for (size_t i = 0; i < clients->size; i++) {
Client *client = ((TreeNode*)clients->nodes[i])->client;
wm_client_draw_frame(wm, client);

View File

@ -104,13 +104,12 @@ void wm_maprequest_handler(Wm *wm, XMapRequestEvent e)
XSetWMName(wm->display, frame, &xtp);
XFree(xtp.value);
XMapWindow(wm->display, frame);
XMapWindow(wm->display, e.window);
c = wm_client_create(wm, e.window);
RETURN_IF_NULL(c);
c->frame = frame;
wm_client_show(wm, c);
wm_ws_tree_insert_client(wm, c->ws, c);
wm_client_handle_window_types(wm, c, e);

View File

@ -1,4 +1,5 @@
#include <X11/X.h>
#include <X11/extensions/Xinerama.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
@ -51,6 +52,31 @@ static void test_wm_nodearray_push(void **state)
wm_treenode_free(node1);
}
static void test_wm_nodearray_remove(void **state)
{
NodeArray *array = wm_nodearray_new();
assert_false(wm_nodearray_remove(array, 1));
wm_nodearray_push(array, (void*)1);
wm_nodearray_push(array, (void*)2);
wm_nodearray_push(array, (void*)3);
wm_nodearray_push(array, (void*)4);
wm_nodearray_push(array, (void*)5);
assert_int_equal(array->size, 5);
assert_true(wm_nodearray_remove(array, 2));
assert_int_equal(array->size, 4);
assert_ptr_equal(array->nodes[0], 1);
assert_ptr_equal(array->nodes[1], 2);
assert_ptr_equal(array->nodes[2], 4);
assert_ptr_equal(array->nodes[3], 5);
wm_nodearray_free(array);
}
static void test_wm_treenode_new(void **state)
{
TreeNode *node = wm_treenode_new(NODE_VERTICAL, NULL);
@ -437,6 +463,79 @@ static void test_wm_find_client_direction_split(void **state)
wm_treenode_free(client3);
}
static void test_wm_find_client_direction_tab(void **state)
{
TreeNode *root = wm_treenode_new(NODE_TAB, NULL);
TreeNode *client1 = wm_treenode_new(NODE_CLIENT, NULL);
TreeNode *client2 = wm_treenode_new(NODE_CLIENT, NULL);
wm_treenode_add_child(root, client1);
wm_treenode_add_child(root, client2);
root->pos = (Rect) {.w = 1600, .h = 900, .x = 0, .y = 0};
client1->pos = (Rect) {.w = 1600, .h = 900, .x = 0, .y = 0};
client2->pos = (Rect) {.w = 1600, .h = 900, .x = 0, .y = 0};
Workspace *w = calloc(1, sizeof(Workspace));
w->tree = root;
client1->client = calloc(1, sizeof(Client));
client2->client = calloc(1, sizeof(Client));
client1->client->name = "";
client2->client->name = "";
client1->client->ws = w;
client2->client->ws = w;
client1->client->w = client1->pos.w; client1->client->h = client1->pos.h;
client1->client->x = client1->pos.x; client1->client->y = client1->pos.y;
client2->client->w = client2->pos.w; client2->client->h = client2->pos.h;
client2->client->x = client2->pos.x; client2->client->y = client2->pos.y;
Monitor *m = calloc(1, sizeof(Workspace));
XineramaScreenInfo info;
info.width = 1600;
info.height = 900;
m->info = info;
client1->client->m = m;
client2->client->m = m;
Client *ret = wm_find_client_direction(client1->client, UP);
assert_ptr_equal(ret, NULL);
ret = wm_find_client_direction(client1->client, DOWN);
assert_ptr_equal(ret, NULL);
ret = wm_find_client_direction(client1->client, LEFT);
assert_ptr_equal(ret, client2->client);
ret = wm_find_client_direction(client1->client, RIGHT);
assert_ptr_equal(ret, client2->client);
ret = wm_find_client_direction(client2->client, UP);
assert_ptr_equal(ret, NULL);
ret = wm_find_client_direction(client2->client, DOWN);
assert_ptr_equal(ret, NULL);
ret = wm_find_client_direction(client2->client, LEFT);
assert_ptr_equal(ret, client1->client);
ret = wm_find_client_direction(client2->client, RIGHT);
assert_ptr_equal(ret, client1->client);
free(client1->client);
free(client2->client);
free(m);
free(w);
wm_treenode_free(root);
wm_treenode_free(client1);
wm_treenode_free(client2);
}
static void test_wm_ptrarray_2d_index(void **state)
{
@ -585,8 +684,16 @@ static void test_wm_read_log(void **state)
wm_uintarray_free(entries);
}
// todo
uint64_t simple_update_cost_function(TreeNode* node1, TreeNode* node2)
{
if (node1->type != node2->type) return 10;
if (node1->type != NODE_CLIENT) return 0;
if (strcmp(node1->client->name, node2->client->name) == 0)
return 0;
else
return 10;
}
@ -598,6 +705,10 @@ static void test_wm_logentries_calculate_distances(void **state)
TreeNode *node = wm_treenode_new(NODE_VERTICAL, NULL);
TreeNode *child = wm_treenode_new(NODE_CLIENT, NULL);
child->client = malloc(sizeof(Client));
child->client->name = "client";
wm_treenode_add_child(node, child);
wm_logentries_calculate_distances(entries, node, &simple_update_cost_function);
@ -607,6 +718,7 @@ static void test_wm_logentries_calculate_distances(void **state)
// TODO check node distances
wm_logentries_free(entries);
free(child->client);
wm_treenode_free(node);
wm_treenode_free(child);
}
@ -1024,6 +1136,26 @@ static void test_wm_tree_edit_distance(void **state)
TreeNode *b_child4 = wm_treenode_new(NODE_VERTICAL, NULL);
TreeNode *b_child5 = wm_treenode_new(NODE_VERTICAL, NULL);
/*
a
/ \
a1 a2
/ \
a3 a4
|
a5
*/
/*
b
/ \
b1 b2
|
b3
/ \
b4 b5
*/
wm_treenode_add_child(a_root, a_child1);
wm_treenode_add_child(a_root, a_child2);
@ -1041,11 +1173,11 @@ static void test_wm_tree_edit_distance(void **state)
size_t dist = wm_tree_edit_distance(a_root, b_root, (TreeEditDistanceCosts) {
.insert_cost = 10,
.remove_cost = 10,
.remove_cost = 20,
.update_cost_function = &simple_update_cost_function,
});
assert_int_equal(dist, 70);
assert_int_equal(dist, 30);
wm_treenode_free(a_root);
wm_treenode_free(a_child1);
@ -1276,6 +1408,7 @@ int main(void)
{
const struct CMUnitTest test_group1[] = {
cmocka_unit_test(test_wm_nodearray_push),
cmocka_unit_test(test_wm_nodearray_remove),
cmocka_unit_test(test_wm_treenode_new),
cmocka_unit_test(test_wm_treenode_split_space),
cmocka_unit_test(test_wm_all_nodes_to_array),
@ -1283,6 +1416,7 @@ int main(void)
cmocka_unit_test(test_wm_treenode_remove_client1),
cmocka_unit_test(test_wm_treenode_remove_client2),
cmocka_unit_test(test_wm_find_client_direction_split),
cmocka_unit_test(test_wm_find_client_direction_tab),
cmocka_unit_test(test_wm_ptrarray_2d_index),
cmocka_unit_test(test_wm_treenode_to_str),
cmocka_unit_test(test_wm_json_obj_to_treenode),

View File

@ -1,3 +1,4 @@
#include <X11/Xlib.h>
#include <fcntl.h>
#include <stdint.h>
#include <sys/stat.h>
@ -259,6 +260,9 @@ void wm_log_state(Wm *wm, const char *prefixstr, const char* logfile)
json_object *log_entry_obj = wm_state_to_json_object(wm);
LogEntry *new_entry = wm_json_obj_to_logentry(prefix, log_entry_obj);
wm_uintarray_push(wm->log_entries, (uint64_t)new_entry);
int fd = -1;
struct json_object *jobj = NULL;
@ -326,10 +330,6 @@ void wm_log_state(Wm *wm, const char *prefixstr, const char* logfile)
goto ret;
}
// TODO
wm_logentries_free(wm->log_entries);
wm->log_entries = wm_read_log(logfile);
ret:
json_object_put(jobj);
}
@ -607,8 +607,8 @@ void wm_logentries_calculate_distances(UIntArray *log_entries, TreeNode *curr_tr
UpdateCostFunction update_cost_function)
{
TreeEditDistanceCosts costs = (TreeEditDistanceCosts) {
.insert_cost = 3,
.remove_cost = 100,
.insert_cost = 10,
.remove_cost = 20,
.update_cost_function = update_cost_function,
};
@ -627,7 +627,7 @@ void wm_logentries_calculate_distances(UIntArray *log_entries, TreeNode *curr_tr
void wm_treenode_print(TreeNode *node)
{
char *str = wm_treenode_to_str(node);
DEBUG_PRINT("%s\n", str);
fprintf(stderr, "%s\n", str);
free(str);
}
@ -749,7 +749,6 @@ static void recursive_mkdir(char *path, mode_t mode)
void wm_logfile_init(const char *path)
{
if (access(path, F_OK) == 0) return;
char *dup = strdup(path);
@ -783,9 +782,73 @@ void wm_logfile_init(const char *path)
free(dup);
}
LogEntry *wm_json_obj_to_logentry(const char *key, struct json_object *jobj)
{
LogEntry *ret = calloc(1, sizeof(LogEntry));
ret->workspaces = wm_nodearray_new();
struct json_object_iterator ws_iter = json_object_iter_begin(jobj);
struct json_object_iterator ws_iter_end = json_object_iter_end(jobj);
const char *ws_name = NULL;
struct json_object *ws_value = NULL;
int i = 0;
char *dup = strdup(key);
char *token = strtok((char*)dup, ":");
while(token != NULL) {
i++;
switch (i) {
case 1:
// TODO
ret->timestamp = atol(token);
break;
case 2:
strncpy(ret->function_name, token, 128);
break;
case 3:
if (strncmp(token, "after", strlen("after")) == 0)
ret->after = true;
else {
fprintf(stderr, "wm: encountered invalid token %s\n. exiting.",
token);
abort();
}
break;
default:
fprintf(stderr, "wm: encountered invalid token %s\n. exiting.",
token);
abort();
}
token = strtok(NULL, ":");
}
while (!json_object_iter_equal(&ws_iter, &ws_iter_end)) {
ws_name = json_object_iter_peek_name(&ws_iter);
ws_value = json_object_iter_peek_value(&ws_iter);
int ws_index = atoi(ws_name);
while (ret->workspaces->size < ws_index) {
wm_nodearray_push(ret->workspaces, NULL);
}
wm_nodearray_push(ret->workspaces, wm_json_obj_to_treenode(ws_value));
json_object_iter_next(&ws_iter);
}
free(dup);
return ret;
}
/* UIntArray<LogEntry*> */
UIntArray* wm_read_log(const char* filename)
{
DEBUG_PRINT("%s\n", __func__);
_Static_assert(sizeof(LogEntry*) == sizeof(uint64_t), "");
// TODO maybe prealloc
UIntArray* ret = wm_uintarray_new();
@ -814,67 +877,11 @@ UIntArray* wm_read_log(const char* filename)
struct json_object_iterator iter = json_object_iter_begin(jobj);
struct json_object_iterator iter_end = json_object_iter_end(jobj);
const char *ws_name = NULL;
struct json_object *ws_value = NULL;
struct json_object_iterator ws_iter = json_object_iter_init_default();
struct json_object_iterator ws_iter_end = json_object_iter_init_default();
while (!json_object_iter_equal(&iter, &iter_end)) {
// TODO maybe prealloc
LogEntry *entry = calloc(1, sizeof(LogEntry));
entry->workspaces = wm_nodearray_new();
name = json_object_iter_peek_name(&iter);
value = json_object_iter_peek_value(&iter);
int i = 0;
char *token = strtok((char*)name, ":");
while(token != NULL) {
i++;
switch (i) {
case 1:
// TODO
entry->timestamp = atol(token);
break;
case 2:
strncpy(entry->function_name, token, 128);
break;
case 3:
if (strncmp(token, "after", strlen("after")) == 0)
entry->after = true;
else {
fprintf(stderr, "wm: encountered invalid token %s\n. exiting.",
token);
abort();
}
break;
default:
fprintf(stderr, "wm: encountered invalid token %s\n. exiting.",
token);
abort();
}
token = strtok(NULL, ":");
}
ws_iter = json_object_iter_begin(value);
ws_iter_end = json_object_iter_end(value);
while (!json_object_iter_equal(&ws_iter, &ws_iter_end)) {
ws_name = json_object_iter_peek_name(&ws_iter);
ws_value = json_object_iter_peek_value(&ws_iter);
int ws_index = atoi(ws_name);
while (entry->workspaces->size < ws_index) {
wm_nodearray_push(entry->workspaces, NULL);
}
wm_nodearray_push(entry->workspaces, wm_json_obj_to_treenode(ws_value));
json_object_iter_next(&ws_iter);
}
LogEntry *entry = wm_json_obj_to_logentry(name, value);
wm_uintarray_push(ret, (uint64_t)entry);
json_object_iter_next(&iter);
@ -1041,6 +1048,34 @@ TreeNode* wm_treenode_remove_client(Wm *wm, TreeNode *root, Client *client)
TreeNode *node = client_node->parent;
if (node->type == NODE_TAB) {
size_t client_node_index = wm_treenode_find_index(node, client_node);
assert(client_node_index != SIZE_MAX);
wm_treenode_free(client_node);
wm_nodearray_remove(node->children, client_node_index);
if (node->children->size == 0) {
XDestroyWindow(wm->display, node->tab_frame);
// node is root node, dont free it
if (!node->parent) return NULL;
wm_treenode_free(node);
size_t node_index = wm_treenode_find_index(node->parent, node);
if (node_index != SIZE_MAX)
wm_nodearray_remove(node->parent->children, node_index);
return NULL;
}
return node->children->nodes[0];
}
for (size_t i = 0; i < node->children->size; i++) {
if (node->children->nodes[i]->type == NODE_CLIENT &&
node->children->nodes[i]->client != client) {
@ -1184,9 +1219,6 @@ 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;
@ -1195,11 +1227,24 @@ bool wm_nodearray_remove(NodeArray *arr, size_t index)
return true;
}
memmove(arr->nodes + index, arr->nodes + index+1, arr->size-1 * sizeof(TreeNode*));
arr->size--;
if (arr->size == 0) return true;
memmove(arr->nodes + index, arr->nodes + index + 1, (arr->size - index) * sizeof(TreeNode*));
return true;
}
bool wm_nodearray_contains(NodeArray *arr, TreeNode *node)
{
for (size_t i = 0; i < arr->size; i++)
if (arr->nodes[i] == node)
return true;
return false;
}
void wm_nodearray_free(NodeArray *arr)
{
assert(arr);
@ -1443,6 +1488,14 @@ NodeArray *wm_treenode_find_client_nodes(TreeNode *root)
return ret;
}
size_t wm_treenode_find_index(TreeNode *parent, TreeNode* child)
{
for (size_t i = 0; i < parent->children->size; i++)
if (parent->children->nodes[i] == child) return i;
return SIZE_MAX;
}
NodeArray* wm_all_nodes_to_array(TreeNode *root)
{
DEBUG_PRINT("%s\n", __func__);

View File

@ -1,6 +1,7 @@
#ifndef UTIL_H
#define UTIL_H
#include <X11/X.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
@ -55,6 +56,8 @@ struct TreeNode {
Client *client;
unsigned int id;
uint64_t distance;
Window tab_frame;
};
typedef struct {
@ -113,6 +116,11 @@ void wm_nodearray_clear(NodeArray *arr);
*/
bool wm_nodearray_remove(NodeArray *arr, size_t index);
/**
* Returns true if `arr` contains `node`.
*/
bool wm_nodearray_contains(NodeArray *arr, TreeNode *node);
/**
* Frees the array.
*/
@ -276,6 +284,12 @@ TreeNode* wm_treenode_ptr_find_client_node(TreeNode *root, Client *client);
*/
NodeArray* wm_treenode_find_client_nodes(TreeNode *root);
/**
* Searches for `child` in `parent->children`, and returns the index of `child`.
* If child could not be found, returns `SIZE_MAX`.
*/
size_t wm_treenode_find_index(TreeNode *parent, TreeNode* child);
/**
* Returns the sibling node of `node`.
* Returns NULL on error.
@ -372,6 +386,9 @@ void wm_logentries_free(UIntArray *entries);
* array with the parsed values.
* Return type: UIntArray<LogEntry*>
*/
LogEntry *wm_json_obj_to_logentry(const char *key, struct json_object *jobj);
/* UIntArray<LogEntry*> */
UIntArray* wm_read_log(const char* filename);
/**

240
src/wm.c
View File

@ -165,6 +165,28 @@ bool wm_window_is_dock(Wm *wm, Window w)
return false;
}
void wm_node_set_up_tab(Wm *wm, TreeNode *node, Client *client)
{
assert(node);
assert(client);
node->type = NODE_TAB;
node->client = NULL;
assert(node->children->size == 0);
node->tab_frame = XCreateSimpleWindow(wm->display, wm->root.window,
node->pos.x, node->pos.y, node->pos.w, node->pos.h, 0, 0, 0);
XReparentWindow(wm->display, client->frame, node->tab_frame, 0, 0);
XMapWindow(wm->display, node->tab_frame);
TreeNode *client_new_node = wm_treenode_new(NODE_CLIENT, node);
wm_treenode_add_child(node, client_new_node);
client_new_node->client = client;
client_new_node->pos = node->pos;
wm_client_focus(wm, client);
client->focused = true;
}
NodeType wm_split_to_nodetype(SplitMode mode)
{
switch (mode) {
@ -181,6 +203,18 @@ NodeType wm_split_to_nodetype(SplitMode mode)
}
}
static uint64_t simple_update_cost_function(TreeNode* node1, TreeNode* node2)
{
if (node1->type != node2->type) return 10;
if (node1->type != NODE_CLIENT) return 0;
if (strcmp(node1->client->name, node2->client->name) == 0)
return 0;
else
return 10;
}
void wm_ws_tree_insert_client(Wm *wm, Workspace *ws, Client *client)
{
DEBUG_PRINT("%s\n", __func__);
@ -194,6 +228,12 @@ void wm_ws_tree_insert_client(Wm *wm, Workspace *ws, Client *client)
}
if (wm_treenode_is_empty(ws->tree) && ws->tree->client == NULL) {
if (ws->split == SPLIT_TAB) {
wm_node_set_up_tab(wm, ws->tree, client);
return;
}
ws->tree->type = NODE_CLIENT;
ws->tree->client = client;
ws->tree->pos = (Rect) {
@ -204,10 +244,22 @@ void wm_ws_tree_insert_client(Wm *wm, Workspace *ws, Client *client)
return;
}
wm_treenode_print(CURRENT_WS(wm).tree);
TreeNode *focused_node = ws->focused_node ? ws->focused_node :
wm_treenode_ptr_find_focused_client_node(ws->tree);
assert(focused_node);
if (focused_node->parent && focused_node->parent->type == NODE_TAB) {
TreeNode *child = wm_treenode_new(NODE_CLIENT, focused_node->parent);
child->pos = focused_node->pos;
child->client = client;
XReparentWindow(wm->display, client->frame, focused_node->parent->tab_frame, 0, 0);
wm_nodearray_push(focused_node->parent->children, child);
wm_client_focus(wm, client);
return;
}
Client *old_client = focused_node->client;
focused_node->type = wm_split_to_nodetype(ws->split);
@ -228,18 +280,29 @@ void wm_switch_ws(Wm *wm, size_t ws)
if (ws > wm->selected_monitor->wscount)
return;
int wscc = 0;
Client *c;
DEBUG_PRINT("switching to ws %ld, clients:", ws);
wm_treenode_print(wm->selected_monitor->workspaces[ws].tree);
NodeArray *current_ws_clients = wm_treenode_find_client_nodes(CURRENT_WS(wm).tree);
DEBUG_PRINT("current ws clients: %ld\n", current_ws_clients->size);
for (size_t i = 0; i < current_ws_clients->size; i++) {
c = ((TreeNode**)current_ws_clients->nodes)[i]->client;
assert(c);
if (c->ws->index == wm->selected_monitor->selected_ws) {
TreeNode *node = wm_treenode_ptr_find_client_node(c->ws->tree, c);
if (node && node->parent && node->parent->type == NODE_TAB) {
DEBUG_PRINT("tab_frame %ld\n", node->parent->tab_frame);
XWindowAttributes att;
XGetWindowAttributes(wm->display, node->parent->tab_frame, &att);
if (att.map_state == IsUnmapped) continue;
XUnmapWindow(wm->display, node->parent->tab_frame);
XSync(wm->display, False);
}
DEBUG_PRINT("wm_switch_ws hide client selws: %ld\n", c->ws->index)
wm_client_hide(wm, c);
}
@ -251,17 +314,22 @@ void wm_switch_ws(Wm *wm, size_t ws)
(unsigned char*) &(ws), XA_CARDINAL, 1);
NodeArray *switch_ws_clients = wm_treenode_find_client_nodes(CURRENT_WS(wm).tree);
NodeArray *mapped_tab_nodes = wm_nodearray_new();
for (size_t i = 0; i < switch_ws_clients->size; i++) {
c = ((TreeNode**)switch_ws_clients->nodes)[i]->client;
if (c->ws->index == wm->selected_monitor->selected_ws) {
wscc++;
TreeNode *node = switch_ws_clients->nodes[i];
c = node->client;
if (c->ws->index != wm->selected_monitor->selected_ws) continue;
if (node->parent && node->parent->type == NODE_TAB &&
!wm_nodearray_contains(mapped_tab_nodes, node->parent)) {
XMapWindow(wm->display, node->parent->tab_frame);
XSync(wm->display, False);
} else {
wm_client_show(wm, c);
}
}
DEBUG_PRINT("wm_switch_ws focused ws client count: %d\n", wscc);
if (CURRENT_WS(wm).focused_node)
wm_client_focus(wm, CURRENT_WS(wm).focused_node->client);
@ -311,6 +379,39 @@ void wm_layout(Wm *wm, Monitor *m)
Client *client = node->client;
assert(client);
if (client->ws->index != m->selected_ws) continue;
if (node->parent && node->parent->type == NODE_TAB) {
for (size_t j = 0; j < node->parent->children->size; j++) {
TreeNode *child_node = node->parent->children->nodes[j];
child_node->client->x = node->pos.x;
child_node->client->y = node->pos.y;
child_node->client->w = node->pos.w;
child_node->client->h = node->pos.h;
unsigned int value_mask = CWX | CWY | CWWidth | CWHeight;
XWindowChanges xwc = wm_client_to_xwchanges(child_node->client);
xwc.y = wm->titlebar_font->height * 2;
xwc.x = 0;
xwc.height -= wm->titlebar_font->height * 2;
XConfigureWindow(wm->display, child_node->client->frame, value_mask, &xwc);
xwc.y = 0;
xwc.x = 0;
XConfigureWindow(wm->display, child_node->client->window, value_mask, &xwc);
if (wm_client_is_focused(wm, child_node->client)) {
wm_client_show(wm, child_node->client);
} else {
wm_client_hide(wm, child_node->client);
}
XSync(wm->display, False);
}
wm_monitor_draw_all_clients_frame(wm, client->m);
continue;
}
client->x = node->pos.x;
client->y = node->pos.y;
client->w = node->pos.w;
@ -396,6 +497,8 @@ void wm_mainloop(Wm *wm)
case MapRequest:
DEBUG_PRINT("MapRequest: %d\n", (int)e.xmaprequest.window)
wm_maprequest_handler(wm, e.xmaprequest);
case UnmapNotify:
DEBUG_PRINT("UnmapNotify: %d\n", (int)e.xmaprequest.window)
break;
default:
DEBUG_PRINT("default: %d\n", e.type);
@ -431,9 +534,6 @@ void wm_init(Wm *wm)
char *display_string = DisplayString(wm->display);
setenv("DISPLAY", display_string, 1);
// TODO: only testing
// wm_socket_init();
wm->root.window = XRootWindow(wm->display, DefaultScreen(wm->display));
DEBUG_PRINT("root window: %ld\n", wm->root.window);
@ -508,9 +608,6 @@ void wm_exit(Wm *wm)
XUngrabKey(wm->display, AnyKey, AnyModifier, wm->root.window);
// TODO: perhaps add created clients to a PtrArray field in workspace.
// would reduce number of calls to wm_treenode_find_client_nodes_ptr
for (size_t i = 0; i < wm->monitors_len; i++) {
for (size_t j = 0; j < wm->monitors[i].wscount; j++) {
NodeArray *clients = wm_treenode_find_client_nodes(
@ -585,7 +682,7 @@ void wm_kb_spawn(Wm *wm, Arg *args)
DEBUG_PRINT("args sl 0: %s\n", args->sl[0])
DEBUG_PRINT("args count: %d\n", args->count)
WM_LOG_STATE_START(wm);
// WM_LOG_STATE_START(wm);
if (args->sl[args->count-1] != NULL) {
fprintf(stderr, "%s recieved non null-terminated args. Returning\n",
@ -615,7 +712,7 @@ void wm_kb_exit(Wm* wm, Arg *args)
void wm_kb_kill(Wm *wm, Arg *args)
{
WM_LOG_STATE_START(wm);
// WM_LOG_STATE_START(wm);
Client *c;
@ -631,18 +728,18 @@ void wm_kb_switch_ws(Wm *wm, Arg *args)
{
RETURN_IF_NULL(args)
WM_LOG_STATE_START(wm);
// WM_LOG_STATE_START(wm);
wm_switch_ws(wm, args->i);
WM_LOG_STATE_END(wm);
// WM_LOG_STATE_END(wm);
}
void wm_kb_move_client_ws(Wm *wm, Arg *args)
{
RETURN_IF_NULL(args)
WM_LOG_STATE_START(wm);
// WM_LOG_STATE_START(wm);
wm_move_client_ws(wm, args->i);
@ -654,13 +751,13 @@ void wm_kb_focus_dir(Wm *wm, Arg *args)
Client *c;
RETURN_IF_NULL(args)
WM_LOG_STATE_START(wm);
// WM_LOG_STATE_START(wm);
c = wm_client_get_focused(wm);
wm_client_focus_dir(wm, c, args->i);
WM_LOG_STATE_END(wm);
// WM_LOG_STATE_END(wm);
}
void wm_kb_move_dir(Wm *wm, Arg *args)
@ -668,7 +765,7 @@ void wm_kb_move_dir(Wm *wm, Arg *args)
Client *c;
RETURN_IF_NULL(args)
WM_LOG_STATE_START(wm);
// WM_LOG_STATE_START(wm);
c = wm_client_get_focused(wm);
@ -679,95 +776,24 @@ void wm_kb_move_dir(Wm *wm, Arg *args)
void wm_kb_switch_split_mode(Wm *wm, Arg *args)
{
DEBUG_PRINT("%s\n", __func__);
RETURN_IF_NULL(args)
WM_LOG_STATE_START(wm);
// WM_LOG_STATE_START(wm);
wm->selected_monitor->workspaces[wm->selected_monitor->selected_ws].split = args->i;
CURRENT_WS(wm).split = args->i;
WM_LOG_STATE_END(wm);
}
struct sockaddr wm_socket_init(Wm *wm)
{
struct sockaddr_un sock_addr;
int ret;
ret = wm->socket_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (ret == -1) {
fprintf(stderr, "wm: could not create socket. Exiting.\n");
exit(EXIT_FAILURE);
}
sock_addr.sun_family = AF_UNIX;
snprintf(sock_addr.sun_path, sizeof(sock_addr.sun_path),
"/tmp/wm_socket_%c", DisplayString(wm->display)[1]);
ret = bind(wm->socket_fd, (struct sockaddr *) &sock_addr, sizeof(sock_addr));
if (ret == -1) {
fprintf(stderr, "wm: could not bind to socket. Exiting.\n");
exit(EXIT_FAILURE);
}
ret = listen(wm->socket_fd, 512);
if (ret == -1) {
fprintf(stderr, "wm: could not listen to socket. Exiting.\n");
exit(EXIT_FAILURE);
}
}
void wm_socket_cleanup(Wm *wm)
{
struct sockaddr s;
socklen_t t = 108;
getsockname(wm->socket_fd, &s, &t);
DEBUG_PRINT("socket cleanup path: %s\n", s.sa_data)
remove(s.sa_data);
}
void wm_sigint_handler(Wm *wm, int signum)
{
wm_socket_cleanup(wm);
exit(1);
}
// TODO: maybe move some parts to wmc
void wm_settings_message_parse(Wm *wm, char *c, int len)
{
if (len <= 0)
return;
int op; /* 0 = set, 1 = get */
size_t i = 0;
char *token;
char tokens[20][50];
char *settings[] = {"border-width", "border-color", "focused-border-color",
"msfact", "focus-on-motion"};
while((token = strtok(c, " ")) != NULL) {
DEBUG_PRINT("message_parse token: %s\n", token)
strncpy(tokens[i], token, 50);
}
if (strcmp(tokens[1], "set") == 0) {
op = 0;
} else if (strcmp(c, "get") == 0) {
op = 1;
} else {
return;
}
for (i = 0; i < sizeof(settings)/sizeof(char*); i++) {
if (strcmp(tokens[1], settings[i])) {
if (op == 1) {
// wm_settings_get(settings[i], &ret);
} else {
// wm_settings_set(settings[i]);
}
}
}
if (args->i == SPLIT_TAB) {
TreeNode *focused_node = CURRENT_WS(wm).focused_node ? CURRENT_WS(wm).focused_node :
wm_treenode_ptr_find_focused_client_node(CURRENT_WS(wm).tree);
if (!focused_node) return;
if (focused_node->parent && focused_node->parent->type == NODE_TAB) return;
Client *client = focused_node->client;
wm_node_set_up_tab(wm, focused_node, client);
wm_layout(wm, CURRENT_WS(wm).monitor);
}
// WM_LOG_STATE_END(wm);
}

View File

@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef WM_H
#define WM_H
#include <X11/Xft/Xft.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
@ -40,17 +41,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "config.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
// TODO: get_atom probably not working correctly
// TODO: important: code cleanliness, ewmh!!
// TODO: config command line utility(sockets), maybe avoid global vars
#define RETURN_IF_NULL(c) \
if (c == NULL) \
{ \
fprintf(stderr, "wm: variable %s was null in function %s\n", #c, __func__); \
DEBUG_PRINT("wm: variable %s was null in function %s\n", #c, __func__); \
return; \
}
@ -149,6 +143,7 @@ void wm_mstack(Wm *wm, Monitor *m);
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);
void wm_node_set_up_tab(Wm *wm, TreeNode *node, Client *client);
NodeType wm_split_to_nodetype(SplitMode mode);
void wm_ws_tree_insert_client(Wm *wm, Workspace *ws, Client *client);
@ -171,10 +166,4 @@ void wm_kb_move_dir(Wm *wm, Arg *args);
void wm_kb_exit(Wm* wm, Arg *args);
void wm_kb_switch_split_mode(Wm *wm, Arg *args);
struct sockaddr wm_socket_init(Wm *wm);
void wm_socket_cleanup(Wm *wm);
void wm_settings_message_parse(Wm *wm, char *c, int len);
void wm_sigint_handler(Wm *wm, int signum);
#endif