add lot of stuff

This commit is contained in:
Akos Horvath 2022-07-28 23:38:57 +02:00
parent aad5ba497f
commit 452d103a08
2 changed files with 448 additions and 46 deletions

437
wm.c
View File

@ -1,6 +1,7 @@
#include "wm.h"
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xinerama.h>
Monitor wm_monitor_open(Display *d, XineramaScreenInfo info)
@ -15,6 +16,7 @@ Monitor wm_monitor_open(Display *d, XineramaScreenInfo info)
void wm_monitors_open_all(Display *d)
{
DEBUG_PRINT("wm_monitors_open_all\n")
int count;
XineramaScreenInfo *xsi = XineramaQueryScreens(d, &count);
@ -29,16 +31,17 @@ void wm_monitors_open_all(Display *d)
*(wm_monitors + i) = wm_monitor_open(d, *(xsi + i));
//cfg
wm_monitors[i].wscount = 9;
wm_monitors[i].wss = malloc(wm_smon->wscount*sizeof(int));
wm_monitors[i].wss = malloc(wm_monitors[i].wscount*sizeof(int));
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 = NULL;
wm_monitors[i].clients = &wm_root;
}
wm_smon = &wm_monitors[0];
wm_smon->clients = &wm_root;
DEBUG_PRINTV("smon width: %d\n", wm_smon->info.width)
}
Display* wm_connect_display()
@ -55,6 +58,14 @@ Display* wm_connect_display()
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;
}
@ -64,13 +75,13 @@ static int wm_xerror_handler(Display *displau, XErrorEvent *e)
static int wm_other_wm_handler(Display *displau, XErrorEvent *e)
{
printf("wm: Error occured. %d\n", e->error_code);
return 0;
}
void wm_create_handler(XCreateWindowEvent e)
{
DEBUG_PRINT("CreateNotify event\n")
Client *nc = malloc(sizeof(Client));
Client *c;
@ -83,17 +94,21 @@ void wm_create_handler(XCreateWindowEvent e)
wm_smon->clients = nc;
nc->prev = NULL;
} else {
LAST_WINDOW(c, wm_smon)
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);
wm_mstack(nc->m);
}
// TODO
void wm_destroy_handler(XDestroyWindowEvent e)
{
DEBUG_PRINT("DestroyNotify event\n")
Client *c;
// TODO: make function for linked list management
@ -121,50 +136,92 @@ void wm_destroy_handler(XDestroyWindowEvent e)
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;
XWindowChanges ch;
int cc_sws = 0;
for (c = wm_smon->clients; c; c = c->next) {
if (c->ws == wm_smon->selws) {
cc_sws++;
}
}
// for (c = wm_smon->clients; c; c = c->next) {
// if (c->ws == wm_smon->selws) {
// cc_sws++;
// }
// }
FIND_WINDOW(c, wm_smon, e.window)
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;
// // 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);
// XConfigureWindow(wm_display, c->wn, e.value_mask, &ch);
wm_layout(c->m);
}
void wm_map_handler(XMapRequestEvent e)
{
DEBUG_PRINT("MapRequest event\n")
Client *c;
XMapWindow(wm_display, e.window);
FIND_WINDOW(c, wm_smon, e.window)
c = wm_client_find(e.window);
c->hidden = false;
}
void wm_keyrelease_handler(XKeyReleasedEvent e)
{
DEBUG_PRINTV("KeyReleased event, code: %d\n", e.keycode)
}
void wm_keypress_handler(XKeyPressedEvent e)
{
DEBUG_PRINTV("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);
switch (e.state) {
case (Mod4Mask):
DEBUG_PRINT("mod4\n")
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(wm_display, XK_Return)) {
if (fork() == 0) {
if (wm_display)
close(ConnectionNumber(wm_display));
setsid();
execvp("st", NULL);
exit(0);
}
}
break;
}
}
// TODO
XWindowChanges wm_client_to_xwchanges(Client c)
{
@ -174,12 +231,24 @@ XWindowChanges wm_client_to_xwchanges(Client c)
ch.y = c.y;
ch.width = c.w;
ch.height = c.h;
ch.border_width = 0;
ch.stack_mode = Above;
// 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->wn == w)
return c;
}
return NULL;
}
void wm_create_client(Window w)
{
@ -187,6 +256,8 @@ void wm_create_client(Window w)
void wm_client_hide(Client *c)
{
RETURN_IF_NULL(c);
if (c->hidden)
return;
@ -196,6 +267,7 @@ void wm_client_hide(Client *c)
void wm_client_show(Client *c)
{
RETURN_IF_NULL(c);
if (!c->hidden)
return;
@ -204,6 +276,144 @@ void wm_client_show(Client *c)
XMapWindow(wm_display, c->wn);
}
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_monitor_clients_border(c->m);
}
void wm_client_kill(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;
}
}
XDestroyWindow(wm_display, c->wn);
free(c);
}
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);
// if (c->has_border)
// return;
// TODO: color config
XVisualInfo vinfo;
XColor color;
XColor fcolor;
Colormap cmap;
char *colorstr = "#555555";
char *focuscolorstr = "#FF0000";
XMatchVisualInfo(wm_display, DefaultScreen(wm_display), 32,
TrueColor, &vinfo);
cmap = XCreateColormap(wm_display, c->wn, vinfo.visual, AllocNone);
XParseColor(wm_display, cmap, focuscolorstr, &fcolor);
XParseColor(wm_display, cmap, colorstr, &color);
XAllocColor(wm_display, cmap, &fcolor);
XAllocColor(wm_display, cmap, &color);
// DEBUG_PRINTV("wm_client_border c: 0x%x\n", c)
if (c) {
// DEBUG_PRINTV("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);
if (wm_client_is_focused(c)) {
DEBUG_PRINT("wm_client_border focused true\n")
XSetWindowBorder(wm_display, c->wn, fcolor.pixel);
}
else
XSetWindowBorder(wm_display, c->wn, color.pixel);
}
// TODO maybe merge two border functios
void wm_client_border_focused(Client *c)
{
RETURN_IF_NULL(c);
// if (!wm_client_is_focused(c))
// return;
XVisualInfo vinfo;
XColor fcolor;
Colormap cmap;
char *focuscolorstr = "#FF0000";
XMatchVisualInfo(wm_display, DefaultScreen(wm_display), 32,
TrueColor, &vinfo);
cmap = XCreateColormap(wm_display, c->wn, vinfo.visual, AllocNone);
XParseColor(wm_display, cmap, focuscolorstr, &fcolor);
XAllocColor(wm_display, cmap, &fcolor);
XSetWindowBorderWidth(wm_display, c->wn, wm_border_width);
DEBUG_PRINTV("drawing border for focused wn: %d\n", c->wn);
XSetWindowBorder(wm_display, c->wn, fcolor.pixel);
}
void wm_monitor_clients_border(Monitor *m)
{
RETURN_IF_NULL(m);
Client *c;
for (c = m->clients; c; c = c->next) {
if (c->wn != wm_root.wn)
wm_client_border(c);
}
}
bool wm_client_is_focused(Client *c)
{
if (c == NULL)
return false;
Client *c1;
Window w;
int r;
XGetInputFocus(wm_display, &w, &r);
if (w == c->w)
return true;
return false;
}
void wm_switch_ws(int ws)
{
if (ws > wm_smon->wscount)
@ -226,8 +436,120 @@ void wm_switch_ws(int ws)
}
}
// TODO maybe x,y is not 0, because of bar
void wm_mstack(Monitor *m)
{
RETURN_IF_NULL(m);
Client *c;
XWindowChanges ch;
int cc_sws = 0;
int n = 0;
XTextProperty xt;
unsigned int value_mask = CWX | CWY | CWWidth | CWHeight;
for (c = m->clients; c; c = c->next) {
if (c->ws == m->selws) {
cc_sws++;
}
}
DEBUG_PRINTV("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)
break;
}
c->x = 0;
c->y = 0;
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);
// 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)
XConfigureWindow(wm_display, c->wn, value_mask, &ch);
wm_client_show(c);
wm_client_focus(c);
}
else {
// TODO
for (c = m->clients; c; c = c->next) {
if (c->ws == m->selws) {
if (n == 0) {
c->x = 0;
c->y = 0;
c->w = m->info.width*wm_ms_p-wm_border_width*2;
c->h = m->info.height-wm_border_width*2;
} else {
c->x = (m->info.width*wm_ms_p)+1;
c->y = (n-1)*m->info.height/(cc_sws-1);
c->w = m->info.width - m->info.width*wm_ms_p;
c->h = m->info.height/(cc_sws-1);
}
n++;
ch = wm_client_to_xwchanges(*c);
// 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)
XConfigureWindow(wm_display, c->wn, value_mask, &ch);
wm_client_show(c);
wm_client_focus(c);
}
}
}
}
void wm_set_layout(void(*f)(Monitor*))
{
wm_active_layout = f;
}
void wm_layout(Monitor *m)
{
RETURN_IF_NULL(m);
(*wm_active_layout)(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.wn, 10, 10, 300, 300,
// 2, WhitePixel(wm_display, s), BlackPixel(wm_display, s));
// XSelectInput(wm_display, w, KeyPressMask | KeyReleaseMask);
// XMapWindow(wm_display, w);
while (1) {
XEvent e;
XNextEvent(wm_display, &e);
@ -235,42 +557,81 @@ void wm_mainloop()
switch (e.type) {
case CreateNotify:
wm_create_handler(e.xcreatewindow);
break;
break;
case DestroyNotify:
wm_destroy_handler(e.xdestroywindow);
break;
break;
case ReparentNotify:
wm_reparent_handler(e.xreparent);
break;
break;
case ConfigureRequest:
wm_configure_handler(e.xconfigurerequest);
break;
break;
case KeyRelease:
wm_keyrelease_handler(e.xkey);
break;
case KeyPress:
wm_keypress_handler(e.xkey);
break;
case FocusIn:
DEBUG_PRINTV("%d window focus in\n", e.xfocus.window);
wm_client_border_focused(wm_client_find(e.xfocus.window));
break;
case FocusOut:
DEBUG_PRINTV("%d window focus out\n", e.xfocus.window);
break;
}
}
}
void wm_init()
{
XSetWindowAttributes xa;
DEBUG_PRINT("wm_init\n")
DEBUG_PRINTV("pid: %ld\n", ((long)getpid()))
Display *d = wm_connect_display();
wm_display = d;
wm_monitors_open_all(d);
wm_root.wn = DefaultRootWindow(d);
wm_root.wn = XRootWindow(wm_display, DefaultScreen(wm_display));
wm_last_client = &wm_root;
XSetErrorHandler(&wm_other_wm_handler);
XSelectInput(d, wm_root.wn, SubstructureRedirectMask |
SubstructureNotifyMask);
XSync(d, false);
xa.event_mask = StructureNotifyMask | StructureNotifyMask |
SubstructureNotifyMask | SubstructureRedirectMask |
ButtonPressMask | PropertyChangeMask;
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))
// TODO: CONFIG KEYS
XUngrabKey(wm_display, AnyKey, AnyModifier, wm_root.wn);
unsigned int modifiers[] = {0, LockMask, 0 | LockMask};
XGrabKey(wm_display, XK_C, Mod4Mask | LockMask, wm_root.wn, True,
GrabModeAsync, GrabModeAsync);
XGrabKey(wm_display, XKeysymToKeycode(wm_display, XK_C), Mod4Mask,
wm_root.wn, True, GrabModeAsync, GrabModeAsync);
XGrabKey(wm_display, XK_C, Mod4Mask | 0, wm_root.wn, True,
GrabModeAsync, GrabModeAsync);
XGrabKey(wm_display, XKeysymToKeycode(wm_display, XK_Return), 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);
}
XSetErrorHandler(&wm_xerror_handler);
//XSetErrorHandler(&wm_xerror_handler);
// TODO: config
wm_ms_p = 0.5;
wm_mainloop();
}

57
wm.h
View File

@ -2,16 +2,33 @@
#include <stdio.h>
#include <stdbool.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
#include <X11/Xproto.h>
#include <unistd.h>
#include <X11/extensions/Xinerama.h>
#define LAST_WINDOW(c, smon) for (c = smon->clients; c; c = c->next) {}
#define FIND_WINDOW(c, smon, window) \
#define RETURN_IF_NULL(c) \
if (c == NULL) \
{ \
fprintf(stderr, "wm: client 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; \
} \
}
{ \
if (c->wn == window) { \
break; \
} \
}
#ifdef DEBUG
#define DEBUG_PRINT(s) printf(s);
#define DEBUG_PRINTV(s, var) printf(s, var);
#endif
typedef struct Client Client;
typedef struct Monitor Monitor;
@ -37,6 +54,7 @@ struct Client {
Window wn;
Client *prev;
Client *next;
bool has_border;
// linked list is not sufficent for manual tiling.
// will propably need a tree.
};
@ -48,6 +66,11 @@ static int wm_mc;
static bool wm_other_wm;
static Client wm_root;
static Client *wm_last_client;
static void (*wm_active_layout)(Monitor*);
static float wm_ms_p;
static unsigned int wm_border_col = 0x222222;
static unsigned int wm_focused_border_col = 0x444444;
static unsigned char wm_border_width = 2;
static int wm_xerror_handler(Display *display, XErrorEvent *e);
static int wm_other_wm_handler(Display *display, XErrorEvent *e);
@ -56,14 +79,32 @@ void wm_destroy_handler(XDestroyWindowEvent e);
void wm_reparent_handler(XReparentEvent e);
void wm_configure_handler(XConfigureRequestEvent e);
void wm_map_handler(XMapRequestEvent e);
void wm_keyrelease_handler(XKeyReleasedEvent e);
void wm_keypress_handler(XKeyPressedEvent e);
Monitor wm_monitor_open(Display *d, XineramaScreenInfo info);
void wm_monitors_open_all(Display *d);
Display* wm_connect_display();
XWindowChanges wm_client_to_xwchanges(Client c);
void wm_mstack(Monitor *m);
void wm_set_layout(void(*f)(Monitor*));
void wm_layout(Monitor *m);
Client *wm_get_last_client(Monitor m);
void wm_create_client(Window w);
void wm_client_hide(Client *c);
void wm_client_show(Client *c);
void wm_client_focus(Client *c);
void wm_client_kill(Client *c);
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);
XWindowChanges wm_client_to_xwchanges(Client c);
Client* wm_client_find(Window w);
void wm_switch_ws(int ws);
void wm_mainloop();
void wm_init();