move source files to src/ directory

This commit is contained in:
2023-07-02 23:36:22 +02:00
parent 6778471950
commit 784fd89cf8
11 changed files with 4 additions and 3 deletions

490
src/client.c Normal file
View File

@@ -0,0 +1,490 @@
/*
The GPLv3 License (GPLv3)
Copyright (c) 2022 Akos Horvath
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "client.h"
#include "util.h"
#include "wm.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
// TODO
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.border_width = 0;
// ch.stack_mode = Above;
return ch;
}
Client* wm_client_find(Wm *wm, Window w)
{
Client *c;
for (c = wm->smon->clients; c; c = c->next) {
if (c->window == w)
return c;
}
return NULL;
}
Client* wm_client_create(Wm *wm, Window w)
{
DEBUG_PRINT("%s\n", __func__);
Client *t;
XTextProperty xtp;
int strln;
XGetWMName(wm->display, w, &xtp);
Client *c = malloc(sizeof(Client));
c->next = NULL;
c->prev = NULL;
c->window = w;
c->m = wm->smon;
c->ws = &wm->smon->workspaces[wm->smon->selws];
c->hidden = true;
c->is_floating = false;
if (xtp.value) {
strln = strlen((char*)xtp.value);
DEBUG_PRINT("%s allocating %d bytes for c->name\n", __func__, strln)
c->name = malloc(strln+1);
strcpy(c->name, (char*)xtp.value);
}
if (wm->smon->clients == NULL) {
wm->smon->clients = c;
} else {
t = wm_get_last_client(wm, *c->m);
//c = &root;
t->next = c;
c->prev = t;
c->next = NULL;
}
XSelectInput(wm->display, c->window, FocusChangeMask | EnterWindowMask |
PointerMotionMask);
wm_client_set_atom(wm, c, "_NET_WM_DESKTOP", (unsigned char*)&c->ws, XA_CARDINAL, 1);
return c;
}
void wm_client_handle_window_types(Wm *wm, Client *c, XMapRequestEvent e)
{
DEBUG_PRINT("%s\n", __func__);
RETURN_IF_NULL(c);
unsigned char *prop_ret;
unsigned long nitems_ret;
Atom type;
bool is_normal = false;
type = wm_client_get_atom(wm, c, "_NET_WM_WINDOW_TYPE", &prop_ret, &nitems_ret);
if (type == ULONG_MAX) {
fprintf(stderr, "wm_client_get_atom failed\n");
return;
}
for (size_t i = 0; i < nitems_ret; i++) {
DEBUG_PRINT("ConfigureRequest handler: window %d has property %s\n",
(int)e.window, XGetAtomName(wm->display, ((Atom*)prop_ret)[i]));
if (strcmp(XGetAtomName(wm->display, ((Atom*)prop_ret)[i]),
"_NET_WM_WINDOW_TYPE_NORMAL") == 0) {
is_normal = true;
} else if (strcmp(XGetAtomName(wm->display, ((Atom*)prop_ret)[i]),
"_NET_WM_WINDOW_TYPE_DOCK") == 0) {
// wm->dock = e.window;
fprintf(stderr, "client should not have _NET_WM_WINDOW_TYPE_DOCK type\n");
// todo function to exit
exit(1);
} else if (strcmp(XGetAtomName(wm->display, ((Atom*)prop_ret)[i]),
"_NET_WM_WINDOW_TYPE_DIALOG") == 0) {
c->is_floating = true;
}
}
if (!is_normal) {
DEBUG_PRINT("configure handler: window %d is not normal\n",
(int)e.window)
// wm_client_kill(wm, c);
return;
}
}
void wm_client_hide(Wm *wm, Client *c)
{
RETURN_IF_NULL(c);
if (c->hidden)
return;
c->hidden = true;
XUnmapWindow(wm->display, c->window);
}
void wm_client_show(Wm *wm, Client *c)
{
RETURN_IF_NULL(c);
if (!c->hidden)
return;
c->hidden = false;
XMapWindow(wm->display, c->window);
}
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);
c->focused = true;
wm->focused_client = c;
wm_monitor_clients_border(wm, c->m);
}
void wm_client_focus_dir(Wm *wm, Client *c, int dir)
{
RETURN_IF_NULL(c);
switch (dir) {
case UP:
wm_client_focus(wm, wm_client_get_dir_rel_c(c, dir));
break;
case DOWN:
wm_client_focus(wm, wm_client_get_dir_rel_c(c, dir));
break;
case LEFT:
wm_client_focus(wm, wm_client_get_dir_rel_c(c, dir));
break;
case RIGHT:
wm_client_focus(wm, wm_client_get_dir_rel_c(c, dir));
break;
default:
fprintf(stderr, "wm: %s invalid direction: %d\n", __func__, dir);
return;
}
}
void wm_client_free(Wm *wm, Client *c)
{
DEBUG_PRINT("%s\n", __func__);
RETURN_IF_NULL(c);
if (c == wm->smon->clients) {
if (c->next)
wm->smon->clients = c->next;
else
wm->smon->clients = NULL;
} else {
if (!c->next)
c->prev->next = NULL;
else {
c->prev->next = c->next;
c->next->prev = c->prev;
}
}
}
void wm_client_kill(Wm *wm, Client *c)
{
DEBUG_PRINT("%s\n", __func__);
RETURN_IF_NULL(c);
Monitor *m;
m = c->m;
XEvent event;
Atom delete_atom = XInternAtom(wm->display, "WM_DELETE_WINDOW", False);
event.type = ClientMessage;
event.xclient.window = c->window;
event.xclient.display = wm->display;
event.xclient.message_type = delete_atom;
event.xclient.format = 32;
event.xclient.data.l[0] = delete_atom;
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,
Atom type, int nelements)
{
RETURN_IF_NULL(c);
RETURN_IF_NULL(name);
RETURN_IF_NULL(data);
XChangeProperty(wm->display, c->window, XInternAtom(wm->display, name, False),
type, 32, PropModeReplace, data, nelements);
}
// probably not working
Atom wm_client_get_atom(Wm *wm, Client *c, const char *name, unsigned char **atom_ret,
unsigned long *nitems_ret)
{
if (!c)
return ULONG_MAX;
if (!name)
return ULONG_MAX;
if (!atom_ret)
return ULONG_MAX;
if (!nitems_ret)
return ULONG_MAX;
Atom type_ret;
int format_ret;
unsigned long bytes_remain_ret;
int ret;
ret = XGetWindowProperty(wm->display, c->window,
XInternAtom(wm->display, name, False), 0, (~0L), False,
AnyPropertyType, &type_ret,
&format_ret, nitems_ret, &bytes_remain_ret, atom_ret);
if (ret != Success || (nitems_ret == 0 && type_ret == 0)) {
fprintf(stderr, "wm: XGetWindowProperty failed\n");
return ULONG_MAX;
}
DEBUG_PRINT("XGetWindowProperty return values, window %d:\n", (int)c->window);
DEBUG_PRINT("actual type return: %d\n", (int)type_ret);
DEBUG_PRINT("actual format return: %d\n", format_ret)
DEBUG_PRINT("actual number of items return: %ld\n", *nitems_ret)
DEBUG_PRINT("bytes remaining: %ld\n", *nitems_ret)
for (int i = 0; i < *nitems_ret; i++) {
printf("property return str: %s\n",
XGetAtomName(wm->display, ((Atom*)*atom_ret)[i]));
}
return type_ret;
}
Client* wm_client_get_dir_rel_c(Client *c, int dir)
{
DEBUG_PRINT("%s c: %p\n", __func__, c)
if (!c)
return NULL;
Client *r;
int x, y, dx, dy;
switch (dir) {
case UP:
x = c->x + c->w/2;
y = c->y;
dx = 0;
dy = -1;
break;
case DOWN:
x = c->x + c->w/2;
y = c->y + c->h;
dx = 0;
dy = 1;
break;
case LEFT:
x = c->x;
y = c->y + c->h/2;
dx = -1;
dy = 0;
break;
case RIGHT:
x = c->x + c->w;
y = c->y + c->h/3;
dx = 1;
dy = 0;
break;
default:
fprintf(stderr, "wm: %s invalid direction: %d\n", __func__, 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 (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)
DEBUG_PRINT("%d\n", dir)
return r;
}
}
// TODO: bar
} while((x > 0 && x < c->m->info.width) &&
(y > 0 && y < c->m->info.height));
return r;
}
Client* wm_client_get_focused(Wm *wm)
{
Client *c = NULL;
Workspace *ws = &wm->smon->workspaces[wm->smon->selws];
TreeNode *node = wm_treenode_ptr_find_focused_client_node(&ws->tree);
assert(node);
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);
XVisualInfo vinfo;
XColor color;
XColor fcolor;
Colormap cmap;
XMatchVisualInfo(wm->display, DefaultScreen(wm->display), 32,
TrueColor, &vinfo);
cmap = XCreateColormap(wm->display, c->window, vinfo.visual, AllocNone);
XParseColor(wm->display, cmap, wm->cfg_focused_border_col, &fcolor);
XParseColor(wm->display, cmap, wm->cfg_border_col, &color);
XAllocColor(wm->display, cmap, &fcolor);
XAllocColor(wm->display, cmap, &color);
// DEBUG_PRINT("wm_client_border c: 0x%x\n", c)
if (c) {
// DEBUG_PRINT("wm_client_border window: %d\n", c->window)
}
XSetWindowBorderWidth(wm->display, c->window, wm->cfg_border_width);
// DEBUG_PRINT("drawing border for: %d\n", c->window);
if (wm_client_is_focused(wm, c)) {
DEBUG_PRINT("drawing focused border for: %d\n", (int)c->window);
XSetWindowBorder(wm->display, c->window, fcolor.pixel);
}
else
XSetWindowBorder(wm->display, c->window, color.pixel);
}
// FIXME probably unused
// void wm_client_border_focused(Client *c)
// {
// RETURN_IF_NULL(c);
// // if (!wm_client_is_focused(c))
// // return;
// XVisualInfo vinfo;
// XColor fcolor;
// Colormap cmap;
//
// XMatchVisualInfo(wm->display, DefaultScreen(wm->display), 32,
// TrueColor, &vinfo);
//
// cmap = XCreateColormap(wm->display, c->window, vinfo.visual, AllocNone);
// XParseColor(wm->display, cmap, wm->cfg_focused_border_col, &fcolor);
// XAllocColor(wm->display, cmap, &fcolor);
//
// XSetWindowBorderWidth(wm->display, c->window, wm->cfg_border_width);
//
// DEBUG_PRINT("drawing border for focused window: %d\n", (int)c->window);
// XSetWindowBorder(wm->display, c->window, fcolor.pixel);
// }
void wm_monitor_clients_border(Wm *wm, Monitor *m)
{
DEBUG_PRINT("%s\n", __func__)
RETURN_IF_NULL(m);
Client *c;
for (c = wm->smon->clients; c; c = c->next) {
if (c->window != wm->root.window) {
DEBUG_PRINT("monitor border c: %p\n", c)
DEBUG_PRINT("monitor border c window: %ld\n", c->window)
wm_client_border(wm, c);
}
}
}
bool wm_client_is_focused(Wm *wm, Client *c)
{
if (c == NULL)
return false;
int prop;
// prop = XGetWindowProperty(wm->display, c->window,
// XInternAtom(wm->display, "_NET_ACTIVE_WINDOW", False), 0, 1, False,
// XInternAtom(wm->display, "_NET_ACTIVE_WINDOW", False), XA_WINDOW,
// XA_WINDOW, unsigned long *, unsigned long *, unsigned char **)
// XChangeProperty(wm->display, root.window,
// XInternAtom(wm->display, "_NET_ACTIVE_WINDOW", False),
// 33, 32, PropModeReplace, (unsigned char *) &c->window, 1);
Client *c1;
Window w;
int r;
XGetInputFocus(wm->display, &w, &r);
//DEBUG_PRINT("is_focused focused window: %d\n", w)
if (w == c->window) {
// DEBUG_PRINT("IS_FOCUSED returning true for client: 0x%x", c)
// DEBUG_PRINT("window: %d\n", c->window)
return true;
}
// DEBUG_PRINT("IS_FOCUSED returning false for client: 0x%x", c)
// DEBUG_PRINT("window: %d\n", c->window)
return false;
}

75
src/client.h Normal file
View File

@@ -0,0 +1,75 @@
/*
The GPLv3 License (GPLv3)
Copyright (c) 2022 Akos Horvath
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CLIENT_H
#define CLIENT_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 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);
void wm_client_handle_window_types(Wm *wm, Client *c, XMapRequestEvent e);
void wm_client_hide(Wm *wm, Client *c);
void wm_client_show(Wm* wm, Client *c);
void wm_client_focus(Wm* wm, Client *c);
void wm_client_focus_dir(Wm* wm, Client *c, int dir);
void wm_client_free(Wm *wm, Client *c);
void wm_client_kill(Wm *wm, Client *c);
void wm_client_set_atom(Wm *wm, Client *c, const char *name, const unsigned char *data,
Atom type, int nelements);
Atom wm_client_get_atom(Wm *wm, Client *c, const char *name, unsigned char **atom_ret,
unsigned long *nitems_ret);
Client* wm_client_get_dir_rel_c(Client *c, int dir);
Client* wm_client_get_focused(Wm* wm);
void wm_client_border(Wm* wm, Client *c);
void wm_client_border_focused(Wm* wm, Client *c);
void wm_monitor_clients_border(Wm* wm, Monitor *m);
bool wm_client_is_focused(Wm* wm, Client *c);
#endif

219
src/handler.c Normal file
View File

@@ -0,0 +1,219 @@
/*
The GPLv3 License (GPLv3)
Copyright (c) 2022 Akos Horvath
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "handler.h"
#include "wm.h"
#include "client.h"
/* global configuration variables */
// TODO: active layout not working
static int wm_xerror_handler(Wm *wm, Display *display, XErrorEvent *e)
{
printf("wm: Error occured. id: %d req_code: %d\n",
e->error_code, e->request_code);
char etext[200];
XGetErrorText(wm->display, e->error_code, etext, 200);
printf("%s\n", etext);
if (e->error_code == BadAccess) {
wm->other_wm = true;
}
return 0;
}
static int wm_other_wm_handler(Display *display, XErrorEvent *e)
{
return 0;
}
// maybe need to handle non-normal windows too
void wm_create_handler(Wm *wm, XCreateWindowEvent e)
{
DEBUG_PRINT("CreateNotify event\n");
// Client *nc;
// Client *c;
// Client t;
// unsigned char *prop_ret;
// unsigned long nitems_ret;
// Atom type;
// bool is_normal = false;
// t.window = e.window;
// t.m = wm->smon;
// t.ws = wm->smon->selws;
// t.hidden = true;
// //wm_client_is_dock(&t);
// /*
// _NET_WM_WINDOW_TYPE_NORMAL indicates that this is a normal,
// top-level window, [...] windows without _NET_WM_WINDOW_TYPE,
// must be taken as this type [...]
// https://specifications.freedesktop.org/wm-spec
// */
// if (type == None)
// is_normal = true;
//
// if (!is_normal) {
// DEBUG_PRINT("Create handler: window %d is not normal, returning",
// (int)e.window)
// return;
// }
// nc = malloc(sizeof(Client*));
// *nc = t;
// if (wm->smon->clients == NULL) {
// wm->smon->clients = nc;
// nc->prev = NULL;
// } else {
// c = wm_get_last_client(*nc->m);
// //c = &wm_root;
// c->next = nc;
// nc->prev = c;
// nc->next = NULL;
// }
//
// XSelectInput(wm->display, nc->window, FocusChangeMask | EnterWindowMask);
// DEBUG_PRINT("%d window created,", nc->window);
// DEBUG_PRINT("client ws: %d\n", nc->ws);
// // wm_client_is_dock(nc);
// wm_mstack(nc->m);
//wm_client_focus(c);
}
void wm_destroy_handler(Wm *wm, XDestroyWindowEvent e)
{
DEBUG_PRINT("DestroyNotify event\n");
Client *c;
c = wm_client_find(wm, e.window);
if (!c)
return;
wm_client_free(wm, c);
}
void wm_reparent_handler(XReparentEvent e)
{
DEBUG_PRINT("ReparentNotify event\n");
}
void wm_keyrelease_handler(XKeyReleasedEvent e)
{
DEBUG_PRINT("KeyReleased event, code: %d\n", e.keycode)
}
void wm_keypress_handler(Wm *wm, XKeyPressedEvent e)
{
DEBUG_PRINT("KeyPressed event, code: %d\n", e.keycode)
Client *c;
Keybind k;
DEBUG_PRINT("wm->cfg_kb_count: %d\n", wm->cfg_kb_count);
for (int i = 0; i < wm->cfg_kb_count; i++) {
k = wm->cfg_keybinds[i];
if (k.mask == e.state && k.keysym == XLookupKeysym(&e, 0))
{
(*k.func)(wm, &k.args);
}
}
}
void wm_maprequest_handler(Wm *wm, XMapRequestEvent e)
{
DEBUG_PRINT("MapRequest event\n");
if (e.window == wm->root.window) {
fprintf(stderr, "%s e.window was root, returning\n", __func__);
return;
}
Client *c;
if (wm_window_is_dock(wm, e.window))
{
DEBUG_PRINT("%s: window is dock, mapping window\n", __func__)
wm->dock = e.window;
XMapWindow(wm->display, e.window);
return;
}
c = wm_client_find(wm, e.window);
if (c) {
DEBUG_PRINT("%s: client found, mapping window\n", __func__)
XMapWindow(wm->display, e.window);
c->hidden = false;
return;
}
DEBUG_PRINT("%s: client not found, creating client\n", __func__)
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)
{
Client *c;
c = wm_client_find(wm, e.window);
RETURN_IF_NULL(c)
if(!wm_client_is_focused(wm, c) && wm->cfg_focus_on_motion)
wm_client_focus(wm, c);
}
// TODO
void wm_configure_handler(Wm *wm, XConfigureRequestEvent e)
{
DEBUG_PRINT("ConfigureRequest event\n");
Client *c;
XWindowChanges wc;
wc.x = e.x;
wc.y = e.y;
wc.width = e.width;
wc.height = e.height;
wc.border_width = e.border_width;
wc.sibling = e.above;
wc.stack_mode = e.detail;
XConfigureWindow(wm->display, e.window, e.value_mask, &wc);
XSync(wm->display, False);
}

44
src/handler.h Normal file
View File

@@ -0,0 +1,44 @@
/*
The GPLv3 License (GPLv3)
Copyright (c) 2022 Akos Horvath
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef HANDLER_H
#define HANDLER_H
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
#include <X11/Xproto.h>
#include <X11/Xatom.h>
#include "wm.h"
static int wm_xerror_handler(Wm *wm, Display *display, XErrorEvent *e);
static int wm_other_wm_handler(Display *display, XErrorEvent *e);
void wm_create_handler(Wm *wm, XCreateWindowEvent e);
void wm_destroy_handler(Wm *wm, XDestroyWindowEvent e);
void wm_reparent_handler(XReparentEvent e);
void wm_keyrelease_handler(XKeyReleasedEvent e);
void wm_keypress_handler(Wm *wm, XKeyPressedEvent e);
void wm_maprequest_handler(Wm *wm, XMapRequestEvent e);
void wm_motion_handler(Wm *wm, XMotionEvent e);
void wm_configure_handler(Wm *wm, XConfigureRequestEvent e);
#endif

28
src/main.c Normal file
View File

@@ -0,0 +1,28 @@
/*
The GPLv3 License (GPLv3)
Copyright (c) 2022 Akos Horvath
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "wm.h"
int main(int argc, char **arg)
{
Wm wm;
wm_init(&wm);
return 0;
}

712
src/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
src/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

870
src/wm.c Normal file
View File

@@ -0,0 +1,870 @@
/*
The GPLv3 License (GPLv3)
Copyright (c) 2022 Akos Horvath
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "wm.h"
#include <X11/X.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#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)
{
Monitor m = {
.display = d,
.info = info
};
return m;
}
void wm_monitors_open_all(Wm *wm, Display *d)
{
DEBUG_PRINT("wm_monitors_open_all\n");
int count;
XineramaScreenInfo *xsi = XineramaQueryScreens(d, &count);
if (!xsi) {
printf("wm: Could not open display. Exiting.\n");
exit(1);
}
wm->monitors = malloc(count * sizeof(Monitor));
for (int i = 0; i < count; i++) {
wm->monitors[i] = wm_monitor_open(d, xsi[i]);
//TODO: cfg
wm->monitors[i].wscount = 9;
wm->monitors[i].workspaces = calloc(wm->monitors[i].wscount, sizeof(Workspace));
wm->monitors[i].selws = 0;
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;
}
wm->smon = &wm->monitors[0];
// wm->smon->clients = &wm->root;
DEBUG_PRINT("smon width: %d\n", wm->smon->info.width);
}
Display* wm_connect_display()
{
Display *d;
if (!(d = XOpenDisplay(NULL)))
{
printf("wm: Could not open display. Exiting.\n");
exit(1);
}
return d;
}
bool wm_window_is_dock(Wm *wm, Window w)
{
// TODO: get atom new function
unsigned char *prop_ret;
Atom atom_ret;
int format_ret;
unsigned long nitems_ret;
unsigned long bytes_remain_ret;
int ret;
ret = XGetWindowProperty(wm->display, w,
XInternAtom(wm->display, "_NET_WM_WINDOW_TYPE", False), 0, (~0L), False,
AnyPropertyType, &atom_ret,
&format_ret, &nitems_ret, &bytes_remain_ret, &prop_ret);
if (ret != Success || nitems_ret == 0) {
fprintf(stderr, "wm: XGetWindowProperty failed\n");
return false;
}
DEBUG_PRINT("XGetWindowProperty return values, window %d:\n", (int)w);
// DEBUG_PRINT("actual atom return: %s\n", XGetAtomName(wm->display, atom_ret));
DEBUG_PRINT("actual format return: %d\n", format_ret)
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 (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]),
"_NET_WM_WINDOW_TYPE_DOCK") == 0) {
DEBUG_PRINT("%s", __func__)
DEBUG_PRINT("returning true\n")
return true;
}
}
return false;
}
NodeType wm_split_to_nodetype(SplitMode mode)
{
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;
Client *c;
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);
}
}
wm->smon->selws = ws;
wm_client_set_atom(wm, &wm-> root, "_NET_CURRENT_DESKTOP",
(unsigned char*) &(ws), XA_CARDINAL, 1);
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);
}
}
DEBUG_PRINT("wm_switch_ws focused ws client count: %d\n", wscc)
}
// TODO maybe x,y is not 0, because of bar
void wm_mstack(Wm *wm, Monitor *m)
{
DEBUG_PRINT("%s\n", __func__)
RETURN_IF_NULL(m);
Client *c;
XWindowChanges ch;
int cc_sws = 0;
int n = 0;
int sx = 0;
int sy = 0;
XEvent e;
unsigned int value_mask = CWX | CWY | CWWidth | CWHeight;
// TODO: function
// FIXME: &root probably wrong
if (wm->dock != ULONG_MAX) {
XWindowAttributes x;
XGetWindowAttributes(wm->display, wm->dock, &x);
sx = x.x;
sy = x.y+x.height;
DEBUG_PRINT("%s dock sx x: %d y: %d\n", __func__, sx, sy)
}
for (c = m->clients; c; c = c->next) {
if (c->ws->index == m->selws && c != &wm->root && !c->is_floating) {
cc_sws++;
}
}
DEBUG_PRINT("mstack cc_sws: %d\n", cc_sws)
if (cc_sws <= 0) {
DEBUG_PRINT("mstack cc_sws <= 0, returning\n")
return;
}
// dynamic
if (cc_sws == 1) {
for (c = m->clients; c; c = c->next) {
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: %p\n", c);
// XGetWMName(wm->display, c->window, &xt);
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)
XConfigureWindow(wm->display, c->window, value_mask, &ch);
wm_client_show(wm, c);
wm_client_focus(wm, c);
}
else {
// FIXME not working with dock
for (c = m->clients; c; c = c->next) {
if (c->ws->index == m->selws && c->window != wm->root.window && !c->is_floating) {
if (n == 0) {
c->x = 0;
c->y = sy;
c->w = m->info.width*wm->cfg_ms_p-wm->cfg_border_width*2;
c->h = (m->info.height-wm->cfg_border_width*2)-sy;
} else {
c->x = (m->info.width*wm->cfg_ms_p);
c->y = ((n-1)*m->info.height/(cc_sws-1))+sy;
c->w = (m->info.width - m->info.width*wm->cfg_ms_p) -
wm->cfg_border_width*2;
c->h = ((m->info.height)/(cc_sws-1)-wm->cfg_border_width*2);
}
n++;
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: %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)
// DEBUG_PRINT("mstack client width: %d ", ch.width)
// DEBUG_PRINT("height: %d\n", ch.height)
XConfigureWindow(wm->display, c->window, value_mask, &ch);
wm_client_show(wm, c);
DEBUG_PRINT("%s %d. client next: %p\n", __func__, n, c->next)
if (c->next == NULL)
{
wm_client_focus(wm, c);
}
}
}
}
DEBUG_PRINT("%s end\n", __func__)
XSync(wm->display, False);
while(XCheckMaskEvent(wm->display, EnterWindowMask, &e));
wm_monitor_clients_border(wm, m);
}
void wm_set_layout(Wm *wm, void(*f)(Wm *wm, Monitor*))
{
wm->active_layout = f;
}
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 (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)
{
Client *c = m.clients;
if (c == NULL)
return NULL;
while (c->next != NULL)
c = c->next;
return c;
}
void wm_mainloop(Wm *wm)
{
// int s = DefaultScreen(wm->display);
// Window w = XCreateSimpleWindow(wm->display, root.window, 10, 10, 300, 300,
// 2, WhitePixel(wm->display, s), BlackPixel(wm->display, s));
// XSelectInput(wm->display, w, KeyPressMask | KeyReleaseMask);
// XMapWindow(wm->display, w);
struct sockaddr s;
socklen_t t = 108;
getsockname(wm->socket_fd, &s, &t);
// int accepted_socket;
// char *buffer;
// accepted_socket = accept(wm->socket_fd, &s, &t);
// if (accepted_socket == -1) {
// if (errno == EAGAIN || errno == EWOULDBLOCK) {
// // fprintf(stderr,
// // "wm: accept failed, no waiting connections are present\n");
// } else {
// fprintf(stderr, "wm: accept failed\n");
// }
// }
// else {
// DEBUG_PRINT("accept success\n")
// len = recv(accepted_socket, buffer, 100, 0);
// if (len != -1) {
// DEBUG_PRINT("got %d bytes:", len)
// for (int i = 0; i < len; i++)
// DEBUG_PRINT("%c", buffer[i])
// DEBUG_PRINT("\n")
// } else {
// fprintf(stderr, "wm: recv failed\n");
// fprintf(stderr, "errno: %d\n", errno);
// }
// }
while (wm->running) {
XEvent e;
XNextEvent(wm->display, &e);
switch (e.type) {
case CreateNotify:
wm_create_handler(wm, e.xcreatewindow);
break;
case DestroyNotify:
wm_destroy_handler(wm, e.xdestroywindow);
break;
case ReparentNotify:
wm_reparent_handler(e.xreparent);
break;
case ConfigureRequest:
wm_configure_handler(wm, e.xconfigurerequest);
break;
case KeyRelease:
wm_keyrelease_handler(e.xkey);
break;
case KeyPress:
wm_keypress_handler(wm, e.xkey);
break;
case MotionNotify:
wm_motion_handler(wm, e.xmotion);
break;
case FocusIn:
break;
case FocusOut:
break;
case EnterNotify:
DEBUG_PRINT("pointer entered window: %d\n",
(int)e.xcrossing.window);
if (wm->cfg_focus_on_motion)
wm_client_focus(wm, wm_client_find(wm, e.xcrossing.window));
break;
case LeaveNotify:
DEBUG_PRINT("pointer left window: %d\n",
(int)e.xcrossing.window);
break;
case ConfigureNotify:
DEBUG_PRINT("ConfigureNotify: %d\n", (int)e.xconfigure.window)
break;
case MapRequest:
DEBUG_PRINT("MapRequest: %d\n", (int)e.xmaprequest.window)
wm_maprequest_handler(wm, e.xmaprequest);
break;
default:
DEBUG_PRINT("default: %d\n", e.type);
}
}
}
void wm_init(Wm *wm)
{
XSetWindowAttributes xa;
Display *d;
signal(SIGINT, wm_sigint_handler);
DEBUG_PRINT("wm_init\n")
DEBUG_PRINT("pid: %ld\n", ((long)getpid()))
d = wm_connect_display();
wm->display = d;
wm_monitors_open_all(wm, d);
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);
XSync(d, false);
xa.event_mask = StructureNotifyMask | StructureNotifyMask |
SubstructureNotifyMask | SubstructureRedirectMask |
ButtonPressMask | PropertyChangeMask;
XChangeWindowAttributes(wm->display, wm->root.window, CWEventMask, &xa);
XSelectInput(wm->display, wm->root.window, xa.event_mask);
// DEBUG_PRINT("root window id: %d\n", (root.window))
// XUngrabKey(wm->display, AnyKey, AnyModifier, root.window);
// TODO: read config
wm_init_cfg_def(wm);
wm_grab_keys(wm);
//XSetErrorHandler(&wm_other_wm_handler);
// if (wm->other_wm) {
// printf("wm: Detected other window manager. Exiting.\n");
// exit(1);
// }
//TODO: new function!
XChangeProperty(wm->display, wm->root.window,
XInternAtom(wm->display, "_NET_NUMBER_OF_DESKTOPS", False), XA_CARDINAL,
32, PropModeReplace, (unsigned char *) &wm->smon->wscount, 1);
char *desktop_names[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9"};
wm_client_set_atom(wm, &wm->root, "_NET_DESKTOP_NAMES",
(unsigned char*) desktop_names, XA_STRING, 9);
Atom wm_supp[] = {
XInternAtom(wm->display, "_NET_NUMBER_OF_DESKTOPS", False),
XInternAtom(wm->display, "_NET_DESKTOP_GEOMETRY", False),
XInternAtom(wm->display, "_NET_DESKTOP_VIEWPORT", False),
XInternAtom(wm->display, "_NET_CURRENT_DESKTOP", False),
XInternAtom(wm->display, "_NET_ACTIVE_WINDOW", False),
XInternAtom(wm->display, "_NET_DESKTOP_NAMES", False),
};
XChangeProperty(wm->display, wm->root.window,
XInternAtom(wm->display, "_NET_SUPPORTED", False), XA_ATOM, 32,
PropModeReplace, (unsigned char *) &wm_supp, sizeof(wm_supp)/sizeof(long));
//XSetErrorHandler(&wm_xerror_handler);
wm->dock = ULONG_MAX;
wm->running = true;
wm_mainloop(wm);
wm_exit(wm);
}
void wm_exit(Wm *wm)
{
DEBUG_PRINT("%s\n", __func__);
Client *c;
XUngrabKey(wm->display, AnyKey, AnyModifier, wm->root.window);
// todo add clients to root client linked list
for (c = wm->smon->clients; c; c = c->next) {
XKillClient(wm->display, c->window);
}
for (c = wm->smon->clients; c; c = c->next) {
wm_client_free(wm, c);
}
DEBUG_PRINT("%s1\n", __func__);
XCloseDisplay(wm->display);
exit(EXIT_SUCCESS);
}
void wm_init_cfg_def(Wm *wm)
{
wm->cfg_ms_p = 0.5;
wm->cfg_border_col = "#222222";
wm->cfg_focused_border_col = "#444444";
wm->cfg_border_width = 2;
wm->cfg_focus_on_motion = true;
wm_keybinds_init_def(wm);
}
void wm_grab_keys(Wm *wm)
{
RETURN_IF_NULL(wm->cfg_keybinds)
Keybind k;
// from dwm
{
int i, j;
XModifierKeymap *modmap;
unsigned int numlockmask = 0;
modmap = XGetModifierMapping(wm->display);
for (i = 0; i < 8; i++)
for (j = 0; j < modmap->max_keypermod; j++)
if (modmap->modifiermap[i * modmap->max_keypermod + j]
== XKeysymToKeycode(wm->display, XK_Num_Lock))
numlockmask = (1 << i);
XFreeModifiermap(modmap);
}
// from dwm
unsigned int modifiers[] = { 0, LockMask, 0|LockMask };
XUngrabKey(wm->display, AnyKey, AnyModifier, wm->root.window);
for (int i = 0; i < wm->cfg_kb_count; i++) {
k = wm->cfg_keybinds[i];
for (int j = 0; j < 3; j++)
XGrabKey(wm->display, XKeysymToKeycode(wm->display, k.keysym),
k.mask | modifiers[j], wm->root.window, True, GrabModeAsync,
GrabModeAsync);
}
}
void wm_kb_spawn(Wm *wm, Arg *args)
{
RETURN_IF_NULL(args)
RETURN_IF_NULL(args->sl)
DEBUG_PRINT("args sl 1: %s\n", args->sl[args->count-1])
DEBUG_PRINT("args sl 0: %s\n", args->sl[0])
DEBUG_PRINT("args count: %d\n", args->count)
if (args->sl[args->count-1] != NULL) {
fprintf(stderr, "%s recieved non null-terminated args. Returning\n",
__func__);
}
if (fork() == 0) {
if (wm->display)
close(ConnectionNumber(wm->display));
setsid();
execvp(args->sl[0], &(args->sl[1]));
exit(0);
}
}
void wm_kb_exit(Wm* wm, Arg *args)
{
wm->running = false;
}
void wm_kb_kill(Wm *wm, Arg *args)
{
Client *c;
c = wm_client_get_focused(wm);
RETURN_IF_NULL(c)
wm_client_kill(wm, c);
}
void wm_kb_switch_ws(Wm *wm, Arg *args)
{
RETURN_IF_NULL(args)
wm_switch_ws(wm, args->i);
}
void wm_kb_focus_dir(Wm *wm, Arg *args)
{
Client *c;
RETURN_IF_NULL(args)
c = wm_client_get_focused(wm);
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[] = {"alacritty", NULL};
char **sth = malloc(sizeof(st));
memcpy(sth, st, sizeof(st));
char *dmenu[] = {"i3-dmenu-desktop", NULL};
char **dmenuh = malloc(sizeof(dmenu));
memcpy(dmenuh, dmenu, sizeof(dmenu));
int size;
Keybind k[] = {
(Keybind) {Mod4Mask, XK_Return, *wm_kb_spawn,
(Arg) {.sl = sth, .count = 2}},
(Keybind) {Mod4Mask | ShiftMask, XK_q, *wm_kb_exit,
(Arg) {0}},
(Keybind) {Mod4Mask, XK_d, *wm_kb_spawn,
(Arg) {.sl = dmenuh, .count = 2}},
(Keybind) {Mod4Mask, XK_c, *wm_kb_kill,
(Arg) {.c = NULL}},
(Keybind) {Mod4Mask, XK_1, *wm_kb_switch_ws,
(Arg) {.i = 0}},
(Keybind) {Mod4Mask, XK_2, *wm_kb_switch_ws,
(Arg) {.i = 1}},
(Keybind) {Mod4Mask, XK_3, *wm_kb_switch_ws,
(Arg) {.i = 2}},
(Keybind) {Mod4Mask, XK_4, *wm_kb_switch_ws,
(Arg) {.i = 3}},
(Keybind) {Mod4Mask, XK_5, *wm_kb_switch_ws,
(Arg) {.i = 4}},
(Keybind) {Mod4Mask, XK_6, *wm_kb_switch_ws,
(Arg) {.i = 5}},
(Keybind) {Mod4Mask, XK_7, *wm_kb_switch_ws,
(Arg) {.i = 6}},
(Keybind) {Mod4Mask, XK_8, *wm_kb_switch_ws,
(Arg) {.i = 7}},
(Keybind) {Mod4Mask, XK_9, *wm_kb_switch_ws,
(Arg) {.i = 8}},
(Keybind) {Mod4Mask, XK_h, *wm_kb_focus_dir,
(Arg) {.i = LEFT}},
(Keybind) {Mod4Mask, XK_j, *wm_kb_focus_dir,
(Arg) {.i = DOWN}},
(Keybind) {Mod4Mask, XK_k, *wm_kb_focus_dir,
(Arg) {.i = UP}},
(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}},
};
size = sizeof(k);
wm->cfg_kb_count = size / sizeof(Keybind);
DEBUG_PRINT("sizeof k: %d\n", size);
wm->cfg_keybinds = malloc(size);
memcpy(wm->cfg_keybinds, k, size);
}
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 ret[50];
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]);
}
}
}
}

180
src/wm.h Normal file
View File

@@ -0,0 +1,180 @@
/*
The GPLv3 License (GPLv3)
Copyright (c) 2022 Akos Horvath
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef WM_H
#define WM_H
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
#include <X11/Xproto.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xinerama.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#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
// 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__); \
return; \
}
#define DEBUG 1
#define DEBUG_PRINT(fmt, ...) \
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;
typedef struct Wm Wm;
enum Direction {
UP,
DOWN,
LEFT,
RIGHT
};
typedef enum {
SPLIT_VERTICAL,
SPLIT_HORIZ,
SPLIT_TAB
} SplitMode;
struct Monitor {
Display *display;
XineramaScreenInfo info;
Client *clients;
Workspace *workspaces;
size_t selws;
size_t wscount;
};
struct Workspace {
TreeNode tree;
size_t index;
char name[WM_WS_NAME_LEN];
Monitor *monitor;
SplitMode split;
};
struct Arg {
int i;
unsigned int ui;
Client *c;
char* s;
char **sl;
int count;
};
struct Keybind {
unsigned int mask;
KeySym keysym;
void (*func)(Wm*, Arg*);
Arg args;
};
struct Wm {
// TODO INIT
Display *display;
Monitor *monitors;
Monitor *smon;
bool running;
int wm_mc;
bool other_wm;
Client root;
Window dock;
Client *focused_client;
// TODO: active layout not working
void (*active_layout)(Wm *wm, Monitor*);
float cfg_ms_p;
char *cfg_border_col;
char *cfg_focused_border_col;
unsigned char cfg_border_width;
Keybind *cfg_keybinds;
int cfg_kb_count;
bool cfg_focus_on_motion;
int socket_fd;
};
Monitor wm_monitor_open(Display *d, XineramaScreenInfo info);
void wm_monitors_open_all(Wm *wm, Display *d);
Display* wm_connect_display();
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);
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, size_t ws);
void wm_mainloop(Wm* wm);
void wm_init(Wm *wm);
void wm_exit(Wm *wm);
void wm_init_cfg_def(Wm *wm);
void wm_grab_keys(Wm *wm);
void wm_kb_spawn(Wm *wm, Arg *args);
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);
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

83
src/wmc/wmc.c Normal file
View File

@@ -0,0 +1,83 @@
/*
The GPLv3 License (GPLv3)
Copyright (c) 2022 Akos Horvath
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <xcb/xcb.h>
int main(int argc, char **argv)
{
int wm_socket_fd;
struct sockaddr_un sock_addr;
char *c;
char buf[100];
int ret, d, s;
if (argc < 2) {
fprintf(stderr, "No arguments\n");
exit(1);
}
ret = xcb_parse_display(NULL, &c, &d, &s);
if (ret == -1) {
fprintf(stderr, "wmc: could not parse display. Exiting.\n");
exit(EXIT_FAILURE);
}
ret = wm_socket_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (ret == -1) {
fprintf(stderr, "wmc: 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_1");
printf("display: %d\n", d);
ret = connect(wm_socket_fd, (struct sockaddr *) &sock_addr, sizeof(sock_addr));
if (ret == -1) {
fprintf(stderr, "wmc: could not connect to socket. Exiting.\n");
exit(EXIT_FAILURE);
}
strncpy(buf, "set window-width 2", 100);
//char *buf = "set window-width 2";
ret = send(wm_socket_fd, buf, 100, 0);
if (ret == -1) {
fprintf(stderr, "wmc: could not send data to socket. Exiting.\n");
exit(EXIT_FAILURE);
} else {
printf("wmc: sent %d bytes\n", ret);
}
// ret = listen(wm_socket_fd, 512);
// if (ret == -1) {
// fprintf(stderr, "wm: could not listen to socket. Exiting.\n");
// exit(EXIT_FAILURE);
// }
}