split up single source file

This commit is contained in:
Akos Horvath 2022-09-16 20:39:24 +02:00
parent 30cf32f830
commit 398e15322d
6 changed files with 769 additions and 734 deletions

388
client.c Normal file
View File

@ -0,0 +1,388 @@
#include "client.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(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;
}

27
client.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef CLIENT_H
#define CLIENT_H
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <X11/Xlib.h>
#include "wm.h"
Client* wm_client_create(Window w);
void wm_client_free(Client *c);
void wm_client_kill(Client *c);
void wm_client_hide(Client *c);
void wm_client_show(Client *c);
void wm_client_focus(Client *c);
void wm_client_focus_dir(Client *c, int dir);
Client* wm_client_get_dir_rel_c(Client *c, int dir);
Client* wm_client_get_focused();
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_window_is_dock(Window w);
XWindowChanges wm_client_to_xwchanges(Client c);
Client* wm_client_find(Window w);
#endif

321
handler.c Normal file
View File

@ -0,0 +1,321 @@
#include "handler.h"
#include "client.h"
#include "wm.h"
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);
}

26
handler.h Normal file
View File

@ -0,0 +1,26 @@
#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 <X11/extensions/Xinerama.h>
#include <unistd.h>
static int wm_xerror_handler(Display *display, XErrorEvent *e);
static int wm_other_wm_handler(Display *display, XErrorEvent *e);
void wm_create_handler(XCreateWindowEvent e);
void wm_destroy_handler(XDestroyWindowEvent e);
void wm_reparent_handler(XReparentEvent e);
void wm_configure_handler(XConfigureRequestEvent e);
void wm_keyrelease_handler(XKeyReleasedEvent e);
void wm_keypress_handler(XKeyPressedEvent e);
void wm_maprequest_handler(XMapRequestEvent e);
void wm_motion_handler(XMotionEvent e);
#endif

709
wm.c
View File

@ -1,4 +1,3 @@
#include "wm.h"
#include <X11/X.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
@ -11,6 +10,9 @@
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include "wm.h"
#include "handler.h"
#include "client.h"
Monitor wm_monitor_open(Display *d, XineramaScreenInfo info)
{
@ -66,711 +68,6 @@ Display* wm_connect_display()
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

32
wm.h
View File

@ -1,3 +1,6 @@
#ifndef WM_H
#define WM_H
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
@ -110,17 +113,6 @@ static bool wm_cfg_focus_on_motion = true;
static int wm_socket_fd;
static int wm_xerror_handler(Display *display, XErrorEvent *e);
static int wm_other_wm_handler(Display *display, XErrorEvent *e);
void wm_create_handler(XCreateWindowEvent e);
void wm_destroy_handler(XDestroyWindowEvent e);
void wm_reparent_handler(XReparentEvent e);
void wm_configure_handler(XConfigureRequestEvent e);
void wm_keyrelease_handler(XKeyReleasedEvent e);
void wm_keypress_handler(XKeyPressedEvent e);
void wm_maprequest_handler(XMapRequestEvent e);
void wm_motion_handler(XMotionEvent e);
Monitor wm_monitor_open(Display *d, XineramaScreenInfo info);
void wm_monitors_open_all(Display *d);
Display* wm_connect_display();
@ -136,23 +128,6 @@ void wm_client_set_atom(Client *c, const char *name, const unsigned char *data,
Atom wm_client_get_atom(Client *c, const char *name, unsigned char **atom_ret,
unsigned long *nitems_ret);
Client* wm_client_create(Window w);
void wm_client_free(Client *c);
void wm_client_kill(Client *c);
void wm_client_hide(Client *c);
void wm_client_show(Client *c);
void wm_client_focus(Client *c);
void wm_client_focus_dir(Client *c, int dir);
Client* wm_client_get_dir_rel_c(Client *c, int dir);
Client* wm_client_get_focused();
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_window_is_dock(Window w);
XWindowChanges wm_client_to_xwchanges(Client c);
Client* wm_client_find(Window w);
void wm_spawn(char **str);
void wm_switch_ws(int ws);
@ -172,3 +147,4 @@ void wm_socket_cleanup();
void wm_settings_message_parse(char *c, int len);
void wm_sigint_handler(int signum);
#endif