work on ewmh, bar/non-normal window handling

This commit is contained in:
Akos Horvath 2022-08-01 12:18:31 +02:00
parent 3206fa9578
commit 4752d41361
2 changed files with 572 additions and 183 deletions

717
wm.c
View File

@ -4,8 +4,10 @@
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xinerama.h>
#include <asm-generic/errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
@ -21,7 +23,7 @@ Monitor wm_monitor_open(Display *d, XineramaScreenInfo info)
void wm_monitors_open_all(Display *d)
{
DEBUG_PRINT("wm_monitors_open_all\n")
DEBUG_PRINT("wm_monitors_open_all\n");
int count;
XineramaScreenInfo *xsi = XineramaQueryScreens(d, &count);
@ -34,10 +36,12 @@ void wm_monitors_open_all(Display *d)
for (int i = 0; i < count; i++) {
*(wm_monitors + i) = wm_monitor_open(d, *(xsi + i));
//cfg
//TODO: cfg
wm_monitors[i].wscount = 9;
wm_monitors[i].wss = malloc(wm_monitors[i].wscount*sizeof(int));
memset(wm_monitors[i].wss, 0, 9);
wm_monitors[i].selws = wm_monitors[i].wss[0];
for (int j = 0; j < wm_monitors[i].wscount; j++)
wm_monitors[i].wss[i] = j;
@ -46,7 +50,7 @@ void wm_monitors_open_all(Display *d)
wm_smon = &wm_monitors[0];
wm_smon->clients = &wm_root;
DEBUG_PRINTV("smon width: %d\n", wm_smon->info.width)
DEBUG_PRINT("smon width: %d\n", wm_smon->info.width);
}
Display* wm_connect_display()
@ -80,36 +84,67 @@ static int wm_xerror_handler(Display *displau, XErrorEvent *e)
static int wm_other_wm_handler(Display *displau, XErrorEvent *e)
{
return 0;
}
// maybe need to handle non-normal windows too
void wm_create_handler(XCreateWindowEvent e)
{
DEBUG_PRINT("CreateNotify event\n")
Client *nc = malloc(sizeof(Client));
Client *c;
DEBUG_PRINT("CreateNotify event\n");
nc->wn = e.window;
nc->m = wm_smon;
nc->ws = nc->m->selws;
nc->hidden = true;
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->wn, FocusChangeMask | EnterWindowMask);
// Client *nc;
// Client *c;
// Client t;
// unsigned char *prop_ret;
// unsigned long nitems_ret;
// Atom type;
// bool is_normal = false;
DEBUG_PRINTV("%d window created.\n", nc->wn);
wm_mstack(nc->m);
// t.wn = 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->wn, FocusChangeMask | EnterWindowMask);
// DEBUG_PRINT("%d window created,", nc->wn);
// DEBUG_PRINT("client ws: %d\n", nc->ws);
// // wm_client_is_dock(nc);
// wm_mstack(nc->m);
//wm_client_focus(c);
}
@ -117,7 +152,7 @@ void wm_create_handler(XCreateWindowEvent e)
// TODO
void wm_destroy_handler(XDestroyWindowEvent e)
{
DEBUG_PRINT("DestroyNotify event\n")
DEBUG_PRINT("DestroyNotify event\n");
Client *c;
// TODO: make function for linked list management
@ -145,41 +180,120 @@ void wm_destroy_handler(XDestroyWindowEvent e)
void wm_reparent_handler(XReparentEvent e)
{
DEBUG_PRINT("ReparentNotify event\n")
DEBUG_PRINT("ReparentNotify event\n");
}
// TODO
void wm_configure_handler(XConfigureRequestEvent e)
{
DEBUG_PRINT("ConfigureRequest event\n")
DEBUG_PRINT("ConfigureRequest event\n");
Client *nc;
Client *c;
XWindowChanges ch;
int cc_sws = 0;
Client t;
unsigned char *prop_ret;
unsigned long nitems_ret;
Atom type;
bool is_normal = false;
// for (c = wm_smon->clients; c; c = c->next) {
// if (c->ws == wm_smon->selws) {
// cc_sws++;
// }
// }
XTextProperty xtp;
c = wm_client_find(e.window);
t.wn = e.window;
t.m = wm_smon;
t.ws = wm_smon->selws;
t.hidden = true;
// // dynamic
// if (cc_sws == 0) {
// c->x = 0;
// c->y = 0;
// c->w = wm_smon->info.width;
// c->h = wm_smon->info.height;
// }
// else {
// // TODO
// // c.x = m.h / cc_sws;
XGetWMName(wm_display, t.wn, &xtp);
// }
DEBUG_PRINT("window %d name: %s\n", (int)e.window, xtp.value);
// XConfigureWindow(wm_display, c->wn, e.value_mask, &ch);
//XFree(xtp);
char *name = "_NET_WM_WINDOW_TYPE";
type = wm_client_get_atom(&t, "_NET_WM_WINDOW_TYPE", &prop_ret, &nitems_ret);
if (type == -1) {
fprintf(stderr, "wm_client_get_atom failed\n");
return;
}
for (int 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;
}
}
/*
_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) {
DEBUG_PRINT("Atom %s does not exist for window.\n", name)
is_normal = true;
}
wm_layout(c->m);
if (!is_normal) {
DEBUG_PRINT("configure handler: window %d is not normal, returning\n",
(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->wn, FocusChangeMask | EnterWindowMask);
// wm_client_is_dock(nc);
wm_mstack(nc->m);
// wm_client_is_dock(wm_client_find(e.window));
// Client *c;
// XWindowChanges ch;
// int cc_sws = 0;
// // for (c = wm_smon->clients; c; c = c->next) {
// // if (c->ws == wm_smon->selws) {
// // cc_sws++;
// // }
// // }
// c = wm_client_find(e.window);
// // // dynamic
// // if (cc_sws == 0) {
// // c->x = 0;
// // c->y = 0;
// // c->w = wm_smon->info.width;
// // c->h = wm_smon->info.height;
// // }
// // else {
// // // TODO
// // // c.x = m.h / cc_sws;
// // }
// // XConfigureWindow(wm_display, c->wn, e.value_mask, &ch);
//
// wm_layout(c->m);
}
void wm_map_handler(XMapRequestEvent e)
@ -195,18 +309,14 @@ void wm_map_handler(XMapRequestEvent e)
void wm_keyrelease_handler(XKeyReleasedEvent e)
{
DEBUG_PRINTV("KeyReleased event, code: %d\n", e.keycode)
DEBUG_PRINT("KeyReleased event, code: %d\n", e.keycode)
}
void wm_keypress_handler(XKeyPressedEvent e)
{
DEBUG_PRINTV("KeyPressed event, code: %d\n", e.keycode)
DEBUG_PRINT("KeyPressed event, code: %d\n", e.keycode)
Client *c;
KeySym k = XKeycodeToKeysym(wm_display, e.keycode, 0);
KeyCode ck = XKeysymToKeycode(wm_display, XK_C);
DEBUG_PRINTV("xk_c keycode: %d\n", ck);
// TODO: bad code below: need to implement key structure with keysyms,
// and functions to be ran for clarity and configurabilty
// TODO: fix focusing left from master!
@ -214,36 +324,88 @@ void wm_keypress_handler(XKeyPressedEvent e)
case (Mod4Mask):
DEBUG_PRINT("mod4\n")
// cant use switch
if (e.keycode == XKeysymToKeycode(wm_display, XK_C)) {
DEBUG_PRINT("Kill key combination\n")
if ((c = wm_client_get_focused()) != NULL) {
DEBUG_PRINTV("kill client: 0x%x\n", c);
wm_client_kill(c);
}
// if (e.keycode == XKeysymToKeycode(e.display, XK_C)) {
// DEBUG_PRINT("Kill key combination\n")
//
// if ((c = wm_client_get_focused()) != NULL) {
// DEBUG_PRINT("kill client: 0x%x\n", c);
// wm_client_kill(c);
// }
// }
switch (XLookupKeysym(&e, 0)) {
case XK_c:
DEBUG_PRINT("Kill key combination\n")
if ((c = wm_client_get_focused()) != NULL) {
DEBUG_PRINT("kill client: 0x%x\n", c);
wm_client_kill(c);
}
break;
case XK_Return:
if (fork() == 0) {
if (wm_display)
close(ConnectionNumber(wm_display));
setsid();
//char *arg[] = {"-t", "Terminal", NULL};
execvp("st", NULL);
exit(0);
}
break;
case XK_h:
wm_client_focus_dir(wm_client_get_focused(), LEFT);
break;
case XK_j:
wm_client_focus_dir(wm_client_get_focused(), DOWN);
break;
case XK_k:
wm_client_focus_dir(wm_client_get_focused(), UP);
break;
case XK_l:
wm_client_focus_dir(wm_client_get_focused(), RIGHT);
break;
case XK_1:
DEBUG_PRINT("wm_switch_ws keycode, ws: %d\n", 0)
wm_switch_ws(0);
break;
case XK_2:
DEBUG_PRINT("wm_switch_ws keycode, ws: %d\n", 1)
wm_switch_ws(1);
break;
case XK_3:
DEBUG_PRINT("wm_switch_ws keycode, ws: %d\n", 2)
wm_switch_ws(2);
break;
case XK_4:
DEBUG_PRINT("wm_switch_ws keycode, ws: %d\n", 3)
wm_switch_ws(3);
break;
case XK_5:
DEBUG_PRINT("wm_switch_ws keycode, ws: %d\n", 4)
wm_switch_ws(4);
break;
case XK_6:
DEBUG_PRINT("wm_switch_ws keycode, ws: %d\n", 5)
wm_switch_ws(5);
break;
case XK_7:
DEBUG_PRINT("wm_switch_ws keycode, ws: %d\n", 6)
wm_switch_ws(6);
break;
case XK_8:
DEBUG_PRINT("wm_switch_ws keycode, ws: %d\n", 7)
wm_switch_ws(7);
break;
case XK_9:
DEBUG_PRINT("wm_switch_ws keycode, ws: %d\n", 8)
wm_switch_ws(8);
break;
default:
DEBUG_PRINT("wm_handle_keys default\n")
char *buf;
KeySym k;
XLookupString(&e, buf, 50, &k, NULL);
DEBUG_PRINT("default keysym: %s\n", buf)
DEBUG_PRINT("default keysym i: %d\n", k)
}
else if (e.keycode == XKeysymToKeycode(wm_display, XK_Return)) {
if (fork() == 0) {
if (wm_display)
close(ConnectionNumber(wm_display));
setsid();
execvp("st", NULL);
exit(0);
}
}
else if (e.keycode == XKeysymToKeycode(wm_display, XK_H)) {
wm_client_focus_dir(wm_client_get_focused(), LEFT);
}
else if (e.keycode == XKeysymToKeycode(wm_display, XK_J)) {
wm_client_focus_dir(wm_client_get_focused(), DOWN);
}
else if (e.keycode == XKeysymToKeycode(wm_display, XK_K)) {
wm_client_focus_dir(wm_client_get_focused(), UP);
}
else if (e.keycode == XKeysymToKeycode(wm_display, XK_L)) {
wm_client_focus_dir(wm_client_get_focused(), RIGHT);
}
break;
}
}
@ -307,9 +469,11 @@ void wm_client_focus(Client *c)
RETURN_IF_NULL(c);
XSetInputFocus(wm_display, c->wn, RevertToPointerRoot, CurrentTime);
XChangeProperty(wm_display, wm_root.wn,
XInternAtom(wm_display, "_NET_ACTIVE_WINDOW", False),
33, 32, PropModeReplace, (unsigned char *) &c->wn, 1);
wm_client_set_atom(c, "_NET_ACTIVE_WINDOW", (unsigned char*) &c->wn,
XA_WINDOW, 1);
// XChangeProperty(wm_display, wm_root.wn,
// XInternAtom(wm_display, "_NET_ACTIVE_WINDOW", False),
// 33, 32, PropModeReplace, (unsigned char *) &c->wn, 1);
wm_monitor_clients_border(c->m);
}
@ -362,9 +526,61 @@ void wm_client_kill(Client *c)
wm_mstack(m);
}
void wm_client_set_atom(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->wn, XInternAtom(wm_display, name, False),
type, 32, PropModeReplace, data, nelements);
}
// probably not working
Atom wm_client_get_atom(Client *c, const char *name, unsigned char **atom_ret,
unsigned long *nitems_ret)
{
if (!c)
return -1;
if (!name)
return -1;
if (!atom_ret)
return -1;
if (!nitems_ret)
return -1;
Atom type_ret;
int format_ret;
unsigned long bytes_remain_ret;
int ret;
ret = XGetWindowProperty(wm_display, c->wn,
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 -1;
}
DEBUG_PRINT("XGetWindowProperty return values, window %d:\n", (int)c->wn);
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_PRINTV("%s\n", __func__)
DEBUG_PRINT("%s\n", __func__)
if (!c)
return NULL;
@ -392,8 +608,8 @@ Client* wm_client_get_dir_rel_c(Client *c, int dir)
dy = 0;
break;
case RIGHT:
x = c->x + c->h/2;
y = c->y + c->w;
x = c->x + c->w;
y = c->y + c->h/3;
dx = 1;
dy = 0;
break;
@ -409,9 +625,9 @@ Client* wm_client_get_dir_rel_c(Client *c, int dir)
y+=dy*2;
for (r = c->m->clients; r; r = r->next) {
if ((x > r->x && x < r->x+r->w) && (y > r->y && y < r->y+r->h)) {
DEBUG_PRINTV("%s ", __func__)
DEBUG_PRINTV("found window %d in direction ", c->wn)
DEBUG_PRINTV("%d\n", dir)
DEBUG_PRINT("%s ", __func__)
DEBUG_PRINT("found window %d in direction ", c->wn)
DEBUG_PRINT("%d\n", dir)
return r;
}
}
@ -436,7 +652,7 @@ Client* wm_client_get_focused()
void wm_client_border(Client *c)
{
DEBUG_PRINTV("%s\n", __func__);
// DEBUG_PRINT("%s\n", __func__);
RETURN_IF_NULL(c);
// if (c->has_border)
// return;
@ -459,17 +675,17 @@ void wm_client_border(Client *c)
XAllocColor(wm_display, cmap, &fcolor);
XAllocColor(wm_display, cmap, &color);
// DEBUG_PRINTV("wm_client_border c: 0x%x\n", c)
// DEBUG_PRINT("wm_client_border c: 0x%x\n", c)
if (c) {
// DEBUG_PRINTV("wm_client_border wn: %d\n", c->wn)
// DEBUG_PRINT("wm_client_border wn: %d\n", c->wn)
}
XSetWindowBorderWidth(wm_display, c->wn, wm_border_width);
DEBUG_PRINTV("drawing border for: %d\n", c->wn);
// DEBUG_PRINT("drawing border for: %d\n", c->wn);
if (wm_client_is_focused(c)) {
DEBUG_PRINTV("drawing focused border for: %d\n", c->wn);
DEBUG_PRINT("drawing focused border for: %d\n", c->wn);
XSetWindowBorder(wm_display, c->wn, fcolor.pixel);
}
else
@ -496,18 +712,18 @@ void wm_client_border_focused(Client *c)
XSetWindowBorderWidth(wm_display, c->wn, wm_border_width);
DEBUG_PRINTV("drawing border for focused wn: %d\n", c->wn);
DEBUG_PRINT("drawing border for focused wn: %d\n", c->wn);
XSetWindowBorder(wm_display, c->wn, fcolor.pixel);
}
void wm_monitor_clients_border(Monitor *m)
{
DEBUG_PRINTV("%s\n", __func__)
//DEBUG_PRINT("%s\n", __func__)
RETURN_IF_NULL(m);
Client *c;
for (c = wm_smon->clients; c; c = c->next) {
DEBUG_PRINTV("monitor border c: 0x%x\n", c)
//DEBUG_PRINT("monitor border c: 0x%x\n", c)
if (c->wn != wm_root.wn)
wm_client_border(c);
}
@ -532,39 +748,94 @@ bool wm_client_is_focused(Client *c)
int r;
XGetInputFocus(wm_display, &w, &r);
DEBUG_PRINTV("is_focused focused window: %d\n", w)
//DEBUG_PRINT("is_focused focused window: %d\n", w)
if (w == c->wn) {
DEBUG_PRINTV("IS_FOCUSED returning true for client: 0x%x", c)
DEBUG_PRINTV("window: %d\n", c->wn)
// DEBUG_PRINT("IS_FOCUSED returning true for client: 0x%x", c)
// DEBUG_PRINT("window: %d\n", c->wn)
return true;
}
DEBUG_PRINTV("IS_FOCUSED returning false for client: 0x%x", c)
DEBUG_PRINTV("window: %d\n", c->wn)
// DEBUG_PRINT("IS_FOCUSED returning false for client: 0x%x", c)
// DEBUG_PRINT("window: %d\n", c->wn)
return false;
}
bool wm_client_is_dock(Client *c)
{
if (!c)
return false;
// 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, c->wn,
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)c->wn);
// 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 (int 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;
}
void wm_switch_ws(int ws)
{
if (ws > wm_smon->wscount)
if (ws > wm_smon->wscount || ws < 0)
return;
int wscc = 0;
int xasws;
Client *c;
for (c = wm_smon->clients; c; c = c->next) {
if (c->ws == wm_smon->selws) {
DEBUG_PRINT("wm_switch_ws hide client selws: %d\n", c->ws)
wm_client_hide(c);
}
}
wm_smon->selws = ws;
wm_client_set_atom(c, "_NET_CURRENT_DESKTOP", (unsigned char*) &(ws),
XA_CARDINAL, 1);
// XChangeProperty(wm_display, wm_root.wn,
// XInternAtom(wm_display, "_NET_CURRENT_DESKTOP", False), XA_CARDINAL,
// 32, PropModeReplace, (unsigned char *) &(xasws), 1);
for (c = wm_smon->clients; c; c = c->next) {
if (c->ws == wm_smon->selws) {
wscc++;
wm_client_show(c);
}
}
DEBUG_PRINT("wm_switch_ws focused ws client count: %d\n", wscc)
}
// TODO maybe x,y is not 0, because of bar
@ -576,17 +847,22 @@ void wm_mstack(Monitor *m)
XWindowChanges ch;
int cc_sws = 0;
int n = 0;
int sx, sy;
XTextProperty xt;
unsigned int value_mask = CWX | CWY | CWWidth | CWHeight;
// TODO: function
for (c = m->clients; c; c = c->next) {
if (c->ws == m->selws) {
if (c->ws == m->selws && c != &wm_root) {
cc_sws++;
}
}
DEBUG_PRINTV("mstack cc_sws: %d\n", cc_sws)
//DEBUG_PRINT("mstack cc_sws: %d\n", cc_sws)
// dynamic
if (cc_sws == 1) {
for (c = m->clients; c; c = c->next) {
@ -598,12 +874,12 @@ void wm_mstack(Monitor *m)
c->w = wm_smon->info.width-wm_border_width*2;
c->h = wm_smon->info.height-wm_border_width*2;
ch = wm_client_to_xwchanges(*c);
// DEBUG_PRINTV("mstack client: 0x%x\n", c);
// DEBUG_PRINT("mstack client: 0x%x\n", c);
// XGetWMName(wm_display, c->wn, &xt);
// DEBUG_PRINTV("mstack window id: %d\n", c->wn);
// DEBUG_PRINTV("mstack wm_name: %s\n", xt.value)
// DEBUG_PRINTV("mstack client width: %d\n", ch.width)
// DEBUG_PRINTV("mstack client height: %d\n", ch.height)
// DEBUG_PRINT("mstack window id: %d\n", c->wn);
// 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->wn, value_mask, &ch);
wm_client_show(c);
@ -612,7 +888,7 @@ void wm_mstack(Monitor *m)
else {
// TODO
for (c = m->clients; c; c = c->next) {
if (c->ws == m->selws) {
if (c->ws == m->selws && c != &wm_root) {
if (n == 0) {
c->x = 0;
c->y = 0;
@ -630,13 +906,13 @@ void wm_mstack(Monitor *m)
// TODO store wm name when client is created
XGetWMName(wm_display, c->wn, &xt);
// DEBUG_PRINTV("mstack client: 0x%x\n", c);
// DEBUG_PRINTV("mstack window id: %d\n", c->wn);
// DEBUG_PRINTV("mstack wm_name: %s\n", xt.value)
// DEBUG_PRINTV("mstack client x: %d ", ch.x)
// DEBUG_PRINTV("y: %d\n", ch.y)
// DEBUG_PRINTV("mstack client width: %d ", ch.width)
// DEBUG_PRINTV("height: %d\n", ch.height)
// DEBUG_PRINT("mstack client: 0x%x\n", c);
// DEBUG_PRINT("mstack window id: %d\n", c->wn);
// 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->wn, value_mask, &ch);
wm_client_show(c);
@ -686,46 +962,88 @@ void wm_mainloop()
// XSelectInput(wm_display, w, KeyPressMask | KeyReleaseMask);
// XMapWindow(wm_display, w);
while (1) {
XEvent e;
XNextEvent(wm_display, &e);
switch (e.type) {
case CreateNotify:
wm_create_handler(e.xcreatewindow);
break;
case DestroyNotify:
wm_destroy_handler(e.xdestroywindow);
break;
case ReparentNotify:
wm_reparent_handler(e.xreparent);
break;
case ConfigureRequest:
wm_configure_handler(e.xconfigurerequest);
break;
case KeyRelease:
wm_keyrelease_handler(e.xkey);
break;
case KeyPress:
wm_keypress_handler(e.xkey);
break;
case FocusIn:
// TODO: draw focus window elsewhere
DEBUG_PRINTV("%d window focus in\n", e.xfocus.window);
// wm_monitor_clients_border(wm_client_find(e.xfocus.window)->m);
// wm_client_border_focused(wm_client_find(e.xfocus.window));
break;
case FocusOut:
DEBUG_PRINTV("%d window focus out\n", e.xfocus.window);
//wm_client_border(wm_client_find(e.xfocus.window));
break;
case EnterNotify:
DEBUG_PRINTV("pointer entered window: %d\n", e.xcrossing.window);
wm_client_focus(wm_client_find(e.xcrossing.window));
break;
case LeaveNotify:
DEBUG_PRINTV("pointer left window: %d\n", e.xcrossing.window);
break;
struct sockaddr s;
socklen_t t = 108;
getsockname(wm_socket_fd, &s, &t);
int accepted_socket;
char *buffer;
int len;
while (1) {
// 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 (XPending(wm_display)) {
XEvent e;
XNextEvent(wm_display, &e);
switch (e.type) {
case CreateNotify:
wm_create_handler(e.xcreatewindow);
break;
case DestroyNotify:
wm_destroy_handler(e.xdestroywindow);
break;
case ReparentNotify:
wm_reparent_handler(e.xreparent);
break;
case ConfigureRequest:
wm_configure_handler(e.xconfigurerequest);
break;
case KeyRelease:
wm_keyrelease_handler(e.xkey);
break;
case KeyPress:
wm_keypress_handler(e.xkey);
break;
case FocusIn:
// TODO: draw focus window elsewhere
// DEBUG_PRINT("%d window focus in\n", e.xfocus.window);
// wm_monitor_clients_border(wm_client_find(e.xfocus.window)->m);
// wm_client_border_focused(wm_client_find(e.xfocus.window));
break;
case FocusOut:
// DEBUG_PRINT("%d window focus out\n", e.xfocus.window);
//wm_client_border(wm_client_find(e.xfocus.window));
break;
case EnterNotify:
DEBUG_PRINT("pointer entered window: %d\n",
e.xcrossing.window);
wm_client_focus(wm_client_find(e.xcrossing.window));
break;
case LeaveNotify:
DEBUG_PRINT("pointer left window: %d\n",
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)
XMapWindow(wm_display, e.xmaprequest.window);
break;
}
}
}
}
@ -736,15 +1054,16 @@ void wm_init()
XSetWindowAttributes xa;
DEBUG_PRINT("wm_init\n")
DEBUG_PRINTV("pid: %ld\n", ((long)getpid()))
DEBUG_PRINT("pid: %ld\n", ((long)getpid()))
Display *d = wm_connect_display();
wm_display = d;
wm_monitors_open_all(d);
// TODO: only testing
wm_socket_init();
// wm_socket_init();
wm_root.wn = XRootWindow(wm_display, DefaultScreen(wm_display));
wm_root.ws = 999;
XSync(d, false);
@ -755,7 +1074,7 @@ void wm_init()
XChangeWindowAttributes(wm_display, wm_root.wn, CWEventMask, &xa);
XSelectInput(wm_display, wm_root.wn, xa.event_mask);
DEBUG_PRINTV("root window id: %d\n", (wm_root.wn))
DEBUG_PRINT("root window id: %d\n", (wm_root.wn))
// TODO: CONFIG KEYS!
XUngrabKey(wm_display, AnyKey, AnyModifier, wm_root.wn);
@ -780,12 +1099,38 @@ void wm_init()
XGrabKey(wm_display, XKeysymToKeycode(wm_display, XK_L), Mod4Mask,
wm_root.wn, True, GrabModeAsync, GrabModeAsync);
// workspace switcher keys
int wskeys[] = {XK_1, XK_2, XK_3, XK_4, XK_5, XK_6, XK_7, XK_8, XK_9};
for (int i = 0; i < 9; i++) {
XGrabKey(wm_display, XKeysymToKeycode(wm_display, wskeys[i]), Mod4Mask,
wm_root.wn, True, GrabModeAsync, GrabModeAsync);
}
//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.wn,
XInternAtom(wm_display, "_NET_NUMBER_OF_DESKTOPS", False), XA_CARDINAL,
32, PropModeReplace, (unsigned char *) &wm_smon->wscount, 1);
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),
};
XChangeProperty(wm_display, wm_root.wn,
XInternAtom(wm_display, "_NET_SUPPORTED", False), XA_ATOM, 32,
PropModeReplace, (unsigned char *) &wm_supp, sizeof(wm_supp)/sizeof(long));
//XSetErrorHandler(&wm_xerror_handler);
@ -796,12 +1141,12 @@ void wm_init()
}
//TODO: avoid global vars
void wm_socket_init()
struct sockaddr wm_socket_init()
{
struct sockaddr_un sock_addr;
int ret;
ret = wm_socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
ret = wm_socket_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (ret == -1) {
fprintf(stderr, "wm: could not create socket. Exiting.\n");
@ -833,7 +1178,7 @@ void wm_socket_cleanup()
struct sockaddr s;
socklen_t t = 108;
getsockname(wm_socket_fd, &s, &t);
DEBUG_PRINTV("socket cleanup path: %s\n", s.sa_data)
DEBUG_PRINT("socket cleanup path: %s\n", s.sa_data)
remove(s.sa_data);
}
@ -842,3 +1187,41 @@ void wm_sigint_handler(int signum)
wm_socket_cleanup();
exit(1);
}
// TODO: maybe move some parts to wmc
void wm_settings_message_parse(char *c, int len)
{
if (len <= 0)
return;
int op; /* 0 = set, 1 = get */
int i = 0;
char ret[50];
char *token;
char tokens[20][50];
char *settings[] = {"border-width", "border-color", "focused-border-color",
"msfact"};
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]);
}
}
}
}

38
wm.h
View File

@ -10,30 +10,28 @@
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/un.h>
// TODO: bar, config command line utility(sockets), avoid global vars
// 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: do not lay out non-regular windows
// TODO: important: code cleanliness, ewmh!!
// TODO: config command line utility(sockets), avoid global vars
#define RETURN_IF_NULL(c) \
if (c == NULL) \
{ \
fprintf(stderr, "wm: client was null in function %s\n", __func__); \
fprintf(stderr, "wm: pointer was null in function %s\n", __func__); \
return; \
}
#define DEBUG
//#define LAST_WINDOW(c, smon) whie {}
//#define FIND_WINDOW(c, smon, window) \
for (c = smon->clients; c; c = c->next) \
{ \
if (c->wn == window) { \
break; \
} \
}
#ifdef DEBUG
#define DEBUG_PRINT(s) printf(s);
#define DEBUG_PRINTV(s, var) printf(s, var);
#endif
#define DEBUG 1
#define DEBUG_PRINT(fmt, ...) \
do { if (DEBUG) fprintf(stderr, fmt, ##__VA_ARGS__); } while (0);
typedef struct Client Client;
typedef struct Monitor Monitor;
@ -76,6 +74,7 @@ static Monitor *wm_smon;
static int wm_mc;
static bool wm_other_wm;
static Client wm_root;
static Window wm_dock;
static void (*wm_active_layout)(Monitor*);
static float wm_ms_p;
static unsigned int wm_border_col = 0x222222;
@ -104,6 +103,11 @@ void wm_layout(Monitor *m);
Client *wm_get_last_client(Monitor m);
void wm_client_set_atom(Client *c, const char *name, const unsigned char *data,
Atom type, int nelements);
Atom wm_client_get_atom(Client *c, const char *name, unsigned char **atom_ret,
unsigned long *nitems_ret);
void wm_create_client(Window w);
void wm_client_hide(Client *c);
void wm_client_show(Client *c);
@ -116,6 +120,7 @@ void wm_client_border(Client *c);
void wm_client_border_focused(Client *c);
void wm_monitor_clients_border(Monitor *m);
bool wm_client_is_focused(Client *c);
bool wm_client_is_dock(Client *c);
XWindowChanges wm_client_to_xwchanges(Client c);
Client* wm_client_find(Window w);
@ -124,7 +129,8 @@ void wm_switch_ws(int ws);
void wm_mainloop();
void wm_init();
void wm_socket_init();
struct sockaddr wm_socket_init();
void wm_socket_cleanup();
void wm_settings_message_parse(char *c, int len);
void wm_sigint_handler(int signum);