wm/wm.c

1352 lines
37 KiB
C

#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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
Monitor wm_monitor_open(Display *d, XineramaScreenInfo info)
{
Monitor m = {
.display = d,
.info = info
};
return m;
}
void wm_monitors_open_all(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].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;
wm_monitors[i].clients = &wm_root;
}
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;
}
static int wm_xerror_handler(Display *displau, 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 *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;
// 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);
}
// TODO
void wm_destroy_handler(XDestroyWindowEvent e)
{
DEBUG_PRINT("DestroyNotify event\n");
Client *c;
// TODO: make function for linked list management
for (c = wm_smon->clients; c; c = c->next) {
if (e.window == c->window) {
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;
}
}
free(c);
}
}
}
void wm_reparent_handler(XReparentEvent e)
{
DEBUG_PRINT("ReparentNotify event\n");
}
// TODO
void wm_configure_handler(XConfigureRequestEvent e)
{
DEBUG_PRINT("ConfigureRequest event\n");
Client *c;
unsigned char *prop_ret;
unsigned long nitems_ret;
Atom type;
bool is_normal = false;
XTextProperty xtp;
c = wm_client_create(e.window);
XGetWMName(wm_display, e.window, &xtp);
DEBUG_PRINT("%s: created window %d name: %s\n", __func__, (int)e.window, xtp.value);
//XFree(xtp);
char *name = "_NET_WM_WINDOW_TYPE";
type = wm_client_get_atom(c, "_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;
} 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, returning\n",
(int)e.window)
wm_client_free(c);
return;
}
wm_layout(c->m);
}
void wm_keyrelease_handler(XKeyReleasedEvent e)
{
DEBUG_PRINT("KeyReleased event, code: %d\n", e.keycode)
}
void wm_keypress_handler(XKeyPressedEvent e)
{
DEBUG_PRINT("KeyPressed event, code: %d\n", e.keycode)
Client *c;
Keybind k;
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)(&k.args);
}
}
// switch (e.state) {
// case (Mod4Mask):
// DEBUG_PRINT("mod4\n")
// // cant use switch
// // 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)
// }
// break;
// }
}
void wm_maprequest_handler(XMapRequestEvent e)
{
DEBUG_PRINT("MapRequest event\n")
Client *c;
c = wm_client_find(e.window);
if (c) {
DEBUG_PRINT("%s: client found, mapping window\n", __func__)
XMapWindow(wm_display, e.window);
c->hidden = false;
return;
}
if (wm_window_is_dock(e.window))
{
DEBUG_PRINT("%s: window is dock, mapping window\n", __func__)
XMapWindow(wm_display, e.window);
return;
}
DEBUG_PRINT("%s: client not found, creating client\n", __func__)
c = wm_client_create(e.window);
RETURN_IF_NULL(c)
//XMapWindow(wm_display, c->window);
wm_layout(c->m);
}
void wm_motion_handler(XMotionEvent e)
{
Client *c;
c = wm_client_find(e.window);
RETURN_IF_NULL(c)
if(!wm_client_is_focused(c) && wm_cfg_focus_on_motion)
wm_client_focus(c);
}
// 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(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(Window w)
{
Client *c = NULL;
Client *t;
XTextProperty xtp;
int strln;
XGetWMName(wm_display, w, &xtp);
c = malloc(sizeof(Client));
c->window = w;
c->m = wm_smon;
c->ws = 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", __func__, strln)
c->name = malloc(strln+1);
strcpy(c->name, (char*)xtp.value);
}
if (wm_smon->clients == NULL) {
wm_smon->clients = c;
c->prev = NULL;
} else {
t = wm_get_last_client(*c->m);
//c = &wm_root;
t->next = c;
c->prev = t;
c->next = NULL;
}
XSelectInput(wm_display, c->window, FocusChangeMask | EnterWindowMask |
PointerMotionMask);
wm_client_set_atom(c, "_NET_WM_DESKTOP", (unsigned char*)&c->ws, XA_CARDINAL, 1);
return c;
}
void wm_client_hide(Client *c)
{
RETURN_IF_NULL(c);
if (c->hidden)
return;
c->hidden = true;
XUnmapWindow(wm_display, c->window);
}
void wm_client_show(Client *c)
{
RETURN_IF_NULL(c);
if (!c->hidden)
return;
c->hidden = false;
XMapWindow(wm_display, c->window);
}
void wm_client_focus(Client *c)
{
RETURN_IF_NULL(c);
XSetInputFocus(wm_display, c->window, RevertToPointerRoot, CurrentTime);
wm_client_set_atom(c, "_NET_ACTIVE_WINDOW", (unsigned char*) &c->window,
XA_WINDOW, 1);
// XChangeProperty(wm_display, wm_root.window,
// XInternAtom(wm_display, "_NET_ACTIVE_WINDOW", False),
// 33, 32, PropModeReplace, (unsigned char *) &c->window, 1);
wm_monitor_clients_border(c->m);
}
void wm_client_focus_dir(Client *c, int dir)
{
RETURN_IF_NULL(c);
switch (dir) {
case UP:
wm_client_focus(wm_client_get_dir_rel_c(c, dir));
break;
case DOWN:
wm_client_focus(wm_client_get_dir_rel_c(c, dir));
break;
case LEFT:
wm_client_focus(wm_client_get_dir_rel_c(c, dir));
break;
case RIGHT:
wm_client_focus(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(Client *c)
{
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(Client *c)
{
RETURN_IF_NULL(c);
Monitor *m;
m = c->m;
XDestroyWindow(wm_display, c->window);
wm_client_free(c);
wm_layout(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->window, 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->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 -1;
}
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\n", __func__)
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;
}
x+=dx*5;
y+=dy*5;
do {
x+=dx*2;
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_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()
{
Client *c;
for (c = wm_smon->clients; c; c = c->next) {
if (wm_client_is_focused(c))
return c;
}
return NULL;
}
void wm_client_border(Client *c)
{
RETURN_IF_NULL(c);
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(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(Monitor *m)
{
//DEBUG_PRINT("%s\n", __func__)
RETURN_IF_NULL(m);
Client *c;
for (c = wm_smon->clients; c; c = c->next) {
//DEBUG_PRINT("monitor border c: 0x%x\n", c)
if (c->window != wm_root.window)
wm_client_border(c);
}
}
bool wm_client_is_focused(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, wm_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;
}
bool wm_window_is_dock(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 (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 || 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(&wm_root, "_NET_CURRENT_DESKTOP", (unsigned char*) &(ws),
XA_CARDINAL, 1);
// XChangeProperty(wm_display, wm_root.window,
// XInternAtom(wm_display, "_NET_CURRENT_DESKTOP", False), XA_CARDINAL,
// 32, PropModeReplace, (unsigned char *) &(xasws), 1);
for (c = wm_smon->clients; c; c = c->next) {
if (c->ws == wm_smon->selws) {
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
void wm_mstack(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;
XTextProperty xt;
XEvent e;
unsigned int value_mask = CWX | CWY | CWWidth | CWHeight;
// TODO: function
// FIXME: &wm_root probably wrong
if (wm_dock != -1) {
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 == m->selws && c != &wm_root && !c->is_floating) {
cc_sws++;
}
}
//DEBUG_PRINT("mstack cc_sws: %d\n", cc_sws)
// dynamic
if (cc_sws == 1) {
for (c = m->clients; c; c = c->next) {
if (c->ws == m->selws && c != &wm_root)
break;
}
c->x = 0;
c->y = sy;
c->w = wm_smon->info.width-wm_cfg_border_width*2;
c->h = (wm_smon->info.height-wm_cfg_border_width*2)-sy;
ch = wm_client_to_xwchanges(*c);
// DEBUG_PRINT("mstack client: 0x%x\n", c);
// XGetWMName(wm_display, c->window, &xt);
// DEBUG_PRINT("mstack window id: %d\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(c);
//wm_client_focus(c);
}
else {
// FIXME not working with dock
for (c = m->clients; c; c = c->next) {
if (c->ws == m->selws && c != &wm_root && !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
XGetWMName(wm_display, c->window, &xt);
// DEBUG_PRINT("mstack client: 0x%x\n", c);
// DEBUG_PRINT("mstack window id: %d\n", c->window);
// DEBUG_PRINT("mstack 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(c);
DEBUG_PRINT("%s %d. client next: 0x%x\n", __func__, n, c->next)
if (c->next == NULL)
{
wm_client_focus(c);
}
}
}
}
XSync(wm_display, False);
while(XCheckMaskEvent(wm_display, EnterWindowMask, &e));
wm_monitor_clients_border(m);
}
void wm_set_layout(void(*f)(Monitor*))
{
wm_active_layout = f;
}
// TODO: layout floating window, selected layout func p
void wm_layout(Monitor *m)
{
RETURN_IF_NULL(m);
wm_mstack(m);
}
Client *wm_get_last_client(Monitor m)
{
Client *c = m.clients;
if (c == NULL)
return NULL;
while (c->next != NULL)
c = c->next;
return c;
}
void wm_mainloop()
{
// int s = DefaultScreen(wm_display);
// Window w = XCreateSimpleWindow(wm_display, wm_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;
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 MotionNotify:
wm_motion_handler(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_client_find(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(e.xmaprequest);
break;
}
}
}
}
void wm_init()
{
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(d);
// TODO: only testing
// wm_socket_init();
wm_root.window = XRootWindow(wm_display, DefaultScreen(wm_display));
wm_root.ws = 999;
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", (wm_root.window))
// XUngrabKey(wm_display, AnyKey, AnyModifier, wm_root.window);
// TODO: read config
wm_keybinds_init_def();
wm_grab_keys();
//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_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_mainloop();
}
void wm_grab_keys()
{
RETURN_IF_NULL(wm_cfg_keybinds)
Keybind k;
for (int i = 0; i < wm_cfg_kb_count; i++) {
k = wm_cfg_keybinds[i];
XGrabKey(wm_display, XKeysymToKeycode(wm_display, k.keysym), k.mask,
wm_root.window, True, GrabModeAsync, GrabModeAsync);
}
}
void wm_kb_spawn(Arg *args)
{
RETURN_IF_NULL(args)
RETURN_IF_NULL(args->sl)
DEBUG_PRINT("args sl 1: %d\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_kill(Arg *args)
{
Client *c;
c = wm_client_get_focused();
RETURN_IF_NULL(c)
wm_client_kill(c);
}
void wm_kb_switch_ws(Arg *args)
{
RETURN_IF_NULL(args)
wm_switch_ws(args->i);
}
void wm_kb_focus_dir(Arg *args)
{
RETURN_IF_NULL(args)
RETURN_IF_NULL(args->c)
wm_client_focus_dir(args->c, args->i);
}
void wm_keybinds_init_def()
{
char *st[] = {"st", NULL};
char **sth = malloc(sizeof(st));
memcpy(sth, st, sizeof(st));
int size;
Keybind k[] = {
(Keybind) {Mod4Mask, XK_Return, *wm_kb_spawn,
(Arg) {.sl = sth, .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}},
};
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()
{
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()
{
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(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", "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]);
}
}
}
}